4c3b4ba638ea335cac11336f778d61623bd01ea5
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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 ;; Recursive :-(.
40 ;; (require 'gnus-art)
41 (require 'nnoo)
42 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
43 (autoload 'gnus-cache-write-active "gnus-cache")
44 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
45 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
46 (autoload 'mm-uu-dissect "mm-uu")
47
48 (defcustom gnus-kill-summary-on-exit t
49   "*If non-nil, kill the summary buffer when you exit from it.
50 If nil, the summary will become a \"*Dead Summary*\" buffer, and
51 it will be killed sometime later."
52   :group 'gnus-summary-exit
53   :type 'boolean)
54
55 (defcustom gnus-fetch-old-headers nil
56   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
57 If an unread article in the group refers to an older, already read (or
58 just marked as read) article, the old article will not normally be
59 displayed in the Summary buffer.  If this variable is non-nil, Gnus
60 will attempt to grab the headers to the old articles, and thereby
61 build complete threads.  If it has the value `some', only enough
62 headers to connect otherwise loose threads will be displayed.  This
63 variable can also be a number.  In that case, no more than that number
64 of old headers will be fetched.  If it has the value `invisible', all
65 old headers will be fetched, but none will be displayed.
66
67 The server has to support NOV for any of this to work."
68   :group 'gnus-thread
69   :type '(choice (const :tag "off" nil)
70                  (const some)
71                  number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-refer-thread-limit 200
75   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
76 If t, fetch all the available old headers."
77   :group 'gnus-thread
78   :type '(choice number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-summary-make-false-root 'adopt
82   "*nil means that Gnus won't gather loose threads.
83 If the root of a thread has expired or been read in a previous
84 session, the information necessary to build a complete thread has been
85 lost.  Instead of having many small sub-threads from this original thread
86 scattered all over the summary buffer, Gnus can gather them.
87
88 If non-nil, Gnus will try to gather all loose sub-threads from an
89 original thread into one large thread.
90
91 If this variable is non-nil, it should be one of `none', `adopt',
92 `dummy' or `empty'.
93
94 If this variable is `none', Gnus will not make a false root, but just
95 present the sub-threads after another.
96 If this variable is `dummy', Gnus will create a dummy root that will
97 have all the sub-threads as children.
98 If this variable is `adopt', Gnus will make one of the \"children\"
99 the parent and mark all the step-children as such.
100 If this variable is `empty', the \"children\" are printed with empty
101 subject fields.  (Or rather, they will be printed with a string
102 given by the `gnus-summary-same-subject' variable.)"
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const none)
106                  (const dummy)
107                  (const adopt)
108                  (const empty)))
109
110 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
111   "*A regexp to match subjects to be excluded from loose thread gathering.
112 As loose thread gathering is done on subjects only, that means that
113 there can be many false gatherings performed.  By rooting out certain
114 common subjects, gathering might become saner."
115   :group 'gnus-thread
116   :type 'regexp)
117
118 (defcustom gnus-summary-gather-subject-limit nil
119   "*Maximum length of subject comparisons when gathering loose threads.
120 Use nil to compare full subjects.  Setting this variable to a low
121 number will help gather threads that have been corrupted by
122 newsreaders chopping off subject lines, but it might also mean that
123 unrelated articles that have subject that happen to begin with the
124 same few characters will be incorrectly gathered.
125
126 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
127 comparing subjects."
128   :group 'gnus-thread
129   :type '(choice (const :tag "off" nil)
130                  (const fuzzy)
131                  (sexp :menu-tag "on" t)))
132
133 (defcustom gnus-simplify-subject-functions nil
134   "List of functions taking a string argument that simplify subjects.
135 The functions are applied recursively.
136
137 Useful functions to put in this list include: `gnus-simplify-subject-re',
138 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
139   :group 'gnus-thread
140   :type '(repeat function))
141
142 (defcustom gnus-simplify-ignored-prefixes nil
143   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  regexp))
147
148 (defcustom gnus-build-sparse-threads nil
149   "*If non-nil, fill in the gaps in threads.
150 If `some', only fill in the gaps that are needed to tie loose threads
151 together.  If `more', fill in all leaf nodes that Gnus can find.  If
152 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const some)
156                  (const more)
157                  (sexp :menu-tag "all" t)))
158
159 (defcustom gnus-summary-thread-gathering-function
160   'gnus-gather-threads-by-subject
161   "*Function used for gathering loose threads.
162 There are two pre-defined functions: `gnus-gather-threads-by-subject',
163 which only takes Subjects into consideration; and
164 `gnus-gather-threads-by-references', which compared the References
165 headers of the articles to find matches."
166   :group 'gnus-thread
167   :type '(radio (function-item gnus-gather-threads-by-subject)
168                 (function-item gnus-gather-threads-by-references)
169                 (function :tag "other")))
170
171 (defcustom gnus-summary-same-subject ""
172   "*String indicating that the current article has the same subject as the previous.
173 This variable will only be used if the value of
174 `gnus-summary-make-false-root' is `empty'."
175   :group 'gnus-summary-format
176   :type 'string)
177
178 (defcustom gnus-summary-goto-unread t
179   "*If t, many commands will go to the next unread article.
180 This applies to marking commands as well as other commands that
181 \"naturally\" select the next article, like, for instance, `SPC' at
182 the end of an article.
183
184 If nil, the marking commands do NOT go to the next unread article
185 (they go to the next article instead).  If `never', commands that
186 usually go to the next unread article, will go to the next article,
187 whether it is read or not."
188   :group 'gnus-summary-marks
189   :link '(custom-manual "(gnus)Setting Marks")
190   :type '(choice (const :tag "off" nil)
191                  (const never)
192                  (sexp :menu-tag "on" t)))
193
194 (defcustom gnus-summary-default-score 0
195   "*Default article score level.
196 All scores generated by the score files will be added to this score.
197 If this variable is nil, scoring will be disabled."
198   :group 'gnus-score-default
199   :type '(choice (const :tag "disable")
200                  integer))
201
202 (defcustom gnus-summary-default-high-score 0
203   "*Default threshold for a high scored article.
204 An article will be highlighted as high scored if its score is greater
205 than this score."
206   :group 'gnus-score-default
207   :type 'integer)
208
209 (defcustom gnus-summary-default-low-score 0
210   "*Default threshold for a low scored article.
211 An article will be highlighted as low scored if its score is smaller
212 than this score."
213   :group 'gnus-score-default
214   :type 'integer)
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
300
301 (defcustom gnus-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-move-split-methods nil
368   "*Variable used to suggest where articles are to be moved to.
369 It uses the same syntax as the `gnus-split-methods' variable.
370 However, whereas `gnus-split-methods' specifies file names as targets,
371 this variable specifies group names."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ?  ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for souped articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-forwarded-mark ?O
438   "*Mark used for articles that have been forwarded."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-cached-mark ?*
443   "*Mark used for articles that are in the cache."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-saved-mark ?S
448   "*Mark used for articles that have been saved to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-no-mark ?  ;Whitespace
453   "*Mark used for articles that have no other secondary mark."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-ancient-mark ?O
458   "*Mark used for ancient articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-sparse-mark ?Q
463   "*Mark used for sparsely reffed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-canceled-mark ?G
468   "*Mark used for canceled articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-duplicate-mark ?M
473   "*Mark used for duplicate articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-undownloaded-mark ?@
478   "*Mark used for articles that weren't downloaded."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-downloadable-mark ?%
483   "*Mark used for articles that are to be downloaded."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-unsendable-mark ?=
488   "*Mark used for articles that won't be sent."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-score-over-mark ?+
493   "*Score mark used for articles with high scores."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-score-below-mark ?-
498   "*Score mark used for articles with low scores."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-empty-thread-mark ?  ;Whitespace
503   "*There is no thread under the article."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-not-empty-thread-mark ?=
508   "*There is a thread under the article."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-view-pseudo-asynchronously nil
513   "*If non-nil, Gnus will view pseudo-articles asynchronously."
514   :group 'gnus-extract-view
515   :type 'boolean)
516
517 (defcustom gnus-auto-expirable-marks
518   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
519         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
520         gnus-souped-mark gnus-duplicate-mark)
521   "*The list of marks converted into expiration if a group is auto-expirable."
522   :version "21.1"
523   :group 'gnus-summary
524   :type '(repeat character))
525
526 (defcustom gnus-inhibit-user-auto-expire t
527   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
528   :version "21.1"
529   :group 'gnus-summary
530   :type 'boolean)
531
532 (defcustom gnus-view-pseudos nil
533   "*If `automatic', pseudo-articles will be viewed automatically.
534 If `not-confirm', pseudos will be viewed automatically, and the user
535 will not be asked to confirm the command."
536   :group 'gnus-extract-view
537   :type '(choice (const :tag "off" nil)
538                  (const automatic)
539                  (const not-confirm)))
540
541 (defcustom gnus-view-pseudos-separately t
542   "*If non-nil, one pseudo-article will be created for each file to be viewed.
543 If nil, all files that use the same viewing command will be given as a
544 list of parameters to that command."
545   :group 'gnus-extract-view
546   :type 'boolean)
547
548 (defcustom gnus-insert-pseudo-articles t
549   "*If non-nil, insert pseudo-articles when decoding articles."
550   :group 'gnus-extract-view
551   :type 'boolean)
552
553 (defcustom gnus-summary-dummy-line-format
554   "  %(:                          :%) %S\n"
555   "*The format specification for the dummy roots in the summary buffer.
556 It works along the same lines as a normal formatting string,
557 with some simple extensions.
558
559 %S  The subject"
560   :group 'gnus-threading
561   :type 'string)
562
563 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
564   "*The format specification for the summary mode line.
565 It works along the same lines as a normal formatting string,
566 with some simple extensions:
567
568 %G  Group name
569 %p  Unprefixed group name
570 %A  Current article number
571 %z  Current article score
572 %V  Gnus version
573 %U  Number of unread articles in the group
574 %e  Number of unselected articles in the group
575 %Z  A string with unread/unselected article counts
576 %g  Shortish group name
577 %S  Subject of the current article
578 %u  User-defined spec
579 %s  Current score file name
580 %d  Number of dormant articles
581 %r  Number of articles that have been marked as read in this session
582 %E  Number of articles expunged by the score files"
583   :group 'gnus-summary-format
584   :type 'string)
585
586 (defcustom gnus-list-identifiers nil
587   "Regexp that matches list identifiers to be removed from subject.
588 This can also be a list of regexps."
589   :version "21.1"
590   :group 'gnus-summary-format
591   :group 'gnus-article-hiding
592   :type '(choice (const :tag "none" nil)
593                  (regexp :value ".*")
594                  (repeat :value (".*") regexp)))
595
596 (defcustom gnus-summary-mark-below 0
597   "*Mark all articles with a score below this variable as read.
598 This variable is local to each summary buffer and usually set by the
599 score file."
600   :group 'gnus-score-default
601   :type 'integer)
602
603 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
604   "*List of functions used for sorting articles in the summary buffer.
605
606 Each function takes two articles and returns non-nil if the first
607 article should be sorted before the other.  If you use more than one
608 function, the primary sort function should be the last.  You should
609 probably always include `gnus-article-sort-by-number' in the list of
610 sorting functions -- preferably first.  Also note that sorting by date
611 is often much slower than sorting by number, and the sorting order is
612 very similar.  (Sorting by date means sorting by the time the message
613 was sent, sorting by number means sorting by arrival time.)
614
615 Ready-made functions include `gnus-article-sort-by-number',
616 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
617 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
618
619 When threading is turned on, the variable `gnus-thread-sort-functions'
620 controls how articles are sorted."
621   :group 'gnus-summary-sort
622   :type '(repeat (choice (function-item gnus-article-sort-by-number)
623                          (function-item gnus-article-sort-by-author)
624                          (function-item gnus-article-sort-by-subject)
625                          (function-item gnus-article-sort-by-date)
626                          (function-item gnus-article-sort-by-score)
627                          (function :tag "other"))))
628
629 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
630   "*List of functions used for sorting threads in the summary buffer.
631 By default, threads are sorted by article number.
632
633 Each function takes two threads and returns non-nil if the first
634 thread should be sorted before the other.  If you use more than one
635 function, the primary sort function should be the last.  You should
636 probably always include `gnus-thread-sort-by-number' in the list of
637 sorting functions -- preferably first.  Also note that sorting by date
638 is often much slower than sorting by number, and the sorting order is
639 very similar.  (Sorting by date means sorting by the time the message
640 was sent, sorting by number means sorting by arrival time.)
641
642 Ready-made functions include `gnus-thread-sort-by-number',
643 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
644 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
645 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
646
647 When threading is turned off, the variable
648 `gnus-article-sort-functions' controls how articles are sorted."
649   :group 'gnus-summary-sort
650   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
651                          (function-item gnus-thread-sort-by-author)
652                          (function-item gnus-thread-sort-by-subject)
653                          (function-item gnus-thread-sort-by-date)
654                          (function-item gnus-thread-sort-by-score)
655                          (function-item gnus-thread-sort-by-total-score)
656                          (function :tag "other"))))
657
658 (defcustom gnus-thread-score-function '+
659   "*Function used for calculating the total score of a thread.
660
661 The function is called with the scores of the article and each
662 subthread and should then return the score of the thread.
663
664 Some functions you can use are `+', `max', or `min'."
665   :group 'gnus-summary-sort
666   :type 'function)
667
668 (defcustom gnus-summary-expunge-below nil
669   "All articles that have a score less than this variable will be expunged.
670 This variable is local to the summary buffers."
671   :group 'gnus-score-default
672   :type '(choice (const :tag "off" nil)
673                  integer))
674
675 (defcustom gnus-thread-expunge-below nil
676   "All threads that have a total score less than this variable will be expunged.
677 See `gnus-thread-score-function' for en explanation of what a
678 \"thread score\" is.
679
680 This variable is local to the summary buffers."
681   :group 'gnus-threading
682   :group 'gnus-score-default
683   :type '(choice (const :tag "off" nil)
684                  integer))
685
686 (defcustom gnus-summary-mode-hook nil
687   "*A hook for Gnus summary mode.
688 This hook is run before any variables are set in the summary buffer."
689   :options '(turn-on-gnus-mailing-list-mode)
690   :group 'gnus-summary-various
691   :type 'hook)
692
693 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
694 (when (featurep 'xemacs)
695   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
696   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
697   (add-hook 'gnus-summary-mode-hook
698             'gnus-xmas-switch-horizontal-scrollbar-off))
699
700 (defcustom gnus-summary-menu-hook nil
701   "*Hook run after the creation of the summary mode menu."
702   :group 'gnus-summary-visual
703   :type 'hook)
704
705 (defcustom gnus-summary-exit-hook nil
706   "*A hook called on exit from the summary buffer.
707 It will be called with point in the group buffer."
708   :group 'gnus-summary-exit
709   :type 'hook)
710
711 (defcustom gnus-summary-prepare-hook nil
712   "*A hook called after the summary buffer has been generated.
713 If you want to modify the summary buffer, you can use this hook."
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 (defcustom gnus-summary-prepared-hook nil
718   "*A hook called as the last thing after the summary buffer has been generated."
719   :group 'gnus-summary-various
720   :type 'hook)
721
722 (defcustom gnus-summary-generate-hook nil
723   "*A hook run just before generating the summary buffer.
724 This hook is commonly used to customize threading variables and the
725 like."
726   :group 'gnus-summary-various
727   :type 'hook)
728
729 (defcustom gnus-select-group-hook nil
730   "*A hook called when a newsgroup is selected.
731
732 If you'd like to simplify subjects like the
733 `gnus-summary-next-same-subject' command does, you can use the
734 following hook:
735
736  (setq gnus-select-group-hook
737       (list
738         (lambda ()
739           (mapcar (lambda (header)
740                      (mail-header-set-subject
741                       header
742                       (gnus-simplify-subject
743                        (mail-header-subject header) 're-only)))
744                   gnus-newsgroup-headers))))"
745   :group 'gnus-group-select
746   :type 'hook)
747
748 (defcustom gnus-select-article-hook nil
749   "*A hook called when an article is selected."
750   :group 'gnus-summary-choose
751   :type 'hook)
752
753 (defcustom gnus-visual-mark-article-hook
754   (list 'gnus-highlight-selected-summary)
755   "*Hook run after selecting an article in the summary buffer.
756 It is meant to be used for highlighting the article in some way.  It
757 is not run if `gnus-visual' is nil."
758   :group 'gnus-summary-visual
759   :type 'hook)
760
761 (defcustom gnus-parse-headers-hook nil
762   "*A hook called before parsing the headers."
763   :group 'gnus-various
764   :type 'hook)
765
766 (defcustom gnus-exit-group-hook nil
767   "*A hook called when exiting summary mode.
768 This hook is not called from the non-updating exit commands like `Q'."
769   :group 'gnus-various
770   :type 'hook)
771
772 (defcustom gnus-summary-update-hook
773   (list 'gnus-summary-highlight-line)
774   "*A hook called when a summary line is changed.
775 The hook will not be called if `gnus-visual' is nil.
776
777 The default function `gnus-summary-highlight-line' will
778 highlight the line according to the `gnus-summary-highlight'
779 variable."
780   :group 'gnus-summary-visual
781   :type 'hook)
782
783 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
784   "*A hook called when an article is selected for the first time.
785 The hook is intended to mark an article as read (or unread)
786 automatically when it is selected."
787   :group 'gnus-summary-choose
788   :type 'hook)
789
790 (defcustom gnus-group-no-more-groups-hook nil
791   "*A hook run when returning to group mode having no more (unread) groups."
792   :group 'gnus-group-select
793   :type 'hook)
794
795 (defcustom gnus-ps-print-hook nil
796   "*A hook run before ps-printing something from Gnus."
797   :group 'gnus-summary
798   :type 'hook)
799
800 (defcustom gnus-summary-display-arrow
801   (and (fboundp 'display-graphic-p)
802        (display-graphic-p))
803   "*If non-nil, display an arrow highlighting the current article."
804   :version "21.1"
805   :group 'gnus-summary
806   :type 'boolean)
807
808 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
809   "Face used for highlighting the current article in the summary buffer."
810   :group 'gnus-summary-visual
811   :type 'face)
812
813 (defcustom gnus-summary-highlight
814   '(((= mark gnus-canceled-mark)
815      . gnus-summary-cancelled-face)
816     ((and (> score default-high)
817           (or (= mark gnus-dormant-mark)
818               (= mark gnus-ticked-mark)))
819      . gnus-summary-high-ticked-face)
820     ((and (< score default-low)
821           (or (= mark gnus-dormant-mark)
822               (= mark gnus-ticked-mark)))
823      . gnus-summary-low-ticked-face)
824     ((or (= mark gnus-dormant-mark)
825          (= mark gnus-ticked-mark))
826      . gnus-summary-normal-ticked-face)
827     ((and (> score default-high) (= mark gnus-ancient-mark))
828      . gnus-summary-high-ancient-face)
829     ((and (< score default-low) (= mark gnus-ancient-mark))
830      . gnus-summary-low-ancient-face)
831     ((= mark gnus-ancient-mark)
832      . gnus-summary-normal-ancient-face)
833     ((and (> score default-high) (= mark gnus-unread-mark))
834      . gnus-summary-high-unread-face)
835     ((and (< score default-low) (= mark gnus-unread-mark))
836      . gnus-summary-low-unread-face)
837     ((= mark gnus-unread-mark)
838      . gnus-summary-normal-unread-face)
839     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
840                                              gnus-undownloaded-mark)))
841      . gnus-summary-high-unread-face)
842     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
843                                              gnus-undownloaded-mark)))
844      . gnus-summary-low-unread-face)
845     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
846           (memq article gnus-newsgroup-unreads))
847      . gnus-summary-normal-unread-face)
848     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
849      . gnus-summary-normal-read-face)
850     ((> score default-high)
851      . gnus-summary-high-read-face)
852     ((< score default-low)
853      . gnus-summary-low-read-face)
854     (t
855      . gnus-summary-normal-read-face))
856   "*Controls the highlighting of summary buffer lines.
857
858 A list of (FORM . FACE) pairs.  When deciding how a a particular
859 summary line should be displayed, each form is evaluated.  The content
860 of the face field after the first true form is used.  You can change
861 how those summary lines are displayed, by editing the face field.
862
863 You can use the following variables in the FORM field.
864
865 score:        The article's score
866 default:      The default article score.
867 default-high: The default score for high scored articles.
868 default-low:  The default score for low scored articles.
869 below:        The score below which articles are automatically marked as read.
870 mark:         The articles mark."
871   :group 'gnus-summary-visual
872   :type '(repeat (cons (sexp :tag "Form" nil)
873                        face)))
874
875 (defcustom gnus-alter-header-function nil
876   "Function called to allow alteration of article header structures.
877 The function is called with one parameter, the article header vector,
878 which it may alter in any way.")
879
880 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
881   "Variable that says which function should be used to decode a string with encoded words.")
882
883 (defcustom gnus-extra-headers nil
884   "*Extra headers to parse."
885   :version "21.1"
886   :group 'gnus-summary
887   :type '(repeat symbol))
888
889 (defcustom gnus-ignored-from-addresses
890   (and user-mail-address (regexp-quote user-mail-address))
891   "*Regexp of From headers that may be suppressed in favor of To headers."
892   :version "21.1"
893   :group 'gnus-summary
894   :type 'regexp)
895
896 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
897   "List of charsets that should be ignored.
898 When these charsets are used in the \"charset\" parameter, the
899 default charset will be used instead."
900   :version "21.1"
901   :type '(repeat symbol)
902   :group 'gnus-charset)
903
904 (gnus-define-group-parameter
905  ignored-charsets
906  :type list
907  :function-document
908  "Return the ignored charsets of GROUP."
909  :variable gnus-group-ignored-charsets-alist
910  :variable-default 
911  '(("alt\\.chinese\\.text" iso-8859-1))
912  :variable-document
913  "Alist of regexps (to match group names) and charsets that should be ignored.
914 When these charsets are used in the \"charset\" parameter, the
915 default charset will be used instead."
916  :variable-group gnus-charset
917  :variable-type '(repeat (cons (regexp :tag "Group")
918                                (repeat symbol)))
919  :parameter-type '(choice :tag "Ignored charsets" 
920                           :value nil
921                           (repeat (symbol)))
922  :parameter-document       "\
923 List of charsets that should be ignored.
924
925 When these charsets are used in the \"charset\" parameter, the
926 default charset will be used instead.")
927
928 (defcustom gnus-group-highlight-words-alist nil
929   "Alist of group regexps and highlight regexps.
930 This variable uses the same syntax as `gnus-emphasis-alist'."
931   :version "21.1"
932   :type '(repeat (cons (regexp :tag "Group")
933                        (repeat (list (regexp :tag "Highlight regexp")
934                                      (number :tag "Group for entire word" 0)
935                                      (number :tag "Group for displayed part" 0)
936                                      (symbol :tag "Face"
937                                              gnus-emphasis-highlight-words)))))
938   :group 'gnus-summary-visual)
939
940 (defcustom gnus-summary-show-article-charset-alist
941   nil
942   "Alist of number and charset.
943 The article will be shown with the charset corresponding to the
944 numbered argument.
945 For example: ((1 . cn-gb-2312) (2 . big5))."
946   :version "21.1"
947   :type '(repeat (cons (number :tag "Argument" 1)
948                        (symbol :tag "Charset")))
949   :group 'gnus-charset)
950
951 (defcustom gnus-preserve-marks t
952   "Whether marks are preserved when moving, copying and respooling messages."
953   :version "21.1"
954   :type 'boolean
955   :group 'gnus-summary-marks)
956
957 (defcustom gnus-alter-articles-to-read-function nil
958   "Function to be called to alter the list of articles to be selected."
959   :type '(choice (const nil) function)
960   :group 'gnus-summary)
961
962 (defcustom gnus-orphan-score nil
963   "*All orphans get this score added.  Set in the score file."
964   :group 'gnus-score-default
965   :type '(choice (const nil)
966                  integer))
967
968 (defcustom gnus-summary-save-parts-default-mime "image/.*"
969   "*A regexp to match MIME parts when saving multiple parts of a message
970 with gnus-summary-save-parts (X m). This regexp will be used by default
971 when prompting the user for which type of files to save."
972   :group 'gnus-summary
973   :type 'regexp)
974
975
976 (defcustom gnus-summary-save-parts-default-mime "image/.*"
977   "*A regexp to match MIME parts when saving multiple parts of a message
978 with gnus-summary-save-parts (X m). This regexp will be used by default
979 when prompting the user for which type of files to save."
980   :group 'gnus-summary
981   :type 'regexp)
982
983 (defcustom gnus-read-all-available-headers nil
984   "Whether Gnus should parse all headers made available to it.
985 This is mostly relevant for slow backends where the user may
986 wish to widen the summary buffer to include all headers
987 that were fetched.  Say, for nnultimate groups."
988   :group 'gnus-summary
989   :type '(choice boolean regexp))
990
991 ;;; Internal variables
992
993 (defvar gnus-article-mime-handles nil)
994 (defvar gnus-article-decoded-p nil)
995 (defvar gnus-article-charset nil)
996 (defvar gnus-article-ignored-charsets nil)
997 (defvar gnus-scores-exclude-files nil)
998 (defvar gnus-page-broken nil)
999 (defvar gnus-inhibit-mime-unbuttonizing nil)
1000
1001 (defvar gnus-original-article nil)
1002 (defvar gnus-article-internal-prepare-hook nil)
1003 (defvar gnus-newsgroup-process-stack nil)
1004
1005 (defvar gnus-thread-indent-array nil)
1006 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1007 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1008   "Function called to sort the articles within a thread after it has been gathered together.")
1009
1010 (defvar gnus-summary-save-parts-type-history nil)
1011 (defvar gnus-summary-save-parts-last-directory nil)
1012
1013 (defvar gnus-summary-save-parts-type-history nil)
1014 (defvar gnus-summary-save-parts-last-directory nil)
1015
1016 ;; Avoid highlighting in kill files.
1017 (defvar gnus-summary-inhibit-highlight nil)
1018 (defvar gnus-newsgroup-selected-overlay nil)
1019 (defvar gnus-inhibit-limiting nil)
1020 (defvar gnus-newsgroup-adaptive-score-file nil)
1021 (defvar gnus-current-score-file nil)
1022 (defvar gnus-current-move-group nil)
1023 (defvar gnus-current-copy-group nil)
1024 (defvar gnus-current-crosspost-group nil)
1025
1026 (defvar gnus-newsgroup-dependencies nil)
1027 (defvar gnus-newsgroup-adaptive nil)
1028 (defvar gnus-summary-display-article-function nil)
1029 (defvar gnus-summary-highlight-line-function nil
1030   "Function called after highlighting a summary line.")
1031
1032 (defvar gnus-summary-line-format-alist
1033   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1034     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1035     (?s gnus-tmp-subject-or-nil ?s)
1036     (?n gnus-tmp-name ?s)
1037     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1038         ?s)
1039     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1040             gnus-tmp-from) ?s)
1041     (?F gnus-tmp-from ?s)
1042     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1043     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1044     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1045     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1046     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1047     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1048     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1049     (?L gnus-tmp-lines ?s)
1050     (?I gnus-tmp-indentation ?s)
1051     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1052     (?R gnus-tmp-replied ?c)
1053     (?\[ gnus-tmp-opening-bracket ?c)
1054     (?\] gnus-tmp-closing-bracket ?c)
1055     (?\> (make-string gnus-tmp-level ? ) ?s)
1056     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1057     (?i gnus-tmp-score ?d)
1058     (?z gnus-tmp-score-char ?c)
1059     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1060     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1061     (?U gnus-tmp-unread ?c)
1062     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1063     (?t (gnus-summary-number-of-articles-in-thread
1064          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1065         ?d)
1066     (?e (gnus-summary-number-of-articles-in-thread
1067          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1068         ?c)
1069     (?u gnus-tmp-user-defined ?s)
1070     (?P (gnus-pick-line-number) ?d)
1071     (?B gnus-tmp-thread-tree-header-string ?s))
1072   "An alist of format specifications that can appear in summary lines.
1073 These are paired with what variables they correspond with, along with
1074 the type of the variable (string, integer, character, etc).")
1075
1076 (defvar gnus-summary-dummy-line-format-alist
1077   `((?S gnus-tmp-subject ?s)
1078     (?N gnus-tmp-number ?d)
1079     (?u gnus-tmp-user-defined ?s)))
1080
1081 (defvar gnus-summary-mode-line-format-alist
1082   `((?G gnus-tmp-group-name ?s)
1083     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1084     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1085     (?A gnus-tmp-article-number ?d)
1086     (?Z gnus-tmp-unread-and-unselected ?s)
1087     (?V gnus-version ?s)
1088     (?U gnus-tmp-unread-and-unticked ?d)
1089     (?S gnus-tmp-subject ?s)
1090     (?e gnus-tmp-unselected ?d)
1091     (?u gnus-tmp-user-defined ?s)
1092     (?d (length gnus-newsgroup-dormant) ?d)
1093     (?t (length gnus-newsgroup-marked) ?d)
1094     (?r (length gnus-newsgroup-reads) ?d)
1095     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1096     (?E gnus-newsgroup-expunged-tally ?d)
1097     (?s (gnus-current-score-file-nondirectory) ?s)))
1098
1099 (defvar gnus-last-search-regexp nil
1100   "Default regexp for article search command.")
1101
1102 (defvar gnus-last-shell-command nil
1103   "Default shell command on article.")
1104
1105 (defvar gnus-newsgroup-begin nil)
1106 (defvar gnus-newsgroup-end nil)
1107 (defvar gnus-newsgroup-last-rmail nil)
1108 (defvar gnus-newsgroup-last-mail nil)
1109 (defvar gnus-newsgroup-last-folder nil)
1110 (defvar gnus-newsgroup-last-file nil)
1111 (defvar gnus-newsgroup-auto-expire nil)
1112 (defvar gnus-newsgroup-active nil)
1113
1114 (defvar gnus-newsgroup-data nil)
1115 (defvar gnus-newsgroup-data-reverse nil)
1116 (defvar gnus-newsgroup-limit nil)
1117 (defvar gnus-newsgroup-limits nil)
1118
1119 (defvar gnus-newsgroup-unreads nil
1120   "List of unread articles in the current newsgroup.")
1121
1122 (defvar gnus-newsgroup-unselected nil
1123   "List of unselected unread articles in the current newsgroup.")
1124
1125 (defvar gnus-newsgroup-reads nil
1126   "Alist of read articles and article marks in the current newsgroup.")
1127
1128 (defvar gnus-newsgroup-expunged-tally nil)
1129
1130 (defvar gnus-newsgroup-marked nil
1131   "List of ticked articles in the current newsgroup (a subset of unread art).")
1132
1133 (defvar gnus-newsgroup-killed nil
1134   "List of ranges of articles that have been through the scoring process.")
1135
1136 (defvar gnus-newsgroup-cached nil
1137   "List of articles that come from the article cache.")
1138
1139 (defvar gnus-newsgroup-saved nil
1140   "List of articles that have been saved.")
1141
1142 (defvar gnus-newsgroup-kill-headers nil)
1143
1144 (defvar gnus-newsgroup-replied nil
1145   "List of articles that have been replied to in the current newsgroup.")
1146
1147 (defvar gnus-newsgroup-forwarded nil
1148   "List of articles that have been forwarded in the current newsgroup.")
1149
1150 (defvar gnus-newsgroup-expirable nil
1151   "List of articles in the current newsgroup that can be expired.")
1152
1153 (defvar gnus-newsgroup-processable nil
1154   "List of articles in the current newsgroup that can be processed.")
1155
1156 (defvar gnus-newsgroup-downloadable nil
1157   "List of articles in the current newsgroup that can be processed.")
1158
1159 (defvar gnus-newsgroup-undownloaded nil
1160   "List of articles in the current newsgroup that haven't been downloaded..")
1161
1162 (defvar gnus-newsgroup-unsendable nil
1163   "List of articles in the current newsgroup that won't be sent.")
1164
1165 (defvar gnus-newsgroup-bookmarks nil
1166   "List of articles in the current newsgroup that have bookmarks.")
1167
1168 (defvar gnus-newsgroup-dormant nil
1169   "List of dormant articles in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-scored nil
1172   "List of scored articles in the current newsgroup.")
1173
1174 (defvar gnus-newsgroup-headers nil
1175   "List of article headers in the current newsgroup.")
1176
1177 (defvar gnus-newsgroup-threads nil)
1178
1179 (defvar gnus-newsgroup-prepared nil
1180   "Whether the current group has been prepared properly.")
1181
1182 (defvar gnus-newsgroup-ancient nil
1183   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-sparse nil)
1186
1187 (defvar gnus-current-article nil)
1188 (defvar gnus-article-current nil)
1189 (defvar gnus-current-headers nil)
1190 (defvar gnus-have-all-headers nil)
1191 (defvar gnus-last-article nil)
1192 (defvar gnus-newsgroup-history nil)
1193 (defvar gnus-newsgroup-charset nil)
1194 (defvar gnus-newsgroup-ephemeral-charset nil)
1195 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1196
1197 (defvar gnus-article-before-search nil)
1198
1199 (defconst gnus-summary-local-variables
1200   '(gnus-newsgroup-name
1201     gnus-newsgroup-begin gnus-newsgroup-end
1202     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1203     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1204     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1205     gnus-newsgroup-unselected gnus-newsgroup-marked
1206     gnus-newsgroup-reads gnus-newsgroup-saved
1207     gnus-newsgroup-replied gnus-newsgroup-forwarded
1208     gnus-newsgroup-expirable
1209     gnus-newsgroup-processable gnus-newsgroup-killed
1210     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1211     gnus-newsgroup-unsendable
1212     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1213     gnus-newsgroup-headers gnus-newsgroup-threads
1214     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1215     gnus-current-article gnus-current-headers gnus-have-all-headers
1216     gnus-last-article gnus-article-internal-prepare-hook
1217     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1218     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1219     gnus-thread-expunge-below
1220     gnus-score-alist gnus-current-score-file
1221     (gnus-summary-expunge-below . global)
1222     (gnus-summary-mark-below . global)
1223     (gnus-orphan-score . global)
1224     gnus-newsgroup-active gnus-scores-exclude-files
1225     gnus-newsgroup-history gnus-newsgroup-ancient
1226     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1227     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1228     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1229     (gnus-newsgroup-expunged-tally . 0)
1230     gnus-cache-removable-articles gnus-newsgroup-cached
1231     gnus-newsgroup-data gnus-newsgroup-data-reverse
1232     gnus-newsgroup-limit gnus-newsgroup-limits
1233     gnus-newsgroup-charset)
1234   "Variables that are buffer-local to the summary buffers.")
1235
1236 (defvar gnus-newsgroup-variables nil
1237   "Variables that have separate values in the newsgroups.")
1238
1239 ;; Byte-compiler warning.
1240 (eval-when-compile (defvar gnus-article-mode-map))
1241
1242 ;; MIME stuff.
1243
1244 (defvar gnus-decode-encoded-word-methods
1245   '(mail-decode-encoded-word-string)
1246   "List of methods used to decode encoded words.
1247
1248 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1249 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1250 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1251 whose names match REGEXP.
1252
1253 For example:
1254 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1255  mail-decode-encoded-word-string
1256  (\"chinese\" . rfc1843-decode-string))")
1257
1258 (defvar gnus-decode-encoded-word-methods-cache nil)
1259
1260 (defun gnus-multi-decode-encoded-word-string (string)
1261   "Apply the functions from `gnus-encoded-word-methods' that match."
1262   (unless (and gnus-decode-encoded-word-methods-cache
1263                (eq gnus-newsgroup-name
1264                    (car gnus-decode-encoded-word-methods-cache)))
1265     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1266     (mapcar (lambda (x)
1267               (if (symbolp x)
1268                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1269                 (if (and gnus-newsgroup-name
1270                          (string-match (car x) gnus-newsgroup-name))
1271                     (nconc gnus-decode-encoded-word-methods-cache
1272                            (list (cdr x))))))
1273           gnus-decode-encoded-word-methods))
1274   (let ((xlist gnus-decode-encoded-word-methods-cache))
1275     (pop xlist)
1276     (while xlist
1277       (setq string (funcall (pop xlist) string))))
1278   string)
1279
1280 ;; Subject simplification.
1281
1282 (defun gnus-simplify-whitespace (str)
1283   "Remove excessive whitespace from STR."
1284   (let ((mystr str))
1285     ;; Multiple spaces.
1286     (while (string-match "[ \t][ \t]+" mystr)
1287       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1288                           " "
1289                           (substring mystr (match-end 0)))))
1290     ;; Leading spaces.
1291     (when (string-match "^[ \t]+" mystr)
1292       (setq mystr (substring mystr (match-end 0))))
1293     ;; Trailing spaces.
1294     (when (string-match "[ \t]+$" mystr)
1295       (setq mystr (substring mystr 0 (match-beginning 0))))
1296     mystr))
1297
1298 (defsubst gnus-simplify-subject-re (subject)
1299   "Remove \"Re:\" from subject lines."
1300   (if (string-match message-subject-re-regexp subject)
1301       (substring subject (match-end 0))
1302     subject))
1303
1304 (defun gnus-simplify-subject (subject &optional re-only)
1305   "Remove `Re:' and words in parentheses.
1306 If RE-ONLY is non-nil, strip leading `Re:'s only."
1307   (let ((case-fold-search t))           ;Ignore case.
1308     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1309     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1310       (setq subject (substring subject (match-end 0))))
1311     ;; Remove uninteresting prefixes.
1312     (when (and (not re-only)
1313                gnus-simplify-ignored-prefixes
1314                (string-match gnus-simplify-ignored-prefixes subject))
1315       (setq subject (substring subject (match-end 0))))
1316     ;; Remove words in parentheses from end.
1317     (unless re-only
1318       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1319         (setq subject (substring subject 0 (match-beginning 0)))))
1320     ;; Return subject string.
1321     subject))
1322
1323 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1324 ;; all whitespace.
1325 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1326   (goto-char (point-min))
1327   (while (re-search-forward regexp nil t)
1328     (replace-match (or newtext ""))))
1329
1330 (defun gnus-simplify-buffer-fuzzy ()
1331   "Simplify string in the buffer fuzzily.
1332 The string in the accessible portion of the current buffer is simplified.
1333 It is assumed to be a single-line subject.
1334 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1335 matter is removed.  Additional things can be deleted by setting
1336 `gnus-simplify-subject-fuzzy-regexp'."
1337   (let ((case-fold-search t)
1338         (modified-tick))
1339     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1340
1341     (while (not (eq modified-tick (buffer-modified-tick)))
1342       (setq modified-tick (buffer-modified-tick))
1343       (cond
1344        ((listp gnus-simplify-subject-fuzzy-regexp)
1345         (mapcar 'gnus-simplify-buffer-fuzzy-step
1346                 gnus-simplify-subject-fuzzy-regexp))
1347        (gnus-simplify-subject-fuzzy-regexp
1348         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1349       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1350       (gnus-simplify-buffer-fuzzy-step
1351        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1352       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1353
1354     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1355     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1356     (gnus-simplify-buffer-fuzzy-step " $")
1357     (gnus-simplify-buffer-fuzzy-step "^ +")))
1358
1359 (defun gnus-simplify-subject-fuzzy (subject)
1360   "Simplify a subject string fuzzily.
1361 See `gnus-simplify-buffer-fuzzy' for details."
1362   (save-excursion
1363     (gnus-set-work-buffer)
1364     (let ((case-fold-search t))
1365       ;; Remove uninteresting prefixes.
1366       (when (and gnus-simplify-ignored-prefixes
1367                  (string-match gnus-simplify-ignored-prefixes subject))
1368         (setq subject (substring subject (match-end 0))))
1369       (insert subject)
1370       (inline (gnus-simplify-buffer-fuzzy))
1371       (buffer-string))))
1372
1373 (defsubst gnus-simplify-subject-fully (subject)
1374   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1375   (cond
1376    (gnus-simplify-subject-functions
1377     (gnus-map-function gnus-simplify-subject-functions subject))
1378    ((null gnus-summary-gather-subject-limit)
1379     (gnus-simplify-subject-re subject))
1380    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1381     (gnus-simplify-subject-fuzzy subject))
1382    ((numberp gnus-summary-gather-subject-limit)
1383     (gnus-limit-string (gnus-simplify-subject-re subject)
1384                        gnus-summary-gather-subject-limit))
1385    (t
1386     subject)))
1387
1388 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1389   "Check whether two subjects are equal.
1390 If optional argument simple-first is t, first argument is already
1391 simplified."
1392   (cond
1393    ((null simple-first)
1394     (equal (gnus-simplify-subject-fully s1)
1395            (gnus-simplify-subject-fully s2)))
1396    (t
1397     (equal s1
1398            (gnus-simplify-subject-fully s2)))))
1399
1400 (defun gnus-summary-bubble-group ()
1401   "Increase the score of the current group.
1402 This is a handy function to add to `gnus-summary-exit-hook' to
1403 increase the score of each group you read."
1404   (gnus-group-add-score gnus-newsgroup-name))
1405
1406 \f
1407 ;;;
1408 ;;; Gnus summary mode
1409 ;;;
1410
1411 (put 'gnus-summary-mode 'mode-class 'special)
1412
1413 (defvar gnus-article-commands-menu)
1414
1415 (when t
1416   ;; Non-orthogonal keys
1417
1418   (gnus-define-keys gnus-summary-mode-map
1419     " " gnus-summary-next-page
1420     "\177" gnus-summary-prev-page
1421     [delete] gnus-summary-prev-page
1422     [backspace] gnus-summary-prev-page
1423     "\r" gnus-summary-scroll-up
1424     "\M-\r" gnus-summary-scroll-down
1425     "n" gnus-summary-next-unread-article
1426     "p" gnus-summary-prev-unread-article
1427     "N" gnus-summary-next-article
1428     "P" gnus-summary-prev-article
1429     "\M-\C-n" gnus-summary-next-same-subject
1430     "\M-\C-p" gnus-summary-prev-same-subject
1431     "\M-n" gnus-summary-next-unread-subject
1432     "\M-p" gnus-summary-prev-unread-subject
1433     "." gnus-summary-first-unread-article
1434     "," gnus-summary-best-unread-article
1435     "\M-s" gnus-summary-search-article-forward
1436     "\M-r" gnus-summary-search-article-backward
1437     "<" gnus-summary-beginning-of-article
1438     ">" gnus-summary-end-of-article
1439     "j" gnus-summary-goto-article
1440     "^" gnus-summary-refer-parent-article
1441     "\M-^" gnus-summary-refer-article
1442     "u" gnus-summary-tick-article-forward
1443     "!" gnus-summary-tick-article-forward
1444     "U" gnus-summary-tick-article-backward
1445     "d" gnus-summary-mark-as-read-forward
1446     "D" gnus-summary-mark-as-read-backward
1447     "E" gnus-summary-mark-as-expirable
1448     "\M-u" gnus-summary-clear-mark-forward
1449     "\M-U" gnus-summary-clear-mark-backward
1450     "k" gnus-summary-kill-same-subject-and-select
1451     "\C-k" gnus-summary-kill-same-subject
1452     "\M-\C-k" gnus-summary-kill-thread
1453     "\M-\C-l" gnus-summary-lower-thread
1454     "e" gnus-summary-edit-article
1455     "#" gnus-summary-mark-as-processable
1456     "\M-#" gnus-summary-unmark-as-processable
1457     "\M-\C-t" gnus-summary-toggle-threads
1458     "\M-\C-s" gnus-summary-show-thread
1459     "\M-\C-h" gnus-summary-hide-thread
1460     "\M-\C-f" gnus-summary-next-thread
1461     "\M-\C-b" gnus-summary-prev-thread
1462     [(meta down)] gnus-summary-next-thread
1463     [(meta up)] gnus-summary-prev-thread
1464     "\M-\C-u" gnus-summary-up-thread
1465     "\M-\C-d" gnus-summary-down-thread
1466     "&" gnus-summary-execute-command
1467     "c" gnus-summary-catchup-and-exit
1468     "\C-w" gnus-summary-mark-region-as-read
1469     "\C-t" gnus-summary-toggle-truncation
1470     "?" gnus-summary-mark-as-dormant
1471     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1472     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1473     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1474     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1475     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1476     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1477     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1478     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1479     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1480     "=" gnus-summary-expand-window
1481     "\C-x\C-s" gnus-summary-reselect-current-group
1482     "\M-g" gnus-summary-rescan-group
1483     "w" gnus-summary-stop-page-breaking
1484     "\C-c\C-r" gnus-summary-caesar-message
1485     "f" gnus-summary-followup
1486     "F" gnus-summary-followup-with-original
1487     "C" gnus-summary-cancel-article
1488     "r" gnus-summary-reply
1489     "R" gnus-summary-reply-with-original
1490     "\C-c\C-f" gnus-summary-mail-forward
1491     "o" gnus-summary-save-article
1492     "\C-o" gnus-summary-save-article-mail
1493     "|" gnus-summary-pipe-output
1494     "\M-k" gnus-summary-edit-local-kill
1495     "\M-K" gnus-summary-edit-global-kill
1496     ;; "V" gnus-version
1497     "\C-c\C-d" gnus-summary-describe-group
1498     "q" gnus-summary-exit
1499     "Q" gnus-summary-exit-no-update
1500     "\C-c\C-i" gnus-info-find-node
1501     gnus-mouse-2 gnus-mouse-pick-article
1502     "m" gnus-summary-mail-other-window
1503     "a" gnus-summary-post-news
1504     "x" gnus-summary-limit-to-unread
1505     "s" gnus-summary-isearch-article
1506     "t" gnus-summary-toggle-header
1507     "g" gnus-summary-show-article
1508     "l" gnus-summary-goto-last-article
1509     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1510     "\C-d" gnus-summary-enter-digest-group
1511     "\M-\C-d" gnus-summary-read-document
1512     "\M-\C-e" gnus-summary-edit-parameters
1513     "\M-\C-a" gnus-summary-customize-parameters
1514     "\C-c\C-b" gnus-bug
1515     "*" gnus-cache-enter-article
1516     "\M-*" gnus-cache-remove-article
1517     "\M-&" gnus-summary-universal-argument
1518     "\C-l" gnus-recenter
1519     "I" gnus-summary-increase-score
1520     "L" gnus-summary-lower-score
1521     "\M-i" gnus-symbolic-argument
1522     "h" gnus-summary-select-article-buffer
1523
1524     "b" gnus-article-view-part
1525     "\M-t" gnus-summary-toggle-display-buttonized
1526
1527     "V" gnus-summary-score-map
1528     "X" gnus-uu-extract-map
1529     "S" gnus-summary-send-map)
1530
1531   ;; Sort of orthogonal keymap
1532   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1533     "t" gnus-summary-tick-article-forward
1534     "!" gnus-summary-tick-article-forward
1535     "d" gnus-summary-mark-as-read-forward
1536     "r" gnus-summary-mark-as-read-forward
1537     "c" gnus-summary-clear-mark-forward
1538     " " gnus-summary-clear-mark-forward
1539     "e" gnus-summary-mark-as-expirable
1540     "x" gnus-summary-mark-as-expirable
1541     "?" gnus-summary-mark-as-dormant
1542     "b" gnus-summary-set-bookmark
1543     "B" gnus-summary-remove-bookmark
1544     "#" gnus-summary-mark-as-processable
1545     "\M-#" gnus-summary-unmark-as-processable
1546     "S" gnus-summary-limit-include-expunged
1547     "C" gnus-summary-catchup
1548     "H" gnus-summary-catchup-to-here
1549     "h" gnus-summary-catchup-from-here
1550     "\C-c" gnus-summary-catchup-all
1551     "k" gnus-summary-kill-same-subject-and-select
1552     "K" gnus-summary-kill-same-subject
1553     "P" gnus-uu-mark-map)
1554
1555   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1556     "c" gnus-summary-clear-above
1557     "u" gnus-summary-tick-above
1558     "m" gnus-summary-mark-above
1559     "k" gnus-summary-kill-below)
1560
1561   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1562     "/" gnus-summary-limit-to-subject
1563     "n" gnus-summary-limit-to-articles
1564     "w" gnus-summary-pop-limit
1565     "s" gnus-summary-limit-to-subject
1566     "a" gnus-summary-limit-to-author
1567     "u" gnus-summary-limit-to-unread
1568     "m" gnus-summary-limit-to-marks
1569     "M" gnus-summary-limit-exclude-marks
1570     "v" gnus-summary-limit-to-score
1571     "*" gnus-summary-limit-include-cached
1572     "D" gnus-summary-limit-include-dormant
1573     "T" gnus-summary-limit-include-thread
1574     "d" gnus-summary-limit-exclude-dormant
1575     "t" gnus-summary-limit-to-age
1576     "x" gnus-summary-limit-to-extra
1577     "E" gnus-summary-limit-include-expunged
1578     "c" gnus-summary-limit-exclude-childless-dormant
1579     "C" gnus-summary-limit-mark-excluded-as-read
1580     "o" gnus-summary-insert-old-articles
1581     "N" gnus-summary-insert-new-articles)
1582
1583   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1584     "n" gnus-summary-next-unread-article
1585     "p" gnus-summary-prev-unread-article
1586     "N" gnus-summary-next-article
1587     "P" gnus-summary-prev-article
1588     "\C-n" gnus-summary-next-same-subject
1589     "\C-p" gnus-summary-prev-same-subject
1590     "\M-n" gnus-summary-next-unread-subject
1591     "\M-p" gnus-summary-prev-unread-subject
1592     "f" gnus-summary-first-unread-article
1593     "b" gnus-summary-best-unread-article
1594     "j" gnus-summary-goto-article
1595     "g" gnus-summary-goto-subject
1596     "l" gnus-summary-goto-last-article
1597     "o" gnus-summary-pop-article)
1598
1599   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1600     "k" gnus-summary-kill-thread
1601     "l" gnus-summary-lower-thread
1602     "i" gnus-summary-raise-thread
1603     "T" gnus-summary-toggle-threads
1604     "t" gnus-summary-rethread-current
1605     "^" gnus-summary-reparent-thread
1606     "s" gnus-summary-show-thread
1607     "S" gnus-summary-show-all-threads
1608     "h" gnus-summary-hide-thread
1609     "H" gnus-summary-hide-all-threads
1610     "n" gnus-summary-next-thread
1611     "p" gnus-summary-prev-thread
1612     "u" gnus-summary-up-thread
1613     "o" gnus-summary-top-thread
1614     "d" gnus-summary-down-thread
1615     "#" gnus-uu-mark-thread
1616     "\M-#" gnus-uu-unmark-thread)
1617
1618   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1619     "g" gnus-summary-prepare
1620     "c" gnus-summary-insert-cached-articles)
1621
1622   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1623     "c" gnus-summary-catchup-and-exit
1624     "C" gnus-summary-catchup-all-and-exit
1625     "E" gnus-summary-exit-no-update
1626     "Q" gnus-summary-exit
1627     "Z" gnus-summary-exit
1628     "n" gnus-summary-catchup-and-goto-next-group
1629     "R" gnus-summary-reselect-current-group
1630     "G" gnus-summary-rescan-group
1631     "N" gnus-summary-next-group
1632     "s" gnus-summary-save-newsrc
1633     "P" gnus-summary-prev-group)
1634
1635   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1636     " " gnus-summary-next-page
1637     "n" gnus-summary-next-page
1638     "\177" gnus-summary-prev-page
1639     [delete] gnus-summary-prev-page
1640     "p" gnus-summary-prev-page
1641     "\r" gnus-summary-scroll-up
1642     "\M-\r" gnus-summary-scroll-down
1643     "<" gnus-summary-beginning-of-article
1644     ">" gnus-summary-end-of-article
1645     "b" gnus-summary-beginning-of-article
1646     "e" gnus-summary-end-of-article
1647     "^" gnus-summary-refer-parent-article
1648     "r" gnus-summary-refer-parent-article
1649     "D" gnus-summary-enter-digest-group
1650     "R" gnus-summary-refer-references
1651     "T" gnus-summary-refer-thread
1652     "g" gnus-summary-show-article
1653     "s" gnus-summary-isearch-article
1654     "P" gnus-summary-print-article
1655     "M" gnus-mailing-list-insinuate
1656     "t" gnus-article-babel)
1657
1658   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1659     "b" gnus-article-add-buttons
1660     "B" gnus-article-add-buttons-to-head
1661     "o" gnus-article-treat-overstrike
1662     "e" gnus-article-emphasize
1663     "w" gnus-article-fill-cited-article
1664     "Q" gnus-article-fill-long-lines
1665     "C" gnus-article-capitalize-sentences
1666     "c" gnus-article-remove-cr
1667     "q" gnus-article-de-quoted-unreadable
1668     "6" gnus-article-de-base64-unreadable
1669     "Z" gnus-article-decode-HZ
1670     "h" gnus-article-wash-html
1671     "s" gnus-summary-force-verify-and-decrypt
1672     "f" gnus-article-display-x-face
1673     "l" gnus-summary-stop-page-breaking
1674     "r" gnus-summary-caesar-message
1675     "t" gnus-summary-toggle-header
1676     "v" gnus-summary-verbose-headers
1677     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1678     "p" gnus-article-verify-x-pgp-sig
1679     "d" gnus-article-treat-dumbquotes)
1680
1681   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1682     "a" gnus-article-hide
1683     "h" gnus-article-hide-headers
1684     "b" gnus-article-hide-boring-headers
1685     "s" gnus-article-hide-signature
1686     "c" gnus-article-hide-citation
1687     "C" gnus-article-hide-citation-in-followups
1688     "l" gnus-article-hide-list-identifiers
1689     "p" gnus-article-hide-pgp
1690     "B" gnus-article-strip-banner
1691     "P" gnus-article-hide-pem
1692     "\C-c" gnus-article-hide-citation-maybe)
1693
1694   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1695     "a" gnus-article-highlight
1696     "h" gnus-article-highlight-headers
1697     "c" gnus-article-highlight-citation
1698     "s" gnus-article-highlight-signature)
1699
1700   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1701     "w" gnus-article-decode-mime-words
1702     "c" gnus-article-decode-charset
1703     "v" gnus-mime-view-all-parts
1704     "b" gnus-article-view-part)
1705
1706   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1707     "z" gnus-article-date-ut
1708     "u" gnus-article-date-ut
1709     "l" gnus-article-date-local
1710     "p" gnus-article-date-english
1711     "e" gnus-article-date-lapsed
1712     "o" gnus-article-date-original
1713     "i" gnus-article-date-iso8601
1714     "s" gnus-article-date-user)
1715
1716   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1717     "t" gnus-article-remove-trailing-blank-lines
1718     "l" gnus-article-strip-leading-blank-lines
1719     "m" gnus-article-strip-multiple-blank-lines
1720     "a" gnus-article-strip-blank-lines
1721     "A" gnus-article-strip-all-blank-lines
1722     "s" gnus-article-strip-leading-space
1723     "e" gnus-article-strip-trailing-space
1724     "w" gnus-article-remove-leading-whitespace)
1725
1726   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1727     "v" gnus-version
1728     "f" gnus-summary-fetch-faq
1729     "d" gnus-summary-describe-group
1730     "h" gnus-summary-describe-briefly
1731     "i" gnus-info-find-node)
1732
1733   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1734     "e" gnus-summary-expire-articles
1735     "\M-\C-e" gnus-summary-expire-articles-now
1736     "\177" gnus-summary-delete-article
1737     [delete] gnus-summary-delete-article
1738     [backspace] gnus-summary-delete-article
1739     "m" gnus-summary-move-article
1740     "r" gnus-summary-respool-article
1741     "w" gnus-summary-edit-article
1742     "c" gnus-summary-copy-article
1743     "B" gnus-summary-crosspost-article
1744     "q" gnus-summary-respool-query
1745     "t" gnus-summary-respool-trace
1746     "i" gnus-summary-import-article
1747     "I" gnus-summary-create-article
1748     "p" gnus-summary-article-posted-p)
1749
1750   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1751     "o" gnus-summary-save-article
1752     "m" gnus-summary-save-article-mail
1753     "F" gnus-summary-write-article-file
1754     "r" gnus-summary-save-article-rmail
1755     "f" gnus-summary-save-article-file
1756     "b" gnus-summary-save-article-body-file
1757     "h" gnus-summary-save-article-folder
1758     "v" gnus-summary-save-article-vm
1759     "p" gnus-summary-pipe-output
1760     "s" gnus-soup-add-article)
1761
1762   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1763     "b" gnus-summary-display-buttonized
1764     "m" gnus-summary-repair-multipart
1765     "v" gnus-article-view-part
1766     "o" gnus-article-save-part
1767     "c" gnus-article-copy-part
1768     "C" gnus-article-view-part-as-charset
1769     "e" gnus-article-externalize-part
1770     "E" gnus-article-encrypt-body
1771     "i" gnus-article-inline-part
1772     "|" gnus-article-pipe-part))
1773
1774 (defvar gnus-article-post-menu nil)
1775
1776 (defun gnus-summary-make-menu-bar ()
1777   (gnus-turn-off-edit-menu 'summary)
1778
1779   (unless (boundp 'gnus-summary-misc-menu)
1780
1781     (easy-menu-define
1782      gnus-summary-kill-menu gnus-summary-mode-map ""
1783      (cons
1784       "Score"
1785       (nconc
1786        (list
1787         ["Customize" gnus-score-customize t])
1788        (gnus-make-score-map 'increase)
1789        (gnus-make-score-map 'lower)
1790        '(("Mark"
1791           ["Kill below" gnus-summary-kill-below t]
1792           ["Mark above" gnus-summary-mark-above t]
1793           ["Tick above" gnus-summary-tick-above t]
1794           ["Clear above" gnus-summary-clear-above t])
1795          ["Current score" gnus-summary-current-score t]
1796          ["Set score" gnus-summary-set-score t]
1797          ["Switch current score file..." gnus-score-change-score-file t]
1798          ["Set mark below..." gnus-score-set-mark-below t]
1799          ["Set expunge below..." gnus-score-set-expunge-below t]
1800          ["Edit current score file" gnus-score-edit-current-scores t]
1801          ["Edit score file" gnus-score-edit-file t]
1802          ["Trace score" gnus-score-find-trace t]
1803          ["Find words" gnus-score-find-favourite-words t]
1804          ["Rescore buffer" gnus-summary-rescore t]
1805          ["Increase score..." gnus-summary-increase-score t]
1806          ["Lower score..." gnus-summary-lower-score t]))))
1807
1808     ;; Define both the Article menu in the summary buffer and the equivalent
1809     ;; Commands menu in the article buffer here for consistency.
1810     (let ((innards
1811            `(("Hide"
1812               ["All" gnus-article-hide t]
1813               ["Headers" gnus-article-hide-headers t]
1814               ["Signature" gnus-article-hide-signature t]
1815               ["Citation" gnus-article-hide-citation t]
1816               ["List identifiers" gnus-article-hide-list-identifiers t]
1817               ["PGP" gnus-article-hide-pgp t]
1818               ["Banner" gnus-article-strip-banner t]
1819               ["Boring headers" gnus-article-hide-boring-headers t])
1820              ("Highlight"
1821               ["All" gnus-article-highlight t]
1822               ["Headers" gnus-article-highlight-headers t]
1823               ["Signature" gnus-article-highlight-signature t]
1824               ["Citation" gnus-article-highlight-citation t])
1825              ("MIME"
1826               ["Words" gnus-article-decode-mime-words t]
1827               ["Charset" gnus-article-decode-charset t]
1828               ["QP" gnus-article-de-quoted-unreadable t]
1829               ["Base64" gnus-article-de-base64-unreadable t]
1830               ["View all" gnus-mime-view-all-parts t]
1831               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1832               ["Encrypt body" gnus-article-encrypt-body t])
1833              ("Date"
1834               ["Local" gnus-article-date-local t]
1835               ["ISO8601" gnus-article-date-iso8601 t]
1836               ["UT" gnus-article-date-ut t]
1837               ["Original" gnus-article-date-original t]
1838               ["Lapsed" gnus-article-date-lapsed t]
1839               ["User-defined" gnus-article-date-user t])
1840              ("Washing"
1841               ("Remove Blanks"
1842                ["Leading" gnus-article-strip-leading-blank-lines t]
1843                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1844                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1845                ["All of the above" gnus-article-strip-blank-lines t]
1846                ["All" gnus-article-strip-all-blank-lines t]
1847                ["Leading space" gnus-article-strip-leading-space t]
1848                ["Trailing space" gnus-article-strip-trailing-space t]
1849                ["Leading space in headers" 
1850                 gnus-article-remove-leading-whitespace t])
1851               ["Overstrike" gnus-article-treat-overstrike t]
1852               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1853               ["Emphasis" gnus-article-emphasize t]
1854               ["Word wrap" gnus-article-fill-cited-article t]
1855               ["Fill long lines" gnus-article-fill-long-lines t]
1856               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1857               ["CR" gnus-article-remove-cr t]
1858               ["Show X-Face" gnus-article-display-x-face t]
1859               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1860               ["Base64" gnus-article-de-base64-unreadable t]
1861               ["Rot 13" gnus-summary-caesar-message
1862                ,@(if (featurep 'xemacs) '(t)
1863                    '(:help "\"Caesar rotate\" article by 13"))]
1864               ["Unix pipe" gnus-summary-pipe-message t]
1865               ["Add buttons" gnus-article-add-buttons t]
1866               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1867               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1868               ["Verbose header" gnus-summary-verbose-headers t]
1869               ["Toggle header" gnus-summary-toggle-header t]
1870               ["Html" gnus-article-wash-html t]
1871               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1872               ["HZ" gnus-article-decode-HZ t])
1873              ("Output"
1874               ["Save in default format" gnus-summary-save-article
1875                ,@(if (featurep 'xemacs) '(t)
1876                    '(:help "Save article using default method"))]
1877               ["Save in file" gnus-summary-save-article-file
1878                ,@(if (featurep 'xemacs) '(t)
1879                    '(:help "Save article in file"))]
1880               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1881               ["Save in MH folder" gnus-summary-save-article-folder t]
1882               ["Save in VM folder" gnus-summary-save-article-vm t]
1883               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1884               ["Save body in file" gnus-summary-save-article-body-file t]
1885               ["Pipe through a filter" gnus-summary-pipe-output t]
1886               ["Add to SOUP packet" gnus-soup-add-article t]
1887               ["Print" gnus-summary-print-article t])
1888              ("Backend"
1889               ["Respool article..." gnus-summary-respool-article t]
1890               ["Move article..." gnus-summary-move-article
1891                (gnus-check-backend-function
1892                 'request-move-article gnus-newsgroup-name)]
1893               ["Copy article..." gnus-summary-copy-article t]
1894               ["Crosspost article..." gnus-summary-crosspost-article
1895                (gnus-check-backend-function
1896                 'request-replace-article gnus-newsgroup-name)]
1897               ["Import file..." gnus-summary-import-article t]
1898               ["Create article..." gnus-summary-create-article t]
1899               ["Check if posted" gnus-summary-article-posted-p t]
1900               ["Edit article" gnus-summary-edit-article
1901                (not (gnus-group-read-only-p))]
1902               ["Delete article" gnus-summary-delete-article
1903                (gnus-check-backend-function
1904                 'request-expire-articles gnus-newsgroup-name)]
1905               ["Query respool" gnus-summary-respool-query t]
1906               ["Trace respool" gnus-summary-respool-trace t]
1907               ["Delete expirable articles" gnus-summary-expire-articles-now
1908                (gnus-check-backend-function
1909                 'request-expire-articles gnus-newsgroup-name)])
1910              ("Extract"
1911               ["Uudecode" gnus-uu-decode-uu
1912                ,@(if (featurep 'xemacs) '(t)
1913                    '(:help "Decode uuencoded article(s)"))]
1914               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1915               ["Unshar" gnus-uu-decode-unshar t]
1916               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1917               ["Save" gnus-uu-decode-save t]
1918               ["Binhex" gnus-uu-decode-binhex t]
1919               ["Postscript" gnus-uu-decode-postscript t])
1920              ("Cache"
1921               ["Enter article" gnus-cache-enter-article t]
1922               ["Remove article" gnus-cache-remove-article t])
1923              ["Translate" gnus-article-babel t]
1924              ["Select article buffer" gnus-summary-select-article-buffer t]
1925              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1926              ["Isearch article..." gnus-summary-isearch-article t]
1927              ["Beginning of the article" gnus-summary-beginning-of-article t]
1928              ["End of the article" gnus-summary-end-of-article t]
1929              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1930              ["Fetch referenced articles" gnus-summary-refer-references t]
1931              ["Fetch current thread" gnus-summary-refer-thread t]
1932              ["Fetch article with id..." gnus-summary-refer-article t]
1933              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1934              ["Redisplay" gnus-summary-show-article t]
1935              ["Raw article" gnus-summary-show-raw-article t])))
1936       (easy-menu-define
1937        gnus-summary-article-menu gnus-summary-mode-map ""
1938        (cons "Article" innards))
1939
1940       (if (not (keymapp gnus-summary-article-menu))
1941           (easy-menu-define
1942             gnus-article-commands-menu gnus-article-mode-map ""
1943             (cons "Commands" innards))
1944         ;; in Emacs, don't share menu.
1945         (setq gnus-article-commands-menu 
1946               (copy-keymap gnus-summary-article-menu))
1947         (define-key gnus-article-mode-map [menu-bar commands]
1948           (cons "Commands" gnus-article-commands-menu))))
1949
1950     (easy-menu-define
1951      gnus-summary-thread-menu gnus-summary-mode-map ""
1952      '("Threads"
1953        ["Toggle threading" gnus-summary-toggle-threads t]
1954        ["Hide threads" gnus-summary-hide-all-threads t]
1955        ["Show threads" gnus-summary-show-all-threads t]
1956        ["Hide thread" gnus-summary-hide-thread t]
1957        ["Show thread" gnus-summary-show-thread t]
1958        ["Go to next thread" gnus-summary-next-thread t]
1959        ["Go to previous thread" gnus-summary-prev-thread t]
1960        ["Go down thread" gnus-summary-down-thread t]
1961        ["Go up thread" gnus-summary-up-thread t]
1962        ["Top of thread" gnus-summary-top-thread t]
1963        ["Mark thread as read" gnus-summary-kill-thread t]
1964        ["Lower thread score" gnus-summary-lower-thread t]
1965        ["Raise thread score" gnus-summary-raise-thread t]
1966        ["Rethread current" gnus-summary-rethread-current t]))
1967
1968     (easy-menu-define
1969      gnus-summary-post-menu gnus-summary-mode-map ""
1970      `("Post"
1971        ["Post an article" gnus-summary-post-news
1972         ,@(if (featurep 'xemacs) '(t)
1973             '(:help "Post an article"))]
1974        ["Followup" gnus-summary-followup
1975         ,@(if (featurep 'xemacs) '(t)
1976             '(:help "Post followup to this article"))]
1977        ["Followup and yank" gnus-summary-followup-with-original
1978         ,@(if (featurep 'xemacs) '(t)
1979             '(:help "Post followup to this article, quoting its contents"))]
1980        ["Supersede article" gnus-summary-supersede-article t]
1981        ["Cancel article" gnus-summary-cancel-article
1982         ,@(if (featurep 'xemacs) '(t)
1983             '(:help "Cancel an article you posted"))]
1984        ["Reply" gnus-summary-reply t]
1985        ["Reply and yank" gnus-summary-reply-with-original t]
1986        ["Wide reply" gnus-summary-wide-reply t]
1987        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1988         ,@(if (featurep 'xemacs) '(t)
1989             '(:help "Mail a reply, quoting this article"))]
1990        ["Mail forward" gnus-summary-mail-forward t]
1991        ["Post forward" gnus-summary-post-forward t]
1992        ["Digest and mail" gnus-uu-digest-mail-forward t]
1993        ["Digest and post" gnus-uu-digest-post-forward t]
1994        ["Resend message" gnus-summary-resend-message t]
1995        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1996        ["Send a mail" gnus-summary-mail-other-window t]
1997        ["Uuencode and post" gnus-uu-post-news
1998         ,@(if (featurep 'xemacs) '(t)
1999             '(:help "Post a uuencoded article"))]
2000        ["Followup via news" gnus-summary-followup-to-mail t]
2001        ["Followup via news and yank"
2002         gnus-summary-followup-to-mail-with-original t]
2003        ;;("Draft"
2004        ;;["Send" gnus-summary-send-draft t]
2005        ;;["Send bounced" gnus-resend-bounced-mail t])
2006        ))
2007
2008     (cond 
2009      ((not (keymapp gnus-summary-post-menu))
2010       (setq gnus-article-post-menu gnus-summary-post-menu))
2011      ((not gnus-article-post-menu)
2012       ;; Don't share post menu.
2013       (setq gnus-article-post-menu
2014             (copy-keymap gnus-summary-post-menu))))
2015     (define-key gnus-article-mode-map [menu-bar post]
2016       (cons "Post" gnus-article-post-menu))
2017
2018     (easy-menu-define
2019      gnus-summary-misc-menu gnus-summary-mode-map ""
2020      `("Misc"
2021        ("Mark Read"
2022         ["Mark as read" gnus-summary-mark-as-read-forward t]
2023         ["Mark same subject and select"
2024          gnus-summary-kill-same-subject-and-select t]
2025         ["Mark same subject" gnus-summary-kill-same-subject t]
2026         ["Catchup" gnus-summary-catchup
2027          ,@(if (featurep 'xemacs) '(t)
2028              '(:help "Mark unread articles in this group as read"))]
2029         ["Catchup all" gnus-summary-catchup-all t]
2030         ["Catchup to here" gnus-summary-catchup-to-here t]
2031         ["Catchup from here" gnus-summary-catchup-from-here t]
2032         ["Catchup region" gnus-summary-mark-region-as-read t]
2033         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2034        ("Mark Various"
2035         ["Tick" gnus-summary-tick-article-forward t]
2036         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2037         ["Remove marks" gnus-summary-clear-mark-forward t]
2038         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2039         ["Set bookmark" gnus-summary-set-bookmark t]
2040         ["Remove bookmark" gnus-summary-remove-bookmark t])
2041        ("Mark Limit"
2042         ["Marks..." gnus-summary-limit-to-marks t]
2043         ["Subject..." gnus-summary-limit-to-subject t]
2044         ["Author..." gnus-summary-limit-to-author t]
2045         ["Age..." gnus-summary-limit-to-age t]
2046         ["Extra..." gnus-summary-limit-to-extra t]
2047         ["Score" gnus-summary-limit-to-score t]
2048         ["Unread" gnus-summary-limit-to-unread t]
2049         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2050         ["Articles" gnus-summary-limit-to-articles t]
2051         ["Pop limit" gnus-summary-pop-limit t]
2052         ["Show dormant" gnus-summary-limit-include-dormant t]
2053         ["Hide childless dormant"
2054          gnus-summary-limit-exclude-childless-dormant t]
2055         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2056         ["Hide marked" gnus-summary-limit-exclude-marks t]
2057         ["Show expunged" gnus-summary-limit-include-expunged t])
2058        ("Process Mark"
2059         ["Set mark" gnus-summary-mark-as-processable t]
2060         ["Remove mark" gnus-summary-unmark-as-processable t]
2061         ["Remove all marks" gnus-summary-unmark-all-processable t]
2062         ["Mark above" gnus-uu-mark-over t]
2063         ["Mark series" gnus-uu-mark-series t]
2064         ["Mark region" gnus-uu-mark-region t]
2065         ["Unmark region" gnus-uu-unmark-region t]
2066         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2067         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2068         ["Mark all" gnus-uu-mark-all t]
2069         ["Mark buffer" gnus-uu-mark-buffer t]
2070         ["Mark sparse" gnus-uu-mark-sparse t]
2071         ["Mark thread" gnus-uu-mark-thread t]
2072         ["Unmark thread" gnus-uu-unmark-thread t]
2073         ("Process Mark Sets"
2074          ["Kill" gnus-summary-kill-process-mark t]
2075          ["Yank" gnus-summary-yank-process-mark
2076           gnus-newsgroup-process-stack]
2077          ["Save" gnus-summary-save-process-mark t]))
2078        ("Scroll article"
2079         ["Page forward" gnus-summary-next-page
2080          ,@(if (featurep 'xemacs) '(t)
2081              '(:help "Show next page of article"))]
2082         ["Page backward" gnus-summary-prev-page
2083          ,@(if (featurep 'xemacs) '(t)
2084              '(:help "Show previous page of article"))]
2085         ["Line forward" gnus-summary-scroll-up t])
2086        ("Move"
2087         ["Next unread article" gnus-summary-next-unread-article t]
2088         ["Previous unread article" gnus-summary-prev-unread-article t]
2089         ["Next article" gnus-summary-next-article t]
2090         ["Previous article" gnus-summary-prev-article t]
2091         ["Next unread subject" gnus-summary-next-unread-subject t]
2092         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2093         ["Next article same subject" gnus-summary-next-same-subject t]
2094         ["Previous article same subject" gnus-summary-prev-same-subject t]
2095         ["First unread article" gnus-summary-first-unread-article t]
2096         ["Best unread article" gnus-summary-best-unread-article t]
2097         ["Go to subject number..." gnus-summary-goto-subject t]
2098         ["Go to article number..." gnus-summary-goto-article t]
2099         ["Go to the last article" gnus-summary-goto-last-article t]
2100         ["Pop article off history" gnus-summary-pop-article t])
2101        ("Sort"
2102         ["Sort by number" gnus-summary-sort-by-number t]
2103         ["Sort by author" gnus-summary-sort-by-author t]
2104         ["Sort by subject" gnus-summary-sort-by-subject t]
2105         ["Sort by date" gnus-summary-sort-by-date t]
2106         ["Sort by score" gnus-summary-sort-by-score t]
2107         ["Sort by lines" gnus-summary-sort-by-lines t]
2108         ["Sort by characters" gnus-summary-sort-by-chars t]
2109         ["Original sort" gnus-summary-sort-by-original t])
2110        ("Help"
2111         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2112         ["Describe group" gnus-summary-describe-group t]
2113         ["Read manual" gnus-info-find-node t])
2114        ("Modes"
2115         ["Pick and read" gnus-pick-mode t]
2116         ["Binary" gnus-binary-mode t])
2117        ("Regeneration"
2118         ["Regenerate" gnus-summary-prepare t]
2119         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2120         ["Toggle threading" gnus-summary-toggle-threads t])
2121        ["See old articles" gnus-summary-insert-old-articles t]
2122        ["See new articles" gnus-summary-insert-new-articles t]
2123        ["Filter articles..." gnus-summary-execute-command t]
2124        ["Run command on subjects..." gnus-summary-universal-argument t]
2125        ["Search articles forward..." gnus-summary-search-article-forward t]
2126        ["Search articles backward..." gnus-summary-search-article-backward t]
2127        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2128        ["Expand window" gnus-summary-expand-window t]
2129        ["Expire expirable articles" gnus-summary-expire-articles
2130         (gnus-check-backend-function
2131          'request-expire-articles gnus-newsgroup-name)]
2132        ["Edit local kill file" gnus-summary-edit-local-kill t]
2133        ["Edit main kill file" gnus-summary-edit-global-kill t]
2134        ["Edit group parameters" gnus-summary-edit-parameters t]
2135        ["Customize group parameters" gnus-summary-customize-parameters t]
2136        ["Send a bug report" gnus-bug t]
2137        ("Exit"
2138         ["Catchup and exit" gnus-summary-catchup-and-exit
2139          ,@(if (featurep 'xemacs) '(t)
2140              '(:help "Mark unread articles in this group as read, then exit"))]
2141         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2142         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2143         ["Exit group" gnus-summary-exit
2144          ,@(if (featurep 'xemacs) '(t)
2145              '(:help "Exit current group, return to group selection mode"))]
2146         ["Exit group without updating" gnus-summary-exit-no-update t]
2147         ["Exit and goto next group" gnus-summary-next-group t]
2148         ["Exit and goto prev group" gnus-summary-prev-group t]
2149         ["Reselect group" gnus-summary-reselect-current-group t]
2150         ["Rescan group" gnus-summary-rescan-group t]
2151         ["Update dribble" gnus-summary-save-newsrc t])))
2152
2153     (gnus-run-hooks 'gnus-summary-menu-hook)))
2154
2155 (defvar gnus-summary-tool-bar-map nil)
2156
2157 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2158 (defun gnus-summary-make-tool-bar ()
2159   (if (and (fboundp 'tool-bar-add-item-from-menu)
2160            (default-value 'tool-bar-mode)
2161            (not gnus-summary-tool-bar-map))
2162       (setq gnus-summary-tool-bar-map
2163             (let ((tool-bar-map (make-sparse-keymap))
2164                   (load-path (mm-image-load-path)))
2165               (tool-bar-add-item-from-menu
2166                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2167               (tool-bar-add-item-from-menu
2168                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2169               (tool-bar-add-item-from-menu
2170                'gnus-summary-post-news "post" gnus-summary-mode-map)
2171               (tool-bar-add-item-from-menu
2172                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2173               (tool-bar-add-item-from-menu
2174                'gnus-summary-followup "followup" gnus-summary-mode-map)
2175               (tool-bar-add-item-from-menu
2176                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2177               (tool-bar-add-item-from-menu
2178                'gnus-summary-reply "reply" gnus-summary-mode-map)
2179               (tool-bar-add-item-from-menu
2180                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2181               (tool-bar-add-item-from-menu
2182                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2183               (tool-bar-add-item-from-menu
2184                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2185               (tool-bar-add-item-from-menu
2186                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2187               (tool-bar-add-item-from-menu
2188                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2189               (tool-bar-add-item-from-menu
2190                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2191               (tool-bar-add-item-from-menu
2192                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2193               (tool-bar-add-item-from-menu
2194                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2195               tool-bar-map)))
2196   (if gnus-summary-tool-bar-map
2197       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2198
2199 (defun gnus-score-set-default (var value)
2200   "A version of set that updates the GNU Emacs menu-bar."
2201   (set var value)
2202   ;; It is the message that forces the active status to be updated.
2203   (message ""))
2204
2205 (defun gnus-make-score-map (type)
2206   "Make a summary score map of type TYPE."
2207   (if t
2208       nil
2209     (let ((headers '(("author" "from" string)
2210                      ("subject" "subject" string)
2211                      ("article body" "body" string)
2212                      ("article head" "head" string)
2213                      ("xref" "xref" string)
2214                      ("extra header" "extra" string)
2215                      ("lines" "lines" number)
2216                      ("followups to author" "followup" string)))
2217           (types '((number ("less than" <)
2218                            ("greater than" >)
2219                            ("equal" =))
2220                    (string ("substring" s)
2221                            ("exact string" e)
2222                            ("fuzzy string" f)
2223                            ("regexp" r))))
2224           (perms '(("temporary" (current-time-string))
2225                    ("permanent" nil)
2226                    ("immediate" now)))
2227           header)
2228       (list
2229        (apply
2230         'nconc
2231         (list
2232          (if (eq type 'lower)
2233              "Lower score"
2234            "Increase score"))
2235         (let (outh)
2236           (while headers
2237             (setq header (car headers))
2238             (setq outh
2239                   (cons
2240                    (apply
2241                     'nconc
2242                     (list (car header))
2243                     (let ((ts (cdr (assoc (nth 2 header) types)))
2244                           outt)
2245                       (while ts
2246                         (setq outt
2247                               (cons
2248                                (apply
2249                                 'nconc
2250                                 (list (caar ts))
2251                                 (let ((ps perms)
2252                                       outp)
2253                                   (while ps
2254                                     (setq outp
2255                                           (cons
2256                                            (vector
2257                                             (caar ps)
2258                                             (list
2259                                              'gnus-summary-score-entry
2260                                              (nth 1 header)
2261                                              (if (or (string= (nth 1 header)
2262                                                               "head")
2263                                                      (string= (nth 1 header)
2264                                                               "body"))
2265                                                  ""
2266                                                (list 'gnus-summary-header
2267                                                      (nth 1 header)))
2268                                              (list 'quote (nth 1 (car ts)))
2269                                              (list 'gnus-score-delta-default
2270                                                    nil)
2271                                              (nth 1 (car ps))
2272                                              t)
2273                                             t)
2274                                            outp))
2275                                     (setq ps (cdr ps)))
2276                                   (list (nreverse outp))))
2277                                outt))
2278                         (setq ts (cdr ts)))
2279                       (list (nreverse outt))))
2280                    outh))
2281             (setq headers (cdr headers)))
2282           (list (nreverse outh))))))))
2283
2284 \f
2285
2286 (defun gnus-summary-mode (&optional group)
2287   "Major mode for reading articles.
2288
2289 All normal editing commands are switched off.
2290 \\<gnus-summary-mode-map>
2291 Each line in this buffer represents one article.  To read an
2292 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2293 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2294 respectively.
2295
2296 You can also post articles and send mail from this buffer.  To
2297 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2298 of an article, type `\\[gnus-summary-reply]'.
2299
2300 There are approx. one gazillion commands you can execute in this
2301 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2302
2303 The following commands are available:
2304
2305 \\{gnus-summary-mode-map}"
2306   (interactive)
2307   (kill-all-local-variables)
2308   (when (gnus-visual-p 'summary-menu 'menu)
2309     (gnus-summary-make-menu-bar)
2310     (gnus-summary-make-tool-bar))
2311   (gnus-summary-make-local-variables)
2312   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2313     (gnus-summary-make-local-variables))
2314   (gnus-make-thread-indent-array)
2315   (gnus-simplify-mode-line)
2316   (setq major-mode 'gnus-summary-mode)
2317   (setq mode-name "Summary")
2318   (make-local-variable 'minor-mode-alist)
2319   (use-local-map gnus-summary-mode-map)
2320   (buffer-disable-undo)
2321   (setq buffer-read-only t)             ;Disable modification
2322   (setq truncate-lines t)
2323   (setq selective-display t)
2324   (setq selective-display-ellipses t)   ;Display `...'
2325   (gnus-summary-set-display-table)
2326   (gnus-set-default-directory)
2327   (setq gnus-newsgroup-name group)
2328   (make-local-variable 'gnus-summary-line-format)
2329   (make-local-variable 'gnus-summary-line-format-spec)
2330   (make-local-variable 'gnus-summary-dummy-line-format)
2331   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2332   (make-local-variable 'gnus-summary-mark-positions)
2333   (make-local-hook 'pre-command-hook)
2334   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2335   (gnus-run-hooks 'gnus-summary-mode-hook)
2336   (turn-on-gnus-mailing-list-mode)
2337   (mm-enable-multibyte-mule4)
2338   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2339   (gnus-update-summary-mark-positions))
2340
2341 (defun gnus-summary-make-local-variables ()
2342   "Make all the local summary buffer variables."
2343   (let (global)
2344     (dolist (local gnus-summary-local-variables)
2345       (if (consp local)
2346           (progn
2347             (if (eq (cdr local) 'global)
2348                 ;; Copy the global value of the variable.
2349                 (setq global (symbol-value (car local)))
2350               ;; Use the value from the list.
2351               (setq global (eval (cdr local))))
2352             (set (make-local-variable (car local)) global))
2353         ;; Simple nil-valued local variable.
2354         (set (make-local-variable local) nil)))))
2355
2356 (defun gnus-summary-clear-local-variables ()
2357   (let ((locals gnus-summary-local-variables))
2358     (while locals
2359       (if (consp (car locals))
2360           (and (vectorp (caar locals))
2361                (set (caar locals) nil))
2362         (and (vectorp (car locals))
2363              (set (car locals) nil)))
2364       (setq locals (cdr locals)))))
2365
2366 ;; Summary data functions.
2367
2368 (defmacro gnus-data-number (data)
2369   `(car ,data))
2370
2371 (defmacro gnus-data-set-number (data number)
2372   `(setcar ,data ,number))
2373
2374 (defmacro gnus-data-mark (data)
2375   `(nth 1 ,data))
2376
2377 (defmacro gnus-data-set-mark (data mark)
2378   `(setcar (nthcdr 1 ,data) ,mark))
2379
2380 (defmacro gnus-data-pos (data)
2381   `(nth 2 ,data))
2382
2383 (defmacro gnus-data-set-pos (data pos)
2384   `(setcar (nthcdr 2 ,data) ,pos))
2385
2386 (defmacro gnus-data-header (data)
2387   `(nth 3 ,data))
2388
2389 (defmacro gnus-data-set-header (data header)
2390   `(setf (nth 3 ,data) ,header))
2391
2392 (defmacro gnus-data-level (data)
2393   `(nth 4 ,data))
2394
2395 (defmacro gnus-data-unread-p (data)
2396   `(= (nth 1 ,data) gnus-unread-mark))
2397
2398 (defmacro gnus-data-read-p (data)
2399   `(/= (nth 1 ,data) gnus-unread-mark))
2400
2401 (defmacro gnus-data-pseudo-p (data)
2402   `(consp (nth 3 ,data)))
2403
2404 (defmacro gnus-data-find (number)
2405   `(assq ,number gnus-newsgroup-data))
2406
2407 (defmacro gnus-data-find-list (number &optional data)
2408   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2409      (memq (assq ,number bdata)
2410            bdata)))
2411
2412 (defmacro gnus-data-make (number mark pos header level)
2413   `(list ,number ,mark ,pos ,header ,level))
2414
2415 (defun gnus-data-enter (after-article number mark pos header level offset)
2416   (let ((data (gnus-data-find-list after-article)))
2417     (unless data
2418       (error "No such article: %d" after-article))
2419     (setcdr data (cons (gnus-data-make number mark pos header level)
2420                        (cdr data)))
2421     (setq gnus-newsgroup-data-reverse nil)
2422     (gnus-data-update-list (cddr data) offset)))
2423
2424 (defun gnus-data-enter-list (after-article list &optional offset)
2425   (when list
2426     (let ((data (and after-article (gnus-data-find-list after-article)))
2427           (ilist list))
2428       (if (not (or data
2429                    after-article))
2430           (let ((odata gnus-newsgroup-data))
2431             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2432             (when offset
2433               (gnus-data-update-list odata offset)))
2434         ;; Find the last element in the list to be spliced into the main
2435         ;; list.
2436         (while (cdr list)
2437           (setq list (cdr list)))
2438         (if (not data)
2439             (progn
2440               (setcdr list gnus-newsgroup-data)
2441               (setq gnus-newsgroup-data ilist)
2442               (when offset
2443                 (gnus-data-update-list (cdr list) offset)))
2444           (setcdr list (cdr data))
2445           (setcdr data ilist)
2446           (when offset
2447             (gnus-data-update-list (cdr list) offset))))
2448       (setq gnus-newsgroup-data-reverse nil))))
2449
2450 (defun gnus-data-remove (article &optional offset)
2451   (let ((data gnus-newsgroup-data))
2452     (if (= (gnus-data-number (car data)) article)
2453         (progn
2454           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2455                 gnus-newsgroup-data-reverse nil)
2456           (when offset
2457             (gnus-data-update-list gnus-newsgroup-data offset)))
2458       (while (cdr data)
2459         (when (= (gnus-data-number (cadr data)) article)
2460           (setcdr data (cddr data))
2461           (when offset
2462             (gnus-data-update-list (cdr data) offset))
2463           (setq data nil
2464                 gnus-newsgroup-data-reverse nil))
2465         (setq data (cdr data))))))
2466
2467 (defmacro gnus-data-list (backward)
2468   `(if ,backward
2469        (or gnus-newsgroup-data-reverse
2470            (setq gnus-newsgroup-data-reverse
2471                  (reverse gnus-newsgroup-data)))
2472      gnus-newsgroup-data))
2473
2474 (defun gnus-data-update-list (data offset)
2475   "Add OFFSET to the POS of all data entries in DATA."
2476   (setq gnus-newsgroup-data-reverse nil)
2477   (while data
2478     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2479     (setq data (cdr data))))
2480
2481 (defun gnus-summary-article-pseudo-p (article)
2482   "Say whether this article is a pseudo article or not."
2483   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2484
2485 (defmacro gnus-summary-article-sparse-p (article)
2486   "Say whether this article is a sparse article or not."
2487   `(memq ,article gnus-newsgroup-sparse))
2488
2489 (defmacro gnus-summary-article-ancient-p (article)
2490   "Say whether this article is a sparse article or not."
2491   `(memq ,article gnus-newsgroup-ancient))
2492
2493 (defun gnus-article-parent-p (number)
2494   "Say whether this article is a parent or not."
2495   (let ((data (gnus-data-find-list number)))
2496     (and (cdr data)                     ; There has to be an article after...
2497          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2498             (gnus-data-level (nth 1 data))))))
2499
2500 (defun gnus-article-children (number)
2501   "Return a list of all children to NUMBER."
2502   (let* ((data (gnus-data-find-list number))
2503          (level (gnus-data-level (car data)))
2504          children)
2505     (setq data (cdr data))
2506     (while (and data
2507                 (= (gnus-data-level (car data)) (1+ level)))
2508       (push (gnus-data-number (car data)) children)
2509       (setq data (cdr data)))
2510     children))
2511
2512 (defmacro gnus-summary-skip-intangible ()
2513   "If the current article is intangible, then jump to a different article."
2514   '(let ((to (get-text-property (point) 'gnus-intangible)))
2515      (and to (gnus-summary-goto-subject to))))
2516
2517 (defmacro gnus-summary-article-intangible-p ()
2518   "Say whether this article is intangible or not."
2519   '(get-text-property (point) 'gnus-intangible))
2520
2521 (defun gnus-article-read-p (article)
2522   "Say whether ARTICLE is read or not."
2523   (not (or (memq article gnus-newsgroup-marked)
2524            (memq article gnus-newsgroup-unreads)
2525            (memq article gnus-newsgroup-unselected)
2526            (memq article gnus-newsgroup-dormant))))
2527
2528 ;; Some summary mode macros.
2529
2530 (defmacro gnus-summary-article-number ()
2531   "The article number of the article on the current line.
2532 If there isn's an article number here, then we return the current
2533 article number."
2534   '(progn
2535      (gnus-summary-skip-intangible)
2536      (or (get-text-property (point) 'gnus-number)
2537          (gnus-summary-last-subject))))
2538
2539 (defmacro gnus-summary-article-header (&optional number)
2540   "Return the header of article NUMBER."
2541   `(gnus-data-header (gnus-data-find
2542                       ,(or number '(gnus-summary-article-number)))))
2543
2544 (defmacro gnus-summary-thread-level (&optional number)
2545   "Return the level of thread that starts with article NUMBER."
2546   `(if (and (eq gnus-summary-make-false-root 'dummy)
2547             (get-text-property (point) 'gnus-intangible))
2548        0
2549      (gnus-data-level (gnus-data-find
2550                        ,(or number '(gnus-summary-article-number))))))
2551
2552 (defmacro gnus-summary-article-mark (&optional number)
2553   "Return the mark of article NUMBER."
2554   `(gnus-data-mark (gnus-data-find
2555                     ,(or number '(gnus-summary-article-number)))))
2556
2557 (defmacro gnus-summary-article-pos (&optional number)
2558   "Return the position of the line of article NUMBER."
2559   `(gnus-data-pos (gnus-data-find
2560                    ,(or number '(gnus-summary-article-number)))))
2561
2562 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2563 (defmacro gnus-summary-article-subject (&optional number)
2564   "Return current subject string or nil if nothing."
2565   `(let ((headers
2566           ,(if number
2567                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2568              '(gnus-data-header (assq (gnus-summary-article-number)
2569                                       gnus-newsgroup-data)))))
2570      (and headers
2571           (vectorp headers)
2572           (mail-header-subject headers))))
2573
2574 (defmacro gnus-summary-article-score (&optional number)
2575   "Return current article score."
2576   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2577                   gnus-newsgroup-scored))
2578        gnus-summary-default-score 0))
2579
2580 (defun gnus-summary-article-children (&optional number)
2581   "Return a list of article numbers that are children of article NUMBER."
2582   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2583          (level (gnus-data-level (car data)))
2584          l children)
2585     (while (and (setq data (cdr data))
2586                 (> (setq l (gnus-data-level (car data))) level))
2587       (and (= (1+ level) l)
2588            (push (gnus-data-number (car data))
2589                  children)))
2590     (nreverse children)))
2591
2592 (defun gnus-summary-article-parent (&optional number)
2593   "Return the article number of the parent of article NUMBER."
2594   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2595                                     (gnus-data-list t)))
2596          (level (gnus-data-level (car data))))
2597     (if (zerop level)
2598         ()                              ; This is a root.
2599       ;; We search until we find an article with a level less than
2600       ;; this one.  That function has to be the parent.
2601       (while (and (setq data (cdr data))
2602                   (not (< (gnus-data-level (car data)) level))))
2603       (and data (gnus-data-number (car data))))))
2604
2605 (defun gnus-unread-mark-p (mark)
2606   "Say whether MARK is the unread mark."
2607   (= mark gnus-unread-mark))
2608
2609 (defun gnus-read-mark-p (mark)
2610   "Say whether MARK is one of the marks that mark as read.
2611 This is all marks except unread, ticked, dormant, and expirable."
2612   (not (or (= mark gnus-unread-mark)
2613            (= mark gnus-ticked-mark)
2614            (= mark gnus-dormant-mark)
2615            (= mark gnus-expirable-mark))))
2616
2617 (defmacro gnus-article-mark (number)
2618   "Return the MARK of article NUMBER.
2619 This macro should only be used when computing the mark the \"first\"
2620 time; i.e., when generating the summary lines.  After that,
2621 `gnus-summary-article-mark' should be used to examine the
2622 marks of articles."
2623   `(cond
2624     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2625     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2626     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2627     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2628     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2629     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2630     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2631     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2632            gnus-ancient-mark))))
2633
2634 ;; Saving hidden threads.
2635
2636 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2637 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2638
2639 (defmacro gnus-save-hidden-threads (&rest forms)
2640   "Save hidden threads, eval FORMS, and restore the hidden threads."
2641   (let ((config (make-symbol "config")))
2642     `(let ((,config (gnus-hidden-threads-configuration)))
2643        (unwind-protect
2644            (save-excursion
2645              ,@forms)
2646          (gnus-restore-hidden-threads-configuration ,config)))))
2647
2648 (defun gnus-data-compute-positions ()
2649   "Compute the positions of all articles."
2650   (setq gnus-newsgroup-data-reverse nil)
2651   (let ((data gnus-newsgroup-data))
2652     (save-excursion
2653       (gnus-save-hidden-threads
2654         (gnus-summary-show-all-threads)
2655         (goto-char (point-min))
2656         (while data
2657           (while (get-text-property (point) 'gnus-intangible)
2658             (forward-line 1))
2659           (gnus-data-set-pos (car data) (+ (point) 3))
2660           (setq data (cdr data))
2661           (forward-line 1))))))
2662
2663 (defun gnus-hidden-threads-configuration ()
2664   "Return the current hidden threads configuration."
2665   (save-excursion
2666     (let (config)
2667       (goto-char (point-min))
2668       (while (search-forward "\r" nil t)
2669         (push (1- (point)) config))
2670       config)))
2671
2672 (defun gnus-restore-hidden-threads-configuration (config)
2673   "Restore hidden threads configuration from CONFIG."
2674   (save-excursion
2675     (let (point buffer-read-only)
2676       (while (setq point (pop config))
2677         (when (and (< point (point-max))
2678                    (goto-char point)
2679                    (eq (char-after) ?\n))
2680           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2681
2682 ;; Various summary mode internalish functions.
2683
2684 (defun gnus-mouse-pick-article (e)
2685   (interactive "e")
2686   (mouse-set-point e)
2687   (gnus-summary-next-page nil t))
2688
2689 (defun gnus-summary-set-display-table ()
2690   "Change the display table.
2691 Odd characters have a tendency to mess
2692 up nicely formatted displays - we make all possible glyphs
2693 display only a single character."
2694
2695   ;; We start from the standard display table, if any.
2696   (let ((table (or (copy-sequence standard-display-table)
2697                    (make-display-table)))
2698         (i 32))
2699     ;; Nix out all the control chars...
2700     (while (>= (setq i (1- i)) 0)
2701       (aset table i [??]))
2702     ;; ... but not newline and cr, of course.  (cr is necessary for the
2703     ;; selective display).
2704     (aset table ?\n nil)
2705     (aset table ?\r nil)
2706     ;; We keep TAB as well.
2707     (aset table ?\t nil)
2708     ;; We nix out any glyphs over 126 that are not set already.
2709     (let ((i 256))
2710       (while (>= (setq i (1- i)) 127)
2711         ;; Only modify if the entry is nil.
2712         (unless (aref table i)
2713           (aset table i [??]))))
2714     (setq buffer-display-table table)))
2715
2716 (defun gnus-summary-set-article-display-arrow (pos)
2717   "Update the overlay arrow to point to line at position POS."
2718   (when (and gnus-summary-display-arrow
2719              (boundp 'overlay-arrow-position)
2720              (boundp 'overlay-arrow-string))
2721     (save-excursion
2722       (goto-char pos)
2723       (beginning-of-line)
2724       (unless overlay-arrow-position
2725         (setq overlay-arrow-position (make-marker)))
2726       (setq overlay-arrow-string "=>"
2727             overlay-arrow-position (set-marker overlay-arrow-position
2728                                                (point)
2729                                                (current-buffer))))))
2730
2731 (defun gnus-summary-buffer-name (group)
2732   "Return the summary buffer name of GROUP."
2733   (concat "*Summary " group "*"))
2734
2735 (defun gnus-summary-setup-buffer (group)
2736   "Initialize summary buffer."
2737   (let ((buffer (gnus-summary-buffer-name group)))
2738     (if (get-buffer buffer)
2739         (progn
2740           (set-buffer buffer)
2741           (setq gnus-summary-buffer (current-buffer))
2742           (not gnus-newsgroup-prepared))
2743       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2744       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2745       (gnus-summary-mode group)
2746       (when gnus-carpal
2747         (gnus-carpal-setup-buffer 'summary))
2748       (unless gnus-single-article-buffer
2749         (make-local-variable 'gnus-article-buffer)
2750         (make-local-variable 'gnus-article-current)
2751         (make-local-variable 'gnus-original-article-buffer))
2752       (setq gnus-newsgroup-name group)
2753       ;; Set any local variables in the group parameters.
2754       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2755       t)))
2756
2757 (defun gnus-set-global-variables ()
2758   "Set the global equivalents of the buffer-local variables.
2759 They are set to the latest values they had.  These reflect the summary
2760 buffer that was in action when the last article was fetched."
2761   (when (eq major-mode 'gnus-summary-mode)
2762     (setq gnus-summary-buffer (current-buffer))
2763     (let ((name gnus-newsgroup-name)
2764           (marked gnus-newsgroup-marked)
2765           (unread gnus-newsgroup-unreads)
2766           (headers gnus-current-headers)
2767           (data gnus-newsgroup-data)
2768           (summary gnus-summary-buffer)
2769           (article-buffer gnus-article-buffer)
2770           (original gnus-original-article-buffer)
2771           (gac gnus-article-current)
2772           (reffed gnus-reffed-article-number)
2773           (score-file gnus-current-score-file)
2774           (default-charset gnus-newsgroup-charset)
2775           vlist)
2776       (let ((locals gnus-newsgroup-variables))
2777         (while locals
2778           (if (consp (car locals))
2779               (push (eval (caar locals)) vlist)
2780             (push (eval (car locals)) vlist))
2781           (setq locals (cdr locals)))
2782         (setq vlist (nreverse vlist)))
2783       (save-excursion
2784         (set-buffer gnus-group-buffer)
2785         (setq gnus-newsgroup-name name
2786               gnus-newsgroup-marked marked
2787               gnus-newsgroup-unreads unread
2788               gnus-current-headers headers
2789               gnus-newsgroup-data data
2790               gnus-article-current gac
2791               gnus-summary-buffer summary
2792               gnus-article-buffer article-buffer
2793               gnus-original-article-buffer original
2794               gnus-reffed-article-number reffed
2795               gnus-current-score-file score-file
2796               gnus-newsgroup-charset default-charset)
2797         (let ((locals gnus-newsgroup-variables))
2798           (while locals
2799             (if (consp (car locals))
2800                 (set (caar locals) (pop vlist))
2801               (set (car locals) (pop vlist)))
2802             (setq locals (cdr locals))))
2803         ;; The article buffer also has local variables.
2804         (when (gnus-buffer-live-p gnus-article-buffer)
2805           (set-buffer gnus-article-buffer)
2806           (setq gnus-summary-buffer summary))))))
2807
2808 (defun gnus-summary-article-unread-p (article)
2809   "Say whether ARTICLE is unread or not."
2810   (memq article gnus-newsgroup-unreads))
2811
2812 (defun gnus-summary-first-article-p (&optional article)
2813   "Return whether ARTICLE is the first article in the buffer."
2814   (if (not (setq article (or article (gnus-summary-article-number))))
2815       nil
2816     (eq article (caar gnus-newsgroup-data))))
2817
2818 (defun gnus-summary-last-article-p (&optional article)
2819   "Return whether ARTICLE is the last article in the buffer."
2820   (if (not (setq article (or article (gnus-summary-article-number))))
2821       ;; All non-existent numbers are the last article.  :-)
2822       t
2823     (not (cdr (gnus-data-find-list article)))))
2824
2825 (defun gnus-make-thread-indent-array ()
2826   (let ((n 200))
2827     (unless (and gnus-thread-indent-array
2828                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2829       (setq gnus-thread-indent-array (make-vector 201 "")
2830             gnus-thread-indent-array-level gnus-thread-indent-level)
2831       (while (>= n 0)
2832         (aset gnus-thread-indent-array n
2833               (make-string (* n gnus-thread-indent-level) ? ))
2834         (setq n (1- n))))))
2835
2836 (defun gnus-update-summary-mark-positions ()
2837   "Compute where the summary marks are to go."
2838   (save-excursion
2839     (when (gnus-buffer-exists-p gnus-summary-buffer)
2840       (set-buffer gnus-summary-buffer))
2841     (let ((gnus-replied-mark 129)
2842           (gnus-score-below-mark 130)
2843           (gnus-score-over-mark 130)
2844           (gnus-download-mark 131)
2845           (spec gnus-summary-line-format-spec)
2846           gnus-visual pos)
2847       (save-excursion
2848         (gnus-set-work-buffer)
2849         (let ((gnus-summary-line-format-spec spec)
2850               (gnus-newsgroup-downloadable '((0 . t))))
2851           (gnus-summary-insert-line
2852            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2853            0 nil 128 t nil "" nil 1)
2854           (goto-char (point-min))
2855           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2856                                              (- (point) 2)))))
2857           (goto-char (point-min))
2858           (push (cons 'replied (and (search-forward "\201" nil t)
2859                                     (- (point) 2)))
2860                 pos)
2861           (goto-char (point-min))
2862           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2863                 pos)
2864           (goto-char (point-min))
2865           (push (cons 'download
2866                       (and (search-forward "\203" nil t) (- (point) 2)))
2867                 pos)))
2868       (setq gnus-summary-mark-positions pos))))
2869
2870 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2871   "Insert a dummy root in the summary buffer."
2872   (beginning-of-line)
2873   (gnus-add-text-properties
2874    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2875    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2876
2877 (defun gnus-summary-from-or-to-or-newsgroups (header)
2878   (let ((to (cdr (assq 'To (mail-header-extra header))))
2879         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2880         (mail-parse-charset gnus-newsgroup-charset)
2881         (mail-parse-ignored-charsets
2882          (save-excursion (set-buffer gnus-summary-buffer)
2883                          gnus-newsgroup-ignored-charsets)))
2884     (cond
2885      ((and to
2886            gnus-ignored-from-addresses
2887            (string-match gnus-ignored-from-addresses
2888                          (mail-header-from header)))
2889       (concat "-> "
2890               (or (car (funcall gnus-extract-address-components
2891                                 (funcall
2892                                  gnus-decode-encoded-word-function to)))
2893                   (funcall gnus-decode-encoded-word-function to))))
2894      ((and newsgroups
2895            gnus-ignored-from-addresses
2896            (string-match gnus-ignored-from-addresses
2897                          (mail-header-from header)))
2898       (concat "=> " newsgroups))
2899      (t
2900       (or (car (funcall gnus-extract-address-components
2901                         (mail-header-from header)))
2902           (mail-header-from header))))))
2903
2904 (defun gnus-summary-insert-line (gnus-tmp-header
2905                                  gnus-tmp-level gnus-tmp-current
2906                                  gnus-tmp-unread gnus-tmp-replied
2907                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2908                                  &optional gnus-tmp-dummy gnus-tmp-score
2909                                  gnus-tmp-process)
2910   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2911          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2912          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2913          (gnus-tmp-score-char
2914           (if (or (null gnus-summary-default-score)
2915                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2916                       gnus-summary-zcore-fuzz))
2917               ?  ;Whitespace
2918             (if (< gnus-tmp-score gnus-summary-default-score)
2919                 gnus-score-below-mark gnus-score-over-mark)))
2920          (gnus-tmp-replied
2921           (cond (gnus-tmp-process gnus-process-mark)
2922                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2923                  gnus-cached-mark)
2924                 (gnus-tmp-replied gnus-replied-mark)
2925                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2926                  gnus-saved-mark)
2927                 (t gnus-no-mark)))
2928          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2929          (gnus-tmp-name
2930           (cond
2931            ((string-match "<[^>]+> *$" gnus-tmp-from)
2932             (let ((beg (match-beginning 0)))
2933               (or (and (string-match "^\".+\"" gnus-tmp-from)
2934                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2935                   (substring gnus-tmp-from 0 beg))))
2936            ((string-match "(.+)" gnus-tmp-from)
2937             (substring gnus-tmp-from
2938                        (1+ (match-beginning 0)) (1- (match-end 0))))
2939            (t gnus-tmp-from)))
2940          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2941          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2942          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2943          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2944          (buffer-read-only nil))
2945     (when (string= gnus-tmp-name "")
2946       (setq gnus-tmp-name gnus-tmp-from))
2947     (unless (numberp gnus-tmp-lines)
2948       (setq gnus-tmp-lines -1))
2949     (when (= gnus-tmp-lines -1)
2950       (setq gnus-tmp-lines "?"))
2951     (gnus-put-text-property
2952      (point)
2953      (progn (eval gnus-summary-line-format-spec) (point))
2954      'gnus-number gnus-tmp-number)
2955     (when (gnus-visual-p 'summary-highlight 'highlight)
2956       (forward-line -1)
2957       (gnus-run-hooks 'gnus-summary-update-hook)
2958       (forward-line 1))))
2959
2960 (defun gnus-summary-update-line (&optional dont-update)
2961   "Update summary line after change."
2962   (when (and gnus-summary-default-score
2963              (not gnus-summary-inhibit-highlight))
2964     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2965            (article (gnus-summary-article-number))
2966            (score (gnus-summary-article-score article)))
2967       (unless dont-update
2968         (if (and gnus-summary-mark-below
2969                  (< (gnus-summary-article-score)
2970                     gnus-summary-mark-below))
2971             ;; This article has a low score, so we mark it as read.
2972             (when (memq article gnus-newsgroup-unreads)
2973               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2974           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2975             ;; This article was previously marked as read on account
2976             ;; of a low score, but now it has risen, so we mark it as
2977             ;; unread.
2978             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2979         (gnus-summary-update-mark
2980          (if (or (null gnus-summary-default-score)
2981                  (<= (abs (- score gnus-summary-default-score))
2982                      gnus-summary-zcore-fuzz))
2983              ?  ;Whitespace
2984            (if (< score gnus-summary-default-score)
2985                gnus-score-below-mark gnus-score-over-mark))
2986          'score))
2987       ;; Do visual highlighting.
2988       (when (gnus-visual-p 'summary-highlight 'highlight)
2989         (gnus-run-hooks 'gnus-summary-update-hook)))))
2990
2991 (defvar gnus-tmp-new-adopts nil)
2992
2993 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2994   "Return the number of articles in THREAD.
2995 This may be 0 in some cases -- if none of the articles in
2996 the thread are to be displayed."
2997   (let* ((number
2998           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2999           (cond
3000            ((not (listp thread))
3001             1)
3002            ((and (consp thread) (cdr thread))
3003             (apply
3004              '+ 1 (mapcar
3005                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3006            ((null thread)
3007             1)
3008            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3009             1)
3010            (t 0))))
3011     (when (and level (zerop level) gnus-tmp-new-adopts)
3012       (incf number
3013             (apply '+ (mapcar
3014                        'gnus-summary-number-of-articles-in-thread
3015                        gnus-tmp-new-adopts))))
3016     (if char
3017         (if (> number 1) gnus-not-empty-thread-mark
3018           gnus-empty-thread-mark)
3019       number)))
3020
3021 (defun gnus-summary-set-local-parameters (group)
3022   "Go through the local params of GROUP and set all variable specs in that list."
3023   (let ((params (gnus-group-find-parameter group))
3024         (vars '(quit-config)) ; Ignore quit-config.
3025         elem)
3026     (while params
3027       (setq elem (car params)
3028             params (cdr params))
3029       (and (consp elem)                 ; Has to be a cons.
3030            (consp (cdr elem))           ; The cdr has to be a list.
3031            (symbolp (car elem))         ; Has to be a symbol in there.
3032            (not (memq (car elem) vars))
3033            (ignore-errors               ; So we set it.
3034              (push (car elem) vars)
3035              (make-local-variable (car elem))
3036              (set (car elem) (eval (nth 1 elem))))))))
3037
3038 (defun gnus-summary-read-group (group &optional show-all no-article
3039                                       kill-buffer no-display backward
3040                                       select-articles)
3041   "Start reading news in newsgroup GROUP.
3042 If SHOW-ALL is non-nil, already read articles are also listed.
3043 If NO-ARTICLE is non-nil, no article is selected initially.
3044 If NO-DISPLAY, don't generate a summary buffer."
3045   (let (result)
3046     (while (and group
3047                 (null (setq result
3048                             (let ((gnus-auto-select-next nil))
3049                               (or (gnus-summary-read-group-1
3050                                    group show-all no-article
3051                                    kill-buffer no-display
3052                                    select-articles)
3053                                   (setq show-all nil
3054                                         select-articles nil)))))
3055                 (eq gnus-auto-select-next 'quietly))
3056       (set-buffer gnus-group-buffer)
3057       ;; The entry function called above goes to the next
3058       ;; group automatically, so we go two groups back
3059       ;; if we are searching for the previous group.
3060       (when backward
3061         (gnus-group-prev-unread-group 2))
3062       (if (not (equal group (gnus-group-group-name)))
3063           (setq group (gnus-group-group-name))
3064         (setq group nil)))
3065     result))
3066
3067 (defun gnus-summary-read-group-1 (group show-all no-article
3068                                         kill-buffer no-display
3069                                         &optional select-articles)
3070   ;; Killed foreign groups can't be entered.
3071   ;;  (when (and (not (gnus-group-native-p group))
3072   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3073   ;;    (error "Dead non-native groups can't be entered"))
3074   (gnus-message 5 "Retrieving newsgroup: %s..." 
3075                 (gnus-group-decoded-name group))
3076   (let* ((new-group (gnus-summary-setup-buffer group))
3077          (quit-config (gnus-group-quit-config group))
3078          (did-select (and new-group (gnus-select-newsgroup
3079                                      group show-all select-articles))))
3080     (cond
3081      ;; This summary buffer exists already, so we just select it.
3082      ((not new-group)
3083       (gnus-set-global-variables)
3084       (when kill-buffer
3085         (gnus-kill-or-deaden-summary kill-buffer))
3086       (gnus-configure-windows 'summary 'force)
3087       (gnus-set-mode-line 'summary)
3088       (gnus-summary-position-point)
3089       (message "")
3090       t)
3091      ;; We couldn't select this group.
3092      ((null did-select)
3093       (when (and (eq major-mode 'gnus-summary-mode)
3094                  (not (equal (current-buffer) kill-buffer)))
3095         (kill-buffer (current-buffer))
3096         (if (not quit-config)
3097             (progn
3098               ;; Update the info -- marks might need to be removed,
3099               ;; for instance.
3100               (gnus-summary-update-info)
3101               (set-buffer gnus-group-buffer)
3102               (gnus-group-jump-to-group group)
3103               (gnus-group-next-unread-group 1))
3104           (gnus-handle-ephemeral-exit quit-config)))
3105       (let ((grpinfo (gnus-get-info group)))
3106         (if (null (gnus-info-read grpinfo))
3107             (gnus-message 3 "Group %s contains no messages" 
3108                           (gnus-group-decoded-name group))
3109           (gnus-message 3 "Can't select group")))
3110       nil)
3111      ;; The user did a `C-g' while prompting for number of articles,
3112      ;; so we exit this group.
3113      ((eq did-select 'quit)
3114       (and (eq major-mode 'gnus-summary-mode)
3115            (not (equal (current-buffer) kill-buffer))
3116            (kill-buffer (current-buffer)))
3117       (when kill-buffer
3118         (gnus-kill-or-deaden-summary kill-buffer))
3119       (if (not quit-config)
3120           (progn
3121             (set-buffer gnus-group-buffer)
3122             (gnus-group-jump-to-group group)
3123             (gnus-group-next-unread-group 1)
3124             (gnus-configure-windows 'group 'force))
3125         (gnus-handle-ephemeral-exit quit-config))
3126       ;; Finally signal the quit.
3127       (signal 'quit nil))
3128      ;; The group was successfully selected.
3129      (t
3130       (gnus-set-global-variables)
3131       ;; Save the active value in effect when the group was entered.
3132       (setq gnus-newsgroup-active
3133             (gnus-copy-sequence
3134              (gnus-active gnus-newsgroup-name)))
3135       ;; You can change the summary buffer in some way with this hook.
3136       (gnus-run-hooks 'gnus-select-group-hook)
3137       (gnus-update-format-specifications
3138        nil 'summary 'summary-mode 'summary-dummy)
3139       (gnus-update-summary-mark-positions)
3140       ;; Do score processing.
3141       (when gnus-use-scoring
3142         (gnus-possibly-score-headers))
3143       ;; Check whether to fill in the gaps in the threads.
3144       (when gnus-build-sparse-threads
3145         (gnus-build-sparse-threads))
3146       ;; Find the initial limit.
3147       (if gnus-show-threads
3148           (if show-all
3149               (let ((gnus-newsgroup-dormant nil))
3150                 (gnus-summary-initial-limit show-all))
3151             (gnus-summary-initial-limit show-all))
3152         ;; When untreaded, all articles are always shown.
3153         (setq gnus-newsgroup-limit
3154               (mapcar
3155                (lambda (header) (mail-header-number header))
3156                gnus-newsgroup-headers)))
3157       ;; Generate the summary buffer.
3158       (unless no-display
3159         (gnus-summary-prepare))
3160       (when gnus-use-trees
3161         (gnus-tree-open group)
3162         (setq gnus-summary-highlight-line-function
3163               'gnus-tree-highlight-article))
3164       ;; If the summary buffer is empty, but there are some low-scored
3165       ;; articles or some excluded dormants, we include these in the
3166       ;; buffer.
3167       (when (and (zerop (buffer-size))
3168                  (not no-display))
3169         (cond (gnus-newsgroup-dormant
3170                (gnus-summary-limit-include-dormant))
3171               ((and gnus-newsgroup-scored show-all)
3172                (gnus-summary-limit-include-expunged t))))
3173       ;; Function `gnus-apply-kill-file' must be called in this hook.
3174       (gnus-run-hooks 'gnus-apply-kill-hook)
3175       (if (and (zerop (buffer-size))
3176                (not no-display))
3177           (progn
3178             ;; This newsgroup is empty.
3179             (gnus-summary-catchup-and-exit nil t)
3180             (gnus-message 6 "No unread news")
3181             (when kill-buffer
3182               (gnus-kill-or-deaden-summary kill-buffer))
3183             ;; Return nil from this function.
3184             nil)
3185         ;; Hide conversation thread subtrees.  We cannot do this in
3186         ;; gnus-summary-prepare-hook since kill processing may not
3187         ;; work with hidden articles.
3188         (and gnus-show-threads
3189              gnus-thread-hide-subtree
3190              (gnus-summary-hide-all-threads))
3191         (when kill-buffer
3192           (gnus-kill-or-deaden-summary kill-buffer))
3193         ;; Show first unread article if requested.
3194         (if (and (not no-article)
3195                  (not no-display)
3196                  gnus-newsgroup-unreads
3197                  gnus-auto-select-first)
3198             (progn
3199               (gnus-configure-windows 'summary)
3200               (cond
3201                ((eq gnus-auto-select-first 'best)
3202                 (gnus-summary-best-unread-article))
3203                ((eq gnus-auto-select-first t)
3204                 (gnus-summary-first-unread-article))
3205                ((gnus-functionp gnus-auto-select-first)
3206                 (funcall gnus-auto-select-first))))
3207           ;; Don't select any articles, just move point to the first
3208           ;; article in the group.
3209           (goto-char (point-min))
3210           (gnus-summary-position-point)
3211           (gnus-configure-windows 'summary 'force)
3212           (gnus-set-mode-line 'summary))
3213         (when (get-buffer-window gnus-group-buffer t)
3214           ;; Gotta use windows, because recenter does weird stuff if
3215           ;; the current buffer ain't the displayed window.
3216           (let ((owin (selected-window)))
3217             (select-window (get-buffer-window gnus-group-buffer t))
3218             (when (gnus-group-goto-group group)
3219               (recenter))
3220             (select-window owin)))
3221         ;; Mark this buffer as "prepared".
3222         (setq gnus-newsgroup-prepared t)
3223         (gnus-run-hooks 'gnus-summary-prepared-hook)
3224         t)))))
3225
3226 (defun gnus-summary-prepare ()
3227   "Generate the summary buffer."
3228   (interactive)
3229   (let ((buffer-read-only nil))
3230     (erase-buffer)
3231     (setq gnus-newsgroup-data nil
3232           gnus-newsgroup-data-reverse nil)
3233     (gnus-run-hooks 'gnus-summary-generate-hook)
3234     ;; Generate the buffer, either with threads or without.
3235     (when gnus-newsgroup-headers
3236       (gnus-summary-prepare-threads
3237        (if gnus-show-threads
3238            (gnus-sort-gathered-threads
3239             (funcall gnus-summary-thread-gathering-function
3240                      (gnus-sort-threads
3241                       (gnus-cut-threads (gnus-make-threads)))))
3242          ;; Unthreaded display.
3243          (gnus-sort-articles gnus-newsgroup-headers))))
3244     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3245     ;; Call hooks for modifying summary buffer.
3246     (goto-char (point-min))
3247     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3248
3249 (defsubst gnus-general-simplify-subject (subject)
3250   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3251   (setq subject
3252         (cond
3253          ;; Truncate the subject.
3254          (gnus-simplify-subject-functions
3255           (gnus-map-function gnus-simplify-subject-functions subject))
3256          ((numberp gnus-summary-gather-subject-limit)
3257           (setq subject (gnus-simplify-subject-re subject))
3258           (if (> (length subject) gnus-summary-gather-subject-limit)
3259               (substring subject 0 gnus-summary-gather-subject-limit)
3260             subject))
3261          ;; Fuzzily simplify it.
3262          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3263           (gnus-simplify-subject-fuzzy subject))
3264          ;; Just remove the leading "Re:".
3265          (t
3266           (gnus-simplify-subject-re subject))))
3267
3268   (if (and gnus-summary-gather-exclude-subject
3269            (string-match gnus-summary-gather-exclude-subject subject))
3270       nil                               ; This article shouldn't be gathered
3271     subject))
3272
3273 (defun gnus-summary-simplify-subject-query ()
3274   "Query where the respool algorithm would put this article."
3275   (interactive)
3276   (gnus-summary-select-article)
3277   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3278
3279 (defun gnus-gather-threads-by-subject (threads)
3280   "Gather threads by looking at Subject headers."
3281   (if (not gnus-summary-make-false-root)
3282       threads
3283     (let ((hashtb (gnus-make-hashtable 1024))
3284           (prev threads)
3285           (result threads)
3286           subject hthread whole-subject)
3287       (while threads
3288         (setq subject (gnus-general-simplify-subject
3289                        (setq whole-subject (mail-header-subject
3290                                             (caar threads)))))
3291         (when subject
3292           (if (setq hthread (gnus-gethash subject hashtb))
3293               (progn
3294                 ;; We enter a dummy root into the thread, if we
3295                 ;; haven't done that already.
3296                 (unless (stringp (caar hthread))
3297                   (setcar hthread (list whole-subject (car hthread))))
3298                 ;; We add this new gathered thread to this gathered
3299                 ;; thread.
3300                 (setcdr (car hthread)
3301                         (nconc (cdar hthread) (list (car threads))))
3302                 ;; Remove it from the list of threads.
3303                 (setcdr prev (cdr threads))
3304                 (setq threads prev))
3305             ;; Enter this thread into the hash table.
3306             (gnus-sethash subject threads hashtb)))
3307         (setq prev threads)
3308         (setq threads (cdr threads)))
3309       result)))
3310
3311 (defun gnus-gather-threads-by-references (threads)
3312   "Gather threads by looking at References headers."
3313   (let ((idhashtb (gnus-make-hashtable 1024))
3314         (thhashtb (gnus-make-hashtable 1024))
3315         (prev threads)
3316         (result threads)
3317         ids references id gthread gid entered ref)
3318     (while threads
3319       (when (setq references (mail-header-references (caar threads)))
3320         (setq id (mail-header-id (caar threads))
3321               ids (gnus-split-references references)
3322               entered nil)
3323         (while (setq ref (pop ids))
3324           (setq ids (delete ref ids))
3325           (if (not (setq gid (gnus-gethash ref idhashtb)))
3326               (progn
3327                 (gnus-sethash ref id idhashtb)
3328                 (gnus-sethash id threads thhashtb))
3329             (setq gthread (gnus-gethash gid thhashtb))
3330             (unless entered
3331               ;; We enter a dummy root into the thread, if we
3332               ;; haven't done that already.
3333               (unless (stringp (caar gthread))
3334                 (setcar gthread (list (mail-header-subject (caar gthread))
3335                                       (car gthread))))
3336               ;; We add this new gathered thread to this gathered
3337               ;; thread.
3338               (setcdr (car gthread)
3339                       (nconc (cdar gthread) (list (car threads)))))
3340             ;; Add it into the thread hash table.
3341             (gnus-sethash id gthread thhashtb)
3342             (setq entered t)
3343             ;; Remove it from the list of threads.
3344             (setcdr prev (cdr threads))
3345             (setq threads prev))))
3346       (setq prev threads)
3347       (setq threads (cdr threads)))
3348     result))
3349
3350 (defun gnus-sort-gathered-threads (threads)
3351   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3352   (let ((result threads))
3353     (while threads
3354       (when (stringp (caar threads))
3355         (setcdr (car threads)
3356                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3357       (setq threads (cdr threads)))
3358     result))
3359
3360 (defun gnus-thread-loop-p (root thread)
3361   "Say whether ROOT is in THREAD."
3362   (let ((stack (list thread))
3363         (infloop 0)
3364         th)
3365     (while (setq thread (pop stack))
3366       (setq th (cdr thread))
3367       (while (and th
3368                   (not (eq (caar th) root)))
3369         (pop th))
3370       (if th
3371           ;; We have found a loop.
3372           (let (ref-dep)
3373             (setcdr thread (delq (car th) (cdr thread)))
3374             (if (boundp (setq ref-dep (intern "none"
3375                                               gnus-newsgroup-dependencies)))
3376                 (setcdr (symbol-value ref-dep)
3377                         (nconc (cdr (symbol-value ref-dep))
3378                                (list (car th))))
3379               (set ref-dep (list nil (car th))))
3380             (setq infloop 1
3381                   stack nil))
3382         ;; Push all the subthreads onto the stack.
3383         (push (cdr thread) stack)))
3384     infloop))
3385
3386 (defun gnus-make-threads ()
3387   "Go through the dependency hashtb and find the roots.  Return all threads."
3388   (let (threads)
3389     (while (catch 'infloop
3390              (mapatoms
3391               (lambda (refs)
3392                 ;; Deal with self-referencing References loops.
3393                 (when (and (car (symbol-value refs))
3394                            (not (zerop
3395                                  (apply
3396                                   '+
3397                                   (mapcar
3398                                    (lambda (thread)
3399                                      (gnus-thread-loop-p
3400                                       (car (symbol-value refs)) thread))
3401                                    (cdr (symbol-value refs)))))))
3402                   (setq threads nil)
3403                   (throw 'infloop t))
3404                 (unless (car (symbol-value refs))
3405                   ;; These threads do not refer back to any other articles,
3406                   ;; so they're roots.
3407                   (setq threads (append (cdr (symbol-value refs)) threads))))
3408               gnus-newsgroup-dependencies)))
3409     threads))
3410
3411 ;; Build the thread tree.
3412 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3413   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3414
3415 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3416 if it was already present.
3417
3418 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3419 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3420 Message-IDs will be renamed be renamed to a unique Message-ID before
3421 being entered.
3422
3423 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3424   (let* ((id (mail-header-id header))
3425          (id-dep (and id (intern id dependencies)))
3426          ref ref-dep ref-header)
3427     ;; Enter this `header' in the `dependencies' table.
3428     (cond
3429      ((not id-dep)
3430       (setq header nil))
3431      ;; The first two cases do the normal part: enter a new `header'
3432      ;; in the `dependencies' table.
3433      ((not (boundp id-dep))
3434       (set id-dep (list header)))
3435      ((null (car (symbol-value id-dep)))
3436       (setcar (symbol-value id-dep) header))
3437
3438      ;; From here the `header' was already present in the
3439      ;; `dependencies' table.
3440      (force-new
3441       ;; Overrides an existing entry;
3442       ;; just set the header part of the entry.
3443       (setcar (symbol-value id-dep) header))
3444
3445      ;; Renames the existing `header' to a unique Message-ID.
3446      ((not gnus-summary-ignore-duplicates)
3447       ;; An article with this Message-ID has already been seen.
3448       ;; We rename the Message-ID.
3449       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3450            (list header))
3451       (mail-header-set-id header id))
3452
3453      ;; The last case ignores an existing entry, except it adds any
3454      ;; additional Xrefs (in case the two articles came from different
3455      ;; servers.
3456      ;; Also sets `header' to `nil' meaning that the `dependencies'
3457      ;; table was *not* modified.
3458      (t
3459       (mail-header-set-xref
3460        (car (symbol-value id-dep))
3461        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3462                    "")
3463                (or (mail-header-xref header) "")))
3464       (setq header nil)))
3465
3466     (when header
3467       ;; First check if that we are not creating a References loop.
3468       (setq ref (gnus-parent-id (mail-header-references header)))
3469       (while (and ref
3470                   (setq ref-dep (intern-soft ref dependencies))
3471                   (boundp ref-dep)
3472                   (setq ref-header (car (symbol-value ref-dep))))
3473         (if (string= id ref)
3474             ;; Yuk!  This is a reference loop.  Make the article be a
3475             ;; root article.
3476             (progn
3477               (mail-header-set-references (car (symbol-value id-dep)) "none")
3478               (setq ref nil))
3479           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3480       (setq ref (gnus-parent-id (mail-header-references header)))
3481       (setq ref-dep (intern (or ref "none") dependencies))
3482       (if (boundp ref-dep)
3483           (setcdr (symbol-value ref-dep)
3484                   (nconc (cdr (symbol-value ref-dep))
3485                          (list (symbol-value id-dep))))
3486         (set ref-dep (list nil (symbol-value id-dep)))))
3487     header))
3488
3489 (defun gnus-build-sparse-threads ()
3490   (let ((headers gnus-newsgroup-headers)
3491         (mail-parse-charset gnus-newsgroup-charset)
3492         (gnus-summary-ignore-duplicates t)
3493         header references generation relations
3494         subject child end new-child date)
3495     ;; First we create an alist of generations/relations, where
3496     ;; generations is how much we trust the relation, and the relation
3497     ;; is parent/child.
3498     (gnus-message 7 "Making sparse threads...")
3499     (save-excursion
3500       (nnheader-set-temp-buffer " *gnus sparse threads*")
3501       (while (setq header (pop headers))
3502         (when (and (setq references (mail-header-references header))
3503                    (not (string= references "")))
3504           (insert references)
3505           (setq child (mail-header-id header)
3506                 subject (mail-header-subject header)
3507                 date (mail-header-date header)
3508                 generation 0)
3509           (while (search-backward ">" nil t)
3510             (setq end (1+ (point)))
3511             (when (search-backward "<" nil t)
3512               (setq new-child (buffer-substring (point) end))
3513               (push (list (incf generation)
3514                           child (setq child new-child)
3515                           subject date)
3516                     relations)))
3517           (when child
3518             (push (list (1+ generation) child nil subject) relations))
3519           (erase-buffer)))
3520       (kill-buffer (current-buffer)))
3521     ;; Sort over trustworthiness.
3522     (mapcar
3523      (lambda (relation)
3524        (when (gnus-dependencies-add-header
3525               (make-full-mail-header
3526                gnus-reffed-article-number
3527                (nth 3 relation) "" (or (nth 4 relation) "")
3528                (nth 1 relation)
3529                (or (nth 2 relation) "") 0 0 "")
3530               gnus-newsgroup-dependencies nil)
3531          (push gnus-reffed-article-number gnus-newsgroup-limit)
3532          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3533          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3534                gnus-newsgroup-reads)
3535          (decf gnus-reffed-article-number)))
3536      (sort relations 'car-less-than-car))
3537     (gnus-message 7 "Making sparse threads...done")))
3538
3539 (defun gnus-build-old-threads ()
3540   ;; Look at all the articles that refer back to old articles, and
3541   ;; fetch the headers for the articles that aren't there.  This will
3542   ;; build complete threads - if the roots haven't been expired by the
3543   ;; server, that is.
3544   (let ((mail-parse-charset gnus-newsgroup-charset)
3545         id heads)
3546     (mapatoms
3547      (lambda (refs)
3548        (when (not (car (symbol-value refs)))
3549          (setq heads (cdr (symbol-value refs)))
3550          (while heads
3551            (if (memq (mail-header-number (caar heads))
3552                      gnus-newsgroup-dormant)
3553                (setq heads (cdr heads))
3554              (setq id (symbol-name refs))
3555              (while (and (setq id (gnus-build-get-header id))
3556                          (not (car (gnus-id-to-thread id)))))
3557              (setq heads nil)))))
3558      gnus-newsgroup-dependencies)))
3559
3560 ;; This function has to be called with point after the article number
3561 ;; on the beginning of the line.
3562 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3563   (let ((eol (gnus-point-at-eol))
3564         (buffer (current-buffer))
3565         header)
3566
3567     ;; overview: [num subject from date id refs chars lines misc]
3568     (unwind-protect
3569         (progn
3570           (narrow-to-region (point) eol)
3571           (unless (eobp)
3572             (forward-char))
3573
3574           (setq header
3575                 (make-full-mail-header
3576                  number                 ; number
3577                  (funcall gnus-decode-encoded-word-function
3578                           (nnheader-nov-field)) ; subject
3579                  (funcall gnus-decode-encoded-word-function
3580                           (nnheader-nov-field)) ; from
3581                  (nnheader-nov-field)   ; date
3582                  (nnheader-nov-read-message-id) ; id
3583                  (nnheader-nov-field)   ; refs
3584                  (nnheader-nov-read-integer) ; chars
3585                  (nnheader-nov-read-integer) ; lines
3586                  (unless (eobp)
3587                    (if (looking-at "Xref: ")
3588                        (goto-char (match-end 0)))
3589                    (nnheader-nov-field)) ; Xref
3590                  (nnheader-nov-parse-extra)))) ; extra
3591
3592       (widen))
3593
3594     (when gnus-alter-header-function
3595       (funcall gnus-alter-header-function header))
3596     (gnus-dependencies-add-header header dependencies force-new)))
3597
3598 (defun gnus-build-get-header (id)
3599   "Look through the buffer of NOV lines and find the header to ID.
3600 Enter this line into the dependencies hash table, and return
3601 the id of the parent article (if any)."
3602   (let ((deps gnus-newsgroup-dependencies)
3603         found header)
3604     (prog1
3605         (save-excursion
3606           (set-buffer nntp-server-buffer)
3607           (let ((case-fold-search nil))
3608             (goto-char (point-min))
3609             (while (and (not found)
3610                         (search-forward id nil t))
3611               (beginning-of-line)
3612               (setq found (looking-at
3613                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3614                                    (regexp-quote id))))
3615               (or found (beginning-of-line 2)))
3616             (when found
3617               (beginning-of-line)
3618               (and
3619                (setq header (gnus-nov-parse-line
3620                              (read (current-buffer)) deps))
3621                (gnus-parent-id (mail-header-references header))))))
3622       (when header
3623         (let ((number (mail-header-number header)))
3624           (push number gnus-newsgroup-limit)
3625           (push header gnus-newsgroup-headers)
3626           (if (memq number gnus-newsgroup-unselected)
3627               (progn
3628                 (push number gnus-newsgroup-unreads)
3629                 (setq gnus-newsgroup-unselected
3630                       (delq number gnus-newsgroup-unselected)))
3631             (push number gnus-newsgroup-ancient)))))))
3632
3633 (defun gnus-build-all-threads ()
3634   "Read all the headers."
3635   (let ((gnus-summary-ignore-duplicates t)
3636         (mail-parse-charset gnus-newsgroup-charset)
3637         (dependencies gnus-newsgroup-dependencies)
3638         header article)
3639     (save-excursion
3640       (set-buffer nntp-server-buffer)
3641       (let ((case-fold-search nil))
3642         (goto-char (point-min))
3643         (while (not (eobp))
3644           (ignore-errors
3645             (setq article (read (current-buffer))
3646                   header (gnus-nov-parse-line article dependencies)))
3647           (when header
3648             (save-excursion
3649               (set-buffer gnus-summary-buffer)
3650               (push header gnus-newsgroup-headers)
3651               (if (memq (setq article (mail-header-number header))
3652                         gnus-newsgroup-unselected)
3653                   (progn
3654                     (push article gnus-newsgroup-unreads)
3655                     (setq gnus-newsgroup-unselected
3656                           (delq article gnus-newsgroup-unselected)))
3657                 (push article gnus-newsgroup-ancient)))
3658             (forward-line 1)))))))
3659
3660 (defun gnus-summary-update-article-line (article header)
3661   "Update the line for ARTICLE using HEADERS."
3662   (let* ((id (mail-header-id header))
3663          (thread (gnus-id-to-thread id)))
3664     (unless thread
3665       (error "Article in no thread"))
3666     ;; Update the thread.
3667     (setcar thread header)
3668     (gnus-summary-goto-subject article)
3669     (let* ((datal (gnus-data-find-list article))
3670            (data (car datal))
3671            (length (when (cdr datal)
3672                      (- (gnus-data-pos data)
3673                         (gnus-data-pos (cadr datal)))))
3674            (buffer-read-only nil)
3675            (level (gnus-summary-thread-level)))
3676       (gnus-delete-line)
3677       (gnus-summary-insert-line
3678        header level nil (gnus-article-mark article)
3679        (memq article gnus-newsgroup-replied)
3680        (memq article gnus-newsgroup-expirable)
3681        ;; Only insert the Subject string when it's different
3682        ;; from the previous Subject string.
3683        (if (and
3684             gnus-show-threads
3685             (gnus-subject-equal
3686              (condition-case ()
3687                  (mail-header-subject
3688                   (gnus-data-header
3689                    (cadr
3690                     (gnus-data-find-list
3691                      article
3692                      (gnus-data-list t)))))
3693                ;; Error on the side of excessive subjects.
3694                (error ""))
3695              (mail-header-subject header)))
3696            ""
3697          (mail-header-subject header))
3698        nil (cdr (assq article gnus-newsgroup-scored))
3699        (memq article gnus-newsgroup-processable))
3700       (when length
3701         (gnus-data-update-list
3702          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3703
3704 (defun gnus-summary-update-article (article &optional iheader)
3705   "Update ARTICLE in the summary buffer."
3706   (set-buffer gnus-summary-buffer)
3707   (let* ((header (gnus-summary-article-header article))
3708          (id (mail-header-id header))
3709          (data (gnus-data-find article))
3710          (thread (gnus-id-to-thread id))
3711          (references (mail-header-references header))
3712          (parent
3713           (gnus-id-to-thread
3714            (or (gnus-parent-id
3715                 (when (and references
3716                            (not (equal "" references)))
3717                   references))
3718                "none")))
3719          (buffer-read-only nil)
3720          (old (car thread)))
3721     (when thread
3722       (unless iheader
3723         (setcar thread nil)
3724         (when parent
3725           (delq thread parent)))
3726       (if (gnus-summary-insert-subject id header)
3727           ;; Set the (possibly) new article number in the data structure.
3728           (gnus-data-set-number data (gnus-id-to-article id))
3729         (setcar thread old)
3730         nil))))
3731
3732 (defun gnus-rebuild-thread (id &optional line)
3733   "Rebuild the thread containing ID.
3734 If LINE, insert the rebuilt thread starting on line LINE."
3735   (let ((buffer-read-only nil)
3736         old-pos current thread data)
3737     (if (not gnus-show-threads)
3738         (setq thread (list (car (gnus-id-to-thread id))))
3739       ;; Get the thread this article is part of.
3740       (setq thread (gnus-remove-thread id)))
3741     (setq old-pos (gnus-point-at-bol))
3742     (setq current (save-excursion
3743                     (and (re-search-backward "[\r\n]" nil t)
3744                          (gnus-summary-article-number))))
3745     ;; If this is a gathered thread, we have to go some re-gathering.
3746     (when (stringp (car thread))
3747       (let ((subject (car thread))
3748             roots thr)
3749         (setq thread (cdr thread))
3750         (while thread
3751           (unless (memq (setq thr (gnus-id-to-thread
3752                                    (gnus-root-id
3753                                     (mail-header-id (caar thread)))))
3754                         roots)
3755             (push thr roots))
3756           (setq thread (cdr thread)))
3757         ;; We now have all (unique) roots.
3758         (if (= (length roots) 1)
3759             ;; All the loose roots are now one solid root.
3760             (setq thread (car roots))
3761           (setq thread (cons subject (gnus-sort-threads roots))))))
3762     (let (threads)
3763       ;; We then insert this thread into the summary buffer.
3764       (when line
3765         (goto-char (point-min))
3766         (forward-line (1- line)))
3767       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3768         (if gnus-show-threads
3769             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3770           (gnus-summary-prepare-unthreaded thread))
3771         (setq data (nreverse gnus-newsgroup-data))
3772         (setq threads gnus-newsgroup-threads))
3773       ;; We splice the new data into the data structure.
3774       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3775       ;;!!! then we want to insert at the beginning of the buffer.
3776       ;;!!! That happens to be true with Gnus now, but that may
3777       ;;!!! change in the future.  Perhaps.
3778       (gnus-data-enter-list
3779        (if line nil current) data (- (point) old-pos))
3780       (setq gnus-newsgroup-threads
3781             (nconc threads gnus-newsgroup-threads))
3782       (gnus-data-compute-positions))))
3783
3784 (defun gnus-number-to-header (number)
3785   "Return the header for article NUMBER."
3786   (let ((headers gnus-newsgroup-headers))
3787     (while (and headers
3788                 (not (= number (mail-header-number (car headers)))))
3789       (pop headers))
3790     (when headers
3791       (car headers))))
3792
3793 (defun gnus-parent-headers (in-headers &optional generation)
3794   "Return the headers of the GENERATIONeth parent of HEADERS."
3795   (unless generation
3796     (setq generation 1))
3797   (let ((parent t)
3798         (headers in-headers)
3799         references)
3800     (while (and parent
3801                 (not (zerop generation))
3802                 (setq references (mail-header-references headers)))
3803       (setq headers (if (and references
3804                              (setq parent (gnus-parent-id references)))
3805                         (car (gnus-id-to-thread parent))
3806                       nil))
3807       (decf generation))
3808     (and (not (eq headers in-headers))
3809          headers)))
3810
3811 (defun gnus-id-to-thread (id)
3812   "Return the (sub-)thread where ID appears."
3813   (gnus-gethash id gnus-newsgroup-dependencies))
3814
3815 (defun gnus-id-to-article (id)
3816   "Return the article number of ID."
3817   (let ((thread (gnus-id-to-thread id)))
3818     (when (and thread
3819                (car thread))
3820       (mail-header-number (car thread)))))
3821
3822 (defun gnus-id-to-header (id)
3823   "Return the article headers of ID."
3824   (car (gnus-id-to-thread id)))
3825
3826 (defun gnus-article-displayed-root-p (article)
3827   "Say whether ARTICLE is a root(ish) article."
3828   (let ((level (gnus-summary-thread-level article))
3829         (refs (mail-header-references  (gnus-summary-article-header article)))
3830         particle)
3831     (cond
3832      ((null level) nil)
3833      ((zerop level) t)
3834      ((null refs) t)
3835      ((null (gnus-parent-id refs)) t)
3836      ((and (= 1 level)
3837            (null (setq particle (gnus-id-to-article
3838                                  (gnus-parent-id refs))))
3839            (null (gnus-summary-thread-level particle)))))))
3840
3841 (defun gnus-root-id (id)
3842   "Return the id of the root of the thread where ID appears."
3843   (let (last-id prev)
3844     (while (and id (setq prev (car (gnus-id-to-thread id))))
3845       (setq last-id id
3846             id (gnus-parent-id (mail-header-references prev))))
3847     last-id))
3848
3849 (defun gnus-articles-in-thread (thread)
3850   "Return the list of articles in THREAD."
3851   (cons (mail-header-number (car thread))
3852         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3853
3854 (defun gnus-remove-thread (id &optional dont-remove)
3855   "Remove the thread that has ID in it."
3856   (let (headers thread last-id)
3857     ;; First go up in this thread until we find the root.
3858     (setq last-id (gnus-root-id id)
3859           headers (message-flatten-list (gnus-id-to-thread last-id)))
3860     ;; We have now found the real root of this thread.  It might have
3861     ;; been gathered into some loose thread, so we have to search
3862     ;; through the threads to find the thread we wanted.
3863     (let ((threads gnus-newsgroup-threads)
3864           sub)
3865       (while threads
3866         (setq sub (car threads))
3867         (if (stringp (car sub))
3868             ;; This is a gathered thread, so we look at the roots
3869             ;; below it to find whether this article is in this
3870             ;; gathered root.
3871             (progn
3872               (setq sub (cdr sub))
3873               (while sub
3874                 (when (member (caar sub) headers)
3875                   (setq thread (car threads)
3876                         threads nil
3877                         sub nil))
3878                 (setq sub (cdr sub))))
3879           ;; It's an ordinary thread, so we check it.
3880           (when (eq (car sub) (car headers))
3881             (setq thread sub
3882                   threads nil)))
3883         (setq threads (cdr threads)))
3884       ;; If this article is in no thread, then it's a root.
3885       (if thread
3886           (unless dont-remove
3887             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3888         (setq thread (gnus-id-to-thread last-id)))
3889       (when thread
3890         (prog1
3891             thread                      ; We return this thread.
3892           (unless dont-remove
3893             (if (stringp (car thread))
3894                 (progn
3895                   ;; If we use dummy roots, then we have to remove the
3896                   ;; dummy root as well.
3897                   (when (eq gnus-summary-make-false-root 'dummy)
3898                     ;; We go to the dummy root by going to
3899                     ;; the first sub-"thread", and then one line up.
3900                     (gnus-summary-goto-article
3901                      (mail-header-number (caadr thread)))
3902                     (forward-line -1)
3903                     (gnus-delete-line)
3904                     (gnus-data-compute-positions))
3905                   (setq thread (cdr thread))
3906                   (while thread
3907                     (gnus-remove-thread-1 (car thread))
3908                     (setq thread (cdr thread))))
3909               (gnus-remove-thread-1 thread))))))))
3910
3911 (defun gnus-remove-thread-1 (thread)
3912   "Remove the thread THREAD recursively."
3913   (let ((number (mail-header-number (pop thread)))
3914         d)
3915     (setq thread (reverse thread))
3916     (while thread
3917       (gnus-remove-thread-1 (pop thread)))
3918     (when (setq d (gnus-data-find number))
3919       (goto-char (gnus-data-pos d))
3920       (gnus-summary-show-thread)
3921       (gnus-data-remove
3922        number
3923        (- (gnus-point-at-bol)
3924           (prog1
3925               (1+ (gnus-point-at-eol))
3926             (gnus-delete-line)))))))
3927
3928 (defun gnus-sort-threads-1 (threads func)
3929   (sort (mapcar (lambda (thread)
3930                   (cons (car thread)
3931                         (and (cdr thread)
3932                              (gnus-sort-threads-1 (cdr thread) func))))
3933                 threads) func))
3934
3935 (defun gnus-sort-threads (threads)
3936   "Sort THREADS."
3937   (if (not gnus-thread-sort-functions)
3938       threads
3939     (gnus-message 8 "Sorting threads...")
3940     (prog1
3941         (gnus-sort-threads-1
3942          threads
3943          (gnus-make-sort-function gnus-thread-sort-functions))
3944       (gnus-message 8 "Sorting threads...done"))))
3945
3946 (defun gnus-sort-articles (articles)
3947   "Sort ARTICLES."
3948   (when gnus-article-sort-functions
3949     (gnus-message 7 "Sorting articles...")
3950     (prog1
3951         (setq gnus-newsgroup-headers
3952               (sort articles (gnus-make-sort-function
3953                               gnus-article-sort-functions)))
3954       (gnus-message 7 "Sorting articles...done"))))
3955
3956 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3957 (defmacro gnus-thread-header (thread)
3958   "Return header of first article in THREAD.
3959 Note that THREAD must never, ever be anything else than a variable -
3960 using some other form will lead to serious barfage."
3961   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3962   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3963   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3964         (vector thread) 2))
3965
3966 (defsubst gnus-article-sort-by-number (h1 h2)
3967   "Sort articles by article number."
3968   (< (mail-header-number h1)
3969      (mail-header-number h2)))
3970
3971 (defun gnus-thread-sort-by-number (h1 h2)
3972   "Sort threads by root article number."
3973   (gnus-article-sort-by-number
3974    (gnus-thread-header h1) (gnus-thread-header h2)))
3975
3976 (defsubst gnus-article-sort-by-lines (h1 h2)
3977   "Sort articles by article Lines header."
3978   (< (mail-header-lines h1)
3979      (mail-header-lines h2)))
3980
3981 (defun gnus-thread-sort-by-lines (h1 h2)
3982   "Sort threads by root article Lines header."
3983   (gnus-article-sort-by-lines
3984    (gnus-thread-header h1) (gnus-thread-header h2)))
3985
3986 (defsubst gnus-article-sort-by-chars (h1 h2)
3987   "Sort articles by octet length."
3988   (< (mail-header-chars h1)
3989      (mail-header-chars h2)))
3990
3991 (defun gnus-thread-sort-by-chars (h1 h2)
3992   "Sort threads by root article octet length."
3993   (gnus-article-sort-by-chars
3994    (gnus-thread-header h1) (gnus-thread-header h2)))
3995
3996 (defsubst gnus-article-sort-by-author (h1 h2)
3997   "Sort articles by root author."
3998   (string-lessp
3999    (let ((extract (funcall
4000                    gnus-extract-address-components
4001                    (mail-header-from h1))))
4002      (or (car extract) (cadr extract) ""))
4003    (let ((extract (funcall
4004                    gnus-extract-address-components
4005                    (mail-header-from h2))))
4006      (or (car extract) (cadr extract) ""))))
4007
4008 (defun gnus-thread-sort-by-author (h1 h2)
4009   "Sort threads by root author."
4010   (gnus-article-sort-by-author
4011    (gnus-thread-header h1)  (gnus-thread-header h2)))
4012
4013 (defsubst gnus-article-sort-by-subject (h1 h2)
4014   "Sort articles by root subject."
4015   (string-lessp
4016    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4017    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4018
4019 (defun gnus-thread-sort-by-subject (h1 h2)
4020   "Sort threads by root subject."
4021   (gnus-article-sort-by-subject
4022    (gnus-thread-header h1) (gnus-thread-header h2)))
4023
4024 (defsubst gnus-article-sort-by-date (h1 h2)
4025   "Sort articles by root article date."
4026   (time-less-p
4027    (gnus-date-get-time (mail-header-date h1))
4028    (gnus-date-get-time (mail-header-date h2))))
4029
4030 (defun gnus-thread-sort-by-date (h1 h2)
4031   "Sort threads by root article date."
4032   (gnus-article-sort-by-date
4033    (gnus-thread-header h1) (gnus-thread-header h2)))
4034
4035 (defsubst gnus-article-sort-by-score (h1 h2)
4036   "Sort articles by root article score.
4037 Unscored articles will be counted as having a score of zero."
4038   (> (or (cdr (assq (mail-header-number h1)
4039                     gnus-newsgroup-scored))
4040          gnus-summary-default-score 0)
4041      (or (cdr (assq (mail-header-number h2)
4042                     gnus-newsgroup-scored))
4043          gnus-summary-default-score 0)))
4044
4045 (defun gnus-thread-sort-by-score (h1 h2)
4046   "Sort threads by root article score."
4047   (gnus-article-sort-by-score
4048    (gnus-thread-header h1) (gnus-thread-header h2)))
4049
4050 (defun gnus-thread-sort-by-total-score (h1 h2)
4051   "Sort threads by the sum of all scores in the thread.
4052 Unscored articles will be counted as having a score of zero."
4053   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4054
4055 (defun gnus-thread-total-score (thread)
4056   ;; This function find the total score of THREAD.
4057   (cond ((null thread)
4058          0)
4059         ((consp thread)
4060          (if (stringp (car thread))
4061              (apply gnus-thread-score-function 0
4062                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4063            (gnus-thread-total-score-1 thread)))
4064         (t
4065          (gnus-thread-total-score-1 (list thread)))))
4066
4067 (defun gnus-thread-total-score-1 (root)
4068   ;; This function find the total score of the thread below ROOT.
4069   (setq root (car root))
4070   (apply gnus-thread-score-function
4071          (or (append
4072               (mapcar 'gnus-thread-total-score
4073                       (cdr (gnus-id-to-thread (mail-header-id root))))
4074               (when (> (mail-header-number root) 0)
4075                 (list (or (cdr (assq (mail-header-number root)
4076                                      gnus-newsgroup-scored))
4077                           gnus-summary-default-score 0))))
4078              (list gnus-summary-default-score)
4079              '(0))))
4080
4081 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4082 (defvar gnus-tmp-prev-subject nil)
4083 (defvar gnus-tmp-false-parent nil)
4084 (defvar gnus-tmp-root-expunged nil)
4085 (defvar gnus-tmp-dummy-line nil)
4086
4087 (eval-when-compile (defvar gnus-tmp-header))
4088 (defun gnus-extra-header (type &optional header)
4089   "Return the extra header of TYPE."
4090   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4091       ""))
4092
4093 (defvar gnus-tmp-thread-tree-header-string "")
4094
4095 (defvar gnus-sum-thread-tree-root "> ")
4096 (defvar gnus-sum-thread-tree-single-indent "")
4097 (defvar gnus-sum-thread-tree-vertical "| ")
4098 (defvar gnus-sum-thread-tree-indent "  ")
4099 (defvar gnus-sum-thread-tree-leaf-with-other "+-> ")
4100 (defvar gnus-sum-thread-tree-single-leaf "\\-> ")
4101
4102 (defun gnus-summary-prepare-threads (threads)
4103   "Prepare summary buffer from THREADS and indentation LEVEL.
4104 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4105 or a straight list of headers."
4106   (gnus-message 7 "Generating summary...")
4107
4108   (setq gnus-newsgroup-threads threads)
4109   (beginning-of-line)
4110
4111   (let ((gnus-tmp-level 0)
4112         (default-score (or gnus-summary-default-score 0))
4113         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4114         thread number subject stack state gnus-tmp-gathered beg-match
4115         new-roots gnus-tmp-new-adopts thread-end
4116         gnus-tmp-header gnus-tmp-unread
4117         gnus-tmp-replied gnus-tmp-subject-or-nil
4118         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4119         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4120         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4121         tree-stack)
4122
4123     (setq gnus-tmp-prev-subject nil)
4124
4125     (if (vectorp (car threads))
4126         ;; If this is a straight (sic) list of headers, then a
4127         ;; threaded summary display isn't required, so we just create
4128         ;; an unthreaded one.
4129         (gnus-summary-prepare-unthreaded threads)
4130
4131       ;; Do the threaded display.
4132
4133       (while (or threads stack gnus-tmp-new-adopts new-roots)
4134
4135         (if (and (= gnus-tmp-level 0)
4136                  (or (not stack)
4137                      (= (caar stack) 0))
4138                  (not gnus-tmp-false-parent)
4139                  (or gnus-tmp-new-adopts new-roots))
4140             (if gnus-tmp-new-adopts
4141                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4142                       thread (list (car gnus-tmp-new-adopts))
4143                       gnus-tmp-header (caar thread)
4144                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4145               (when new-roots
4146                 (setq thread (list (car new-roots))
4147                       gnus-tmp-header (caar thread)
4148                       new-roots (cdr new-roots))))
4149
4150           (if threads
4151               ;; If there are some threads, we do them before the
4152               ;; threads on the stack.
4153               (setq thread threads
4154                     gnus-tmp-header (caar thread))
4155             ;; There were no current threads, so we pop something off
4156             ;; the stack.
4157             (setq state (car stack)
4158                   gnus-tmp-level (car state)
4159                   tree-stack (cadr state)
4160                   thread (caddr state)
4161                   stack (cdr stack)
4162                   gnus-tmp-header (caar thread))))
4163
4164         (setq gnus-tmp-false-parent nil)
4165         (setq gnus-tmp-root-expunged nil)
4166         (setq thread-end nil)
4167
4168         (if (stringp gnus-tmp-header)
4169             ;; The header is a dummy root.
4170             (cond
4171              ((eq gnus-summary-make-false-root 'adopt)
4172               ;; We let the first article adopt the rest.
4173               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4174                                                (cddar thread)))
4175               (setq gnus-tmp-gathered
4176                     (nconc (mapcar
4177                             (lambda (h) (mail-header-number (car h)))
4178                             (cddar thread))
4179                            gnus-tmp-gathered))
4180               (setq thread (cons (list (caar thread)
4181                                        (cadar thread))
4182                                  (cdr thread)))
4183               (setq gnus-tmp-level -1
4184                     gnus-tmp-false-parent t))
4185              ((eq gnus-summary-make-false-root 'empty)
4186               ;; We print adopted articles with empty subject fields.
4187               (setq gnus-tmp-gathered
4188                     (nconc (mapcar
4189                             (lambda (h) (mail-header-number (car h)))
4190                             (cddar thread))
4191                            gnus-tmp-gathered))
4192               (setq gnus-tmp-level -1))
4193              ((eq gnus-summary-make-false-root 'dummy)
4194               ;; We remember that we probably want to output a dummy
4195               ;; root.
4196               (setq gnus-tmp-dummy-line gnus-tmp-header)
4197               (setq gnus-tmp-prev-subject gnus-tmp-header))
4198              (t
4199               ;; We do not make a root for the gathered
4200               ;; sub-threads at all.
4201               (setq gnus-tmp-level -1)))
4202
4203           (setq number (mail-header-number gnus-tmp-header)
4204                 subject (mail-header-subject gnus-tmp-header))
4205
4206           (cond
4207            ;; If the thread has changed subject, we might want to make
4208            ;; this subthread into a root.
4209            ((and (null gnus-thread-ignore-subject)
4210                  (not (zerop gnus-tmp-level))
4211                  gnus-tmp-prev-subject
4212                  (not (inline
4213                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4214             (setq new-roots (nconc new-roots (list (car thread)))
4215                   thread-end t
4216                   gnus-tmp-header nil))
4217            ;; If the article lies outside the current limit,
4218            ;; then we do not display it.
4219            ((not (memq number gnus-newsgroup-limit))
4220             (setq gnus-tmp-gathered
4221                   (nconc (mapcar
4222                           (lambda (h) (mail-header-number (car h)))
4223                           (cdar thread))
4224                          gnus-tmp-gathered))
4225             (setq gnus-tmp-new-adopts (if (cdar thread)
4226                                           (append gnus-tmp-new-adopts
4227                                                   (cdar thread))
4228                                         gnus-tmp-new-adopts)
4229                   thread-end t
4230                   gnus-tmp-header nil)
4231             (when (zerop gnus-tmp-level)
4232               (setq gnus-tmp-root-expunged t)))
4233            ;; Perhaps this article is to be marked as read?
4234            ((and gnus-summary-mark-below
4235                  (< (or (cdr (assq number gnus-newsgroup-scored))
4236                         default-score)
4237                     gnus-summary-mark-below)
4238                  ;; Don't touch sparse articles.
4239                  (not (gnus-summary-article-sparse-p number))
4240                  (not (gnus-summary-article-ancient-p number)))
4241             (setq gnus-newsgroup-unreads
4242                   (delq number gnus-newsgroup-unreads))
4243             (if gnus-newsgroup-auto-expire
4244                 (push number gnus-newsgroup-expirable)
4245               (push (cons number gnus-low-score-mark)
4246                     gnus-newsgroup-reads))))
4247
4248           (when gnus-tmp-header
4249             ;; We may have an old dummy line to output before this
4250             ;; article.
4251             (when (and gnus-tmp-dummy-line
4252                        (gnus-subject-equal
4253                         gnus-tmp-dummy-line
4254                         (mail-header-subject gnus-tmp-header)))
4255               (gnus-summary-insert-dummy-line
4256                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4257               (setq gnus-tmp-dummy-line nil))
4258
4259             ;; Compute the mark.
4260             (setq gnus-tmp-unread (gnus-article-mark number))
4261
4262             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4263                                   gnus-tmp-header gnus-tmp-level)
4264                   gnus-newsgroup-data)
4265
4266             ;; Actually insert the line.
4267             (setq
4268              gnus-tmp-subject-or-nil
4269              (cond
4270               ((and gnus-thread-ignore-subject
4271                     gnus-tmp-prev-subject
4272                     (not (inline (gnus-subject-equal
4273                                   gnus-tmp-prev-subject subject))))
4274                subject)
4275               ((zerop gnus-tmp-level)
4276                (if (and (eq gnus-summary-make-false-root 'empty)
4277                         (memq number gnus-tmp-gathered)
4278                         gnus-tmp-prev-subject
4279                         (inline (gnus-subject-equal
4280                                  gnus-tmp-prev-subject subject)))
4281                    gnus-summary-same-subject
4282                  subject))
4283               (t gnus-summary-same-subject)))
4284             (if (and (eq gnus-summary-make-false-root 'adopt)
4285                      (= gnus-tmp-level 1)
4286                      (memq number gnus-tmp-gathered))
4287                 (setq gnus-tmp-opening-bracket ?\<
4288                       gnus-tmp-closing-bracket ?\>)
4289               (setq gnus-tmp-opening-bracket ?\[
4290                     gnus-tmp-closing-bracket ?\]))
4291             (setq
4292              gnus-tmp-indentation
4293              (aref gnus-thread-indent-array gnus-tmp-level)
4294              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4295              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4296                                 gnus-summary-default-score 0)
4297              gnus-tmp-score-char
4298              (if (or (null gnus-summary-default-score)
4299                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4300                          gnus-summary-zcore-fuzz))
4301                  ?  ;Whitespace
4302                (if (< gnus-tmp-score gnus-summary-default-score)
4303                    gnus-score-below-mark gnus-score-over-mark))
4304              gnus-tmp-replied
4305              (cond ((memq number gnus-newsgroup-processable)
4306                     gnus-process-mark)
4307                    ((memq number gnus-newsgroup-cached)
4308                     gnus-cached-mark)
4309                    ((memq number gnus-newsgroup-replied)
4310                     gnus-replied-mark)
4311                    ((memq number gnus-newsgroup-forwarded)
4312                     gnus-forwarded-mark)
4313                    ((memq number gnus-newsgroup-saved)
4314                     gnus-saved-mark)
4315                    (t gnus-no-mark))
4316              gnus-tmp-from (mail-header-from gnus-tmp-header)
4317              gnus-tmp-name
4318              (cond
4319               ((string-match "<[^>]+> *$" gnus-tmp-from)
4320                (setq beg-match (match-beginning 0))
4321                (or (and (string-match "^\".+\"" gnus-tmp-from)
4322                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4323                    (substring gnus-tmp-from 0 beg-match)))
4324               ((string-match "(.+)" gnus-tmp-from)
4325                (substring gnus-tmp-from
4326                           (1+ (match-beginning 0)) (1- (match-end 0))))
4327               (t gnus-tmp-from))
4328              gnus-tmp-thread-tree-header-string 
4329              (if (zerop gnus-tmp-level)
4330                  (if (cdar thread) 
4331                      gnus-sum-thread-tree-root
4332                    gnus-sum-thread-tree-single-indent)
4333                (concat (apply 'concat
4334                               (mapcar (lambda (item) 
4335                                         (if (= item 1) 
4336                                             gnus-sum-thread-tree-vertical
4337                                           gnus-sum-thread-tree-indent))
4338                                       (cdr (reverse tree-stack))))
4339                        (if (nth 1 thread) 
4340                            gnus-sum-thread-tree-leaf-with-other
4341                          gnus-sum-thread-tree-single-leaf))))
4342
4343             (when (string= gnus-tmp-name "")
4344               (setq gnus-tmp-name gnus-tmp-from))
4345             (unless (numberp gnus-tmp-lines)
4346               (setq gnus-tmp-lines -1))
4347             (when (= gnus-tmp-lines -1)
4348               (setq gnus-tmp-lines "?"))
4349             (gnus-put-text-property
4350              (point)
4351              (progn (eval gnus-summary-line-format-spec) (point))
4352              'gnus-number number)
4353             (when gnus-visual-p
4354               (forward-line -1)
4355               (gnus-run-hooks 'gnus-summary-update-hook)
4356               (forward-line 1))
4357
4358             (setq gnus-tmp-prev-subject subject)))
4359
4360         (when (nth 1 thread)
4361           (push (list (max 0 gnus-tmp-level) 
4362                       (copy-list tree-stack)
4363                       (nthcdr 1 thread))
4364                 stack))        
4365         (push (if (nth 1 thread) 1 0) tree-stack)
4366         (incf gnus-tmp-level)
4367         (setq threads (if thread-end nil (cdar thread)))
4368         (unless threads
4369           (setq gnus-tmp-level 0)))))
4370   (gnus-message 7 "Generating summary...done"))
4371
4372 (defun gnus-summary-prepare-unthreaded (headers)
4373   "Generate an unthreaded summary buffer based on HEADERS."
4374   (let (header number mark)
4375
4376     (beginning-of-line)
4377
4378     (while headers
4379       ;; We may have to root out some bad articles...
4380       (when (memq (setq number (mail-header-number
4381                                 (setq header (pop headers))))
4382                   gnus-newsgroup-limit)
4383         ;; Mark article as read when it has a low score.
4384         (when (and gnus-summary-mark-below
4385                    (< (or (cdr (assq number gnus-newsgroup-scored))
4386                           gnus-summary-default-score 0)
4387                       gnus-summary-mark-below)
4388                    (not (gnus-summary-article-ancient-p number)))
4389           (setq gnus-newsgroup-unreads
4390                 (delq number gnus-newsgroup-unreads))
4391           (if gnus-newsgroup-auto-expire
4392               (push number gnus-newsgroup-expirable)
4393             (push (cons number gnus-low-score-mark)
4394                   gnus-newsgroup-reads)))
4395
4396         (setq mark (gnus-article-mark number))
4397         (push (gnus-data-make number mark (1+ (point)) header 0)
4398               gnus-newsgroup-data)
4399         (gnus-summary-insert-line
4400          header 0 number
4401          mark (memq number gnus-newsgroup-replied)
4402          (memq number gnus-newsgroup-expirable)
4403          (mail-header-subject header) nil
4404          (cdr (assq number gnus-newsgroup-scored))
4405          (memq number gnus-newsgroup-processable))))))
4406
4407 (defun gnus-summary-remove-list-identifiers ()
4408   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4409   (let ((regexp (if (consp gnus-list-identifiers)
4410                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4411                   gnus-list-identifiers))
4412         changed subject)
4413     (when regexp
4414       (dolist (header gnus-newsgroup-headers)
4415         (setq subject (mail-header-subject header)
4416               changed nil)
4417         (while (string-match
4418                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4419                 subject)
4420           (setq subject
4421                 (concat (substring subject 0 (match-beginning 2))
4422                         (substring subject (match-end 0)))
4423                 changed t))
4424         (when (and changed
4425                    (string-match
4426                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4427           (setq subject
4428                 (concat (substring subject 0 (match-beginning 1))
4429                         (substring subject (match-end 1)))))
4430         (when changed
4431           (mail-header-set-subject header subject))))))
4432
4433 (defun gnus-fetch-headers (articles)
4434   "Fetch headers of ARTICLES."
4435   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4436     (gnus-message 5 "Fetching headers for %s..." name)
4437     (prog1
4438         (if (eq 'nov
4439                 (setq gnus-headers-retrieved-by
4440                       (gnus-retrieve-headers
4441                        articles gnus-newsgroup-name
4442                        ;; We might want to fetch old headers, but
4443                        ;; not if there is only 1 article.
4444                        (and (or (and
4445                                  (not (eq gnus-fetch-old-headers 'some))
4446                                  (not (numberp gnus-fetch-old-headers)))
4447                                 (> (length articles) 1))
4448                             gnus-fetch-old-headers))))
4449             (gnus-get-newsgroup-headers-xover
4450              articles nil nil gnus-newsgroup-name t)
4451           (gnus-get-newsgroup-headers))
4452       (gnus-message 5 "Fetching headers for %s...done" name))))
4453
4454 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4455   "Select newsgroup GROUP.
4456 If READ-ALL is non-nil, all articles in the group are selected.
4457 If SELECT-ARTICLES, only select those articles from GROUP."
4458   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4459          ;;!!! Dirty hack; should be removed.
4460          (gnus-summary-ignore-duplicates
4461           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4462               t
4463             gnus-summary-ignore-duplicates))
4464          (info (nth 2 entry))
4465          articles fetched-articles cached)
4466
4467     (unless (gnus-check-server
4468              (setq gnus-current-select-method
4469                    (gnus-find-method-for-group group)))
4470       (error "Couldn't open server"))
4471
4472     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4473         (gnus-activate-group group)     ; Or we can activate it...
4474         (progn                          ; Or we bug out.
4475           (when (equal major-mode 'gnus-summary-mode)
4476             (kill-buffer (current-buffer)))
4477           (error "Couldn't activate group %s: %s"
4478                  group (gnus-status-message group))))
4479
4480     (unless (gnus-request-group group t)
4481       (when (equal major-mode 'gnus-summary-mode)
4482         (kill-buffer (current-buffer)))
4483       (error "Couldn't request group %s: %s"
4484              group (gnus-status-message group)))
4485
4486     (setq gnus-newsgroup-name group)
4487     (setq gnus-newsgroup-unselected nil)
4488     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4489     (gnus-summary-setup-default-charset)
4490
4491     ;; Adjust and set lists of article marks.
4492     (when info
4493       (gnus-adjust-marked-articles info))
4494
4495     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4496     (when (gnus-virtual-group-p group)
4497       (setq cached gnus-newsgroup-cached))
4498
4499     (setq gnus-newsgroup-unreads
4500           (gnus-set-difference
4501            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4502            gnus-newsgroup-dormant))
4503
4504     (setq gnus-newsgroup-processable nil)
4505
4506     (gnus-update-read-articles group gnus-newsgroup-unreads)
4507
4508     (if (setq articles select-articles)
4509         (setq gnus-newsgroup-unselected
4510               (gnus-sorted-intersection
4511                gnus-newsgroup-unreads
4512                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4513       (setq articles (gnus-articles-to-read group read-all)))
4514
4515     (cond
4516      ((null articles)
4517       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4518       'quit)
4519      ((eq articles 0) nil)
4520      (t
4521       ;; Init the dependencies hash table.
4522       (setq gnus-newsgroup-dependencies
4523             (gnus-make-hashtable (length articles)))
4524       (gnus-set-global-variables)
4525       ;; Retrieve the headers and read them in.
4526       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4527
4528       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4529       (when cached
4530         (setq gnus-newsgroup-cached cached))
4531
4532       ;; Suppress duplicates?
4533       (when gnus-suppress-duplicates
4534         (gnus-dup-suppress-articles))
4535
4536       ;; Set the initial limit.
4537       (setq gnus-newsgroup-limit (copy-sequence articles))
4538       ;; Remove canceled articles from the list of unread articles.
4539       (setq gnus-newsgroup-unreads
4540             (gnus-set-sorted-intersection
4541              gnus-newsgroup-unreads
4542              (setq fetched-articles
4543                    (mapcar (lambda (headers) (mail-header-number headers))
4544                            gnus-newsgroup-headers))))
4545       ;; Removed marked articles that do not exist.
4546       (gnus-update-missing-marks
4547        (gnus-sorted-complement fetched-articles articles))
4548       ;; We might want to build some more threads first.
4549       (when (and gnus-fetch-old-headers
4550                  (eq gnus-headers-retrieved-by 'nov))
4551         (if (eq gnus-fetch-old-headers 'invisible)
4552             (gnus-build-all-threads)
4553           (gnus-build-old-threads)))
4554       ;; Let the Gnus agent mark articles as read.
4555       (when gnus-agent
4556         (gnus-agent-get-undownloaded-list))
4557       ;; Remove list identifiers from subject
4558       (when gnus-list-identifiers
4559         (gnus-summary-remove-list-identifiers))
4560       ;; Check whether auto-expire is to be done in this group.
4561       (setq gnus-newsgroup-auto-expire
4562             (gnus-group-auto-expirable-p group))
4563       ;; Set up the article buffer now, if necessary.
4564       (unless gnus-single-article-buffer
4565         (gnus-article-setup-buffer))
4566       ;; First and last article in this newsgroup.
4567       (when gnus-newsgroup-headers
4568         (setq gnus-newsgroup-begin
4569               (mail-header-number (car gnus-newsgroup-headers))
4570               gnus-newsgroup-end
4571               (mail-header-number
4572                (gnus-last-element gnus-newsgroup-headers))))
4573       ;; GROUP is successfully selected.
4574       (or gnus-newsgroup-headers t)))))
4575
4576 (defun gnus-articles-to-read (group &optional read-all)
4577   "Find out what articles the user wants to read."
4578   (let* ((articles
4579           ;; Select all articles if `read-all' is non-nil, or if there
4580           ;; are no unread articles.
4581           (if (or read-all
4582                   (and (zerop (length gnus-newsgroup-marked))
4583                        (zerop (length gnus-newsgroup-unreads)))
4584                   (eq (gnus-group-find-parameter group 'display)
4585                       'all))
4586               (or
4587                (gnus-uncompress-range (gnus-active group))
4588                (gnus-cache-articles-in-group group))
4589             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4590                           (copy-sequence gnus-newsgroup-unreads))
4591                   '<)))
4592          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4593          (scored (length scored-list))
4594          (number (length articles))
4595          (marked (+ (length gnus-newsgroup-marked)
4596                     (length gnus-newsgroup-dormant)))
4597          (select
4598           (cond
4599            ((numberp read-all)
4600             read-all)
4601            (t
4602             (condition-case ()
4603                 (cond
4604                  ((and (or (<= scored marked) (= scored number))
4605                        (numberp gnus-large-newsgroup)
4606                        (> number gnus-large-newsgroup))
4607                   (let ((input
4608                          (read-string
4609                           (format
4610                            "How many articles from %s (default %d): "
4611                            (gnus-limit-string 
4612                             (gnus-group-decoded-name gnus-newsgroup-name)
4613                             35) 
4614                            number))))
4615                     (if (string-match "^[ \t]*$" input) number input)))
4616                  ((and (> scored marked) (< scored number)
4617                        (> (- scored number) 20))
4618                   (let ((input
4619                          (read-string
4620                           (format "%s %s (%d scored, %d total): "
4621                                   "How many articles from"
4622                                   (gnus-group-decoded-name group)
4623                                   scored number))))
4624                     (if (string-match "^[ \t]*$" input)
4625                         number input)))
4626                  (t number))
4627               (quit
4628                (message "Quit getting the articles to read")
4629                nil))))))
4630     (setq select (if (stringp select) (string-to-number select) select))
4631     (if (or (null select) (zerop select))
4632         select
4633       (if (and (not (zerop scored)) (<= (abs select) scored))
4634           (progn
4635             (setq articles (sort scored-list '<))
4636             (setq number (length articles)))
4637         (setq articles (copy-sequence articles)))
4638
4639       (when (< (abs select) number)
4640         (if (< select 0)
4641             ;; Select the N oldest articles.
4642             (setcdr (nthcdr (1- (abs select)) articles) nil)
4643           ;; Select the N most recent articles.
4644           (setq articles (nthcdr (- number select) articles))))
4645       (setq gnus-newsgroup-unselected
4646             (gnus-sorted-intersection
4647              gnus-newsgroup-unreads
4648              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4649       (when gnus-alter-articles-to-read-function
4650         (setq gnus-newsgroup-unreads
4651               (sort
4652                (funcall gnus-alter-articles-to-read-function
4653                         gnus-newsgroup-name gnus-newsgroup-unreads)
4654                '<)))
4655       articles)))
4656
4657 (defun gnus-killed-articles (killed articles)
4658   (let (out)
4659     (while articles
4660       (when (inline (gnus-member-of-range (car articles) killed))
4661         (push (car articles) out))
4662       (setq articles (cdr articles)))
4663     out))
4664
4665 (defun gnus-uncompress-marks (marks)
4666   "Uncompress the mark ranges in MARKS."
4667   (let ((uncompressed '(score bookmark))
4668         out)
4669     (while marks
4670       (if (memq (caar marks) uncompressed)
4671           (push (car marks) out)
4672         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4673       (setq marks (cdr marks)))
4674     out))
4675
4676 (defun gnus-adjust-marked-articles (info)
4677   "Set all article lists and remove all marks that are no longer valid."
4678   (let* ((marked-lists (gnus-info-marks info))
4679          (active (gnus-active (gnus-info-group info)))
4680          (min (car active))
4681          (max (cdr active))
4682          (types gnus-article-mark-lists)
4683          (uncompressed '(score bookmark killed))
4684          marks var articles article mark)
4685
4686     (while marked-lists
4687       (setq marks (pop marked-lists))
4688       (set (setq var (intern (format "gnus-newsgroup-%s"
4689                                      (car (rassq (setq mark (car marks))
4690                                                  types)))))
4691            (if (memq (car marks) uncompressed) (cdr marks)
4692              (gnus-uncompress-range (cdr marks))))
4693
4694       (setq articles (symbol-value var))
4695
4696       ;; All articles have to be subsets of the active articles.
4697       (cond
4698        ;; Adjust "simple" lists.
4699        ((memq mark '(tick dormant expire reply save))
4700         (while articles
4701           (when (or (< (setq article (pop articles)) min) (> article max))
4702             (set var (delq article (symbol-value var))))))
4703        ;; Adjust assocs.
4704        ((memq mark uncompressed)
4705         (when (not (listp (cdr (symbol-value var))))
4706           (set var (list (symbol-value var))))
4707         (when (not (listp (cdr articles)))
4708           (setq articles (list articles)))
4709         (while articles
4710           (when (or (not (consp (setq article (pop articles))))
4711                     (< (car article) min)
4712                     (> (car article) max))
4713             (set var (delq article (symbol-value var))))))))))
4714
4715 (defun gnus-update-missing-marks (missing)
4716   "Go through the list of MISSING articles and remove them from the mark lists."
4717   (when missing
4718     (let ((types gnus-article-mark-lists)
4719           var m)
4720       ;; Go through all types.
4721       (while types
4722         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4723         (when (symbol-value var)
4724           ;; This list has articles.  So we delete all missing articles
4725           ;; from it.
4726           (setq m missing)
4727           (while m
4728             (set var (delq (pop m) (symbol-value var)))))))))
4729
4730 (defun gnus-update-marks ()
4731   "Enter the various lists of marked articles into the newsgroup info list."
4732   (let ((types gnus-article-mark-lists)
4733         (info (gnus-get-info gnus-newsgroup-name))
4734         (uncompressed '(score bookmark killed))
4735         type list newmarked symbol delta-marks)
4736     (when info
4737       ;; Add all marks lists to the list of marks lists.
4738       (while (setq type (pop types))
4739         (setq list (symbol-value
4740                     (setq symbol
4741                           (intern (format "gnus-newsgroup-%s"
4742                                           (car type))))))
4743
4744         (when list
4745           ;; Get rid of the entries of the articles that have the
4746           ;; default score.
4747           (when (and (eq (cdr type) 'score)
4748                      gnus-save-score
4749                      list)
4750             (let* ((arts list)
4751                    (prev (cons nil list))
4752                    (all prev))
4753               (while arts
4754                 (if (or (not (consp (car arts)))
4755                         (= (cdar arts) gnus-summary-default-score))
4756                     (setcdr prev (cdr arts))
4757                   (setq prev arts))
4758                 (setq arts (cdr arts)))
4759               (setq list (cdr all)))))
4760
4761         (unless (memq (cdr type) uncompressed)
4762           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4763
4764         (when (gnus-check-backend-function
4765                'request-set-mark gnus-newsgroup-name)
4766           ;; propagate flags to server, with the following exceptions:
4767           ;; uncompressed:s are not proper flags (they are cons cells)
4768           ;; cache is a internal gnus flag
4769           ;; download are local to one gnus installation (well)
4770           ;; unsend are for nndraft groups only
4771           ;; xxx: generality of this?  this suits nnimap anyway
4772           (unless (memq (cdr type) (append '(cache download unsend)
4773                                            uncompressed))
4774             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4775                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4776                    (add (gnus-remove-from-range
4777                          (gnus-copy-sequence list) old)))
4778               (when add
4779                 (push (list add 'add (list (cdr type))) delta-marks))
4780               (when del
4781                 (push (list del 'del (list (cdr type))) delta-marks)))))
4782
4783         (when list
4784           (push (cons (cdr type) list) newmarked)))
4785
4786       (when delta-marks
4787         (unless (gnus-check-group gnus-newsgroup-name)
4788           (error "Can't open server for %s" gnus-newsgroup-name))
4789         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4790
4791       ;; Enter these new marks into the info of the group.
4792       (if (nthcdr 3 info)
4793           (setcar (nthcdr 3 info) newmarked)
4794         ;; Add the marks lists to the end of the info.
4795         (when newmarked
4796           (setcdr (nthcdr 2 info) (list newmarked))))
4797
4798       ;; Cut off the end of the info if there's nothing else there.
4799       (let ((i 5))
4800         (while (and (> i 2)
4801                     (not (nth i info)))
4802           (when (nthcdr (decf i) info)
4803             (setcdr (nthcdr i info) nil)))))))
4804
4805 (defun gnus-set-mode-line (where)
4806   "Set the mode line of the article or summary buffers.
4807 If WHERE is `summary', the summary mode line format will be used."
4808   ;; Is this mode line one we keep updated?
4809   (when (and (memq where gnus-updated-mode-lines)
4810              (symbol-value
4811               (intern (format "gnus-%s-mode-line-format-spec" where))))
4812     (let (mode-string)
4813       (save-excursion
4814         ;; We evaluate this in the summary buffer since these
4815         ;; variables are buffer-local to that buffer.
4816         (set-buffer gnus-summary-buffer)
4817         ;; We bind all these variables that are used in the `eval' form
4818         ;; below.
4819         (let* ((mformat (symbol-value
4820                          (intern
4821                           (format "gnus-%s-mode-line-format-spec" where))))
4822                (gnus-tmp-group-name (gnus-group-decoded-name 
4823                                      gnus-newsgroup-name))
4824                (gnus-tmp-article-number (or gnus-current-article 0))
4825                (gnus-tmp-unread gnus-newsgroup-unreads)
4826                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4827                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4828                (gnus-tmp-unread-and-unselected
4829                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4830                             (zerop gnus-tmp-unselected))
4831                        "")
4832                       ((zerop gnus-tmp-unselected)
4833                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4834                       (t (format "{%d(+%d) more}"
4835                                  gnus-tmp-unread-and-unticked
4836                                  gnus-tmp-unselected))))
4837                (gnus-tmp-subject
4838                 (if (and gnus-current-headers
4839                          (vectorp gnus-current-headers))
4840                     (gnus-mode-string-quote
4841                      (mail-header-subject gnus-current-headers))
4842                   ""))
4843                bufname-length max-len
4844                gnus-tmp-header);; passed as argument to any user-format-funcs
4845           (setq mode-string (eval mformat))
4846           (setq bufname-length (if (string-match "%b" mode-string)
4847                                    (- (length
4848                                        (buffer-name
4849                                         (if (eq where 'summary)
4850                                             nil
4851                                           (get-buffer gnus-article-buffer))))
4852                                       2)
4853                                  0))
4854           (setq max-len (max 4 (if gnus-mode-non-string-length
4855                                    (- (window-width)
4856                                       gnus-mode-non-string-length
4857                                       bufname-length)
4858                                  (length mode-string))))
4859           ;; We might have to chop a bit of the string off...
4860           (when (> (length mode-string) max-len)
4861             (setq mode-string
4862                   (concat (truncate-string-to-width mode-string (- max-len 3))
4863                           "...")))
4864           ;; Pad the mode string a bit.
4865           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4866       ;; Update the mode line.
4867       (setq mode-line-buffer-identification
4868             (gnus-mode-line-buffer-identification (list mode-string)))
4869       (set-buffer-modified-p t))))
4870
4871 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4872   "Go through the HEADERS list and add all Xrefs to a hash table.
4873 The resulting hash table is returned, or nil if no Xrefs were found."
4874   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4875          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4876          (xref-hashtb (gnus-make-hashtable))
4877          start group entry number xrefs header)
4878     (while headers
4879       (setq header (pop headers))
4880       (when (and (setq xrefs (mail-header-xref header))
4881                  (not (memq (setq number (mail-header-number header))
4882                             unreads)))
4883         (setq start 0)
4884         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4885           (setq start (match-end 0))
4886           (setq group (if prefix
4887                           (concat prefix (substring xrefs (match-beginning 1)
4888                                                     (match-end 1)))
4889                         (substring xrefs (match-beginning 1) (match-end 1))))
4890           (setq number
4891                 (string-to-int (substring xrefs (match-beginning 2)
4892                                           (match-end 2))))
4893           (if (setq entry (gnus-gethash group xref-hashtb))
4894               (setcdr entry (cons number (cdr entry)))
4895             (gnus-sethash group (cons number nil) xref-hashtb)))))
4896     (and start xref-hashtb)))
4897
4898 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4899   "Look through all the headers and mark the Xrefs as read."
4900   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4901         name entry info xref-hashtb idlist method nth4)
4902     (save-excursion
4903       (set-buffer gnus-group-buffer)
4904       (when (setq xref-hashtb
4905                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4906         (mapatoms
4907          (lambda (group)
4908            (unless (string= from-newsgroup (setq name (symbol-name group)))
4909              (setq idlist (symbol-value group))
4910              ;; Dead groups are not updated.
4911              (and (prog1
4912                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4913                             info (nth 2 entry))
4914                     (when (stringp (setq nth4 (gnus-info-method info)))
4915                       (setq nth4 (gnus-server-to-method nth4))))
4916                   ;; Only do the xrefs if the group has the same
4917                   ;; select method as the group we have just read.
4918                   (or (gnus-methods-equal-p
4919                        nth4 (gnus-find-method-for-group from-newsgroup))
4920                       virtual
4921                       (equal nth4 (setq method (gnus-find-method-for-group
4922                                                 from-newsgroup)))
4923                       (and (equal (car nth4) (car method))
4924                            (equal (nth 1 nth4) (nth 1 method))))
4925                   gnus-use-cross-reference
4926                   (or (not (eq gnus-use-cross-reference t))
4927                       virtual
4928                       ;; Only do cross-references on subscribed
4929                       ;; groups, if that is what is wanted.
4930                       (<= (gnus-info-level info) gnus-level-subscribed))
4931                   (gnus-group-make-articles-read name idlist))))
4932          xref-hashtb)))))
4933
4934 (defun gnus-compute-read-articles (group articles)
4935   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4936          (info (nth 2 entry))
4937          (active (gnus-active group))
4938          ninfo)
4939     (when entry
4940       ;; First peel off all invalid article numbers.
4941       (when active
4942         (let ((ids articles)
4943               id first)
4944           (while (setq id (pop ids))
4945             (when (and first (> id (cdr active)))
4946               ;; We'll end up in this situation in one particular
4947               ;; obscure situation.  If you re-scan a group and get
4948               ;; a new article that is cross-posted to a different
4949               ;; group that has not been re-scanned, you might get
4950               ;; crossposted article that has a higher number than
4951               ;; Gnus believes possible.  So we re-activate this
4952               ;; group as well.  This might mean doing the
4953               ;; crossposting thingy will *increase* the number
4954               ;; of articles in some groups.  Tsk, tsk.
4955               (setq active (or (gnus-activate-group group) active)))
4956             (when (or (> id (cdr active))
4957                       (< id (car active)))
4958               (setq articles (delq id articles))))))
4959       ;; If the read list is nil, we init it.
4960       (if (and active
4961                (null (gnus-info-read info))
4962                (> (car active) 1))
4963           (setq ninfo (cons 1 (1- (car active))))
4964         (setq ninfo (gnus-info-read info)))
4965       ;; Then we add the read articles to the range.
4966       (gnus-add-to-range
4967        ninfo (setq articles (sort articles '<))))))
4968
4969 (defun gnus-group-make-articles-read (group articles)
4970   "Update the info of GROUP to say that ARTICLES are read."
4971   (let* ((num 0)
4972          (entry (gnus-gethash group gnus-newsrc-hashtb))
4973          (info (nth 2 entry))
4974          (active (gnus-active group))
4975          range)
4976     (when entry
4977       (setq range (gnus-compute-read-articles group articles))
4978       (save-excursion
4979         (set-buffer gnus-group-buffer)
4980         (gnus-undo-register
4981           `(progn
4982              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4983              (gnus-info-set-read ',info ',(gnus-info-read info))
4984              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4985              (gnus-group-update-group ,group t))))
4986       ;; Add the read articles to the range.
4987       (gnus-info-set-read info range)
4988       ;; Then we have to re-compute how many unread
4989       ;; articles there are in this group.
4990       (when active
4991         (cond
4992          ((not range)
4993           (setq num (- (1+ (cdr active)) (car active))))
4994          ((not (listp (cdr range)))
4995           (setq num (- (cdr active) (- (1+ (cdr range))
4996                                        (car range)))))
4997          (t
4998           (while range
4999             (if (numberp (car range))
5000                 (setq num (1+ num))
5001               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5002             (setq range (cdr range)))
5003           (setq num (- (cdr active) num))))
5004         ;; Update the number of unread articles.
5005         (setcar entry num)
5006         ;; Update the group buffer.
5007         (gnus-group-update-group group t)))))
5008
5009 (defvar gnus-newsgroup-none-id 0)
5010
5011 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5012   (let ((cur nntp-server-buffer)
5013         (dependencies
5014          (or dependencies
5015              (save-excursion (set-buffer gnus-summary-buffer)
5016                              gnus-newsgroup-dependencies)))
5017         headers id end ref
5018         (mail-parse-charset gnus-newsgroup-charset)
5019         (mail-parse-ignored-charsets
5020          (save-excursion (condition-case nil
5021                              (set-buffer gnus-summary-buffer)
5022                            (error))
5023                          gnus-newsgroup-ignored-charsets)))
5024     (save-excursion
5025       (set-buffer nntp-server-buffer)
5026       ;; Translate all TAB characters into SPACE characters.
5027       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5028       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5029       (gnus-run-hooks 'gnus-parse-headers-hook)
5030       (let ((case-fold-search t)
5031             in-reply-to header p lines chars)
5032         (goto-char (point-min))
5033         ;; Search to the beginning of the next header.  Error messages
5034         ;; do not begin with 2 or 3.
5035         (while (re-search-forward "^[23][0-9]+ " nil t)
5036           (setq id nil
5037                 ref nil)
5038           ;; This implementation of this function, with nine
5039           ;; search-forwards instead of the one re-search-forward and
5040           ;; a case (which basically was the old function) is actually
5041           ;; about twice as fast, even though it looks messier.  You
5042           ;; can't have everything, I guess.  Speed and elegance
5043           ;; doesn't always go hand in hand.
5044           (setq
5045            header
5046            (vector
5047             ;; Number.
5048             (prog1
5049                 (read cur)
5050               (end-of-line)
5051               (setq p (point))
5052               (narrow-to-region (point)
5053                                 (or (and (search-forward "\n.\n" nil t)
5054                                          (- (point) 2))
5055                                     (point))))
5056             ;; Subject.
5057             (progn
5058               (goto-char p)
5059               (if (search-forward "\nsubject:" nil t)
5060                   (funcall gnus-decode-encoded-word-function
5061                            (nnheader-header-value))
5062                 "(none)"))
5063             ;; From.
5064             (progn
5065               (goto-char p)
5066               (if (search-forward "\nfrom:" nil t)
5067                   (funcall gnus-decode-encoded-word-function
5068                            (nnheader-header-value))
5069                 "(nobody)"))
5070             ;; Date.
5071             (progn
5072               (goto-char p)
5073               (if (search-forward "\ndate:" nil t)
5074                   (nnheader-header-value) ""))
5075             ;; Message-ID.
5076             (progn
5077               (goto-char p)
5078               (setq id (if (re-search-forward
5079                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5080                            ;; We do it this way to make sure the Message-ID
5081                            ;; is (somewhat) syntactically valid.
5082                            (buffer-substring (match-beginning 1)
5083                                              (match-end 1))
5084                          ;; If there was no message-id, we just fake one
5085                          ;; to make subsequent routines simpler.
5086                          (nnheader-generate-fake-message-id))))
5087             ;; References.
5088             (progn
5089               (goto-char p)
5090               (if (search-forward "\nreferences:" nil t)
5091                   (progn
5092                     (setq end (point))
5093                     (prog1
5094                         (nnheader-header-value)
5095                       (setq ref
5096                             (buffer-substring
5097                              (progn
5098                                (end-of-line)
5099                                (search-backward ">" end t)
5100                                (1+ (point)))
5101                              (progn
5102                                (search-backward "<" end t)
5103                                (point))))))
5104                 ;; Get the references from the in-reply-to header if there
5105                 ;; were no references and the in-reply-to header looks
5106                 ;; promising.
5107                 (if (and (search-forward "\nin-reply-to:" nil t)
5108                          (setq in-reply-to (nnheader-header-value))
5109                          (string-match "<[^>]+>" in-reply-to))
5110                     (let (ref2)
5111                       (setq ref (substring in-reply-to (match-beginning 0)
5112                                            (match-end 0)))
5113                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5114                         (setq ref2 (substring in-reply-to (match-beginning 0)
5115                                               (match-end 0)))
5116                         (when (> (length ref2) (length ref))
5117                           (setq ref ref2)))
5118                       ref)
5119                   (setq ref nil))))
5120             ;; Chars.
5121             (progn
5122               (goto-char p)
5123               (if (search-forward "\nchars: " nil t)
5124                   (if (numberp (setq chars (ignore-errors (read cur))))
5125                       chars -1)
5126                 -1))
5127             ;; Lines.
5128             (progn
5129               (goto-char p)
5130               (if (search-forward "\nlines: " nil t)
5131                   (if (numberp (setq lines (ignore-errors (read cur))))
5132                       lines -1)
5133                 -1))
5134             ;; Xref.
5135             (progn
5136               (goto-char p)
5137               (and (search-forward "\nxref:" nil t)
5138                    (nnheader-header-value)))
5139             ;; Extra.
5140             (when gnus-extra-headers
5141               (let ((extra gnus-extra-headers)
5142                     out)
5143                 (while extra
5144                   (goto-char p)
5145                   (when (search-forward
5146                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5147                     (push (cons (car extra) (nnheader-header-value))
5148                           out))
5149                   (pop extra))
5150                 out))))
5151           (when (equal id ref)
5152             (setq ref nil))
5153
5154           (when gnus-alter-header-function
5155             (funcall gnus-alter-header-function header)
5156             (setq id (mail-header-id header)
5157                   ref (gnus-parent-id (mail-header-references header))))
5158
5159           (when (setq header
5160                       (gnus-dependencies-add-header
5161                        header dependencies force-new))
5162             (push header headers))
5163           (goto-char (point-max))
5164           (widen))
5165         (nreverse headers)))))
5166
5167 ;; Goes through the xover lines and returns a list of vectors
5168 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5169                                                   force-new dependencies
5170                                                   group also-fetch-heads)
5171   "Parse the news overview data in the server buffer.
5172 Return a list of headers that match SEQUENCE (see
5173 `nntp-retrieve-headers')."
5174   ;; Get the Xref when the users reads the articles since most/some
5175   ;; NNTP servers do not include Xrefs when using XOVER.
5176   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5177   (let ((mail-parse-charset gnus-newsgroup-charset)
5178         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5179         (cur nntp-server-buffer)
5180         (dependencies (or dependencies gnus-newsgroup-dependencies))
5181         (allp (cond
5182                ((eq gnus-read-all-available-headers t)
5183                 t)
5184                ((stringp gnus-read-all-available-headers)
5185                 (string-match gnus-read-all-available-headers group))
5186                (t
5187                 nil)))
5188         number headers header)
5189     (save-excursion
5190       (set-buffer nntp-server-buffer)
5191       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5192       ;; Allow the user to mangle the headers before parsing them.
5193       (gnus-run-hooks 'gnus-parse-headers-hook)
5194       (goto-char (point-min))
5195       (while (not (eobp))
5196         (condition-case ()
5197             (while (and (or sequence allp)
5198                         (not (eobp)))
5199               (setq number (read cur))
5200               (when (not allp)
5201                 (while (and sequence
5202                             (< (car sequence) number))
5203                   (setq sequence (cdr sequence))))
5204               (when (and (or allp
5205                              (and sequence
5206                                   (eq number (car sequence))))
5207                          (progn
5208                            (setq sequence (cdr sequence))
5209                            (setq header (inline
5210                                           (gnus-nov-parse-line
5211                                            number dependencies force-new)))))
5212                 (push header headers))
5213               (forward-line 1))
5214           (error
5215            (gnus-error 4 "Strange nov line (%d)"
5216                        (count-lines (point-min) (point)))))
5217         (forward-line 1))
5218       ;; A common bug in inn is that if you have posted an article and
5219       ;; then retrieves the active file, it will answer correctly --
5220       ;; the new article is included.  However, a NOV entry for the
5221       ;; article may not have been generated yet, so this may fail.
5222       ;; We work around this problem by retrieving the last few
5223       ;; headers using HEAD.
5224       (if (or (not also-fetch-heads)
5225               (not sequence))
5226           ;; We (probably) got all the headers.
5227           (nreverse headers)
5228         (let ((gnus-nov-is-evil t))
5229           (nconc
5230            (nreverse headers)
5231            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5232              (gnus-get-newsgroup-headers))))))))
5233
5234 (defun gnus-article-get-xrefs ()
5235   "Fill in the Xref value in `gnus-current-headers', if necessary.
5236 This is meant to be called in `gnus-article-internal-prepare-hook'."
5237   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5238                                  gnus-current-headers)))
5239     (or (not gnus-use-cross-reference)
5240         (not headers)
5241         (and (mail-header-xref headers)
5242              (not (string= (mail-header-xref headers) "")))
5243         (let ((case-fold-search t)
5244               xref)
5245           (save-restriction
5246             (nnheader-narrow-to-headers)
5247             (goto-char (point-min))
5248             (when (or (and (not (eobp))
5249                            (eq (downcase (char-after)) ?x)
5250                            (looking-at "Xref:"))
5251                       (search-forward "\nXref:" nil t))
5252               (goto-char (1+ (match-end 0)))
5253               (setq xref (buffer-substring (point)
5254                                            (progn (end-of-line) (point))))
5255               (mail-header-set-xref headers xref)))))))
5256
5257 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5258   "Find article ID and insert the summary line for that article.
5259 OLD-HEADER can either be a header or a line number to insert
5260 the subject line on."
5261   (let* ((line (and (numberp old-header) old-header))
5262          (old-header (and (vectorp old-header) old-header))
5263          (header (cond ((and old-header use-old-header)
5264                         old-header)
5265                        ((and (numberp id)
5266                              (gnus-number-to-header id))
5267                         (gnus-number-to-header id))
5268                        (t
5269                         (gnus-read-header id))))
5270          (number (and (numberp id) id))
5271          d)
5272     (when header
5273       ;; Rebuild the thread that this article is part of and go to the
5274       ;; article we have fetched.
5275       (when (and (not gnus-show-threads)
5276                  old-header)
5277         (when (and number
5278                    (setq d (gnus-data-find (mail-header-number old-header))))
5279           (goto-char (gnus-data-pos d))
5280           (gnus-data-remove
5281            number
5282            (- (gnus-point-at-bol)
5283               (prog1
5284                   (1+ (gnus-point-at-eol))
5285                 (gnus-delete-line))))))
5286       (when old-header
5287         (mail-header-set-number header (mail-header-number old-header)))
5288       (setq gnus-newsgroup-sparse
5289             (delq (setq number (mail-header-number header))
5290                   gnus-newsgroup-sparse))
5291       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5292       (push number gnus-newsgroup-limit)
5293       (gnus-rebuild-thread (mail-header-id header) line)
5294       (gnus-summary-goto-subject number nil t))
5295     (when (and (numberp number)
5296                (> number 0))
5297       ;; We have to update the boundaries even if we can't fetch the
5298       ;; article if ID is a number -- so that the next `P' or `N'
5299       ;; command will fetch the previous (or next) article even
5300       ;; if the one we tried to fetch this time has been canceled.
5301       (when (> number gnus-newsgroup-end)
5302         (setq gnus-newsgroup-end number))
5303       (when (< number gnus-newsgroup-begin)
5304         (setq gnus-newsgroup-begin number))
5305       (setq gnus-newsgroup-unselected
5306             (delq number gnus-newsgroup-unselected)))
5307     ;; Report back a success?
5308     (and header (mail-header-number header))))
5309
5310 ;;; Process/prefix in the summary buffer
5311
5312 (defun gnus-summary-work-articles (n)
5313   "Return a list of articles to be worked upon.
5314 The prefix argument, the list of process marked articles, and the
5315 current article will be taken into consideration."
5316   (save-excursion
5317     (set-buffer gnus-summary-buffer)
5318     (cond
5319      (n
5320       ;; A numerical prefix has been given.
5321       (setq n (prefix-numeric-value n))
5322       (let ((backward (< n 0))
5323             (n (abs (prefix-numeric-value n)))
5324             articles article)
5325         (save-excursion
5326           (while
5327               (and (> n 0)
5328                    (push (setq article (gnus-summary-article-number))
5329                          articles)
5330                    (if backward
5331                        (gnus-summary-find-prev nil article)
5332                      (gnus-summary-find-next nil article)))
5333             (decf n)))
5334         (nreverse articles)))
5335      ((and (gnus-region-active-p) (mark))
5336       (message "region active")
5337       ;; Work on the region between point and mark.
5338       (let ((max (max (point) (mark)))
5339             articles article)
5340         (save-excursion
5341           (goto-char (min (min (point) (mark))))
5342           (while
5343               (and
5344                (push (setq article (gnus-summary-article-number)) articles)
5345                (gnus-summary-find-next nil article)
5346                (< (point) max)))
5347           (nreverse articles))))
5348      (gnus-newsgroup-processable
5349       ;; There are process-marked articles present.
5350       ;; Save current state.
5351       (gnus-summary-save-process-mark)
5352       ;; Return the list.
5353       (reverse gnus-newsgroup-processable))
5354      (t
5355       ;; Just return the current article.
5356       (list (gnus-summary-article-number))))))
5357
5358 (defmacro gnus-summary-iterate (arg &rest forms)
5359   "Iterate over the process/prefixed articles and do FORMS.
5360 ARG is the interactive prefix given to the command.  FORMS will be
5361 executed with point over the summary line of the articles."
5362   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5363     `(let ((,articles (gnus-summary-work-articles ,arg)))
5364        (while ,articles
5365          (gnus-summary-goto-subject (car ,articles))
5366          ,@forms
5367          (pop ,articles)))))
5368
5369 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5370 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5371
5372 (defun gnus-summary-save-process-mark ()
5373   "Push the current set of process marked articles on the stack."
5374   (interactive)
5375   (push (copy-sequence gnus-newsgroup-processable)
5376         gnus-newsgroup-process-stack))
5377
5378 (defun gnus-summary-kill-process-mark ()
5379   "Push the current set of process marked articles on the stack and unmark."
5380   (interactive)
5381   (gnus-summary-save-process-mark)
5382   (gnus-summary-unmark-all-processable))
5383
5384 (defun gnus-summary-yank-process-mark ()
5385   "Pop the last process mark state off the stack and restore it."
5386   (interactive)
5387   (unless gnus-newsgroup-process-stack
5388     (error "Empty mark stack"))
5389   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5390
5391 (defun gnus-summary-process-mark-set (set)
5392   "Make SET into the current process marked articles."
5393   (gnus-summary-unmark-all-processable)
5394   (while set
5395     (gnus-summary-set-process-mark (pop set))))
5396
5397 ;;; Searching and stuff
5398
5399 (defun gnus-summary-search-group (&optional backward use-level)
5400   "Search for next unread newsgroup.
5401 If optional argument BACKWARD is non-nil, search backward instead."
5402   (save-excursion
5403     (set-buffer gnus-group-buffer)
5404     (when (gnus-group-search-forward
5405            backward nil (if use-level (gnus-group-group-level) nil))
5406       (gnus-group-group-name))))
5407
5408 (defun gnus-summary-best-group (&optional exclude-group)
5409   "Find the name of the best unread group.
5410 If EXCLUDE-GROUP, do not go to this group."
5411   (save-excursion
5412     (set-buffer gnus-group-buffer)
5413     (save-excursion
5414       (gnus-group-best-unread-group exclude-group))))
5415
5416 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5417   (if backward (gnus-summary-find-prev)
5418     (let* ((dummy (gnus-summary-article-intangible-p))
5419            (article (or article (gnus-summary-article-number)))
5420            (arts (gnus-data-find-list article))
5421            result)
5422       (when (and (not dummy)
5423                  (or (not gnus-summary-check-current)
5424                      (not unread)
5425                      (not (gnus-data-unread-p (car arts)))))
5426         (setq arts (cdr arts)))
5427       (when (setq result
5428                   (if unread
5429                       (progn
5430                         (while arts
5431                           (when (or (and undownloaded
5432                                          (eq gnus-undownloaded-mark
5433                                              (gnus-data-mark (car arts))))
5434                                     (gnus-data-unread-p (car arts)))
5435                             (setq result (car arts)
5436                                   arts nil))
5437                           (setq arts (cdr arts)))
5438                         result)
5439                     (car arts)))
5440         (goto-char (gnus-data-pos result))
5441         (gnus-data-number result)))))
5442
5443 (defun gnus-summary-find-prev (&optional unread article)
5444   (let* ((eobp (eobp))
5445          (article (or article (gnus-summary-article-number)))
5446          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5447          result)
5448     (when (and (not eobp)
5449                (or (not gnus-summary-check-current)
5450                    (not unread)
5451                    (not (gnus-data-unread-p (car arts)))))
5452       (setq arts (cdr arts)))
5453     (when (setq result
5454                 (if unread
5455                     (progn
5456                       (while arts
5457                         (when (gnus-data-unread-p (car arts))
5458                           (setq result (car arts)
5459                                 arts nil))
5460                         (setq arts (cdr arts)))
5461                       result)
5462                   (car arts)))
5463       (goto-char (gnus-data-pos result))
5464       (gnus-data-number result))))
5465
5466 (defun gnus-summary-find-subject (subject &optional unread backward article)
5467   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5468          (article (or article (gnus-summary-article-number)))
5469          (articles (gnus-data-list backward))
5470          (arts (gnus-data-find-list article articles))
5471          result)
5472     (when (or (not gnus-summary-check-current)
5473               (not unread)
5474               (not (gnus-data-unread-p (car arts))))
5475       (setq arts (cdr arts)))
5476     (while arts
5477       (and (or (not unread)
5478                (gnus-data-unread-p (car arts)))
5479            (vectorp (gnus-data-header (car arts)))
5480            (gnus-subject-equal
5481             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5482            (setq result (car arts)
5483                  arts nil))
5484       (setq arts (cdr arts)))
5485     (and result
5486          (goto-char (gnus-data-pos result))
5487          (gnus-data-number result))))
5488
5489 (defun gnus-summary-search-forward (&optional unread subject backward)
5490   "Search forward for an article.
5491 If UNREAD, look for unread articles.  If SUBJECT, look for
5492 articles with that subject.  If BACKWARD, search backward instead."
5493   (cond (subject (gnus-summary-find-subject subject unread backward))
5494         (backward (gnus-summary-find-prev unread))
5495         (t (gnus-summary-find-next unread))))
5496
5497 (defun gnus-recenter (&optional n)
5498   "Center point in window and redisplay frame.
5499 Also do horizontal recentering."
5500   (interactive "P")
5501   (when (and gnus-auto-center-summary
5502              (not (eq gnus-auto-center-summary 'vertical)))
5503     (gnus-horizontal-recenter))
5504   (recenter n))
5505
5506 (defun gnus-summary-recenter ()
5507   "Center point in the summary window.
5508 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5509 displayed, no centering will be performed."
5510   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5511   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5512   (interactive)
5513   (let* ((top (cond ((< (window-height) 4) 0)
5514                     ((< (window-height) 7) 1)
5515                     (t (if (numberp gnus-auto-center-summary)
5516                            gnus-auto-center-summary
5517                          2))))
5518          (height (1- (window-height)))
5519          (bottom (save-excursion (goto-char (point-max))
5520                                  (forward-line (- height))
5521                                  (point)))
5522          (window (get-buffer-window (current-buffer))))
5523     ;; The user has to want it.
5524     (when gnus-auto-center-summary
5525       (when (get-buffer-window gnus-article-buffer)
5526         ;; Only do recentering when the article buffer is displayed,
5527         ;; Set the window start to either `bottom', which is the biggest
5528         ;; possible valid number, or the second line from the top,
5529         ;; whichever is the least.
5530         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5531           (if (> bottom top-pos)
5532               ;; Keep the second line from the top visible
5533               (set-window-start window top-pos t)
5534             ;; Try to keep the bottom line visible; if it's partially
5535             ;; obscured, either scroll one more line to make it fully
5536             ;; visible, or revert to using TOP-POS.
5537             (save-excursion
5538               (goto-char (point-max))
5539               (forward-line -1)
5540               (let ((last-line-start (point)))
5541                 (goto-char bottom)
5542                 (set-window-start window (point) t)
5543                 (when (not (pos-visible-in-window-p last-line-start window))
5544                   (forward-line 1)
5545                   (set-window-start window (min (point) top-pos) t)))))))
5546       ;; Do horizontal recentering while we're at it.
5547       (when (and (get-buffer-window (current-buffer) t)
5548                  (not (eq gnus-auto-center-summary 'vertical)))
5549         (let ((selected (selected-window)))
5550           (select-window (get-buffer-window (current-buffer) t))
5551           (gnus-summary-position-point)
5552           (gnus-horizontal-recenter)
5553           (select-window selected))))))
5554
5555 (defun gnus-summary-jump-to-group (newsgroup)
5556   "Move point to NEWSGROUP in group mode buffer."
5557   ;; Keep update point of group mode buffer if visible.
5558   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5559       (save-window-excursion
5560         ;; Take care of tree window mode.
5561         (when (get-buffer-window gnus-group-buffer)
5562           (pop-to-buffer gnus-group-buffer))
5563         (gnus-group-jump-to-group newsgroup))
5564     (save-excursion
5565       ;; Take care of tree window mode.
5566       (if (get-buffer-window gnus-group-buffer)
5567           (pop-to-buffer gnus-group-buffer)
5568         (set-buffer gnus-group-buffer))
5569       (gnus-group-jump-to-group newsgroup))))
5570
5571 ;; This function returns a list of article numbers based on the
5572 ;; difference between the ranges of read articles in this group and
5573 ;; the range of active articles.
5574 (defun gnus-list-of-unread-articles (group)
5575   (let* ((read (gnus-info-read (gnus-get-info group)))
5576          (active (or (gnus-active group) (gnus-activate-group group)))
5577          (last (cdr active))
5578          first nlast unread)
5579     ;; If none are read, then all are unread.
5580     (if (not read)
5581         (setq first (car active))
5582       ;; If the range of read articles is a single range, then the
5583       ;; first unread article is the article after the last read
5584       ;; article.  Sounds logical, doesn't it?
5585       (if (and (not (listp (cdr read)))
5586                (or (< (car read) (car active))
5587                    (progn (setq read (list read))
5588                           nil)))
5589           (setq first (max (car active) (1+ (cdr read))))
5590         ;; `read' is a list of ranges.
5591         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5592                                   (caar read)))
5593                   1)
5594           (setq first (car active)))
5595         (while read
5596           (when first
5597             (while (< first nlast)
5598               (push first unread)
5599               (setq first (1+ first))))
5600           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5601           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5602           (setq read (cdr read)))))
5603     ;; And add the last unread articles.
5604     (while (<= first last)
5605       (push first unread)
5606       (setq first (1+ first)))
5607     ;; Return the list of unread articles.
5608     (delq 0 (nreverse unread))))
5609
5610 (defun gnus-list-of-read-articles (group)
5611   "Return a list of unread, unticked and non-dormant articles."
5612   (let* ((info (gnus-get-info group))
5613          (marked (gnus-info-marks info))
5614          (active (gnus-active group)))
5615     (and info active
5616          (gnus-set-difference
5617           (gnus-sorted-complement
5618            (gnus-uncompress-range active)
5619            (gnus-list-of-unread-articles group))
5620           (append
5621            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5622            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5623
5624 ;; Various summary commands
5625
5626 (defun gnus-summary-select-article-buffer ()
5627   "Reconfigure windows to show article buffer."
5628   (interactive)
5629   (if (not (gnus-buffer-live-p gnus-article-buffer))
5630       (error "There is no article buffer for this summary buffer")
5631     (gnus-configure-windows 'article)
5632     (select-window (get-buffer-window gnus-article-buffer))))
5633
5634 (defun gnus-summary-universal-argument (arg)
5635   "Perform any operation on all articles that are process/prefixed."
5636   (interactive "P")
5637   (let ((articles (gnus-summary-work-articles arg))
5638         func article)
5639     (if (eq
5640          (setq
5641           func
5642           (key-binding
5643            (read-key-sequence
5644             (substitute-command-keys
5645              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5646          'undefined)
5647         (gnus-error 1 "Undefined key")
5648       (save-excursion
5649         (while articles
5650           (gnus-summary-goto-subject (setq article (pop articles)))
5651           (let (gnus-newsgroup-processable)
5652             (command-execute func))
5653           (gnus-summary-remove-process-mark article)))))
5654   (gnus-summary-position-point))
5655
5656 (defun gnus-summary-toggle-truncation (&optional arg)
5657   "Toggle truncation of summary lines.
5658 With arg, turn line truncation on iff arg is positive."
5659   (interactive "P")
5660   (setq truncate-lines
5661         (if (null arg) (not truncate-lines)
5662           (> (prefix-numeric-value arg) 0)))
5663   (redraw-display))
5664
5665 (defun gnus-summary-reselect-current-group (&optional all rescan)
5666   "Exit and then reselect the current newsgroup.
5667 The prefix argument ALL means to select all articles."
5668   (interactive "P")
5669   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5670     (error "Ephemeral groups can't be reselected"))
5671   (let ((current-subject (gnus-summary-article-number))
5672         (group gnus-newsgroup-name))
5673     (setq gnus-newsgroup-begin nil)
5674     (gnus-summary-exit)
5675     ;; We have to adjust the point of group mode buffer because
5676     ;; point was moved to the next unread newsgroup by exiting.
5677     (gnus-summary-jump-to-group group)
5678     (when rescan
5679       (save-excursion
5680         (gnus-group-get-new-news-this-group 1)))
5681     (gnus-group-read-group all t)
5682     (gnus-summary-goto-subject current-subject nil t)))
5683
5684 (defun gnus-summary-rescan-group (&optional all)
5685   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5686   (interactive "P")
5687   (gnus-summary-reselect-current-group all t))
5688
5689 (defun gnus-summary-update-info (&optional non-destructive)
5690   (save-excursion
5691     (let ((group gnus-newsgroup-name))
5692       (when group
5693         (when gnus-newsgroup-kill-headers
5694           (setq gnus-newsgroup-killed
5695                 (gnus-compress-sequence
5696                  (nconc
5697                   (gnus-set-sorted-intersection
5698                    (gnus-uncompress-range gnus-newsgroup-killed)
5699                    (setq gnus-newsgroup-unselected
5700                          (sort gnus-newsgroup-unselected '<)))
5701                   (setq gnus-newsgroup-unreads
5702                         (sort gnus-newsgroup-unreads '<)))
5703                  t)))
5704         (unless (listp (cdr gnus-newsgroup-killed))
5705           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5706         (let ((headers gnus-newsgroup-headers))
5707           ;; Set the new ranges of read articles.
5708           (save-excursion
5709             (set-buffer gnus-group-buffer)
5710             (gnus-undo-force-boundary))
5711           (gnus-update-read-articles
5712            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5713           ;; Set the current article marks.
5714           (let ((gnus-newsgroup-scored
5715                  (if (and (not gnus-save-score)
5716                           (not non-destructive))
5717                      nil
5718                    gnus-newsgroup-scored)))
5719             (save-excursion
5720               (gnus-update-marks)))
5721           ;; Do the cross-ref thing.
5722           (when gnus-use-cross-reference
5723             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5724           ;; Do not switch windows but change the buffer to work.
5725           (set-buffer gnus-group-buffer)
5726           (unless (gnus-ephemeral-group-p group)
5727             (gnus-group-update-group group)))))))
5728
5729 (defun gnus-summary-save-newsrc (&optional force)
5730   "Save the current number of read/marked articles in the dribble buffer.
5731 The dribble buffer will then be saved.
5732 If FORCE (the prefix), also save the .newsrc file(s)."
5733   (interactive "P")
5734   (gnus-summary-update-info t)
5735   (if force
5736       (gnus-save-newsrc-file)
5737     (gnus-dribble-save)))
5738
5739 (defun gnus-summary-exit (&optional temporary)
5740   "Exit reading current newsgroup, and then return to group selection mode.
5741 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5742   (interactive)
5743   (gnus-set-global-variables)
5744   (when (gnus-buffer-live-p gnus-article-buffer)
5745     (save-excursion
5746       (set-buffer gnus-article-buffer)
5747       (mm-destroy-parts gnus-article-mime-handles)
5748       ;; Set it to nil for safety reason.
5749       (setq gnus-article-mime-handle-alist nil)
5750       (setq gnus-article-mime-handles nil)))
5751   (gnus-kill-save-kill-buffer)
5752   (gnus-async-halt-prefetch)
5753   (let* ((group gnus-newsgroup-name)
5754          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5755          (mode major-mode)
5756          (group-point nil)
5757          (buf (current-buffer)))
5758     (unless quit-config
5759       ;; Do adaptive scoring, and possibly save score files.
5760       (when gnus-newsgroup-adaptive
5761         (gnus-score-adaptive))
5762       (when gnus-use-scoring
5763         (gnus-score-save)))
5764     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5765     ;; If we have several article buffers, we kill them at exit.
5766     (unless gnus-single-article-buffer
5767       (gnus-kill-buffer gnus-original-article-buffer)
5768       (setq gnus-article-current nil))
5769     (when gnus-use-cache
5770       (gnus-cache-possibly-remove-articles)
5771       (gnus-cache-save-buffers))
5772     (gnus-async-prefetch-remove-group group)
5773     (when gnus-suppress-duplicates
5774       (gnus-dup-enter-articles))
5775     (when gnus-use-trees
5776       (gnus-tree-close group))
5777     (when gnus-use-cache
5778       (gnus-cache-write-active))
5779     ;; Remove entries for this group.
5780     (nnmail-purge-split-history (gnus-group-real-name group))
5781     ;; Make all changes in this group permanent.
5782     (unless quit-config
5783       (gnus-run-hooks 'gnus-exit-group-hook)
5784       (gnus-summary-update-info))
5785     (gnus-close-group group)
5786     ;; Make sure where we were, and go to next newsgroup.
5787     (set-buffer gnus-group-buffer)
5788     (unless quit-config
5789       (gnus-group-jump-to-group group))
5790     (gnus-run-hooks 'gnus-summary-exit-hook)
5791     (unless (or quit-config
5792                 ;; If this group has disappeared from the summary
5793                 ;; buffer, don't skip forwards.
5794                 (not (string= group (gnus-group-group-name))))
5795       (gnus-group-next-unread-group 1))
5796     (setq group-point (point))
5797     (if temporary
5798         nil                             ;Nothing to do.
5799       ;; If we have several article buffers, we kill them at exit.
5800       (unless gnus-single-article-buffer
5801         (gnus-kill-buffer gnus-article-buffer)
5802         (gnus-kill-buffer gnus-original-article-buffer)
5803         (setq gnus-article-current nil))
5804       (set-buffer buf)
5805       (if (not gnus-kill-summary-on-exit)
5806           (progn
5807             (gnus-deaden-summary)
5808             (setq mode nil))
5809         ;; We set all buffer-local variables to nil.  It is unclear why
5810         ;; this is needed, but if we don't, buffer-local variables are
5811         ;; not garbage-collected, it seems.  This would the lead to en
5812         ;; ever-growing Emacs.
5813         (gnus-summary-clear-local-variables)
5814         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5815           (gnus-summary-clear-local-variables))
5816         (when (get-buffer gnus-article-buffer)
5817           (bury-buffer gnus-article-buffer))
5818         ;; We clear the global counterparts of the buffer-local
5819         ;; variables as well, just to be on the safe side.
5820         (set-buffer gnus-group-buffer)
5821         (gnus-summary-clear-local-variables)
5822         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5823           (gnus-summary-clear-local-variables)))
5824       (setq gnus-current-select-method gnus-select-method)
5825       (pop-to-buffer gnus-group-buffer)
5826       (if (not quit-config)
5827           (progn
5828             (goto-char group-point)
5829             (gnus-configure-windows 'group 'force))
5830         (gnus-handle-ephemeral-exit quit-config))
5831       ;; Return to group mode buffer.
5832       (when (eq mode 'gnus-summary-mode)
5833         (gnus-kill-buffer buf))
5834       ;; Clear the current group name.
5835       (unless quit-config
5836         (setq gnus-newsgroup-name nil)))))
5837
5838 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5839 (defun gnus-summary-exit-no-update (&optional no-questions)
5840   "Quit reading current newsgroup without updating read article info."
5841   (interactive)
5842   (let* ((group gnus-newsgroup-name)
5843          (quit-config (gnus-group-quit-config group)))
5844     (when (or no-questions
5845               gnus-expert-user
5846               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5847       (gnus-async-halt-prefetch)
5848       (mapcar 'funcall
5849               (delq 'gnus-summary-expire-articles
5850                     (copy-sequence gnus-summary-prepare-exit-hook)))
5851       (when (gnus-buffer-live-p gnus-article-buffer)
5852         (save-excursion
5853           (set-buffer gnus-article-buffer)
5854           (mm-destroy-parts gnus-article-mime-handles)
5855           ;; Set it to nil for safety reason.
5856           (setq gnus-article-mime-handle-alist nil)
5857           (setq gnus-article-mime-handles nil)))
5858       ;; If we have several article buffers, we kill them at exit.
5859       (unless gnus-single-article-buffer
5860         (gnus-kill-buffer gnus-article-buffer)
5861         (gnus-kill-buffer gnus-original-article-buffer)
5862         (setq gnus-article-current nil))
5863       (if (not gnus-kill-summary-on-exit)
5864           (gnus-deaden-summary)
5865         (gnus-close-group group)
5866         (gnus-summary-clear-local-variables)
5867         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5868           (gnus-summary-clear-local-variables))
5869         (set-buffer gnus-group-buffer)
5870         (gnus-summary-clear-local-variables)
5871         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5872           (gnus-summary-clear-local-variables))
5873         (when (get-buffer gnus-summary-buffer)
5874           (kill-buffer gnus-summary-buffer)))
5875       (unless gnus-single-article-buffer
5876         (setq gnus-article-current nil))
5877       (when gnus-use-trees
5878         (gnus-tree-close group))
5879       (gnus-async-prefetch-remove-group group)
5880       (when (get-buffer gnus-article-buffer)
5881         (bury-buffer gnus-article-buffer))
5882       ;; Return to the group buffer.
5883       (gnus-configure-windows 'group 'force)
5884       ;; Clear the current group name.
5885       (setq gnus-newsgroup-name nil)
5886       (when (equal (gnus-group-group-name) group)
5887         (gnus-group-next-unread-group 1))
5888       (when quit-config
5889         (gnus-handle-ephemeral-exit quit-config)))))
5890
5891 (defun gnus-handle-ephemeral-exit (quit-config)
5892   "Handle movement when leaving an ephemeral group.
5893 The state which existed when entering the ephemeral is reset."
5894   (if (not (buffer-name (car quit-config)))
5895       (gnus-configure-windows 'group 'force)
5896     (set-buffer (car quit-config))
5897     (cond ((eq major-mode 'gnus-summary-mode)
5898            (gnus-set-global-variables))
5899           ((eq major-mode 'gnus-article-mode)
5900            (save-excursion
5901              ;; The `gnus-summary-buffer' variable may point
5902              ;; to the old summary buffer when using a single
5903              ;; article buffer.
5904              (unless (gnus-buffer-live-p gnus-summary-buffer)
5905                (set-buffer gnus-group-buffer))
5906              (set-buffer gnus-summary-buffer)
5907              (gnus-set-global-variables))))
5908     (if (or (eq (cdr quit-config) 'article)
5909             (eq (cdr quit-config) 'pick))
5910         (progn
5911           ;; The current article may be from the ephemeral group
5912           ;; thus it is best that we reload this article
5913           (gnus-summary-show-article)
5914           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5915               (gnus-configure-windows 'pick 'force)
5916             (gnus-configure-windows (cdr quit-config) 'force)))
5917       (gnus-configure-windows (cdr quit-config) 'force))
5918     (when (eq major-mode 'gnus-summary-mode)
5919       (gnus-summary-next-subject 1 nil t)
5920       (gnus-summary-recenter)
5921       (gnus-summary-position-point))))
5922
5923 ;;; Dead summaries.
5924
5925 (defvar gnus-dead-summary-mode-map nil)
5926
5927 (unless gnus-dead-summary-mode-map
5928   (setq gnus-dead-summary-mode-map (make-keymap))
5929   (suppress-keymap gnus-dead-summary-mode-map)
5930   (substitute-key-definition
5931    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5932   (let ((keys '("\C-d" "\r" "\177" [delete])))
5933     (while keys
5934       (define-key gnus-dead-summary-mode-map
5935         (pop keys) 'gnus-summary-wake-up-the-dead))))
5936
5937 (defvar gnus-dead-summary-mode nil
5938   "Minor mode for Gnus summary buffers.")
5939
5940 (defun gnus-dead-summary-mode (&optional arg)
5941   "Minor mode for Gnus summary buffers."
5942   (interactive "P")
5943   (when (eq major-mode 'gnus-summary-mode)
5944     (make-local-variable 'gnus-dead-summary-mode)
5945     (setq gnus-dead-summary-mode
5946           (if (null arg) (not gnus-dead-summary-mode)
5947             (> (prefix-numeric-value arg) 0)))
5948     (when gnus-dead-summary-mode
5949       (gnus-add-minor-mode
5950        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5951
5952 (defun gnus-deaden-summary ()
5953   "Make the current summary buffer into a dead summary buffer."
5954   ;; Kill any previous dead summary buffer.
5955   (when (and gnus-dead-summary
5956              (buffer-name gnus-dead-summary))
5957     (save-excursion
5958       (set-buffer gnus-dead-summary)
5959       (when gnus-dead-summary-mode
5960         (kill-buffer (current-buffer)))))
5961   ;; Make this the current dead summary.
5962   (setq gnus-dead-summary (current-buffer))
5963   (gnus-dead-summary-mode 1)
5964   (let ((name (buffer-name)))
5965     (when (string-match "Summary" name)
5966       (rename-buffer
5967        (concat (substring name 0 (match-beginning 0)) "Dead "
5968                (substring name (match-beginning 0)))
5969        t)
5970       (bury-buffer))))
5971
5972 (defun gnus-kill-or-deaden-summary (buffer)
5973   "Kill or deaden the summary BUFFER."
5974   (save-excursion
5975     (when (and (buffer-name buffer)
5976                (not gnus-single-article-buffer))
5977       (save-excursion
5978         (set-buffer buffer)
5979         (gnus-kill-buffer gnus-article-buffer)
5980         (gnus-kill-buffer gnus-original-article-buffer)))
5981     (cond (gnus-kill-summary-on-exit
5982            (when (and gnus-use-trees
5983                       (gnus-buffer-exists-p buffer))
5984              (save-excursion
5985                (set-buffer buffer)
5986                (gnus-tree-close gnus-newsgroup-name)))
5987            (gnus-kill-buffer buffer))
5988           ((gnus-buffer-exists-p buffer)
5989            (save-excursion
5990              (set-buffer buffer)
5991              (gnus-deaden-summary))))))
5992
5993 (defun gnus-summary-wake-up-the-dead (&rest args)
5994   "Wake up the dead summary buffer."
5995   (interactive)
5996   (gnus-dead-summary-mode -1)
5997   (let ((name (buffer-name)))
5998     (when (string-match "Dead " name)
5999       (rename-buffer
6000        (concat (substring name 0 (match-beginning 0))
6001                (substring name (match-end 0)))
6002        t)))
6003   (gnus-message 3 "This dead summary is now alive again"))
6004
6005 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6006 (defun gnus-summary-fetch-faq (&optional faq-dir)
6007   "Fetch the FAQ for the current group.
6008 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6009 in."
6010   (interactive
6011    (list
6012     (when current-prefix-arg
6013       (completing-read
6014        "Faq dir: " (and (listp gnus-group-faq-directory)
6015                         (mapcar (lambda (file) (list file))
6016                                 gnus-group-faq-directory))))))
6017   (let (gnus-faq-buffer)
6018     (when (setq gnus-faq-buffer
6019                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6020       (gnus-configure-windows 'summary-faq))))
6021
6022 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6023 (defun gnus-summary-describe-group (&optional force)
6024   "Describe the current newsgroup."
6025   (interactive "P")
6026   (gnus-group-describe-group force gnus-newsgroup-name))
6027
6028 (defun gnus-summary-describe-briefly ()
6029   "Describe summary mode commands briefly."
6030   (interactive)
6031   (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")))
6032
6033 ;; Walking around group mode buffer from summary mode.
6034
6035 (defun gnus-summary-next-group (&optional no-article target-group backward)
6036   "Exit current newsgroup and then select next unread newsgroup.
6037 If prefix argument NO-ARTICLE is non-nil, no article is selected
6038 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6039 previous group instead."
6040   (interactive "P")
6041   ;; Stop pre-fetching.
6042   (gnus-async-halt-prefetch)
6043   (let ((current-group gnus-newsgroup-name)
6044         (current-buffer (current-buffer))
6045         entered)
6046     ;; First we semi-exit this group to update Xrefs and all variables.
6047     ;; We can't do a real exit, because the window conf must remain
6048     ;; the same in case the user is prompted for info, and we don't
6049     ;; want the window conf to change before that...
6050     (gnus-summary-exit t)
6051     (while (not entered)
6052       ;; Then we find what group we are supposed to enter.
6053       (set-buffer gnus-group-buffer)
6054       (gnus-group-jump-to-group current-group)
6055       (setq target-group
6056             (or target-group
6057                 (if (eq gnus-keep-same-level 'best)
6058                     (gnus-summary-best-group gnus-newsgroup-name)
6059                   (gnus-summary-search-group backward gnus-keep-same-level))))
6060       (if (not target-group)
6061           ;; There are no further groups, so we return to the group
6062           ;; buffer.
6063           (progn
6064             (gnus-message 5 "Returning to the group buffer")
6065             (setq entered t)
6066             (when (gnus-buffer-live-p current-buffer)
6067               (set-buffer current-buffer)
6068               (gnus-summary-exit))
6069             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6070         ;; We try to enter the target group.
6071         (gnus-group-jump-to-group target-group)
6072         (let ((unreads (gnus-group-group-unread)))
6073           (if (and (or (eq t unreads)
6074                        (and unreads (not (zerop unreads))))
6075                    (gnus-summary-read-group
6076                     target-group nil no-article
6077                     (and (buffer-name current-buffer) current-buffer)
6078                     nil backward))
6079               (setq entered t)
6080             (setq current-group target-group
6081                   target-group nil)))))))
6082
6083 (defun gnus-summary-prev-group (&optional no-article)
6084   "Exit current newsgroup and then select previous unread newsgroup.
6085 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6086   (interactive "P")
6087   (gnus-summary-next-group no-article nil t))
6088
6089 ;; Walking around summary lines.
6090
6091 (defun gnus-summary-first-subject (&optional unread undownloaded)
6092   "Go to the first unread subject.
6093 If UNREAD is non-nil, go to the first unread article.
6094 Returns the article selected or nil if there are no unread articles."
6095   (interactive "P")
6096   (prog1
6097       (cond
6098        ;; Empty summary.
6099        ((null gnus-newsgroup-data)
6100         (gnus-message 3 "No articles in the group")
6101         nil)
6102        ;; Pick the first article.
6103        ((not unread)
6104         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6105         (gnus-data-number (car gnus-newsgroup-data)))
6106        ;; No unread articles.
6107        ((null gnus-newsgroup-unreads)
6108         (gnus-message 3 "No more unread articles")
6109         nil)
6110        ;; Find the first unread article.
6111        (t
6112         (let ((data gnus-newsgroup-data))
6113           (while (and data
6114                       (and (not (and undownloaded
6115                                      (eq gnus-undownloaded-mark
6116                                          (gnus-data-mark (car data)))))
6117                            (not (gnus-data-unread-p (car data)))))
6118             (setq data (cdr data)))
6119           (when data
6120             (goto-char (gnus-data-pos (car data)))
6121             (gnus-data-number (car data))))))
6122     (gnus-summary-position-point)))
6123
6124 (defun gnus-summary-next-subject (n &optional unread dont-display)
6125   "Go to next N'th summary line.
6126 If N is negative, go to the previous N'th subject line.
6127 If UNREAD is non-nil, only unread articles are selected.
6128 The difference between N and the actual number of steps taken is
6129 returned."
6130   (interactive "p")
6131   (let ((backward (< n 0))
6132         (n (abs n)))
6133     (while (and (> n 0)
6134                 (if backward
6135                     (gnus-summary-find-prev unread)
6136                   (gnus-summary-find-next unread)))
6137       (unless (zerop (setq n (1- n)))
6138         (gnus-summary-show-thread)))
6139     (when (/= 0 n)
6140       (gnus-message 7 "No more%s articles"
6141                     (if unread " unread" "")))
6142     (unless dont-display
6143       (gnus-summary-recenter)
6144       (gnus-summary-position-point))
6145     n))
6146
6147 (defun gnus-summary-next-unread-subject (n)
6148   "Go to next N'th unread summary line."
6149   (interactive "p")
6150   (gnus-summary-next-subject n t))
6151
6152 (defun gnus-summary-prev-subject (n &optional unread)
6153   "Go to previous N'th summary line.
6154 If optional argument UNREAD is non-nil, only unread article is selected."
6155   (interactive "p")
6156   (gnus-summary-next-subject (- n) unread))
6157
6158 (defun gnus-summary-prev-unread-subject (n)
6159   "Go to previous N'th unread summary line."
6160   (interactive "p")
6161   (gnus-summary-next-subject (- n) t))
6162
6163 (defun gnus-summary-goto-subject (article &optional force silent)
6164   "Go the subject line of ARTICLE.
6165 If FORCE, also allow jumping to articles not currently shown."
6166   (interactive "nArticle number: ")
6167   (let ((b (point))
6168         (data (gnus-data-find article)))
6169     ;; We read in the article if we have to.
6170     (and (not data)
6171          force
6172          (gnus-summary-insert-subject
6173           article
6174           (if (or (numberp force) (vectorp force)) force)
6175           t)
6176          (setq data (gnus-data-find article)))
6177     (goto-char b)
6178     (if (not data)
6179         (progn
6180           (unless silent
6181             (gnus-message 3 "Can't find article %d" article))
6182           nil)
6183       (let ((pt (gnus-data-pos data)))
6184         (goto-char pt)
6185         (gnus-summary-set-article-display-arrow pt))
6186       (gnus-summary-position-point)
6187       article)))
6188
6189 ;; Walking around summary lines with displaying articles.
6190
6191 (defun gnus-summary-expand-window (&optional arg)
6192   "Make the summary buffer take up the entire Emacs frame.
6193 Given a prefix, will force an `article' buffer configuration."
6194   (interactive "P")
6195   (if arg
6196       (gnus-configure-windows 'article 'force)
6197     (gnus-configure-windows 'summary 'force)))
6198
6199 (defun gnus-summary-display-article (article &optional all-header)
6200   "Display ARTICLE in article buffer."
6201   (when (gnus-buffer-live-p gnus-article-buffer)
6202     (with-current-buffer gnus-article-buffer
6203       (mm-enable-multibyte-mule4)))
6204   (gnus-set-global-variables)
6205   (when (gnus-buffer-live-p gnus-article-buffer)
6206     (with-current-buffer gnus-article-buffer
6207       (setq gnus-article-charset gnus-newsgroup-charset)
6208       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6209       (mm-enable-multibyte-mule4)))
6210   (if (null article)
6211       nil
6212     (prog1
6213         (if gnus-summary-display-article-function
6214             (funcall gnus-summary-display-article-function article all-header)
6215           (gnus-article-prepare article all-header))
6216       (gnus-run-hooks 'gnus-select-article-hook)
6217       (when (and gnus-current-article
6218                  (not (zerop gnus-current-article)))
6219         (gnus-summary-goto-subject gnus-current-article))
6220       (gnus-summary-recenter)
6221       (when (and gnus-use-trees gnus-show-threads)
6222         (gnus-possibly-generate-tree article)
6223         (gnus-highlight-selected-tree article))
6224       ;; Successfully display article.
6225       (gnus-article-set-window-start
6226        (cdr (assq article gnus-newsgroup-bookmarks))))))
6227
6228 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6229   "Select the current article.
6230 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6231 non-nil, the article will be re-fetched even if it already present in
6232 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6233 be displayed."
6234   ;; Make sure we are in the summary buffer to work around bbdb bug.
6235   (unless (eq major-mode 'gnus-summary-mode)
6236     (set-buffer gnus-summary-buffer))
6237   (let ((article (or article (gnus-summary-article-number)))
6238         (all-headers (not (not all-headers))) ;Must be T or NIL.
6239         gnus-summary-display-article-function)
6240     (and (not pseudo)
6241          (gnus-summary-article-pseudo-p article)
6242          (error "This is a pseudo-article"))
6243     (save-excursion
6244       (set-buffer gnus-summary-buffer)
6245       (if (or (and gnus-single-article-buffer
6246                    (or (null gnus-current-article)
6247                        (null gnus-article-current)
6248                        (null (get-buffer gnus-article-buffer))
6249                        (not (eq article (cdr gnus-article-current)))
6250                        (not (equal (car gnus-article-current)
6251                                    gnus-newsgroup-name))))
6252               (and (not gnus-single-article-buffer)
6253                    (or (null gnus-current-article)
6254                        (not (eq gnus-current-article article))))
6255               force)
6256           ;; The requested article is different from the current article.
6257           (progn
6258             (gnus-summary-display-article article all-headers)
6259             (when (gnus-buffer-live-p gnus-article-buffer)
6260                (with-current-buffer gnus-article-buffer
6261                 (if (not gnus-article-decoded-p) ;; a local variable
6262                     (mm-disable-multibyte-mule4))))
6263             (when (or all-headers gnus-show-all-headers)
6264               (gnus-article-show-all-headers))
6265             (gnus-article-set-window-start
6266              (cdr (assq article gnus-newsgroup-bookmarks)))
6267             article)
6268         (when (or all-headers gnus-show-all-headers)
6269           (gnus-article-show-all-headers))
6270         'old))))
6271
6272 (defun gnus-summary-force-verify-and-decrypt ()
6273   (interactive)
6274   (let ((mm-verify-option 'known)
6275         (mm-decrypt-option 'known))
6276     (gnus-summary-select-article nil 'force)))
6277
6278 (defun gnus-summary-set-current-mark (&optional current-mark)
6279   "Obsolete function."
6280   nil)
6281
6282 (defun gnus-summary-next-article (&optional unread subject backward push)
6283   "Select the next article.
6284 If UNREAD, only unread articles are selected.
6285 If SUBJECT, only articles with SUBJECT are selected.
6286 If BACKWARD, the previous article is selected instead of the next."
6287   (interactive "P")
6288   (cond
6289    ;; Is there such an article?
6290    ((and (gnus-summary-search-forward unread subject backward)
6291          (or (gnus-summary-display-article (gnus-summary-article-number))
6292              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6293     (gnus-summary-position-point))
6294    ;; If not, we try the first unread, if that is wanted.
6295    ((and subject
6296          gnus-auto-select-same
6297          (gnus-summary-first-unread-article))
6298     (gnus-summary-position-point)
6299     (gnus-message 6 "Wrapped"))
6300    ;; Try to get next/previous article not displayed in this group.
6301    ((and gnus-auto-extend-newsgroup
6302          (not unread) (not subject))
6303     (gnus-summary-goto-article
6304      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6305      nil (count-lines (point-min) (point))))
6306    ;; Go to next/previous group.
6307    (t
6308     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6309       (gnus-summary-jump-to-group gnus-newsgroup-name))
6310     (let ((cmd last-command-char)
6311           (point
6312            (save-excursion
6313              (set-buffer gnus-group-buffer)
6314              (point)))
6315           (group
6316            (if (eq gnus-keep-same-level 'best)
6317                (gnus-summary-best-group gnus-newsgroup-name)
6318              (gnus-summary-search-group backward gnus-keep-same-level))))
6319       ;; For some reason, the group window gets selected.  We change
6320       ;; it back.
6321       (select-window (get-buffer-window (current-buffer)))
6322       ;; Select next unread newsgroup automagically.
6323       (cond
6324        ((or (not gnus-auto-select-next)
6325             (not cmd))
6326         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6327        ((or (eq gnus-auto-select-next 'quietly)
6328             (and (eq gnus-auto-select-next 'slightly-quietly)
6329                  push)
6330             (and (eq gnus-auto-select-next 'almost-quietly)
6331                  (gnus-summary-last-article-p)))
6332         ;; Select quietly.
6333         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6334             (gnus-summary-exit)
6335           (gnus-message 7 "No more%s articles (%s)..."
6336                         (if unread " unread" "")
6337                         (if group (concat "selecting " group)
6338                           "exiting"))
6339           (gnus-summary-next-group nil group backward)))
6340        (t
6341         (when (gnus-key-press-event-p last-input-event)
6342           (gnus-summary-walk-group-buffer
6343            gnus-newsgroup-name cmd unread backward point))))))))
6344
6345 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6346   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6347                       (?\C-p (gnus-group-prev-unread-group 1))))
6348         (cursor-in-echo-area t)
6349         keve key group ended)
6350     (save-excursion
6351       (set-buffer gnus-group-buffer)
6352       (goto-char start)
6353       (setq group
6354             (if (eq gnus-keep-same-level 'best)
6355                 (gnus-summary-best-group gnus-newsgroup-name)
6356               (gnus-summary-search-group backward gnus-keep-same-level))))
6357     (while (not ended)
6358       (gnus-message
6359        5 "No more%s articles%s" (if unread " unread" "")
6360        (if (and group
6361                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6362            (format " (Type %s for %s [%s])"
6363                    (single-key-description cmd) group
6364                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6365          (format " (Type %s to exit %s)"
6366                  (single-key-description cmd)
6367                  gnus-newsgroup-name)))
6368       ;; Confirm auto selection.
6369       (setq key (car (setq keve (gnus-read-event-char))))
6370       (setq ended t)
6371       (cond
6372        ((assq key keystrokes)
6373         (let ((obuf (current-buffer)))
6374           (switch-to-buffer gnus-group-buffer)
6375           (when group
6376             (gnus-group-jump-to-group group))
6377           (eval (cadr (assq key keystrokes)))
6378           (setq group (gnus-group-group-name))
6379           (switch-to-buffer obuf))
6380         (setq ended nil))
6381        ((equal key cmd)
6382         (if (or (not group)
6383                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6384             (gnus-summary-exit)
6385           (gnus-summary-next-group nil group backward)))
6386        (t
6387         (push (cdr keve) unread-command-events))))))
6388
6389 (defun gnus-summary-next-unread-article ()
6390   "Select unread article after current one."
6391   (interactive)
6392   (gnus-summary-next-article
6393    (or (not (eq gnus-summary-goto-unread 'never))
6394        (gnus-summary-last-article-p (gnus-summary-article-number)))
6395    (and gnus-auto-select-same
6396         (gnus-summary-article-subject))))
6397
6398 (defun gnus-summary-prev-article (&optional unread subject)
6399   "Select the article after the current one.
6400 If UNREAD is non-nil, only unread articles are selected."
6401   (interactive "P")
6402   (gnus-summary-next-article unread subject t))
6403
6404 (defun gnus-summary-prev-unread-article ()
6405   "Select unread article before current one."
6406   (interactive)
6407   (gnus-summary-prev-article
6408    (or (not (eq gnus-summary-goto-unread 'never))
6409        (gnus-summary-first-article-p (gnus-summary-article-number)))
6410    (and gnus-auto-select-same
6411         (gnus-summary-article-subject))))
6412
6413 (defun gnus-summary-next-page (&optional lines circular)
6414   "Show next page of the selected article.
6415 If at the end of the current article, select the next article.
6416 LINES says how many lines should be scrolled up.
6417
6418 If CIRCULAR is non-nil, go to the start of the article instead of
6419 selecting the next article when reaching the end of the current
6420 article."
6421   (interactive "P")
6422   (setq gnus-summary-buffer (current-buffer))
6423   (gnus-set-global-variables)
6424   (let ((article (gnus-summary-article-number))
6425         (article-window (get-buffer-window gnus-article-buffer t))
6426         endp)
6427     ;; If the buffer is empty, we have no article.
6428     (unless article
6429       (error "No article to select"))
6430     (gnus-configure-windows 'article)
6431     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6432         (if (and (eq gnus-summary-goto-unread 'never)
6433                  (not (gnus-summary-last-article-p article)))
6434             (gnus-summary-next-article)
6435           (gnus-summary-next-unread-article))
6436       (if (or (null gnus-current-article)
6437               (null gnus-article-current)
6438               (/= article (cdr gnus-article-current))
6439               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6440           ;; Selected subject is different from current article's.
6441           (gnus-summary-display-article article)
6442         (when article-window
6443           (gnus-eval-in-buffer-window gnus-article-buffer
6444             (setq endp (gnus-article-next-page lines)))
6445           (when endp
6446             (cond (circular
6447                    (gnus-summary-beginning-of-article))
6448                   (lines
6449                    (gnus-message 3 "End of message"))
6450                   ((null lines)
6451                    (if (and (eq gnus-summary-goto-unread 'never)
6452                             (not (gnus-summary-last-article-p article)))
6453                        (gnus-summary-next-article)
6454                      (gnus-summary-next-unread-article))))))))
6455     (gnus-summary-recenter)
6456     (gnus-summary-position-point)))
6457
6458 (defun gnus-summary-prev-page (&optional lines move)
6459   "Show previous page of selected article.
6460 Argument LINES specifies lines to be scrolled down.
6461 If MOVE, move to the previous unread article if point is at
6462 the beginning of the buffer."
6463   (interactive "P")
6464   (let ((article (gnus-summary-article-number))
6465         (article-window (get-buffer-window gnus-article-buffer t))
6466         endp)
6467     (gnus-configure-windows 'article)
6468     (if (or (null gnus-current-article)
6469             (null gnus-article-current)
6470             (/= article (cdr gnus-article-current))
6471             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6472         ;; Selected subject is different from current article's.
6473         (gnus-summary-display-article article)
6474       (gnus-summary-recenter)
6475       (when article-window
6476         (gnus-eval-in-buffer-window gnus-article-buffer
6477           (setq endp (gnus-article-prev-page lines)))
6478         (when (and move endp)
6479           (cond (lines
6480                  (gnus-message 3 "Beginning of message"))
6481                 ((null lines)
6482                  (if (and (eq gnus-summary-goto-unread 'never)
6483                           (not (gnus-summary-first-article-p article)))
6484                      (gnus-summary-prev-article)
6485                    (gnus-summary-prev-unread-article))))))))
6486   (gnus-summary-position-point))
6487
6488 (defun gnus-summary-prev-page-or-article (&optional lines)
6489   "Show previous page of selected article.
6490 Argument LINES specifies lines to be scrolled down.
6491 If at the beginning of the article, go to the next article."
6492   (interactive "P")
6493   (gnus-summary-prev-page lines t))
6494
6495 (defun gnus-summary-scroll-up (lines)
6496   "Scroll up (or down) one line current article.
6497 Argument LINES specifies lines to be scrolled up (or down if negative)."
6498   (interactive "p")
6499   (gnus-configure-windows 'article)
6500   (gnus-summary-show-thread)
6501   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6502     (gnus-eval-in-buffer-window gnus-article-buffer
6503       (cond ((> lines 0)
6504              (when (gnus-article-next-page lines)
6505                (gnus-message 3 "End of message")))
6506             ((< lines 0)
6507              (gnus-article-prev-page (- lines))))))
6508   (gnus-summary-recenter)
6509   (gnus-summary-position-point))
6510
6511 (defun gnus-summary-scroll-down (lines)
6512   "Scroll down (or up) one line current article.
6513 Argument LINES specifies lines to be scrolled down (or up if negative)."
6514   (interactive "p")
6515   (gnus-summary-scroll-up (- lines)))
6516
6517 (defun gnus-summary-next-same-subject ()
6518   "Select next article which has the same subject as current one."
6519   (interactive)
6520   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6521
6522 (defun gnus-summary-prev-same-subject ()
6523   "Select previous article which has the same subject as current one."
6524   (interactive)
6525   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6526
6527 (defun gnus-summary-next-unread-same-subject ()
6528   "Select next unread article which has the same subject as current one."
6529   (interactive)
6530   (gnus-summary-next-article t (gnus-summary-article-subject)))
6531
6532 (defun gnus-summary-prev-unread-same-subject ()
6533   "Select previous unread article which has the same subject as current one."
6534   (interactive)
6535   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6536
6537 (defun gnus-summary-first-unread-article ()
6538   "Select the first unread article.
6539 Return nil if there are no unread articles."
6540   (interactive)
6541   (prog1
6542       (when (gnus-summary-first-subject t)
6543         (gnus-summary-show-thread)
6544         (gnus-summary-first-subject t)
6545         (gnus-summary-display-article (gnus-summary-article-number)))
6546     (gnus-summary-position-point)))
6547
6548 (defun gnus-summary-first-unread-subject ()
6549   "Place the point on the subject line of the first unread article.
6550 Return nil if there are no unread articles."
6551   (interactive)
6552   (prog1
6553       (when (gnus-summary-first-subject t)
6554         (gnus-summary-show-thread)
6555         (gnus-summary-first-subject t))
6556     (gnus-summary-position-point)))
6557
6558 (defun gnus-summary-first-article ()
6559   "Select the first article.
6560 Return nil if there are no articles."
6561   (interactive)
6562   (prog1
6563       (when (gnus-summary-first-subject)
6564         (gnus-summary-show-thread)
6565         (gnus-summary-first-subject)
6566         (gnus-summary-display-article (gnus-summary-article-number)))
6567     (gnus-summary-position-point)))
6568
6569 (defun gnus-summary-best-unread-article ()
6570   "Select the unread article with the highest score."
6571   (interactive)
6572   (let ((best -1000000)
6573         (data gnus-newsgroup-data)
6574         article score)
6575     (while data
6576       (and (gnus-data-unread-p (car data))
6577            (> (setq score
6578                     (gnus-summary-article-score (gnus-data-number (car data))))
6579               best)
6580            (setq best score
6581                  article (gnus-data-number (car data))))
6582       (setq data (cdr data)))
6583     (prog1
6584         (if article
6585             (gnus-summary-goto-article article)
6586           (error "No unread articles"))
6587       (gnus-summary-position-point))))
6588
6589 (defun gnus-summary-last-subject ()
6590   "Go to the last displayed subject line in the group."
6591   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6592     (when article
6593       (gnus-summary-goto-subject article))))
6594
6595 (defun gnus-summary-goto-article (article &optional all-headers force)
6596   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6597 If ALL-HEADERS is non-nil, no header lines are hidden.
6598 If FORCE, go to the article even if it isn't displayed.  If FORCE
6599 is a number, it is the line the article is to be displayed on."
6600   (interactive
6601    (list
6602     (completing-read
6603      "Article number or Message-ID: "
6604      (mapcar (lambda (number) (list (int-to-string number)))
6605              gnus-newsgroup-limit))
6606     current-prefix-arg
6607     t))
6608   (prog1
6609       (if (and (stringp article)
6610                (string-match "@" article))
6611           (gnus-summary-refer-article article)
6612         (when (stringp article)
6613           (setq article (string-to-number article)))
6614         (if (gnus-summary-goto-subject article force)
6615             (gnus-summary-display-article article all-headers)
6616           (gnus-message 4 "Couldn't go to article %s" article) nil))
6617     (gnus-summary-position-point)))
6618
6619 (defun gnus-summary-goto-last-article ()
6620   "Go to the previously read article."
6621   (interactive)
6622   (prog1
6623       (when gnus-last-article
6624         (gnus-summary-goto-article gnus-last-article nil t))
6625     (gnus-summary-position-point)))
6626
6627 (defun gnus-summary-pop-article (number)
6628   "Pop one article off the history and go to the previous.
6629 NUMBER articles will be popped off."
6630   (interactive "p")
6631   (let (to)
6632     (setq gnus-newsgroup-history
6633           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6634     (if to
6635         (gnus-summary-goto-article (car to) nil t)
6636       (error "Article history empty")))
6637   (gnus-summary-position-point))
6638
6639 ;; Summary commands and functions for limiting the summary buffer.
6640
6641 (defun gnus-summary-limit-to-articles (n)
6642   "Limit the summary buffer to the next N articles.
6643 If not given a prefix, use the process marked articles instead."
6644   (interactive "P")
6645   (prog1
6646       (let ((articles (gnus-summary-work-articles n)))
6647         (setq gnus-newsgroup-processable nil)
6648         (gnus-summary-limit articles))
6649     (gnus-summary-position-point)))
6650
6651 (defun gnus-summary-pop-limit (&optional total)
6652   "Restore the previous limit.
6653 If given a prefix, remove all limits."
6654   (interactive "P")
6655   (when total
6656     (setq gnus-newsgroup-limits
6657           (list (mapcar (lambda (h) (mail-header-number h))
6658                         gnus-newsgroup-headers))))
6659   (unless gnus-newsgroup-limits
6660     (error "No limit to pop"))
6661   (prog1
6662       (gnus-summary-limit nil 'pop)
6663     (gnus-summary-position-point)))
6664
6665 (defun gnus-summary-limit-to-subject (subject &optional header)
6666   "Limit the summary buffer to articles that have subjects that match a regexp."
6667   (interactive "sLimit to subject (regexp): ")
6668   (unless header
6669     (setq header "subject"))
6670   (when (not (equal "" subject))
6671     (prog1
6672         (let ((articles (gnus-summary-find-matching
6673                          (or header "subject") subject 'all)))
6674           (unless articles
6675             (error "Found no matches for \"%s\"" subject))
6676           (gnus-summary-limit articles))
6677       (gnus-summary-position-point))))
6678
6679 (defun gnus-summary-limit-to-author (from)
6680   "Limit the summary buffer to articles that have authors that match a regexp."
6681   (interactive "sLimit to author (regexp): ")
6682   (gnus-summary-limit-to-subject from "from"))
6683
6684 (defun gnus-summary-limit-to-age (age &optional younger-p)
6685   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6686 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6687 articles that are younger than AGE days."
6688   (interactive
6689    (let ((younger current-prefix-arg)
6690          (days-got nil)
6691          days)
6692      (while (not days-got)
6693        (setq days (if younger
6694                       (read-string "Limit to articles within (in days): ")
6695                     (read-string "Limit to articles older than (in days): ")))
6696        (when (> (length days) 0)
6697          (setq days (read days)))
6698        (if (numberp days)
6699            (setq days-got t)
6700          (message "Please enter a number.")
6701          (sleep-for 1)))
6702      (list days younger)))
6703   (prog1
6704       (let ((data gnus-newsgroup-data)
6705             (cutoff (days-to-time age))
6706             articles d date is-younger)
6707         (while (setq d (pop data))
6708           (when (and (vectorp (gnus-data-header d))
6709                      (setq date (mail-header-date (gnus-data-header d))))
6710             (setq is-younger (time-less-p
6711                               (time-since (condition-case ()
6712                                               (date-to-time date)
6713                                             (error '(0 0))))
6714                               cutoff))
6715             (when (if younger-p
6716                       is-younger
6717                     (not is-younger))
6718               (push (gnus-data-number d) articles))))
6719         (gnus-summary-limit (nreverse articles)))
6720     (gnus-summary-position-point)))
6721
6722 (defun gnus-summary-limit-to-extra (header regexp)
6723   "Limit the summary buffer to articles that match an 'extra' header."
6724   (interactive
6725    (let ((header
6726           (intern
6727            (gnus-completing-read
6728             (symbol-name (car gnus-extra-headers))
6729             "Limit extra header:"
6730             (mapcar (lambda (x)
6731                       (cons (symbol-name x) x))
6732                     gnus-extra-headers)
6733             nil
6734             t))))
6735      (list header
6736            (read-string (format "Limit to header %s (regexp): " header)))))
6737   (when (not (equal "" regexp))
6738     (prog1
6739         (let ((articles (gnus-summary-find-matching
6740                          (cons 'extra header) regexp 'all)))
6741           (unless articles
6742             (error "Found no matches for \"%s\"" regexp))
6743           (gnus-summary-limit articles))
6744       (gnus-summary-position-point))))
6745
6746 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6747 (make-obsolete
6748  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6749
6750 (defun gnus-summary-limit-to-unread (&optional all)
6751   "Limit the summary buffer to articles that are not marked as read.
6752 If ALL is non-nil, limit strictly to unread articles."
6753   (interactive "P")
6754   (if all
6755       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6756     (gnus-summary-limit-to-marks
6757      ;; Concat all the marks that say that an article is read and have
6758      ;; those removed.
6759      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6760            gnus-killed-mark gnus-kill-file-mark
6761            gnus-low-score-mark gnus-expirable-mark
6762            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6763            gnus-duplicate-mark gnus-souped-mark)
6764      'reverse)))
6765
6766 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6767 (make-obsolete 'gnus-summary-delete-marked-with
6768                'gnus-summary-limit-exlude-marks)
6769
6770 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6771   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6772 If REVERSE, limit the summary buffer to articles that are marked
6773 with MARKS.  MARKS can either be a string of marks or a list of marks.
6774 Returns how many articles were removed."
6775   (interactive "sMarks: ")
6776   (gnus-summary-limit-to-marks marks t))
6777
6778 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6779   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6780 If REVERSE (the prefix), limit the summary buffer to articles that are
6781 not marked with MARKS.  MARKS can either be a string of marks or a
6782 list of marks.
6783 Returns how many articles were removed."
6784   (interactive "sMarks: \nP")
6785   (prog1
6786       (let ((data gnus-newsgroup-data)
6787             (marks (if (listp marks) marks
6788                      (append marks nil))) ; Transform to list.
6789             articles)
6790         (while data
6791           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6792                   (memq (gnus-data-mark (car data)) marks))
6793             (push (gnus-data-number (car data)) articles))
6794           (setq data (cdr data)))
6795         (gnus-summary-limit articles))
6796     (gnus-summary-position-point)))
6797
6798 (defun gnus-summary-limit-to-score (&optional score)
6799   "Limit to articles with score at or above SCORE."
6800   (interactive "P")
6801   (setq score (if score
6802                   (prefix-numeric-value score)
6803                 (or gnus-summary-default-score 0)))
6804   (let ((data gnus-newsgroup-data)
6805         articles)
6806     (while data
6807       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6808                 score)
6809         (push (gnus-data-number (car data)) articles))
6810       (setq data (cdr data)))
6811     (prog1
6812         (gnus-summary-limit articles)
6813       (gnus-summary-position-point))))
6814
6815 (defun gnus-summary-limit-include-thread (id)
6816   "Display all the hidden articles that is in the thread with ID in it.
6817 When called interactively, ID is the Message-ID of the current
6818 article."
6819   (interactive (list (mail-header-id (gnus-summary-article-header))))
6820   (let ((articles (gnus-articles-in-thread
6821                    (gnus-id-to-thread (gnus-root-id id)))))
6822     (prog1
6823         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6824         (gnus-summary-limit-include-matching-articles
6825          "subject"
6826          (regexp-quote (gnus-simplify-subject-re
6827                         (mail-header-subject (gnus-id-to-header id)))))
6828       (gnus-summary-position-point))))
6829
6830 (defun gnus-summary-limit-include-matching-articles (header regexp)
6831   "Display all the hidden articles that have HEADERs that match REGEXP."
6832   (interactive (list (read-string "Match on header: ")
6833                      (read-string "Regexp: ")))
6834   (let ((articles (gnus-find-matching-articles header regexp)))
6835     (prog1
6836         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6837       (gnus-summary-position-point))))
6838
6839 (defun gnus-summary-limit-include-dormant ()
6840   "Display all the hidden articles that are marked as dormant.
6841 Note that this command only works on a subset of the articles currently
6842 fetched for this group."
6843   (interactive)
6844   (unless gnus-newsgroup-dormant
6845     (error "There are no dormant articles in this group"))
6846   (prog1
6847       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6848     (gnus-summary-position-point)))
6849
6850 (defun gnus-summary-limit-exclude-dormant ()
6851   "Hide all dormant articles."
6852   (interactive)
6853   (prog1
6854       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6855     (gnus-summary-position-point)))
6856
6857 (defun gnus-summary-limit-exclude-childless-dormant ()
6858   "Hide all dormant articles that have no children."
6859   (interactive)
6860   (let ((data (gnus-data-list t))
6861         articles d children)
6862     ;; Find all articles that are either not dormant or have
6863     ;; children.
6864     (while (setq d (pop data))
6865       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6866                 (and (setq children
6867                            (gnus-article-children (gnus-data-number d)))
6868                      (let (found)
6869                        (while children
6870                          (when (memq (car children) articles)
6871                            (setq children nil
6872                                  found t))
6873                          (pop children))
6874                        found)))
6875         (push (gnus-data-number d) articles)))
6876     ;; Do the limiting.
6877     (prog1
6878         (gnus-summary-limit articles)
6879       (gnus-summary-position-point))))
6880
6881 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6882   "Mark all unread excluded articles as read.
6883 If ALL, mark even excluded ticked and dormants as read."
6884   (interactive "P")
6885   (let ((articles (gnus-sorted-complement
6886                    (sort
6887                     (mapcar (lambda (h) (mail-header-number h))
6888                             gnus-newsgroup-headers)
6889                     '<)
6890                    (sort gnus-newsgroup-limit '<)))
6891         article)
6892     (setq gnus-newsgroup-unreads
6893           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6894     (if all
6895         (setq gnus-newsgroup-dormant nil
6896               gnus-newsgroup-marked nil
6897               gnus-newsgroup-reads
6898               (nconc
6899                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6900                gnus-newsgroup-reads))
6901       (while (setq article (pop articles))
6902         (unless (or (memq article gnus-newsgroup-dormant)
6903                     (memq article gnus-newsgroup-marked))
6904           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6905
6906 (defun gnus-summary-limit (articles &optional pop)
6907   (if pop
6908       ;; We pop the previous limit off the stack and use that.
6909       (setq articles (car gnus-newsgroup-limits)
6910             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6911     ;; We use the new limit, so we push the old limit on the stack.
6912     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6913   ;; Set the limit.
6914   (setq gnus-newsgroup-limit articles)
6915   (let ((total (length gnus-newsgroup-data))
6916         (data (gnus-data-find-list (gnus-summary-article-number)))
6917         (gnus-summary-mark-below nil)   ; Inhibit this.
6918         found)
6919     ;; This will do all the work of generating the new summary buffer
6920     ;; according to the new limit.
6921     (gnus-summary-prepare)
6922     ;; Hide any threads, possibly.
6923     (and gnus-show-threads
6924          gnus-thread-hide-subtree
6925          (gnus-summary-hide-all-threads))
6926     ;; Try to return to the article you were at, or one in the
6927     ;; neighborhood.
6928     (when data
6929       ;; We try to find some article after the current one.
6930       (while data
6931         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6932           (setq data nil
6933                 found t))
6934         (setq data (cdr data))))
6935     (unless found
6936       ;; If there is no data, that means that we were after the last
6937       ;; article.  The same goes when we can't find any articles
6938       ;; after the current one.
6939       (goto-char (point-max))
6940       (gnus-summary-find-prev))
6941     (gnus-set-mode-line 'summary)
6942     ;; We return how many articles were removed from the summary
6943     ;; buffer as a result of the new limit.
6944     (- total (length gnus-newsgroup-data))))
6945
6946 (defsubst gnus-invisible-cut-children (threads)
6947   (let ((num 0))
6948     (while threads
6949       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6950         (incf num))
6951       (pop threads))
6952     (< num 2)))
6953
6954 (defsubst gnus-cut-thread (thread)
6955   "Go forwards in the thread until we find an article that we want to display."
6956   (when (or (eq gnus-fetch-old-headers 'some)
6957             (eq gnus-fetch-old-headers 'invisible)
6958             (numberp gnus-fetch-old-headers)
6959             (eq gnus-build-sparse-threads 'some)
6960             (eq gnus-build-sparse-threads 'more))
6961     ;; Deal with old-fetched headers and sparse threads.
6962     (while (and
6963             thread
6964             (or
6965              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6966              (gnus-summary-article-ancient-p
6967               (mail-header-number (car thread))))
6968             (if (or (<= (length (cdr thread)) 1)
6969                     (eq gnus-fetch-old-headers 'invisible))
6970                 (setq gnus-newsgroup-limit
6971                       (delq (mail-header-number (car thread))
6972                             gnus-newsgroup-limit)
6973                       thread (cadr thread))
6974               (when (gnus-invisible-cut-children (cdr thread))
6975                 (let ((th (cdr thread)))
6976                   (while th
6977                     (if (memq (mail-header-number (caar th))
6978                               gnus-newsgroup-limit)
6979                         (setq thread (car th)
6980                               th nil)
6981                       (setq th (cdr th))))))))))
6982   thread)
6983
6984 (defun gnus-cut-threads (threads)
6985   "Cut off all uninteresting articles from the beginning of threads."
6986   (when (or (eq gnus-fetch-old-headers 'some)
6987             (eq gnus-fetch-old-headers 'invisible)
6988             (numberp gnus-fetch-old-headers)
6989             (eq gnus-build-sparse-threads 'some)
6990             (eq gnus-build-sparse-threads 'more))
6991     (let ((th threads))
6992       (while th
6993         (setcar th (gnus-cut-thread (car th)))
6994         (setq th (cdr th)))))
6995   ;; Remove nixed out threads.
6996   (delq nil threads))
6997
6998 (defun gnus-summary-initial-limit (&optional show-if-empty)
6999   "Figure out what the initial limit is supposed to be on group entry.
7000 This entails weeding out unwanted dormants, low-scored articles,
7001 fetch-old-headers verbiage, and so on."
7002   ;; Most groups have nothing to remove.
7003   (if (or gnus-inhibit-limiting
7004           (and (null gnus-newsgroup-dormant)
7005                (not (eq gnus-fetch-old-headers 'some))
7006                (not (numberp gnus-fetch-old-headers))
7007                (not (eq gnus-fetch-old-headers 'invisible))
7008                (null gnus-summary-expunge-below)
7009                (not (eq gnus-build-sparse-threads 'some))
7010                (not (eq gnus-build-sparse-threads 'more))
7011                (null gnus-thread-expunge-below)
7012                (not gnus-use-nocem)))
7013       ()                                ; Do nothing.
7014     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7015     (setq gnus-newsgroup-limit nil)
7016     (mapatoms
7017      (lambda (node)
7018        (unless (car (symbol-value node))
7019          ;; These threads have no parents -- they are roots.
7020          (let ((nodes (cdr (symbol-value node)))
7021                thread)
7022            (while nodes
7023              (if (and gnus-thread-expunge-below
7024                       (< (gnus-thread-total-score (car nodes))
7025                          gnus-thread-expunge-below))
7026                  (gnus-expunge-thread (pop nodes))
7027                (setq thread (pop nodes))
7028                (gnus-summary-limit-children thread))))))
7029      gnus-newsgroup-dependencies)
7030     ;; If this limitation resulted in an empty group, we might
7031     ;; pop the previous limit and use it instead.
7032     (when (and (not gnus-newsgroup-limit)
7033                show-if-empty)
7034       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7035     gnus-newsgroup-limit))
7036
7037 (defun gnus-summary-limit-children (thread)
7038   "Return 1 if this subthread is visible and 0 if it is not."
7039   ;; First we get the number of visible children to this thread.  This
7040   ;; is done by recursing down the thread using this function, so this
7041   ;; will really go down to a leaf article first, before slowly
7042   ;; working its way up towards the root.
7043   (when thread
7044     (let ((children
7045            (if (cdr thread)
7046                (apply '+ (mapcar 'gnus-summary-limit-children
7047                                  (cdr thread)))
7048              0))
7049           (number (mail-header-number (car thread)))
7050           score)
7051       (if (and
7052            (not (memq number gnus-newsgroup-marked))
7053            (or
7054             ;; If this article is dormant and has absolutely no visible
7055             ;; children, then this article isn't visible.
7056             (and (memq number gnus-newsgroup-dormant)
7057                  (zerop children))
7058             ;; If this is "fetch-old-headered" and there is no
7059             ;; visible children, then we don't want this article.
7060             (and (or (eq gnus-fetch-old-headers 'some)
7061                      (numberp gnus-fetch-old-headers))
7062                  (gnus-summary-article-ancient-p number)
7063                  (zerop children))
7064             ;; If this is "fetch-old-headered" and `invisible', then
7065             ;; we don't want this article.
7066             (and (eq gnus-fetch-old-headers 'invisible)
7067                  (gnus-summary-article-ancient-p number))
7068             ;; If this is a sparsely inserted article with no children,
7069             ;; we don't want it.
7070             (and (eq gnus-build-sparse-threads 'some)
7071                  (gnus-summary-article-sparse-p number)
7072                  (zerop children))
7073             ;; If we use expunging, and this article is really
7074             ;; low-scored, then we don't want this article.
7075             (when (and gnus-summary-expunge-below
7076                        (< (setq score
7077                                 (or (cdr (assq number gnus-newsgroup-scored))
7078                                     gnus-summary-default-score))
7079                           gnus-summary-expunge-below))
7080               ;; We increase the expunge-tally here, but that has
7081               ;; nothing to do with the limits, really.
7082               (incf gnus-newsgroup-expunged-tally)
7083               ;; We also mark as read here, if that's wanted.
7084               (when (and gnus-summary-mark-below
7085                          (< score gnus-summary-mark-below))
7086                 (setq gnus-newsgroup-unreads
7087                       (delq number gnus-newsgroup-unreads))
7088                 (if gnus-newsgroup-auto-expire
7089                     (push number gnus-newsgroup-expirable)
7090                   (push (cons number gnus-low-score-mark)
7091                         gnus-newsgroup-reads)))
7092               t)
7093             ;; Check NoCeM things.
7094             (if (and gnus-use-nocem
7095                      (gnus-nocem-unwanted-article-p
7096                       (mail-header-id (car thread))))
7097                 (progn
7098                   (setq gnus-newsgroup-unreads
7099                         (delq number gnus-newsgroup-unreads))
7100                   t))))
7101           ;; Nope, invisible article.
7102           0
7103         ;; Ok, this article is to be visible, so we add it to the limit
7104         ;; and return 1.
7105         (push number gnus-newsgroup-limit)
7106         1))))
7107
7108 (defun gnus-expunge-thread (thread)
7109   "Mark all articles in THREAD as read."
7110   (let* ((number (mail-header-number (car thread))))
7111     (incf gnus-newsgroup-expunged-tally)
7112     ;; We also mark as read here, if that's wanted.
7113     (setq gnus-newsgroup-unreads
7114           (delq number gnus-newsgroup-unreads))
7115     (if gnus-newsgroup-auto-expire
7116         (push number gnus-newsgroup-expirable)
7117       (push (cons number gnus-low-score-mark)
7118             gnus-newsgroup-reads)))
7119   ;; Go recursively through all subthreads.
7120   (mapcar 'gnus-expunge-thread (cdr thread)))
7121
7122 ;; Summary article oriented commands
7123
7124 (defun gnus-summary-refer-parent-article (n)
7125   "Refer parent article N times.
7126 If N is negative, go to ancestor -N instead.
7127 The difference between N and the number of articles fetched is returned."
7128   (interactive "p")
7129   (let ((skip 1)
7130         error header ref)
7131     (when (not (natnump n))
7132       (setq skip (abs n)
7133             n 1))
7134     (while (and (> n 0)
7135                 (not error))
7136       (setq header (gnus-summary-article-header))
7137       (if (and (eq (mail-header-number header)
7138                    (cdr gnus-article-current))
7139                (equal gnus-newsgroup-name
7140                       (car gnus-article-current)))
7141           ;; If we try to find the parent of the currently
7142           ;; displayed article, then we take a look at the actual
7143           ;; References header, since this is slightly more
7144           ;; reliable than the References field we got from the
7145           ;; server.
7146           (save-excursion
7147             (set-buffer gnus-original-article-buffer)
7148             (nnheader-narrow-to-headers)
7149             (unless (setq ref (message-fetch-field "references"))
7150               (setq ref (message-fetch-field "in-reply-to")))
7151             (widen))
7152         (setq ref
7153               ;; It's not the current article, so we take a bet on
7154               ;; the value we got from the server.
7155               (mail-header-references header)))
7156       (if (and ref
7157                (not (equal ref "")))
7158           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7159             (gnus-message 1 "Couldn't find parent"))
7160         (gnus-message 1 "No references in article %d"
7161                       (gnus-summary-article-number))
7162         (setq error t))
7163       (decf n))
7164     (gnus-summary-position-point)
7165     n))
7166
7167 (defun gnus-summary-refer-references ()
7168   "Fetch all articles mentioned in the References header.
7169 Return the number of articles fetched."
7170   (interactive)
7171   (let ((ref (mail-header-references (gnus-summary-article-header)))
7172         (current (gnus-summary-article-number))
7173         (n 0))
7174     (if (or (not ref)
7175             (equal ref ""))
7176         (error "No References in the current article")
7177       ;; For each Message-ID in the References header...
7178       (while (string-match "<[^>]*>" ref)
7179         (incf n)
7180         ;; ... fetch that article.
7181         (gnus-summary-refer-article
7182          (prog1 (match-string 0 ref)
7183            (setq ref (substring ref (match-end 0))))))
7184       (gnus-summary-goto-subject current)
7185       (gnus-summary-position-point)
7186       n)))
7187
7188 (defun gnus-summary-refer-thread (&optional limit)
7189   "Fetch all articles in the current thread.
7190 If LIMIT (the numerical prefix), fetch that many old headers instead
7191 of what's specified by the `gnus-refer-thread-limit' variable."
7192   (interactive "P")
7193   (let ((id (mail-header-id (gnus-summary-article-header)))
7194         (limit (if limit (prefix-numeric-value limit)
7195                  gnus-refer-thread-limit)))
7196     ;; We want to fetch LIMIT *old* headers, but we also have to
7197     ;; re-fetch all the headers in the current buffer, because many of
7198     ;; them may be undisplayed.  So we adjust LIMIT.
7199     (when (numberp limit)
7200       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7201     (unless (eq gnus-fetch-old-headers 'invisible)
7202       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7203       ;; Retrieve the headers and read them in.
7204       (if (eq (gnus-retrieve-headers
7205                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7206               'nov)
7207           (gnus-build-all-threads)
7208         (error "Can't fetch thread from backends that don't support NOV"))
7209       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7210     (gnus-summary-limit-include-thread id)))
7211
7212 (defun gnus-summary-refer-article (message-id)
7213   "Fetch an article specified by MESSAGE-ID."
7214   (interactive "sMessage-ID: ")
7215   (when (and (stringp message-id)
7216              (not (zerop (length message-id))))
7217     ;; Construct the correct Message-ID if necessary.
7218     ;; Suggested by tale@pawl.rpi.edu.
7219     (unless (string-match "^<" message-id)
7220       (setq message-id (concat "<" message-id)))
7221     (unless (string-match ">$" message-id)
7222       (setq message-id (concat message-id ">")))
7223     (let* ((header (gnus-id-to-header message-id))
7224            (sparse (and header
7225                         (gnus-summary-article-sparse-p
7226                          (mail-header-number header))
7227                         (memq (mail-header-number header)
7228                               gnus-newsgroup-limit)))
7229            number)
7230       (cond
7231        ;; If the article is present in the buffer we just go to it.
7232        ((and header
7233              (or (not (gnus-summary-article-sparse-p
7234                        (mail-header-number header)))
7235                  sparse))
7236         (prog1
7237             (gnus-summary-goto-article
7238              (mail-header-number header) nil t)
7239           (when sparse
7240             (gnus-summary-update-article (mail-header-number header)))))
7241        (t
7242         ;; We fetch the article.
7243         (catch 'found
7244           (dolist (gnus-override-method (gnus-refer-article-methods))
7245             (gnus-check-server gnus-override-method)
7246             ;; Fetch the header, and display the article.
7247             (when (setq number (gnus-summary-insert-subject message-id))
7248               (gnus-summary-select-article nil nil nil number)
7249               (throw 'found t)))
7250           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7251
7252 (defun gnus-refer-article-methods ()
7253   "Return a list of referrable methods."
7254   (cond
7255    ;; No method, so we default to current and native.
7256    ((null gnus-refer-article-method)
7257     (list gnus-current-select-method gnus-select-method))
7258    ;; Current.
7259    ((eq 'current gnus-refer-article-method)
7260     (list gnus-current-select-method))
7261    ;; List of select methods.
7262    ((not (and (symbolp (car gnus-refer-article-method))
7263               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7264     (let (out)
7265       (dolist (method gnus-refer-article-method)
7266         (push (if (eq 'current method)
7267                   gnus-current-select-method
7268                 method)
7269               out))
7270       (nreverse out)))
7271    ;; One single select method.
7272    (t
7273     (list gnus-refer-article-method))))
7274
7275 (defun gnus-summary-edit-parameters ()
7276   "Edit the group parameters of the current group."
7277   (interactive)
7278   (gnus-group-edit-group gnus-newsgroup-name 'params))
7279
7280 (defun gnus-summary-customize-parameters ()
7281   "Customize the group parameters of the current group."
7282   (interactive)
7283   (gnus-group-customize gnus-newsgroup-name))
7284
7285 (defun gnus-summary-enter-digest-group (&optional force)
7286   "Enter an nndoc group based on the current article.
7287 If FORCE, force a digest interpretation.  If not, try
7288 to guess what the document format is."
7289   (interactive "P")
7290   (let ((conf gnus-current-window-configuration))
7291     (save-excursion
7292       (gnus-summary-select-article))
7293     (setq gnus-current-window-configuration conf)
7294     (let* ((name (format "%s-%d"
7295                          (gnus-group-prefixed-name
7296                           gnus-newsgroup-name (list 'nndoc ""))
7297                          (save-excursion
7298                            (set-buffer gnus-summary-buffer)
7299                            gnus-current-article)))
7300            (ogroup gnus-newsgroup-name)
7301            (params (append (gnus-info-params (gnus-get-info ogroup))
7302                            (list (cons 'to-group ogroup))
7303                            (list (cons 'save-article-group ogroup))))
7304            (case-fold-search t)
7305            (buf (current-buffer))
7306            dig to-address)
7307       (save-excursion
7308         (set-buffer gnus-original-article-buffer)
7309         ;; Have the digest group inherit the main mail address of
7310         ;; the parent article.
7311         (when (setq to-address (or (message-fetch-field "reply-to")
7312                                    (message-fetch-field "from")))
7313           (setq params (append
7314                         (list (cons 'to-address
7315                                     (funcall gnus-decode-encoded-word-function
7316                                              to-address))))))
7317         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7318         (insert-buffer-substring gnus-original-article-buffer)
7319         ;; Remove lines that may lead nndoc to misinterpret the
7320         ;; document type.
7321         (narrow-to-region
7322          (goto-char (point-min))
7323          (or (search-forward "\n\n" nil t) (point)))
7324         (goto-char (point-min))
7325         (delete-matching-lines "^Path:\\|^From ")
7326         (widen))
7327       (unwind-protect
7328           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7329                     (gnus-newsgroup-ephemeral-ignored-charsets
7330                      gnus-newsgroup-ignored-charsets))
7331                 (gnus-group-read-ephemeral-group
7332                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7333                               (nndoc-article-type
7334                                ,(if force 'mbox 'guess))) t))
7335               ;; Make all postings to this group go to the parent group.
7336               (nconc (gnus-info-params (gnus-get-info name))
7337                      params)
7338             ;; Couldn't select this doc group.
7339             (switch-to-buffer buf)
7340             (gnus-set-global-variables)
7341             (gnus-configure-windows 'summary)
7342             (gnus-message 3 "Article couldn't be entered?"))
7343         (kill-buffer dig)))))
7344
7345 (defun gnus-summary-read-document (n)
7346   "Open a new group based on the current article(s).
7347 This will allow you to read digests and other similar
7348 documents as newsgroups.
7349 Obeys the standard process/prefix convention."
7350   (interactive "P")
7351   (let* ((articles (gnus-summary-work-articles n))
7352          (ogroup gnus-newsgroup-name)
7353          (params (append (gnus-info-params (gnus-get-info ogroup))
7354                          (list (cons 'to-group ogroup))))
7355          article group egroup groups vgroup)
7356     (while (setq article (pop articles))
7357       (setq group (format "%s-%d" gnus-newsgroup-name article))
7358       (gnus-summary-remove-process-mark article)
7359       (when (gnus-summary-display-article article)
7360         (save-excursion
7361           (with-temp-buffer
7362             (insert-buffer-substring gnus-original-article-buffer)
7363             ;; Remove some headers that may lead nndoc to make
7364             ;; the wrong guess.
7365             (message-narrow-to-head)
7366             (goto-char (point-min))
7367             (delete-matching-lines "^\\(Path\\):\\|^From ")
7368             (widen)
7369             (if (setq egroup
7370                       (gnus-group-read-ephemeral-group
7371                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7372                                      (nndoc-article-type guess))
7373                        t nil t))
7374                 (progn
7375                   ;; Make all postings to this group go to the parent group.
7376                   (nconc (gnus-info-params (gnus-get-info egroup))
7377                          params)
7378                   (push egroup groups))
7379               ;; Couldn't select this doc group.
7380               (gnus-error 3 "Article couldn't be entered"))))))
7381     ;; Now we have selected all the documents.
7382     (cond
7383      ((not groups)
7384       (error "None of the articles could be interpreted as documents"))
7385      ((gnus-group-read-ephemeral-group
7386        (setq vgroup (format
7387                      "nnvirtual:%s-%s" gnus-newsgroup-name
7388                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7389        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7390        t
7391        (cons (current-buffer) 'summary)))
7392      (t
7393       (error "Couldn't select virtual nndoc group")))))
7394
7395 (defun gnus-summary-isearch-article (&optional regexp-p)
7396   "Do incremental search forward on the current article.
7397 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7398   (interactive "P")
7399   (gnus-summary-select-article)
7400   (gnus-configure-windows 'article)
7401   (gnus-eval-in-buffer-window gnus-article-buffer
7402     (save-restriction
7403       (widen)
7404       (isearch-forward regexp-p))))
7405
7406 (defun gnus-summary-search-article-forward (regexp &optional backward)
7407   "Search for an article containing REGEXP forward.
7408 If BACKWARD, search backward instead."
7409   (interactive
7410    (list (read-string
7411           (format "Search article %s (regexp%s): "
7412                   (if current-prefix-arg "backward" "forward")
7413                   (if gnus-last-search-regexp
7414                       (concat ", default " gnus-last-search-regexp)
7415                     "")))
7416          current-prefix-arg))
7417   (if (string-equal regexp "")
7418       (setq regexp (or gnus-last-search-regexp ""))
7419     (setq gnus-last-search-regexp regexp)
7420     (setq gnus-article-before-search gnus-current-article))
7421   ;; Intentionally set gnus-last-article.
7422   (setq gnus-last-article gnus-article-before-search)
7423   (let ((gnus-last-article gnus-last-article))
7424     (if (gnus-summary-search-article regexp backward)
7425         (gnus-summary-show-thread)
7426       (error "Search failed: \"%s\"" regexp))))
7427
7428 (defun gnus-summary-search-article-backward (regexp)
7429   "Search for an article containing REGEXP backward."
7430   (interactive
7431    (list (read-string
7432           (format "Search article backward (regexp%s): "
7433                   (if gnus-last-search-regexp
7434                       (concat ", default " gnus-last-search-regexp)
7435                     "")))))
7436   (gnus-summary-search-article-forward regexp 'backward))
7437
7438 (defun gnus-summary-search-article (regexp &optional backward)
7439   "Search for an article containing REGEXP.
7440 Optional argument BACKWARD means do search for backward.
7441 `gnus-select-article-hook' is not called during the search."
7442   ;; We have to require this here to make sure that the following
7443   ;; dynamic binding isn't shadowed by autoloading.
7444   (require 'gnus-async)
7445   (require 'gnus-art)
7446   (let ((gnus-select-article-hook nil)  ;Disable hook.
7447         (gnus-article-prepare-hook nil)
7448         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7449         (gnus-use-article-prefetch nil)
7450         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7451         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7452         (sum (current-buffer))
7453         (gnus-display-mime-function nil)
7454         (found nil)
7455         point)
7456     (gnus-save-hidden-threads
7457       (gnus-summary-select-article)
7458       (set-buffer gnus-article-buffer)
7459       (goto-char (window-point (get-buffer-window (current-buffer))))
7460       (when backward
7461         (forward-line -1))
7462       (while (not found)
7463         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7464         (if (if backward
7465                 (re-search-backward regexp nil t)
7466               (re-search-forward regexp nil t))
7467             ;; We found the regexp.
7468             (progn
7469               (setq found 'found)
7470               (beginning-of-line)
7471               (set-window-start
7472                (get-buffer-window (current-buffer))
7473                (point))
7474               (forward-line 1)
7475               (set-window-point
7476                (get-buffer-window (current-buffer))
7477                (point))
7478               (set-buffer sum)
7479               (setq point (point)))
7480           ;; We didn't find it, so we go to the next article.
7481           (set-buffer sum)
7482           (setq found 'not)
7483           (while (eq found 'not)
7484             (if (not (if backward (gnus-summary-find-prev)
7485                        (gnus-summary-find-next)))
7486                 ;; No more articles.
7487                 (setq found t)
7488               ;; Select the next article and adjust point.
7489               (unless (gnus-summary-article-sparse-p
7490                        (gnus-summary-article-number))
7491                 (setq found nil)
7492                 (gnus-summary-select-article)
7493                 (set-buffer gnus-article-buffer)
7494                 (widen)
7495                 (goto-char (if backward (point-max) (point-min))))))))
7496       (gnus-message 7 ""))
7497     ;; Return whether we found the regexp.
7498     (when (eq found 'found)
7499       (goto-char point)
7500       (gnus-summary-show-thread)
7501       (gnus-summary-goto-subject gnus-current-article)
7502       (gnus-summary-position-point)
7503       t)))
7504
7505 (defun gnus-find-matching-articles (header regexp)
7506   "Return a list of all articles that match REGEXP on HEADER.
7507 This search includes all articles in the current group that Gnus has
7508 fetched headers for, whether they are displayed or not."
7509   (let ((articles nil)
7510         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7511         (case-fold-search t))
7512     (dolist (header gnus-newsgroup-headers)
7513       (when (string-match regexp (funcall func header))
7514         (push (mail-header-number header) articles)))
7515     (nreverse articles)))
7516
7517 (defun gnus-summary-find-matching (header regexp &optional backward unread
7518                                           not-case-fold)
7519   "Return a list of all articles that match REGEXP on HEADER.
7520 The search stars on the current article and goes forwards unless
7521 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7522 If UNREAD is non-nil, only unread articles will
7523 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7524 in the comparisons."
7525   (let ((case-fold-search (not not-case-fold))
7526         articles d func)
7527     (if (consp header)
7528         (if (eq (car header) 'extra)
7529             (setq func
7530                   `(lambda (h)
7531                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7532                          "")))
7533           (error "%s is an invalid header" header))
7534       (unless (fboundp (intern (concat "mail-header-" header)))
7535         (error "%s is not a valid header" header))
7536       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7537     (dolist (d (if (eq backward 'all)
7538                    gnus-newsgroup-data
7539                  (gnus-data-find-list
7540                   (gnus-summary-article-number)
7541                   (gnus-data-list backward))))
7542       (when (and (or (not unread)       ; We want all articles...
7543                      (gnus-data-unread-p d)) ; Or just unreads.
7544                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7545                  (string-match regexp
7546                                (funcall func (gnus-data-header d)))) ; Match.
7547         (push (gnus-data-number d) articles))) ; Success!
7548     (nreverse articles)))
7549
7550 (defun gnus-summary-execute-command (header regexp command &optional backward)
7551   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7552 If HEADER is an empty string (or nil), the match is done on the entire
7553 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7554   (interactive
7555    (list (let ((completion-ignore-case t))
7556            (completing-read
7557             "Header name: "
7558             (mapcar (lambda (header) (list (format "%s" header)))
7559                     (append
7560                      '("Number" "Subject" "From" "Lines" "Date"
7561                        "Message-ID" "Xref" "References" "Body")
7562                      gnus-extra-headers))
7563             nil 'require-match))
7564          (read-string "Regexp: ")
7565          (read-key-sequence "Command: ")
7566          current-prefix-arg))
7567   (when (equal header "Body")
7568     (setq header ""))
7569   ;; Hidden thread subtrees must be searched as well.
7570   (gnus-summary-show-all-threads)
7571   ;; We don't want to change current point nor window configuration.
7572   (save-excursion
7573     (save-window-excursion
7574       (gnus-message 6 "Executing %s..." (key-description command))
7575       ;; We'd like to execute COMMAND interactively so as to give arguments.
7576       (gnus-execute header regexp
7577                     `(call-interactively ',(key-binding command))
7578                     backward)
7579       (gnus-message 6 "Executing %s...done" (key-description command)))))
7580
7581 (defun gnus-summary-beginning-of-article ()
7582   "Scroll the article back to the beginning."
7583   (interactive)
7584   (gnus-summary-select-article)
7585   (gnus-configure-windows 'article)
7586   (gnus-eval-in-buffer-window gnus-article-buffer
7587     (widen)
7588     (goto-char (point-min))
7589     (when gnus-page-broken
7590       (gnus-narrow-to-page))))
7591
7592 (defun gnus-summary-end-of-article ()
7593   "Scroll to the end of the article."
7594   (interactive)
7595   (gnus-summary-select-article)
7596   (gnus-configure-windows 'article)
7597   (gnus-eval-in-buffer-window gnus-article-buffer
7598     (widen)
7599     (goto-char (point-max))
7600     (recenter -3)
7601     (when gnus-page-broken
7602       (gnus-narrow-to-page))))
7603
7604 (defun gnus-summary-print-article (&optional filename n)
7605   "Generate and print a PostScript image of the N next (mail) articles.
7606
7607 If N is negative, print the N previous articles.  If N is nil and articles
7608 have been marked with the process mark, print these instead.
7609
7610 If the optional first argument FILENAME is nil, send the image to the
7611 printer.  If FILENAME is a string, save the PostScript image in a file with
7612 that name.  If FILENAME is a number, prompt the user for the name of the file
7613 to save in."
7614   (interactive (list (ps-print-preprint current-prefix-arg)))
7615   (dolist (article (gnus-summary-work-articles n))
7616     (gnus-summary-select-article nil nil 'pseudo article)
7617     (gnus-eval-in-buffer-window gnus-article-buffer
7618       (let ((buffer (generate-new-buffer " *print*")))
7619         (unwind-protect
7620             (progn
7621               (copy-to-buffer buffer (point-min) (point-max))
7622               (set-buffer buffer)
7623               (gnus-article-delete-invisible-text)
7624               (when (gnus-visual-p 'article-highlight 'highlight)
7625                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7626                 ;; highlight.
7627                 (let ((gnus-article-buffer buffer))
7628                   (gnus-article-highlight-citation t)
7629                   (gnus-article-highlight-signature)))
7630               (let ((ps-left-header
7631                      (list
7632                       (concat "("
7633                               (mail-header-subject gnus-current-headers) ")")
7634                       (concat "("
7635                               (mail-header-from gnus-current-headers) ")")))
7636                     (ps-right-header
7637                      (list
7638                       "/pagenumberstring load"
7639                       (concat "("
7640                               (mail-header-date gnus-current-headers) ")"))))
7641                 (gnus-run-hooks 'gnus-ps-print-hook)
7642                 (save-excursion
7643                   (if window-system
7644                       (ps-spool-buffer-with-faces)
7645                     (ps-spool-buffer)))))
7646           (kill-buffer buffer))))
7647     (gnus-summary-remove-process-mark article))
7648   (ps-despool filename))
7649
7650 (defun gnus-summary-show-article (&optional arg)
7651   "Force re-fetching of the current article.
7652 If ARG (the prefix) is a number, show the article with the charset
7653 defined in `gnus-summary-show-article-charset-alist', or the charset
7654 inputed.
7655 If ARG (the prefix) is non-nil and not a number, show the raw article
7656 without any article massaging functions being run."
7657   (interactive "P")
7658   (cond
7659    ((numberp arg)
7660     (let ((gnus-newsgroup-charset
7661            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7662                (mm-read-coding-system "Charset: ")))
7663           (gnus-newsgroup-ignored-charsets 'gnus-all))
7664       (gnus-summary-select-article nil 'force)
7665       (let ((deps gnus-newsgroup-dependencies)
7666             head header)
7667         (save-excursion
7668           (set-buffer gnus-original-article-buffer)
7669           (save-restriction
7670             (message-narrow-to-head)
7671             (setq head (buffer-string)))
7672           (with-temp-buffer
7673             (insert (format "211 %d Article retrieved.\n"
7674                             (cdr gnus-article-current)))
7675             (insert head)
7676             (insert ".\n")
7677             (let ((nntp-server-buffer (current-buffer)))
7678               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7679         (gnus-data-set-header
7680          (gnus-data-find (cdr gnus-article-current))
7681          header)
7682         (gnus-summary-update-article-line
7683          (cdr gnus-article-current) header))))
7684    ((not arg)
7685     ;; Select the article the normal way.
7686     (gnus-summary-select-article nil 'force))
7687    (t
7688     ;; We have to require this here to make sure that the following
7689     ;; dynamic binding isn't shadowed by autoloading.
7690     (require 'gnus-async)
7691     (require 'gnus-art)
7692     ;; Bind the article treatment functions to nil.
7693     (let ((gnus-have-all-headers t)
7694           gnus-article-prepare-hook
7695           gnus-article-decode-hook
7696           gnus-display-mime-function
7697           gnus-break-pages)
7698       ;; Destroy any MIME parts.
7699       (when (gnus-buffer-live-p gnus-article-buffer)
7700         (save-excursion
7701           (set-buffer gnus-article-buffer)
7702           (mm-destroy-parts gnus-article-mime-handles)
7703           ;; Set it to nil for safety reason.
7704           (setq gnus-article-mime-handle-alist nil)
7705           (setq gnus-article-mime-handles nil)))
7706       (gnus-summary-select-article nil 'force))))
7707   (gnus-summary-goto-subject gnus-current-article)
7708   (gnus-summary-position-point))
7709
7710 (defun gnus-summary-show-raw-article ()
7711   "Show the raw article without any article massaging functions being run."
7712   (interactive)
7713   (gnus-summary-show-article t))
7714
7715 (defun gnus-summary-verbose-headers (&optional arg)
7716   "Toggle permanent full header display.
7717 If ARG is a positive number, turn header display on.
7718 If ARG is a negative number, turn header display off."
7719   (interactive "P")
7720   (setq gnus-show-all-headers
7721         (cond ((or (not (numberp arg))
7722                    (zerop arg))
7723                (not gnus-show-all-headers))
7724               ((natnump arg)
7725                t)))
7726   (gnus-summary-show-article))
7727
7728 (defun gnus-summary-toggle-header (&optional arg)
7729   "Show the headers if they are hidden, or hide them if they are shown.
7730 If ARG is a positive number, show the entire header.
7731 If ARG is a negative number, hide the unwanted header lines."
7732   (interactive "P")
7733   (save-excursion
7734     (set-buffer gnus-article-buffer)
7735     (save-restriction
7736       (let* ((buffer-read-only nil)
7737              (inhibit-point-motion-hooks t)
7738              hidden e)
7739         (setq hidden
7740               (if (numberp arg)
7741                   (>= arg 0)
7742                 (save-restriction
7743                   (article-narrow-to-head)
7744                   (gnus-article-hidden-text-p 'headers))))
7745         (goto-char (point-min))
7746         (when (search-forward "\n\n" nil t)
7747           (delete-region (point-min) (1- (point))))
7748         (goto-char (point-min))
7749         (save-excursion
7750           (set-buffer gnus-original-article-buffer)
7751           (goto-char (point-min))
7752           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7753         (insert-buffer-substring gnus-original-article-buffer 1 e)
7754         (save-restriction
7755           (narrow-to-region (point-min) (point))
7756           (article-decode-encoded-words)
7757           (if  hidden
7758               (let ((gnus-treat-hide-headers nil)
7759                     (gnus-treat-hide-boring-headers nil))
7760                 (setq gnus-article-wash-types
7761                       (delq 'headers gnus-article-wash-types))
7762                 (gnus-treat-article 'head))
7763             (gnus-treat-article 'head)))
7764         (gnus-set-mode-line 'article)))))
7765
7766 (defun gnus-summary-show-all-headers ()
7767   "Make all header lines visible."
7768   (interactive)
7769   (gnus-summary-toggle-header 1))
7770
7771 (defun gnus-summary-caesar-message (&optional arg)
7772   "Caesar rotate the current article by 13.
7773 The numerical prefix specifies how many places to rotate each letter
7774 forward."
7775   (interactive "P")
7776   (gnus-summary-select-article)
7777   (let ((mail-header-separator ""))
7778     (gnus-eval-in-buffer-window gnus-article-buffer
7779       (save-restriction
7780         (widen)
7781         (let ((start (window-start))
7782               buffer-read-only)
7783           (message-caesar-buffer-body arg)
7784           (set-window-start (get-buffer-window (current-buffer)) start))))))
7785
7786 (defun gnus-summary-stop-page-breaking ()
7787   "Stop page breaking in the current article."
7788   (interactive)
7789   (gnus-summary-select-article)
7790   (gnus-eval-in-buffer-window gnus-article-buffer
7791     (widen)
7792     (when (gnus-visual-p 'page-marker)
7793       (let ((buffer-read-only nil))
7794         (gnus-remove-text-with-property 'gnus-prev)
7795         (gnus-remove-text-with-property 'gnus-next))
7796       (setq gnus-page-broken nil))))
7797
7798 (defun gnus-summary-move-article (&optional n to-newsgroup
7799                                             select-method action)
7800   "Move the current article to a different newsgroup.
7801 If N is a positive number, move the N next articles.
7802 If N is a negative number, move the N previous articles.
7803 If N is nil and any articles have been marked with the process mark,
7804 move those articles instead.
7805 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7806 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7807 re-spool using this method.
7808
7809 For this function to work, both the current newsgroup and the
7810 newsgroup that you want to move to have to support the `request-move'
7811 and `request-accept' functions.
7812
7813 ACTION can be either `move' (the default), `crosspost' or `copy'."
7814   (interactive "P")
7815   (unless action
7816     (setq action 'move))
7817   ;; Disable marking as read.
7818   (let (gnus-mark-article-hook)
7819     (save-window-excursion
7820       (gnus-summary-select-article)))
7821   ;; Check whether the source group supports the required functions.
7822   (cond ((and (eq action 'move)
7823               (not (gnus-check-backend-function
7824                     'request-move-article gnus-newsgroup-name)))
7825          (error "The current group does not support article moving"))
7826         ((and (eq action 'crosspost)
7827               (not (gnus-check-backend-function
7828                     'request-replace-article gnus-newsgroup-name)))
7829          (error "The current group does not support article editing")))
7830   (let ((articles (gnus-summary-work-articles n))
7831         (prefix (if (gnus-check-backend-function
7832                     'request-move-article gnus-newsgroup-name)
7833                     (gnus-group-real-prefix gnus-newsgroup-name)
7834                   ""))
7835         (names '((move "Move" "Moving")
7836                  (copy "Copy" "Copying")
7837                  (crosspost "Crosspost" "Crossposting")))
7838         (copy-buf (save-excursion
7839                     (nnheader-set-temp-buffer " *copy article*")))
7840         art-group to-method new-xref article to-groups)
7841     (unless (assq action names)
7842       (error "Unknown action %s" action))
7843     ;; Read the newsgroup name.
7844     (when (and (not to-newsgroup)
7845                (not select-method))
7846       (setq to-newsgroup
7847             (gnus-read-move-group-name
7848              (cadr (assq action names))
7849              (symbol-value (intern (format "gnus-current-%s-group" action)))
7850              articles prefix))
7851       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7852     (setq to-method (or select-method
7853                         (gnus-server-to-method
7854                          (gnus-group-method to-newsgroup))))
7855     ;; Check the method we are to move this article to...
7856     (unless (gnus-check-backend-function
7857              'request-accept-article (car to-method))
7858       (error "%s does not support article copying" (car to-method)))
7859     (unless (gnus-check-server to-method)
7860       (error "Can't open server %s" (car to-method)))
7861     (gnus-message 6 "%s to %s: %s..."
7862                   (caddr (assq action names))
7863                   (or (car select-method) to-newsgroup) articles)
7864     (while articles
7865       (setq article (pop articles))
7866       (setq
7867        art-group
7868        (cond
7869         ;; Move the article.
7870         ((eq action 'move)
7871          ;; Remove this article from future suppression.
7872          (gnus-dup-unsuppress-article article)
7873          (gnus-request-move-article
7874           article                       ; Article to move
7875           gnus-newsgroup-name           ; From newsgroup
7876           (nth 1 (gnus-find-method-for-group
7877                   gnus-newsgroup-name)) ; Server
7878           (list 'gnus-request-accept-article
7879                 to-newsgroup (list 'quote select-method)
7880                 (not articles) t)       ; Accept form
7881           (not articles)))              ; Only save nov last time
7882         ;; Copy the article.
7883         ((eq action 'copy)
7884          (save-excursion
7885            (set-buffer copy-buf)
7886            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7887              (gnus-request-accept-article
7888               to-newsgroup select-method (not articles) t))))
7889         ;; Crosspost the article.
7890         ((eq action 'crosspost)
7891          (let ((xref (message-tokenize-header
7892                       (mail-header-xref (gnus-summary-article-header article))
7893                       " ")))
7894            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7895                                   ":" article))
7896            (unless xref
7897              (setq xref (list (system-name))))
7898            (setq new-xref
7899                  (concat
7900                   (mapconcat 'identity
7901                              (delete "Xref:" (delete new-xref xref))
7902                              " ")
7903                   " " new-xref))
7904            (save-excursion
7905              (set-buffer copy-buf)
7906              ;; First put the article in the destination group.
7907              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7908              (when (consp (setq art-group
7909                                 (gnus-request-accept-article
7910                                  to-newsgroup select-method (not articles))))
7911                (setq new-xref (concat new-xref " " (car art-group)
7912                                       ":" (cdr art-group)))
7913                ;; Now we have the new Xrefs header, so we insert
7914                ;; it and replace the new article.
7915                (nnheader-replace-header "Xref" new-xref)
7916                (gnus-request-replace-article
7917                 (cdr art-group) to-newsgroup (current-buffer))
7918                art-group))))))
7919       (cond
7920        ((not art-group)
7921         (gnus-message 1 "Couldn't %s article %s: %s"
7922                       (cadr (assq action names)) article
7923                       (nnheader-get-report (car to-method))))
7924        ((eq art-group 'junk)
7925         (when (eq action 'move)
7926           (gnus-summary-mark-article article gnus-canceled-mark)
7927           (gnus-message 4 "Deleted article %s" article)))
7928        (t
7929         (let* ((pto-group (gnus-group-prefixed-name
7930                            (car art-group) to-method))
7931                (entry
7932                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7933                (info (nth 2 entry))
7934                (to-group (gnus-info-group info))
7935                to-marks)
7936           ;; Update the group that has been moved to.
7937           (when (and info
7938                      (memq action '(move copy)))
7939             (unless (member to-group to-groups)
7940               (push to-group to-groups))
7941
7942             (unless (memq article gnus-newsgroup-unreads)
7943               (push 'read to-marks)
7944               (gnus-info-set-read
7945                info (gnus-add-to-range (gnus-info-read info)
7946                                        (list (cdr art-group)))))
7947
7948             ;; See whether the article is to be put in the cache.
7949             (let ((marks gnus-article-mark-lists)
7950                   (to-article (cdr art-group)))
7951
7952               ;; Enter the article into the cache in the new group,
7953               ;; if that is required.
7954               (when gnus-use-cache
7955                 (gnus-cache-possibly-enter-article
7956                  to-group to-article
7957                  (memq article gnus-newsgroup-marked)
7958                  (memq article gnus-newsgroup-dormant)
7959                  (memq article gnus-newsgroup-unreads)))
7960
7961               (when gnus-preserve-marks
7962                 ;; Copy any marks over to the new group.
7963                 (when (and (equal to-group gnus-newsgroup-name)
7964                            (not (memq article gnus-newsgroup-unreads)))
7965                   ;; Mark this article as read in this group.
7966                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7967                   (setcdr (gnus-active to-group) to-article)
7968                   (setcdr gnus-newsgroup-active to-article))
7969
7970                 (while marks
7971                   (when (memq article (symbol-value
7972                                        (intern (format "gnus-newsgroup-%s"
7973                                                        (caar marks)))))
7974                     (push (cdar marks) to-marks)
7975                     ;; If the other group is the same as this group,
7976                     ;; then we have to add the mark to the list.
7977                     (when (equal to-group gnus-newsgroup-name)
7978                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7979                            (cons to-article
7980                                  (symbol-value
7981                                   (intern (format "gnus-newsgroup-%s"
7982                                                   (caar marks)))))))
7983                     ;; Copy the marks to other group.
7984                     (gnus-add-marked-articles
7985                      to-group (cdar marks) (list to-article) info))
7986                   (setq marks (cdr marks)))
7987
7988                 (gnus-request-set-mark to-group (list (list (list to-article)
7989                                                             'set
7990                                                             to-marks))))
7991
7992               (gnus-dribble-enter
7993                (concat "(gnus-group-set-info '"
7994                        (gnus-prin1-to-string (gnus-get-info to-group))
7995                        ")"))))
7996
7997           ;; Update the Xref header in this article to point to
7998           ;; the new crossposted article we have just created.
7999           (when (eq action 'crosspost)
8000             (save-excursion
8001               (set-buffer copy-buf)
8002               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8003               (nnheader-replace-header "Xref" new-xref)
8004               (gnus-request-replace-article
8005                article gnus-newsgroup-name (current-buffer)))))
8006
8007         ;;;!!!Why is this necessary?
8008         (set-buffer gnus-summary-buffer)
8009
8010         (gnus-summary-goto-subject article)
8011         (when (eq action 'move)
8012           (gnus-summary-mark-article article gnus-canceled-mark))))
8013       (gnus-summary-remove-process-mark article))
8014     ;; Re-activate all groups that have been moved to.
8015     (while to-groups
8016       (save-excursion
8017         (set-buffer gnus-group-buffer)
8018         (when (gnus-group-goto-group (car to-groups) t)
8019           (gnus-group-get-new-news-this-group 1 t))
8020         (pop to-groups)))
8021
8022     (gnus-kill-buffer copy-buf)
8023     (gnus-summary-position-point)
8024     (gnus-set-mode-line 'summary)))
8025
8026 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8027   "Move the current article to a different newsgroup.
8028 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8029 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8030 re-spool using this method."
8031   (interactive "P")
8032   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8033
8034 (defun gnus-summary-crosspost-article (&optional n)
8035   "Crosspost the current article to some other group."
8036   (interactive "P")
8037   (gnus-summary-move-article n nil nil 'crosspost))
8038
8039 (defcustom gnus-summary-respool-default-method nil
8040   "Default method for respooling an article.
8041 If nil, use to the current newsgroup method."
8042   :type '(choice (gnus-select-method :value (nnml ""))
8043                  (const nil))
8044   :group 'gnus-summary-mail)
8045
8046 (defun gnus-summary-respool-article (&optional n method)
8047   "Respool the current article.
8048 The article will be squeezed through the mail spooling process again,
8049 which means that it will be put in some mail newsgroup or other
8050 depending on `nnmail-split-methods'.
8051 If N is a positive number, respool the N next articles.
8052 If N is a negative number, respool the N previous articles.
8053 If N is nil and any articles have been marked with the process mark,
8054 respool those articles instead.
8055
8056 Respooling can be done both from mail groups and \"real\" newsgroups.
8057 In the former case, the articles in question will be moved from the
8058 current group into whatever groups they are destined to.  In the
8059 latter case, they will be copied into the relevant groups."
8060   (interactive
8061    (list current-prefix-arg
8062          (let* ((methods (gnus-methods-using 'respool))
8063                 (methname
8064                  (symbol-name (or gnus-summary-respool-default-method
8065                                   (car (gnus-find-method-for-group
8066                                         gnus-newsgroup-name)))))
8067                 (method
8068                  (gnus-completing-read
8069                   methname "What backend do you want to use when respooling?"
8070                   methods nil t nil 'gnus-mail-method-history))
8071                 ms)
8072            (cond
8073             ((zerop (length (setq ms (gnus-servers-using-backend
8074                                       (intern method)))))
8075              (list (intern method) ""))
8076             ((= 1 (length ms))
8077              (car ms))
8078             (t
8079              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8080                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8081                            ms-alist))))))))
8082   (unless method
8083     (error "No method given for respooling"))
8084   (if (assoc (symbol-name
8085               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8086              (gnus-methods-using 'respool))
8087       (gnus-summary-move-article n nil method)
8088     (gnus-summary-copy-article n nil method)))
8089
8090 (defun gnus-summary-import-article (file &optional edit)
8091   "Import an arbitrary file into a mail newsgroup."
8092   (interactive "fImport file: \nP")
8093   (let ((group gnus-newsgroup-name)
8094         (now (current-time))
8095         atts lines group-art)
8096     (unless (gnus-check-backend-function 'request-accept-article group)
8097       (error "%s does not support article importing" group))
8098     (or (file-readable-p file)
8099         (not (file-regular-p file))
8100         (error "Can't read %s" file))
8101     (save-excursion
8102       (set-buffer (gnus-get-buffer-create " *import file*"))
8103       (erase-buffer)
8104       (nnheader-insert-file-contents file)
8105       (goto-char (point-min))
8106       (if (nnheader-article-p)
8107           (save-restriction
8108             (goto-char (point-min))
8109             (search-forward "\n\n" nil t)
8110             (narrow-to-region (point-min) (1- (point)))
8111             (goto-char (point-min))
8112             (unless (re-search-forward "^date:" nil t)
8113               (goto-char (point-max))
8114               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8115         ;; This doesn't look like an article, so we fudge some headers.
8116         (setq atts (file-attributes file)
8117               lines (count-lines (point-min) (point-max)))
8118         (insert "From: " (read-string "From: ") "\n"
8119                 "Subject: " (read-string "Subject: ") "\n"
8120                 "Date: " (message-make-date (nth 5 atts)) "\n"
8121                 "Message-ID: " (message-make-message-id) "\n"
8122                 "Lines: " (int-to-string lines) "\n"
8123                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8124       (setq group-art (gnus-request-accept-article group nil t))
8125       (kill-buffer (current-buffer)))
8126     (setq gnus-newsgroup-active (gnus-activate-group group))
8127     (forward-line 1)
8128     (gnus-summary-goto-article (cdr group-art) nil t)
8129     (when edit
8130       (gnus-summary-edit-article))))
8131
8132 (defun gnus-summary-create-article ()
8133   "Create an article in a mail newsgroup."
8134   (interactive)
8135   (let ((group gnus-newsgroup-name)
8136         (now (current-time))
8137         group-art)
8138     (unless (gnus-check-backend-function 'request-accept-article group)
8139       (error "%s does not support article importing" group))
8140     (save-excursion
8141       (set-buffer (gnus-get-buffer-create " *import file*"))
8142       (erase-buffer)
8143       (goto-char (point-min))
8144       ;; This doesn't look like an article, so we fudge some headers.
8145       (insert "From: " (read-string "From: ") "\n"
8146               "Subject: " (read-string "Subject: ") "\n"
8147               "Date: " (message-make-date now) "\n"
8148               "Message-ID: " (message-make-message-id) "\n")
8149       (setq group-art (gnus-request-accept-article group nil t))
8150       (kill-buffer (current-buffer)))
8151     (setq gnus-newsgroup-active (gnus-activate-group group))
8152     (forward-line 1)
8153     (gnus-summary-goto-article (cdr group-art) nil t)
8154     (gnus-summary-edit-article)))
8155
8156 (defun gnus-summary-article-posted-p ()
8157   "Say whether the current (mail) article is available from news as well.
8158 This will be the case if the article has both been mailed and posted."
8159   (interactive)
8160   (let ((id (mail-header-references (gnus-summary-article-header)))
8161         (gnus-override-method (car (gnus-refer-article-methods))))
8162     (if (gnus-request-head id "")
8163         (gnus-message 2 "The current message was found on %s"
8164                       gnus-override-method)
8165       (gnus-message 2 "The current message couldn't be found on %s"
8166                     gnus-override-method)
8167       nil)))
8168
8169 (defun gnus-summary-expire-articles (&optional now)
8170   "Expire all articles that are marked as expirable in the current group."
8171   (interactive)
8172   (when (gnus-check-backend-function
8173          'request-expire-articles gnus-newsgroup-name)
8174     ;; This backend supports expiry.
8175     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8176            (expirable (if total
8177                           (progn
8178                             ;; We need to update the info for
8179                             ;; this group for `gnus-list-of-read-articles'
8180                             ;; to give us the right answer.
8181                             (gnus-run-hooks 'gnus-exit-group-hook)
8182                             (gnus-summary-update-info)
8183                             (gnus-list-of-read-articles gnus-newsgroup-name))
8184                         (setq gnus-newsgroup-expirable
8185                               (sort gnus-newsgroup-expirable '<))))
8186            (expiry-wait (if now 'immediate
8187                           (gnus-group-find-parameter
8188                            gnus-newsgroup-name 'expiry-wait)))
8189            (nnmail-expiry-target
8190             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8191                 nnmail-expiry-target))
8192            es)
8193       (when expirable
8194         ;; There are expirable articles in this group, so we run them
8195         ;; through the expiry process.
8196         (gnus-message 6 "Expiring articles...")
8197         (unless (gnus-check-group gnus-newsgroup-name)
8198           (error "Can't open server for %s" gnus-newsgroup-name))
8199         ;; The list of articles that weren't expired is returned.
8200         (save-excursion
8201           (if expiry-wait
8202               (let ((nnmail-expiry-wait-function nil)
8203                     (nnmail-expiry-wait expiry-wait))
8204                 (setq es (gnus-request-expire-articles
8205                           expirable gnus-newsgroup-name)))
8206             (setq es (gnus-request-expire-articles
8207                       expirable gnus-newsgroup-name)))
8208           (unless total
8209             (setq gnus-newsgroup-expirable es))
8210           ;; We go through the old list of expirable, and mark all
8211           ;; really expired articles as nonexistent.
8212           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8213             (let ((gnus-use-cache nil))
8214               (while expirable
8215                 (unless (memq (car expirable) es)
8216                   (when (gnus-data-find (car expirable))
8217                     (gnus-summary-mark-article
8218                      (car expirable) gnus-canceled-mark)))
8219                 (setq expirable (cdr expirable))))))
8220         (gnus-message 6 "Expiring articles...done")))))
8221
8222 (defun gnus-summary-expire-articles-now ()
8223   "Expunge all expirable articles in the current group.
8224 This means that *all* articles that are marked as expirable will be
8225 deleted forever, right now."
8226   (interactive)
8227   (or gnus-expert-user
8228       (gnus-yes-or-no-p
8229        "Are you really, really, really sure you want to delete all these messages? ")
8230       (error "Phew!"))
8231   (gnus-summary-expire-articles t))
8232
8233 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8234 (defun gnus-summary-delete-article (&optional n)
8235   "Delete the N next (mail) articles.
8236 This command actually deletes articles.  This is not a marking
8237 command.  The article will disappear forever from your life, never to
8238 return.
8239 If N is negative, delete backwards.
8240 If N is nil and articles have been marked with the process mark,
8241 delete these instead."
8242   (interactive "P")
8243   (unless (gnus-check-backend-function 'request-expire-articles
8244                                        gnus-newsgroup-name)
8245     (error "The current newsgroup does not support article deletion"))
8246   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8247     (error "Couldn't open server"))
8248   ;; Compute the list of articles to delete.
8249   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8250         not-deleted)
8251     (if (and gnus-novice-user
8252              (not (gnus-yes-or-no-p
8253                    (format "Do you really want to delete %s forever? "
8254                            (if (> (length articles) 1)
8255                                (format "these %s articles" (length articles))
8256                              "this article")))))
8257         ()
8258       ;; Delete the articles.
8259       (setq not-deleted (gnus-request-expire-articles
8260                          articles gnus-newsgroup-name 'force))
8261       (while articles
8262         (gnus-summary-remove-process-mark (car articles))
8263         ;; The backend might not have been able to delete the article
8264         ;; after all.
8265         (unless (memq (car articles) not-deleted)
8266           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8267         (setq articles (cdr articles)))
8268       (when not-deleted
8269         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8270     (gnus-summary-position-point)
8271     (gnus-set-mode-line 'summary)
8272     not-deleted))
8273
8274 (defun gnus-summary-edit-article (&optional arg)
8275   "Edit the current article.
8276 This will have permanent effect only in mail groups.
8277 If ARG is nil, edit the decoded articles.
8278 If ARG is 1, edit the raw articles.
8279 If ARG is 2, edit the raw articles even in read-only groups.
8280 If ARG is 3, edit the articles with the current handles.
8281 Otherwise, allow editing of articles even in read-only
8282 groups."
8283   (interactive "P")
8284   (let (force raw current-handles)
8285     (cond
8286      ((null arg))
8287      ((eq arg 1) (setq raw t))
8288      ((eq arg 2) (setq raw t
8289                        force t))
8290      ((eq arg 3) (setq current-handles
8291                        (and (gnus-buffer-live-p gnus-article-buffer)
8292                             (with-current-buffer gnus-article-buffer
8293                               (prog1
8294                                   gnus-article-mime-handles
8295                                   (setq gnus-article-mime-handles nil))))))
8296      (t (setq force t)))
8297     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8298         (error "Can't edit the raw article in group nndraft:drafts"))
8299     (save-excursion
8300       (set-buffer gnus-summary-buffer)
8301       (let ((mail-parse-charset gnus-newsgroup-charset)
8302             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8303         (gnus-set-global-variables)
8304         (when (and (not force)
8305                    (gnus-group-read-only-p))
8306           (error "The current newsgroup does not support article editing"))
8307         (gnus-summary-show-article t)
8308         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8309           (with-current-buffer gnus-article-buffer
8310             (mm-enable-multibyte-mule4)))
8311         (if (equal gnus-newsgroup-name "nndraft:drafts")
8312             (setq raw t))
8313         (gnus-article-edit-article
8314          (if raw 'ignore
8315            `(lambda ()
8316               (let ((mbl mml-buffer-list))
8317                 (setq mml-buffer-list nil)
8318                 (mime-to-mml ,'current-handles)
8319                 (let ((mbl1 mml-buffer-list))
8320                   (setq mml-buffer-list mbl)
8321                   (set (make-local-variable 'mml-buffer-list) mbl1))
8322                 (make-local-hook 'kill-buffer-hook)
8323                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8324          `(lambda (no-highlight)
8325             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8326                   (message-options message-options)
8327                   (message-options-set-recipient)
8328                   (mail-parse-ignored-charsets
8329                    ',gnus-newsgroup-ignored-charsets))
8330               ,(if (not raw) '(progn
8331                                 (mml-to-mime)
8332                                 (mml-destroy-buffers)
8333                                 (remove-hook 'kill-buffer-hook
8334                                              'mml-destroy-buffers t)
8335                                 (kill-local-variable 'mml-buffer-list)))
8336               (gnus-summary-edit-article-done
8337                ,(or (mail-header-references gnus-current-headers) "")
8338                ,(gnus-group-read-only-p)
8339                ,gnus-summary-buffer no-highlight))))))))
8340
8341 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8342
8343 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8344                                                  no-highlight)
8345   "Make edits to the current article permanent."
8346   (interactive)
8347   (save-excursion
8348     ;; The buffer restriction contains the entire article if it exists.
8349     (when (article-goto-body)
8350       (let ((lines (count-lines (point) (point-max)))
8351             (length (- (point-max) (point)))
8352             (case-fold-search t)
8353             (body (copy-marker (point))))
8354         (goto-char (point-min))
8355         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8356           (delete-region (match-beginning 1) (match-end 1))
8357           (insert (number-to-string length)))
8358         (goto-char (point-min))
8359         (when (re-search-forward
8360                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8361           (delete-region (match-beginning 1) (match-end 1))
8362           (insert (number-to-string length)))
8363         (goto-char (point-min))
8364         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8365           (delete-region (match-beginning 1) (match-end 1))
8366           (insert (number-to-string lines))))))
8367   ;; Replace the article.
8368   (let ((buf (current-buffer)))
8369     (with-temp-buffer
8370       (insert-buffer-substring buf)
8371
8372       (if (and (not read-only)
8373                (not (gnus-request-replace-article
8374                      (cdr gnus-article-current) (car gnus-article-current)
8375                      (current-buffer) t)))
8376           (error "Couldn't replace article")
8377         ;; Update the summary buffer.
8378         (if (and references
8379                  (equal (message-tokenize-header references " ")
8380                         (message-tokenize-header
8381                          (or (message-fetch-field "references") "") " ")))
8382             ;; We only have to update this line.
8383             (save-excursion
8384               (save-restriction
8385                 (message-narrow-to-head)
8386                 (let ((head (buffer-string))
8387                       header)
8388                   (with-temp-buffer
8389                     (insert (format "211 %d Article retrieved.\n"
8390                                     (cdr gnus-article-current)))
8391                     (insert head)
8392                     (insert ".\n")
8393                     (let ((nntp-server-buffer (current-buffer)))
8394                       (setq header (car (gnus-get-newsgroup-headers
8395                                          (save-excursion
8396                                            (set-buffer gnus-summary-buffer)
8397                                            gnus-newsgroup-dependencies)
8398                                          t))))
8399                     (save-excursion
8400                       (set-buffer gnus-summary-buffer)
8401                       (gnus-data-set-header
8402                        (gnus-data-find (cdr gnus-article-current))
8403                        header)
8404                       (gnus-summary-update-article-line
8405                        (cdr gnus-article-current) header))))))
8406           ;; Update threads.
8407           (set-buffer (or buffer gnus-summary-buffer))
8408           (gnus-summary-update-article (cdr gnus-article-current)))
8409         ;; Prettify the article buffer again.
8410         (unless no-highlight
8411           (save-excursion
8412             (set-buffer gnus-article-buffer)
8413             ;;;!!! Fix this -- article should be rehighlighted.
8414             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8415             (set-buffer gnus-original-article-buffer)
8416             (gnus-request-article
8417              (cdr gnus-article-current)
8418              (car gnus-article-current) (current-buffer))))
8419         ;; Prettify the summary buffer line.
8420         (when (gnus-visual-p 'summary-highlight 'highlight)
8421           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8422
8423 (defun gnus-summary-edit-wash (key)
8424   "Perform editing command KEY in the article buffer."
8425   (interactive
8426    (list
8427     (progn
8428       (message "%s" (concat (this-command-keys) "- "))
8429       (read-char))))
8430   (message "")
8431   (gnus-summary-edit-article)
8432   (execute-kbd-macro (concat (this-command-keys) key))
8433   (gnus-article-edit-done))
8434
8435 ;;; Respooling
8436
8437 (defun gnus-summary-respool-query (&optional silent trace)
8438   "Query where the respool algorithm would put this article."
8439   (interactive)
8440   (let (gnus-mark-article-hook)
8441     (gnus-summary-select-article)
8442     (save-excursion
8443       (set-buffer gnus-original-article-buffer)
8444       (save-restriction
8445         (message-narrow-to-head)
8446         (let ((groups (nnmail-article-group 'identity trace)))
8447           (unless silent
8448             (if groups
8449                 (message "This message would go to %s"
8450                          (mapconcat 'car groups ", "))
8451               (message "This message would go to no groups"))
8452             groups))))))
8453
8454 (defun gnus-summary-respool-trace ()
8455   "Trace where the respool algorithm would put this article.
8456 Display a buffer showing all fancy splitting patterns which matched."
8457   (interactive)
8458   (gnus-summary-respool-query nil t))
8459
8460 ;; Summary marking commands.
8461
8462 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8463   "Mark articles which has the same subject as read, and then select the next.
8464 If UNMARK is positive, remove any kind of mark.
8465 If UNMARK is negative, tick articles."
8466   (interactive "P")
8467   (when unmark
8468     (setq unmark (prefix-numeric-value unmark)))
8469   (let ((count
8470          (gnus-summary-mark-same-subject
8471           (gnus-summary-article-subject) unmark)))
8472     ;; Select next unread article.  If auto-select-same mode, should
8473     ;; select the first unread article.
8474     (gnus-summary-next-article t (and gnus-auto-select-same
8475                                       (gnus-summary-article-subject)))
8476     (gnus-message 7 "%d article%s marked as %s"
8477                   count (if (= count 1) " is" "s are")
8478                   (if unmark "unread" "read"))))
8479
8480 (defun gnus-summary-kill-same-subject (&optional unmark)
8481   "Mark articles which has the same subject as read.
8482 If UNMARK is positive, remove any kind of mark.
8483 If UNMARK is negative, tick articles."
8484   (interactive "P")
8485   (when unmark
8486     (setq unmark (prefix-numeric-value unmark)))
8487   (let ((count
8488          (gnus-summary-mark-same-subject
8489           (gnus-summary-article-subject) unmark)))
8490     ;; If marked as read, go to next unread subject.
8491     (when (null unmark)
8492       ;; Go to next unread subject.
8493       (gnus-summary-next-subject 1 t))
8494     (gnus-message 7 "%d articles are marked as %s"
8495                   count (if unmark "unread" "read"))))
8496
8497 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8498   "Mark articles with same SUBJECT as read, and return marked number.
8499 If optional argument UNMARK is positive, remove any kinds of marks.
8500 If optional argument UNMARK is negative, mark articles as unread instead."
8501   (let ((count 1))
8502     (save-excursion
8503       (cond
8504        ((null unmark)                   ; Mark as read.
8505         (while (and
8506                 (progn
8507                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8508                   (gnus-summary-show-thread) t)
8509                 (gnus-summary-find-subject subject))
8510           (setq count (1+ count))))
8511        ((> unmark 0)                    ; Tick.
8512         (while (and
8513                 (progn
8514                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8515                   (gnus-summary-show-thread) t)
8516                 (gnus-summary-find-subject subject))
8517           (setq count (1+ count))))
8518        (t                               ; Mark as unread.
8519         (while (and
8520                 (progn
8521                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8522                   (gnus-summary-show-thread) t)
8523                 (gnus-summary-find-subject subject))
8524           (setq count (1+ count)))))
8525       (gnus-set-mode-line 'summary)
8526       ;; Return the number of marked articles.
8527       count)))
8528
8529 (defun gnus-summary-mark-as-processable (n &optional unmark)
8530   "Set the process mark on the next N articles.
8531 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8532 the process mark instead.  The difference between N and the actual
8533 number of articles marked is returned."
8534   (interactive "P")
8535   (if (and (null n) (gnus-region-active-p))
8536       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8537     (setq n (prefix-numeric-value n))
8538     (let ((backward (< n 0))
8539           (n (abs n)))
8540       (while (and
8541               (> n 0)
8542               (if unmark
8543                 (gnus-summary-remove-process-mark
8544                  (gnus-summary-article-number))
8545                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8546               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8547         (setq n (1- n)))
8548       (when (/= 0 n)
8549         (gnus-message 7 "No more articles"))
8550       (gnus-summary-recenter)
8551       (gnus-summary-position-point)
8552       n)))
8553
8554 (defun gnus-summary-unmark-as-processable (n)
8555   "Remove the process mark from the next N articles.
8556 If N is negative, unmark backward instead.  The difference between N and
8557 the actual number of articles unmarked is returned."
8558   (interactive "P")
8559   (gnus-summary-mark-as-processable n t))
8560
8561 (defun gnus-summary-unmark-all-processable ()
8562   "Remove the process mark from all articles."
8563   (interactive)
8564   (save-excursion
8565     (while gnus-newsgroup-processable
8566       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8567   (gnus-summary-position-point))
8568
8569 (defun gnus-summary-add-mark (article type)
8570   "Mark ARTICLE with a mark of TYPE."
8571   (let ((vtype (car (assq type gnus-article-mark-lists)))
8572         var)
8573     (if (not vtype)
8574         (error "No such mark type: %s" type)
8575       (setq var (intern (format "gnus-newsgroup-%s" type)))
8576       (set var (cons article (symbol-value var)))
8577       (if (memq type '(processable cached replied forwarded saved))
8578           (gnus-summary-update-secondary-mark article)
8579         ;;; !!! This is bobus.  We should find out what primary
8580         ;;; !!! mark we want to set.
8581         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8582
8583 (defun gnus-summary-mark-as-expirable (n)
8584   "Mark N articles forward as expirable.
8585 If N is negative, mark backward instead.  The difference between N and
8586 the actual number of articles marked is returned."
8587   (interactive "p")
8588   (gnus-summary-mark-forward n gnus-expirable-mark))
8589
8590 (defun gnus-summary-mark-article-as-replied (article)
8591   "Mark ARTICLE as replied to and update the summary line.
8592 ARTICLE can also be a list of articles."
8593   (interactive (list (gnus-summary-article-number)))
8594   (let ((articles (if (listp article) article (list article))))
8595     (dolist (article articles)
8596       (push article gnus-newsgroup-replied)
8597       (let ((buffer-read-only nil))
8598         (when (gnus-summary-goto-subject article nil t)
8599           (gnus-summary-update-secondary-mark article))))))
8600
8601 (defun gnus-summary-mark-article-as-forwarded (article)
8602   "Mark ARTICLE as forwarded and update the summary line.
8603 ARTICLE can also be a list of articles."
8604   (let ((articles (if (listp article) article (list article))))
8605     (dolist (article articles)
8606       (push article gnus-newsgroup-forwarded)
8607       (let ((buffer-read-only nil))
8608         (when (gnus-summary-goto-subject article nil t)
8609           (gnus-summary-update-secondary-mark article))))))
8610
8611 (defun gnus-summary-set-bookmark (article)
8612   "Set a bookmark in current article."
8613   (interactive (list (gnus-summary-article-number)))
8614   (when (or (not (get-buffer gnus-article-buffer))
8615             (not gnus-current-article)
8616             (not gnus-article-current)
8617             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8618     (error "No current article selected"))
8619   ;; Remove old bookmark, if one exists.
8620   (let ((old (assq article gnus-newsgroup-bookmarks)))
8621     (when old
8622       (setq gnus-newsgroup-bookmarks
8623             (delq old gnus-newsgroup-bookmarks))))
8624   ;; Set the new bookmark, which is on the form
8625   ;; (article-number . line-number-in-body).
8626   (push
8627    (cons article
8628          (save-excursion
8629            (set-buffer gnus-article-buffer)
8630            (count-lines
8631             (min (point)
8632                  (save-excursion
8633                    (goto-char (point-min))
8634                    (search-forward "\n\n" nil t)
8635                    (point)))
8636             (point))))
8637    gnus-newsgroup-bookmarks)
8638   (gnus-message 6 "A bookmark has been added to the current article."))
8639
8640 (defun gnus-summary-remove-bookmark (article)
8641   "Remove the bookmark from the current article."
8642   (interactive (list (gnus-summary-article-number)))
8643   ;; Remove old bookmark, if one exists.
8644   (let ((old (assq article gnus-newsgroup-bookmarks)))
8645     (if old
8646         (progn
8647           (setq gnus-newsgroup-bookmarks
8648                 (delq old gnus-newsgroup-bookmarks))
8649           (gnus-message 6 "Removed bookmark."))
8650       (gnus-message 6 "No bookmark in current article."))))
8651
8652 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8653 (defun gnus-summary-mark-as-dormant (n)
8654   "Mark N articles forward as dormant.
8655 If N is negative, mark backward instead.  The difference between N and
8656 the actual number of articles marked is returned."
8657   (interactive "p")
8658   (gnus-summary-mark-forward n gnus-dormant-mark))
8659
8660 (defun gnus-summary-set-process-mark (article)
8661   "Set the process mark on ARTICLE and update the summary line."
8662   (setq gnus-newsgroup-processable
8663         (cons article
8664               (delq article gnus-newsgroup-processable)))
8665   (when (gnus-summary-goto-subject article)
8666     (gnus-summary-show-thread)
8667     (gnus-summary-goto-subject article)
8668     (gnus-summary-update-secondary-mark article)))
8669
8670 (defun gnus-summary-remove-process-mark (article)
8671   "Remove the process mark from ARTICLE and update the summary line."
8672   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8673   (when (gnus-summary-goto-subject article)
8674     (gnus-summary-show-thread)
8675     (gnus-summary-goto-subject article)
8676     (gnus-summary-update-secondary-mark article)))
8677
8678 (defun gnus-summary-set-saved-mark (article)
8679   "Set the process mark on ARTICLE and update the summary line."
8680   (push article gnus-newsgroup-saved)
8681   (when (gnus-summary-goto-subject article)
8682     (gnus-summary-update-secondary-mark article)))
8683
8684 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8685   "Mark N articles as read forwards.
8686 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8687 The difference between N and the actual number of articles marked is
8688 returned.
8689 Iff NO-EXPIRE, auto-expiry will be inhibited."
8690   (interactive "p")
8691   (gnus-summary-show-thread)
8692   (let ((backward (< n 0))
8693         (gnus-summary-goto-unread
8694          (and gnus-summary-goto-unread
8695               (not (eq gnus-summary-goto-unread 'never))
8696               (not (memq mark (list gnus-unread-mark
8697                                     gnus-ticked-mark gnus-dormant-mark)))))
8698         (n (abs n))
8699         (mark (or mark gnus-del-mark)))
8700     (while (and (> n 0)
8701                 (gnus-summary-mark-article nil mark no-expire)
8702                 (zerop (gnus-summary-next-subject
8703                         (if backward -1 1)
8704                         (and gnus-summary-goto-unread
8705                              (not (eq gnus-summary-goto-unread 'never)))
8706                         t)))
8707       (setq n (1- n)))
8708     (when (/= 0 n)
8709       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8710     (gnus-summary-recenter)
8711     (gnus-summary-position-point)
8712     (gnus-set-mode-line 'summary)
8713     n))
8714
8715 (defun gnus-summary-mark-article-as-read (mark)
8716   "Mark the current article quickly as read with MARK."
8717   (let ((article (gnus-summary-article-number)))
8718     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8719     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8720     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8721     (push (cons article mark) gnus-newsgroup-reads)
8722     ;; Possibly remove from cache, if that is used.
8723     (when gnus-use-cache
8724       (gnus-cache-enter-remove-article article))
8725     ;; Allow the backend to change the mark.
8726     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8727     ;; Check for auto-expiry.
8728     (when (and gnus-newsgroup-auto-expire
8729                (memq mark gnus-auto-expirable-marks))
8730       (setq mark gnus-expirable-mark)
8731       ;; Let the backend know about the mark change.
8732       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8733       (push article gnus-newsgroup-expirable))
8734     ;; Set the mark in the buffer.
8735     (gnus-summary-update-mark mark 'unread)
8736     t))
8737
8738 (defun gnus-summary-mark-article-as-unread (mark)
8739   "Mark the current article quickly as unread with MARK."
8740   (let* ((article (gnus-summary-article-number))
8741          (old-mark (gnus-summary-article-mark article)))
8742     ;; Allow the backend to change the mark.
8743     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8744     (if (eq mark old-mark)
8745         t
8746       (if (<= article 0)
8747           (progn
8748             (gnus-error 1 "Can't mark negative article numbers")
8749             nil)
8750         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8751         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8752         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8753         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8754         (cond ((= mark gnus-ticked-mark)
8755                (push article gnus-newsgroup-marked))
8756               ((= mark gnus-dormant-mark)
8757                (push article gnus-newsgroup-dormant))
8758               (t
8759                (push article gnus-newsgroup-unreads)))
8760         (gnus-pull article gnus-newsgroup-reads)
8761
8762         ;; See whether the article is to be put in the cache.
8763         (and gnus-use-cache
8764              (vectorp (gnus-summary-article-header article))
8765              (save-excursion
8766                (gnus-cache-possibly-enter-article
8767                 gnus-newsgroup-name article
8768                 (= mark gnus-ticked-mark)
8769                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8770
8771         ;; Fix the mark.
8772         (gnus-summary-update-mark mark 'unread)
8773         t))))
8774
8775 (defun gnus-summary-mark-article (&optional article mark no-expire)
8776   "Mark ARTICLE with MARK.  MARK can be any character.
8777 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8778 `??' (dormant) and `?E' (expirable).
8779 If MARK is nil, then the default character `?r' is used.
8780 If ARTICLE is nil, then the article on the current line will be
8781 marked.
8782 Iff NO-EXPIRE, auto-expiry will be inhibited."
8783   ;; The mark might be a string.
8784   (when (stringp mark)
8785     (setq mark (aref mark 0)))
8786   ;; If no mark is given, then we check auto-expiring.
8787   (when (null mark)
8788     (setq mark gnus-del-mark))
8789   (when (and (not no-expire)
8790              gnus-newsgroup-auto-expire
8791              (memq mark gnus-auto-expirable-marks))
8792     (setq mark gnus-expirable-mark))
8793   (let ((article (or article (gnus-summary-article-number)))
8794         (old-mark (gnus-summary-article-mark article)))
8795     ;; Allow the backend to change the mark.
8796     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8797     (if (eq mark old-mark)
8798         t
8799       (unless article
8800         (error "No article on current line"))
8801       (if (not (if (or (= mark gnus-unread-mark)
8802                        (= mark gnus-ticked-mark)
8803                        (= mark gnus-dormant-mark))
8804                    (gnus-mark-article-as-unread article mark)
8805                  (gnus-mark-article-as-read article mark)))
8806           t
8807         ;; See whether the article is to be put in the cache.
8808         (and gnus-use-cache
8809              (not (= mark gnus-canceled-mark))
8810              (vectorp (gnus-summary-article-header article))
8811              (save-excursion
8812                (gnus-cache-possibly-enter-article
8813                 gnus-newsgroup-name article
8814                 (= mark gnus-ticked-mark)
8815                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8816
8817         (when (gnus-summary-goto-subject article nil t)
8818           (let ((buffer-read-only nil))
8819             (gnus-summary-show-thread)
8820             ;; Fix the mark.
8821             (gnus-summary-update-mark mark 'unread)
8822             t))))))
8823
8824 (defun gnus-summary-update-secondary-mark (article)
8825   "Update the secondary (read, process, cache) mark."
8826   (gnus-summary-update-mark
8827    (cond ((memq article gnus-newsgroup-processable)
8828           gnus-process-mark)
8829          ((memq article gnus-newsgroup-cached)
8830           gnus-cached-mark)
8831          ((memq article gnus-newsgroup-replied)
8832           gnus-replied-mark)
8833          ((memq article gnus-newsgroup-forwarded)
8834           gnus-forwarded-mark)
8835          ((memq article gnus-newsgroup-saved)
8836           gnus-saved-mark)
8837          (t gnus-no-mark))
8838    'replied)
8839   (when (gnus-visual-p 'summary-highlight 'highlight)
8840     (gnus-run-hooks 'gnus-summary-update-hook))
8841   t)
8842
8843 (defun gnus-summary-update-mark (mark type)
8844   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8845         (buffer-read-only nil))
8846     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8847     (when forward
8848       (when (looking-at "\r")
8849         (incf forward))
8850       (when (<= (+ forward (point)) (point-max))
8851         ;; Go to the right position on the line.
8852         (goto-char (+ forward (point)))
8853         ;; Replace the old mark with the new mark.
8854         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8855         ;; Optionally update the marks by some user rule.
8856         (when (eq type 'unread)
8857           (gnus-data-set-mark
8858            (gnus-data-find (gnus-summary-article-number)) mark)
8859           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8860
8861 (defun gnus-mark-article-as-read (article &optional mark)
8862   "Enter ARTICLE in the pertinent lists and remove it from others."
8863   ;; Make the article expirable.
8864   (let ((mark (or mark gnus-del-mark)))
8865     (if (= mark gnus-expirable-mark)
8866         (push article gnus-newsgroup-expirable)
8867       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8868     ;; Remove from unread and marked lists.
8869     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8870     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8871     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8872     (push (cons article mark) gnus-newsgroup-reads)
8873     ;; Possibly remove from cache, if that is used.
8874     (when gnus-use-cache
8875       (gnus-cache-enter-remove-article article))
8876     t))
8877
8878 (defun gnus-mark-article-as-unread (article &optional mark)
8879   "Enter ARTICLE in the pertinent lists and remove it from others."
8880   (let ((mark (or mark gnus-ticked-mark)))
8881     (if (<= article 0)
8882         (progn
8883           (gnus-error 1 "Can't mark negative article numbers")
8884           nil)
8885       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8886             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8887             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8888             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8889
8890       ;; Unsuppress duplicates?
8891       (when gnus-suppress-duplicates
8892         (gnus-dup-unsuppress-article article))
8893
8894       (cond ((= mark gnus-ticked-mark)
8895              (push article gnus-newsgroup-marked))
8896             ((= mark gnus-dormant-mark)
8897              (push article gnus-newsgroup-dormant))
8898             (t
8899              (push article gnus-newsgroup-unreads)))
8900       (gnus-pull article gnus-newsgroup-reads)
8901       t)))
8902
8903 (defalias 'gnus-summary-mark-as-unread-forward
8904   'gnus-summary-tick-article-forward)
8905 (make-obsolete 'gnus-summary-mark-as-unread-forward
8906                'gnus-summary-tick-article-forward)
8907 (defun gnus-summary-tick-article-forward (n)
8908   "Tick N articles forwards.
8909 If N is negative, tick backwards instead.
8910 The difference between N and the number of articles ticked is returned."
8911   (interactive "p")
8912   (gnus-summary-mark-forward n gnus-ticked-mark))
8913
8914 (defalias 'gnus-summary-mark-as-unread-backward
8915   'gnus-summary-tick-article-backward)
8916 (make-obsolete 'gnus-summary-mark-as-unread-backward
8917                'gnus-summary-tick-article-backward)
8918 (defun gnus-summary-tick-article-backward (n)
8919   "Tick N articles backwards.
8920 The difference between N and the number of articles ticked is returned."
8921   (interactive "p")
8922   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8923
8924 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8925 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8926 (defun gnus-summary-tick-article (&optional article clear-mark)
8927   "Mark current article as unread.
8928 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8929 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8930   (interactive)
8931   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8932                                        gnus-ticked-mark)))
8933
8934 (defun gnus-summary-mark-as-read-forward (n)
8935   "Mark N articles as read forwards.
8936 If N is negative, mark backwards instead.
8937 The difference between N and the actual number of articles marked is
8938 returned."
8939   (interactive "p")
8940   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8941
8942 (defun gnus-summary-mark-as-read-backward (n)
8943   "Mark the N articles as read backwards.
8944 The difference between N and the actual number of articles marked is
8945 returned."
8946   (interactive "p")
8947   (gnus-summary-mark-forward
8948    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8949
8950 (defun gnus-summary-mark-as-read (&optional article mark)
8951   "Mark current article as read.
8952 ARTICLE specifies the article to be marked as read.
8953 MARK specifies a string to be inserted at the beginning of the line."
8954   (gnus-summary-mark-article article mark))
8955
8956 (defun gnus-summary-clear-mark-forward (n)
8957   "Clear marks from N articles forward.
8958 If N is negative, clear backward instead.
8959 The difference between N and the number of marks cleared is returned."
8960   (interactive "p")
8961   (gnus-summary-mark-forward n gnus-unread-mark))
8962
8963 (defun gnus-summary-clear-mark-backward (n)
8964   "Clear marks from N articles backward.
8965 The difference between N and the number of marks cleared is returned."
8966   (interactive "p")
8967   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8968
8969 (defun gnus-summary-mark-unread-as-read ()
8970   "Intended to be used by `gnus-summary-mark-article-hook'."
8971   (when (memq gnus-current-article gnus-newsgroup-unreads)
8972     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8973
8974 (defun gnus-summary-mark-read-and-unread-as-read ()
8975   "Intended to be used by `gnus-summary-mark-article-hook'."
8976   (let ((mark (gnus-summary-article-mark)))
8977     (when (or (gnus-unread-mark-p mark)
8978               (gnus-read-mark-p mark))
8979       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8980
8981 (defun gnus-summary-mark-unread-as-ticked ()
8982    "Intended to be used by `gnus-summary-mark-article-hook'."
8983   (when (memq gnus-current-article gnus-newsgroup-unreads)
8984     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8985
8986 (defun gnus-summary-mark-region-as-read (point mark all)
8987   "Mark all unread articles between point and mark as read.
8988 If given a prefix, mark all articles between point and mark as read,
8989 even ticked and dormant ones."
8990   (interactive "r\nP")
8991   (save-excursion
8992     (let (article)
8993       (goto-char point)
8994       (beginning-of-line)
8995       (while (and
8996               (< (point) mark)
8997               (progn
8998                 (when (or all
8999                           (memq (setq article (gnus-summary-article-number))
9000                                 gnus-newsgroup-unreads))
9001                   (gnus-summary-mark-article article gnus-del-mark))
9002                 t)
9003               (gnus-summary-find-next))))))
9004
9005 (defun gnus-summary-mark-below (score mark)
9006   "Mark articles with score less than SCORE with MARK."
9007   (interactive "P\ncMark: ")
9008   (setq score (if score
9009                   (prefix-numeric-value score)
9010                 (or gnus-summary-default-score 0)))
9011   (save-excursion
9012     (set-buffer gnus-summary-buffer)
9013     (goto-char (point-min))
9014     (while
9015         (progn
9016           (and (< (gnus-summary-article-score) score)
9017                (gnus-summary-mark-article nil mark))
9018           (gnus-summary-find-next)))))
9019
9020 (defun gnus-summary-kill-below (&optional score)
9021   "Mark articles with score below SCORE as read."
9022   (interactive "P")
9023   (gnus-summary-mark-below score gnus-killed-mark))
9024
9025 (defun gnus-summary-clear-above (&optional score)
9026   "Clear all marks from articles with score above SCORE."
9027   (interactive "P")
9028   (gnus-summary-mark-above score gnus-unread-mark))
9029
9030 (defun gnus-summary-tick-above (&optional score)
9031   "Tick all articles with score above SCORE."
9032   (interactive "P")
9033   (gnus-summary-mark-above score gnus-ticked-mark))
9034
9035 (defun gnus-summary-mark-above (score mark)
9036   "Mark articles with score over SCORE with MARK."
9037   (interactive "P\ncMark: ")
9038   (setq score (if score
9039                   (prefix-numeric-value score)
9040                 (or gnus-summary-default-score 0)))
9041   (save-excursion
9042     (set-buffer gnus-summary-buffer)
9043     (goto-char (point-min))
9044     (while (and (progn
9045                   (when (> (gnus-summary-article-score) score)
9046                     (gnus-summary-mark-article nil mark))
9047                   t)
9048                 (gnus-summary-find-next)))))
9049
9050 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9051 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9052 (defun gnus-summary-limit-include-expunged (&optional no-error)
9053   "Display all the hidden articles that were expunged for low scores."
9054   (interactive)
9055   (let ((buffer-read-only nil))
9056     (let ((scored gnus-newsgroup-scored)
9057           headers h)
9058       (while scored
9059         (unless (gnus-summary-article-header (caar scored))
9060           (and (setq h (gnus-number-to-header (caar scored)))
9061                (< (cdar scored) gnus-summary-expunge-below)
9062                (push h headers)))
9063         (setq scored (cdr scored)))
9064       (if (not headers)
9065           (when (not no-error)
9066             (error "No expunged articles hidden"))
9067         (goto-char (point-min))
9068         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9069         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9070         (mapcar (lambda (x) (push (mail-header-number x) 
9071                                   gnus-newsgroup-limit))
9072                 headers)
9073         (gnus-summary-prepare-unthreaded (nreverse headers))
9074         (goto-char (point-min))
9075         (gnus-summary-position-point)
9076         t))))
9077
9078 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9079   "Mark all unread articles in this newsgroup as read.
9080 If prefix argument ALL is non-nil, ticked and dormant articles will
9081 also be marked as read.
9082 If QUIETLY is non-nil, no questions will be asked.
9083 If TO-HERE is non-nil, it should be a point in the buffer.  All
9084 articles before (after, if REVERSE is set) this point will be marked as read.
9085 Note that this function will only catch up the unread article
9086 in the current summary buffer limitation.
9087 The number of articles marked as read is returned."
9088   (interactive "P")
9089   (prog1
9090       (save-excursion
9091         (when (or quietly
9092                   (not gnus-interactive-catchup) ;Without confirmation?
9093                   gnus-expert-user
9094                   (gnus-y-or-n-p
9095                    (if all
9096                        "Mark absolutely all articles as read? "
9097                      "Mark all unread articles as read? ")))
9098           (if (and not-mark
9099                    (not gnus-newsgroup-adaptive)
9100                    (not gnus-newsgroup-auto-expire)
9101                    (not gnus-suppress-duplicates)
9102                    (or (not gnus-use-cache)
9103                        (eq gnus-use-cache 'passive)))
9104               (progn
9105                 (when all
9106                   (setq gnus-newsgroup-marked nil
9107                         gnus-newsgroup-dormant nil))
9108                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9109             ;; We actually mark all articles as canceled, which we
9110             ;; have to do when using auto-expiry or adaptive scoring.
9111             (gnus-summary-show-all-threads)
9112             (if (and to-here reverse)
9113                 (progn
9114                   (goto-char to-here)
9115                   (while (and
9116                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9117                           (gnus-summary-find-next (not all) nil nil t))))
9118               (when (gnus-summary-first-subject (not all) t)
9119                 (while (and
9120                         (if to-here (< (point) to-here) t)
9121                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9122                         (gnus-summary-find-next (not all) nil nil t)))))
9123             (gnus-set-mode-line 'summary))
9124           t))
9125     (gnus-summary-position-point)))
9126
9127 (defun gnus-summary-catchup-to-here (&optional all)
9128   "Mark all unticked articles before the current one as read.
9129 If ALL is non-nil, also mark ticked and dormant articles as read."
9130   (interactive "P")
9131   (save-excursion
9132     (gnus-save-hidden-threads
9133       (let ((beg (point)))
9134         ;; We check that there are unread articles.
9135         (when (or all (gnus-summary-find-prev))
9136           (gnus-summary-catchup all t beg)))))
9137   (gnus-summary-position-point))
9138
9139 (defun gnus-summary-catchup-from-here (&optional all)
9140   "Mark all unticked articles after the current one as read.
9141 If ALL is non-nil, also mark ticked and dormant articles as read."
9142   (interactive "P")
9143   (save-excursion
9144     (gnus-save-hidden-threads
9145       (let ((beg (point)))
9146         ;; We check that there are unread articles.
9147         (when (or all (gnus-summary-find-next))
9148           (gnus-summary-catchup all t beg nil t)))))
9149
9150   (gnus-summary-position-point))
9151 (defun gnus-summary-catchup-all (&optional quietly)
9152   "Mark all articles in this newsgroup as read."
9153   (interactive "P")
9154   (gnus-summary-catchup t quietly))
9155
9156 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9157   "Mark all unread articles in this group as read, then exit.
9158 If prefix argument ALL is non-nil, all articles are marked as read.
9159 If QUIETLY is non-nil, no questions will be asked."
9160   (interactive "P")
9161   (when (gnus-summary-catchup all quietly nil 'fast)
9162     ;; Select next newsgroup or exit.
9163     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9164              (eq gnus-auto-select-next 'quietly))
9165         (gnus-summary-next-group nil)
9166       (gnus-summary-exit))))
9167
9168 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9169   "Mark all articles in this newsgroup as read, and then exit."
9170   (interactive "P")
9171   (gnus-summary-catchup-and-exit t quietly))
9172
9173 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9174   "Mark all articles in this group as read and select the next group.
9175 If given a prefix, mark all articles, unread as well as ticked, as
9176 read."
9177   (interactive "P")
9178   (save-excursion
9179     (gnus-summary-catchup all))
9180   (gnus-summary-next-group))
9181
9182 ;;;
9183 ;;; with article
9184 ;;;
9185
9186 (defmacro gnus-with-article (article &rest forms)
9187   "Select ARTICLE and perform FORMS in the original article buffer.
9188 Then replace the article with the result."
9189   `(progn
9190      ;; We don't want the article to be marked as read.
9191      (let (gnus-mark-article-hook)
9192        (gnus-summary-select-article t t nil ,article))
9193      (set-buffer gnus-original-article-buffer)
9194      ,@forms
9195      (if (not (gnus-check-backend-function
9196                'request-replace-article (car gnus-article-current)))
9197          (gnus-message 5 "Read-only group; not replacing")
9198        (unless (gnus-request-replace-article
9199                 ,article (car gnus-article-current)
9200                 (current-buffer) t)
9201          (error "Couldn't replace article")))
9202      ;; The cache and backlog have to be flushed somewhat.
9203      (when gnus-keep-backlog
9204        (gnus-backlog-remove-article
9205         (car gnus-article-current) (cdr gnus-article-current)))
9206      (when gnus-use-cache
9207        (gnus-cache-update-article
9208         (car gnus-article-current) (cdr gnus-article-current)))))
9209
9210 (put 'gnus-with-article 'lisp-indent-function 1)
9211 (put 'gnus-with-article 'edebug-form-spec '(form body))
9212
9213 ;; Thread-based commands.
9214
9215 (defun gnus-summary-articles-in-thread (&optional article)
9216   "Return a list of all articles in the current thread.
9217 If ARTICLE is non-nil, return all articles in the thread that starts
9218 with that article."
9219   (let* ((article (or article (gnus-summary-article-number)))
9220          (data (gnus-data-find-list article))
9221          (top-level (gnus-data-level (car data)))
9222          (top-subject
9223           (cond ((null gnus-thread-operation-ignore-subject)
9224                  (gnus-simplify-subject-re
9225                   (mail-header-subject (gnus-data-header (car data)))))
9226                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9227                  (gnus-simplify-subject-fuzzy
9228                   (mail-header-subject (gnus-data-header (car data)))))
9229                 (t nil)))
9230          (end-point (save-excursion
9231                       (if (gnus-summary-go-to-next-thread)
9232                           (point) (point-max))))
9233          articles)
9234     (while (and data
9235                 (< (gnus-data-pos (car data)) end-point))
9236       (when (or (not top-subject)
9237                 (string= top-subject
9238                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9239                              (gnus-simplify-subject-fuzzy
9240                               (mail-header-subject
9241                                (gnus-data-header (car data))))
9242                            (gnus-simplify-subject-re
9243                             (mail-header-subject
9244                              (gnus-data-header (car data)))))))
9245         (push (gnus-data-number (car data)) articles))
9246       (unless (and (setq data (cdr data))
9247                    (> (gnus-data-level (car data)) top-level))
9248         (setq data nil)))
9249     ;; Return the list of articles.
9250     (nreverse articles)))
9251
9252 (defun gnus-summary-rethread-current ()
9253   "Rethread the thread the current article is part of."
9254   (interactive)
9255   (let* ((gnus-show-threads t)
9256          (article (gnus-summary-article-number))
9257          (id (mail-header-id (gnus-summary-article-header)))
9258          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9259     (unless id
9260       (error "No article on the current line"))
9261     (gnus-rebuild-thread id)
9262     (gnus-summary-goto-subject article)))
9263
9264 (defun gnus-summary-reparent-thread ()
9265   "Make the current article child of the marked (or previous) article.
9266
9267 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9268 is non-nil or the Subject: of both articles are the same."
9269   (interactive)
9270   (unless (not (gnus-group-read-only-p))
9271     (error "The current newsgroup does not support article editing"))
9272   (unless (<= (length gnus-newsgroup-processable) 1)
9273     (error "No more than one article may be marked"))
9274   (save-window-excursion
9275     (let ((gnus-article-buffer " *reparent*")
9276           (current-article (gnus-summary-article-number))
9277           ;; First grab the marked article, otherwise one line up.
9278           (parent-article (if (not (null gnus-newsgroup-processable))
9279                               (car gnus-newsgroup-processable)
9280                             (save-excursion
9281                               (if (eq (forward-line -1) 0)
9282                                   (gnus-summary-article-number)
9283                                 (error "Beginning of summary buffer"))))))
9284       (unless (not (eq current-article parent-article))
9285         (error "An article may not be self-referential"))
9286       (let ((message-id (mail-header-id
9287                          (gnus-summary-article-header parent-article))))
9288         (unless (and message-id (not (equal message-id "")))
9289           (error "No message-id in desired parent"))
9290         (gnus-with-article current-article
9291           (save-restriction
9292             (goto-char (point-min))
9293             (message-narrow-to-head)
9294             (if (re-search-forward "^References: " nil t)
9295                 (progn
9296                   (re-search-forward "^[^ \t]" nil t)
9297                   (forward-line -1)
9298                   (end-of-line)
9299                   (insert " " message-id))
9300               (insert "References: " message-id "\n"))))
9301         (set-buffer gnus-summary-buffer)
9302         (gnus-summary-unmark-all-processable)
9303         (gnus-summary-update-article current-article)
9304         (gnus-summary-rethread-current)
9305         (gnus-message 3 "Article %d is now the child of article %d"
9306                       current-article parent-article)))))
9307
9308 (defun gnus-summary-toggle-threads (&optional arg)
9309   "Toggle showing conversation threads.
9310 If ARG is positive number, turn showing conversation threads on."
9311   (interactive "P")
9312   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9313     (setq gnus-show-threads
9314           (if (null arg) (not gnus-show-threads)
9315             (> (prefix-numeric-value arg) 0)))
9316     (gnus-summary-prepare)
9317     (gnus-summary-goto-subject current)
9318     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9319     (gnus-summary-position-point)))
9320
9321 (defun gnus-summary-show-all-threads ()
9322   "Show all threads."
9323   (interactive)
9324   (save-excursion
9325     (let ((buffer-read-only nil))
9326       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9327   (gnus-summary-position-point))
9328
9329 (defun gnus-summary-show-thread ()
9330   "Show thread subtrees.
9331 Returns nil if no thread was there to be shown."
9332   (interactive)
9333   (let ((buffer-read-only nil)
9334         (orig (point))
9335         ;; first goto end then to beg, to have point at beg after let
9336         (end (progn (end-of-line) (point)))
9337         (beg (progn (beginning-of-line) (point))))
9338     (prog1
9339         ;; Any hidden lines here?
9340         (search-forward "\r" end t)
9341       (subst-char-in-region beg end ?\^M ?\n t)
9342       (goto-char orig)
9343       (gnus-summary-position-point))))
9344
9345 (defun gnus-summary-hide-all-threads ()
9346   "Hide all thread subtrees."
9347   (interactive)
9348   (save-excursion
9349     (goto-char (point-min))
9350     (gnus-summary-hide-thread)
9351     (while (zerop (gnus-summary-next-thread 1 t))
9352       (gnus-summary-hide-thread)))
9353   (gnus-summary-position-point))
9354
9355 (defun gnus-summary-hide-thread ()
9356   "Hide thread subtrees.
9357 Returns nil if no threads were there to be hidden."
9358   (interactive)
9359   (let ((buffer-read-only nil)
9360         (start (point))
9361         (article (gnus-summary-article-number)))
9362     (goto-char start)
9363     ;; Go forward until either the buffer ends or the subthread
9364     ;; ends.
9365     (when (and (not (eobp))
9366                (or (zerop (gnus-summary-next-thread 1 t))
9367                    (goto-char (point-max))))
9368       (prog1
9369           (if (and (> (point) start)
9370                    (search-backward "\n" start t))
9371               (progn
9372                 (subst-char-in-region start (point) ?\n ?\^M)
9373                 (gnus-summary-goto-subject article))
9374             (goto-char start)
9375             nil)))))
9376
9377 (defun gnus-summary-go-to-next-thread (&optional previous)
9378   "Go to the same level (or less) next thread.
9379 If PREVIOUS is non-nil, go to previous thread instead.
9380 Return the article number moved to, or nil if moving was impossible."
9381   (let ((level (gnus-summary-thread-level))
9382         (way (if previous -1 1))
9383         (beg (point)))
9384     (forward-line way)
9385     (while (and (not (eobp))
9386                 (< level (gnus-summary-thread-level)))
9387       (forward-line way))
9388     (if (eobp)
9389         (progn
9390           (goto-char beg)
9391           nil)
9392       (setq beg (point))
9393       (prog1
9394           (gnus-summary-article-number)
9395         (goto-char beg)))))
9396
9397 (defun gnus-summary-next-thread (n &optional silent)
9398   "Go to the same level next N'th thread.
9399 If N is negative, search backward instead.
9400 Returns the difference between N and the number of skips actually
9401 done.
9402
9403 If SILENT, don't output messages."
9404   (interactive "p")
9405   (let ((backward (< n 0))
9406         (n (abs n)))
9407     (while (and (> n 0)
9408                 (gnus-summary-go-to-next-thread backward))
9409       (decf n))
9410     (unless silent
9411       (gnus-summary-position-point))
9412     (when (and (not silent) (/= 0 n))
9413       (gnus-message 7 "No more threads"))
9414     n))
9415
9416 (defun gnus-summary-prev-thread (n)
9417   "Go to the same level previous N'th thread.
9418 Returns the difference between N and the number of skips actually
9419 done."
9420   (interactive "p")
9421   (gnus-summary-next-thread (- n)))
9422
9423 (defun gnus-summary-go-down-thread ()
9424   "Go down one level in the current thread."
9425   (let ((children (gnus-summary-article-children)))
9426     (when children
9427       (gnus-summary-goto-subject (car children)))))
9428
9429 (defun gnus-summary-go-up-thread ()
9430   "Go up one level in the current thread."
9431   (let ((parent (gnus-summary-article-parent)))
9432     (when parent
9433       (gnus-summary-goto-subject parent))))
9434
9435 (defun gnus-summary-down-thread (n)
9436   "Go down thread N steps.
9437 If N is negative, go up instead.
9438 Returns the difference between N and how many steps down that were
9439 taken."
9440   (interactive "p")
9441   (let ((up (< n 0))
9442         (n (abs n)))
9443     (while (and (> n 0)
9444                 (if up (gnus-summary-go-up-thread)
9445                   (gnus-summary-go-down-thread)))
9446       (setq n (1- n)))
9447     (gnus-summary-position-point)
9448     (when (/= 0 n)
9449       (gnus-message 7 "Can't go further"))
9450     n))
9451
9452 (defun gnus-summary-up-thread (n)
9453   "Go up thread N steps.
9454 If N is negative, go down instead.
9455 Returns the difference between N and how many steps down that were
9456 taken."
9457   (interactive "p")
9458   (gnus-summary-down-thread (- n)))
9459
9460 (defun gnus-summary-top-thread ()
9461   "Go to the top of the thread."
9462   (interactive)
9463   (while (gnus-summary-go-up-thread))
9464   (gnus-summary-article-number))
9465
9466 (defun gnus-summary-kill-thread (&optional unmark)
9467   "Mark articles under current thread as read.
9468 If the prefix argument is positive, remove any kinds of marks.
9469 If the prefix argument is negative, tick articles instead."
9470   (interactive "P")
9471   (when unmark
9472     (setq unmark (prefix-numeric-value unmark)))
9473   (let ((articles (gnus-summary-articles-in-thread)))
9474     (save-excursion
9475       ;; Expand the thread.
9476       (gnus-summary-show-thread)
9477       ;; Mark all the articles.
9478       (while articles
9479         (gnus-summary-goto-subject (car articles))
9480         (cond ((null unmark)
9481                (gnus-summary-mark-article-as-read gnus-killed-mark))
9482               ((> unmark 0)
9483                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9484               (t
9485                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9486         (setq articles (cdr articles))))
9487     ;; Hide killed subtrees.
9488     (and (null unmark)
9489          gnus-thread-hide-killed
9490          (gnus-summary-hide-thread))
9491     ;; If marked as read, go to next unread subject.
9492     (when (null unmark)
9493       ;; Go to next unread subject.
9494       (gnus-summary-next-subject 1 t)))
9495   (gnus-set-mode-line 'summary))
9496
9497 ;; Summary sorting commands
9498
9499 (defun gnus-summary-sort-by-number (&optional reverse)
9500   "Sort the summary buffer by article number.
9501 Argument REVERSE means reverse order."
9502   (interactive "P")
9503   (gnus-summary-sort 'number reverse))
9504
9505 (defun gnus-summary-sort-by-author (&optional reverse)
9506   "Sort the summary buffer by author name alphabetically.
9507 If `case-fold-search' is non-nil, case of letters is ignored.
9508 Argument REVERSE means reverse order."
9509   (interactive "P")
9510   (gnus-summary-sort 'author reverse))
9511
9512 (defun gnus-summary-sort-by-subject (&optional reverse)
9513   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9514 If `case-fold-search' is non-nil, case of letters is ignored.
9515 Argument REVERSE means reverse order."
9516   (interactive "P")
9517   (gnus-summary-sort 'subject reverse))
9518
9519 (defun gnus-summary-sort-by-date (&optional reverse)
9520   "Sort the summary buffer by date.
9521 Argument REVERSE means reverse order."
9522   (interactive "P")
9523   (gnus-summary-sort 'date reverse))
9524
9525 (defun gnus-summary-sort-by-score (&optional reverse)
9526   "Sort the summary buffer by score.
9527 Argument REVERSE means reverse order."
9528   (interactive "P")
9529   (gnus-summary-sort 'score reverse))
9530
9531 (defun gnus-summary-sort-by-lines (&optional reverse)
9532   "Sort the summary buffer by the number of lines.
9533 Argument REVERSE means reverse order."
9534   (interactive "P")
9535   (gnus-summary-sort 'lines reverse))
9536
9537 (defun gnus-summary-sort-by-chars (&optional reverse)
9538   "Sort the summary buffer by article length.
9539 Argument REVERSE means reverse order."
9540   (interactive "P")
9541   (gnus-summary-sort 'chars reverse))
9542
9543 (defun gnus-summary-sort-by-original (&optional reverse)
9544   "Sort the summary buffer using the default sorting method.
9545 Argument REVERSE means reverse order."
9546   (interactive "P")
9547   (let* ((buffer-read-only)
9548          (gnus-summary-prepare-hook nil))
9549     ;; We do the sorting by regenerating the threads.
9550     (gnus-summary-prepare)
9551     ;; Hide subthreads if needed.
9552     (when (and gnus-show-threads gnus-thread-hide-subtree)
9553       (gnus-summary-hide-all-threads))))
9554
9555 (defun gnus-summary-sort (predicate reverse)
9556   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9557   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9558          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9559          (gnus-thread-sort-functions
9560           (if (not reverse)
9561               thread
9562             `(lambda (t1 t2)
9563                (,thread t2 t1))))
9564          (gnus-sort-gathered-threads-function
9565           gnus-thread-sort-functions)
9566          (gnus-article-sort-functions
9567           (if (not reverse)
9568               article
9569             `(lambda (t1 t2)
9570                (,article t2 t1))))
9571          (buffer-read-only)
9572          (gnus-summary-prepare-hook nil))
9573     ;; We do the sorting by regenerating the threads.
9574     (gnus-summary-prepare)
9575     ;; Hide subthreads if needed.
9576     (when (and gnus-show-threads gnus-thread-hide-subtree)
9577       (gnus-summary-hide-all-threads))))
9578
9579 ;; Summary saving commands.
9580
9581 (defun gnus-summary-save-article (&optional n not-saved)
9582   "Save the current article using the default saver function.
9583 If N is a positive number, save the N next articles.
9584 If N is a negative number, save the N previous articles.
9585 If N is nil and any articles have been marked with the process mark,
9586 save those articles instead.
9587 The variable `gnus-default-article-saver' specifies the saver function."
9588   (interactive "P")
9589   (let* ((articles (gnus-summary-work-articles n))
9590          (save-buffer (save-excursion
9591                         (nnheader-set-temp-buffer " *Gnus Save*")))
9592          (num (length articles))
9593          header file)
9594     (dolist (article articles)
9595       (setq header (gnus-summary-article-header article))
9596       (if (not (vectorp header))
9597           ;; This is a pseudo-article.
9598           (if (assq 'name header)
9599               (gnus-copy-file (cdr (assq 'name header)))
9600             (gnus-message 1 "Article %d is unsaveable" article))
9601         ;; This is a real article.
9602         (save-window-excursion
9603           (gnus-summary-select-article t nil nil article))
9604         (save-excursion
9605           (set-buffer save-buffer)
9606           (erase-buffer)
9607           (insert-buffer-substring gnus-original-article-buffer))
9608         (setq file (gnus-article-save save-buffer file num))
9609         (gnus-summary-remove-process-mark article)
9610         (unless not-saved
9611           (gnus-summary-set-saved-mark article))))
9612     (gnus-kill-buffer save-buffer)
9613     (gnus-summary-position-point)
9614     (gnus-set-mode-line 'summary)
9615     n))
9616
9617 (defun gnus-summary-pipe-output (&optional arg)
9618   "Pipe the current article to a subprocess.
9619 If N is a positive number, pipe the N next articles.
9620 If N is a negative number, pipe the N previous articles.
9621 If N is nil and any articles have been marked with the process mark,
9622 pipe those articles instead."
9623   (interactive "P")
9624   (require 'gnus-art)
9625   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9626     (gnus-summary-save-article arg t))
9627   (let ((buffer (get-buffer "*Shell Command Output*")))
9628     (if (and buffer 
9629              (with-current-buffer buffer (> (point-max) (point-min))))
9630         (gnus-configure-windows 'pipe))))
9631
9632 (defun gnus-summary-save-article-mail (&optional arg)
9633   "Append the current article to an mail file.
9634 If N is a positive number, save the N next articles.
9635 If N is a negative number, save the N previous articles.
9636 If N is nil and any articles have been marked with the process mark,
9637 save those articles instead."
9638   (interactive "P")
9639   (require 'gnus-art)
9640   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9641     (gnus-summary-save-article arg)))
9642
9643 (defun gnus-summary-save-article-rmail (&optional arg)
9644   "Append the current article to an rmail file.
9645 If N is a positive number, save the N next articles.
9646 If N is a negative number, save the N previous articles.
9647 If N is nil and any articles have been marked with the process mark,
9648 save those articles instead."
9649   (interactive "P")
9650   (require 'gnus-art)
9651   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9652     (gnus-summary-save-article arg)))
9653
9654 (defun gnus-summary-save-article-file (&optional arg)
9655   "Append the current article to a file.
9656 If N is a positive number, save the N next articles.
9657 If N is a negative number, save the N previous articles.
9658 If N is nil and any articles have been marked with the process mark,
9659 save those articles instead."
9660   (interactive "P")
9661   (require 'gnus-art)
9662   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9663     (gnus-summary-save-article arg)))
9664
9665 (defun gnus-summary-write-article-file (&optional arg)
9666   "Write the current article to a file, deleting the previous file.
9667 If N is a positive number, save the N next articles.
9668 If N is a negative number, save the N previous articles.
9669 If N is nil and any articles have been marked with the process mark,
9670 save those articles instead."
9671   (interactive "P")
9672   (require 'gnus-art)
9673   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9674     (gnus-summary-save-article arg)))
9675
9676 (defun gnus-summary-save-article-body-file (&optional arg)
9677   "Append the current article body to a file.
9678 If N is a positive number, save the N next articles.
9679 If N is a negative number, save the N previous articles.
9680 If N is nil and any articles have been marked with the process mark,
9681 save those articles instead."
9682   (interactive "P")
9683   (require 'gnus-art)
9684   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9685     (gnus-summary-save-article arg)))
9686
9687 (defun gnus-summary-pipe-message (program)
9688   "Pipe the current article through PROGRAM."
9689   (interactive "sProgram: ")
9690   (gnus-summary-select-article)
9691   (let ((mail-header-separator ""))
9692     (gnus-eval-in-buffer-window gnus-article-buffer
9693       (save-restriction
9694         (widen)
9695         (let ((start (window-start))
9696               buffer-read-only)
9697           (message-pipe-buffer-body program)
9698           (set-window-start (get-buffer-window (current-buffer)) start))))))
9699
9700 (defun gnus-get-split-value (methods)
9701   "Return a value based on the split METHODS."
9702   (let (split-name method result match)
9703     (when methods
9704       (save-excursion
9705         (set-buffer gnus-original-article-buffer)
9706         (save-restriction
9707           (nnheader-narrow-to-headers)
9708           (while (and methods (not split-name))
9709             (goto-char (point-min))
9710             (setq method (pop methods))
9711             (setq match (car method))
9712             (when (cond
9713                    ((stringp match)
9714                     ;; Regular expression.
9715                     (ignore-errors
9716                       (re-search-forward match nil t)))
9717                    ((gnus-functionp match)
9718                     ;; Function.
9719                     (save-restriction
9720                       (widen)
9721                       (setq result (funcall match gnus-newsgroup-name))))
9722                    ((consp match)
9723                     ;; Form.
9724                     (save-restriction
9725                       (widen)
9726                       (setq result (eval match)))))
9727               (setq split-name (cdr method))
9728               (cond ((stringp result)
9729                      (push (expand-file-name
9730                             result gnus-article-save-directory)
9731                            split-name))
9732                     ((consp result)
9733                      (setq split-name (append result split-name)))))))))
9734     (nreverse split-name)))
9735
9736 (defun gnus-valid-move-group-p (group)
9737   (and (boundp group)
9738        (symbol-name group)
9739        (symbol-value group)
9740        (gnus-get-function (gnus-find-method-for-group
9741                            (symbol-name group)) 'request-accept-article t)))
9742
9743 (defun gnus-read-move-group-name (prompt default articles prefix)
9744   "Read a group name."
9745   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9746          (minibuffer-confirm-incomplete nil) ; XEmacs
9747          (prom
9748           (format "%s %s to:"
9749                   prompt
9750                   (if (> (length articles) 1)
9751                       (format "these %d articles" (length articles))
9752                     "this article")))
9753          (to-newsgroup
9754           (cond
9755            ((null split-name)
9756             (gnus-completing-read default prom
9757                                   gnus-active-hashtb
9758                                   'gnus-valid-move-group-p
9759                                   nil prefix
9760                                   'gnus-group-history))
9761            ((= 1 (length split-name))
9762             (gnus-completing-read (car split-name) prom
9763                                   gnus-active-hashtb
9764                                   'gnus-valid-move-group-p
9765                                   nil nil
9766                                   'gnus-group-history))
9767            (t
9768             (gnus-completing-read nil prom
9769                                   (mapcar (lambda (el) (list el))
9770                                           (nreverse split-name))
9771                                   nil nil nil
9772                                   'gnus-group-history))))
9773          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9774     (when to-newsgroup
9775       (if (or (string= to-newsgroup "")
9776               (string= to-newsgroup prefix))
9777           (setq to-newsgroup default))
9778       (unless to-newsgroup
9779         (error "No group name entered"))
9780       (or (gnus-active to-newsgroup)
9781           (gnus-activate-group to-newsgroup nil nil to-method)
9782           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9783                                      to-newsgroup))
9784               (or (and (gnus-request-create-group to-newsgroup to-method)
9785                        (gnus-activate-group
9786                         to-newsgroup nil nil to-method)
9787                        (gnus-subscribe-group to-newsgroup))
9788                   (error "Couldn't create group %s" to-newsgroup)))
9789           (error "No such group: %s" to-newsgroup)))
9790     to-newsgroup))
9791
9792 (defun gnus-summary-save-parts (type dir n &optional reverse)
9793   "Save parts matching TYPE to DIR.
9794 If REVERSE, save parts that do not match TYPE."
9795   (interactive
9796    (list (read-string "Save parts of type: "
9797                       (or (car gnus-summary-save-parts-type-history)
9798                           gnus-summary-save-parts-default-mime)
9799                       'gnus-summary-save-parts-type-history)
9800          (setq gnus-summary-save-parts-last-directory
9801                (read-file-name "Save to directory: "
9802                                gnus-summary-save-parts-last-directory
9803                                nil t))
9804          current-prefix-arg))
9805   (gnus-summary-iterate n
9806     (let ((gnus-display-mime-function nil)
9807           (gnus-inhibit-treatment t))
9808       (gnus-summary-select-article))
9809     (save-excursion
9810       (set-buffer gnus-article-buffer)
9811       (let ((handles (or gnus-article-mime-handles
9812                          (mm-dissect-buffer) (mm-uu-dissect))))
9813         (when handles
9814           (gnus-summary-save-parts-1 type dir handles reverse)
9815           (unless gnus-article-mime-handles ;; Don't destroy this case.
9816             (mm-destroy-parts handles)))))))
9817
9818 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9819   (if (stringp (car handle))
9820       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9821               (cdr handle))
9822     (when (if reverse
9823               (not (string-match type (mm-handle-media-type handle)))
9824             (string-match type (mm-handle-media-type handle)))
9825       (let ((file (expand-file-name
9826                    (file-name-nondirectory
9827                     (or
9828                      (mail-content-type-get
9829                       (mm-handle-disposition handle) 'filename)
9830                      (concat gnus-newsgroup-name
9831                              "." (number-to-string
9832                                   (cdr gnus-article-current)))))
9833                    dir)))
9834         (unless (file-exists-p file)
9835           (mm-save-part-to-file handle file))))))
9836
9837 ;; Summary extract commands
9838
9839 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9840   (let ((buffer-read-only nil)
9841         (article (gnus-summary-article-number))
9842         after-article b e)
9843     (unless (gnus-summary-goto-subject article)
9844       (error "No such article: %d" article))
9845     (gnus-summary-position-point)
9846     ;; If all commands are to be bunched up on one line, we collect
9847     ;; them here.
9848     (unless gnus-view-pseudos-separately
9849       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9850             files action)
9851         (while ps
9852           (setq action (cdr (assq 'action (car ps))))
9853           (setq files (list (cdr (assq 'name (car ps)))))
9854           (while (and ps (cdr ps)
9855                       (string= (or action "1")
9856                                (or (cdr (assq 'action (cadr ps))) "2")))
9857             (push (cdr (assq 'name (cadr ps))) files)
9858             (setcdr ps (cddr ps)))
9859           (when files
9860             (when (not (string-match "%s" action))
9861               (push " " files))
9862             (push " " files)
9863             (when (assq 'execute (car ps))
9864               (setcdr (assq 'execute (car ps))
9865                       (funcall (if (string-match "%s" action)
9866                                    'format 'concat)
9867                                action
9868                                (mapconcat
9869                                 (lambda (f)
9870                                   (if (equal f " ")
9871                                       f
9872                                     (mm-quote-arg f)))
9873                                 files " ")))))
9874           (setq ps (cdr ps)))))
9875     (if (and gnus-view-pseudos (not not-view))
9876         (while pslist
9877           (when (assq 'execute (car pslist))
9878             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9879                                   (eq gnus-view-pseudos 'not-confirm)))
9880           (setq pslist (cdr pslist)))
9881       (save-excursion
9882         (while pslist
9883           (setq after-article (or (cdr (assq 'article (car pslist)))
9884                                   (gnus-summary-article-number)))
9885           (gnus-summary-goto-subject after-article)
9886           (forward-line 1)
9887           (setq b (point))
9888           (insert "    " (file-name-nondirectory
9889                           (cdr (assq 'name (car pslist))))
9890                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9891           (setq e (point))
9892           (forward-line -1)             ; back to `b'
9893           (gnus-add-text-properties
9894            b (1- e) (list 'gnus-number gnus-reffed-article-number
9895                           gnus-mouse-face-prop gnus-mouse-face))
9896           (gnus-data-enter
9897            after-article gnus-reffed-article-number
9898            gnus-unread-mark b (car pslist) 0 (- e b))
9899           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9900           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9901           (setq pslist (cdr pslist)))))))
9902
9903 (defun gnus-pseudos< (p1 p2)
9904   (let ((c1 (cdr (assq 'action p1)))
9905         (c2 (cdr (assq 'action p2))))
9906     (and c1 c2 (string< c1 c2))))
9907
9908 (defun gnus-request-pseudo-article (props)
9909   (cond ((assq 'execute props)
9910          (gnus-execute-command (cdr (assq 'execute props)))))
9911   (let ((gnus-current-article (gnus-summary-article-number)))
9912     (gnus-run-hooks 'gnus-mark-article-hook)))
9913
9914 (defun gnus-execute-command (command &optional automatic)
9915   (save-excursion
9916     (gnus-article-setup-buffer)
9917     (set-buffer gnus-article-buffer)
9918     (setq buffer-read-only nil)
9919     (let ((command (if automatic command
9920                      (read-string "Command: " (cons command 0)))))
9921       (erase-buffer)
9922       (insert "$ " command "\n\n")
9923       (if gnus-view-pseudo-asynchronously
9924           (start-process "gnus-execute" (current-buffer) shell-file-name
9925                          shell-command-switch command)
9926         (call-process shell-file-name nil t nil
9927                       shell-command-switch command)))))
9928
9929 ;; Summary kill commands.
9930
9931 (defun gnus-summary-edit-global-kill (article)
9932   "Edit the \"global\" kill file."
9933   (interactive (list (gnus-summary-article-number)))
9934   (gnus-group-edit-global-kill article))
9935
9936 (defun gnus-summary-edit-local-kill ()
9937   "Edit a local kill file applied to the current newsgroup."
9938   (interactive)
9939   (setq gnus-current-headers (gnus-summary-article-header))
9940   (gnus-group-edit-local-kill
9941    (gnus-summary-article-number) gnus-newsgroup-name))
9942
9943 ;;; Header reading.
9944
9945 (defun gnus-read-header (id &optional header)
9946   "Read the headers of article ID and enter them into the Gnus system."
9947   (let ((group gnus-newsgroup-name)
9948         (gnus-override-method
9949          (or
9950           gnus-override-method
9951           (and (gnus-news-group-p gnus-newsgroup-name)
9952                (car (gnus-refer-article-methods)))))
9953         where)
9954     ;; First we check to see whether the header in question is already
9955     ;; fetched.
9956     (if (stringp id)
9957         ;; This is a Message-ID.
9958         (setq header (or header (gnus-id-to-header id)))
9959       ;; This is an article number.
9960       (setq header (or header (gnus-summary-article-header id))))
9961     (if (and header
9962              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9963         ;; We have found the header.
9964         header
9965       ;; If this is a sparse article, we have to nix out its
9966       ;; previous entry in the thread hashtb.
9967       (when (and header
9968                  (gnus-summary-article-sparse-p (mail-header-number header)))
9969         (let* ((parent (gnus-parent-id (mail-header-references header)))
9970                (thread (and parent (gnus-id-to-thread parent))))
9971           (when thread
9972             (delq (assq header thread) thread))))
9973       ;; We have to really fetch the header to this article.
9974       (save-excursion
9975         (set-buffer nntp-server-buffer)
9976         (when (setq where (gnus-request-head id group))
9977           (nnheader-fold-continuation-lines)
9978           (goto-char (point-max))
9979           (insert ".\n")
9980           (goto-char (point-min))
9981           (insert "211 ")
9982           (princ (cond
9983                   ((numberp id) id)
9984                   ((cdr where) (cdr where))
9985                   (header (mail-header-number header))
9986                   (t gnus-reffed-article-number))
9987                  (current-buffer))
9988           (insert " Article retrieved.\n"))
9989         (if (or (not where)
9990                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9991             ()                          ; Malformed head.
9992           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9993             (when (and (stringp id)
9994                        (not (string= (gnus-group-real-name group)
9995                                      (car where))))
9996               ;; If we fetched by Message-ID and the article came
9997               ;; from a different group, we fudge some bogus article
9998               ;; numbers for this article.
9999               (mail-header-set-number header gnus-reffed-article-number))
10000             (save-excursion
10001               (set-buffer gnus-summary-buffer)
10002               (decf gnus-reffed-article-number)
10003               (gnus-remove-header (mail-header-number header))
10004               (push header gnus-newsgroup-headers)
10005               (setq gnus-current-headers header)
10006               (push (mail-header-number header) gnus-newsgroup-limit)))
10007           header)))))
10008
10009 (defun gnus-remove-header (number)
10010   "Remove header NUMBER from `gnus-newsgroup-headers'."
10011   (if (and gnus-newsgroup-headers
10012            (= number (mail-header-number (car gnus-newsgroup-headers))))
10013       (pop gnus-newsgroup-headers)
10014     (let ((headers gnus-newsgroup-headers))
10015       (while (and (cdr headers)
10016                   (not (= number (mail-header-number (cadr headers)))))
10017         (pop headers))
10018       (when (cdr headers)
10019         (setcdr headers (cddr headers))))))
10020
10021 ;;;
10022 ;;; summary highlights
10023 ;;;
10024
10025 (defun gnus-highlight-selected-summary ()
10026   "Highlight selected article in summary buffer."
10027   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10028   (when gnus-summary-selected-face
10029     (save-excursion
10030       (let* ((beg (progn (beginning-of-line) (point)))
10031              (end (progn (end-of-line) (point)))
10032              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10033              (from (if (get-text-property beg gnus-mouse-face-prop)
10034                        beg
10035                      (or (next-single-property-change
10036                           beg gnus-mouse-face-prop nil end)
10037                          beg)))
10038              (to
10039               (if (= from end)
10040                   (- from 2)
10041                 (or (next-single-property-change
10042                      from gnus-mouse-face-prop nil end)
10043                     end))))
10044         ;; If no mouse-face prop on line we will have to = from = end,
10045         ;; so we highlight the entire line instead.
10046         (when (= (+ to 2) from)
10047           (setq from beg)
10048           (setq to end))
10049         (if gnus-newsgroup-selected-overlay
10050             ;; Move old overlay.
10051             (gnus-move-overlay
10052              gnus-newsgroup-selected-overlay from to (current-buffer))
10053           ;; Create new overlay.
10054           (gnus-overlay-put
10055            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10056            'face gnus-summary-selected-face))))))
10057
10058 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10059 (defun gnus-summary-highlight-line ()
10060   "Highlight current line according to `gnus-summary-highlight'."
10061   (let* ((list gnus-summary-highlight)
10062          (p (point))
10063          (end (progn (end-of-line) (point)))
10064          ;; now find out where the line starts and leave point there.
10065          (beg (progn (beginning-of-line) (point)))
10066          (article (gnus-summary-article-number))
10067          (score (or (cdr (assq (or article gnus-current-article)
10068                                gnus-newsgroup-scored))
10069                     gnus-summary-default-score 0))
10070          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10071          (inhibit-read-only t))
10072     ;; Eval the cars of the lists until we find a match.
10073     (let ((default gnus-summary-default-score)
10074           (default-high gnus-summary-default-high-score)
10075           (default-low gnus-summary-default-low-score))
10076       (while (and list
10077                   (not (eval (caar list))))
10078         (setq list (cdr list))))
10079     (let ((face (cdar list)))
10080       (unless (eq face (get-text-property beg 'face))
10081         (gnus-put-text-property-excluding-characters-with-faces
10082          beg end 'face
10083          (setq face (if (boundp face) (symbol-value face) face)))
10084         (when gnus-summary-highlight-line-function
10085           (funcall gnus-summary-highlight-line-function article face))))
10086     (goto-char p)))
10087
10088 (defun gnus-update-read-articles (group unread &optional compute)
10089   "Update the list of read articles in GROUP."
10090   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10091          (entry (gnus-gethash group gnus-newsrc-hashtb))
10092          (info (nth 2 entry))
10093          (prev 1)
10094          (unread (sort (copy-sequence unread) '<))
10095          read)
10096     (if (or (not info) (not active))
10097         ;; There is no info on this group if it was, in fact,
10098         ;; killed.  Gnus stores no information on killed groups, so
10099         ;; there's nothing to be done.
10100         ;; One could store the information somewhere temporarily,
10101         ;; perhaps...  Hmmm...
10102         ()
10103       ;; Remove any negative articles numbers.
10104       (while (and unread (< (car unread) 0))
10105         (setq unread (cdr unread)))
10106       ;; Remove any expired article numbers
10107       (while (and unread (< (car unread) (car active)))
10108         (setq unread (cdr unread)))
10109       ;; Compute the ranges of read articles by looking at the list of
10110       ;; unread articles.
10111       (while unread
10112         (when (/= (car unread) prev)
10113           (push (if (= prev (1- (car unread))) prev
10114                   (cons prev (1- (car unread))))
10115                 read))
10116         (setq prev (1+ (car unread)))
10117         (setq unread (cdr unread)))
10118       (when (<= prev (cdr active))
10119         (push (cons prev (cdr active)) read))
10120       (setq read (if (> (length read) 1) (nreverse read) read))
10121       (if compute
10122           read
10123         (save-excursion
10124           (let (setmarkundo)
10125             ;; Propagate the read marks to the backend.
10126             (when (gnus-check-backend-function 'request-set-mark group)
10127               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10128                     (add (gnus-remove-from-range read (gnus-info-read info))))
10129                 (when (or add del)
10130                   (unless (gnus-check-group group)
10131                     (error "Can't open server for %s" group))
10132                   (gnus-request-set-mark
10133                    group (delq nil (list (if add (list add 'add '(read)))
10134                                          (if del (list del 'del '(read))))))
10135                   (setq setmarkundo
10136                         `(gnus-request-set-mark
10137                           ,group
10138                           ',(delq nil (list
10139                                        (if del (list del 'add '(read)))
10140                                        (if add (list add 'del '(read))))))))))
10141             (set-buffer gnus-group-buffer)
10142             (gnus-undo-register
10143               `(progn
10144                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10145                  (gnus-info-set-read ',info ',(gnus-info-read info))
10146                  (gnus-get-unread-articles-in-group ',info
10147                                                     (gnus-active ,group))
10148                  (gnus-group-update-group ,group t)
10149                  ,setmarkundo))))
10150         ;; Enter this list into the group info.
10151         (gnus-info-set-read info read)
10152         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10153         (gnus-get-unread-articles-in-group info (gnus-active group))
10154         t))))
10155
10156 (defun gnus-offer-save-summaries ()
10157   "Offer to save all active summary buffers."
10158   (let (buffers)
10159     ;; Go through all buffers and find all summaries.
10160     (dolist (buffer (buffer-list))
10161       (when (and (setq buffer (buffer-name buffer))
10162                  (string-match "Summary" buffer)
10163                  (save-excursion
10164                    (set-buffer buffer)
10165                    ;; We check that this is, indeed, a summary buffer.
10166                    (and (eq major-mode 'gnus-summary-mode)
10167                         ;; Also make sure this isn't bogus.
10168                         gnus-newsgroup-prepared
10169                         ;; Also make sure that this isn't a
10170                         ;; dead summary buffer.
10171                         (not gnus-dead-summary-mode))))
10172         (push buffer buffers)))
10173     ;; Go through all these summary buffers and offer to save them.
10174     (when buffers
10175       (save-excursion
10176         (map-y-or-n-p
10177          "Update summary buffer %s? "
10178          (lambda (buf)
10179            (switch-to-buffer buf)
10180            (gnus-summary-exit))
10181          buffers)))))
10182
10183 (defun gnus-summary-setup-default-charset ()
10184   "Setup newsgroup default charset."
10185   (if (equal gnus-newsgroup-name "nndraft:drafts")
10186       (setq gnus-newsgroup-charset nil)
10187     (let* ((ignored-charsets
10188             (or gnus-newsgroup-ephemeral-ignored-charsets
10189                 (append
10190                  (and gnus-newsgroup-name
10191                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10192                  gnus-newsgroup-ignored-charsets))))
10193       (setq gnus-newsgroup-charset
10194             (or gnus-newsgroup-ephemeral-charset
10195                 (and gnus-newsgroup-name
10196                      (gnus-parameter-charset gnus-newsgroup-name))
10197                 gnus-default-charset))
10198       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10199            ignored-charsets))))
10200
10201 ;;;
10202 ;;; Mime Commands
10203 ;;;
10204
10205 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10206   "Display the current article buffer fully MIME-buttonized.
10207 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10208 treated as multipart/mixed."
10209   (interactive "P")
10210   (require 'gnus-art)
10211   (let ((gnus-unbuttonized-mime-types nil)
10212         (gnus-mime-display-multipart-as-mixed show-all-parts))
10213     (gnus-summary-show-article)))
10214
10215 (defun gnus-summary-repair-multipart (article)
10216   "Add a Content-Type header to a multipart article without one."
10217   (interactive (list (gnus-summary-article-number)))
10218   (gnus-with-article article
10219     (message-narrow-to-head)
10220     (message-remove-header "Mime-Version")
10221     (goto-char (point-max))
10222     (insert "Mime-Version: 1.0\n")
10223     (widen)
10224     (when (search-forward "\n--" nil t)
10225       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10226         (message-narrow-to-head)
10227         (message-remove-header "Content-Type")
10228         (goto-char (point-max))
10229         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10230                         separator))
10231         (widen))))
10232   (let (gnus-mark-article-hook)
10233     (gnus-summary-select-article t t nil article)))
10234
10235 (defun gnus-summary-toggle-display-buttonized ()
10236   "Toggle the buttonizing of the article buffer."
10237   (interactive)
10238   (require 'gnus-art)
10239   (if (setq gnus-inhibit-mime-unbuttonizing
10240             (not gnus-inhibit-mime-unbuttonizing))
10241       (let ((gnus-unbuttonized-mime-types nil))
10242         (gnus-summary-show-article))
10243     (gnus-summary-show-article)))
10244
10245 ;;;
10246 ;;; Generic summary marking commands
10247 ;;;
10248
10249 (defvar gnus-summary-marking-alist
10250   '((read gnus-del-mark "d")
10251     (unread gnus-unread-mark "u")
10252     (ticked gnus-ticked-mark "!")
10253     (dormant gnus-dormant-mark "?")
10254     (expirable gnus-expirable-mark "e"))
10255   "An alist of names/marks/keystrokes.")
10256
10257 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10258 (defvar gnus-summary-mark-map)
10259
10260 (defun gnus-summary-make-all-marking-commands ()
10261   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10262   (dolist (elem gnus-summary-marking-alist)
10263     (apply 'gnus-summary-make-marking-command elem)))
10264
10265 (defun gnus-summary-make-marking-command (name mark keystroke)
10266   (let ((map (make-sparse-keymap)))
10267     (define-key gnus-summary-generic-mark-map keystroke map)
10268     (dolist (lway `((next "next" next nil "n")
10269                     (next-unread "next unread" next t "N")
10270                     (prev "previous" prev nil "p")
10271                     (prev-unread "previous unread" prev t "P")
10272                     (nomove "" nil nil ,keystroke)))
10273       (let ((func (gnus-summary-make-marking-command-1
10274                    mark (car lway) lway name)))
10275         (setq func (eval func))
10276         (define-key map (nth 4 lway) func)))))
10277
10278 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10279   `(defun ,(intern
10280             (format "gnus-summary-put-mark-as-%s%s"
10281                     name (if (eq way 'nomove)
10282                              ""
10283                            (concat "-" (symbol-name way)))))
10284      (n)
10285      ,(format
10286        "Mark the current article as %s%s.
10287 If N, the prefix, then repeat N times.
10288 If N is negative, move in reverse order.
10289 The difference between N and the actual number of articles marked is
10290 returned."
10291        name (cadr lway))
10292      (interactive "p")
10293      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10294
10295 (defun gnus-summary-generic-mark (n mark move unread)
10296   "Mark N articles with MARK."
10297   (unless (eq major-mode 'gnus-summary-mode)
10298     (error "This command can only be used in the summary buffer"))
10299   (gnus-summary-show-thread)
10300   (let ((nummove
10301          (cond
10302           ((eq move 'next) 1)
10303           ((eq move 'prev) -1)
10304           (t 0))))
10305     (if (zerop nummove)
10306         (setq n 1)
10307       (when (< n 0)
10308         (setq n (abs n)
10309               nummove (* -1 nummove))))
10310     (while (and (> n 0)
10311                 (gnus-summary-mark-article nil mark)
10312                 (zerop (gnus-summary-next-subject nummove unread t)))
10313       (setq n (1- n)))
10314     (when (/= 0 n)
10315       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10316     (gnus-summary-recenter)
10317     (gnus-summary-position-point)
10318     (gnus-set-mode-line 'summary)
10319     n))
10320
10321 (defun gnus-summary-insert-articles (articles)
10322   (when (setq articles
10323               (gnus-set-difference articles
10324                                    (mapcar (lambda (h) (mail-header-number h))
10325                                            gnus-newsgroup-headers)))
10326     (setq gnus-newsgroup-headers 
10327           (merge 'list
10328                  gnus-newsgroup-headers
10329                  (gnus-fetch-headers articles)
10330                  'gnus-article-sort-by-number))
10331     ;; Suppress duplicates?
10332     (when gnus-suppress-duplicates
10333       (gnus-dup-suppress-articles))
10334     
10335     ;; We might want to build some more threads first.
10336     (when (and gnus-fetch-old-headers
10337                (eq gnus-headers-retrieved-by 'nov))
10338       (if (eq gnus-fetch-old-headers 'invisible)
10339         (gnus-build-all-threads)
10340         (gnus-build-old-threads)))
10341     ;; Let the Gnus agent mark articles as read.
10342     (when gnus-agent
10343       (gnus-agent-get-undownloaded-list))
10344     ;; Remove list identifiers from subject
10345     (when gnus-list-identifiers
10346       (gnus-summary-remove-list-identifiers))
10347     ;; First and last article in this newsgroup.
10348     (when gnus-newsgroup-headers
10349       (setq gnus-newsgroup-begin
10350             (mail-header-number (car gnus-newsgroup-headers))
10351             gnus-newsgroup-end
10352             (mail-header-number
10353              (gnus-last-element gnus-newsgroup-headers))))
10354     (when gnus-use-scoring
10355       (gnus-possibly-score-headers))))
10356
10357 (defun gnus-summary-insert-old-articles (&optional all)
10358   "Insert all old articles in this group.
10359 If ALL is non-nil, already read articles become readable.
10360 If ALL is a number, fetch this number of articles."
10361   (interactive "P")
10362   (prog1
10363       (let ((old (mapcar 'car gnus-newsgroup-data))
10364             (i (car gnus-newsgroup-active))
10365             older len)
10366         (while (<= i (cdr gnus-newsgroup-active))
10367           (or (memq i old) (push i older))
10368           (incf i))
10369         (setq len (length older))
10370         (cond 
10371          ((null older) nil)
10372          ((numberp all) 
10373           (if (< all len)
10374               (setq older (subseq older 0 all))))
10375          (all nil)
10376          (t
10377           (if (and (numberp gnus-large-newsgroup)
10378                    (> len gnus-large-newsgroup))
10379               (let ((input
10380                      (read-string
10381                       (format
10382                        "How many articles from %s (default %d): "
10383                        (gnus-limit-string 
10384                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10385                        len))))
10386                 (unless (string-match "^[ \t]*$" input) 
10387                   (setq all (string-to-number input))
10388                   (if (< all len)
10389                       (setq older (subseq older 0 all))))))))
10390         (if (not older)
10391             (message "No old news.")
10392           (gnus-summary-insert-articles older)
10393           (gnus-summary-limit (gnus-union older old))))
10394     (gnus-summary-position-point)))
10395
10396 (defun gnus-summary-insert-new-articles ()
10397   "Insert all new articles in this group."
10398   (interactive)
10399   (prog1
10400       (let ((old (mapcar 'car gnus-newsgroup-data))
10401             (old-active gnus-newsgroup-active)
10402             (nnmail-fetched-sources (list t))
10403             i new)
10404         (setq gnus-newsgroup-active 
10405               (gnus-activate-group gnus-newsgroup-name 'scan))
10406         (setq i (1+ (cdr old-active)))
10407         (while (<= i (cdr gnus-newsgroup-active))
10408           (push i new)
10409           (incf i))
10410         (if (not new)
10411             (message "No gnus is bad news.")
10412           (setq new (nreverse new))
10413           (gnus-summary-insert-articles new)
10414           (setq gnus-newsgroup-unreads
10415                 (append gnus-newsgroup-unreads new))
10416           (gnus-summary-limit (gnus-union old new))))
10417     (gnus-summary-position-point)))
10418
10419 (gnus-summary-make-all-marking-commands)
10420
10421 (gnus-ems-redefine)
10422
10423 (provide 'gnus-sum)
10424
10425 (run-hooks 'gnus-sum-load-hook)
10426
10427 ;;; gnus-sum.el ends here