2001-12-20 15:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 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 ?F
438   "*Mark used for articles that have been forwarded."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-recent-mark ?N
443   "*Mark used for articles that are recent."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-cached-mark ?*
448   "*Mark used for articles that are in the cache."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-saved-mark ?S
453   "*Mark used for articles that have been saved."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-unseen-mark ?.
458   "*Mark used for articles that haven't been seen."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-no-mark ?               ;Whitespace
463   "*Mark used for articles that have no other secondary mark."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-ancient-mark ?O
468   "*Mark used for ancient articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-sparse-mark ?Q
473   "*Mark used for sparsely reffed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-canceled-mark ?G
478   "*Mark used for canceled articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-duplicate-mark ?M
483   "*Mark used for duplicate articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-undownloaded-mark ?@
488   "*Mark used for articles that weren't downloaded."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-downloadable-mark ?%
493   "*Mark used for articles that are to be downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-unsendable-mark ?=
498   "*Mark used for articles that won't be sent."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-score-over-mark ?+
503   "*Score mark used for articles with high scores."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-below-mark ?-
508   "*Score mark used for articles with low scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-empty-thread-mark ?     ;Whitespace
513   "*There is no thread under the article."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-not-empty-thread-mark ?=
518   "*There is a thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-view-pseudo-asynchronously nil
523   "*If non-nil, Gnus will view pseudo-articles asynchronously."
524   :group 'gnus-extract-view
525   :type 'boolean)
526
527 (defcustom gnus-auto-expirable-marks
528   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
529         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
530         gnus-souped-mark gnus-duplicate-mark)
531   "*The list of marks converted into expiration if a group is auto-expirable."
532   :version "21.1"
533   :group 'gnus-summary
534   :type '(repeat character))
535
536 (defcustom gnus-inhibit-user-auto-expire t
537   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
538   :version "21.1"
539   :group 'gnus-summary
540   :type 'boolean)
541
542 (defcustom gnus-view-pseudos nil
543   "*If `automatic', pseudo-articles will be viewed automatically.
544 If `not-confirm', pseudos will be viewed automatically, and the user
545 will not be asked to confirm the command."
546   :group 'gnus-extract-view
547   :type '(choice (const :tag "off" nil)
548                  (const automatic)
549                  (const not-confirm)))
550
551 (defcustom gnus-view-pseudos-separately t
552   "*If non-nil, one pseudo-article will be created for each file to be viewed.
553 If nil, all files that use the same viewing command will be given as a
554 list of parameters to that command."
555   :group 'gnus-extract-view
556   :type 'boolean)
557
558 (defcustom gnus-insert-pseudo-articles t
559   "*If non-nil, insert pseudo-articles when decoding articles."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-summary-dummy-line-format
564   "  %(:                          :%) %S\n"
565   "*The format specification for the dummy roots in the summary buffer.
566 It works along the same lines as a normal formatting string,
567 with some simple extensions.
568
569 %S  The subject"
570   :group 'gnus-threading
571   :type 'string)
572
573 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
574   "*The format specification for the summary mode line.
575 It works along the same lines as a normal formatting string,
576 with some simple extensions:
577
578 %G  Group name
579 %p  Unprefixed group name
580 %A  Current article number
581 %z  Current article score
582 %V  Gnus version
583 %U  Number of unread articles in the group
584 %e  Number of unselected articles in the group
585 %Z  A string with unread/unselected article counts
586 %g  Shortish group name
587 %S  Subject of the current article
588 %u  User-defined spec
589 %s  Current score file name
590 %d  Number of dormant articles
591 %r  Number of articles that have been marked as read in this session
592 %E  Number of articles expunged by the score files"
593   :group 'gnus-summary-format
594   :type 'string)
595
596 (defcustom gnus-list-identifiers nil
597   "Regexp that matches list identifiers to be removed from subject.
598 This can also be a list of regexps."
599   :version "21.1"
600   :group 'gnus-summary-format
601   :group 'gnus-article-hiding
602   :type '(choice (const :tag "none" nil)
603                  (regexp :value ".*")
604                  (repeat :value (".*") regexp)))
605
606 (defcustom gnus-summary-mark-below 0
607   "*Mark all articles with a score below this variable as read.
608 This variable is local to each summary buffer and usually set by the
609 score file."
610   :group 'gnus-score-default
611   :type 'integer)
612
613 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
614   "*List of functions used for sorting articles in the summary buffer.
615
616 Each function takes two articles and returns non-nil if the first
617 article should be sorted before the other.  If you use more than one
618 function, the primary sort function should be the last.  You should
619 probably always include `gnus-article-sort-by-number' in the list of
620 sorting functions -- preferably first.  Also note that sorting by date
621 is often much slower than sorting by number, and the sorting order is
622 very similar.  (Sorting by date means sorting by the time the message
623 was sent, sorting by number means sorting by arrival time.)
624
625 Ready-made functions include `gnus-article-sort-by-number',
626 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
627 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
628
629 When threading is turned on, the variable `gnus-thread-sort-functions'
630 controls how articles are sorted."
631   :group 'gnus-summary-sort
632   :type '(repeat (choice (function-item gnus-article-sort-by-number)
633                          (function-item gnus-article-sort-by-author)
634                          (function-item gnus-article-sort-by-subject)
635                          (function-item gnus-article-sort-by-date)
636                          (function-item gnus-article-sort-by-score)
637                          (function :tag "other"))))
638
639 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
640   "*List of functions used for sorting threads in the summary buffer.
641 By default, threads are sorted by article number.
642
643 Each function takes two threads and returns non-nil if the first
644 thread should be sorted before the other.  If you use more than one
645 function, the primary sort function should be the last.  You should
646 probably always include `gnus-thread-sort-by-number' in the list of
647 sorting functions -- preferably first.  Also note that sorting by date
648 is often much slower than sorting by number, and the sorting order is
649 very similar.  (Sorting by date means sorting by the time the message
650 was sent, sorting by number means sorting by arrival time.)
651
652 Ready-made functions include `gnus-thread-sort-by-number',
653 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
654 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
655 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
656
657 When threading is turned off, the variable
658 `gnus-article-sort-functions' controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
661                          (function-item gnus-thread-sort-by-author)
662                          (function-item gnus-thread-sort-by-subject)
663                          (function-item gnus-thread-sort-by-date)
664                          (function-item gnus-thread-sort-by-score)
665                          (function-item gnus-thread-sort-by-total-score)
666                          (function :tag "other"))))
667
668 (defcustom gnus-thread-score-function '+
669   "*Function used for calculating the total score of a thread.
670
671 The function is called with the scores of the article and each
672 subthread and should then return the score of the thread.
673
674 Some functions you can use are `+', `max', or `min'."
675   :group 'gnus-summary-sort
676   :type 'function)
677
678 (defcustom gnus-summary-expunge-below nil
679   "All articles that have a score less than this variable will be expunged.
680 This variable is local to the summary buffers."
681   :group 'gnus-score-default
682   :type '(choice (const :tag "off" nil)
683                  integer))
684
685 (defcustom gnus-thread-expunge-below nil
686   "All threads that have a total score less than this variable will be expunged.
687 See `gnus-thread-score-function' for en explanation of what a
688 \"thread score\" is.
689
690 This variable is local to the summary buffers."
691   :group 'gnus-threading
692   :group 'gnus-score-default
693   :type '(choice (const :tag "off" nil)
694                  integer))
695
696 (defcustom gnus-summary-mode-hook nil
697   "*A hook for Gnus summary mode.
698 This hook is run before any variables are set in the summary buffer."
699   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
704 (when (featurep 'xemacs)
705   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
706   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
707   (add-hook 'gnus-summary-mode-hook
708             'gnus-xmas-switch-horizontal-scrollbar-off))
709
710 (defcustom gnus-summary-menu-hook nil
711   "*Hook run after the creation of the summary mode menu."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-summary-exit-hook nil
716   "*A hook called on exit from the summary buffer.
717 It will be called with point in the group buffer."
718   :group 'gnus-summary-exit
719   :type 'hook)
720
721 (defcustom gnus-summary-prepare-hook nil
722   "*A hook called after the summary buffer has been generated.
723 If you want to modify the summary buffer, you can use this hook."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-prepared-hook nil
728   "*A hook called as the last thing after the summary buffer has been generated."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-generate-hook nil
733   "*A hook run just before generating the summary buffer.
734 This hook is commonly used to customize threading variables and the
735 like."
736   :group 'gnus-summary-various
737   :type 'hook)
738
739 (defcustom gnus-select-group-hook nil
740   "*A hook called when a newsgroup is selected.
741
742 If you'd like to simplify subjects like the
743 `gnus-summary-next-same-subject' command does, you can use the
744 following hook:
745
746  (setq gnus-select-group-hook
747       (list
748         (lambda ()
749           (mapcar (lambda (header)
750                      (mail-header-set-subject
751                       header
752                       (gnus-simplify-subject
753                        (mail-header-subject header) 're-only)))
754                   gnus-newsgroup-headers))))"
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-select-article-hook nil
759   "*A hook called when an article is selected."
760   :group 'gnus-summary-choose
761   :type 'hook)
762
763 (defcustom gnus-visual-mark-article-hook
764   (list 'gnus-highlight-selected-summary)
765   "*Hook run after selecting an article in the summary buffer.
766 It is meant to be used for highlighting the article in some way.  It
767 is not run if `gnus-visual' is nil."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-parse-headers-hook nil
772   "*A hook called before parsing the headers."
773   :group 'gnus-various
774   :type 'hook)
775
776 (defcustom gnus-exit-group-hook nil
777   "*A hook called when exiting summary mode.
778 This hook is not called from the non-updating exit commands like `Q'."
779   :group 'gnus-various
780   :type 'hook)
781
782 (defcustom gnus-summary-update-hook
783   (list 'gnus-summary-highlight-line)
784   "*A hook called when a summary line is changed.
785 The hook will not be called if `gnus-visual' is nil.
786
787 The default function `gnus-summary-highlight-line' will
788 highlight the line according to the `gnus-summary-highlight'
789 variable."
790   :group 'gnus-summary-visual
791   :type 'hook)
792
793 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
794   "*A hook called when an article is selected for the first time.
795 The hook is intended to mark an article as read (or unread)
796 automatically when it is selected."
797   :group 'gnus-summary-choose
798   :type 'hook)
799
800 (defcustom gnus-group-no-more-groups-hook nil
801   "*A hook run when returning to group mode having no more (unread) groups."
802   :group 'gnus-group-select
803   :type 'hook)
804
805 (defcustom gnus-ps-print-hook nil
806   "*A hook run before ps-printing something from Gnus."
807   :group 'gnus-summary
808   :type 'hook)
809
810 (defcustom gnus-summary-display-arrow
811   (and (fboundp 'display-graphic-p)
812        (display-graphic-p))
813   "*If non-nil, display an arrow highlighting the current article."
814   :version "21.1"
815   :group 'gnus-summary
816   :type 'boolean)
817
818 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
819   "Face used for highlighting the current article in the summary buffer."
820   :group 'gnus-summary-visual
821   :type 'face)
822
823 (defcustom gnus-summary-highlight
824   '(((= mark gnus-canceled-mark)
825      . gnus-summary-cancelled-face)
826     ((and (> score default-high)
827           (or (= mark gnus-dormant-mark)
828               (= mark gnus-ticked-mark)))
829      . gnus-summary-high-ticked-face)
830     ((and (< score default-low)
831           (or (= mark gnus-dormant-mark)
832               (= mark gnus-ticked-mark)))
833      . gnus-summary-low-ticked-face)
834     ((or (= mark gnus-dormant-mark)
835          (= mark gnus-ticked-mark))
836      . gnus-summary-normal-ticked-face)
837     ((and (> score default-high) (= mark gnus-ancient-mark))
838      . gnus-summary-high-ancient-face)
839     ((and (< score default-low) (= mark gnus-ancient-mark))
840      . gnus-summary-low-ancient-face)
841     ((= mark gnus-ancient-mark)
842      . gnus-summary-normal-ancient-face)
843     ((and (> score default-high) (= mark gnus-unread-mark))
844      . gnus-summary-high-unread-face)
845     ((and (< score default-low) (= mark gnus-unread-mark))
846      . gnus-summary-low-unread-face)
847     ((= mark gnus-unread-mark)
848      . gnus-summary-normal-unread-face)
849     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
850                                                   gnus-undownloaded-mark)))
851      . gnus-summary-high-unread-face)
852     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
853                                                  gnus-undownloaded-mark)))
854      . gnus-summary-low-unread-face)
855     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856           (memq article gnus-newsgroup-unreads))
857      . gnus-summary-normal-unread-face)
858     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
859      . gnus-summary-normal-read-face)
860     ((> score default-high)
861      . gnus-summary-high-read-face)
862     ((< score default-low)
863      . gnus-summary-low-read-face)
864     (t
865      . gnus-summary-normal-read-face))
866   "*Controls the highlighting of summary buffer lines.
867
868 A list of (FORM . FACE) pairs.  When deciding how a a particular
869 summary line should be displayed, each form is evaluated.  The content
870 of the face field after the first true form is used.  You can change
871 how those summary lines are displayed, by editing the face field.
872
873 You can use the following variables in the FORM field.
874
875 score:        The article's score
876 default:      The default article score.
877 default-high: The default score for high scored articles.
878 default-low:  The default score for low scored articles.
879 below:        The score below which articles are automatically marked as read.
880 mark:         The articles mark."
881   :group 'gnus-summary-visual
882   :type '(repeat (cons (sexp :tag "Form" nil)
883                        face)))
884
885 (defcustom gnus-alter-header-function nil
886   "Function called to allow alteration of article header structures.
887 The function is called with one parameter, the article header vector,
888 which it may alter in any way.")
889
890 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
891   "Variable that says which function should be used to decode a string with encoded words.")
892
893 (defcustom gnus-extra-headers nil
894   "*Extra headers to parse."
895   :version "21.1"
896   :group 'gnus-summary
897   :type '(repeat symbol))
898
899 (defcustom gnus-ignored-from-addresses
900   (and user-mail-address (regexp-quote user-mail-address))
901   "*Regexp of From headers that may be suppressed in favor of To headers."
902   :version "21.1"
903   :group 'gnus-summary
904   :type 'regexp)
905
906 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
907   "List of charsets that should be ignored.
908 When these charsets are used in the \"charset\" parameter, the
909 default charset will be used instead."
910   :version "21.1"
911   :type '(repeat symbol)
912   :group 'gnus-charset)
913
914 (gnus-define-group-parameter
915  ignored-charsets
916  :type list
917  :function-document
918  "Return the ignored charsets of GROUP."
919  :variable gnus-group-ignored-charsets-alist
920  :variable-default
921  '(("alt\\.chinese\\.text" iso-8859-1))
922  :variable-document
923  "Alist of regexps (to match group names) and charsets that should be ignored.
924 When these charsets are used in the \"charset\" parameter, the
925 default charset will be used instead."
926  :variable-group gnus-charset
927  :variable-type '(repeat (cons (regexp :tag "Group")
928                                (repeat symbol)))
929  :parameter-type '(choice :tag "Ignored charsets"
930                           :value nil
931                           (repeat (symbol)))
932  :parameter-document       "\
933 List of charsets that should be ignored.
934
935 When these charsets are used in the \"charset\" parameter, the
936 default charset will be used instead.")
937
938 (defcustom gnus-group-highlight-words-alist nil
939   "Alist of group regexps and highlight regexps.
940 This variable uses the same syntax as `gnus-emphasis-alist'."
941   :version "21.1"
942   :type '(repeat (cons (regexp :tag "Group")
943                        (repeat (list (regexp :tag "Highlight regexp")
944                                      (number :tag "Group for entire word" 0)
945                                      (number :tag "Group for displayed part" 0)
946                                      (symbol :tag "Face"
947                                              gnus-emphasis-highlight-words)))))
948   :group 'gnus-summary-visual)
949
950 (defcustom gnus-summary-show-article-charset-alist
951   nil
952   "Alist of number and charset.
953 The article will be shown with the charset corresponding to the
954 numbered argument.
955 For example: ((1 . cn-gb-2312) (2 . big5))."
956   :version "21.1"
957   :type '(repeat (cons (number :tag "Argument" 1)
958                        (symbol :tag "Charset")))
959   :group 'gnus-charset)
960
961 (defcustom gnus-preserve-marks t
962   "Whether marks are preserved when moving, copying and respooling messages."
963   :version "21.1"
964   :type 'boolean
965   :group 'gnus-summary-marks)
966
967 (defcustom gnus-alter-articles-to-read-function nil
968   "Function to be called to alter the list of articles to be selected."
969   :type '(choice (const nil) function)
970   :group 'gnus-summary)
971
972 (defcustom gnus-orphan-score nil
973   "*All orphans get this score added.  Set in the score file."
974   :group 'gnus-score-default
975   :type '(choice (const nil)
976                  integer))
977
978 (defcustom gnus-summary-save-parts-default-mime "image/.*"
979   "*A regexp to match MIME parts when saving multiple parts of a message
980 with gnus-summary-save-parts (X m). This regexp will be used by default
981 when prompting the user for which type of files to save."
982   :group 'gnus-summary
983   :type 'regexp)
984
985
986 (defcustom gnus-summary-save-parts-default-mime "image/.*"
987   "*A regexp to match MIME parts when saving multiple parts of a message
988 with gnus-summary-save-parts (X m). This regexp will be used by default
989 when prompting the user for which type of files to save."
990   :group 'gnus-summary
991   :type 'regexp)
992
993 (defcustom gnus-read-all-available-headers nil
994   "Whether Gnus should parse all headers made available to it.
995 This is mostly relevant for slow backends where the user may
996 wish to widen the summary buffer to include all headers
997 that were fetched.  Say, for nnultimate groups."
998   :group 'gnus-summary
999   :type '(choice boolean regexp))
1000
1001 (defcustom gnus-summary-muttprint-program "muttprint"
1002   "Command (and optional arguments) used to run Muttprint."
1003   :group 'gnus-summary
1004   :type 'string)
1005
1006 ;;; Internal variables
1007
1008 (defvar gnus-summary-display-cache nil)
1009 (defvar gnus-article-mime-handles nil)
1010 (defvar gnus-article-decoded-p nil)
1011 (defvar gnus-article-charset nil)
1012 (defvar gnus-article-ignored-charsets nil)
1013 (defvar gnus-scores-exclude-files nil)
1014 (defvar gnus-page-broken nil)
1015 (defvar gnus-inhibit-mime-unbuttonizing nil)
1016
1017 (defvar gnus-original-article nil)
1018 (defvar gnus-article-internal-prepare-hook nil)
1019 (defvar gnus-newsgroup-process-stack nil)
1020
1021 (defvar gnus-thread-indent-array nil)
1022 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1023 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1024   "Function called to sort the articles within a thread after it has been gathered together.")
1025
1026 (defvar gnus-summary-save-parts-type-history nil)
1027 (defvar gnus-summary-save-parts-last-directory nil)
1028
1029 (defvar gnus-summary-save-parts-type-history nil)
1030 (defvar gnus-summary-save-parts-last-directory nil)
1031
1032 ;; Avoid highlighting in kill files.
1033 (defvar gnus-summary-inhibit-highlight nil)
1034 (defvar gnus-newsgroup-selected-overlay nil)
1035 (defvar gnus-inhibit-limiting nil)
1036 (defvar gnus-newsgroup-adaptive-score-file nil)
1037 (defvar gnus-current-score-file nil)
1038 (defvar gnus-current-move-group nil)
1039 (defvar gnus-current-copy-group nil)
1040 (defvar gnus-current-crosspost-group nil)
1041 (defvar gnus-newsgroup-display nil)
1042
1043 (defvar gnus-newsgroup-dependencies nil)
1044 (defvar gnus-newsgroup-adaptive nil)
1045 (defvar gnus-summary-display-article-function nil)
1046 (defvar gnus-summary-highlight-line-function nil
1047   "Function called after highlighting a summary line.")
1048
1049 (defvar gnus-summary-line-format-alist
1050   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1051     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1052     (?s gnus-tmp-subject-or-nil ?s)
1053     (?n gnus-tmp-name ?s)
1054     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1055         ?s)
1056     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1057             gnus-tmp-from) ?s)
1058     (?F gnus-tmp-from ?s)
1059     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1060     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1061     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1062     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1063     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1064     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1065     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1066     (?L gnus-tmp-lines ?s)
1067     (?I gnus-tmp-indentation ?s)
1068     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1069     (?R gnus-tmp-replied ?c)
1070     (?\[ gnus-tmp-opening-bracket ?c)
1071     (?\] gnus-tmp-closing-bracket ?c)
1072     (?\> (make-string gnus-tmp-level ? ) ?s)
1073     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1074     (?i gnus-tmp-score ?d)
1075     (?z gnus-tmp-score-char ?c)
1076     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1077     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1078     (?U gnus-tmp-unread ?c)
1079     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1080         ?s)
1081     (?t (gnus-summary-number-of-articles-in-thread
1082          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1083         ?d)
1084     (?e (gnus-summary-number-of-articles-in-thread
1085          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1086         ?c)
1087     (?u gnus-tmp-user-defined ?s)
1088     (?P (gnus-pick-line-number) ?d)
1089     (?B gnus-tmp-thread-tree-header-string ?s)
1090     (user-date (gnus-user-date
1091                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1092   "An alist of format specifications that can appear in summary lines.
1093 These are paired with what variables they correspond with, along with
1094 the type of the variable (string, integer, character, etc).")
1095
1096 (defvar gnus-summary-dummy-line-format-alist
1097   `((?S gnus-tmp-subject ?s)
1098     (?N gnus-tmp-number ?d)
1099     (?u gnus-tmp-user-defined ?s)))
1100
1101 (defvar gnus-summary-mode-line-format-alist
1102   `((?G gnus-tmp-group-name ?s)
1103     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1104     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1105     (?A gnus-tmp-article-number ?d)
1106     (?Z gnus-tmp-unread-and-unselected ?s)
1107     (?V gnus-version ?s)
1108     (?U gnus-tmp-unread-and-unticked ?d)
1109     (?S gnus-tmp-subject ?s)
1110     (?e gnus-tmp-unselected ?d)
1111     (?u gnus-tmp-user-defined ?s)
1112     (?d (length gnus-newsgroup-dormant) ?d)
1113     (?t (length gnus-newsgroup-marked) ?d)
1114     (?r (length gnus-newsgroup-reads) ?d)
1115     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1116     (?E gnus-newsgroup-expunged-tally ?d)
1117     (?s (gnus-current-score-file-nondirectory) ?s)))
1118
1119 (defvar gnus-last-search-regexp nil
1120   "Default regexp for article search command.")
1121
1122 (defvar gnus-last-shell-command nil
1123   "Default shell command on article.")
1124
1125 (defvar gnus-newsgroup-begin nil)
1126 (defvar gnus-newsgroup-end nil)
1127 (defvar gnus-newsgroup-last-rmail nil)
1128 (defvar gnus-newsgroup-last-mail nil)
1129 (defvar gnus-newsgroup-last-folder nil)
1130 (defvar gnus-newsgroup-last-file nil)
1131 (defvar gnus-newsgroup-auto-expire nil)
1132 (defvar gnus-newsgroup-active nil)
1133
1134 (defvar gnus-newsgroup-data nil)
1135 (defvar gnus-newsgroup-data-reverse nil)
1136 (defvar gnus-newsgroup-limit nil)
1137 (defvar gnus-newsgroup-limits nil)
1138
1139 (defvar gnus-newsgroup-unreads nil
1140   "List of unread articles in the current newsgroup.")
1141
1142 (defvar gnus-newsgroup-unselected nil
1143   "List of unselected unread articles in the current newsgroup.")
1144
1145 (defvar gnus-newsgroup-reads nil
1146   "Alist of read articles and article marks in the current newsgroup.")
1147
1148 (defvar gnus-newsgroup-expunged-tally nil)
1149
1150 (defvar gnus-newsgroup-marked nil
1151   "List of ticked articles in the current newsgroup (a subset of unread art).")
1152
1153 (defvar gnus-newsgroup-killed nil
1154   "List of ranges of articles that have been through the scoring process.")
1155
1156 (defvar gnus-newsgroup-cached nil
1157   "List of articles that come from the article cache.")
1158
1159 (defvar gnus-newsgroup-saved nil
1160   "List of articles that have been saved.")
1161
1162 (defvar gnus-newsgroup-kill-headers nil)
1163
1164 (defvar gnus-newsgroup-replied nil
1165   "List of articles that have been replied to in the current newsgroup.")
1166
1167 (defvar gnus-newsgroup-forwarded nil
1168   "List of articles that have been forwarded in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-recent nil
1171   "List of articles that have are recent in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-expirable nil
1174   "List of articles in the current newsgroup that can be expired.")
1175
1176 (defvar gnus-newsgroup-processable nil
1177   "List of articles in the current newsgroup that can be processed.")
1178
1179 (defvar gnus-newsgroup-downloadable nil
1180   "List of articles in the current newsgroup that can be processed.")
1181
1182 (defvar gnus-newsgroup-undownloaded nil
1183   "List of articles in the current newsgroup that haven't been downloaded..")
1184
1185 (defvar gnus-newsgroup-unsendable nil
1186   "List of articles in the current newsgroup that won't be sent.")
1187
1188 (defvar gnus-newsgroup-bookmarks nil
1189   "List of articles in the current newsgroup that have bookmarks.")
1190
1191 (defvar gnus-newsgroup-dormant nil
1192   "List of dormant articles in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-unseen nil
1195   "List of unseen articles in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-seen nil
1198   "Range of seen articles in the current newsgroup.")
1199
1200 (defvar gnus-newsgroup-articles nil
1201   "List of articles in the current newsgroup.")
1202
1203 (defvar gnus-newsgroup-scored nil
1204   "List of scored articles in the current newsgroup.")
1205
1206 (defvar gnus-newsgroup-headers nil
1207   "List of article headers in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-threads nil)
1210
1211 (defvar gnus-newsgroup-prepared nil
1212   "Whether the current group has been prepared properly.")
1213
1214 (defvar gnus-newsgroup-ancient nil
1215   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1216
1217 (defvar gnus-newsgroup-sparse nil)
1218
1219 (defvar gnus-current-article nil)
1220 (defvar gnus-article-current nil)
1221 (defvar gnus-current-headers nil)
1222 (defvar gnus-have-all-headers nil)
1223 (defvar gnus-last-article nil)
1224 (defvar gnus-newsgroup-history nil)
1225 (defvar gnus-newsgroup-charset nil)
1226 (defvar gnus-newsgroup-ephemeral-charset nil)
1227 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1228
1229 (defvar gnus-article-before-search nil)
1230
1231 (defconst gnus-summary-local-variables
1232   '(gnus-newsgroup-name
1233     gnus-newsgroup-begin gnus-newsgroup-end
1234     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1235     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1236     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1237     gnus-newsgroup-unselected gnus-newsgroup-marked
1238     gnus-newsgroup-reads gnus-newsgroup-saved
1239     gnus-newsgroup-replied gnus-newsgroup-forwarded
1240     gnus-newsgroup-recent
1241     gnus-newsgroup-expirable
1242     gnus-newsgroup-processable gnus-newsgroup-killed
1243     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1244     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1245     gnus-newsgroup-seen gnus-newsgroup-articles
1246     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1247     gnus-newsgroup-headers gnus-newsgroup-threads
1248     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1249     gnus-current-article gnus-current-headers gnus-have-all-headers
1250     gnus-last-article gnus-article-internal-prepare-hook
1251     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1252     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1253     gnus-thread-expunge-below
1254     gnus-score-alist gnus-current-score-file
1255     (gnus-summary-expunge-below . global)
1256     (gnus-summary-mark-below . global)
1257     (gnus-orphan-score . global)
1258     gnus-newsgroup-active gnus-scores-exclude-files
1259     gnus-newsgroup-history gnus-newsgroup-ancient
1260     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1261     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1262     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1263     (gnus-newsgroup-expunged-tally . 0)
1264     gnus-cache-removable-articles gnus-newsgroup-cached
1265     gnus-newsgroup-data gnus-newsgroup-data-reverse
1266     gnus-newsgroup-limit gnus-newsgroup-limits
1267     gnus-newsgroup-charset gnus-newsgroup-display)
1268   "Variables that are buffer-local to the summary buffers.")
1269
1270 (defvar gnus-newsgroup-variables nil
1271   "A list of variables that have separate values in different newsgroups.
1272 A list of newsgroup (summary buffer) local variables, or cons of
1273 variables and their default values (when the default values are not
1274 nil), that should be made global while the summary buffer is active.
1275 These variables can be used to set variables in the group parameters
1276 while still allowing them to affect operations done in other
1277 buffers. For example:
1278
1279 (setq gnus-newsgroup-variables
1280      '(message-use-followup-to
1281        (gnus-visible-headers .
1282          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1283 ")
1284
1285 ;; Byte-compiler warning.
1286 (eval-when-compile (defvar gnus-article-mode-map))
1287
1288 ;; MIME stuff.
1289
1290 (defvar gnus-decode-encoded-word-methods
1291   '(mail-decode-encoded-word-string)
1292   "List of methods used to decode encoded words.
1293
1294 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1295 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1296 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1297 whose names match REGEXP.
1298
1299 For example:
1300 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1301  mail-decode-encoded-word-string
1302  (\"chinese\" . rfc1843-decode-string))")
1303
1304 (defvar gnus-decode-encoded-word-methods-cache nil)
1305
1306 (defun gnus-multi-decode-encoded-word-string (string)
1307   "Apply the functions from `gnus-encoded-word-methods' that match."
1308   (unless (and gnus-decode-encoded-word-methods-cache
1309                (eq gnus-newsgroup-name
1310                    (car gnus-decode-encoded-word-methods-cache)))
1311     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1312     (mapcar (lambda (x)
1313               (if (symbolp x)
1314                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1315                 (if (and gnus-newsgroup-name
1316                          (string-match (car x) gnus-newsgroup-name))
1317                     (nconc gnus-decode-encoded-word-methods-cache
1318                            (list (cdr x))))))
1319             gnus-decode-encoded-word-methods))
1320   (let ((xlist gnus-decode-encoded-word-methods-cache))
1321     (pop xlist)
1322     (while xlist
1323       (setq string (funcall (pop xlist) string))))
1324   string)
1325
1326 ;; Subject simplification.
1327
1328 (defun gnus-simplify-whitespace (str)
1329   "Remove excessive whitespace from STR."
1330   (let ((mystr str))
1331     ;; Multiple spaces.
1332     (while (string-match "[ \t][ \t]+" mystr)
1333       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1334                           " "
1335                           (substring mystr (match-end 0)))))
1336     ;; Leading spaces.
1337     (when (string-match "^[ \t]+" mystr)
1338       (setq mystr (substring mystr (match-end 0))))
1339     ;; Trailing spaces.
1340     (when (string-match "[ \t]+$" mystr)
1341       (setq mystr (substring mystr 0 (match-beginning 0))))
1342     mystr))
1343
1344 (defsubst gnus-simplify-subject-re (subject)
1345   "Remove \"Re:\" from subject lines."
1346   (if (string-match message-subject-re-regexp subject)
1347       (substring subject (match-end 0))
1348     subject))
1349
1350 (defun gnus-simplify-subject (subject &optional re-only)
1351   "Remove `Re:' and words in parentheses.
1352 If RE-ONLY is non-nil, strip leading `Re:'s only."
1353   (let ((case-fold-search t))           ;Ignore case.
1354     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1355     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1356       (setq subject (substring subject (match-end 0))))
1357     ;; Remove uninteresting prefixes.
1358     (when (and (not re-only)
1359                gnus-simplify-ignored-prefixes
1360                (string-match gnus-simplify-ignored-prefixes subject))
1361       (setq subject (substring subject (match-end 0))))
1362     ;; Remove words in parentheses from end.
1363     (unless re-only
1364       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1365         (setq subject (substring subject 0 (match-beginning 0)))))
1366     ;; Return subject string.
1367     subject))
1368
1369 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1370 ;; all whitespace.
1371 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1372   (goto-char (point-min))
1373   (while (re-search-forward regexp nil t)
1374     (replace-match (or newtext ""))))
1375
1376 (defun gnus-simplify-buffer-fuzzy ()
1377   "Simplify string in the buffer fuzzily.
1378 The string in the accessible portion of the current buffer is simplified.
1379 It is assumed to be a single-line subject.
1380 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1381 matter is removed.  Additional things can be deleted by setting
1382 `gnus-simplify-subject-fuzzy-regexp'."
1383   (let ((case-fold-search t)
1384         (modified-tick))
1385     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1386
1387     (while (not (eq modified-tick (buffer-modified-tick)))
1388       (setq modified-tick (buffer-modified-tick))
1389       (cond
1390        ((listp gnus-simplify-subject-fuzzy-regexp)
1391         (mapcar 'gnus-simplify-buffer-fuzzy-step
1392                 gnus-simplify-subject-fuzzy-regexp))
1393        (gnus-simplify-subject-fuzzy-regexp
1394         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1395       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1396       (gnus-simplify-buffer-fuzzy-step
1397        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1398       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1399
1400     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1401     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1402     (gnus-simplify-buffer-fuzzy-step " $")
1403     (gnus-simplify-buffer-fuzzy-step "^ +")))
1404
1405 (defun gnus-simplify-subject-fuzzy (subject)
1406   "Simplify a subject string fuzzily.
1407 See `gnus-simplify-buffer-fuzzy' for details."
1408   (save-excursion
1409     (gnus-set-work-buffer)
1410     (let ((case-fold-search t))
1411       ;; Remove uninteresting prefixes.
1412       (when (and gnus-simplify-ignored-prefixes
1413                  (string-match gnus-simplify-ignored-prefixes subject))
1414         (setq subject (substring subject (match-end 0))))
1415       (insert subject)
1416       (inline (gnus-simplify-buffer-fuzzy))
1417       (buffer-string))))
1418
1419 (defsubst gnus-simplify-subject-fully (subject)
1420   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1421   (cond
1422    (gnus-simplify-subject-functions
1423     (gnus-map-function gnus-simplify-subject-functions subject))
1424    ((null gnus-summary-gather-subject-limit)
1425     (gnus-simplify-subject-re subject))
1426    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1427     (gnus-simplify-subject-fuzzy subject))
1428    ((numberp gnus-summary-gather-subject-limit)
1429     (gnus-limit-string (gnus-simplify-subject-re subject)
1430                        gnus-summary-gather-subject-limit))
1431    (t
1432     subject)))
1433
1434 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1435   "Check whether two subjects are equal.
1436 If optional argument simple-first is t, first argument is already
1437 simplified."
1438   (cond
1439    ((null simple-first)
1440     (equal (gnus-simplify-subject-fully s1)
1441            (gnus-simplify-subject-fully s2)))
1442    (t
1443     (equal s1
1444            (gnus-simplify-subject-fully s2)))))
1445
1446 (defun gnus-summary-bubble-group ()
1447   "Increase the score of the current group.
1448 This is a handy function to add to `gnus-summary-exit-hook' to
1449 increase the score of each group you read."
1450   (gnus-group-add-score gnus-newsgroup-name))
1451
1452 \f
1453 ;;;
1454 ;;; Gnus summary mode
1455 ;;;
1456
1457 (put 'gnus-summary-mode 'mode-class 'special)
1458
1459 (defvar gnus-article-commands-menu)
1460
1461 (when t
1462   ;; Non-orthogonal keys
1463
1464   (gnus-define-keys gnus-summary-mode-map
1465     " " gnus-summary-next-page
1466     "\177" gnus-summary-prev-page
1467     [delete] gnus-summary-prev-page
1468     [backspace] gnus-summary-prev-page
1469     "\r" gnus-summary-scroll-up
1470     "\M-\r" gnus-summary-scroll-down
1471     "n" gnus-summary-next-unread-article
1472     "p" gnus-summary-prev-unread-article
1473     "N" gnus-summary-next-article
1474     "P" gnus-summary-prev-article
1475     "\M-\C-n" gnus-summary-next-same-subject
1476     "\M-\C-p" gnus-summary-prev-same-subject
1477     "\M-n" gnus-summary-next-unread-subject
1478     "\M-p" gnus-summary-prev-unread-subject
1479     "." gnus-summary-first-unread-article
1480     "," gnus-summary-best-unread-article
1481     "\M-s" gnus-summary-search-article-forward
1482     "\M-r" gnus-summary-search-article-backward
1483     "<" gnus-summary-beginning-of-article
1484     ">" gnus-summary-end-of-article
1485     "j" gnus-summary-goto-article
1486     "^" gnus-summary-refer-parent-article
1487     "\M-^" gnus-summary-refer-article
1488     "u" gnus-summary-tick-article-forward
1489     "!" gnus-summary-tick-article-forward
1490     "U" gnus-summary-tick-article-backward
1491     "d" gnus-summary-mark-as-read-forward
1492     "D" gnus-summary-mark-as-read-backward
1493     "E" gnus-summary-mark-as-expirable
1494     "\M-u" gnus-summary-clear-mark-forward
1495     "\M-U" gnus-summary-clear-mark-backward
1496     "k" gnus-summary-kill-same-subject-and-select
1497     "\C-k" gnus-summary-kill-same-subject
1498     "\M-\C-k" gnus-summary-kill-thread
1499     "\M-\C-l" gnus-summary-lower-thread
1500     "e" gnus-summary-edit-article
1501     "#" gnus-summary-mark-as-processable
1502     "\M-#" gnus-summary-unmark-as-processable
1503     "\M-\C-t" gnus-summary-toggle-threads
1504     "\M-\C-s" gnus-summary-show-thread
1505     "\M-\C-h" gnus-summary-hide-thread
1506     "\M-\C-f" gnus-summary-next-thread
1507     "\M-\C-b" gnus-summary-prev-thread
1508     [(meta down)] gnus-summary-next-thread
1509     [(meta up)] gnus-summary-prev-thread
1510     "\M-\C-u" gnus-summary-up-thread
1511     "\M-\C-d" gnus-summary-down-thread
1512     "&" gnus-summary-execute-command
1513     "c" gnus-summary-catchup-and-exit
1514     "\C-w" gnus-summary-mark-region-as-read
1515     "\C-t" gnus-summary-toggle-truncation
1516     "?" gnus-summary-mark-as-dormant
1517     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1518     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1519     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1520     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1521     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1522     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1523     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1524     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1525     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1526     "=" gnus-summary-expand-window
1527     "\C-x\C-s" gnus-summary-reselect-current-group
1528     "\M-g" gnus-summary-rescan-group
1529     "w" gnus-summary-stop-page-breaking
1530     "\C-c\C-r" gnus-summary-caesar-message
1531     "f" gnus-summary-followup
1532     "F" gnus-summary-followup-with-original
1533     "C" gnus-summary-cancel-article
1534     "r" gnus-summary-reply
1535     "R" gnus-summary-reply-with-original
1536     "\C-c\C-f" gnus-summary-mail-forward
1537     "o" gnus-summary-save-article
1538     "\C-o" gnus-summary-save-article-mail
1539     "|" gnus-summary-pipe-output
1540     "\M-k" gnus-summary-edit-local-kill
1541     "\M-K" gnus-summary-edit-global-kill
1542     ;; "V" gnus-version
1543     "\C-c\C-d" gnus-summary-describe-group
1544     "q" gnus-summary-exit
1545     "Q" gnus-summary-exit-no-update
1546     "\C-c\C-i" gnus-info-find-node
1547     gnus-mouse-2 gnus-mouse-pick-article
1548     "m" gnus-summary-mail-other-window
1549     "a" gnus-summary-post-news
1550     "i" gnus-summary-news-other-window
1551     "x" gnus-summary-limit-to-unread
1552     "s" gnus-summary-isearch-article
1553     "t" gnus-summary-toggle-header
1554     "g" gnus-summary-show-article
1555     "l" gnus-summary-goto-last-article
1556     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1557     "\C-d" gnus-summary-enter-digest-group
1558     "\M-\C-d" gnus-summary-read-document
1559     "\M-\C-e" gnus-summary-edit-parameters
1560     "\M-\C-a" gnus-summary-customize-parameters
1561     "\C-c\C-b" gnus-bug
1562     "*" gnus-cache-enter-article
1563     "\M-*" gnus-cache-remove-article
1564     "\M-&" gnus-summary-universal-argument
1565     "\C-l" gnus-recenter
1566     "I" gnus-summary-increase-score
1567     "L" gnus-summary-lower-score
1568     "\M-i" gnus-symbolic-argument
1569     "h" gnus-summary-select-article-buffer
1570
1571     "b" gnus-article-view-part
1572     "\M-t" gnus-summary-toggle-display-buttonized
1573
1574     "V" gnus-summary-score-map
1575     "X" gnus-uu-extract-map
1576     "S" gnus-summary-send-map)
1577
1578   ;; Sort of orthogonal keymap
1579   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1580     "t" gnus-summary-tick-article-forward
1581     "!" gnus-summary-tick-article-forward
1582     "d" gnus-summary-mark-as-read-forward
1583     "r" gnus-summary-mark-as-read-forward
1584     "c" gnus-summary-clear-mark-forward
1585     " " gnus-summary-clear-mark-forward
1586     "e" gnus-summary-mark-as-expirable
1587     "x" gnus-summary-mark-as-expirable
1588     "?" gnus-summary-mark-as-dormant
1589     "b" gnus-summary-set-bookmark
1590     "B" gnus-summary-remove-bookmark
1591     "#" gnus-summary-mark-as-processable
1592     "\M-#" gnus-summary-unmark-as-processable
1593     "S" gnus-summary-limit-include-expunged
1594     "C" gnus-summary-catchup
1595     "H" gnus-summary-catchup-to-here
1596     "h" gnus-summary-catchup-from-here
1597     "\C-c" gnus-summary-catchup-all
1598     "k" gnus-summary-kill-same-subject-and-select
1599     "K" gnus-summary-kill-same-subject
1600     "P" gnus-uu-mark-map)
1601
1602   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1603     "c" gnus-summary-clear-above
1604     "u" gnus-summary-tick-above
1605     "m" gnus-summary-mark-above
1606     "k" gnus-summary-kill-below)
1607
1608   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1609     "/" gnus-summary-limit-to-subject
1610     "n" gnus-summary-limit-to-articles
1611     "w" gnus-summary-pop-limit
1612     "s" gnus-summary-limit-to-subject
1613     "a" gnus-summary-limit-to-author
1614     "u" gnus-summary-limit-to-unread
1615     "m" gnus-summary-limit-to-marks
1616     "M" gnus-summary-limit-exclude-marks
1617     "v" gnus-summary-limit-to-score
1618     "*" gnus-summary-limit-include-cached
1619     "D" gnus-summary-limit-include-dormant
1620     "T" gnus-summary-limit-include-thread
1621     "d" gnus-summary-limit-exclude-dormant
1622     "t" gnus-summary-limit-to-age
1623     "x" gnus-summary-limit-to-extra
1624     "p" gnus-summary-limit-to-display-predicate
1625     "E" gnus-summary-limit-include-expunged
1626     "c" gnus-summary-limit-exclude-childless-dormant
1627     "C" gnus-summary-limit-mark-excluded-as-read
1628     "o" gnus-summary-insert-old-articles
1629     "N" gnus-summary-insert-new-articles)
1630
1631   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1632     "n" gnus-summary-next-unread-article
1633     "p" gnus-summary-prev-unread-article
1634     "N" gnus-summary-next-article
1635     "P" gnus-summary-prev-article
1636     "\C-n" gnus-summary-next-same-subject
1637     "\C-p" gnus-summary-prev-same-subject
1638     "\M-n" gnus-summary-next-unread-subject
1639     "\M-p" gnus-summary-prev-unread-subject
1640     "f" gnus-summary-first-unread-article
1641     "b" gnus-summary-best-unread-article
1642     "j" gnus-summary-goto-article
1643     "g" gnus-summary-goto-subject
1644     "l" gnus-summary-goto-last-article
1645     "o" gnus-summary-pop-article)
1646
1647   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1648     "k" gnus-summary-kill-thread
1649     "l" gnus-summary-lower-thread
1650     "i" gnus-summary-raise-thread
1651     "T" gnus-summary-toggle-threads
1652     "t" gnus-summary-rethread-current
1653     "^" gnus-summary-reparent-thread
1654     "s" gnus-summary-show-thread
1655     "S" gnus-summary-show-all-threads
1656     "h" gnus-summary-hide-thread
1657     "H" gnus-summary-hide-all-threads
1658     "n" gnus-summary-next-thread
1659     "p" gnus-summary-prev-thread
1660     "u" gnus-summary-up-thread
1661     "o" gnus-summary-top-thread
1662     "d" gnus-summary-down-thread
1663     "#" gnus-uu-mark-thread
1664     "\M-#" gnus-uu-unmark-thread)
1665
1666   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1667     "g" gnus-summary-prepare
1668     "c" gnus-summary-insert-cached-articles)
1669
1670   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1671     "c" gnus-summary-catchup-and-exit
1672     "C" gnus-summary-catchup-all-and-exit
1673     "E" gnus-summary-exit-no-update
1674     "Q" gnus-summary-exit
1675     "Z" gnus-summary-exit
1676     "n" gnus-summary-catchup-and-goto-next-group
1677     "R" gnus-summary-reselect-current-group
1678     "G" gnus-summary-rescan-group
1679     "N" gnus-summary-next-group
1680     "s" gnus-summary-save-newsrc
1681     "P" gnus-summary-prev-group)
1682
1683   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1684     " " gnus-summary-next-page
1685     "n" gnus-summary-next-page
1686     "\177" gnus-summary-prev-page
1687     [delete] gnus-summary-prev-page
1688     "p" gnus-summary-prev-page
1689     "\r" gnus-summary-scroll-up
1690     "\M-\r" gnus-summary-scroll-down
1691     "<" gnus-summary-beginning-of-article
1692     ">" gnus-summary-end-of-article
1693     "b" gnus-summary-beginning-of-article
1694     "e" gnus-summary-end-of-article
1695     "^" gnus-summary-refer-parent-article
1696     "r" gnus-summary-refer-parent-article
1697     "D" gnus-summary-enter-digest-group
1698     "R" gnus-summary-refer-references
1699     "T" gnus-summary-refer-thread
1700     "g" gnus-summary-show-article
1701     "s" gnus-summary-isearch-article
1702     "P" gnus-summary-print-article
1703     "M" gnus-mailing-list-insinuate
1704     "t" gnus-article-babel)
1705
1706   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1707     "b" gnus-article-add-buttons
1708     "B" gnus-article-add-buttons-to-head
1709     "o" gnus-article-treat-overstrike
1710     "e" gnus-article-emphasize
1711     "w" gnus-article-fill-cited-article
1712     "Q" gnus-article-fill-long-lines
1713     "C" gnus-article-capitalize-sentences
1714     "c" gnus-article-remove-cr
1715     "q" gnus-article-de-quoted-unreadable
1716     "6" gnus-article-de-base64-unreadable
1717     "Z" gnus-article-decode-HZ
1718     "h" gnus-article-wash-html
1719     "s" gnus-summary-force-verify-and-decrypt
1720     "f" gnus-article-display-x-face
1721     "l" gnus-summary-stop-page-breaking
1722     "r" gnus-summary-caesar-message
1723     "t" gnus-summary-toggle-header
1724     "g" gnus-summary-toggle-smiley
1725     "v" gnus-summary-verbose-headers
1726     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1727     "p" gnus-article-verify-x-pgp-sig
1728     "d" gnus-article-treat-dumbquotes)
1729
1730   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1731     "a" gnus-article-hide
1732     "h" gnus-article-hide-headers
1733     "b" gnus-article-hide-boring-headers
1734     "s" gnus-article-hide-signature
1735     "c" gnus-article-hide-citation
1736     "C" gnus-article-hide-citation-in-followups
1737     "l" gnus-article-hide-list-identifiers
1738     "p" gnus-article-hide-pgp
1739     "B" gnus-article-strip-banner
1740     "P" gnus-article-hide-pem
1741     "\C-c" gnus-article-hide-citation-maybe)
1742
1743   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1744     "a" gnus-article-highlight
1745     "h" gnus-article-highlight-headers
1746     "c" gnus-article-highlight-citation
1747     "s" gnus-article-highlight-signature)
1748
1749   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1750     "w" gnus-article-decode-mime-words
1751     "c" gnus-article-decode-charset
1752     "v" gnus-mime-view-all-parts
1753     "b" gnus-article-view-part)
1754
1755   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1756     "z" gnus-article-date-ut
1757     "u" gnus-article-date-ut
1758     "l" gnus-article-date-local
1759     "p" gnus-article-date-english
1760     "e" gnus-article-date-lapsed
1761     "o" gnus-article-date-original
1762     "i" gnus-article-date-iso8601
1763     "s" gnus-article-date-user)
1764
1765   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1766     "t" gnus-article-remove-trailing-blank-lines
1767     "l" gnus-article-strip-leading-blank-lines
1768     "m" gnus-article-strip-multiple-blank-lines
1769     "a" gnus-article-strip-blank-lines
1770     "A" gnus-article-strip-all-blank-lines
1771     "s" gnus-article-strip-leading-space
1772     "e" gnus-article-strip-trailing-space
1773     "w" gnus-article-remove-leading-whitespace)
1774
1775   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1776     "v" gnus-version
1777     "f" gnus-summary-fetch-faq
1778     "d" gnus-summary-describe-group
1779     "h" gnus-summary-describe-briefly
1780     "i" gnus-info-find-node)
1781
1782   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1783     "e" gnus-summary-expire-articles
1784     "\M-\C-e" gnus-summary-expire-articles-now
1785     "\177" gnus-summary-delete-article
1786     [delete] gnus-summary-delete-article
1787     [backspace] gnus-summary-delete-article
1788     "m" gnus-summary-move-article
1789     "r" gnus-summary-respool-article
1790     "w" gnus-summary-edit-article
1791     "c" gnus-summary-copy-article
1792     "B" gnus-summary-crosspost-article
1793     "q" gnus-summary-respool-query
1794     "t" gnus-summary-respool-trace
1795     "i" gnus-summary-import-article
1796     "I" gnus-summary-create-article
1797     "p" gnus-summary-article-posted-p)
1798
1799   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1800     "o" gnus-summary-save-article
1801     "m" gnus-summary-save-article-mail
1802     "F" gnus-summary-write-article-file
1803     "r" gnus-summary-save-article-rmail
1804     "f" gnus-summary-save-article-file
1805     "b" gnus-summary-save-article-body-file
1806     "h" gnus-summary-save-article-folder
1807     "v" gnus-summary-save-article-vm
1808     "p" gnus-summary-pipe-output
1809     "P" gnus-summary-muttprint
1810     "s" gnus-soup-add-article)
1811
1812   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1813     "b" gnus-summary-display-buttonized
1814     "m" gnus-summary-repair-multipart
1815     "v" gnus-article-view-part
1816     "o" gnus-article-save-part
1817     "c" gnus-article-copy-part
1818     "C" gnus-article-view-part-as-charset
1819     "e" gnus-article-externalize-part
1820     "E" gnus-article-encrypt-body
1821     "i" gnus-article-inline-part
1822     "|" gnus-article-pipe-part))
1823
1824 (defvar gnus-article-post-menu nil)
1825
1826 (defun gnus-summary-make-menu-bar ()
1827   (gnus-turn-off-edit-menu 'summary)
1828
1829   (unless (boundp 'gnus-summary-misc-menu)
1830
1831     (easy-menu-define
1832       gnus-summary-kill-menu gnus-summary-mode-map ""
1833       (cons
1834        "Score"
1835        (nconc
1836         (list
1837          ["Customize" gnus-score-customize t])
1838         (gnus-make-score-map 'increase)
1839         (gnus-make-score-map 'lower)
1840         '(("Mark"
1841            ["Kill below" gnus-summary-kill-below t]
1842            ["Mark above" gnus-summary-mark-above t]
1843            ["Tick above" gnus-summary-tick-above t]
1844            ["Clear above" gnus-summary-clear-above t])
1845           ["Current score" gnus-summary-current-score t]
1846           ["Set score" gnus-summary-set-score t]
1847           ["Switch current score file..." gnus-score-change-score-file t]
1848           ["Set mark below..." gnus-score-set-mark-below t]
1849           ["Set expunge below..." gnus-score-set-expunge-below t]
1850           ["Edit current score file" gnus-score-edit-current-scores t]
1851           ["Edit score file" gnus-score-edit-file t]
1852           ["Trace score" gnus-score-find-trace t]
1853           ["Find words" gnus-score-find-favourite-words t]
1854           ["Rescore buffer" gnus-summary-rescore t]
1855           ["Increase score..." gnus-summary-increase-score t]
1856           ["Lower score..." gnus-summary-lower-score t]))))
1857
1858 ;; Define both the Article menu in the summary buffer and the equivalent
1859     ;; Commands menu in the article buffer here for consistency.
1860     (let ((innards
1861            `(("Hide"
1862               ["All" gnus-article-hide t]
1863               ["Headers" gnus-article-hide-headers t]
1864               ["Signature" gnus-article-hide-signature t]
1865               ["Citation" gnus-article-hide-citation t]
1866               ["List identifiers" gnus-article-hide-list-identifiers t]
1867               ["PGP" gnus-article-hide-pgp t]
1868               ["Banner" gnus-article-strip-banner t]
1869               ["Boring headers" gnus-article-hide-boring-headers t])
1870              ("Highlight"
1871               ["All" gnus-article-highlight t]
1872               ["Headers" gnus-article-highlight-headers t]
1873               ["Signature" gnus-article-highlight-signature t]
1874               ["Citation" gnus-article-highlight-citation t])
1875              ("MIME"
1876               ["Words" gnus-article-decode-mime-words t]
1877               ["Charset" gnus-article-decode-charset t]
1878               ["QP" gnus-article-de-quoted-unreadable t]
1879               ["Base64" gnus-article-de-base64-unreadable t]
1880               ["View all" gnus-mime-view-all-parts t]
1881               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1882               ["Encrypt body" gnus-article-encrypt-body t])
1883              ("Date"
1884               ["Local" gnus-article-date-local t]
1885               ["ISO8601" gnus-article-date-iso8601 t]
1886               ["UT" gnus-article-date-ut t]
1887               ["Original" gnus-article-date-original t]
1888               ["Lapsed" gnus-article-date-lapsed t]
1889               ["User-defined" gnus-article-date-user t])
1890              ("Washing"
1891               ("Remove Blanks"
1892                ["Leading" gnus-article-strip-leading-blank-lines t]
1893                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1894                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1895                ["All of the above" gnus-article-strip-blank-lines t]
1896                ["All" gnus-article-strip-all-blank-lines t]
1897                ["Leading space" gnus-article-strip-leading-space t]
1898                ["Trailing space" gnus-article-strip-trailing-space t]
1899                ["Leading space in headers"
1900                 gnus-article-remove-leading-whitespace t])
1901               ["Overstrike" gnus-article-treat-overstrike t]
1902               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1903               ["Emphasis" gnus-article-emphasize t]
1904               ["Word wrap" gnus-article-fill-cited-article t]
1905               ["Fill long lines" gnus-article-fill-long-lines t]
1906               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1907               ["CR" gnus-article-remove-cr t]
1908               ["Show X-Face" gnus-article-display-x-face t]
1909               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1910               ["Base64" gnus-article-de-base64-unreadable t]
1911               ["Rot 13" gnus-summary-caesar-message
1912                ,@(if (featurep 'xemacs) '(t)
1913                    '(:help "\"Caesar rotate\" article by 13"))]
1914               ["Unix pipe" gnus-summary-pipe-message t]
1915               ["Add buttons" gnus-article-add-buttons t]
1916               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1917               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1918               ["Verbose header" gnus-summary-verbose-headers t]
1919               ["Toggle header" gnus-summary-toggle-header t]
1920               ["Toggle smiley" gnus-summary-toggle-smiley t]
1921               ["Html" gnus-article-wash-html t]
1922               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1923               ["HZ" gnus-article-decode-HZ t])
1924              ("Output"
1925               ["Save in default format" gnus-summary-save-article
1926                ,@(if (featurep 'xemacs) '(t)
1927                    '(:help "Save article using default method"))]
1928               ["Save in file" gnus-summary-save-article-file
1929                ,@(if (featurep 'xemacs) '(t)
1930                    '(:help "Save article in file"))]
1931               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1932               ["Save in MH folder" gnus-summary-save-article-folder t]
1933               ["Save in VM folder" gnus-summary-save-article-vm t]
1934               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1935               ["Save body in file" gnus-summary-save-article-body-file t]
1936               ["Pipe through a filter" gnus-summary-pipe-output t]
1937               ["Add to SOUP packet" gnus-soup-add-article t]
1938               ["Print with Muttprint" gnus-summary-muttprint t]
1939               ["Print" gnus-summary-print-article t])
1940              ("Backend"
1941               ["Respool article..." gnus-summary-respool-article t]
1942               ["Move article..." gnus-summary-move-article
1943                (gnus-check-backend-function
1944                 'request-move-article gnus-newsgroup-name)]
1945               ["Copy article..." gnus-summary-copy-article t]
1946               ["Crosspost article..." gnus-summary-crosspost-article
1947                (gnus-check-backend-function
1948                 'request-replace-article gnus-newsgroup-name)]
1949               ["Import file..." gnus-summary-import-article t]
1950               ["Create article..." gnus-summary-create-article t]
1951               ["Check if posted" gnus-summary-article-posted-p t]
1952               ["Edit article" gnus-summary-edit-article
1953                (not (gnus-group-read-only-p))]
1954               ["Delete article" gnus-summary-delete-article
1955                (gnus-check-backend-function
1956                 'request-expire-articles gnus-newsgroup-name)]
1957               ["Query respool" gnus-summary-respool-query t]
1958               ["Trace respool" gnus-summary-respool-trace t]
1959               ["Delete expirable articles" gnus-summary-expire-articles-now
1960                (gnus-check-backend-function
1961                 'request-expire-articles gnus-newsgroup-name)])
1962              ("Extract"
1963               ["Uudecode" gnus-uu-decode-uu
1964                ,@(if (featurep 'xemacs) '(t)
1965                    '(:help "Decode uuencoded article(s)"))]
1966               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1967               ["Unshar" gnus-uu-decode-unshar t]
1968               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1969               ["Save" gnus-uu-decode-save t]
1970               ["Binhex" gnus-uu-decode-binhex t]
1971               ["Postscript" gnus-uu-decode-postscript t])
1972              ("Cache"
1973               ["Enter article" gnus-cache-enter-article t]
1974               ["Remove article" gnus-cache-remove-article t])
1975              ["Translate" gnus-article-babel t]
1976              ["Select article buffer" gnus-summary-select-article-buffer t]
1977              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1978              ["Isearch article..." gnus-summary-isearch-article t]
1979              ["Beginning of the article" gnus-summary-beginning-of-article t]
1980              ["End of the article" gnus-summary-end-of-article t]
1981              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1982              ["Fetch referenced articles" gnus-summary-refer-references t]
1983              ["Fetch current thread" gnus-summary-refer-thread t]
1984              ["Fetch article with id..." gnus-summary-refer-article t]
1985              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1986              ["Redisplay" gnus-summary-show-article t]
1987              ["Raw article" gnus-summary-show-raw-article t])))
1988       (easy-menu-define
1989         gnus-summary-article-menu gnus-summary-mode-map ""
1990         (cons "Article" innards))
1991
1992       (if (not (keymapp gnus-summary-article-menu))
1993           (easy-menu-define
1994             gnus-article-commands-menu gnus-article-mode-map ""
1995             (cons "Commands" innards))
1996         ;; in Emacs, don't share menu.
1997         (setq gnus-article-commands-menu
1998               (copy-keymap gnus-summary-article-menu))
1999         (define-key gnus-article-mode-map [menu-bar commands]
2000           (cons "Commands" gnus-article-commands-menu))))
2001
2002     (easy-menu-define
2003       gnus-summary-thread-menu gnus-summary-mode-map ""
2004       '("Threads"
2005         ["Toggle threading" gnus-summary-toggle-threads t]
2006         ["Hide threads" gnus-summary-hide-all-threads t]
2007         ["Show threads" gnus-summary-show-all-threads t]
2008         ["Hide thread" gnus-summary-hide-thread t]
2009         ["Show thread" gnus-summary-show-thread t]
2010         ["Go to next thread" gnus-summary-next-thread t]
2011         ["Go to previous thread" gnus-summary-prev-thread t]
2012         ["Go down thread" gnus-summary-down-thread t]
2013         ["Go up thread" gnus-summary-up-thread t]
2014         ["Top of thread" gnus-summary-top-thread t]
2015         ["Mark thread as read" gnus-summary-kill-thread t]
2016         ["Lower thread score" gnus-summary-lower-thread t]
2017         ["Raise thread score" gnus-summary-raise-thread t]
2018         ["Rethread current" gnus-summary-rethread-current t]))
2019
2020     (easy-menu-define
2021       gnus-summary-post-menu gnus-summary-mode-map ""
2022       `("Post"
2023         ["Send a message (mail or news)" gnus-summary-post-news
2024          ,@(if (featurep 'xemacs) '(t)
2025              '(:help "Post an article"))]
2026         ["Followup" gnus-summary-followup
2027          ,@(if (featurep 'xemacs) '(t)
2028              '(:help "Post followup to this article"))]
2029         ["Followup and yank" gnus-summary-followup-with-original
2030          ,@(if (featurep 'xemacs) '(t)
2031              '(:help "Post followup to this article, quoting its contents"))]
2032         ["Supersede article" gnus-summary-supersede-article t]
2033         ["Cancel article" gnus-summary-cancel-article
2034          ,@(if (featurep 'xemacs) '(t)
2035              '(:help "Cancel an article you posted"))]
2036         ["Reply" gnus-summary-reply t]
2037         ["Reply and yank" gnus-summary-reply-with-original t]
2038         ["Wide reply" gnus-summary-wide-reply t]
2039         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2040          ,@(if (featurep 'xemacs) '(t)
2041              '(:help "Mail a reply, quoting this article"))]
2042         ["Mail forward" gnus-summary-mail-forward t]
2043         ["Post forward" gnus-summary-post-forward t]
2044         ["Digest and mail" gnus-uu-digest-mail-forward t]
2045         ["Digest and post" gnus-uu-digest-post-forward t]
2046         ["Resend message" gnus-summary-resend-message t]
2047         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2048         ["Send a mail" gnus-summary-mail-other-window t]
2049         ["Create a local message" gnus-summary-news-other-window t]
2050         ["Uuencode and post" gnus-uu-post-news
2051          ,@(if (featurep 'xemacs) '(t)
2052              '(:help "Post a uuencoded article"))]
2053         ["Followup via news" gnus-summary-followup-to-mail t]
2054         ["Followup via news and yank"
2055          gnus-summary-followup-to-mail-with-original t]
2056         ;;("Draft"
2057         ;;["Send" gnus-summary-send-draft t]
2058         ;;["Send bounced" gnus-resend-bounced-mail t])
2059         ))
2060
2061     (cond
2062      ((not (keymapp gnus-summary-post-menu))
2063       (setq gnus-article-post-menu gnus-summary-post-menu))
2064      ((not gnus-article-post-menu)
2065       ;; Don't share post menu.
2066       (setq gnus-article-post-menu
2067             (copy-keymap gnus-summary-post-menu))))
2068     (define-key gnus-article-mode-map [menu-bar post]
2069       (cons "Post" gnus-article-post-menu))
2070
2071     (easy-menu-define
2072       gnus-summary-misc-menu gnus-summary-mode-map ""
2073       `("Gnus"
2074         ("Mark Read"
2075          ["Mark as read" gnus-summary-mark-as-read-forward t]
2076          ["Mark same subject and select"
2077           gnus-summary-kill-same-subject-and-select t]
2078          ["Mark same subject" gnus-summary-kill-same-subject t]
2079          ["Catchup" gnus-summary-catchup
2080           ,@(if (featurep 'xemacs) '(t)
2081               '(:help "Mark unread articles in this group as read"))]
2082          ["Catchup all" gnus-summary-catchup-all t]
2083          ["Catchup to here" gnus-summary-catchup-to-here t]
2084          ["Catchup from here" gnus-summary-catchup-from-here t]
2085          ["Catchup region" gnus-summary-mark-region-as-read t]
2086          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2087         ("Mark Various"
2088          ["Tick" gnus-summary-tick-article-forward t]
2089          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2090          ["Remove marks" gnus-summary-clear-mark-forward t]
2091          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2092          ["Set bookmark" gnus-summary-set-bookmark t]
2093          ["Remove bookmark" gnus-summary-remove-bookmark t])
2094         ("Mark Limit"
2095          ["Marks..." gnus-summary-limit-to-marks t]
2096          ["Subject..." gnus-summary-limit-to-subject t]
2097          ["Author..." gnus-summary-limit-to-author t]
2098          ["Age..." gnus-summary-limit-to-age t]
2099          ["Extra..." gnus-summary-limit-to-extra t]
2100          ["Score" gnus-summary-limit-to-score t]
2101          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2102          ["Unread" gnus-summary-limit-to-unread t]
2103          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2104          ["Articles" gnus-summary-limit-to-articles t]
2105          ["Pop limit" gnus-summary-pop-limit t]
2106          ["Show dormant" gnus-summary-limit-include-dormant t]
2107          ["Hide childless dormant"
2108           gnus-summary-limit-exclude-childless-dormant t]
2109          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2110          ["Hide marked" gnus-summary-limit-exclude-marks t]
2111          ["Show expunged" gnus-summary-limit-include-expunged t])
2112         ("Process Mark"
2113          ["Set mark" gnus-summary-mark-as-processable t]
2114          ["Remove mark" gnus-summary-unmark-as-processable t]
2115          ["Remove all marks" gnus-summary-unmark-all-processable t]
2116          ["Mark above" gnus-uu-mark-over t]
2117          ["Mark series" gnus-uu-mark-series t]
2118          ["Mark region" gnus-uu-mark-region t]
2119          ["Unmark region" gnus-uu-unmark-region t]
2120          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2121          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2122          ["Mark all" gnus-uu-mark-all t]
2123          ["Mark buffer" gnus-uu-mark-buffer t]
2124          ["Mark sparse" gnus-uu-mark-sparse t]
2125          ["Mark thread" gnus-uu-mark-thread t]
2126          ["Unmark thread" gnus-uu-unmark-thread t]
2127          ("Process Mark Sets"
2128           ["Kill" gnus-summary-kill-process-mark t]
2129           ["Yank" gnus-summary-yank-process-mark
2130            gnus-newsgroup-process-stack]
2131           ["Save" gnus-summary-save-process-mark t]))
2132         ("Scroll article"
2133          ["Page forward" gnus-summary-next-page
2134           ,@(if (featurep 'xemacs) '(t)
2135               '(:help "Show next page of article"))]
2136          ["Page backward" gnus-summary-prev-page
2137           ,@(if (featurep 'xemacs) '(t)
2138               '(:help "Show previous page of article"))]
2139          ["Line forward" gnus-summary-scroll-up t])
2140         ("Move"
2141          ["Next unread article" gnus-summary-next-unread-article t]
2142          ["Previous unread article" gnus-summary-prev-unread-article t]
2143          ["Next article" gnus-summary-next-article t]
2144          ["Previous article" gnus-summary-prev-article t]
2145          ["Next unread subject" gnus-summary-next-unread-subject t]
2146          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2147          ["Next article same subject" gnus-summary-next-same-subject t]
2148          ["Previous article same subject" gnus-summary-prev-same-subject t]
2149          ["First unread article" gnus-summary-first-unread-article t]
2150          ["Best unread article" gnus-summary-best-unread-article t]
2151          ["Go to subject number..." gnus-summary-goto-subject t]
2152          ["Go to article number..." gnus-summary-goto-article t]
2153          ["Go to the last article" gnus-summary-goto-last-article t]
2154          ["Pop article off history" gnus-summary-pop-article t])
2155         ("Sort"
2156          ["Sort by number" gnus-summary-sort-by-number t]
2157          ["Sort by author" gnus-summary-sort-by-author t]
2158          ["Sort by subject" gnus-summary-sort-by-subject t]
2159          ["Sort by date" gnus-summary-sort-by-date t]
2160          ["Sort by score" gnus-summary-sort-by-score t]
2161          ["Sort by lines" gnus-summary-sort-by-lines t]
2162          ["Sort by characters" gnus-summary-sort-by-chars t]
2163          ["Original sort" gnus-summary-sort-by-original t])
2164         ("Help"
2165          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2166          ["Describe group" gnus-summary-describe-group t]
2167          ["Read manual" gnus-info-find-node t])
2168         ("Modes"
2169          ["Pick and read" gnus-pick-mode t]
2170          ["Binary" gnus-binary-mode t])
2171         ("Regeneration"
2172          ["Regenerate" gnus-summary-prepare t]
2173          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2174          ["Toggle threading" gnus-summary-toggle-threads t])
2175         ["See old articles" gnus-summary-insert-old-articles t]
2176         ["See new articles" gnus-summary-insert-new-articles t]
2177         ["Filter articles..." gnus-summary-execute-command t]
2178         ["Run command on subjects..." gnus-summary-universal-argument t]
2179         ["Search articles forward..." gnus-summary-search-article-forward t]
2180         ["Search articles backward..." gnus-summary-search-article-backward t]
2181         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2182         ["Expand window" gnus-summary-expand-window t]
2183         ["Expire expirable articles" gnus-summary-expire-articles
2184          (gnus-check-backend-function
2185           'request-expire-articles gnus-newsgroup-name)]
2186         ["Edit local kill file" gnus-summary-edit-local-kill t]
2187         ["Edit main kill file" gnus-summary-edit-global-kill t]
2188         ["Edit group parameters" gnus-summary-edit-parameters t]
2189         ["Customize group parameters" gnus-summary-customize-parameters t]
2190         ["Send a bug report" gnus-bug t]
2191         ("Exit"
2192          ["Catchup and exit" gnus-summary-catchup-and-exit
2193           ,@(if (featurep 'xemacs) '(t)
2194               '(:help "Mark unread articles in this group as read, then exit"))]
2195          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2196          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2197          ["Exit group" gnus-summary-exit
2198           ,@(if (featurep 'xemacs) '(t)
2199               '(:help "Exit current group, return to group selection mode"))]
2200          ["Exit group without updating" gnus-summary-exit-no-update t]
2201          ["Exit and goto next group" gnus-summary-next-group t]
2202          ["Exit and goto prev group" gnus-summary-prev-group t]
2203          ["Reselect group" gnus-summary-reselect-current-group t]
2204          ["Rescan group" gnus-summary-rescan-group t]
2205          ["Update dribble" gnus-summary-save-newsrc t])))
2206
2207     (gnus-run-hooks 'gnus-summary-menu-hook)))
2208
2209 (defvar gnus-summary-tool-bar-map nil)
2210
2211 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2212 (defun gnus-summary-make-tool-bar ()
2213   (if (and (fboundp 'tool-bar-add-item-from-menu)
2214            (default-value 'tool-bar-mode)
2215            (not gnus-summary-tool-bar-map))
2216       (setq gnus-summary-tool-bar-map
2217             (let ((tool-bar-map (make-sparse-keymap))
2218                   (load-path (mm-image-load-path)))
2219               (tool-bar-add-item-from-menu
2220                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2221               (tool-bar-add-item-from-menu
2222                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2223               (tool-bar-add-item-from-menu
2224                'gnus-summary-post-news "post" gnus-summary-mode-map)
2225               (tool-bar-add-item-from-menu
2226                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2227               (tool-bar-add-item-from-menu
2228                'gnus-summary-followup "followup" gnus-summary-mode-map)
2229               (tool-bar-add-item-from-menu
2230                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2231               (tool-bar-add-item-from-menu
2232                'gnus-summary-reply "reply" gnus-summary-mode-map)
2233               (tool-bar-add-item-from-menu
2234                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2235               (tool-bar-add-item-from-menu
2236                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2237               (tool-bar-add-item-from-menu
2238                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2239               (tool-bar-add-item-from-menu
2240                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2241               (tool-bar-add-item-from-menu
2242                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2243               (tool-bar-add-item-from-menu
2244                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2245               (tool-bar-add-item-from-menu
2246                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2247               (tool-bar-add-item-from-menu
2248                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2249               tool-bar-map)))
2250   (if gnus-summary-tool-bar-map
2251       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2252
2253 (defun gnus-score-set-default (var value)
2254   "A version of set that updates the GNU Emacs menu-bar."
2255   (set var value)
2256   ;; It is the message that forces the active status to be updated.
2257   (message ""))
2258
2259 (defun gnus-make-score-map (type)
2260   "Make a summary score map of type TYPE."
2261   (if t
2262       nil
2263     (let ((headers '(("author" "from" string)
2264                      ("subject" "subject" string)
2265                      ("article body" "body" string)
2266                      ("article head" "head" string)
2267                      ("xref" "xref" string)
2268                      ("extra header" "extra" string)
2269                      ("lines" "lines" number)
2270                      ("followups to author" "followup" string)))
2271           (types '((number ("less than" <)
2272                            ("greater than" >)
2273                            ("equal" =))
2274                    (string ("substring" s)
2275                            ("exact string" e)
2276                            ("fuzzy string" f)
2277                            ("regexp" r))))
2278           (perms '(("temporary" (current-time-string))
2279                    ("permanent" nil)
2280                    ("immediate" now)))
2281           header)
2282       (list
2283        (apply
2284         'nconc
2285         (list
2286          (if (eq type 'lower)
2287              "Lower score"
2288            "Increase score"))
2289         (let (outh)
2290           (while headers
2291             (setq header (car headers))
2292             (setq outh
2293                   (cons
2294                    (apply
2295                     'nconc
2296                     (list (car header))
2297                     (let ((ts (cdr (assoc (nth 2 header) types)))
2298                           outt)
2299                       (while ts
2300                         (setq outt
2301                               (cons
2302                                (apply
2303                                 'nconc
2304                                 (list (caar ts))
2305                                 (let ((ps perms)
2306                                       outp)
2307                                   (while ps
2308                                     (setq outp
2309                                           (cons
2310                                            (vector
2311                                             (caar ps)
2312                                             (list
2313                                              'gnus-summary-score-entry
2314                                              (nth 1 header)
2315                                              (if (or (string= (nth 1 header)
2316                                                               "head")
2317                                                      (string= (nth 1 header)
2318                                                               "body"))
2319                                                  ""
2320                                                (list 'gnus-summary-header
2321                                                      (nth 1 header)))
2322                                              (list 'quote (nth 1 (car ts)))
2323                                              (list 'gnus-score-delta-default
2324                                                    nil)
2325                                              (nth 1 (car ps))
2326                                              t)
2327                                             t)
2328                                            outp))
2329                                     (setq ps (cdr ps)))
2330                                   (list (nreverse outp))))
2331                                outt))
2332                         (setq ts (cdr ts)))
2333                       (list (nreverse outt))))
2334                    outh))
2335             (setq headers (cdr headers)))
2336           (list (nreverse outh))))))))
2337
2338 \f
2339
2340 (defun gnus-summary-mode (&optional group)
2341   "Major mode for reading articles.
2342
2343 All normal editing commands are switched off.
2344 \\<gnus-summary-mode-map>
2345 Each line in this buffer represents one article.  To read an
2346 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2347 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2348 respectively.
2349
2350 You can also post articles and send mail from this buffer.  To
2351 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2352 of an article, type `\\[gnus-summary-reply]'.
2353
2354 There are approx. one gazillion commands you can execute in this
2355 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2356
2357 The following commands are available:
2358
2359 \\{gnus-summary-mode-map}"
2360   (interactive)
2361   (kill-all-local-variables)
2362   (when (gnus-visual-p 'summary-menu 'menu)
2363     (gnus-summary-make-menu-bar)
2364     (gnus-summary-make-tool-bar))
2365   (gnus-summary-make-local-variables)
2366   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2367     (gnus-summary-make-local-variables))
2368   (gnus-make-thread-indent-array)
2369   (gnus-simplify-mode-line)
2370   (setq major-mode 'gnus-summary-mode)
2371   (setq mode-name "Summary")
2372   (make-local-variable 'minor-mode-alist)
2373   (use-local-map gnus-summary-mode-map)
2374   (buffer-disable-undo)
2375   (setq buffer-read-only t)             ;Disable modification
2376   (setq truncate-lines t)
2377   (setq selective-display t)
2378   (setq selective-display-ellipses t)   ;Display `...'
2379   (gnus-summary-set-display-table)
2380   (gnus-set-default-directory)
2381   (setq gnus-newsgroup-name group)
2382   (make-local-variable 'gnus-summary-line-format)
2383   (make-local-variable 'gnus-summary-line-format-spec)
2384   (make-local-variable 'gnus-summary-dummy-line-format)
2385   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2386   (make-local-variable 'gnus-summary-mark-positions)
2387   (make-local-hook 'pre-command-hook)
2388   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2389   (gnus-run-hooks 'gnus-summary-mode-hook)
2390   (turn-on-gnus-mailing-list-mode)
2391   (mm-enable-multibyte-mule4)
2392   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2393   (gnus-update-summary-mark-positions))
2394
2395 (defun gnus-summary-make-local-variables ()
2396   "Make all the local summary buffer variables."
2397   (let (global)
2398     (dolist (local gnus-summary-local-variables)
2399       (if (consp local)
2400           (progn
2401             (if (eq (cdr local) 'global)
2402                 ;; Copy the global value of the variable.
2403                 (setq global (symbol-value (car local)))
2404               ;; Use the value from the list.
2405               (setq global (eval (cdr local))))
2406             (set (make-local-variable (car local)) global))
2407         ;; Simple nil-valued local variable.
2408         (set (make-local-variable local) nil)))))
2409
2410 (defun gnus-summary-clear-local-variables ()
2411   (let ((locals gnus-summary-local-variables))
2412     (while locals
2413       (if (consp (car locals))
2414           (and (vectorp (caar locals))
2415                (set (caar locals) nil))
2416         (and (vectorp (car locals))
2417              (set (car locals) nil)))
2418       (setq locals (cdr locals)))))
2419
2420 ;; Summary data functions.
2421
2422 (defmacro gnus-data-number (data)
2423   `(car ,data))
2424
2425 (defmacro gnus-data-set-number (data number)
2426   `(setcar ,data ,number))
2427
2428 (defmacro gnus-data-mark (data)
2429   `(nth 1 ,data))
2430
2431 (defmacro gnus-data-set-mark (data mark)
2432   `(setcar (nthcdr 1 ,data) ,mark))
2433
2434 (defmacro gnus-data-pos (data)
2435   `(nth 2 ,data))
2436
2437 (defmacro gnus-data-set-pos (data pos)
2438   `(setcar (nthcdr 2 ,data) ,pos))
2439
2440 (defmacro gnus-data-header (data)
2441   `(nth 3 ,data))
2442
2443 (defmacro gnus-data-set-header (data header)
2444   `(setf (nth 3 ,data) ,header))
2445
2446 (defmacro gnus-data-level (data)
2447   `(nth 4 ,data))
2448
2449 (defmacro gnus-data-unread-p (data)
2450   `(= (nth 1 ,data) gnus-unread-mark))
2451
2452 (defmacro gnus-data-read-p (data)
2453   `(/= (nth 1 ,data) gnus-unread-mark))
2454
2455 (defmacro gnus-data-pseudo-p (data)
2456   `(consp (nth 3 ,data)))
2457
2458 (defmacro gnus-data-find (number)
2459   `(assq ,number gnus-newsgroup-data))
2460
2461 (defmacro gnus-data-find-list (number &optional data)
2462   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2463      (memq (assq ,number bdata)
2464            bdata)))
2465
2466 (defmacro gnus-data-make (number mark pos header level)
2467   `(list ,number ,mark ,pos ,header ,level))
2468
2469 (defun gnus-data-enter (after-article number mark pos header level offset)
2470   (let ((data (gnus-data-find-list after-article)))
2471     (unless data
2472       (error "No such article: %d" after-article))
2473     (setcdr data (cons (gnus-data-make number mark pos header level)
2474                        (cdr data)))
2475     (setq gnus-newsgroup-data-reverse nil)
2476     (gnus-data-update-list (cddr data) offset)))
2477
2478 (defun gnus-data-enter-list (after-article list &optional offset)
2479   (when list
2480     (let ((data (and after-article (gnus-data-find-list after-article)))
2481           (ilist list))
2482       (if (not (or data
2483                    after-article))
2484           (let ((odata gnus-newsgroup-data))
2485             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2486             (when offset
2487               (gnus-data-update-list odata offset)))
2488       ;; Find the last element in the list to be spliced into the main
2489         ;; list.
2490         (while (cdr list)
2491           (setq list (cdr list)))
2492         (if (not data)
2493             (progn
2494               (setcdr list gnus-newsgroup-data)
2495               (setq gnus-newsgroup-data ilist)
2496               (when offset
2497                 (gnus-data-update-list (cdr list) offset)))
2498           (setcdr list (cdr data))
2499           (setcdr data ilist)
2500           (when offset
2501             (gnus-data-update-list (cdr list) offset))))
2502       (setq gnus-newsgroup-data-reverse nil))))
2503
2504 (defun gnus-data-remove (article &optional offset)
2505   (let ((data gnus-newsgroup-data))
2506     (if (= (gnus-data-number (car data)) article)
2507         (progn
2508           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2509                 gnus-newsgroup-data-reverse nil)
2510           (when offset
2511             (gnus-data-update-list gnus-newsgroup-data offset)))
2512       (while (cdr data)
2513         (when (= (gnus-data-number (cadr data)) article)
2514           (setcdr data (cddr data))
2515           (when offset
2516             (gnus-data-update-list (cdr data) offset))
2517           (setq data nil
2518                 gnus-newsgroup-data-reverse nil))
2519         (setq data (cdr data))))))
2520
2521 (defmacro gnus-data-list (backward)
2522   `(if ,backward
2523        (or gnus-newsgroup-data-reverse
2524            (setq gnus-newsgroup-data-reverse
2525                  (reverse gnus-newsgroup-data)))
2526      gnus-newsgroup-data))
2527
2528 (defun gnus-data-update-list (data offset)
2529   "Add OFFSET to the POS of all data entries in DATA."
2530   (setq gnus-newsgroup-data-reverse nil)
2531   (while data
2532     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2533     (setq data (cdr data))))
2534
2535 (defun gnus-summary-article-pseudo-p (article)
2536   "Say whether this article is a pseudo article or not."
2537   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2538
2539 (defmacro gnus-summary-article-sparse-p (article)
2540   "Say whether this article is a sparse article or not."
2541   `(memq ,article gnus-newsgroup-sparse))
2542
2543 (defmacro gnus-summary-article-ancient-p (article)
2544   "Say whether this article is a sparse article or not."
2545   `(memq ,article gnus-newsgroup-ancient))
2546
2547 (defun gnus-article-parent-p (number)
2548   "Say whether this article is a parent or not."
2549   (let ((data (gnus-data-find-list number)))
2550     (and (cdr data)              ; There has to be an article after...
2551          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2552             (gnus-data-level (nth 1 data))))))
2553
2554 (defun gnus-article-children (number)
2555   "Return a list of all children to NUMBER."
2556   (let* ((data (gnus-data-find-list number))
2557          (level (gnus-data-level (car data)))
2558          children)
2559     (setq data (cdr data))
2560     (while (and data
2561                 (= (gnus-data-level (car data)) (1+ level)))
2562       (push (gnus-data-number (car data)) children)
2563       (setq data (cdr data)))
2564     children))
2565
2566 (defmacro gnus-summary-skip-intangible ()
2567   "If the current article is intangible, then jump to a different article."
2568   '(let ((to (get-text-property (point) 'gnus-intangible)))
2569      (and to (gnus-summary-goto-subject to))))
2570
2571 (defmacro gnus-summary-article-intangible-p ()
2572   "Say whether this article is intangible or not."
2573   '(get-text-property (point) 'gnus-intangible))
2574
2575 (defun gnus-article-read-p (article)
2576   "Say whether ARTICLE is read or not."
2577   (not (or (memq article gnus-newsgroup-marked)
2578            (memq article gnus-newsgroup-unreads)
2579            (memq article gnus-newsgroup-unselected)
2580            (memq article gnus-newsgroup-dormant))))
2581
2582 ;; Some summary mode macros.
2583
2584 (defmacro gnus-summary-article-number ()
2585   "The article number of the article on the current line.
2586 If there isn's an article number here, then we return the current
2587 article number."
2588   '(progn
2589      (gnus-summary-skip-intangible)
2590      (or (get-text-property (point) 'gnus-number)
2591          (gnus-summary-last-subject))))
2592
2593 (defmacro gnus-summary-article-header (&optional number)
2594   "Return the header of article NUMBER."
2595   `(gnus-data-header (gnus-data-find
2596                       ,(or number '(gnus-summary-article-number)))))
2597
2598 (defmacro gnus-summary-thread-level (&optional number)
2599   "Return the level of thread that starts with article NUMBER."
2600   `(if (and (eq gnus-summary-make-false-root 'dummy)
2601             (get-text-property (point) 'gnus-intangible))
2602        0
2603      (gnus-data-level (gnus-data-find
2604                        ,(or number '(gnus-summary-article-number))))))
2605
2606 (defmacro gnus-summary-article-mark (&optional number)
2607   "Return the mark of article NUMBER."
2608   `(gnus-data-mark (gnus-data-find
2609                     ,(or number '(gnus-summary-article-number)))))
2610
2611 (defmacro gnus-summary-article-pos (&optional number)
2612   "Return the position of the line of article NUMBER."
2613   `(gnus-data-pos (gnus-data-find
2614                    ,(or number '(gnus-summary-article-number)))))
2615
2616 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2617 (defmacro gnus-summary-article-subject (&optional number)
2618   "Return current subject string or nil if nothing."
2619   `(let ((headers
2620           ,(if number
2621                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2622              '(gnus-data-header (assq (gnus-summary-article-number)
2623                                       gnus-newsgroup-data)))))
2624      (and headers
2625           (vectorp headers)
2626           (mail-header-subject headers))))
2627
2628 (defmacro gnus-summary-article-score (&optional number)
2629   "Return current article score."
2630   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2631                   gnus-newsgroup-scored))
2632        gnus-summary-default-score 0))
2633
2634 (defun gnus-summary-article-children (&optional number)
2635   "Return a list of article numbers that are children of article NUMBER."
2636   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2637          (level (gnus-data-level (car data)))
2638          l children)
2639     (while (and (setq data (cdr data))
2640                 (> (setq l (gnus-data-level (car data))) level))
2641       (and (= (1+ level) l)
2642            (push (gnus-data-number (car data))
2643                  children)))
2644     (nreverse children)))
2645
2646 (defun gnus-summary-article-parent (&optional number)
2647   "Return the article number of the parent of article NUMBER."
2648   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2649                                     (gnus-data-list t)))
2650          (level (gnus-data-level (car data))))
2651     (if (zerop level)
2652         ()                              ; This is a root.
2653       ;; We search until we find an article with a level less than
2654       ;; this one.  That function has to be the parent.
2655       (while (and (setq data (cdr data))
2656                   (not (< (gnus-data-level (car data)) level))))
2657       (and data (gnus-data-number (car data))))))
2658
2659 (defun gnus-unread-mark-p (mark)
2660   "Say whether MARK is the unread mark."
2661   (= mark gnus-unread-mark))
2662
2663 (defun gnus-read-mark-p (mark)
2664   "Say whether MARK is one of the marks that mark as read.
2665 This is all marks except unread, ticked, dormant, and expirable."
2666   (not (or (= mark gnus-unread-mark)
2667            (= mark gnus-ticked-mark)
2668            (= mark gnus-dormant-mark)
2669            (= mark gnus-expirable-mark))))
2670
2671 (defmacro gnus-article-mark (number)
2672   "Return the MARK of article NUMBER.
2673 This macro should only be used when computing the mark the \"first\"
2674 time; i.e., when generating the summary lines.  After that,
2675 `gnus-summary-article-mark' should be used to examine the
2676 marks of articles."
2677   `(cond
2678     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2679     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2680     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2681     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2682     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2683     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2684     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2685     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2686            gnus-ancient-mark))))
2687
2688 ;; Saving hidden threads.
2689
2690 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2691 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2692
2693 (defmacro gnus-save-hidden-threads (&rest forms)
2694   "Save hidden threads, eval FORMS, and restore the hidden threads."
2695   (let ((config (make-symbol "config")))
2696     `(let ((,config (gnus-hidden-threads-configuration)))
2697        (unwind-protect
2698            (save-excursion
2699              ,@forms)
2700          (gnus-restore-hidden-threads-configuration ,config)))))
2701
2702 (defun gnus-data-compute-positions ()
2703   "Compute the positions of all articles."
2704   (setq gnus-newsgroup-data-reverse nil)
2705   (let ((data gnus-newsgroup-data))
2706     (save-excursion
2707       (gnus-save-hidden-threads
2708         (gnus-summary-show-all-threads)
2709         (goto-char (point-min))
2710         (while data
2711           (while (get-text-property (point) 'gnus-intangible)
2712             (forward-line 1))
2713           (gnus-data-set-pos (car data) (+ (point) 3))
2714           (setq data (cdr data))
2715           (forward-line 1))))))
2716
2717 (defun gnus-hidden-threads-configuration ()
2718   "Return the current hidden threads configuration."
2719   (save-excursion
2720     (let (config)
2721       (goto-char (point-min))
2722       (while (search-forward "\r" nil t)
2723         (push (1- (point)) config))
2724       config)))
2725
2726 (defun gnus-restore-hidden-threads-configuration (config)
2727   "Restore hidden threads configuration from CONFIG."
2728   (save-excursion
2729     (let (point buffer-read-only)
2730       (while (setq point (pop config))
2731         (when (and (< point (point-max))
2732                    (goto-char point)
2733                    (eq (char-after) ?\n))
2734           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2735
2736 ;; Various summary mode internalish functions.
2737
2738 (defun gnus-mouse-pick-article (e)
2739   (interactive "e")
2740   (mouse-set-point e)
2741   (gnus-summary-next-page nil t))
2742
2743 (defun gnus-summary-set-display-table ()
2744   "Change the display table.
2745 Odd characters have a tendency to mess
2746 up nicely formatted displays - we make all possible glyphs
2747 display only a single character."
2748
2749   ;; We start from the standard display table, if any.
2750   (let ((table (or (copy-sequence standard-display-table)
2751                    (make-display-table)))
2752         (i 32))
2753     ;; Nix out all the control chars...
2754     (while (>= (setq i (1- i)) 0)
2755       (aset table i [??]))
2756    ;; ... but not newline and cr, of course.  (cr is necessary for the
2757     ;; selective display).
2758     (aset table ?\n nil)
2759     (aset table ?\r nil)
2760     ;; We keep TAB as well.
2761     (aset table ?\t nil)
2762     ;; We nix out any glyphs over 126 that are not set already.
2763     (let ((i 256))
2764       (while (>= (setq i (1- i)) 127)
2765         ;; Only modify if the entry is nil.
2766         (unless (aref table i)
2767           (aset table i [??]))))
2768     (setq buffer-display-table table)))
2769
2770 (defun gnus-summary-set-article-display-arrow (pos)
2771   "Update the overlay arrow to point to line at position POS."
2772   (when (and gnus-summary-display-arrow
2773              (boundp 'overlay-arrow-position)
2774              (boundp 'overlay-arrow-string))
2775     (save-excursion
2776       (goto-char pos)
2777       (beginning-of-line)
2778       (unless overlay-arrow-position
2779         (setq overlay-arrow-position (make-marker)))
2780       (setq overlay-arrow-string "=>"
2781             overlay-arrow-position (set-marker overlay-arrow-position
2782                                                (point)
2783                                                (current-buffer))))))
2784
2785 (defun gnus-summary-buffer-name (group)
2786   "Return the summary buffer name of GROUP."
2787   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2788
2789 (defun gnus-summary-setup-buffer (group)
2790   "Initialize summary buffer."
2791   (let ((buffer (gnus-summary-buffer-name group)))
2792     (if (get-buffer buffer)
2793         (progn
2794           (set-buffer buffer)
2795           (setq gnus-summary-buffer (current-buffer))
2796           (not gnus-newsgroup-prepared))
2797       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2798       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2799       (gnus-summary-mode group)
2800       (when gnus-carpal
2801         (gnus-carpal-setup-buffer 'summary))
2802       (unless gnus-single-article-buffer
2803         (make-local-variable 'gnus-article-buffer)
2804         (make-local-variable 'gnus-article-current)
2805         (make-local-variable 'gnus-original-article-buffer))
2806       (setq gnus-newsgroup-name group)
2807       ;; Set any local variables in the group parameters.
2808       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2809       t)))
2810
2811 (defun gnus-set-global-variables ()
2812   "Set the global equivalents of the buffer-local variables.
2813 They are set to the latest values they had.  These reflect the summary
2814 buffer that was in action when the last article was fetched."
2815   (when (eq major-mode 'gnus-summary-mode)
2816     (setq gnus-summary-buffer (current-buffer))
2817     (let ((name gnus-newsgroup-name)
2818           (marked gnus-newsgroup-marked)
2819           (unread gnus-newsgroup-unreads)
2820           (headers gnus-current-headers)
2821           (data gnus-newsgroup-data)
2822           (summary gnus-summary-buffer)
2823           (article-buffer gnus-article-buffer)
2824           (original gnus-original-article-buffer)
2825           (gac gnus-article-current)
2826           (reffed gnus-reffed-article-number)
2827           (score-file gnus-current-score-file)
2828           (default-charset gnus-newsgroup-charset)
2829           vlist)
2830       (let ((locals gnus-newsgroup-variables))
2831         (while locals
2832           (if (consp (car locals))
2833               (push (eval (caar locals)) vlist)
2834             (push (eval (car locals)) vlist))
2835           (setq locals (cdr locals)))
2836         (setq vlist (nreverse vlist)))
2837       (save-excursion
2838         (set-buffer gnus-group-buffer)
2839         (setq gnus-newsgroup-name name
2840               gnus-newsgroup-marked marked
2841               gnus-newsgroup-unreads unread
2842               gnus-current-headers headers
2843               gnus-newsgroup-data data
2844               gnus-article-current gac
2845               gnus-summary-buffer summary
2846               gnus-article-buffer article-buffer
2847               gnus-original-article-buffer original
2848               gnus-reffed-article-number reffed
2849               gnus-current-score-file score-file
2850               gnus-newsgroup-charset default-charset)
2851         (let ((locals gnus-newsgroup-variables))
2852           (while locals
2853             (if (consp (car locals))
2854                 (set (caar locals) (pop vlist))
2855               (set (car locals) (pop vlist)))
2856             (setq locals (cdr locals))))
2857         ;; The article buffer also has local variables.
2858         (when (gnus-buffer-live-p gnus-article-buffer)
2859           (set-buffer gnus-article-buffer)
2860           (setq gnus-summary-buffer summary))))))
2861
2862 (defun gnus-summary-article-unread-p (article)
2863   "Say whether ARTICLE is unread or not."
2864   (memq article gnus-newsgroup-unreads))
2865
2866 (defun gnus-summary-first-article-p (&optional article)
2867   "Return whether ARTICLE is the first article in the buffer."
2868   (if (not (setq article (or article (gnus-summary-article-number))))
2869       nil
2870     (eq article (caar gnus-newsgroup-data))))
2871
2872 (defun gnus-summary-last-article-p (&optional article)
2873   "Return whether ARTICLE is the last article in the buffer."
2874   (if (not (setq article (or article (gnus-summary-article-number))))
2875       ;; All non-existent numbers are the last article.  :-)
2876       t
2877     (not (cdr (gnus-data-find-list article)))))
2878
2879 (defun gnus-make-thread-indent-array ()
2880   (let ((n 200))
2881     (unless (and gnus-thread-indent-array
2882                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2883       (setq gnus-thread-indent-array (make-vector 201 "")
2884             gnus-thread-indent-array-level gnus-thread-indent-level)
2885       (while (>= n 0)
2886         (aset gnus-thread-indent-array n
2887               (make-string (* n gnus-thread-indent-level) ? ))
2888         (setq n (1- n))))))
2889
2890 (defun gnus-update-summary-mark-positions ()
2891   "Compute where the summary marks are to go."
2892   (save-excursion
2893     (when (gnus-buffer-exists-p gnus-summary-buffer)
2894       (set-buffer gnus-summary-buffer))
2895     (let ((gnus-replied-mark 129)
2896           (gnus-score-below-mark 130)
2897           (gnus-score-over-mark 130)
2898           (gnus-download-mark 131)
2899           (spec gnus-summary-line-format-spec)
2900           gnus-visual pos)
2901       (save-excursion
2902         (gnus-set-work-buffer)
2903         (let ((gnus-summary-line-format-spec spec)
2904               (gnus-newsgroup-downloadable '((0 . t))))
2905           (gnus-summary-insert-line
2906            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2907            0 nil 128 t nil "" nil 1)
2908           (goto-char (point-min))
2909           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2910                                              (- (point) 2)))))
2911           (goto-char (point-min))
2912           (push (cons 'replied (and (search-forward "\201" nil t)
2913                                     (- (point) 2)))
2914                 pos)
2915           (goto-char (point-min))
2916           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2917                 pos)
2918           (goto-char (point-min))
2919           (push (cons 'download
2920                       (and (search-forward "\203" nil t) (- (point) 2)))
2921                 pos)))
2922       (setq gnus-summary-mark-positions pos))))
2923
2924 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2925   "Insert a dummy root in the summary buffer."
2926   (beginning-of-line)
2927   (gnus-add-text-properties
2928    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2929    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2930
2931 (defun gnus-summary-extract-address-component (from)
2932   (or (car (funcall gnus-extract-address-components from))
2933       from))
2934
2935 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2936   (let ((mail-parse-charset gnus-newsgroup-charset)
2937         ; Is it really necessary to do this next part for each summary line?
2938         ; Luckily, doesn't seem to slow things down much.
2939         (mail-parse-ignored-charsets
2940          (save-excursion (set-buffer gnus-summary-buffer)
2941                          gnus-newsgroup-ignored-charsets)))
2942     (or
2943      (and gnus-ignored-from-addresses
2944           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2945           (let ((extra-headers (mail-header-extra header))
2946                 to
2947                 newsgroups)
2948             (cond
2949              ((setq to (cdr (assq 'To extra-headers)))
2950               (concat "-> "
2951                       (gnus-summary-extract-address-component
2952                        (funcall gnus-decode-encoded-word-function to))))
2953              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2954               (concat "=> " newsgroups)))))
2955      (gnus-summary-extract-address-component gnus-tmp-from))))
2956
2957 (defun gnus-summary-insert-line (gnus-tmp-header
2958                                  gnus-tmp-level gnus-tmp-current
2959                                  gnus-tmp-unread gnus-tmp-replied
2960                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2961                                  &optional gnus-tmp-dummy gnus-tmp-score
2962                                  gnus-tmp-process)
2963   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2964          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2965          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2966          (gnus-tmp-score-char
2967           (if (or (null gnus-summary-default-score)
2968                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2969                       gnus-summary-zcore-fuzz))
2970               ?                         ;Whitespace
2971             (if (< gnus-tmp-score gnus-summary-default-score)
2972                 gnus-score-below-mark gnus-score-over-mark)))
2973          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2974          (gnus-tmp-replied
2975           (cond (gnus-tmp-process gnus-process-mark)
2976                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2977                  gnus-cached-mark)
2978                 (gnus-tmp-replied gnus-replied-mark)
2979                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2980                  gnus-forwarded-mark)
2981                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2982                  gnus-saved-mark)
2983                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2984                  gnus-recent-mark)
2985                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2986                  gnus-unseen-mark)
2987                 (t gnus-no-mark)))
2988          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2989          (gnus-tmp-name
2990           (cond
2991            ((string-match "<[^>]+> *$" gnus-tmp-from)
2992             (let ((beg (match-beginning 0)))
2993               (or (and (string-match "^\".+\"" gnus-tmp-from)
2994                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2995                   (substring gnus-tmp-from 0 beg))))
2996            ((string-match "(.+)" gnus-tmp-from)
2997             (substring gnus-tmp-from
2998                        (1+ (match-beginning 0)) (1- (match-end 0))))
2999            (t gnus-tmp-from)))
3000          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3001          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3002          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3003          (buffer-read-only nil))
3004     (when (string= gnus-tmp-name "")
3005       (setq gnus-tmp-name gnus-tmp-from))
3006     (unless (numberp gnus-tmp-lines)
3007       (setq gnus-tmp-lines -1))
3008     (if (= gnus-tmp-lines -1)
3009         (setq gnus-tmp-lines "?")
3010       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3011     (gnus-put-text-property
3012      (point)
3013      (progn (eval gnus-summary-line-format-spec) (point))
3014      'gnus-number gnus-tmp-number)
3015     (when (gnus-visual-p 'summary-highlight 'highlight)
3016       (forward-line -1)
3017       (gnus-run-hooks 'gnus-summary-update-hook)
3018       (forward-line 1))))
3019
3020 (defun gnus-summary-update-line (&optional dont-update)
3021   "Update summary line after change."
3022   (when (and gnus-summary-default-score
3023              (not gnus-summary-inhibit-highlight))
3024     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3025            (article (gnus-summary-article-number))
3026            (score (gnus-summary-article-score article)))
3027       (unless dont-update
3028         (if (and gnus-summary-mark-below
3029                  (< (gnus-summary-article-score)
3030                     gnus-summary-mark-below))
3031             ;; This article has a low score, so we mark it as read.
3032             (when (memq article gnus-newsgroup-unreads)
3033               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3034           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3035             ;; This article was previously marked as read on account
3036             ;; of a low score, but now it has risen, so we mark it as
3037             ;; unread.
3038             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3039         (gnus-summary-update-mark
3040          (if (or (null gnus-summary-default-score)
3041                  (<= (abs (- score gnus-summary-default-score))
3042                      gnus-summary-zcore-fuzz))
3043              ?                          ;Whitespace
3044            (if (< score gnus-summary-default-score)
3045                gnus-score-below-mark gnus-score-over-mark))
3046          'score))
3047       ;; Do visual highlighting.
3048       (when (gnus-visual-p 'summary-highlight 'highlight)
3049         (gnus-run-hooks 'gnus-summary-update-hook)))))
3050
3051 (defvar gnus-tmp-new-adopts nil)
3052
3053 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3054   "Return the number of articles in THREAD.
3055 This may be 0 in some cases -- if none of the articles in
3056 the thread are to be displayed."
3057   (let* ((number
3058          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3059           (cond
3060            ((not (listp thread))
3061             1)
3062            ((and (consp thread) (cdr thread))
3063             (apply
3064              '+ 1 (mapcar
3065                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3066            ((null thread)
3067             1)
3068            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3069             1)
3070            (t 0))))
3071     (when (and level (zerop level) gnus-tmp-new-adopts)
3072       (incf number
3073             (apply '+ (mapcar
3074                        'gnus-summary-number-of-articles-in-thread
3075                        gnus-tmp-new-adopts))))
3076     (if char
3077         (if (> number 1) gnus-not-empty-thread-mark
3078           gnus-empty-thread-mark)
3079       number)))
3080
3081 (defun gnus-summary-set-local-parameters (group)
3082   "Go through the local params of GROUP and set all variable specs in that list."
3083   (let ((params (gnus-group-find-parameter group))
3084         (vars '(quit-config))           ; Ignore quit-config.
3085         elem)
3086     (while params
3087       (setq elem (car params)
3088             params (cdr params))
3089       (and (consp elem)                 ; Has to be a cons.
3090            (consp (cdr elem))           ; The cdr has to be a list.
3091            (symbolp (car elem))         ; Has to be a symbol in there.
3092            (not (memq (car elem) vars))
3093            (ignore-errors               ; So we set it.
3094              (push (car elem) vars)
3095              (make-local-variable (car elem))
3096              (set (car elem) (eval (nth 1 elem))))))))
3097
3098 (defun gnus-summary-read-group (group &optional show-all no-article
3099                                       kill-buffer no-display backward
3100                                       select-articles)
3101   "Start reading news in newsgroup GROUP.
3102 If SHOW-ALL is non-nil, already read articles are also listed.
3103 If NO-ARTICLE is non-nil, no article is selected initially.
3104 If NO-DISPLAY, don't generate a summary buffer."
3105   (let (result)
3106     (while (and group
3107                 (null (setq result
3108                             (let ((gnus-auto-select-next nil))
3109                               (or (gnus-summary-read-group-1
3110                                    group show-all no-article
3111                                    kill-buffer no-display
3112                                    select-articles)
3113                                   (setq show-all nil
3114                                         select-articles nil)))))
3115                 (eq gnus-auto-select-next 'quietly))
3116       (set-buffer gnus-group-buffer)
3117       ;; The entry function called above goes to the next
3118       ;; group automatically, so we go two groups back
3119       ;; if we are searching for the previous group.
3120       (when backward
3121         (gnus-group-prev-unread-group 2))
3122       (if (not (equal group (gnus-group-group-name)))
3123           (setq group (gnus-group-group-name))
3124         (setq group nil)))
3125     result))
3126
3127 (defun gnus-summary-read-group-1 (group show-all no-article
3128                                         kill-buffer no-display
3129                                         &optional select-articles)
3130   ;; Killed foreign groups can't be entered.
3131   ;;  (when (and (not (gnus-group-native-p group))
3132   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3133   ;;    (error "Dead non-native groups can't be entered"))
3134   (gnus-message 5 "Retrieving newsgroup: %s..."
3135                 (gnus-group-decoded-name group))
3136   (let* ((new-group (gnus-summary-setup-buffer group))
3137          (quit-config (gnus-group-quit-config group))
3138          (did-select (and new-group (gnus-select-newsgroup
3139                                      group show-all select-articles))))
3140     (cond
3141      ;; This summary buffer exists already, so we just select it.
3142      ((not new-group)
3143       (gnus-set-global-variables)
3144       (when kill-buffer
3145         (gnus-kill-or-deaden-summary kill-buffer))
3146       (gnus-configure-windows 'summary 'force)
3147       (gnus-set-mode-line 'summary)
3148       (gnus-summary-position-point)
3149       (message "")
3150       t)
3151      ;; We couldn't select this group.
3152      ((null did-select)
3153       (when (and (eq major-mode 'gnus-summary-mode)
3154                  (not (equal (current-buffer) kill-buffer)))
3155         (kill-buffer (current-buffer))
3156         (if (not quit-config)
3157             (progn
3158               ;; Update the info -- marks might need to be removed,
3159               ;; for instance.
3160               (gnus-summary-update-info)
3161               (set-buffer gnus-group-buffer)
3162               (gnus-group-jump-to-group group)
3163               (gnus-group-next-unread-group 1))
3164           (gnus-handle-ephemeral-exit quit-config)))
3165       (let ((grpinfo (gnus-get-info group)))
3166         (if (null (gnus-info-read grpinfo))
3167             (gnus-message 3 "Group %s contains no messages"
3168                           (gnus-group-decoded-name group))
3169           (gnus-message 3 "Can't select group")))
3170       nil)
3171      ;; The user did a `C-g' while prompting for number of articles,
3172      ;; so we exit this group.
3173      ((eq did-select 'quit)
3174       (and (eq major-mode 'gnus-summary-mode)
3175            (not (equal (current-buffer) kill-buffer))
3176            (kill-buffer (current-buffer)))
3177       (when kill-buffer
3178         (gnus-kill-or-deaden-summary kill-buffer))
3179       (if (not quit-config)
3180           (progn
3181             (set-buffer gnus-group-buffer)
3182             (gnus-group-jump-to-group group)
3183             (gnus-group-next-unread-group 1)
3184             (gnus-configure-windows 'group 'force))
3185         (gnus-handle-ephemeral-exit quit-config))
3186       ;; Finally signal the quit.
3187       (signal 'quit nil))
3188      ;; The group was successfully selected.
3189      (t
3190       (gnus-set-global-variables)
3191       ;; Save the active value in effect when the group was entered.
3192       (setq gnus-newsgroup-active
3193             (gnus-copy-sequence
3194              (gnus-active gnus-newsgroup-name)))
3195       ;; You can change the summary buffer in some way with this hook.
3196       (gnus-run-hooks 'gnus-select-group-hook)
3197       (gnus-update-format-specifications
3198        nil 'summary 'summary-mode 'summary-dummy)
3199       (gnus-update-summary-mark-positions)
3200       ;; Do score processing.
3201       (when gnus-use-scoring
3202         (gnus-possibly-score-headers))
3203       ;; Check whether to fill in the gaps in the threads.
3204       (when gnus-build-sparse-threads
3205         (gnus-build-sparse-threads))
3206       ;; Find the initial limit.
3207       (if gnus-show-threads
3208           (if show-all
3209               (let ((gnus-newsgroup-dormant nil))
3210                 (gnus-summary-initial-limit show-all))
3211             (gnus-summary-initial-limit show-all))
3212         ;; When untreaded, all articles are always shown.
3213         (setq gnus-newsgroup-limit
3214               (mapcar
3215                (lambda (header) (mail-header-number header))
3216                gnus-newsgroup-headers)))
3217       ;; Generate the summary buffer.
3218       (unless no-display
3219         (gnus-summary-prepare))
3220       (when gnus-use-trees
3221         (gnus-tree-open group)
3222         (setq gnus-summary-highlight-line-function
3223               'gnus-tree-highlight-article))
3224       ;; If the summary buffer is empty, but there are some low-scored
3225       ;; articles or some excluded dormants, we include these in the
3226       ;; buffer.
3227       (when (and (zerop (buffer-size))
3228                  (not no-display))
3229         (cond (gnus-newsgroup-dormant
3230                (gnus-summary-limit-include-dormant))
3231               ((and gnus-newsgroup-scored show-all)
3232                (gnus-summary-limit-include-expunged t))))
3233       ;; Function `gnus-apply-kill-file' must be called in this hook.
3234       (gnus-run-hooks 'gnus-apply-kill-hook)
3235       (if (and (zerop (buffer-size))
3236                (not no-display))
3237           (progn
3238             ;; This newsgroup is empty.
3239             (gnus-summary-catchup-and-exit nil t)
3240             (gnus-message 6 "No unread news")
3241             (when kill-buffer
3242               (gnus-kill-or-deaden-summary kill-buffer))
3243             ;; Return nil from this function.
3244             nil)
3245         ;; Hide conversation thread subtrees.  We cannot do this in
3246         ;; gnus-summary-prepare-hook since kill processing may not
3247         ;; work with hidden articles.
3248         (and gnus-show-threads
3249              gnus-thread-hide-subtree
3250              (gnus-summary-hide-all-threads))
3251         (when kill-buffer
3252           (gnus-kill-or-deaden-summary kill-buffer))
3253         ;; Show first unread article if requested.
3254         (if (and (not no-article)
3255                  (not no-display)
3256                  gnus-newsgroup-unreads
3257                  gnus-auto-select-first)
3258             (progn
3259               (gnus-configure-windows 'summary)
3260               (cond
3261                ((eq gnus-auto-select-first 'best)
3262                 (gnus-summary-best-unread-article))
3263                ((eq gnus-auto-select-first t)
3264                 (gnus-summary-first-unread-article))
3265                ((gnus-functionp gnus-auto-select-first)
3266                 (funcall gnus-auto-select-first))))
3267           ;; Don't select any articles, just move point to the first
3268           ;; article in the group.
3269           (goto-char (point-min))
3270           (gnus-summary-position-point)
3271           (gnus-configure-windows 'summary 'force)
3272           (gnus-set-mode-line 'summary))
3273         (when (get-buffer-window gnus-group-buffer t)
3274           ;; Gotta use windows, because recenter does weird stuff if
3275           ;; the current buffer ain't the displayed window.
3276           (let ((owin (selected-window)))
3277             (select-window (get-buffer-window gnus-group-buffer t))
3278             (when (gnus-group-goto-group group)
3279               (recenter))
3280             (select-window owin)))
3281         ;; Mark this buffer as "prepared".
3282         (setq gnus-newsgroup-prepared t)
3283         (gnus-run-hooks 'gnus-summary-prepared-hook)
3284         t)))))
3285
3286 (defun gnus-summary-prepare ()
3287   "Generate the summary buffer."
3288   (interactive)
3289   (let ((buffer-read-only nil))
3290     (erase-buffer)
3291     (setq gnus-newsgroup-data nil
3292           gnus-newsgroup-data-reverse nil)
3293     (gnus-run-hooks 'gnus-summary-generate-hook)
3294     ;; Generate the buffer, either with threads or without.
3295     (when gnus-newsgroup-headers
3296       (gnus-summary-prepare-threads
3297        (if gnus-show-threads
3298            (gnus-sort-gathered-threads
3299             (funcall gnus-summary-thread-gathering-function
3300                      (gnus-sort-threads
3301                       (gnus-cut-threads (gnus-make-threads)))))
3302          ;; Unthreaded display.
3303          (gnus-sort-articles gnus-newsgroup-headers))))
3304     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3305     ;; Call hooks for modifying summary buffer.
3306     (goto-char (point-min))
3307     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3308
3309 (defsubst gnus-general-simplify-subject (subject)
3310   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3311   (setq subject
3312         (cond
3313          ;; Truncate the subject.
3314          (gnus-simplify-subject-functions
3315           (gnus-map-function gnus-simplify-subject-functions subject))
3316          ((numberp gnus-summary-gather-subject-limit)
3317           (setq subject (gnus-simplify-subject-re subject))
3318           (if (> (length subject) gnus-summary-gather-subject-limit)
3319               (substring subject 0 gnus-summary-gather-subject-limit)
3320             subject))
3321          ;; Fuzzily simplify it.
3322          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3323           (gnus-simplify-subject-fuzzy subject))
3324          ;; Just remove the leading "Re:".
3325          (t
3326           (gnus-simplify-subject-re subject))))
3327
3328   (if (and gnus-summary-gather-exclude-subject
3329            (string-match gnus-summary-gather-exclude-subject subject))
3330       nil                         ; This article shouldn't be gathered
3331     subject))
3332
3333 (defun gnus-summary-simplify-subject-query ()
3334   "Query where the respool algorithm would put this article."
3335   (interactive)
3336   (gnus-summary-select-article)
3337   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3338
3339 (defun gnus-gather-threads-by-subject (threads)
3340   "Gather threads by looking at Subject headers."
3341   (if (not gnus-summary-make-false-root)
3342       threads
3343     (let ((hashtb (gnus-make-hashtable 1024))
3344           (prev threads)
3345           (result threads)
3346           subject hthread whole-subject)
3347       (while threads
3348         (setq subject (gnus-general-simplify-subject
3349                        (setq whole-subject (mail-header-subject
3350                                             (caar threads)))))
3351         (when subject
3352           (if (setq hthread (gnus-gethash subject hashtb))
3353               (progn
3354                 ;; We enter a dummy root into the thread, if we
3355                 ;; haven't done that already.
3356                 (unless (stringp (caar hthread))
3357                   (setcar hthread (list whole-subject (car hthread))))
3358                 ;; We add this new gathered thread to this gathered
3359                 ;; thread.
3360                 (setcdr (car hthread)
3361                         (nconc (cdar hthread) (list (car threads))))
3362                 ;; Remove it from the list of threads.
3363                 (setcdr prev (cdr threads))
3364                 (setq threads prev))
3365             ;; Enter this thread into the hash table.
3366             (gnus-sethash subject threads hashtb)))
3367         (setq prev threads)
3368         (setq threads (cdr threads)))
3369       result)))
3370
3371 (defun gnus-gather-threads-by-references (threads)
3372   "Gather threads by looking at References headers."
3373   (let ((idhashtb (gnus-make-hashtable 1024))
3374         (thhashtb (gnus-make-hashtable 1024))
3375         (prev threads)
3376         (result threads)
3377         ids references id gthread gid entered ref)
3378     (while threads
3379       (when (setq references (mail-header-references (caar threads)))
3380         (setq id (mail-header-id (caar threads))
3381               ids (gnus-split-references references)
3382               entered nil)
3383         (while (setq ref (pop ids))
3384           (setq ids (delete ref ids))
3385           (if (not (setq gid (gnus-gethash ref idhashtb)))
3386               (progn
3387                 (gnus-sethash ref id idhashtb)
3388                 (gnus-sethash id threads thhashtb))
3389             (setq gthread (gnus-gethash gid thhashtb))
3390             (unless entered
3391               ;; We enter a dummy root into the thread, if we
3392               ;; haven't done that already.
3393               (unless (stringp (caar gthread))
3394                 (setcar gthread (list (mail-header-subject (caar gthread))
3395                                       (car gthread))))
3396               ;; We add this new gathered thread to this gathered
3397               ;; thread.
3398               (setcdr (car gthread)
3399                       (nconc (cdar gthread) (list (car threads)))))
3400             ;; Add it into the thread hash table.
3401             (gnus-sethash id gthread thhashtb)
3402             (setq entered t)
3403             ;; Remove it from the list of threads.
3404             (setcdr prev (cdr threads))
3405             (setq threads prev))))
3406       (setq prev threads)
3407       (setq threads (cdr threads)))
3408     result))
3409
3410 (defun gnus-sort-gathered-threads (threads)
3411   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3412   (let ((result threads))
3413     (while threads
3414       (when (stringp (caar threads))
3415         (setcdr (car threads)
3416                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3417       (setq threads (cdr threads)))
3418     result))
3419
3420 (defun gnus-thread-loop-p (root thread)
3421   "Say whether ROOT is in THREAD."
3422   (let ((stack (list thread))
3423         (infloop 0)
3424         th)
3425     (while (setq thread (pop stack))
3426       (setq th (cdr thread))
3427       (while (and th
3428                   (not (eq (caar th) root)))
3429         (pop th))
3430       (if th
3431           ;; We have found a loop.
3432           (let (ref-dep)
3433             (setcdr thread (delq (car th) (cdr thread)))
3434             (if (boundp (setq ref-dep (intern "none"
3435                                               gnus-newsgroup-dependencies)))
3436                 (setcdr (symbol-value ref-dep)
3437                         (nconc (cdr (symbol-value ref-dep))
3438                                (list (car th))))
3439               (set ref-dep (list nil (car th))))
3440             (setq infloop 1
3441                   stack nil))
3442         ;; Push all the subthreads onto the stack.
3443         (push (cdr thread) stack)))
3444     infloop))
3445
3446 (defun gnus-make-threads ()
3447   "Go through the dependency hashtb and find the roots.  Return all threads."
3448   (let (threads)
3449     (while (catch 'infloop
3450              (mapatoms
3451               (lambda (refs)
3452                 ;; Deal with self-referencing References loops.
3453                 (when (and (car (symbol-value refs))
3454                            (not (zerop
3455                                  (apply
3456                                   '+
3457                                   (mapcar
3458                                    (lambda (thread)
3459                                      (gnus-thread-loop-p
3460                                       (car (symbol-value refs)) thread))
3461                                    (cdr (symbol-value refs)))))))
3462                   (setq threads nil)
3463                   (throw 'infloop t))
3464                 (unless (car (symbol-value refs))
3465              ;; These threads do not refer back to any other articles,
3466                   ;; so they're roots.
3467                   (setq threads (append (cdr (symbol-value refs)) threads))))
3468               gnus-newsgroup-dependencies)))
3469     threads))
3470
3471 ;; Build the thread tree.
3472 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3473   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3474
3475 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3476 if it was already present.
3477
3478 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3479 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3480 Message-IDs will be renamed be renamed to a unique Message-ID before
3481 being entered.
3482
3483 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3484   (let* ((id (mail-header-id header))
3485          (id-dep (and id (intern id dependencies)))
3486          ref ref-dep ref-header)
3487     ;; Enter this `header' in the `dependencies' table.
3488     (cond
3489      ((not id-dep)
3490       (setq header nil))
3491      ;; The first two cases do the normal part: enter a new `header'
3492      ;; in the `dependencies' table.
3493      ((not (boundp id-dep))
3494       (set id-dep (list header)))
3495      ((null (car (symbol-value id-dep)))
3496       (setcar (symbol-value id-dep) header))
3497
3498      ;; From here the `header' was already present in the
3499      ;; `dependencies' table.
3500      (force-new
3501       ;; Overrides an existing entry;
3502       ;; just set the header part of the entry.
3503       (setcar (symbol-value id-dep) header))
3504
3505      ;; Renames the existing `header' to a unique Message-ID.
3506      ((not gnus-summary-ignore-duplicates)
3507       ;; An article with this Message-ID has already been seen.
3508       ;; We rename the Message-ID.
3509       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3510            (list header))
3511       (mail-header-set-id header id))
3512
3513      ;; The last case ignores an existing entry, except it adds any
3514      ;; additional Xrefs (in case the two articles came from different
3515      ;; servers.
3516      ;; Also sets `header' to `nil' meaning that the `dependencies'
3517      ;; table was *not* modified.
3518      (t
3519       (mail-header-set-xref
3520        (car (symbol-value id-dep))
3521        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3522                    "")
3523                (or (mail-header-xref header) "")))
3524       (setq header nil)))
3525
3526     (when header
3527       ;; First check if that we are not creating a References loop.
3528       (setq ref (gnus-parent-id (mail-header-references header)))
3529       (while (and ref
3530                   (setq ref-dep (intern-soft ref dependencies))
3531                   (boundp ref-dep)
3532                   (setq ref-header (car (symbol-value ref-dep))))
3533         (if (string= id ref)
3534             ;; Yuk!  This is a reference loop.  Make the article be a
3535             ;; root article.
3536             (progn
3537               (mail-header-set-references (car (symbol-value id-dep)) "none")
3538               (setq ref nil))
3539           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3540       (setq ref (gnus-parent-id (mail-header-references header)))
3541       (setq ref-dep (intern (or ref "none") dependencies))
3542       (if (boundp ref-dep)
3543           (setcdr (symbol-value ref-dep)
3544                   (nconc (cdr (symbol-value ref-dep))
3545                          (list (symbol-value id-dep))))
3546         (set ref-dep (list nil (symbol-value id-dep)))))
3547     header))
3548
3549 (defun gnus-build-sparse-threads ()
3550   (let ((headers gnus-newsgroup-headers)
3551         (mail-parse-charset gnus-newsgroup-charset)
3552         (gnus-summary-ignore-duplicates t)
3553         header references generation relations
3554         subject child end new-child date)
3555     ;; First we create an alist of generations/relations, where
3556     ;; generations is how much we trust the relation, and the relation
3557     ;; is parent/child.
3558     (gnus-message 7 "Making sparse threads...")
3559     (save-excursion
3560       (nnheader-set-temp-buffer " *gnus sparse threads*")
3561       (while (setq header (pop headers))
3562         (when (and (setq references (mail-header-references header))
3563                    (not (string= references "")))
3564           (insert references)
3565           (setq child (mail-header-id header)
3566                 subject (mail-header-subject header)
3567                 date (mail-header-date header)
3568                 generation 0)
3569           (while (search-backward ">" nil t)
3570             (setq end (1+ (point)))
3571             (when (search-backward "<" nil t)
3572               (setq new-child (buffer-substring (point) end))
3573               (push (list (incf generation)
3574                           child (setq child new-child)
3575                           subject date)
3576                     relations)))
3577           (when child
3578             (push (list (1+ generation) child nil subject) relations))
3579           (erase-buffer)))
3580       (kill-buffer (current-buffer)))
3581     ;; Sort over trustworthiness.
3582     (mapcar
3583      (lambda (relation)
3584        (when (gnus-dependencies-add-header
3585               (make-full-mail-header
3586                gnus-reffed-article-number
3587                (nth 3 relation) "" (or (nth 4 relation) "")
3588                (nth 1 relation)
3589                (or (nth 2 relation) "") 0 0 "")
3590               gnus-newsgroup-dependencies nil)
3591          (push gnus-reffed-article-number gnus-newsgroup-limit)
3592          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3593          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3594                gnus-newsgroup-reads)
3595          (decf gnus-reffed-article-number)))
3596      (sort relations 'car-less-than-car))
3597     (gnus-message 7 "Making sparse threads...done")))
3598
3599 (defun gnus-build-old-threads ()
3600   ;; Look at all the articles that refer back to old articles, and
3601   ;; fetch the headers for the articles that aren't there.  This will
3602   ;; build complete threads - if the roots haven't been expired by the
3603   ;; server, that is.
3604   (let ((mail-parse-charset gnus-newsgroup-charset)
3605         id heads)
3606     (mapatoms
3607      (lambda (refs)
3608        (when (not (car (symbol-value refs)))
3609          (setq heads (cdr (symbol-value refs)))
3610          (while heads
3611            (if (memq (mail-header-number (caar heads))
3612                      gnus-newsgroup-dormant)
3613                (setq heads (cdr heads))
3614              (setq id (symbol-name refs))
3615              (while (and (setq id (gnus-build-get-header id))
3616                          (not (car (gnus-id-to-thread id)))))
3617              (setq heads nil)))))
3618      gnus-newsgroup-dependencies)))
3619
3620 ;; This function has to be called with point after the article number
3621 ;; on the beginning of the line.
3622 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3623   (let ((eol (gnus-point-at-eol))
3624         (buffer (current-buffer))
3625         header)
3626
3627     ;; overview: [num subject from date id refs chars lines misc]
3628     (unwind-protect
3629         (progn
3630           (narrow-to-region (point) eol)
3631           (unless (eobp)
3632             (forward-char))
3633
3634           (setq header
3635                 (make-full-mail-header
3636                  number                 ; number
3637                  (funcall gnus-decode-encoded-word-function
3638                           (nnheader-nov-field)) ; subject
3639                  (funcall gnus-decode-encoded-word-function
3640                           (nnheader-nov-field)) ; from
3641                  (nnheader-nov-field)   ; date
3642                  (nnheader-nov-read-message-id) ; id
3643                  (nnheader-nov-field)   ; refs
3644                  (nnheader-nov-read-integer) ; chars
3645                  (nnheader-nov-read-integer) ; lines
3646                  (unless (eobp)
3647                    (if (looking-at "Xref: ")
3648                        (goto-char (match-end 0)))
3649                    (nnheader-nov-field)) ; Xref
3650                  (nnheader-nov-parse-extra)))) ; extra
3651
3652       (widen))
3653
3654     (when gnus-alter-header-function
3655       (funcall gnus-alter-header-function header))
3656     (gnus-dependencies-add-header header dependencies force-new)))
3657
3658 (defun gnus-build-get-header (id)
3659   "Look through the buffer of NOV lines and find the header to ID.
3660 Enter this line into the dependencies hash table, and return
3661 the id of the parent article (if any)."
3662   (let ((deps gnus-newsgroup-dependencies)
3663         found header)
3664     (prog1
3665         (save-excursion
3666           (set-buffer nntp-server-buffer)
3667           (let ((case-fold-search nil))
3668             (goto-char (point-min))
3669             (while (and (not found)
3670                         (search-forward id nil t))
3671               (beginning-of-line)
3672               (setq found (looking-at
3673                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3674                                    (regexp-quote id))))
3675               (or found (beginning-of-line 2)))
3676             (when found
3677               (beginning-of-line)
3678               (and
3679                (setq header (gnus-nov-parse-line
3680                              (read (current-buffer)) deps))
3681                (gnus-parent-id (mail-header-references header))))))
3682       (when header
3683         (let ((number (mail-header-number header)))
3684           (push number gnus-newsgroup-limit)
3685           (push header gnus-newsgroup-headers)
3686           (if (memq number gnus-newsgroup-unselected)
3687               (progn
3688                 (push number gnus-newsgroup-unreads)
3689                 (setq gnus-newsgroup-unselected
3690                       (delq number gnus-newsgroup-unselected)))
3691             (push number gnus-newsgroup-ancient)))))))
3692
3693 (defun gnus-build-all-threads ()
3694   "Read all the headers."
3695   (let ((gnus-summary-ignore-duplicates t)
3696         (mail-parse-charset gnus-newsgroup-charset)
3697         (dependencies gnus-newsgroup-dependencies)
3698         header article)
3699     (save-excursion
3700       (set-buffer nntp-server-buffer)
3701       (let ((case-fold-search nil))
3702         (goto-char (point-min))
3703         (while (not (eobp))
3704           (ignore-errors
3705             (setq article (read (current-buffer))
3706                   header (gnus-nov-parse-line article dependencies)))
3707           (when header
3708             (save-excursion
3709               (set-buffer gnus-summary-buffer)
3710               (push header gnus-newsgroup-headers)
3711               (if (memq (setq article (mail-header-number header))
3712                         gnus-newsgroup-unselected)
3713                   (progn
3714                     (push article gnus-newsgroup-unreads)
3715                     (setq gnus-newsgroup-unselected
3716                           (delq article gnus-newsgroup-unselected)))
3717                 (push article gnus-newsgroup-ancient)))
3718             (forward-line 1)))))))
3719
3720 (defun gnus-summary-update-article-line (article header)
3721   "Update the line for ARTICLE using HEADERS."
3722   (let* ((id (mail-header-id header))
3723          (thread (gnus-id-to-thread id)))
3724     (unless thread
3725       (error "Article in no thread"))
3726     ;; Update the thread.
3727     (setcar thread header)
3728     (gnus-summary-goto-subject article)
3729     (let* ((datal (gnus-data-find-list article))
3730            (data (car datal))
3731            (length (when (cdr datal)
3732                      (- (gnus-data-pos data)
3733                         (gnus-data-pos (cadr datal)))))
3734            (buffer-read-only nil)
3735            (level (gnus-summary-thread-level)))
3736       (gnus-delete-line)
3737       (gnus-summary-insert-line
3738        header level nil (gnus-article-mark article)
3739        (memq article gnus-newsgroup-replied)
3740        (memq article gnus-newsgroup-expirable)
3741        ;; Only insert the Subject string when it's different
3742        ;; from the previous Subject string.
3743        (if (and
3744             gnus-show-threads
3745             (gnus-subject-equal
3746              (condition-case ()
3747                  (mail-header-subject
3748                   (gnus-data-header
3749                    (cadr
3750                     (gnus-data-find-list
3751                      article
3752                      (gnus-data-list t)))))
3753                ;; Error on the side of excessive subjects.
3754                (error ""))
3755              (mail-header-subject header)))
3756            ""
3757          (mail-header-subject header))
3758        nil (cdr (assq article gnus-newsgroup-scored))
3759        (memq article gnus-newsgroup-processable))
3760       (when length
3761         (gnus-data-update-list
3762          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3763
3764 (defun gnus-summary-update-article (article &optional iheader)
3765   "Update ARTICLE in the summary buffer."
3766   (set-buffer gnus-summary-buffer)
3767   (let* ((header (gnus-summary-article-header article))
3768          (id (mail-header-id header))
3769          (data (gnus-data-find article))
3770          (thread (gnus-id-to-thread id))
3771          (references (mail-header-references header))
3772          (parent
3773           (gnus-id-to-thread
3774            (or (gnus-parent-id
3775                 (when (and references
3776                            (not (equal "" references)))
3777                   references))
3778                "none")))
3779          (buffer-read-only nil)
3780          (old (car thread)))
3781     (when thread
3782       (unless iheader
3783         (setcar thread nil)
3784         (when parent
3785           (delq thread parent)))
3786       (if (gnus-summary-insert-subject id header)
3787        ;; Set the (possibly) new article number in the data structure.
3788           (gnus-data-set-number data (gnus-id-to-article id))
3789         (setcar thread old)
3790         nil))))
3791
3792 (defun gnus-rebuild-thread (id &optional line)
3793   "Rebuild the thread containing ID.
3794 If LINE, insert the rebuilt thread starting on line LINE."
3795   (let ((buffer-read-only nil)
3796         old-pos current thread data)
3797     (if (not gnus-show-threads)
3798         (setq thread (list (car (gnus-id-to-thread id))))
3799       ;; Get the thread this article is part of.
3800       (setq thread (gnus-remove-thread id)))
3801     (setq old-pos (gnus-point-at-bol))
3802     (setq current (save-excursion
3803                     (and (re-search-backward "[\r\n]" nil t)
3804                          (gnus-summary-article-number))))
3805     ;; If this is a gathered thread, we have to go some re-gathering.
3806     (when (stringp (car thread))
3807       (let ((subject (car thread))
3808             roots thr)
3809         (setq thread (cdr thread))
3810         (while thread
3811           (unless (memq (setq thr (gnus-id-to-thread
3812                                    (gnus-root-id
3813                                     (mail-header-id (caar thread)))))
3814                         roots)
3815             (push thr roots))
3816           (setq thread (cdr thread)))
3817         ;; We now have all (unique) roots.
3818         (if (= (length roots) 1)
3819             ;; All the loose roots are now one solid root.
3820             (setq thread (car roots))
3821           (setq thread (cons subject (gnus-sort-threads roots))))))
3822     (let (threads)
3823       ;; We then insert this thread into the summary buffer.
3824       (when line
3825         (goto-char (point-min))
3826         (forward-line (1- line)))
3827       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3828         (if gnus-show-threads
3829             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3830           (gnus-summary-prepare-unthreaded thread))
3831         (setq data (nreverse gnus-newsgroup-data))
3832         (setq threads gnus-newsgroup-threads))
3833       ;; We splice the new data into the data structure.
3834       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3835       ;;!!! then we want to insert at the beginning of the buffer.
3836       ;;!!! That happens to be true with Gnus now, but that may
3837       ;;!!! change in the future.  Perhaps.
3838       (gnus-data-enter-list
3839        (if line nil current) data (- (point) old-pos))
3840       (setq gnus-newsgroup-threads
3841             (nconc threads gnus-newsgroup-threads))
3842       (gnus-data-compute-positions))))
3843
3844 (defun gnus-number-to-header (number)
3845   "Return the header for article NUMBER."
3846   (let ((headers gnus-newsgroup-headers))
3847     (while (and headers
3848                 (not (= number (mail-header-number (car headers)))))
3849       (pop headers))
3850     (when headers
3851       (car headers))))
3852
3853 (defun gnus-parent-headers (in-headers &optional generation)
3854   "Return the headers of the GENERATIONeth parent of HEADERS."
3855   (unless generation
3856     (setq generation 1))
3857   (let ((parent t)
3858         (headers in-headers)
3859         references)
3860     (while (and parent
3861                 (not (zerop generation))
3862                 (setq references (mail-header-references headers)))
3863       (setq headers (if (and references
3864                              (setq parent (gnus-parent-id references)))
3865                         (car (gnus-id-to-thread parent))
3866                       nil))
3867       (decf generation))
3868     (and (not (eq headers in-headers))
3869          headers)))
3870
3871 (defun gnus-id-to-thread (id)
3872   "Return the (sub-)thread where ID appears."
3873   (gnus-gethash id gnus-newsgroup-dependencies))
3874
3875 (defun gnus-id-to-article (id)
3876   "Return the article number of ID."
3877   (let ((thread (gnus-id-to-thread id)))
3878     (when (and thread
3879                (car thread))
3880       (mail-header-number (car thread)))))
3881
3882 (defun gnus-id-to-header (id)
3883   "Return the article headers of ID."
3884   (car (gnus-id-to-thread id)))
3885
3886 (defun gnus-article-displayed-root-p (article)
3887   "Say whether ARTICLE is a root(ish) article."
3888   (let ((level (gnus-summary-thread-level article))
3889         (refs (mail-header-references  (gnus-summary-article-header article)))
3890         particle)
3891     (cond
3892      ((null level) nil)
3893      ((zerop level) t)
3894      ((null refs) t)
3895      ((null (gnus-parent-id refs)) t)
3896      ((and (= 1 level)
3897            (null (setq particle (gnus-id-to-article
3898                                  (gnus-parent-id refs))))
3899            (null (gnus-summary-thread-level particle)))))))
3900
3901 (defun gnus-root-id (id)
3902   "Return the id of the root of the thread where ID appears."
3903   (let (last-id prev)
3904     (while (and id (setq prev (car (gnus-id-to-thread id))))
3905       (setq last-id id
3906             id (gnus-parent-id (mail-header-references prev))))
3907     last-id))
3908
3909 (defun gnus-articles-in-thread (thread)
3910   "Return the list of articles in THREAD."
3911   (cons (mail-header-number (car thread))
3912         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3913
3914 (defun gnus-remove-thread (id &optional dont-remove)
3915   "Remove the thread that has ID in it."
3916   (let (headers thread last-id)
3917     ;; First go up in this thread until we find the root.
3918     (setq last-id (gnus-root-id id)
3919           headers (message-flatten-list (gnus-id-to-thread last-id)))
3920     ;; We have now found the real root of this thread.  It might have
3921     ;; been gathered into some loose thread, so we have to search
3922     ;; through the threads to find the thread we wanted.
3923     (let ((threads gnus-newsgroup-threads)
3924           sub)
3925       (while threads
3926         (setq sub (car threads))
3927         (if (stringp (car sub))
3928             ;; This is a gathered thread, so we look at the roots
3929             ;; below it to find whether this article is in this
3930             ;; gathered root.
3931             (progn
3932               (setq sub (cdr sub))
3933               (while sub
3934                 (when (member (caar sub) headers)
3935                   (setq thread (car threads)
3936                         threads nil
3937                         sub nil))
3938                 (setq sub (cdr sub))))
3939           ;; It's an ordinary thread, so we check it.
3940           (when (eq (car sub) (car headers))
3941             (setq thread sub
3942                   threads nil)))
3943         (setq threads (cdr threads)))
3944       ;; If this article is in no thread, then it's a root.
3945       (if thread
3946           (unless dont-remove
3947             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3948         (setq thread (gnus-id-to-thread last-id)))
3949       (when thread
3950         (prog1
3951             thread                      ; We return this thread.
3952           (unless dont-remove
3953             (if (stringp (car thread))
3954                 (progn
3955                   ;; If we use dummy roots, then we have to remove the
3956                   ;; dummy root as well.
3957                   (when (eq gnus-summary-make-false-root 'dummy)
3958                     ;; We go to the dummy root by going to
3959                     ;; the first sub-"thread", and then one line up.
3960                     (gnus-summary-goto-article
3961                      (mail-header-number (caadr thread)))
3962                     (forward-line -1)
3963                     (gnus-delete-line)
3964                     (gnus-data-compute-positions))
3965                   (setq thread (cdr thread))
3966                   (while thread
3967                     (gnus-remove-thread-1 (car thread))
3968                     (setq thread (cdr thread))))
3969               (gnus-remove-thread-1 thread))))))))
3970
3971 (defun gnus-remove-thread-1 (thread)
3972   "Remove the thread THREAD recursively."
3973   (let ((number (mail-header-number (pop thread)))
3974         d)
3975     (setq thread (reverse thread))
3976     (while thread
3977       (gnus-remove-thread-1 (pop thread)))
3978     (when (setq d (gnus-data-find number))
3979       (goto-char (gnus-data-pos d))
3980       (gnus-summary-show-thread)
3981       (gnus-data-remove
3982        number
3983        (- (gnus-point-at-bol)
3984           (prog1
3985               (1+ (gnus-point-at-eol))
3986             (gnus-delete-line)))))))
3987
3988 (defun gnus-sort-threads-1 (threads func)
3989   (sort (mapcar (lambda (thread)
3990                   (cons (car thread)
3991                         (and (cdr thread)
3992                              (gnus-sort-threads-1 (cdr thread) func))))
3993                 threads) func))
3994
3995 (defun gnus-sort-threads (threads)
3996   "Sort THREADS."
3997   (if (not gnus-thread-sort-functions)
3998       threads
3999     (gnus-message 8 "Sorting threads...")
4000     (prog1
4001         (gnus-sort-threads-1
4002          threads
4003          (gnus-make-sort-function gnus-thread-sort-functions))
4004       (gnus-message 8 "Sorting threads...done"))))
4005
4006 (defun gnus-sort-articles (articles)
4007   "Sort ARTICLES."
4008   (when gnus-article-sort-functions
4009     (gnus-message 7 "Sorting articles...")
4010     (prog1
4011         (setq gnus-newsgroup-headers
4012               (sort articles (gnus-make-sort-function
4013                               gnus-article-sort-functions)))
4014       (gnus-message 7 "Sorting articles...done"))))
4015
4016 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4017 (defmacro gnus-thread-header (thread)
4018   "Return header of first article in THREAD.
4019 Note that THREAD must never, ever be anything else than a variable -
4020 using some other form will lead to serious barfage."
4021   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4022   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4023   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4024         (vector thread) 2))
4025
4026 (defsubst gnus-article-sort-by-number (h1 h2)
4027   "Sort articles by article number."
4028   (< (mail-header-number h1)
4029      (mail-header-number h2)))
4030
4031 (defun gnus-thread-sort-by-number (h1 h2)
4032   "Sort threads by root article number."
4033   (gnus-article-sort-by-number
4034    (gnus-thread-header h1) (gnus-thread-header h2)))
4035
4036 (defsubst gnus-article-sort-by-lines (h1 h2)
4037   "Sort articles by article Lines header."
4038   (< (mail-header-lines h1)
4039      (mail-header-lines h2)))
4040
4041 (defun gnus-thread-sort-by-lines (h1 h2)
4042   "Sort threads by root article Lines header."
4043   (gnus-article-sort-by-lines
4044    (gnus-thread-header h1) (gnus-thread-header h2)))
4045
4046 (defsubst gnus-article-sort-by-chars (h1 h2)
4047   "Sort articles by octet length."
4048   (< (mail-header-chars h1)
4049      (mail-header-chars h2)))
4050
4051 (defun gnus-thread-sort-by-chars (h1 h2)
4052   "Sort threads by root article octet length."
4053   (gnus-article-sort-by-chars
4054    (gnus-thread-header h1) (gnus-thread-header h2)))
4055
4056 (defsubst gnus-article-sort-by-author (h1 h2)
4057   "Sort articles by root author."
4058   (string-lessp
4059    (let ((extract (funcall
4060                    gnus-extract-address-components
4061                    (mail-header-from h1))))
4062      (or (car extract) (cadr extract) ""))
4063    (let ((extract (funcall
4064                    gnus-extract-address-components
4065                    (mail-header-from h2))))
4066      (or (car extract) (cadr extract) ""))))
4067
4068 (defun gnus-thread-sort-by-author (h1 h2)
4069   "Sort threads by root author."
4070   (gnus-article-sort-by-author
4071    (gnus-thread-header h1)  (gnus-thread-header h2)))
4072
4073 (defsubst gnus-article-sort-by-subject (h1 h2)
4074   "Sort articles by root subject."
4075   (string-lessp
4076    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4077    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4078
4079 (defun gnus-thread-sort-by-subject (h1 h2)
4080   "Sort threads by root subject."
4081   (gnus-article-sort-by-subject
4082    (gnus-thread-header h1) (gnus-thread-header h2)))
4083
4084 (defsubst gnus-article-sort-by-date (h1 h2)
4085   "Sort articles by root article date."
4086   (time-less-p
4087    (gnus-date-get-time (mail-header-date h1))
4088    (gnus-date-get-time (mail-header-date h2))))
4089
4090 (defun gnus-thread-sort-by-date (h1 h2)
4091   "Sort threads by root article date."
4092   (gnus-article-sort-by-date
4093    (gnus-thread-header h1) (gnus-thread-header h2)))
4094
4095 (defsubst gnus-article-sort-by-score (h1 h2)
4096   "Sort articles by root article score.
4097 Unscored articles will be counted as having a score of zero."
4098   (> (or (cdr (assq (mail-header-number h1)
4099                     gnus-newsgroup-scored))
4100          gnus-summary-default-score 0)
4101      (or (cdr (assq (mail-header-number h2)
4102                     gnus-newsgroup-scored))
4103          gnus-summary-default-score 0)))
4104
4105 (defun gnus-thread-sort-by-score (h1 h2)
4106   "Sort threads by root article score."
4107   (gnus-article-sort-by-score
4108    (gnus-thread-header h1) (gnus-thread-header h2)))
4109
4110 (defun gnus-thread-sort-by-total-score (h1 h2)
4111   "Sort threads by the sum of all scores in the thread.
4112 Unscored articles will be counted as having a score of zero."
4113   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4114
4115 (defun gnus-thread-total-score (thread)
4116   ;; This function find the total score of THREAD.
4117   (cond ((null thread)
4118          0)
4119         ((consp thread)
4120          (if (stringp (car thread))
4121              (apply gnus-thread-score-function 0
4122                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4123            (gnus-thread-total-score-1 thread)))
4124         (t
4125          (gnus-thread-total-score-1 (list thread)))))
4126
4127 (defun gnus-thread-total-score-1 (root)
4128   ;; This function find the total score of the thread below ROOT.
4129   (setq root (car root))
4130   (apply gnus-thread-score-function
4131          (or (append
4132               (mapcar 'gnus-thread-total-score
4133                       (cdr (gnus-id-to-thread (mail-header-id root))))
4134               (when (> (mail-header-number root) 0)
4135                 (list (or (cdr (assq (mail-header-number root)
4136                                      gnus-newsgroup-scored))
4137                           gnus-summary-default-score 0))))
4138              (list gnus-summary-default-score)
4139              '(0))))
4140
4141 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4142 (defvar gnus-tmp-prev-subject nil)
4143 (defvar gnus-tmp-false-parent nil)
4144 (defvar gnus-tmp-root-expunged nil)
4145 (defvar gnus-tmp-dummy-line nil)
4146
4147 (eval-when-compile (defvar gnus-tmp-header))
4148 (defun gnus-extra-header (type &optional header)
4149   "Return the extra header of TYPE."
4150   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4151       ""))
4152
4153 (defvar gnus-tmp-thread-tree-header-string "")
4154
4155 (defvar gnus-sum-thread-tree-root "> "
4156   "With %B spec, used for the root of a thread.
4157 If nil, use subject instead.")
4158 (defvar gnus-sum-thread-tree-single-indent ""
4159   "With %B spec, used for a thread with just one message.
4160 If nil, use subject instead.")
4161 (defvar gnus-sum-thread-tree-vertical "| "
4162   "With %B spec, used for drawing a vertical line.")
4163 (defvar gnus-sum-thread-tree-indent "  "
4164   "With %B spec, used for indenting.")
4165 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4166   "With %B spec, used for a leaf with brothers.")
4167 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4168   "With %B spec, used for a leaf without brothers.")
4169
4170 (defun gnus-summary-prepare-threads (threads)
4171   "Prepare summary buffer from THREADS and indentation LEVEL.
4172 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4173 or a straight list of headers."
4174   (gnus-message 7 "Generating summary...")
4175
4176   (setq gnus-newsgroup-threads threads)
4177   (beginning-of-line)
4178
4179   (let ((gnus-tmp-level 0)
4180         (default-score (or gnus-summary-default-score 0))
4181         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4182         thread number subject stack state gnus-tmp-gathered beg-match
4183         new-roots gnus-tmp-new-adopts thread-end
4184         gnus-tmp-header gnus-tmp-unread
4185         gnus-tmp-replied gnus-tmp-subject-or-nil
4186         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4187         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4188         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4189         tree-stack)
4190
4191     (setq gnus-tmp-prev-subject nil)
4192
4193     (if (vectorp (car threads))
4194         ;; If this is a straight (sic) list of headers, then a
4195         ;; threaded summary display isn't required, so we just create
4196         ;; an unthreaded one.
4197         (gnus-summary-prepare-unthreaded threads)
4198
4199       ;; Do the threaded display.
4200
4201       (while (or threads stack gnus-tmp-new-adopts new-roots)
4202
4203         (if (and (= gnus-tmp-level 0)
4204                  (or (not stack)
4205                      (= (caar stack) 0))
4206                  (not gnus-tmp-false-parent)
4207                  (or gnus-tmp-new-adopts new-roots))
4208             (if gnus-tmp-new-adopts
4209                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4210                       thread (list (car gnus-tmp-new-adopts))
4211                       gnus-tmp-header (caar thread)
4212                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4213               (when new-roots
4214                 (setq thread (list (car new-roots))
4215                       gnus-tmp-header (caar thread)
4216                       new-roots (cdr new-roots))))
4217
4218           (if threads
4219               ;; If there are some threads, we do them before the
4220               ;; threads on the stack.
4221               (setq thread threads
4222                     gnus-tmp-header (caar thread))
4223             ;; There were no current threads, so we pop something off
4224             ;; the stack.
4225             (setq state (car stack)
4226                   gnus-tmp-level (car state)
4227                   tree-stack (cadr state)
4228                   thread (caddr state)
4229                   stack (cdr stack)
4230                   gnus-tmp-header (caar thread))))
4231
4232         (setq gnus-tmp-false-parent nil)
4233         (setq gnus-tmp-root-expunged nil)
4234         (setq thread-end nil)
4235
4236         (if (stringp gnus-tmp-header)
4237             ;; The header is a dummy root.
4238             (cond
4239              ((eq gnus-summary-make-false-root 'adopt)
4240               ;; We let the first article adopt the rest.
4241               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4242                                                (cddar thread)))
4243               (setq gnus-tmp-gathered
4244                     (nconc (mapcar
4245                             (lambda (h) (mail-header-number (car h)))
4246                             (cddar thread))
4247                            gnus-tmp-gathered))
4248               (setq thread (cons (list (caar thread)
4249                                        (cadar thread))
4250                                  (cdr thread)))
4251               (setq gnus-tmp-level -1
4252                     gnus-tmp-false-parent t))
4253              ((eq gnus-summary-make-false-root 'empty)
4254               ;; We print adopted articles with empty subject fields.
4255               (setq gnus-tmp-gathered
4256                     (nconc (mapcar
4257                             (lambda (h) (mail-header-number (car h)))
4258                             (cddar thread))
4259                            gnus-tmp-gathered))
4260               (setq gnus-tmp-level -1))
4261              ((eq gnus-summary-make-false-root 'dummy)
4262               ;; We remember that we probably want to output a dummy
4263               ;; root.
4264               (setq gnus-tmp-dummy-line gnus-tmp-header)
4265               (setq gnus-tmp-prev-subject gnus-tmp-header))
4266              (t
4267               ;; We do not make a root for the gathered
4268               ;; sub-threads at all.
4269               (setq gnus-tmp-level -1)))
4270
4271           (setq number (mail-header-number gnus-tmp-header)
4272                 subject (mail-header-subject gnus-tmp-header))
4273
4274           (cond
4275            ;; If the thread has changed subject, we might want to make
4276            ;; this subthread into a root.
4277            ((and (null gnus-thread-ignore-subject)
4278                  (not (zerop gnus-tmp-level))
4279                  gnus-tmp-prev-subject
4280                  (not (inline
4281                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4282             (setq new-roots (nconc new-roots (list (car thread)))
4283                   thread-end t
4284                   gnus-tmp-header nil))
4285            ;; If the article lies outside the current limit,
4286            ;; then we do not display it.
4287            ((not (memq number gnus-newsgroup-limit))
4288             (setq gnus-tmp-gathered
4289                   (nconc (mapcar
4290                           (lambda (h) (mail-header-number (car h)))
4291                           (cdar thread))
4292                          gnus-tmp-gathered))
4293             (setq gnus-tmp-new-adopts (if (cdar thread)
4294                                           (append gnus-tmp-new-adopts
4295                                                   (cdar thread))
4296                                         gnus-tmp-new-adopts)
4297                   thread-end t
4298                   gnus-tmp-header nil)
4299             (when (zerop gnus-tmp-level)
4300               (setq gnus-tmp-root-expunged t)))
4301            ;; Perhaps this article is to be marked as read?
4302            ((and gnus-summary-mark-below
4303                  (< (or (cdr (assq number gnus-newsgroup-scored))
4304                         default-score)
4305                     gnus-summary-mark-below)
4306                  ;; Don't touch sparse articles.
4307                  (not (gnus-summary-article-sparse-p number))
4308                  (not (gnus-summary-article-ancient-p number)))
4309             (setq gnus-newsgroup-unreads
4310                   (delq number gnus-newsgroup-unreads))
4311             (if gnus-newsgroup-auto-expire
4312                 (push number gnus-newsgroup-expirable)
4313               (push (cons number gnus-low-score-mark)
4314                     gnus-newsgroup-reads))))
4315
4316           (when gnus-tmp-header
4317             ;; We may have an old dummy line to output before this
4318             ;; article.
4319             (when (and gnus-tmp-dummy-line
4320                        (gnus-subject-equal
4321                         gnus-tmp-dummy-line
4322                         (mail-header-subject gnus-tmp-header)))
4323               (gnus-summary-insert-dummy-line
4324                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4325               (setq gnus-tmp-dummy-line nil))
4326
4327             ;; Compute the mark.
4328             (setq gnus-tmp-unread (gnus-article-mark number))
4329
4330             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4331                                   gnus-tmp-header gnus-tmp-level)
4332                   gnus-newsgroup-data)
4333
4334             ;; Actually insert the line.
4335             (setq
4336              gnus-tmp-subject-or-nil
4337              (cond
4338               ((and gnus-thread-ignore-subject
4339                     gnus-tmp-prev-subject
4340                     (not (inline (gnus-subject-equal
4341                                   gnus-tmp-prev-subject subject))))
4342                subject)
4343               ((zerop gnus-tmp-level)
4344                (if (and (eq gnus-summary-make-false-root 'empty)
4345                         (memq number gnus-tmp-gathered)
4346                         gnus-tmp-prev-subject
4347                         (inline (gnus-subject-equal
4348                                  gnus-tmp-prev-subject subject)))
4349                    gnus-summary-same-subject
4350                  subject))
4351               (t gnus-summary-same-subject)))
4352             (if (and (eq gnus-summary-make-false-root 'adopt)
4353                      (= gnus-tmp-level 1)
4354                      (memq number gnus-tmp-gathered))
4355                 (setq gnus-tmp-opening-bracket ?\<
4356                       gnus-tmp-closing-bracket ?\>)
4357               (setq gnus-tmp-opening-bracket ?\[
4358                     gnus-tmp-closing-bracket ?\]))
4359             (setq
4360              gnus-tmp-indentation
4361              (aref gnus-thread-indent-array gnus-tmp-level)
4362              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4363              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4364                                 gnus-summary-default-score 0)
4365              gnus-tmp-score-char
4366              (if (or (null gnus-summary-default-score)
4367                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4368                          gnus-summary-zcore-fuzz))
4369                  ?                      ;Whitespace
4370                (if (< gnus-tmp-score gnus-summary-default-score)
4371                    gnus-score-below-mark gnus-score-over-mark))
4372              gnus-tmp-replied
4373              (cond ((memq number gnus-newsgroup-processable)
4374                     gnus-process-mark)
4375                    ((memq number gnus-newsgroup-cached)
4376                     gnus-cached-mark)
4377                    ((memq number gnus-newsgroup-replied)
4378                     gnus-replied-mark)
4379                    ((memq number gnus-newsgroup-forwarded)
4380                     gnus-forwarded-mark)
4381                    ((memq number gnus-newsgroup-saved)
4382                     gnus-saved-mark)
4383                    ((memq number gnus-newsgroup-recent)
4384                     gnus-recent-mark)
4385                    ((memq number gnus-newsgroup-unseen)
4386                     gnus-unseen-mark)
4387                    (t gnus-no-mark))
4388              gnus-tmp-from (mail-header-from gnus-tmp-header)
4389              gnus-tmp-name
4390              (cond
4391               ((string-match "<[^>]+> *$" gnus-tmp-from)
4392                (setq beg-match (match-beginning 0))
4393                (or (and (string-match "^\".+\"" gnus-tmp-from)
4394                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4395                    (substring gnus-tmp-from 0 beg-match)))
4396               ((string-match "(.+)" gnus-tmp-from)
4397                (substring gnus-tmp-from
4398                           (1+ (match-beginning 0)) (1- (match-end 0))))
4399               (t gnus-tmp-from))
4400              gnus-tmp-thread-tree-header-string
4401              (cond
4402               ((not gnus-show-threads) "")
4403               ((zerop gnus-tmp-level)
4404                (if (cdar thread)
4405                    (or gnus-sum-thread-tree-root subject)
4406                  (or gnus-sum-thread-tree-single-indent subject)))
4407               (t
4408                (concat (apply 'concat
4409                               (mapcar (lambda (item)
4410                                         (if (= item 1)
4411                                             gnus-sum-thread-tree-vertical
4412                                           gnus-sum-thread-tree-indent))
4413                                       (cdr (reverse tree-stack))))
4414                        (if (nth 1 thread)
4415                            gnus-sum-thread-tree-leaf-with-other
4416                          gnus-sum-thread-tree-single-leaf)))))
4417             (when (string= gnus-tmp-name "")
4418               (setq gnus-tmp-name gnus-tmp-from))
4419             (unless (numberp gnus-tmp-lines)
4420               (setq gnus-tmp-lines -1))
4421             (if (= gnus-tmp-lines -1)
4422                 (setq gnus-tmp-lines "?")
4423               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4424             (gnus-put-text-property
4425              (point)
4426              (progn (eval gnus-summary-line-format-spec) (point))
4427              'gnus-number number)
4428             (when gnus-visual-p
4429               (forward-line -1)
4430               (gnus-run-hooks 'gnus-summary-update-hook)
4431               (forward-line 1))
4432
4433             (setq gnus-tmp-prev-subject subject)))
4434
4435         (when (nth 1 thread)
4436           (push (list (max 0 gnus-tmp-level)
4437                       (copy-list tree-stack)
4438                       (nthcdr 1 thread))
4439                 stack))
4440         (push (if (nth 1 thread) 1 0) tree-stack)
4441         (incf gnus-tmp-level)
4442         (setq threads (if thread-end nil (cdar thread)))
4443         (unless threads
4444           (setq gnus-tmp-level 0)))))
4445   (gnus-message 7 "Generating summary...done"))
4446
4447 (defun gnus-summary-prepare-unthreaded (headers)
4448   "Generate an unthreaded summary buffer based on HEADERS."
4449   (let (header number mark)
4450
4451     (beginning-of-line)
4452
4453     (while headers
4454       ;; We may have to root out some bad articles...
4455       (when (memq (setq number (mail-header-number
4456                                 (setq header (pop headers))))
4457                   gnus-newsgroup-limit)
4458         ;; Mark article as read when it has a low score.
4459         (when (and gnus-summary-mark-below
4460                    (< (or (cdr (assq number gnus-newsgroup-scored))
4461                           gnus-summary-default-score 0)
4462                       gnus-summary-mark-below)
4463                    (not (gnus-summary-article-ancient-p number)))
4464           (setq gnus-newsgroup-unreads
4465                 (delq number gnus-newsgroup-unreads))
4466           (if gnus-newsgroup-auto-expire
4467               (push number gnus-newsgroup-expirable)
4468             (push (cons number gnus-low-score-mark)
4469                   gnus-newsgroup-reads)))
4470
4471         (setq mark (gnus-article-mark number))
4472         (push (gnus-data-make number mark (1+ (point)) header 0)
4473               gnus-newsgroup-data)
4474         (gnus-summary-insert-line
4475          header 0 number
4476          mark (memq number gnus-newsgroup-replied)
4477          (memq number gnus-newsgroup-expirable)
4478          (mail-header-subject header) nil
4479          (cdr (assq number gnus-newsgroup-scored))
4480          (memq number gnus-newsgroup-processable))))))
4481
4482 (defun gnus-summary-remove-list-identifiers ()
4483   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4484   (let ((regexp (if (consp gnus-list-identifiers)
4485                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4486                   gnus-list-identifiers))
4487         changed subject)
4488     (when regexp
4489       (dolist (header gnus-newsgroup-headers)
4490         (setq subject (mail-header-subject header)
4491               changed nil)
4492         (while (string-match
4493                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4494                 subject)
4495           (setq subject
4496                 (concat (substring subject 0 (match-beginning 2))
4497                         (substring subject (match-end 0)))
4498                 changed t))
4499         (when (and changed
4500                    (string-match
4501                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4502           (setq subject
4503                 (concat (substring subject 0 (match-beginning 1))
4504                         (substring subject (match-end 1)))))
4505         (when changed
4506           (mail-header-set-subject header subject))))))
4507
4508 (defun gnus-fetch-headers (articles)
4509   "Fetch headers of ARTICLES."
4510   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4511     (gnus-message 5 "Fetching headers for %s..." name)
4512     (prog1
4513         (if (eq 'nov
4514                 (setq gnus-headers-retrieved-by
4515                       (gnus-retrieve-headers
4516                        articles gnus-newsgroup-name
4517                        ;; We might want to fetch old headers, but
4518                        ;; not if there is only 1 article.
4519                        (and (or (and
4520                                  (not (eq gnus-fetch-old-headers 'some))
4521                                  (not (numberp gnus-fetch-old-headers)))
4522                                 (> (length articles) 1))
4523                             gnus-fetch-old-headers))))
4524             (gnus-get-newsgroup-headers-xover
4525              articles nil nil gnus-newsgroup-name t)
4526           (gnus-get-newsgroup-headers))
4527       (gnus-message 5 "Fetching headers for %s...done" name))))
4528
4529 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4530   "Select newsgroup GROUP.
4531 If READ-ALL is non-nil, all articles in the group are selected.
4532 If SELECT-ARTICLES, only select those articles from GROUP."
4533   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4534          ;;!!! Dirty hack; should be removed.
4535          (gnus-summary-ignore-duplicates
4536           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4537               t
4538             gnus-summary-ignore-duplicates))
4539          (info (nth 2 entry))
4540          articles fetched-articles cached)
4541
4542     (unless (gnus-check-server
4543              (set (make-local-variable 'gnus-current-select-method)
4544                   (gnus-find-method-for-group group)))
4545       (error "Couldn't open server"))
4546
4547     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4548         (gnus-activate-group group)     ; Or we can activate it...
4549         (progn                          ; Or we bug out.
4550           (when (equal major-mode 'gnus-summary-mode)
4551             (kill-buffer (current-buffer)))
4552           (error "Couldn't activate group %s: %s"
4553                  group (gnus-status-message group))))
4554
4555     (unless (gnus-request-group group t)
4556       (when (equal major-mode 'gnus-summary-mode)
4557         (kill-buffer (current-buffer)))
4558       (error "Couldn't request group %s: %s"
4559              group (gnus-status-message group)))
4560
4561     (setq gnus-newsgroup-name group
4562           gnus-newsgroup-unselected nil
4563           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4564
4565     (let ((display (gnus-group-find-parameter group 'display)))
4566       (setq gnus-newsgroup-display
4567             (cond
4568              ((not (zerop (or (car-safe read-all) 0)))
4569               ;; The user entered the group with C-u SPC/RET, let's show
4570               ;; all articles.
4571               'gnus-not-ignore)
4572              ((eq display 'all)
4573               'gnus-not-ignore)
4574              ((arrayp display)
4575               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4576              ((numberp display)
4577               ;; The following is probably the "correct" solution, but
4578               ;; it makes Gnus fetch all headers and then limit the
4579               ;; articles (which is slow), so instead we hack the
4580               ;; select-articles parameter instead. -- Simon Josefsson
4581               ;; <jas@kth.se>
4582               ;;
4583               ;; (gnus-byte-compile
4584               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4585               ;;                         display)))))
4586               (setq select-articles
4587                     (gnus-uncompress-range
4588                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4589                              (if (> tmp 0)
4590                                  tmp
4591                                1))
4592                            (cdr (gnus-active group)))))
4593               nil)
4594              (t
4595               nil))))
4596
4597     (gnus-summary-setup-default-charset)
4598
4599     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4600     (when (gnus-virtual-group-p group)
4601       (setq cached gnus-newsgroup-cached))
4602
4603     (setq gnus-newsgroup-unreads
4604           (gnus-set-difference
4605            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4606            gnus-newsgroup-dormant))
4607
4608     (setq gnus-newsgroup-processable nil)
4609
4610     (gnus-update-read-articles group gnus-newsgroup-unreads)
4611
4612     ;; Adjust and set lists of article marks.
4613     (when info
4614       (gnus-adjust-marked-articles info))
4615
4616     (if (setq articles select-articles)
4617         (setq gnus-newsgroup-unselected
4618               (gnus-sorted-intersection
4619                gnus-newsgroup-unreads
4620                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4621       (setq articles (gnus-articles-to-read group read-all)))
4622
4623     (cond
4624      ((null articles)
4625       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4626       'quit)
4627      ((eq articles 0) nil)
4628      (t
4629       ;; Init the dependencies hash table.
4630       (setq gnus-newsgroup-dependencies
4631             (gnus-make-hashtable (length articles)))
4632       (gnus-set-global-variables)
4633       ;; Retrieve the headers and read them in.
4634       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4635
4636       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4637       (when cached
4638         (setq gnus-newsgroup-cached cached))
4639
4640       ;; Suppress duplicates?
4641       (when gnus-suppress-duplicates
4642         (gnus-dup-suppress-articles))
4643
4644       ;; Set the initial limit.
4645       (setq gnus-newsgroup-limit (copy-sequence articles))
4646       ;; Remove canceled articles from the list of unread articles.
4647       (setq fetched-articles
4648             (mapcar (lambda (headers) (mail-header-number headers))
4649                     gnus-newsgroup-headers))
4650       (setq gnus-newsgroup-articles fetched-articles)
4651       (setq gnus-newsgroup-unreads
4652             (gnus-set-sorted-intersection
4653              gnus-newsgroup-unreads fetched-articles))
4654
4655       (let ((marks (assq 'seen (gnus-info-marks info))))
4656         ;; The `seen' marks are treated specially.
4657         (when (setq gnus-newsgroup-seen (cdr marks))
4658           (dolist (article gnus-newsgroup-articles)
4659             (unless (gnus-member-of-range
4660                      article gnus-newsgroup-seen)
4661               (push article gnus-newsgroup-unseen)))))
4662
4663       ;; Removed marked articles that do not exist.
4664       (gnus-update-missing-marks
4665        (gnus-sorted-complement fetched-articles articles))
4666       ;; We might want to build some more threads first.
4667       (when (and gnus-fetch-old-headers
4668                  (eq gnus-headers-retrieved-by 'nov))
4669         (if (eq gnus-fetch-old-headers 'invisible)
4670             (gnus-build-all-threads)
4671           (gnus-build-old-threads)))
4672       ;; Let the Gnus agent mark articles as read.
4673       (when gnus-agent
4674         (gnus-agent-get-undownloaded-list))
4675       ;; Remove list identifiers from subject
4676       (when gnus-list-identifiers
4677         (gnus-summary-remove-list-identifiers))
4678       ;; Check whether auto-expire is to be done in this group.
4679       (setq gnus-newsgroup-auto-expire
4680             (gnus-group-auto-expirable-p group))
4681       ;; Set up the article buffer now, if necessary.
4682       (unless gnus-single-article-buffer
4683         (gnus-article-setup-buffer))
4684       ;; First and last article in this newsgroup.
4685       (when gnus-newsgroup-headers
4686         (setq gnus-newsgroup-begin
4687               (mail-header-number (car gnus-newsgroup-headers))
4688               gnus-newsgroup-end
4689               (mail-header-number
4690                (gnus-last-element gnus-newsgroup-headers))))
4691       ;; GROUP is successfully selected.
4692       (or gnus-newsgroup-headers t)))))
4693
4694 (defun gnus-summary-display-make-predicate (display)
4695   (require 'gnus-agent)
4696   (when (= (length display) 1)
4697     (setq display (car display)))
4698   (unless gnus-summary-display-cache
4699     (dolist (elem (append (list (cons 'read 'read)
4700                                 (cons 'unseen 'unseen))
4701                           gnus-article-mark-lists))
4702       (push (cons (cdr elem)
4703                   (gnus-byte-compile
4704                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4705             gnus-summary-display-cache)))
4706   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4707     (gnus-get-predicate display)))
4708
4709 ;; Uses the dynamically bound `number' variable.
4710 (defvar number)
4711 (defun gnus-article-marked-p (type &optional article)
4712   (let ((article (or article number)))
4713     (cond
4714      ((eq type 'tick)
4715       (memq article gnus-newsgroup-marked))
4716      ((eq type 'unsend)
4717       (memq article gnus-newsgroup-unsendable))
4718      ((eq type 'undownload)
4719       (memq article gnus-newsgroup-undownloaded))
4720      ((eq type 'download)
4721       (memq article gnus-newsgroup-downloadable))
4722      ((eq type 'unread)
4723       (memq article gnus-newsgroup-unreads))
4724      ((eq type 'read)
4725       (memq article gnus-newsgroup-reads))
4726      ((eq type 'dormant)
4727       (memq article gnus-newsgroup-dormant) )
4728      ((eq type 'expire)
4729       (memq article gnus-newsgroup-expirable))
4730      ((eq type 'reply)
4731       (memq article gnus-newsgroup-replied))
4732      ((eq type 'killed)
4733       (memq article gnus-newsgroup-killed))
4734      ((eq type 'bookmark)
4735       (assq article gnus-newsgroup-bookmarks))
4736      ((eq type 'score)
4737       (assq article gnus-newsgroup-scored))
4738      ((eq type 'save)
4739       (memq article gnus-newsgroup-saved))
4740      ((eq type 'cache)
4741       (memq article gnus-newsgroup-cached))
4742      ((eq type 'forward)
4743       (memq article gnus-newsgroup-forwarded))
4744      ((eq type 'seen)
4745       (not (memq article gnus-newsgroup-unseen)))
4746      ((eq type 'recent)
4747       (memq article gnus-newsgroup-recent))
4748      (t t))))
4749
4750 (defun gnus-articles-to-read (group &optional read-all)
4751   "Find out what articles the user wants to read."
4752   (let* ((articles
4753           ;; Select all articles if `read-all' is non-nil, or if there
4754           ;; are no unread articles.
4755           (if (or read-all
4756                   (and (zerop (length gnus-newsgroup-marked))
4757                        (zerop (length gnus-newsgroup-unreads)))
4758                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4759               ;; We want to select the headers for all the articles in
4760               ;; the group, so we select either all the active
4761               ;; articles in the group, or (if that's nil), the
4762               ;; articles in the cache.
4763               (or
4764                (gnus-uncompress-range (gnus-active group))
4765                (gnus-cache-articles-in-group group))
4766             ;; Select only the "normal" subset of articles.
4767             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4768                           (copy-sequence gnus-newsgroup-unreads))
4769                   '<)))
4770          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4771          (scored (length scored-list))
4772          (number (length articles))
4773          (marked (+ (length gnus-newsgroup-marked)
4774                     (length gnus-newsgroup-dormant)))
4775          (select
4776           (cond
4777            ((numberp read-all)
4778             read-all)
4779            (t
4780             (condition-case ()
4781                 (cond
4782                  ((and (or (<= scored marked) (= scored number))
4783                        (numberp gnus-large-newsgroup)
4784                        (> number gnus-large-newsgroup))
4785                   (let ((input
4786                          (read-string
4787                           (format
4788                            "How many articles from %s (default %d): "
4789                            (gnus-limit-string
4790                             (gnus-group-decoded-name gnus-newsgroup-name)
4791                             35)
4792                            number))))
4793                     (if (string-match "^[ \t]*$" input) number input)))
4794                  ((and (> scored marked) (< scored number)
4795                        (> (- scored number) 20))
4796                   (let ((input
4797                          (read-string
4798                           (format "%s %s (%d scored, %d total): "
4799                                   "How many articles from"
4800                                   (gnus-group-decoded-name group)
4801                                   scored number))))
4802                     (if (string-match "^[ \t]*$" input)
4803                         number input)))
4804                  (t number))
4805               (quit
4806                (message "Quit getting the articles to read")
4807                nil))))))
4808     (setq select (if (stringp select) (string-to-number select) select))
4809     (if (or (null select) (zerop select))
4810         select
4811       (if (and (not (zerop scored)) (<= (abs select) scored))
4812           (progn
4813             (setq articles (sort scored-list '<))
4814             (setq number (length articles)))
4815         (setq articles (copy-sequence articles)))
4816
4817       (when (< (abs select) number)
4818         (if (< select 0)
4819             ;; Select the N oldest articles.
4820             (setcdr (nthcdr (1- (abs select)) articles) nil)
4821           ;; Select the N most recent articles.
4822           (setq articles (nthcdr (- number select) articles))))
4823       (setq gnus-newsgroup-unselected
4824             (gnus-sorted-intersection
4825              gnus-newsgroup-unreads
4826              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4827       (when gnus-alter-articles-to-read-function
4828         (setq gnus-newsgroup-unreads
4829               (sort
4830                (funcall gnus-alter-articles-to-read-function
4831                         gnus-newsgroup-name gnus-newsgroup-unreads)
4832                '<)))
4833       articles)))
4834
4835 (defun gnus-killed-articles (killed articles)
4836   (let (out)
4837     (while articles
4838       (when (inline (gnus-member-of-range (car articles) killed))
4839         (push (car articles) out))
4840       (setq articles (cdr articles)))
4841     out))
4842
4843 (defun gnus-uncompress-marks (marks)
4844   "Uncompress the mark ranges in MARKS."
4845   (let ((uncompressed '(score bookmark))
4846         out)
4847     (while marks
4848       (if (memq (caar marks) uncompressed)
4849           (push (car marks) out)
4850         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4851       (setq marks (cdr marks)))
4852     out))
4853
4854 (defun gnus-article-mark-to-type (mark)
4855   "Return the type of MARK."
4856   (or (cadr (assq mark gnus-article-special-mark-lists))
4857       'list))
4858
4859 (defun gnus-article-unpropagatable-p (mark)
4860   "Return whether MARK should be propagated to backend."
4861   (memq mark gnus-article-unpropagated-mark-lists))
4862
4863 (defun gnus-adjust-marked-articles (info)
4864   "Set all article lists and remove all marks that are no longer valid."
4865   (let* ((marked-lists (gnus-info-marks info))
4866          (active (gnus-active (gnus-info-group info)))
4867          (min (car active))
4868          (max (cdr active))
4869          (types gnus-article-mark-lists)
4870          marks var articles article mark mark-type)
4871
4872     (dolist (marks marked-lists)
4873       (setq mark (car marks)
4874             mark-type (gnus-article-mark-to-type mark)
4875             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4876
4877       ;; We set the variable according to the type of the marks list,
4878       ;; and then adjust the marks to a subset of the active articles.
4879       (cond
4880        ;; Adjust "simple" lists.
4881        ((eq mark-type 'list)
4882         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4883         (when (memq mark '(tick dormant expire reply save))
4884           (while articles
4885             (when (or (< (setq article (pop articles)) min) (> article max))
4886               (set var (delq article (symbol-value var)))))))
4887        ;; Adjust assocs.
4888        ((eq mark-type 'tuple)
4889         (set var (setq articles (cdr marks)))
4890         (when (not (listp (cdr (symbol-value var))))
4891           (set var (list (symbol-value var))))
4892         (when (not (listp (cdr articles)))
4893           (setq articles (list articles)))
4894         (while articles
4895           (when (or (not (consp (setq article (pop articles))))
4896                     (< (car article) min)
4897                     (> (car article) max))
4898             (set var (delq article (symbol-value var))))))
4899        ((eq mark-type 'range)
4900         (cond
4901          ((eq mark 'seen))))))))
4902
4903 (defun gnus-update-missing-marks (missing)
4904   "Go through the list of MISSING articles and remove them from the mark lists."
4905   (when missing
4906     (let (var m)
4907       ;; Go through all types.
4908       (dolist (elem gnus-article-mark-lists)
4909         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4910           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4911           (when (symbol-value var)
4912             ;; This list has articles.  So we delete all missing
4913             ;; articles from it.
4914             (setq m missing)
4915             (while m
4916               (set var (delq (pop m) (symbol-value var))))))))))
4917
4918 (defun gnus-update-marks ()
4919   "Enter the various lists of marked articles into the newsgroup info list."
4920   (let ((types gnus-article-mark-lists)
4921         (info (gnus-get-info gnus-newsgroup-name))
4922         type list newmarked symbol delta-marks)
4923     (when info
4924       ;; Add all marks lists to the list of marks lists.
4925       (while (setq type (pop types))
4926         (setq list (symbol-value
4927                     (setq symbol
4928                           (intern (format "gnus-newsgroup-%s" (car type))))))
4929
4930         (when list
4931           ;; Get rid of the entries of the articles that have the
4932           ;; default score.
4933           (when (and (eq (cdr type) 'score)
4934                      gnus-save-score
4935                      list)
4936             (let* ((arts list)
4937                    (prev (cons nil list))
4938                    (all prev))
4939               (while arts
4940                 (if (or (not (consp (car arts)))
4941                         (= (cdar arts) gnus-summary-default-score))
4942                     (setcdr prev (cdr arts))
4943                   (setq prev arts))
4944                 (setq arts (cdr arts)))
4945               (setq list (cdr all)))))
4946
4947         (when (eq (cdr type) 'seen)
4948           (setq list
4949                 (if list
4950                     (gnus-add-to-range list gnus-newsgroup-unseen)
4951                   (gnus-compress-sequence gnus-newsgroup-articles))))
4952
4953         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4954           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4955
4956         (when (and (gnus-check-backend-function
4957                     'request-set-mark gnus-newsgroup-name)
4958                    (not (gnus-article-unpropagatable-p (cdr type))))
4959           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4960                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4961                  (add (gnus-remove-from-range
4962                        (gnus-copy-sequence list) old)))
4963             (when add
4964               (push (list add 'add (list (cdr type))) delta-marks))
4965             (when del
4966               (push (list del 'del (list (cdr type))) delta-marks))))
4967
4968         (when list
4969           (push (cons (cdr type) list) newmarked)))
4970
4971       (when delta-marks
4972         (unless (gnus-check-group gnus-newsgroup-name)
4973           (error "Can't open server for %s" gnus-newsgroup-name))
4974         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4975
4976       ;; Enter these new marks into the info of the group.
4977       (if (nthcdr 3 info)
4978           (setcar (nthcdr 3 info) newmarked)
4979         ;; Add the marks lists to the end of the info.
4980         (when newmarked
4981           (setcdr (nthcdr 2 info) (list newmarked))))
4982
4983       ;; Cut off the end of the info if there's nothing else there.
4984       (let ((i 5))
4985         (while (and (> i 2)
4986                     (not (nth i info)))
4987           (when (nthcdr (decf i) info)
4988             (setcdr (nthcdr i info) nil)))))))
4989
4990 (defun gnus-set-mode-line (where)
4991   "Set the mode line of the article or summary buffers.
4992 If WHERE is `summary', the summary mode line format will be used."
4993   ;; Is this mode line one we keep updated?
4994   (when (and (memq where gnus-updated-mode-lines)
4995              (symbol-value
4996               (intern (format "gnus-%s-mode-line-format-spec" where))))
4997     (let (mode-string)
4998       (save-excursion
4999         ;; We evaluate this in the summary buffer since these
5000         ;; variables are buffer-local to that buffer.
5001         (set-buffer gnus-summary-buffer)
5002        ;; We bind all these variables that are used in the `eval' form
5003         ;; below.
5004         (let* ((mformat (symbol-value
5005                          (intern
5006                           (format "gnus-%s-mode-line-format-spec" where))))
5007                (gnus-tmp-group-name (gnus-group-decoded-name
5008                                      gnus-newsgroup-name))
5009                (gnus-tmp-article-number (or gnus-current-article 0))
5010                (gnus-tmp-unread gnus-newsgroup-unreads)
5011                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5012                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5013                (gnus-tmp-unread-and-unselected
5014                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5015                             (zerop gnus-tmp-unselected))
5016                        "")
5017                       ((zerop gnus-tmp-unselected)
5018                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5019                       (t (format "{%d(+%d) more}"
5020                                  gnus-tmp-unread-and-unticked
5021                                  gnus-tmp-unselected))))
5022                (gnus-tmp-subject
5023                 (if (and gnus-current-headers
5024                          (vectorp gnus-current-headers))
5025                     (gnus-mode-string-quote
5026                      (mail-header-subject gnus-current-headers))
5027                   ""))
5028                bufname-length max-len
5029                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5030           (setq mode-string (eval mformat))
5031           (setq bufname-length (if (string-match "%b" mode-string)
5032                                    (- (length
5033                                        (buffer-name
5034                                         (if (eq where 'summary)
5035                                             nil
5036                                           (get-buffer gnus-article-buffer))))
5037                                       2)
5038                                  0))
5039           (setq max-len (max 4 (if gnus-mode-non-string-length
5040                                    (- (window-width)
5041                                       gnus-mode-non-string-length
5042                                       bufname-length)
5043                                  (length mode-string))))
5044           ;; We might have to chop a bit of the string off...
5045           (when (> (length mode-string) max-len)
5046             (setq mode-string
5047                   (concat (truncate-string-to-width mode-string (- max-len 3))
5048                           "...")))
5049           ;; Pad the mode string a bit.
5050           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5051       ;; Update the mode line.
5052       (setq mode-line-buffer-identification
5053             (gnus-mode-line-buffer-identification (list mode-string)))
5054       (set-buffer-modified-p t))))
5055
5056 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5057   "Go through the HEADERS list and add all Xrefs to a hash table.
5058 The resulting hash table is returned, or nil if no Xrefs were found."
5059   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5060          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5061          (xref-hashtb (gnus-make-hashtable))
5062          start group entry number xrefs header)
5063     (while headers
5064       (setq header (pop headers))
5065       (when (and (setq xrefs (mail-header-xref header))
5066                  (not (memq (setq number (mail-header-number header))
5067                             unreads)))
5068         (setq start 0)
5069         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5070           (setq start (match-end 0))
5071           (setq group (if prefix
5072                           (concat prefix (substring xrefs (match-beginning 1)
5073                                                     (match-end 1)))
5074                         (substring xrefs (match-beginning 1) (match-end 1))))
5075           (setq number
5076                 (string-to-int (substring xrefs (match-beginning 2)
5077                                           (match-end 2))))
5078           (if (setq entry (gnus-gethash group xref-hashtb))
5079               (setcdr entry (cons number (cdr entry)))
5080             (gnus-sethash group (cons number nil) xref-hashtb)))))
5081     (and start xref-hashtb)))
5082
5083 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5084   "Look through all the headers and mark the Xrefs as read."
5085   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5086         name entry info xref-hashtb idlist method nth4)
5087     (save-excursion
5088       (set-buffer gnus-group-buffer)
5089       (when (setq xref-hashtb
5090                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5091         (mapatoms
5092          (lambda (group)
5093            (unless (string= from-newsgroup (setq name (symbol-name group)))
5094              (setq idlist (symbol-value group))
5095              ;; Dead groups are not updated.
5096              (and (prog1
5097                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5098                             info (nth 2 entry))
5099                     (when (stringp (setq nth4 (gnus-info-method info)))
5100                       (setq nth4 (gnus-server-to-method nth4))))
5101                   ;; Only do the xrefs if the group has the same
5102                   ;; select method as the group we have just read.
5103                   (or (gnus-methods-equal-p
5104                        nth4 (gnus-find-method-for-group from-newsgroup))
5105                       virtual
5106                       (equal nth4 (setq method (gnus-find-method-for-group
5107                                                 from-newsgroup)))
5108                       (and (equal (car nth4) (car method))
5109                            (equal (nth 1 nth4) (nth 1 method))))
5110                   gnus-use-cross-reference
5111                   (or (not (eq gnus-use-cross-reference t))
5112                       virtual
5113                       ;; Only do cross-references on subscribed
5114                       ;; groups, if that is what is wanted.
5115                       (<= (gnus-info-level info) gnus-level-subscribed))
5116                   (gnus-group-make-articles-read name idlist))))
5117          xref-hashtb)))))
5118
5119 (defun gnus-compute-read-articles (group articles)
5120   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5121          (info (nth 2 entry))
5122          (active (gnus-active group))
5123          ninfo)
5124     (when entry
5125       ;; First peel off all invalid article numbers.
5126       (when active
5127         (let ((ids articles)
5128               id first)
5129           (while (setq id (pop ids))
5130             (when (and first (> id (cdr active)))
5131               ;; We'll end up in this situation in one particular
5132               ;; obscure situation.  If you re-scan a group and get
5133               ;; a new article that is cross-posted to a different
5134               ;; group that has not been re-scanned, you might get
5135               ;; crossposted article that has a higher number than
5136               ;; Gnus believes possible.  So we re-activate this
5137               ;; group as well.  This might mean doing the
5138               ;; crossposting thingy will *increase* the number
5139               ;; of articles in some groups.  Tsk, tsk.
5140               (setq active (or (gnus-activate-group group) active)))
5141             (when (or (> id (cdr active))
5142                       (< id (car active)))
5143               (setq articles (delq id articles))))))
5144       ;; If the read list is nil, we init it.
5145       (if (and active
5146                (null (gnus-info-read info))
5147                (> (car active) 1))
5148           (setq ninfo (cons 1 (1- (car active))))
5149         (setq ninfo (gnus-info-read info)))
5150       ;; Then we add the read articles to the range.
5151       (gnus-add-to-range
5152        ninfo (setq articles (sort articles '<))))))
5153
5154 (defun gnus-group-make-articles-read (group articles)
5155   "Update the info of GROUP to say that ARTICLES are read."
5156   (let* ((num 0)
5157          (entry (gnus-gethash group gnus-newsrc-hashtb))
5158          (info (nth 2 entry))
5159          (active (gnus-active group))
5160          range)
5161     (when entry
5162       (setq range (gnus-compute-read-articles group articles))
5163       (save-excursion
5164         (set-buffer gnus-group-buffer)
5165         (gnus-undo-register
5166           `(progn
5167              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5168              (gnus-info-set-read ',info ',(gnus-info-read info))
5169              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5170              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5171              (gnus-group-update-group ,group t))))
5172       ;; Add the read articles to the range.
5173       (gnus-info-set-read info range)
5174       (gnus-request-set-mark group (list (list range 'add '(read))))
5175       ;; Then we have to re-compute how many unread
5176       ;; articles there are in this group.
5177       (when active
5178         (cond
5179          ((not range)
5180           (setq num (- (1+ (cdr active)) (car active))))
5181          ((not (listp (cdr range)))
5182           (setq num (- (cdr active) (- (1+ (cdr range))
5183                                        (car range)))))
5184          (t
5185           (while range
5186             (if (numberp (car range))
5187                 (setq num (1+ num))
5188               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5189             (setq range (cdr range)))
5190           (setq num (- (cdr active) num))))
5191         ;; Update the number of unread articles.
5192         (setcar entry num)
5193         ;; Update the group buffer.
5194         (gnus-group-update-group group t)))))
5195
5196 (defvar gnus-newsgroup-none-id 0)
5197
5198 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5199   (let ((cur nntp-server-buffer)
5200         (dependencies
5201          (or dependencies
5202              (save-excursion (set-buffer gnus-summary-buffer)
5203                              gnus-newsgroup-dependencies)))
5204         headers id end ref
5205         (mail-parse-charset gnus-newsgroup-charset)
5206         (mail-parse-ignored-charsets
5207          (save-excursion (condition-case nil
5208                              (set-buffer gnus-summary-buffer)
5209                            (error))
5210                          gnus-newsgroup-ignored-charsets)))
5211     (save-excursion
5212       (set-buffer nntp-server-buffer)
5213       ;; Translate all TAB characters into SPACE characters.
5214       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5215       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5216       (gnus-run-hooks 'gnus-parse-headers-hook)
5217       (let ((case-fold-search t)
5218             in-reply-to header p lines chars)
5219         (goto-char (point-min))
5220         ;; Search to the beginning of the next header.  Error messages
5221         ;; do not begin with 2 or 3.
5222         (while (re-search-forward "^[23][0-9]+ " nil t)
5223           (setq id nil
5224                 ref nil)
5225           ;; This implementation of this function, with nine
5226           ;; search-forwards instead of the one re-search-forward and
5227           ;; a case (which basically was the old function) is actually
5228           ;; about twice as fast, even though it looks messier.  You
5229           ;; can't have everything, I guess.  Speed and elegance
5230           ;; doesn't always go hand in hand.
5231           (setq
5232            header
5233            (vector
5234             ;; Number.
5235             (prog1
5236                 (read cur)
5237               (end-of-line)
5238               (setq p (point))
5239               (narrow-to-region (point)
5240                                 (or (and (search-forward "\n.\n" nil t)
5241                                          (- (point) 2))
5242                                     (point))))
5243             ;; Subject.
5244             (progn
5245               (goto-char p)
5246               (if (search-forward "\nsubject:" nil t)
5247                   (funcall gnus-decode-encoded-word-function
5248                            (nnheader-header-value))
5249                 "(none)"))
5250             ;; From.
5251             (progn
5252               (goto-char p)
5253               (if (search-forward "\nfrom:" nil t)
5254                   (funcall gnus-decode-encoded-word-function
5255                            (nnheader-header-value))
5256                 "(nobody)"))
5257             ;; Date.
5258             (progn
5259               (goto-char p)
5260               (if (search-forward "\ndate:" nil t)
5261                   (nnheader-header-value) ""))
5262             ;; Message-ID.
5263             (progn
5264               (goto-char p)
5265               (setq id (if (re-search-forward
5266                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5267                       ;; We do it this way to make sure the Message-ID
5268                            ;; is (somewhat) syntactically valid.
5269                            (buffer-substring (match-beginning 1)
5270                                              (match-end 1))
5271                        ;; If there was no message-id, we just fake one
5272                          ;; to make subsequent routines simpler.
5273                          (nnheader-generate-fake-message-id))))
5274             ;; References.
5275             (progn
5276               (goto-char p)
5277               (if (search-forward "\nreferences:" nil t)
5278                   (progn
5279                     (setq end (point))
5280                     (prog1
5281                         (nnheader-header-value)
5282                       (setq ref
5283                             (buffer-substring
5284                              (progn
5285                                (end-of-line)
5286                                (search-backward ">" end t)
5287                                (1+ (point)))
5288                              (progn
5289                                (search-backward "<" end t)
5290                                (point))))))
5291             ;; Get the references from the in-reply-to header if there
5292                 ;; were no references and the in-reply-to header looks
5293                 ;; promising.
5294                 (if (and (search-forward "\nin-reply-to:" nil t)
5295                          (setq in-reply-to (nnheader-header-value))
5296                          (string-match "<[^>]+>" in-reply-to))
5297                     (let (ref2)
5298                       (setq ref (substring in-reply-to (match-beginning 0)
5299                                            (match-end 0)))
5300                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5301                         (setq ref2 (substring in-reply-to (match-beginning 0)
5302                                               (match-end 0)))
5303                         (when (> (length ref2) (length ref))
5304                           (setq ref ref2)))
5305                       ref)
5306                   (setq ref nil))))
5307             ;; Chars.
5308             (progn
5309               (goto-char p)
5310               (if (search-forward "\nchars: " nil t)
5311                   (if (numberp (setq chars (ignore-errors (read cur))))
5312                       chars -1)
5313                 -1))
5314             ;; Lines.
5315             (progn
5316               (goto-char p)
5317               (if (search-forward "\nlines: " nil t)
5318                   (if (numberp (setq lines (ignore-errors (read cur))))
5319                       lines -1)
5320                 -1))
5321             ;; Xref.
5322             (progn
5323               (goto-char p)
5324               (and (search-forward "\nxref:" nil t)
5325                    (nnheader-header-value)))
5326             ;; Extra.
5327             (when gnus-extra-headers
5328               (let ((extra gnus-extra-headers)
5329                     out)
5330                 (while extra
5331                   (goto-char p)
5332                   (when (search-forward
5333                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5334                     (push (cons (car extra) (nnheader-header-value))
5335                           out))
5336                   (pop extra))
5337                 out))))
5338           (when (equal id ref)
5339             (setq ref nil))
5340
5341           (when gnus-alter-header-function
5342             (funcall gnus-alter-header-function header)
5343             (setq id (mail-header-id header)
5344                   ref (gnus-parent-id (mail-header-references header))))
5345
5346           (when (setq header
5347                       (gnus-dependencies-add-header
5348                        header dependencies force-new))
5349             (push header headers))
5350           (goto-char (point-max))
5351           (widen))
5352         (nreverse headers)))))
5353
5354 ;; Goes through the xover lines and returns a list of vectors
5355 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5356                                                   force-new dependencies
5357                                                   group also-fetch-heads)
5358   "Parse the news overview data in the server buffer.
5359 Return a list of headers that match SEQUENCE (see
5360 `nntp-retrieve-headers')."
5361   ;; Get the Xref when the users reads the articles since most/some
5362   ;; NNTP servers do not include Xrefs when using XOVER.
5363   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5364   (let ((mail-parse-charset gnus-newsgroup-charset)
5365         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5366         (cur nntp-server-buffer)
5367         (dependencies (or dependencies gnus-newsgroup-dependencies))
5368         (allp (cond
5369                ((eq gnus-read-all-available-headers t)
5370                 t)
5371                ((stringp gnus-read-all-available-headers)
5372                 (string-match gnus-read-all-available-headers group))
5373                (t
5374                 nil)))
5375         number headers header)
5376     (save-excursion
5377       (set-buffer nntp-server-buffer)
5378       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5379       ;; Allow the user to mangle the headers before parsing them.
5380       (gnus-run-hooks 'gnus-parse-headers-hook)
5381       (goto-char (point-min))
5382       (while (not (eobp))
5383         (condition-case ()
5384             (while (and (or sequence allp)
5385                         (not (eobp)))
5386               (setq number (read cur))
5387               (when (not allp)
5388                 (while (and sequence
5389                             (< (car sequence) number))
5390                   (setq sequence (cdr sequence))))
5391               (when (and (or allp
5392                              (and sequence
5393                                   (eq number (car sequence))))
5394                          (progn
5395                            (setq sequence (cdr sequence))
5396                            (setq header (inline
5397                                           (gnus-nov-parse-line
5398                                            number dependencies force-new)))))
5399                 (push header headers))
5400               (forward-line 1))
5401           (error
5402            (gnus-error 4 "Strange nov line (%d)"
5403                        (count-lines (point-min) (point)))))
5404         (forward-line 1))
5405       ;; A common bug in inn is that if you have posted an article and
5406       ;; then retrieves the active file, it will answer correctly --
5407       ;; the new article is included.  However, a NOV entry for the
5408       ;; article may not have been generated yet, so this may fail.
5409       ;; We work around this problem by retrieving the last few
5410       ;; headers using HEAD.
5411       (if (or (not also-fetch-heads)
5412               (not sequence))
5413           ;; We (probably) got all the headers.
5414           (nreverse headers)
5415         (let ((gnus-nov-is-evil t))
5416           (nconc
5417            (nreverse headers)
5418            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5419              (gnus-get-newsgroup-headers))))))))
5420
5421 (defun gnus-article-get-xrefs ()
5422   "Fill in the Xref value in `gnus-current-headers', if necessary.
5423 This is meant to be called in `gnus-article-internal-prepare-hook'."
5424   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5425                                  gnus-current-headers)))
5426     (or (not gnus-use-cross-reference)
5427         (not headers)
5428         (and (mail-header-xref headers)
5429              (not (string= (mail-header-xref headers) "")))
5430         (let ((case-fold-search t)
5431               xref)
5432           (save-restriction
5433             (nnheader-narrow-to-headers)
5434             (goto-char (point-min))
5435             (when (or (and (not (eobp))
5436                            (eq (downcase (char-after)) ?x)
5437                            (looking-at "Xref:"))
5438                       (search-forward "\nXref:" nil t))
5439               (goto-char (1+ (match-end 0)))
5440               (setq xref (buffer-substring (point)
5441                                            (progn (end-of-line) (point))))
5442               (mail-header-set-xref headers xref)))))))
5443
5444 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5445   "Find article ID and insert the summary line for that article.
5446 OLD-HEADER can either be a header or a line number to insert
5447 the subject line on."
5448   (let* ((line (and (numberp old-header) old-header))
5449          (old-header (and (vectorp old-header) old-header))
5450          (header (cond ((and old-header use-old-header)
5451                         old-header)
5452                        ((and (numberp id)
5453                              (gnus-number-to-header id))
5454                         (gnus-number-to-header id))
5455                        (t
5456                         (gnus-read-header id))))
5457          (number (and (numberp id) id))
5458          d)
5459     (when header
5460       ;; Rebuild the thread that this article is part of and go to the
5461       ;; article we have fetched.
5462       (when (and (not gnus-show-threads)
5463                  old-header)
5464         (when (and number
5465                    (setq d (gnus-data-find (mail-header-number old-header))))
5466           (goto-char (gnus-data-pos d))
5467           (gnus-data-remove
5468            number
5469            (- (gnus-point-at-bol)
5470               (prog1
5471                   (1+ (gnus-point-at-eol))
5472                 (gnus-delete-line))))))
5473       (when old-header
5474         (mail-header-set-number header (mail-header-number old-header)))
5475       (setq gnus-newsgroup-sparse
5476             (delq (setq number (mail-header-number header))
5477                   gnus-newsgroup-sparse))
5478       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5479       (push number gnus-newsgroup-limit)
5480       (gnus-rebuild-thread (mail-header-id header) line)
5481       (gnus-summary-goto-subject number nil t))
5482     (when (and (numberp number)
5483                (> number 0))
5484       ;; We have to update the boundaries even if we can't fetch the
5485       ;; article if ID is a number -- so that the next `P' or `N'
5486       ;; command will fetch the previous (or next) article even
5487       ;; if the one we tried to fetch this time has been canceled.
5488       (when (> number gnus-newsgroup-end)
5489         (setq gnus-newsgroup-end number))
5490       (when (< number gnus-newsgroup-begin)
5491         (setq gnus-newsgroup-begin number))
5492       (setq gnus-newsgroup-unselected
5493             (delq number gnus-newsgroup-unselected)))
5494     ;; Report back a success?
5495     (and header (mail-header-number header))))
5496
5497 ;;; Process/prefix in the summary buffer
5498
5499 (defun gnus-summary-work-articles (n)
5500   "Return a list of articles to be worked upon.
5501 The prefix argument, the list of process marked articles, and the
5502 current article will be taken into consideration."
5503   (save-excursion
5504     (set-buffer gnus-summary-buffer)
5505     (cond
5506      (n
5507       ;; A numerical prefix has been given.
5508       (setq n (prefix-numeric-value n))
5509       (let ((backward (< n 0))
5510             (n (abs (prefix-numeric-value n)))
5511             articles article)
5512         (save-excursion
5513           (while
5514               (and (> n 0)
5515                    (push (setq article (gnus-summary-article-number))
5516                          articles)
5517                    (if backward
5518                        (gnus-summary-find-prev nil article)
5519                      (gnus-summary-find-next nil article)))
5520             (decf n)))
5521         (nreverse articles)))
5522      ((and (gnus-region-active-p) (mark))
5523       (message "region active")
5524       ;; Work on the region between point and mark.
5525       (let ((max (max (point) (mark)))
5526             articles article)
5527         (save-excursion
5528           (goto-char (min (min (point) (mark))))
5529           (while
5530               (and
5531                (push (setq article (gnus-summary-article-number)) articles)
5532                (gnus-summary-find-next nil article)
5533                (< (point) max)))
5534           (nreverse articles))))
5535      (gnus-newsgroup-processable
5536       ;; There are process-marked articles present.
5537       ;; Save current state.
5538       (gnus-summary-save-process-mark)
5539       ;; Return the list.
5540       (reverse gnus-newsgroup-processable))
5541      (t
5542       ;; Just return the current article.
5543       (list (gnus-summary-article-number))))))
5544
5545 (defmacro gnus-summary-iterate (arg &rest forms)
5546   "Iterate over the process/prefixed articles and do FORMS.
5547 ARG is the interactive prefix given to the command.  FORMS will be
5548 executed with point over the summary line of the articles."
5549   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5550     `(let ((,articles (gnus-summary-work-articles ,arg)))
5551        (while ,articles
5552          (gnus-summary-goto-subject (car ,articles))
5553          ,@forms
5554          (pop ,articles)))))
5555
5556 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5557 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5558
5559 (defun gnus-summary-save-process-mark ()
5560   "Push the current set of process marked articles on the stack."
5561   (interactive)
5562   (push (copy-sequence gnus-newsgroup-processable)
5563         gnus-newsgroup-process-stack))
5564
5565 (defun gnus-summary-kill-process-mark ()
5566   "Push the current set of process marked articles on the stack and unmark."
5567   (interactive)
5568   (gnus-summary-save-process-mark)
5569   (gnus-summary-unmark-all-processable))
5570
5571 (defun gnus-summary-yank-process-mark ()
5572   "Pop the last process mark state off the stack and restore it."
5573   (interactive)
5574   (unless gnus-newsgroup-process-stack
5575     (error "Empty mark stack"))
5576   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5577
5578 (defun gnus-summary-process-mark-set (set)
5579   "Make SET into the current process marked articles."
5580   (gnus-summary-unmark-all-processable)
5581   (while set
5582     (gnus-summary-set-process-mark (pop set))))
5583
5584 ;;; Searching and stuff
5585
5586 (defun gnus-summary-search-group (&optional backward use-level)
5587   "Search for next unread newsgroup.
5588 If optional argument BACKWARD is non-nil, search backward instead."
5589   (save-excursion
5590     (set-buffer gnus-group-buffer)
5591     (when (gnus-group-search-forward
5592            backward nil (if use-level (gnus-group-group-level) nil))
5593       (gnus-group-group-name))))
5594
5595 (defun gnus-summary-best-group (&optional exclude-group)
5596   "Find the name of the best unread group.
5597 If EXCLUDE-GROUP, do not go to this group."
5598   (save-excursion
5599     (set-buffer gnus-group-buffer)
5600     (save-excursion
5601       (gnus-group-best-unread-group exclude-group))))
5602
5603 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5604   (if backward (gnus-summary-find-prev)
5605     (let* ((dummy (gnus-summary-article-intangible-p))
5606            (article (or article (gnus-summary-article-number)))
5607            (arts (gnus-data-find-list article))
5608            result)
5609       (when (and (not dummy)
5610                  (or (not gnus-summary-check-current)
5611                      (not unread)
5612                      (not (gnus-data-unread-p (car arts)))))
5613         (setq arts (cdr arts)))
5614       (when (setq result
5615                   (if unread
5616                       (progn
5617                         (while arts
5618                           (when (or (and undownloaded
5619                                          (eq gnus-undownloaded-mark
5620                                              (gnus-data-mark (car arts))))
5621                                     (gnus-data-unread-p (car arts)))
5622                             (setq result (car arts)
5623                                   arts nil))
5624                           (setq arts (cdr arts)))
5625                         result)
5626                     (car arts)))
5627         (goto-char (gnus-data-pos result))
5628         (gnus-data-number result)))))
5629
5630 (defun gnus-summary-find-prev (&optional unread article)
5631   (let* ((eobp (eobp))
5632          (article (or article (gnus-summary-article-number)))
5633          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5634          result)
5635     (when (and (not eobp)
5636                (or (not gnus-summary-check-current)
5637                    (not unread)
5638                    (not (gnus-data-unread-p (car arts)))))
5639       (setq arts (cdr arts)))
5640     (when (setq result
5641                 (if unread
5642                     (progn
5643                       (while arts
5644                         (when (gnus-data-unread-p (car arts))
5645                           (setq result (car arts)
5646                                 arts nil))
5647                         (setq arts (cdr arts)))
5648                       result)
5649                   (car arts)))
5650       (goto-char (gnus-data-pos result))
5651       (gnus-data-number result))))
5652
5653 (defun gnus-summary-find-subject (subject &optional unread backward article)
5654   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5655          (article (or article (gnus-summary-article-number)))
5656          (articles (gnus-data-list backward))
5657          (arts (gnus-data-find-list article articles))
5658          result)
5659     (when (or (not gnus-summary-check-current)
5660               (not unread)
5661               (not (gnus-data-unread-p (car arts))))
5662       (setq arts (cdr arts)))
5663     (while arts
5664       (and (or (not unread)
5665                (gnus-data-unread-p (car arts)))
5666            (vectorp (gnus-data-header (car arts)))
5667            (gnus-subject-equal
5668             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5669            (setq result (car arts)
5670                  arts nil))
5671       (setq arts (cdr arts)))
5672     (and result
5673          (goto-char (gnus-data-pos result))
5674          (gnus-data-number result))))
5675
5676 (defun gnus-summary-search-forward (&optional unread subject backward)
5677   "Search forward for an article.
5678 If UNREAD, look for unread articles.  If SUBJECT, look for
5679 articles with that subject.  If BACKWARD, search backward instead."
5680   (cond (subject (gnus-summary-find-subject subject unread backward))
5681         (backward (gnus-summary-find-prev unread))
5682         (t (gnus-summary-find-next unread))))
5683
5684 (defun gnus-recenter (&optional n)
5685   "Center point in window and redisplay frame.
5686 Also do horizontal recentering."
5687   (interactive "P")
5688   (when (and gnus-auto-center-summary
5689              (not (eq gnus-auto-center-summary 'vertical)))
5690     (gnus-horizontal-recenter))
5691   (recenter n))
5692
5693 (defun gnus-summary-recenter ()
5694   "Center point in the summary window.
5695 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5696 displayed, no centering will be performed."
5697   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5698 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5699   (interactive)
5700   (let* ((top (cond ((< (window-height) 4) 0)
5701                     ((< (window-height) 7) 1)
5702                     (t (if (numberp gnus-auto-center-summary)
5703                            gnus-auto-center-summary
5704                          2))))
5705          (height (1- (window-height)))
5706          (bottom (save-excursion (goto-char (point-max))
5707                                  (forward-line (- height))
5708                                  (point)))
5709          (window (get-buffer-window (current-buffer))))
5710     ;; The user has to want it.
5711     (when gnus-auto-center-summary
5712       (when (get-buffer-window gnus-article-buffer)
5713         ;; Only do recentering when the article buffer is displayed,
5714       ;; Set the window start to either `bottom', which is the biggest
5715         ;; possible valid number, or the second line from the top,
5716         ;; whichever is the least.
5717         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5718           (if (> bottom top-pos)
5719               ;; Keep the second line from the top visible
5720               (set-window-start window top-pos t)
5721             ;; Try to keep the bottom line visible; if it's partially
5722             ;; obscured, either scroll one more line to make it fully
5723             ;; visible, or revert to using TOP-POS.
5724             (save-excursion
5725               (goto-char (point-max))
5726               (forward-line -1)
5727               (let ((last-line-start (point)))
5728                 (goto-char bottom)
5729                 (set-window-start window (point) t)
5730                 (when (not (pos-visible-in-window-p last-line-start window))
5731                   (forward-line 1)
5732                   (set-window-start window (min (point) top-pos) t)))))))
5733       ;; Do horizontal recentering while we're at it.
5734       (when (and (get-buffer-window (current-buffer) t)
5735                  (not (eq gnus-auto-center-summary 'vertical)))
5736         (let ((selected (selected-window)))
5737           (select-window (get-buffer-window (current-buffer) t))
5738           (gnus-summary-position-point)
5739           (gnus-horizontal-recenter)
5740           (select-window selected))))))
5741
5742 (defun gnus-summary-jump-to-group (newsgroup)
5743   "Move point to NEWSGROUP in group mode buffer."
5744   ;; Keep update point of group mode buffer if visible.
5745   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5746       (save-window-excursion
5747         ;; Take care of tree window mode.
5748         (when (get-buffer-window gnus-group-buffer)
5749           (pop-to-buffer gnus-group-buffer))
5750         (gnus-group-jump-to-group newsgroup))
5751     (save-excursion
5752       ;; Take care of tree window mode.
5753       (if (get-buffer-window gnus-group-buffer)
5754           (pop-to-buffer gnus-group-buffer)
5755         (set-buffer gnus-group-buffer))
5756       (gnus-group-jump-to-group newsgroup))))
5757
5758 ;; This function returns a list of article numbers based on the
5759 ;; difference between the ranges of read articles in this group and
5760 ;; the range of active articles.
5761 (defun gnus-list-of-unread-articles (group)
5762   (let* ((read (gnus-info-read (gnus-get-info group)))
5763          (active (or (gnus-active group) (gnus-activate-group group)))
5764          (last (cdr active))
5765          first nlast unread)
5766     ;; If none are read, then all are unread.
5767     (if (not read)
5768         (setq first (car active))
5769       ;; If the range of read articles is a single range, then the
5770       ;; first unread article is the article after the last read
5771       ;; article.  Sounds logical, doesn't it?
5772       (if (and (not (listp (cdr read)))
5773                (or (< (car read) (car active))
5774                    (progn (setq read (list read))
5775                           nil)))
5776           (setq first (max (car active) (1+ (cdr read))))
5777         ;; `read' is a list of ranges.
5778         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5779                                   (caar read)))
5780                   1)
5781           (setq first (car active)))
5782         (while read
5783           (when first
5784             (while (< first nlast)
5785               (push first unread)
5786               (setq first (1+ first))))
5787           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5788           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5789           (setq read (cdr read)))))
5790     ;; And add the last unread articles.
5791     (while (<= first last)
5792       (push first unread)
5793       (setq first (1+ first)))
5794     ;; Return the list of unread articles.
5795     (delq 0 (nreverse unread))))
5796
5797 (defun gnus-list-of-read-articles (group)
5798   "Return a list of unread, unticked and non-dormant articles."
5799   (let* ((info (gnus-get-info group))
5800          (marked (gnus-info-marks info))
5801          (active (gnus-active group)))
5802     (and info active
5803          (gnus-set-difference
5804           (gnus-sorted-complement
5805            (gnus-uncompress-range active)
5806            (gnus-list-of-unread-articles group))
5807           (append
5808            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5809            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5810
5811 ;; Various summary commands
5812
5813 (defun gnus-summary-select-article-buffer ()
5814   "Reconfigure windows to show article buffer."
5815   (interactive)
5816   (if (not (gnus-buffer-live-p gnus-article-buffer))
5817       (error "There is no article buffer for this summary buffer")
5818     (gnus-configure-windows 'article)
5819     (select-window (get-buffer-window gnus-article-buffer))))
5820
5821 (defun gnus-summary-universal-argument (arg)
5822   "Perform any operation on all articles that are process/prefixed."
5823   (interactive "P")
5824   (let ((articles (gnus-summary-work-articles arg))
5825         func article)
5826     (if (eq
5827          (setq
5828           func
5829           (key-binding
5830            (read-key-sequence
5831             (substitute-command-keys
5832              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5833          'undefined)
5834         (gnus-error 1 "Undefined key")
5835       (save-excursion
5836         (while articles
5837           (gnus-summary-goto-subject (setq article (pop articles)))
5838           (let (gnus-newsgroup-processable)
5839             (command-execute func))
5840           (gnus-summary-remove-process-mark article)))))
5841   (gnus-summary-position-point))
5842
5843 (defun gnus-summary-toggle-truncation (&optional arg)
5844   "Toggle truncation of summary lines.
5845 With arg, turn line truncation on iff arg is positive."
5846   (interactive "P")
5847   (setq truncate-lines
5848         (if (null arg) (not truncate-lines)
5849           (> (prefix-numeric-value arg) 0)))
5850   (redraw-display))
5851
5852 (defun gnus-summary-reselect-current-group (&optional all rescan)
5853   "Exit and then reselect the current newsgroup.
5854 The prefix argument ALL means to select all articles."
5855   (interactive "P")
5856   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5857     (error "Ephemeral groups can't be reselected"))
5858   (let ((current-subject (gnus-summary-article-number))
5859         (group gnus-newsgroup-name))
5860     (setq gnus-newsgroup-begin nil)
5861     (gnus-summary-exit)
5862     ;; We have to adjust the point of group mode buffer because
5863     ;; point was moved to the next unread newsgroup by exiting.
5864     (gnus-summary-jump-to-group group)
5865     (when rescan
5866       (save-excursion
5867         (gnus-group-get-new-news-this-group 1)))
5868     (gnus-group-read-group all t)
5869     (gnus-summary-goto-subject current-subject nil t)))
5870
5871 (defun gnus-summary-rescan-group (&optional all)
5872   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5873   (interactive "P")
5874   (gnus-summary-reselect-current-group all t))
5875
5876 (defun gnus-summary-update-info (&optional non-destructive)
5877   (save-excursion
5878     (let ((group gnus-newsgroup-name))
5879       (when group
5880         (when gnus-newsgroup-kill-headers
5881           (setq gnus-newsgroup-killed
5882                 (gnus-compress-sequence
5883                  (nconc
5884                   (gnus-set-sorted-intersection
5885                    (gnus-uncompress-range gnus-newsgroup-killed)
5886                    (setq gnus-newsgroup-unselected
5887                          (sort gnus-newsgroup-unselected '<)))
5888                   (setq gnus-newsgroup-unreads
5889                         (sort gnus-newsgroup-unreads '<)))
5890                  t)))
5891         (unless (listp (cdr gnus-newsgroup-killed))
5892           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5893         (let ((headers gnus-newsgroup-headers))
5894           ;; Set the new ranges of read articles.
5895           (save-excursion
5896             (set-buffer gnus-group-buffer)
5897             (gnus-undo-force-boundary))
5898           (gnus-update-read-articles
5899            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5900           ;; Set the current article marks.
5901           (let ((gnus-newsgroup-scored
5902                  (if (and (not gnus-save-score)
5903                           (not non-destructive))
5904                      nil
5905                    gnus-newsgroup-scored)))
5906             (save-excursion
5907               (gnus-update-marks)))
5908           ;; Do the cross-ref thing.
5909           (when gnus-use-cross-reference
5910             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5911           ;; Do not switch windows but change the buffer to work.
5912           (set-buffer gnus-group-buffer)
5913           (unless (gnus-ephemeral-group-p group)
5914             (gnus-group-update-group group)))))))
5915
5916 (defun gnus-summary-save-newsrc (&optional force)
5917   "Save the current number of read/marked articles in the dribble buffer.
5918 The dribble buffer will then be saved.
5919 If FORCE (the prefix), also save the .newsrc file(s)."
5920   (interactive "P")
5921   (gnus-summary-update-info t)
5922   (if force
5923       (gnus-save-newsrc-file)
5924     (gnus-dribble-save)))
5925
5926 (defun gnus-summary-exit (&optional temporary)
5927   "Exit reading current newsgroup, and then return to group selection mode.
5928 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5929   (interactive)
5930   (gnus-set-global-variables)
5931   (when (gnus-buffer-live-p gnus-article-buffer)
5932     (save-excursion
5933       (set-buffer gnus-article-buffer)
5934       (mm-destroy-parts gnus-article-mime-handles)
5935       ;; Set it to nil for safety reason.
5936       (setq gnus-article-mime-handle-alist nil)
5937       (setq gnus-article-mime-handles nil)))
5938   (gnus-kill-save-kill-buffer)
5939   (gnus-async-halt-prefetch)
5940   (let* ((group gnus-newsgroup-name)
5941          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5942          (mode major-mode)
5943          (group-point nil)
5944          (buf (current-buffer)))
5945     (unless quit-config
5946       ;; Do adaptive scoring, and possibly save score files.
5947       (when gnus-newsgroup-adaptive
5948         (gnus-score-adaptive))
5949       (when gnus-use-scoring
5950         (gnus-score-save)))
5951     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5952     ;; If we have several article buffers, we kill them at exit.
5953     (unless gnus-single-article-buffer
5954       (gnus-kill-buffer gnus-original-article-buffer)
5955       (setq gnus-article-current nil))
5956     (when gnus-use-cache
5957       (gnus-cache-possibly-remove-articles)
5958       (gnus-cache-save-buffers))
5959     (gnus-async-prefetch-remove-group group)
5960     (when gnus-suppress-duplicates
5961       (gnus-dup-enter-articles))
5962     (when gnus-use-trees
5963       (gnus-tree-close group))
5964     (when gnus-use-cache
5965       (gnus-cache-write-active))
5966     ;; Remove entries for this group.
5967     (nnmail-purge-split-history (gnus-group-real-name group))
5968     ;; Make all changes in this group permanent.
5969     (unless quit-config
5970       (gnus-run-hooks 'gnus-exit-group-hook)
5971       (gnus-summary-update-info))
5972     (gnus-close-group group)
5973     ;; Make sure where we were, and go to next newsgroup.
5974     (set-buffer gnus-group-buffer)
5975     (unless quit-config
5976       (gnus-group-jump-to-group group))
5977     (gnus-run-hooks 'gnus-summary-exit-hook)
5978     (unless (or quit-config
5979                 ;; If this group has disappeared from the summary
5980                 ;; buffer, don't skip forwards.
5981                 (not (string= group (gnus-group-group-name))))
5982       (gnus-group-next-unread-group 1))
5983     (setq group-point (point))
5984     (if temporary
5985         nil                             ;Nothing to do.
5986       ;; If we have several article buffers, we kill them at exit.
5987       (unless gnus-single-article-buffer
5988         (gnus-kill-buffer gnus-article-buffer)
5989         (gnus-kill-buffer gnus-original-article-buffer)
5990         (setq gnus-article-current nil))
5991       (set-buffer buf)
5992       (if (not gnus-kill-summary-on-exit)
5993           (progn
5994             (gnus-deaden-summary)
5995             (setq mode nil))
5996        ;; We set all buffer-local variables to nil.  It is unclear why
5997         ;; this is needed, but if we don't, buffer-local variables are
5998         ;; not garbage-collected, it seems.  This would the lead to en
5999         ;; ever-growing Emacs.
6000         (gnus-summary-clear-local-variables)
6001         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6002           (gnus-summary-clear-local-variables))
6003         (when (get-buffer gnus-article-buffer)
6004           (bury-buffer gnus-article-buffer))
6005         ;; We clear the global counterparts of the buffer-local
6006         ;; variables as well, just to be on the safe side.
6007         (set-buffer gnus-group-buffer)
6008         (gnus-summary-clear-local-variables)
6009         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6010           (gnus-summary-clear-local-variables)))
6011       (setq gnus-current-select-method gnus-select-method)
6012       (pop-to-buffer gnus-group-buffer)
6013       (if (not quit-config)
6014           (progn
6015             (goto-char group-point)
6016             (gnus-configure-windows 'group 'force))
6017         (gnus-handle-ephemeral-exit quit-config))
6018       ;; Return to group mode buffer.
6019       (when (eq mode 'gnus-summary-mode)
6020         (gnus-kill-buffer buf))
6021       ;; Clear the current group name.
6022       (unless quit-config
6023         (setq gnus-newsgroup-name nil)))))
6024
6025 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6026 (defun gnus-summary-exit-no-update (&optional no-questions)
6027   "Quit reading current newsgroup without updating read article info."
6028   (interactive)
6029   (let* ((group gnus-newsgroup-name)
6030          (quit-config (gnus-group-quit-config group)))
6031     (when (or no-questions
6032               gnus-expert-user
6033               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6034       (gnus-async-halt-prefetch)
6035       (mapcar 'funcall
6036               (delq 'gnus-summary-expire-articles
6037                     (copy-sequence gnus-summary-prepare-exit-hook)))
6038       (when (gnus-buffer-live-p gnus-article-buffer)
6039         (save-excursion
6040           (set-buffer gnus-article-buffer)
6041           (mm-destroy-parts gnus-article-mime-handles)
6042           ;; Set it to nil for safety reason.
6043           (setq gnus-article-mime-handle-alist nil)
6044           (setq gnus-article-mime-handles nil)))
6045       ;; If we have several article buffers, we kill them at exit.
6046       (unless gnus-single-article-buffer
6047         (gnus-kill-buffer gnus-article-buffer)
6048         (gnus-kill-buffer gnus-original-article-buffer)
6049         (setq gnus-article-current nil))
6050       (if (not gnus-kill-summary-on-exit)
6051           (gnus-deaden-summary)
6052         (gnus-close-group group)
6053         (gnus-summary-clear-local-variables)
6054         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6055           (gnus-summary-clear-local-variables))
6056         (set-buffer gnus-group-buffer)
6057         (gnus-summary-clear-local-variables)
6058         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6059           (gnus-summary-clear-local-variables))
6060         (when (get-buffer gnus-summary-buffer)
6061           (kill-buffer gnus-summary-buffer)))
6062       (unless gnus-single-article-buffer
6063         (setq gnus-article-current nil))
6064       (when gnus-use-trees
6065         (gnus-tree-close group))
6066       (gnus-async-prefetch-remove-group group)
6067       (when (get-buffer gnus-article-buffer)
6068         (bury-buffer gnus-article-buffer))
6069       ;; Return to the group buffer.
6070       (gnus-configure-windows 'group 'force)
6071       ;; Clear the current group name.
6072       (setq gnus-newsgroup-name nil)
6073       (when (equal (gnus-group-group-name) group)
6074         (gnus-group-next-unread-group 1))
6075       (when quit-config
6076         (gnus-handle-ephemeral-exit quit-config)))))
6077
6078 (defun gnus-handle-ephemeral-exit (quit-config)
6079   "Handle movement when leaving an ephemeral group.
6080 The state which existed when entering the ephemeral is reset."
6081   (if (not (buffer-name (car quit-config)))
6082       (gnus-configure-windows 'group 'force)
6083     (set-buffer (car quit-config))
6084     (cond ((eq major-mode 'gnus-summary-mode)
6085            (gnus-set-global-variables))
6086           ((eq major-mode 'gnus-article-mode)
6087            (save-excursion
6088              ;; The `gnus-summary-buffer' variable may point
6089              ;; to the old summary buffer when using a single
6090              ;; article buffer.
6091              (unless (gnus-buffer-live-p gnus-summary-buffer)
6092                (set-buffer gnus-group-buffer))
6093              (set-buffer gnus-summary-buffer)
6094              (gnus-set-global-variables))))
6095     (if (or (eq (cdr quit-config) 'article)
6096             (eq (cdr quit-config) 'pick))
6097         (progn
6098           ;; The current article may be from the ephemeral group
6099           ;; thus it is best that we reload this article
6100           (gnus-summary-show-article)
6101           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6102               (gnus-configure-windows 'pick 'force)
6103             (gnus-configure-windows (cdr quit-config) 'force)))
6104       (gnus-configure-windows (cdr quit-config) 'force))
6105     (when (eq major-mode 'gnus-summary-mode)
6106       (gnus-summary-next-subject 1 nil t)
6107       (gnus-summary-recenter)
6108       (gnus-summary-position-point))))
6109
6110 ;;; Dead summaries.
6111
6112 (defvar gnus-dead-summary-mode-map nil)
6113
6114 (unless gnus-dead-summary-mode-map
6115   (setq gnus-dead-summary-mode-map (make-keymap))
6116   (suppress-keymap gnus-dead-summary-mode-map)
6117   (substitute-key-definition
6118    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6119   (let ((keys '("\C-d" "\r" "\177" [delete])))
6120     (while keys
6121       (define-key gnus-dead-summary-mode-map
6122         (pop keys) 'gnus-summary-wake-up-the-dead))))
6123
6124 (defvar gnus-dead-summary-mode nil
6125   "Minor mode for Gnus summary buffers.")
6126
6127 (defun gnus-dead-summary-mode (&optional arg)
6128   "Minor mode for Gnus summary buffers."
6129   (interactive "P")
6130   (when (eq major-mode 'gnus-summary-mode)
6131     (make-local-variable 'gnus-dead-summary-mode)
6132     (setq gnus-dead-summary-mode
6133           (if (null arg) (not gnus-dead-summary-mode)
6134             (> (prefix-numeric-value arg) 0)))
6135     (when gnus-dead-summary-mode
6136       (gnus-add-minor-mode
6137        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6138
6139 (defun gnus-deaden-summary ()
6140   "Make the current summary buffer into a dead summary buffer."
6141   ;; Kill any previous dead summary buffer.
6142   (when (and gnus-dead-summary
6143              (buffer-name gnus-dead-summary))
6144     (save-excursion
6145       (set-buffer gnus-dead-summary)
6146       (when gnus-dead-summary-mode
6147         (kill-buffer (current-buffer)))))
6148   ;; Make this the current dead summary.
6149   (setq gnus-dead-summary (current-buffer))
6150   (gnus-dead-summary-mode 1)
6151   (let ((name (buffer-name)))
6152     (when (string-match "Summary" name)
6153       (rename-buffer
6154        (concat (substring name 0 (match-beginning 0)) "Dead "
6155                (substring name (match-beginning 0)))
6156        t)
6157       (bury-buffer))))
6158
6159 (defun gnus-kill-or-deaden-summary (buffer)
6160   "Kill or deaden the summary BUFFER."
6161   (save-excursion
6162     (when (and (buffer-name buffer)
6163                (not gnus-single-article-buffer))
6164       (save-excursion
6165         (set-buffer buffer)
6166         (gnus-kill-buffer gnus-article-buffer)
6167         (gnus-kill-buffer gnus-original-article-buffer)))
6168     (cond (gnus-kill-summary-on-exit
6169            (when (and gnus-use-trees
6170                       (gnus-buffer-exists-p buffer))
6171              (save-excursion
6172                (set-buffer buffer)
6173                (gnus-tree-close gnus-newsgroup-name)))
6174            (gnus-kill-buffer buffer))
6175           ((gnus-buffer-exists-p buffer)
6176            (save-excursion
6177              (set-buffer buffer)
6178              (gnus-deaden-summary))))))
6179
6180 (defun gnus-summary-wake-up-the-dead (&rest args)
6181   "Wake up the dead summary buffer."
6182   (interactive)
6183   (gnus-dead-summary-mode -1)
6184   (let ((name (buffer-name)))
6185     (when (string-match "Dead " name)
6186       (rename-buffer
6187        (concat (substring name 0 (match-beginning 0))
6188                (substring name (match-end 0)))
6189        t)))
6190   (gnus-message 3 "This dead summary is now alive again"))
6191
6192 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6193 (defun gnus-summary-fetch-faq (&optional faq-dir)
6194   "Fetch the FAQ for the current group.
6195 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6196 in."
6197   (interactive
6198    (list
6199     (when current-prefix-arg
6200       (completing-read
6201        "Faq dir: " (and (listp gnus-group-faq-directory)
6202                         (mapcar (lambda (file) (list file))
6203                                 gnus-group-faq-directory))))))
6204   (let (gnus-faq-buffer)
6205     (when (setq gnus-faq-buffer
6206                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6207       (gnus-configure-windows 'summary-faq))))
6208
6209 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6210 (defun gnus-summary-describe-group (&optional force)
6211   "Describe the current newsgroup."
6212   (interactive "P")
6213   (gnus-group-describe-group force gnus-newsgroup-name))
6214
6215 (defun gnus-summary-describe-briefly ()
6216   "Describe summary mode commands briefly."
6217   (interactive)
6218   (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")))
6219
6220 ;; Walking around group mode buffer from summary mode.
6221
6222 (defun gnus-summary-next-group (&optional no-article target-group backward)
6223   "Exit current newsgroup and then select next unread newsgroup.
6224 If prefix argument NO-ARTICLE is non-nil, no article is selected
6225 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6226 previous group instead."
6227   (interactive "P")
6228   ;; Stop pre-fetching.
6229   (gnus-async-halt-prefetch)
6230   (let ((current-group gnus-newsgroup-name)
6231         (current-buffer (current-buffer))
6232         entered)
6233    ;; First we semi-exit this group to update Xrefs and all variables.
6234     ;; We can't do a real exit, because the window conf must remain
6235     ;; the same in case the user is prompted for info, and we don't
6236     ;; want the window conf to change before that...
6237     (gnus-summary-exit t)
6238     (while (not entered)
6239       ;; Then we find what group we are supposed to enter.
6240       (set-buffer gnus-group-buffer)
6241       (gnus-group-jump-to-group current-group)
6242       (setq target-group
6243             (or target-group
6244                 (if (eq gnus-keep-same-level 'best)
6245                     (gnus-summary-best-group gnus-newsgroup-name)
6246                   (gnus-summary-search-group backward gnus-keep-same-level))))
6247       (if (not target-group)
6248           ;; There are no further groups, so we return to the group
6249           ;; buffer.
6250           (progn
6251             (gnus-message 5 "Returning to the group buffer")
6252             (setq entered t)
6253             (when (gnus-buffer-live-p current-buffer)
6254               (set-buffer current-buffer)
6255               (gnus-summary-exit))
6256             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6257         ;; We try to enter the target group.
6258         (gnus-group-jump-to-group target-group)
6259         (let ((unreads (gnus-group-group-unread)))
6260           (if (and (or (eq t unreads)
6261                        (and unreads (not (zerop unreads))))
6262                    (gnus-summary-read-group
6263                     target-group nil no-article
6264                     (and (buffer-name current-buffer) current-buffer)
6265                     nil backward))
6266               (setq entered t)
6267             (setq current-group target-group
6268                   target-group nil)))))))
6269
6270 (defun gnus-summary-prev-group (&optional no-article)
6271   "Exit current newsgroup and then select previous unread newsgroup.
6272 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6273   (interactive "P")
6274   (gnus-summary-next-group no-article nil t))
6275
6276 ;; Walking around summary lines.
6277
6278 (defun gnus-summary-first-subject (&optional unread undownloaded)
6279   "Go to the first unread subject.
6280 If UNREAD is non-nil, go to the first unread article.
6281 Returns the article selected or nil if there are no unread articles."
6282   (interactive "P")
6283   (prog1
6284       (cond
6285        ;; Empty summary.
6286        ((null gnus-newsgroup-data)
6287         (gnus-message 3 "No articles in the group")
6288         nil)
6289        ;; Pick the first article.
6290        ((not unread)
6291         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6292         (gnus-data-number (car gnus-newsgroup-data)))
6293        ;; No unread articles.
6294        ((null gnus-newsgroup-unreads)
6295         (gnus-message 3 "No more unread articles")
6296         nil)
6297        ;; Find the first unread article.
6298        (t
6299         (let ((data gnus-newsgroup-data))
6300           (while (and data
6301                       (and (not (and undownloaded
6302                                      (eq gnus-undownloaded-mark
6303                                          (gnus-data-mark (car data)))))
6304                            (not (gnus-data-unread-p (car data)))))
6305             (setq data (cdr data)))
6306           (when data
6307             (goto-char (gnus-data-pos (car data)))
6308             (gnus-data-number (car data))))))
6309     (gnus-summary-position-point)))
6310
6311 (defun gnus-summary-next-subject (n &optional unread dont-display)
6312   "Go to next N'th summary line.
6313 If N is negative, go to the previous N'th subject line.
6314 If UNREAD is non-nil, only unread articles are selected.
6315 The difference between N and the actual number of steps taken is
6316 returned."
6317   (interactive "p")
6318   (let ((backward (< n 0))
6319         (n (abs n)))
6320     (while (and (> n 0)
6321                 (if backward
6322                     (gnus-summary-find-prev unread)
6323                   (gnus-summary-find-next unread)))
6324       (unless (zerop (setq n (1- n)))
6325         (gnus-summary-show-thread)))
6326     (when (/= 0 n)
6327       (gnus-message 7 "No more%s articles"
6328                     (if unread " unread" "")))
6329     (unless dont-display
6330       (gnus-summary-recenter)
6331       (gnus-summary-position-point))
6332     n))
6333
6334 (defun gnus-summary-next-unread-subject (n)
6335   "Go to next N'th unread summary line."
6336   (interactive "p")
6337   (gnus-summary-next-subject n t))
6338
6339 (defun gnus-summary-prev-subject (n &optional unread)
6340   "Go to previous N'th summary line.
6341 If optional argument UNREAD is non-nil, only unread article is selected."
6342   (interactive "p")
6343   (gnus-summary-next-subject (- n) unread))
6344
6345 (defun gnus-summary-prev-unread-subject (n)
6346   "Go to previous N'th unread summary line."
6347   (interactive "p")
6348   (gnus-summary-next-subject (- n) t))
6349
6350 (defun gnus-summary-goto-subject (article &optional force silent)
6351   "Go the subject line of ARTICLE.
6352 If FORCE, also allow jumping to articles not currently shown."
6353   (interactive "nArticle number: ")
6354   (let ((b (point))
6355         (data (gnus-data-find article)))
6356     ;; We read in the article if we have to.
6357     (and (not data)
6358          force
6359          (gnus-summary-insert-subject
6360           article
6361           (if (or (numberp force) (vectorp force)) force)
6362           t)
6363          (setq data (gnus-data-find article)))
6364     (goto-char b)
6365     (if (not data)
6366         (progn
6367           (unless silent
6368             (gnus-message 3 "Can't find article %d" article))
6369           nil)
6370       (let ((pt (gnus-data-pos data)))
6371         (goto-char pt)
6372         (gnus-summary-set-article-display-arrow pt))
6373       (gnus-summary-position-point)
6374       article)))
6375
6376 ;; Walking around summary lines with displaying articles.
6377
6378 (defun gnus-summary-expand-window (&optional arg)
6379   "Make the summary buffer take up the entire Emacs frame.
6380 Given a prefix, will force an `article' buffer configuration."
6381   (interactive "P")
6382   (if arg
6383       (gnus-configure-windows 'article 'force)
6384     (gnus-configure-windows 'summary 'force)))
6385
6386 (defun gnus-summary-display-article (article &optional all-header)
6387   "Display ARTICLE in article buffer."
6388   (when (gnus-buffer-live-p gnus-article-buffer)
6389     (with-current-buffer gnus-article-buffer
6390       (mm-enable-multibyte-mule4)))
6391   (gnus-set-global-variables)
6392   (when (gnus-buffer-live-p gnus-article-buffer)
6393     (with-current-buffer gnus-article-buffer
6394       (setq gnus-article-charset gnus-newsgroup-charset)
6395       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6396       (mm-enable-multibyte-mule4)))
6397   (if (null article)
6398       nil
6399     (prog1
6400         (if gnus-summary-display-article-function
6401             (funcall gnus-summary-display-article-function article all-header)
6402           (gnus-article-prepare article all-header))
6403       (gnus-run-hooks 'gnus-select-article-hook)
6404       (when (and gnus-current-article
6405                  (not (zerop gnus-current-article)))
6406         (gnus-summary-goto-subject gnus-current-article))
6407       (gnus-summary-recenter)
6408       (when (and gnus-use-trees gnus-show-threads)
6409         (gnus-possibly-generate-tree article)
6410         (gnus-highlight-selected-tree article))
6411       ;; Successfully display article.
6412       (gnus-article-set-window-start
6413        (cdr (assq article gnus-newsgroup-bookmarks))))))
6414
6415 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6416   "Select the current article.
6417 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6418 non-nil, the article will be re-fetched even if it already present in
6419 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6420 be displayed."
6421   ;; Make sure we are in the summary buffer to work around bbdb bug.
6422   (unless (eq major-mode 'gnus-summary-mode)
6423     (set-buffer gnus-summary-buffer))
6424   (let ((article (or article (gnus-summary-article-number)))
6425         (all-headers (not (not all-headers))) ;Must be T or NIL.
6426         gnus-summary-display-article-function)
6427     (and (not pseudo)
6428          (gnus-summary-article-pseudo-p article)
6429          (error "This is a pseudo-article"))
6430     (save-excursion
6431       (set-buffer gnus-summary-buffer)
6432       (if (or (and gnus-single-article-buffer
6433                    (or (null gnus-current-article)
6434                        (null gnus-article-current)
6435                        (null (get-buffer gnus-article-buffer))
6436                        (not (eq article (cdr gnus-article-current)))
6437                        (not (equal (car gnus-article-current)
6438                                    gnus-newsgroup-name))))
6439               (and (not gnus-single-article-buffer)
6440                    (or (null gnus-current-article)
6441                        (not (eq gnus-current-article article))))
6442               force)
6443        ;; The requested article is different from the current article.
6444           (progn
6445             (gnus-summary-display-article article all-headers)
6446             (when (gnus-buffer-live-p gnus-article-buffer)
6447               (with-current-buffer gnus-article-buffer
6448                 (if (not gnus-article-decoded-p) ;; a local variable
6449                     (mm-disable-multibyte-mule4))))
6450             (when (or all-headers gnus-show-all-headers)
6451               (gnus-article-show-all-headers))
6452             (gnus-article-set-window-start
6453              (cdr (assq article gnus-newsgroup-bookmarks)))
6454             article)
6455         (when (or all-headers gnus-show-all-headers)
6456           (gnus-article-show-all-headers))
6457         'old))))
6458
6459 (defun gnus-summary-force-verify-and-decrypt ()
6460   (interactive)
6461   (let ((mm-verify-option 'known)
6462         (mm-decrypt-option 'known))
6463     (gnus-summary-select-article nil 'force)))
6464
6465 (defun gnus-summary-set-current-mark (&optional current-mark)
6466   "Obsolete function."
6467   nil)
6468
6469 (defun gnus-summary-next-article (&optional unread subject backward push)
6470   "Select the next article.
6471 If UNREAD, only unread articles are selected.
6472 If SUBJECT, only articles with SUBJECT are selected.
6473 If BACKWARD, the previous article is selected instead of the next."
6474   (interactive "P")
6475   (cond
6476    ;; Is there such an article?
6477    ((and (gnus-summary-search-forward unread subject backward)
6478          (or (gnus-summary-display-article (gnus-summary-article-number))
6479              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6480     (gnus-summary-position-point))
6481    ;; If not, we try the first unread, if that is wanted.
6482    ((and subject
6483          gnus-auto-select-same
6484          (gnus-summary-first-unread-article))
6485     (gnus-summary-position-point)
6486     (gnus-message 6 "Wrapped"))
6487    ;; Try to get next/previous article not displayed in this group.
6488    ((and gnus-auto-extend-newsgroup
6489          (not unread) (not subject))
6490     (gnus-summary-goto-article
6491      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6492      nil (count-lines (point-min) (point))))
6493    ;; Go to next/previous group.
6494    (t
6495     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6496       (gnus-summary-jump-to-group gnus-newsgroup-name))
6497     (let ((cmd last-command-char)
6498           (point
6499            (save-excursion
6500              (set-buffer gnus-group-buffer)
6501              (point)))
6502           (group
6503            (if (eq gnus-keep-same-level 'best)
6504                (gnus-summary-best-group gnus-newsgroup-name)
6505              (gnus-summary-search-group backward gnus-keep-same-level))))
6506       ;; For some reason, the group window gets selected.  We change
6507       ;; it back.
6508       (select-window (get-buffer-window (current-buffer)))
6509       ;; Select next unread newsgroup automagically.
6510       (cond
6511        ((or (not gnus-auto-select-next)
6512             (not cmd))
6513         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6514        ((or (eq gnus-auto-select-next 'quietly)
6515             (and (eq gnus-auto-select-next 'slightly-quietly)
6516                  push)
6517             (and (eq gnus-auto-select-next 'almost-quietly)
6518                  (gnus-summary-last-article-p)))
6519         ;; Select quietly.
6520         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6521             (gnus-summary-exit)
6522           (gnus-message 7 "No more%s articles (%s)..."
6523                         (if unread " unread" "")
6524                         (if group (concat "selecting " group)
6525                           "exiting"))
6526           (gnus-summary-next-group nil group backward)))
6527        (t
6528         (when (gnus-key-press-event-p last-input-event)
6529           (gnus-summary-walk-group-buffer
6530            gnus-newsgroup-name cmd unread backward point))))))))
6531
6532 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6533   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6534                       (?\C-p (gnus-group-prev-unread-group 1))))
6535         (cursor-in-echo-area t)
6536         keve key group ended)
6537     (save-excursion
6538       (set-buffer gnus-group-buffer)
6539       (goto-char start)
6540       (setq group
6541             (if (eq gnus-keep-same-level 'best)
6542                 (gnus-summary-best-group gnus-newsgroup-name)
6543               (gnus-summary-search-group backward gnus-keep-same-level))))
6544     (while (not ended)
6545       (gnus-message
6546        5 "No more%s articles%s" (if unread " unread" "")
6547        (if (and group
6548                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6549            (format " (Type %s for %s [%s])"
6550                    (single-key-description cmd) group
6551                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6552          (format " (Type %s to exit %s)"
6553                  (single-key-description cmd)
6554                  gnus-newsgroup-name)))
6555       ;; Confirm auto selection.
6556       (setq key (car (setq keve (gnus-read-event-char))))
6557       (setq ended t)
6558       (cond
6559        ((assq key keystrokes)
6560         (let ((obuf (current-buffer)))
6561           (switch-to-buffer gnus-group-buffer)
6562           (when group
6563             (gnus-group-jump-to-group group))
6564           (eval (cadr (assq key keystrokes)))
6565           (setq group (gnus-group-group-name))
6566           (switch-to-buffer obuf))
6567         (setq ended nil))
6568        ((equal key cmd)
6569         (if (or (not group)
6570                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6571             (gnus-summary-exit)
6572           (gnus-summary-next-group nil group backward)))
6573        (t
6574         (push (cdr keve) unread-command-events))))))
6575
6576 (defun gnus-summary-next-unread-article ()
6577   "Select unread article after current one."
6578   (interactive)
6579   (gnus-summary-next-article
6580    (or (not (eq gnus-summary-goto-unread 'never))
6581        (gnus-summary-last-article-p (gnus-summary-article-number)))
6582    (and gnus-auto-select-same
6583         (gnus-summary-article-subject))))
6584
6585 (defun gnus-summary-prev-article (&optional unread subject)
6586   "Select the article after the current one.
6587 If UNREAD is non-nil, only unread articles are selected."
6588   (interactive "P")
6589   (gnus-summary-next-article unread subject t))
6590
6591 (defun gnus-summary-prev-unread-article ()
6592   "Select unread article before current one."
6593   (interactive)
6594   (gnus-summary-prev-article
6595    (or (not (eq gnus-summary-goto-unread 'never))
6596        (gnus-summary-first-article-p (gnus-summary-article-number)))
6597    (and gnus-auto-select-same
6598         (gnus-summary-article-subject))))
6599
6600 (defun gnus-summary-next-page (&optional lines circular)
6601   "Show next page of the selected article.
6602 If at the end of the current article, select the next article.
6603 LINES says how many lines should be scrolled up.
6604
6605 If CIRCULAR is non-nil, go to the start of the article instead of
6606 selecting the next article when reaching the end of the current
6607 article."
6608   (interactive "P")
6609   (setq gnus-summary-buffer (current-buffer))
6610   (gnus-set-global-variables)
6611   (let ((article (gnus-summary-article-number))
6612         (article-window (get-buffer-window gnus-article-buffer t))
6613         endp)
6614     ;; If the buffer is empty, we have no article.
6615     (unless article
6616       (error "No article to select"))
6617     (gnus-configure-windows 'article)
6618     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6619         (if (and (eq gnus-summary-goto-unread 'never)
6620                  (not (gnus-summary-last-article-p article)))
6621             (gnus-summary-next-article)
6622           (gnus-summary-next-unread-article))
6623       (if (or (null gnus-current-article)
6624               (null gnus-article-current)
6625               (/= article (cdr gnus-article-current))
6626               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6627           ;; Selected subject is different from current article's.
6628           (gnus-summary-display-article article)
6629         (when article-window
6630           (gnus-eval-in-buffer-window gnus-article-buffer
6631             (setq endp (gnus-article-next-page lines)))
6632           (when endp
6633             (cond (circular
6634                    (gnus-summary-beginning-of-article))
6635                   (lines
6636                    (gnus-message 3 "End of message"))
6637                   ((null lines)
6638                    (if (and (eq gnus-summary-goto-unread 'never)
6639                             (not (gnus-summary-last-article-p article)))
6640                        (gnus-summary-next-article)
6641                      (gnus-summary-next-unread-article))))))))
6642     (gnus-summary-recenter)
6643     (gnus-summary-position-point)))
6644
6645 (defun gnus-summary-prev-page (&optional lines move)
6646   "Show previous page of selected article.
6647 Argument LINES specifies lines to be scrolled down.
6648 If MOVE, move to the previous unread article if point is at
6649 the beginning of the buffer."
6650   (interactive "P")
6651   (let ((article (gnus-summary-article-number))
6652         (article-window (get-buffer-window gnus-article-buffer t))
6653         endp)
6654     (gnus-configure-windows 'article)
6655     (if (or (null gnus-current-article)
6656             (null gnus-article-current)
6657             (/= article (cdr gnus-article-current))
6658             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6659         ;; Selected subject is different from current article's.
6660         (gnus-summary-display-article article)
6661       (gnus-summary-recenter)
6662       (when article-window
6663         (gnus-eval-in-buffer-window gnus-article-buffer
6664           (setq endp (gnus-article-prev-page lines)))
6665         (when (and move endp)
6666           (cond (lines
6667                  (gnus-message 3 "Beginning of message"))
6668                 ((null lines)
6669                  (if (and (eq gnus-summary-goto-unread 'never)
6670                           (not (gnus-summary-first-article-p article)))
6671                      (gnus-summary-prev-article)
6672                    (gnus-summary-prev-unread-article))))))))
6673   (gnus-summary-position-point))
6674
6675 (defun gnus-summary-prev-page-or-article (&optional lines)
6676   "Show previous page of selected article.
6677 Argument LINES specifies lines to be scrolled down.
6678 If at the beginning of the article, go to the next article."
6679   (interactive "P")
6680   (gnus-summary-prev-page lines t))
6681
6682 (defun gnus-summary-scroll-up (lines)
6683   "Scroll up (or down) one line current article.
6684 Argument LINES specifies lines to be scrolled up (or down if negative)."
6685   (interactive "p")
6686   (gnus-configure-windows 'article)
6687   (gnus-summary-show-thread)
6688   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6689     (gnus-eval-in-buffer-window gnus-article-buffer
6690       (cond ((> lines 0)
6691              (when (gnus-article-next-page lines)
6692                (gnus-message 3 "End of message")))
6693             ((< lines 0)
6694              (gnus-article-prev-page (- lines))))))
6695   (gnus-summary-recenter)
6696   (gnus-summary-position-point))
6697
6698 (defun gnus-summary-scroll-down (lines)
6699   "Scroll down (or up) one line current article.
6700 Argument LINES specifies lines to be scrolled down (or up if negative)."
6701   (interactive "p")
6702   (gnus-summary-scroll-up (- lines)))
6703
6704 (defun gnus-summary-next-same-subject ()
6705   "Select next article which has the same subject as current one."
6706   (interactive)
6707   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6708
6709 (defun gnus-summary-prev-same-subject ()
6710   "Select previous article which has the same subject as current one."
6711   (interactive)
6712   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6713
6714 (defun gnus-summary-next-unread-same-subject ()
6715   "Select next unread article which has the same subject as current one."
6716   (interactive)
6717   (gnus-summary-next-article t (gnus-summary-article-subject)))
6718
6719 (defun gnus-summary-prev-unread-same-subject ()
6720   "Select previous unread article which has the same subject as current one."
6721   (interactive)
6722   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6723
6724 (defun gnus-summary-first-unread-article ()
6725   "Select the first unread article.
6726 Return nil if there are no unread articles."
6727   (interactive)
6728   (prog1
6729       (when (gnus-summary-first-subject t)
6730         (gnus-summary-show-thread)
6731         (gnus-summary-first-subject t)
6732         (gnus-summary-display-article (gnus-summary-article-number)))
6733     (gnus-summary-position-point)))
6734
6735 (defun gnus-summary-first-unread-subject ()
6736   "Place the point on the subject line of the first unread article.
6737 Return nil if there are no unread articles."
6738   (interactive)
6739   (prog1
6740       (when (gnus-summary-first-subject t)
6741         (gnus-summary-show-thread)
6742         (gnus-summary-first-subject t))
6743     (gnus-summary-position-point)))
6744
6745 (defun gnus-summary-first-article ()
6746   "Select the first article.
6747 Return nil if there are no articles."
6748   (interactive)
6749   (prog1
6750       (when (gnus-summary-first-subject)
6751         (gnus-summary-show-thread)
6752         (gnus-summary-first-subject)
6753         (gnus-summary-display-article (gnus-summary-article-number)))
6754     (gnus-summary-position-point)))
6755
6756 (defun gnus-summary-best-unread-article ()
6757   "Select the unread article with the highest score."
6758   (interactive)
6759   (let ((best -1000000)
6760         (data gnus-newsgroup-data)
6761         article score)
6762     (while data
6763       (and (gnus-data-unread-p (car data))
6764            (> (setq score
6765                     (gnus-summary-article-score (gnus-data-number (car data))))
6766               best)
6767            (setq best score
6768                  article (gnus-data-number (car data))))
6769       (setq data (cdr data)))
6770     (prog1
6771         (if article
6772             (gnus-summary-goto-article article)
6773           (error "No unread articles"))
6774       (gnus-summary-position-point))))
6775
6776 (defun gnus-summary-last-subject ()
6777   "Go to the last displayed subject line in the group."
6778   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6779     (when article
6780       (gnus-summary-goto-subject article))))
6781
6782 (defun gnus-summary-goto-article (article &optional all-headers force)
6783   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6784 If ALL-HEADERS is non-nil, no header lines are hidden.
6785 If FORCE, go to the article even if it isn't displayed.  If FORCE
6786 is a number, it is the line the article is to be displayed on."
6787   (interactive
6788    (list
6789     (completing-read
6790      "Article number or Message-ID: "
6791      (mapcar (lambda (number) (list (int-to-string number)))
6792              gnus-newsgroup-limit))
6793     current-prefix-arg
6794     t))
6795   (prog1
6796       (if (and (stringp article)
6797                (string-match "@" article))
6798           (gnus-summary-refer-article article)
6799         (when (stringp article)
6800           (setq article (string-to-number article)))
6801         (if (gnus-summary-goto-subject article force)
6802             (gnus-summary-display-article article all-headers)
6803           (gnus-message 4 "Couldn't go to article %s" article) nil))
6804     (gnus-summary-position-point)))
6805
6806 (defun gnus-summary-goto-last-article ()
6807   "Go to the previously read article."
6808   (interactive)
6809   (prog1
6810       (when gnus-last-article
6811         (gnus-summary-goto-article gnus-last-article nil t))
6812     (gnus-summary-position-point)))
6813
6814 (defun gnus-summary-pop-article (number)
6815   "Pop one article off the history and go to the previous.
6816 NUMBER articles will be popped off."
6817   (interactive "p")
6818   (let (to)
6819     (setq gnus-newsgroup-history
6820           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6821     (if to
6822         (gnus-summary-goto-article (car to) nil t)
6823       (error "Article history empty")))
6824   (gnus-summary-position-point))
6825
6826 ;; Summary commands and functions for limiting the summary buffer.
6827
6828 (defun gnus-summary-limit-to-articles (n)
6829   "Limit the summary buffer to the next N articles.
6830 If not given a prefix, use the process marked articles instead."
6831   (interactive "P")
6832   (prog1
6833       (let ((articles (gnus-summary-work-articles n)))
6834         (setq gnus-newsgroup-processable nil)
6835         (gnus-summary-limit articles))
6836     (gnus-summary-position-point)))
6837
6838 (defun gnus-summary-pop-limit (&optional total)
6839   "Restore the previous limit.
6840 If given a prefix, remove all limits."
6841   (interactive "P")
6842   (when total
6843     (setq gnus-newsgroup-limits
6844           (list (mapcar (lambda (h) (mail-header-number h))
6845                         gnus-newsgroup-headers))))
6846   (unless gnus-newsgroup-limits
6847     (error "No limit to pop"))
6848   (prog1
6849       (gnus-summary-limit nil 'pop)
6850     (gnus-summary-position-point)))
6851
6852 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
6853   "Limit the summary buffer to articles that have subjects that match a regexp.
6854 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
6855   (interactive
6856    (list (read-string (if current-prefix-arg
6857                           "Exclude subject (regexp): "
6858                         "Limit to subject (regexp): "))
6859          nil current-prefix-arg))
6860   (unless header
6861     (setq header "subject"))
6862   (when (not (equal "" subject))
6863     (prog1
6864         (let ((articles (gnus-summary-find-matching
6865                          (or header "subject") subject 'all nil nil
6866                          not-matching)))
6867           (unless articles
6868             (error "Found no matches for \"%s\"" subject))
6869           (gnus-summary-limit articles))
6870       (gnus-summary-position-point))))
6871
6872 (defun gnus-summary-limit-to-author (from &optional not-matching)
6873   "Limit the summary buffer to articles that have authors that match a regexp.
6874 If NOT-MATCHING, excluding articles that have authors that match a regexp."
6875   (interactive
6876    (list (read-string (if current-prefix-arg
6877                           "Exclude author (regexp): "
6878                         "Limit to author (regexp): "))
6879          current-prefix-arg))
6880   (gnus-summary-limit-to-subject from "from" not-matching))
6881
6882 (defun gnus-summary-limit-to-age (age &optional younger-p)
6883   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6884 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6885 articles that are younger than AGE days."
6886   (interactive
6887    (let ((younger current-prefix-arg)
6888          (days-got nil)
6889          days)
6890      (while (not days-got)
6891        (setq days (if younger
6892                       (read-string "Limit to articles within (in days): ")
6893                     (read-string "Limit to articles older than (in days): ")))
6894        (when (> (length days) 0)
6895          (setq days (read days)))
6896        (if (numberp days)
6897            (setq days-got t)
6898          (message "Please enter a number.")
6899          (sleep-for 1)))
6900      (list days younger)))
6901   (prog1
6902       (let ((data gnus-newsgroup-data)
6903             (cutoff (days-to-time age))
6904             articles d date is-younger)
6905         (while (setq d (pop data))
6906           (when (and (vectorp (gnus-data-header d))
6907                      (setq date (mail-header-date (gnus-data-header d))))
6908             (setq is-younger (time-less-p
6909                               (time-since (condition-case ()
6910                                               (date-to-time date)
6911                                             (error '(0 0))))
6912                               cutoff))
6913             (when (if younger-p
6914                       is-younger
6915                     (not is-younger))
6916               (push (gnus-data-number d) articles))))
6917         (gnus-summary-limit (nreverse articles)))
6918     (gnus-summary-position-point)))
6919
6920 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
6921   "Limit the summary buffer to articles that match an 'extra' header."
6922   (interactive
6923    (let ((header
6924           (intern
6925            (gnus-completing-read
6926             (symbol-name (car gnus-extra-headers))
6927             (if current-prefix-arg
6928                 "Exclude extra header:"
6929               "Limit extra header:")
6930             (mapcar (lambda (x)
6931                       (cons (symbol-name x) x))
6932                     gnus-extra-headers)
6933             nil
6934             t))))
6935      (list header
6936            (read-string (format "%s header %s (regexp): "
6937                                 (if current-prefix-arg "Exclude" "Limit to")
6938                                 header))
6939            current-prefix-arg)))
6940   (when (not (equal "" regexp))
6941     (prog1
6942         (let ((articles (gnus-summary-find-matching
6943                          (cons 'extra header) regexp 'all nil nil
6944                          not-matching)))
6945           (unless articles
6946             (error "Found no matches for \"%s\"" regexp))
6947           (gnus-summary-limit articles))
6948       (gnus-summary-position-point))))
6949
6950 (defun gnus-summary-limit-to-display-predicate ()
6951   "Limit the summary buffer to the predicated in the `display' group parameter."
6952   (interactive)
6953   (unless gnus-newsgroup-display
6954     (error "There is no `display' group parameter"))
6955   (let (articles)
6956     (dolist (number gnus-newsgroup-articles)
6957       (when (funcall gnus-newsgroup-display)
6958         (push number articles)))
6959     (gnus-summary-limit articles))
6960   (gnus-summary-position-point))
6961
6962 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6963 (make-obsolete
6964  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6965
6966 (defun gnus-summary-limit-to-unread (&optional all)
6967   "Limit the summary buffer to articles that are not marked as read.
6968 If ALL is non-nil, limit strictly to unread articles."
6969   (interactive "P")
6970   (if all
6971       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6972     (gnus-summary-limit-to-marks
6973      ;; Concat all the marks that say that an article is read and have
6974      ;; those removed.
6975      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6976            gnus-killed-mark gnus-kill-file-mark
6977            gnus-low-score-mark gnus-expirable-mark
6978            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6979            gnus-duplicate-mark gnus-souped-mark)
6980      'reverse)))
6981
6982 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6983 (make-obsolete 'gnus-summary-delete-marked-with
6984                'gnus-summary-limit-exlude-marks)
6985
6986 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6987   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6988 If REVERSE, limit the summary buffer to articles that are marked
6989 with MARKS.  MARKS can either be a string of marks or a list of marks.
6990 Returns how many articles were removed."
6991   (interactive "sMarks: ")
6992   (gnus-summary-limit-to-marks marks t))
6993
6994 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6995   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6996 If REVERSE (the prefix), limit the summary buffer to articles that are
6997 not marked with MARKS.  MARKS can either be a string of marks or a
6998 list of marks.
6999 Returns how many articles were removed."
7000   (interactive "sMarks: \nP")
7001   (prog1
7002       (let ((data gnus-newsgroup-data)
7003             (marks (if (listp marks) marks
7004                      (append marks nil))) ; Transform to list.
7005             articles)
7006         (while data
7007           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7008                   (memq (gnus-data-mark (car data)) marks))
7009             (push (gnus-data-number (car data)) articles))
7010           (setq data (cdr data)))
7011         (gnus-summary-limit articles))
7012     (gnus-summary-position-point)))
7013
7014 (defun gnus-summary-limit-to-score (score)
7015   "Limit to articles with score at or above SCORE."
7016   (interactive "NLimit to articles with score of at least: ")
7017   (let ((data gnus-newsgroup-data)
7018         articles)
7019     (while data
7020       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7021                 score)
7022         (push (gnus-data-number (car data)) articles))
7023       (setq data (cdr data)))
7024     (prog1
7025         (gnus-summary-limit articles)
7026       (gnus-summary-position-point))))
7027
7028 (defun gnus-summary-limit-include-thread (id)
7029   "Display all the hidden articles that is in the thread with ID in it.
7030 When called interactively, ID is the Message-ID of the current
7031 article."
7032   (interactive (list (mail-header-id (gnus-summary-article-header))))
7033   (let ((articles (gnus-articles-in-thread
7034                    (gnus-id-to-thread (gnus-root-id id)))))
7035     (prog1
7036         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7037       (gnus-summary-limit-include-matching-articles
7038        "subject"
7039        (regexp-quote (gnus-simplify-subject-re
7040                       (mail-header-subject (gnus-id-to-header id)))))
7041       (gnus-summary-position-point))))
7042
7043 (defun gnus-summary-limit-include-matching-articles (header regexp)
7044   "Display all the hidden articles that have HEADERs that match REGEXP."
7045   (interactive (list (read-string "Match on header: ")
7046                      (read-string "Regexp: ")))
7047   (let ((articles (gnus-find-matching-articles header regexp)))
7048     (prog1
7049         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7050       (gnus-summary-position-point))))
7051
7052 (defun gnus-summary-limit-include-dormant ()
7053   "Display all the hidden articles that are marked as dormant.
7054 Note that this command only works on a subset of the articles currently
7055 fetched for this group."
7056   (interactive)
7057   (unless gnus-newsgroup-dormant
7058     (error "There are no dormant articles in this group"))
7059   (prog1
7060       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7061     (gnus-summary-position-point)))
7062
7063 (defun gnus-summary-limit-exclude-dormant ()
7064   "Hide all dormant articles."
7065   (interactive)
7066   (prog1
7067       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7068     (gnus-summary-position-point)))
7069
7070 (defun gnus-summary-limit-exclude-childless-dormant ()
7071   "Hide all dormant articles that have no children."
7072   (interactive)
7073   (let ((data (gnus-data-list t))
7074         articles d children)
7075     ;; Find all articles that are either not dormant or have
7076     ;; children.
7077     (while (setq d (pop data))
7078       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7079                 (and (setq children
7080                            (gnus-article-children (gnus-data-number d)))
7081                      (let (found)
7082                        (while children
7083                          (when (memq (car children) articles)
7084                            (setq children nil
7085                                  found t))
7086                          (pop children))
7087                        found)))
7088         (push (gnus-data-number d) articles)))
7089     ;; Do the limiting.
7090     (prog1
7091         (gnus-summary-limit articles)
7092       (gnus-summary-position-point))))
7093
7094 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7095   "Mark all unread excluded articles as read.
7096 If ALL, mark even excluded ticked and dormants as read."
7097   (interactive "P")
7098   (let ((articles (gnus-sorted-complement
7099                    (sort
7100                     (mapcar (lambda (h) (mail-header-number h))
7101                             gnus-newsgroup-headers)
7102                     '<)
7103                    (sort gnus-newsgroup-limit '<)))
7104         article)
7105     (setq gnus-newsgroup-unreads
7106           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7107     (if all
7108         (setq gnus-newsgroup-dormant nil
7109               gnus-newsgroup-marked nil
7110               gnus-newsgroup-reads
7111               (nconc
7112                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7113                gnus-newsgroup-reads))
7114       (while (setq article (pop articles))
7115         (unless (or (memq article gnus-newsgroup-dormant)
7116                     (memq article gnus-newsgroup-marked))
7117           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7118
7119 (defun gnus-summary-limit (articles &optional pop)
7120   (if pop
7121       ;; We pop the previous limit off the stack and use that.
7122       (setq articles (car gnus-newsgroup-limits)
7123             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7124     ;; We use the new limit, so we push the old limit on the stack.
7125     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7126   ;; Set the limit.
7127   (setq gnus-newsgroup-limit articles)
7128   (let ((total (length gnus-newsgroup-data))
7129         (data (gnus-data-find-list (gnus-summary-article-number)))
7130         (gnus-summary-mark-below nil)   ; Inhibit this.
7131         found)
7132     ;; This will do all the work of generating the new summary buffer
7133     ;; according to the new limit.
7134     (gnus-summary-prepare)
7135     ;; Hide any threads, possibly.
7136     (and gnus-show-threads
7137          gnus-thread-hide-subtree
7138          (gnus-summary-hide-all-threads))
7139     ;; Try to return to the article you were at, or one in the
7140     ;; neighborhood.
7141     (when data
7142       ;; We try to find some article after the current one.
7143       (while data
7144         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7145           (setq data nil
7146                 found t))
7147         (setq data (cdr data))))
7148     (unless found
7149       ;; If there is no data, that means that we were after the last
7150       ;; article.  The same goes when we can't find any articles
7151       ;; after the current one.
7152       (goto-char (point-max))
7153       (gnus-summary-find-prev))
7154     (gnus-set-mode-line 'summary)
7155     ;; We return how many articles were removed from the summary
7156     ;; buffer as a result of the new limit.
7157     (- total (length gnus-newsgroup-data))))
7158
7159 (defsubst gnus-invisible-cut-children (threads)
7160   (let ((num 0))
7161     (while threads
7162       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7163         (incf num))
7164       (pop threads))
7165     (< num 2)))
7166
7167 (defsubst gnus-cut-thread (thread)
7168   "Go forwards in the thread until we find an article that we want to display."
7169   (when (or (eq gnus-fetch-old-headers 'some)
7170             (eq gnus-fetch-old-headers 'invisible)
7171             (numberp gnus-fetch-old-headers)
7172             (eq gnus-build-sparse-threads 'some)
7173             (eq gnus-build-sparse-threads 'more))
7174     ;; Deal with old-fetched headers and sparse threads.
7175     (while (and
7176             thread
7177             (or
7178              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7179              (gnus-summary-article-ancient-p
7180               (mail-header-number (car thread))))
7181             (if (or (<= (length (cdr thread)) 1)
7182                     (eq gnus-fetch-old-headers 'invisible))
7183                 (setq gnus-newsgroup-limit
7184                       (delq (mail-header-number (car thread))
7185                             gnus-newsgroup-limit)
7186                       thread (cadr thread))
7187               (when (gnus-invisible-cut-children (cdr thread))
7188                 (let ((th (cdr thread)))
7189                   (while th
7190                     (if (memq (mail-header-number (caar th))
7191                               gnus-newsgroup-limit)
7192                         (setq thread (car th)
7193                               th nil)
7194                       (setq th (cdr th))))))))))
7195   thread)
7196
7197 (defun gnus-cut-threads (threads)
7198   "Cut off all uninteresting articles from the beginning of threads."
7199   (when (or (eq gnus-fetch-old-headers 'some)
7200             (eq gnus-fetch-old-headers 'invisible)
7201             (numberp gnus-fetch-old-headers)
7202             (eq gnus-build-sparse-threads 'some)
7203             (eq gnus-build-sparse-threads 'more))
7204     (let ((th threads))
7205       (while th
7206         (setcar th (gnus-cut-thread (car th)))
7207         (setq th (cdr th)))))
7208   ;; Remove nixed out threads.
7209   (delq nil threads))
7210
7211 (defun gnus-summary-initial-limit (&optional show-if-empty)
7212   "Figure out what the initial limit is supposed to be on group entry.
7213 This entails weeding out unwanted dormants, low-scored articles,
7214 fetch-old-headers verbiage, and so on."
7215   ;; Most groups have nothing to remove.
7216   (if (or gnus-inhibit-limiting
7217           (and (null gnus-newsgroup-dormant)
7218                (eq gnus-newsgroup-display 'gnus-not-ignore)
7219                (not (eq gnus-fetch-old-headers 'some))
7220                (not (numberp gnus-fetch-old-headers))
7221                (not (eq gnus-fetch-old-headers 'invisible))
7222                (null gnus-summary-expunge-below)
7223                (not (eq gnus-build-sparse-threads 'some))
7224                (not (eq gnus-build-sparse-threads 'more))
7225                (null gnus-thread-expunge-below)
7226                (not gnus-use-nocem)))
7227       ()                                ; Do nothing.
7228     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7229     (setq gnus-newsgroup-limit nil)
7230     (mapatoms
7231      (lambda (node)
7232        (unless (car (symbol-value node))
7233          ;; These threads have no parents -- they are roots.
7234          (let ((nodes (cdr (symbol-value node)))
7235                thread)
7236            (while nodes
7237              (if (and gnus-thread-expunge-below
7238                       (< (gnus-thread-total-score (car nodes))
7239                          gnus-thread-expunge-below))
7240                  (gnus-expunge-thread (pop nodes))
7241                (setq thread (pop nodes))
7242                (gnus-summary-limit-children thread))))))
7243      gnus-newsgroup-dependencies)
7244     ;; If this limitation resulted in an empty group, we might
7245     ;; pop the previous limit and use it instead.
7246     (when (and (not gnus-newsgroup-limit)
7247                show-if-empty)
7248       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7249     gnus-newsgroup-limit))
7250
7251 (defun gnus-summary-limit-children (thread)
7252   "Return 1 if this subthread is visible and 0 if it is not."
7253   ;; First we get the number of visible children to this thread.  This
7254   ;; is done by recursing down the thread using this function, so this
7255   ;; will really go down to a leaf article first, before slowly
7256   ;; working its way up towards the root.
7257   (when thread
7258     (let ((children
7259            (if (cdr thread)
7260                (apply '+ (mapcar 'gnus-summary-limit-children
7261                                  (cdr thread)))
7262              0))
7263           (number (mail-header-number (car thread)))
7264           score)
7265       (if (and
7266            (not (memq number gnus-newsgroup-marked))
7267            (or
7268             ;; If this article is dormant and has absolutely no visible
7269             ;; children, then this article isn't visible.
7270             (and (memq number gnus-newsgroup-dormant)
7271                  (zerop children))
7272             ;; If this is "fetch-old-headered" and there is no
7273             ;; visible children, then we don't want this article.
7274             (and (or (eq gnus-fetch-old-headers 'some)
7275                      (numberp gnus-fetch-old-headers))
7276                  (gnus-summary-article-ancient-p number)
7277                  (zerop children))
7278             ;; If this is "fetch-old-headered" and `invisible', then
7279             ;; we don't want this article.
7280             (and (eq gnus-fetch-old-headers 'invisible)
7281                  (gnus-summary-article-ancient-p number))
7282             ;; If this is a sparsely inserted article with no children,
7283             ;; we don't want it.
7284             (and (eq gnus-build-sparse-threads 'some)
7285                  (gnus-summary-article-sparse-p number)
7286                  (zerop children))
7287             ;; If we use expunging, and this article is really
7288             ;; low-scored, then we don't want this article.
7289             (when (and gnus-summary-expunge-below
7290                        (< (setq score
7291                                 (or (cdr (assq number gnus-newsgroup-scored))
7292                                     gnus-summary-default-score))
7293                           gnus-summary-expunge-below))
7294               ;; We increase the expunge-tally here, but that has
7295               ;; nothing to do with the limits, really.
7296               (incf gnus-newsgroup-expunged-tally)
7297               ;; We also mark as read here, if that's wanted.
7298               (when (and gnus-summary-mark-below
7299                          (< score gnus-summary-mark-below))
7300                 (setq gnus-newsgroup-unreads
7301                       (delq number gnus-newsgroup-unreads))
7302                 (if gnus-newsgroup-auto-expire
7303                     (push number gnus-newsgroup-expirable)
7304                   (push (cons number gnus-low-score-mark)
7305                         gnus-newsgroup-reads)))
7306               t)
7307             ;; Do the `display' group parameter.
7308             (and gnus-newsgroup-display
7309                  (not (funcall gnus-newsgroup-display)))
7310             ;; Check NoCeM things.
7311             (if (and gnus-use-nocem
7312                      (gnus-nocem-unwanted-article-p
7313                       (mail-header-id (car thread))))
7314                 (progn
7315                   (setq gnus-newsgroup-unreads
7316                         (delq number gnus-newsgroup-unreads))
7317                   t))))
7318           ;; Nope, invisible article.
7319           0
7320         ;; Ok, this article is to be visible, so we add it to the limit
7321         ;; and return 1.
7322         (push number gnus-newsgroup-limit)
7323         1))))
7324
7325 (defun gnus-expunge-thread (thread)
7326   "Mark all articles in THREAD as read."
7327   (let* ((number (mail-header-number (car thread))))
7328     (incf gnus-newsgroup-expunged-tally)
7329     ;; We also mark as read here, if that's wanted.
7330     (setq gnus-newsgroup-unreads
7331           (delq number gnus-newsgroup-unreads))
7332     (if gnus-newsgroup-auto-expire
7333         (push number gnus-newsgroup-expirable)
7334       (push (cons number gnus-low-score-mark)
7335             gnus-newsgroup-reads)))
7336   ;; Go recursively through all subthreads.
7337   (mapcar 'gnus-expunge-thread (cdr thread)))
7338
7339 ;; Summary article oriented commands
7340
7341 (defun gnus-summary-refer-parent-article (n)
7342   "Refer parent article N times.
7343 If N is negative, go to ancestor -N instead.
7344 The difference between N and the number of articles fetched is returned."
7345   (interactive "p")
7346   (let ((skip 1)
7347         error header ref)
7348     (when (not (natnump n))
7349       (setq skip (abs n)
7350             n 1))
7351     (while (and (> n 0)
7352                 (not error))
7353       (setq header (gnus-summary-article-header))
7354       (if (and (eq (mail-header-number header)
7355                    (cdr gnus-article-current))
7356                (equal gnus-newsgroup-name
7357                       (car gnus-article-current)))
7358           ;; If we try to find the parent of the currently
7359           ;; displayed article, then we take a look at the actual
7360           ;; References header, since this is slightly more
7361           ;; reliable than the References field we got from the
7362           ;; server.
7363           (save-excursion
7364             (set-buffer gnus-original-article-buffer)
7365             (nnheader-narrow-to-headers)
7366             (unless (setq ref (message-fetch-field "references"))
7367               (setq ref (message-fetch-field "in-reply-to")))
7368             (widen))
7369         (setq ref
7370               ;; It's not the current article, so we take a bet on
7371               ;; the value we got from the server.
7372               (mail-header-references header)))
7373       (if (and ref
7374                (not (equal ref "")))
7375           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7376             (gnus-message 1 "Couldn't find parent"))
7377         (gnus-message 1 "No references in article %d"
7378                       (gnus-summary-article-number))
7379         (setq error t))
7380       (decf n))
7381     (gnus-summary-position-point)
7382     n))
7383
7384 (defun gnus-summary-refer-references ()
7385   "Fetch all articles mentioned in the References header.
7386 Return the number of articles fetched."
7387   (interactive)
7388   (let ((ref (mail-header-references (gnus-summary-article-header)))
7389         (current (gnus-summary-article-number))
7390         (n 0))
7391     (if (or (not ref)
7392             (equal ref ""))
7393         (error "No References in the current article")
7394       ;; For each Message-ID in the References header...
7395       (while (string-match "<[^>]*>" ref)
7396         (incf n)
7397         ;; ... fetch that article.
7398         (gnus-summary-refer-article
7399          (prog1 (match-string 0 ref)
7400            (setq ref (substring ref (match-end 0))))))
7401       (gnus-summary-goto-subject current)
7402       (gnus-summary-position-point)
7403       n)))
7404
7405 (defun gnus-summary-refer-thread (&optional limit)
7406   "Fetch all articles in the current thread.
7407 If LIMIT (the numerical prefix), fetch that many old headers instead
7408 of what's specified by the `gnus-refer-thread-limit' variable."
7409   (interactive "P")
7410   (let ((id (mail-header-id (gnus-summary-article-header)))
7411         (limit (if limit (prefix-numeric-value limit)
7412                  gnus-refer-thread-limit)))
7413     ;; We want to fetch LIMIT *old* headers, but we also have to
7414     ;; re-fetch all the headers in the current buffer, because many of
7415     ;; them may be undisplayed.  So we adjust LIMIT.
7416     (when (numberp limit)
7417       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7418     (unless (eq gnus-fetch-old-headers 'invisible)
7419       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7420       ;; Retrieve the headers and read them in.
7421       (if (eq (gnus-retrieve-headers
7422                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7423               'nov)
7424           (gnus-build-all-threads)
7425         (error "Can't fetch thread from backends that don't support NOV"))
7426       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7427     (gnus-summary-limit-include-thread id)))
7428
7429 (defun gnus-summary-refer-article (message-id)
7430   "Fetch an article specified by MESSAGE-ID."
7431   (interactive "sMessage-ID: ")
7432   (when (and (stringp message-id)
7433              (not (zerop (length message-id))))
7434     ;; Construct the correct Message-ID if necessary.
7435     ;; Suggested by tale@pawl.rpi.edu.
7436     (unless (string-match "^<" message-id)
7437       (setq message-id (concat "<" message-id)))
7438     (unless (string-match ">$" message-id)
7439       (setq message-id (concat message-id ">")))
7440     (let* ((header (gnus-id-to-header message-id))
7441            (sparse (and header
7442                         (gnus-summary-article-sparse-p
7443                          (mail-header-number header))
7444                         (memq (mail-header-number header)
7445                               gnus-newsgroup-limit)))
7446            number)
7447       (cond
7448        ;; If the article is present in the buffer we just go to it.
7449        ((and header
7450              (or (not (gnus-summary-article-sparse-p
7451                        (mail-header-number header)))
7452                  sparse))
7453         (prog1
7454             (gnus-summary-goto-article
7455              (mail-header-number header) nil t)
7456           (when sparse
7457             (gnus-summary-update-article (mail-header-number header)))))
7458        (t
7459         ;; We fetch the article.
7460         (catch 'found
7461           (dolist (gnus-override-method (gnus-refer-article-methods))
7462             (gnus-check-server gnus-override-method)
7463             ;; Fetch the header, and display the article.
7464             (when (setq number (gnus-summary-insert-subject message-id))
7465               (gnus-summary-select-article nil nil nil number)
7466               (throw 'found t)))
7467           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7468
7469 (defun gnus-refer-article-methods ()
7470   "Return a list of referrable methods."
7471   (cond
7472    ;; No method, so we default to current and native.
7473    ((null gnus-refer-article-method)
7474     (list gnus-current-select-method gnus-select-method))
7475    ;; Current.
7476    ((eq 'current gnus-refer-article-method)
7477     (list gnus-current-select-method))
7478    ;; List of select methods.
7479    ((not (and (symbolp (car gnus-refer-article-method))
7480               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7481     (let (out)
7482       (dolist (method gnus-refer-article-method)
7483         (push (if (eq 'current method)
7484                   gnus-current-select-method
7485                 method)
7486               out))
7487       (nreverse out)))
7488    ;; One single select method.
7489    (t
7490     (list gnus-refer-article-method))))
7491
7492 (defun gnus-summary-edit-parameters ()
7493   "Edit the group parameters of the current group."
7494   (interactive)
7495   (gnus-group-edit-group gnus-newsgroup-name 'params))
7496
7497 (defun gnus-summary-customize-parameters ()
7498   "Customize the group parameters of the current group."
7499   (interactive)
7500   (gnus-group-customize gnus-newsgroup-name))
7501
7502 (defun gnus-summary-enter-digest-group (&optional force)
7503   "Enter an nndoc group based on the current article.
7504 If FORCE, force a digest interpretation.  If not, try
7505 to guess what the document format is."
7506   (interactive "P")
7507   (let ((conf gnus-current-window-configuration))
7508     (save-excursion
7509       (gnus-summary-select-article))
7510     (setq gnus-current-window-configuration conf)
7511     (let* ((name (format "%s-%d"
7512                          (gnus-group-prefixed-name
7513                           gnus-newsgroup-name (list 'nndoc ""))
7514                          (save-excursion
7515                            (set-buffer gnus-summary-buffer)
7516                            gnus-current-article)))
7517            (ogroup gnus-newsgroup-name)
7518            (params (append (gnus-info-params (gnus-get-info ogroup))
7519                            (list (cons 'to-group ogroup))
7520                            (list (cons 'save-article-group ogroup))))
7521            (case-fold-search t)
7522            (buf (current-buffer))
7523            dig to-address)
7524       (save-excursion
7525         (set-buffer gnus-original-article-buffer)
7526         ;; Have the digest group inherit the main mail address of
7527         ;; the parent article.
7528         (when (setq to-address (or (message-fetch-field "reply-to")
7529                                    (message-fetch-field "from")))
7530           (setq params (append
7531                         (list (cons 'to-address
7532                                     (funcall gnus-decode-encoded-word-function
7533                                              to-address))))))
7534         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7535         (insert-buffer-substring gnus-original-article-buffer)
7536         ;; Remove lines that may lead nndoc to misinterpret the
7537         ;; document type.
7538         (narrow-to-region
7539          (goto-char (point-min))
7540          (or (search-forward "\n\n" nil t) (point)))
7541         (goto-char (point-min))
7542         (delete-matching-lines "^Path:\\|^From ")
7543         (widen))
7544       (unwind-protect
7545           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7546                     (gnus-newsgroup-ephemeral-ignored-charsets
7547                      gnus-newsgroup-ignored-charsets))
7548                 (gnus-group-read-ephemeral-group
7549                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7550                               (nndoc-article-type
7551                                ,(if force 'mbox 'guess))) t))
7552             ;; Make all postings to this group go to the parent group.
7553               (nconc (gnus-info-params (gnus-get-info name))
7554                      params)
7555             ;; Couldn't select this doc group.
7556             (switch-to-buffer buf)
7557             (gnus-set-global-variables)
7558             (gnus-configure-windows 'summary)
7559             (gnus-message 3 "Article couldn't be entered?"))
7560         (kill-buffer dig)))))
7561
7562 (defun gnus-summary-read-document (n)
7563   "Open a new group based on the current article(s).
7564 This will allow you to read digests and other similar
7565 documents as newsgroups.
7566 Obeys the standard process/prefix convention."
7567   (interactive "P")
7568   (let* ((articles (gnus-summary-work-articles n))
7569          (ogroup gnus-newsgroup-name)
7570          (params (append (gnus-info-params (gnus-get-info ogroup))
7571                          (list (cons 'to-group ogroup))))
7572          article group egroup groups vgroup)
7573     (while (setq article (pop articles))
7574       (setq group (format "%s-%d" gnus-newsgroup-name article))
7575       (gnus-summary-remove-process-mark article)
7576       (when (gnus-summary-display-article article)
7577         (save-excursion
7578           (with-temp-buffer
7579             (insert-buffer-substring gnus-original-article-buffer)
7580             ;; Remove some headers that may lead nndoc to make
7581             ;; the wrong guess.
7582             (message-narrow-to-head)
7583             (goto-char (point-min))
7584             (delete-matching-lines "^\\(Path\\):\\|^From ")
7585             (widen)
7586             (if (setq egroup
7587                       (gnus-group-read-ephemeral-group
7588                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7589                                      (nndoc-article-type guess))
7590                        t nil t))
7591                 (progn
7592             ;; Make all postings to this group go to the parent group.
7593                   (nconc (gnus-info-params (gnus-get-info egroup))
7594                          params)
7595                   (push egroup groups))
7596               ;; Couldn't select this doc group.
7597               (gnus-error 3 "Article couldn't be entered"))))))
7598     ;; Now we have selected all the documents.
7599     (cond
7600      ((not groups)
7601       (error "None of the articles could be interpreted as documents"))
7602      ((gnus-group-read-ephemeral-group
7603        (setq vgroup (format
7604                      "nnvirtual:%s-%s" gnus-newsgroup-name
7605                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7606        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7607        t
7608        (cons (current-buffer) 'summary)))
7609      (t
7610       (error "Couldn't select virtual nndoc group")))))
7611
7612 (defun gnus-summary-isearch-article (&optional regexp-p)
7613   "Do incremental search forward on the current article.
7614 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7615   (interactive "P")
7616   (gnus-summary-select-article)
7617   (gnus-configure-windows 'article)
7618   (gnus-eval-in-buffer-window gnus-article-buffer
7619     (save-restriction
7620       (widen)
7621       (isearch-forward regexp-p))))
7622
7623 (defun gnus-summary-search-article-forward (regexp &optional backward)
7624   "Search for an article containing REGEXP forward.
7625 If BACKWARD, search backward instead."
7626   (interactive
7627    (list (read-string
7628           (format "Search article %s (regexp%s): "
7629                   (if current-prefix-arg "backward" "forward")
7630                   (if gnus-last-search-regexp
7631                       (concat ", default " gnus-last-search-regexp)
7632                     "")))
7633          current-prefix-arg))
7634   (if (string-equal regexp "")
7635       (setq regexp (or gnus-last-search-regexp ""))
7636     (setq gnus-last-search-regexp regexp)
7637     (setq gnus-article-before-search gnus-current-article))
7638   ;; Intentionally set gnus-last-article.
7639   (setq gnus-last-article gnus-article-before-search)
7640   (let ((gnus-last-article gnus-last-article))
7641     (if (gnus-summary-search-article regexp backward)
7642         (gnus-summary-show-thread)
7643       (error "Search failed: \"%s\"" regexp))))
7644
7645 (defun gnus-summary-search-article-backward (regexp)
7646   "Search for an article containing REGEXP backward."
7647   (interactive
7648    (list (read-string
7649           (format "Search article backward (regexp%s): "
7650                   (if gnus-last-search-regexp
7651                       (concat ", default " gnus-last-search-regexp)
7652                     "")))))
7653   (gnus-summary-search-article-forward regexp 'backward))
7654
7655 (defun gnus-summary-search-article (regexp &optional backward)
7656   "Search for an article containing REGEXP.
7657 Optional argument BACKWARD means do search for backward.
7658 `gnus-select-article-hook' is not called during the search."
7659   ;; We have to require this here to make sure that the following
7660   ;; dynamic binding isn't shadowed by autoloading.
7661   (require 'gnus-async)
7662   (require 'gnus-art)
7663   (let ((gnus-select-article-hook nil)  ;Disable hook.
7664         (gnus-article-prepare-hook nil)
7665         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7666         (gnus-use-article-prefetch nil)
7667         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7668         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7669         (sum (current-buffer))
7670         (gnus-display-mime-function nil)
7671         (found nil)
7672         point)
7673     (gnus-save-hidden-threads
7674       (gnus-summary-select-article)
7675       (set-buffer gnus-article-buffer)
7676       (goto-char (window-point (get-buffer-window (current-buffer))))
7677       (when backward
7678         (forward-line -1))
7679       (while (not found)
7680         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7681         (if (if backward
7682                 (re-search-backward regexp nil t)
7683               (re-search-forward regexp nil t))
7684             ;; We found the regexp.
7685             (progn
7686               (setq found 'found)
7687               (beginning-of-line)
7688               (set-window-start
7689                (get-buffer-window (current-buffer))
7690                (point))
7691               (forward-line 1)
7692               (set-window-point
7693                (get-buffer-window (current-buffer))
7694                (point))
7695               (set-buffer sum)
7696               (setq point (point)))
7697           ;; We didn't find it, so we go to the next article.
7698           (set-buffer sum)
7699           (setq found 'not)
7700           (while (eq found 'not)
7701             (if (not (if backward (gnus-summary-find-prev)
7702                        (gnus-summary-find-next)))
7703                 ;; No more articles.
7704                 (setq found t)
7705               ;; Select the next article and adjust point.
7706               (unless (gnus-summary-article-sparse-p
7707                        (gnus-summary-article-number))
7708                 (setq found nil)
7709                 (gnus-summary-select-article)
7710                 (set-buffer gnus-article-buffer)
7711                 (widen)
7712                 (goto-char (if backward (point-max) (point-min))))))))
7713       (gnus-message 7 ""))
7714     ;; Return whether we found the regexp.
7715     (when (eq found 'found)
7716       (goto-char point)
7717       (gnus-summary-show-thread)
7718       (gnus-summary-goto-subject gnus-current-article)
7719       (gnus-summary-position-point)
7720       t)))
7721
7722 (defun gnus-find-matching-articles (header regexp)
7723   "Return a list of all articles that match REGEXP on HEADER.
7724 This search includes all articles in the current group that Gnus has
7725 fetched headers for, whether they are displayed or not."
7726   (let ((articles nil)
7727         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7728         (case-fold-search t))
7729     (dolist (header gnus-newsgroup-headers)
7730       (when (string-match regexp (funcall func header))
7731         (push (mail-header-number header) articles)))
7732     (nreverse articles)))
7733
7734 (defun gnus-summary-find-matching (header regexp &optional backward unread
7735                                           not-case-fold not-matching)
7736   "Return a list of all articles that match REGEXP on HEADER.
7737 The search stars on the current article and goes forwards unless
7738 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7739 If UNREAD is non-nil, only unread articles will
7740 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7741 in the comparisons. If NOT-MATCHING, return a list of all articles that
7742 not match REGEXP on HEADER."
7743   (let ((case-fold-search (not not-case-fold))
7744         articles d func)
7745     (if (consp header)
7746         (if (eq (car header) 'extra)
7747             (setq func
7748                   `(lambda (h)
7749                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7750                          "")))
7751           (error "%s is an invalid header" header))
7752       (unless (fboundp (intern (concat "mail-header-" header)))
7753         (error "%s is not a valid header" header))
7754       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7755     (dolist (d (if (eq backward 'all)
7756                    gnus-newsgroup-data
7757                  (gnus-data-find-list
7758                   (gnus-summary-article-number)
7759                   (gnus-data-list backward))))
7760       (when (and (or (not unread)       ; We want all articles...
7761                      (gnus-data-unread-p d)) ; Or just unreads.
7762                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7763                  (if not-matching
7764                      (not (string-match
7765                            regexp
7766                            (funcall func (gnus-data-header d))))
7767                    (string-match regexp
7768                                  (funcall func (gnus-data-header d)))))
7769         (push (gnus-data-number d) articles))) ; Success!
7770     (nreverse articles)))
7771
7772 (defun gnus-summary-execute-command (header regexp command &optional backward)
7773   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7774 If HEADER is an empty string (or nil), the match is done on the entire
7775 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7776   (interactive
7777    (list (let ((completion-ignore-case t))
7778            (completing-read
7779             "Header name: "
7780             (mapcar (lambda (header) (list (format "%s" header)))
7781                     (append
7782                      '("Number" "Subject" "From" "Lines" "Date"
7783                        "Message-ID" "Xref" "References" "Body")
7784                      gnus-extra-headers))
7785             nil 'require-match))
7786          (read-string "Regexp: ")
7787          (read-key-sequence "Command: ")
7788          current-prefix-arg))
7789   (when (equal header "Body")
7790     (setq header ""))
7791   ;; Hidden thread subtrees must be searched as well.
7792   (gnus-summary-show-all-threads)
7793   ;; We don't want to change current point nor window configuration.
7794   (save-excursion
7795     (save-window-excursion
7796       (gnus-message 6 "Executing %s..." (key-description command))
7797 ;; We'd like to execute COMMAND interactively so as to give arguments.
7798       (gnus-execute header regexp
7799                     `(call-interactively ',(key-binding command))
7800                     backward)
7801       (gnus-message 6 "Executing %s...done" (key-description command)))))
7802
7803 (defun gnus-summary-beginning-of-article ()
7804   "Scroll the article back to the beginning."
7805   (interactive)
7806   (gnus-summary-select-article)
7807   (gnus-configure-windows 'article)
7808   (gnus-eval-in-buffer-window gnus-article-buffer
7809     (widen)
7810     (goto-char (point-min))
7811     (when gnus-page-broken
7812       (gnus-narrow-to-page))))
7813
7814 (defun gnus-summary-end-of-article ()
7815   "Scroll to the end of the article."
7816   (interactive)
7817   (gnus-summary-select-article)
7818   (gnus-configure-windows 'article)
7819   (gnus-eval-in-buffer-window gnus-article-buffer
7820     (widen)
7821     (goto-char (point-max))
7822     (recenter -3)
7823     (when gnus-page-broken
7824       (gnus-narrow-to-page))))
7825
7826 (defun gnus-summary-print-truncate-and-quote (string &optional len)
7827   "Truncate to LEN and quote all \"(\"'s in STRING."
7828   (gnus-replace-in-string (if (and len (> (length string) len))
7829                               (substring string 0 len)
7830                             string)
7831                           "[()]" "\\\\\\&"))
7832
7833 (defun gnus-summary-print-article (&optional filename n)
7834   "Generate and print a PostScript image of the N next (mail) articles.
7835
7836 If N is negative, print the N previous articles.  If N is nil and articles
7837 have been marked with the process mark, print these instead.
7838
7839 If the optional first argument FILENAME is nil, send the image to the
7840 printer.  If FILENAME is a string, save the PostScript image in a file with
7841 that name.  If FILENAME is a number, prompt the user for the name of the file
7842 to save in."
7843   (interactive (list (ps-print-preprint current-prefix-arg)))
7844   (dolist (article (gnus-summary-work-articles n))
7845     (gnus-summary-select-article nil nil 'pseudo article)
7846     (gnus-eval-in-buffer-window gnus-article-buffer
7847       (let ((buffer (generate-new-buffer " *print*")))
7848         (unwind-protect
7849             (progn
7850               (copy-to-buffer buffer (point-min) (point-max))
7851               (set-buffer buffer)
7852               (gnus-article-delete-invisible-text)
7853               (when (gnus-visual-p 'article-highlight 'highlight)
7854                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7855                 ;; highlight.
7856                 (let ((gnus-article-buffer buffer))
7857                   (gnus-article-highlight-citation t)
7858                   (gnus-article-highlight-signature)))
7859               (let ((ps-left-header
7860                      (list
7861                       (concat "("
7862                               (gnus-summary-print-truncate-and-quote
7863                                (mail-header-subject gnus-current-headers)
7864                                66) ")")
7865                       (concat "("
7866                               (gnus-summary-print-truncate-and-quote
7867                                (mail-header-from gnus-current-headers)
7868                                45) ")")))
7869                     (ps-right-header
7870                      (list
7871                       "/pagenumberstring load"
7872                       (concat "("
7873                               (mail-header-date gnus-current-headers) ")"))))
7874                 (gnus-run-hooks 'gnus-ps-print-hook)
7875                 (save-excursion
7876                   (if window-system
7877                       (ps-spool-buffer-with-faces)
7878                     (ps-spool-buffer)))))
7879           (kill-buffer buffer))))
7880     (gnus-summary-remove-process-mark article))
7881   (ps-despool filename))
7882
7883 (defun gnus-summary-show-article (&optional arg)
7884   "Force redisplaying of the current article.
7885 If ARG (the prefix) is a number, show the article with the charset
7886 defined in `gnus-summary-show-article-charset-alist', or the charset
7887 input.
7888 If ARG (the prefix) is non-nil and not a number, show the raw article
7889 without any article massaging functions being run.  Normally, the key strokes 
7890 are `C-u g'."
7891   (interactive "P")
7892   (cond
7893    ((numberp arg)
7894     (gnus-summary-show-article t)
7895     (let ((gnus-newsgroup-charset
7896            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7897                (mm-read-coding-system
7898                 "View as charset: "
7899                 (save-excursion
7900                   (set-buffer gnus-article-buffer)
7901                   (let ((coding-systems
7902                          (detect-coding-region (point) (point-max))))
7903                     (or (car-safe coding-systems)
7904                         coding-systems))))))
7905           (gnus-newsgroup-ignored-charsets 'gnus-all))
7906       (gnus-summary-select-article nil 'force)
7907       (let ((deps gnus-newsgroup-dependencies)
7908             head header lines)
7909         (save-excursion
7910           (set-buffer gnus-original-article-buffer)
7911           (save-restriction
7912             (message-narrow-to-head)
7913             (setq head (buffer-string))
7914             (goto-char (point-min))
7915             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
7916               (goto-char (point-max))
7917               (widen)
7918               (setq lines (1- (count-lines (point) (point-max))))))
7919           (with-temp-buffer
7920             (insert (format "211 %d Article retrieved.\n"
7921                             (cdr gnus-article-current)))
7922             (insert head)
7923             (if lines (insert (format "Lines: %d\n" lines)))
7924             (insert ".\n")
7925             (let ((nntp-server-buffer (current-buffer)))
7926               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7927         (gnus-data-set-header
7928          (gnus-data-find (cdr gnus-article-current))
7929          header)
7930         (gnus-summary-update-article-line
7931          (cdr gnus-article-current) header)
7932         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7933           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7934    ((not arg)
7935     ;; Select the article the normal way.
7936     (gnus-summary-select-article nil 'force))
7937    (t
7938     ;; We have to require this here to make sure that the following
7939     ;; dynamic binding isn't shadowed by autoloading.
7940     (require 'gnus-async)
7941     (require 'gnus-art)
7942     ;; Bind the article treatment functions to nil.
7943     (let ((gnus-have-all-headers t)
7944           gnus-article-prepare-hook
7945           gnus-article-decode-hook
7946           gnus-display-mime-function
7947           gnus-break-pages)
7948       ;; Destroy any MIME parts.
7949       (when (gnus-buffer-live-p gnus-article-buffer)
7950         (save-excursion
7951           (set-buffer gnus-article-buffer)
7952           (mm-destroy-parts gnus-article-mime-handles)
7953           ;; Set it to nil for safety reason.
7954           (setq gnus-article-mime-handle-alist nil)
7955           (setq gnus-article-mime-handles nil)))
7956       (gnus-summary-select-article nil 'force))))
7957   (gnus-summary-goto-subject gnus-current-article)
7958   (gnus-summary-position-point))
7959
7960 (defun gnus-summary-show-raw-article ()
7961   "Show the raw article without any article massaging functions being run."
7962   (interactive)
7963   (gnus-summary-show-article t))
7964
7965 (defun gnus-summary-verbose-headers (&optional arg)
7966   "Toggle permanent full header display.
7967 If ARG is a positive number, turn header display on.
7968 If ARG is a negative number, turn header display off."
7969   (interactive "P")
7970   (setq gnus-show-all-headers
7971         (cond ((or (not (numberp arg))
7972                    (zerop arg))
7973                (not gnus-show-all-headers))
7974               ((natnump arg)
7975                t)))
7976   (gnus-summary-show-article))
7977
7978 (defun gnus-summary-toggle-header (&optional arg)
7979   "Show the headers if they are hidden, or hide them if they are shown.
7980 If ARG is a positive number, show the entire header.
7981 If ARG is a negative number, hide the unwanted header lines."
7982   (interactive "P")
7983   (save-excursion
7984     (set-buffer gnus-article-buffer)
7985     (save-restriction
7986       (let* ((buffer-read-only nil)
7987              (inhibit-point-motion-hooks t)
7988              hidden e)
7989         (setq hidden
7990               (if (numberp arg)
7991                   (>= arg 0)
7992                 (save-restriction
7993                   (article-narrow-to-head)
7994                   (gnus-article-hidden-text-p 'headers))))
7995         (goto-char (point-min))
7996         (when (search-forward "\n\n" nil t)
7997           (delete-region (point-min) (1- (point))))
7998         (goto-char (point-min))
7999         (save-excursion
8000           (set-buffer gnus-original-article-buffer)
8001           (goto-char (point-min))
8002           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8003         (insert-buffer-substring gnus-original-article-buffer 1 e)
8004         (save-restriction
8005           (narrow-to-region (point-min) (point))
8006           (article-decode-encoded-words)
8007           (if  hidden
8008               (let ((gnus-treat-hide-headers nil)
8009                     (gnus-treat-hide-boring-headers nil))
8010                 (setq gnus-article-wash-types
8011                       (delq 'headers gnus-article-wash-types))
8012                 (gnus-treat-article 'head))
8013             (gnus-treat-article 'head)))
8014         (gnus-set-mode-line 'article)))))
8015
8016 (defun gnus-summary-show-all-headers ()
8017   "Make all header lines visible."
8018   (interactive)
8019   (gnus-summary-toggle-header 1))
8020
8021 (defun gnus-summary-caesar-message (&optional arg)
8022   "Caesar rotate the current article by 13.
8023 The numerical prefix specifies how many places to rotate each letter
8024 forward."
8025   (interactive "P")
8026   (gnus-summary-select-article)
8027   (let ((mail-header-separator ""))
8028     (gnus-eval-in-buffer-window gnus-article-buffer
8029       (save-restriction
8030         (widen)
8031         (let ((start (window-start))
8032               buffer-read-only)
8033           (message-caesar-buffer-body arg)
8034           (set-window-start (get-buffer-window (current-buffer)) start))))))
8035
8036 (defun gnus-summary-stop-page-breaking ()
8037   "Stop page breaking in the current article."
8038   (interactive)
8039   (gnus-summary-select-article)
8040   (gnus-eval-in-buffer-window gnus-article-buffer
8041     (widen)
8042     (when (gnus-visual-p 'page-marker)
8043       (let ((buffer-read-only nil))
8044         (gnus-remove-text-with-property 'gnus-prev)
8045         (gnus-remove-text-with-property 'gnus-next))
8046       (setq gnus-page-broken nil))))
8047
8048 (defun gnus-summary-move-article (&optional n to-newsgroup
8049                                             select-method action)
8050   "Move the current article to a different newsgroup.
8051 If N is a positive number, move the N next articles.
8052 If N is a negative number, move the N previous articles.
8053 If N is nil and any articles have been marked with the process mark,
8054 move those articles instead.
8055 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8056 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8057 re-spool using this method.
8058
8059 For this function to work, both the current newsgroup and the
8060 newsgroup that you want to move to have to support the `request-move'
8061 and `request-accept' functions.
8062
8063 ACTION can be either `move' (the default), `crosspost' or `copy'."
8064   (interactive "P")
8065   (unless action
8066     (setq action 'move))
8067   ;; Check whether the source group supports the required functions.
8068   (cond ((and (eq action 'move)
8069               (not (gnus-check-backend-function
8070                     'request-move-article gnus-newsgroup-name)))
8071          (error "The current group does not support article moving"))
8072         ((and (eq action 'crosspost)
8073               (not (gnus-check-backend-function
8074                     'request-replace-article gnus-newsgroup-name)))
8075          (error "The current group does not support article editing")))
8076   (let ((articles (gnus-summary-work-articles n))
8077         (prefix (if (gnus-check-backend-function
8078                      'request-move-article gnus-newsgroup-name)
8079                     (gnus-group-real-prefix gnus-newsgroup-name)
8080                   ""))
8081         (names '((move "Move" "Moving")
8082                  (copy "Copy" "Copying")
8083                  (crosspost "Crosspost" "Crossposting")))
8084         (copy-buf (save-excursion
8085                     (nnheader-set-temp-buffer " *copy article*")))
8086         art-group to-method new-xref article to-groups)
8087     (unless (assq action names)
8088       (error "Unknown action %s" action))
8089     ;; Read the newsgroup name.
8090     (when (and (not to-newsgroup)
8091                (not select-method))
8092       (setq to-newsgroup
8093             (gnus-read-move-group-name
8094              (cadr (assq action names))
8095              (symbol-value (intern (format "gnus-current-%s-group" action)))
8096              articles prefix))
8097       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8098     (setq to-method (or select-method
8099                         (gnus-server-to-method
8100                          (gnus-group-method to-newsgroup))))
8101     ;; Check the method we are to move this article to...
8102     (unless (gnus-check-backend-function
8103              'request-accept-article (car to-method))
8104       (error "%s does not support article copying" (car to-method)))
8105     (unless (gnus-check-server to-method)
8106       (error "Can't open server %s" (car to-method)))
8107     (gnus-message 6 "%s to %s: %s..."
8108                   (caddr (assq action names))
8109                   (or (car select-method) to-newsgroup) articles)
8110     (while articles
8111       (setq article (pop articles))
8112       (setq
8113        art-group
8114        (cond
8115         ;; Move the article.
8116         ((eq action 'move)
8117          ;; Remove this article from future suppression.
8118          (gnus-dup-unsuppress-article article)
8119          (gnus-request-move-article
8120           article                       ; Article to move
8121           gnus-newsgroup-name           ; From newsgroup
8122           (nth 1 (gnus-find-method-for-group
8123                   gnus-newsgroup-name)) ; Server
8124           (list 'gnus-request-accept-article
8125                 to-newsgroup (list 'quote select-method)
8126                 (not articles) t)       ; Accept form
8127           (not articles)))              ; Only save nov last time
8128         ;; Copy the article.
8129         ((eq action 'copy)
8130          (save-excursion
8131            (set-buffer copy-buf)
8132            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8133              (gnus-request-accept-article
8134               to-newsgroup select-method (not articles) t))))
8135         ;; Crosspost the article.
8136         ((eq action 'crosspost)
8137          (let ((xref (message-tokenize-header
8138                       (mail-header-xref (gnus-summary-article-header article))
8139                       " ")))
8140            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8141                                   ":" (number-to-string article)))
8142            (unless xref
8143              (setq xref (list (system-name))))
8144            (setq new-xref
8145                  (concat
8146                   (mapconcat 'identity
8147                              (delete "Xref:" (delete new-xref xref))
8148                              " ")
8149                   " " new-xref))
8150            (save-excursion
8151              (set-buffer copy-buf)
8152              ;; First put the article in the destination group.
8153              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8154              (when (consp (setq art-group
8155                                 (gnus-request-accept-article
8156                                  to-newsgroup select-method (not articles))))
8157                (setq new-xref (concat new-xref " " (car art-group)
8158                                       ":"
8159                                       (number-to-string (cdr art-group))))
8160                ;; Now we have the new Xrefs header, so we insert
8161                ;; it and replace the new article.
8162                (nnheader-replace-header "Xref" new-xref)
8163                (gnus-request-replace-article
8164                 (cdr art-group) to-newsgroup (current-buffer))
8165                art-group))))))
8166       (cond
8167        ((not art-group)
8168         (gnus-message 1 "Couldn't %s article %s: %s"
8169                       (cadr (assq action names)) article
8170                       (nnheader-get-report (car to-method))))
8171        ((eq art-group 'junk)
8172         (when (eq action 'move)
8173           (gnus-summary-mark-article article gnus-canceled-mark)
8174           (gnus-message 4 "Deleted article %s" article)))
8175        (t
8176         (let* ((pto-group (gnus-group-prefixed-name
8177                            (car art-group) to-method))
8178                (entry
8179                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8180                (info (nth 2 entry))
8181                (to-group (gnus-info-group info))
8182                to-marks)
8183           ;; Update the group that has been moved to.
8184           (when (and info
8185                      (memq action '(move copy)))
8186             (unless (member to-group to-groups)
8187               (push to-group to-groups))
8188
8189             (unless (memq article gnus-newsgroup-unreads)
8190               (push 'read to-marks)
8191               (gnus-info-set-read
8192                info (gnus-add-to-range (gnus-info-read info)
8193                                        (list (cdr art-group)))))
8194
8195             ;; See whether the article is to be put in the cache.
8196             (let ((marks gnus-article-mark-lists)
8197                   (to-article (cdr art-group)))
8198
8199               ;; Enter the article into the cache in the new group,
8200               ;; if that is required.
8201               (when gnus-use-cache
8202                 (gnus-cache-possibly-enter-article
8203                  to-group to-article
8204                  (memq article gnus-newsgroup-marked)
8205                  (memq article gnus-newsgroup-dormant)
8206                  (memq article gnus-newsgroup-unreads)))
8207
8208               (when gnus-preserve-marks
8209                 ;; Copy any marks over to the new group.
8210                 (when (and (equal to-group gnus-newsgroup-name)
8211                            (not (memq article gnus-newsgroup-unreads)))
8212                   ;; Mark this article as read in this group.
8213                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8214                   (setcdr (gnus-active to-group) to-article)
8215                   (setcdr gnus-newsgroup-active to-article))
8216
8217                 (while marks
8218                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8219                     (when (memq article (symbol-value
8220                                          (intern (format "gnus-newsgroup-%s"
8221                                                          (caar marks)))))
8222                       (push (cdar marks) to-marks)
8223                       ;; If the other group is the same as this group,
8224                       ;; then we have to add the mark to the list.
8225                       (when (equal to-group gnus-newsgroup-name)
8226                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8227                              (cons to-article
8228                                    (symbol-value
8229                                     (intern (format "gnus-newsgroup-%s"
8230                                                     (caar marks)))))))
8231                       ;; Copy the marks to other group.
8232                       (gnus-add-marked-articles
8233                        to-group (cdar marks) (list to-article) info)))
8234                   (setq marks (cdr marks)))
8235
8236                 (gnus-request-set-mark to-group (list (list (list to-article)
8237                                                             'add
8238                                                             to-marks))))
8239
8240               (gnus-dribble-enter
8241                (concat "(gnus-group-set-info '"
8242                        (gnus-prin1-to-string (gnus-get-info to-group))
8243                        ")"))))
8244
8245           ;; Update the Xref header in this article to point to
8246           ;; the new crossposted article we have just created.
8247           (when (eq action 'crosspost)
8248             (save-excursion
8249               (set-buffer copy-buf)
8250               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8251               (nnheader-replace-header "Xref" new-xref)
8252               (gnus-request-replace-article
8253                article gnus-newsgroup-name (current-buffer)))))
8254
8255         ;;;!!!Why is this necessary?
8256         (set-buffer gnus-summary-buffer)
8257
8258         (gnus-summary-goto-subject article)
8259         (when (eq action 'move)
8260           (gnus-summary-mark-article article gnus-canceled-mark))))
8261       (gnus-summary-remove-process-mark article))
8262     ;; Re-activate all groups that have been moved to.
8263     (save-excursion
8264       (set-buffer gnus-group-buffer)
8265       (let ((gnus-group-marked to-groups))
8266         (gnus-group-get-new-news-this-group nil t)))
8267
8268     (gnus-kill-buffer copy-buf)
8269     (gnus-summary-position-point)
8270     (gnus-set-mode-line 'summary)))
8271
8272 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8273   "Move the current article to a different newsgroup.
8274 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8275 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8276 re-spool using this method."
8277   (interactive "P")
8278   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8279
8280 (defun gnus-summary-crosspost-article (&optional n)
8281   "Crosspost the current article to some other group."
8282   (interactive "P")
8283   (gnus-summary-move-article n nil nil 'crosspost))
8284
8285 (defcustom gnus-summary-respool-default-method nil
8286   "Default method type for respooling an article.
8287 If nil, use to the current newsgroup method."
8288   :type 'symbol
8289   :group 'gnus-summary-mail)
8290
8291 (defun gnus-summary-respool-article (&optional n method)
8292   "Respool the current article.
8293 The article will be squeezed through the mail spooling process again,
8294 which means that it will be put in some mail newsgroup or other
8295 depending on `nnmail-split-methods'.
8296 If N is a positive number, respool the N next articles.
8297 If N is a negative number, respool the N previous articles.
8298 If N is nil and any articles have been marked with the process mark,
8299 respool those articles instead.
8300
8301 Respooling can be done both from mail groups and \"real\" newsgroups.
8302 In the former case, the articles in question will be moved from the
8303 current group into whatever groups they are destined to.  In the
8304 latter case, they will be copied into the relevant groups."
8305   (interactive
8306    (list current-prefix-arg
8307          (let* ((methods (gnus-methods-using 'respool))
8308                 (methname
8309                  (symbol-name (or gnus-summary-respool-default-method
8310                                   (car (gnus-find-method-for-group
8311                                         gnus-newsgroup-name)))))
8312                 (method
8313                  (gnus-completing-read
8314                   methname "What backend do you want to use when respooling?"
8315                   methods nil t nil 'gnus-mail-method-history))
8316                 ms)
8317            (cond
8318             ((zerop (length (setq ms (gnus-servers-using-backend
8319                                       (intern method)))))
8320              (list (intern method) ""))
8321             ((= 1 (length ms))
8322              (car ms))
8323             (t
8324              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8325                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8326                            ms-alist))))))))
8327   (unless method
8328     (error "No method given for respooling"))
8329   (if (assoc (symbol-name
8330               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8331              (gnus-methods-using 'respool))
8332       (gnus-summary-move-article n nil method)
8333     (gnus-summary-copy-article n nil method)))
8334
8335 (defun gnus-summary-import-article (file &optional edit)
8336   "Import an arbitrary file into a mail newsgroup."
8337   (interactive "fImport file: \nP")
8338   (let ((group gnus-newsgroup-name)
8339         (now (current-time))
8340         atts lines group-art)
8341     (unless (gnus-check-backend-function 'request-accept-article group)
8342       (error "%s does not support article importing" group))
8343     (or (file-readable-p file)
8344         (not (file-regular-p file))
8345         (error "Can't read %s" file))
8346     (save-excursion
8347       (set-buffer (gnus-get-buffer-create " *import file*"))
8348       (erase-buffer)
8349       (nnheader-insert-file-contents file)
8350       (goto-char (point-min))
8351       (if (nnheader-article-p)
8352           (save-restriction
8353             (goto-char (point-min))
8354             (search-forward "\n\n" nil t)
8355             (narrow-to-region (point-min) (1- (point)))
8356             (goto-char (point-min))
8357             (unless (re-search-forward "^date:" nil t)
8358               (goto-char (point-max))
8359               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8360        ;; This doesn't look like an article, so we fudge some headers.
8361         (setq atts (file-attributes file)
8362               lines (count-lines (point-min) (point-max)))
8363         (insert "From: " (read-string "From: ") "\n"
8364                 "Subject: " (read-string "Subject: ") "\n"
8365                 "Date: " (message-make-date (nth 5 atts)) "\n"
8366                 "Message-ID: " (message-make-message-id) "\n"
8367                 "Lines: " (int-to-string lines) "\n"
8368                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8369       (setq group-art (gnus-request-accept-article group nil t))
8370       (kill-buffer (current-buffer)))
8371     (setq gnus-newsgroup-active (gnus-activate-group group))
8372     (forward-line 1)
8373     (gnus-summary-goto-article (cdr group-art) nil t)
8374     (when edit
8375       (gnus-summary-edit-article))))
8376
8377 (defun gnus-summary-create-article ()
8378   "Create an article in a mail newsgroup."
8379   (interactive)
8380   (let ((group gnus-newsgroup-name)
8381         (now (current-time))
8382         group-art)
8383     (unless (gnus-check-backend-function 'request-accept-article group)
8384       (error "%s does not support article importing" group))
8385     (save-excursion
8386       (set-buffer (gnus-get-buffer-create " *import file*"))
8387       (erase-buffer)
8388       (goto-char (point-min))
8389       ;; This doesn't look like an article, so we fudge some headers.
8390       (insert "From: " (read-string "From: ") "\n"
8391               "Subject: " (read-string "Subject: ") "\n"
8392               "Date: " (message-make-date now) "\n"
8393               "Message-ID: " (message-make-message-id) "\n")
8394       (setq group-art (gnus-request-accept-article group nil t))
8395       (kill-buffer (current-buffer)))
8396     (setq gnus-newsgroup-active (gnus-activate-group group))
8397     (forward-line 1)
8398     (gnus-summary-goto-article (cdr group-art) nil t)
8399     (gnus-summary-edit-article)))
8400
8401 (defun gnus-summary-article-posted-p ()
8402   "Say whether the current (mail) article is available from news as well.
8403 This will be the case if the article has both been mailed and posted."
8404   (interactive)
8405   (let ((id (mail-header-references (gnus-summary-article-header)))
8406         (gnus-override-method (car (gnus-refer-article-methods))))
8407     (if (gnus-request-head id "")
8408         (gnus-message 2 "The current message was found on %s"
8409                       gnus-override-method)
8410       (gnus-message 2 "The current message couldn't be found on %s"
8411                     gnus-override-method)
8412       nil)))
8413
8414 (defun gnus-summary-expire-articles (&optional now)
8415   "Expire all articles that are marked as expirable in the current group."
8416   (interactive)
8417   (when (gnus-check-backend-function
8418          'request-expire-articles gnus-newsgroup-name)
8419     ;; This backend supports expiry.
8420     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8421            (expirable (if total
8422                           (progn
8423                             ;; We need to update the info for
8424                             ;; this group for `gnus-list-of-read-articles'
8425                             ;; to give us the right answer.
8426                             (gnus-run-hooks 'gnus-exit-group-hook)
8427                             (gnus-summary-update-info)
8428                             (gnus-list-of-read-articles gnus-newsgroup-name))
8429                         (setq gnus-newsgroup-expirable
8430                               (sort gnus-newsgroup-expirable '<))))
8431            (expiry-wait (if now 'immediate
8432                           (gnus-group-find-parameter
8433                            gnus-newsgroup-name 'expiry-wait)))
8434            (nnmail-expiry-target
8435             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8436                 nnmail-expiry-target))
8437            es)
8438       (when expirable
8439         ;; There are expirable articles in this group, so we run them
8440         ;; through the expiry process.
8441         (gnus-message 6 "Expiring articles...")
8442         (unless (gnus-check-group gnus-newsgroup-name)
8443           (error "Can't open server for %s" gnus-newsgroup-name))
8444         ;; The list of articles that weren't expired is returned.
8445         (save-excursion
8446           (if expiry-wait
8447               (let ((nnmail-expiry-wait-function nil)
8448                     (nnmail-expiry-wait expiry-wait))
8449                 (setq es (gnus-request-expire-articles
8450                           expirable gnus-newsgroup-name)))
8451             (setq es (gnus-request-expire-articles
8452                       expirable gnus-newsgroup-name)))
8453           (unless total
8454             (setq gnus-newsgroup-expirable es))
8455           ;; We go through the old list of expirable, and mark all
8456           ;; really expired articles as nonexistent.
8457           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8458             (let ((gnus-use-cache nil))
8459               (while expirable
8460                 (unless (memq (car expirable) es)
8461                   (when (gnus-data-find (car expirable))
8462                     (gnus-summary-mark-article
8463                      (car expirable) gnus-canceled-mark)))
8464                 (setq expirable (cdr expirable))))))
8465         (gnus-message 6 "Expiring articles...done")))))
8466
8467 (defun gnus-summary-expire-articles-now ()
8468   "Expunge all expirable articles in the current group.
8469 This means that *all* articles that are marked as expirable will be
8470 deleted forever, right now."
8471   (interactive)
8472   (or gnus-expert-user
8473       (gnus-yes-or-no-p
8474        "Are you really, really, really sure you want to delete all these messages? ")
8475       (error "Phew!"))
8476   (gnus-summary-expire-articles t))
8477
8478 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8479 (defun gnus-summary-delete-article (&optional n)
8480   "Delete the N next (mail) articles.
8481 This command actually deletes articles.  This is not a marking
8482 command.  The article will disappear forever from your life, never to
8483 return.
8484 If N is negative, delete backwards.
8485 If N is nil and articles have been marked with the process mark,
8486 delete these instead."
8487   (interactive "P")
8488   (unless (gnus-check-backend-function 'request-expire-articles
8489                                        gnus-newsgroup-name)
8490     (error "The current newsgroup does not support article deletion"))
8491   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8492     (error "Couldn't open server"))
8493   ;; Compute the list of articles to delete.
8494   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8495         not-deleted)
8496     (if (and gnus-novice-user
8497              (not (gnus-yes-or-no-p
8498                    (format "Do you really want to delete %s forever? "
8499                            (if (> (length articles) 1)
8500                                (format "these %s articles" (length articles))
8501                              "this article")))))
8502         ()
8503       ;; Delete the articles.
8504       (setq not-deleted (gnus-request-expire-articles
8505                          articles gnus-newsgroup-name 'force))
8506       (while articles
8507         (gnus-summary-remove-process-mark (car articles))
8508         ;; The backend might not have been able to delete the article
8509         ;; after all.
8510         (unless (memq (car articles) not-deleted)
8511           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8512         (setq articles (cdr articles)))
8513       (when not-deleted
8514         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8515     (gnus-summary-position-point)
8516     (gnus-set-mode-line 'summary)
8517     not-deleted))
8518
8519 (defun gnus-summary-edit-article (&optional arg)
8520   "Edit the current article.
8521 This will have permanent effect only in mail groups.
8522 If ARG is nil, edit the decoded articles.
8523 If ARG is 1, edit the raw articles.
8524 If ARG is 2, edit the raw articles even in read-only groups.
8525 If ARG is 3, edit the articles with the current handles.
8526 Otherwise, allow editing of articles even in read-only
8527 groups."
8528   (interactive "P")
8529   (let (force raw current-handles)
8530     (cond
8531      ((null arg))
8532      ((eq arg 1) (setq raw t))
8533      ((eq arg 2) (setq raw t
8534                        force t))
8535      ((eq arg 3) (setq current-handles
8536                        (and (gnus-buffer-live-p gnus-article-buffer)
8537                             (with-current-buffer gnus-article-buffer
8538                               (prog1
8539                                   gnus-article-mime-handles
8540                                 (setq gnus-article-mime-handles nil))))))
8541      (t (setq force t)))
8542     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8543         (error "Can't edit the raw article in group nndraft:drafts"))
8544     (save-excursion
8545       (set-buffer gnus-summary-buffer)
8546       (let ((mail-parse-charset gnus-newsgroup-charset)
8547             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8548         (gnus-set-global-variables)
8549         (when (and (not force)
8550                    (gnus-group-read-only-p))
8551           (error "The current newsgroup does not support article editing"))
8552         (gnus-summary-show-article t)
8553         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8554           (with-current-buffer gnus-article-buffer
8555             (mm-enable-multibyte-mule4)))
8556         (if (equal gnus-newsgroup-name "nndraft:drafts")
8557             (setq raw t))
8558         (gnus-article-edit-article
8559          (if raw 'ignore
8560            `(lambda ()
8561               (let ((mbl mml-buffer-list))
8562                 (setq mml-buffer-list nil)
8563                 (mime-to-mml ,'current-handles)
8564                 (let ((mbl1 mml-buffer-list))
8565                   (setq mml-buffer-list mbl)
8566                   (set (make-local-variable 'mml-buffer-list) mbl1))
8567                 (make-local-hook 'kill-buffer-hook)
8568                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8569          `(lambda (no-highlight)
8570             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8571                   (message-options message-options)
8572                   (message-options-set-recipient)
8573                   (mail-parse-ignored-charsets
8574                    ',gnus-newsgroup-ignored-charsets))
8575               ,(if (not raw) '(progn
8576                                 (mml-to-mime)
8577                                 (mml-destroy-buffers)
8578                                 (remove-hook 'kill-buffer-hook
8579                                              'mml-destroy-buffers t)
8580                                 (kill-local-variable 'mml-buffer-list)))
8581               (gnus-summary-edit-article-done
8582                ,(or (mail-header-references gnus-current-headers) "")
8583                ,(gnus-group-read-only-p)
8584                ,gnus-summary-buffer no-highlight))))))))
8585
8586 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8587
8588 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8589                                                  no-highlight)
8590   "Make edits to the current article permanent."
8591   (interactive)
8592   (save-excursion
8593    ;; The buffer restriction contains the entire article if it exists.
8594     (when (article-goto-body)
8595       (let ((lines (count-lines (point) (point-max)))
8596             (length (- (point-max) (point)))
8597             (case-fold-search t)
8598             (body (copy-marker (point))))
8599         (goto-char (point-min))
8600         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8601           (delete-region (match-beginning 1) (match-end 1))
8602           (insert (number-to-string length)))
8603         (goto-char (point-min))
8604         (when (re-search-forward
8605                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8606           (delete-region (match-beginning 1) (match-end 1))
8607           (insert (number-to-string length)))
8608         (goto-char (point-min))
8609         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8610           (delete-region (match-beginning 1) (match-end 1))
8611           (insert (number-to-string lines))))))
8612   ;; Replace the article.
8613   (let ((buf (current-buffer)))
8614     (with-temp-buffer
8615       (insert-buffer-substring buf)
8616
8617       (if (and (not read-only)
8618                (not (gnus-request-replace-article
8619                      (cdr gnus-article-current) (car gnus-article-current)
8620                      (current-buffer) t)))
8621           (error "Couldn't replace article")
8622         ;; Update the summary buffer.
8623         (if (and references
8624                  (equal (message-tokenize-header references " ")
8625                         (message-tokenize-header
8626                          (or (message-fetch-field "references") "") " ")))
8627             ;; We only have to update this line.
8628             (save-excursion
8629               (save-restriction
8630                 (message-narrow-to-head)
8631                 (let ((head (buffer-string))
8632                       header)
8633                   (with-temp-buffer
8634                     (insert (format "211 %d Article retrieved.\n"
8635                                     (cdr gnus-article-current)))
8636                     (insert head)
8637                     (insert ".\n")
8638                     (let ((nntp-server-buffer (current-buffer)))
8639                       (setq header (car (gnus-get-newsgroup-headers
8640                                          (save-excursion
8641                                            (set-buffer gnus-summary-buffer)
8642                                            gnus-newsgroup-dependencies)
8643                                          t))))
8644                     (save-excursion
8645                       (set-buffer gnus-summary-buffer)
8646                       (gnus-data-set-header
8647                        (gnus-data-find (cdr gnus-article-current))
8648                        header)
8649                       (gnus-summary-update-article-line
8650                        (cdr gnus-article-current) header)
8651                       (if (gnus-summary-goto-subject
8652                            (cdr gnus-article-current) nil t)
8653                           (gnus-summary-update-secondary-mark
8654                            (cdr gnus-article-current))))))))
8655           ;; Update threads.
8656           (set-buffer (or buffer gnus-summary-buffer))
8657           (gnus-summary-update-article (cdr gnus-article-current))
8658           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8659               (gnus-summary-update-secondary-mark
8660                (cdr gnus-article-current))))
8661         ;; Prettify the article buffer again.
8662         (unless no-highlight
8663           (save-excursion
8664             (set-buffer gnus-article-buffer)
8665             ;;;!!! Fix this -- article should be rehighlighted.
8666             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8667             (set-buffer gnus-original-article-buffer)
8668             (gnus-request-article
8669              (cdr gnus-article-current)
8670              (car gnus-article-current) (current-buffer))))
8671         ;; Prettify the summary buffer line.
8672         (when (gnus-visual-p 'summary-highlight 'highlight)
8673           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8674
8675 (defun gnus-summary-edit-wash (key)
8676   "Perform editing command KEY in the article buffer."
8677   (interactive
8678    (list
8679     (progn
8680       (message "%s" (concat (this-command-keys) "- "))
8681       (read-char))))
8682   (message "")
8683   (gnus-summary-edit-article)
8684   (execute-kbd-macro (concat (this-command-keys) key))
8685   (gnus-article-edit-done))
8686
8687
8688 (defun gnus-summary-toggle-smiley (&optional arg)
8689   "Toggle the display of smilies as small graphical icons."
8690   (interactive "P")
8691   (save-excursion
8692     (set-buffer gnus-article-buffer)
8693     (gnus-smiley-display arg)
8694     ))
8695
8696 ;;; Respooling
8697
8698 (defun gnus-summary-respool-query (&optional silent trace)
8699   "Query where the respool algorithm would put this article."
8700   (interactive)
8701   (let (gnus-mark-article-hook)
8702     (gnus-summary-select-article)
8703     (save-excursion
8704       (set-buffer gnus-original-article-buffer)
8705       (save-restriction
8706         (message-narrow-to-head)
8707         (let ((groups (nnmail-article-group 'identity trace)))
8708           (unless silent
8709             (if groups
8710                 (message "This message would go to %s"
8711                          (mapconcat 'car groups ", "))
8712               (message "This message would go to no groups"))
8713             groups))))))
8714
8715 (defun gnus-summary-respool-trace ()
8716   "Trace where the respool algorithm would put this article.
8717 Display a buffer showing all fancy splitting patterns which matched."
8718   (interactive)
8719   (gnus-summary-respool-query nil t))
8720
8721 ;; Summary marking commands.
8722
8723 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8724   "Mark articles which has the same subject as read, and then select the next.
8725 If UNMARK is positive, remove any kind of mark.
8726 If UNMARK is negative, tick articles."
8727   (interactive "P")
8728   (when unmark
8729     (setq unmark (prefix-numeric-value unmark)))
8730   (let ((count
8731          (gnus-summary-mark-same-subject
8732           (gnus-summary-article-subject) unmark)))
8733     ;; Select next unread article.  If auto-select-same mode, should
8734     ;; select the first unread article.
8735     (gnus-summary-next-article t (and gnus-auto-select-same
8736                                       (gnus-summary-article-subject)))
8737     (gnus-message 7 "%d article%s marked as %s"
8738                   count (if (= count 1) " is" "s are")
8739                   (if unmark "unread" "read"))))
8740
8741 (defun gnus-summary-kill-same-subject (&optional unmark)
8742   "Mark articles which has the same subject as read.
8743 If UNMARK is positive, remove any kind of mark.
8744 If UNMARK is negative, tick articles."
8745   (interactive "P")
8746   (when unmark
8747     (setq unmark (prefix-numeric-value unmark)))
8748   (let ((count
8749          (gnus-summary-mark-same-subject
8750           (gnus-summary-article-subject) unmark)))
8751     ;; If marked as read, go to next unread subject.
8752     (when (null unmark)
8753       ;; Go to next unread subject.
8754       (gnus-summary-next-subject 1 t))
8755     (gnus-message 7 "%d articles are marked as %s"
8756                   count (if unmark "unread" "read"))))
8757
8758 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8759   "Mark articles with same SUBJECT as read, and return marked number.
8760 If optional argument UNMARK is positive, remove any kinds of marks.
8761 If optional argument UNMARK is negative, mark articles as unread instead."
8762   (let ((count 1))
8763     (save-excursion
8764       (cond
8765        ((null unmark)                   ; Mark as read.
8766         (while (and
8767                 (progn
8768                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8769                   (gnus-summary-show-thread) t)
8770                 (gnus-summary-find-subject subject))
8771           (setq count (1+ count))))
8772        ((> unmark 0)                    ; Tick.
8773         (while (and
8774                 (progn
8775                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8776                   (gnus-summary-show-thread) t)
8777                 (gnus-summary-find-subject subject))
8778           (setq count (1+ count))))
8779        (t                               ; Mark as unread.
8780         (while (and
8781                 (progn
8782                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8783                   (gnus-summary-show-thread) t)
8784                 (gnus-summary-find-subject subject))
8785           (setq count (1+ count)))))
8786       (gnus-set-mode-line 'summary)
8787       ;; Return the number of marked articles.
8788       count)))
8789
8790 (defun gnus-summary-mark-as-processable (n &optional unmark)
8791   "Set the process mark on the next N articles.
8792 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8793 the process mark instead.  The difference between N and the actual
8794 number of articles marked is returned."
8795   (interactive "P")
8796   (if (and (null n) (gnus-region-active-p))
8797       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8798     (setq n (prefix-numeric-value n))
8799     (let ((backward (< n 0))
8800           (n (abs n)))
8801       (while (and
8802               (> n 0)
8803               (if unmark
8804                   (gnus-summary-remove-process-mark
8805                    (gnus-summary-article-number))
8806                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8807               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8808         (setq n (1- n)))
8809       (when (/= 0 n)
8810         (gnus-message 7 "No more articles"))
8811       (gnus-summary-recenter)
8812       (gnus-summary-position-point)
8813       n)))
8814
8815 (defun gnus-summary-unmark-as-processable (n)
8816   "Remove the process mark from the next N articles.
8817 If N is negative, unmark backward instead.  The difference between N and
8818 the actual number of articles unmarked is returned."
8819   (interactive "P")
8820   (gnus-summary-mark-as-processable n t))
8821
8822 (defun gnus-summary-unmark-all-processable ()
8823   "Remove the process mark from all articles."
8824   (interactive)
8825   (save-excursion
8826     (while gnus-newsgroup-processable
8827       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8828   (gnus-summary-position-point))
8829
8830 (defun gnus-summary-add-mark (article type)
8831   "Mark ARTICLE with a mark of TYPE."
8832   (let ((vtype (car (assq type gnus-article-mark-lists)))
8833         var)
8834     (if (not vtype)
8835         (error "No such mark type: %s" type)
8836       (setq var (intern (format "gnus-newsgroup-%s" type)))
8837       (set var (cons article (symbol-value var)))
8838       (if (memq type '(processable cached replied forwarded recent saved))
8839           (gnus-summary-update-secondary-mark article)
8840         ;;; !!! This is bogus.  We should find out what primary
8841         ;;; !!! mark we want to set.
8842         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8843
8844 (defun gnus-summary-mark-as-expirable (n)
8845   "Mark N articles forward as expirable.
8846 If N is negative, mark backward instead.  The difference between N and
8847 the actual number of articles marked is returned."
8848   (interactive "p")
8849   (gnus-summary-mark-forward n gnus-expirable-mark))
8850
8851 (defun gnus-summary-mark-article-as-replied (article)
8852   "Mark ARTICLE as replied to and update the summary line.
8853 ARTICLE can also be a list of articles."
8854   (interactive (list (gnus-summary-article-number)))
8855   (let ((articles (if (listp article) article (list article))))
8856     (dolist (article articles)
8857       (push article gnus-newsgroup-replied)
8858       (let ((buffer-read-only nil))
8859         (when (gnus-summary-goto-subject article nil t)
8860           (gnus-summary-update-secondary-mark article))))))
8861
8862 (defun gnus-summary-mark-article-as-forwarded (article)
8863   "Mark ARTICLE as forwarded and update the summary line.
8864 ARTICLE can also be a list of articles."
8865   (let ((articles (if (listp article) article (list article))))
8866     (dolist (article articles)
8867       (push article gnus-newsgroup-forwarded)
8868       (let ((buffer-read-only nil))
8869         (when (gnus-summary-goto-subject article nil t)
8870           (gnus-summary-update-secondary-mark article))))))
8871
8872 (defun gnus-summary-set-bookmark (article)
8873   "Set a bookmark in current article."
8874   (interactive (list (gnus-summary-article-number)))
8875   (when (or (not (get-buffer gnus-article-buffer))
8876             (not gnus-current-article)
8877             (not gnus-article-current)
8878             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8879     (error "No current article selected"))
8880   ;; Remove old bookmark, if one exists.
8881   (let ((old (assq article gnus-newsgroup-bookmarks)))
8882     (when old
8883       (setq gnus-newsgroup-bookmarks
8884             (delq old gnus-newsgroup-bookmarks))))
8885   ;; Set the new bookmark, which is on the form
8886   ;; (article-number . line-number-in-body).
8887   (push
8888    (cons article
8889          (save-excursion
8890            (set-buffer gnus-article-buffer)
8891            (count-lines
8892             (min (point)
8893                  (save-excursion
8894                    (goto-char (point-min))
8895                    (search-forward "\n\n" nil t)
8896                    (point)))
8897             (point))))
8898    gnus-newsgroup-bookmarks)
8899   (gnus-message 6 "A bookmark has been added to the current article."))
8900
8901 (defun gnus-summary-remove-bookmark (article)
8902   "Remove the bookmark from the current article."
8903   (interactive (list (gnus-summary-article-number)))
8904   ;; Remove old bookmark, if one exists.
8905   (let ((old (assq article gnus-newsgroup-bookmarks)))
8906     (if old
8907         (progn
8908           (setq gnus-newsgroup-bookmarks
8909                 (delq old gnus-newsgroup-bookmarks))
8910           (gnus-message 6 "Removed bookmark."))
8911       (gnus-message 6 "No bookmark in current article."))))
8912
8913 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8914 (defun gnus-summary-mark-as-dormant (n)
8915   "Mark N articles forward as dormant.
8916 If N is negative, mark backward instead.  The difference between N and
8917 the actual number of articles marked is returned."
8918   (interactive "p")
8919   (gnus-summary-mark-forward n gnus-dormant-mark))
8920
8921 (defun gnus-summary-set-process-mark (article)
8922   "Set the process mark on ARTICLE and update the summary line."
8923   (setq gnus-newsgroup-processable
8924         (cons article
8925               (delq article gnus-newsgroup-processable)))
8926   (when (gnus-summary-goto-subject article)
8927     (gnus-summary-show-thread)
8928     (gnus-summary-goto-subject article)
8929     (gnus-summary-update-secondary-mark article)))
8930
8931 (defun gnus-summary-remove-process-mark (article)
8932   "Remove the process mark from ARTICLE and update the summary line."
8933   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8934   (when (gnus-summary-goto-subject article)
8935     (gnus-summary-show-thread)
8936     (gnus-summary-goto-subject article)
8937     (gnus-summary-update-secondary-mark article)))
8938
8939 (defun gnus-summary-set-saved-mark (article)
8940   "Set the process mark on ARTICLE and update the summary line."
8941   (push article gnus-newsgroup-saved)
8942   (when (gnus-summary-goto-subject article)
8943     (gnus-summary-update-secondary-mark article)))
8944
8945 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8946   "Mark N articles as read forwards.
8947 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8948 The difference between N and the actual number of articles marked is
8949 returned.
8950 Iff NO-EXPIRE, auto-expiry will be inhibited."
8951   (interactive "p")
8952   (gnus-summary-show-thread)
8953   (let ((backward (< n 0))
8954         (gnus-summary-goto-unread
8955          (and gnus-summary-goto-unread
8956               (not (eq gnus-summary-goto-unread 'never))
8957               (not (memq mark (list gnus-unread-mark
8958                                     gnus-ticked-mark gnus-dormant-mark)))))
8959         (n (abs n))
8960         (mark (or mark gnus-del-mark)))
8961     (while (and (> n 0)
8962                 (gnus-summary-mark-article nil mark no-expire)
8963                 (zerop (gnus-summary-next-subject
8964                         (if backward -1 1)
8965                         (and gnus-summary-goto-unread
8966                              (not (eq gnus-summary-goto-unread 'never)))
8967                         t)))
8968       (setq n (1- n)))
8969     (when (/= 0 n)
8970       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8971     (gnus-summary-recenter)
8972     (gnus-summary-position-point)
8973     (gnus-set-mode-line 'summary)
8974     n))
8975
8976 (defun gnus-summary-mark-article-as-read (mark)
8977   "Mark the current article quickly as read with MARK."
8978   (let ((article (gnus-summary-article-number)))
8979     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8980     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8981     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8982     (push (cons article mark) gnus-newsgroup-reads)
8983     ;; Possibly remove from cache, if that is used.
8984     (when gnus-use-cache
8985       (gnus-cache-enter-remove-article article))
8986     ;; Allow the backend to change the mark.
8987     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8988     ;; Check for auto-expiry.
8989     (when (and gnus-newsgroup-auto-expire
8990                (memq mark gnus-auto-expirable-marks))
8991       (setq mark gnus-expirable-mark)
8992       ;; Let the backend know about the mark change.
8993       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8994       (push article gnus-newsgroup-expirable))
8995     ;; Set the mark in the buffer.
8996     (gnus-summary-update-mark mark 'unread)
8997     t))
8998
8999 (defun gnus-summary-mark-article-as-unread (mark)
9000   "Mark the current article quickly as unread with MARK."
9001   (let* ((article (gnus-summary-article-number))
9002          (old-mark (gnus-summary-article-mark article)))
9003     ;; Allow the backend to change the mark.
9004     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9005     (if (eq mark old-mark)
9006         t
9007       (if (<= article 0)
9008           (progn
9009             (gnus-error 1 "Can't mark negative article numbers")
9010             nil)
9011         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9012         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9013         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9014         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9015         (cond ((= mark gnus-ticked-mark)
9016                (push article gnus-newsgroup-marked))
9017               ((= mark gnus-dormant-mark)
9018                (push article gnus-newsgroup-dormant))
9019               (t
9020                (push article gnus-newsgroup-unreads)))
9021         (gnus-pull article gnus-newsgroup-reads)
9022
9023         ;; See whether the article is to be put in the cache.
9024         (and gnus-use-cache
9025              (vectorp (gnus-summary-article-header article))
9026              (save-excursion
9027                (gnus-cache-possibly-enter-article
9028                 gnus-newsgroup-name article
9029                 (= mark gnus-ticked-mark)
9030                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9031
9032         ;; Fix the mark.
9033         (gnus-summary-update-mark mark 'unread)
9034         t))))
9035
9036 (defun gnus-summary-mark-article (&optional article mark no-expire)
9037   "Mark ARTICLE with MARK.  MARK can be any character.
9038 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9039 `??' (dormant) and `?E' (expirable).
9040 If MARK is nil, then the default character `?r' is used.
9041 If ARTICLE is nil, then the article on the current line will be
9042 marked.
9043 Iff NO-EXPIRE, auto-expiry will be inhibited."
9044   ;; The mark might be a string.
9045   (when (stringp mark)
9046     (setq mark (aref mark 0)))
9047   ;; If no mark is given, then we check auto-expiring.
9048   (when (null mark)
9049     (setq mark gnus-del-mark))
9050   (when (and (not no-expire)
9051              gnus-newsgroup-auto-expire
9052              (memq mark gnus-auto-expirable-marks))
9053     (setq mark gnus-expirable-mark))
9054   (let ((article (or article (gnus-summary-article-number)))
9055         (old-mark (gnus-summary-article-mark article)))
9056     ;; Allow the backend to change the mark.
9057     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9058     (if (eq mark old-mark)
9059         t
9060       (unless article
9061         (error "No article on current line"))
9062       (if (not (if (or (= mark gnus-unread-mark)
9063                        (= mark gnus-ticked-mark)
9064                        (= mark gnus-dormant-mark))
9065                    (gnus-mark-article-as-unread article mark)
9066                  (gnus-mark-article-as-read article mark)))
9067           t
9068         ;; See whether the article is to be put in the cache.
9069         (and gnus-use-cache
9070              (not (= mark gnus-canceled-mark))
9071              (vectorp (gnus-summary-article-header article))
9072              (save-excursion
9073                (gnus-cache-possibly-enter-article
9074                 gnus-newsgroup-name article
9075                 (= mark gnus-ticked-mark)
9076                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9077
9078         (when (gnus-summary-goto-subject article nil t)
9079           (let ((buffer-read-only nil))
9080             (gnus-summary-show-thread)
9081             ;; Fix the mark.
9082             (gnus-summary-update-mark mark 'unread)
9083             t))))))
9084
9085 (defun gnus-summary-update-secondary-mark (article)
9086   "Update the secondary (read, process, cache) mark."
9087   (gnus-summary-update-mark
9088    (cond ((memq article gnus-newsgroup-processable)
9089           gnus-process-mark)
9090          ((memq article gnus-newsgroup-cached)
9091           gnus-cached-mark)
9092          ((memq article gnus-newsgroup-replied)
9093           gnus-replied-mark)
9094          ((memq article gnus-newsgroup-forwarded)
9095           gnus-forwarded-mark)
9096          ((memq article gnus-newsgroup-saved)
9097           gnus-saved-mark)
9098          ((memq article gnus-newsgroup-recent)
9099           gnus-recent-mark)
9100          ((memq article gnus-newsgroup-unseen)
9101           gnus-unseen-mark)
9102          (t gnus-no-mark))
9103    'replied)
9104   (when (gnus-visual-p 'summary-highlight 'highlight)
9105     (gnus-run-hooks 'gnus-summary-update-hook))
9106   t)
9107
9108 (defun gnus-summary-update-mark (mark type)
9109   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9110         (buffer-read-only nil))
9111     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9112     (when forward
9113       (when (looking-at "\r")
9114         (incf forward))
9115       (when (<= (+ forward (point)) (point-max))
9116         ;; Go to the right position on the line.
9117         (goto-char (+ forward (point)))
9118         ;; Replace the old mark with the new mark.
9119         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9120         ;; Optionally update the marks by some user rule.
9121         (when (eq type 'unread)
9122           (gnus-data-set-mark
9123            (gnus-data-find (gnus-summary-article-number)) mark)
9124           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9125
9126 (defun gnus-mark-article-as-read (article &optional mark)
9127   "Enter ARTICLE in the pertinent lists and remove it from others."
9128   ;; Make the article expirable.
9129   (let ((mark (or mark gnus-del-mark)))
9130     (if (= mark gnus-expirable-mark)
9131         (push article gnus-newsgroup-expirable)
9132       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9133     ;; Remove from unread and marked lists.
9134     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9135     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9136     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9137     (push (cons article mark) gnus-newsgroup-reads)
9138     ;; Possibly remove from cache, if that is used.
9139     (when gnus-use-cache
9140       (gnus-cache-enter-remove-article article))
9141     t))
9142
9143 (defun gnus-mark-article-as-unread (article &optional mark)
9144   "Enter ARTICLE in the pertinent lists and remove it from others."
9145   (let ((mark (or mark gnus-ticked-mark)))
9146     (if (<= article 0)
9147         (progn
9148           (gnus-error 1 "Can't mark negative article numbers")
9149           nil)
9150       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9151             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9152             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9153             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9154
9155       ;; Unsuppress duplicates?
9156       (when gnus-suppress-duplicates
9157         (gnus-dup-unsuppress-article article))
9158
9159       (cond ((= mark gnus-ticked-mark)
9160              (push article gnus-newsgroup-marked))
9161             ((= mark gnus-dormant-mark)
9162              (push article gnus-newsgroup-dormant))
9163             (t
9164              (push article gnus-newsgroup-unreads)))
9165       (gnus-pull article gnus-newsgroup-reads)
9166       t)))
9167
9168 (defalias 'gnus-summary-mark-as-unread-forward
9169   'gnus-summary-tick-article-forward)
9170 (make-obsolete 'gnus-summary-mark-as-unread-forward
9171                'gnus-summary-tick-article-forward)
9172 (defun gnus-summary-tick-article-forward (n)
9173   "Tick N articles forwards.
9174 If N is negative, tick backwards instead.
9175 The difference between N and the number of articles ticked is returned."
9176   (interactive "p")
9177   (gnus-summary-mark-forward n gnus-ticked-mark))
9178
9179 (defalias 'gnus-summary-mark-as-unread-backward
9180   'gnus-summary-tick-article-backward)
9181 (make-obsolete 'gnus-summary-mark-as-unread-backward
9182                'gnus-summary-tick-article-backward)
9183 (defun gnus-summary-tick-article-backward (n)
9184   "Tick N articles backwards.
9185 The difference between N and the number of articles ticked is returned."
9186   (interactive "p")
9187   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9188
9189 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9190 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9191 (defun gnus-summary-tick-article (&optional article clear-mark)
9192   "Mark current article as unread.
9193 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9194 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9195   (interactive)
9196   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9197                                        gnus-ticked-mark)))
9198
9199 (defun gnus-summary-mark-as-read-forward (n)
9200   "Mark N articles as read forwards.
9201 If N is negative, mark backwards instead.
9202 The difference between N and the actual number of articles marked is
9203 returned."
9204   (interactive "p")
9205   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9206
9207 (defun gnus-summary-mark-as-read-backward (n)
9208   "Mark the N articles as read backwards.
9209 The difference between N and the actual number of articles marked is
9210 returned."
9211   (interactive "p")
9212   (gnus-summary-mark-forward
9213    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9214
9215 (defun gnus-summary-mark-as-read (&optional article mark)
9216   "Mark current article as read.
9217 ARTICLE specifies the article to be marked as read.
9218 MARK specifies a string to be inserted at the beginning of the line."
9219   (gnus-summary-mark-article article mark))
9220
9221 (defun gnus-summary-clear-mark-forward (n)
9222   "Clear marks from N articles forward.
9223 If N is negative, clear backward instead.
9224 The difference between N and the number of marks cleared is returned."
9225   (interactive "p")
9226   (gnus-summary-mark-forward n gnus-unread-mark))
9227
9228 (defun gnus-summary-clear-mark-backward (n)
9229   "Clear marks from N articles backward.
9230 The difference between N and the number of marks cleared is returned."
9231   (interactive "p")
9232   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9233
9234 (defun gnus-summary-mark-unread-as-read ()
9235   "Intended to be used by `gnus-summary-mark-article-hook'."
9236   (when (memq gnus-current-article gnus-newsgroup-unreads)
9237     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9238
9239 (defun gnus-summary-mark-read-and-unread-as-read ()
9240   "Intended to be used by `gnus-summary-mark-article-hook'."
9241   (let ((mark (gnus-summary-article-mark)))
9242     (when (or (gnus-unread-mark-p mark)
9243               (gnus-read-mark-p mark))
9244       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9245
9246 (defun gnus-summary-mark-unread-as-ticked ()
9247   "Intended to be used by `gnus-summary-mark-article-hook'."
9248   (when (memq gnus-current-article gnus-newsgroup-unreads)
9249     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9250
9251 (defun gnus-summary-mark-region-as-read (point mark all)
9252   "Mark all unread articles between point and mark as read.
9253 If given a prefix, mark all articles between point and mark as read,
9254 even ticked and dormant ones."
9255   (interactive "r\nP")
9256   (save-excursion
9257     (let (article)
9258       (goto-char point)
9259       (beginning-of-line)
9260       (while (and
9261               (< (point) mark)
9262               (progn
9263                 (when (or all
9264                           (memq (setq article (gnus-summary-article-number))
9265                                 gnus-newsgroup-unreads))
9266                   (gnus-summary-mark-article article gnus-del-mark))
9267                 t)
9268               (gnus-summary-find-next))))))
9269
9270 (defun gnus-summary-mark-below (score mark)
9271   "Mark articles with score less than SCORE with MARK."
9272   (interactive "P\ncMark: ")
9273   (setq score (if score
9274                   (prefix-numeric-value score)
9275                 (or gnus-summary-default-score 0)))
9276   (save-excursion
9277     (set-buffer gnus-summary-buffer)
9278     (goto-char (point-min))
9279     (while
9280         (progn
9281           (and (< (gnus-summary-article-score) score)
9282                (gnus-summary-mark-article nil mark))
9283           (gnus-summary-find-next)))))
9284
9285 (defun gnus-summary-kill-below (&optional score)
9286   "Mark articles with score below SCORE as read."
9287   (interactive "P")
9288   (gnus-summary-mark-below score gnus-killed-mark))
9289
9290 (defun gnus-summary-clear-above (&optional score)
9291   "Clear all marks from articles with score above SCORE."
9292   (interactive "P")
9293   (gnus-summary-mark-above score gnus-unread-mark))
9294
9295 (defun gnus-summary-tick-above (&optional score)
9296   "Tick all articles with score above SCORE."
9297   (interactive "P")
9298   (gnus-summary-mark-above score gnus-ticked-mark))
9299
9300 (defun gnus-summary-mark-above (score mark)
9301   "Mark articles with score over SCORE with MARK."
9302   (interactive "P\ncMark: ")
9303   (setq score (if score
9304                   (prefix-numeric-value score)
9305                 (or gnus-summary-default-score 0)))
9306   (save-excursion
9307     (set-buffer gnus-summary-buffer)
9308     (goto-char (point-min))
9309     (while (and (progn
9310                   (when (> (gnus-summary-article-score) score)
9311                     (gnus-summary-mark-article nil mark))
9312                   t)
9313                 (gnus-summary-find-next)))))
9314
9315 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9316 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9317 (defun gnus-summary-limit-include-expunged (&optional no-error)
9318   "Display all the hidden articles that were expunged for low scores."
9319   (interactive)
9320   (let ((buffer-read-only nil))
9321     (let ((scored gnus-newsgroup-scored)
9322           headers h)
9323       (while scored
9324         (unless (gnus-summary-article-header (caar scored))
9325           (and (setq h (gnus-number-to-header (caar scored)))
9326                (< (cdar scored) gnus-summary-expunge-below)
9327                (push h headers)))
9328         (setq scored (cdr scored)))
9329       (if (not headers)
9330           (when (not no-error)
9331             (error "No expunged articles hidden"))
9332         (goto-char (point-min))
9333         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9334         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9335         (mapcar (lambda (x) (push (mail-header-number x)
9336                                   gnus-newsgroup-limit))
9337                 headers)
9338         (gnus-summary-prepare-unthreaded (nreverse headers))
9339         (goto-char (point-min))
9340         (gnus-summary-position-point)
9341         t))))
9342
9343 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9344   "Mark all unread articles in this newsgroup as read.
9345 If prefix argument ALL is non-nil, ticked and dormant articles will
9346 also be marked as read.
9347 If QUIETLY is non-nil, no questions will be asked.
9348 If TO-HERE is non-nil, it should be a point in the buffer.  All
9349 articles before (after, if REVERSE is set) this point will be marked as read.
9350 Note that this function will only catch up the unread article
9351 in the current summary buffer limitation.
9352 The number of articles marked as read is returned."
9353   (interactive "P")
9354   (prog1
9355       (save-excursion
9356         (when (or quietly
9357                   (not gnus-interactive-catchup) ;Without confirmation?
9358                   gnus-expert-user
9359                   (gnus-y-or-n-p
9360                    (if all
9361                        "Mark absolutely all articles as read? "
9362                      "Mark all unread articles as read? ")))
9363           (if (and not-mark
9364                    (not gnus-newsgroup-adaptive)
9365                    (not gnus-newsgroup-auto-expire)
9366                    (not gnus-suppress-duplicates)
9367                    (or (not gnus-use-cache)
9368                        (eq gnus-use-cache 'passive)))
9369               (progn
9370                 (when all
9371                   (setq gnus-newsgroup-marked nil
9372                         gnus-newsgroup-dormant nil))
9373                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9374             ;; We actually mark all articles as canceled, which we
9375             ;; have to do when using auto-expiry or adaptive scoring.
9376             (gnus-summary-show-all-threads)
9377             (if (and to-here reverse)
9378                 (progn
9379                   (goto-char to-here)
9380                   (while (and
9381                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9382                           (gnus-summary-find-next (not all) nil nil t))))
9383               (when (gnus-summary-first-subject (not all) t)
9384                 (while (and
9385                         (if to-here (< (point) to-here) t)
9386                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9387                         (gnus-summary-find-next (not all) nil nil t)))))
9388             (gnus-set-mode-line 'summary))
9389           t))
9390     (gnus-summary-position-point)))
9391
9392 (defun gnus-summary-catchup-to-here (&optional all)
9393   "Mark all unticked articles before the current one as read.
9394 If ALL is non-nil, also mark ticked and dormant articles as read."
9395   (interactive "P")
9396   (save-excursion
9397     (gnus-save-hidden-threads
9398       (let ((beg (point)))
9399         ;; We check that there are unread articles.
9400         (when (or all (gnus-summary-find-prev))
9401           (gnus-summary-catchup all t beg)))))
9402   (gnus-summary-position-point))
9403
9404 (defun gnus-summary-catchup-from-here (&optional all)
9405   "Mark all unticked articles after the current one as read.
9406 If ALL is non-nil, also mark ticked and dormant articles as read."
9407   (interactive "P")
9408   (save-excursion
9409     (gnus-save-hidden-threads
9410       (let ((beg (point)))
9411         ;; We check that there are unread articles.
9412         (when (or all (gnus-summary-find-next))
9413           (gnus-summary-catchup all t beg nil t)))))
9414
9415   (gnus-summary-position-point))
9416 (defun gnus-summary-catchup-all (&optional quietly)
9417   "Mark all articles in this newsgroup as read."
9418   (interactive "P")
9419   (gnus-summary-catchup t quietly))
9420
9421 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9422   "Mark all unread articles in this group as read, then exit.
9423 If prefix argument ALL is non-nil, all articles are marked as read.
9424 If QUIETLY is non-nil, no questions will be asked."
9425   (interactive "P")
9426   (when (gnus-summary-catchup all quietly nil 'fast)
9427     ;; Select next newsgroup or exit.
9428     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9429              (eq gnus-auto-select-next 'quietly))
9430         (gnus-summary-next-group nil)
9431       (gnus-summary-exit))))
9432
9433 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9434   "Mark all articles in this newsgroup as read, and then exit."
9435   (interactive "P")
9436   (gnus-summary-catchup-and-exit t quietly))
9437
9438 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9439   "Mark all articles in this group as read and select the next group.
9440 If given a prefix, mark all articles, unread as well as ticked, as
9441 read."
9442   (interactive "P")
9443   (save-excursion
9444     (gnus-summary-catchup all))
9445   (gnus-summary-next-group))
9446
9447 ;;;
9448 ;;; with article
9449 ;;;
9450
9451 (defmacro gnus-with-article (article &rest forms)
9452   "Select ARTICLE and perform FORMS in the original article buffer.
9453 Then replace the article with the result."
9454   `(progn
9455      ;; We don't want the article to be marked as read.
9456      (let (gnus-mark-article-hook)
9457        (gnus-summary-select-article t t nil ,article))
9458      (set-buffer gnus-original-article-buffer)
9459      ,@forms
9460      (if (not (gnus-check-backend-function
9461                'request-replace-article (car gnus-article-current)))
9462          (gnus-message 5 "Read-only group; not replacing")
9463        (unless (gnus-request-replace-article
9464                 ,article (car gnus-article-current)
9465                 (current-buffer) t)
9466          (error "Couldn't replace article")))
9467      ;; The cache and backlog have to be flushed somewhat.
9468      (when gnus-keep-backlog
9469        (gnus-backlog-remove-article
9470         (car gnus-article-current) (cdr gnus-article-current)))
9471      (when gnus-use-cache
9472        (gnus-cache-update-article
9473         (car gnus-article-current) (cdr gnus-article-current)))))
9474
9475 (put 'gnus-with-article 'lisp-indent-function 1)
9476 (put 'gnus-with-article 'edebug-form-spec '(form body))
9477
9478 ;; Thread-based commands.
9479
9480 (defun gnus-summary-articles-in-thread (&optional article)
9481   "Return a list of all articles in the current thread.
9482 If ARTICLE is non-nil, return all articles in the thread that starts
9483 with that article."
9484   (let* ((article (or article (gnus-summary-article-number)))
9485          (data (gnus-data-find-list article))
9486          (top-level (gnus-data-level (car data)))
9487          (top-subject
9488           (cond ((null gnus-thread-operation-ignore-subject)
9489                  (gnus-simplify-subject-re
9490                   (mail-header-subject (gnus-data-header (car data)))))
9491                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9492                  (gnus-simplify-subject-fuzzy
9493                   (mail-header-subject (gnus-data-header (car data)))))
9494                 (t nil)))
9495          (end-point (save-excursion
9496                       (if (gnus-summary-go-to-next-thread)
9497                           (point) (point-max))))
9498          articles)
9499     (while (and data
9500                 (< (gnus-data-pos (car data)) end-point))
9501       (when (or (not top-subject)
9502                 (string= top-subject
9503                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9504                              (gnus-simplify-subject-fuzzy
9505                               (mail-header-subject
9506                                (gnus-data-header (car data))))
9507                            (gnus-simplify-subject-re
9508                             (mail-header-subject
9509                              (gnus-data-header (car data)))))))
9510         (push (gnus-data-number (car data)) articles))
9511       (unless (and (setq data (cdr data))
9512                    (> (gnus-data-level (car data)) top-level))
9513         (setq data nil)))
9514     ;; Return the list of articles.
9515     (nreverse articles)))
9516
9517 (defun gnus-summary-rethread-current ()
9518   "Rethread the thread the current article is part of."
9519   (interactive)
9520   (let* ((gnus-show-threads t)
9521          (article (gnus-summary-article-number))
9522          (id (mail-header-id (gnus-summary-article-header)))
9523          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9524     (unless id
9525       (error "No article on the current line"))
9526     (gnus-rebuild-thread id)
9527     (gnus-summary-goto-subject article)))
9528
9529 (defun gnus-summary-reparent-thread ()
9530   "Make the current article child of the marked (or previous) article.
9531
9532 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9533 is non-nil or the Subject: of both articles are the same."
9534   (interactive)
9535   (unless (not (gnus-group-read-only-p))
9536     (error "The current newsgroup does not support article editing"))
9537   (unless (<= (length gnus-newsgroup-processable) 1)
9538     (error "No more than one article may be marked"))
9539   (save-window-excursion
9540     (let ((gnus-article-buffer " *reparent*")
9541           (current-article (gnus-summary-article-number))
9542           ;; First grab the marked article, otherwise one line up.
9543           (parent-article (if (not (null gnus-newsgroup-processable))
9544                               (car gnus-newsgroup-processable)
9545                             (save-excursion
9546                               (if (eq (forward-line -1) 0)
9547                                   (gnus-summary-article-number)
9548                                 (error "Beginning of summary buffer"))))))
9549       (unless (not (eq current-article parent-article))
9550         (error "An article may not be self-referential"))
9551       (let ((message-id (mail-header-id
9552                          (gnus-summary-article-header parent-article))))
9553         (unless (and message-id (not (equal message-id "")))
9554           (error "No message-id in desired parent"))
9555         (gnus-with-article current-article
9556           (save-restriction
9557             (goto-char (point-min))
9558             (message-narrow-to-head)
9559             (if (re-search-forward "^References: " nil t)
9560                 (progn
9561                   (re-search-forward "^[^ \t]" nil t)
9562                   (forward-line -1)
9563                   (end-of-line)
9564                   (insert " " message-id))
9565               (insert "References: " message-id "\n"))))
9566         (set-buffer gnus-summary-buffer)
9567         (gnus-summary-unmark-all-processable)
9568         (gnus-summary-update-article current-article)
9569         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9570             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9571         (gnus-summary-rethread-current)
9572         (gnus-message 3 "Article %d is now the child of article %d"
9573                       current-article parent-article)))))
9574
9575 (defun gnus-summary-toggle-threads (&optional arg)
9576   "Toggle showing conversation threads.
9577 If ARG is positive number, turn showing conversation threads on."
9578   (interactive "P")
9579   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9580     (setq gnus-show-threads
9581           (if (null arg) (not gnus-show-threads)
9582             (> (prefix-numeric-value arg) 0)))
9583     (gnus-summary-prepare)
9584     (gnus-summary-goto-subject current)
9585     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9586     (gnus-summary-position-point)))
9587
9588 (defun gnus-summary-show-all-threads ()
9589   "Show all threads."
9590   (interactive)
9591   (save-excursion
9592     (let ((buffer-read-only nil))
9593       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9594   (gnus-summary-position-point))
9595
9596 (defun gnus-summary-show-thread ()
9597   "Show thread subtrees.
9598 Returns nil if no thread was there to be shown."
9599   (interactive)
9600   (let ((buffer-read-only nil)
9601         (orig (point))
9602         ;; first goto end then to beg, to have point at beg after let
9603         (end (progn (end-of-line) (point)))
9604         (beg (progn (beginning-of-line) (point))))
9605     (prog1
9606         ;; Any hidden lines here?
9607         (search-forward "\r" end t)
9608       (subst-char-in-region beg end ?\^M ?\n t)
9609       (goto-char orig)
9610       (gnus-summary-position-point))))
9611
9612 (defun gnus-summary-hide-all-threads ()
9613   "Hide all thread subtrees."
9614   (interactive)
9615   (save-excursion
9616     (goto-char (point-min))
9617     (gnus-summary-hide-thread)
9618     (while (zerop (gnus-summary-next-thread 1 t))
9619       (gnus-summary-hide-thread)))
9620   (gnus-summary-position-point))
9621
9622 (defun gnus-summary-hide-thread ()
9623   "Hide thread subtrees.
9624 Returns nil if no threads were there to be hidden."
9625   (interactive)
9626   (let ((buffer-read-only nil)
9627         (start (point))
9628         (article (gnus-summary-article-number)))
9629     (goto-char start)
9630     ;; Go forward until either the buffer ends or the subthread
9631     ;; ends.
9632     (when (and (not (eobp))
9633                (or (zerop (gnus-summary-next-thread 1 t))
9634                    (goto-char (point-max))))
9635       (prog1
9636           (if (and (> (point) start)
9637                    (search-backward "\n" start t))
9638               (progn
9639                 (subst-char-in-region start (point) ?\n ?\^M)
9640                 (gnus-summary-goto-subject article))
9641             (goto-char start)
9642             nil)))))
9643
9644 (defun gnus-summary-go-to-next-thread (&optional previous)
9645   "Go to the same level (or less) next thread.
9646 If PREVIOUS is non-nil, go to previous thread instead.
9647 Return the article number moved to, or nil if moving was impossible."
9648   (let ((level (gnus-summary-thread-level))
9649         (way (if previous -1 1))
9650         (beg (point)))
9651     (forward-line way)
9652     (while (and (not (eobp))
9653                 (< level (gnus-summary-thread-level)))
9654       (forward-line way))
9655     (if (eobp)
9656         (progn
9657           (goto-char beg)
9658           nil)
9659       (setq beg (point))
9660       (prog1
9661           (gnus-summary-article-number)
9662         (goto-char beg)))))
9663
9664 (defun gnus-summary-next-thread (n &optional silent)
9665   "Go to the same level next N'th thread.
9666 If N is negative, search backward instead.
9667 Returns the difference between N and the number of skips actually
9668 done.
9669
9670 If SILENT, don't output messages."
9671   (interactive "p")
9672   (let ((backward (< n 0))
9673         (n (abs n)))
9674     (while (and (> n 0)
9675                 (gnus-summary-go-to-next-thread backward))
9676       (decf n))
9677     (unless silent
9678       (gnus-summary-position-point))
9679     (when (and (not silent) (/= 0 n))
9680       (gnus-message 7 "No more threads"))
9681     n))
9682
9683 (defun gnus-summary-prev-thread (n)
9684   "Go to the same level previous N'th thread.
9685 Returns the difference between N and the number of skips actually
9686 done."
9687   (interactive "p")
9688   (gnus-summary-next-thread (- n)))
9689
9690 (defun gnus-summary-go-down-thread ()
9691   "Go down one level in the current thread."
9692   (let ((children (gnus-summary-article-children)))
9693     (when children
9694       (gnus-summary-goto-subject (car children)))))
9695
9696 (defun gnus-summary-go-up-thread ()
9697   "Go up one level in the current thread."
9698   (let ((parent (gnus-summary-article-parent)))
9699     (when parent
9700       (gnus-summary-goto-subject parent))))
9701
9702 (defun gnus-summary-down-thread (n)
9703   "Go down thread N steps.
9704 If N is negative, go up instead.
9705 Returns the difference between N and how many steps down that were
9706 taken."
9707   (interactive "p")
9708   (let ((up (< n 0))
9709         (n (abs n)))
9710     (while (and (> n 0)
9711                 (if up (gnus-summary-go-up-thread)
9712                   (gnus-summary-go-down-thread)))
9713       (setq n (1- n)))
9714     (gnus-summary-position-point)
9715     (when (/= 0 n)
9716       (gnus-message 7 "Can't go further"))
9717     n))
9718
9719 (defun gnus-summary-up-thread (n)
9720   "Go up thread N steps.
9721 If N is negative, go down instead.
9722 Returns the difference between N and how many steps down that were
9723 taken."
9724   (interactive "p")
9725   (gnus-summary-down-thread (- n)))
9726
9727 (defun gnus-summary-top-thread ()
9728   "Go to the top of the thread."
9729   (interactive)
9730   (while (gnus-summary-go-up-thread))
9731   (gnus-summary-article-number))
9732
9733 (defun gnus-summary-kill-thread (&optional unmark)
9734   "Mark articles under current thread as read.
9735 If the prefix argument is positive, remove any kinds of marks.
9736 If the prefix argument is negative, tick articles instead."
9737   (interactive "P")
9738   (when unmark
9739     (setq unmark (prefix-numeric-value unmark)))
9740   (let ((articles (gnus-summary-articles-in-thread)))
9741     (save-excursion
9742       ;; Expand the thread.
9743       (gnus-summary-show-thread)
9744       ;; Mark all the articles.
9745       (while articles
9746         (gnus-summary-goto-subject (car articles))
9747         (cond ((null unmark)
9748                (gnus-summary-mark-article-as-read gnus-killed-mark))
9749               ((> unmark 0)
9750                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9751               (t
9752                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9753         (setq articles (cdr articles))))
9754     ;; Hide killed subtrees.
9755     (and (null unmark)
9756          gnus-thread-hide-killed
9757          (gnus-summary-hide-thread))
9758     ;; If marked as read, go to next unread subject.
9759     (when (null unmark)
9760       ;; Go to next unread subject.
9761       (gnus-summary-next-subject 1 t)))
9762   (gnus-set-mode-line 'summary))
9763
9764 ;; Summary sorting commands
9765
9766 (defun gnus-summary-sort-by-number (&optional reverse)
9767   "Sort the summary buffer by article number.
9768 Argument REVERSE means reverse order."
9769   (interactive "P")
9770   (gnus-summary-sort 'number reverse))
9771
9772 (defun gnus-summary-sort-by-author (&optional reverse)
9773   "Sort the summary buffer by author name alphabetically.
9774 If `case-fold-search' is non-nil, case of letters is ignored.
9775 Argument REVERSE means reverse order."
9776   (interactive "P")
9777   (gnus-summary-sort 'author reverse))
9778
9779 (defun gnus-summary-sort-by-subject (&optional reverse)
9780   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9781 If `case-fold-search' is non-nil, case of letters is ignored.
9782 Argument REVERSE means reverse order."
9783   (interactive "P")
9784   (gnus-summary-sort 'subject reverse))
9785
9786 (defun gnus-summary-sort-by-date (&optional reverse)
9787   "Sort the summary buffer by date.
9788 Argument REVERSE means reverse order."
9789   (interactive "P")
9790   (gnus-summary-sort 'date reverse))
9791
9792 (defun gnus-summary-sort-by-score (&optional reverse)
9793   "Sort the summary buffer by score.
9794 Argument REVERSE means reverse order."
9795   (interactive "P")
9796   (gnus-summary-sort 'score reverse))
9797
9798 (defun gnus-summary-sort-by-lines (&optional reverse)
9799   "Sort the summary buffer by the number of lines.
9800 Argument REVERSE means reverse order."
9801   (interactive "P")
9802   (gnus-summary-sort 'lines reverse))
9803
9804 (defun gnus-summary-sort-by-chars (&optional reverse)
9805   "Sort the summary buffer by article length.
9806 Argument REVERSE means reverse order."
9807   (interactive "P")
9808   (gnus-summary-sort 'chars reverse))
9809
9810 (defun gnus-summary-sort-by-original (&optional reverse)
9811   "Sort the summary buffer using the default sorting method.
9812 Argument REVERSE means reverse order."
9813   (interactive "P")
9814   (let* ((buffer-read-only)
9815          (gnus-summary-prepare-hook nil))
9816     ;; We do the sorting by regenerating the threads.
9817     (gnus-summary-prepare)
9818     ;; Hide subthreads if needed.
9819     (when (and gnus-show-threads gnus-thread-hide-subtree)
9820       (gnus-summary-hide-all-threads))))
9821
9822 (defun gnus-summary-sort (predicate reverse)
9823   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9824   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9825          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9826          (gnus-thread-sort-functions
9827           (if (not reverse)
9828               thread
9829             `(lambda (t1 t2)
9830                (,thread t2 t1))))
9831          (gnus-sort-gathered-threads-function
9832           gnus-thread-sort-functions)
9833          (gnus-article-sort-functions
9834           (if (not reverse)
9835               article
9836             `(lambda (t1 t2)
9837                (,article t2 t1))))
9838          (buffer-read-only)
9839          (gnus-summary-prepare-hook nil))
9840     ;; We do the sorting by regenerating the threads.
9841     (gnus-summary-prepare)
9842     ;; Hide subthreads if needed.
9843     (when (and gnus-show-threads gnus-thread-hide-subtree)
9844       (gnus-summary-hide-all-threads))))
9845
9846 ;; Summary saving commands.
9847
9848 (defun gnus-summary-save-article (&optional n not-saved)
9849   "Save the current article using the default saver function.
9850 If N is a positive number, save the N next articles.
9851 If N is a negative number, save the N previous articles.
9852 If N is nil and any articles have been marked with the process mark,
9853 save those articles instead.
9854 The variable `gnus-default-article-saver' specifies the saver function."
9855   (interactive "P")
9856   (let* ((articles (gnus-summary-work-articles n))
9857          (save-buffer (save-excursion
9858                         (nnheader-set-temp-buffer " *Gnus Save*")))
9859          (num (length articles))
9860          header file)
9861     (dolist (article articles)
9862       (setq header (gnus-summary-article-header article))
9863       (if (not (vectorp header))
9864           ;; This is a pseudo-article.
9865           (if (assq 'name header)
9866               (gnus-copy-file (cdr (assq 'name header)))
9867             (gnus-message 1 "Article %d is unsaveable" article))
9868         ;; This is a real article.
9869         (save-window-excursion
9870           (let ((gnus-display-mime-function nil)
9871                 (gnus-article-prepare-hook nil))
9872             (gnus-summary-select-article t nil nil article)))
9873         (save-excursion
9874           (set-buffer save-buffer)
9875           (erase-buffer)
9876           (insert-buffer-substring gnus-original-article-buffer))
9877         (setq file (gnus-article-save save-buffer file num))
9878         (gnus-summary-remove-process-mark article)
9879         (unless not-saved
9880           (gnus-summary-set-saved-mark article))))
9881     (gnus-kill-buffer save-buffer)
9882     (gnus-summary-position-point)
9883     (gnus-set-mode-line 'summary)
9884     n))
9885
9886 (defun gnus-summary-pipe-output (&optional arg)
9887   "Pipe the current article to a subprocess.
9888 If N is a positive number, pipe the N next articles.
9889 If N is a negative number, pipe the N previous articles.
9890 If N is nil and any articles have been marked with the process mark,
9891 pipe those articles instead."
9892   (interactive "P")
9893   (require 'gnus-art)
9894   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9895     (gnus-summary-save-article arg t))
9896   (let ((buffer (get-buffer "*Shell Command Output*")))
9897     (if (and buffer
9898              (with-current-buffer buffer (> (point-max) (point-min))))
9899         (gnus-configure-windows 'pipe))))
9900
9901 (defun gnus-summary-save-article-mail (&optional arg)
9902   "Append the current article to an mail file.
9903 If N is a positive number, save the N next articles.
9904 If N is a negative number, save the N previous articles.
9905 If N is nil and any articles have been marked with the process mark,
9906 save those articles instead."
9907   (interactive "P")
9908   (require 'gnus-art)
9909   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9910     (gnus-summary-save-article arg)))
9911
9912 (defun gnus-summary-save-article-rmail (&optional arg)
9913   "Append the current article to an rmail file.
9914 If N is a positive number, save the N next articles.
9915 If N is a negative number, save the N previous articles.
9916 If N is nil and any articles have been marked with the process mark,
9917 save those articles instead."
9918   (interactive "P")
9919   (require 'gnus-art)
9920   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9921     (gnus-summary-save-article arg)))
9922
9923 (defun gnus-summary-save-article-file (&optional arg)
9924   "Append the current article to a file.
9925 If N is a positive number, save the N next articles.
9926 If N is a negative number, save the N previous articles.
9927 If N is nil and any articles have been marked with the process mark,
9928 save those articles instead."
9929   (interactive "P")
9930   (require 'gnus-art)
9931   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9932     (gnus-summary-save-article arg)))
9933
9934 (defun gnus-summary-write-article-file (&optional arg)
9935   "Write the current article to a file, deleting the previous file.
9936 If N is a positive number, save the N next articles.
9937 If N is a negative number, save the N previous articles.
9938 If N is nil and any articles have been marked with the process mark,
9939 save those articles instead."
9940   (interactive "P")
9941   (require 'gnus-art)
9942   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9943     (gnus-summary-save-article arg)))
9944
9945 (defun gnus-summary-save-article-body-file (&optional arg)
9946   "Append the current article body to a file.
9947 If N is a positive number, save the N next articles.
9948 If N is a negative number, save the N previous articles.
9949 If N is nil and any articles have been marked with the process mark,
9950 save those articles instead."
9951   (interactive "P")
9952   (require 'gnus-art)
9953   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9954     (gnus-summary-save-article arg)))
9955
9956 (defun gnus-summary-muttprint (&optional arg)
9957   "Print the current article using Muttprint.
9958 If N is a positive number, save the N next articles.
9959 If N is a negative number, save the N previous articles.
9960 If N is nil and any articles have been marked with the process mark,
9961 save those articles instead."
9962   (interactive "P")
9963   (require 'gnus-art)
9964   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
9965     (gnus-summary-save-article arg t)))
9966
9967 (defun gnus-summary-pipe-message (program)
9968   "Pipe the current article through PROGRAM."
9969   (interactive "sProgram: ")
9970   (gnus-summary-select-article)
9971   (let ((mail-header-separator ""))
9972     (gnus-eval-in-buffer-window gnus-article-buffer
9973       (save-restriction
9974         (widen)
9975         (let ((start (window-start))
9976               buffer-read-only)
9977           (message-pipe-buffer-body program)
9978           (set-window-start (get-buffer-window (current-buffer)) start))))))
9979
9980 (defun gnus-get-split-value (methods)
9981   "Return a value based on the split METHODS."
9982   (let (split-name method result match)
9983     (when methods
9984       (save-excursion
9985         (set-buffer gnus-original-article-buffer)
9986         (save-restriction
9987           (nnheader-narrow-to-headers)
9988           (while (and methods (not split-name))
9989             (goto-char (point-min))
9990             (setq method (pop methods))
9991             (setq match (car method))
9992             (when (cond
9993                    ((stringp match)
9994                     ;; Regular expression.
9995                     (ignore-errors
9996                       (re-search-forward match nil t)))
9997                    ((gnus-functionp match)
9998                     ;; Function.
9999                     (save-restriction
10000                       (widen)
10001                       (setq result (funcall match gnus-newsgroup-name))))
10002                    ((consp match)
10003                     ;; Form.
10004                     (save-restriction
10005                       (widen)
10006                       (setq result (eval match)))))
10007               (setq split-name (cdr method))
10008               (cond ((stringp result)
10009                      (push (expand-file-name
10010                             result gnus-article-save-directory)
10011                            split-name))
10012                     ((consp result)
10013                      (setq split-name (append result split-name)))))))))
10014     (nreverse split-name)))
10015
10016 (defun gnus-valid-move-group-p (group)
10017   (and (boundp group)
10018        (symbol-name group)
10019        (symbol-value group)
10020        (gnus-get-function (gnus-find-method-for-group
10021                            (symbol-name group)) 'request-accept-article t)))
10022
10023 (defun gnus-read-move-group-name (prompt default articles prefix)
10024   "Read a group name."
10025   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10026          (minibuffer-confirm-incomplete nil) ; XEmacs
10027          (prom
10028           (format "%s %s to:"
10029                   prompt
10030                   (if (> (length articles) 1)
10031                       (format "these %d articles" (length articles))
10032                     "this article")))
10033          (to-newsgroup
10034           (cond
10035            ((null split-name)
10036             (gnus-completing-read default prom
10037                                   gnus-active-hashtb
10038                                   'gnus-valid-move-group-p
10039                                   nil prefix
10040                                   'gnus-group-history))
10041            ((= 1 (length split-name))
10042             (gnus-completing-read (car split-name) prom
10043                                   gnus-active-hashtb
10044                                   'gnus-valid-move-group-p
10045                                   nil nil
10046                                   'gnus-group-history))
10047            (t
10048             (gnus-completing-read nil prom
10049                                   (mapcar (lambda (el) (list el))
10050                                           (nreverse split-name))
10051                                   nil nil nil
10052                                   'gnus-group-history))))
10053          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10054     (when to-newsgroup
10055       (if (or (string= to-newsgroup "")
10056               (string= to-newsgroup prefix))
10057           (setq to-newsgroup default))
10058       (unless to-newsgroup
10059         (error "No group name entered"))
10060       (or (gnus-active to-newsgroup)
10061           (gnus-activate-group to-newsgroup nil nil to-method)
10062           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10063                                      to-newsgroup))
10064               (or (and (gnus-request-create-group to-newsgroup to-method)
10065                        (gnus-activate-group
10066                         to-newsgroup nil nil to-method)
10067                        (gnus-subscribe-group to-newsgroup))
10068                   (error "Couldn't create group %s" to-newsgroup)))
10069           (error "No such group: %s" to-newsgroup)))
10070     to-newsgroup))
10071
10072 (defun gnus-summary-save-parts (type dir n &optional reverse)
10073   "Save parts matching TYPE to DIR.
10074 If REVERSE, save parts that do not match TYPE."
10075   (interactive
10076    (list (read-string "Save parts of type: "
10077                       (or (car gnus-summary-save-parts-type-history)
10078                           gnus-summary-save-parts-default-mime)
10079                       'gnus-summary-save-parts-type-history)
10080          (setq gnus-summary-save-parts-last-directory
10081                (read-file-name "Save to directory: "
10082                                gnus-summary-save-parts-last-directory
10083                                nil t))
10084          current-prefix-arg))
10085   (gnus-summary-iterate n
10086     (let ((gnus-display-mime-function nil)
10087           (gnus-inhibit-treatment t))
10088       (gnus-summary-select-article))
10089     (save-excursion
10090       (set-buffer gnus-article-buffer)
10091       (let ((handles (or gnus-article-mime-handles
10092                          (mm-dissect-buffer) (mm-uu-dissect))))
10093         (when handles
10094           (gnus-summary-save-parts-1 type dir handles reverse)
10095           (unless gnus-article-mime-handles ;; Don't destroy this case.
10096             (mm-destroy-parts handles)))))))
10097
10098 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10099   (if (stringp (car handle))
10100       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10101               (cdr handle))
10102     (when (if reverse
10103               (not (string-match type (mm-handle-media-type handle)))
10104             (string-match type (mm-handle-media-type handle)))
10105       (let ((file (expand-file-name
10106                    (file-name-nondirectory
10107                     (or
10108                      (mail-content-type-get
10109                       (mm-handle-disposition handle) 'filename)
10110                      (concat gnus-newsgroup-name
10111                              "." (number-to-string
10112                                   (cdr gnus-article-current)))))
10113                    dir)))
10114         (unless (file-exists-p file)
10115           (mm-save-part-to-file handle file))))))
10116
10117 ;; Summary extract commands
10118
10119 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10120   (let ((buffer-read-only nil)
10121         (article (gnus-summary-article-number))
10122         after-article b e)
10123     (unless (gnus-summary-goto-subject article)
10124       (error "No such article: %d" article))
10125     (gnus-summary-position-point)
10126     ;; If all commands are to be bunched up on one line, we collect
10127     ;; them here.
10128     (unless gnus-view-pseudos-separately
10129       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10130             files action)
10131         (while ps
10132           (setq action (cdr (assq 'action (car ps))))
10133           (setq files (list (cdr (assq 'name (car ps)))))
10134           (while (and ps (cdr ps)
10135                       (string= (or action "1")
10136                                (or (cdr (assq 'action (cadr ps))) "2")))
10137             (push (cdr (assq 'name (cadr ps))) files)
10138             (setcdr ps (cddr ps)))
10139           (when files
10140             (when (not (string-match "%s" action))
10141               (push " " files))
10142             (push " " files)
10143             (when (assq 'execute (car ps))
10144               (setcdr (assq 'execute (car ps))
10145                       (funcall (if (string-match "%s" action)
10146                                    'format 'concat)
10147                                action
10148                                (mapconcat
10149                                 (lambda (f)
10150                                   (if (equal f " ")
10151                                       f
10152                                     (mm-quote-arg f)))
10153                                 files " ")))))
10154           (setq ps (cdr ps)))))
10155     (if (and gnus-view-pseudos (not not-view))
10156         (while pslist
10157           (when (assq 'execute (car pslist))
10158             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10159                                   (eq gnus-view-pseudos 'not-confirm)))
10160           (setq pslist (cdr pslist)))
10161       (save-excursion
10162         (while pslist
10163           (setq after-article (or (cdr (assq 'article (car pslist)))
10164                                   (gnus-summary-article-number)))
10165           (gnus-summary-goto-subject after-article)
10166           (forward-line 1)
10167           (setq b (point))
10168           (insert "    " (file-name-nondirectory
10169                           (cdr (assq 'name (car pslist))))
10170                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10171           (setq e (point))
10172           (forward-line -1)             ; back to `b'
10173           (gnus-add-text-properties
10174            b (1- e) (list 'gnus-number gnus-reffed-article-number
10175                           gnus-mouse-face-prop gnus-mouse-face))
10176           (gnus-data-enter
10177            after-article gnus-reffed-article-number
10178            gnus-unread-mark b (car pslist) 0 (- e b))
10179           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10180           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10181           (setq pslist (cdr pslist)))))))
10182
10183 (defun gnus-pseudos< (p1 p2)
10184   (let ((c1 (cdr (assq 'action p1)))
10185         (c2 (cdr (assq 'action p2))))
10186     (and c1 c2 (string< c1 c2))))
10187
10188 (defun gnus-request-pseudo-article (props)
10189   (cond ((assq 'execute props)
10190          (gnus-execute-command (cdr (assq 'execute props)))))
10191   (let ((gnus-current-article (gnus-summary-article-number)))
10192     (gnus-run-hooks 'gnus-mark-article-hook)))
10193
10194 (defun gnus-execute-command (command &optional automatic)
10195   (save-excursion
10196     (gnus-article-setup-buffer)
10197     (set-buffer gnus-article-buffer)
10198     (setq buffer-read-only nil)
10199     (let ((command (if automatic command
10200                      (read-string "Command: " (cons command 0)))))
10201       (erase-buffer)
10202       (insert "$ " command "\n\n")
10203       (if gnus-view-pseudo-asynchronously
10204           (start-process "gnus-execute" (current-buffer) shell-file-name
10205                          shell-command-switch command)
10206         (call-process shell-file-name nil t nil
10207                       shell-command-switch command)))))
10208
10209 ;; Summary kill commands.
10210
10211 (defun gnus-summary-edit-global-kill (article)
10212   "Edit the \"global\" kill file."
10213   (interactive (list (gnus-summary-article-number)))
10214   (gnus-group-edit-global-kill article))
10215
10216 (defun gnus-summary-edit-local-kill ()
10217   "Edit a local kill file applied to the current newsgroup."
10218   (interactive)
10219   (setq gnus-current-headers (gnus-summary-article-header))
10220   (gnus-group-edit-local-kill
10221    (gnus-summary-article-number) gnus-newsgroup-name))
10222
10223 ;;; Header reading.
10224
10225 (defun gnus-read-header (id &optional header)
10226   "Read the headers of article ID and enter them into the Gnus system."
10227   (let ((group gnus-newsgroup-name)
10228         (gnus-override-method
10229          (or
10230           gnus-override-method
10231           (and (gnus-news-group-p gnus-newsgroup-name)
10232                (car (gnus-refer-article-methods)))))
10233         where)
10234     ;; First we check to see whether the header in question is already
10235     ;; fetched.
10236     (if (stringp id)
10237         ;; This is a Message-ID.
10238         (setq header (or header (gnus-id-to-header id)))
10239       ;; This is an article number.
10240       (setq header (or header (gnus-summary-article-header id))))
10241     (if (and header
10242              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10243         ;; We have found the header.
10244         header
10245       ;; If this is a sparse article, we have to nix out its
10246       ;; previous entry in the thread hashtb.
10247       (when (and header
10248                  (gnus-summary-article-sparse-p (mail-header-number header)))
10249         (let* ((parent (gnus-parent-id (mail-header-references header)))
10250                (thread (and parent (gnus-id-to-thread parent))))
10251           (when thread
10252             (delq (assq header thread) thread))))
10253       ;; We have to really fetch the header to this article.
10254       (save-excursion
10255         (set-buffer nntp-server-buffer)
10256         (when (setq where (gnus-request-head id group))
10257           (nnheader-fold-continuation-lines)
10258           (goto-char (point-max))
10259           (insert ".\n")
10260           (goto-char (point-min))
10261           (insert "211 ")
10262           (princ (cond
10263                   ((numberp id) id)
10264                   ((cdr where) (cdr where))
10265                   (header (mail-header-number header))
10266                   (t gnus-reffed-article-number))
10267                  (current-buffer))
10268           (insert " Article retrieved.\n"))
10269         (if (or (not where)
10270                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10271             ()                          ; Malformed head.
10272           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10273             (when (and (stringp id)
10274                        (not (string= (gnus-group-real-name group)
10275                                      (car where))))
10276               ;; If we fetched by Message-ID and the article came
10277               ;; from a different group, we fudge some bogus article
10278               ;; numbers for this article.
10279               (mail-header-set-number header gnus-reffed-article-number))
10280             (save-excursion
10281               (set-buffer gnus-summary-buffer)
10282               (decf gnus-reffed-article-number)
10283               (gnus-remove-header (mail-header-number header))
10284               (push header gnus-newsgroup-headers)
10285               (setq gnus-current-headers header)
10286               (push (mail-header-number header) gnus-newsgroup-limit)))
10287           header)))))
10288
10289 (defun gnus-remove-header (number)
10290   "Remove header NUMBER from `gnus-newsgroup-headers'."
10291   (if (and gnus-newsgroup-headers
10292            (= number (mail-header-number (car gnus-newsgroup-headers))))
10293       (pop gnus-newsgroup-headers)
10294     (let ((headers gnus-newsgroup-headers))
10295       (while (and (cdr headers)
10296                   (not (= number (mail-header-number (cadr headers)))))
10297         (pop headers))
10298       (when (cdr headers)
10299         (setcdr headers (cddr headers))))))
10300
10301 ;;;
10302 ;;; summary highlights
10303 ;;;
10304
10305 (defun gnus-highlight-selected-summary ()
10306   "Highlight selected article in summary buffer."
10307   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10308   (when gnus-summary-selected-face
10309     (save-excursion
10310       (let* ((beg (progn (beginning-of-line) (point)))
10311              (end (progn (end-of-line) (point)))
10312              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10313              (from (if (get-text-property beg gnus-mouse-face-prop)
10314                        beg
10315                      (or (next-single-property-change
10316                           beg gnus-mouse-face-prop nil end)
10317                          beg)))
10318              (to
10319               (if (= from end)
10320                   (- from 2)
10321                 (or (next-single-property-change
10322                      from gnus-mouse-face-prop nil end)
10323                     end))))
10324         ;; If no mouse-face prop on line we will have to = from = end,
10325         ;; so we highlight the entire line instead.
10326         (when (= (+ to 2) from)
10327           (setq from beg)
10328           (setq to end))
10329         (if gnus-newsgroup-selected-overlay
10330             ;; Move old overlay.
10331             (gnus-move-overlay
10332              gnus-newsgroup-selected-overlay from to (current-buffer))
10333           ;; Create new overlay.
10334           (gnus-overlay-put
10335            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10336            'face gnus-summary-selected-face))))))
10337
10338 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10339 (defun gnus-summary-highlight-line ()
10340   "Highlight current line according to `gnus-summary-highlight'."
10341   (let* ((list gnus-summary-highlight)
10342          (p (point))
10343          (end (progn (end-of-line) (point)))
10344          ;; now find out where the line starts and leave point there.
10345          (beg (progn (beginning-of-line) (point)))
10346          (article (gnus-summary-article-number))
10347          (score (or (cdr (assq (or article gnus-current-article)
10348                                gnus-newsgroup-scored))
10349                     gnus-summary-default-score 0))
10350          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10351          (inhibit-read-only t))
10352     ;; Eval the cars of the lists until we find a match.
10353     (let ((default gnus-summary-default-score)
10354           (default-high gnus-summary-default-high-score)
10355           (default-low gnus-summary-default-low-score))
10356       (while (and list
10357                   (not (eval (caar list))))
10358         (setq list (cdr list))))
10359     (let ((face (cdar list)))
10360       (unless (eq face (get-text-property beg 'face))
10361         (gnus-put-text-property-excluding-characters-with-faces
10362          beg end 'face
10363          (setq face (if (boundp face) (symbol-value face) face)))
10364         (when gnus-summary-highlight-line-function
10365           (funcall gnus-summary-highlight-line-function article face))))
10366     (goto-char p)))
10367
10368 (defun gnus-update-read-articles (group unread &optional compute)
10369   "Update the list of read articles in GROUP."
10370   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10371          (entry (gnus-gethash group gnus-newsrc-hashtb))
10372          (info (nth 2 entry))
10373          (prev 1)
10374          (unread (sort (copy-sequence unread) '<))
10375          read)
10376     (if (or (not info) (not active))
10377         ;; There is no info on this group if it was, in fact,
10378         ;; killed.  Gnus stores no information on killed groups, so
10379         ;; there's nothing to be done.
10380         ;; One could store the information somewhere temporarily,
10381         ;; perhaps...  Hmmm...
10382         ()
10383       ;; Remove any negative articles numbers.
10384       (while (and unread (< (car unread) 0))
10385         (setq unread (cdr unread)))
10386       ;; Remove any expired article numbers
10387       (while (and unread (< (car unread) (car active)))
10388         (setq unread (cdr unread)))
10389       ;; Compute the ranges of read articles by looking at the list of
10390       ;; unread articles.
10391       (while unread
10392         (when (/= (car unread) prev)
10393           (push (if (= prev (1- (car unread))) prev
10394                   (cons prev (1- (car unread))))
10395                 read))
10396         (setq prev (1+ (car unread)))
10397         (setq unread (cdr unread)))
10398       (when (<= prev (cdr active))
10399         (push (cons prev (cdr active)) read))
10400       (setq read (if (> (length read) 1) (nreverse read) read))
10401       (if compute
10402           read
10403         (save-excursion
10404           (let (setmarkundo)
10405             ;; Propagate the read marks to the backend.
10406             (when (gnus-check-backend-function 'request-set-mark group)
10407               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10408                     (add (gnus-remove-from-range read (gnus-info-read info))))
10409                 (when (or add del)
10410                   (unless (gnus-check-group group)
10411                     (error "Can't open server for %s" group))
10412                   (gnus-request-set-mark
10413                    group (delq nil (list (if add (list add 'add '(read)))
10414                                          (if del (list del 'del '(read))))))
10415                   (setq setmarkundo
10416                         `(gnus-request-set-mark
10417                           ,group
10418                           ',(delq nil (list
10419                                        (if del (list del 'add '(read)))
10420                                        (if add (list add 'del '(read))))))))))
10421             (set-buffer gnus-group-buffer)
10422             (gnus-undo-register
10423               `(progn
10424                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10425                  (gnus-info-set-read ',info ',(gnus-info-read info))
10426                  (gnus-get-unread-articles-in-group ',info
10427                                                     (gnus-active ,group))
10428                  (gnus-group-update-group ,group t)
10429                  ,setmarkundo))))
10430         ;; Enter this list into the group info.
10431         (gnus-info-set-read info read)
10432         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10433         (gnus-get-unread-articles-in-group info (gnus-active group))
10434         t))))
10435
10436 (defun gnus-offer-save-summaries ()
10437   "Offer to save all active summary buffers."
10438   (let (buffers)
10439     ;; Go through all buffers and find all summaries.
10440     (dolist (buffer (buffer-list))
10441       (when (and (setq buffer (buffer-name buffer))
10442                  (string-match "Summary" buffer)
10443                  (save-excursion
10444                    (set-buffer buffer)
10445                    ;; We check that this is, indeed, a summary buffer.
10446                    (and (eq major-mode 'gnus-summary-mode)
10447                         ;; Also make sure this isn't bogus.
10448                         gnus-newsgroup-prepared
10449                         ;; Also make sure that this isn't a
10450                         ;; dead summary buffer.
10451                         (not gnus-dead-summary-mode))))
10452         (push buffer buffers)))
10453     ;; Go through all these summary buffers and offer to save them.
10454     (when buffers
10455       (save-excursion
10456         (map-y-or-n-p
10457          "Update summary buffer %s? "
10458          (lambda (buf)
10459            (switch-to-buffer buf)
10460            (gnus-summary-exit))
10461          buffers)))))
10462
10463 (defun gnus-summary-setup-default-charset ()
10464   "Setup newsgroup default charset."
10465   (if (equal gnus-newsgroup-name "nndraft:drafts")
10466       (setq gnus-newsgroup-charset nil)
10467     (let* ((ignored-charsets
10468             (or gnus-newsgroup-ephemeral-ignored-charsets
10469                 (append
10470                  (and gnus-newsgroup-name
10471                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10472                  gnus-newsgroup-ignored-charsets))))
10473       (setq gnus-newsgroup-charset
10474             (or gnus-newsgroup-ephemeral-charset
10475                 (and gnus-newsgroup-name
10476                      (gnus-parameter-charset gnus-newsgroup-name))
10477                 gnus-default-charset))
10478       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10479            ignored-charsets))))
10480
10481 ;;;
10482 ;;; Mime Commands
10483 ;;;
10484
10485 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10486   "Display the current article buffer fully MIME-buttonized.
10487 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10488 treated as multipart/mixed."
10489   (interactive "P")
10490   (require 'gnus-art)
10491   (let ((gnus-unbuttonized-mime-types nil)
10492         (gnus-mime-display-multipart-as-mixed show-all-parts))
10493     (gnus-summary-show-article)))
10494
10495 (defun gnus-summary-repair-multipart (article)
10496   "Add a Content-Type header to a multipart article without one."
10497   (interactive (list (gnus-summary-article-number)))
10498   (gnus-with-article article
10499     (message-narrow-to-head)
10500     (message-remove-header "Mime-Version")
10501     (goto-char (point-max))
10502     (insert "Mime-Version: 1.0\n")
10503     (widen)
10504     (when (search-forward "\n--" nil t)
10505       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10506         (message-narrow-to-head)
10507         (message-remove-header "Content-Type")
10508         (goto-char (point-max))
10509         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10510                         separator))
10511         (widen))))
10512   (let (gnus-mark-article-hook)
10513     (gnus-summary-select-article t t nil article)))
10514
10515 (defun gnus-summary-toggle-display-buttonized ()
10516   "Toggle the buttonizing of the article buffer."
10517   (interactive)
10518   (require 'gnus-art)
10519   (if (setq gnus-inhibit-mime-unbuttonizing
10520             (not gnus-inhibit-mime-unbuttonizing))
10521       (let ((gnus-unbuttonized-mime-types nil))
10522         (gnus-summary-show-article))
10523     (gnus-summary-show-article)))
10524
10525 ;;;
10526 ;;; Generic summary marking commands
10527 ;;;
10528
10529 (defvar gnus-summary-marking-alist
10530   '((read gnus-del-mark "d")
10531     (unread gnus-unread-mark "u")
10532     (ticked gnus-ticked-mark "!")
10533     (dormant gnus-dormant-mark "?")
10534     (expirable gnus-expirable-mark "e"))
10535   "An alist of names/marks/keystrokes.")
10536
10537 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10538 (defvar gnus-summary-mark-map)
10539
10540 (defun gnus-summary-make-all-marking-commands ()
10541   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10542   (dolist (elem gnus-summary-marking-alist)
10543     (apply 'gnus-summary-make-marking-command elem)))
10544
10545 (defun gnus-summary-make-marking-command (name mark keystroke)
10546   (let ((map (make-sparse-keymap)))
10547     (define-key gnus-summary-generic-mark-map keystroke map)
10548     (dolist (lway `((next "next" next nil "n")
10549                     (next-unread "next unread" next t "N")
10550                     (prev "previous" prev nil "p")
10551                     (prev-unread "previous unread" prev t "P")
10552                     (nomove "" nil nil ,keystroke)))
10553       (let ((func (gnus-summary-make-marking-command-1
10554                    mark (car lway) lway name)))
10555         (setq func (eval func))
10556         (define-key map (nth 4 lway) func)))))
10557
10558 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10559   `(defun ,(intern
10560             (format "gnus-summary-put-mark-as-%s%s"
10561                     name (if (eq way 'nomove)
10562                              ""
10563                            (concat "-" (symbol-name way)))))
10564      (n)
10565      ,(format
10566        "Mark the current article as %s%s.
10567 If N, the prefix, then repeat N times.
10568 If N is negative, move in reverse order.
10569 The difference between N and the actual number of articles marked is
10570 returned."
10571        name (cadr lway))
10572      (interactive "p")
10573      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10574
10575 (defun gnus-summary-generic-mark (n mark move unread)
10576   "Mark N articles with MARK."
10577   (unless (eq major-mode 'gnus-summary-mode)
10578     (error "This command can only be used in the summary buffer"))
10579   (gnus-summary-show-thread)
10580   (let ((nummove
10581          (cond
10582           ((eq move 'next) 1)
10583           ((eq move 'prev) -1)
10584           (t 0))))
10585     (if (zerop nummove)
10586         (setq n 1)
10587       (when (< n 0)
10588         (setq n (abs n)
10589               nummove (* -1 nummove))))
10590     (while (and (> n 0)
10591                 (gnus-summary-mark-article nil mark)
10592                 (zerop (gnus-summary-next-subject nummove unread t)))
10593       (setq n (1- n)))
10594     (when (/= 0 n)
10595       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10596     (gnus-summary-recenter)
10597     (gnus-summary-position-point)
10598     (gnus-set-mode-line 'summary)
10599     n))
10600
10601 (defun gnus-summary-insert-articles (articles)
10602   (when (setq articles
10603               (gnus-set-difference articles
10604                                    (mapcar (lambda (h) (mail-header-number h))
10605                                            gnus-newsgroup-headers)))
10606     (setq gnus-newsgroup-headers
10607           (merge 'list
10608                  gnus-newsgroup-headers
10609                  (gnus-fetch-headers articles)
10610                  'gnus-article-sort-by-number))
10611     ;; Suppress duplicates?
10612     (when gnus-suppress-duplicates
10613       (gnus-dup-suppress-articles))
10614
10615     ;; We might want to build some more threads first.
10616     (when (and gnus-fetch-old-headers
10617                (eq gnus-headers-retrieved-by 'nov))
10618       (if (eq gnus-fetch-old-headers 'invisible)
10619           (gnus-build-all-threads)
10620         (gnus-build-old-threads)))
10621     ;; Let the Gnus agent mark articles as read.
10622     (when gnus-agent
10623       (gnus-agent-get-undownloaded-list))
10624     ;; Remove list identifiers from subject
10625     (when gnus-list-identifiers
10626       (gnus-summary-remove-list-identifiers))
10627     ;; First and last article in this newsgroup.
10628     (when gnus-newsgroup-headers
10629       (setq gnus-newsgroup-begin
10630             (mail-header-number (car gnus-newsgroup-headers))
10631             gnus-newsgroup-end
10632             (mail-header-number
10633              (gnus-last-element gnus-newsgroup-headers))))
10634     (when gnus-use-scoring
10635       (gnus-possibly-score-headers))))
10636
10637 (defun gnus-summary-insert-old-articles (&optional all)
10638   "Insert all old articles in this group.
10639 If ALL is non-nil, already read articles become readable.
10640 If ALL is a number, fetch this number of articles."
10641   (interactive "P")
10642   (prog1
10643       (let ((old (mapcar 'car gnus-newsgroup-data))
10644             (i (car gnus-newsgroup-active))
10645             older len)
10646         (while (<= i (cdr gnus-newsgroup-active))
10647           (or (memq i old) (push i older))
10648           (incf i))
10649         (setq len (length older))
10650         (cond
10651          ((null older) nil)
10652          ((numberp all)
10653           (if (< all len)
10654               (setq older (subseq older 0 all))))
10655          (all nil)
10656          (t
10657           (if (and (numberp gnus-large-newsgroup)
10658                    (> len gnus-large-newsgroup))
10659               (let ((input
10660                      (read-string
10661                       (format
10662                        "How many articles from %s (default %d): "
10663                        (gnus-limit-string
10664                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10665                        len))))
10666                 (unless (string-match "^[ \t]*$" input)
10667                   (setq all (string-to-number input))
10668                   (if (< all len)
10669                       (setq older (subseq older 0 all))))))))
10670         (if (not older)
10671             (message "No old news.")
10672           (gnus-summary-insert-articles older)
10673           (gnus-summary-limit (gnus-union older old))))
10674     (gnus-summary-position-point)))
10675
10676 (defun gnus-summary-insert-new-articles ()
10677   "Insert all new articles in this group."
10678   (interactive)
10679   (prog1
10680       (let ((old (mapcar 'car gnus-newsgroup-data))
10681             (old-active gnus-newsgroup-active)
10682             (nnmail-fetched-sources (list t))
10683             i new)
10684         (setq gnus-newsgroup-active
10685               (gnus-activate-group gnus-newsgroup-name 'scan))
10686         (setq i (1+ (cdr old-active)))
10687         (while (<= i (cdr gnus-newsgroup-active))
10688           (push i new)
10689           (incf i))
10690         (if (not new)
10691             (message "No gnus is bad news.")
10692           (setq new (nreverse new))
10693           (gnus-summary-insert-articles new)
10694           (setq gnus-newsgroup-unreads
10695                 (append gnus-newsgroup-unreads new))
10696           (gnus-summary-limit (gnus-union old new))))
10697     (gnus-summary-position-point)))
10698
10699 (gnus-summary-make-all-marking-commands)
10700
10701 (gnus-ems-redefine)
10702
10703 (provide 'gnus-sum)
10704
10705 (run-hooks 'gnus-sum-load-hook)
10706
10707 ;;; gnus-sum.el ends here