2000-11-16 Justin Sheehy <justin@iago.org>
[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 However, whereas `gnus-split-methods' specifies file names as targets,
351 this variable specifies group names."
352   :group 'gnus-summary-mail
353   :type '(repeat (choice (list :value (fun) function)
354                          (cons :value ("" "") regexp (repeat string))
355                          (sexp :value nil))))
356
357 (defcustom gnus-unread-mark ?  ;Whitespace
358   "*Mark used for unread articles."
359   :group 'gnus-summary-marks
360   :type 'character)
361
362 (defcustom gnus-ticked-mark ?!
363   "*Mark used for ticked articles."
364   :group 'gnus-summary-marks
365   :type 'character)
366
367 (defcustom gnus-dormant-mark ??
368   "*Mark used for dormant articles."
369   :group 'gnus-summary-marks
370   :type 'character)
371
372 (defcustom gnus-del-mark ?r
373   "*Mark used for del'd articles."
374   :group 'gnus-summary-marks
375   :type 'character)
376
377 (defcustom gnus-read-mark ?R
378   "*Mark used for read articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-expirable-mark ?E
383   "*Mark used for expirable articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-killed-mark ?K
388   "*Mark used for killed articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-souped-mark ?F
393   "*Mark used for killed articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-kill-file-mark ?X
398   "*Mark used for articles killed by kill files."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-low-score-mark ?Y
403   "*Mark used for articles with a low score."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-catchup-mark ?C
408   "*Mark used for articles that are caught up."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-replied-mark ?A
413   "*Mark used for articles that have been replied to."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-cached-mark ?*
418   "*Mark used for articles that are in the cache."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-saved-mark ?S
423   "*Mark used for articles that have been saved to."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-ancient-mark ?O
428   "*Mark used for ancient articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-sparse-mark ?Q
433   "*Mark used for sparsely reffed articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-canceled-mark ?G
438   "*Mark used for canceled articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-duplicate-mark ?M
443   "*Mark used for duplicate articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-undownloaded-mark ?@
448   "*Mark used for articles that weren't downloaded."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-downloadable-mark ?%
453   "*Mark used for articles that are to be downloaded."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-unsendable-mark ?=
458   "*Mark used for articles that won't be sent."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-score-over-mark ?+
463   "*Score mark used for articles with high scores."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-score-below-mark ?-
468   "*Score mark used for articles with low scores."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-empty-thread-mark ?  ;Whitespace
473   "*There is no thread under the article."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-not-empty-thread-mark ?=
478   "*There is a thread under the article."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-view-pseudo-asynchronously nil
483   "*If non-nil, Gnus will view pseudo-articles asynchronously."
484   :group 'gnus-extract-view
485   :type 'boolean)
486
487 (defcustom gnus-auto-expirable-marks
488   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
489         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
490         gnus-souped-mark gnus-duplicate-mark)
491   "*The list of marks converted into expiration if a group is auto-expirable."
492   :group 'gnus-summary
493   :type '(repeat character))
494
495 (defcustom gnus-inhibit-user-auto-expire t
496   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
497   :group 'gnus-summary
498   :type 'boolean)
499
500 (defcustom gnus-view-pseudos nil
501   "*If `automatic', pseudo-articles will be viewed automatically.
502 If `not-confirm', pseudos will be viewed automatically, and the user
503 will not be asked to confirm the command."
504   :group 'gnus-extract-view
505   :type '(choice (const :tag "off" nil)
506                  (const automatic)
507                  (const not-confirm)))
508
509 (defcustom gnus-view-pseudos-separately t
510   "*If non-nil, one pseudo-article will be created for each file to be viewed.
511 If nil, all files that use the same viewing command will be given as a
512 list of parameters to that command."
513   :group 'gnus-extract-view
514   :type 'boolean)
515
516 (defcustom gnus-insert-pseudo-articles t
517   "*If non-nil, insert pseudo-articles when decoding articles."
518   :group 'gnus-extract-view
519   :type 'boolean)
520
521 (defcustom gnus-summary-dummy-line-format
522   "  %(:                          :%) %S\n"
523   "*The format specification for the dummy roots in the summary buffer.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions.
526
527 %S  The subject"
528   :group 'gnus-threading
529   :type 'string)
530
531 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
532   "*The format specification for the summary mode line.
533 It works along the same lines as a normal formatting string,
534 with some simple extensions:
535
536 %G  Group name
537 %p  Unprefixed group name
538 %A  Current article number
539 %z  Current article score
540 %V  Gnus version
541 %U  Number of unread articles in the group
542 %e  Number of unselected articles in the group
543 %Z  A string with unread/unselected article counts
544 %g  Shortish group name
545 %S  Subject of the current article
546 %u  User-defined spec
547 %s  Current score file name
548 %d  Number of dormant articles
549 %r  Number of articles that have been marked as read in this session
550 %E  Number of articles expunged by the score files"
551   :group 'gnus-summary-format
552   :type 'string)
553
554 (defcustom gnus-list-identifiers nil
555   "Regexp that matches list identifiers to be removed from subject.
556 This can also be a list of regexps."
557   :group 'gnus-summary-format
558   :group 'gnus-article-hiding
559   :type '(choice (const :tag "none" nil)
560                  (regexp :value ".*")
561                  (repeat :value (".*") regexp)))
562
563 (defcustom gnus-summary-mark-below 0
564   "*Mark all articles with a score below this variable as read.
565 This variable is local to each summary buffer and usually set by the
566 score file."
567   :group 'gnus-score-default
568   :type 'integer)
569
570 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
571   "*List of functions used for sorting articles in the summary buffer.
572 This variable is only used when not using a threaded display."
573   :group 'gnus-summary-sort
574   :type '(repeat (choice (function-item gnus-article-sort-by-number)
575                          (function-item gnus-article-sort-by-author)
576                          (function-item gnus-article-sort-by-subject)
577                          (function-item gnus-article-sort-by-date)
578                          (function-item gnus-article-sort-by-score)
579                          (function :tag "other"))))
580
581 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
582   "*List of functions used for sorting threads in the summary buffer.
583 By default, threads are sorted by article number.
584
585 Each function takes two threads and return non-nil if the first thread
586 should be sorted before the other.  If you use more than one function,
587 the primary sort function should be the last.  You should probably
588 always include `gnus-thread-sort-by-number' in the list of sorting
589 functions -- preferably first.
590
591 Ready-made functions include `gnus-thread-sort-by-number',
592 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
593 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
594 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
595   :group 'gnus-summary-sort
596   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
597                          (function-item gnus-thread-sort-by-author)
598                          (function-item gnus-thread-sort-by-subject)
599                          (function-item gnus-thread-sort-by-date)
600                          (function-item gnus-thread-sort-by-score)
601                          (function-item gnus-thread-sort-by-total-score)
602                          (function :tag "other"))))
603
604 (defcustom gnus-thread-score-function '+
605   "*Function used for calculating the total score of a thread.
606
607 The function is called with the scores of the article and each
608 subthread and should then return the score of the thread.
609
610 Some functions you can use are `+', `max', or `min'."
611   :group 'gnus-summary-sort
612   :type 'function)
613
614 (defcustom gnus-summary-expunge-below nil
615   "All articles that have a score less than this variable will be expunged.
616 This variable is local to the summary buffers."
617   :group 'gnus-score-default
618   :type '(choice (const :tag "off" nil)
619                  integer))
620
621 (defcustom gnus-thread-expunge-below nil
622   "All threads that have a total score less than this variable will be expunged.
623 See `gnus-thread-score-function' for en explanation of what a
624 \"thread score\" is.
625
626 This variable is local to the summary buffers."
627   :group 'gnus-threading
628   :group 'gnus-score-default
629   :type '(choice (const :tag "off" nil)
630                  integer))
631
632 (defcustom gnus-summary-mode-hook nil
633   "*A hook for Gnus summary mode.
634 This hook is run before any variables are set in the summary buffer."
635   :group 'gnus-summary-various
636   :type 'hook)
637
638 (defcustom gnus-summary-menu-hook nil
639   "*Hook run after the creation of the summary mode menu."
640   :group 'gnus-summary-visual
641   :type 'hook)
642
643 (defcustom gnus-summary-exit-hook nil
644   "*A hook called on exit from the summary buffer.
645 It will be called with point in the group buffer."
646   :group 'gnus-summary-exit
647   :type 'hook)
648
649 (defcustom gnus-summary-prepare-hook nil
650   "*A hook called after the summary buffer has been generated.
651 If you want to modify the summary buffer, you can use this hook."
652   :group 'gnus-summary-various
653   :type 'hook)
654
655 (defcustom gnus-summary-prepared-hook nil
656   "*A hook called as the last thing after the summary buffer has been generated."
657   :group 'gnus-summary-various
658   :type 'hook)
659
660 (defcustom gnus-summary-generate-hook nil
661   "*A hook run just before generating the summary buffer.
662 This hook is commonly used to customize threading variables and the
663 like."
664   :group 'gnus-summary-various
665   :type 'hook)
666
667 (defcustom gnus-select-group-hook nil
668   "*A hook called when a newsgroup is selected.
669
670 If you'd like to simplify subjects like the
671 `gnus-summary-next-same-subject' command does, you can use the
672 following hook:
673
674  (setq gnus-select-group-hook
675       (list
676         (lambda ()
677           (mapcar (lambda (header)
678                      (mail-header-set-subject
679                       header
680                       (gnus-simplify-subject
681                        (mail-header-subject header) 're-only)))
682                   gnus-newsgroup-headers))))"
683   :group 'gnus-group-select
684   :type 'hook)
685
686 (defcustom gnus-select-article-hook nil
687   "*A hook called when an article is selected."
688   :group 'gnus-summary-choose
689   :type 'hook)
690
691 (defcustom gnus-visual-mark-article-hook
692   (list 'gnus-highlight-selected-summary)
693   "*Hook run after selecting an article in the summary buffer.
694 It is meant to be used for highlighting the article in some way.  It
695 is not run if `gnus-visual' is nil."
696   :group 'gnus-summary-visual
697   :type 'hook)
698
699 (defcustom gnus-parse-headers-hook nil
700   "*A hook called before parsing the headers."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-exit-group-hook nil
705   "*A hook called when exiting summary mode.
706 This hook is not called from the non-updating exit commands like `Q'."
707   :group 'gnus-various
708   :type 'hook)
709
710 (defcustom gnus-summary-update-hook
711   (list 'gnus-summary-highlight-line)
712   "*A hook called when a summary line is changed.
713 The hook will not be called if `gnus-visual' is nil.
714
715 The default function `gnus-summary-highlight-line' will
716 highlight the line according to the `gnus-summary-highlight'
717 variable."
718   :group 'gnus-summary-visual
719   :type 'hook)
720
721 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
722   "*A hook called when an article is selected for the first time.
723 The hook is intended to mark an article as read (or unread)
724 automatically when it is selected."
725   :group 'gnus-summary-choose
726   :type 'hook)
727
728 (defcustom gnus-group-no-more-groups-hook nil
729   "*A hook run when returning to group mode having no more (unread) groups."
730   :group 'gnus-group-select
731   :type 'hook)
732
733 (defcustom gnus-ps-print-hook nil
734   "*A hook run before ps-printing something from Gnus."
735   :group 'gnus-summary
736   :type 'hook)
737
738 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
739   "Face used for highlighting the current article in the summary buffer."
740   :group 'gnus-summary-visual
741   :type 'face)
742
743 (defcustom gnus-summary-highlight
744   '(((= mark gnus-canceled-mark)
745      . gnus-summary-cancelled-face)
746     ((and (> score default)
747           (or (= mark gnus-dormant-mark)
748               (= mark gnus-ticked-mark)))
749      . gnus-summary-high-ticked-face)
750     ((and (< score default)
751           (or (= mark gnus-dormant-mark)
752               (= mark gnus-ticked-mark)))
753      . gnus-summary-low-ticked-face)
754     ((or (= mark gnus-dormant-mark)
755          (= mark gnus-ticked-mark))
756      . gnus-summary-normal-ticked-face)
757     ((and (> score default) (= mark gnus-ancient-mark))
758      . gnus-summary-high-ancient-face)
759     ((and (< score default) (= mark gnus-ancient-mark))
760      . gnus-summary-low-ancient-face)
761     ((= mark gnus-ancient-mark)
762      . gnus-summary-normal-ancient-face)
763     ((and (> score default) (= mark gnus-unread-mark))
764      . gnus-summary-high-unread-face)
765     ((and (< score default) (= mark gnus-unread-mark))
766      . gnus-summary-low-unread-face)
767     ((= mark gnus-unread-mark)
768      . gnus-summary-normal-unread-face)
769     ((and (> score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-high-unread-face)
772     ((and (< score default) (memq mark (list gnus-downloadable-mark
773                                              gnus-undownloaded-mark)))
774      . gnus-summary-low-unread-face)
775     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
776      . gnus-summary-normal-unread-face)
777     ((> score default)
778      . gnus-summary-high-read-face)
779     ((< score default)
780      . gnus-summary-low-read-face)
781     (t
782      . gnus-summary-normal-read-face))
783   "*Controls the highlighting of summary buffer lines.
784
785 A list of (FORM . FACE) pairs.  When deciding how a a particular
786 summary line should be displayed, each form is evaluated.  The content
787 of the face field after the first true form is used.  You can change
788 how those summary lines are displayed, by editing the face field.
789
790 You can use the following variables in the FORM field.
791
792 score:   The articles score
793 default: The default article score.
794 below:   The score below which articles are automatically marked as read.
795 mark:    The articles mark."
796   :group 'gnus-summary-visual
797   :type '(repeat (cons (sexp :tag "Form" nil)
798                        face)))
799
800 (defcustom gnus-alter-header-function nil
801   "Function called to allow alteration of article header structures.
802 The function is called with one parameter, the article header vector,
803 which it may alter in any way.")
804
805 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
806   "Variable that says which function should be used to decode a string with encoded words.")
807
808 (defcustom gnus-extra-headers nil
809   "*Extra headers to parse."
810   :group 'gnus-summary
811   :type '(repeat symbol))
812
813 (defcustom gnus-ignored-from-addresses
814   (and user-mail-address (regexp-quote user-mail-address))
815   "*Regexp of From headers that may be suppressed in favor of To headers."
816   :group 'gnus-summary
817   :type 'regexp)
818
819 (defcustom gnus-group-charset-alist
820   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
821     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
822     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
823     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
824     ("^relcom\\>" koi8-r)
825     ("^fido7\\>" koi8-r)
826     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
827     ("^israel\\>" iso-8859-1)
828     ("^han\\>" euc-kr)
829     ("^alt.chinese.text.big5\\>" chinese-big5)
830     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
831     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
832     (".*" iso-8859-1))
833   "Alist of regexps (to match group names) and default charsets to be used when reading."
834   :type '(repeat (list (regexp :tag "Group")
835                        (symbol :tag "Charset")))
836   :group 'gnus-charset)
837
838 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
839   "List of charsets that should be ignored.
840 When these charsets are used in the \"charset\" parameter, the
841 default charset will be used instead."
842   :type '(repeat symbol)
843   :group 'gnus-charset)
844
845 (defcustom gnus-group-ignored-charsets-alist
846   '(("alt\\.chinese\\.text" iso-8859-1))
847   "Alist of regexps (to match group names) and charsets that should be ignored.
848 When these charsets are used in the \"charset\" parameter, the
849 default charset will be used instead."
850   :type '(repeat (cons (regexp :tag "Group")
851                        (repeat symbol)))
852   :group 'gnus-charset)
853
854 (defcustom gnus-group-highlight-words-alist nil
855   "Alist of group regexps and highlight regexps.
856 This variable uses the same syntax as `gnus-emphasis-alist'."
857   :type '(repeat (cons (regexp :tag "Group")
858                        (repeat (list (regexp :tag "Highlight regexp")
859                                      (number :tag "Group for entire word" 0)
860                                      (number :tag "Group for displayed part" 0)
861                                      (symbol :tag "Face"
862                                              gnus-emphasis-highlight-words)))))
863   :group 'gnus-summary-visual)
864
865 (defcustom gnus-summary-show-article-charset-alist
866   nil
867   "Alist of number and charset.
868 The article will be shown with the charset corresponding to the
869 numbered argument.
870 For example: ((1 . cn-gb-2312) (2 . big5))."
871   :type '(repeat (cons (number :tag "Argument" 1)
872                        (symbol :tag "Charset")))
873   :group 'gnus-charset)
874
875 (defcustom gnus-preserve-marks t
876   "Whether marks are preserved when moving, copying and respooling messages."
877   :type 'boolean
878   :group 'gnus-summary-marks)
879
880 (defcustom gnus-alter-articles-to-read-function nil
881   "Function to be called to alter the list of articles to be selected."
882   :type 'function
883   :group 'gnus-summary)
884
885 (defcustom gnus-orphan-score nil
886   "*All orphans get this score added.  Set in the score file."
887   :group 'gnus-score-default
888   :type '(choice (const nil)
889                  integer))
890
891 (defcustom gnus-summary-save-parts-default-mime "image/.*"
892   "*A regexp to match MIME parts when saving multiple parts of a message
893 with gnus-summary-save-parts (X m). This regexp will be used by default
894 when prompting the user for which type of files to save."
895   :group 'gnus-summary
896   :type 'regexp)
897
898
899 ;;; Internal variables
900
901 (defvar gnus-article-mime-handles nil)
902 (defvar gnus-article-decoded-p nil)
903 (defvar gnus-article-charset nil)
904 (defvar gnus-article-ignored-charsets nil)
905 (defvar gnus-scores-exclude-files nil)
906 (defvar gnus-page-broken nil)
907 (defvar gnus-inhibit-mime-unbuttonizing nil)
908
909 (defvar gnus-original-article nil)
910 (defvar gnus-article-internal-prepare-hook nil)
911 (defvar gnus-newsgroup-process-stack nil)
912
913 (defvar gnus-thread-indent-array nil)
914 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
915 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
916   "Function called to sort the articles within a thread after it has been gathered together.")
917
918 (defvar gnus-summary-save-parts-type-history nil)
919 (defvar gnus-summary-save-parts-last-directory nil)
920
921 ;; Avoid highlighting in kill files.
922 (defvar gnus-summary-inhibit-highlight nil)
923 (defvar gnus-newsgroup-selected-overlay nil)
924 (defvar gnus-inhibit-limiting nil)
925 (defvar gnus-newsgroup-adaptive-score-file nil)
926 (defvar gnus-current-score-file nil)
927 (defvar gnus-current-move-group nil)
928 (defvar gnus-current-copy-group nil)
929 (defvar gnus-current-crosspost-group nil)
930
931 (defvar gnus-newsgroup-dependencies nil)
932 (defvar gnus-newsgroup-adaptive nil)
933 (defvar gnus-summary-display-article-function nil)
934 (defvar gnus-summary-highlight-line-function nil
935   "Function called after highlighting a summary line.")
936
937 (defvar gnus-summary-line-format-alist
938   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
939     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
940     (?s gnus-tmp-subject-or-nil ?s)
941     (?n gnus-tmp-name ?s)
942     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
943         ?s)
944     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
945             gnus-tmp-from) ?s)
946     (?F gnus-tmp-from ?s)
947     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
948     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
949     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
950     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
951     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
952     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
953     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
954     (?L gnus-tmp-lines ?d)
955     (?I gnus-tmp-indentation ?s)
956     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
957     (?R gnus-tmp-replied ?c)
958     (?\[ gnus-tmp-opening-bracket ?c)
959     (?\] gnus-tmp-closing-bracket ?c)
960     (?\> (make-string gnus-tmp-level ? ) ?s)
961     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
962     (?i gnus-tmp-score ?d)
963     (?z gnus-tmp-score-char ?c)
964     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
965     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
966     (?U gnus-tmp-unread ?c)
967     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
968     (?t (gnus-summary-number-of-articles-in-thread
969          (and (boundp 'thread) (car thread)) gnus-tmp-level)
970         ?d)
971     (?e (gnus-summary-number-of-articles-in-thread
972          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
973         ?c)
974     (?u gnus-tmp-user-defined ?s)
975     (?P (gnus-pick-line-number) ?d))
976   "An alist of format specifications that can appear in summary lines.
977 These are paired with what variables they correspond with, along with
978 the type of the variable (string, integer, character, etc).")
979
980 (defvar gnus-summary-dummy-line-format-alist
981   `((?S gnus-tmp-subject ?s)
982     (?N gnus-tmp-number ?d)
983     (?u gnus-tmp-user-defined ?s)))
984
985 (defvar gnus-summary-mode-line-format-alist
986   `((?G gnus-tmp-group-name ?s)
987     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
988     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
989     (?A gnus-tmp-article-number ?d)
990     (?Z gnus-tmp-unread-and-unselected ?s)
991     (?V gnus-version ?s)
992     (?U gnus-tmp-unread-and-unticked ?d)
993     (?S gnus-tmp-subject ?s)
994     (?e gnus-tmp-unselected ?d)
995     (?u gnus-tmp-user-defined ?s)
996     (?d (length gnus-newsgroup-dormant) ?d)
997     (?t (length gnus-newsgroup-marked) ?d)
998     (?r (length gnus-newsgroup-reads) ?d)
999     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1000     (?E gnus-newsgroup-expunged-tally ?d)
1001     (?s (gnus-current-score-file-nondirectory) ?s)))
1002
1003 (defvar gnus-last-search-regexp nil
1004   "Default regexp for article search command.")
1005
1006 (defvar gnus-last-shell-command nil
1007   "Default shell command on article.")
1008
1009 (defvar gnus-newsgroup-begin nil)
1010 (defvar gnus-newsgroup-end nil)
1011 (defvar gnus-newsgroup-last-rmail nil)
1012 (defvar gnus-newsgroup-last-mail nil)
1013 (defvar gnus-newsgroup-last-folder nil)
1014 (defvar gnus-newsgroup-last-file nil)
1015 (defvar gnus-newsgroup-auto-expire nil)
1016 (defvar gnus-newsgroup-active nil)
1017
1018 (defvar gnus-newsgroup-data nil)
1019 (defvar gnus-newsgroup-data-reverse nil)
1020 (defvar gnus-newsgroup-limit nil)
1021 (defvar gnus-newsgroup-limits nil)
1022
1023 (defvar gnus-newsgroup-unreads nil
1024   "List of unread articles in the current newsgroup.")
1025
1026 (defvar gnus-newsgroup-unselected nil
1027   "List of unselected unread articles in the current newsgroup.")
1028
1029 (defvar gnus-newsgroup-reads nil
1030   "Alist of read articles and article marks in the current newsgroup.")
1031
1032 (defvar gnus-newsgroup-expunged-tally nil)
1033
1034 (defvar gnus-newsgroup-marked nil
1035   "List of ticked articles in the current newsgroup (a subset of unread art).")
1036
1037 (defvar gnus-newsgroup-killed nil
1038   "List of ranges of articles that have been through the scoring process.")
1039
1040 (defvar gnus-newsgroup-cached nil
1041   "List of articles that come from the article cache.")
1042
1043 (defvar gnus-newsgroup-saved nil
1044   "List of articles that have been saved.")
1045
1046 (defvar gnus-newsgroup-kill-headers nil)
1047
1048 (defvar gnus-newsgroup-replied nil
1049   "List of articles that have been replied to in the current newsgroup.")
1050
1051 (defvar gnus-newsgroup-expirable nil
1052   "List of articles in the current newsgroup that can be expired.")
1053
1054 (defvar gnus-newsgroup-processable nil
1055   "List of articles in the current newsgroup that can be processed.")
1056
1057 (defvar gnus-newsgroup-downloadable nil
1058   "List of articles in the current newsgroup that can be processed.")
1059
1060 (defvar gnus-newsgroup-undownloaded nil
1061   "List of articles in the current newsgroup that haven't been downloaded..")
1062
1063 (defvar gnus-newsgroup-unsendable nil
1064   "List of articles in the current newsgroup that won't be sent.")
1065
1066 (defvar gnus-newsgroup-bookmarks nil
1067   "List of articles in the current newsgroup that have bookmarks.")
1068
1069 (defvar gnus-newsgroup-dormant nil
1070   "List of dormant articles in the current newsgroup.")
1071
1072 (defvar gnus-newsgroup-scored nil
1073   "List of scored articles in the current newsgroup.")
1074
1075 (defvar gnus-newsgroup-headers nil
1076   "List of article headers in the current newsgroup.")
1077
1078 (defvar gnus-newsgroup-threads nil)
1079
1080 (defvar gnus-newsgroup-prepared nil
1081   "Whether the current group has been prepared properly.")
1082
1083 (defvar gnus-newsgroup-ancient nil
1084   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1085
1086 (defvar gnus-newsgroup-sparse nil)
1087
1088 (defvar gnus-current-article nil)
1089 (defvar gnus-article-current nil)
1090 (defvar gnus-current-headers nil)
1091 (defvar gnus-have-all-headers nil)
1092 (defvar gnus-last-article nil)
1093 (defvar gnus-newsgroup-history nil)
1094 (defvar gnus-newsgroup-charset nil)
1095 (defvar gnus-newsgroup-ephemeral-charset nil)
1096 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1097
1098 (defvar gnus-article-before-search nil)
1099
1100 (defconst gnus-summary-local-variables
1101   '(gnus-newsgroup-name
1102     gnus-newsgroup-begin gnus-newsgroup-end
1103     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1104     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1105     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1106     gnus-newsgroup-unselected gnus-newsgroup-marked
1107     gnus-newsgroup-reads gnus-newsgroup-saved
1108     gnus-newsgroup-replied gnus-newsgroup-expirable
1109     gnus-newsgroup-processable gnus-newsgroup-killed
1110     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1111     gnus-newsgroup-unsendable
1112     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1113     gnus-newsgroup-headers gnus-newsgroup-threads
1114     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1115     gnus-current-article gnus-current-headers gnus-have-all-headers
1116     gnus-last-article gnus-article-internal-prepare-hook
1117     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1118     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1119     gnus-thread-expunge-below
1120     gnus-score-alist gnus-current-score-file
1121     (gnus-summary-expunge-below . global)
1122     (gnus-summary-mark-below . global)
1123     (gnus-orphan-score . global)
1124     gnus-newsgroup-active gnus-scores-exclude-files
1125     gnus-newsgroup-history gnus-newsgroup-ancient
1126     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1127     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1128     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1129     (gnus-newsgroup-expunged-tally . 0)
1130     gnus-cache-removable-articles gnus-newsgroup-cached
1131     gnus-newsgroup-data gnus-newsgroup-data-reverse
1132     gnus-newsgroup-limit gnus-newsgroup-limits
1133     gnus-newsgroup-charset)
1134   "Variables that are buffer-local to the summary buffers.")
1135
1136 (defvar gnus-newsgroup-variables nil
1137   "Variables that have separate values in the newsgroups.")
1138
1139 ;; Byte-compiler warning.
1140 (defvar gnus-article-mode-map)
1141
1142 ;; MIME stuff.
1143
1144 (defvar gnus-decode-encoded-word-methods
1145   '(mail-decode-encoded-word-string)
1146   "List of methods used to decode encoded words.
1147
1148 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1149 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1150 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1151 whose names match REGEXP.
1152
1153 For example:
1154 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1155  mail-decode-encoded-word-string
1156  (\"chinese\" . rfc1843-decode-string))")
1157
1158 (defvar gnus-decode-encoded-word-methods-cache nil)
1159
1160 (defun gnus-multi-decode-encoded-word-string (string)
1161   "Apply the functions from `gnus-encoded-word-methods' that match."
1162   (unless (and gnus-decode-encoded-word-methods-cache
1163                (eq gnus-newsgroup-name
1164                    (car gnus-decode-encoded-word-methods-cache)))
1165     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1166     (mapcar (lambda (x)
1167               (if (symbolp x)
1168                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1169                 (if (and gnus-newsgroup-name
1170                          (string-match (car x) gnus-newsgroup-name))
1171                     (nconc gnus-decode-encoded-word-methods-cache
1172                            (list (cdr x))))))
1173           gnus-decode-encoded-word-methods))
1174   (let ((xlist gnus-decode-encoded-word-methods-cache))
1175     (pop xlist)
1176     (while xlist
1177       (setq string (funcall (pop xlist) string))))
1178   string)
1179
1180 ;; Subject simplification.
1181
1182 (defun gnus-simplify-whitespace (str)
1183   "Remove excessive whitespace from STR."
1184   (let ((mystr str))
1185     ;; Multiple spaces.
1186     (while (string-match "[ \t][ \t]+" mystr)
1187       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1188                           " "
1189                           (substring mystr (match-end 0)))))
1190     ;; Leading spaces.
1191     (when (string-match "^[ \t]+" mystr)
1192       (setq mystr (substring mystr (match-end 0))))
1193     ;; Trailing spaces.
1194     (when (string-match "[ \t]+$" mystr)
1195       (setq mystr (substring mystr 0 (match-beginning 0))))
1196     mystr))
1197
1198 (defsubst gnus-simplify-subject-re (subject)
1199   "Remove \"Re:\" from subject lines."
1200   (if (string-match "^[Rr][Ee]: *" subject)
1201       (substring subject (match-end 0))
1202     subject))
1203
1204 (defun gnus-simplify-subject (subject &optional re-only)
1205   "Remove `Re:' and words in parentheses.
1206 If RE-ONLY is non-nil, strip leading `Re:'s only."
1207   (let ((case-fold-search t))           ;Ignore case.
1208     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1209     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1210       (setq subject (substring subject (match-end 0))))
1211     ;; Remove uninteresting prefixes.
1212     (when (and (not re-only)
1213                gnus-simplify-ignored-prefixes
1214                (string-match gnus-simplify-ignored-prefixes subject))
1215       (setq subject (substring subject (match-end 0))))
1216     ;; Remove words in parentheses from end.
1217     (unless re-only
1218       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1219         (setq subject (substring subject 0 (match-beginning 0)))))
1220     ;; Return subject string.
1221     subject))
1222
1223 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1224 ;; all whitespace.
1225 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1226   (goto-char (point-min))
1227   (while (re-search-forward regexp nil t)
1228     (replace-match (or newtext ""))))
1229
1230 (defun gnus-simplify-buffer-fuzzy ()
1231   "Simplify string in the buffer fuzzily.
1232 The string in the accessible portion of the current buffer is simplified.
1233 It is assumed to be a single-line subject.
1234 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1235 matter is removed.  Additional things can be deleted by setting
1236 `gnus-simplify-subject-fuzzy-regexp'."
1237   (let ((case-fold-search t)
1238         (modified-tick))
1239     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1240
1241     (while (not (eq modified-tick (buffer-modified-tick)))
1242       (setq modified-tick (buffer-modified-tick))
1243       (cond
1244        ((listp gnus-simplify-subject-fuzzy-regexp)
1245         (mapcar 'gnus-simplify-buffer-fuzzy-step
1246                 gnus-simplify-subject-fuzzy-regexp))
1247        (gnus-simplify-subject-fuzzy-regexp
1248         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1249       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1250       (gnus-simplify-buffer-fuzzy-step
1251        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1252       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1253
1254     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1255     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1256     (gnus-simplify-buffer-fuzzy-step " $")
1257     (gnus-simplify-buffer-fuzzy-step "^ +")))
1258
1259 (defun gnus-simplify-subject-fuzzy (subject)
1260   "Simplify a subject string fuzzily.
1261 See `gnus-simplify-buffer-fuzzy' for details."
1262   (save-excursion
1263     (gnus-set-work-buffer)
1264     (let ((case-fold-search t))
1265       ;; Remove uninteresting prefixes.
1266       (when (and gnus-simplify-ignored-prefixes
1267                  (string-match gnus-simplify-ignored-prefixes subject))
1268         (setq subject (substring subject (match-end 0))))
1269       (insert subject)
1270       (inline (gnus-simplify-buffer-fuzzy))
1271       (buffer-string))))
1272
1273 (defsubst gnus-simplify-subject-fully (subject)
1274   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1275   (cond
1276    (gnus-simplify-subject-functions
1277     (gnus-map-function gnus-simplify-subject-functions subject))
1278    ((null gnus-summary-gather-subject-limit)
1279     (gnus-simplify-subject-re subject))
1280    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1281     (gnus-simplify-subject-fuzzy subject))
1282    ((numberp gnus-summary-gather-subject-limit)
1283     (gnus-limit-string (gnus-simplify-subject-re subject)
1284                        gnus-summary-gather-subject-limit))
1285    (t
1286     subject)))
1287
1288 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1289   "Check whether two subjects are equal.
1290 If optional argument simple-first is t, first argument is already
1291 simplified."
1292   (cond
1293    ((null simple-first)
1294     (equal (gnus-simplify-subject-fully s1)
1295            (gnus-simplify-subject-fully s2)))
1296    (t
1297     (equal s1
1298            (gnus-simplify-subject-fully s2)))))
1299
1300 (defun gnus-summary-bubble-group ()
1301   "Increase the score of the current group.
1302 This is a handy function to add to `gnus-summary-exit-hook' to
1303 increase the score of each group you read."
1304   (gnus-group-add-score gnus-newsgroup-name))
1305
1306 \f
1307 ;;;
1308 ;;; Gnus summary mode
1309 ;;;
1310
1311 (put 'gnus-summary-mode 'mode-class 'special)
1312
1313 (when t
1314   ;; Non-orthogonal keys
1315
1316   (gnus-define-keys gnus-summary-mode-map
1317     " " gnus-summary-next-page
1318     "\177" gnus-summary-prev-page
1319     [delete] gnus-summary-prev-page
1320     [backspace] gnus-summary-prev-page
1321     "\r" gnus-summary-scroll-up
1322     "\M-\r" gnus-summary-scroll-down
1323     "n" gnus-summary-next-unread-article
1324     "p" gnus-summary-prev-unread-article
1325     "N" gnus-summary-next-article
1326     "P" gnus-summary-prev-article
1327     "\M-\C-n" gnus-summary-next-same-subject
1328     "\M-\C-p" gnus-summary-prev-same-subject
1329     "\M-n" gnus-summary-next-unread-subject
1330     "\M-p" gnus-summary-prev-unread-subject
1331     "." gnus-summary-first-unread-article
1332     "," gnus-summary-best-unread-article
1333     "\M-s" gnus-summary-search-article-forward
1334     "\M-r" gnus-summary-search-article-backward
1335     "<" gnus-summary-beginning-of-article
1336     ">" gnus-summary-end-of-article
1337     "j" gnus-summary-goto-article
1338     "^" gnus-summary-refer-parent-article
1339     "\M-^" gnus-summary-refer-article
1340     "u" gnus-summary-tick-article-forward
1341     "!" gnus-summary-tick-article-forward
1342     "U" gnus-summary-tick-article-backward
1343     "d" gnus-summary-mark-as-read-forward
1344     "D" gnus-summary-mark-as-read-backward
1345     "E" gnus-summary-mark-as-expirable
1346     "\M-u" gnus-summary-clear-mark-forward
1347     "\M-U" gnus-summary-clear-mark-backward
1348     "k" gnus-summary-kill-same-subject-and-select
1349     "\C-k" gnus-summary-kill-same-subject
1350     "\M-\C-k" gnus-summary-kill-thread
1351     "\M-\C-l" gnus-summary-lower-thread
1352     "e" gnus-summary-edit-article
1353     "#" gnus-summary-mark-as-processable
1354     "\M-#" gnus-summary-unmark-as-processable
1355     "\M-\C-t" gnus-summary-toggle-threads
1356     "\M-\C-s" gnus-summary-show-thread
1357     "\M-\C-h" gnus-summary-hide-thread
1358     "\M-\C-f" gnus-summary-next-thread
1359     "\M-\C-b" gnus-summary-prev-thread
1360     [(meta down)] gnus-summary-next-thread
1361     [(meta up)] gnus-summary-prev-thread
1362     "\M-\C-u" gnus-summary-up-thread
1363     "\M-\C-d" gnus-summary-down-thread
1364     "&" gnus-summary-execute-command
1365     "c" gnus-summary-catchup-and-exit
1366     "\C-w" gnus-summary-mark-region-as-read
1367     "\C-t" gnus-summary-toggle-truncation
1368     "?" gnus-summary-mark-as-dormant
1369     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1370     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1371     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1372     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1373     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1374     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1375     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1376     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1377     "=" gnus-summary-expand-window
1378     "\C-x\C-s" gnus-summary-reselect-current-group
1379     "\M-g" gnus-summary-rescan-group
1380     "w" gnus-summary-stop-page-breaking
1381     "\C-c\C-r" gnus-summary-caesar-message
1382     "f" gnus-summary-followup
1383     "F" gnus-summary-followup-with-original
1384     "C" gnus-summary-cancel-article
1385     "r" gnus-summary-reply
1386     "R" gnus-summary-reply-with-original
1387     "\C-c\C-f" gnus-summary-mail-forward
1388     "o" gnus-summary-save-article
1389     "\C-o" gnus-summary-save-article-mail
1390     "|" gnus-summary-pipe-output
1391     "\M-k" gnus-summary-edit-local-kill
1392     "\M-K" gnus-summary-edit-global-kill
1393     ;; "V" gnus-version
1394     "\C-c\C-d" gnus-summary-describe-group
1395     "q" gnus-summary-exit
1396     "Q" gnus-summary-exit-no-update
1397     "\C-c\C-i" gnus-info-find-node
1398     gnus-mouse-2 gnus-mouse-pick-article
1399     "m" gnus-summary-mail-other-window
1400     "a" gnus-summary-post-news
1401     "x" gnus-summary-limit-to-unread
1402     "s" gnus-summary-isearch-article
1403     "t" gnus-summary-toggle-header
1404     "g" gnus-summary-show-article
1405     "l" gnus-summary-goto-last-article
1406     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1407     "\C-d" gnus-summary-enter-digest-group
1408     "\M-\C-d" gnus-summary-read-document
1409     "\M-\C-e" gnus-summary-edit-parameters
1410     "\M-\C-a" gnus-summary-customize-parameters
1411     "\C-c\C-b" gnus-bug
1412     "*" gnus-cache-enter-article
1413     "\M-*" gnus-cache-remove-article
1414     "\M-&" gnus-summary-universal-argument
1415     "\C-l" gnus-recenter
1416     "I" gnus-summary-increase-score
1417     "L" gnus-summary-lower-score
1418     "\M-i" gnus-symbolic-argument
1419     "h" gnus-summary-select-article-buffer
1420
1421     "b" gnus-article-view-part
1422     "\M-t" gnus-summary-toggle-display-buttonized
1423
1424     "V" gnus-summary-score-map
1425     "X" gnus-uu-extract-map
1426     "S" gnus-summary-send-map)
1427
1428   ;; Sort of orthogonal keymap
1429   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1430     "t" gnus-summary-tick-article-forward
1431     "!" gnus-summary-tick-article-forward
1432     "d" gnus-summary-mark-as-read-forward
1433     "r" gnus-summary-mark-as-read-forward
1434     "c" gnus-summary-clear-mark-forward
1435     " " gnus-summary-clear-mark-forward
1436     "e" gnus-summary-mark-as-expirable
1437     "x" gnus-summary-mark-as-expirable
1438     "?" gnus-summary-mark-as-dormant
1439     "b" gnus-summary-set-bookmark
1440     "B" gnus-summary-remove-bookmark
1441     "#" gnus-summary-mark-as-processable
1442     "\M-#" gnus-summary-unmark-as-processable
1443     "S" gnus-summary-limit-include-expunged
1444     "C" gnus-summary-catchup
1445     "H" gnus-summary-catchup-to-here
1446     "\C-c" gnus-summary-catchup-all
1447     "k" gnus-summary-kill-same-subject-and-select
1448     "K" gnus-summary-kill-same-subject
1449     "P" gnus-uu-mark-map)
1450
1451   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1452     "c" gnus-summary-clear-above
1453     "u" gnus-summary-tick-above
1454     "m" gnus-summary-mark-above
1455     "k" gnus-summary-kill-below)
1456
1457   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1458     "/" gnus-summary-limit-to-subject
1459     "n" gnus-summary-limit-to-articles
1460     "w" gnus-summary-pop-limit
1461     "s" gnus-summary-limit-to-subject
1462     "a" gnus-summary-limit-to-author
1463     "u" gnus-summary-limit-to-unread
1464     "m" gnus-summary-limit-to-marks
1465     "M" gnus-summary-limit-exclude-marks
1466     "v" gnus-summary-limit-to-score
1467     "*" gnus-summary-limit-include-cached
1468     "D" gnus-summary-limit-include-dormant
1469     "T" gnus-summary-limit-include-thread
1470     "d" gnus-summary-limit-exclude-dormant
1471     "t" gnus-summary-limit-to-age
1472     "x" gnus-summary-limit-to-extra
1473     "E" gnus-summary-limit-include-expunged
1474     "c" gnus-summary-limit-exclude-childless-dormant
1475     "C" gnus-summary-limit-mark-excluded-as-read)
1476
1477   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1478     "n" gnus-summary-next-unread-article
1479     "p" gnus-summary-prev-unread-article
1480     "N" gnus-summary-next-article
1481     "P" gnus-summary-prev-article
1482     "\C-n" gnus-summary-next-same-subject
1483     "\C-p" gnus-summary-prev-same-subject
1484     "\M-n" gnus-summary-next-unread-subject
1485     "\M-p" gnus-summary-prev-unread-subject
1486     "f" gnus-summary-first-unread-article
1487     "b" gnus-summary-best-unread-article
1488     "j" gnus-summary-goto-article
1489     "g" gnus-summary-goto-subject
1490     "l" gnus-summary-goto-last-article
1491     "o" gnus-summary-pop-article)
1492
1493   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1494     "k" gnus-summary-kill-thread
1495     "l" gnus-summary-lower-thread
1496     "i" gnus-summary-raise-thread
1497     "T" gnus-summary-toggle-threads
1498     "t" gnus-summary-rethread-current
1499     "^" gnus-summary-reparent-thread
1500     "s" gnus-summary-show-thread
1501     "S" gnus-summary-show-all-threads
1502     "h" gnus-summary-hide-thread
1503     "H" gnus-summary-hide-all-threads
1504     "n" gnus-summary-next-thread
1505     "p" gnus-summary-prev-thread
1506     "u" gnus-summary-up-thread
1507     "o" gnus-summary-top-thread
1508     "d" gnus-summary-down-thread
1509     "#" gnus-uu-mark-thread
1510     "\M-#" gnus-uu-unmark-thread)
1511
1512   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1513     "g" gnus-summary-prepare
1514     "c" gnus-summary-insert-cached-articles)
1515
1516   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1517     "c" gnus-summary-catchup-and-exit
1518     "C" gnus-summary-catchup-all-and-exit
1519     "E" gnus-summary-exit-no-update
1520     "Q" gnus-summary-exit
1521     "Z" gnus-summary-exit
1522     "n" gnus-summary-catchup-and-goto-next-group
1523     "R" gnus-summary-reselect-current-group
1524     "G" gnus-summary-rescan-group
1525     "N" gnus-summary-next-group
1526     "s" gnus-summary-save-newsrc
1527     "P" gnus-summary-prev-group)
1528
1529   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1530     " " gnus-summary-next-page
1531     "n" gnus-summary-next-page
1532     "\177" gnus-summary-prev-page
1533     [delete] gnus-summary-prev-page
1534     "p" gnus-summary-prev-page
1535     "\r" gnus-summary-scroll-up
1536     "\M-\r" gnus-summary-scroll-down
1537     "<" gnus-summary-beginning-of-article
1538     ">" gnus-summary-end-of-article
1539     "b" gnus-summary-beginning-of-article
1540     "e" gnus-summary-end-of-article
1541     "^" gnus-summary-refer-parent-article
1542     "r" gnus-summary-refer-parent-article
1543     "D" gnus-summary-enter-digest-group
1544     "R" gnus-summary-refer-references
1545     "T" gnus-summary-refer-thread
1546     "g" gnus-summary-show-article
1547     "s" gnus-summary-isearch-article
1548     "P" gnus-summary-print-article
1549     "t" gnus-article-babel)
1550
1551   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1552     "b" gnus-article-add-buttons
1553     "B" gnus-article-add-buttons-to-head
1554     "o" gnus-article-treat-overstrike
1555     "e" gnus-article-emphasize
1556     "w" gnus-article-fill-cited-article
1557     "Q" gnus-article-fill-long-lines
1558     "C" gnus-article-capitalize-sentences
1559     "c" gnus-article-remove-cr
1560     "q" gnus-article-de-quoted-unreadable
1561     "6" gnus-article-de-base64-unreadable
1562     "Z" gnus-article-decode-HZ
1563     "h" gnus-article-wash-html
1564     "s" gnus-summary-force-verify-and-decrypt
1565     "f" gnus-article-display-x-face
1566     "l" gnus-summary-stop-page-breaking
1567     "r" gnus-summary-caesar-message
1568     "t" gnus-summary-toggle-header
1569     "v" gnus-summary-verbose-headers
1570     "H" gnus-article-strip-headers-in-body
1571     "p" gnus-article-verify-x-pgp-sig
1572     "d" gnus-article-treat-dumbquotes)
1573
1574   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1575     "a" gnus-article-hide
1576     "h" gnus-article-hide-headers
1577     "b" gnus-article-hide-boring-headers
1578     "s" gnus-article-hide-signature
1579     "c" gnus-article-hide-citation
1580     "C" gnus-article-hide-citation-in-followups
1581     "l" gnus-article-hide-list-identifiers
1582     "p" gnus-article-hide-pgp
1583     "B" gnus-article-strip-banner
1584     "P" gnus-article-hide-pem
1585     "\C-c" gnus-article-hide-citation-maybe)
1586
1587   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1588     "a" gnus-article-highlight
1589     "h" gnus-article-highlight-headers
1590     "c" gnus-article-highlight-citation
1591     "s" gnus-article-highlight-signature)
1592
1593   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1594     "w" gnus-article-decode-mime-words
1595     "c" gnus-article-decode-charset
1596     "v" gnus-mime-view-all-parts
1597     "b" gnus-article-view-part)
1598
1599   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1600     "z" gnus-article-date-ut
1601     "u" gnus-article-date-ut
1602     "l" gnus-article-date-local
1603     "e" gnus-article-date-lapsed
1604     "o" gnus-article-date-original
1605     "i" gnus-article-date-iso8601
1606     "s" gnus-article-date-user)
1607
1608   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1609     "t" gnus-article-remove-trailing-blank-lines
1610     "l" gnus-article-strip-leading-blank-lines
1611     "m" gnus-article-strip-multiple-blank-lines
1612     "a" gnus-article-strip-blank-lines
1613     "A" gnus-article-strip-all-blank-lines
1614     "s" gnus-article-strip-leading-space
1615     "e" gnus-article-strip-trailing-space)
1616
1617   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1618     "v" gnus-version
1619     "f" gnus-summary-fetch-faq
1620     "d" gnus-summary-describe-group
1621     "h" gnus-summary-describe-briefly
1622     "i" gnus-info-find-node)
1623
1624   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1625     "e" gnus-summary-expire-articles
1626     "\M-\C-e" gnus-summary-expire-articles-now
1627     "\177" gnus-summary-delete-article
1628     [delete] gnus-summary-delete-article
1629     [backspace] gnus-summary-delete-article
1630     "m" gnus-summary-move-article
1631     "r" gnus-summary-respool-article
1632     "w" gnus-summary-edit-article
1633     "c" gnus-summary-copy-article
1634     "B" gnus-summary-crosspost-article
1635     "q" gnus-summary-respool-query
1636     "t" gnus-summary-respool-trace
1637     "i" gnus-summary-import-article
1638     "p" gnus-summary-article-posted-p)
1639
1640   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1641     "o" gnus-summary-save-article
1642     "m" gnus-summary-save-article-mail
1643     "F" gnus-summary-write-article-file
1644     "r" gnus-summary-save-article-rmail
1645     "f" gnus-summary-save-article-file
1646     "b" gnus-summary-save-article-body-file
1647     "h" gnus-summary-save-article-folder
1648     "v" gnus-summary-save-article-vm
1649     "p" gnus-summary-pipe-output
1650     "s" gnus-soup-add-article)
1651
1652   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1653     "b" gnus-summary-display-buttonized
1654     "m" gnus-summary-repair-multipart
1655     "v" gnus-article-view-part
1656     "o" gnus-article-save-part
1657     "c" gnus-article-copy-part
1658     "C" gnus-article-view-part-as-charset
1659     "e" gnus-article-externalize-part
1660     "E" gnus-article-encrypt-body
1661     "i" gnus-article-inline-part
1662     "|" gnus-article-pipe-part))
1663
1664 (defun gnus-summary-make-menu-bar ()
1665   (gnus-turn-off-edit-menu 'summary)
1666
1667   (unless (boundp 'gnus-summary-misc-menu)
1668
1669     (easy-menu-define
1670      gnus-summary-kill-menu gnus-summary-mode-map ""
1671      (cons
1672       "Score"
1673       (nconc
1674        (list
1675         ["Enter score..." gnus-summary-score-entry t]
1676         ["Customize" gnus-score-customize t])
1677        (gnus-make-score-map 'increase)
1678        (gnus-make-score-map 'lower)
1679        '(("Mark"
1680           ["Kill below" gnus-summary-kill-below t]
1681           ["Mark above" gnus-summary-mark-above t]
1682           ["Tick above" gnus-summary-tick-above t]
1683           ["Clear above" gnus-summary-clear-above t])
1684          ["Current score" gnus-summary-current-score t]
1685          ["Set score" gnus-summary-set-score t]
1686          ["Switch current score file..." gnus-score-change-score-file t]
1687          ["Set mark below..." gnus-score-set-mark-below t]
1688          ["Set expunge below..." gnus-score-set-expunge-below t]
1689          ["Edit current score file" gnus-score-edit-current-scores t]
1690          ["Edit score file" gnus-score-edit-file t]
1691          ["Trace score" gnus-score-find-trace t]
1692          ["Find words" gnus-score-find-favourite-words t]
1693          ["Rescore buffer" gnus-summary-rescore t]
1694          ["Increase score..." gnus-summary-increase-score t]
1695          ["Lower score..." gnus-summary-lower-score t]))))
1696
1697     ;; Define both the Article menu in the summary buffer and the equivalent
1698     ;; Commands menu in the article buffer here for consistency.
1699     (let ((innards
1700            '(("Hide"
1701               ["All" gnus-article-hide t]
1702               ["Headers" gnus-article-hide-headers t]
1703               ["Signature" gnus-article-hide-signature t]
1704               ["Citation" gnus-article-hide-citation t]
1705               ["List identifiers" gnus-article-hide-list-identifiers t]
1706               ["PGP" gnus-article-hide-pgp t]
1707               ["Banner" gnus-article-strip-banner t]
1708               ["Boring headers" gnus-article-hide-boring-headers t])
1709              ("Highlight"
1710               ["All" gnus-article-highlight t]
1711               ["Headers" gnus-article-highlight-headers t]
1712               ["Signature" gnus-article-highlight-signature t]
1713               ["Citation" gnus-article-highlight-citation t])
1714              ("MIME"
1715               ["Words" gnus-article-decode-mime-words t]
1716               ["Charset" gnus-article-decode-charset t]
1717               ["QP" gnus-article-de-quoted-unreadable t]
1718               ["Base64" gnus-article-de-base64-unreadable t]
1719               ["View all" gnus-mime-view-all-parts t]
1720               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1721               ["Encrypt body" gnus-article-encrypt-body t])
1722              ("Date"
1723               ["Local" gnus-article-date-local t]
1724               ["ISO8601" gnus-article-date-iso8601 t]
1725               ["UT" gnus-article-date-ut t]
1726               ["Original" gnus-article-date-original t]
1727               ["Lapsed" gnus-article-date-lapsed t]
1728               ["User-defined" gnus-article-date-user t])
1729              ("Washing"
1730               ("Remove Blanks"
1731                ["Leading" gnus-article-strip-leading-blank-lines t]
1732                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1733                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1734                ["All of the above" gnus-article-strip-blank-lines t]
1735                ["All" gnus-article-strip-all-blank-lines t]
1736                ["Leading space" gnus-article-strip-leading-space t]
1737                ["Trailing space" gnus-article-strip-trailing-space t])
1738               ["Overstrike" gnus-article-treat-overstrike t]
1739               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1740               ["Emphasis" gnus-article-emphasize t]
1741               ["Word wrap" gnus-article-fill-cited-article t]
1742               ["Fill long lines" gnus-article-fill-long-lines t]
1743               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1744               ["CR" gnus-article-remove-cr t]
1745               ["Show X-Face" gnus-article-display-x-face t]
1746               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1747               ["Base64" gnus-article-de-base64-unreadable t]
1748               ["Rot 13" gnus-summary-caesar-message t]
1749               ["Unix pipe" gnus-summary-pipe-message t]
1750               ["Add buttons" gnus-article-add-buttons t]
1751               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1752               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1753               ["Verbose header" gnus-summary-verbose-headers t]
1754               ["Toggle header" gnus-summary-toggle-header t]
1755               ["Html" gnus-article-wash-html t]
1756               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1757               ["HZ" gnus-article-decode-HZ t])
1758              ("Output"
1759               ["Save in default format" gnus-summary-save-article t]
1760               ["Save in file" gnus-summary-save-article-file t]
1761               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1762               ["Save in MH folder" gnus-summary-save-article-folder t]
1763               ["Save in VM folder" gnus-summary-save-article-vm t]
1764               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1765               ["Save body in file" gnus-summary-save-article-body-file t]
1766               ["Pipe through a filter" gnus-summary-pipe-output t]
1767               ["Add to SOUP packet" gnus-soup-add-article t]
1768               ["Print" gnus-summary-print-article t])
1769              ("Backend"
1770               ["Respool article..." gnus-summary-respool-article t]
1771               ["Move article..." gnus-summary-move-article
1772                (gnus-check-backend-function
1773                 'request-move-article gnus-newsgroup-name)]
1774               ["Copy article..." gnus-summary-copy-article t]
1775               ["Crosspost article..." gnus-summary-crosspost-article
1776                (gnus-check-backend-function
1777                 'request-replace-article gnus-newsgroup-name)]
1778               ["Import file..." gnus-summary-import-article t]
1779               ["Check if posted" gnus-summary-article-posted-p t]
1780               ["Edit article" gnus-summary-edit-article
1781                (not (gnus-group-read-only-p))]
1782               ["Delete article" gnus-summary-delete-article
1783                (gnus-check-backend-function
1784                 'request-expire-articles gnus-newsgroup-name)]
1785               ["Query respool" gnus-summary-respool-query t]
1786               ["Trace respool" gnus-summary-respool-trace t]
1787               ["Delete expirable articles" gnus-summary-expire-articles-now
1788                (gnus-check-backend-function
1789                 'request-expire-articles gnus-newsgroup-name)])
1790              ("Extract"
1791               ["Uudecode" gnus-uu-decode-uu t]
1792               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1793               ["Unshar" gnus-uu-decode-unshar t]
1794               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1795               ["Save" gnus-uu-decode-save t]
1796               ["Binhex" gnus-uu-decode-binhex t]
1797               ["Postscript" gnus-uu-decode-postscript t])
1798              ("Cache"
1799               ["Enter article" gnus-cache-enter-article t]
1800               ["Remove article" gnus-cache-remove-article t])
1801              ["Translate" gnus-article-babel t]
1802              ["Select article buffer" gnus-summary-select-article-buffer t]
1803              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1804              ["Isearch article..." gnus-summary-isearch-article t]
1805              ["Beginning of the article" gnus-summary-beginning-of-article t]
1806              ["End of the article" gnus-summary-end-of-article t]
1807              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1808              ["Fetch referenced articles" gnus-summary-refer-references t]
1809              ["Fetch current thread" gnus-summary-refer-thread t]
1810              ["Fetch article with id..." gnus-summary-refer-article t]
1811              ["Redisplay" gnus-summary-show-article t])))
1812       (easy-menu-define
1813        gnus-summary-article-menu gnus-summary-mode-map ""
1814        (cons "Article" innards))
1815
1816       (easy-menu-define
1817        gnus-article-commands-menu gnus-article-mode-map ""
1818        (cons "Commands" innards)))
1819
1820     (easy-menu-define
1821      gnus-summary-thread-menu gnus-summary-mode-map ""
1822      '("Threads"
1823        ["Toggle threading" gnus-summary-toggle-threads t]
1824        ["Hide threads" gnus-summary-hide-all-threads t]
1825        ["Show threads" gnus-summary-show-all-threads t]
1826        ["Hide thread" gnus-summary-hide-thread t]
1827        ["Show thread" gnus-summary-show-thread t]
1828        ["Go to next thread" gnus-summary-next-thread t]
1829        ["Go to previous thread" gnus-summary-prev-thread t]
1830        ["Go down thread" gnus-summary-down-thread t]
1831        ["Go up thread" gnus-summary-up-thread t]
1832        ["Top of thread" gnus-summary-top-thread t]
1833        ["Mark thread as read" gnus-summary-kill-thread t]
1834        ["Lower thread score" gnus-summary-lower-thread t]
1835        ["Raise thread score" gnus-summary-raise-thread t]
1836        ["Rethread current" gnus-summary-rethread-current t]))
1837
1838     (easy-menu-define
1839      gnus-summary-post-menu gnus-summary-mode-map ""
1840      '("Post"
1841        ["Post an article" gnus-summary-post-news t]
1842        ["Followup" gnus-summary-followup t]
1843        ["Followup and yank" gnus-summary-followup-with-original t]
1844        ["Supersede article" gnus-summary-supersede-article t]
1845        ["Cancel article" gnus-summary-cancel-article t]
1846        ["Reply" gnus-summary-reply t]
1847        ["Reply and yank" gnus-summary-reply-with-original t]
1848        ["Wide reply" gnus-summary-wide-reply t]
1849        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1850        ["Mail forward" gnus-summary-mail-forward t]
1851        ["Post forward" gnus-summary-post-forward t]
1852        ["Digest and mail" gnus-uu-digest-mail-forward t]
1853        ["Digest and post" gnus-uu-digest-post-forward t]
1854        ["Resend message" gnus-summary-resend-message t]
1855        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1856        ["Send a mail" gnus-summary-mail-other-window t]
1857        ["Uuencode and post" gnus-uu-post-news t]
1858        ["Followup via news" gnus-summary-followup-to-mail t]
1859        ["Followup via news and yank"
1860         gnus-summary-followup-to-mail-with-original t]
1861        ;;("Draft"
1862        ;;["Send" gnus-summary-send-draft t]
1863        ;;["Send bounced" gnus-resend-bounced-mail t])
1864        ))
1865
1866     (easy-menu-define
1867      gnus-summary-misc-menu gnus-summary-mode-map ""
1868      '("Misc"
1869        ("Mark Read"
1870         ["Mark as read" gnus-summary-mark-as-read-forward t]
1871         ["Mark same subject and select"
1872          gnus-summary-kill-same-subject-and-select t]
1873         ["Mark same subject" gnus-summary-kill-same-subject t]
1874         ["Catchup" gnus-summary-catchup t]
1875         ["Catchup all" gnus-summary-catchup-all t]
1876         ["Catchup to here" gnus-summary-catchup-to-here t]
1877         ["Catchup region" gnus-summary-mark-region-as-read t]
1878         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1879        ("Mark Various"
1880         ["Tick" gnus-summary-tick-article-forward t]
1881         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1882         ["Remove marks" gnus-summary-clear-mark-forward t]
1883         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1884         ["Set bookmark" gnus-summary-set-bookmark t]
1885         ["Remove bookmark" gnus-summary-remove-bookmark t])
1886        ("Mark Limit"
1887         ["Marks..." gnus-summary-limit-to-marks t]
1888         ["Subject..." gnus-summary-limit-to-subject t]
1889         ["Author..." gnus-summary-limit-to-author t]
1890         ["Age..." gnus-summary-limit-to-age t]
1891         ["Extra..." gnus-summary-limit-to-extra t]
1892         ["Score" gnus-summary-limit-to-score t]
1893         ["Unread" gnus-summary-limit-to-unread t]
1894         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1895         ["Articles" gnus-summary-limit-to-articles t]
1896         ["Pop limit" gnus-summary-pop-limit t]
1897         ["Show dormant" gnus-summary-limit-include-dormant t]
1898         ["Hide childless dormant"
1899          gnus-summary-limit-exclude-childless-dormant t]
1900         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1901         ["Hide marked" gnus-summary-limit-exclude-marks t]
1902         ["Show expunged" gnus-summary-show-all-expunged t])
1903        ("Process Mark"
1904         ["Set mark" gnus-summary-mark-as-processable t]
1905         ["Remove mark" gnus-summary-unmark-as-processable t]
1906         ["Remove all marks" gnus-summary-unmark-all-processable t]
1907         ["Mark above" gnus-uu-mark-over t]
1908         ["Mark series" gnus-uu-mark-series t]
1909         ["Mark region" gnus-uu-mark-region t]
1910         ["Unmark region" gnus-uu-unmark-region t]
1911         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1912         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1913         ["Mark all" gnus-uu-mark-all t]
1914         ["Mark buffer" gnus-uu-mark-buffer t]
1915         ["Mark sparse" gnus-uu-mark-sparse t]
1916         ["Mark thread" gnus-uu-mark-thread t]
1917         ["Unmark thread" gnus-uu-unmark-thread t]
1918         ("Process Mark Sets"
1919          ["Kill" gnus-summary-kill-process-mark t]
1920          ["Yank" gnus-summary-yank-process-mark
1921           gnus-newsgroup-process-stack]
1922          ["Save" gnus-summary-save-process-mark t]))
1923        ("Scroll article"
1924         ["Page forward" gnus-summary-next-page t]
1925         ["Page backward" gnus-summary-prev-page t]
1926         ["Line forward" gnus-summary-scroll-up t])
1927        ("Move"
1928         ["Next unread article" gnus-summary-next-unread-article t]
1929         ["Previous unread article" gnus-summary-prev-unread-article t]
1930         ["Next article" gnus-summary-next-article t]
1931         ["Previous article" gnus-summary-prev-article t]
1932         ["Next unread subject" gnus-summary-next-unread-subject t]
1933         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1934         ["Next article same subject" gnus-summary-next-same-subject t]
1935         ["Previous article same subject" gnus-summary-prev-same-subject t]
1936         ["First unread article" gnus-summary-first-unread-article t]
1937         ["Best unread article" gnus-summary-best-unread-article t]
1938         ["Go to subject number..." gnus-summary-goto-subject t]
1939         ["Go to article number..." gnus-summary-goto-article t]
1940         ["Go to the last article" gnus-summary-goto-last-article t]
1941         ["Pop article off history" gnus-summary-pop-article t])
1942        ("Sort"
1943         ["Sort by number" gnus-summary-sort-by-number t]
1944         ["Sort by author" gnus-summary-sort-by-author t]
1945         ["Sort by subject" gnus-summary-sort-by-subject t]
1946         ["Sort by date" gnus-summary-sort-by-date t]
1947         ["Sort by score" gnus-summary-sort-by-score t]
1948         ["Sort by lines" gnus-summary-sort-by-lines t]
1949         ["Sort by characters" gnus-summary-sort-by-chars t])
1950        ("Help"
1951         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1952         ["Describe group" gnus-summary-describe-group t]
1953         ["Read manual" gnus-info-find-node t])
1954        ("Modes"
1955         ["Pick and read" gnus-pick-mode t]
1956         ["Binary" gnus-binary-mode t])
1957        ("Regeneration"
1958         ["Regenerate" gnus-summary-prepare t]
1959         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1960         ["Toggle threading" gnus-summary-toggle-threads t])
1961        ["Filter articles..." gnus-summary-execute-command t]
1962        ["Run command on subjects..." gnus-summary-universal-argument t]
1963        ["Search articles forward..." gnus-summary-search-article-forward t]
1964        ["Search articles backward..." gnus-summary-search-article-backward t]
1965        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1966        ["Expand window" gnus-summary-expand-window t]
1967        ["Expire expirable articles" gnus-summary-expire-articles
1968         (gnus-check-backend-function
1969          'request-expire-articles gnus-newsgroup-name)]
1970        ["Edit local kill file" gnus-summary-edit-local-kill t]
1971        ["Edit main kill file" gnus-summary-edit-global-kill t]
1972        ["Edit group parameters" gnus-summary-edit-parameters t]
1973        ["Customize group parameters" gnus-summary-customize-parameters t]
1974        ["Send a bug report" gnus-bug t]
1975        ("Exit"
1976         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1977         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1978         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1979         ["Exit group" gnus-summary-exit t]
1980         ["Exit group without updating" gnus-summary-exit-no-update t]
1981         ["Exit and goto next group" gnus-summary-next-group t]
1982         ["Exit and goto prev group" gnus-summary-prev-group t]
1983         ["Reselect group" gnus-summary-reselect-current-group t]
1984         ["Rescan group" gnus-summary-rescan-group t]
1985         ["Update dribble" gnus-summary-save-newsrc t])))
1986
1987     (gnus-run-hooks 'gnus-summary-menu-hook)))
1988
1989 (defun gnus-score-set-default (var value)
1990   "A version of set that updates the GNU Emacs menu-bar."
1991   (set var value)
1992   ;; It is the message that forces the active status to be updated.
1993   (message ""))
1994
1995 (defun gnus-make-score-map (type)
1996   "Make a summary score map of type TYPE."
1997   (if t
1998       nil
1999     (let ((headers '(("author" "from" string)
2000                      ("subject" "subject" string)
2001                      ("article body" "body" string)
2002                      ("article head" "head" string)
2003                      ("xref" "xref" string)
2004                      ("extra header" "extra" string)
2005                      ("lines" "lines" number)
2006                      ("followups to author" "followup" string)))
2007           (types '((number ("less than" <)
2008                            ("greater than" >)
2009                            ("equal" =))
2010                    (string ("substring" s)
2011                            ("exact string" e)
2012                            ("fuzzy string" f)
2013                            ("regexp" r))))
2014           (perms '(("temporary" (current-time-string))
2015                    ("permanent" nil)
2016                    ("immediate" now)))
2017           header)
2018       (list
2019        (apply
2020         'nconc
2021         (list
2022          (if (eq type 'lower)
2023              "Lower score"
2024            "Increase score"))
2025         (let (outh)
2026           (while headers
2027             (setq header (car headers))
2028             (setq outh
2029                   (cons
2030                    (apply
2031                     'nconc
2032                     (list (car header))
2033                     (let ((ts (cdr (assoc (nth 2 header) types)))
2034                           outt)
2035                       (while ts
2036                         (setq outt
2037                               (cons
2038                                (apply
2039                                 'nconc
2040                                 (list (caar ts))
2041                                 (let ((ps perms)
2042                                       outp)
2043                                   (while ps
2044                                     (setq outp
2045                                           (cons
2046                                            (vector
2047                                             (caar ps)
2048                                             (list
2049                                              'gnus-summary-score-entry
2050                                              (nth 1 header)
2051                                              (if (or (string= (nth 1 header)
2052                                                               "head")
2053                                                      (string= (nth 1 header)
2054                                                               "body"))
2055                                                  ""
2056                                                (list 'gnus-summary-header
2057                                                      (nth 1 header)))
2058                                              (list 'quote (nth 1 (car ts)))
2059                                              (list 'gnus-score-delta-default
2060                                                    nil)
2061                                              (nth 1 (car ps))
2062                                              t)
2063                                             t)
2064                                            outp))
2065                                     (setq ps (cdr ps)))
2066                                   (list (nreverse outp))))
2067                                outt))
2068                         (setq ts (cdr ts)))
2069                       (list (nreverse outt))))
2070                    outh))
2071             (setq headers (cdr headers)))
2072           (list (nreverse outh))))))))
2073
2074 \f
2075
2076 (defun gnus-summary-mode (&optional group)
2077   "Major mode for reading articles.
2078
2079 All normal editing commands are switched off.
2080 \\<gnus-summary-mode-map>
2081 Each line in this buffer represents one article.  To read an
2082 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2083 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2084 respectively.
2085
2086 You can also post articles and send mail from this buffer.  To
2087 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2088 of an article, type `\\[gnus-summary-reply]'.
2089
2090 There are approx. one gazillion commands you can execute in this
2091 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2092
2093 The following commands are available:
2094
2095 \\{gnus-summary-mode-map}"
2096   (interactive)
2097   (when (gnus-visual-p 'summary-menu 'menu)
2098     (gnus-summary-make-menu-bar))
2099   (kill-all-local-variables)
2100   (gnus-summary-make-local-variables)
2101   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2102     (gnus-summary-make-local-variables))
2103   (gnus-make-thread-indent-array)
2104   (gnus-simplify-mode-line)
2105   (setq major-mode 'gnus-summary-mode)
2106   (setq mode-name "Summary")
2107   (make-local-variable 'minor-mode-alist)
2108   (use-local-map gnus-summary-mode-map)
2109   (buffer-disable-undo)
2110   (setq buffer-read-only t)             ;Disable modification
2111   (setq truncate-lines t)
2112   (setq selective-display t)
2113   (setq selective-display-ellipses t)   ;Display `...'
2114   (gnus-summary-set-display-table)
2115   (gnus-set-default-directory)
2116   (setq gnus-newsgroup-name group)
2117   (make-local-variable 'gnus-summary-line-format)
2118   (make-local-variable 'gnus-summary-line-format-spec)
2119   (make-local-variable 'gnus-summary-dummy-line-format)
2120   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2121   (make-local-variable 'gnus-summary-mark-positions)
2122   (make-local-hook 'pre-command-hook)
2123   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2124   (gnus-run-hooks 'gnus-summary-mode-hook)
2125   (mm-enable-multibyte-mule4)
2126   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2127   (gnus-update-summary-mark-positions))
2128
2129 (defun gnus-summary-make-local-variables ()
2130   "Make all the local summary buffer variables."
2131   (let (global)
2132     (dolist (local gnus-summary-local-variables)
2133       (if (consp local)
2134           (progn
2135             (if (eq (cdr local) 'global)
2136                 ;; Copy the global value of the variable.
2137                 (setq global (symbol-value (car local)))
2138               ;; Use the value from the list.
2139               (setq global (eval (cdr local))))
2140             (set (make-local-variable (car local)) global))
2141         ;; Simple nil-valued local variable.
2142         (set (make-local-variable local) nil)))))
2143
2144 (defun gnus-summary-clear-local-variables ()
2145   (let ((locals gnus-summary-local-variables))
2146     (while locals
2147       (if (consp (car locals))
2148           (and (vectorp (caar locals))
2149                (set (caar locals) nil))
2150         (and (vectorp (car locals))
2151              (set (car locals) nil)))
2152       (setq locals (cdr locals)))))
2153
2154 ;; Summary data functions.
2155
2156 (defmacro gnus-data-number (data)
2157   `(car ,data))
2158
2159 (defmacro gnus-data-set-number (data number)
2160   `(setcar ,data ,number))
2161
2162 (defmacro gnus-data-mark (data)
2163   `(nth 1 ,data))
2164
2165 (defmacro gnus-data-set-mark (data mark)
2166   `(setcar (nthcdr 1 ,data) ,mark))
2167
2168 (defmacro gnus-data-pos (data)
2169   `(nth 2 ,data))
2170
2171 (defmacro gnus-data-set-pos (data pos)
2172   `(setcar (nthcdr 2 ,data) ,pos))
2173
2174 (defmacro gnus-data-header (data)
2175   `(nth 3 ,data))
2176
2177 (defmacro gnus-data-set-header (data header)
2178   `(setf (nth 3 ,data) ,header))
2179
2180 (defmacro gnus-data-level (data)
2181   `(nth 4 ,data))
2182
2183 (defmacro gnus-data-unread-p (data)
2184   `(= (nth 1 ,data) gnus-unread-mark))
2185
2186 (defmacro gnus-data-read-p (data)
2187   `(/= (nth 1 ,data) gnus-unread-mark))
2188
2189 (defmacro gnus-data-pseudo-p (data)
2190   `(consp (nth 3 ,data)))
2191
2192 (defmacro gnus-data-find (number)
2193   `(assq ,number gnus-newsgroup-data))
2194
2195 (defmacro gnus-data-find-list (number &optional data)
2196   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2197      (memq (assq ,number bdata)
2198            bdata)))
2199
2200 (defmacro gnus-data-make (number mark pos header level)
2201   `(list ,number ,mark ,pos ,header ,level))
2202
2203 (defun gnus-data-enter (after-article number mark pos header level offset)
2204   (let ((data (gnus-data-find-list after-article)))
2205     (unless data
2206       (error "No such article: %d" after-article))
2207     (setcdr data (cons (gnus-data-make number mark pos header level)
2208                        (cdr data)))
2209     (setq gnus-newsgroup-data-reverse nil)
2210     (gnus-data-update-list (cddr data) offset)))
2211
2212 (defun gnus-data-enter-list (after-article list &optional offset)
2213   (when list
2214     (let ((data (and after-article (gnus-data-find-list after-article)))
2215           (ilist list))
2216       (if (not (or data
2217                    after-article))
2218           (let ((odata gnus-newsgroup-data))
2219             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2220             (when offset
2221               (gnus-data-update-list odata offset)))
2222         ;; Find the last element in the list to be spliced into the main
2223         ;; list.
2224         (while (cdr list)
2225           (setq list (cdr list)))
2226         (if (not data)
2227             (progn
2228               (setcdr list gnus-newsgroup-data)
2229               (setq gnus-newsgroup-data ilist)
2230               (when offset
2231                 (gnus-data-update-list (cdr list) offset)))
2232           (setcdr list (cdr data))
2233           (setcdr data ilist)
2234           (when offset
2235             (gnus-data-update-list (cdr list) offset))))
2236       (setq gnus-newsgroup-data-reverse nil))))
2237
2238 (defun gnus-data-remove (article &optional offset)
2239   (let ((data gnus-newsgroup-data))
2240     (if (= (gnus-data-number (car data)) article)
2241         (progn
2242           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2243                 gnus-newsgroup-data-reverse nil)
2244           (when offset
2245             (gnus-data-update-list gnus-newsgroup-data offset)))
2246       (while (cdr data)
2247         (when (= (gnus-data-number (cadr data)) article)
2248           (setcdr data (cddr data))
2249           (when offset
2250             (gnus-data-update-list (cdr data) offset))
2251           (setq data nil
2252                 gnus-newsgroup-data-reverse nil))
2253         (setq data (cdr data))))))
2254
2255 (defmacro gnus-data-list (backward)
2256   `(if ,backward
2257        (or gnus-newsgroup-data-reverse
2258            (setq gnus-newsgroup-data-reverse
2259                  (reverse gnus-newsgroup-data)))
2260      gnus-newsgroup-data))
2261
2262 (defun gnus-data-update-list (data offset)
2263   "Add OFFSET to the POS of all data entries in DATA."
2264   (setq gnus-newsgroup-data-reverse nil)
2265   (while data
2266     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2267     (setq data (cdr data))))
2268
2269 (defun gnus-summary-article-pseudo-p (article)
2270   "Say whether this article is a pseudo article or not."
2271   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2272
2273 (defmacro gnus-summary-article-sparse-p (article)
2274   "Say whether this article is a sparse article or not."
2275   `(memq ,article gnus-newsgroup-sparse))
2276
2277 (defmacro gnus-summary-article-ancient-p (article)
2278   "Say whether this article is a sparse article or not."
2279   `(memq ,article gnus-newsgroup-ancient))
2280
2281 (defun gnus-article-parent-p (number)
2282   "Say whether this article is a parent or not."
2283   (let ((data (gnus-data-find-list number)))
2284     (and (cdr data)                     ; There has to be an article after...
2285          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2286             (gnus-data-level (nth 1 data))))))
2287
2288 (defun gnus-article-children (number)
2289   "Return a list of all children to NUMBER."
2290   (let* ((data (gnus-data-find-list number))
2291          (level (gnus-data-level (car data)))
2292          children)
2293     (setq data (cdr data))
2294     (while (and data
2295                 (= (gnus-data-level (car data)) (1+ level)))
2296       (push (gnus-data-number (car data)) children)
2297       (setq data (cdr data)))
2298     children))
2299
2300 (defmacro gnus-summary-skip-intangible ()
2301   "If the current article is intangible, then jump to a different article."
2302   '(let ((to (get-text-property (point) 'gnus-intangible)))
2303      (and to (gnus-summary-goto-subject to))))
2304
2305 (defmacro gnus-summary-article-intangible-p ()
2306   "Say whether this article is intangible or not."
2307   '(get-text-property (point) 'gnus-intangible))
2308
2309 (defun gnus-article-read-p (article)
2310   "Say whether ARTICLE is read or not."
2311   (not (or (memq article gnus-newsgroup-marked)
2312            (memq article gnus-newsgroup-unreads)
2313            (memq article gnus-newsgroup-unselected)
2314            (memq article gnus-newsgroup-dormant))))
2315
2316 ;; Some summary mode macros.
2317
2318 (defmacro gnus-summary-article-number ()
2319   "The article number of the article on the current line.
2320 If there isn's an article number here, then we return the current
2321 article number."
2322   '(progn
2323      (gnus-summary-skip-intangible)
2324      (or (get-text-property (point) 'gnus-number)
2325          (gnus-summary-last-subject))))
2326
2327 (defmacro gnus-summary-article-header (&optional number)
2328   "Return the header of article NUMBER."
2329   `(gnus-data-header (gnus-data-find
2330                       ,(or number '(gnus-summary-article-number)))))
2331
2332 (defmacro gnus-summary-thread-level (&optional number)
2333   "Return the level of thread that starts with article NUMBER."
2334   `(if (and (eq gnus-summary-make-false-root 'dummy)
2335             (get-text-property (point) 'gnus-intangible))
2336        0
2337      (gnus-data-level (gnus-data-find
2338                        ,(or number '(gnus-summary-article-number))))))
2339
2340 (defmacro gnus-summary-article-mark (&optional number)
2341   "Return the mark of article NUMBER."
2342   `(gnus-data-mark (gnus-data-find
2343                     ,(or number '(gnus-summary-article-number)))))
2344
2345 (defmacro gnus-summary-article-pos (&optional number)
2346   "Return the position of the line of article NUMBER."
2347   `(gnus-data-pos (gnus-data-find
2348                    ,(or number '(gnus-summary-article-number)))))
2349
2350 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2351 (defmacro gnus-summary-article-subject (&optional number)
2352   "Return current subject string or nil if nothing."
2353   `(let ((headers
2354           ,(if number
2355                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2356              '(gnus-data-header (assq (gnus-summary-article-number)
2357                                       gnus-newsgroup-data)))))
2358      (and headers
2359           (vectorp headers)
2360           (mail-header-subject headers))))
2361
2362 (defmacro gnus-summary-article-score (&optional number)
2363   "Return current article score."
2364   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2365                   gnus-newsgroup-scored))
2366        gnus-summary-default-score 0))
2367
2368 (defun gnus-summary-article-children (&optional number)
2369   "Return a list of article numbers that are children of article NUMBER."
2370   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2371          (level (gnus-data-level (car data)))
2372          l children)
2373     (while (and (setq data (cdr data))
2374                 (> (setq l (gnus-data-level (car data))) level))
2375       (and (= (1+ level) l)
2376            (push (gnus-data-number (car data))
2377                  children)))
2378     (nreverse children)))
2379
2380 (defun gnus-summary-article-parent (&optional number)
2381   "Return the article number of the parent of article NUMBER."
2382   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2383                                     (gnus-data-list t)))
2384          (level (gnus-data-level (car data))))
2385     (if (zerop level)
2386         ()                              ; This is a root.
2387       ;; We search until we find an article with a level less than
2388       ;; this one.  That function has to be the parent.
2389       (while (and (setq data (cdr data))
2390                   (not (< (gnus-data-level (car data)) level))))
2391       (and data (gnus-data-number (car data))))))
2392
2393 (defun gnus-unread-mark-p (mark)
2394   "Say whether MARK is the unread mark."
2395   (= mark gnus-unread-mark))
2396
2397 (defun gnus-read-mark-p (mark)
2398   "Say whether MARK is one of the marks that mark as read.
2399 This is all marks except unread, ticked, dormant, and expirable."
2400   (not (or (= mark gnus-unread-mark)
2401            (= mark gnus-ticked-mark)
2402            (= mark gnus-dormant-mark)
2403            (= mark gnus-expirable-mark))))
2404
2405 (defmacro gnus-article-mark (number)
2406   "Return the MARK of article NUMBER.
2407 This macro should only be used when computing the mark the \"first\"
2408 time; i.e., when generating the summary lines.  After that,
2409 `gnus-summary-article-mark' should be used to examine the
2410 marks of articles."
2411   `(cond
2412     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2413     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2414     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2415     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2416     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2417     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2418     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2419     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2420            gnus-ancient-mark))))
2421
2422 ;; Saving hidden threads.
2423
2424 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2425 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2426
2427 (defmacro gnus-save-hidden-threads (&rest forms)
2428   "Save hidden threads, eval FORMS, and restore the hidden threads."
2429   (let ((config (make-symbol "config")))
2430     `(let ((,config (gnus-hidden-threads-configuration)))
2431        (unwind-protect
2432            (save-excursion
2433              ,@forms)
2434          (gnus-restore-hidden-threads-configuration ,config)))))
2435
2436 (defun gnus-data-compute-positions ()
2437   "Compute the positions of all articles."
2438   (setq gnus-newsgroup-data-reverse nil)
2439   (let ((data gnus-newsgroup-data))
2440     (save-excursion
2441       (gnus-save-hidden-threads
2442         (gnus-summary-show-all-threads)
2443         (goto-char (point-min))
2444         (while data
2445           (while (get-text-property (point) 'gnus-intangible)
2446             (forward-line 1))
2447           (gnus-data-set-pos (car data) (+ (point) 3))
2448           (setq data (cdr data))
2449           (forward-line 1))))))
2450
2451 (defun gnus-hidden-threads-configuration ()
2452   "Return the current hidden threads configuration."
2453   (save-excursion
2454     (let (config)
2455       (goto-char (point-min))
2456       (while (search-forward "\r" nil t)
2457         (push (1- (point)) config))
2458       config)))
2459
2460 (defun gnus-restore-hidden-threads-configuration (config)
2461   "Restore hidden threads configuration from CONFIG."
2462   (save-excursion
2463     (let (point buffer-read-only)
2464       (while (setq point (pop config))
2465         (when (and (< point (point-max))
2466                    (goto-char point)
2467                    (eq (char-after) ?\n))
2468           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2469
2470 ;; Various summary mode internalish functions.
2471
2472 (defun gnus-mouse-pick-article (e)
2473   (interactive "e")
2474   (mouse-set-point e)
2475   (gnus-summary-next-page nil t))
2476
2477 (defun gnus-summary-set-display-table ()
2478   "Change the display table.
2479 Odd characters have a tendency to mess
2480 up nicely formatted displays - we make all possible glyphs
2481 display only a single character."
2482
2483   ;; We start from the standard display table, if any.
2484   (let ((table (or (copy-sequence standard-display-table)
2485                    (make-display-table)))
2486         (i 32))
2487     ;; Nix out all the control chars...
2488     (while (>= (setq i (1- i)) 0)
2489       (aset table i [??]))
2490     ;; ... but not newline and cr, of course.  (cr is necessary for the
2491     ;; selective display).
2492     (aset table ?\n nil)
2493     (aset table ?\r nil)
2494     ;; We keep TAB as well.
2495     (aset table ?\t nil)
2496     ;; We nix out any glyphs over 126 that are not set already.
2497     (let ((i 256))
2498       (while (>= (setq i (1- i)) 127)
2499         ;; Only modify if the entry is nil.
2500         (unless (aref table i)
2501           (aset table i [??]))))
2502     (setq buffer-display-table table)))
2503
2504 (defun gnus-summary-setup-buffer (group)
2505   "Initialize summary buffer."
2506   (let ((buffer (concat "*Summary " group "*")))
2507     (if (get-buffer buffer)
2508         (progn
2509           (set-buffer buffer)
2510           (setq gnus-summary-buffer (current-buffer))
2511           (not gnus-newsgroup-prepared))
2512       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2513       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2514       (gnus-summary-mode group)
2515       (when gnus-carpal
2516         (gnus-carpal-setup-buffer 'summary))
2517       (unless gnus-single-article-buffer
2518         (make-local-variable 'gnus-article-buffer)
2519         (make-local-variable 'gnus-article-current)
2520         (make-local-variable 'gnus-original-article-buffer))
2521       (setq gnus-newsgroup-name group)
2522       t)))
2523
2524 (defun gnus-set-global-variables ()
2525   "Set the global equivalents of the buffer-local variables.
2526 They are set to the latest values they had.  These reflect the summary
2527 buffer that was in action when the last article was fetched."
2528   (when (eq major-mode 'gnus-summary-mode)
2529     (setq gnus-summary-buffer (current-buffer))
2530     (let ((name gnus-newsgroup-name)
2531           (marked gnus-newsgroup-marked)
2532           (unread gnus-newsgroup-unreads)
2533           (headers gnus-current-headers)
2534           (data gnus-newsgroup-data)
2535           (summary gnus-summary-buffer)
2536           (article-buffer gnus-article-buffer)
2537           (original gnus-original-article-buffer)
2538           (gac gnus-article-current)
2539           (reffed gnus-reffed-article-number)
2540           (score-file gnus-current-score-file)
2541           (default-charset gnus-newsgroup-charset)
2542           vlist)
2543       (let ((locals gnus-newsgroup-variables))
2544         (while locals
2545           (if (consp (car locals))
2546               (push (eval (caar locals)) vlist)
2547             (push (eval (car locals)) vlist))
2548           (setq locals (cdr locals)))
2549         (setq vlist (nreverse vlist)))
2550       (save-excursion
2551         (set-buffer gnus-group-buffer)
2552         (setq gnus-newsgroup-name name
2553               gnus-newsgroup-marked marked
2554               gnus-newsgroup-unreads unread
2555               gnus-current-headers headers
2556               gnus-newsgroup-data data
2557               gnus-article-current gac
2558               gnus-summary-buffer summary
2559               gnus-article-buffer article-buffer
2560               gnus-original-article-buffer original
2561               gnus-reffed-article-number reffed
2562               gnus-current-score-file score-file
2563               gnus-newsgroup-charset default-charset)
2564         (let ((locals gnus-newsgroup-variables))
2565           (while locals
2566             (if (consp (car locals))
2567                 (set (caar locals) (pop vlist))
2568               (set (car locals) (pop vlist)))
2569             (setq locals (cdr locals))))
2570         ;; The article buffer also has local variables.
2571         (when (gnus-buffer-live-p gnus-article-buffer)
2572           (set-buffer gnus-article-buffer)
2573           (setq gnus-summary-buffer summary))))))
2574
2575 (defun gnus-summary-article-unread-p (article)
2576   "Say whether ARTICLE is unread or not."
2577   (memq article gnus-newsgroup-unreads))
2578
2579 (defun gnus-summary-first-article-p (&optional article)
2580   "Return whether ARTICLE is the first article in the buffer."
2581   (if (not (setq article (or article (gnus-summary-article-number))))
2582       nil
2583     (eq article (caar gnus-newsgroup-data))))
2584
2585 (defun gnus-summary-last-article-p (&optional article)
2586   "Return whether ARTICLE is the last article in the buffer."
2587   (if (not (setq article (or article (gnus-summary-article-number))))
2588       ;; All non-existent numbers are the last article.  :-)
2589       t
2590     (not (cdr (gnus-data-find-list article)))))
2591
2592 (defun gnus-make-thread-indent-array ()
2593   (let ((n 200))
2594     (unless (and gnus-thread-indent-array
2595                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2596       (setq gnus-thread-indent-array (make-vector 201 "")
2597             gnus-thread-indent-array-level gnus-thread-indent-level)
2598       (while (>= n 0)
2599         (aset gnus-thread-indent-array n
2600               (make-string (* n gnus-thread-indent-level) ? ))
2601         (setq n (1- n))))))
2602
2603 (defun gnus-update-summary-mark-positions ()
2604   "Compute where the summary marks are to go."
2605   (save-excursion
2606     (when (gnus-buffer-exists-p gnus-summary-buffer)
2607       (set-buffer gnus-summary-buffer))
2608     (let ((gnus-replied-mark 129)
2609           (gnus-score-below-mark 130)
2610           (gnus-score-over-mark 130)
2611           (gnus-download-mark 131)
2612           (spec gnus-summary-line-format-spec)
2613           gnus-visual pos)
2614       (save-excursion
2615         (gnus-set-work-buffer)
2616         (let ((gnus-summary-line-format-spec spec)
2617               (gnus-newsgroup-downloadable '((0 . t))))
2618           (gnus-summary-insert-line
2619            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2620           (goto-char (point-min))
2621           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2622                                              (- (point) 2)))))
2623           (goto-char (point-min))
2624           (push (cons 'replied (and (search-forward "\201" nil t)
2625                                     (- (point) 2)))
2626                 pos)
2627           (goto-char (point-min))
2628           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2629                 pos)
2630           (goto-char (point-min))
2631           (push (cons 'download
2632                       (and (search-forward "\203" nil t) (- (point) 2)))
2633                 pos)))
2634       (setq gnus-summary-mark-positions pos))))
2635
2636 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2637   "Insert a dummy root in the summary buffer."
2638   (beginning-of-line)
2639   (gnus-add-text-properties
2640    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2641    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2642
2643 (defun gnus-summary-from-or-to-or-newsgroups (header)
2644   (let ((to (cdr (assq 'To (mail-header-extra header))))
2645         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2646         (mail-parse-charset gnus-newsgroup-charset)
2647         (mail-parse-ignored-charsets
2648          (save-excursion (set-buffer gnus-summary-buffer)
2649                          gnus-newsgroup-ignored-charsets)))
2650     (cond
2651      ((and to
2652            gnus-ignored-from-addresses
2653            (string-match gnus-ignored-from-addresses
2654                          (mail-header-from header)))
2655       (concat "-> "
2656               (or (car (funcall gnus-extract-address-components
2657                                 (funcall
2658                                  gnus-decode-encoded-word-function to)))
2659                   (funcall gnus-decode-encoded-word-function to))))
2660      ((and newsgroups
2661            gnus-ignored-from-addresses
2662            (string-match gnus-ignored-from-addresses
2663                          (mail-header-from header)))
2664       (concat "=> " newsgroups))
2665      (t
2666       (or (car (funcall gnus-extract-address-components
2667                         (mail-header-from header)))
2668           (mail-header-from header))))))
2669
2670 (defun gnus-summary-insert-line (gnus-tmp-header
2671                                  gnus-tmp-level gnus-tmp-current
2672                                  gnus-tmp-unread gnus-tmp-replied
2673                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2674                                  &optional gnus-tmp-dummy gnus-tmp-score
2675                                  gnus-tmp-process)
2676   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2677          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2678          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2679          (gnus-tmp-score-char
2680           (if (or (null gnus-summary-default-score)
2681                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2682                       gnus-summary-zcore-fuzz))
2683               ?  ;Whitespace
2684             (if (< gnus-tmp-score gnus-summary-default-score)
2685                 gnus-score-below-mark gnus-score-over-mark)))
2686          (gnus-tmp-replied
2687           (cond (gnus-tmp-process gnus-process-mark)
2688                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2689                  gnus-cached-mark)
2690                 (gnus-tmp-replied gnus-replied-mark)
2691                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2692                  gnus-saved-mark)
2693                 (t gnus-unread-mark)))
2694          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2695          (gnus-tmp-name
2696           (cond
2697            ((string-match "<[^>]+> *$" gnus-tmp-from)
2698             (let ((beg (match-beginning 0)))
2699               (or (and (string-match "^\".+\"" gnus-tmp-from)
2700                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2701                   (substring gnus-tmp-from 0 beg))))
2702            ((string-match "(.+)" gnus-tmp-from)
2703             (substring gnus-tmp-from
2704                        (1+ (match-beginning 0)) (1- (match-end 0))))
2705            (t gnus-tmp-from)))
2706          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2707          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2708          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2709          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2710          (buffer-read-only nil))
2711     (when (string= gnus-tmp-name "")
2712       (setq gnus-tmp-name gnus-tmp-from))
2713     (unless (numberp gnus-tmp-lines)
2714       (setq gnus-tmp-lines 0))
2715     (gnus-put-text-property
2716      (point)
2717      (progn (eval gnus-summary-line-format-spec) (point))
2718      'gnus-number gnus-tmp-number)
2719     (when (gnus-visual-p 'summary-highlight 'highlight)
2720       (forward-line -1)
2721       (gnus-run-hooks 'gnus-summary-update-hook)
2722       (forward-line 1))))
2723
2724 (defun gnus-summary-update-line (&optional dont-update)
2725   "Update summary line after change."
2726   (when (and gnus-summary-default-score
2727              (not gnus-summary-inhibit-highlight))
2728     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2729            (article (gnus-summary-article-number))
2730            (score (gnus-summary-article-score article)))
2731       (unless dont-update
2732         (if (and gnus-summary-mark-below
2733                  (< (gnus-summary-article-score)
2734                     gnus-summary-mark-below))
2735             ;; This article has a low score, so we mark it as read.
2736             (when (memq article gnus-newsgroup-unreads)
2737               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2738           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2739             ;; This article was previously marked as read on account
2740             ;; of a low score, but now it has risen, so we mark it as
2741             ;; unread.
2742             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2743         (gnus-summary-update-mark
2744          (if (or (null gnus-summary-default-score)
2745                  (<= (abs (- score gnus-summary-default-score))
2746                      gnus-summary-zcore-fuzz))
2747              ?  ;Whitespace
2748            (if (< score gnus-summary-default-score)
2749                gnus-score-below-mark gnus-score-over-mark))
2750          'score))
2751       ;; Do visual highlighting.
2752       (when (gnus-visual-p 'summary-highlight 'highlight)
2753         (gnus-run-hooks 'gnus-summary-update-hook)))))
2754
2755 (defvar gnus-tmp-new-adopts nil)
2756
2757 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2758   "Return the number of articles in THREAD.
2759 This may be 0 in some cases -- if none of the articles in
2760 the thread are to be displayed."
2761   (let* ((number
2762           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2763           (cond
2764            ((not (listp thread))
2765             1)
2766            ((and (consp thread) (cdr thread))
2767             (apply
2768              '+ 1 (mapcar
2769                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2770            ((null thread)
2771             1)
2772            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2773             1)
2774            (t 0))))
2775     (when (and level (zerop level) gnus-tmp-new-adopts)
2776       (incf number
2777             (apply '+ (mapcar
2778                        'gnus-summary-number-of-articles-in-thread
2779                        gnus-tmp-new-adopts))))
2780     (if char
2781         (if (> number 1) gnus-not-empty-thread-mark
2782           gnus-empty-thread-mark)
2783       number)))
2784
2785 (defun gnus-summary-set-local-parameters (group)
2786   "Go through the local params of GROUP and set all variable specs in that list."
2787   (let ((params (gnus-group-find-parameter group))
2788         elem)
2789     (while params
2790       (setq elem (car params)
2791             params (cdr params))
2792       (and (consp elem)                 ; Has to be a cons.
2793            (consp (cdr elem))           ; The cdr has to be a list.
2794            (symbolp (car elem))         ; Has to be a symbol in there.
2795            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2796            (ignore-errors               ; So we set it.
2797              (make-local-variable (car elem))
2798              (set (car elem) (eval (nth 1 elem))))))))
2799
2800 (defun gnus-summary-read-group (group &optional show-all no-article
2801                                       kill-buffer no-display backward
2802                                       select-articles)
2803   "Start reading news in newsgroup GROUP.
2804 If SHOW-ALL is non-nil, already read articles are also listed.
2805 If NO-ARTICLE is non-nil, no article is selected initially.
2806 If NO-DISPLAY, don't generate a summary buffer."
2807   (let (result)
2808     (while (and group
2809                 (null (setq result
2810                             (let ((gnus-auto-select-next nil))
2811                               (or (gnus-summary-read-group-1
2812                                    group show-all no-article
2813                                    kill-buffer no-display
2814                                    select-articles)
2815                                   (setq show-all nil
2816                                         select-articles nil)))))
2817                 (eq gnus-auto-select-next 'quietly))
2818       (set-buffer gnus-group-buffer)
2819       ;; The entry function called above goes to the next
2820       ;; group automatically, so we go two groups back
2821       ;; if we are searching for the previous group.
2822       (when backward
2823         (gnus-group-prev-unread-group 2))
2824       (if (not (equal group (gnus-group-group-name)))
2825           (setq group (gnus-group-group-name))
2826         (setq group nil)))
2827     result))
2828
2829 (defun gnus-summary-read-group-1 (group show-all no-article
2830                                         kill-buffer no-display
2831                                         &optional select-articles)
2832   ;; Killed foreign groups can't be entered.
2833   (when (and (not (gnus-group-native-p group))
2834              (not (gnus-gethash group gnus-newsrc-hashtb)))
2835     (error "Dead non-native groups can't be entered"))
2836   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2837   (let* ((new-group (gnus-summary-setup-buffer group))
2838          (quit-config (gnus-group-quit-config group))
2839          (did-select (and new-group (gnus-select-newsgroup
2840                                      group show-all select-articles))))
2841     (cond
2842      ;; This summary buffer exists already, so we just select it.
2843      ((not new-group)
2844       (gnus-set-global-variables)
2845       (when kill-buffer
2846         (gnus-kill-or-deaden-summary kill-buffer))
2847       (gnus-configure-windows 'summary 'force)
2848       (gnus-set-mode-line 'summary)
2849       (gnus-summary-position-point)
2850       (message "")
2851       t)
2852      ;; We couldn't select this group.
2853      ((null did-select)
2854       (when (and (eq major-mode 'gnus-summary-mode)
2855                  (not (equal (current-buffer) kill-buffer)))
2856         (kill-buffer (current-buffer))
2857         (if (not quit-config)
2858             (progn
2859               ;; Update the info -- marks might need to be removed,
2860               ;; for instance.
2861               (gnus-summary-update-info)
2862               (set-buffer gnus-group-buffer)
2863               (gnus-group-jump-to-group group)
2864               (gnus-group-next-unread-group 1))
2865           (gnus-handle-ephemeral-exit quit-config)))
2866       (gnus-message 3 "Can't select group")
2867       nil)
2868      ;; The user did a `C-g' while prompting for number of articles,
2869      ;; so we exit this group.
2870      ((eq did-select 'quit)
2871       (and (eq major-mode 'gnus-summary-mode)
2872            (not (equal (current-buffer) kill-buffer))
2873            (kill-buffer (current-buffer)))
2874       (when kill-buffer
2875         (gnus-kill-or-deaden-summary kill-buffer))
2876       (if (not quit-config)
2877           (progn
2878             (set-buffer gnus-group-buffer)
2879             (gnus-group-jump-to-group group)
2880             (gnus-group-next-unread-group 1)
2881             (gnus-configure-windows 'group 'force))
2882         (gnus-handle-ephemeral-exit quit-config))
2883       ;; Finally signal the quit.
2884       (signal 'quit nil))
2885      ;; The group was successfully selected.
2886      (t
2887       (gnus-set-global-variables)
2888       ;; Save the active value in effect when the group was entered.
2889       (setq gnus-newsgroup-active
2890             (gnus-copy-sequence
2891              (gnus-active gnus-newsgroup-name)))
2892       ;; You can change the summary buffer in some way with this hook.
2893       (gnus-run-hooks 'gnus-select-group-hook)
2894       ;; Set any local variables in the group parameters.
2895       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2896       (gnus-update-format-specifications
2897        nil 'summary 'summary-mode 'summary-dummy)
2898       (gnus-update-summary-mark-positions)
2899       ;; Do score processing.
2900       (when gnus-use-scoring
2901         (gnus-possibly-score-headers))
2902       ;; Check whether to fill in the gaps in the threads.
2903       (when gnus-build-sparse-threads
2904         (gnus-build-sparse-threads))
2905       ;; Find the initial limit.
2906       (if gnus-show-threads
2907           (if show-all
2908               (let ((gnus-newsgroup-dormant nil))
2909                 (gnus-summary-initial-limit show-all))
2910             (gnus-summary-initial-limit show-all))
2911         ;; When untreaded, all articles are always shown.
2912         (setq gnus-newsgroup-limit
2913               (mapcar
2914                (lambda (header) (mail-header-number header))
2915                gnus-newsgroup-headers)))
2916       ;; Generate the summary buffer.
2917       (unless no-display
2918         (gnus-summary-prepare))
2919       (when gnus-use-trees
2920         (gnus-tree-open group)
2921         (setq gnus-summary-highlight-line-function
2922               'gnus-tree-highlight-article))
2923       ;; If the summary buffer is empty, but there are some low-scored
2924       ;; articles or some excluded dormants, we include these in the
2925       ;; buffer.
2926       (when (and (zerop (buffer-size))
2927                  (not no-display))
2928         (cond (gnus-newsgroup-dormant
2929                (gnus-summary-limit-include-dormant))
2930               ((and gnus-newsgroup-scored show-all)
2931                (gnus-summary-limit-include-expunged t))))
2932       ;; Function `gnus-apply-kill-file' must be called in this hook.
2933       (gnus-run-hooks 'gnus-apply-kill-hook)
2934       (if (and (zerop (buffer-size))
2935                (not no-display))
2936           (progn
2937             ;; This newsgroup is empty.
2938             (gnus-summary-catchup-and-exit nil t)
2939             (gnus-message 6 "No unread news")
2940             (when kill-buffer
2941               (gnus-kill-or-deaden-summary kill-buffer))
2942             ;; Return nil from this function.
2943             nil)
2944         ;; Hide conversation thread subtrees.  We cannot do this in
2945         ;; gnus-summary-prepare-hook since kill processing may not
2946         ;; work with hidden articles.
2947         (and gnus-show-threads
2948              gnus-thread-hide-subtree
2949              (gnus-summary-hide-all-threads))
2950         (when kill-buffer
2951           (gnus-kill-or-deaden-summary kill-buffer))
2952         ;; Show first unread article if requested.
2953         (if (and (not no-article)
2954                  (not no-display)
2955                  gnus-newsgroup-unreads
2956                  gnus-auto-select-first)
2957             (progn
2958               (gnus-configure-windows 'summary)
2959               (cond
2960                ((eq gnus-auto-select-first 'best)
2961                 (gnus-summary-best-unread-article))
2962                ((eq gnus-auto-select-first t)
2963                 (gnus-summary-first-unread-article))
2964                ((gnus-functionp gnus-auto-select-first)
2965                 (funcall gnus-auto-select-first))))
2966           ;; Don't select any articles, just move point to the first
2967           ;; article in the group.
2968           (goto-char (point-min))
2969           (gnus-summary-position-point)
2970           (gnus-configure-windows 'summary 'force)
2971           (gnus-set-mode-line 'summary))
2972         (when (get-buffer-window gnus-group-buffer t)
2973           ;; Gotta use windows, because recenter does weird stuff if
2974           ;; the current buffer ain't the displayed window.
2975           (let ((owin (selected-window)))
2976             (select-window (get-buffer-window gnus-group-buffer t))
2977             (when (gnus-group-goto-group group)
2978               (recenter))
2979             (select-window owin)))
2980         ;; Mark this buffer as "prepared".
2981         (setq gnus-newsgroup-prepared t)
2982         (gnus-run-hooks 'gnus-summary-prepared-hook)
2983         t)))))
2984
2985 (defun gnus-summary-prepare ()
2986   "Generate the summary buffer."
2987   (interactive)
2988   (let ((buffer-read-only nil))
2989     (erase-buffer)
2990     (setq gnus-newsgroup-data nil
2991           gnus-newsgroup-data-reverse nil)
2992     (gnus-run-hooks 'gnus-summary-generate-hook)
2993     ;; Generate the buffer, either with threads or without.
2994     (when gnus-newsgroup-headers
2995       (gnus-summary-prepare-threads
2996        (if gnus-show-threads
2997            (gnus-sort-gathered-threads
2998             (funcall gnus-summary-thread-gathering-function
2999                      (gnus-sort-threads
3000                       (gnus-cut-threads (gnus-make-threads)))))
3001          ;; Unthreaded display.
3002          (gnus-sort-articles gnus-newsgroup-headers))))
3003     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3004     ;; Call hooks for modifying summary buffer.
3005     (goto-char (point-min))
3006     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3007
3008 (defsubst gnus-general-simplify-subject (subject)
3009   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3010   (setq subject
3011         (cond
3012          ;; Truncate the subject.
3013          (gnus-simplify-subject-functions
3014           (gnus-map-function gnus-simplify-subject-functions subject))
3015          ((numberp gnus-summary-gather-subject-limit)
3016           (setq subject (gnus-simplify-subject-re subject))
3017           (if (> (length subject) gnus-summary-gather-subject-limit)
3018               (substring subject 0 gnus-summary-gather-subject-limit)
3019             subject))
3020          ;; Fuzzily simplify it.
3021          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3022           (gnus-simplify-subject-fuzzy subject))
3023          ;; Just remove the leading "Re:".
3024          (t
3025           (gnus-simplify-subject-re subject))))
3026
3027   (if (and gnus-summary-gather-exclude-subject
3028            (string-match gnus-summary-gather-exclude-subject subject))
3029       nil                               ; This article shouldn't be gathered
3030     subject))
3031
3032 (defun gnus-summary-simplify-subject-query ()
3033   "Query where the respool algorithm would put this article."
3034   (interactive)
3035   (gnus-summary-select-article)
3036   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3037
3038 (defun gnus-gather-threads-by-subject (threads)
3039   "Gather threads by looking at Subject headers."
3040   (if (not gnus-summary-make-false-root)
3041       threads
3042     (let ((hashtb (gnus-make-hashtable 1024))
3043           (prev threads)
3044           (result threads)
3045           subject hthread whole-subject)
3046       (while threads
3047         (setq subject (gnus-general-simplify-subject
3048                        (setq whole-subject (mail-header-subject
3049                                             (caar threads)))))
3050         (when subject
3051           (if (setq hthread (gnus-gethash subject hashtb))
3052               (progn
3053                 ;; We enter a dummy root into the thread, if we
3054                 ;; haven't done that already.
3055                 (unless (stringp (caar hthread))
3056                   (setcar hthread (list whole-subject (car hthread))))
3057                 ;; We add this new gathered thread to this gathered
3058                 ;; thread.
3059                 (setcdr (car hthread)
3060                         (nconc (cdar hthread) (list (car threads))))
3061                 ;; Remove it from the list of threads.
3062                 (setcdr prev (cdr threads))
3063                 (setq threads prev))
3064             ;; Enter this thread into the hash table.
3065             (gnus-sethash subject threads hashtb)))
3066         (setq prev threads)
3067         (setq threads (cdr threads)))
3068       result)))
3069
3070 (defun gnus-gather-threads-by-references (threads)
3071   "Gather threads by looking at References headers."
3072   (let ((idhashtb (gnus-make-hashtable 1024))
3073         (thhashtb (gnus-make-hashtable 1024))
3074         (prev threads)
3075         (result threads)
3076         ids references id gthread gid entered ref)
3077     (while threads
3078       (when (setq references (mail-header-references (caar threads)))
3079         (setq id (mail-header-id (caar threads))
3080               ids (gnus-split-references references)
3081               entered nil)
3082         (while (setq ref (pop ids))
3083           (setq ids (delete ref ids))
3084           (if (not (setq gid (gnus-gethash ref idhashtb)))
3085               (progn
3086                 (gnus-sethash ref id idhashtb)
3087                 (gnus-sethash id threads thhashtb))
3088             (setq gthread (gnus-gethash gid thhashtb))
3089             (unless entered
3090               ;; We enter a dummy root into the thread, if we
3091               ;; haven't done that already.
3092               (unless (stringp (caar gthread))
3093                 (setcar gthread (list (mail-header-subject (caar gthread))
3094                                       (car gthread))))
3095               ;; We add this new gathered thread to this gathered
3096               ;; thread.
3097               (setcdr (car gthread)
3098                       (nconc (cdar gthread) (list (car threads)))))
3099             ;; Add it into the thread hash table.
3100             (gnus-sethash id gthread thhashtb)
3101             (setq entered t)
3102             ;; Remove it from the list of threads.
3103             (setcdr prev (cdr threads))
3104             (setq threads prev))))
3105       (setq prev threads)
3106       (setq threads (cdr threads)))
3107     result))
3108
3109 (defun gnus-sort-gathered-threads (threads)
3110   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3111   (let ((result threads))
3112     (while threads
3113       (when (stringp (caar threads))
3114         (setcdr (car threads)
3115                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3116       (setq threads (cdr threads)))
3117     result))
3118
3119 (defun gnus-thread-loop-p (root thread)
3120   "Say whether ROOT is in THREAD."
3121   (let ((stack (list thread))
3122         (infloop 0)
3123         th)
3124     (while (setq thread (pop stack))
3125       (setq th (cdr thread))
3126       (while (and th
3127                   (not (eq (caar th) root)))
3128         (pop th))
3129       (if th
3130           ;; We have found a loop.
3131           (let (ref-dep)
3132             (setcdr thread (delq (car th) (cdr thread)))
3133             (if (boundp (setq ref-dep (intern "none"
3134                                               gnus-newsgroup-dependencies)))
3135                 (setcdr (symbol-value ref-dep)
3136                         (nconc (cdr (symbol-value ref-dep))
3137                                (list (car th))))
3138               (set ref-dep (list nil (car th))))
3139             (setq infloop 1
3140                   stack nil))
3141         ;; Push all the subthreads onto the stack.
3142         (push (cdr thread) stack)))
3143     infloop))
3144
3145 (defun gnus-make-threads ()
3146   "Go through the dependency hashtb and find the roots.  Return all threads."
3147   (let (threads)
3148     (while (catch 'infloop
3149              (mapatoms
3150               (lambda (refs)
3151                 ;; Deal with self-referencing References loops.
3152                 (when (and (car (symbol-value refs))
3153                            (not (zerop
3154                                  (apply
3155                                   '+
3156                                   (mapcar
3157                                    (lambda (thread)
3158                                      (gnus-thread-loop-p
3159                                       (car (symbol-value refs)) thread))
3160                                    (cdr (symbol-value refs)))))))
3161                   (setq threads nil)
3162                   (throw 'infloop t))
3163                 (unless (car (symbol-value refs))
3164                   ;; These threads do not refer back to any other articles,
3165                   ;; so they're roots.
3166                   (setq threads (append (cdr (symbol-value refs)) threads))))
3167               gnus-newsgroup-dependencies)))
3168     threads))
3169
3170 ;; Build the thread tree.
3171 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3172   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3173
3174 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3175 if it was already present.
3176
3177 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3178 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3179 Message-IDs will be renamed be renamed to a unique Message-ID before
3180 being entered.
3181
3182 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3183   (let* ((id (mail-header-id header))
3184          (id-dep (and id (intern id dependencies)))
3185          ref ref-dep ref-header)
3186     ;; Enter this `header' in the `dependencies' table.
3187     (cond
3188      ((not id-dep)
3189       (setq header nil))
3190      ;; The first two cases do the normal part: enter a new `header'
3191      ;; in the `dependencies' table.
3192      ((not (boundp id-dep))
3193       (set id-dep (list header)))
3194      ((null (car (symbol-value id-dep)))
3195       (setcar (symbol-value id-dep) header))
3196
3197      ;; From here the `header' was already present in the
3198      ;; `dependencies' table.
3199      (force-new
3200       ;; Overrides an existing entry;
3201       ;; just set the header part of the entry.
3202       (setcar (symbol-value id-dep) header))
3203
3204      ;; Renames the existing `header' to a unique Message-ID.
3205      ((not gnus-summary-ignore-duplicates)
3206       ;; An article with this Message-ID has already been seen.
3207       ;; We rename the Message-ID.
3208       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3209            (list header))
3210       (mail-header-set-id header id))
3211
3212      ;; The last case ignores an existing entry, except it adds any
3213      ;; additional Xrefs (in case the two articles came from different
3214      ;; servers.
3215      ;; Also sets `header' to `nil' meaning that the `dependencies'
3216      ;; table was *not* modified.
3217      (t
3218       (mail-header-set-xref
3219        (car (symbol-value id-dep))
3220        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3221                    "")
3222                (or (mail-header-xref header) "")))
3223       (setq header nil)))
3224
3225     (when header
3226       ;; First check if that we are not creating a References loop.
3227       (setq ref (gnus-parent-id (mail-header-references header)))
3228       (while (and ref
3229                   (setq ref-dep (intern-soft ref dependencies))
3230                   (boundp ref-dep)
3231                   (setq ref-header (car (symbol-value ref-dep))))
3232         (if (string= id ref)
3233             ;; Yuk!  This is a reference loop.  Make the article be a
3234             ;; root article.
3235             (progn
3236               (mail-header-set-references (car (symbol-value id-dep)) "none")
3237               (setq ref nil))
3238           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3239       (setq ref (gnus-parent-id (mail-header-references header)))
3240       (setq ref-dep (intern (or ref "none") dependencies))
3241       (if (boundp ref-dep)
3242           (setcdr (symbol-value ref-dep)
3243                   (nconc (cdr (symbol-value ref-dep))
3244                          (list (symbol-value id-dep))))
3245         (set ref-dep (list nil (symbol-value id-dep)))))
3246     header))
3247
3248 (defun gnus-build-sparse-threads ()
3249   (let ((headers gnus-newsgroup-headers)
3250         (mail-parse-charset gnus-newsgroup-charset)
3251         (gnus-summary-ignore-duplicates t)
3252         header references generation relations
3253         subject child end new-child date)
3254     ;; First we create an alist of generations/relations, where
3255     ;; generations is how much we trust the relation, and the relation
3256     ;; is parent/child.
3257     (gnus-message 7 "Making sparse threads...")
3258     (save-excursion
3259       (nnheader-set-temp-buffer " *gnus sparse threads*")
3260       (while (setq header (pop headers))
3261         (when (and (setq references (mail-header-references header))
3262                    (not (string= references "")))
3263           (insert references)
3264           (setq child (mail-header-id header)
3265                 subject (mail-header-subject header)
3266                 date (mail-header-date header)
3267                 generation 0)
3268           (while (search-backward ">" nil t)
3269             (setq end (1+ (point)))
3270             (when (search-backward "<" nil t)
3271               (setq new-child (buffer-substring (point) end))
3272               (push (list (incf generation)
3273                           child (setq child new-child)
3274                           subject date)
3275                     relations)))
3276           (when child
3277             (push (list (1+ generation) child nil subject) relations))
3278           (erase-buffer)))
3279       (kill-buffer (current-buffer)))
3280     ;; Sort over trustworthiness.
3281     (mapcar
3282      (lambda (relation)
3283        (when (gnus-dependencies-add-header
3284               (make-full-mail-header
3285                gnus-reffed-article-number
3286                (nth 3 relation) "" (or (nth 4 relation) "")
3287                (nth 1 relation)
3288                (or (nth 2 relation) "") 0 0 "")
3289               gnus-newsgroup-dependencies nil)
3290          (push gnus-reffed-article-number gnus-newsgroup-limit)
3291          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3292          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3293                gnus-newsgroup-reads)
3294          (decf gnus-reffed-article-number)))
3295      (sort relations 'car-less-than-car))
3296     (gnus-message 7 "Making sparse threads...done")))
3297
3298 (defun gnus-build-old-threads ()
3299   ;; Look at all the articles that refer back to old articles, and
3300   ;; fetch the headers for the articles that aren't there.  This will
3301   ;; build complete threads - if the roots haven't been expired by the
3302   ;; server, that is.
3303   (let ((mail-parse-charset gnus-newsgroup-charset)
3304         id heads)
3305     (mapatoms
3306      (lambda (refs)
3307        (when (not (car (symbol-value refs)))
3308          (setq heads (cdr (symbol-value refs)))
3309          (while heads
3310            (if (memq (mail-header-number (caar heads))
3311                      gnus-newsgroup-dormant)
3312                (setq heads (cdr heads))
3313              (setq id (symbol-name refs))
3314              (while (and (setq id (gnus-build-get-header id))
3315                          (not (car (gnus-id-to-thread id)))))
3316              (setq heads nil)))))
3317      gnus-newsgroup-dependencies)))
3318
3319 ;; This function has to be called with point after the article number
3320 ;; on the beginning of the line.
3321 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3322   (let ((eol (gnus-point-at-eol))
3323         (buffer (current-buffer))
3324         header)
3325
3326     ;; overview: [num subject from date id refs chars lines misc]
3327     (unwind-protect
3328         (progn
3329           (narrow-to-region (point) eol)
3330           (unless (eobp)
3331             (forward-char))
3332
3333           (setq header
3334                 (make-full-mail-header
3335                  number                 ; number
3336                  (funcall gnus-decode-encoded-word-function
3337                           (nnheader-nov-field)) ; subject
3338                  (funcall gnus-decode-encoded-word-function
3339                           (nnheader-nov-field)) ; from
3340                  (nnheader-nov-field)   ; date
3341                  (nnheader-nov-read-message-id) ; id
3342                  (nnheader-nov-field)   ; refs
3343                  (nnheader-nov-read-integer) ; chars
3344                  (nnheader-nov-read-integer) ; lines
3345                  (unless (eobp)
3346                    (if (looking-at "Xref: ")
3347                        (goto-char (match-end 0)))
3348                    (nnheader-nov-field)) ; Xref
3349                  (nnheader-nov-parse-extra)))) ; extra
3350
3351       (widen))
3352
3353     (when gnus-alter-header-function
3354       (funcall gnus-alter-header-function header))
3355     (gnus-dependencies-add-header header dependencies force-new)))
3356
3357 (defun gnus-build-get-header (id)
3358   "Look through the buffer of NOV lines and find the header to ID.
3359 Enter this line into the dependencies hash table, and return
3360 the id of the parent article (if any)."
3361   (let ((deps gnus-newsgroup-dependencies)
3362         found header)
3363     (prog1
3364         (save-excursion
3365           (set-buffer nntp-server-buffer)
3366           (let ((case-fold-search nil))
3367             (goto-char (point-min))
3368             (while (and (not found)
3369                         (search-forward id nil t))
3370               (beginning-of-line)
3371               (setq found (looking-at
3372                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3373                                    (regexp-quote id))))
3374               (or found (beginning-of-line 2)))
3375             (when found
3376               (beginning-of-line)
3377               (and
3378                (setq header (gnus-nov-parse-line
3379                              (read (current-buffer)) deps))
3380                (gnus-parent-id (mail-header-references header))))))
3381       (when header
3382         (let ((number (mail-header-number header)))
3383           (push number gnus-newsgroup-limit)
3384           (push header gnus-newsgroup-headers)
3385           (if (memq number gnus-newsgroup-unselected)
3386               (progn
3387                 (push number gnus-newsgroup-unreads)
3388                 (setq gnus-newsgroup-unselected
3389                       (delq number gnus-newsgroup-unselected)))
3390             (push number gnus-newsgroup-ancient)))))))
3391
3392 (defun gnus-build-all-threads ()
3393   "Read all the headers."
3394   (let ((gnus-summary-ignore-duplicates t)
3395         (mail-parse-charset gnus-newsgroup-charset)
3396         (dependencies gnus-newsgroup-dependencies)
3397         header article)
3398     (save-excursion
3399       (set-buffer nntp-server-buffer)
3400       (let ((case-fold-search nil))
3401         (goto-char (point-min))
3402         (while (not (eobp))
3403           (ignore-errors
3404             (setq article (read (current-buffer))
3405                   header (gnus-nov-parse-line article dependencies)))
3406           (when header
3407             (save-excursion
3408               (set-buffer gnus-summary-buffer)
3409               (push header gnus-newsgroup-headers)
3410               (if (memq (setq article (mail-header-number header))
3411                         gnus-newsgroup-unselected)
3412                   (progn
3413                     (push article gnus-newsgroup-unreads)
3414                     (setq gnus-newsgroup-unselected
3415                           (delq article gnus-newsgroup-unselected)))
3416                 (push article gnus-newsgroup-ancient)))
3417             (forward-line 1)))))))
3418
3419 (defun gnus-summary-update-article-line (article header)
3420   "Update the line for ARTICLE using HEADERS."
3421   (let* ((id (mail-header-id header))
3422          (thread (gnus-id-to-thread id)))
3423     (unless thread
3424       (error "Article in no thread"))
3425     ;; Update the thread.
3426     (setcar thread header)
3427     (gnus-summary-goto-subject article)
3428     (let* ((datal (gnus-data-find-list article))
3429            (data (car datal))
3430            (length (when (cdr datal)
3431                      (- (gnus-data-pos data)
3432                         (gnus-data-pos (cadr datal)))))
3433            (buffer-read-only nil)
3434            (level (gnus-summary-thread-level)))
3435       (gnus-delete-line)
3436       (gnus-summary-insert-line
3437        header level nil (gnus-article-mark article)
3438        (memq article gnus-newsgroup-replied)
3439        (memq article gnus-newsgroup-expirable)
3440        ;; Only insert the Subject string when it's different
3441        ;; from the previous Subject string.
3442        (if (and
3443             gnus-show-threads
3444             (gnus-subject-equal
3445              (condition-case ()
3446                  (mail-header-subject
3447                   (gnus-data-header
3448                    (cadr
3449                     (gnus-data-find-list
3450                      article
3451                      (gnus-data-list t)))))
3452                ;; Error on the side of excessive subjects.
3453                (error ""))
3454              (mail-header-subject header)))
3455            ""
3456          (mail-header-subject header))
3457        nil (cdr (assq article gnus-newsgroup-scored))
3458        (memq article gnus-newsgroup-processable))
3459       (when length
3460         (gnus-data-update-list
3461          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3462
3463 (defun gnus-summary-update-article (article &optional iheader)
3464   "Update ARTICLE in the summary buffer."
3465   (set-buffer gnus-summary-buffer)
3466   (let* ((header (gnus-summary-article-header article))
3467          (id (mail-header-id header))
3468          (data (gnus-data-find article))
3469          (thread (gnus-id-to-thread id))
3470          (references (mail-header-references header))
3471          (parent
3472           (gnus-id-to-thread
3473            (or (gnus-parent-id
3474                 (when (and references
3475                            (not (equal "" references)))
3476                   references))
3477                "none")))
3478          (buffer-read-only nil)
3479          (old (car thread)))
3480     (when thread
3481       (unless iheader
3482         (setcar thread nil)
3483         (when parent
3484           (delq thread parent)))
3485       (if (gnus-summary-insert-subject id header)
3486           ;; Set the (possibly) new article number in the data structure.
3487           (gnus-data-set-number data (gnus-id-to-article id))
3488         (setcar thread old)
3489         nil))))
3490
3491 (defun gnus-rebuild-thread (id &optional line)
3492   "Rebuild the thread containing ID.
3493 If LINE, insert the rebuilt thread starting on line LINE."
3494   (let ((buffer-read-only nil)
3495         old-pos current thread data)
3496     (if (not gnus-show-threads)
3497         (setq thread (list (car (gnus-id-to-thread id))))
3498       ;; Get the thread this article is part of.
3499       (setq thread (gnus-remove-thread id)))
3500     (setq old-pos (gnus-point-at-bol))
3501     (setq current (save-excursion
3502                     (and (zerop (forward-line -1))
3503                          (gnus-summary-article-number))))
3504     ;; If this is a gathered thread, we have to go some re-gathering.
3505     (when (stringp (car thread))
3506       (let ((subject (car thread))
3507             roots thr)
3508         (setq thread (cdr thread))
3509         (while thread
3510           (unless (memq (setq thr (gnus-id-to-thread
3511                                    (gnus-root-id
3512                                     (mail-header-id (caar thread)))))
3513                         roots)
3514             (push thr roots))
3515           (setq thread (cdr thread)))
3516         ;; We now have all (unique) roots.
3517         (if (= (length roots) 1)
3518             ;; All the loose roots are now one solid root.
3519             (setq thread (car roots))
3520           (setq thread (cons subject (gnus-sort-threads roots))))))
3521     (let (threads)
3522       ;; We then insert this thread into the summary buffer.
3523       (when line
3524         (goto-char (point-min))
3525         (forward-line (1- line)))
3526       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3527         (if gnus-show-threads
3528             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3529           (gnus-summary-prepare-unthreaded thread))
3530         (setq data (nreverse gnus-newsgroup-data))
3531         (setq threads gnus-newsgroup-threads))
3532       ;; We splice the new data into the data structure.
3533       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3534       ;;!!! then we want to insert at the beginning of the buffer.
3535       ;;!!! That happens to be true with Gnus now, but that may
3536       ;;!!! change in the future.  Perhaps.
3537       (gnus-data-enter-list
3538        (if line nil current) data (- (point) old-pos))
3539       (setq gnus-newsgroup-threads
3540             (nconc threads gnus-newsgroup-threads))
3541       (gnus-data-compute-positions))))
3542
3543 (defun gnus-number-to-header (number)
3544   "Return the header for article NUMBER."
3545   (let ((headers gnus-newsgroup-headers))
3546     (while (and headers
3547                 (not (= number (mail-header-number (car headers)))))
3548       (pop headers))
3549     (when headers
3550       (car headers))))
3551
3552 (defun gnus-parent-headers (in-headers &optional generation)
3553   "Return the headers of the GENERATIONeth parent of HEADERS."
3554   (unless generation
3555     (setq generation 1))
3556   (let ((parent t)
3557         (headers in-headers)
3558         references)
3559     (while (and parent
3560                 (not (zerop generation))
3561                 (setq references (mail-header-references headers)))
3562       (setq headers (if (and references
3563                              (setq parent (gnus-parent-id references)))
3564                         (car (gnus-id-to-thread parent))
3565                       nil))
3566       (decf generation))
3567     (and (not (eq headers in-headers))
3568          headers)))
3569
3570 (defun gnus-id-to-thread (id)
3571   "Return the (sub-)thread where ID appears."
3572   (gnus-gethash id gnus-newsgroup-dependencies))
3573
3574 (defun gnus-id-to-article (id)
3575   "Return the article number of ID."
3576   (let ((thread (gnus-id-to-thread id)))
3577     (when (and thread
3578                (car thread))
3579       (mail-header-number (car thread)))))
3580
3581 (defun gnus-id-to-header (id)
3582   "Return the article headers of ID."
3583   (car (gnus-id-to-thread id)))
3584
3585 (defun gnus-article-displayed-root-p (article)
3586   "Say whether ARTICLE is a root(ish) article."
3587   (let ((level (gnus-summary-thread-level article))
3588         (refs (mail-header-references  (gnus-summary-article-header article)))
3589         particle)
3590     (cond
3591      ((null level) nil)
3592      ((zerop level) t)
3593      ((null refs) t)
3594      ((null (gnus-parent-id refs)) t)
3595      ((and (= 1 level)
3596            (null (setq particle (gnus-id-to-article
3597                                  (gnus-parent-id refs))))
3598            (null (gnus-summary-thread-level particle)))))))
3599
3600 (defun gnus-root-id (id)
3601   "Return the id of the root of the thread where ID appears."
3602   (let (last-id prev)
3603     (while (and id (setq prev (car (gnus-id-to-thread id))))
3604       (setq last-id id
3605             id (gnus-parent-id (mail-header-references prev))))
3606     last-id))
3607
3608 (defun gnus-articles-in-thread (thread)
3609   "Return the list of articles in THREAD."
3610   (cons (mail-header-number (car thread))
3611         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3612
3613 (defun gnus-remove-thread (id &optional dont-remove)
3614   "Remove the thread that has ID in it."
3615   (let (headers thread last-id)
3616     ;; First go up in this thread until we find the root.
3617     (setq last-id (gnus-root-id id)
3618           headers (message-flatten-list (gnus-id-to-thread last-id)))
3619     ;; We have now found the real root of this thread.  It might have
3620     ;; been gathered into some loose thread, so we have to search
3621     ;; through the threads to find the thread we wanted.
3622     (let ((threads gnus-newsgroup-threads)
3623           sub)
3624       (while threads
3625         (setq sub (car threads))
3626         (if (stringp (car sub))
3627             ;; This is a gathered thread, so we look at the roots
3628             ;; below it to find whether this article is in this
3629             ;; gathered root.
3630             (progn
3631               (setq sub (cdr sub))
3632               (while sub
3633                 (when (member (caar sub) headers)
3634                   (setq thread (car threads)
3635                         threads nil
3636                         sub nil))
3637                 (setq sub (cdr sub))))
3638           ;; It's an ordinary thread, so we check it.
3639           (when (eq (car sub) (car headers))
3640             (setq thread sub
3641                   threads nil)))
3642         (setq threads (cdr threads)))
3643       ;; If this article is in no thread, then it's a root.
3644       (if thread
3645           (unless dont-remove
3646             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3647         (setq thread (gnus-id-to-thread last-id)))
3648       (when thread
3649         (prog1
3650             thread                      ; We return this thread.
3651           (unless dont-remove
3652             (if (stringp (car thread))
3653                 (progn
3654                   ;; If we use dummy roots, then we have to remove the
3655                   ;; dummy root as well.
3656                   (when (eq gnus-summary-make-false-root 'dummy)
3657                     ;; We go to the dummy root by going to
3658                     ;; the first sub-"thread", and then one line up.
3659                     (gnus-summary-goto-article
3660                      (mail-header-number (caadr thread)))
3661                     (forward-line -1)
3662                     (gnus-delete-line)
3663                     (gnus-data-compute-positions))
3664                   (setq thread (cdr thread))
3665                   (while thread
3666                     (gnus-remove-thread-1 (car thread))
3667                     (setq thread (cdr thread))))
3668               (gnus-remove-thread-1 thread))))))))
3669
3670 (defun gnus-remove-thread-1 (thread)
3671   "Remove the thread THREAD recursively."
3672   (let ((number (mail-header-number (pop thread)))
3673         d)
3674     (setq thread (reverse thread))
3675     (while thread
3676       (gnus-remove-thread-1 (pop thread)))
3677     (when (setq d (gnus-data-find number))
3678       (goto-char (gnus-data-pos d))
3679       (gnus-summary-show-thread)
3680       (gnus-data-remove
3681        number
3682        (- (gnus-point-at-bol)
3683           (prog1
3684               (1+ (gnus-point-at-eol))
3685             (gnus-delete-line)))))))
3686
3687 (defun gnus-sort-threads-1 (threads func)
3688   (sort (mapcar (lambda (thread)
3689                   (cons (car thread)
3690                         (and (cdr thread)
3691                              (gnus-sort-threads-1 (cdr thread) func))))
3692                 threads) func))
3693
3694 (defun gnus-sort-threads (threads)
3695   "Sort THREADS."
3696   (if (not gnus-thread-sort-functions)
3697       threads
3698     (gnus-message 8 "Sorting threads...")
3699     (prog1
3700         (gnus-sort-threads-1 
3701          threads 
3702          (gnus-make-sort-function gnus-thread-sort-functions))
3703       (gnus-message 8 "Sorting threads...done"))))
3704
3705 (defun gnus-sort-articles (articles)
3706   "Sort ARTICLES."
3707   (when gnus-article-sort-functions
3708     (gnus-message 7 "Sorting articles...")
3709     (prog1
3710         (setq gnus-newsgroup-headers
3711               (sort articles (gnus-make-sort-function
3712                               gnus-article-sort-functions)))
3713       (gnus-message 7 "Sorting articles...done"))))
3714
3715 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3716 (defmacro gnus-thread-header (thread)
3717   "Return header of first article in THREAD.
3718 Note that THREAD must never, ever be anything else than a variable -
3719 using some other form will lead to serious barfage."
3720   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3721   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3722   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3723         (vector thread) 2))
3724
3725 (defsubst gnus-article-sort-by-number (h1 h2)
3726   "Sort articles by article number."
3727   (< (mail-header-number h1)
3728      (mail-header-number h2)))
3729
3730 (defun gnus-thread-sort-by-number (h1 h2)
3731   "Sort threads by root article number."
3732   (gnus-article-sort-by-number
3733    (gnus-thread-header h1) (gnus-thread-header h2)))
3734
3735 (defsubst gnus-article-sort-by-lines (h1 h2)
3736   "Sort articles by article Lines header."
3737   (< (mail-header-lines h1)
3738      (mail-header-lines h2)))
3739
3740 (defun gnus-thread-sort-by-lines (h1 h2)
3741   "Sort threads by root article Lines header."
3742   (gnus-article-sort-by-lines
3743    (gnus-thread-header h1) (gnus-thread-header h2)))
3744
3745 (defsubst gnus-article-sort-by-chars (h1 h2)
3746   "Sort articles by octet length."
3747   (< (mail-header-chars h1)
3748      (mail-header-chars h2)))
3749
3750 (defun gnus-thread-sort-by-chars (h1 h2)
3751   "Sort threads by root article octet length."
3752   (gnus-article-sort-by-chars
3753    (gnus-thread-header h1) (gnus-thread-header h2)))
3754
3755 (defsubst gnus-article-sort-by-author (h1 h2)
3756   "Sort articles by root author."
3757   (string-lessp
3758    (let ((extract (funcall
3759                    gnus-extract-address-components
3760                    (mail-header-from h1))))
3761      (or (car extract) (cadr extract) ""))
3762    (let ((extract (funcall
3763                    gnus-extract-address-components
3764                    (mail-header-from h2))))
3765      (or (car extract) (cadr extract) ""))))
3766
3767 (defun gnus-thread-sort-by-author (h1 h2)
3768   "Sort threads by root author."
3769   (gnus-article-sort-by-author
3770    (gnus-thread-header h1)  (gnus-thread-header h2)))
3771
3772 (defsubst gnus-article-sort-by-subject (h1 h2)
3773   "Sort articles by root subject."
3774   (string-lessp
3775    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3776    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3777
3778 (defun gnus-thread-sort-by-subject (h1 h2)
3779   "Sort threads by root subject."
3780   (gnus-article-sort-by-subject
3781    (gnus-thread-header h1) (gnus-thread-header h2)))
3782
3783 (defsubst gnus-article-sort-by-date (h1 h2)
3784   "Sort articles by root article date."
3785   (time-less-p
3786    (gnus-date-get-time (mail-header-date h1))
3787    (gnus-date-get-time (mail-header-date h2))))
3788
3789 (defun gnus-thread-sort-by-date (h1 h2)
3790   "Sort threads by root article date."
3791   (gnus-article-sort-by-date
3792    (gnus-thread-header h1) (gnus-thread-header h2)))
3793
3794 (defsubst gnus-article-sort-by-score (h1 h2)
3795   "Sort articles by root article score.
3796 Unscored articles will be counted as having a score of zero."
3797   (> (or (cdr (assq (mail-header-number h1)
3798                     gnus-newsgroup-scored))
3799          gnus-summary-default-score 0)
3800      (or (cdr (assq (mail-header-number h2)
3801                     gnus-newsgroup-scored))
3802          gnus-summary-default-score 0)))
3803
3804 (defun gnus-thread-sort-by-score (h1 h2)
3805   "Sort threads by root article score."
3806   (gnus-article-sort-by-score
3807    (gnus-thread-header h1) (gnus-thread-header h2)))
3808
3809 (defun gnus-thread-sort-by-total-score (h1 h2)
3810   "Sort threads by the sum of all scores in the thread.
3811 Unscored articles will be counted as having a score of zero."
3812   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3813
3814 (defun gnus-thread-total-score (thread)
3815   ;; This function find the total score of THREAD.
3816   (cond ((null thread)
3817          0)
3818         ((consp thread)
3819          (if (stringp (car thread))
3820              (apply gnus-thread-score-function 0
3821                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3822            (gnus-thread-total-score-1 thread)))
3823         (t
3824          (gnus-thread-total-score-1 (list thread)))))
3825
3826 (defun gnus-thread-total-score-1 (root)
3827   ;; This function find the total score of the thread below ROOT.
3828   (setq root (car root))
3829   (apply gnus-thread-score-function
3830          (or (append
3831               (mapcar 'gnus-thread-total-score
3832                       (cdr (gnus-id-to-thread (mail-header-id root))))
3833               (when (> (mail-header-number root) 0)
3834                 (list (or (cdr (assq (mail-header-number root)
3835                                      gnus-newsgroup-scored))
3836                           gnus-summary-default-score 0))))
3837              (list gnus-summary-default-score)
3838              '(0))))
3839
3840 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3841 (defvar gnus-tmp-prev-subject nil)
3842 (defvar gnus-tmp-false-parent nil)
3843 (defvar gnus-tmp-root-expunged nil)
3844 (defvar gnus-tmp-dummy-line nil)
3845
3846 (defvar gnus-tmp-header)
3847 (defun gnus-extra-header (type &optional header)
3848   "Return the extra header of TYPE."
3849   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3850       ""))
3851
3852 (defun gnus-summary-prepare-threads (threads)
3853   "Prepare summary buffer from THREADS and indentation LEVEL.
3854 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3855 or a straight list of headers."
3856   (gnus-message 7 "Generating summary...")
3857
3858   (setq gnus-newsgroup-threads threads)
3859   (beginning-of-line)
3860
3861   (let ((gnus-tmp-level 0)
3862         (default-score (or gnus-summary-default-score 0))
3863         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3864         thread number subject stack state gnus-tmp-gathered beg-match
3865         new-roots gnus-tmp-new-adopts thread-end
3866         gnus-tmp-header gnus-tmp-unread
3867         gnus-tmp-replied gnus-tmp-subject-or-nil
3868         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3869         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3870         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3871
3872     (setq gnus-tmp-prev-subject nil)
3873
3874     (if (vectorp (car threads))
3875         ;; If this is a straight (sic) list of headers, then a
3876         ;; threaded summary display isn't required, so we just create
3877         ;; an unthreaded one.
3878         (gnus-summary-prepare-unthreaded threads)
3879
3880       ;; Do the threaded display.
3881
3882       (while (or threads stack gnus-tmp-new-adopts new-roots)
3883
3884         (if (and (= gnus-tmp-level 0)
3885                  (or (not stack)
3886                      (= (caar stack) 0))
3887                  (not gnus-tmp-false-parent)
3888                  (or gnus-tmp-new-adopts new-roots))
3889             (if gnus-tmp-new-adopts
3890                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3891                       thread (list (car gnus-tmp-new-adopts))
3892                       gnus-tmp-header (caar thread)
3893                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3894               (when new-roots
3895                 (setq thread (list (car new-roots))
3896                       gnus-tmp-header (caar thread)
3897                       new-roots (cdr new-roots))))
3898
3899           (if threads
3900               ;; If there are some threads, we do them before the
3901               ;; threads on the stack.
3902               (setq thread threads
3903                     gnus-tmp-header (caar thread))
3904             ;; There were no current threads, so we pop something off
3905             ;; the stack.
3906             (setq state (car stack)
3907                   gnus-tmp-level (car state)
3908                   thread (cdr state)
3909                   stack (cdr stack)
3910                   gnus-tmp-header (caar thread))))
3911
3912         (setq gnus-tmp-false-parent nil)
3913         (setq gnus-tmp-root-expunged nil)
3914         (setq thread-end nil)
3915
3916         (if (stringp gnus-tmp-header)
3917             ;; The header is a dummy root.
3918             (cond
3919              ((eq gnus-summary-make-false-root 'adopt)
3920               ;; We let the first article adopt the rest.
3921               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3922                                                (cddar thread)))
3923               (setq gnus-tmp-gathered
3924                     (nconc (mapcar
3925                             (lambda (h) (mail-header-number (car h)))
3926                             (cddar thread))
3927                            gnus-tmp-gathered))
3928               (setq thread (cons (list (caar thread)
3929                                        (cadar thread))
3930                                  (cdr thread)))
3931               (setq gnus-tmp-level -1
3932                     gnus-tmp-false-parent t))
3933              ((eq gnus-summary-make-false-root 'empty)
3934               ;; We print adopted articles with empty subject fields.
3935               (setq gnus-tmp-gathered
3936                     (nconc (mapcar
3937                             (lambda (h) (mail-header-number (car h)))
3938                             (cddar thread))
3939                            gnus-tmp-gathered))
3940               (setq gnus-tmp-level -1))
3941              ((eq gnus-summary-make-false-root 'dummy)
3942               ;; We remember that we probably want to output a dummy
3943               ;; root.
3944               (setq gnus-tmp-dummy-line gnus-tmp-header)
3945               (setq gnus-tmp-prev-subject gnus-tmp-header))
3946              (t
3947               ;; We do not make a root for the gathered
3948               ;; sub-threads at all.
3949               (setq gnus-tmp-level -1)))
3950
3951           (setq number (mail-header-number gnus-tmp-header)
3952                 subject (mail-header-subject gnus-tmp-header))
3953
3954           (cond
3955            ;; If the thread has changed subject, we might want to make
3956            ;; this subthread into a root.
3957            ((and (null gnus-thread-ignore-subject)
3958                  (not (zerop gnus-tmp-level))
3959                  gnus-tmp-prev-subject
3960                  (not (inline
3961                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3962             (setq new-roots (nconc new-roots (list (car thread)))
3963                   thread-end t
3964                   gnus-tmp-header nil))
3965            ;; If the article lies outside the current limit,
3966            ;; then we do not display it.
3967            ((not (memq number gnus-newsgroup-limit))
3968             (setq gnus-tmp-gathered
3969                   (nconc (mapcar
3970                           (lambda (h) (mail-header-number (car h)))
3971                           (cdar thread))
3972                          gnus-tmp-gathered))
3973             (setq gnus-tmp-new-adopts (if (cdar thread)
3974                                           (append gnus-tmp-new-adopts
3975                                                   (cdar thread))
3976                                         gnus-tmp-new-adopts)
3977                   thread-end t
3978                   gnus-tmp-header nil)
3979             (when (zerop gnus-tmp-level)
3980               (setq gnus-tmp-root-expunged t)))
3981            ;; Perhaps this article is to be marked as read?
3982            ((and gnus-summary-mark-below
3983                  (< (or (cdr (assq number gnus-newsgroup-scored))
3984                         default-score)
3985                     gnus-summary-mark-below)
3986                  ;; Don't touch sparse articles.
3987                  (not (gnus-summary-article-sparse-p number))
3988                  (not (gnus-summary-article-ancient-p number)))
3989             (setq gnus-newsgroup-unreads
3990                   (delq number gnus-newsgroup-unreads))
3991             (if gnus-newsgroup-auto-expire
3992                 (push number gnus-newsgroup-expirable)
3993               (push (cons number gnus-low-score-mark)
3994                     gnus-newsgroup-reads))))
3995
3996           (when gnus-tmp-header
3997             ;; We may have an old dummy line to output before this
3998             ;; article.
3999             (when (and gnus-tmp-dummy-line
4000                        (gnus-subject-equal
4001                         gnus-tmp-dummy-line
4002                         (mail-header-subject gnus-tmp-header)))
4003               (gnus-summary-insert-dummy-line
4004                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4005               (setq gnus-tmp-dummy-line nil))
4006
4007             ;; Compute the mark.
4008             (setq gnus-tmp-unread (gnus-article-mark number))
4009
4010             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4011                                   gnus-tmp-header gnus-tmp-level)
4012                   gnus-newsgroup-data)
4013
4014             ;; Actually insert the line.
4015             (setq
4016              gnus-tmp-subject-or-nil
4017              (cond
4018               ((and gnus-thread-ignore-subject
4019                     gnus-tmp-prev-subject
4020                     (not (inline (gnus-subject-equal
4021                                   gnus-tmp-prev-subject subject))))
4022                subject)
4023               ((zerop gnus-tmp-level)
4024                (if (and (eq gnus-summary-make-false-root 'empty)
4025                         (memq number gnus-tmp-gathered)
4026                         gnus-tmp-prev-subject
4027                         (inline (gnus-subject-equal
4028                                  gnus-tmp-prev-subject subject)))
4029                    gnus-summary-same-subject
4030                  subject))
4031               (t gnus-summary-same-subject)))
4032             (if (and (eq gnus-summary-make-false-root 'adopt)
4033                      (= gnus-tmp-level 1)
4034                      (memq number gnus-tmp-gathered))
4035                 (setq gnus-tmp-opening-bracket ?\<
4036                       gnus-tmp-closing-bracket ?\>)
4037               (setq gnus-tmp-opening-bracket ?\[
4038                     gnus-tmp-closing-bracket ?\]))
4039             (setq
4040              gnus-tmp-indentation
4041              (aref gnus-thread-indent-array gnus-tmp-level)
4042              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4043              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4044                                 gnus-summary-default-score 0)
4045              gnus-tmp-score-char
4046              (if (or (null gnus-summary-default-score)
4047                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4048                          gnus-summary-zcore-fuzz))
4049                  ?  ;Whitespace
4050                (if (< gnus-tmp-score gnus-summary-default-score)
4051                    gnus-score-below-mark gnus-score-over-mark))
4052              gnus-tmp-replied
4053              (cond ((memq number gnus-newsgroup-processable)
4054                     gnus-process-mark)
4055                    ((memq number gnus-newsgroup-cached)
4056                     gnus-cached-mark)
4057                    ((memq number gnus-newsgroup-replied)
4058                     gnus-replied-mark)
4059                    ((memq number gnus-newsgroup-saved)
4060                     gnus-saved-mark)
4061                    (t gnus-unread-mark))
4062              gnus-tmp-from (mail-header-from gnus-tmp-header)
4063              gnus-tmp-name
4064              (cond
4065               ((string-match "<[^>]+> *$" gnus-tmp-from)
4066                (setq beg-match (match-beginning 0))
4067                (or (and (string-match "^\".+\"" gnus-tmp-from)
4068                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4069                    (substring gnus-tmp-from 0 beg-match)))
4070               ((string-match "(.+)" gnus-tmp-from)
4071                (substring gnus-tmp-from
4072                           (1+ (match-beginning 0)) (1- (match-end 0))))
4073               (t gnus-tmp-from)))
4074             (when (string= gnus-tmp-name "")
4075               (setq gnus-tmp-name gnus-tmp-from))
4076             (unless (numberp gnus-tmp-lines)
4077               (setq gnus-tmp-lines 0))
4078             (gnus-put-text-property
4079              (point)
4080              (progn (eval gnus-summary-line-format-spec) (point))
4081              'gnus-number number)
4082             (when gnus-visual-p
4083               (forward-line -1)
4084               (gnus-run-hooks 'gnus-summary-update-hook)
4085               (forward-line 1))
4086
4087             (setq gnus-tmp-prev-subject subject)))
4088
4089         (when (nth 1 thread)
4090           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4091         (incf gnus-tmp-level)
4092         (setq threads (if thread-end nil (cdar thread)))
4093         (unless threads
4094           (setq gnus-tmp-level 0)))))
4095   (gnus-message 7 "Generating summary...done"))
4096
4097 (defun gnus-summary-prepare-unthreaded (headers)
4098   "Generate an unthreaded summary buffer based on HEADERS."
4099   (let (header number mark)
4100
4101     (beginning-of-line)
4102
4103     (while headers
4104       ;; We may have to root out some bad articles...
4105       (when (memq (setq number (mail-header-number
4106                                 (setq header (pop headers))))
4107                   gnus-newsgroup-limit)
4108         ;; Mark article as read when it has a low score.
4109         (when (and gnus-summary-mark-below
4110                    (< (or (cdr (assq number gnus-newsgroup-scored))
4111                           gnus-summary-default-score 0)
4112                       gnus-summary-mark-below)
4113                    (not (gnus-summary-article-ancient-p number)))
4114           (setq gnus-newsgroup-unreads
4115                 (delq number gnus-newsgroup-unreads))
4116           (if gnus-newsgroup-auto-expire
4117               (push number gnus-newsgroup-expirable)
4118             (push (cons number gnus-low-score-mark)
4119                   gnus-newsgroup-reads)))
4120
4121         (setq mark (gnus-article-mark number))
4122         (push (gnus-data-make number mark (1+ (point)) header 0)
4123               gnus-newsgroup-data)
4124         (gnus-summary-insert-line
4125          header 0 number
4126          mark (memq number gnus-newsgroup-replied)
4127          (memq number gnus-newsgroup-expirable)
4128          (mail-header-subject header) nil
4129          (cdr (assq number gnus-newsgroup-scored))
4130          (memq number gnus-newsgroup-processable))))))
4131
4132 (defun gnus-summary-remove-list-identifiers ()
4133   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4134   (let ((regexp (if (stringp gnus-list-identifiers)
4135                     gnus-list-identifiers
4136                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4137     (dolist (header gnus-newsgroup-headers)
4138       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4139                                   " *\\)\\)+\\(Re: +\\)?\\)")
4140                           (mail-header-subject header))
4141         (mail-header-set-subject
4142          header (concat (substring (mail-header-subject header)
4143                                    0 (match-beginning 1))
4144                         (or
4145                          (match-string 3 (mail-header-subject header))
4146                          (match-string 5 (mail-header-subject header)))
4147                         (substring (mail-header-subject header)
4148                                    (match-end 1))))))))
4149
4150 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4151   "Select newsgroup GROUP.
4152 If READ-ALL is non-nil, all articles in the group are selected.
4153 If SELECT-ARTICLES, only select those articles from GROUP."
4154   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4155          ;;!!! Dirty hack; should be removed.
4156          (gnus-summary-ignore-duplicates
4157           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4158               t
4159             gnus-summary-ignore-duplicates))
4160          (info (nth 2 entry))
4161          articles fetched-articles cached)
4162
4163     (unless (gnus-check-server
4164              (setq gnus-current-select-method
4165                    (gnus-find-method-for-group group)))
4166       (error "Couldn't open server"))
4167
4168     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4169         (gnus-activate-group group)     ; Or we can activate it...
4170         (progn                          ; Or we bug out.
4171           (when (equal major-mode 'gnus-summary-mode)
4172             (kill-buffer (current-buffer)))
4173           (error "Couldn't activate group %s: %s"
4174                  group (gnus-status-message group))))
4175
4176     (unless (gnus-request-group group t)
4177       (when (equal major-mode 'gnus-summary-mode)
4178         (kill-buffer (current-buffer)))
4179       (error "Couldn't request group %s: %s"
4180              group (gnus-status-message group)))
4181
4182     (setq gnus-newsgroup-name group)
4183     (setq gnus-newsgroup-unselected nil)
4184     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4185     (gnus-summary-setup-default-charset)
4186
4187     ;; Adjust and set lists of article marks.
4188     (when info
4189       (gnus-adjust-marked-articles info))
4190
4191     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4192     (when (gnus-virtual-group-p group)
4193       (setq cached gnus-newsgroup-cached))
4194
4195     (setq gnus-newsgroup-unreads
4196           (gnus-set-difference
4197            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4198            gnus-newsgroup-dormant))
4199
4200     (setq gnus-newsgroup-processable nil)
4201
4202     (gnus-update-read-articles group gnus-newsgroup-unreads)
4203
4204     (if (setq articles select-articles)
4205         (setq gnus-newsgroup-unselected
4206               (gnus-sorted-intersection
4207                gnus-newsgroup-unreads
4208                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4209       (setq articles (gnus-articles-to-read group read-all)))
4210
4211     (cond
4212      ((null articles)
4213       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4214       'quit)
4215      ((eq articles 0) nil)
4216      (t
4217       ;; Init the dependencies hash table.
4218       (setq gnus-newsgroup-dependencies
4219             (gnus-make-hashtable (length articles)))
4220       (gnus-set-global-variables)
4221       ;; Retrieve the headers and read them in.
4222       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4223       (setq gnus-newsgroup-headers
4224             (if (eq 'nov
4225                     (setq gnus-headers-retrieved-by
4226                           (gnus-retrieve-headers
4227                            articles gnus-newsgroup-name
4228                            ;; We might want to fetch old headers, but
4229                            ;; not if there is only 1 article.
4230                            (and (or (and
4231                                      (not (eq gnus-fetch-old-headers 'some))
4232                                      (not (numberp gnus-fetch-old-headers)))
4233                                     (> (length articles) 1))
4234                                 gnus-fetch-old-headers))))
4235                 (gnus-get-newsgroup-headers-xover
4236                  articles nil nil gnus-newsgroup-name t)
4237               (gnus-get-newsgroup-headers)))
4238       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4239
4240       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4241       (when cached
4242         (setq gnus-newsgroup-cached cached))
4243
4244       ;; Suppress duplicates?
4245       (when gnus-suppress-duplicates
4246         (gnus-dup-suppress-articles))
4247
4248       ;; Set the initial limit.
4249       (setq gnus-newsgroup-limit (copy-sequence articles))
4250       ;; Remove canceled articles from the list of unread articles.
4251       (setq gnus-newsgroup-unreads
4252             (gnus-set-sorted-intersection
4253              gnus-newsgroup-unreads
4254              (setq fetched-articles
4255                    (mapcar (lambda (headers) (mail-header-number headers))
4256                            gnus-newsgroup-headers))))
4257       ;; Removed marked articles that do not exist.
4258       (gnus-update-missing-marks
4259        (gnus-sorted-complement fetched-articles articles))
4260       ;; We might want to build some more threads first.
4261       (when (and gnus-fetch-old-headers
4262                  (eq gnus-headers-retrieved-by 'nov))
4263         (if (eq gnus-fetch-old-headers 'invisible)
4264             (gnus-build-all-threads)
4265           (gnus-build-old-threads)))
4266       ;; Let the Gnus agent mark articles as read.
4267       (when gnus-agent
4268         (gnus-agent-get-undownloaded-list))
4269       ;; Remove list identifiers from subject
4270       (when gnus-list-identifiers
4271         (gnus-summary-remove-list-identifiers))
4272       ;; Check whether auto-expire is to be done in this group.
4273       (setq gnus-newsgroup-auto-expire
4274             (gnus-group-auto-expirable-p group))
4275       ;; Set up the article buffer now, if necessary.
4276       (unless gnus-single-article-buffer
4277         (gnus-article-setup-buffer))
4278       ;; First and last article in this newsgroup.
4279       (when gnus-newsgroup-headers
4280         (setq gnus-newsgroup-begin
4281               (mail-header-number (car gnus-newsgroup-headers))
4282               gnus-newsgroup-end
4283               (mail-header-number
4284                (gnus-last-element gnus-newsgroup-headers))))
4285       ;; GROUP is successfully selected.
4286       (or gnus-newsgroup-headers t)))))
4287
4288 (defun gnus-articles-to-read (group &optional read-all)
4289   "Find out what articles the user wants to read."
4290   (let* ((articles
4291           ;; Select all articles if `read-all' is non-nil, or if there
4292           ;; are no unread articles.
4293           (if (or read-all
4294                   (and (zerop (length gnus-newsgroup-marked))
4295                        (zerop (length gnus-newsgroup-unreads)))
4296                   (eq (gnus-group-find-parameter group 'display)
4297                       'all))
4298               (or
4299                (gnus-uncompress-range (gnus-active group))
4300                (gnus-cache-articles-in-group group))
4301             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4302                           (copy-sequence gnus-newsgroup-unreads))
4303                   '<)))
4304          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4305          (scored (length scored-list))
4306          (number (length articles))
4307          (marked (+ (length gnus-newsgroup-marked)
4308                     (length gnus-newsgroup-dormant)))
4309          (select
4310           (cond
4311            ((numberp read-all)
4312             read-all)
4313            (t
4314             (condition-case ()
4315                 (cond
4316                  ((and (or (<= scored marked) (= scored number))
4317                        (numberp gnus-large-newsgroup)
4318                        (> number gnus-large-newsgroup))
4319                   (let ((input
4320                          (read-string
4321                           (format
4322                            "How many articles from %s (default %d): "
4323                            (gnus-limit-string gnus-newsgroup-name 35)
4324                            number))))
4325                     (if (string-match "^[ \t]*$" input) number input)))
4326                  ((and (> scored marked) (< scored number)
4327                        (> (- scored number) 20))
4328                   (let ((input
4329                          (read-string
4330                           (format "%s %s (%d scored, %d total): "
4331                                   "How many articles from"
4332                                   group scored number))))
4333                     (if (string-match "^[ \t]*$" input)
4334                         number input)))
4335                  (t number))
4336               (quit
4337                (message "Quit getting the articles to read")
4338                nil))))))
4339     (setq select (if (stringp select) (string-to-number select) select))
4340     (if (or (null select) (zerop select))
4341         select
4342       (if (and (not (zerop scored)) (<= (abs select) scored))
4343           (progn
4344             (setq articles (sort scored-list '<))
4345             (setq number (length articles)))
4346         (setq articles (copy-sequence articles)))
4347
4348       (when (< (abs select) number)
4349         (if (< select 0)
4350             ;; Select the N oldest articles.
4351             (setcdr (nthcdr (1- (abs select)) articles) nil)
4352           ;; Select the N most recent articles.
4353           (setq articles (nthcdr (- number select) articles))))
4354       (setq gnus-newsgroup-unselected
4355             (gnus-sorted-intersection
4356              gnus-newsgroup-unreads
4357              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4358       (when gnus-alter-articles-to-read-function
4359         (setq gnus-newsgroup-unreads
4360               (sort 
4361                (funcall gnus-alter-articles-to-read-function
4362                         gnus-newsgroup-name gnus-newsgroup-unreads)
4363                '<)))
4364       articles)))
4365
4366 (defun gnus-killed-articles (killed articles)
4367   (let (out)
4368     (while articles
4369       (when (inline (gnus-member-of-range (car articles) killed))
4370         (push (car articles) out))
4371       (setq articles (cdr articles)))
4372     out))
4373
4374 (defun gnus-uncompress-marks (marks)
4375   "Uncompress the mark ranges in MARKS."
4376   (let ((uncompressed '(score bookmark))
4377         out)
4378     (while marks
4379       (if (memq (caar marks) uncompressed)
4380           (push (car marks) out)
4381         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4382       (setq marks (cdr marks)))
4383     out))
4384
4385 (defun gnus-adjust-marked-articles (info)
4386   "Set all article lists and remove all marks that are no longer valid."
4387   (let* ((marked-lists (gnus-info-marks info))
4388          (active (gnus-active (gnus-info-group info)))
4389          (min (car active))
4390          (max (cdr active))
4391          (types gnus-article-mark-lists)
4392          (uncompressed '(score bookmark killed))
4393          marks var articles article mark)
4394
4395     (while marked-lists
4396       (setq marks (pop marked-lists))
4397       (set (setq var (intern (format "gnus-newsgroup-%s"
4398                                      (car (rassq (setq mark (car marks))
4399                                                  types)))))
4400            (if (memq (car marks) uncompressed) (cdr marks)
4401              (gnus-uncompress-range (cdr marks))))
4402
4403       (setq articles (symbol-value var))
4404
4405       ;; All articles have to be subsets of the active articles.
4406       (cond
4407        ;; Adjust "simple" lists.
4408        ((memq mark '(tick dormant expire reply save))
4409         (while articles
4410           (when (or (< (setq article (pop articles)) min) (> article max))
4411             (set var (delq article (symbol-value var))))))
4412        ;; Adjust assocs.
4413        ((memq mark uncompressed)
4414         (when (not (listp (cdr (symbol-value var))))
4415           (set var (list (symbol-value var))))
4416         (when (not (listp (cdr articles)))
4417           (setq articles (list articles)))
4418         (while articles
4419           (when (or (not (consp (setq article (pop articles))))
4420                     (< (car article) min)
4421                     (> (car article) max))
4422             (set var (delq article (symbol-value var))))))))))
4423
4424 (defun gnus-update-missing-marks (missing)
4425   "Go through the list of MISSING articles and remove them from the mark lists."
4426   (when missing
4427     (let ((types gnus-article-mark-lists)
4428           var m)
4429       ;; Go through all types.
4430       (while types
4431         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4432         (when (symbol-value var)
4433           ;; This list has articles.  So we delete all missing articles
4434           ;; from it.
4435           (setq m missing)
4436           (while m
4437             (set var (delq (pop m) (symbol-value var)))))))))
4438
4439 (defun gnus-update-marks ()
4440   "Enter the various lists of marked articles into the newsgroup info list."
4441   (let ((types gnus-article-mark-lists)
4442         (info (gnus-get-info gnus-newsgroup-name))
4443         (uncompressed '(score bookmark killed))
4444         type list newmarked symbol delta-marks)
4445     (when info
4446       ;; Add all marks lists to the list of marks lists.
4447       (while (setq type (pop types))
4448         (setq list (symbol-value
4449                     (setq symbol
4450                           (intern (format "gnus-newsgroup-%s"
4451                                           (car type))))))
4452
4453         (when list
4454           ;; Get rid of the entries of the articles that have the
4455           ;; default score.
4456           (when (and (eq (cdr type) 'score)
4457                      gnus-save-score
4458                      list)
4459             (let* ((arts list)
4460                    (prev (cons nil list))
4461                    (all prev))
4462               (while arts
4463                 (if (or (not (consp (car arts)))
4464                         (= (cdar arts) gnus-summary-default-score))
4465                     (setcdr prev (cdr arts))
4466                   (setq prev arts))
4467                 (setq arts (cdr arts)))
4468               (setq list (cdr all)))))
4469
4470         (unless (memq (cdr type) uncompressed)
4471           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4472        
4473         (when (gnus-check-backend-function
4474                'request-set-mark gnus-newsgroup-name)
4475           ;; propagate flags to server, with the following exceptions:
4476           ;; uncompressed:s are not proper flags (they are cons cells)
4477           ;; cache is a internal gnus flag
4478           ;; download are local to one gnus installation (well)
4479           ;; unsend are for nndraft groups only
4480           ;; xxx: generality of this?  this suits nnimap anyway
4481           (unless (memq (cdr type) (append '(cache download unsend)
4482                                            uncompressed))
4483             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4484                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4485                    (add (gnus-remove-from-range
4486                          (gnus-copy-sequence list) old)))
4487               (when add
4488                 (push (list add 'add (list (cdr type))) delta-marks))
4489               (when del
4490                 (push (list del 'del (list (cdr type))) delta-marks)))))
4491           
4492         (when list
4493           (push (cons (cdr type) list) newmarked)))
4494
4495       (when delta-marks
4496         (unless (gnus-check-group gnus-newsgroup-name)
4497           (error "Can't open server for %s" gnus-newsgroup-name))
4498         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4499           
4500       ;; Enter these new marks into the info of the group.
4501       (if (nthcdr 3 info)
4502           (setcar (nthcdr 3 info) newmarked)
4503         ;; Add the marks lists to the end of the info.
4504         (when newmarked
4505           (setcdr (nthcdr 2 info) (list newmarked))))
4506
4507       ;; Cut off the end of the info if there's nothing else there.
4508       (let ((i 5))
4509         (while (and (> i 2)
4510                     (not (nth i info)))
4511           (when (nthcdr (decf i) info)
4512             (setcdr (nthcdr i info) nil)))))))
4513
4514 (defun gnus-set-mode-line (where)
4515   "Set the mode line of the article or summary buffers.
4516 If WHERE is `summary', the summary mode line format will be used."
4517   ;; Is this mode line one we keep updated?
4518   (when (and (memq where gnus-updated-mode-lines)
4519              (symbol-value
4520               (intern (format "gnus-%s-mode-line-format-spec" where))))
4521     (let (mode-string)
4522       (save-excursion
4523         ;; We evaluate this in the summary buffer since these
4524         ;; variables are buffer-local to that buffer.
4525         (set-buffer gnus-summary-buffer)
4526         ;; We bind all these variables that are used in the `eval' form
4527         ;; below.
4528         (let* ((mformat (symbol-value
4529                          (intern
4530                           (format "gnus-%s-mode-line-format-spec" where))))
4531                (gnus-tmp-group-name (gnus-group-name-decode 
4532                                      gnus-newsgroup-name
4533                                      (gnus-group-name-charset 
4534                                       nil
4535                                       gnus-newsgroup-name)))
4536                (gnus-tmp-article-number (or gnus-current-article 0))
4537                (gnus-tmp-unread gnus-newsgroup-unreads)
4538                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4539                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4540                (gnus-tmp-unread-and-unselected
4541                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4542                             (zerop gnus-tmp-unselected))
4543                        "")
4544                       ((zerop gnus-tmp-unselected)
4545                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4546                       (t (format "{%d(+%d) more}"
4547                                  gnus-tmp-unread-and-unticked
4548                                  gnus-tmp-unselected))))
4549                (gnus-tmp-subject
4550                 (if (and gnus-current-headers
4551                          (vectorp gnus-current-headers))
4552                     (gnus-mode-string-quote
4553                      (mail-header-subject gnus-current-headers))
4554                   ""))
4555                bufname-length max-len
4556                gnus-tmp-header);; passed as argument to any user-format-funcs
4557           (setq mode-string (eval mformat))
4558           (setq bufname-length (if (string-match "%b" mode-string)
4559                                    (- (length
4560                                        (buffer-name
4561                                         (if (eq where 'summary)
4562                                             nil
4563                                           (get-buffer gnus-article-buffer))))
4564                                       2)
4565                                  0))
4566           (setq max-len (max 4 (if gnus-mode-non-string-length
4567                                    (- (window-width)
4568                                       gnus-mode-non-string-length
4569                                       bufname-length)
4570                                  (length mode-string))))
4571           ;; We might have to chop a bit of the string off...
4572           (when (> (length mode-string) max-len)
4573             (setq mode-string
4574                   (concat (truncate-string-to-width mode-string (- max-len 3))
4575                           "...")))
4576           ;; Pad the mode string a bit.
4577           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4578       ;; Update the mode line.
4579       (setq mode-line-buffer-identification
4580             (gnus-mode-line-buffer-identification (list mode-string)))
4581       (set-buffer-modified-p t))))
4582
4583 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4584   "Go through the HEADERS list and add all Xrefs to a hash table.
4585 The resulting hash table is returned, or nil if no Xrefs were found."
4586   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4587          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4588          (xref-hashtb (gnus-make-hashtable))
4589          start group entry number xrefs header)
4590     (while headers
4591       (setq header (pop headers))
4592       (when (and (setq xrefs (mail-header-xref header))
4593                  (not (memq (setq number (mail-header-number header))
4594                             unreads)))
4595         (setq start 0)
4596         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4597           (setq start (match-end 0))
4598           (setq group (if prefix
4599                           (concat prefix (substring xrefs (match-beginning 1)
4600                                                     (match-end 1)))
4601                         (substring xrefs (match-beginning 1) (match-end 1))))
4602           (setq number
4603                 (string-to-int (substring xrefs (match-beginning 2)
4604                                           (match-end 2))))
4605           (if (setq entry (gnus-gethash group xref-hashtb))
4606               (setcdr entry (cons number (cdr entry)))
4607             (gnus-sethash group (cons number nil) xref-hashtb)))))
4608     (and start xref-hashtb)))
4609
4610 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4611   "Look through all the headers and mark the Xrefs as read."
4612   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4613         name entry info xref-hashtb idlist method nth4)
4614     (save-excursion
4615       (set-buffer gnus-group-buffer)
4616       (when (setq xref-hashtb
4617                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4618         (mapatoms
4619          (lambda (group)
4620            (unless (string= from-newsgroup (setq name (symbol-name group)))
4621              (setq idlist (symbol-value group))
4622              ;; Dead groups are not updated.
4623              (and (prog1
4624                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4625                             info (nth 2 entry))
4626                     (when (stringp (setq nth4 (gnus-info-method info)))
4627                       (setq nth4 (gnus-server-to-method nth4))))
4628                   ;; Only do the xrefs if the group has the same
4629                   ;; select method as the group we have just read.
4630                   (or (gnus-methods-equal-p
4631                        nth4 (gnus-find-method-for-group from-newsgroup))
4632                       virtual
4633                       (equal nth4 (setq method (gnus-find-method-for-group
4634                                                 from-newsgroup)))
4635                       (and (equal (car nth4) (car method))
4636                            (equal (nth 1 nth4) (nth 1 method))))
4637                   gnus-use-cross-reference
4638                   (or (not (eq gnus-use-cross-reference t))
4639                       virtual
4640                       ;; Only do cross-references on subscribed
4641                       ;; groups, if that is what is wanted.
4642                       (<= (gnus-info-level info) gnus-level-subscribed))
4643                   (gnus-group-make-articles-read name idlist))))
4644          xref-hashtb)))))
4645
4646 (defun gnus-compute-read-articles (group articles)
4647   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4648          (info (nth 2 entry))
4649          (active (gnus-active group))
4650          ninfo)
4651     (when entry
4652       ;; First peel off all invalid article numbers.
4653       (when active
4654         (let ((ids articles)
4655               id first)
4656           (while (setq id (pop ids))
4657             (when (and first (> id (cdr active)))
4658               ;; We'll end up in this situation in one particular
4659               ;; obscure situation.  If you re-scan a group and get
4660               ;; a new article that is cross-posted to a different
4661               ;; group that has not been re-scanned, you might get
4662               ;; crossposted article that has a higher number than
4663               ;; Gnus believes possible.  So we re-activate this
4664               ;; group as well.  This might mean doing the
4665               ;; crossposting thingy will *increase* the number
4666               ;; of articles in some groups.  Tsk, tsk.
4667               (setq active (or (gnus-activate-group group) active)))
4668             (when (or (> id (cdr active))
4669                       (< id (car active)))
4670               (setq articles (delq id articles))))))
4671       ;; If the read list is nil, we init it.
4672       (if (and active
4673                (null (gnus-info-read info))
4674                (> (car active) 1))
4675           (setq ninfo (cons 1 (1- (car active))))
4676         (setq ninfo (gnus-info-read info)))
4677       ;; Then we add the read articles to the range.
4678       (gnus-add-to-range
4679        ninfo (setq articles (sort articles '<))))))
4680
4681 (defun gnus-group-make-articles-read (group articles)
4682   "Update the info of GROUP to say that ARTICLES are read."
4683   (let* ((num 0)
4684          (entry (gnus-gethash group gnus-newsrc-hashtb))
4685          (info (nth 2 entry))
4686          (active (gnus-active group))
4687          range)
4688     (when entry
4689       (setq range (gnus-compute-read-articles group articles))
4690       (save-excursion
4691         (set-buffer gnus-group-buffer)
4692         (gnus-undo-register
4693           `(progn
4694              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4695              (gnus-info-set-read ',info ',(gnus-info-read info))
4696              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4697              (gnus-group-update-group ,group t))))
4698       ;; Add the read articles to the range.
4699       (gnus-info-set-read info range)
4700       ;; Then we have to re-compute how many unread
4701       ;; articles there are in this group.
4702       (when active
4703         (cond
4704          ((not range)
4705           (setq num (- (1+ (cdr active)) (car active))))
4706          ((not (listp (cdr range)))
4707           (setq num (- (cdr active) (- (1+ (cdr range))
4708                                        (car range)))))
4709          (t
4710           (while range
4711             (if (numberp (car range))
4712                 (setq num (1+ num))
4713               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4714             (setq range (cdr range)))
4715           (setq num (- (cdr active) num))))
4716         ;; Update the number of unread articles.
4717         (setcar entry num)
4718         ;; Update the group buffer.
4719         (gnus-group-update-group group t)))))
4720
4721 (defvar gnus-newsgroup-none-id 0)
4722
4723 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4724   (let ((cur nntp-server-buffer)
4725         (dependencies
4726          (or dependencies
4727              (save-excursion (set-buffer gnus-summary-buffer)
4728                              gnus-newsgroup-dependencies)))
4729         headers id end ref
4730         (mail-parse-charset gnus-newsgroup-charset)
4731         (mail-parse-ignored-charsets
4732          (save-excursion (condition-case nil
4733                              (set-buffer gnus-summary-buffer)
4734                            (error))
4735                          gnus-newsgroup-ignored-charsets)))
4736     (save-excursion
4737       (set-buffer nntp-server-buffer)
4738       ;; Translate all TAB characters into SPACE characters.
4739       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4740       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4741       (gnus-run-hooks 'gnus-parse-headers-hook)
4742       (let ((case-fold-search t)
4743             in-reply-to header p lines chars)
4744         (goto-char (point-min))
4745         ;; Search to the beginning of the next header.  Error messages
4746         ;; do not begin with 2 or 3.
4747         (while (re-search-forward "^[23][0-9]+ " nil t)
4748           (setq id nil
4749                 ref nil)
4750           ;; This implementation of this function, with nine
4751           ;; search-forwards instead of the one re-search-forward and
4752           ;; a case (which basically was the old function) is actually
4753           ;; about twice as fast, even though it looks messier.  You
4754           ;; can't have everything, I guess.  Speed and elegance
4755           ;; doesn't always go hand in hand.
4756           (setq
4757            header
4758            (vector
4759             ;; Number.
4760             (prog1
4761                 (read cur)
4762               (end-of-line)
4763               (setq p (point))
4764               (narrow-to-region (point)
4765                                 (or (and (search-forward "\n.\n" nil t)
4766                                          (- (point) 2))
4767                                     (point))))
4768             ;; Subject.
4769             (progn
4770               (goto-char p)
4771               (if (search-forward "\nsubject: " nil t)
4772                   (funcall gnus-decode-encoded-word-function
4773                            (nnheader-header-value))
4774                 "(none)"))
4775             ;; From.
4776             (progn
4777               (goto-char p)
4778               (if (or (search-forward "\nfrom: " nil t)
4779                       (search-forward "\nfrom:" nil t))
4780                   (funcall gnus-decode-encoded-word-function
4781                            (nnheader-header-value))
4782                 "(nobody)"))
4783             ;; Date.
4784             (progn
4785               (goto-char p)
4786               (if (search-forward "\ndate: " nil t)
4787                   (nnheader-header-value) ""))
4788             ;; Message-ID.
4789             (progn
4790               (goto-char p)
4791               (setq id (if (re-search-forward
4792                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4793                            ;; We do it this way to make sure the Message-ID
4794                            ;; is (somewhat) syntactically valid.
4795                            (buffer-substring (match-beginning 1)
4796                                              (match-end 1))
4797                          ;; If there was no message-id, we just fake one
4798                          ;; to make subsequent routines simpler.
4799                          (nnheader-generate-fake-message-id))))
4800             ;; References.
4801             (progn
4802               (goto-char p)
4803               (if (search-forward "\nreferences: " nil t)
4804                   (progn
4805                     (setq end (point))
4806                     (prog1
4807                         (nnheader-header-value)
4808                       (setq ref
4809                             (buffer-substring
4810                              (progn
4811                                (end-of-line)
4812                                (search-backward ">" end t)
4813                                (1+ (point)))
4814                              (progn
4815                                (search-backward "<" end t)
4816                                (point))))))
4817                 ;; Get the references from the in-reply-to header if there
4818                 ;; were no references and the in-reply-to header looks
4819                 ;; promising.
4820                 (if (and (search-forward "\nin-reply-to: " nil t)
4821                          (setq in-reply-to (nnheader-header-value))
4822                          (string-match "<[^>]+>" in-reply-to))
4823                     (let (ref2)
4824                       (setq ref (substring in-reply-to (match-beginning 0)
4825                                            (match-end 0)))
4826                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4827                         (setq ref2 (substring in-reply-to (match-beginning 0)
4828                                               (match-end 0)))
4829                         (when (> (length ref2) (length ref))
4830                           (setq ref ref2)))
4831                       ref)
4832                   (setq ref nil))))
4833             ;; Chars.
4834             (progn
4835               (goto-char p)
4836               (if (search-forward "\nchars: " nil t)
4837                   (if (numberp (setq chars (ignore-errors (read cur))))
4838                       chars 0)
4839                 0))
4840             ;; Lines.
4841             (progn
4842               (goto-char p)
4843               (if (search-forward "\nlines: " nil t)
4844                   (if (numberp (setq lines (ignore-errors (read cur))))
4845                       lines 0)
4846                 0))
4847             ;; Xref.
4848             (progn
4849               (goto-char p)
4850               (and (search-forward "\nxref: " nil t)
4851                    (nnheader-header-value)))
4852             ;; Extra.
4853             (when gnus-extra-headers
4854               (let ((extra gnus-extra-headers)
4855                     out)
4856                 (while extra
4857                   (goto-char p)
4858                   (when (search-forward
4859                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4860                     (push (cons (car extra) (nnheader-header-value))
4861                           out))
4862                   (pop extra))
4863                 out))))
4864           (when (equal id ref)
4865             (setq ref nil))
4866
4867           (when gnus-alter-header-function
4868             (funcall gnus-alter-header-function header)
4869             (setq id (mail-header-id header)
4870                   ref (gnus-parent-id (mail-header-references header))))
4871
4872           (when (setq header
4873                       (gnus-dependencies-add-header
4874                        header dependencies force-new))
4875             (push header headers))
4876           (goto-char (point-max))
4877           (widen))
4878         (nreverse headers)))))
4879
4880 ;; Goes through the xover lines and returns a list of vectors
4881 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4882                                                   force-new dependencies
4883                                                   group also-fetch-heads)
4884   "Parse the news overview data in the server buffer.
4885 Return a list of headers that match SEQUENCE (see
4886 `nntp-retrieve-headers')."
4887   ;; Get the Xref when the users reads the articles since most/some
4888   ;; NNTP servers do not include Xrefs when using XOVER.
4889   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4890   (let ((mail-parse-charset gnus-newsgroup-charset)
4891         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4892         (cur nntp-server-buffer)
4893         (dependencies (or dependencies gnus-newsgroup-dependencies))
4894         number headers header)
4895     (save-excursion
4896       (set-buffer nntp-server-buffer)
4897       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4898       ;; Allow the user to mangle the headers before parsing them.
4899       (gnus-run-hooks 'gnus-parse-headers-hook)
4900       (goto-char (point-min))
4901       (while (not (eobp))
4902         (condition-case ()
4903             (while (and sequence (not (eobp)))
4904               (setq number (read cur))
4905               (while (and sequence
4906                           (< (car sequence) number))
4907                 (setq sequence (cdr sequence)))
4908               (and sequence
4909                    (eq number (car sequence))
4910                    (progn
4911                      (setq sequence (cdr sequence))
4912                      (setq header (inline
4913                                     (gnus-nov-parse-line
4914                                      number dependencies force-new))))
4915                    (push header headers))
4916               (forward-line 1))
4917           (error
4918            (gnus-error 4 "Strange nov line (%d)"
4919                        (count-lines (point-min) (point)))))
4920         (forward-line 1))
4921       ;; A common bug in inn is that if you have posted an article and
4922       ;; then retrieves the active file, it will answer correctly --
4923       ;; the new article is included.  However, a NOV entry for the
4924       ;; article may not have been generated yet, so this may fail.
4925       ;; We work around this problem by retrieving the last few
4926       ;; headers using HEAD.
4927       (if (or (not also-fetch-heads)
4928               (not sequence))
4929           ;; We (probably) got all the headers.
4930           (nreverse headers)
4931         (let ((gnus-nov-is-evil t))
4932           (nconc
4933            (nreverse headers)
4934            (when (gnus-retrieve-headers sequence group)
4935              (gnus-get-newsgroup-headers))))))))
4936
4937 (defun gnus-article-get-xrefs ()
4938   "Fill in the Xref value in `gnus-current-headers', if necessary.
4939 This is meant to be called in `gnus-article-internal-prepare-hook'."
4940   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4941                                  gnus-current-headers)))
4942     (or (not gnus-use-cross-reference)
4943         (not headers)
4944         (and (mail-header-xref headers)
4945              (not (string= (mail-header-xref headers) "")))
4946         (let ((case-fold-search t)
4947               xref)
4948           (save-restriction
4949             (nnheader-narrow-to-headers)
4950             (goto-char (point-min))
4951             (when (or (and (not (eobp))
4952                            (eq (downcase (char-after)) ?x)
4953                            (looking-at "Xref:"))
4954                       (search-forward "\nXref:" nil t))
4955               (goto-char (1+ (match-end 0)))
4956               (setq xref (buffer-substring (point)
4957                                            (progn (end-of-line) (point))))
4958               (mail-header-set-xref headers xref)))))))
4959
4960 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4961   "Find article ID and insert the summary line for that article.
4962 OLD-HEADER can either be a header or a line number to insert
4963 the subject line on."
4964   (let* ((line (and (numberp old-header) old-header))
4965          (old-header (and (vectorp old-header) old-header))
4966          (header (cond ((and old-header use-old-header)
4967                         old-header)
4968                        ((and (numberp id)
4969                              (gnus-number-to-header id))
4970                         (gnus-number-to-header id))
4971                        (t
4972                         (gnus-read-header id))))
4973          (number (and (numberp id) id))
4974          d)
4975     (when header
4976       ;; Rebuild the thread that this article is part of and go to the
4977       ;; article we have fetched.
4978       (when (and (not gnus-show-threads)
4979                  old-header)
4980         (when (and number
4981                    (setq d (gnus-data-find (mail-header-number old-header))))
4982           (goto-char (gnus-data-pos d))
4983           (gnus-data-remove
4984            number
4985            (- (gnus-point-at-bol)
4986               (prog1
4987                   (1+ (gnus-point-at-eol))
4988                 (gnus-delete-line))))))
4989       (when old-header
4990         (mail-header-set-number header (mail-header-number old-header)))
4991       (setq gnus-newsgroup-sparse
4992             (delq (setq number (mail-header-number header))
4993                   gnus-newsgroup-sparse))
4994       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4995       (push number gnus-newsgroup-limit)
4996       (gnus-rebuild-thread (mail-header-id header) line)
4997       (gnus-summary-goto-subject number nil t))
4998     (when (and (numberp number)
4999                (> number 0))
5000       ;; We have to update the boundaries even if we can't fetch the
5001       ;; article if ID is a number -- so that the next `P' or `N'
5002       ;; command will fetch the previous (or next) article even
5003       ;; if the one we tried to fetch this time has been canceled.
5004       (when (> number gnus-newsgroup-end)
5005         (setq gnus-newsgroup-end number))
5006       (when (< number gnus-newsgroup-begin)
5007         (setq gnus-newsgroup-begin number))
5008       (setq gnus-newsgroup-unselected
5009             (delq number gnus-newsgroup-unselected)))
5010     ;; Report back a success?
5011     (and header (mail-header-number header))))
5012
5013 ;;; Process/prefix in the summary buffer
5014
5015 (defun gnus-summary-work-articles (n)
5016   "Return a list of articles to be worked upon.
5017 The prefix argument, the list of process marked articles, and the
5018 current article will be taken into consideration."
5019   (save-excursion
5020     (set-buffer gnus-summary-buffer)
5021     (cond
5022      (n
5023       ;; A numerical prefix has been given.
5024       (setq n (prefix-numeric-value n))
5025       (let ((backward (< n 0))
5026             (n (abs (prefix-numeric-value n)))
5027             articles article)
5028         (save-excursion
5029           (while
5030               (and (> n 0)
5031                    (push (setq article (gnus-summary-article-number))
5032                          articles)
5033                    (if backward
5034                        (gnus-summary-find-prev nil article)
5035                      (gnus-summary-find-next nil article)))
5036             (decf n)))
5037         (nreverse articles)))
5038      ((and (gnus-region-active-p) (mark))
5039       (message "region active")
5040       ;; Work on the region between point and mark.
5041       (let ((max (max (point) (mark)))
5042             articles article)
5043         (save-excursion
5044           (goto-char (min (min (point) (mark))))
5045           (while
5046               (and
5047                (push (setq article (gnus-summary-article-number)) articles)
5048                (gnus-summary-find-next nil article)
5049                (< (point) max)))
5050           (nreverse articles))))
5051      (gnus-newsgroup-processable
5052       ;; There are process-marked articles present.
5053       ;; Save current state.
5054       (gnus-summary-save-process-mark)
5055       ;; Return the list.
5056       (reverse gnus-newsgroup-processable))
5057      (t
5058       ;; Just return the current article.
5059       (list (gnus-summary-article-number))))))
5060
5061 (defmacro gnus-summary-iterate (arg &rest forms)
5062   "Iterate over the process/prefixed articles and do FORMS.
5063 ARG is the interactive prefix given to the command.  FORMS will be
5064 executed with point over the summary line of the articles."
5065   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5066     `(let ((,articles (gnus-summary-work-articles ,arg)))
5067        (while ,articles
5068          (gnus-summary-goto-subject (car ,articles))
5069          ,@forms
5070          (pop ,articles)))))
5071
5072 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5073 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5074
5075 (defun gnus-summary-save-process-mark ()
5076   "Push the current set of process marked articles on the stack."
5077   (interactive)
5078   (push (copy-sequence gnus-newsgroup-processable)
5079         gnus-newsgroup-process-stack))
5080
5081 (defun gnus-summary-kill-process-mark ()
5082   "Push the current set of process marked articles on the stack and unmark."
5083   (interactive)
5084   (gnus-summary-save-process-mark)
5085   (gnus-summary-unmark-all-processable))
5086
5087 (defun gnus-summary-yank-process-mark ()
5088   "Pop the last process mark state off the stack and restore it."
5089   (interactive)
5090   (unless gnus-newsgroup-process-stack
5091     (error "Empty mark stack"))
5092   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5093
5094 (defun gnus-summary-process-mark-set (set)
5095   "Make SET into the current process marked articles."
5096   (gnus-summary-unmark-all-processable)
5097   (while set
5098     (gnus-summary-set-process-mark (pop set))))
5099
5100 ;;; Searching and stuff
5101
5102 (defun gnus-summary-search-group (&optional backward use-level)
5103   "Search for next unread newsgroup.
5104 If optional argument BACKWARD is non-nil, search backward instead."
5105   (save-excursion
5106     (set-buffer gnus-group-buffer)
5107     (when (gnus-group-search-forward
5108            backward nil (if use-level (gnus-group-group-level) nil))
5109       (gnus-group-group-name))))
5110
5111 (defun gnus-summary-best-group (&optional exclude-group)
5112   "Find the name of the best unread group.
5113 If EXCLUDE-GROUP, do not go to this group."
5114   (save-excursion
5115     (set-buffer gnus-group-buffer)
5116     (save-excursion
5117       (gnus-group-best-unread-group exclude-group))))
5118
5119 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5120   (if backward (gnus-summary-find-prev)
5121     (let* ((dummy (gnus-summary-article-intangible-p))
5122            (article (or article (gnus-summary-article-number)))
5123            (arts (gnus-data-find-list article))
5124            result)
5125       (when (and (not dummy)
5126                  (or (not gnus-summary-check-current)
5127                      (not unread)
5128                      (not (gnus-data-unread-p (car arts)))))
5129         (setq arts (cdr arts)))
5130       (when (setq result
5131                   (if unread
5132                       (progn
5133                         (while arts
5134                           (when (or (and undownloaded
5135                                          (eq gnus-undownloaded-mark
5136                                              (gnus-data-mark (car arts))))
5137                                     (gnus-data-unread-p (car arts)))
5138                             (setq result (car arts)
5139                                   arts nil))
5140                           (setq arts (cdr arts)))
5141                         result)
5142                     (car arts)))
5143         (goto-char (gnus-data-pos result))
5144         (gnus-data-number result)))))
5145
5146 (defun gnus-summary-find-prev (&optional unread article)
5147   (let* ((eobp (eobp))
5148          (article (or article (gnus-summary-article-number)))
5149          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5150          result)
5151     (when (and (not eobp)
5152                (or (not gnus-summary-check-current)
5153                    (not unread)
5154                    (not (gnus-data-unread-p (car arts)))))
5155       (setq arts (cdr arts)))
5156     (when (setq result
5157                 (if unread
5158                     (progn
5159                       (while arts
5160                         (when (gnus-data-unread-p (car arts))
5161                           (setq result (car arts)
5162                                 arts nil))
5163                         (setq arts (cdr arts)))
5164                       result)
5165                   (car arts)))
5166       (goto-char (gnus-data-pos result))
5167       (gnus-data-number result))))
5168
5169 (defun gnus-summary-find-subject (subject &optional unread backward article)
5170   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5171          (article (or article (gnus-summary-article-number)))
5172          (articles (gnus-data-list backward))
5173          (arts (gnus-data-find-list article articles))
5174          result)
5175     (when (or (not gnus-summary-check-current)
5176               (not unread)
5177               (not (gnus-data-unread-p (car arts))))
5178       (setq arts (cdr arts)))
5179     (while arts
5180       (and (or (not unread)
5181                (gnus-data-unread-p (car arts)))
5182            (vectorp (gnus-data-header (car arts)))
5183            (gnus-subject-equal
5184             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5185            (setq result (car arts)
5186                  arts nil))
5187       (setq arts (cdr arts)))
5188     (and result
5189          (goto-char (gnus-data-pos result))
5190          (gnus-data-number result))))
5191
5192 (defun gnus-summary-search-forward (&optional unread subject backward)
5193   "Search forward for an article.
5194 If UNREAD, look for unread articles.  If SUBJECT, look for
5195 articles with that subject.  If BACKWARD, search backward instead."
5196   (cond (subject (gnus-summary-find-subject subject unread backward))
5197         (backward (gnus-summary-find-prev unread))
5198         (t (gnus-summary-find-next unread))))
5199
5200 (defun gnus-recenter (&optional n)
5201   "Center point in window and redisplay frame.
5202 Also do horizontal recentering."
5203   (interactive "P")
5204   (when (and gnus-auto-center-summary
5205              (not (eq gnus-auto-center-summary 'vertical)))
5206     (gnus-horizontal-recenter))
5207   (recenter n))
5208
5209 (defun gnus-summary-recenter ()
5210   "Center point in the summary window.
5211 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5212 displayed, no centering will be performed."
5213   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5214   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5215   (interactive)
5216   (let* ((top (cond ((< (window-height) 4) 0)
5217                     ((< (window-height) 7) 1)
5218                     (t (if (numberp gnus-auto-center-summary)
5219                            gnus-auto-center-summary
5220                          2))))
5221          (height (1- (window-height)))
5222          (bottom (save-excursion (goto-char (point-max))
5223                                  (forward-line (- height))
5224                                  (point)))
5225          (window (get-buffer-window (current-buffer))))
5226     ;; The user has to want it.
5227     (when gnus-auto-center-summary
5228       (when (get-buffer-window gnus-article-buffer)
5229         ;; Only do recentering when the article buffer is displayed,
5230         ;; Set the window start to either `bottom', which is the biggest
5231         ;; possible valid number, or the second line from the top,
5232         ;; whichever is the least.
5233         (set-window-start
5234          window (min bottom (save-excursion
5235                               (forward-line (- top)) (point)))
5236          t))
5237       ;; Do horizontal recentering while we're at it.
5238       (when (and (get-buffer-window (current-buffer) t)
5239                  (not (eq gnus-auto-center-summary 'vertical)))
5240         (let ((selected (selected-window)))
5241           (select-window (get-buffer-window (current-buffer) t))
5242           (gnus-summary-position-point)
5243           (gnus-horizontal-recenter)
5244           (select-window selected))))))
5245
5246 (defun gnus-summary-jump-to-group (newsgroup)
5247   "Move point to NEWSGROUP in group mode buffer."
5248   ;; Keep update point of group mode buffer if visible.
5249   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5250       (save-window-excursion
5251         ;; Take care of tree window mode.
5252         (when (get-buffer-window gnus-group-buffer)
5253           (pop-to-buffer gnus-group-buffer))
5254         (gnus-group-jump-to-group newsgroup))
5255     (save-excursion
5256       ;; Take care of tree window mode.
5257       (if (get-buffer-window gnus-group-buffer)
5258           (pop-to-buffer gnus-group-buffer)
5259         (set-buffer gnus-group-buffer))
5260       (gnus-group-jump-to-group newsgroup))))
5261
5262 ;; This function returns a list of article numbers based on the
5263 ;; difference between the ranges of read articles in this group and
5264 ;; the range of active articles.
5265 (defun gnus-list-of-unread-articles (group)
5266   (let* ((read (gnus-info-read (gnus-get-info group)))
5267          (active (or (gnus-active group) (gnus-activate-group group)))
5268          (last (cdr active))
5269          first nlast unread)
5270     ;; If none are read, then all are unread.
5271     (if (not read)
5272         (setq first (car active))
5273       ;; If the range of read articles is a single range, then the
5274       ;; first unread article is the article after the last read
5275       ;; article.  Sounds logical, doesn't it?
5276       (if (and (not (listp (cdr read)))
5277                (or (< (car read) (car active))
5278                    (progn (setq read (list read))
5279                           nil)))
5280           (setq first (max (car active) (1+ (cdr read))))
5281         ;; `read' is a list of ranges.
5282         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5283                                   (caar read)))
5284                   1)
5285           (setq first (car active)))
5286         (while read
5287           (when first
5288             (while (< first nlast)
5289               (push first unread)
5290               (setq first (1+ first))))
5291           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5292           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5293           (setq read (cdr read)))))
5294     ;; And add the last unread articles.
5295     (while (<= first last)
5296       (push first unread)
5297       (setq first (1+ first)))
5298     ;; Return the list of unread articles.
5299     (delq 0 (nreverse unread))))
5300
5301 (defun gnus-list-of-read-articles (group)
5302   "Return a list of unread, unticked and non-dormant articles."
5303   (let* ((info (gnus-get-info group))
5304          (marked (gnus-info-marks info))
5305          (active (gnus-active group)))
5306     (and info active
5307          (gnus-set-difference
5308           (gnus-sorted-complement
5309            (gnus-uncompress-range active)
5310            (gnus-list-of-unread-articles group))
5311           (append
5312            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5313            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5314
5315 ;; Various summary commands
5316
5317 (defun gnus-summary-select-article-buffer ()
5318   "Reconfigure windows to show article buffer."
5319   (interactive)
5320   (if (not (gnus-buffer-live-p gnus-article-buffer))
5321       (error "There is no article buffer for this summary buffer")
5322     (gnus-configure-windows 'article)
5323     (select-window (get-buffer-window gnus-article-buffer))))
5324
5325 (defun gnus-summary-universal-argument (arg)
5326   "Perform any operation on all articles that are process/prefixed."
5327   (interactive "P")
5328   (let ((articles (gnus-summary-work-articles arg))
5329         func article)
5330     (if (eq
5331          (setq
5332           func
5333           (key-binding
5334            (read-key-sequence
5335             (substitute-command-keys
5336              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5337          'undefined)
5338         (gnus-error 1 "Undefined key")
5339       (save-excursion
5340         (while articles
5341           (gnus-summary-goto-subject (setq article (pop articles)))
5342           (let (gnus-newsgroup-processable)
5343             (command-execute func))
5344           (gnus-summary-remove-process-mark article)))))
5345   (gnus-summary-position-point))
5346
5347 (defun gnus-summary-toggle-truncation (&optional arg)
5348   "Toggle truncation of summary lines.
5349 With arg, turn line truncation on iff arg is positive."
5350   (interactive "P")
5351   (setq truncate-lines
5352         (if (null arg) (not truncate-lines)
5353           (> (prefix-numeric-value arg) 0)))
5354   (redraw-display))
5355
5356 (defun gnus-summary-reselect-current-group (&optional all rescan)
5357   "Exit and then reselect the current newsgroup.
5358 The prefix argument ALL means to select all articles."
5359   (interactive "P")
5360   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5361     (error "Ephemeral groups can't be reselected"))
5362   (let ((current-subject (gnus-summary-article-number))
5363         (group gnus-newsgroup-name))
5364     (setq gnus-newsgroup-begin nil)
5365     (gnus-summary-exit)
5366     ;; We have to adjust the point of group mode buffer because
5367     ;; point was moved to the next unread newsgroup by exiting.
5368     (gnus-summary-jump-to-group group)
5369     (when rescan
5370       (save-excursion
5371         (gnus-group-get-new-news-this-group 1)))
5372     (gnus-group-read-group all t)
5373     (gnus-summary-goto-subject current-subject nil t)))
5374
5375 (defun gnus-summary-rescan-group (&optional all)
5376   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5377   (interactive "P")
5378   (gnus-summary-reselect-current-group all t))
5379
5380 (defun gnus-summary-update-info (&optional non-destructive)
5381   (save-excursion
5382     (let ((group gnus-newsgroup-name))
5383       (when group
5384         (when gnus-newsgroup-kill-headers
5385           (setq gnus-newsgroup-killed
5386                 (gnus-compress-sequence
5387                  (nconc
5388                   (gnus-set-sorted-intersection
5389                    (gnus-uncompress-range gnus-newsgroup-killed)
5390                    (setq gnus-newsgroup-unselected
5391                          (sort gnus-newsgroup-unselected '<)))
5392                   (setq gnus-newsgroup-unreads
5393                         (sort gnus-newsgroup-unreads '<)))
5394                  t)))
5395         (unless (listp (cdr gnus-newsgroup-killed))
5396           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5397         (let ((headers gnus-newsgroup-headers))
5398           ;; Set the new ranges of read articles.
5399           (save-excursion
5400             (set-buffer gnus-group-buffer)
5401             (gnus-undo-force-boundary))
5402           (gnus-update-read-articles
5403            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5404           ;; Set the current article marks.
5405           (let ((gnus-newsgroup-scored
5406                  (if (and (not gnus-save-score)
5407                           (not non-destructive))
5408                      nil
5409                    gnus-newsgroup-scored)))
5410             (save-excursion
5411               (gnus-update-marks)))
5412           ;; Do the cross-ref thing.
5413           (when gnus-use-cross-reference
5414             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5415           ;; Do not switch windows but change the buffer to work.
5416           (set-buffer gnus-group-buffer)
5417           (unless (gnus-ephemeral-group-p group)
5418             (gnus-group-update-group group)))))))
5419
5420 (defun gnus-summary-save-newsrc (&optional force)
5421   "Save the current number of read/marked articles in the dribble buffer.
5422 The dribble buffer will then be saved.
5423 If FORCE (the prefix), also save the .newsrc file(s)."
5424   (interactive "P")
5425   (gnus-summary-update-info t)
5426   (if force
5427       (gnus-save-newsrc-file)
5428     (gnus-dribble-save)))
5429
5430 (defun gnus-summary-exit (&optional temporary)
5431   "Exit reading current newsgroup, and then return to group selection mode.
5432 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5433   (interactive)
5434   (gnus-set-global-variables)
5435   (when (gnus-buffer-live-p gnus-article-buffer)
5436     (save-excursion
5437       (set-buffer gnus-article-buffer)
5438       (mm-destroy-parts gnus-article-mime-handles)
5439       ;; Set it to nil for safety reason.
5440       (setq gnus-article-mime-handle-alist nil)
5441       (setq gnus-article-mime-handles nil)))
5442   (gnus-kill-save-kill-buffer)
5443   (gnus-async-halt-prefetch)
5444   (let* ((group gnus-newsgroup-name)
5445          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5446          (mode major-mode)
5447          (group-point nil)
5448          (buf (current-buffer)))
5449     (unless quit-config
5450       ;; Do adaptive scoring, and possibly save score files.
5451       (when gnus-newsgroup-adaptive
5452         (gnus-score-adaptive))
5453       (when gnus-use-scoring
5454         (gnus-score-save)))
5455     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5456     ;; If we have several article buffers, we kill them at exit.
5457     (unless gnus-single-article-buffer
5458       (gnus-kill-buffer gnus-original-article-buffer)
5459       (setq gnus-article-current nil))
5460     (when gnus-use-cache
5461       (gnus-cache-possibly-remove-articles)
5462       (gnus-cache-save-buffers))
5463     (gnus-async-prefetch-remove-group group)
5464     (when gnus-suppress-duplicates
5465       (gnus-dup-enter-articles))
5466     (when gnus-use-trees
5467       (gnus-tree-close group))
5468     (when gnus-use-cache
5469       (gnus-cache-write-active))
5470     ;; Remove entries for this group.
5471     (nnmail-purge-split-history (gnus-group-real-name group))
5472     ;; Make all changes in this group permanent.
5473     (unless quit-config
5474       (gnus-run-hooks 'gnus-exit-group-hook)
5475       (gnus-summary-update-info))
5476     (gnus-close-group group)
5477     ;; Make sure where we were, and go to next newsgroup.
5478     (set-buffer gnus-group-buffer)
5479     (unless quit-config
5480       (gnus-group-jump-to-group group))
5481     (gnus-run-hooks 'gnus-summary-exit-hook)
5482     (unless (or quit-config
5483                 ;; If this group has disappeared from the summary
5484                 ;; buffer, don't skip forwards.
5485                 (not (string= group (gnus-group-group-name))))
5486       (gnus-group-next-unread-group 1))
5487     (setq group-point (point))
5488     (if temporary
5489         nil                             ;Nothing to do.
5490       ;; If we have several article buffers, we kill them at exit.
5491       (unless gnus-single-article-buffer
5492         (gnus-kill-buffer gnus-article-buffer)
5493         (gnus-kill-buffer gnus-original-article-buffer)
5494         (setq gnus-article-current nil))
5495       (set-buffer buf)
5496       (if (not gnus-kill-summary-on-exit)
5497           (gnus-deaden-summary)
5498         ;; We set all buffer-local variables to nil.  It is unclear why
5499         ;; this is needed, but if we don't, buffer-local variables are
5500         ;; not garbage-collected, it seems.  This would the lead to en
5501         ;; ever-growing Emacs.
5502         (gnus-summary-clear-local-variables)
5503         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5504           (gnus-summary-clear-local-variables))
5505         (when (get-buffer gnus-article-buffer)
5506           (bury-buffer gnus-article-buffer))
5507         ;; We clear the global counterparts of the buffer-local
5508         ;; variables as well, just to be on the safe side.
5509         (set-buffer gnus-group-buffer)
5510         (gnus-summary-clear-local-variables)
5511         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5512           (gnus-summary-clear-local-variables))
5513         ;; Return to group mode buffer.
5514         (when (eq mode 'gnus-summary-mode)
5515           (gnus-kill-buffer buf)))
5516       (setq gnus-current-select-method gnus-select-method)
5517       (pop-to-buffer gnus-group-buffer)
5518       (if (not quit-config)
5519           (progn
5520             (goto-char group-point)
5521             (gnus-configure-windows 'group 'force))
5522         (gnus-handle-ephemeral-exit quit-config))
5523       ;; Clear the current group name.
5524       (unless quit-config
5525         (setq gnus-newsgroup-name nil)))))
5526
5527 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5528 (defun gnus-summary-exit-no-update (&optional no-questions)
5529   "Quit reading current newsgroup without updating read article info."
5530   (interactive)
5531   (let* ((group gnus-newsgroup-name)
5532          (quit-config (gnus-group-quit-config group)))
5533     (when (or no-questions
5534               gnus-expert-user
5535               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5536       (gnus-async-halt-prefetch)
5537       (mapcar 'funcall
5538               (delq 'gnus-summary-expire-articles
5539                     (copy-sequence gnus-summary-prepare-exit-hook)))
5540       (when (gnus-buffer-live-p gnus-article-buffer)
5541         (save-excursion
5542           (set-buffer gnus-article-buffer)
5543           (mm-destroy-parts gnus-article-mime-handles)
5544           ;; Set it to nil for safety reason.
5545           (setq gnus-article-mime-handle-alist nil)
5546           (setq gnus-article-mime-handles nil)))
5547       ;; If we have several article buffers, we kill them at exit.
5548       (unless gnus-single-article-buffer
5549         (gnus-kill-buffer gnus-article-buffer)
5550         (gnus-kill-buffer gnus-original-article-buffer)
5551         (setq gnus-article-current nil))
5552       (if (not gnus-kill-summary-on-exit)
5553           (gnus-deaden-summary)
5554         (gnus-close-group group)
5555         (gnus-summary-clear-local-variables)
5556         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5557           (gnus-summary-clear-local-variables))
5558         (set-buffer gnus-group-buffer)
5559         (gnus-summary-clear-local-variables)
5560         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5561           (gnus-summary-clear-local-variables))
5562         (when (get-buffer gnus-summary-buffer)
5563           (kill-buffer gnus-summary-buffer)))
5564       (unless gnus-single-article-buffer
5565         (setq gnus-article-current nil))
5566       (when gnus-use-trees
5567         (gnus-tree-close group))
5568       (gnus-async-prefetch-remove-group group)
5569       (when (get-buffer gnus-article-buffer)
5570         (bury-buffer gnus-article-buffer))
5571       ;; Return to the group buffer.
5572       (gnus-configure-windows 'group 'force)
5573       ;; Clear the current group name.
5574       (setq gnus-newsgroup-name nil)
5575       (when (equal (gnus-group-group-name) group)
5576         (gnus-group-next-unread-group 1))
5577       (when quit-config
5578         (gnus-handle-ephemeral-exit quit-config)))))
5579
5580 (defun gnus-handle-ephemeral-exit (quit-config)
5581   "Handle movement when leaving an ephemeral group.
5582 The state which existed when entering the ephemeral is reset."
5583   (if (not (buffer-name (car quit-config)))
5584       (gnus-configure-windows 'group 'force)
5585     (set-buffer (car quit-config))
5586     (cond ((eq major-mode 'gnus-summary-mode)
5587            (gnus-set-global-variables))
5588           ((eq major-mode 'gnus-article-mode)
5589            (save-excursion
5590              ;; The `gnus-summary-buffer' variable may point
5591              ;; to the old summary buffer when using a single
5592              ;; article buffer.
5593              (unless (gnus-buffer-live-p gnus-summary-buffer)
5594                (set-buffer gnus-group-buffer))
5595              (set-buffer gnus-summary-buffer)
5596              (gnus-set-global-variables))))
5597     (if (or (eq (cdr quit-config) 'article)
5598             (eq (cdr quit-config) 'pick))
5599         (progn
5600           ;; The current article may be from the ephemeral group
5601           ;; thus it is best that we reload this article
5602           (gnus-summary-show-article)
5603           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5604               (gnus-configure-windows 'pick 'force)
5605             (gnus-configure-windows (cdr quit-config) 'force)))
5606       (gnus-configure-windows (cdr quit-config) 'force))
5607     (when (eq major-mode 'gnus-summary-mode)
5608       (gnus-summary-next-subject 1 nil t)
5609       (gnus-summary-recenter)
5610       (gnus-summary-position-point))))
5611
5612 ;;; Dead summaries.
5613
5614 (defvar gnus-dead-summary-mode-map nil)
5615
5616 (unless gnus-dead-summary-mode-map
5617   (setq gnus-dead-summary-mode-map (make-keymap))
5618   (suppress-keymap gnus-dead-summary-mode-map)
5619   (substitute-key-definition
5620    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5621   (let ((keys '("\C-d" "\r" "\177" [delete])))
5622     (while keys
5623       (define-key gnus-dead-summary-mode-map
5624         (pop keys) 'gnus-summary-wake-up-the-dead))))
5625
5626 (defvar gnus-dead-summary-mode nil
5627   "Minor mode for Gnus summary buffers.")
5628
5629 (defun gnus-dead-summary-mode (&optional arg)
5630   "Minor mode for Gnus summary buffers."
5631   (interactive "P")
5632   (when (eq major-mode 'gnus-summary-mode)
5633     (make-local-variable 'gnus-dead-summary-mode)
5634     (setq gnus-dead-summary-mode
5635           (if (null arg) (not gnus-dead-summary-mode)
5636             (> (prefix-numeric-value arg) 0)))
5637     (when gnus-dead-summary-mode
5638       (gnus-add-minor-mode
5639        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5640
5641 (defun gnus-deaden-summary ()
5642   "Make the current summary buffer into a dead summary buffer."
5643   ;; Kill any previous dead summary buffer.
5644   (when (and gnus-dead-summary
5645              (buffer-name gnus-dead-summary))
5646     (save-excursion
5647       (set-buffer gnus-dead-summary)
5648       (when gnus-dead-summary-mode
5649         (kill-buffer (current-buffer)))))
5650   ;; Make this the current dead summary.
5651   (setq gnus-dead-summary (current-buffer))
5652   (gnus-dead-summary-mode 1)
5653   (let ((name (buffer-name)))
5654     (when (string-match "Summary" name)
5655       (rename-buffer
5656        (concat (substring name 0 (match-beginning 0)) "Dead "
5657                (substring name (match-beginning 0)))
5658        t)
5659       (bury-buffer))))
5660
5661 (defun gnus-kill-or-deaden-summary (buffer)
5662   "Kill or deaden the summary BUFFER."
5663   (save-excursion
5664     (when (and (buffer-name buffer)
5665                (not gnus-single-article-buffer))
5666       (save-excursion
5667         (set-buffer buffer)
5668         (gnus-kill-buffer gnus-article-buffer)
5669         (gnus-kill-buffer gnus-original-article-buffer)))
5670     (cond (gnus-kill-summary-on-exit
5671            (when (and gnus-use-trees
5672                       (gnus-buffer-exists-p buffer))
5673              (save-excursion
5674                (set-buffer buffer)
5675                (gnus-tree-close gnus-newsgroup-name)))
5676            (gnus-kill-buffer buffer))
5677           ((gnus-buffer-exists-p buffer)
5678            (save-excursion
5679              (set-buffer buffer)
5680              (gnus-deaden-summary))))))
5681
5682 (defun gnus-summary-wake-up-the-dead (&rest args)
5683   "Wake up the dead summary buffer."
5684   (interactive)
5685   (gnus-dead-summary-mode -1)
5686   (let ((name (buffer-name)))
5687     (when (string-match "Dead " name)
5688       (rename-buffer
5689        (concat (substring name 0 (match-beginning 0))
5690                (substring name (match-end 0)))
5691        t)))
5692   (gnus-message 3 "This dead summary is now alive again"))
5693
5694 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5695 (defun gnus-summary-fetch-faq (&optional faq-dir)
5696   "Fetch the FAQ for the current group.
5697 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5698 in."
5699   (interactive
5700    (list
5701     (when current-prefix-arg
5702       (completing-read
5703        "Faq dir: " (and (listp gnus-group-faq-directory)
5704                         (mapcar (lambda (file) (list file))
5705                                 gnus-group-faq-directory))))))
5706   (let (gnus-faq-buffer)
5707     (when (setq gnus-faq-buffer
5708                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5709       (gnus-configure-windows 'summary-faq))))
5710
5711 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5712 (defun gnus-summary-describe-group (&optional force)
5713   "Describe the current newsgroup."
5714   (interactive "P")
5715   (gnus-group-describe-group force gnus-newsgroup-name))
5716
5717 (defun gnus-summary-describe-briefly ()
5718   "Describe summary mode commands briefly."
5719   (interactive)
5720   (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")))
5721
5722 ;; Walking around group mode buffer from summary mode.
5723
5724 (defun gnus-summary-next-group (&optional no-article target-group backward)
5725   "Exit current newsgroup and then select next unread newsgroup.
5726 If prefix argument NO-ARTICLE is non-nil, no article is selected
5727 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5728 previous group instead."
5729   (interactive "P")
5730   ;; Stop pre-fetching.
5731   (gnus-async-halt-prefetch)
5732   (let ((current-group gnus-newsgroup-name)
5733         (current-buffer (current-buffer))
5734         entered)
5735     ;; First we semi-exit this group to update Xrefs and all variables.
5736     ;; We can't do a real exit, because the window conf must remain
5737     ;; the same in case the user is prompted for info, and we don't
5738     ;; want the window conf to change before that...
5739     (gnus-summary-exit t)
5740     (while (not entered)
5741       ;; Then we find what group we are supposed to enter.
5742       (set-buffer gnus-group-buffer)
5743       (gnus-group-jump-to-group current-group)
5744       (setq target-group
5745             (or target-group
5746                 (if (eq gnus-keep-same-level 'best)
5747                     (gnus-summary-best-group gnus-newsgroup-name)
5748                   (gnus-summary-search-group backward gnus-keep-same-level))))
5749       (if (not target-group)
5750           ;; There are no further groups, so we return to the group
5751           ;; buffer.
5752           (progn
5753             (gnus-message 5 "Returning to the group buffer")
5754             (setq entered t)
5755             (when (gnus-buffer-live-p current-buffer)
5756               (set-buffer current-buffer)
5757               (gnus-summary-exit))
5758             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5759         ;; We try to enter the target group.
5760         (gnus-group-jump-to-group target-group)
5761         (let ((unreads (gnus-group-group-unread)))
5762           (if (and (or (eq t unreads)
5763                        (and unreads (not (zerop unreads))))
5764                    (gnus-summary-read-group
5765                     target-group nil no-article
5766                     (and (buffer-name current-buffer) current-buffer)
5767                     nil backward))
5768               (setq entered t)
5769             (setq current-group target-group
5770                   target-group nil)))))))
5771
5772 (defun gnus-summary-prev-group (&optional no-article)
5773   "Exit current newsgroup and then select previous unread newsgroup.
5774 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5775   (interactive "P")
5776   (gnus-summary-next-group no-article nil t))
5777
5778 ;; Walking around summary lines.
5779
5780 (defun gnus-summary-first-subject (&optional unread undownloaded)
5781   "Go to the first unread subject.
5782 If UNREAD is non-nil, go to the first unread article.
5783 Returns the article selected or nil if there are no unread articles."
5784   (interactive "P")
5785   (prog1
5786       (cond
5787        ;; Empty summary.
5788        ((null gnus-newsgroup-data)
5789         (gnus-message 3 "No articles in the group")
5790         nil)
5791        ;; Pick the first article.
5792        ((not unread)
5793         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5794         (gnus-data-number (car gnus-newsgroup-data)))
5795        ;; No unread articles.
5796        ((null gnus-newsgroup-unreads)
5797         (gnus-message 3 "No more unread articles")
5798         nil)
5799        ;; Find the first unread article.
5800        (t
5801         (let ((data gnus-newsgroup-data))
5802           (while (and data
5803                       (and (not (and undownloaded
5804                                      (eq gnus-undownloaded-mark
5805                                          (gnus-data-mark (car data)))))
5806                            (not (gnus-data-unread-p (car data)))))
5807             (setq data (cdr data)))
5808           (when data
5809             (goto-char (gnus-data-pos (car data)))
5810             (gnus-data-number (car data))))))
5811     (gnus-summary-position-point)))
5812
5813 (defun gnus-summary-next-subject (n &optional unread dont-display)
5814   "Go to next N'th summary line.
5815 If N is negative, go to the previous N'th subject line.
5816 If UNREAD is non-nil, only unread articles are selected.
5817 The difference between N and the actual number of steps taken is
5818 returned."
5819   (interactive "p")
5820   (let ((backward (< n 0))
5821         (n (abs n)))
5822     (while (and (> n 0)
5823                 (if backward
5824                     (gnus-summary-find-prev unread)
5825                   (gnus-summary-find-next unread)))
5826       (unless (zerop (setq n (1- n)))
5827         (gnus-summary-show-thread)))
5828     (when (/= 0 n)
5829       (gnus-message 7 "No more%s articles"
5830                     (if unread " unread" "")))
5831     (unless dont-display
5832       (gnus-summary-recenter)
5833       (gnus-summary-position-point))
5834     n))
5835
5836 (defun gnus-summary-next-unread-subject (n)
5837   "Go to next N'th unread summary line."
5838   (interactive "p")
5839   (gnus-summary-next-subject n t))
5840
5841 (defun gnus-summary-prev-subject (n &optional unread)
5842   "Go to previous N'th summary line.
5843 If optional argument UNREAD is non-nil, only unread article is selected."
5844   (interactive "p")
5845   (gnus-summary-next-subject (- n) unread))
5846
5847 (defun gnus-summary-prev-unread-subject (n)
5848   "Go to previous N'th unread summary line."
5849   (interactive "p")
5850   (gnus-summary-next-subject (- n) t))
5851
5852 (defun gnus-summary-goto-subject (article &optional force silent)
5853   "Go the subject line of ARTICLE.
5854 If FORCE, also allow jumping to articles not currently shown."
5855   (interactive "nArticle number: ")
5856   (let ((b (point))
5857         (data (gnus-data-find article)))
5858     ;; We read in the article if we have to.
5859     (and (not data)
5860          force
5861          (gnus-summary-insert-subject
5862           article
5863           (if (or (numberp force) (vectorp force)) force)
5864           t)
5865          (setq data (gnus-data-find article)))
5866     (goto-char b)
5867     (if (not data)
5868         (progn
5869           (unless silent
5870             (gnus-message 3 "Can't find article %d" article))
5871           nil)
5872       (goto-char (gnus-data-pos data))
5873       (gnus-summary-position-point)
5874       article)))
5875
5876 ;; Walking around summary lines with displaying articles.
5877
5878 (defun gnus-summary-expand-window (&optional arg)
5879   "Make the summary buffer take up the entire Emacs frame.
5880 Given a prefix, will force an `article' buffer configuration."
5881   (interactive "P")
5882   (if arg
5883       (gnus-configure-windows 'article 'force)
5884     (gnus-configure-windows 'summary 'force)))
5885
5886 (defun gnus-summary-display-article (article &optional all-header)
5887   "Display ARTICLE in article buffer."
5888   (gnus-set-global-variables)
5889   (when (gnus-buffer-live-p gnus-article-buffer)
5890     (with-current-buffer gnus-article-buffer
5891       (setq gnus-article-charset gnus-newsgroup-charset)
5892       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
5893       (mm-enable-multibyte-mule4)))
5894   (if (null article)
5895       nil
5896     (prog1
5897         (if gnus-summary-display-article-function
5898             (funcall gnus-summary-display-article-function article all-header)
5899           (gnus-article-prepare article all-header))
5900       (gnus-run-hooks 'gnus-select-article-hook)
5901       (when (and gnus-current-article
5902                  (not (zerop gnus-current-article)))
5903         (gnus-summary-goto-subject gnus-current-article))
5904       (gnus-summary-recenter)
5905       (when (and gnus-use-trees gnus-show-threads)
5906         (gnus-possibly-generate-tree article)
5907         (gnus-highlight-selected-tree article))
5908       ;; Successfully display article.
5909       (gnus-article-set-window-start
5910        (cdr (assq article gnus-newsgroup-bookmarks))))))
5911
5912 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5913   "Select the current article.
5914 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5915 non-nil, the article will be re-fetched even if it already present in
5916 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5917 be displayed."
5918   ;; Make sure we are in the summary buffer to work around bbdb bug.
5919   (unless (eq major-mode 'gnus-summary-mode)
5920     (set-buffer gnus-summary-buffer))
5921   (let ((article (or article (gnus-summary-article-number)))
5922         (all-headers (not (not all-headers))) ;Must be T or NIL.
5923         gnus-summary-display-article-function)
5924     (and (not pseudo)
5925          (gnus-summary-article-pseudo-p article)
5926          (error "This is a pseudo-article"))
5927     (save-excursion
5928       (set-buffer gnus-summary-buffer)
5929       (if (or (and gnus-single-article-buffer
5930                    (or (null gnus-current-article)
5931                        (null gnus-article-current)
5932                        (null (get-buffer gnus-article-buffer))
5933                        (not (eq article (cdr gnus-article-current)))
5934                        (not (equal (car gnus-article-current)
5935                                    gnus-newsgroup-name))))
5936               (and (not gnus-single-article-buffer)
5937                    (or (null gnus-current-article)
5938                        (not (eq gnus-current-article article))))
5939               force)
5940           ;; The requested article is different from the current article.
5941           (progn
5942             (gnus-summary-display-article article all-headers)
5943             (when (gnus-buffer-live-p gnus-article-buffer)
5944                (with-current-buffer gnus-article-buffer
5945                 (if (not gnus-article-decoded-p) ;; a local variable
5946                     (mm-disable-multibyte-mule4))))
5947             (when (or all-headers gnus-show-all-headers)
5948               (gnus-article-show-all-headers))
5949             (gnus-article-set-window-start
5950              (cdr (assq article gnus-newsgroup-bookmarks)))
5951             article)
5952         (when (or all-headers gnus-show-all-headers)
5953           (gnus-article-show-all-headers))
5954         'old))))
5955
5956 (defun gnus-summary-force-verify-and-decrypt ()
5957   (interactive)
5958   (let ((mm-verify-option 'known)
5959         (mm-decrypt-option 'known))
5960     (gnus-summary-select-article nil 'force)))
5961
5962 (defun gnus-summary-set-current-mark (&optional current-mark)
5963   "Obsolete function."
5964   nil)
5965
5966 (defun gnus-summary-next-article (&optional unread subject backward push)
5967   "Select the next article.
5968 If UNREAD, only unread articles are selected.
5969 If SUBJECT, only articles with SUBJECT are selected.
5970 If BACKWARD, the previous article is selected instead of the next."
5971   (interactive "P")
5972   (cond
5973    ;; Is there such an article?
5974    ((and (gnus-summary-search-forward unread subject backward)
5975          (or (gnus-summary-display-article (gnus-summary-article-number))
5976              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5977     (gnus-summary-position-point))
5978    ;; If not, we try the first unread, if that is wanted.
5979    ((and subject
5980          gnus-auto-select-same
5981          (gnus-summary-first-unread-article))
5982     (gnus-summary-position-point)
5983     (gnus-message 6 "Wrapped"))
5984    ;; Try to get next/previous article not displayed in this group.
5985    ((and gnus-auto-extend-newsgroup
5986          (not unread) (not subject))
5987     (gnus-summary-goto-article
5988      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5989      nil (count-lines (point-min) (point))))
5990    ;; Go to next/previous group.
5991    (t
5992     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5993       (gnus-summary-jump-to-group gnus-newsgroup-name))
5994     (let ((cmd last-command-char)
5995           (point
5996            (save-excursion
5997              (set-buffer gnus-group-buffer)
5998              (point)))
5999           (group
6000            (if (eq gnus-keep-same-level 'best)
6001                (gnus-summary-best-group gnus-newsgroup-name)
6002              (gnus-summary-search-group backward gnus-keep-same-level))))
6003       ;; For some reason, the group window gets selected.  We change
6004       ;; it back.
6005       (select-window (get-buffer-window (current-buffer)))
6006       ;; Select next unread newsgroup automagically.
6007       (cond
6008        ((or (not gnus-auto-select-next)
6009             (not cmd))
6010         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6011        ((or (eq gnus-auto-select-next 'quietly)
6012             (and (eq gnus-auto-select-next 'slightly-quietly)
6013                  push)
6014             (and (eq gnus-auto-select-next 'almost-quietly)
6015                  (gnus-summary-last-article-p)))
6016         ;; Select quietly.
6017         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6018             (gnus-summary-exit)
6019           (gnus-message 7 "No more%s articles (%s)..."
6020                         (if unread " unread" "")
6021                         (if group (concat "selecting " group)
6022                           "exiting"))
6023           (gnus-summary-next-group nil group backward)))
6024        (t
6025         (when (gnus-key-press-event-p last-input-event)
6026           (gnus-summary-walk-group-buffer
6027            gnus-newsgroup-name cmd unread backward point))))))))
6028
6029 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6030   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6031                       (?\C-p (gnus-group-prev-unread-group 1))))
6032         (cursor-in-echo-area t)
6033         keve key group ended)
6034     (save-excursion
6035       (set-buffer gnus-group-buffer)
6036       (goto-char start)
6037       (setq group
6038             (if (eq gnus-keep-same-level 'best)
6039                 (gnus-summary-best-group gnus-newsgroup-name)
6040               (gnus-summary-search-group backward gnus-keep-same-level))))
6041     (while (not ended)
6042       (gnus-message
6043        5 "No more%s articles%s" (if unread " unread" "")
6044        (if (and group
6045                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6046            (format " (Type %s for %s [%s])"
6047                    (single-key-description cmd) group
6048                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6049          (format " (Type %s to exit %s)"
6050                  (single-key-description cmd)
6051                  gnus-newsgroup-name)))
6052       ;; Confirm auto selection.
6053       (setq key (car (setq keve (gnus-read-event-char))))
6054       (setq ended t)
6055       (cond
6056        ((assq key keystrokes)
6057         (let ((obuf (current-buffer)))
6058           (switch-to-buffer gnus-group-buffer)
6059           (when group
6060             (gnus-group-jump-to-group group))
6061           (eval (cadr (assq key keystrokes)))
6062           (setq group (gnus-group-group-name))
6063           (switch-to-buffer obuf))
6064         (setq ended nil))
6065        ((equal key cmd)
6066         (if (or (not group)
6067                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6068             (gnus-summary-exit)
6069           (gnus-summary-next-group nil group backward)))
6070        (t
6071         (push (cdr keve) unread-command-events))))))
6072
6073 (defun gnus-summary-next-unread-article ()
6074   "Select unread article after current one."
6075   (interactive)
6076   (gnus-summary-next-article
6077    (or (not (eq gnus-summary-goto-unread 'never))
6078        (gnus-summary-last-article-p (gnus-summary-article-number)))
6079    (and gnus-auto-select-same
6080         (gnus-summary-article-subject))))
6081
6082 (defun gnus-summary-prev-article (&optional unread subject)
6083   "Select the article after the current one.
6084 If UNREAD is non-nil, only unread articles are selected."
6085   (interactive "P")
6086   (gnus-summary-next-article unread subject t))
6087
6088 (defun gnus-summary-prev-unread-article ()
6089   "Select unread article before current one."
6090   (interactive)
6091   (gnus-summary-prev-article
6092    (or (not (eq gnus-summary-goto-unread 'never))
6093        (gnus-summary-first-article-p (gnus-summary-article-number)))
6094    (and gnus-auto-select-same
6095         (gnus-summary-article-subject))))
6096
6097 (defun gnus-summary-next-page (&optional lines circular)
6098   "Show next page of the selected article.
6099 If at the end of the current article, select the next article.
6100 LINES says how many lines should be scrolled up.
6101
6102 If CIRCULAR is non-nil, go to the start of the article instead of
6103 selecting the next article when reaching the end of the current
6104 article."
6105   (interactive "P")
6106   (setq gnus-summary-buffer (current-buffer))
6107   (gnus-set-global-variables)
6108   (let ((article (gnus-summary-article-number))
6109         (article-window (get-buffer-window gnus-article-buffer t))
6110         endp)
6111     ;; If the buffer is empty, we have no article.
6112     (unless article
6113       (error "No article to select"))
6114     (gnus-configure-windows 'article)
6115     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6116         (if (and (eq gnus-summary-goto-unread 'never)
6117                  (not (gnus-summary-last-article-p article)))
6118             (gnus-summary-next-article)
6119           (gnus-summary-next-unread-article))
6120       (if (or (null gnus-current-article)
6121               (null gnus-article-current)
6122               (/= article (cdr gnus-article-current))
6123               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6124           ;; Selected subject is different from current article's.
6125           (gnus-summary-display-article article)
6126         (when article-window
6127           (gnus-eval-in-buffer-window gnus-article-buffer
6128             (setq endp (gnus-article-next-page lines)))
6129           (when endp
6130             (cond (circular
6131                    (gnus-summary-beginning-of-article))
6132                   (lines
6133                    (gnus-message 3 "End of message"))
6134                   ((null lines)
6135                    (if (and (eq gnus-summary-goto-unread 'never)
6136                             (not (gnus-summary-last-article-p article)))
6137                        (gnus-summary-next-article)
6138                      (gnus-summary-next-unread-article))))))))
6139     (gnus-summary-recenter)
6140     (gnus-summary-position-point)))
6141
6142 (defun gnus-summary-prev-page (&optional lines move)
6143   "Show previous page of selected article.
6144 Argument LINES specifies lines to be scrolled down.
6145 If MOVE, move to the previous unread article if point is at
6146 the beginning of the buffer."
6147   (interactive "P")
6148   (let ((article (gnus-summary-article-number))
6149         (article-window (get-buffer-window gnus-article-buffer t))
6150         endp)
6151     (gnus-configure-windows 'article)
6152     (if (or (null gnus-current-article)
6153             (null gnus-article-current)
6154             (/= article (cdr gnus-article-current))
6155             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6156         ;; Selected subject is different from current article's.
6157         (gnus-summary-display-article article)
6158       (gnus-summary-recenter)
6159       (when article-window
6160         (gnus-eval-in-buffer-window gnus-article-buffer
6161           (setq endp (gnus-article-prev-page lines)))
6162         (when (and move endp)
6163           (cond (lines
6164                  (gnus-message 3 "Beginning of message"))
6165                 ((null lines)
6166                  (if (and (eq gnus-summary-goto-unread 'never)
6167                           (not (gnus-summary-first-article-p article)))
6168                      (gnus-summary-prev-article)
6169                    (gnus-summary-prev-unread-article))))))))
6170   (gnus-summary-position-point))
6171
6172 (defun gnus-summary-prev-page-or-article (&optional lines)
6173   "Show previous page of selected article.
6174 Argument LINES specifies lines to be scrolled down.
6175 If at the beginning of the article, go to the next article."
6176   (interactive "P")
6177   (gnus-summary-prev-page lines t))
6178
6179 (defun gnus-summary-scroll-up (lines)
6180   "Scroll up (or down) one line current article.
6181 Argument LINES specifies lines to be scrolled up (or down if negative)."
6182   (interactive "p")
6183   (gnus-configure-windows 'article)
6184   (gnus-summary-show-thread)
6185   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6186     (gnus-eval-in-buffer-window gnus-article-buffer
6187       (cond ((> lines 0)
6188              (when (gnus-article-next-page lines)
6189                (gnus-message 3 "End of message")))
6190             ((< lines 0)
6191              (gnus-article-prev-page (- lines))))))
6192   (gnus-summary-recenter)
6193   (gnus-summary-position-point))
6194
6195 (defun gnus-summary-scroll-down (lines)
6196   "Scroll down (or up) one line current article.
6197 Argument LINES specifies lines to be scrolled down (or up if negative)."
6198   (interactive "p")
6199   (gnus-summary-scroll-up (- lines)))
6200
6201 (defun gnus-summary-next-same-subject ()
6202   "Select next article which has the same subject as current one."
6203   (interactive)
6204   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6205
6206 (defun gnus-summary-prev-same-subject ()
6207   "Select previous article which has the same subject as current one."
6208   (interactive)
6209   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6210
6211 (defun gnus-summary-next-unread-same-subject ()
6212   "Select next unread article which has the same subject as current one."
6213   (interactive)
6214   (gnus-summary-next-article t (gnus-summary-article-subject)))
6215
6216 (defun gnus-summary-prev-unread-same-subject ()
6217   "Select previous unread article which has the same subject as current one."
6218   (interactive)
6219   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6220
6221 (defun gnus-summary-first-unread-article ()
6222   "Select the first unread article.
6223 Return nil if there are no unread articles."
6224   (interactive)
6225   (prog1
6226       (when (gnus-summary-first-subject t)
6227         (gnus-summary-show-thread)
6228         (gnus-summary-first-subject t)
6229         (gnus-summary-display-article (gnus-summary-article-number)))
6230     (gnus-summary-position-point)))
6231
6232 (defun gnus-summary-first-unread-subject ()
6233   "Place the point on the subject line of the first unread article.
6234 Return nil if there are no unread articles."
6235   (interactive)
6236   (prog1
6237       (when (gnus-summary-first-subject t)
6238         (gnus-summary-show-thread)
6239         (gnus-summary-first-subject t))
6240     (gnus-summary-position-point)))
6241
6242 (defun gnus-summary-first-article ()
6243   "Select the first article.
6244 Return nil if there are no articles."
6245   (interactive)
6246   (prog1
6247       (when (gnus-summary-first-subject)
6248         (gnus-summary-show-thread)
6249         (gnus-summary-first-subject)
6250         (gnus-summary-display-article (gnus-summary-article-number)))
6251     (gnus-summary-position-point)))
6252
6253 (defun gnus-summary-best-unread-article ()
6254   "Select the unread article with the highest score."
6255   (interactive)
6256   (let ((best -1000000)
6257         (data gnus-newsgroup-data)
6258         article score)
6259     (while data
6260       (and (gnus-data-unread-p (car data))
6261            (> (setq score
6262                     (gnus-summary-article-score (gnus-data-number (car data))))
6263               best)
6264            (setq best score
6265                  article (gnus-data-number (car data))))
6266       (setq data (cdr data)))
6267     (prog1
6268         (if article
6269             (gnus-summary-goto-article article)
6270           (error "No unread articles"))
6271       (gnus-summary-position-point))))
6272
6273 (defun gnus-summary-last-subject ()
6274   "Go to the last displayed subject line in the group."
6275   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6276     (when article
6277       (gnus-summary-goto-subject article))))
6278
6279 (defun gnus-summary-goto-article (article &optional all-headers force)
6280   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6281 If ALL-HEADERS is non-nil, no header lines are hidden.
6282 If FORCE, go to the article even if it isn't displayed.  If FORCE
6283 is a number, it is the line the article is to be displayed on."
6284   (interactive
6285    (list
6286     (completing-read
6287      "Article number or Message-ID: "
6288      (mapcar (lambda (number) (list (int-to-string number)))
6289              gnus-newsgroup-limit))
6290     current-prefix-arg
6291     t))
6292   (prog1
6293       (if (and (stringp article)
6294                (string-match "@" article))
6295           (gnus-summary-refer-article article)
6296         (when (stringp article)
6297           (setq article (string-to-number article)))
6298         (if (gnus-summary-goto-subject article force)
6299             (gnus-summary-display-article article all-headers)
6300           (gnus-message 4 "Couldn't go to article %s" article) nil))
6301     (gnus-summary-position-point)))
6302
6303 (defun gnus-summary-goto-last-article ()
6304   "Go to the previously read article."
6305   (interactive)
6306   (prog1
6307       (when gnus-last-article
6308         (gnus-summary-goto-article gnus-last-article nil t))
6309     (gnus-summary-position-point)))
6310
6311 (defun gnus-summary-pop-article (number)
6312   "Pop one article off the history and go to the previous.
6313 NUMBER articles will be popped off."
6314   (interactive "p")
6315   (let (to)
6316     (setq gnus-newsgroup-history
6317           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6318     (if to
6319         (gnus-summary-goto-article (car to) nil t)
6320       (error "Article history empty")))
6321   (gnus-summary-position-point))
6322
6323 ;; Summary commands and functions for limiting the summary buffer.
6324
6325 (defun gnus-summary-limit-to-articles (n)
6326   "Limit the summary buffer to the next N articles.
6327 If not given a prefix, use the process marked articles instead."
6328   (interactive "P")
6329   (prog1
6330       (let ((articles (gnus-summary-work-articles n)))
6331         (setq gnus-newsgroup-processable nil)
6332         (gnus-summary-limit articles))
6333     (gnus-summary-position-point)))
6334
6335 (defun gnus-summary-pop-limit (&optional total)
6336   "Restore the previous limit.
6337 If given a prefix, remove all limits."
6338   (interactive "P")
6339   (when total
6340     (setq gnus-newsgroup-limits
6341           (list (mapcar (lambda (h) (mail-header-number h))
6342                         gnus-newsgroup-headers))))
6343   (unless gnus-newsgroup-limits
6344     (error "No limit to pop"))
6345   (prog1
6346       (gnus-summary-limit nil 'pop)
6347     (gnus-summary-position-point)))
6348
6349 (defun gnus-summary-limit-to-subject (subject &optional header)
6350   "Limit the summary buffer to articles that have subjects that match a regexp."
6351   (interactive "sLimit to subject (regexp): ")
6352   (unless header
6353     (setq header "subject"))
6354   (when (not (equal "" subject))
6355     (prog1
6356         (let ((articles (gnus-summary-find-matching
6357                          (or header "subject") subject 'all)))
6358           (unless articles
6359             (error "Found no matches for \"%s\"" subject))
6360           (gnus-summary-limit articles))
6361       (gnus-summary-position-point))))
6362
6363 (defun gnus-summary-limit-to-author (from)
6364   "Limit the summary buffer to articles that have authors that match a regexp."
6365   (interactive "sLimit to author (regexp): ")
6366   (gnus-summary-limit-to-subject from "from"))
6367
6368 (defun gnus-summary-limit-to-age (age &optional younger-p)
6369   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6370 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6371 articles that are younger than AGE days."
6372   (interactive
6373    (let ((younger current-prefix-arg)
6374          (days-got nil)
6375          days)
6376      (while (not days-got)
6377        (setq days (if younger
6378                       (read-string "Limit to articles within (in days): ")
6379                     (read-string "Limit to articles old than (in days): ")))
6380        (when (> (length days) 0)
6381          (setq days (read days)))
6382        (if (numberp days)
6383            (setq days-got t)
6384          (message "Please enter a number.")
6385          (sleep-for 1)))
6386      (list days younger)))
6387   (prog1
6388       (let ((data gnus-newsgroup-data)
6389             (cutoff (days-to-time age))
6390             articles d date is-younger)
6391         (while (setq d (pop data))
6392           (when (and (vectorp (gnus-data-header d))
6393                      (setq date (mail-header-date (gnus-data-header d))))
6394             (setq is-younger (time-less-p
6395                               (time-since (condition-case ()
6396                                               (date-to-time date)
6397                                             (error '(0 0))))
6398                               cutoff))
6399             (when (if younger-p
6400                       is-younger
6401                     (not is-younger))
6402               (push (gnus-data-number d) articles))))
6403         (gnus-summary-limit (nreverse articles)))
6404     (gnus-summary-position-point)))
6405
6406 (defun gnus-summary-limit-to-extra (header regexp)
6407   "Limit the summary buffer to articles that match an 'extra' header."
6408   (interactive
6409    (let ((header
6410           (intern
6411            (gnus-completing-read
6412             (symbol-name (car gnus-extra-headers))
6413             "Limit extra header:"
6414             (mapcar (lambda (x)
6415                       (cons (symbol-name x) x))
6416                     gnus-extra-headers)
6417             nil
6418             t))))
6419      (list header
6420            (read-string (format "Limit to header %s (regexp): " header)))))
6421   (when (not (equal "" regexp))
6422     (prog1
6423         (let ((articles (gnus-summary-find-matching
6424                          (cons 'extra header) regexp 'all)))
6425           (unless articles
6426             (error "Found no matches for \"%s\"" regexp))
6427           (gnus-summary-limit articles))
6428       (gnus-summary-position-point))))
6429
6430 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6431 (make-obsolete
6432  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6433
6434 (defun gnus-summary-limit-to-unread (&optional all)
6435   "Limit the summary buffer to articles that are not marked as read.
6436 If ALL is non-nil, limit strictly to unread articles."
6437   (interactive "P")
6438   (if all
6439       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6440     (gnus-summary-limit-to-marks
6441      ;; Concat all the marks that say that an article is read and have
6442      ;; those removed.
6443      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6444            gnus-killed-mark gnus-kill-file-mark
6445            gnus-low-score-mark gnus-expirable-mark
6446            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6447            gnus-duplicate-mark gnus-souped-mark)
6448      'reverse)))
6449
6450 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6451 (make-obsolete 'gnus-summary-delete-marked-with
6452                'gnus-summary-limit-exlude-marks)
6453
6454 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6455   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6456 If REVERSE, limit the summary buffer to articles that are marked
6457 with MARKS.  MARKS can either be a string of marks or a list of marks.
6458 Returns how many articles were removed."
6459   (interactive "sMarks: ")
6460   (gnus-summary-limit-to-marks marks t))
6461
6462 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6463   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6464 If REVERSE (the prefix), limit the summary buffer to articles that are
6465 not marked with MARKS.  MARKS can either be a string of marks or a
6466 list of marks.
6467 Returns how many articles were removed."
6468   (interactive "sMarks: \nP")
6469   (prog1
6470       (let ((data gnus-newsgroup-data)
6471             (marks (if (listp marks) marks
6472                      (append marks nil))) ; Transform to list.
6473             articles)
6474         (while data
6475           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6476                   (memq (gnus-data-mark (car data)) marks))
6477             (push (gnus-data-number (car data)) articles))
6478           (setq data (cdr data)))
6479         (gnus-summary-limit articles))
6480     (gnus-summary-position-point)))
6481
6482 (defun gnus-summary-limit-to-score (&optional score)
6483   "Limit to articles with score at or above SCORE."
6484   (interactive "P")
6485   (setq score (if score
6486                   (prefix-numeric-value score)
6487                 (or gnus-summary-default-score 0)))
6488   (let ((data gnus-newsgroup-data)
6489         articles)
6490     (while data
6491       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6492                 score)
6493         (push (gnus-data-number (car data)) articles))
6494       (setq data (cdr data)))
6495     (prog1
6496         (gnus-summary-limit articles)
6497       (gnus-summary-position-point))))
6498
6499 (defun gnus-summary-limit-include-thread (id)
6500   "Display all the hidden articles that in the current thread."
6501   (interactive (list (mail-header-id (gnus-summary-article-header))))
6502   (let ((articles (gnus-articles-in-thread
6503                    (gnus-id-to-thread (gnus-root-id id)))))
6504     (prog1
6505         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6506       (gnus-summary-position-point))))
6507
6508 (defun gnus-summary-limit-include-dormant ()
6509   "Display all the hidden articles that are marked as dormant.
6510 Note that this command only works on a subset of the articles currently
6511 fetched for this group."
6512   (interactive)
6513   (unless gnus-newsgroup-dormant
6514     (error "There are no dormant articles in this group"))
6515   (prog1
6516       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6517     (gnus-summary-position-point)))
6518
6519 (defun gnus-summary-limit-exclude-dormant ()
6520   "Hide all dormant articles."
6521   (interactive)
6522   (prog1
6523       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6524     (gnus-summary-position-point)))
6525
6526 (defun gnus-summary-limit-exclude-childless-dormant ()
6527   "Hide all dormant articles that have no children."
6528   (interactive)
6529   (let ((data (gnus-data-list t))
6530         articles d children)
6531     ;; Find all articles that are either not dormant or have
6532     ;; children.
6533     (while (setq d (pop data))
6534       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6535                 (and (setq children
6536                            (gnus-article-children (gnus-data-number d)))
6537                      (let (found)
6538                        (while children
6539                          (when (memq (car children) articles)
6540                            (setq children nil
6541                                  found t))
6542                          (pop children))
6543                        found)))
6544         (push (gnus-data-number d) articles)))
6545     ;; Do the limiting.
6546     (prog1
6547         (gnus-summary-limit articles)
6548       (gnus-summary-position-point))))
6549
6550 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6551   "Mark all unread excluded articles as read.
6552 If ALL, mark even excluded ticked and dormants as read."
6553   (interactive "P")
6554   (let ((articles (gnus-sorted-complement
6555                    (sort
6556                     (mapcar (lambda (h) (mail-header-number h))
6557                             gnus-newsgroup-headers)
6558                     '<)
6559                    (sort gnus-newsgroup-limit '<)))
6560         article)
6561     (setq gnus-newsgroup-unreads
6562           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6563     (if all
6564         (setq gnus-newsgroup-dormant nil
6565               gnus-newsgroup-marked nil
6566               gnus-newsgroup-reads
6567               (nconc
6568                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6569                gnus-newsgroup-reads))
6570       (while (setq article (pop articles))
6571         (unless (or (memq article gnus-newsgroup-dormant)
6572                     (memq article gnus-newsgroup-marked))
6573           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6574
6575 (defun gnus-summary-limit (articles &optional pop)
6576   (if pop
6577       ;; We pop the previous limit off the stack and use that.
6578       (setq articles (car gnus-newsgroup-limits)
6579             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6580     ;; We use the new limit, so we push the old limit on the stack.
6581     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6582   ;; Set the limit.
6583   (setq gnus-newsgroup-limit articles)
6584   (let ((total (length gnus-newsgroup-data))
6585         (data (gnus-data-find-list (gnus-summary-article-number)))
6586         (gnus-summary-mark-below nil)   ; Inhibit this.
6587         found)
6588     ;; This will do all the work of generating the new summary buffer
6589     ;; according to the new limit.
6590     (gnus-summary-prepare)
6591     ;; Hide any threads, possibly.
6592     (and gnus-show-threads
6593          gnus-thread-hide-subtree
6594          (gnus-summary-hide-all-threads))
6595     ;; Try to return to the article you were at, or one in the
6596     ;; neighborhood.
6597     (when data
6598       ;; We try to find some article after the current one.
6599       (while data
6600         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6601           (setq data nil
6602                 found t))
6603         (setq data (cdr data))))
6604     (unless found
6605       ;; If there is no data, that means that we were after the last
6606       ;; article.  The same goes when we can't find any articles
6607       ;; after the current one.
6608       (goto-char (point-max))
6609       (gnus-summary-find-prev))
6610     (gnus-set-mode-line 'summary)
6611     ;; We return how many articles were removed from the summary
6612     ;; buffer as a result of the new limit.
6613     (- total (length gnus-newsgroup-data))))
6614
6615 (defsubst gnus-invisible-cut-children (threads)
6616   (let ((num 0))
6617     (while threads
6618       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6619         (incf num))
6620       (pop threads))
6621     (< num 2)))
6622
6623 (defsubst gnus-cut-thread (thread)
6624   "Go forwards in the thread until we find an article that we want to display."
6625   (when (or (eq gnus-fetch-old-headers 'some)
6626             (eq gnus-fetch-old-headers 'invisible)
6627             (numberp gnus-fetch-old-headers)
6628             (eq gnus-build-sparse-threads 'some)
6629             (eq gnus-build-sparse-threads 'more))
6630     ;; Deal with old-fetched headers and sparse threads.
6631     (while (and
6632             thread
6633             (or
6634              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6635              (gnus-summary-article-ancient-p
6636               (mail-header-number (car thread))))
6637             (if (or (<= (length (cdr thread)) 1)
6638                     (eq gnus-fetch-old-headers 'invisible))
6639                 (setq gnus-newsgroup-limit
6640                       (delq (mail-header-number (car thread))
6641                             gnus-newsgroup-limit)
6642                       thread (cadr thread))
6643               (when (gnus-invisible-cut-children (cdr thread))
6644                 (let ((th (cdr thread)))
6645                   (while th
6646                     (if (memq (mail-header-number (caar th))
6647                               gnus-newsgroup-limit)
6648                         (setq thread (car th)
6649                               th nil)
6650                       (setq th (cdr th))))))))))
6651   thread)
6652
6653 (defun gnus-cut-threads (threads)
6654   "Cut off all uninteresting articles from the beginning of threads."
6655   (when (or (eq gnus-fetch-old-headers 'some)
6656             (eq gnus-fetch-old-headers 'invisible)
6657             (numberp gnus-fetch-old-headers)
6658             (eq gnus-build-sparse-threads 'some)
6659             (eq gnus-build-sparse-threads 'more))
6660     (let ((th threads))
6661       (while th
6662         (setcar th (gnus-cut-thread (car th)))
6663         (setq th (cdr th)))))
6664   ;; Remove nixed out threads.
6665   (delq nil threads))
6666
6667 (defun gnus-summary-initial-limit (&optional show-if-empty)
6668   "Figure out what the initial limit is supposed to be on group entry.
6669 This entails weeding out unwanted dormants, low-scored articles,
6670 fetch-old-headers verbiage, and so on."
6671   ;; Most groups have nothing to remove.
6672   (if (or gnus-inhibit-limiting
6673           (and (null gnus-newsgroup-dormant)
6674                (not (eq gnus-fetch-old-headers 'some))
6675                (not (numberp gnus-fetch-old-headers))
6676                (not (eq gnus-fetch-old-headers 'invisible))
6677                (null gnus-summary-expunge-below)
6678                (not (eq gnus-build-sparse-threads 'some))
6679                (not (eq gnus-build-sparse-threads 'more))
6680                (null gnus-thread-expunge-below)
6681                (not gnus-use-nocem)))
6682       ()                                ; Do nothing.
6683     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6684     (setq gnus-newsgroup-limit nil)
6685     (mapatoms
6686      (lambda (node)
6687        (unless (car (symbol-value node))
6688          ;; These threads have no parents -- they are roots.
6689          (let ((nodes (cdr (symbol-value node)))
6690                thread)
6691            (while nodes
6692              (if (and gnus-thread-expunge-below
6693                       (< (gnus-thread-total-score (car nodes))
6694                          gnus-thread-expunge-below))
6695                  (gnus-expunge-thread (pop nodes))
6696                (setq thread (pop nodes))
6697                (gnus-summary-limit-children thread))))))
6698      gnus-newsgroup-dependencies)
6699     ;; If this limitation resulted in an empty group, we might
6700     ;; pop the previous limit and use it instead.
6701     (when (and (not gnus-newsgroup-limit)
6702                show-if-empty)
6703       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6704     gnus-newsgroup-limit))
6705
6706 (defun gnus-summary-limit-children (thread)
6707   "Return 1 if this subthread is visible and 0 if it is not."
6708   ;; First we get the number of visible children to this thread.  This
6709   ;; is done by recursing down the thread using this function, so this
6710   ;; will really go down to a leaf article first, before slowly
6711   ;; working its way up towards the root.
6712   (when thread
6713     (let ((children
6714            (if (cdr thread)
6715                (apply '+ (mapcar 'gnus-summary-limit-children
6716                                  (cdr thread)))
6717              0))
6718           (number (mail-header-number (car thread)))
6719           score)
6720       (if (and
6721            (not (memq number gnus-newsgroup-marked))
6722            (or
6723             ;; If this article is dormant and has absolutely no visible
6724             ;; children, then this article isn't visible.
6725             (and (memq number gnus-newsgroup-dormant)
6726                  (zerop children))
6727             ;; If this is "fetch-old-headered" and there is no
6728             ;; visible children, then we don't want this article.
6729             (and (or (eq gnus-fetch-old-headers 'some)
6730                      (numberp gnus-fetch-old-headers))
6731                  (gnus-summary-article-ancient-p number)
6732                  (zerop children))
6733             ;; If this is "fetch-old-headered" and `invisible', then
6734             ;; we don't want this article.
6735             (and (eq gnus-fetch-old-headers 'invisible)
6736                  (gnus-summary-article-ancient-p number))
6737             ;; If this is a sparsely inserted article with no children,
6738             ;; we don't want it.
6739             (and (eq gnus-build-sparse-threads 'some)
6740                  (gnus-summary-article-sparse-p number)
6741                  (zerop children))
6742             ;; If we use expunging, and this article is really
6743             ;; low-scored, then we don't want this article.
6744             (when (and gnus-summary-expunge-below
6745                        (< (setq score
6746                                 (or (cdr (assq number gnus-newsgroup-scored))
6747                                     gnus-summary-default-score))
6748                           gnus-summary-expunge-below))
6749               ;; We increase the expunge-tally here, but that has
6750               ;; nothing to do with the limits, really.
6751               (incf gnus-newsgroup-expunged-tally)
6752               ;; We also mark as read here, if that's wanted.
6753               (when (and gnus-summary-mark-below
6754                          (< score gnus-summary-mark-below))
6755                 (setq gnus-newsgroup-unreads
6756                       (delq number gnus-newsgroup-unreads))
6757                 (if gnus-newsgroup-auto-expire
6758                     (push number gnus-newsgroup-expirable)
6759                   (push (cons number gnus-low-score-mark)
6760                         gnus-newsgroup-reads)))
6761               t)
6762             ;; Check NoCeM things.
6763             (if (and gnus-use-nocem
6764                      (gnus-nocem-unwanted-article-p
6765                       (mail-header-id (car thread))))
6766                 (progn
6767                   (setq gnus-newsgroup-unreads
6768                         (delq number gnus-newsgroup-unreads))
6769                   t))))
6770           ;; Nope, invisible article.
6771           0
6772         ;; Ok, this article is to be visible, so we add it to the limit
6773         ;; and return 1.
6774         (push number gnus-newsgroup-limit)
6775         1))))
6776
6777 (defun gnus-expunge-thread (thread)
6778   "Mark all articles in THREAD as read."
6779   (let* ((number (mail-header-number (car thread))))
6780     (incf gnus-newsgroup-expunged-tally)
6781     ;; We also mark as read here, if that's wanted.
6782     (setq gnus-newsgroup-unreads
6783           (delq number gnus-newsgroup-unreads))
6784     (if gnus-newsgroup-auto-expire
6785         (push number gnus-newsgroup-expirable)
6786       (push (cons number gnus-low-score-mark)
6787             gnus-newsgroup-reads)))
6788   ;; Go recursively through all subthreads.
6789   (mapcar 'gnus-expunge-thread (cdr thread)))
6790
6791 ;; Summary article oriented commands
6792
6793 (defun gnus-summary-refer-parent-article (n)
6794   "Refer parent article N times.
6795 If N is negative, go to ancestor -N instead.
6796 The difference between N and the number of articles fetched is returned."
6797   (interactive "p")
6798   (let ((skip 1)
6799         error header ref)
6800     (when (not (natnump n))
6801       (setq skip (abs n)
6802             n 1))
6803     (while (and (> n 0)
6804                 (not error))
6805       (setq header (gnus-summary-article-header))
6806       (if (and (eq (mail-header-number header)
6807                    (cdr gnus-article-current))
6808                (equal gnus-newsgroup-name
6809                       (car gnus-article-current)))
6810           ;; If we try to find the parent of the currently
6811           ;; displayed article, then we take a look at the actual
6812           ;; References header, since this is slightly more
6813           ;; reliable than the References field we got from the
6814           ;; server.
6815           (save-excursion
6816             (set-buffer gnus-original-article-buffer)
6817             (nnheader-narrow-to-headers)
6818             (unless (setq ref (message-fetch-field "references"))
6819               (setq ref (message-fetch-field "in-reply-to")))
6820             (widen))
6821         (setq ref
6822               ;; It's not the current article, so we take a bet on
6823               ;; the value we got from the server.
6824               (mail-header-references header)))
6825       (if (and ref
6826                (not (equal ref "")))
6827           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6828             (gnus-message 1 "Couldn't find parent"))
6829         (gnus-message 1 "No references in article %d"
6830                       (gnus-summary-article-number))
6831         (setq error t))
6832       (decf n))
6833     (gnus-summary-position-point)
6834     n))
6835
6836 (defun gnus-summary-refer-references ()
6837   "Fetch all articles mentioned in the References header.
6838 Return the number of articles fetched."
6839   (interactive)
6840   (let ((ref (mail-header-references (gnus-summary-article-header)))
6841         (current (gnus-summary-article-number))
6842         (n 0))
6843     (if (or (not ref)
6844             (equal ref ""))
6845         (error "No References in the current article")
6846       ;; For each Message-ID in the References header...
6847       (while (string-match "<[^>]*>" ref)
6848         (incf n)
6849         ;; ... fetch that article.
6850         (gnus-summary-refer-article
6851          (prog1 (match-string 0 ref)
6852            (setq ref (substring ref (match-end 0))))))
6853       (gnus-summary-goto-subject current)
6854       (gnus-summary-position-point)
6855       n)))
6856
6857 (defun gnus-summary-refer-thread (&optional limit)
6858   "Fetch all articles in the current thread.
6859 If LIMIT (the numerical prefix), fetch that many old headers instead
6860 of what's specified by the `gnus-refer-thread-limit' variable."
6861   (interactive "P")
6862   (let ((id (mail-header-id (gnus-summary-article-header)))
6863         (limit (if limit (prefix-numeric-value limit)
6864                  gnus-refer-thread-limit)))
6865     ;; We want to fetch LIMIT *old* headers, but we also have to
6866     ;; re-fetch all the headers in the current buffer, because many of
6867     ;; them may be undisplayed.  So we adjust LIMIT.
6868     (when (numberp limit)
6869       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6870     (unless (eq gnus-fetch-old-headers 'invisible)
6871       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6872       ;; Retrieve the headers and read them in.
6873       (if (eq (gnus-retrieve-headers
6874                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6875               'nov)
6876           (gnus-build-all-threads)
6877         (error "Can't fetch thread from backends that don't support NOV"))
6878       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6879     (gnus-summary-limit-include-thread id)))
6880
6881 (defun gnus-summary-refer-article (message-id)
6882   "Fetch an article specified by MESSAGE-ID."
6883   (interactive "sMessage-ID: ")
6884   (when (and (stringp message-id)
6885              (not (zerop (length message-id))))
6886     ;; Construct the correct Message-ID if necessary.
6887     ;; Suggested by tale@pawl.rpi.edu.
6888     (unless (string-match "^<" message-id)
6889       (setq message-id (concat "<" message-id)))
6890     (unless (string-match ">$" message-id)
6891       (setq message-id (concat message-id ">")))
6892     (let* ((header (gnus-id-to-header message-id))
6893            (sparse (and header
6894                         (gnus-summary-article-sparse-p
6895                          (mail-header-number header))
6896                         (memq (mail-header-number header)
6897                               gnus-newsgroup-limit)))
6898            number)
6899       (cond
6900        ;; If the article is present in the buffer we just go to it.
6901        ((and header
6902              (or (not (gnus-summary-article-sparse-p
6903                        (mail-header-number header)))
6904                  sparse))
6905         (prog1
6906             (gnus-summary-goto-article
6907              (mail-header-number header) nil t)
6908           (when sparse
6909             (gnus-summary-update-article (mail-header-number header)))))
6910        (t
6911         ;; We fetch the article.
6912         (catch 'found
6913           (dolist (gnus-override-method (gnus-refer-article-methods))
6914             (gnus-check-server gnus-override-method)
6915             ;; Fetch the header, and display the article.
6916             (when (setq number (gnus-summary-insert-subject message-id))
6917               (gnus-summary-select-article nil nil nil number)
6918               (throw 'found t)))
6919           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6920
6921 (defun gnus-refer-article-methods ()
6922   "Return a list of referrable methods."
6923   (cond
6924    ;; No method, so we default to current and native.
6925    ((null gnus-refer-article-method)
6926     (list gnus-current-select-method gnus-select-method))
6927    ;; Current.
6928    ((eq 'current gnus-refer-article-method)
6929     (list gnus-current-select-method))
6930    ;; List of select methods.
6931    ((not (and (symbolp (car gnus-refer-article-method))
6932               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6933     (let (out)
6934       (dolist (method gnus-refer-article-method)
6935         (push (if (eq 'current method)
6936                   gnus-current-select-method
6937                 method)
6938               out))
6939       (nreverse out)))
6940    ;; One single select method.
6941    (t
6942     (list gnus-refer-article-method))))
6943
6944 (defun gnus-summary-edit-parameters ()
6945   "Edit the group parameters of the current group."
6946   (interactive)
6947   (gnus-group-edit-group gnus-newsgroup-name 'params))
6948
6949 (defun gnus-summary-customize-parameters ()
6950   "Customize the group parameters of the current group."
6951   (interactive)
6952   (gnus-group-customize gnus-newsgroup-name))
6953
6954 (defun gnus-summary-enter-digest-group (&optional force)
6955   "Enter an nndoc group based on the current article.
6956 If FORCE, force a digest interpretation.  If not, try
6957 to guess what the document format is."
6958   (interactive "P")
6959   (let ((conf gnus-current-window-configuration))
6960     (save-excursion
6961       (gnus-summary-select-article))
6962     (setq gnus-current-window-configuration conf)
6963     (let* ((name (format "%s-%d"
6964                          (gnus-group-prefixed-name
6965                           gnus-newsgroup-name (list 'nndoc ""))
6966                          (save-excursion
6967                            (set-buffer gnus-summary-buffer)
6968                            gnus-current-article)))
6969            (ogroup gnus-newsgroup-name)
6970            (params (append (gnus-info-params (gnus-get-info ogroup))
6971                            (list (cons 'to-group ogroup))
6972                            (list (cons 'save-article-group ogroup))))
6973            (case-fold-search t)
6974            (buf (current-buffer))
6975            dig to-address)
6976       (save-excursion
6977         (set-buffer gnus-original-article-buffer)
6978         ;; Have the digest group inherit the main mail address of
6979         ;; the parent article.
6980         (when (setq to-address (or (message-fetch-field "reply-to")
6981                                    (message-fetch-field "from")))
6982           (setq params (append 
6983                         (list (cons 'to-address 
6984                                     (funcall gnus-decode-encoded-word-function
6985                                              to-address))))))
6986         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6987         (insert-buffer-substring gnus-original-article-buffer)
6988         ;; Remove lines that may lead nndoc to misinterpret the
6989         ;; document type.
6990         (narrow-to-region
6991          (goto-char (point-min))
6992          (or (search-forward "\n\n" nil t) (point)))
6993         (goto-char (point-min))
6994         (delete-matching-lines "^Path:\\|^From ")
6995         (widen))
6996       (unwind-protect
6997           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6998                     (gnus-newsgroup-ephemeral-ignored-charsets
6999                      gnus-newsgroup-ignored-charsets))
7000                 (gnus-group-read-ephemeral-group
7001                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7002                               (nndoc-article-type
7003                                ,(if force 'mbox 'guess))) t))
7004               ;; Make all postings to this group go to the parent group.
7005               (nconc (gnus-info-params (gnus-get-info name))
7006                      params)
7007             ;; Couldn't select this doc group.
7008             (switch-to-buffer buf)
7009             (gnus-set-global-variables)
7010             (gnus-configure-windows 'summary)
7011             (gnus-message 3 "Article couldn't be entered?"))
7012         (kill-buffer dig)))))
7013
7014 (defun gnus-summary-read-document (n)
7015   "Open a new group based on the current article(s).
7016 This will allow you to read digests and other similar
7017 documents as newsgroups.
7018 Obeys the standard process/prefix convention."
7019   (interactive "P")
7020   (let* ((articles (gnus-summary-work-articles n))
7021          (ogroup gnus-newsgroup-name)
7022          (params (append (gnus-info-params (gnus-get-info ogroup))
7023                          (list (cons 'to-group ogroup))))
7024          article group egroup groups vgroup)
7025     (while (setq article (pop articles))
7026       (setq group (format "%s-%d" gnus-newsgroup-name article))
7027       (gnus-summary-remove-process-mark article)
7028       (when (gnus-summary-display-article article)
7029         (save-excursion
7030           (with-temp-buffer
7031             (insert-buffer-substring gnus-original-article-buffer)
7032             ;; Remove some headers that may lead nndoc to make
7033             ;; the wrong guess.
7034             (message-narrow-to-head)
7035             (goto-char (point-min))
7036             (delete-matching-lines "^\\(Path\\):\\|^From ")
7037             (widen)
7038             (if (setq egroup
7039                       (gnus-group-read-ephemeral-group
7040                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7041                                      (nndoc-article-type guess))
7042                        t nil t))
7043                 (progn
7044                   ;; Make all postings to this group go to the parent group.
7045                   (nconc (gnus-info-params (gnus-get-info egroup))
7046                          params)
7047                   (push egroup groups))
7048               ;; Couldn't select this doc group.
7049               (gnus-error 3 "Article couldn't be entered"))))))
7050     ;; Now we have selected all the documents.
7051     (cond
7052      ((not groups)
7053       (error "None of the articles could be interpreted as documents"))
7054      ((gnus-group-read-ephemeral-group
7055        (setq vgroup (format
7056                      "nnvirtual:%s-%s" gnus-newsgroup-name
7057                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7058        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7059        t
7060        (cons (current-buffer) 'summary)))
7061      (t
7062       (error "Couldn't select virtual nndoc group")))))
7063
7064 (defun gnus-summary-isearch-article (&optional regexp-p)
7065   "Do incremental search forward on the current article.
7066 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7067   (interactive "P")
7068   (gnus-summary-select-article)
7069   (gnus-configure-windows 'article)
7070   (gnus-eval-in-buffer-window gnus-article-buffer
7071     (save-restriction
7072       (widen)
7073       (isearch-forward regexp-p))))
7074
7075 (defun gnus-summary-search-article-forward (regexp &optional backward)
7076   "Search for an article containing REGEXP forward.
7077 If BACKWARD, search backward instead."
7078   (interactive
7079    (list (read-string
7080           (format "Search article %s (regexp%s): "
7081                   (if current-prefix-arg "backward" "forward")
7082                   (if gnus-last-search-regexp
7083                       (concat ", default " gnus-last-search-regexp)
7084                     "")))
7085          current-prefix-arg))
7086   (if (string-equal regexp "")
7087       (setq regexp (or gnus-last-search-regexp ""))
7088     (setq gnus-last-search-regexp regexp)
7089     (setq gnus-article-before-search gnus-current-article))
7090   ;; Intentionally set gnus-last-article.
7091   (setq gnus-last-article gnus-article-before-search)
7092   (let ((gnus-last-article gnus-last-article))
7093     (if (gnus-summary-search-article regexp backward)
7094         (gnus-summary-show-thread)
7095       (error "Search failed: \"%s\"" regexp))))
7096
7097 (defun gnus-summary-search-article-backward (regexp)
7098   "Search for an article containing REGEXP backward."
7099   (interactive
7100    (list (read-string
7101           (format "Search article backward (regexp%s): "
7102                   (if gnus-last-search-regexp
7103                       (concat ", default " gnus-last-search-regexp)
7104                     "")))))
7105   (gnus-summary-search-article-forward regexp 'backward))
7106
7107 (defun gnus-summary-search-article (regexp &optional backward)
7108   "Search for an article containing REGEXP.
7109 Optional argument BACKWARD means do search for backward.
7110 `gnus-select-article-hook' is not called during the search."
7111   ;; We have to require this here to make sure that the following
7112   ;; dynamic binding isn't shadowed by autoloading.
7113   (require 'gnus-async)
7114   (require 'gnus-art)
7115   (let ((gnus-select-article-hook nil)  ;Disable hook.
7116         (gnus-article-prepare-hook nil)
7117         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7118         (gnus-use-article-prefetch nil)
7119         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7120         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7121         (sum (current-buffer))
7122         (gnus-display-mime-function nil)
7123         (found nil)
7124         point)
7125     (gnus-save-hidden-threads
7126       (gnus-summary-select-article)
7127       (set-buffer gnus-article-buffer)
7128       (goto-char (window-point (get-buffer-window (current-buffer))))
7129       (when backward
7130         (forward-line -1))
7131       (while (not found)
7132         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7133         (if (if backward
7134                 (re-search-backward regexp nil t)
7135               (re-search-forward regexp nil t))
7136             ;; We found the regexp.
7137             (progn
7138               (setq found 'found)
7139               (beginning-of-line)
7140               (set-window-start
7141                (get-buffer-window (current-buffer))
7142                (point))
7143               (forward-line 1)
7144               (set-window-point
7145                (get-buffer-window (current-buffer))
7146                (point))
7147               (set-buffer sum)
7148               (setq point (point)))
7149           ;; We didn't find it, so we go to the next article.
7150           (set-buffer sum)
7151           (setq found 'not)
7152           (while (eq found 'not)
7153             (if (not (if backward (gnus-summary-find-prev)
7154                        (gnus-summary-find-next)))
7155                 ;; No more articles.
7156                 (setq found t)
7157               ;; Select the next article and adjust point.
7158               (unless (gnus-summary-article-sparse-p
7159                        (gnus-summary-article-number))
7160                 (setq found nil)
7161                 (gnus-summary-select-article)
7162                 (set-buffer gnus-article-buffer)
7163                 (widen)
7164                 (goto-char (if backward (point-max) (point-min))))))))
7165       (gnus-message 7 ""))
7166     ;; Return whether we found the regexp.
7167     (when (eq found 'found)
7168       (goto-char point)
7169       (gnus-summary-show-thread)
7170       (gnus-summary-goto-subject gnus-current-article)
7171       (gnus-summary-position-point)
7172       t)))
7173
7174 (defun gnus-summary-find-matching (header regexp &optional backward unread
7175                                           not-case-fold)
7176   "Return a list of all articles that match REGEXP on HEADER.
7177 The search stars on the current article and goes forwards unless
7178 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7179 If UNREAD is non-nil, only unread articles will
7180 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7181 in the comparisons."
7182   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7183                 (gnus-data-find-list
7184                  (gnus-summary-article-number) (gnus-data-list backward))))
7185         (case-fold-search (not not-case-fold))
7186         articles d func)
7187     (if (consp header)
7188         (if (eq (car header) 'extra)
7189             (setq func
7190                   `(lambda (h)
7191                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7192                          "")))
7193           (error "%s is an invalid header" header))
7194       (unless (fboundp (intern (concat "mail-header-" header)))
7195         (error "%s is not a valid header" header))
7196       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7197     (while data
7198       (setq d (car data))
7199       (and (or (not unread)             ; We want all articles...
7200                (gnus-data-unread-p d))  ; Or just unreads.
7201            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7202            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7203            (push (gnus-data-number d) articles)) ; Success!
7204       (setq data (cdr data)))
7205     (nreverse articles)))
7206
7207 (defun gnus-summary-execute-command (header regexp command &optional backward)
7208   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7209 If HEADER is an empty string (or nil), the match is done on the entire
7210 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7211   (interactive
7212    (list (let ((completion-ignore-case t))
7213            (completing-read
7214             "Header name: "
7215             (mapcar (lambda (string) (list string))
7216                     '("Number" "Subject" "From" "Lines" "Date"
7217                       "Message-ID" "Xref" "References" "Body"))
7218             nil 'require-match))
7219          (read-string "Regexp: ")
7220          (read-key-sequence "Command: ")
7221          current-prefix-arg))
7222   (when (equal header "Body")
7223     (setq header ""))
7224   ;; Hidden thread subtrees must be searched as well.
7225   (gnus-summary-show-all-threads)
7226   ;; We don't want to change current point nor window configuration.
7227   (save-excursion
7228     (save-window-excursion
7229       (gnus-message 6 "Executing %s..." (key-description command))
7230       ;; We'd like to execute COMMAND interactively so as to give arguments.
7231       (gnus-execute header regexp
7232                     `(call-interactively ',(key-binding command))
7233                     backward)
7234       (gnus-message 6 "Executing %s...done" (key-description command)))))
7235
7236 (defun gnus-summary-beginning-of-article ()
7237   "Scroll the article back to the beginning."
7238   (interactive)
7239   (gnus-summary-select-article)
7240   (gnus-configure-windows 'article)
7241   (gnus-eval-in-buffer-window gnus-article-buffer
7242     (widen)
7243     (goto-char (point-min))
7244     (when gnus-page-broken
7245       (gnus-narrow-to-page))))
7246
7247 (defun gnus-summary-end-of-article ()
7248   "Scroll to the end of the article."
7249   (interactive)
7250   (gnus-summary-select-article)
7251   (gnus-configure-windows 'article)
7252   (gnus-eval-in-buffer-window gnus-article-buffer
7253     (widen)
7254     (goto-char (point-max))
7255     (recenter -3)
7256     (when gnus-page-broken
7257       (gnus-narrow-to-page))))
7258
7259 (defun gnus-summary-print-article (&optional filename n)
7260   "Generate and print a PostScript image of the N next (mail) articles.
7261
7262 If N is negative, print the N previous articles.  If N is nil and articles
7263 have been marked with the process mark, print these instead.
7264
7265 If the optional first argument FILENAME is nil, send the image to the
7266 printer.  If FILENAME is a string, save the PostScript image in a file with
7267 that name.  If FILENAME is a number, prompt the user for the name of the file
7268 to save in."
7269   (interactive (list (ps-print-preprint current-prefix-arg)
7270                      current-prefix-arg))
7271   (dolist (article (gnus-summary-work-articles n))
7272     (gnus-summary-select-article nil nil 'pseudo article)
7273     (gnus-eval-in-buffer-window gnus-article-buffer
7274       (let ((buffer (generate-new-buffer " *print*")))
7275         (unwind-protect
7276             (progn
7277               (copy-to-buffer buffer (point-min) (point-max))
7278               (set-buffer buffer)
7279               (gnus-article-delete-invisible-text)
7280               (let ((ps-left-header
7281                      (list
7282                       (concat "("
7283                               (mail-header-subject gnus-current-headers) ")")
7284                       (concat "("
7285                               (mail-header-from gnus-current-headers) ")")))
7286                     (ps-right-header
7287                      (list
7288                       "/pagenumberstring load"
7289                       (concat "("
7290                               (mail-header-date gnus-current-headers) ")"))))
7291                 (gnus-run-hooks 'gnus-ps-print-hook)
7292                 (save-excursion
7293                   (ps-print-buffer-with-faces filename))))
7294           (kill-buffer buffer))))))
7295
7296 (defun gnus-summary-show-article (&optional arg)
7297   "Force re-fetching of the current article.
7298 If ARG (the prefix) is a number, show the article with the charset
7299 defined in `gnus-summary-show-article-charset-alist', or the charset
7300 inputed.
7301 If ARG (the prefix) is non-nil and not a number, show the raw article
7302 without any article massaging functions being run."
7303   (interactive "P")
7304   (cond
7305    ((numberp arg)
7306     (let ((gnus-newsgroup-charset
7307            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7308                (read-coding-system "Charset: ")))
7309           (gnus-newsgroup-ignored-charsets 'gnus-all))
7310       (gnus-summary-select-article nil 'force)
7311       (let ((deps gnus-newsgroup-dependencies)
7312             head header)
7313         (save-excursion
7314           (set-buffer gnus-original-article-buffer)
7315           (save-restriction
7316             (message-narrow-to-head)
7317             (setq head (buffer-string)))
7318           (with-temp-buffer
7319             (insert (format "211 %d Article retrieved.\n"
7320                             (cdr gnus-article-current)))
7321             (insert head)
7322             (insert ".\n")
7323             (let ((nntp-server-buffer (current-buffer)))
7324               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7325         (gnus-data-set-header
7326          (gnus-data-find (cdr gnus-article-current))
7327          header)
7328         (gnus-summary-update-article-line
7329          (cdr gnus-article-current) header))))
7330    ((not arg)
7331     ;; Select the article the normal way.
7332     (gnus-summary-select-article nil 'force))
7333    (t
7334     ;; We have to require this here to make sure that the following
7335     ;; dynamic binding isn't shadowed by autoloading.
7336     (require 'gnus-async)
7337     (require 'gnus-art)
7338     ;; Bind the article treatment functions to nil.
7339     (let ((gnus-have-all-headers t)
7340           gnus-article-prepare-hook
7341           gnus-article-decode-hook
7342           gnus-display-mime-function
7343           gnus-break-pages)
7344       ;; Destroy any MIME parts.
7345       (when (gnus-buffer-live-p gnus-article-buffer)
7346         (save-excursion
7347           (set-buffer gnus-article-buffer)
7348           (mm-destroy-parts gnus-article-mime-handles)
7349           ;; Set it to nil for safety reason.
7350           (setq gnus-article-mime-handle-alist nil)
7351           (setq gnus-article-mime-handles nil)))
7352       (gnus-summary-select-article nil 'force))))
7353   (gnus-summary-goto-subject gnus-current-article)
7354   (gnus-summary-position-point))
7355
7356 (defun gnus-summary-verbose-headers (&optional arg)
7357   "Toggle permanent full header display.
7358 If ARG is a positive number, turn header display on.
7359 If ARG is a negative number, turn header display off."
7360   (interactive "P")
7361   (setq gnus-show-all-headers
7362         (cond ((or (not (numberp arg))
7363                    (zerop arg))
7364                (not gnus-show-all-headers))
7365               ((natnump arg)
7366                t)))
7367   (gnus-summary-show-article))
7368
7369 (defun gnus-summary-toggle-header (&optional arg)
7370   "Show the headers if they are hidden, or hide them if they are shown.
7371 If ARG is a positive number, show the entire header.
7372 If ARG is a negative number, hide the unwanted header lines."
7373   (interactive "P")
7374   (save-excursion
7375     (set-buffer gnus-article-buffer)
7376     (save-restriction
7377       (let* ((buffer-read-only nil)
7378              (inhibit-point-motion-hooks t)
7379              hidden e)
7380         (setq hidden
7381               (if (numberp arg)
7382                   (>= arg 0)
7383                 (save-restriction
7384                   (article-narrow-to-head)
7385                   (gnus-article-hidden-text-p 'headers))))
7386         (goto-char (point-min))
7387         (when (search-forward "\n\n" nil t)
7388           (delete-region (point-min) (1- (point))))
7389         (goto-char (point-min))
7390         (save-excursion
7391           (set-buffer gnus-original-article-buffer)
7392           (goto-char (point-min))
7393           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7394         (insert-buffer-substring gnus-original-article-buffer 1 e)
7395         (save-restriction
7396           (narrow-to-region (point-min) (point))
7397           (article-decode-encoded-words)
7398           (if  hidden
7399               (let ((gnus-treat-hide-headers nil)
7400                     (gnus-treat-hide-boring-headers nil))
7401                 (setq gnus-article-wash-types
7402                       (delq 'headers gnus-article-wash-types))
7403                 (gnus-treat-article 'head))
7404             (gnus-treat-article 'head)))
7405         (gnus-set-mode-line 'article)))))
7406
7407 (defun gnus-summary-show-all-headers ()
7408   "Make all header lines visible."
7409   (interactive)
7410   (gnus-article-show-all-headers))
7411
7412 (defun gnus-summary-caesar-message (&optional arg)
7413   "Caesar rotate the current article by 13.
7414 The numerical prefix specifies how many places to rotate each letter
7415 forward."
7416   (interactive "P")
7417   (gnus-summary-select-article)
7418   (let ((mail-header-separator ""))
7419     (gnus-eval-in-buffer-window gnus-article-buffer
7420       (save-restriction
7421         (widen)
7422         (let ((start (window-start))
7423               buffer-read-only)
7424           (message-caesar-buffer-body arg)
7425           (set-window-start (get-buffer-window (current-buffer)) start))))))
7426
7427 (defun gnus-summary-stop-page-breaking ()
7428   "Stop page breaking in the current article."
7429   (interactive)
7430   (gnus-summary-select-article)
7431   (gnus-eval-in-buffer-window gnus-article-buffer
7432     (widen)
7433     (when (gnus-visual-p 'page-marker)
7434       (let ((buffer-read-only nil))
7435         (gnus-remove-text-with-property 'gnus-prev)
7436         (gnus-remove-text-with-property 'gnus-next))
7437       (setq gnus-page-broken nil))))
7438
7439 (defun gnus-summary-move-article (&optional n to-newsgroup
7440                                             select-method action)
7441   "Move the current article to a different newsgroup.
7442 If N is a positive number, move the N next articles.
7443 If N is a negative number, move the N previous articles.
7444 If N is nil and any articles have been marked with the process mark,
7445 move those articles instead.
7446 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7447 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7448 re-spool using this method.
7449
7450 For this function to work, both the current newsgroup and the
7451 newsgroup that you want to move to have to support the `request-move'
7452 and `request-accept' functions.
7453
7454 ACTION can be either `move' (the default), `crosspost' or `copy'."
7455   (interactive "P")
7456   (unless action
7457     (setq action 'move))
7458   ;; Disable marking as read.
7459   (let (gnus-mark-article-hook)
7460     (save-window-excursion
7461       (gnus-summary-select-article)))
7462   ;; Check whether the source group supports the required functions.
7463   (cond ((and (eq action 'move)
7464               (not (gnus-check-backend-function
7465                     'request-move-article gnus-newsgroup-name)))
7466          (error "The current group does not support article moving"))
7467         ((and (eq action 'crosspost)
7468               (not (gnus-check-backend-function
7469                     'request-replace-article gnus-newsgroup-name)))
7470          (error "The current group does not support article editing")))
7471   (let ((articles (gnus-summary-work-articles n))
7472         (prefix (if (gnus-check-backend-function
7473                     'request-move-article gnus-newsgroup-name)
7474                     (gnus-group-real-prefix gnus-newsgroup-name)
7475                   ""))
7476         (names '((move "Move" "Moving")
7477                  (copy "Copy" "Copying")
7478                  (crosspost "Crosspost" "Crossposting")))
7479         (copy-buf (save-excursion
7480                     (nnheader-set-temp-buffer " *copy article*")))
7481         art-group to-method new-xref article to-groups)
7482     (unless (assq action names)
7483       (error "Unknown action %s" action))
7484     ;; Read the newsgroup name.
7485     (when (and (not to-newsgroup)
7486                (not select-method))
7487       (setq to-newsgroup
7488             (gnus-read-move-group-name
7489              (cadr (assq action names))
7490              (symbol-value (intern (format "gnus-current-%s-group" action)))
7491              articles prefix))
7492       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7493     (setq to-method (or select-method
7494                         (gnus-server-to-method
7495                          (gnus-group-method to-newsgroup))))
7496     ;; Check the method we are to move this article to...
7497     (unless (gnus-check-backend-function
7498              'request-accept-article (car to-method))
7499       (error "%s does not support article copying" (car to-method)))
7500     (unless (gnus-check-server to-method)
7501       (error "Can't open server %s" (car to-method)))
7502     (gnus-message 6 "%s to %s: %s..."
7503                   (caddr (assq action names))
7504                   (or (car select-method) to-newsgroup) articles)
7505     (while articles
7506       (setq article (pop articles))
7507       (setq
7508        art-group
7509        (cond
7510         ;; Move the article.
7511         ((eq action 'move)
7512          ;; Remove this article from future suppression.
7513          (gnus-dup-unsuppress-article article)
7514          (gnus-request-move-article
7515           article                       ; Article to move
7516           gnus-newsgroup-name           ; From newsgroup
7517           (nth 1 (gnus-find-method-for-group
7518                   gnus-newsgroup-name)) ; Server
7519           (list 'gnus-request-accept-article
7520                 to-newsgroup (list 'quote select-method)
7521                 (not articles) t)       ; Accept form
7522           (not articles)))              ; Only save nov last time
7523         ;; Copy the article.
7524         ((eq action 'copy)
7525          (save-excursion
7526            (set-buffer copy-buf)
7527            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7528              (gnus-request-accept-article
7529               to-newsgroup select-method (not articles) t))))
7530         ;; Crosspost the article.
7531         ((eq action 'crosspost)
7532          (let ((xref (message-tokenize-header
7533                       (mail-header-xref (gnus-summary-article-header article))
7534                       " ")))
7535            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7536                                   ":" article))
7537            (unless xref
7538              (setq xref (list (system-name))))
7539            (setq new-xref
7540                  (concat
7541                   (mapconcat 'identity
7542                              (delete "Xref:" (delete new-xref xref))
7543                              " ")
7544                   " " new-xref))
7545            (save-excursion
7546              (set-buffer copy-buf)
7547              ;; First put the article in the destination group.
7548              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7549              (when (consp (setq art-group
7550                                 (gnus-request-accept-article
7551                                  to-newsgroup select-method (not articles))))
7552                (setq new-xref (concat new-xref " " (car art-group)
7553                                       ":" (cdr art-group)))
7554                ;; Now we have the new Xrefs header, so we insert
7555                ;; it and replace the new article.
7556                (nnheader-replace-header "Xref" new-xref)
7557                (gnus-request-replace-article
7558                 (cdr art-group) to-newsgroup (current-buffer))
7559                art-group))))))
7560       (cond
7561        ((not art-group)
7562         (gnus-message 1 "Couldn't %s article %s: %s"
7563                       (cadr (assq action names)) article
7564                       (nnheader-get-report (car to-method))))
7565        ((eq art-group 'junk)
7566         (when (eq action 'move)
7567           (gnus-summary-mark-article article gnus-canceled-mark)
7568           (gnus-message 4 "Deleted article %s" article)))
7569        (t
7570         (let* ((pto-group (gnus-group-prefixed-name
7571                            (car art-group) to-method))
7572                (entry
7573                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7574                (info (nth 2 entry))
7575                (to-group (gnus-info-group info))
7576                to-marks)
7577           ;; Update the group that has been moved to.
7578           (when (and info
7579                      (memq action '(move copy)))
7580             (unless (member to-group to-groups)
7581               (push to-group to-groups))
7582
7583             (unless (memq article gnus-newsgroup-unreads)
7584               (push 'read to-marks)
7585               (gnus-info-set-read
7586                info (gnus-add-to-range (gnus-info-read info)
7587                                        (list (cdr art-group)))))
7588
7589             ;; See whether the article is to be put in the cache.
7590             (let ((marks gnus-article-mark-lists)
7591                   (to-article (cdr art-group)))
7592
7593               ;; Enter the article into the cache in the new group,
7594               ;; if that is required.
7595               (when gnus-use-cache
7596                 (gnus-cache-possibly-enter-article
7597                  to-group to-article
7598                  (memq article gnus-newsgroup-marked)
7599                  (memq article gnus-newsgroup-dormant)
7600                  (memq article gnus-newsgroup-unreads)))
7601
7602               (when gnus-preserve-marks
7603                 ;; Copy any marks over to the new group.
7604                 (when (and (equal to-group gnus-newsgroup-name)
7605                            (not (memq article gnus-newsgroup-unreads)))
7606                   ;; Mark this article as read in this group.
7607                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7608                   (setcdr (gnus-active to-group) to-article)
7609                   (setcdr gnus-newsgroup-active to-article))
7610
7611                 (while marks
7612                   (when (memq article (symbol-value
7613                                        (intern (format "gnus-newsgroup-%s"
7614                                                        (caar marks)))))
7615                     (push (cdar marks) to-marks)
7616                     ;; If the other group is the same as this group,
7617                     ;; then we have to add the mark to the list.
7618                     (when (equal to-group gnus-newsgroup-name)
7619                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7620                            (cons to-article
7621                                  (symbol-value
7622                                   (intern (format "gnus-newsgroup-%s"
7623                                                   (caar marks)))))))
7624                     ;; Copy the marks to other group.
7625                     (gnus-add-marked-articles
7626                      to-group (cdar marks) (list to-article) info))
7627                   (setq marks (cdr marks)))
7628
7629                 (gnus-request-set-mark to-group (list (list (list to-article)
7630                                                             'set
7631                                                             to-marks))))
7632
7633               (gnus-dribble-enter
7634                (concat "(gnus-group-set-info '"
7635                        (gnus-prin1-to-string (gnus-get-info to-group))
7636                        ")"))))
7637
7638           ;; Update the Xref header in this article to point to
7639           ;; the new crossposted article we have just created.
7640           (when (eq action 'crosspost)
7641             (save-excursion
7642               (set-buffer copy-buf)
7643               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7644               (nnheader-replace-header "Xref" new-xref)
7645               (gnus-request-replace-article
7646                article gnus-newsgroup-name (current-buffer)))))
7647
7648         ;;;!!!Why is this necessary?
7649         (set-buffer gnus-summary-buffer)
7650
7651         (gnus-summary-goto-subject article)
7652         (when (eq action 'move)
7653           (gnus-summary-mark-article article gnus-canceled-mark))))
7654       (gnus-summary-remove-process-mark article))
7655     ;; Re-activate all groups that have been moved to.
7656     (while to-groups
7657       (save-excursion
7658         (set-buffer gnus-group-buffer)
7659         (when (gnus-group-goto-group (car to-groups) t)
7660           (gnus-group-get-new-news-this-group 1 t))
7661         (pop to-groups)))
7662
7663     (gnus-kill-buffer copy-buf)
7664     (gnus-summary-position-point)
7665     (gnus-set-mode-line 'summary)))
7666
7667 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7668   "Move the current article to a different newsgroup.
7669 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7670 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7671 re-spool using this method."
7672   (interactive "P")
7673   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7674
7675 (defun gnus-summary-crosspost-article (&optional n)
7676   "Crosspost the current article to some other group."
7677   (interactive "P")
7678   (gnus-summary-move-article n nil nil 'crosspost))
7679
7680 (defcustom gnus-summary-respool-default-method nil
7681   "Default method for respooling an article.
7682 If nil, use to the current newsgroup method."
7683   :type '(choice (gnus-select-method :value (nnml ""))
7684                  (const nil))
7685   :group 'gnus-summary-mail)
7686
7687 (defun gnus-summary-respool-article (&optional n method)
7688   "Respool the current article.
7689 The article will be squeezed through the mail spooling process again,
7690 which means that it will be put in some mail newsgroup or other
7691 depending on `nnmail-split-methods'.
7692 If N is a positive number, respool the N next articles.
7693 If N is a negative number, respool the N previous articles.
7694 If N is nil and any articles have been marked with the process mark,
7695 respool those articles instead.
7696
7697 Respooling can be done both from mail groups and \"real\" newsgroups.
7698 In the former case, the articles in question will be moved from the
7699 current group into whatever groups they are destined to.  In the
7700 latter case, they will be copied into the relevant groups."
7701   (interactive
7702    (list current-prefix-arg
7703          (let* ((methods (gnus-methods-using 'respool))
7704                 (methname
7705                  (symbol-name (or gnus-summary-respool-default-method
7706                                   (car (gnus-find-method-for-group
7707                                         gnus-newsgroup-name)))))
7708                 (method
7709                  (gnus-completing-read
7710                   methname "What backend do you want to use when respooling?"
7711                   methods nil t nil 'gnus-mail-method-history))
7712                 ms)
7713            (cond
7714             ((zerop (length (setq ms (gnus-servers-using-backend
7715                                       (intern method)))))
7716              (list (intern method) ""))
7717             ((= 1 (length ms))
7718              (car ms))
7719             (t
7720              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7721                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7722                            ms-alist))))))))
7723   (unless method
7724     (error "No method given for respooling"))
7725   (if (assoc (symbol-name
7726               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7727              (gnus-methods-using 'respool))
7728       (gnus-summary-move-article n nil method)
7729     (gnus-summary-copy-article n nil method)))
7730
7731 (defun gnus-summary-import-article (file)
7732   "Import an arbitrary file into a mail newsgroup."
7733   (interactive "fImport file: ")
7734   (let ((group gnus-newsgroup-name)
7735         (now (current-time))
7736         atts lines)
7737     (unless (gnus-check-backend-function 'request-accept-article group)
7738       (error "%s does not support article importing" group))
7739     (or (file-readable-p file)
7740         (not (file-regular-p file))
7741         (error "Can't read %s" file))
7742     (save-excursion
7743       (set-buffer (gnus-get-buffer-create " *import file*"))
7744       (erase-buffer)
7745       (nnheader-insert-file-contents file)
7746       (goto-char (point-min))
7747       (unless (nnheader-article-p)
7748         ;; This doesn't look like an article, so we fudge some headers.
7749         (setq atts (file-attributes file)
7750               lines (count-lines (point-min) (point-max)))
7751         (insert "From: " (read-string "From: ") "\n"
7752                 "Subject: " (read-string "Subject: ") "\n"
7753                 "Date: " (message-make-date (nth 5 atts))
7754                 "\n"
7755                 "Message-ID: " (message-make-message-id) "\n"
7756                 "Lines: " (int-to-string lines) "\n"
7757                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7758       (gnus-request-accept-article group nil t)
7759       (kill-buffer (current-buffer)))))
7760
7761 (defun gnus-summary-article-posted-p ()
7762   "Say whether the current (mail) article is available from news as well.
7763 This will be the case if the article has both been mailed and posted."
7764   (interactive)
7765   (let ((id (mail-header-references (gnus-summary-article-header)))
7766         (gnus-override-method (car (gnus-refer-article-methods))))
7767     (if (gnus-request-head id "")
7768         (gnus-message 2 "The current message was found on %s"
7769                       gnus-override-method)
7770       (gnus-message 2 "The current message couldn't be found on %s"
7771                     gnus-override-method)
7772       nil)))
7773
7774 (defun gnus-summary-expire-articles (&optional now)
7775   "Expire all articles that are marked as expirable in the current group."
7776   (interactive)
7777   (when (gnus-check-backend-function
7778          'request-expire-articles gnus-newsgroup-name)
7779     ;; This backend supports expiry.
7780     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7781            (expirable (if total
7782                           (progn
7783                             ;; We need to update the info for
7784                             ;; this group for `gnus-list-of-read-articles'
7785                             ;; to give us the right answer.
7786                             (gnus-run-hooks 'gnus-exit-group-hook)
7787                             (gnus-summary-update-info)
7788                             (gnus-list-of-read-articles gnus-newsgroup-name))
7789                         (setq gnus-newsgroup-expirable
7790                               (sort gnus-newsgroup-expirable '<))))
7791            (expiry-wait (if now 'immediate
7792                           (gnus-group-find-parameter
7793                            gnus-newsgroup-name 'expiry-wait)))
7794            (nnmail-expiry-target
7795             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7796                 nnmail-expiry-target))
7797            es)
7798       (when expirable
7799         ;; There are expirable articles in this group, so we run them
7800         ;; through the expiry process.
7801         (gnus-message 6 "Expiring articles...")
7802         (unless (gnus-check-group gnus-newsgroup-name)
7803           (error "Can't open server for %s" gnus-newsgroup-name))
7804         ;; The list of articles that weren't expired is returned.
7805         (save-excursion
7806           (if expiry-wait
7807               (let ((nnmail-expiry-wait-function nil)
7808                     (nnmail-expiry-wait expiry-wait))
7809                 (setq es (gnus-request-expire-articles
7810                           expirable gnus-newsgroup-name)))
7811             (setq es (gnus-request-expire-articles
7812                       expirable gnus-newsgroup-name)))
7813           (unless total
7814             (setq gnus-newsgroup-expirable es))
7815           ;; We go through the old list of expirable, and mark all
7816           ;; really expired articles as nonexistent.
7817           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7818             (let ((gnus-use-cache nil))
7819               (while expirable
7820                 (unless (memq (car expirable) es)
7821                   (when (gnus-data-find (car expirable))
7822                     (gnus-summary-mark-article
7823                      (car expirable) gnus-canceled-mark)))
7824                 (setq expirable (cdr expirable))))))
7825         (gnus-message 6 "Expiring articles...done")))))
7826
7827 (defun gnus-summary-expire-articles-now ()
7828   "Expunge all expirable articles in the current group.
7829 This means that *all* articles that are marked as expirable will be
7830 deleted forever, right now."
7831   (interactive)
7832   (or gnus-expert-user
7833       (gnus-yes-or-no-p
7834        "Are you really, really, really sure you want to delete all these messages? ")
7835       (error "Phew!"))
7836   (gnus-summary-expire-articles t))
7837
7838 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7839 (defun gnus-summary-delete-article (&optional n)
7840   "Delete the N next (mail) articles.
7841 This command actually deletes articles.  This is not a marking
7842 command.  The article will disappear forever from your life, never to
7843 return.
7844 If N is negative, delete backwards.
7845 If N is nil and articles have been marked with the process mark,
7846 delete these instead."
7847   (interactive "P")
7848   (unless (gnus-check-backend-function 'request-expire-articles
7849                                        gnus-newsgroup-name)
7850     (error "The current newsgroup does not support article deletion"))
7851   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7852     (error "Couldn't open server"))
7853   ;; Compute the list of articles to delete.
7854   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7855         not-deleted)
7856     (if (and gnus-novice-user
7857              (not (gnus-yes-or-no-p
7858                    (format "Do you really want to delete %s forever? "
7859                            (if (> (length articles) 1)
7860                                (format "these %s articles" (length articles))
7861                              "this article")))))
7862         ()
7863       ;; Delete the articles.
7864       (setq not-deleted (gnus-request-expire-articles
7865                          articles gnus-newsgroup-name 'force))
7866       (while articles
7867         (gnus-summary-remove-process-mark (car articles))
7868         ;; The backend might not have been able to delete the article
7869         ;; after all.
7870         (unless (memq (car articles) not-deleted)
7871           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7872         (setq articles (cdr articles)))
7873       (when not-deleted
7874         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7875     (gnus-summary-position-point)
7876     (gnus-set-mode-line 'summary)
7877     not-deleted))
7878
7879 (defun gnus-summary-edit-article (&optional arg)
7880   "Edit the current article.
7881 This will have permanent effect only in mail groups.
7882 If ARG is nil, edit the decoded articles.
7883 If ARG is 1, edit the raw articles. 
7884 If ARG is 2, edit the raw articles even in read-only groups.
7885 If ARG is 3, edit the articles with the current handles.
7886 Otherwise, allow editing of articles even in read-only
7887 groups."
7888   (interactive "P")
7889   (let (force raw current-handles)
7890     (cond 
7891      ((null arg))
7892      ((eq arg 1) (setq raw t))
7893      ((eq arg 2) (setq raw t
7894                        force t))
7895      ((eq arg 3) (setq current-handles 
7896                        (and (gnus-buffer-live-p gnus-article-buffer)
7897                             (with-current-buffer gnus-article-buffer
7898                               (prog1
7899                                   gnus-article-mime-handles
7900                                   (setq gnus-article-mime-handles nil))))))
7901      (t (setq force t)))
7902     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7903         (error "Can't edit the raw article in group nndraft:drafts."))
7904     (save-excursion
7905       (set-buffer gnus-summary-buffer)
7906       (let ((mail-parse-charset gnus-newsgroup-charset)
7907             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7908         (gnus-set-global-variables)
7909         (when (and (not force)
7910                    (gnus-group-read-only-p))
7911           (error "The current newsgroup does not support article editing"))
7912         (gnus-summary-show-article t)
7913         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7914           (with-current-buffer gnus-article-buffer
7915             (mm-enable-multibyte-mule4)))
7916         (if (equal gnus-newsgroup-name "nndraft:drafts")
7917             (setq raw t))
7918         (gnus-article-edit-article
7919          (if raw 'ignore 
7920            `(lambda () 
7921               (let ((mbl mml-buffer-list))
7922                 (setq mml-buffer-list nil)
7923                 (mime-to-mml ,'current-handles)
7924                 (make-local-hook 'kill-buffer-hook)
7925                 (let ((mbl1 mml-buffer-list))
7926                   (setq mml-buffer-list mbl)
7927                   (set (make-local-variable 'mml-buffer-list) mbl1))
7928                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7929          `(lambda (no-highlight)
7930             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7931                   (message-options message-options)
7932                   (message-options-set-recipient)
7933                   (mail-parse-ignored-charsets 
7934                    ',gnus-newsgroup-ignored-charsets))
7935               ,(if (not raw) '(progn 
7936                                 (mml-to-mime)
7937                                 (mml-destroy-buffers)
7938                                 (remove-hook 'kill-buffer-hook 
7939                                              'mml-destroy-buffers t)
7940                                 (kill-local-variable 'mml-buffer-list)))
7941               (gnus-summary-edit-article-done
7942                ,(or (mail-header-references gnus-current-headers) "")
7943                ,(gnus-group-read-only-p) 
7944                ,gnus-summary-buffer no-highlight))))))))
7945
7946 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7947
7948 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7949                                                  no-highlight)
7950   "Make edits to the current article permanent."
7951   (interactive)
7952   (save-excursion
7953     ;; The buffer restriction contains the entire article if it exists.
7954     (when (article-goto-body)
7955       (let ((lines (count-lines (point) (point-max)))
7956             (length (- (point-max) (point)))
7957             (case-fold-search t)
7958             (body (copy-marker (point))))
7959         (goto-char (point-min))
7960         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
7961           (delete-region (match-beginning 1) (match-end 1))
7962           (insert (number-to-string length)))
7963         (goto-char (point-min))
7964         (when (re-search-forward
7965                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
7966           (delete-region (match-beginning 1) (match-end 1))
7967           (insert (number-to-string length)))
7968         (goto-char (point-min))
7969         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
7970           (delete-region (match-beginning 1) (match-end 1))
7971           (insert (number-to-string lines))))))
7972   ;; Replace the article.
7973   (let ((buf (current-buffer)))
7974     (with-temp-buffer
7975       (insert-buffer-substring buf)
7976       
7977       (if (and (not read-only)
7978                (not (gnus-request-replace-article
7979                      (cdr gnus-article-current) (car gnus-article-current)
7980                      (current-buffer) t)))
7981           (error "Couldn't replace article")
7982         ;; Update the summary buffer.
7983         (if (and references
7984                  (equal (message-tokenize-header references " ")
7985                         (message-tokenize-header
7986                          (or (message-fetch-field "references") "") " ")))
7987             ;; We only have to update this line.
7988             (save-excursion
7989               (save-restriction
7990                 (message-narrow-to-head)
7991                 (let ((head (buffer-string))
7992                       header)
7993                   (with-temp-buffer
7994                     (insert (format "211 %d Article retrieved.\n"
7995                                     (cdr gnus-article-current)))
7996                     (insert head)
7997                     (insert ".\n")
7998                     (let ((nntp-server-buffer (current-buffer)))
7999                       (setq header (car (gnus-get-newsgroup-headers
8000                                          (save-excursion
8001                                            (set-buffer gnus-summary-buffer)
8002                                            gnus-newsgroup-dependencies)
8003                                          t))))
8004                     (save-excursion
8005                       (set-buffer gnus-summary-buffer)
8006                       (gnus-data-set-header
8007                        (gnus-data-find (cdr gnus-article-current))
8008                        header)
8009                       (gnus-summary-update-article-line
8010                        (cdr gnus-article-current) header))))))
8011           ;; Update threads.
8012           (set-buffer (or buffer gnus-summary-buffer))
8013           (gnus-summary-update-article (cdr gnus-article-current)))
8014         ;; Prettify the article buffer again.
8015         (unless no-highlight
8016           (save-excursion
8017             (set-buffer gnus-article-buffer)
8018             ;;;!!! Fix this -- article should be rehighlighted.
8019             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8020             (set-buffer gnus-original-article-buffer)
8021             (gnus-request-article
8022              (cdr gnus-article-current)
8023              (car gnus-article-current) (current-buffer))))
8024         ;; Prettify the summary buffer line.
8025         (when (gnus-visual-p 'summary-highlight 'highlight)
8026           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8027
8028 (defun gnus-summary-edit-wash (key)
8029   "Perform editing command KEY in the article buffer."
8030   (interactive
8031    (list
8032     (progn
8033       (message "%s" (concat (this-command-keys) "- "))
8034       (read-char))))
8035   (message "")
8036   (gnus-summary-edit-article)
8037   (execute-kbd-macro (concat (this-command-keys) key))
8038   (gnus-article-edit-done))
8039
8040 ;;; Respooling
8041
8042 (defun gnus-summary-respool-query (&optional silent trace)
8043   "Query where the respool algorithm would put this article."
8044   (interactive)
8045   (let (gnus-mark-article-hook)
8046     (gnus-summary-select-article)
8047     (save-excursion
8048       (set-buffer gnus-original-article-buffer)
8049       (save-restriction
8050         (message-narrow-to-head)
8051         (let ((groups (nnmail-article-group 'identity trace)))
8052           (unless silent
8053             (if groups
8054                 (message "This message would go to %s"
8055                          (mapconcat 'car groups ", "))
8056               (message "This message would go to no groups"))
8057             groups))))))
8058
8059 (defun gnus-summary-respool-trace ()
8060   "Trace where the respool algorithm would put this article.
8061 Display a buffer showing all fancy splitting patterns which matched."
8062   (interactive)
8063   (gnus-summary-respool-query nil t))
8064
8065 ;; Summary marking commands.
8066
8067 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8068   "Mark articles which has the same subject as read, and then select the next.
8069 If UNMARK is positive, remove any kind of mark.
8070 If UNMARK is negative, tick articles."
8071   (interactive "P")
8072   (when unmark
8073     (setq unmark (prefix-numeric-value unmark)))
8074   (let ((count
8075          (gnus-summary-mark-same-subject
8076           (gnus-summary-article-subject) unmark)))
8077     ;; Select next unread article.  If auto-select-same mode, should
8078     ;; select the first unread article.
8079     (gnus-summary-next-article t (and gnus-auto-select-same
8080                                       (gnus-summary-article-subject)))
8081     (gnus-message 7 "%d article%s marked as %s"
8082                   count (if (= count 1) " is" "s are")
8083                   (if unmark "unread" "read"))))
8084
8085 (defun gnus-summary-kill-same-subject (&optional unmark)
8086   "Mark articles which has the same subject as read.
8087 If UNMARK is positive, remove any kind of mark.
8088 If UNMARK is negative, tick articles."
8089   (interactive "P")
8090   (when unmark
8091     (setq unmark (prefix-numeric-value unmark)))
8092   (let ((count
8093          (gnus-summary-mark-same-subject
8094           (gnus-summary-article-subject) unmark)))
8095     ;; If marked as read, go to next unread subject.
8096     (when (null unmark)
8097       ;; Go to next unread subject.
8098       (gnus-summary-next-subject 1 t))
8099     (gnus-message 7 "%d articles are marked as %s"
8100                   count (if unmark "unread" "read"))))
8101
8102 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8103   "Mark articles with same SUBJECT as read, and return marked number.
8104 If optional argument UNMARK is positive, remove any kinds of marks.
8105 If optional argument UNMARK is negative, mark articles as unread instead."
8106   (let ((count 1))
8107     (save-excursion
8108       (cond
8109        ((null unmark)                   ; Mark as read.
8110         (while (and
8111                 (progn
8112                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8113                   (gnus-summary-show-thread) t)
8114                 (gnus-summary-find-subject subject))
8115           (setq count (1+ count))))
8116        ((> unmark 0)                    ; Tick.
8117         (while (and
8118                 (progn
8119                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8120                   (gnus-summary-show-thread) t)
8121                 (gnus-summary-find-subject subject))
8122           (setq count (1+ count))))
8123        (t                               ; Mark as unread.
8124         (while (and
8125                 (progn
8126                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8127                   (gnus-summary-show-thread) t)
8128                 (gnus-summary-find-subject subject))
8129           (setq count (1+ count)))))
8130       (gnus-set-mode-line 'summary)
8131       ;; Return the number of marked articles.
8132       count)))
8133
8134 (defun gnus-summary-mark-as-processable (n &optional unmark)
8135   "Set the process mark on the next N articles.
8136 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8137 the process mark instead.  The difference between N and the actual
8138 number of articles marked is returned."
8139   (interactive "p")
8140   (let ((backward (< n 0))
8141         (n (abs n)))
8142     (while (and
8143             (> n 0)
8144             (if unmark
8145                 (gnus-summary-remove-process-mark
8146                  (gnus-summary-article-number))
8147               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8148             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8149       (setq n (1- n)))
8150     (when (/= 0 n)
8151       (gnus-message 7 "No more articles"))
8152     (gnus-summary-recenter)
8153     (gnus-summary-position-point)
8154     n))
8155
8156 (defun gnus-summary-unmark-as-processable (n)
8157   "Remove the process mark from the next N articles.
8158 If N is negative, unmark backward instead.  The difference between N and
8159 the actual number of articles unmarked is returned."
8160   (interactive "p")
8161   (gnus-summary-mark-as-processable n t))
8162
8163 (defun gnus-summary-unmark-all-processable ()
8164   "Remove the process mark from all articles."
8165   (interactive)
8166   (save-excursion
8167     (while gnus-newsgroup-processable
8168       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8169   (gnus-summary-position-point))
8170
8171 (defun gnus-summary-mark-as-expirable (n)
8172   "Mark N articles forward as expirable.
8173 If N is negative, mark backward instead.  The difference between N and
8174 the actual number of articles marked is returned."
8175   (interactive "p")
8176   (gnus-summary-mark-forward n gnus-expirable-mark))
8177
8178 (defun gnus-summary-mark-article-as-replied (article)
8179   "Mark ARTICLE replied and update the summary line."
8180   (push article gnus-newsgroup-replied)
8181   (let ((buffer-read-only nil))
8182     (when (gnus-summary-goto-subject article nil t)
8183       (gnus-summary-update-secondary-mark article))))
8184
8185 (defun gnus-summary-set-bookmark (article)
8186   "Set a bookmark in current article."
8187   (interactive (list (gnus-summary-article-number)))
8188   (when (or (not (get-buffer gnus-article-buffer))
8189             (not gnus-current-article)
8190             (not gnus-article-current)
8191             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8192     (error "No current article selected"))
8193   ;; Remove old bookmark, if one exists.
8194   (let ((old (assq article gnus-newsgroup-bookmarks)))
8195     (when old
8196       (setq gnus-newsgroup-bookmarks
8197             (delq old gnus-newsgroup-bookmarks))))
8198   ;; Set the new bookmark, which is on the form
8199   ;; (article-number . line-number-in-body).
8200   (push
8201    (cons article
8202          (save-excursion
8203            (set-buffer gnus-article-buffer)
8204            (count-lines
8205             (min (point)
8206                  (save-excursion
8207                    (goto-char (point-min))
8208                    (search-forward "\n\n" nil t)
8209                    (point)))
8210             (point))))
8211    gnus-newsgroup-bookmarks)
8212   (gnus-message 6 "A bookmark has been added to the current article."))
8213
8214 (defun gnus-summary-remove-bookmark (article)
8215   "Remove the bookmark from the current article."
8216   (interactive (list (gnus-summary-article-number)))
8217   ;; Remove old bookmark, if one exists.
8218   (let ((old (assq article gnus-newsgroup-bookmarks)))
8219     (if old
8220         (progn
8221           (setq gnus-newsgroup-bookmarks
8222                 (delq old gnus-newsgroup-bookmarks))
8223           (gnus-message 6 "Removed bookmark."))
8224       (gnus-message 6 "No bookmark in current article."))))
8225
8226 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8227 (defun gnus-summary-mark-as-dormant (n)
8228   "Mark N articles forward as dormant.
8229 If N is negative, mark backward instead.  The difference between N and
8230 the actual number of articles marked is returned."
8231   (interactive "p")
8232   (gnus-summary-mark-forward n gnus-dormant-mark))
8233
8234 (defun gnus-summary-set-process-mark (article)
8235   "Set the process mark on ARTICLE and update the summary line."
8236   (setq gnus-newsgroup-processable
8237         (cons article
8238               (delq article gnus-newsgroup-processable)))
8239   (when (gnus-summary-goto-subject article)
8240     (gnus-summary-show-thread)
8241     (gnus-summary-goto-subject article)
8242     (gnus-summary-update-secondary-mark article)))
8243
8244 (defun gnus-summary-remove-process-mark (article)
8245   "Remove the process mark from ARTICLE and update the summary line."
8246   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8247   (when (gnus-summary-goto-subject article)
8248     (gnus-summary-show-thread)
8249     (gnus-summary-goto-subject article)
8250     (gnus-summary-update-secondary-mark article)))
8251
8252 (defun gnus-summary-set-saved-mark (article)
8253   "Set the process mark on ARTICLE and update the summary line."
8254   (push article gnus-newsgroup-saved)
8255   (when (gnus-summary-goto-subject article)
8256     (gnus-summary-update-secondary-mark article)))
8257
8258 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8259   "Mark N articles as read forwards.
8260 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8261 The difference between N and the actual number of articles marked is
8262 returned.
8263 Iff NO-EXPIRE, auto-expiry will be inhibited."
8264   (interactive "p")
8265   (gnus-summary-show-thread)
8266   (let ((backward (< n 0))
8267         (gnus-summary-goto-unread
8268          (and gnus-summary-goto-unread
8269               (not (eq gnus-summary-goto-unread 'never))
8270               (not (memq mark (list gnus-unread-mark
8271                                     gnus-ticked-mark gnus-dormant-mark)))))
8272         (n (abs n))
8273         (mark (or mark gnus-del-mark)))
8274     (while (and (> n 0)
8275                 (gnus-summary-mark-article nil mark no-expire)
8276                 (zerop (gnus-summary-next-subject
8277                         (if backward -1 1)
8278                         (and gnus-summary-goto-unread
8279                              (not (eq gnus-summary-goto-unread 'never)))
8280                         t)))
8281       (setq n (1- n)))
8282     (when (/= 0 n)
8283       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8284     (gnus-summary-recenter)
8285     (gnus-summary-position-point)
8286     (gnus-set-mode-line 'summary)
8287     n))
8288
8289 (defun gnus-summary-mark-article-as-read (mark)
8290   "Mark the current article quickly as read with MARK."
8291   (let ((article (gnus-summary-article-number)))
8292     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8293     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8294     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8295     (push (cons article mark) gnus-newsgroup-reads)
8296     ;; Possibly remove from cache, if that is used.
8297     (when gnus-use-cache
8298       (gnus-cache-enter-remove-article article))
8299     ;; Allow the backend to change the mark.
8300     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8301     ;; Check for auto-expiry.
8302     (when (and gnus-newsgroup-auto-expire
8303                (memq mark gnus-auto-expirable-marks))
8304       (setq mark gnus-expirable-mark)
8305       ;; Let the backend know about the mark change.
8306       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8307       (push article gnus-newsgroup-expirable))
8308     ;; Set the mark in the buffer.
8309     (gnus-summary-update-mark mark 'unread)
8310     t))
8311
8312 (defun gnus-summary-mark-article-as-unread (mark)
8313   "Mark the current article quickly as unread with MARK."
8314   (let* ((article (gnus-summary-article-number))
8315          (old-mark (gnus-summary-article-mark article)))
8316     ;; Allow the backend to change the mark.
8317     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8318     (if (eq mark old-mark)
8319         t
8320       (if (<= article 0)
8321           (progn
8322             (gnus-error 1 "Can't mark negative article numbers")
8323             nil)
8324         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8325         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8326         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8327         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8328         (cond ((= mark gnus-ticked-mark)
8329                (push article gnus-newsgroup-marked))
8330               ((= mark gnus-dormant-mark)
8331                (push article gnus-newsgroup-dormant))
8332               (t
8333                (push article gnus-newsgroup-unreads)))
8334         (gnus-pull article gnus-newsgroup-reads)
8335
8336         ;; See whether the article is to be put in the cache.
8337         (and gnus-use-cache
8338              (vectorp (gnus-summary-article-header article))
8339              (save-excursion
8340                (gnus-cache-possibly-enter-article
8341                 gnus-newsgroup-name article
8342                 (= mark gnus-ticked-mark)
8343                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8344
8345         ;; Fix the mark.
8346         (gnus-summary-update-mark mark 'unread)
8347         t))))
8348
8349 (defun gnus-summary-mark-article (&optional article mark no-expire)
8350   "Mark ARTICLE with MARK.  MARK can be any character.
8351 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8352 `??' (dormant) and `?E' (expirable).
8353 If MARK is nil, then the default character `?r' is used.
8354 If ARTICLE is nil, then the article on the current line will be
8355 marked.
8356 Iff NO-EXPIRE, auto-expiry will be inhibited."
8357   ;; The mark might be a string.
8358   (when (stringp mark)
8359     (setq mark (aref mark 0)))
8360   ;; If no mark is given, then we check auto-expiring.
8361   (when (null mark)
8362     (setq mark gnus-del-mark))
8363   (when (and (not no-expire)
8364              gnus-newsgroup-auto-expire
8365              (memq mark gnus-auto-expirable-marks))
8366     (setq mark gnus-expirable-mark))
8367   (let ((article (or article (gnus-summary-article-number)))
8368         (old-mark (gnus-summary-article-mark article)))
8369     ;; Allow the backend to change the mark.
8370     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8371     (if (eq mark old-mark)
8372         t
8373       (unless article
8374         (error "No article on current line"))
8375       (if (not (if (or (= mark gnus-unread-mark)
8376                        (= mark gnus-ticked-mark)
8377                        (= mark gnus-dormant-mark))
8378                    (gnus-mark-article-as-unread article mark)
8379                  (gnus-mark-article-as-read article mark)))
8380           t
8381         ;; See whether the article is to be put in the cache.
8382         (and gnus-use-cache
8383              (not (= mark gnus-canceled-mark))
8384              (vectorp (gnus-summary-article-header article))
8385              (save-excursion
8386                (gnus-cache-possibly-enter-article
8387                 gnus-newsgroup-name article
8388                 (= mark gnus-ticked-mark)
8389                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8390
8391         (when (gnus-summary-goto-subject article nil t)
8392           (let ((buffer-read-only nil))
8393             (gnus-summary-show-thread)
8394             ;; Fix the mark.
8395             (gnus-summary-update-mark mark 'unread)
8396             t))))))
8397
8398 (defun gnus-summary-update-secondary-mark (article)
8399   "Update the secondary (read, process, cache) mark."
8400   (gnus-summary-update-mark
8401    (cond ((memq article gnus-newsgroup-processable)
8402           gnus-process-mark)
8403          ((memq article gnus-newsgroup-cached)
8404           gnus-cached-mark)
8405          ((memq article gnus-newsgroup-replied)
8406           gnus-replied-mark)
8407          ((memq article gnus-newsgroup-saved)
8408           gnus-saved-mark)
8409          (t gnus-unread-mark))
8410    'replied)
8411   (when (gnus-visual-p 'summary-highlight 'highlight)
8412     (gnus-run-hooks 'gnus-summary-update-hook))
8413   t)
8414
8415 (defun gnus-summary-update-mark (mark type)
8416   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8417         (buffer-read-only nil))
8418     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8419     (when forward
8420       (when (looking-at "\r")
8421         (incf forward))
8422       (when (<= (+ forward (point)) (point-max))
8423         ;; Go to the right position on the line.
8424         (goto-char (+ forward (point)))
8425         ;; Replace the old mark with the new mark.
8426         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8427         ;; Optionally update the marks by some user rule.
8428         (when (eq type 'unread)
8429           (gnus-data-set-mark
8430            (gnus-data-find (gnus-summary-article-number)) mark)
8431           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8432
8433 (defun gnus-mark-article-as-read (article &optional mark)
8434   "Enter ARTICLE in the pertinent lists and remove it from others."
8435   ;; Make the article expirable.
8436   (let ((mark (or mark gnus-del-mark)))
8437     (if (= mark gnus-expirable-mark)
8438         (push article gnus-newsgroup-expirable)
8439       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8440     ;; Remove from unread and marked lists.
8441     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8442     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8443     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8444     (push (cons article mark) gnus-newsgroup-reads)
8445     ;; Possibly remove from cache, if that is used.
8446     (when gnus-use-cache
8447       (gnus-cache-enter-remove-article article))
8448     t))
8449
8450 (defun gnus-mark-article-as-unread (article &optional mark)
8451   "Enter ARTICLE in the pertinent lists and remove it from others."
8452   (let ((mark (or mark gnus-ticked-mark)))
8453     (if (<= article 0)
8454         (progn
8455           (gnus-error 1 "Can't mark negative article numbers")
8456           nil)
8457       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8458             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8459             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8460             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8461
8462       ;; Unsuppress duplicates?
8463       (when gnus-suppress-duplicates
8464         (gnus-dup-unsuppress-article article))
8465
8466       (cond ((= mark gnus-ticked-mark)
8467              (push article gnus-newsgroup-marked))
8468             ((= mark gnus-dormant-mark)
8469              (push article gnus-newsgroup-dormant))
8470             (t
8471              (push article gnus-newsgroup-unreads)))
8472       (gnus-pull article gnus-newsgroup-reads)
8473       t)))
8474
8475 (defalias 'gnus-summary-mark-as-unread-forward
8476   'gnus-summary-tick-article-forward)
8477 (make-obsolete 'gnus-summary-mark-as-unread-forward
8478                'gnus-summary-tick-article-forward)
8479 (defun gnus-summary-tick-article-forward (n)
8480   "Tick N articles forwards.
8481 If N is negative, tick backwards instead.
8482 The difference between N and the number of articles ticked is returned."
8483   (interactive "p")
8484   (gnus-summary-mark-forward n gnus-ticked-mark))
8485
8486 (defalias 'gnus-summary-mark-as-unread-backward
8487   'gnus-summary-tick-article-backward)
8488 (make-obsolete 'gnus-summary-mark-as-unread-backward
8489                'gnus-summary-tick-article-backward)
8490 (defun gnus-summary-tick-article-backward (n)
8491   "Tick N articles backwards.
8492 The difference between N and the number of articles ticked is returned."
8493   (interactive "p")
8494   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8495
8496 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8497 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8498 (defun gnus-summary-tick-article (&optional article clear-mark)
8499   "Mark current article as unread.
8500 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8501 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8502   (interactive)
8503   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8504                                        gnus-ticked-mark)))
8505
8506 (defun gnus-summary-mark-as-read-forward (n)
8507   "Mark N articles as read forwards.
8508 If N is negative, mark backwards instead.
8509 The difference between N and the actual number of articles marked is
8510 returned."
8511   (interactive "p")
8512   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8513
8514 (defun gnus-summary-mark-as-read-backward (n)
8515   "Mark the N articles as read backwards.
8516 The difference between N and the actual number of articles marked is
8517 returned."
8518   (interactive "p")
8519   (gnus-summary-mark-forward
8520    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8521
8522 (defun gnus-summary-mark-as-read (&optional article mark)
8523   "Mark current article as read.
8524 ARTICLE specifies the article to be marked as read.
8525 MARK specifies a string to be inserted at the beginning of the line."
8526   (gnus-summary-mark-article article mark))
8527
8528 (defun gnus-summary-clear-mark-forward (n)
8529   "Clear marks from N articles forward.
8530 If N is negative, clear backward instead.
8531 The difference between N and the number of marks cleared is returned."
8532   (interactive "p")
8533   (gnus-summary-mark-forward n gnus-unread-mark))
8534
8535 (defun gnus-summary-clear-mark-backward (n)
8536   "Clear marks from N articles backward.
8537 The difference between N and the number of marks cleared is returned."
8538   (interactive "p")
8539   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8540
8541 (defun gnus-summary-mark-unread-as-read ()
8542   "Intended to be used by `gnus-summary-mark-article-hook'."
8543   (when (memq gnus-current-article gnus-newsgroup-unreads)
8544     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8545
8546 (defun gnus-summary-mark-read-and-unread-as-read ()
8547   "Intended to be used by `gnus-summary-mark-article-hook'."
8548   (let ((mark (gnus-summary-article-mark)))
8549     (when (or (gnus-unread-mark-p mark)
8550               (gnus-read-mark-p mark))
8551       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8552
8553 (defun gnus-summary-mark-region-as-read (point mark all)
8554   "Mark all unread articles between point and mark as read.
8555 If given a prefix, mark all articles between point and mark as read,
8556 even ticked and dormant ones."
8557   (interactive "r\nP")
8558   (save-excursion
8559     (let (article)
8560       (goto-char point)
8561       (beginning-of-line)
8562       (while (and
8563               (< (point) mark)
8564               (progn
8565                 (when (or all
8566                           (memq (setq article (gnus-summary-article-number))
8567                                 gnus-newsgroup-unreads))
8568                   (gnus-summary-mark-article article gnus-del-mark))
8569                 t)
8570               (gnus-summary-find-next))))))
8571
8572 (defun gnus-summary-mark-below (score mark)
8573   "Mark articles with score less than SCORE with MARK."
8574   (interactive "P\ncMark: ")
8575   (setq score (if score
8576                   (prefix-numeric-value score)
8577                 (or gnus-summary-default-score 0)))
8578   (save-excursion
8579     (set-buffer gnus-summary-buffer)
8580     (goto-char (point-min))
8581     (while
8582         (progn
8583           (and (< (gnus-summary-article-score) score)
8584                (gnus-summary-mark-article nil mark))
8585           (gnus-summary-find-next)))))
8586
8587 (defun gnus-summary-kill-below (&optional score)
8588   "Mark articles with score below SCORE as read."
8589   (interactive "P")
8590   (gnus-summary-mark-below score gnus-killed-mark))
8591
8592 (defun gnus-summary-clear-above (&optional score)
8593   "Clear all marks from articles with score above SCORE."
8594   (interactive "P")
8595   (gnus-summary-mark-above score gnus-unread-mark))
8596
8597 (defun gnus-summary-tick-above (&optional score)
8598   "Tick all articles with score above SCORE."
8599   (interactive "P")
8600   (gnus-summary-mark-above score gnus-ticked-mark))
8601
8602 (defun gnus-summary-mark-above (score mark)
8603   "Mark articles with score over SCORE with MARK."
8604   (interactive "P\ncMark: ")
8605   (setq score (if score
8606                   (prefix-numeric-value score)
8607                 (or gnus-summary-default-score 0)))
8608   (save-excursion
8609     (set-buffer gnus-summary-buffer)
8610     (goto-char (point-min))
8611     (while (and (progn
8612                   (when (> (gnus-summary-article-score) score)
8613                     (gnus-summary-mark-article nil mark))
8614                   t)
8615                 (gnus-summary-find-next)))))
8616
8617 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8618 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8619 (defun gnus-summary-limit-include-expunged (&optional no-error)
8620   "Display all the hidden articles that were expunged for low scores."
8621   (interactive)
8622   (let ((buffer-read-only nil))
8623     (let ((scored gnus-newsgroup-scored)
8624           headers h)
8625       (while scored
8626         (unless (gnus-summary-goto-subject (caar scored))
8627           (and (setq h (gnus-summary-article-header (caar scored)))
8628                (< (cdar scored) gnus-summary-expunge-below)
8629                (push h headers)))
8630         (setq scored (cdr scored)))
8631       (if (not headers)
8632           (when (not no-error)
8633             (error "No expunged articles hidden"))
8634         (goto-char (point-min))
8635         (gnus-summary-prepare-unthreaded (nreverse headers))
8636         (goto-char (point-min))
8637         (gnus-summary-position-point)
8638         t))))
8639
8640 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8641   "Mark all unread articles in this newsgroup as read.
8642 If prefix argument ALL is non-nil, ticked and dormant articles will
8643 also be marked as read.
8644 If QUIETLY is non-nil, no questions will be asked.
8645 If TO-HERE is non-nil, it should be a point in the buffer.  All
8646 articles before this point will be marked as read.
8647 Note that this function will only catch up the unread article
8648 in the current summary buffer limitation.
8649 The number of articles marked as read is returned."
8650   (interactive "P")
8651   (prog1
8652       (save-excursion
8653         (when (or quietly
8654                   (not gnus-interactive-catchup) ;Without confirmation?
8655                   gnus-expert-user
8656                   (gnus-y-or-n-p
8657                    (if all
8658                        "Mark absolutely all articles as read? "
8659                      "Mark all unread articles as read? ")))
8660           (if (and not-mark
8661                    (not gnus-newsgroup-adaptive)
8662                    (not gnus-newsgroup-auto-expire)
8663                    (not gnus-suppress-duplicates)
8664                    (or (not gnus-use-cache)
8665                        (eq gnus-use-cache 'passive)))
8666               (progn
8667                 (when all
8668                   (setq gnus-newsgroup-marked nil
8669                         gnus-newsgroup-dormant nil))
8670                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8671             ;; We actually mark all articles as canceled, which we
8672             ;; have to do when using auto-expiry or adaptive scoring.
8673             (gnus-summary-show-all-threads)
8674             (when (gnus-summary-first-subject (not all) t)
8675               (while (and
8676                       (if to-here (< (point) to-here) t)
8677                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8678                       (gnus-summary-find-next (not all) nil nil t))))
8679             (gnus-set-mode-line 'summary))
8680           t))
8681     (gnus-summary-position-point)))
8682
8683 (defun gnus-summary-catchup-to-here (&optional all)
8684   "Mark all unticked articles before the current one as read.
8685 If ALL is non-nil, also mark ticked and dormant articles as read."
8686   (interactive "P")
8687   (save-excursion
8688     (gnus-save-hidden-threads
8689       (let ((beg (point)))
8690         ;; We check that there are unread articles.
8691         (when (or all (gnus-summary-find-prev))
8692           (gnus-summary-catchup all t beg)))))
8693   (gnus-summary-position-point))
8694
8695 (defun gnus-summary-catchup-all (&optional quietly)
8696   "Mark all articles in this newsgroup as read."
8697   (interactive "P")
8698   (gnus-summary-catchup t quietly))
8699
8700 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8701   "Mark all unread articles in this group as read, then exit.
8702 If prefix argument ALL is non-nil, all articles are marked as read."
8703   (interactive "P")
8704   (when (gnus-summary-catchup all quietly nil 'fast)
8705     ;; Select next newsgroup or exit.
8706     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8707              (eq gnus-auto-select-next 'quietly))
8708         (gnus-summary-next-group nil)
8709       (gnus-summary-exit))))
8710
8711 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8712   "Mark all articles in this newsgroup as read, and then exit."
8713   (interactive "P")
8714   (gnus-summary-catchup-and-exit t quietly))
8715
8716 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8717   "Mark all articles in this group as read and select the next group.
8718 If given a prefix, mark all articles, unread as well as ticked, as
8719 read."
8720   (interactive "P")
8721   (save-excursion
8722     (gnus-summary-catchup all))
8723   (gnus-summary-next-group))
8724
8725 ;;;
8726 ;;; with article
8727 ;;;
8728
8729 (defmacro gnus-with-article (article &rest forms)
8730   "Select ARTICLE and perform FORMS in the original article buffer.
8731 Then replace the article with the result."
8732   `(progn
8733      ;; We don't want the article to be marked as read.
8734      (let (gnus-mark-article-hook)
8735        (gnus-summary-select-article t t nil ,article))
8736      (set-buffer gnus-original-article-buffer)
8737      ,@forms
8738      (if (not (gnus-check-backend-function
8739                'request-replace-article (car gnus-article-current)))
8740          (gnus-message 5 "Read-only group; not replacing")
8741        (unless (gnus-request-replace-article
8742                 ,article (car gnus-article-current)
8743                 (current-buffer) t)
8744          (error "Couldn't replace article")))
8745      ;; The cache and backlog have to be flushed somewhat.
8746      (when gnus-keep-backlog
8747        (gnus-backlog-remove-article
8748         (car gnus-article-current) (cdr gnus-article-current)))
8749      (when gnus-use-cache
8750        (gnus-cache-update-article
8751         (car gnus-article-current) (cdr gnus-article-current)))))
8752
8753 (put 'gnus-with-article 'lisp-indent-function 1)
8754 (put 'gnus-with-article 'edebug-form-spec '(form body))
8755
8756 ;; Thread-based commands.
8757
8758 (defun gnus-summary-articles-in-thread (&optional article)
8759   "Return a list of all articles in the current thread.
8760 If ARTICLE is non-nil, return all articles in the thread that starts
8761 with that article."
8762   (let* ((article (or article (gnus-summary-article-number)))
8763          (data (gnus-data-find-list article))
8764          (top-level (gnus-data-level (car data)))
8765          (top-subject
8766           (cond ((null gnus-thread-operation-ignore-subject)
8767                  (gnus-simplify-subject-re
8768                   (mail-header-subject (gnus-data-header (car data)))))
8769                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8770                  (gnus-simplify-subject-fuzzy
8771                   (mail-header-subject (gnus-data-header (car data)))))
8772                 (t nil)))
8773          (end-point (save-excursion
8774                       (if (gnus-summary-go-to-next-thread)
8775                           (point) (point-max))))
8776          articles)
8777     (while (and data
8778                 (< (gnus-data-pos (car data)) end-point))
8779       (when (or (not top-subject)
8780                 (string= top-subject
8781                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8782                              (gnus-simplify-subject-fuzzy
8783                               (mail-header-subject
8784                                (gnus-data-header (car data))))
8785                            (gnus-simplify-subject-re
8786                             (mail-header-subject
8787                              (gnus-data-header (car data)))))))
8788         (push (gnus-data-number (car data)) articles))
8789       (unless (and (setq data (cdr data))
8790                    (> (gnus-data-level (car data)) top-level))
8791         (setq data nil)))
8792     ;; Return the list of articles.
8793     (nreverse articles)))
8794
8795 (defun gnus-summary-rethread-current ()
8796   "Rethread the thread the current article is part of."
8797   (interactive)
8798   (let* ((gnus-show-threads t)
8799          (article (gnus-summary-article-number))
8800          (id (mail-header-id (gnus-summary-article-header)))
8801          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8802     (unless id
8803       (error "No article on the current line"))
8804     (gnus-rebuild-thread id)
8805     (gnus-summary-goto-subject article)))
8806
8807 (defun gnus-summary-reparent-thread ()
8808   "Make the current article child of the marked (or previous) article.
8809
8810 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8811 is non-nil or the Subject: of both articles are the same."
8812   (interactive)
8813   (unless (not (gnus-group-read-only-p))
8814     (error "The current newsgroup does not support article editing"))
8815   (unless (<= (length gnus-newsgroup-processable) 1)
8816     (error "No more than one article may be marked"))
8817   (save-window-excursion
8818     (let ((gnus-article-buffer " *reparent*")
8819           (current-article (gnus-summary-article-number))
8820           ;; First grab the marked article, otherwise one line up.
8821           (parent-article (if (not (null gnus-newsgroup-processable))
8822                               (car gnus-newsgroup-processable)
8823                             (save-excursion
8824                               (if (eq (forward-line -1) 0)
8825                                   (gnus-summary-article-number)
8826                                 (error "Beginning of summary buffer"))))))
8827       (unless (not (eq current-article parent-article))
8828         (error "An article may not be self-referential"))
8829       (let ((message-id (mail-header-id
8830                          (gnus-summary-article-header parent-article))))
8831         (unless (and message-id (not (equal message-id "")))
8832           (error "No message-id in desired parent"))
8833         (gnus-with-article current-article
8834           (save-restriction
8835             (goto-char (point-min))
8836             (message-narrow-to-head)
8837             (if (re-search-forward "^References: " nil t)
8838                 (progn
8839                   (re-search-forward "^[^ \t]" nil t)
8840                   (forward-line -1)
8841                   (end-of-line)
8842                   (insert " " message-id))
8843               (insert "References: " message-id "\n"))))
8844         (set-buffer gnus-summary-buffer)
8845         (gnus-summary-unmark-all-processable)
8846         (gnus-summary-update-article current-article)
8847         (gnus-summary-rethread-current)
8848         (gnus-message 3 "Article %d is now the child of article %d"
8849                       current-article parent-article)))))
8850
8851 (defun gnus-summary-toggle-threads (&optional arg)
8852   "Toggle showing conversation threads.
8853 If ARG is positive number, turn showing conversation threads on."
8854   (interactive "P")
8855   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8856     (setq gnus-show-threads
8857           (if (null arg) (not gnus-show-threads)
8858             (> (prefix-numeric-value arg) 0)))
8859     (gnus-summary-prepare)
8860     (gnus-summary-goto-subject current)
8861     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8862     (gnus-summary-position-point)))
8863
8864 (defun gnus-summary-show-all-threads ()
8865   "Show all threads."
8866   (interactive)
8867   (save-excursion
8868     (let ((buffer-read-only nil))
8869       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8870   (gnus-summary-position-point))
8871
8872 (defun gnus-summary-show-thread ()
8873   "Show thread subtrees.
8874 Returns nil if no thread was there to be shown."
8875   (interactive)
8876   (let ((buffer-read-only nil)
8877         (orig (point))
8878         ;; first goto end then to beg, to have point at beg after let
8879         (end (progn (end-of-line) (point)))
8880         (beg (progn (beginning-of-line) (point))))
8881     (prog1
8882         ;; Any hidden lines here?
8883         (search-forward "\r" end t)
8884       (subst-char-in-region beg end ?\^M ?\n t)
8885       (goto-char orig)
8886       (gnus-summary-position-point))))
8887
8888 (defun gnus-summary-hide-all-threads ()
8889   "Hide all thread subtrees."
8890   (interactive)
8891   (save-excursion
8892     (goto-char (point-min))
8893     (gnus-summary-hide-thread)
8894     (while (zerop (gnus-summary-next-thread 1 t))
8895       (gnus-summary-hide-thread)))
8896   (gnus-summary-position-point))
8897
8898 (defun gnus-summary-hide-thread ()
8899   "Hide thread subtrees.
8900 Returns nil if no threads were there to be hidden."
8901   (interactive)
8902   (let ((buffer-read-only nil)
8903         (start (point))
8904         (article (gnus-summary-article-number)))
8905     (goto-char start)
8906     ;; Go forward until either the buffer ends or the subthread
8907     ;; ends.
8908     (when (and (not (eobp))
8909                (or (zerop (gnus-summary-next-thread 1 t))
8910                    (goto-char (point-max))))
8911       (prog1
8912           (if (and (> (point) start)
8913                    (search-backward "\n" start t))
8914               (progn
8915                 (subst-char-in-region start (point) ?\n ?\^M)
8916                 (gnus-summary-goto-subject article))
8917             (goto-char start)
8918             nil)))))
8919
8920 (defun gnus-summary-go-to-next-thread (&optional previous)
8921   "Go to the same level (or less) next thread.
8922 If PREVIOUS is non-nil, go to previous thread instead.
8923 Return the article number moved to, or nil if moving was impossible."
8924   (let ((level (gnus-summary-thread-level))
8925         (way (if previous -1 1))
8926         (beg (point)))
8927     (forward-line way)
8928     (while (and (not (eobp))
8929                 (< level (gnus-summary-thread-level)))
8930       (forward-line way))
8931     (if (eobp)
8932         (progn
8933           (goto-char beg)
8934           nil)
8935       (setq beg (point))
8936       (prog1
8937           (gnus-summary-article-number)
8938         (goto-char beg)))))
8939
8940 (defun gnus-summary-next-thread (n &optional silent)
8941   "Go to the same level next N'th thread.
8942 If N is negative, search backward instead.
8943 Returns the difference between N and the number of skips actually
8944 done.
8945
8946 If SILENT, don't output messages."
8947   (interactive "p")
8948   (let ((backward (< n 0))
8949         (n (abs n)))
8950     (while (and (> n 0)
8951                 (gnus-summary-go-to-next-thread backward))
8952       (decf n))
8953     (unless silent
8954       (gnus-summary-position-point))
8955     (when (and (not silent) (/= 0 n))
8956       (gnus-message 7 "No more threads"))
8957     n))
8958
8959 (defun gnus-summary-prev-thread (n)
8960   "Go to the same level previous N'th thread.
8961 Returns the difference between N and the number of skips actually
8962 done."
8963   (interactive "p")
8964   (gnus-summary-next-thread (- n)))
8965
8966 (defun gnus-summary-go-down-thread ()
8967   "Go down one level in the current thread."
8968   (let ((children (gnus-summary-article-children)))
8969     (when children
8970       (gnus-summary-goto-subject (car children)))))
8971
8972 (defun gnus-summary-go-up-thread ()
8973   "Go up one level in the current thread."
8974   (let ((parent (gnus-summary-article-parent)))
8975     (when parent
8976       (gnus-summary-goto-subject parent))))
8977
8978 (defun gnus-summary-down-thread (n)
8979   "Go down thread N steps.
8980 If N is negative, go up instead.
8981 Returns the difference between N and how many steps down that were
8982 taken."
8983   (interactive "p")
8984   (let ((up (< n 0))
8985         (n (abs n)))
8986     (while (and (> n 0)
8987                 (if up (gnus-summary-go-up-thread)
8988                   (gnus-summary-go-down-thread)))
8989       (setq n (1- n)))
8990     (gnus-summary-position-point)
8991     (when (/= 0 n)
8992       (gnus-message 7 "Can't go further"))
8993     n))
8994
8995 (defun gnus-summary-up-thread (n)
8996   "Go up thread N steps.
8997 If N is negative, go up instead.
8998 Returns the difference between N and how many steps down that were
8999 taken."
9000   (interactive "p")
9001   (gnus-summary-down-thread (- n)))
9002
9003 (defun gnus-summary-top-thread ()
9004   "Go to the top of the thread."
9005   (interactive)
9006   (while (gnus-summary-go-up-thread))
9007   (gnus-summary-article-number))
9008
9009 (defun gnus-summary-kill-thread (&optional unmark)
9010   "Mark articles under current thread as read.
9011 If the prefix argument is positive, remove any kinds of marks.
9012 If the prefix argument is negative, tick articles instead."
9013   (interactive "P")
9014   (when unmark
9015     (setq unmark (prefix-numeric-value unmark)))
9016   (let ((articles (gnus-summary-articles-in-thread)))
9017     (save-excursion
9018       ;; Expand the thread.
9019       (gnus-summary-show-thread)
9020       ;; Mark all the articles.
9021       (while articles
9022         (gnus-summary-goto-subject (car articles))
9023         (cond ((null unmark)
9024                (gnus-summary-mark-article-as-read gnus-killed-mark))
9025               ((> unmark 0)
9026                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9027               (t
9028                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9029         (setq articles (cdr articles))))
9030     ;; Hide killed subtrees.
9031     (and (null unmark)
9032          gnus-thread-hide-killed
9033          (gnus-summary-hide-thread))
9034     ;; If marked as read, go to next unread subject.
9035     (when (null unmark)
9036       ;; Go to next unread subject.
9037       (gnus-summary-next-subject 1 t)))
9038   (gnus-set-mode-line 'summary))
9039
9040 ;; Summary sorting commands
9041
9042 (defun gnus-summary-sort-by-number (&optional reverse)
9043   "Sort the summary buffer by article number.
9044 Argument REVERSE means reverse order."
9045   (interactive "P")
9046   (gnus-summary-sort 'number reverse))
9047
9048 (defun gnus-summary-sort-by-author (&optional reverse)
9049   "Sort the summary buffer by author name alphabetically.
9050 If `case-fold-search' is non-nil, case of letters is ignored.
9051 Argument REVERSE means reverse order."
9052   (interactive "P")
9053   (gnus-summary-sort 'author reverse))
9054
9055 (defun gnus-summary-sort-by-subject (&optional reverse)
9056   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9057 If `case-fold-search' is non-nil, case of letters is ignored.
9058 Argument REVERSE means reverse order."
9059   (interactive "P")
9060   (gnus-summary-sort 'subject reverse))
9061
9062 (defun gnus-summary-sort-by-date (&optional reverse)
9063   "Sort the summary buffer by date.
9064 Argument REVERSE means reverse order."
9065   (interactive "P")
9066   (gnus-summary-sort 'date reverse))
9067
9068 (defun gnus-summary-sort-by-score (&optional reverse)
9069   "Sort the summary buffer by score.
9070 Argument REVERSE means reverse order."
9071   (interactive "P")
9072   (gnus-summary-sort 'score reverse))
9073
9074 (defun gnus-summary-sort-by-lines (&optional reverse)
9075   "Sort the summary buffer by the number of lines.
9076 Argument REVERSE means reverse order."
9077   (interactive "P")
9078   (gnus-summary-sort 'lines reverse))
9079
9080 (defun gnus-summary-sort-by-chars (&optional reverse)
9081   "Sort the summary buffer by article length.
9082 Argument REVERSE means reverse order."
9083   (interactive "P")
9084   (gnus-summary-sort 'chars reverse))
9085
9086 (defun gnus-summary-sort (predicate reverse)
9087   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9088   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9089          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9090          (gnus-thread-sort-functions
9091           (if (not reverse)
9092               thread
9093             `(lambda (t1 t2)
9094                (,thread t2 t1))))
9095          (gnus-sort-gathered-threads-function
9096           gnus-thread-sort-functions)
9097          (gnus-article-sort-functions
9098           (if (not reverse)
9099               article
9100             `(lambda (t1 t2)
9101                (,article t2 t1))))
9102          (buffer-read-only)
9103          (gnus-summary-prepare-hook nil))
9104     ;; We do the sorting by regenerating the threads.
9105     (gnus-summary-prepare)
9106     ;; Hide subthreads if needed.
9107     (when (and gnus-show-threads gnus-thread-hide-subtree)
9108       (gnus-summary-hide-all-threads))))
9109
9110 ;; Summary saving commands.
9111
9112 (defun gnus-summary-save-article (&optional n not-saved)
9113   "Save the current article using the default saver function.
9114 If N is a positive number, save the N next articles.
9115 If N is a negative number, save the N previous articles.
9116 If N is nil and any articles have been marked with the process mark,
9117 save those articles instead.
9118 The variable `gnus-default-article-saver' specifies the saver function."
9119   (interactive "P")
9120   (let* ((articles (gnus-summary-work-articles n))
9121          (save-buffer (save-excursion
9122                         (nnheader-set-temp-buffer " *Gnus Save*")))
9123          (num (length articles))
9124          header file)
9125     (dolist (article articles)
9126       (setq header (gnus-summary-article-header article))
9127       (if (not (vectorp header))
9128           ;; This is a pseudo-article.
9129           (if (assq 'name header)
9130               (gnus-copy-file (cdr (assq 'name header)))
9131             (gnus-message 1 "Article %d is unsaveable" article))
9132         ;; This is a real article.
9133         (save-window-excursion
9134           (gnus-summary-select-article t nil nil article))
9135         (save-excursion
9136           (set-buffer save-buffer)
9137           (erase-buffer)
9138           (insert-buffer-substring gnus-original-article-buffer))
9139         (setq file (gnus-article-save save-buffer file num))
9140         (gnus-summary-remove-process-mark article)
9141         (unless not-saved
9142           (gnus-summary-set-saved-mark article))))
9143     (gnus-kill-buffer save-buffer)
9144     (gnus-summary-position-point)
9145     (gnus-set-mode-line 'summary)
9146     n))
9147
9148 (defun gnus-summary-pipe-output (&optional arg)
9149   "Pipe the current article to a subprocess.
9150 If N is a positive number, pipe the N next articles.
9151 If N is a negative number, pipe the N previous articles.
9152 If N is nil and any articles have been marked with the process mark,
9153 pipe those articles instead."
9154   (interactive "P")
9155   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9156     (gnus-summary-save-article arg t))
9157   (gnus-configure-windows 'pipe))
9158
9159 (defun gnus-summary-save-article-mail (&optional arg)
9160   "Append the current article to an mail file.
9161 If N is a positive number, save the N next articles.
9162 If N is a negative number, save the N previous articles.
9163 If N is nil and any articles have been marked with the process mark,
9164 save those articles instead."
9165   (interactive "P")
9166   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9167     (gnus-summary-save-article arg)))
9168
9169 (defun gnus-summary-save-article-rmail (&optional arg)
9170   "Append the current article to an rmail file.
9171 If N is a positive number, save the N next articles.
9172 If N is a negative number, save the N previous articles.
9173 If N is nil and any articles have been marked with the process mark,
9174 save those articles instead."
9175   (interactive "P")
9176   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9177     (gnus-summary-save-article arg)))
9178
9179 (defun gnus-summary-save-article-file (&optional arg)
9180   "Append the current article to a file.
9181 If N is a positive number, save the N next articles.
9182 If N is a negative number, save the N previous articles.
9183 If N is nil and any articles have been marked with the process mark,
9184 save those articles instead."
9185   (interactive "P")
9186   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9187     (gnus-summary-save-article arg)))
9188
9189 (defun gnus-summary-write-article-file (&optional arg)
9190   "Write the current article to a file, deleting the previous file.
9191 If N is a positive number, save the N next articles.
9192 If N is a negative number, save the N previous articles.
9193 If N is nil and any articles have been marked with the process mark,
9194 save those articles instead."
9195   (interactive "P")
9196   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9197     (gnus-summary-save-article arg)))
9198
9199 (defun gnus-summary-save-article-body-file (&optional arg)
9200   "Append the current article body to a file.
9201 If N is a positive number, save the N next articles.
9202 If N is a negative number, save the N previous articles.
9203 If N is nil and any articles have been marked with the process mark,
9204 save those articles instead."
9205   (interactive "P")
9206   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9207     (gnus-summary-save-article arg)))
9208
9209 (defun gnus-summary-pipe-message (program)
9210   "Pipe the current article through PROGRAM."
9211   (interactive "sProgram: ")
9212   (gnus-summary-select-article)
9213   (let ((mail-header-separator ""))
9214     (gnus-eval-in-buffer-window gnus-article-buffer
9215       (save-restriction
9216         (widen)
9217         (let ((start (window-start))
9218               buffer-read-only)
9219           (message-pipe-buffer-body program)
9220           (set-window-start (get-buffer-window (current-buffer)) start))))))
9221
9222 (defun gnus-get-split-value (methods)
9223   "Return a value based on the split METHODS."
9224   (let (split-name method result match)
9225     (when methods
9226       (save-excursion
9227         (set-buffer gnus-original-article-buffer)
9228         (save-restriction
9229           (nnheader-narrow-to-headers)
9230           (while (and methods (not split-name))
9231             (goto-char (point-min))
9232             (setq method (pop methods))
9233             (setq match (car method))
9234             (when (cond
9235                    ((stringp match)
9236                     ;; Regular expression.
9237                     (ignore-errors
9238                       (re-search-forward match nil t)))
9239                    ((gnus-functionp match)
9240                     ;; Function.
9241                     (save-restriction
9242                       (widen)
9243                       (setq result (funcall match gnus-newsgroup-name))))
9244                    ((consp match)
9245                     ;; Form.
9246                     (save-restriction
9247                       (widen)
9248                       (setq result (eval match)))))
9249               (setq split-name (cdr method))
9250               (cond ((stringp result)
9251                      (push (expand-file-name
9252                             result gnus-article-save-directory)
9253                            split-name))
9254                     ((consp result)
9255                      (setq split-name (append result split-name)))))))))
9256     (nreverse split-name)))
9257
9258 (defun gnus-valid-move-group-p (group)
9259   (and (boundp group)
9260        (symbol-name group)
9261        (symbol-value group)
9262        (gnus-get-function (gnus-find-method-for-group
9263                            (symbol-name group)) 'request-accept-article t)))
9264
9265 (defun gnus-read-move-group-name (prompt default articles prefix)
9266   "Read a group name."
9267   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9268          (minibuffer-confirm-incomplete nil) ; XEmacs
9269          (prom
9270           (format "%s %s to:"
9271                   prompt
9272                   (if (> (length articles) 1)
9273                       (format "these %d articles" (length articles))
9274                     "this article")))
9275          (to-newsgroup
9276           (cond
9277            ((null split-name)
9278             (gnus-completing-read default prom
9279                                   gnus-active-hashtb
9280                                   'gnus-valid-move-group-p
9281                                   nil prefix
9282                                   'gnus-group-history))
9283            ((= 1 (length split-name))
9284             (gnus-completing-read (car split-name) prom
9285                                   gnus-active-hashtb
9286                                   'gnus-valid-move-group-p
9287                                   nil nil
9288                                   'gnus-group-history))
9289            (t
9290             (gnus-completing-read nil prom
9291                                   (mapcar (lambda (el) (list el))
9292                                           (nreverse split-name))
9293                                   nil nil nil
9294                                   'gnus-group-history))))
9295          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9296     (when to-newsgroup
9297       (if (or (string= to-newsgroup "")
9298               (string= to-newsgroup prefix))
9299           (setq to-newsgroup default))
9300       (unless to-newsgroup
9301         (error "No group name entered"))
9302       (or (gnus-active to-newsgroup)
9303           (gnus-activate-group to-newsgroup nil nil to-method)
9304           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9305                                      to-newsgroup))
9306               (or (and (gnus-request-create-group to-newsgroup to-method)
9307                        (gnus-activate-group
9308                         to-newsgroup nil nil to-method)
9309                        (gnus-subscribe-group to-newsgroup))
9310                   (error "Couldn't create group %s" to-newsgroup)))
9311           (error "No such group: %s" to-newsgroup)))
9312     to-newsgroup))
9313
9314 (defun gnus-summary-save-parts (type dir n &optional reverse)
9315   "Save parts matching TYPE to DIR.
9316 If REVERSE, save parts that do not match TYPE."
9317   (interactive
9318    (list (read-string "Save parts of type: " 
9319                       (or (car gnus-summary-save-parts-type-history)
9320                           gnus-summary-save-parts-default-mime)
9321                       'gnus-summary-save-parts-type-history)
9322          (setq gnus-summary-save-parts-last-directory
9323                (read-file-name "Save to directory: " 
9324                                gnus-summary-save-parts-last-directory
9325                                nil t))
9326          current-prefix-arg))
9327   (gnus-summary-iterate n
9328     (let ((gnus-display-mime-function nil)
9329           (gnus-inhibit-treatment t))
9330       (gnus-summary-select-article))
9331     (save-excursion
9332       (set-buffer gnus-article-buffer)
9333       (let ((handles (or gnus-article-mime-handles
9334                          (mm-dissect-buffer) (mm-uu-dissect))))
9335         (when handles
9336           (gnus-summary-save-parts-1 type dir handles reverse)
9337           (unless gnus-article-mime-handles ;; Don't destroy this case.
9338             (mm-destroy-parts handles)))))))
9339
9340 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9341   (if (stringp (car handle))
9342       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9343               (cdr handle))
9344     (when (if reverse
9345               (not (string-match type (mm-handle-media-type handle)))
9346             (string-match type (mm-handle-media-type handle)))
9347       (let ((file (expand-file-name
9348                    (file-name-nondirectory
9349                     (or
9350                      (mail-content-type-get
9351                       (mm-handle-disposition handle) 'filename)
9352                      (concat gnus-newsgroup-name
9353                              "." (number-to-string
9354                                   (cdr gnus-article-current)))))
9355                    dir)))
9356         (unless (file-exists-p file)
9357           (mm-save-part-to-file handle file))))))
9358
9359 ;; Summary extract commands
9360
9361 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9362   (let ((buffer-read-only nil)
9363         (article (gnus-summary-article-number))
9364         after-article b e)
9365     (unless (gnus-summary-goto-subject article)
9366       (error "No such article: %d" article))
9367     (gnus-summary-position-point)
9368     ;; If all commands are to be bunched up on one line, we collect
9369     ;; them here.
9370     (unless gnus-view-pseudos-separately
9371       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9372             files action)
9373         (while ps
9374           (setq action (cdr (assq 'action (car ps))))
9375           (setq files (list (cdr (assq 'name (car ps)))))
9376           (while (and ps (cdr ps)
9377                       (string= (or action "1")
9378                                (or (cdr (assq 'action (cadr ps))) "2")))
9379             (push (cdr (assq 'name (cadr ps))) files)
9380             (setcdr ps (cddr ps)))
9381           (when files
9382             (when (not (string-match "%s" action))
9383               (push " " files))
9384             (push " " files)
9385             (when (assq 'execute (car ps))
9386               (setcdr (assq 'execute (car ps))
9387                       (funcall (if (string-match "%s" action)
9388                                    'format 'concat)
9389                                action
9390                                (mapconcat
9391                                 (lambda (f)
9392                                   (if (equal f " ")
9393                                       f
9394                                     (mm-quote-arg f)))
9395                                 files " ")))))
9396           (setq ps (cdr ps)))))
9397     (if (and gnus-view-pseudos (not not-view))
9398         (while pslist
9399           (when (assq 'execute (car pslist))
9400             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9401                                   (eq gnus-view-pseudos 'not-confirm)))
9402           (setq pslist (cdr pslist)))
9403       (save-excursion
9404         (while pslist
9405           (setq after-article (or (cdr (assq 'article (car pslist)))
9406                                   (gnus-summary-article-number)))
9407           (gnus-summary-goto-subject after-article)
9408           (forward-line 1)
9409           (setq b (point))
9410           (insert "    " (file-name-nondirectory
9411                           (cdr (assq 'name (car pslist))))
9412                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9413           (setq e (point))
9414           (forward-line -1)             ; back to `b'
9415           (gnus-add-text-properties
9416            b (1- e) (list 'gnus-number gnus-reffed-article-number
9417                           gnus-mouse-face-prop gnus-mouse-face))
9418           (gnus-data-enter
9419            after-article gnus-reffed-article-number
9420            gnus-unread-mark b (car pslist) 0 (- e b))
9421           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9422           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9423           (setq pslist (cdr pslist)))))))
9424
9425 (defun gnus-pseudos< (p1 p2)
9426   (let ((c1 (cdr (assq 'action p1)))
9427         (c2 (cdr (assq 'action p2))))
9428     (and c1 c2 (string< c1 c2))))
9429
9430 (defun gnus-request-pseudo-article (props)
9431   (cond ((assq 'execute props)
9432          (gnus-execute-command (cdr (assq 'execute props)))))
9433   (let ((gnus-current-article (gnus-summary-article-number)))
9434     (gnus-run-hooks 'gnus-mark-article-hook)))
9435
9436 (defun gnus-execute-command (command &optional automatic)
9437   (save-excursion
9438     (gnus-article-setup-buffer)
9439     (set-buffer gnus-article-buffer)
9440     (setq buffer-read-only nil)
9441     (let ((command (if automatic command
9442                      (read-string "Command: " (cons command 0)))))
9443       (erase-buffer)
9444       (insert "$ " command "\n\n")
9445       (if gnus-view-pseudo-asynchronously
9446           (start-process "gnus-execute" (current-buffer) shell-file-name
9447                          shell-command-switch command)
9448         (call-process shell-file-name nil t nil
9449                       shell-command-switch command)))))
9450
9451 ;; Summary kill commands.
9452
9453 (defun gnus-summary-edit-global-kill (article)
9454   "Edit the \"global\" kill file."
9455   (interactive (list (gnus-summary-article-number)))
9456   (gnus-group-edit-global-kill article))
9457
9458 (defun gnus-summary-edit-local-kill ()
9459   "Edit a local kill file applied to the current newsgroup."
9460   (interactive)
9461   (setq gnus-current-headers (gnus-summary-article-header))
9462   (gnus-group-edit-local-kill
9463    (gnus-summary-article-number) gnus-newsgroup-name))
9464
9465 ;;; Header reading.
9466
9467 (defun gnus-read-header (id &optional header)
9468   "Read the headers of article ID and enter them into the Gnus system."
9469   (let ((group gnus-newsgroup-name)
9470         (gnus-override-method
9471          (or
9472           gnus-override-method
9473           (and (gnus-news-group-p gnus-newsgroup-name)
9474                (car (gnus-refer-article-methods)))))
9475         where)
9476     ;; First we check to see whether the header in question is already
9477     ;; fetched.
9478     (if (stringp id)
9479         ;; This is a Message-ID.
9480         (setq header (or header (gnus-id-to-header id)))
9481       ;; This is an article number.
9482       (setq header (or header (gnus-summary-article-header id))))
9483     (if (and header
9484              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9485         ;; We have found the header.
9486         header
9487       ;; If this is a sparse article, we have to nix out its
9488       ;; previous entry in the thread hashtb.
9489       (when (and header
9490                  (gnus-summary-article-sparse-p (mail-header-number header)))
9491         (let* ((parent (gnus-parent-id (mail-header-references header)))
9492                (thread (and parent (gnus-id-to-thread parent))))
9493           (when thread
9494             (delq (assq header thread) thread))))
9495       ;; We have to really fetch the header to this article.
9496       (save-excursion
9497         (set-buffer nntp-server-buffer)
9498         (when (setq where (gnus-request-head id group))
9499           (nnheader-fold-continuation-lines)
9500           (goto-char (point-max))
9501           (insert ".\n")
9502           (goto-char (point-min))
9503           (insert "211 ")
9504           (princ (cond
9505                   ((numberp id) id)
9506                   ((cdr where) (cdr where))
9507                   (header (mail-header-number header))
9508                   (t gnus-reffed-article-number))
9509                  (current-buffer))
9510           (insert " Article retrieved.\n"))
9511         (if (or (not where)
9512                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9513             ()                          ; Malformed head.
9514           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9515             (when (and (stringp id)
9516                        (not (string= (gnus-group-real-name group)
9517                                      (car where))))
9518               ;; If we fetched by Message-ID and the article came
9519               ;; from a different group, we fudge some bogus article
9520               ;; numbers for this article.
9521               (mail-header-set-number header gnus-reffed-article-number))
9522             (save-excursion
9523               (set-buffer gnus-summary-buffer)
9524               (decf gnus-reffed-article-number)
9525               (gnus-remove-header (mail-header-number header))
9526               (push header gnus-newsgroup-headers)
9527               (setq gnus-current-headers header)
9528               (push (mail-header-number header) gnus-newsgroup-limit)))
9529           header)))))
9530
9531 (defun gnus-remove-header (number)
9532   "Remove header NUMBER from `gnus-newsgroup-headers'."
9533   (if (and gnus-newsgroup-headers
9534            (= number (mail-header-number (car gnus-newsgroup-headers))))
9535       (pop gnus-newsgroup-headers)
9536     (let ((headers gnus-newsgroup-headers))
9537       (while (and (cdr headers)
9538                   (not (= number (mail-header-number (cadr headers)))))
9539         (pop headers))
9540       (when (cdr headers)
9541         (setcdr headers (cddr headers))))))
9542
9543 ;;;
9544 ;;; summary highlights
9545 ;;;
9546
9547 (defun gnus-highlight-selected-summary ()
9548   "Highlight selected article in summary buffer."
9549   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9550   (when gnus-summary-selected-face
9551     (save-excursion
9552       (let* ((beg (progn (beginning-of-line) (point)))
9553              (end (progn (end-of-line) (point)))
9554              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9555              (from (if (get-text-property beg gnus-mouse-face-prop)
9556                        beg
9557                      (or (next-single-property-change
9558                           beg gnus-mouse-face-prop nil end)
9559                          beg)))
9560              (to
9561               (if (= from end)
9562                   (- from 2)
9563                 (or (next-single-property-change
9564                      from gnus-mouse-face-prop nil end)
9565                     end))))
9566         ;; If no mouse-face prop on line we will have to = from = end,
9567         ;; so we highlight the entire line instead.
9568         (when (= (+ to 2) from)
9569           (setq from beg)
9570           (setq to end))
9571         (if gnus-newsgroup-selected-overlay
9572             ;; Move old overlay.
9573             (gnus-move-overlay
9574              gnus-newsgroup-selected-overlay from to (current-buffer))
9575           ;; Create new overlay.
9576           (gnus-overlay-put
9577            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9578            'face gnus-summary-selected-face))))))
9579
9580 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9581 (defun gnus-summary-highlight-line ()
9582   "Highlight current line according to `gnus-summary-highlight'."
9583   (let* ((list gnus-summary-highlight)
9584          (p (point))
9585          (end (progn (end-of-line) (point)))
9586          ;; now find out where the line starts and leave point there.
9587          (beg (progn (beginning-of-line) (point)))
9588          (article (gnus-summary-article-number))
9589          (score (or (cdr (assq (or article gnus-current-article)
9590                                gnus-newsgroup-scored))
9591                     gnus-summary-default-score 0))
9592          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9593          (inhibit-read-only t))
9594     ;; Eval the cars of the lists until we find a match.
9595     (let ((default gnus-summary-default-score))
9596       (while (and list
9597                   (not (eval (caar list))))
9598         (setq list (cdr list))))
9599     (let ((face (cdar list)))
9600       (unless (eq face (get-text-property beg 'face))
9601         (gnus-put-text-property-excluding-characters-with-faces
9602          beg end 'face
9603          (setq face (if (boundp face) (symbol-value face) face)))
9604         (when gnus-summary-highlight-line-function
9605           (funcall gnus-summary-highlight-line-function article face))))
9606     (goto-char p)))
9607
9608 (defun gnus-update-read-articles (group unread &optional compute)
9609   "Update the list of read articles in GROUP."
9610   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9611          (entry (gnus-gethash group gnus-newsrc-hashtb))
9612          (info (nth 2 entry))
9613          (prev 1)
9614          (unread (sort (copy-sequence unread) '<))
9615          read)
9616     (if (or (not info) (not active))
9617         ;; There is no info on this group if it was, in fact,
9618         ;; killed.  Gnus stores no information on killed groups, so
9619         ;; there's nothing to be done.
9620         ;; One could store the information somewhere temporarily,
9621         ;; perhaps...  Hmmm...
9622         ()
9623       ;; Remove any negative articles numbers.
9624       (while (and unread (< (car unread) 0))
9625         (setq unread (cdr unread)))
9626       ;; Remove any expired article numbers
9627       (while (and unread (< (car unread) (car active)))
9628         (setq unread (cdr unread)))
9629       ;; Compute the ranges of read articles by looking at the list of
9630       ;; unread articles.
9631       (while unread
9632         (when (/= (car unread) prev)
9633           (push (if (= prev (1- (car unread))) prev
9634                   (cons prev (1- (car unread))))
9635                 read))
9636         (setq prev (1+ (car unread)))
9637         (setq unread (cdr unread)))
9638       (when (<= prev (cdr active))
9639         (push (cons prev (cdr active)) read))
9640       (setq read (if (> (length read) 1) (nreverse read) read))
9641       (if compute
9642           read
9643         (save-excursion
9644           (let (setmarkundo)
9645             ;; Propagate the read marks to the backend.
9646             (when (gnus-check-backend-function 'request-set-mark group)
9647               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9648                     (add (gnus-remove-from-range read (gnus-info-read info))))
9649                 (when (or add del)
9650                   (unless (gnus-check-group group)
9651                     (error "Can't open server for %s" group))
9652                   (gnus-request-set-mark
9653                    group (delq nil (list (if add (list add 'add '(read)))
9654                                          (if del (list del 'del '(read))))))
9655                   (setq setmarkundo
9656                         `(gnus-request-set-mark
9657                           ,group
9658                           ',(delq nil (list
9659                                        (if del (list del 'add '(read)))
9660                                        (if add (list add 'del '(read))))))))))
9661             (set-buffer gnus-group-buffer)
9662             (gnus-undo-register
9663               `(progn
9664                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9665                  (gnus-info-set-read ',info ',(gnus-info-read info))
9666                  (gnus-get-unread-articles-in-group ',info 
9667                                                     (gnus-active ,group))
9668                  (gnus-group-update-group ,group t)
9669                  ,setmarkundo))))
9670         ;; Enter this list into the group info.
9671         (gnus-info-set-read info read)
9672         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9673         (gnus-get-unread-articles-in-group info (gnus-active group))
9674         t))))
9675
9676 (defun gnus-offer-save-summaries ()
9677   "Offer to save all active summary buffers."
9678   (save-excursion
9679     (let ((buflist (buffer-list))
9680           buffers bufname)
9681       ;; Go through all buffers and find all summaries.
9682       (while buflist
9683         (and (setq bufname (buffer-name (car buflist)))
9684              (string-match "Summary" bufname)
9685              (save-excursion
9686                (set-buffer bufname)
9687                ;; We check that this is, indeed, a summary buffer.
9688                (and (eq major-mode 'gnus-summary-mode)
9689                     ;; Also make sure this isn't bogus.
9690                     gnus-newsgroup-prepared
9691                     ;; Also make sure that this isn't a dead summary buffer.
9692                     (not gnus-dead-summary-mode)))
9693              (push bufname buffers))
9694         (setq buflist (cdr buflist)))
9695       ;; Go through all these summary buffers and offer to save them.
9696       (when buffers
9697         (map-y-or-n-p
9698          "Update summary buffer %s? "
9699          (lambda (buf)
9700            (switch-to-buffer buf)
9701            (gnus-summary-exit))
9702          buffers)))))
9703
9704 (defun gnus-summary-setup-default-charset ()
9705   "Setup newsgroup default charset."
9706   (if (equal gnus-newsgroup-name "nndraft:drafts")
9707       (setq gnus-newsgroup-charset nil)
9708     (let* ((name (and gnus-newsgroup-name
9709                       (gnus-group-real-name gnus-newsgroup-name)))
9710            (ignored-charsets
9711             (or gnus-newsgroup-ephemeral-ignored-charsets
9712                 (append
9713                  (and gnus-newsgroup-name
9714                       (or (gnus-group-find-parameter gnus-newsgroup-name
9715                                                      'ignored-charsets t)
9716                           (let ((alist gnus-group-ignored-charsets-alist)
9717                                 elem (charsets nil))
9718                             (while (setq elem (pop alist))
9719                               (when (and name
9720                                          (string-match (car elem) name))
9721                                 (setq alist nil
9722                                       charsets (cdr elem))))
9723                             charsets)))
9724                  gnus-newsgroup-ignored-charsets))))
9725       (setq gnus-newsgroup-charset
9726             (or gnus-newsgroup-ephemeral-charset
9727                 (and gnus-newsgroup-name
9728                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9729                          (let ((alist gnus-group-charset-alist)
9730                                elem charset)
9731                            (while (setq elem (pop alist))
9732                              (when (and name
9733                                         (string-match (car elem) name))
9734                                (setq alist nil
9735                                      charset (cadr elem))))
9736                            charset)))
9737                 gnus-default-charset))
9738       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9739            ignored-charsets))))
9740
9741 ;;;
9742 ;;; Mime Commands
9743 ;;;
9744
9745 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9746   "Display the current article buffer fully MIME-buttonized.
9747 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9748 treated as multipart/mixed."
9749   (interactive "P")
9750   (require 'gnus-art)
9751   (let ((gnus-unbuttonized-mime-types nil)
9752         (gnus-mime-display-multipart-as-mixed show-all-parts))
9753     (gnus-summary-show-article)))
9754
9755 (defun gnus-summary-repair-multipart (article)
9756   "Add a Content-Type header to a multipart article without one."
9757   (interactive (list (gnus-summary-article-number)))
9758   (gnus-with-article article
9759     (message-narrow-to-head)
9760     (message-remove-header "Mime-Version")
9761     (goto-char (point-max))
9762     (insert "Mime-Version: 1.0\n")
9763     (widen)
9764     (when (search-forward "\n--" nil t)
9765       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9766         (message-narrow-to-head)
9767         (message-remove-header "Content-Type")
9768         (goto-char (point-max))
9769         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9770                         separator))
9771         (widen))))
9772   (let (gnus-mark-article-hook)
9773     (gnus-summary-select-article t t nil article)))
9774
9775 (defun gnus-summary-toggle-display-buttonized ()
9776   "Toggle the buttonizing of the article buffer."
9777   (interactive)
9778   (require 'gnus-art)
9779   (if (setq gnus-inhibit-mime-unbuttonizing
9780             (not gnus-inhibit-mime-unbuttonizing))
9781       (let ((gnus-unbuttonized-mime-types nil))
9782         (gnus-summary-show-article))
9783     (gnus-summary-show-article)))
9784
9785 ;;;
9786 ;;; Generic summary marking commands
9787 ;;;
9788
9789 (defvar gnus-summary-marking-alist
9790   '((read gnus-del-mark "d")
9791     (unread gnus-unread-mark "u")
9792     (ticked gnus-ticked-mark "!")
9793     (dormant gnus-dormant-mark "?")
9794     (expirable gnus-expirable-mark "e"))
9795   "An alist of names/marks/keystrokes.")
9796
9797 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9798 (defvar gnus-summary-mark-map)
9799
9800 (defun gnus-summary-make-all-marking-commands ()
9801   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9802   (dolist (elem gnus-summary-marking-alist)
9803     (apply 'gnus-summary-make-marking-command elem)))
9804
9805 (defun gnus-summary-make-marking-command (name mark keystroke)
9806   (let ((map (make-sparse-keymap)))
9807     (define-key gnus-summary-generic-mark-map keystroke map)
9808     (dolist (lway `((next "next" next nil "n")
9809                     (next-unread "next unread" next t "N")
9810                     (prev "previous" prev nil "p")
9811                     (prev-unread "previous unread" prev t "P")
9812                     (nomove "" nil nil ,keystroke)))
9813       (let ((func (gnus-summary-make-marking-command-1
9814                    mark (car lway) lway name)))
9815         (setq func (eval func))
9816         (define-key map (nth 4 lway) func)))))
9817       
9818 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9819   `(defun ,(intern
9820             (format "gnus-summary-put-mark-as-%s%s"
9821                     name (if (eq way 'nomove)
9822                              ""
9823                            (concat "-" (symbol-name way)))))
9824      (n)
9825      ,(format
9826        "Mark the current article as %s%s.
9827 If N, the prefix, then repeat N times.
9828 If N is negative, move in reverse order.
9829 The difference between N and the actual number of articles marked is
9830 returned."
9831        name (cadr lway))
9832      (interactive "p")
9833      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9834     
9835 (defun gnus-summary-generic-mark (n mark move unread)
9836   "Mark N articles with MARK."
9837   (unless (eq major-mode 'gnus-summary-mode)
9838     (error "This command can only be used in the summary buffer"))
9839   (gnus-summary-show-thread)
9840   (let ((nummove
9841          (cond
9842           ((eq move 'next) 1)
9843           ((eq move 'prev) -1)
9844           (t 0))))
9845     (if (zerop nummove)
9846         (setq n 1)
9847       (when (< n 0)
9848         (setq n (abs n)
9849               nummove (* -1 nummove))))
9850     (while (and (> n 0)
9851                 (gnus-summary-mark-article nil mark)
9852                 (zerop (gnus-summary-next-subject nummove unread t)))
9853       (setq n (1- n)))
9854     (when (/= 0 n)
9855       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9856     (gnus-summary-recenter)
9857     (gnus-summary-position-point)
9858     (gnus-set-mode-line 'summary)
9859     n))
9860
9861 (gnus-summary-make-all-marking-commands)
9862
9863 (gnus-ems-redefine)
9864
9865 (provide 'gnus-sum)
9866
9867 (run-hooks 'gnus-sum-load-hook)
9868
9869 ;;; gnus-sum.el ends here