2002-11-17 Markus Rost <rost@math.ohio-state.edu>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
113   "*A regexp to match subjects to be excluded from loose thread gathering.
114 As loose thread gathering is done on subjects only, that means that
115 there can be many false gatherings performed.  By rooting out certain
116 common subjects, gathering might become saner."
117   :group 'gnus-thread
118   :type 'regexp)
119
120 (defcustom gnus-summary-gather-subject-limit nil
121   "*Maximum length of subject comparisons when gathering loose threads.
122 Use nil to compare full subjects.  Setting this variable to a low
123 number will help gather threads that have been corrupted by
124 newsreaders chopping off subject lines, but it might also mean that
125 unrelated articles that have subject that happen to begin with the
126 same few characters will be incorrectly gathered.
127
128 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
129 comparing subjects."
130   :group 'gnus-thread
131   :type '(choice (const :tag "off" nil)
132                  (const fuzzy)
133                  (sexp :menu-tag "on" t)))
134
135 (defcustom gnus-simplify-subject-functions nil
136   "List of functions taking a string argument that simplify subjects.
137 The functions are applied recursively.
138
139 Useful functions to put in this list include:
140 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
141 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, many commands will go to the next unread article.
183 This applies to marking commands as well as other commands that
184 \"naturally\" select the next article, like, for instance, `SPC' at
185 the end of an article.
186
187 If nil, the marking commands do NOT go to the next unread article
188 \(they go to the next article instead).  If `never', commands that
189 usually go to the next unread article, will go to the next article,
190 whether it is read or not."
191   :group 'gnus-summary-marks
192   :link '(custom-manual "(gnus)Setting Marks")
193   :type '(choice (const :tag "off" nil)
194                  (const never)
195                  (sexp :menu-tag "on" t)))
196
197 (defcustom gnus-summary-default-score 0
198   "*Default article score level.
199 All scores generated by the score files will be added to this score.
200 If this variable is nil, scoring will be disabled."
201   :group 'gnus-score-default
202   :type '(choice (const :tag "disable")
203                  integer))
204
205 (defcustom gnus-summary-default-high-score 0
206   "*Default threshold for a high scored article.
207 An article will be highlighted as high scored if its score is greater
208 than this score."
209   :group 'gnus-score-default
210   :type 'integer)
211
212 (defcustom gnus-summary-default-low-score 0
213   "*Default threshold for a low scored article.
214 An article will be highlighted as low scored if its score is smaller
215 than this score."
216   :group 'gnus-score-default
217   :type 'integer)
218
219 (defcustom gnus-summary-zcore-fuzz 0
220   "*Fuzziness factor for the zcore in the summary buffer.
221 Articles with scores closer than this to `gnus-summary-default-score'
222 will not be marked."
223   :group 'gnus-summary-format
224   :type 'integer)
225
226 (defcustom gnus-simplify-subject-fuzzy-regexp nil
227   "*Strings to be removed when doing fuzzy matches.
228 This can either be a regular expression or list of regular expressions
229 that will be removed from subject strings if fuzzy subject
230 simplification is selected."
231   :group 'gnus-thread
232   :type '(repeat regexp))
233
234 (defcustom gnus-show-threads t
235   "*If non-nil, display threads in summary mode."
236   :group 'gnus-thread
237   :type 'boolean)
238
239 (defcustom gnus-thread-hide-subtree nil
240   "*If non-nil, hide all threads initially.
241 This can be a predicate specifier which says which threads to hide.
242 If threads are hidden, you have to run the command
243 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
244 to expose hidden threads."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-hide-killed t
249   "*If non-nil, hide killed threads automatically."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-ignore-subject t
254   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
255 If nil, articles that have different subjects from their parents will
256 start separate threads."
257   :group 'gnus-thread
258   :type 'boolean)
259
260 (defcustom gnus-thread-operation-ignore-subject t
261   "*If non-nil, subjects will be ignored when doing thread commands.
262 This affects commands like `gnus-summary-kill-thread' and
263 `gnus-summary-lower-thread'.
264
265 If this variable is nil, articles in the same thread with different
266 subjects will not be included in the operation in question.  If this
267 variable is `fuzzy', only articles that have subjects that are fuzzily
268 equal will be included."
269   :group 'gnus-thread
270   :type '(choice (const :tag "off" nil)
271                  (const fuzzy)
272                  (sexp :tag "on" t)))
273
274 (defcustom gnus-thread-indent-level 4
275   "*Number that says how much each sub-thread should be indented."
276   :group 'gnus-thread
277   :type 'integer)
278
279 (defcustom gnus-auto-extend-newsgroup t
280   "*If non-nil, extend newsgroup forward and backward when requested."
281   :group 'gnus-summary-choose
282   :type 'boolean)
283
284 (defcustom gnus-auto-select-first t
285   "*If non-nil, select the article under point.
286 Which article this is is controlled by the `gnus-auto-select-subject'
287 variable.
288
289 If you want to prevent automatic selection of articles in some
290 newsgroups, set the variable to nil in `gnus-select-group-hook'."
291   :group 'gnus-group-select
292   :type '(choice (const :tag "none" nil)
293                  (sexp :menu-tag "first" t)))
294
295 (defcustom gnus-auto-select-subject 'unread
296   "*Says what subject to place under point when entering a group.
297
298 This variable can either be the symbols `first' (place point on the
299 first subject), `unread' (place point on the subject line of the first
300 unread article), `best' (place point on the subject line of the
301 higest-scored article), `unseen' (place point on the subject line of
302 the first unseen article), 'unseen-or-unread' (place point on the subject
303 line of the first unseen article or, if all article have been seen, on the
304 subject line of the first unread article), or a function to be called to
305 place point on some subject line."
306   :group 'gnus-group-select
307   :type '(choice (const best)
308                  (const unread)
309                  (const first)
310                  (const unseen)
311                  (const unseen-or-unread)))
312
313 (defcustom gnus-auto-select-next t
314   "*If non-nil, offer to go to the next group from the end of the previous.
315 If the value is t and the next newsgroup is empty, Gnus will exit
316 summary mode and go back to group mode.  If the value is neither nil
317 nor t, Gnus will select the following unread newsgroup.  In
318 particular, if the value is the symbol `quietly', the next unread
319 newsgroup will be selected without any confirmation, and if it is
320 `almost-quietly', the next group will be selected without any
321 confirmation if you are located on the last article in the group.
322 Finally, if this variable is `slightly-quietly', the `Z n' command
323 will go to the next group without confirmation."
324   :group 'gnus-summary-maneuvering
325   :type '(choice (const :tag "off" nil)
326                  (const quietly)
327                  (const almost-quietly)
328                  (const slightly-quietly)
329                  (sexp :menu-tag "on" t)))
330
331 (defcustom gnus-auto-select-same nil
332   "*If non-nil, select the next article with the same subject.
333 If there are no more articles with the same subject, go to
334 the first unread article."
335   :group 'gnus-summary-maneuvering
336   :type 'boolean)
337
338 (defcustom gnus-summary-check-current nil
339   "*If non-nil, consider the current article when moving.
340 The \"unread\" movement commands will stay on the same line if the
341 current article is unread."
342   :group 'gnus-summary-maneuvering
343   :type 'boolean)
344
345 (defcustom gnus-auto-center-summary t
346   "*If non-nil, always center the current summary buffer.
347 In particular, if `vertical' do only vertical recentering.  If non-nil
348 and non-`vertical', do both horizontal and vertical recentering."
349   :group 'gnus-summary-maneuvering
350   :type '(choice (const :tag "none" nil)
351                  (const vertical)
352                  (integer :tag "height")
353                  (sexp :menu-tag "both" t)))
354
355 (defcustom gnus-show-all-headers nil
356   "*If non-nil, don't hide any headers."
357   :group 'gnus-article-hiding
358   :group 'gnus-article-headers
359   :type 'boolean)
360
361 (defcustom gnus-summary-ignore-duplicates nil
362   "*If non-nil, ignore articles with identical Message-ID headers."
363   :group 'gnus-summary
364   :type 'boolean)
365
366 (defcustom gnus-single-article-buffer t
367   "*If non-nil, display all articles in the same buffer.
368 If nil, each group will get its own article buffer."
369   :group 'gnus-article-various
370   :type 'boolean)
371
372 (defcustom gnus-break-pages t
373   "*If non-nil, do page breaking on articles.
374 The page delimiter is specified by the `gnus-page-delimiter'
375 variable."
376   :group 'gnus-article-various
377   :type 'boolean)
378
379 (defcustom gnus-move-split-methods nil
380   "*Variable used to suggest where articles are to be moved to.
381 It uses the same syntax as the `gnus-split-methods' variable.
382 However, whereas `gnus-split-methods' specifies file names as targets,
383 this variable specifies group names."
384   :group 'gnus-summary-mail
385   :type '(repeat (choice (list :value (fun) function)
386                          (cons :value ("" "") regexp (repeat string))
387                          (sexp :value nil))))
388
389 (defcustom gnus-unread-mark ?           ;Whitespace
390   "*Mark used for unread articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-ticked-mark ?!
395   "*Mark used for ticked articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-dormant-mark ??
400   "*Mark used for dormant articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-del-mark ?r
405   "*Mark used for del'd articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-read-mark ?R
410   "*Mark used for read articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-expirable-mark ?E
415   "*Mark used for expirable articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-killed-mark ?K
420   "*Mark used for killed articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-spam-mark ?H
425   "*Mark used for spam articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-souped-mark ?F
430   "*Mark used for souped articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-kill-file-mark ?X
435   "*Mark used for articles killed by kill files."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-low-score-mark ?Y
440   "*Mark used for articles with a low score."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-catchup-mark ?C
445   "*Mark used for articles that are caught up."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-replied-mark ?A
450   "*Mark used for articles that have been replied to."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-forwarded-mark ?F
455   "*Mark used for articles that have been forwarded."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-recent-mark ?N
460   "*Mark used for articles that are recent."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-cached-mark ?*
465   "*Mark used for articles that are in the cache."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-saved-mark ?S
470   "*Mark used for articles that have been saved."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-unseen-mark ?.
475   "*Mark used for articles that haven't been seen."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-no-mark ?               ;Whitespace
480   "*Mark used for articles that have no other secondary mark."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-ancient-mark ?O
485   "*Mark used for ancient articles."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-sparse-mark ?Q
490   "*Mark used for sparsely reffed articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-canceled-mark ?G
495   "*Mark used for canceled articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-duplicate-mark ?M
500   "*Mark used for duplicate articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-undownloaded-mark ?@
505   "*Mark used for articles that weren't downloaded."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-downloadable-mark ?%
510   "*Mark used for articles that are to be downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-unsendable-mark ?=
515   "*Mark used for articles that won't be sent."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-score-over-mark ?+
520   "*Score mark used for articles with high scores."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-score-below-mark ?-
525   "*Score mark used for articles with low scores."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-empty-thread-mark ?     ;Whitespace
530   "*There is no thread under the article."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-not-empty-thread-mark ?=
535   "*There is a thread under the article."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-view-pseudo-asynchronously nil
540   "*If non-nil, Gnus will view pseudo-articles asynchronously."
541   :group 'gnus-extract-view
542   :type 'boolean)
543
544 (defcustom gnus-auto-expirable-marks
545   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
546         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
547         gnus-souped-mark gnus-duplicate-mark)
548   "*The list of marks converted into expiration if a group is auto-expirable."
549   :version "21.1"
550   :group 'gnus-summary
551   :type '(repeat character))
552
553 (defcustom gnus-inhibit-user-auto-expire t
554   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
555   :version "21.1"
556   :group 'gnus-summary
557   :type 'boolean)
558
559 (defcustom gnus-view-pseudos nil
560   "*If `automatic', pseudo-articles will be viewed automatically.
561 If `not-confirm', pseudos will be viewed automatically, and the user
562 will not be asked to confirm the command."
563   :group 'gnus-extract-view
564   :type '(choice (const :tag "off" nil)
565                  (const automatic)
566                  (const not-confirm)))
567
568 (defcustom gnus-view-pseudos-separately t
569   "*If non-nil, one pseudo-article will be created for each file to be viewed.
570 If nil, all files that use the same viewing command will be given as a
571 list of parameters to that command."
572   :group 'gnus-extract-view
573   :type 'boolean)
574
575 (defcustom gnus-insert-pseudo-articles t
576   "*If non-nil, insert pseudo-articles when decoding articles."
577   :group 'gnus-extract-view
578   :type 'boolean)
579
580 (defcustom gnus-summary-dummy-line-format
581   "  %(:                          :%) %S\n"
582   "*The format specification for the dummy roots in the summary buffer.
583 It works along the same lines as a normal formatting string,
584 with some simple extensions.
585
586 %S  The subject
587
588 General format specifiers can also be used.
589 See `(gnus)Formatting Variables'."
590   :link '(custom-manual "(gnus)Formatting Variables")
591   :group 'gnus-threading
592   :type 'string)
593
594 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
595   "*The format specification for the summary mode line.
596 It works along the same lines as a normal formatting string,
597 with some simple extensions:
598
599 %G  Group name
600 %p  Unprefixed group name
601 %A  Current article number
602 %z  Current article score
603 %V  Gnus version
604 %U  Number of unread articles in the group
605 %e  Number of unselected articles in the group
606 %Z  A string with unread/unselected article counts
607 %g  Shortish group name
608 %S  Subject of the current article
609 %u  User-defined spec
610 %s  Current score file name
611 %d  Number of dormant articles
612 %r  Number of articles that have been marked as read in this session
613 %E  Number of articles expunged by the score files"
614   :group 'gnus-summary-format
615   :type 'string)
616
617 (defcustom gnus-list-identifiers nil
618   "Regexp that matches list identifiers to be removed from subject.
619 This can also be a list of regexps."
620   :version "21.1"
621   :group 'gnus-summary-format
622   :group 'gnus-article-hiding
623   :type '(choice (const :tag "none" nil)
624                  (regexp :value ".*")
625                  (repeat :value (".*") regexp)))
626
627 (defcustom gnus-summary-mark-below 0
628   "*Mark all articles with a score below this variable as read.
629 This variable is local to each summary buffer and usually set by the
630 score file."
631   :group 'gnus-score-default
632   :type 'integer)
633
634 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
635   "*List of functions used for sorting articles in the summary buffer.
636
637 Each function takes two articles and returns non-nil if the first
638 article should be sorted before the other.  If you use more than one
639 function, the primary sort function should be the last.  You should
640 probably always include `gnus-article-sort-by-number' in the list of
641 sorting functions -- preferably first.  Also note that sorting by date
642 is often much slower than sorting by number, and the sorting order is
643 very similar.  (Sorting by date means sorting by the time the message
644 was sent, sorting by number means sorting by arrival time.)
645
646 Ready-made functions include `gnus-article-sort-by-number',
647 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
648 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
649 and `gnus-article-sort-by-score'.
650
651 When threading is turned on, the variable `gnus-thread-sort-functions'
652 controls how articles are sorted."
653   :group 'gnus-summary-sort
654   :type '(repeat (choice (function-item gnus-article-sort-by-number)
655                          (function-item gnus-article-sort-by-author)
656                          (function-item gnus-article-sort-by-subject)
657                          (function-item gnus-article-sort-by-date)
658                          (function-item gnus-article-sort-by-score)
659                          (function-item gnus-article-sort-by-random)
660                          (function :tag "other"))))
661
662 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
663   "*List of functions used for sorting threads in the summary buffer.
664 By default, threads are sorted by article number.
665
666 Each function takes two threads and returns non-nil if the first
667 thread should be sorted before the other.  If you use more than one
668 function, the primary sort function should be the last.  You should
669 probably always include `gnus-thread-sort-by-number' in the list of
670 sorting functions -- preferably first.  Also note that sorting by date
671 is often much slower than sorting by number, and the sorting order is
672 very similar.  (Sorting by date means sorting by the time the message
673 was sent, sorting by number means sorting by arrival time.)
674
675 Ready-made functions include `gnus-thread-sort-by-number',
676 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
677 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
678 `gnus-thread-sort-by-most-recent-number',
679 `gnus-thread-sort-by-most-recent-date',
680 `gnus-thread-sort-by-random', and
681 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
682
683 When threading is turned off, the variable
684 `gnus-article-sort-functions' controls how articles are sorted."
685   :group 'gnus-summary-sort
686   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
687                          (function-item gnus-thread-sort-by-author)
688                          (function-item gnus-thread-sort-by-subject)
689                          (function-item gnus-thread-sort-by-date)
690                          (function-item gnus-thread-sort-by-score)
691                          (function-item gnus-thread-sort-by-total-score)
692                          (function-item gnus-thread-sort-by-random)
693                          (function :tag "other"))))
694
695 (defcustom gnus-thread-score-function '+
696   "*Function used for calculating the total score of a thread.
697
698 The function is called with the scores of the article and each
699 subthread and should then return the score of the thread.
700
701 Some functions you can use are `+', `max', or `min'."
702   :group 'gnus-summary-sort
703   :type 'function)
704
705 (defcustom gnus-summary-expunge-below nil
706   "All articles that have a score less than this variable will be expunged.
707 This variable is local to the summary buffers."
708   :group 'gnus-score-default
709   :type '(choice (const :tag "off" nil)
710                  integer))
711
712 (defcustom gnus-thread-expunge-below nil
713   "All threads that have a total score less than this variable will be expunged.
714 See `gnus-thread-score-function' for en explanation of what a
715 \"thread score\" is.
716
717 This variable is local to the summary buffers."
718   :group 'gnus-threading
719   :group 'gnus-score-default
720   :type '(choice (const :tag "off" nil)
721                  integer))
722
723 (defcustom gnus-summary-mode-hook nil
724   "*A hook for Gnus summary mode.
725 This hook is run before any variables are set in the summary buffer."
726   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
727   :group 'gnus-summary-various
728   :type 'hook)
729
730 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
731 (when (featurep 'xemacs)
732   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
733   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
734   (add-hook 'gnus-summary-mode-hook
735             'gnus-xmas-switch-horizontal-scrollbar-off))
736
737 (defcustom gnus-summary-menu-hook nil
738   "*Hook run after the creation of the summary mode menu."
739   :group 'gnus-summary-visual
740   :type 'hook)
741
742 (defcustom gnus-summary-exit-hook nil
743   "*A hook called on exit from the summary buffer.
744 It will be called with point in the group buffer."
745   :group 'gnus-summary-exit
746   :type 'hook)
747
748 (defcustom gnus-summary-prepare-hook nil
749   "*A hook called after the summary buffer has been generated.
750 If you want to modify the summary buffer, you can use this hook."
751   :group 'gnus-summary-various
752   :type 'hook)
753
754 (defcustom gnus-summary-prepared-hook nil
755   "*A hook called as the last thing after the summary buffer has been generated."
756   :group 'gnus-summary-various
757   :type 'hook)
758
759 (defcustom gnus-summary-generate-hook nil
760   "*A hook run just before generating the summary buffer.
761 This hook is commonly used to customize threading variables and the
762 like."
763   :group 'gnus-summary-various
764   :type 'hook)
765
766 (defcustom gnus-select-group-hook nil
767   "*A hook called when a newsgroup is selected.
768
769 If you'd like to simplify subjects like the
770 `gnus-summary-next-same-subject' command does, you can use the
771 following hook:
772
773  (add-hook gnus-select-group-hook
774            (lambda ()
775              (mapcar (lambda (header)
776                        (mail-header-set-subject
777                         header
778                         (gnus-simplify-subject
779                          (mail-header-subject header) 're-only)))
780                      gnus-newsgroup-headers)))"
781   :group 'gnus-group-select
782   :type 'hook)
783
784 (defcustom gnus-select-article-hook nil
785   "*A hook called when an article is selected."
786   :group 'gnus-summary-choose
787   :type 'hook)
788
789 (defcustom gnus-visual-mark-article-hook
790   (list 'gnus-highlight-selected-summary)
791   "*Hook run after selecting an article in the summary buffer.
792 It is meant to be used for highlighting the article in some way.  It
793 is not run if `gnus-visual' is nil."
794   :group 'gnus-summary-visual
795   :type 'hook)
796
797 (defcustom gnus-parse-headers-hook nil
798   "*A hook called before parsing the headers."
799   :group 'gnus-various
800   :type 'hook)
801
802 (defcustom gnus-exit-group-hook nil
803   "*A hook called when exiting summary mode.
804 This hook is not called from the non-updating exit commands like `Q'."
805   :group 'gnus-various
806   :type 'hook)
807
808 (defcustom gnus-summary-update-hook
809   (list 'gnus-summary-highlight-line)
810   "*A hook called when a summary line is changed.
811 The hook will not be called if `gnus-visual' is nil.
812
813 The default function `gnus-summary-highlight-line' will
814 highlight the line according to the `gnus-summary-highlight'
815 variable."
816   :group 'gnus-summary-visual
817   :type 'hook)
818
819 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
820   "*A hook called when an article is selected for the first time.
821 The hook is intended to mark an article as read (or unread)
822 automatically when it is selected."
823   :group 'gnus-summary-choose
824   :type 'hook)
825
826 (defcustom gnus-group-no-more-groups-hook nil
827   "*A hook run when returning to group mode having no more (unread) groups."
828   :group 'gnus-group-select
829   :type 'hook)
830
831 (defcustom gnus-ps-print-hook nil
832   "*A hook run before ps-printing something from Gnus."
833   :group 'gnus-summary
834   :type 'hook)
835
836 (defcustom gnus-summary-display-arrow
837   (and (fboundp 'display-graphic-p)
838        (display-graphic-p))
839   "*If non-nil, display an arrow highlighting the current article."
840   :version "21.1"
841   :group 'gnus-summary
842   :type 'boolean)
843
844 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
845   "Face used for highlighting the current article in the summary buffer."
846   :group 'gnus-summary-visual
847   :type 'face)
848
849 (defcustom gnus-summary-highlight
850   '(((eq mark gnus-canceled-mark)
851      . gnus-summary-cancelled-face)
852     ((and (> score default-high)
853           (or (eq mark gnus-dormant-mark)
854               (eq mark gnus-ticked-mark)))
855      . gnus-summary-high-ticked-face)
856     ((and (< score default-low)
857           (or (eq mark gnus-dormant-mark)
858               (eq mark gnus-ticked-mark)))
859      . gnus-summary-low-ticked-face)
860     ((or (eq mark gnus-dormant-mark)
861          (eq mark gnus-ticked-mark))
862      . gnus-summary-normal-ticked-face)
863     ((and (> score default-high) (eq mark gnus-ancient-mark))
864      . gnus-summary-high-ancient-face)
865     ((and (< score default-low) (eq mark gnus-ancient-mark))
866      . gnus-summary-low-ancient-face)
867     ((eq mark gnus-ancient-mark)
868      . gnus-summary-normal-ancient-face)
869     ((and (> score default-high) (eq mark gnus-unread-mark))
870      . gnus-summary-high-unread-face)
871     ((and (< score default-low) (eq mark gnus-unread-mark))
872      . gnus-summary-low-unread-face)
873     ((eq mark gnus-unread-mark)
874      . gnus-summary-normal-unread-face)
875     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
876                                                   gnus-undownloaded-mark)))
877      . gnus-summary-high-unread-face)
878     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
879                                                  gnus-undownloaded-mark)))
880      . gnus-summary-low-unread-face)
881     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
882           (memq article gnus-newsgroup-unreads))
883      . gnus-summary-normal-unread-face)
884     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
885      . gnus-summary-normal-read-face)
886     ((> score default-high)
887      . gnus-summary-high-read-face)
888     ((< score default-low)
889      . gnus-summary-low-read-face)
890     (t
891      . gnus-summary-normal-read-face))
892   "*Controls the highlighting of summary buffer lines.
893
894 A list of (FORM . FACE) pairs.  When deciding how a a particular
895 summary line should be displayed, each form is evaluated.  The content
896 of the face field after the first true form is used.  You can change
897 how those summary lines are displayed, by editing the face field.
898
899 You can use the following variables in the FORM field.
900
901 score:        The article's score
902 default:      The default article score.
903 default-high: The default score for high scored articles.
904 default-low:  The default score for low scored articles.
905 below:        The score below which articles are automatically marked as read.
906 mark:         The articles mark."
907   :group 'gnus-summary-visual
908   :type '(repeat (cons (sexp :tag "Form" nil)
909                        face)))
910
911 (defcustom gnus-alter-header-function nil
912   "Function called to allow alteration of article header structures.
913 The function is called with one parameter, the article header vector,
914 which it may alter in any way.")
915
916 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
917   "Variable that says which function should be used to decode a string with encoded words.")
918
919 (defcustom gnus-extra-headers '(To Newsgroups)
920   "*Extra headers to parse."
921   :version "21.1"
922   :group 'gnus-summary
923   :type '(repeat symbol))
924
925 (defcustom gnus-ignored-from-addresses
926   (and user-mail-address (regexp-quote user-mail-address))
927   "*Regexp of From headers that may be suppressed in favor of To headers."
928   :version "21.1"
929   :group 'gnus-summary
930   :type 'regexp)
931
932 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
933   "List of charsets that should be ignored.
934 When these charsets are used in the \"charset\" parameter, the
935 default charset will be used instead."
936   :version "21.1"
937   :type '(repeat symbol)
938   :group 'gnus-charset)
939
940 (gnus-define-group-parameter
941  ignored-charsets
942  :type list
943  :function-document
944  "Return the ignored charsets of GROUP."
945  :variable gnus-group-ignored-charsets-alist
946  :variable-default
947  '(("alt\\.chinese\\.text" iso-8859-1))
948  :variable-document
949  "Alist of regexps (to match group names) and charsets that should be ignored.
950 When these charsets are used in the \"charset\" parameter, the
951 default charset will be used instead."
952  :variable-group gnus-charset
953  :variable-type '(repeat (cons (regexp :tag "Group")
954                                (repeat symbol)))
955  :parameter-type '(choice :tag "Ignored charsets"
956                           :value nil
957                           (repeat (symbol)))
958  :parameter-document       "\
959 List of charsets that should be ignored.
960
961 When these charsets are used in the \"charset\" parameter, the
962 default charset will be used instead.")
963
964 (defcustom gnus-group-highlight-words-alist nil
965   "Alist of group regexps and highlight regexps.
966 This variable uses the same syntax as `gnus-emphasis-alist'."
967   :version "21.1"
968   :type '(repeat (cons (regexp :tag "Group")
969                        (repeat (list (regexp :tag "Highlight regexp")
970                                      (number :tag "Group for entire word" 0)
971                                      (number :tag "Group for displayed part" 0)
972                                      (symbol :tag "Face"
973                                              gnus-emphasis-highlight-words)))))
974   :group 'gnus-summary-visual)
975
976 (defcustom gnus-summary-show-article-charset-alist
977   nil
978   "Alist of number and charset.
979 The article will be shown with the charset corresponding to the
980 numbered argument.
981 For example: ((1 . cn-gb-2312) (2 . big5))."
982   :version "21.1"
983   :type '(repeat (cons (number :tag "Argument" 1)
984                        (symbol :tag "Charset")))
985   :group 'gnus-charset)
986
987 (defcustom gnus-preserve-marks t
988   "Whether marks are preserved when moving, copying and respooling messages."
989   :version "21.1"
990   :type 'boolean
991   :group 'gnus-summary-marks)
992
993 (defcustom gnus-alter-articles-to-read-function nil
994   "Function to be called to alter the list of articles to be selected."
995   :type '(choice (const nil) function)
996   :group 'gnus-summary)
997
998 (defcustom gnus-orphan-score nil
999   "*All orphans get this score added.  Set in the score file."
1000   :group 'gnus-score-default
1001   :type '(choice (const nil)
1002                  integer))
1003
1004 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1005   "*A regexp to match MIME parts when saving multiple parts of a message
1006 with gnus-summary-save-parts (X m). This regexp will be used by default
1007 when prompting the user for which type of files to save."
1008   :group 'gnus-summary
1009   :type 'regexp)
1010
1011 (defcustom gnus-read-all-available-headers nil
1012   "Whether Gnus should parse all headers made available to it.
1013 This is mostly relevant for slow backends where the user may
1014 wish to widen the summary buffer to include all headers
1015 that were fetched.  Say, for nnultimate groups."
1016   :group 'gnus-summary
1017   :type '(choice boolean regexp))
1018
1019 (defcustom gnus-summary-muttprint-program "muttprint"
1020   "Command (and optional arguments) used to run Muttprint."
1021   :version "21.3"
1022   :group 'gnus-summary
1023   :type 'string)
1024
1025 (defcustom gnus-article-loose-mime nil
1026   "If non-nil, don't require MIME-Version header.
1027 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1028 supply the MIME-Version header or deliberately strip it From the mail.
1029 Set it to non-nil, Gnus will treat some articles as MIME even if
1030 the MIME-Version header is missed."
1031   :version "21.3"
1032   :type 'boolean
1033   :group 'gnus-article)
1034
1035 ;;; Internal variables
1036
1037 (defvar gnus-summary-display-cache nil)
1038 (defvar gnus-article-mime-handles nil)
1039 (defvar gnus-article-decoded-p nil)
1040 (defvar gnus-article-charset nil)
1041 (defvar gnus-article-ignored-charsets nil)
1042 (defvar gnus-scores-exclude-files nil)
1043 (defvar gnus-page-broken nil)
1044 (defvar gnus-inhibit-mime-unbuttonizing nil)
1045
1046 (defvar gnus-original-article nil)
1047 (defvar gnus-article-internal-prepare-hook nil)
1048 (defvar gnus-newsgroup-process-stack nil)
1049
1050 (defvar gnus-thread-indent-array nil)
1051 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1052 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1053   "Function called to sort the articles within a thread after it has been gathered together.")
1054
1055 (defvar gnus-summary-save-parts-type-history nil)
1056 (defvar gnus-summary-save-parts-last-directory nil)
1057
1058 ;; Avoid highlighting in kill files.
1059 (defvar gnus-summary-inhibit-highlight nil)
1060 (defvar gnus-newsgroup-selected-overlay nil)
1061 (defvar gnus-inhibit-limiting nil)
1062 (defvar gnus-newsgroup-adaptive-score-file nil)
1063 (defvar gnus-current-score-file nil)
1064 (defvar gnus-current-move-group nil)
1065 (defvar gnus-current-copy-group nil)
1066 (defvar gnus-current-crosspost-group nil)
1067 (defvar gnus-newsgroup-display nil)
1068
1069 (defvar gnus-newsgroup-dependencies nil)
1070 (defvar gnus-newsgroup-adaptive nil)
1071 (defvar gnus-summary-display-article-function nil)
1072 (defvar gnus-summary-highlight-line-function nil
1073   "Function called after highlighting a summary line.")
1074
1075 (defvar gnus-summary-line-format-alist
1076   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1077     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1078     (?s gnus-tmp-subject-or-nil ?s)
1079     (?n gnus-tmp-name ?s)
1080     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1081         ?s)
1082     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1083             gnus-tmp-from) ?s)
1084     (?F gnus-tmp-from ?s)
1085     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1086     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1087     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1088     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1089     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1090     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1091     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1092     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1093     (?L gnus-tmp-lines ?s)
1094     (?I gnus-tmp-indentation ?s)
1095     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1096     (?R gnus-tmp-replied ?c)
1097     (?\[ gnus-tmp-opening-bracket ?c)
1098     (?\] gnus-tmp-closing-bracket ?c)
1099     (?\> (make-string gnus-tmp-level ? ) ?s)
1100     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1101     (?i gnus-tmp-score ?d)
1102     (?z gnus-tmp-score-char ?c)
1103     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1104     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1105     (?U gnus-tmp-unread ?c)
1106     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1107         ?s)
1108     (?t (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1110         ?d)
1111     (?e (gnus-summary-number-of-articles-in-thread
1112          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1113         ?c)
1114     (?u gnus-tmp-user-defined ?s)
1115     (?P (gnus-pick-line-number) ?d)
1116     (?B gnus-tmp-thread-tree-header-string ?s)
1117     (user-date (gnus-user-date
1118                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1119   "An alist of format specifications that can appear in summary lines.
1120 These are paired with what variables they correspond with, along with
1121 the type of the variable (string, integer, character, etc).")
1122
1123 (defvar gnus-summary-dummy-line-format-alist
1124   `((?S gnus-tmp-subject ?s)
1125     (?N gnus-tmp-number ?d)
1126     (?u gnus-tmp-user-defined ?s)))
1127
1128 (defvar gnus-summary-mode-line-format-alist
1129   `((?G gnus-tmp-group-name ?s)
1130     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1131     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1132     (?A gnus-tmp-article-number ?d)
1133     (?Z gnus-tmp-unread-and-unselected ?s)
1134     (?V gnus-version ?s)
1135     (?U gnus-tmp-unread-and-unticked ?d)
1136     (?S gnus-tmp-subject ?s)
1137     (?e gnus-tmp-unselected ?d)
1138     (?u gnus-tmp-user-defined ?s)
1139     (?d (length gnus-newsgroup-dormant) ?d)
1140     (?t (length gnus-newsgroup-marked) ?d)
1141     (?h (length gnus-newsgroup-spam-marked) ?d)
1142     (?r (length gnus-newsgroup-reads) ?d)
1143     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1144     (?E gnus-newsgroup-expunged-tally ?d)
1145     (?s (gnus-current-score-file-nondirectory) ?s)))
1146
1147 (defvar gnus-last-search-regexp nil
1148   "Default regexp for article search command.")
1149
1150 (defvar gnus-last-shell-command nil
1151   "Default shell command on article.")
1152
1153 (defvar gnus-newsgroup-begin nil)
1154 (defvar gnus-newsgroup-end nil)
1155 (defvar gnus-newsgroup-last-rmail nil)
1156 (defvar gnus-newsgroup-last-mail nil)
1157 (defvar gnus-newsgroup-last-folder nil)
1158 (defvar gnus-newsgroup-last-file nil)
1159 (defvar gnus-newsgroup-auto-expire nil)
1160 (defvar gnus-newsgroup-active nil)
1161
1162 (defvar gnus-newsgroup-data nil)
1163 (defvar gnus-newsgroup-data-reverse nil)
1164 (defvar gnus-newsgroup-limit nil)
1165 (defvar gnus-newsgroup-limits nil)
1166
1167 (defvar gnus-newsgroup-unreads nil
1168   "Sorted list of unread articles in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-unselected nil
1171   "Sorted list of unselected unread articles in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-reads nil
1174   "Alist of read articles and article marks in the current newsgroup.")
1175
1176 (defvar gnus-newsgroup-expunged-tally nil)
1177
1178 (defvar gnus-newsgroup-marked nil
1179   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1180
1181 (defvar gnus-newsgroup-spam-marked nil
1182   "List of ranges of articles that have been marked as spam.")
1183
1184 (defvar gnus-newsgroup-killed nil
1185   "List of ranges of articles that have been through the scoring process.")
1186
1187 (defvar gnus-newsgroup-cached nil
1188   "Sorted list of articles that come from the article cache.")
1189
1190 (defvar gnus-newsgroup-saved nil
1191   "List of articles that have been saved.")
1192
1193 (defvar gnus-newsgroup-kill-headers nil)
1194
1195 (defvar gnus-newsgroup-replied nil
1196   "List of articles that have been replied to in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-forwarded nil
1199   "List of articles that have been forwarded in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-recent nil
1202   "List of articles that have are recent in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-expirable nil
1205   "Sorted list of articles in the current newsgroup that can be expired.")
1206
1207 (defvar gnus-newsgroup-processable nil
1208   "List of articles in the current newsgroup that can be processed.")
1209
1210 (defvar gnus-newsgroup-downloadable nil
1211   "Sorted list of articles in the current newsgroup that can be processed.")
1212
1213 (defvar gnus-newsgroup-undownloaded nil
1214   "List of articles in the current newsgroup that haven't been downloaded..")
1215
1216 (defvar gnus-newsgroup-unsendable nil
1217   "List of articles in the current newsgroup that won't be sent.")
1218
1219 (defvar gnus-newsgroup-bookmarks nil
1220   "List of articles in the current newsgroup that have bookmarks.")
1221
1222 (defvar gnus-newsgroup-dormant nil
1223   "Sorted list of dormant articles in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-unseen nil
1226   "List of unseen articles in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-seen nil
1229   "Range of seen articles in the current newsgroup.")
1230
1231 (defvar gnus-newsgroup-articles nil
1232   "List of articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-scored nil
1235   "List of scored articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-headers nil
1238   "List of article headers in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-threads nil)
1241
1242 (defvar gnus-newsgroup-prepared nil
1243   "Whether the current group has been prepared properly.")
1244
1245 (defvar gnus-newsgroup-ancient nil
1246   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1247
1248 (defvar gnus-newsgroup-sparse nil)
1249
1250 (defvar gnus-current-article nil)
1251 (defvar gnus-article-current nil)
1252 (defvar gnus-current-headers nil)
1253 (defvar gnus-have-all-headers nil)
1254 (defvar gnus-last-article nil)
1255 (defvar gnus-newsgroup-history nil)
1256 (defvar gnus-newsgroup-charset nil)
1257 (defvar gnus-newsgroup-ephemeral-charset nil)
1258 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1259
1260 (defvar gnus-article-before-search nil)
1261
1262 (defconst gnus-summary-local-variables
1263   '(gnus-newsgroup-name
1264     gnus-newsgroup-begin gnus-newsgroup-end
1265     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1266     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1267     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1268     gnus-newsgroup-unselected gnus-newsgroup-marked
1269     gnus-newsgroup-spam-marked
1270     gnus-newsgroup-reads gnus-newsgroup-saved
1271     gnus-newsgroup-replied gnus-newsgroup-forwarded
1272     gnus-newsgroup-recent
1273     gnus-newsgroup-expirable
1274     gnus-newsgroup-processable gnus-newsgroup-killed
1275     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1276     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1277     gnus-newsgroup-seen gnus-newsgroup-articles
1278     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1279     gnus-newsgroup-headers gnus-newsgroup-threads
1280     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1281     gnus-current-article gnus-current-headers gnus-have-all-headers
1282     gnus-last-article gnus-article-internal-prepare-hook
1283     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1284     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1285     gnus-thread-expunge-below
1286     gnus-score-alist gnus-current-score-file
1287     (gnus-summary-expunge-below . global)
1288     (gnus-summary-mark-below . global)
1289     (gnus-orphan-score . global)
1290     gnus-newsgroup-active gnus-scores-exclude-files
1291     gnus-newsgroup-history gnus-newsgroup-ancient
1292     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1293     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1294     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1295     (gnus-newsgroup-expunged-tally . 0)
1296     gnus-cache-removable-articles gnus-newsgroup-cached
1297     gnus-newsgroup-data gnus-newsgroup-data-reverse
1298     gnus-newsgroup-limit gnus-newsgroup-limits
1299     gnus-newsgroup-charset gnus-newsgroup-display)
1300   "Variables that are buffer-local to the summary buffers.")
1301
1302 (defvar gnus-newsgroup-variables nil
1303   "A list of variables that have separate values in different newsgroups.
1304 A list of newsgroup (summary buffer) local variables, or cons of
1305 variables and their default values (when the default values are not
1306 nil), that should be made global while the summary buffer is active.
1307 These variables can be used to set variables in the group parameters
1308 while still allowing them to affect operations done in other
1309 buffers. For example:
1310
1311 \(setq gnus-newsgroup-variables
1312      '(message-use-followup-to
1313        (gnus-visible-headers .
1314          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1315 ")
1316
1317 ;; Byte-compiler warning.
1318 (eval-when-compile (defvar gnus-article-mode-map))
1319
1320 ;; MIME stuff.
1321
1322 (defvar gnus-decode-encoded-word-methods
1323   '(mail-decode-encoded-word-string)
1324   "List of methods used to decode encoded words.
1325
1326 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1327 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1328 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1329 whose names match REGEXP.
1330
1331 For example:
1332 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1333  mail-decode-encoded-word-string
1334  (\"chinese\" . rfc1843-decode-string))")
1335
1336 (defvar gnus-decode-encoded-word-methods-cache nil)
1337
1338 (defun gnus-multi-decode-encoded-word-string (string)
1339   "Apply the functions from `gnus-encoded-word-methods' that match."
1340   (unless (and gnus-decode-encoded-word-methods-cache
1341                (eq gnus-newsgroup-name
1342                    (car gnus-decode-encoded-word-methods-cache)))
1343     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1344     (mapcar (lambda (x)
1345               (if (symbolp x)
1346                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1347                 (if (and gnus-newsgroup-name
1348                          (string-match (car x) gnus-newsgroup-name))
1349                     (nconc gnus-decode-encoded-word-methods-cache
1350                            (list (cdr x))))))
1351             gnus-decode-encoded-word-methods))
1352   (let ((xlist gnus-decode-encoded-word-methods-cache))
1353     (pop xlist)
1354     (while xlist
1355       (setq string (funcall (pop xlist) string))))
1356   string)
1357
1358 ;; Subject simplification.
1359
1360 (defun gnus-simplify-whitespace (str)
1361   "Remove excessive whitespace from STR."
1362   (let ((mystr str))
1363     ;; Multiple spaces.
1364     (while (string-match "[ \t][ \t]+" mystr)
1365       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1366                           " "
1367                           (substring mystr (match-end 0)))))
1368     ;; Leading spaces.
1369     (when (string-match "^[ \t]+" mystr)
1370       (setq mystr (substring mystr (match-end 0))))
1371     ;; Trailing spaces.
1372     (when (string-match "[ \t]+$" mystr)
1373       (setq mystr (substring mystr 0 (match-beginning 0))))
1374     mystr))
1375
1376 (defun gnus-simplify-all-whitespace (str)
1377   "Remove all whitespace from STR."
1378   (let ((mystr str))
1379     (while (string-match "[ \t\n]+" mystr)
1380       (setq mystr (replace-match "" nil nil mystr)))
1381     mystr))
1382
1383 (defsubst gnus-simplify-subject-re (subject)
1384   "Remove \"Re:\" from subject lines."
1385   (if (string-match message-subject-re-regexp subject)
1386       (substring subject (match-end 0))
1387     subject))
1388
1389 (defun gnus-simplify-subject (subject &optional re-only)
1390   "Remove `Re:' and words in parentheses.
1391 If RE-ONLY is non-nil, strip leading `Re:'s only."
1392   (let ((case-fold-search t))           ;Ignore case.
1393     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1394     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1395       (setq subject (substring subject (match-end 0))))
1396     ;; Remove uninteresting prefixes.
1397     (when (and (not re-only)
1398                gnus-simplify-ignored-prefixes
1399                (string-match gnus-simplify-ignored-prefixes subject))
1400       (setq subject (substring subject (match-end 0))))
1401     ;; Remove words in parentheses from end.
1402     (unless re-only
1403       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1404         (setq subject (substring subject 0 (match-beginning 0)))))
1405     ;; Return subject string.
1406     subject))
1407
1408 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1409 ;; all whitespace.
1410 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1411   (goto-char (point-min))
1412   (while (re-search-forward regexp nil t)
1413     (replace-match (or newtext ""))))
1414
1415 (defun gnus-simplify-buffer-fuzzy ()
1416   "Simplify string in the buffer fuzzily.
1417 The string in the accessible portion of the current buffer is simplified.
1418 It is assumed to be a single-line subject.
1419 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1420 matter is removed.  Additional things can be deleted by setting
1421 `gnus-simplify-subject-fuzzy-regexp'."
1422   (let ((case-fold-search t)
1423         (modified-tick))
1424     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1425
1426     (while (not (eq modified-tick (buffer-modified-tick)))
1427       (setq modified-tick (buffer-modified-tick))
1428       (cond
1429        ((listp gnus-simplify-subject-fuzzy-regexp)
1430         (mapcar 'gnus-simplify-buffer-fuzzy-step
1431                 gnus-simplify-subject-fuzzy-regexp))
1432        (gnus-simplify-subject-fuzzy-regexp
1433         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1434       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1435       (gnus-simplify-buffer-fuzzy-step
1436        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1437       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1438
1439     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1440     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1441     (gnus-simplify-buffer-fuzzy-step " $")
1442     (gnus-simplify-buffer-fuzzy-step "^ +")))
1443
1444 (defun gnus-simplify-subject-fuzzy (subject)
1445   "Simplify a subject string fuzzily.
1446 See `gnus-simplify-buffer-fuzzy' for details."
1447   (save-excursion
1448     (gnus-set-work-buffer)
1449     (let ((case-fold-search t))
1450       ;; Remove uninteresting prefixes.
1451       (when (and gnus-simplify-ignored-prefixes
1452                  (string-match gnus-simplify-ignored-prefixes subject))
1453         (setq subject (substring subject (match-end 0))))
1454       (insert subject)
1455       (inline (gnus-simplify-buffer-fuzzy))
1456       (buffer-string))))
1457
1458 (defsubst gnus-simplify-subject-fully (subject)
1459   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1460   (cond
1461    (gnus-simplify-subject-functions
1462     (gnus-map-function gnus-simplify-subject-functions subject))
1463    ((null gnus-summary-gather-subject-limit)
1464     (gnus-simplify-subject-re subject))
1465    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1466     (gnus-simplify-subject-fuzzy subject))
1467    ((numberp gnus-summary-gather-subject-limit)
1468     (gnus-limit-string (gnus-simplify-subject-re subject)
1469                        gnus-summary-gather-subject-limit))
1470    (t
1471     subject)))
1472
1473 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1474   "Check whether two subjects are equal.
1475 If optional argument simple-first is t, first argument is already
1476 simplified."
1477   (cond
1478    ((null simple-first)
1479     (equal (gnus-simplify-subject-fully s1)
1480            (gnus-simplify-subject-fully s2)))
1481    (t
1482     (equal s1
1483            (gnus-simplify-subject-fully s2)))))
1484
1485 (defun gnus-summary-bubble-group ()
1486   "Increase the score of the current group.
1487 This is a handy function to add to `gnus-summary-exit-hook' to
1488 increase the score of each group you read."
1489   (gnus-group-add-score gnus-newsgroup-name))
1490
1491 \f
1492 ;;;
1493 ;;; Gnus summary mode
1494 ;;;
1495
1496 (put 'gnus-summary-mode 'mode-class 'special)
1497
1498 (defvar gnus-article-commands-menu)
1499
1500 (when t
1501   ;; Non-orthogonal keys
1502
1503   (gnus-define-keys gnus-summary-mode-map
1504     " " gnus-summary-next-page
1505     "\177" gnus-summary-prev-page
1506     [delete] gnus-summary-prev-page
1507     [backspace] gnus-summary-prev-page
1508     "\r" gnus-summary-scroll-up
1509     "\M-\r" gnus-summary-scroll-down
1510     "n" gnus-summary-next-unread-article
1511     "p" gnus-summary-prev-unread-article
1512     "N" gnus-summary-next-article
1513     "P" gnus-summary-prev-article
1514     "\M-\C-n" gnus-summary-next-same-subject
1515     "\M-\C-p" gnus-summary-prev-same-subject
1516     "\M-n" gnus-summary-next-unread-subject
1517     "\M-p" gnus-summary-prev-unread-subject
1518     "." gnus-summary-first-unread-article
1519     "," gnus-summary-best-unread-article
1520     "\M-s" gnus-summary-search-article-forward
1521     "\M-r" gnus-summary-search-article-backward
1522     "<" gnus-summary-beginning-of-article
1523     ">" gnus-summary-end-of-article
1524     "j" gnus-summary-goto-article
1525     "^" gnus-summary-refer-parent-article
1526     "\M-^" gnus-summary-refer-article
1527     "u" gnus-summary-tick-article-forward
1528     "!" gnus-summary-tick-article-forward
1529     "U" gnus-summary-tick-article-backward
1530     "d" gnus-summary-mark-as-read-forward
1531     "D" gnus-summary-mark-as-read-backward
1532     "E" gnus-summary-mark-as-expirable
1533     "\M-u" gnus-summary-clear-mark-forward
1534     "\M-U" gnus-summary-clear-mark-backward
1535     "k" gnus-summary-kill-same-subject-and-select
1536     "\C-k" gnus-summary-kill-same-subject
1537     "\M-\C-k" gnus-summary-kill-thread
1538     "\M-\C-l" gnus-summary-lower-thread
1539     "e" gnus-summary-edit-article
1540     "#" gnus-summary-mark-as-processable
1541     "\M-#" gnus-summary-unmark-as-processable
1542     "\M-\C-t" gnus-summary-toggle-threads
1543     "\M-\C-s" gnus-summary-show-thread
1544     "\M-\C-h" gnus-summary-hide-thread
1545     "\M-\C-f" gnus-summary-next-thread
1546     "\M-\C-b" gnus-summary-prev-thread
1547     [(meta down)] gnus-summary-next-thread
1548     [(meta up)] gnus-summary-prev-thread
1549     "\M-\C-u" gnus-summary-up-thread
1550     "\M-\C-d" gnus-summary-down-thread
1551     "&" gnus-summary-execute-command
1552     "c" gnus-summary-catchup-and-exit
1553     "\C-w" gnus-summary-mark-region-as-read
1554     "\C-t" gnus-summary-toggle-truncation
1555     "?" gnus-summary-mark-as-dormant
1556     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1557     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1558     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1559     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1560     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1561     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1562     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1563     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1564     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1565     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1566     "=" gnus-summary-expand-window
1567     "\C-x\C-s" gnus-summary-reselect-current-group
1568     "\M-g" gnus-summary-rescan-group
1569     "w" gnus-summary-stop-page-breaking
1570     "\C-c\C-r" gnus-summary-caesar-message
1571     "f" gnus-summary-followup
1572     "F" gnus-summary-followup-with-original
1573     "C" gnus-summary-cancel-article
1574     "r" gnus-summary-reply
1575     "R" gnus-summary-reply-with-original
1576     "\C-c\C-f" gnus-summary-mail-forward
1577     "o" gnus-summary-save-article
1578     "\C-o" gnus-summary-save-article-mail
1579     "|" gnus-summary-pipe-output
1580     "\M-k" gnus-summary-edit-local-kill
1581     "\M-K" gnus-summary-edit-global-kill
1582     ;; "V" gnus-version
1583     "\C-c\C-d" gnus-summary-describe-group
1584     "q" gnus-summary-exit
1585     "Q" gnus-summary-exit-no-update
1586     "\C-c\C-i" gnus-info-find-node
1587     gnus-mouse-2 gnus-mouse-pick-article
1588     "m" gnus-summary-mail-other-window
1589     "a" gnus-summary-post-news
1590     "i" gnus-summary-news-other-window
1591     "x" gnus-summary-limit-to-unread
1592     "s" gnus-summary-isearch-article
1593     "t" gnus-summary-toggle-header
1594     "g" gnus-summary-show-article
1595     "l" gnus-summary-goto-last-article
1596     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1597     "\C-d" gnus-summary-enter-digest-group
1598     "\M-\C-d" gnus-summary-read-document
1599     "\M-\C-e" gnus-summary-edit-parameters
1600     "\M-\C-a" gnus-summary-customize-parameters
1601     "\C-c\C-b" gnus-bug
1602     "*" gnus-cache-enter-article
1603     "\M-*" gnus-cache-remove-article
1604     "\M-&" gnus-summary-universal-argument
1605     "\C-l" gnus-recenter
1606     "I" gnus-summary-increase-score
1607     "L" gnus-summary-lower-score
1608     "\M-i" gnus-symbolic-argument
1609     "h" gnus-summary-select-article-buffer
1610
1611     "b" gnus-article-view-part
1612     "\M-t" gnus-summary-toggle-display-buttonized
1613
1614     "V" gnus-summary-score-map
1615     "X" gnus-uu-extract-map
1616     "S" gnus-summary-send-map)
1617
1618   ;; Sort of orthogonal keymap
1619   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1620     "t" gnus-summary-tick-article-forward
1621     "!" gnus-summary-tick-article-forward
1622     "d" gnus-summary-mark-as-read-forward
1623     "r" gnus-summary-mark-as-read-forward
1624     "c" gnus-summary-clear-mark-forward
1625     " " gnus-summary-clear-mark-forward
1626     "e" gnus-summary-mark-as-expirable
1627     "x" gnus-summary-mark-as-expirable
1628     "?" gnus-summary-mark-as-dormant
1629     "b" gnus-summary-set-bookmark
1630     "B" gnus-summary-remove-bookmark
1631     "#" gnus-summary-mark-as-processable
1632     "\M-#" gnus-summary-unmark-as-processable
1633     "S" gnus-summary-limit-include-expunged
1634     "C" gnus-summary-catchup
1635     "H" gnus-summary-catchup-to-here
1636     "h" gnus-summary-catchup-from-here
1637     "\C-c" gnus-summary-catchup-all
1638     "k" gnus-summary-kill-same-subject-and-select
1639     "K" gnus-summary-kill-same-subject
1640     "P" gnus-uu-mark-map)
1641
1642   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1643     "c" gnus-summary-clear-above
1644     "u" gnus-summary-tick-above
1645     "m" gnus-summary-mark-above
1646     "k" gnus-summary-kill-below)
1647
1648   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1649     "/" gnus-summary-limit-to-subject
1650     "n" gnus-summary-limit-to-articles
1651     "w" gnus-summary-pop-limit
1652     "s" gnus-summary-limit-to-subject
1653     "a" gnus-summary-limit-to-author
1654     "u" gnus-summary-limit-to-unread
1655     "m" gnus-summary-limit-to-marks
1656     "M" gnus-summary-limit-exclude-marks
1657     "v" gnus-summary-limit-to-score
1658     "*" gnus-summary-limit-include-cached
1659     "D" gnus-summary-limit-include-dormant
1660     "T" gnus-summary-limit-include-thread
1661     "d" gnus-summary-limit-exclude-dormant
1662     "t" gnus-summary-limit-to-age
1663     "." gnus-summary-limit-to-unseen
1664     "x" gnus-summary-limit-to-extra
1665     "p" gnus-summary-limit-to-display-predicate
1666     "E" gnus-summary-limit-include-expunged
1667     "c" gnus-summary-limit-exclude-childless-dormant
1668     "C" gnus-summary-limit-mark-excluded-as-read
1669     "o" gnus-summary-insert-old-articles
1670     "N" gnus-summary-insert-new-articles)
1671
1672   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1673     "n" gnus-summary-next-unread-article
1674     "p" gnus-summary-prev-unread-article
1675     "N" gnus-summary-next-article
1676     "P" gnus-summary-prev-article
1677     "\C-n" gnus-summary-next-same-subject
1678     "\C-p" gnus-summary-prev-same-subject
1679     "\M-n" gnus-summary-next-unread-subject
1680     "\M-p" gnus-summary-prev-unread-subject
1681     "f" gnus-summary-first-unread-article
1682     "b" gnus-summary-best-unread-article
1683     "j" gnus-summary-goto-article
1684     "g" gnus-summary-goto-subject
1685     "l" gnus-summary-goto-last-article
1686     "o" gnus-summary-pop-article)
1687
1688   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1689     "k" gnus-summary-kill-thread
1690     "l" gnus-summary-lower-thread
1691     "i" gnus-summary-raise-thread
1692     "T" gnus-summary-toggle-threads
1693     "t" gnus-summary-rethread-current
1694     "^" gnus-summary-reparent-thread
1695     "s" gnus-summary-show-thread
1696     "S" gnus-summary-show-all-threads
1697     "h" gnus-summary-hide-thread
1698     "H" gnus-summary-hide-all-threads
1699     "n" gnus-summary-next-thread
1700     "p" gnus-summary-prev-thread
1701     "u" gnus-summary-up-thread
1702     "o" gnus-summary-top-thread
1703     "d" gnus-summary-down-thread
1704     "#" gnus-uu-mark-thread
1705     "\M-#" gnus-uu-unmark-thread)
1706
1707   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1708     "g" gnus-summary-prepare
1709     "c" gnus-summary-insert-cached-articles)
1710
1711   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1712     "c" gnus-summary-catchup-and-exit
1713     "C" gnus-summary-catchup-all-and-exit
1714     "E" gnus-summary-exit-no-update
1715     "Q" gnus-summary-exit
1716     "Z" gnus-summary-exit
1717     "n" gnus-summary-catchup-and-goto-next-group
1718     "R" gnus-summary-reselect-current-group
1719     "G" gnus-summary-rescan-group
1720     "N" gnus-summary-next-group
1721     "s" gnus-summary-save-newsrc
1722     "P" gnus-summary-prev-group)
1723
1724   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1725     " " gnus-summary-next-page
1726     "n" gnus-summary-next-page
1727     "\177" gnus-summary-prev-page
1728     [delete] gnus-summary-prev-page
1729     "p" gnus-summary-prev-page
1730     "\r" gnus-summary-scroll-up
1731     "\M-\r" gnus-summary-scroll-down
1732     "<" gnus-summary-beginning-of-article
1733     ">" gnus-summary-end-of-article
1734     "b" gnus-summary-beginning-of-article
1735     "e" gnus-summary-end-of-article
1736     "^" gnus-summary-refer-parent-article
1737     "r" gnus-summary-refer-parent-article
1738     "D" gnus-summary-enter-digest-group
1739     "R" gnus-summary-refer-references
1740     "T" gnus-summary-refer-thread
1741     "g" gnus-summary-show-article
1742     "s" gnus-summary-isearch-article
1743     "P" gnus-summary-print-article
1744     "M" gnus-mailing-list-insinuate
1745     "t" gnus-article-babel)
1746
1747   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1748     "b" gnus-article-add-buttons
1749     "B" gnus-article-add-buttons-to-head
1750     "o" gnus-article-treat-overstrike
1751     "e" gnus-article-emphasize
1752     "w" gnus-article-fill-cited-article
1753     "Q" gnus-article-fill-long-lines
1754     "C" gnus-article-capitalize-sentences
1755     "c" gnus-article-remove-cr
1756     "q" gnus-article-de-quoted-unreadable
1757     "6" gnus-article-de-base64-unreadable
1758     "Z" gnus-article-decode-HZ
1759     "h" gnus-article-wash-html
1760     "u" gnus-article-unsplit-urls
1761     "s" gnus-summary-force-verify-and-decrypt
1762     "f" gnus-article-display-x-face
1763     "l" gnus-summary-stop-page-breaking
1764     "r" gnus-summary-caesar-message
1765     "t" gnus-summary-toggle-header
1766     "g" gnus-treat-smiley
1767     "v" gnus-summary-verbose-headers
1768     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1769     "p" gnus-article-verify-x-pgp-sig
1770     "d" gnus-article-treat-dumbquotes
1771     "k" gnus-article-outlook-deuglify-article)
1772
1773   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1774     "a" gnus-article-hide
1775     "h" gnus-article-hide-headers
1776     "b" gnus-article-hide-boring-headers
1777     "s" gnus-article-hide-signature
1778     "c" gnus-article-hide-citation
1779     "C" gnus-article-hide-citation-in-followups
1780     "l" gnus-article-hide-list-identifiers
1781     "p" gnus-article-hide-pgp
1782     "B" gnus-article-strip-banner
1783     "P" gnus-article-hide-pem
1784     "\C-c" gnus-article-hide-citation-maybe)
1785
1786   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1787     "a" gnus-article-highlight
1788     "h" gnus-article-highlight-headers
1789     "c" gnus-article-highlight-citation
1790     "s" gnus-article-highlight-signature)
1791
1792   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1793     "f" gnus-article-treat-fold-headers
1794     "u" gnus-article-treat-unfold-headers
1795     "n" gnus-article-treat-fold-newsgroups)
1796
1797   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1798     "x" gnus-article-display-x-face
1799     "s" gnus-treat-smiley
1800     "D" gnus-article-remove-images
1801     "f" gnus-treat-from-picon
1802     "m" gnus-treat-mail-picon
1803     "n" gnus-treat-newsgroups-picon)
1804
1805   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1806     "w" gnus-article-decode-mime-words
1807     "c" gnus-article-decode-charset
1808     "v" gnus-mime-view-all-parts
1809     "b" gnus-article-view-part)
1810
1811   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1812     "z" gnus-article-date-ut
1813     "u" gnus-article-date-ut
1814     "l" gnus-article-date-local
1815     "p" gnus-article-date-english
1816     "e" gnus-article-date-lapsed
1817     "o" gnus-article-date-original
1818     "i" gnus-article-date-iso8601
1819     "s" gnus-article-date-user)
1820
1821   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1822     "t" gnus-article-remove-trailing-blank-lines
1823     "l" gnus-article-strip-leading-blank-lines
1824     "m" gnus-article-strip-multiple-blank-lines
1825     "a" gnus-article-strip-blank-lines
1826     "A" gnus-article-strip-all-blank-lines
1827     "s" gnus-article-strip-leading-space
1828     "e" gnus-article-strip-trailing-space
1829     "w" gnus-article-remove-leading-whitespace)
1830
1831   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1832     "v" gnus-version
1833     "f" gnus-summary-fetch-faq
1834     "d" gnus-summary-describe-group
1835     "h" gnus-summary-describe-briefly
1836     "i" gnus-info-find-node
1837     "c" gnus-group-fetch-charter
1838     "C" gnus-group-fetch-control)
1839
1840   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1841     "e" gnus-summary-expire-articles
1842     "\M-\C-e" gnus-summary-expire-articles-now
1843     "\177" gnus-summary-delete-article
1844     [delete] gnus-summary-delete-article
1845     [backspace] gnus-summary-delete-article
1846     "m" gnus-summary-move-article
1847     "r" gnus-summary-respool-article
1848     "w" gnus-summary-edit-article
1849     "c" gnus-summary-copy-article
1850     "B" gnus-summary-crosspost-article
1851     "q" gnus-summary-respool-query
1852     "t" gnus-summary-respool-trace
1853     "i" gnus-summary-import-article
1854     "I" gnus-summary-create-article
1855     "p" gnus-summary-article-posted-p)
1856
1857   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1858     "o" gnus-summary-save-article
1859     "m" gnus-summary-save-article-mail
1860     "F" gnus-summary-write-article-file
1861     "r" gnus-summary-save-article-rmail
1862     "f" gnus-summary-save-article-file
1863     "b" gnus-summary-save-article-body-file
1864     "h" gnus-summary-save-article-folder
1865     "v" gnus-summary-save-article-vm
1866     "p" gnus-summary-pipe-output
1867     "P" gnus-summary-muttprint
1868     "s" gnus-soup-add-article)
1869
1870   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1871     "b" gnus-summary-display-buttonized
1872     "m" gnus-summary-repair-multipart
1873     "v" gnus-article-view-part
1874     "o" gnus-article-save-part
1875     "c" gnus-article-copy-part
1876     "C" gnus-article-view-part-as-charset
1877     "e" gnus-article-view-part-externally
1878     "E" gnus-article-encrypt-body
1879     "i" gnus-article-inline-part
1880     "|" gnus-article-pipe-part)
1881
1882   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1883     "p" gnus-summary-mark-as-processable
1884     "u" gnus-summary-unmark-as-processable
1885     "U" gnus-summary-unmark-all-processable
1886     "v" gnus-uu-mark-over
1887     "s" gnus-uu-mark-series
1888     "r" gnus-uu-mark-region
1889     "g" gnus-uu-unmark-region
1890     "R" gnus-uu-mark-by-regexp
1891     "G" gnus-uu-unmark-by-regexp
1892     "t" gnus-uu-mark-thread
1893     "T" gnus-uu-unmark-thread
1894     "a" gnus-uu-mark-all
1895     "b" gnus-uu-mark-buffer
1896     "S" gnus-uu-mark-sparse
1897     "k" gnus-summary-kill-process-mark
1898     "y" gnus-summary-yank-process-mark
1899     "w" gnus-summary-save-process-mark
1900     "i" gnus-uu-invert-processable)
1901
1902   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1903     ;;"x" gnus-uu-extract-any
1904     "m" gnus-summary-save-parts
1905     "u" gnus-uu-decode-uu
1906     "U" gnus-uu-decode-uu-and-save
1907     "s" gnus-uu-decode-unshar
1908     "S" gnus-uu-decode-unshar-and-save
1909     "o" gnus-uu-decode-save
1910     "O" gnus-uu-decode-save
1911     "b" gnus-uu-decode-binhex
1912     "B" gnus-uu-decode-binhex
1913     "p" gnus-uu-decode-postscript
1914     "P" gnus-uu-decode-postscript-and-save)
1915
1916   (gnus-define-keys
1917       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1918     "u" gnus-uu-decode-uu-view
1919     "U" gnus-uu-decode-uu-and-save-view
1920     "s" gnus-uu-decode-unshar-view
1921     "S" gnus-uu-decode-unshar-and-save-view
1922     "o" gnus-uu-decode-save-view
1923     "O" gnus-uu-decode-save-view
1924     "b" gnus-uu-decode-binhex-view
1925     "B" gnus-uu-decode-binhex-view
1926     "p" gnus-uu-decode-postscript-view
1927     "P" gnus-uu-decode-postscript-and-save-view))
1928
1929 (defvar gnus-article-post-menu nil)
1930
1931 (defconst gnus-summary-menu-maxlen 20)
1932
1933 (defun gnus-summary-menu-split (menu)
1934   ;; If we have lots of elements, divide them into groups of 20
1935   ;; and make a pane (or submenu) for each one.
1936   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1937       (let ((menu menu) sublists next
1938             (i 1))
1939         (while menu
1940           ;; Pull off the next gnus-summary-menu-maxlen elements
1941           ;; and make them the next element of sublist.
1942           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1943           (if next
1944               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1945                       nil))
1946           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1947                                              (aref (car (last menu)) 0)) menu)
1948                                sublists))
1949           (setq i (1+ i))
1950           (setq menu next))
1951         (nreverse sublists))
1952     ;; Few elements--put them all in one pane.
1953     menu))
1954
1955 (defun gnus-summary-make-menu-bar ()
1956   (gnus-turn-off-edit-menu 'summary)
1957
1958   (unless (boundp 'gnus-summary-misc-menu)
1959
1960     (easy-menu-define
1961       gnus-summary-kill-menu gnus-summary-mode-map ""
1962       (cons
1963        "Score"
1964        (nconc
1965         (list
1966          ["Customize" gnus-score-customize t])
1967         (gnus-make-score-map 'increase)
1968         (gnus-make-score-map 'lower)
1969         '(("Mark"
1970            ["Kill below" gnus-summary-kill-below t]
1971            ["Mark above" gnus-summary-mark-above t]
1972            ["Tick above" gnus-summary-tick-above t]
1973            ["Clear above" gnus-summary-clear-above t])
1974           ["Current score" gnus-summary-current-score t]
1975           ["Set score" gnus-summary-set-score t]
1976           ["Switch current score file..." gnus-score-change-score-file t]
1977           ["Set mark below..." gnus-score-set-mark-below t]
1978           ["Set expunge below..." gnus-score-set-expunge-below t]
1979           ["Edit current score file" gnus-score-edit-current-scores t]
1980           ["Edit score file" gnus-score-edit-file t]
1981           ["Trace score" gnus-score-find-trace t]
1982           ["Find words" gnus-score-find-favourite-words t]
1983           ["Rescore buffer" gnus-summary-rescore t]
1984           ["Increase score..." gnus-summary-increase-score t]
1985           ["Lower score..." gnus-summary-lower-score t]))))
1986
1987     ;; Define both the Article menu in the summary buffer and the
1988     ;; equivalent Commands menu in the article buffer here for
1989     ;; consistency.
1990     (let ((innards
1991            `(("Hide"
1992               ["All" gnus-article-hide t]
1993               ["Headers" gnus-article-hide-headers t]
1994               ["Signature" gnus-article-hide-signature t]
1995               ["Citation" gnus-article-hide-citation t]
1996               ["List identifiers" gnus-article-hide-list-identifiers t]
1997               ["PGP" gnus-article-hide-pgp t]
1998               ["Banner" gnus-article-strip-banner t]
1999               ["Boring headers" gnus-article-hide-boring-headers t])
2000              ("Highlight"
2001               ["All" gnus-article-highlight t]
2002               ["Headers" gnus-article-highlight-headers t]
2003               ["Signature" gnus-article-highlight-signature t]
2004               ["Citation" gnus-article-highlight-citation t])
2005              ("MIME"
2006               ["Words" gnus-article-decode-mime-words t]
2007               ["Charset" gnus-article-decode-charset t]
2008               ["QP" gnus-article-de-quoted-unreadable t]
2009               ["Base64" gnus-article-de-base64-unreadable t]
2010               ["View MIME buttons" gnus-summary-display-buttonized t]
2011               ["View all" gnus-mime-view-all-parts t]
2012               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2013               ["Encrypt body" gnus-article-encrypt-body t]
2014               ["Extract all parts" gnus-summary-save-parts t])
2015              ("Date"
2016               ["Local" gnus-article-date-local t]
2017               ["ISO8601" gnus-article-date-iso8601 t]
2018               ["UT" gnus-article-date-ut t]
2019               ["Original" gnus-article-date-original t]
2020               ["Lapsed" gnus-article-date-lapsed t]
2021               ["User-defined" gnus-article-date-user t])
2022              ("Display"
2023               ["Remove images" gnus-article-remove-images t]
2024               ["Toggle smiley" gnus-treat-smiley t]
2025               ["Show X-Face" gnus-article-display-x-face t]
2026               ["Show picons in From" gnus-treat-from-picon t]
2027               ["Show picons in mail headers" gnus-treat-mail-picon t]
2028               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2029               ("View as different encoding"
2030                ,@(gnus-summary-menu-split
2031                   (mapcar
2032                    (lambda (cs)
2033                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2034                      ;; forms for menu commands, we should provide intern'ed
2035                      ;; function symbols.
2036                      (let ((command (intern (format "\
2037 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2038                        (fset command
2039                              `(lambda ()
2040                                 (interactive)
2041                                 (let ((gnus-summary-show-article-charset-alist
2042                                        '((1 . ,cs))))
2043                                   (gnus-summary-show-article 1))))
2044                        `[,(symbol-name cs) ,command t]))
2045                    (sort (if (fboundp 'coding-system-list)
2046                              (coding-system-list)
2047                            (mapcar 'car mm-mime-mule-charset-alist))
2048                          'string<)))))
2049              ("Washing"
2050               ("Remove Blanks"
2051                ["Leading" gnus-article-strip-leading-blank-lines t]
2052                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2053                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2054                ["All of the above" gnus-article-strip-blank-lines t]
2055                ["All" gnus-article-strip-all-blank-lines t]
2056                ["Leading space" gnus-article-strip-leading-space t]
2057                ["Trailing space" gnus-article-strip-trailing-space t]
2058                ["Leading space in headers"
2059                 gnus-article-remove-leading-whitespace t])
2060               ["Overstrike" gnus-article-treat-overstrike t]
2061               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2062               ["Emphasis" gnus-article-emphasize t]
2063               ["Word wrap" gnus-article-fill-cited-article t]
2064               ["Fill long lines" gnus-article-fill-long-lines t]
2065               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2066               ["CR" gnus-article-remove-cr t]
2067               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2068               ["Base64" gnus-article-de-base64-unreadable t]
2069               ["Rot 13" gnus-summary-caesar-message
2070                ,@(if (featurep 'xemacs) '(t)
2071                    '(:help "\"Caesar rotate\" article by 13"))]
2072               ["Unix pipe..." gnus-summary-pipe-message t]
2073               ["Add buttons" gnus-article-add-buttons t]
2074               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2075               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2076               ["Verbose header" gnus-summary-verbose-headers t]
2077               ["Toggle header" gnus-summary-toggle-header t]
2078               ["Unfold headers" gnus-article-treat-unfold-headers t]
2079               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2080               ["Html" gnus-article-wash-html t]
2081               ["URLs" gnus-article-unsplit-urls t]
2082               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2083               ["HZ" gnus-article-decode-HZ t]
2084               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2085               )
2086              ("Output"
2087               ["Save in default format" gnus-summary-save-article
2088                ,@(if (featurep 'xemacs) '(t)
2089                    '(:help "Save article using default method"))]
2090               ["Save in file" gnus-summary-save-article-file
2091                ,@(if (featurep 'xemacs) '(t)
2092                    '(:help "Save article in file"))]
2093               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2094               ["Save in MH folder" gnus-summary-save-article-folder t]
2095               ["Save in VM folder" gnus-summary-save-article-vm t]
2096               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2097               ["Save body in file" gnus-summary-save-article-body-file t]
2098               ["Pipe through a filter" gnus-summary-pipe-output t]
2099               ["Add to SOUP packet" gnus-soup-add-article t]
2100               ["Print with Muttprint" gnus-summary-muttprint t]
2101               ["Print" gnus-summary-print-article t])
2102              ("Backend"
2103               ["Respool article..." gnus-summary-respool-article t]
2104               ["Move article..." gnus-summary-move-article
2105                (gnus-check-backend-function
2106                 'request-move-article gnus-newsgroup-name)]
2107               ["Copy article..." gnus-summary-copy-article t]
2108               ["Crosspost article..." gnus-summary-crosspost-article
2109                (gnus-check-backend-function
2110                 'request-replace-article gnus-newsgroup-name)]
2111               ["Import file..." gnus-summary-import-article t]
2112               ["Create article..." gnus-summary-create-article t]
2113               ["Check if posted" gnus-summary-article-posted-p t]
2114               ["Edit article" gnus-summary-edit-article
2115                (not (gnus-group-read-only-p))]
2116               ["Delete article" gnus-summary-delete-article
2117                (gnus-check-backend-function
2118                 'request-expire-articles gnus-newsgroup-name)]
2119               ["Query respool" gnus-summary-respool-query t]
2120               ["Trace respool" gnus-summary-respool-trace t]
2121               ["Delete expirable articles" gnus-summary-expire-articles-now
2122                (gnus-check-backend-function
2123                 'request-expire-articles gnus-newsgroup-name)])
2124              ("Extract"
2125               ["Uudecode" gnus-uu-decode-uu
2126                ,@(if (featurep 'xemacs) '(t)
2127                    '(:help "Decode uuencoded article(s)"))]
2128               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2129               ["Unshar" gnus-uu-decode-unshar t]
2130               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2131               ["Save" gnus-uu-decode-save t]
2132               ["Binhex" gnus-uu-decode-binhex t]
2133               ["Postscript" gnus-uu-decode-postscript t]
2134               ["All MIME parts" gnus-summary-save-parts t])
2135              ("Cache"
2136               ["Enter article" gnus-cache-enter-article t]
2137               ["Remove article" gnus-cache-remove-article t])
2138              ["Translate" gnus-article-babel t]
2139              ["Select article buffer" gnus-summary-select-article-buffer t]
2140              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2141              ["Isearch article..." gnus-summary-isearch-article t]
2142              ["Beginning of the article" gnus-summary-beginning-of-article t]
2143              ["End of the article" gnus-summary-end-of-article t]
2144              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2145              ["Fetch referenced articles" gnus-summary-refer-references t]
2146              ["Fetch current thread" gnus-summary-refer-thread t]
2147              ["Fetch article with id..." gnus-summary-refer-article t]
2148              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2149              ["Redisplay" gnus-summary-show-article t]
2150              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2151       (easy-menu-define
2152         gnus-summary-article-menu gnus-summary-mode-map ""
2153         (cons "Article" innards))
2154
2155       (if (not (keymapp gnus-summary-article-menu))
2156           (easy-menu-define
2157             gnus-article-commands-menu gnus-article-mode-map ""
2158             (cons "Commands" innards))
2159         ;; in Emacs, don't share menu.
2160         (setq gnus-article-commands-menu
2161               (copy-keymap gnus-summary-article-menu))
2162         (define-key gnus-article-mode-map [menu-bar commands]
2163           (cons "Commands" gnus-article-commands-menu))))
2164
2165     (easy-menu-define
2166       gnus-summary-thread-menu gnus-summary-mode-map ""
2167       '("Threads"
2168         ["Toggle threading" gnus-summary-toggle-threads t]
2169         ["Hide threads" gnus-summary-hide-all-threads t]
2170         ["Show threads" gnus-summary-show-all-threads t]
2171         ["Hide thread" gnus-summary-hide-thread t]
2172         ["Show thread" gnus-summary-show-thread t]
2173         ["Go to next thread" gnus-summary-next-thread t]
2174         ["Go to previous thread" gnus-summary-prev-thread t]
2175         ["Go down thread" gnus-summary-down-thread t]
2176         ["Go up thread" gnus-summary-up-thread t]
2177         ["Top of thread" gnus-summary-top-thread t]
2178         ["Mark thread as read" gnus-summary-kill-thread t]
2179         ["Lower thread score" gnus-summary-lower-thread t]
2180         ["Raise thread score" gnus-summary-raise-thread t]
2181         ["Rethread current" gnus-summary-rethread-current t]))
2182
2183     (easy-menu-define
2184       gnus-summary-post-menu gnus-summary-mode-map ""
2185       `("Post"
2186         ["Send a message (mail or news)" gnus-summary-post-news
2187          ,@(if (featurep 'xemacs) '(t)
2188              '(:help "Post an article"))]
2189         ["Followup" gnus-summary-followup
2190          ,@(if (featurep 'xemacs) '(t)
2191              '(:help "Post followup to this article"))]
2192         ["Followup and yank" gnus-summary-followup-with-original
2193          ,@(if (featurep 'xemacs) '(t)
2194              '(:help "Post followup to this article, quoting its contents"))]
2195         ["Supersede article" gnus-summary-supersede-article t]
2196         ["Cancel article" gnus-summary-cancel-article
2197          ,@(if (featurep 'xemacs) '(t)
2198              '(:help "Cancel an article you posted"))]
2199         ["Reply" gnus-summary-reply t]
2200         ["Reply and yank" gnus-summary-reply-with-original t]
2201         ["Wide reply" gnus-summary-wide-reply t]
2202         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2203          ,@(if (featurep 'xemacs) '(t)
2204              '(:help "Mail a reply, quoting this article"))]
2205         ["Very wide reply" gnus-summary-very-wide-reply t]
2206         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2207          ,@(if (featurep 'xemacs) '(t)
2208              '(:help "Mail a very wide reply, quoting this article"))]
2209         ["Mail forward" gnus-summary-mail-forward t]
2210         ["Post forward" gnus-summary-post-forward t]
2211         ["Digest and mail" gnus-uu-digest-mail-forward t]
2212         ["Digest and post" gnus-uu-digest-post-forward t]
2213         ["Resend message" gnus-summary-resend-message t]
2214         ["Resend message edit" gnus-summary-resend-message-edit t]
2215         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2216         ["Send a mail" gnus-summary-mail-other-window t]
2217         ["Create a local message" gnus-summary-news-other-window t]
2218         ["Uuencode and post" gnus-uu-post-news
2219          ,@(if (featurep 'xemacs) '(t)
2220              '(:help "Post a uuencoded article"))]
2221         ["Followup via news" gnus-summary-followup-to-mail t]
2222         ["Followup via news and yank"
2223          gnus-summary-followup-to-mail-with-original t]
2224         ;;("Draft"
2225         ;;["Send" gnus-summary-send-draft t]
2226         ;;["Send bounced" gnus-resend-bounced-mail t])
2227         ))
2228
2229     (cond
2230      ((not (keymapp gnus-summary-post-menu))
2231       (setq gnus-article-post-menu gnus-summary-post-menu))
2232      ((not gnus-article-post-menu)
2233       ;; Don't share post menu.
2234       (setq gnus-article-post-menu
2235             (copy-keymap gnus-summary-post-menu))))
2236     (define-key gnus-article-mode-map [menu-bar post]
2237       (cons "Post" gnus-article-post-menu))
2238
2239     (easy-menu-define
2240       gnus-summary-misc-menu gnus-summary-mode-map ""
2241       `("Gnus"
2242         ("Mark Read"
2243          ["Mark as read" gnus-summary-mark-as-read-forward t]
2244          ["Mark same subject and select"
2245           gnus-summary-kill-same-subject-and-select t]
2246          ["Mark same subject" gnus-summary-kill-same-subject t]
2247          ["Catchup" gnus-summary-catchup
2248           ,@(if (featurep 'xemacs) '(t)
2249               '(:help "Mark unread articles in this group as read"))]
2250          ["Catchup all" gnus-summary-catchup-all t]
2251          ["Catchup to here" gnus-summary-catchup-to-here t]
2252          ["Catchup from here" gnus-summary-catchup-from-here t]
2253          ["Catchup region" gnus-summary-mark-region-as-read t]
2254          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2255         ("Mark Various"
2256          ["Tick" gnus-summary-tick-article-forward t]
2257          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2258          ["Remove marks" gnus-summary-clear-mark-forward t]
2259          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2260          ["Set bookmark" gnus-summary-set-bookmark t]
2261          ["Remove bookmark" gnus-summary-remove-bookmark t])
2262         ("Limit to"
2263          ["Marks..." gnus-summary-limit-to-marks t]
2264          ["Subject..." gnus-summary-limit-to-subject t]
2265          ["Author..." gnus-summary-limit-to-author t]
2266          ["Age..." gnus-summary-limit-to-age t]
2267          ["Extra..." gnus-summary-limit-to-extra t]
2268          ["Score..." gnus-summary-limit-to-score t]
2269          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2270          ["Unread" gnus-summary-limit-to-unread t]
2271          ["Unseen" gnus-summary-limit-to-unseen t]
2272          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2273          ["Articles" gnus-summary-limit-to-articles t]
2274          ["Pop limit" gnus-summary-pop-limit t]
2275          ["Show dormant" gnus-summary-limit-include-dormant t]
2276          ["Hide childless dormant"
2277           gnus-summary-limit-exclude-childless-dormant t]
2278          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2279          ["Hide marked" gnus-summary-limit-exclude-marks t]
2280          ["Show expunged" gnus-summary-limit-include-expunged t])
2281         ("Process Mark"
2282          ["Set mark" gnus-summary-mark-as-processable t]
2283          ["Remove mark" gnus-summary-unmark-as-processable t]
2284          ["Remove all marks" gnus-summary-unmark-all-processable t]
2285          ["Mark above" gnus-uu-mark-over t]
2286          ["Mark series" gnus-uu-mark-series t]
2287          ["Mark region" gnus-uu-mark-region t]
2288          ["Unmark region" gnus-uu-unmark-region t]
2289          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2290          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2291          ["Mark all" gnus-uu-mark-all t]
2292          ["Mark buffer" gnus-uu-mark-buffer t]
2293          ["Mark sparse" gnus-uu-mark-sparse t]
2294          ["Mark thread" gnus-uu-mark-thread t]
2295          ["Unmark thread" gnus-uu-unmark-thread t]
2296          ("Process Mark Sets"
2297           ["Kill" gnus-summary-kill-process-mark t]
2298           ["Yank" gnus-summary-yank-process-mark
2299            gnus-newsgroup-process-stack]
2300           ["Save" gnus-summary-save-process-mark t]))
2301         ("Scroll article"
2302          ["Page forward" gnus-summary-next-page
2303           ,@(if (featurep 'xemacs) '(t)
2304               '(:help "Show next page of article"))]
2305          ["Page backward" gnus-summary-prev-page
2306           ,@(if (featurep 'xemacs) '(t)
2307               '(:help "Show previous page of article"))]
2308          ["Line forward" gnus-summary-scroll-up t])
2309         ("Move"
2310          ["Next unread article" gnus-summary-next-unread-article t]
2311          ["Previous unread article" gnus-summary-prev-unread-article t]
2312          ["Next article" gnus-summary-next-article t]
2313          ["Previous article" gnus-summary-prev-article t]
2314          ["Next unread subject" gnus-summary-next-unread-subject t]
2315          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2316          ["Next article same subject" gnus-summary-next-same-subject t]
2317          ["Previous article same subject" gnus-summary-prev-same-subject t]
2318          ["First unread article" gnus-summary-first-unread-article t]
2319          ["Best unread article" gnus-summary-best-unread-article t]
2320          ["Go to subject number..." gnus-summary-goto-subject t]
2321          ["Go to article number..." gnus-summary-goto-article t]
2322          ["Go to the last article" gnus-summary-goto-last-article t]
2323          ["Pop article off history" gnus-summary-pop-article t])
2324         ("Sort"
2325          ["Sort by number" gnus-summary-sort-by-number t]
2326          ["Sort by author" gnus-summary-sort-by-author t]
2327          ["Sort by subject" gnus-summary-sort-by-subject t]
2328          ["Sort by date" gnus-summary-sort-by-date t]
2329          ["Sort by score" gnus-summary-sort-by-score t]
2330          ["Sort by lines" gnus-summary-sort-by-lines t]
2331          ["Sort by characters" gnus-summary-sort-by-chars t]
2332          ["Randomize" gnus-summary-sort-by-random t]
2333          ["Original sort" gnus-summary-sort-by-original t])
2334         ("Help"
2335          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2336          ["Describe group" gnus-summary-describe-group t]
2337          ["Fetch charter" gnus-group-fetch-charter
2338           ,@(if (featurep 'xemacs) nil
2339               '(:help "Display the charter of the current group"))]
2340          ["Fetch control message" gnus-group-fetch-control
2341           ,@(if (featurep 'xemacs) nil
2342               '(:help "Display the archived control message for the current group"))]
2343          ["Read manual" gnus-info-find-node t])
2344         ("Modes"
2345          ["Pick and read" gnus-pick-mode t]
2346          ["Binary" gnus-binary-mode t])
2347         ("Regeneration"
2348          ["Regenerate" gnus-summary-prepare t]
2349          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2350          ["Toggle threading" gnus-summary-toggle-threads t])
2351         ["See old articles" gnus-summary-insert-old-articles t]
2352         ["See new articles" gnus-summary-insert-new-articles t]
2353         ["Filter articles..." gnus-summary-execute-command t]
2354         ["Run command on subjects..." gnus-summary-universal-argument t]
2355         ["Search articles forward..." gnus-summary-search-article-forward t]
2356         ["Search articles backward..." gnus-summary-search-article-backward t]
2357         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2358         ["Expand window" gnus-summary-expand-window t]
2359         ["Expire expirable articles" gnus-summary-expire-articles
2360          (gnus-check-backend-function
2361           'request-expire-articles gnus-newsgroup-name)]
2362         ["Edit local kill file" gnus-summary-edit-local-kill t]
2363         ["Edit main kill file" gnus-summary-edit-global-kill t]
2364         ["Edit group parameters" gnus-summary-edit-parameters t]
2365         ["Customize group parameters" gnus-summary-customize-parameters t]
2366         ["Send a bug report" gnus-bug t]
2367         ("Exit"
2368          ["Catchup and exit" gnus-summary-catchup-and-exit
2369           ,@(if (featurep 'xemacs) '(t)
2370               '(:help "Mark unread articles in this group as read, then exit"))]
2371          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2372          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2373          ["Exit group" gnus-summary-exit
2374           ,@(if (featurep 'xemacs) '(t)
2375               '(:help "Exit current group, return to group selection mode"))]
2376          ["Exit group without updating" gnus-summary-exit-no-update t]
2377          ["Exit and goto next group" gnus-summary-next-group t]
2378          ["Exit and goto prev group" gnus-summary-prev-group t]
2379          ["Reselect group" gnus-summary-reselect-current-group t]
2380          ["Rescan group" gnus-summary-rescan-group t]
2381          ["Update dribble" gnus-summary-save-newsrc t])))
2382
2383     (gnus-run-hooks 'gnus-summary-menu-hook)))
2384
2385 (defvar gnus-summary-tool-bar-map nil)
2386
2387 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2388 (defun gnus-summary-make-tool-bar ()
2389   (if (and (fboundp 'tool-bar-add-item-from-menu)
2390            (default-value 'tool-bar-mode)
2391            (not gnus-summary-tool-bar-map))
2392       (setq gnus-summary-tool-bar-map
2393             (let ((tool-bar-map (make-sparse-keymap))
2394                   (load-path (mm-image-load-path)))
2395               (tool-bar-add-item-from-menu
2396                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2397               (tool-bar-add-item-from-menu
2398                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2399               (tool-bar-add-item-from-menu
2400                'gnus-summary-post-news "post" gnus-summary-mode-map)
2401               (tool-bar-add-item-from-menu
2402                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2403               (tool-bar-add-item-from-menu
2404                'gnus-summary-followup "followup" gnus-summary-mode-map)
2405               (tool-bar-add-item-from-menu
2406                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2407               (tool-bar-add-item-from-menu
2408                'gnus-summary-reply "reply" gnus-summary-mode-map)
2409               (tool-bar-add-item-from-menu
2410                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2411               (tool-bar-add-item-from-menu
2412                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2413               (tool-bar-add-item-from-menu
2414                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2415               (tool-bar-add-item-from-menu
2416                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2417               (tool-bar-add-item-from-menu
2418                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2419               (tool-bar-add-item-from-menu
2420                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2421               (tool-bar-add-item-from-menu
2422                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2423               (tool-bar-add-item-from-menu
2424                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2425               tool-bar-map)))
2426   (if gnus-summary-tool-bar-map
2427       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2428
2429 (defun gnus-score-set-default (var value)
2430   "A version of set that updates the GNU Emacs menu-bar."
2431   (set var value)
2432   ;; It is the message that forces the active status to be updated.
2433   (message ""))
2434
2435 (defun gnus-make-score-map (type)
2436   "Make a summary score map of type TYPE."
2437   (if t
2438       nil
2439     (let ((headers '(("author" "from" string)
2440                      ("subject" "subject" string)
2441                      ("article body" "body" string)
2442                      ("article head" "head" string)
2443                      ("xref" "xref" string)
2444                      ("extra header" "extra" string)
2445                      ("lines" "lines" number)
2446                      ("followups to author" "followup" string)))
2447           (types '((number ("less than" <)
2448                            ("greater than" >)
2449                            ("equal" =))
2450                    (string ("substring" s)
2451                            ("exact string" e)
2452                            ("fuzzy string" f)
2453                            ("regexp" r))))
2454           (perms '(("temporary" (current-time-string))
2455                    ("permanent" nil)
2456                    ("immediate" now)))
2457           header)
2458       (list
2459        (apply
2460         'nconc
2461         (list
2462          (if (eq type 'lower)
2463              "Lower score"
2464            "Increase score"))
2465         (let (outh)
2466           (while headers
2467             (setq header (car headers))
2468             (setq outh
2469                   (cons
2470                    (apply
2471                     'nconc
2472                     (list (car header))
2473                     (let ((ts (cdr (assoc (nth 2 header) types)))
2474                           outt)
2475                       (while ts
2476                         (setq outt
2477                               (cons
2478                                (apply
2479                                 'nconc
2480                                 (list (caar ts))
2481                                 (let ((ps perms)
2482                                       outp)
2483                                   (while ps
2484                                     (setq outp
2485                                           (cons
2486                                            (vector
2487                                             (caar ps)
2488                                             (list
2489                                              'gnus-summary-score-entry
2490                                              (nth 1 header)
2491                                              (if (or (string= (nth 1 header)
2492                                                               "head")
2493                                                      (string= (nth 1 header)
2494                                                               "body"))
2495                                                  ""
2496                                                (list 'gnus-summary-header
2497                                                      (nth 1 header)))
2498                                              (list 'quote (nth 1 (car ts)))
2499                                              (list 'gnus-score-delta-default
2500                                                    nil)
2501                                              (nth 1 (car ps))
2502                                              t)
2503                                             t)
2504                                            outp))
2505                                     (setq ps (cdr ps)))
2506                                   (list (nreverse outp))))
2507                                outt))
2508                         (setq ts (cdr ts)))
2509                       (list (nreverse outt))))
2510                    outh))
2511             (setq headers (cdr headers)))
2512           (list (nreverse outh))))))))
2513
2514 \f
2515
2516 (defun gnus-summary-mode (&optional group)
2517   "Major mode for reading articles.
2518
2519 All normal editing commands are switched off.
2520 \\<gnus-summary-mode-map>
2521 Each line in this buffer represents one article.  To read an
2522 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2523 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2524 respectively.
2525
2526 You can also post articles and send mail from this buffer.  To
2527 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2528 of an article, type `\\[gnus-summary-reply]'.
2529
2530 There are approx. one gazillion commands you can execute in this
2531 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2532
2533 The following commands are available:
2534
2535 \\{gnus-summary-mode-map}"
2536   (interactive)
2537   (kill-all-local-variables)
2538   (when (gnus-visual-p 'summary-menu 'menu)
2539     (gnus-summary-make-menu-bar)
2540     (gnus-summary-make-tool-bar))
2541   (gnus-summary-make-local-variables)
2542   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2543     (gnus-summary-make-local-variables))
2544   (gnus-make-thread-indent-array)
2545   (gnus-simplify-mode-line)
2546   (setq major-mode 'gnus-summary-mode)
2547   (setq mode-name "Summary")
2548   (make-local-variable 'minor-mode-alist)
2549   (use-local-map gnus-summary-mode-map)
2550   (buffer-disable-undo)
2551   (setq buffer-read-only t)             ;Disable modification
2552   (setq truncate-lines t)
2553   (setq selective-display t)
2554   (setq selective-display-ellipses t)   ;Display `...'
2555   (gnus-summary-set-display-table)
2556   (gnus-set-default-directory)
2557   (setq gnus-newsgroup-name group)
2558   (make-local-variable 'gnus-summary-line-format)
2559   (make-local-variable 'gnus-summary-line-format-spec)
2560   (make-local-variable 'gnus-summary-dummy-line-format)
2561   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2562   (make-local-variable 'gnus-summary-mark-positions)
2563   (make-local-hook 'pre-command-hook)
2564   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2565   (gnus-run-hooks 'gnus-summary-mode-hook)
2566   (turn-on-gnus-mailing-list-mode)
2567   (mm-enable-multibyte)
2568   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2569   (gnus-update-summary-mark-positions))
2570
2571 (defun gnus-summary-make-local-variables ()
2572   "Make all the local summary buffer variables."
2573   (let (global)
2574     (dolist (local gnus-summary-local-variables)
2575       (if (consp local)
2576           (progn
2577             (if (eq (cdr local) 'global)
2578                 ;; Copy the global value of the variable.
2579                 (setq global (symbol-value (car local)))
2580               ;; Use the value from the list.
2581               (setq global (eval (cdr local))))
2582             (set (make-local-variable (car local)) global))
2583         ;; Simple nil-valued local variable.
2584         (set (make-local-variable local) nil)))))
2585
2586 (defun gnus-summary-clear-local-variables ()
2587   (let ((locals gnus-summary-local-variables))
2588     (while locals
2589       (if (consp (car locals))
2590           (and (vectorp (caar locals))
2591                (set (caar locals) nil))
2592         (and (vectorp (car locals))
2593              (set (car locals) nil)))
2594       (setq locals (cdr locals)))))
2595
2596 ;; Summary data functions.
2597
2598 (defmacro gnus-data-number (data)
2599   `(car ,data))
2600
2601 (defmacro gnus-data-set-number (data number)
2602   `(setcar ,data ,number))
2603
2604 (defmacro gnus-data-mark (data)
2605   `(nth 1 ,data))
2606
2607 (defmacro gnus-data-set-mark (data mark)
2608   `(setcar (nthcdr 1 ,data) ,mark))
2609
2610 (defmacro gnus-data-pos (data)
2611   `(nth 2 ,data))
2612
2613 (defmacro gnus-data-set-pos (data pos)
2614   `(setcar (nthcdr 2 ,data) ,pos))
2615
2616 (defmacro gnus-data-header (data)
2617   `(nth 3 ,data))
2618
2619 (defmacro gnus-data-set-header (data header)
2620   `(setf (nth 3 ,data) ,header))
2621
2622 (defmacro gnus-data-level (data)
2623   `(nth 4 ,data))
2624
2625 (defmacro gnus-data-unread-p (data)
2626   `(= (nth 1 ,data) gnus-unread-mark))
2627
2628 (defmacro gnus-data-read-p (data)
2629   `(/= (nth 1 ,data) gnus-unread-mark))
2630
2631 (defmacro gnus-data-pseudo-p (data)
2632   `(consp (nth 3 ,data)))
2633
2634 (defmacro gnus-data-find (number)
2635   `(assq ,number gnus-newsgroup-data))
2636
2637 (defmacro gnus-data-find-list (number &optional data)
2638   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2639      (memq (assq ,number bdata)
2640            bdata)))
2641
2642 (defmacro gnus-data-make (number mark pos header level)
2643   `(list ,number ,mark ,pos ,header ,level))
2644
2645 (defun gnus-data-enter (after-article number mark pos header level offset)
2646   (let ((data (gnus-data-find-list after-article)))
2647     (unless data
2648       (error "No such article: %d" after-article))
2649     (setcdr data (cons (gnus-data-make number mark pos header level)
2650                        (cdr data)))
2651     (setq gnus-newsgroup-data-reverse nil)
2652     (gnus-data-update-list (cddr data) offset)))
2653
2654 (defun gnus-data-enter-list (after-article list &optional offset)
2655   (when list
2656     (let ((data (and after-article (gnus-data-find-list after-article)))
2657           (ilist list))
2658       (if (not (or data
2659                    after-article))
2660           (let ((odata gnus-newsgroup-data))
2661             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2662             (when offset
2663               (gnus-data-update-list odata offset)))
2664       ;; Find the last element in the list to be spliced into the main
2665         ;; list.
2666         (while (cdr list)
2667           (setq list (cdr list)))
2668         (if (not data)
2669             (progn
2670               (setcdr list gnus-newsgroup-data)
2671               (setq gnus-newsgroup-data ilist)
2672               (when offset
2673                 (gnus-data-update-list (cdr list) offset)))
2674           (setcdr list (cdr data))
2675           (setcdr data ilist)
2676           (when offset
2677             (gnus-data-update-list (cdr list) offset))))
2678       (setq gnus-newsgroup-data-reverse nil))))
2679
2680 (defun gnus-data-remove (article &optional offset)
2681   (let ((data gnus-newsgroup-data))
2682     (if (= (gnus-data-number (car data)) article)
2683         (progn
2684           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2685                 gnus-newsgroup-data-reverse nil)
2686           (when offset
2687             (gnus-data-update-list gnus-newsgroup-data offset)))
2688       (while (cdr data)
2689         (when (= (gnus-data-number (cadr data)) article)
2690           (setcdr data (cddr data))
2691           (when offset
2692             (gnus-data-update-list (cdr data) offset))
2693           (setq data nil
2694                 gnus-newsgroup-data-reverse nil))
2695         (setq data (cdr data))))))
2696
2697 (defmacro gnus-data-list (backward)
2698   `(if ,backward
2699        (or gnus-newsgroup-data-reverse
2700            (setq gnus-newsgroup-data-reverse
2701                  (reverse gnus-newsgroup-data)))
2702      gnus-newsgroup-data))
2703
2704 (defun gnus-data-update-list (data offset)
2705   "Add OFFSET to the POS of all data entries in DATA."
2706   (setq gnus-newsgroup-data-reverse nil)
2707   (while data
2708     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2709     (setq data (cdr data))))
2710
2711 (defun gnus-summary-article-pseudo-p (article)
2712   "Say whether this article is a pseudo article or not."
2713   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2714
2715 (defmacro gnus-summary-article-sparse-p (article)
2716   "Say whether this article is a sparse article or not."
2717   `(memq ,article gnus-newsgroup-sparse))
2718
2719 (defmacro gnus-summary-article-ancient-p (article)
2720   "Say whether this article is a sparse article or not."
2721   `(memq ,article gnus-newsgroup-ancient))
2722
2723 (defun gnus-article-parent-p (number)
2724   "Say whether this article is a parent or not."
2725   (let ((data (gnus-data-find-list number)))
2726     (and (cdr data)              ; There has to be an article after...
2727          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2728             (gnus-data-level (nth 1 data))))))
2729
2730 (defun gnus-article-children (number)
2731   "Return a list of all children to NUMBER."
2732   (let* ((data (gnus-data-find-list number))
2733          (level (gnus-data-level (car data)))
2734          children)
2735     (setq data (cdr data))
2736     (while (and data
2737                 (= (gnus-data-level (car data)) (1+ level)))
2738       (push (gnus-data-number (car data)) children)
2739       (setq data (cdr data)))
2740     children))
2741
2742 (defmacro gnus-summary-skip-intangible ()
2743   "If the current article is intangible, then jump to a different article."
2744   '(let ((to (get-text-property (point) 'gnus-intangible)))
2745      (and to (gnus-summary-goto-subject to))))
2746
2747 (defmacro gnus-summary-article-intangible-p ()
2748   "Say whether this article is intangible or not."
2749   '(get-text-property (point) 'gnus-intangible))
2750
2751 (defun gnus-article-read-p (article)
2752   "Say whether ARTICLE is read or not."
2753   (not (or (memq article gnus-newsgroup-marked)
2754            (memq article gnus-newsgroup-spam-marked)
2755            (memq article gnus-newsgroup-unreads)
2756            (memq article gnus-newsgroup-unselected)
2757            (memq article gnus-newsgroup-dormant))))
2758
2759 ;; Some summary mode macros.
2760
2761 (defmacro gnus-summary-article-number ()
2762   "The article number of the article on the current line.
2763 If there isn's an article number here, then we return the current
2764 article number."
2765   '(progn
2766      (gnus-summary-skip-intangible)
2767      (or (get-text-property (point) 'gnus-number)
2768          (gnus-summary-last-subject))))
2769
2770 (defmacro gnus-summary-article-header (&optional number)
2771   "Return the header of article NUMBER."
2772   `(gnus-data-header (gnus-data-find
2773                       ,(or number '(gnus-summary-article-number)))))
2774
2775 (defmacro gnus-summary-thread-level (&optional number)
2776   "Return the level of thread that starts with article NUMBER."
2777   `(if (and (eq gnus-summary-make-false-root 'dummy)
2778             (get-text-property (point) 'gnus-intangible))
2779        0
2780      (gnus-data-level (gnus-data-find
2781                        ,(or number '(gnus-summary-article-number))))))
2782
2783 (defmacro gnus-summary-article-mark (&optional number)
2784   "Return the mark of article NUMBER."
2785   `(gnus-data-mark (gnus-data-find
2786                     ,(or number '(gnus-summary-article-number)))))
2787
2788 (defmacro gnus-summary-article-pos (&optional number)
2789   "Return the position of the line of article NUMBER."
2790   `(gnus-data-pos (gnus-data-find
2791                    ,(or number '(gnus-summary-article-number)))))
2792
2793 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2794 (defmacro gnus-summary-article-subject (&optional number)
2795   "Return current subject string or nil if nothing."
2796   `(let ((headers
2797           ,(if number
2798                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2799              '(gnus-data-header (assq (gnus-summary-article-number)
2800                                       gnus-newsgroup-data)))))
2801      (and headers
2802           (vectorp headers)
2803           (mail-header-subject headers))))
2804
2805 (defmacro gnus-summary-article-score (&optional number)
2806   "Return current article score."
2807   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2808                   gnus-newsgroup-scored))
2809        gnus-summary-default-score 0))
2810
2811 (defun gnus-summary-article-children (&optional number)
2812   "Return a list of article numbers that are children of article NUMBER."
2813   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2814          (level (gnus-data-level (car data)))
2815          l children)
2816     (while (and (setq data (cdr data))
2817                 (> (setq l (gnus-data-level (car data))) level))
2818       (and (= (1+ level) l)
2819            (push (gnus-data-number (car data))
2820                  children)))
2821     (nreverse children)))
2822
2823 (defun gnus-summary-article-parent (&optional number)
2824   "Return the article number of the parent of article NUMBER."
2825   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2826                                     (gnus-data-list t)))
2827          (level (gnus-data-level (car data))))
2828     (if (zerop level)
2829         ()                              ; This is a root.
2830       ;; We search until we find an article with a level less than
2831       ;; this one.  That function has to be the parent.
2832       (while (and (setq data (cdr data))
2833                   (not (< (gnus-data-level (car data)) level))))
2834       (and data (gnus-data-number (car data))))))
2835
2836 (defun gnus-unread-mark-p (mark)
2837   "Say whether MARK is the unread mark."
2838   (= mark gnus-unread-mark))
2839
2840 (defun gnus-read-mark-p (mark)
2841   "Say whether MARK is one of the marks that mark as read.
2842 This is all marks except unread, ticked, dormant, and expirable."
2843   (not (or (= mark gnus-unread-mark)
2844            (= mark gnus-ticked-mark)
2845            (= mark gnus-dormant-mark)
2846            (= mark gnus-expirable-mark))))
2847
2848 (defmacro gnus-article-mark (number)
2849   "Return the MARK of article NUMBER.
2850 This macro should only be used when computing the mark the \"first\"
2851 time; i.e., when generating the summary lines.  After that,
2852 `gnus-summary-article-mark' should be used to examine the
2853 marks of articles."
2854   `(cond
2855     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2856     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2857     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2858     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2859     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2860     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2861     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2862     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2863     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2864            gnus-ancient-mark))))
2865
2866 ;; Saving hidden threads.
2867
2868 (defmacro gnus-save-hidden-threads (&rest forms)
2869   "Save hidden threads, eval FORMS, and restore the hidden threads."
2870   (let ((config (make-symbol "config")))
2871     `(let ((,config (gnus-hidden-threads-configuration)))
2872        (unwind-protect
2873            (save-excursion
2874              ,@forms)
2875          (gnus-restore-hidden-threads-configuration ,config)))))
2876 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2877 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2878
2879 (defun gnus-data-compute-positions ()
2880   "Compute the positions of all articles."
2881   (setq gnus-newsgroup-data-reverse nil)
2882   (let ((data gnus-newsgroup-data))
2883     (save-excursion
2884       (gnus-save-hidden-threads
2885         (gnus-summary-show-all-threads)
2886         (goto-char (point-min))
2887         (while data
2888           (while (get-text-property (point) 'gnus-intangible)
2889             (forward-line 1))
2890           (gnus-data-set-pos (car data) (+ (point) 3))
2891           (setq data (cdr data))
2892           (forward-line 1))))))
2893
2894 (defun gnus-hidden-threads-configuration ()
2895   "Return the current hidden threads configuration."
2896   (save-excursion
2897     (let (config)
2898       (goto-char (point-min))
2899       (while (search-forward "\r" nil t)
2900         (push (1- (point)) config))
2901       config)))
2902
2903 (defun gnus-restore-hidden-threads-configuration (config)
2904   "Restore hidden threads configuration from CONFIG."
2905   (save-excursion
2906     (let (point buffer-read-only)
2907       (while (setq point (pop config))
2908         (when (and (< point (point-max))
2909                    (goto-char point)
2910                    (eq (char-after) ?\n))
2911           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2912
2913 ;; Various summary mode internalish functions.
2914
2915 (defun gnus-mouse-pick-article (e)
2916   (interactive "e")
2917   (mouse-set-point e)
2918   (gnus-summary-next-page nil t))
2919
2920 (defun gnus-summary-set-display-table ()
2921   "Change the display table.
2922 Odd characters have a tendency to mess
2923 up nicely formatted displays - we make all possible glyphs
2924 display only a single character."
2925
2926   ;; We start from the standard display table, if any.
2927   (let ((table (or (copy-sequence standard-display-table)
2928                    (make-display-table)))
2929         (i 32))
2930     ;; Nix out all the control chars...
2931     (while (>= (setq i (1- i)) 0)
2932       (aset table i [??]))
2933    ;; ... but not newline and cr, of course.  (cr is necessary for the
2934     ;; selective display).
2935     (aset table ?\n nil)
2936     (aset table ?\r nil)
2937     ;; We keep TAB as well.
2938     (aset table ?\t nil)
2939     ;; We nix out any glyphs over 126 that are not set already.
2940     (let ((i 256))
2941       (while (>= (setq i (1- i)) 127)
2942         ;; Only modify if the entry is nil.
2943         (unless (aref table i)
2944           (aset table i [??]))))
2945     (setq buffer-display-table table)))
2946
2947 (defun gnus-summary-set-article-display-arrow (pos)
2948   "Update the overlay arrow to point to line at position POS."
2949   (when (and gnus-summary-display-arrow
2950              (boundp 'overlay-arrow-position)
2951              (boundp 'overlay-arrow-string))
2952     (save-excursion
2953       (goto-char pos)
2954       (beginning-of-line)
2955       (unless overlay-arrow-position
2956         (setq overlay-arrow-position (make-marker)))
2957       (setq overlay-arrow-string "=>"
2958             overlay-arrow-position (set-marker overlay-arrow-position
2959                                                (point)
2960                                                (current-buffer))))))
2961
2962 (defun gnus-summary-buffer-name (group)
2963   "Return the summary buffer name of GROUP."
2964   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2965
2966 (defun gnus-summary-setup-buffer (group)
2967   "Initialize summary buffer."
2968   (let ((buffer (gnus-summary-buffer-name group))
2969         (dead-name (concat "*Dead Summary "
2970                            (gnus-group-decoded-name group) "*")))
2971     ;; If a dead summary buffer exists, we kill it.
2972     (when (gnus-buffer-live-p dead-name)
2973       (gnus-kill-buffer dead-name))
2974     (if (get-buffer buffer)
2975         (progn
2976           (set-buffer buffer)
2977           (setq gnus-summary-buffer (current-buffer))
2978           (not gnus-newsgroup-prepared))
2979       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2980       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2981       (gnus-summary-mode group)
2982       (when gnus-carpal
2983         (gnus-carpal-setup-buffer 'summary))
2984       (unless gnus-single-article-buffer
2985         (make-local-variable 'gnus-article-buffer)
2986         (make-local-variable 'gnus-article-current)
2987         (make-local-variable 'gnus-original-article-buffer))
2988       (setq gnus-newsgroup-name group)
2989       ;; Set any local variables in the group parameters.
2990       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2991       t)))
2992
2993 (defun gnus-set-global-variables ()
2994   "Set the global equivalents of the buffer-local variables.
2995 They are set to the latest values they had.  These reflect the summary
2996 buffer that was in action when the last article was fetched."
2997   (when (eq major-mode 'gnus-summary-mode)
2998     (setq gnus-summary-buffer (current-buffer))
2999     (let ((name gnus-newsgroup-name)
3000           (marked gnus-newsgroup-marked)
3001           (spam gnus-newsgroup-spam-marked)
3002           (unread gnus-newsgroup-unreads)
3003           (headers gnus-current-headers)
3004           (data gnus-newsgroup-data)
3005           (summary gnus-summary-buffer)
3006           (article-buffer gnus-article-buffer)
3007           (original gnus-original-article-buffer)
3008           (gac gnus-article-current)
3009           (reffed gnus-reffed-article-number)
3010           (score-file gnus-current-score-file)
3011           (default-charset gnus-newsgroup-charset)
3012           vlist)
3013       (let ((locals gnus-newsgroup-variables))
3014         (while locals
3015           (if (consp (car locals))
3016               (push (eval (caar locals)) vlist)
3017             (push (eval (car locals)) vlist))
3018           (setq locals (cdr locals)))
3019         (setq vlist (nreverse vlist)))
3020       (save-excursion
3021         (set-buffer gnus-group-buffer)
3022         (setq gnus-newsgroup-name name
3023               gnus-newsgroup-marked marked
3024               gnus-newsgroup-spam-marked spam
3025               gnus-newsgroup-unreads unread
3026               gnus-current-headers headers
3027               gnus-newsgroup-data data
3028               gnus-article-current gac
3029               gnus-summary-buffer summary
3030               gnus-article-buffer article-buffer
3031               gnus-original-article-buffer original
3032               gnus-reffed-article-number reffed
3033               gnus-current-score-file score-file
3034               gnus-newsgroup-charset default-charset)
3035         (let ((locals gnus-newsgroup-variables))
3036           (while locals
3037             (if (consp (car locals))
3038                 (set (caar locals) (pop vlist))
3039               (set (car locals) (pop vlist)))
3040             (setq locals (cdr locals))))
3041         ;; The article buffer also has local variables.
3042         (when (gnus-buffer-live-p gnus-article-buffer)
3043           (set-buffer gnus-article-buffer)
3044           (setq gnus-summary-buffer summary))))))
3045
3046 (defun gnus-summary-article-unread-p (article)
3047   "Say whether ARTICLE is unread or not."
3048   (memq article gnus-newsgroup-unreads))
3049
3050 (defun gnus-summary-first-article-p (&optional article)
3051   "Return whether ARTICLE is the first article in the buffer."
3052   (if (not (setq article (or article (gnus-summary-article-number))))
3053       nil
3054     (eq article (caar gnus-newsgroup-data))))
3055
3056 (defun gnus-summary-last-article-p (&optional article)
3057   "Return whether ARTICLE is the last article in the buffer."
3058   (if (not (setq article (or article (gnus-summary-article-number))))
3059       ;; All non-existent numbers are the last article.  :-)
3060       t
3061     (not (cdr (gnus-data-find-list article)))))
3062
3063 (defun gnus-make-thread-indent-array ()
3064   (let ((n 200))
3065     (unless (and gnus-thread-indent-array
3066                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3067       (setq gnus-thread-indent-array (make-vector 201 "")
3068             gnus-thread-indent-array-level gnus-thread-indent-level)
3069       (while (>= n 0)
3070         (aset gnus-thread-indent-array n
3071               (make-string (* n gnus-thread-indent-level) ? ))
3072         (setq n (1- n))))))
3073
3074 (defun gnus-update-summary-mark-positions ()
3075   "Compute where the summary marks are to go."
3076   (save-excursion
3077     (when (gnus-buffer-exists-p gnus-summary-buffer)
3078       (set-buffer gnus-summary-buffer))
3079     (let ((gnus-replied-mark 129)
3080           (gnus-score-below-mark 130)
3081           (gnus-score-over-mark 130)
3082           (gnus-download-mark 131)
3083           (spec gnus-summary-line-format-spec)
3084           gnus-visual pos)
3085       (save-excursion
3086         (gnus-set-work-buffer)
3087         (let ((gnus-summary-line-format-spec spec)
3088               (gnus-newsgroup-downloadable '((0 . t))))
3089           (gnus-summary-insert-line
3090            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3091            0 nil 128 t nil "" nil 1)
3092           (goto-char (point-min))
3093           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3094                                              (- (point) (point-min) 1)))))
3095           (goto-char (point-min))
3096           (push (cons 'replied (and (search-forward "\201" nil t)
3097                                     (- (point) (point-min) 1)))
3098                 pos)
3099           (goto-char (point-min))
3100           (push (cons 'score (and (search-forward "\202" nil t)
3101                                   (- (point) (point-min) 1)))
3102                 pos)
3103           (goto-char (point-min))
3104           (push (cons 'download
3105                       (and (search-forward "\203" nil t)
3106                            (- (point) (point-min) 1)))
3107                 pos)))
3108       (setq gnus-summary-mark-positions pos))))
3109
3110 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3111   "Insert a dummy root in the summary buffer."
3112   (beginning-of-line)
3113   (gnus-add-text-properties
3114    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3115    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3116
3117 (defun gnus-summary-extract-address-component (from)
3118   (or (car (funcall gnus-extract-address-components from))
3119       from))
3120
3121 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3122   (let ((mail-parse-charset gnus-newsgroup-charset)
3123         ; Is it really necessary to do this next part for each summary line?
3124         ; Luckily, doesn't seem to slow things down much.
3125         (mail-parse-ignored-charsets
3126          (save-excursion (set-buffer gnus-summary-buffer)
3127                          gnus-newsgroup-ignored-charsets)))
3128     (or
3129      (and gnus-ignored-from-addresses
3130           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3131           (let ((extra-headers (mail-header-extra header))
3132                 to
3133                 newsgroups)
3134             (cond
3135              ((setq to (cdr (assq 'To extra-headers)))
3136               (concat "-> "
3137                       (inline
3138                         (gnus-summary-extract-address-component
3139                          (funcall gnus-decode-encoded-word-function to)))))
3140              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3141               (concat "=> " newsgroups)))))
3142      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3143
3144 (defun gnus-summary-insert-line (gnus-tmp-header
3145                                  gnus-tmp-level gnus-tmp-current
3146                                  gnus-tmp-unread gnus-tmp-replied
3147                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3148                                  &optional gnus-tmp-dummy gnus-tmp-score
3149                                  gnus-tmp-process)
3150   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3151          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3152          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3153          (gnus-tmp-score-char
3154           (if (or (null gnus-summary-default-score)
3155                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3156                       gnus-summary-zcore-fuzz))
3157               ?                         ;Whitespace
3158             (if (< gnus-tmp-score gnus-summary-default-score)
3159                 gnus-score-below-mark gnus-score-over-mark)))
3160          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3161          (gnus-tmp-replied
3162           (cond (gnus-tmp-process gnus-process-mark)
3163                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3164                  gnus-cached-mark)
3165                 (gnus-tmp-replied gnus-replied-mark)
3166                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3167                  gnus-forwarded-mark)
3168                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3169                  gnus-saved-mark)
3170                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3171                  gnus-recent-mark)
3172                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3173                  gnus-unseen-mark)
3174                 (t gnus-no-mark)))
3175          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3176          (gnus-tmp-name
3177           (cond
3178            ((string-match "<[^>]+> *$" gnus-tmp-from)
3179             (let ((beg (match-beginning 0)))
3180               (or (and (string-match "^\".+\"" gnus-tmp-from)
3181                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3182                   (substring gnus-tmp-from 0 beg))))
3183            ((string-match "(.+)" gnus-tmp-from)
3184             (substring gnus-tmp-from
3185                        (1+ (match-beginning 0)) (1- (match-end 0))))
3186            (t gnus-tmp-from)))
3187          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3188          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3189          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3190          (buffer-read-only nil))
3191     (when (string= gnus-tmp-name "")
3192       (setq gnus-tmp-name gnus-tmp-from))
3193     (unless (numberp gnus-tmp-lines)
3194       (setq gnus-tmp-lines -1))
3195     (if (= gnus-tmp-lines -1)
3196         (setq gnus-tmp-lines "?")
3197       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3198     (gnus-put-text-property
3199      (point)
3200      (progn (eval gnus-summary-line-format-spec) (point))
3201      'gnus-number gnus-tmp-number)
3202     (when (gnus-visual-p 'summary-highlight 'highlight)
3203       (forward-line -1)
3204       (gnus-run-hooks 'gnus-summary-update-hook)
3205       (forward-line 1))))
3206
3207 (defun gnus-summary-update-line (&optional dont-update)
3208   "Update summary line after change."
3209   (when (and gnus-summary-default-score
3210              (not gnus-summary-inhibit-highlight))
3211     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3212            (article (gnus-summary-article-number))
3213            (score (gnus-summary-article-score article)))
3214       (unless dont-update
3215         (if (and gnus-summary-mark-below
3216                  (< (gnus-summary-article-score)
3217                     gnus-summary-mark-below))
3218             ;; This article has a low score, so we mark it as read.
3219             (when (memq article gnus-newsgroup-unreads)
3220               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3221           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3222             ;; This article was previously marked as read on account
3223             ;; of a low score, but now it has risen, so we mark it as
3224             ;; unread.
3225             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3226         (gnus-summary-update-mark
3227          (if (or (null gnus-summary-default-score)
3228                  (<= (abs (- score gnus-summary-default-score))
3229                      gnus-summary-zcore-fuzz))
3230              ?                          ;Whitespace
3231            (if (< score gnus-summary-default-score)
3232                gnus-score-below-mark gnus-score-over-mark))
3233          'score))
3234       ;; Do visual highlighting.
3235       (when (gnus-visual-p 'summary-highlight 'highlight)
3236         (gnus-run-hooks 'gnus-summary-update-hook)))))
3237
3238 (defvar gnus-tmp-new-adopts nil)
3239
3240 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3241   "Return the number of articles in THREAD.
3242 This may be 0 in some cases -- if none of the articles in
3243 the thread are to be displayed."
3244   (let* ((number
3245          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3246           (cond
3247            ((not (listp thread))
3248             1)
3249            ((and (consp thread) (cdr thread))
3250             (apply
3251              '+ 1 (mapcar
3252                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3253            ((null thread)
3254             1)
3255            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3256             1)
3257            (t 0))))
3258     (when (and level (zerop level) gnus-tmp-new-adopts)
3259       (incf number
3260             (apply '+ (mapcar
3261                        'gnus-summary-number-of-articles-in-thread
3262                        gnus-tmp-new-adopts))))
3263     (if char
3264         (if (> number 1) gnus-not-empty-thread-mark
3265           gnus-empty-thread-mark)
3266       number)))
3267
3268 (defsubst gnus-summary-line-message-size (head)
3269   "Return pretty-printed version of message size.
3270 This function is intended to be used in
3271 `gnus-summary-line-format-alist', which see."
3272   (let ((c (or (mail-header-chars head) -1)))
3273     (cond ((< c 0) "n/a")               ; chars not available
3274           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3275           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3276           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3277           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3278
3279
3280 (defun gnus-summary-set-local-parameters (group)
3281   "Go through the local params of GROUP and set all variable specs in that list."
3282   (let ((params (gnus-group-find-parameter group))
3283         (vars '(quit-config))           ; Ignore quit-config.
3284         elem)
3285     (while params
3286       (setq elem (car params)
3287             params (cdr params))
3288       (and (consp elem)                 ; Has to be a cons.
3289            (consp (cdr elem))           ; The cdr has to be a list.
3290            (symbolp (car elem))         ; Has to be a symbol in there.
3291            (not (memq (car elem) vars))
3292            (ignore-errors               ; So we set it.
3293              (push (car elem) vars)
3294              (make-local-variable (car elem))
3295              (set (car elem) (eval (nth 1 elem))))))))
3296
3297 (defun gnus-summary-read-group (group &optional show-all no-article
3298                                       kill-buffer no-display backward
3299                                       select-articles)
3300   "Start reading news in newsgroup GROUP.
3301 If SHOW-ALL is non-nil, already read articles are also listed.
3302 If NO-ARTICLE is non-nil, no article is selected initially.
3303 If NO-DISPLAY, don't generate a summary buffer."
3304   (let (result)
3305     (while (and group
3306                 (null (setq result
3307                             (let ((gnus-auto-select-next nil))
3308                               (or (gnus-summary-read-group-1
3309                                    group show-all no-article
3310                                    kill-buffer no-display
3311                                    select-articles)
3312                                   (setq show-all nil
3313                                         select-articles nil)))))
3314                 (eq gnus-auto-select-next 'quietly))
3315       (set-buffer gnus-group-buffer)
3316       ;; The entry function called above goes to the next
3317       ;; group automatically, so we go two groups back
3318       ;; if we are searching for the previous group.
3319       (when backward
3320         (gnus-group-prev-unread-group 2))
3321       (if (not (equal group (gnus-group-group-name)))
3322           (setq group (gnus-group-group-name))
3323         (setq group nil)))
3324     result))
3325
3326 (defun gnus-summary-read-group-1 (group show-all no-article
3327                                         kill-buffer no-display
3328                                         &optional select-articles)
3329   ;; Killed foreign groups can't be entered.
3330   ;;  (when (and (not (gnus-group-native-p group))
3331   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3332   ;;    (error "Dead non-native groups can't be entered"))
3333   (gnus-message 5 "Retrieving newsgroup: %s..."
3334                 (gnus-group-decoded-name group))
3335   (let* ((new-group (gnus-summary-setup-buffer group))
3336          (quit-config (gnus-group-quit-config group))
3337          (did-select (and new-group (gnus-select-newsgroup
3338                                      group show-all select-articles))))
3339     (cond
3340      ;; This summary buffer exists already, so we just select it.
3341      ((not new-group)
3342       (gnus-set-global-variables)
3343       (when kill-buffer
3344         (gnus-kill-or-deaden-summary kill-buffer))
3345       (gnus-configure-windows 'summary 'force)
3346       (gnus-set-mode-line 'summary)
3347       (gnus-summary-position-point)
3348       (message "")
3349       t)
3350      ;; We couldn't select this group.
3351      ((null did-select)
3352       (when (and (eq major-mode 'gnus-summary-mode)
3353                  (not (equal (current-buffer) kill-buffer)))
3354         (kill-buffer (current-buffer))
3355         (if (not quit-config)
3356             (progn
3357               ;; Update the info -- marks might need to be removed,
3358               ;; for instance.
3359               (gnus-summary-update-info)
3360               (set-buffer gnus-group-buffer)
3361               (gnus-group-jump-to-group group)
3362               (gnus-group-next-unread-group 1))
3363           (gnus-handle-ephemeral-exit quit-config)))
3364       (let ((grpinfo (gnus-get-info group)))
3365         (if (null (gnus-info-read grpinfo))
3366             (gnus-message 3 "Group %s contains no messages"
3367                           (gnus-group-decoded-name group))
3368           (gnus-message 3 "Can't select group")))
3369       nil)
3370      ;; The user did a `C-g' while prompting for number of articles,
3371      ;; so we exit this group.
3372      ((eq did-select 'quit)
3373       (and (eq major-mode 'gnus-summary-mode)
3374            (not (equal (current-buffer) kill-buffer))
3375            (kill-buffer (current-buffer)))
3376       (when kill-buffer
3377         (gnus-kill-or-deaden-summary kill-buffer))
3378       (if (not quit-config)
3379           (progn
3380             (set-buffer gnus-group-buffer)
3381             (gnus-group-jump-to-group group)
3382             (gnus-group-next-unread-group 1)
3383             (gnus-configure-windows 'group 'force))
3384         (gnus-handle-ephemeral-exit quit-config))
3385       ;; Finally signal the quit.
3386       (signal 'quit nil))
3387      ;; The group was successfully selected.
3388      (t
3389       (gnus-set-global-variables)
3390       ;; Save the active value in effect when the group was entered.
3391       (setq gnus-newsgroup-active
3392             (gnus-copy-sequence
3393              (gnus-active gnus-newsgroup-name)))
3394       ;; You can change the summary buffer in some way with this hook.
3395       (gnus-run-hooks 'gnus-select-group-hook)
3396       (gnus-update-format-specifications
3397        nil 'summary 'summary-mode 'summary-dummy)
3398       (gnus-update-summary-mark-positions)
3399       ;; Do score processing.
3400       (when gnus-use-scoring
3401         (gnus-possibly-score-headers))
3402       ;; Check whether to fill in the gaps in the threads.
3403       (when gnus-build-sparse-threads
3404         (gnus-build-sparse-threads))
3405       ;; Find the initial limit.
3406       (if gnus-show-threads
3407           (if show-all
3408               (let ((gnus-newsgroup-dormant nil))
3409                 (gnus-summary-initial-limit show-all))
3410             (gnus-summary-initial-limit show-all))
3411         ;; When untreaded, all articles are always shown.
3412         (setq gnus-newsgroup-limit
3413               (mapcar
3414                (lambda (header) (mail-header-number header))
3415                gnus-newsgroup-headers)))
3416       ;; Generate the summary buffer.
3417       (unless no-display
3418         (gnus-summary-prepare))
3419       (when gnus-use-trees
3420         (gnus-tree-open group)
3421         (setq gnus-summary-highlight-line-function
3422               'gnus-tree-highlight-article))
3423       ;; If the summary buffer is empty, but there are some low-scored
3424       ;; articles or some excluded dormants, we include these in the
3425       ;; buffer.
3426       (when (and (zerop (buffer-size))
3427                  (not no-display))
3428         (cond (gnus-newsgroup-dormant
3429                (gnus-summary-limit-include-dormant))
3430               ((and gnus-newsgroup-scored show-all)
3431                (gnus-summary-limit-include-expunged t))))
3432       ;; Function `gnus-apply-kill-file' must be called in this hook.
3433       (gnus-run-hooks 'gnus-apply-kill-hook)
3434       (if (and (zerop (buffer-size))
3435                (not no-display))
3436           (progn
3437             ;; This newsgroup is empty.
3438             (gnus-summary-catchup-and-exit nil t)
3439             (gnus-message 6 "No unread news")
3440             (when kill-buffer
3441               (gnus-kill-or-deaden-summary kill-buffer))
3442             ;; Return nil from this function.
3443             nil)
3444         ;; Hide conversation thread subtrees.  We cannot do this in
3445         ;; gnus-summary-prepare-hook since kill processing may not
3446         ;; work with hidden articles.
3447         (gnus-summary-maybe-hide-threads)
3448         (when kill-buffer
3449           (gnus-kill-or-deaden-summary kill-buffer))
3450         (gnus-summary-auto-select-subject)
3451         ;; Show first unread article if requested.
3452         (if (and (not no-article)
3453                  (not no-display)
3454                  gnus-newsgroup-unreads
3455                  gnus-auto-select-first)
3456             (progn
3457               (gnus-configure-windows 'summary)
3458               (let ((art (gnus-summary-article-number)))
3459                 (unless (or (memq art gnus-newsgroup-undownloaded)
3460                             (memq art gnus-newsgroup-downloadable))
3461                   (gnus-summary-goto-article art))))
3462           ;; Don't select any articles.
3463           (gnus-summary-position-point)
3464           (gnus-configure-windows 'summary 'force)
3465           (gnus-set-mode-line 'summary))
3466         (when (get-buffer-window gnus-group-buffer t)
3467           ;; Gotta use windows, because recenter does weird stuff if
3468           ;; the current buffer ain't the displayed window.
3469           (let ((owin (selected-window)))
3470             (select-window (get-buffer-window gnus-group-buffer t))
3471             (when (gnus-group-goto-group group)
3472               (recenter))
3473             (select-window owin)))
3474         ;; Mark this buffer as "prepared".
3475         (setq gnus-newsgroup-prepared t)
3476         (gnus-run-hooks 'gnus-summary-prepared-hook)
3477         t)))))
3478
3479 (defun gnus-summary-auto-select-subject ()
3480   "Select the subject line on initial group entry."
3481   (goto-char (point-min))
3482   (cond
3483    ((eq gnus-auto-select-subject 'best)
3484     (gnus-summary-best-unread-subject))
3485    ((eq gnus-auto-select-subject 'unread)
3486     (gnus-summary-first-unread-subject))
3487    ((eq gnus-auto-select-subject 'unseen)
3488     (gnus-summary-first-unseen-subject))
3489    ((eq gnus-auto-select-subject 'unseen-or-unread)
3490     (gnus-summary-first-unseen-or-unread-subject))
3491    ((eq gnus-auto-select-subject 'first)
3492     ;; Do nothing.
3493     )
3494    ((gnus-functionp gnus-auto-select-subject)
3495     (funcall gnus-auto-select-subject))))
3496
3497 (defun gnus-summary-prepare ()
3498   "Generate the summary buffer."
3499   (interactive)
3500   (let ((buffer-read-only nil))
3501     (erase-buffer)
3502     (setq gnus-newsgroup-data nil
3503           gnus-newsgroup-data-reverse nil)
3504     (gnus-run-hooks 'gnus-summary-generate-hook)
3505     ;; Generate the buffer, either with threads or without.
3506     (when gnus-newsgroup-headers
3507       (gnus-summary-prepare-threads
3508        (if gnus-show-threads
3509            (gnus-sort-gathered-threads
3510             (funcall gnus-summary-thread-gathering-function
3511                      (gnus-sort-threads
3512                       (gnus-cut-threads (gnus-make-threads)))))
3513          ;; Unthreaded display.
3514          (gnus-sort-articles gnus-newsgroup-headers))))
3515     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3516     ;; Call hooks for modifying summary buffer.
3517     (goto-char (point-min))
3518     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3519
3520 (defsubst gnus-general-simplify-subject (subject)
3521   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3522   (setq subject
3523         (cond
3524          ;; Truncate the subject.
3525          (gnus-simplify-subject-functions
3526           (gnus-map-function gnus-simplify-subject-functions subject))
3527          ((numberp gnus-summary-gather-subject-limit)
3528           (setq subject (gnus-simplify-subject-re subject))
3529           (if (> (length subject) gnus-summary-gather-subject-limit)
3530               (substring subject 0 gnus-summary-gather-subject-limit)
3531             subject))
3532          ;; Fuzzily simplify it.
3533          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3534           (gnus-simplify-subject-fuzzy subject))
3535          ;; Just remove the leading "Re:".
3536          (t
3537           (gnus-simplify-subject-re subject))))
3538
3539   (if (and gnus-summary-gather-exclude-subject
3540            (string-match gnus-summary-gather-exclude-subject subject))
3541       nil                         ; This article shouldn't be gathered
3542     subject))
3543
3544 (defun gnus-summary-simplify-subject-query ()
3545   "Query where the respool algorithm would put this article."
3546   (interactive)
3547   (gnus-summary-select-article)
3548   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3549
3550 (defun gnus-gather-threads-by-subject (threads)
3551   "Gather threads by looking at Subject headers."
3552   (if (not gnus-summary-make-false-root)
3553       threads
3554     (let ((hashtb (gnus-make-hashtable 1024))
3555           (prev threads)
3556           (result threads)
3557           subject hthread whole-subject)
3558       (while threads
3559         (setq subject (gnus-general-simplify-subject
3560                        (setq whole-subject (mail-header-subject
3561                                             (caar threads)))))
3562         (when subject
3563           (if (setq hthread (gnus-gethash subject hashtb))
3564               (progn
3565                 ;; We enter a dummy root into the thread, if we
3566                 ;; haven't done that already.
3567                 (unless (stringp (caar hthread))
3568                   (setcar hthread (list whole-subject (car hthread))))
3569                 ;; We add this new gathered thread to this gathered
3570                 ;; thread.
3571                 (setcdr (car hthread)
3572                         (nconc (cdar hthread) (list (car threads))))
3573                 ;; Remove it from the list of threads.
3574                 (setcdr prev (cdr threads))
3575                 (setq threads prev))
3576             ;; Enter this thread into the hash table.
3577             (gnus-sethash subject threads hashtb)))
3578         (setq prev threads)
3579         (setq threads (cdr threads)))
3580       result)))
3581
3582 (defun gnus-gather-threads-by-references (threads)
3583   "Gather threads by looking at References headers."
3584   (let ((idhashtb (gnus-make-hashtable 1024))
3585         (thhashtb (gnus-make-hashtable 1024))
3586         (prev threads)
3587         (result threads)
3588         ids references id gthread gid entered ref)
3589     (while threads
3590       (when (setq references (mail-header-references (caar threads)))
3591         (setq id (mail-header-id (caar threads))
3592               ids (inline (gnus-split-references references))
3593               entered nil)
3594         (while (setq ref (pop ids))
3595           (setq ids (delete ref ids))
3596           (if (not (setq gid (gnus-gethash ref idhashtb)))
3597               (progn
3598                 (gnus-sethash ref id idhashtb)
3599                 (gnus-sethash id threads thhashtb))
3600             (setq gthread (gnus-gethash gid thhashtb))
3601             (unless entered
3602               ;; We enter a dummy root into the thread, if we
3603               ;; haven't done that already.
3604               (unless (stringp (caar gthread))
3605                 (setcar gthread (list (mail-header-subject (caar gthread))
3606                                       (car gthread))))
3607               ;; We add this new gathered thread to this gathered
3608               ;; thread.
3609               (setcdr (car gthread)
3610                       (nconc (cdar gthread) (list (car threads)))))
3611             ;; Add it into the thread hash table.
3612             (gnus-sethash id gthread thhashtb)
3613             (setq entered t)
3614             ;; Remove it from the list of threads.
3615             (setcdr prev (cdr threads))
3616             (setq threads prev))))
3617       (setq prev threads)
3618       (setq threads (cdr threads)))
3619     result))
3620
3621 (defun gnus-sort-gathered-threads (threads)
3622   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3623   (let ((result threads))
3624     (while threads
3625       (when (stringp (caar threads))
3626         (setcdr (car threads)
3627                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3628       (setq threads (cdr threads)))
3629     result))
3630
3631 (defun gnus-thread-loop-p (root thread)
3632   "Say whether ROOT is in THREAD."
3633   (let ((stack (list thread))
3634         (infloop 0)
3635         th)
3636     (while (setq thread (pop stack))
3637       (setq th (cdr thread))
3638       (while (and th
3639                   (not (eq (caar th) root)))
3640         (pop th))
3641       (if th
3642           ;; We have found a loop.
3643           (let (ref-dep)
3644             (setcdr thread (delq (car th) (cdr thread)))
3645             (if (boundp (setq ref-dep (intern "none"
3646                                               gnus-newsgroup-dependencies)))
3647                 (setcdr (symbol-value ref-dep)
3648                         (nconc (cdr (symbol-value ref-dep))
3649                                (list (car th))))
3650               (set ref-dep (list nil (car th))))
3651             (setq infloop 1
3652                   stack nil))
3653         ;; Push all the subthreads onto the stack.
3654         (push (cdr thread) stack)))
3655     infloop))
3656
3657 (defun gnus-make-threads ()
3658   "Go through the dependency hashtb and find the roots.  Return all threads."
3659   (let (threads)
3660     (while (catch 'infloop
3661              (mapatoms
3662               (lambda (refs)
3663                 ;; Deal with self-referencing References loops.
3664                 (when (and (car (symbol-value refs))
3665                            (not (zerop
3666                                  (apply
3667                                   '+
3668                                   (mapcar
3669                                    (lambda (thread)
3670                                      (gnus-thread-loop-p
3671                                       (car (symbol-value refs)) thread))
3672                                    (cdr (symbol-value refs)))))))
3673                   (setq threads nil)
3674                   (throw 'infloop t))
3675                 (unless (car (symbol-value refs))
3676                   ;; These threads do not refer back to any other
3677                   ;; articles, so they're roots.
3678                   (setq threads (append (cdr (symbol-value refs)) threads))))
3679               gnus-newsgroup-dependencies)))
3680     threads))
3681
3682 ;; Build the thread tree.
3683 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3684   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3685
3686 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3687 if it was already present.
3688
3689 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3690 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3691 Message-IDs will be renamed to a unique Message-ID before being
3692 entered.
3693
3694 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3695   (let* ((id (mail-header-id header))
3696          (id-dep (and id (intern id dependencies)))
3697          parent-id ref ref-dep ref-header replaced)
3698     ;; Enter this `header' in the `dependencies' table.
3699     (cond
3700      ((not id-dep)
3701       (setq header nil))
3702      ;; The first two cases do the normal part: enter a new `header'
3703      ;; in the `dependencies' table.
3704      ((not (boundp id-dep))
3705       (set id-dep (list header)))
3706      ((null (car (symbol-value id-dep)))
3707       (setcar (symbol-value id-dep) header))
3708
3709      ;; From here the `header' was already present in the
3710      ;; `dependencies' table.
3711      (force-new
3712       ;; Overrides an existing entry;
3713       ;; just set the header part of the entry.
3714       (setcar (symbol-value id-dep) header)
3715       (setq replaced t))
3716
3717      ;; Renames the existing `header' to a unique Message-ID.
3718      ((not gnus-summary-ignore-duplicates)
3719       ;; An article with this Message-ID has already been seen.
3720       ;; We rename the Message-ID.
3721       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3722            (list header))
3723       (mail-header-set-id header id))
3724
3725      ;; The last case ignores an existing entry, except it adds any
3726      ;; additional Xrefs (in case the two articles came from different
3727      ;; servers.
3728      ;; Also sets `header' to `nil' meaning that the `dependencies'
3729      ;; table was *not* modified.
3730      (t
3731       (mail-header-set-xref
3732        (car (symbol-value id-dep))
3733        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3734                    "")
3735                (or (mail-header-xref header) "")))
3736       (setq header nil)))
3737
3738     (when (and header (not replaced))
3739       ;; First check that we are not creating a References loop.
3740       (setq parent-id (gnus-parent-id (mail-header-references header)))
3741       (setq ref parent-id)
3742       (while (and ref
3743                   (setq ref-dep (intern-soft ref dependencies))
3744                   (boundp ref-dep)
3745                   (setq ref-header (car (symbol-value ref-dep))))
3746         (if (string= id ref)
3747             ;; Yuk!  This is a reference loop.  Make the article be a
3748             ;; root article.
3749             (progn
3750               (mail-header-set-references (car (symbol-value id-dep)) "none")
3751               (setq ref nil)
3752               (setq parent-id nil))
3753           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3754       (setq ref-dep (intern (or parent-id "none") dependencies))
3755       (if (boundp ref-dep)
3756           (setcdr (symbol-value ref-dep)
3757                   (nconc (cdr (symbol-value ref-dep))
3758                          (list (symbol-value id-dep))))
3759         (set ref-dep (list nil (symbol-value id-dep)))))
3760     header))
3761
3762 (defun gnus-extract-message-id-from-in-reply-to (string)
3763   (if (string-match "<[^>]+>" string)
3764       (substring string (match-beginning 0) (match-end 0))
3765     nil))
3766
3767 (defun gnus-build-sparse-threads ()
3768   (let ((headers gnus-newsgroup-headers)
3769         (mail-parse-charset gnus-newsgroup-charset)
3770         (gnus-summary-ignore-duplicates t)
3771         header references generation relations
3772         subject child end new-child date)
3773     ;; First we create an alist of generations/relations, where
3774     ;; generations is how much we trust the relation, and the relation
3775     ;; is parent/child.
3776     (gnus-message 7 "Making sparse threads...")
3777     (save-excursion
3778       (nnheader-set-temp-buffer " *gnus sparse threads*")
3779       (while (setq header (pop headers))
3780         (when (and (setq references (mail-header-references header))
3781                    (not (string= references "")))
3782           (insert references)
3783           (setq child (mail-header-id header)
3784                 subject (mail-header-subject header)
3785                 date (mail-header-date header)
3786                 generation 0)
3787           (while (search-backward ">" nil t)
3788             (setq end (1+ (point)))
3789             (when (search-backward "<" nil t)
3790               (setq new-child (buffer-substring (point) end))
3791               (push (list (incf generation)
3792                           child (setq child new-child)
3793                           subject date)
3794                     relations)))
3795           (when child
3796             (push (list (1+ generation) child nil subject) relations))
3797           (erase-buffer)))
3798       (kill-buffer (current-buffer)))
3799     ;; Sort over trustworthiness.
3800     (mapcar
3801      (lambda (relation)
3802        (when (gnus-dependencies-add-header
3803               (make-full-mail-header
3804                gnus-reffed-article-number
3805                (nth 3 relation) "" (or (nth 4 relation) "")
3806                (nth 1 relation)
3807                (or (nth 2 relation) "") 0 0 "")
3808               gnus-newsgroup-dependencies nil)
3809          (push gnus-reffed-article-number gnus-newsgroup-limit)
3810          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3811          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3812                gnus-newsgroup-reads)
3813          (decf gnus-reffed-article-number)))
3814      (sort relations 'car-less-than-car))
3815     (gnus-message 7 "Making sparse threads...done")))
3816
3817 (defun gnus-build-old-threads ()
3818   ;; Look at all the articles that refer back to old articles, and
3819   ;; fetch the headers for the articles that aren't there.  This will
3820   ;; build complete threads - if the roots haven't been expired by the
3821   ;; server, that is.
3822   (let ((mail-parse-charset gnus-newsgroup-charset)
3823         id heads)
3824     (mapatoms
3825      (lambda (refs)
3826        (when (not (car (symbol-value refs)))
3827          (setq heads (cdr (symbol-value refs)))
3828          (while heads
3829            (if (memq (mail-header-number (caar heads))
3830                      gnus-newsgroup-dormant)
3831                (setq heads (cdr heads))
3832              (setq id (symbol-name refs))
3833              (while (and (setq id (gnus-build-get-header id))
3834                          (not (car (gnus-id-to-thread id)))))
3835              (setq heads nil)))))
3836      gnus-newsgroup-dependencies)))
3837
3838 ;; This function has to be called with point after the article number
3839 ;; on the beginning of the line.
3840 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3841   (let ((eol (gnus-point-at-eol))
3842         (buffer (current-buffer))
3843         header references in-reply-to)
3844
3845     ;; overview: [num subject from date id refs chars lines misc]
3846     (unwind-protect
3847         (let (x)
3848           (narrow-to-region (point) eol)
3849           (unless (eobp)
3850             (forward-char))
3851
3852           (setq header
3853                 (make-full-mail-header
3854                  number                 ; number
3855                  (condition-case ()     ; subject
3856                      (funcall gnus-decode-encoded-word-function
3857                               (setq x (nnheader-nov-field)))
3858                    (error x))
3859                  (condition-case ()     ; from
3860                      (funcall gnus-decode-encoded-word-function
3861                               (setq x (nnheader-nov-field)))
3862                    (error x))
3863                  (nnheader-nov-field)   ; date
3864                  (nnheader-nov-read-message-id) ; id
3865                  (setq references (nnheader-nov-field)) ; refs
3866                  (nnheader-nov-read-integer) ; chars
3867                  (nnheader-nov-read-integer) ; lines
3868                  (unless (eobp)
3869                    (if (looking-at "Xref: ")
3870                        (goto-char (match-end 0)))
3871                    (nnheader-nov-field)) ; Xref
3872                  (nnheader-nov-parse-extra)))) ; extra
3873
3874       (widen))
3875
3876     (when (and (string= references "")
3877                (setq in-reply-to (mail-header-extra header))
3878                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3879       (mail-header-set-references
3880        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3881
3882     (when gnus-alter-header-function
3883       (funcall gnus-alter-header-function header))
3884     (gnus-dependencies-add-header header dependencies force-new)))
3885
3886 (defun gnus-build-get-header (id)
3887   "Look through the buffer of NOV lines and find the header to ID.
3888 Enter this line into the dependencies hash table, and return
3889 the id of the parent article (if any)."
3890   (let ((deps gnus-newsgroup-dependencies)
3891         found header)
3892     (prog1
3893         (save-excursion
3894           (set-buffer nntp-server-buffer)
3895           (let ((case-fold-search nil))
3896             (goto-char (point-min))
3897             (while (and (not found)
3898                         (search-forward id nil t))
3899               (beginning-of-line)
3900               (setq found (looking-at
3901                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3902                                    (regexp-quote id))))
3903               (or found (beginning-of-line 2)))
3904             (when found
3905               (beginning-of-line)
3906               (and
3907                (setq header (gnus-nov-parse-line
3908                              (read (current-buffer)) deps))
3909                (gnus-parent-id (mail-header-references header))))))
3910       (when header
3911         (let ((number (mail-header-number header)))
3912           (push number gnus-newsgroup-limit)
3913           (push header gnus-newsgroup-headers)
3914           (if (memq number gnus-newsgroup-unselected)
3915               (progn
3916                 (setq gnus-newsgroup-unreads
3917                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3918                                                number))
3919                 (setq gnus-newsgroup-unselected
3920                       (delq number gnus-newsgroup-unselected)))
3921             (push number gnus-newsgroup-ancient)))))))
3922
3923 (defun gnus-build-all-threads ()
3924   "Read all the headers."
3925   (let ((gnus-summary-ignore-duplicates t)
3926         (mail-parse-charset gnus-newsgroup-charset)
3927         (dependencies gnus-newsgroup-dependencies)
3928         header article)
3929     (save-excursion
3930       (set-buffer nntp-server-buffer)
3931       (let ((case-fold-search nil))
3932         (goto-char (point-min))
3933         (while (not (eobp))
3934           (ignore-errors
3935             (setq article (read (current-buffer))
3936                   header (gnus-nov-parse-line article dependencies)))
3937           (when header
3938             (save-excursion
3939               (set-buffer gnus-summary-buffer)
3940               (push header gnus-newsgroup-headers)
3941               (if (memq (setq article (mail-header-number header))
3942                         gnus-newsgroup-unselected)
3943                   (progn
3944                     (setq gnus-newsgroup-unreads
3945                           (gnus-add-to-sorted-list
3946                            gnus-newsgroup-unreads article))
3947                     (setq gnus-newsgroup-unselected
3948                           (delq article gnus-newsgroup-unselected)))
3949                 (push article gnus-newsgroup-ancient)))
3950             (forward-line 1)))))))
3951
3952 (defun gnus-summary-update-article-line (article header)
3953   "Update the line for ARTICLE using HEADERS."
3954   (let* ((id (mail-header-id header))
3955          (thread (gnus-id-to-thread id)))
3956     (unless thread
3957       (error "Article in no thread"))
3958     ;; Update the thread.
3959     (setcar thread header)
3960     (gnus-summary-goto-subject article)
3961     (let* ((datal (gnus-data-find-list article))
3962            (data (car datal))
3963            (length (when (cdr datal)
3964                      (- (gnus-data-pos data)
3965                         (gnus-data-pos (cadr datal)))))
3966            (buffer-read-only nil)
3967            (level (gnus-summary-thread-level)))
3968       (gnus-delete-line)
3969       (gnus-summary-insert-line
3970        header level nil (gnus-article-mark article)
3971        (memq article gnus-newsgroup-replied)
3972        (memq article gnus-newsgroup-expirable)
3973        ;; Only insert the Subject string when it's different
3974        ;; from the previous Subject string.
3975        (if (and
3976             gnus-show-threads
3977             (gnus-subject-equal
3978              (condition-case ()
3979                  (mail-header-subject
3980                   (gnus-data-header
3981                    (cadr
3982                     (gnus-data-find-list
3983                      article
3984                      (gnus-data-list t)))))
3985                ;; Error on the side of excessive subjects.
3986                (error ""))
3987              (mail-header-subject header)))
3988            ""
3989          (mail-header-subject header))
3990        nil (cdr (assq article gnus-newsgroup-scored))
3991        (memq article gnus-newsgroup-processable))
3992       (when length
3993         (gnus-data-update-list
3994          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3995
3996 (defun gnus-summary-update-article (article &optional iheader)
3997   "Update ARTICLE in the summary buffer."
3998   (set-buffer gnus-summary-buffer)
3999   (let* ((header (gnus-summary-article-header article))
4000          (id (mail-header-id header))
4001          (data (gnus-data-find article))
4002          (thread (gnus-id-to-thread id))
4003          (references (mail-header-references header))
4004          (parent
4005           (gnus-id-to-thread
4006            (or (gnus-parent-id
4007                 (when (and references
4008                            (not (equal "" references)))
4009                   references))
4010                "none")))
4011          (buffer-read-only nil)
4012          (old (car thread)))
4013     (when thread
4014       (unless iheader
4015         (setcar thread nil)
4016         (when parent
4017           (delq thread parent)))
4018       (if (gnus-summary-insert-subject id header)
4019        ;; Set the (possibly) new article number in the data structure.
4020           (gnus-data-set-number data (gnus-id-to-article id))
4021         (setcar thread old)
4022         nil))))
4023
4024 (defun gnus-rebuild-thread (id &optional line)
4025   "Rebuild the thread containing ID.
4026 If LINE, insert the rebuilt thread starting on line LINE."
4027   (let ((buffer-read-only nil)
4028         old-pos current thread data)
4029     (if (not gnus-show-threads)
4030         (setq thread (list (car (gnus-id-to-thread id))))
4031       ;; Get the thread this article is part of.
4032       (setq thread (gnus-remove-thread id)))
4033     (setq old-pos (gnus-point-at-bol))
4034     (setq current (save-excursion
4035                     (and (re-search-backward "[\r\n]" nil t)
4036                          (gnus-summary-article-number))))
4037     ;; If this is a gathered thread, we have to go some re-gathering.
4038     (when (stringp (car thread))
4039       (let ((subject (car thread))
4040             roots thr)
4041         (setq thread (cdr thread))
4042         (while thread
4043           (unless (memq (setq thr (gnus-id-to-thread
4044                                    (gnus-root-id
4045                                     (mail-header-id (caar thread)))))
4046                         roots)
4047             (push thr roots))
4048           (setq thread (cdr thread)))
4049         ;; We now have all (unique) roots.
4050         (if (= (length roots) 1)
4051             ;; All the loose roots are now one solid root.
4052             (setq thread (car roots))
4053           (setq thread (cons subject (gnus-sort-threads roots))))))
4054     (let (threads)
4055       ;; We then insert this thread into the summary buffer.
4056       (when line
4057         (goto-char (point-min))
4058         (forward-line (1- line)))
4059       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4060         (if gnus-show-threads
4061             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4062           (gnus-summary-prepare-unthreaded thread))
4063         (setq data (nreverse gnus-newsgroup-data))
4064         (setq threads gnus-newsgroup-threads))
4065       ;; We splice the new data into the data structure.
4066       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4067       ;;!!! then we want to insert at the beginning of the buffer.
4068       ;;!!! That happens to be true with Gnus now, but that may
4069       ;;!!! change in the future.  Perhaps.
4070       (gnus-data-enter-list
4071        (if line nil current) data (- (point) old-pos))
4072       (setq gnus-newsgroup-threads
4073             (nconc threads gnus-newsgroup-threads))
4074       (gnus-data-compute-positions))))
4075
4076 (defun gnus-number-to-header (number)
4077   "Return the header for article NUMBER."
4078   (let ((headers gnus-newsgroup-headers))
4079     (while (and headers
4080                 (not (= number (mail-header-number (car headers)))))
4081       (pop headers))
4082     (when headers
4083       (car headers))))
4084
4085 (defun gnus-parent-headers (in-headers &optional generation)
4086   "Return the headers of the GENERATIONeth parent of HEADERS."
4087   (unless generation
4088     (setq generation 1))
4089   (let ((parent t)
4090         (headers in-headers)
4091         references)
4092     (while (and parent
4093                 (not (zerop generation))
4094                 (setq references (mail-header-references headers)))
4095       (setq headers (if (and references
4096                              (setq parent (gnus-parent-id references)))
4097                         (car (gnus-id-to-thread parent))
4098                       nil))
4099       (decf generation))
4100     (and (not (eq headers in-headers))
4101          headers)))
4102
4103 (defun gnus-id-to-thread (id)
4104   "Return the (sub-)thread where ID appears."
4105   (gnus-gethash id gnus-newsgroup-dependencies))
4106
4107 (defun gnus-id-to-article (id)
4108   "Return the article number of ID."
4109   (let ((thread (gnus-id-to-thread id)))
4110     (when (and thread
4111                (car thread))
4112       (mail-header-number (car thread)))))
4113
4114 (defun gnus-id-to-header (id)
4115   "Return the article headers of ID."
4116   (car (gnus-id-to-thread id)))
4117
4118 (defun gnus-article-displayed-root-p (article)
4119   "Say whether ARTICLE is a root(ish) article."
4120   (let ((level (gnus-summary-thread-level article))
4121         (refs (mail-header-references  (gnus-summary-article-header article)))
4122         particle)
4123     (cond
4124      ((null level) nil)
4125      ((zerop level) t)
4126      ((null refs) t)
4127      ((null (gnus-parent-id refs)) t)
4128      ((and (= 1 level)
4129            (null (setq particle (gnus-id-to-article
4130                                  (gnus-parent-id refs))))
4131            (null (gnus-summary-thread-level particle)))))))
4132
4133 (defun gnus-root-id (id)
4134   "Return the id of the root of the thread where ID appears."
4135   (let (last-id prev)
4136     (while (and id (setq prev (car (gnus-id-to-thread id))))
4137       (setq last-id id
4138             id (gnus-parent-id (mail-header-references prev))))
4139     last-id))
4140
4141 (defun gnus-articles-in-thread (thread)
4142   "Return the list of articles in THREAD."
4143   (cons (mail-header-number (car thread))
4144         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4145
4146 (defun gnus-remove-thread (id &optional dont-remove)
4147   "Remove the thread that has ID in it."
4148   (let (headers thread last-id)
4149     ;; First go up in this thread until we find the root.
4150     (setq last-id (gnus-root-id id)
4151           headers (message-flatten-list (gnus-id-to-thread last-id)))
4152     ;; We have now found the real root of this thread.  It might have
4153     ;; been gathered into some loose thread, so we have to search
4154     ;; through the threads to find the thread we wanted.
4155     (let ((threads gnus-newsgroup-threads)
4156           sub)
4157       (while threads
4158         (setq sub (car threads))
4159         (if (stringp (car sub))
4160             ;; This is a gathered thread, so we look at the roots
4161             ;; below it to find whether this article is in this
4162             ;; gathered root.
4163             (progn
4164               (setq sub (cdr sub))
4165               (while sub
4166                 (when (member (caar sub) headers)
4167                   (setq thread (car threads)
4168                         threads nil
4169                         sub nil))
4170                 (setq sub (cdr sub))))
4171           ;; It's an ordinary thread, so we check it.
4172           (when (eq (car sub) (car headers))
4173             (setq thread sub
4174                   threads nil)))
4175         (setq threads (cdr threads)))
4176       ;; If this article is in no thread, then it's a root.
4177       (if thread
4178           (unless dont-remove
4179             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4180         (setq thread (gnus-id-to-thread last-id)))
4181       (when thread
4182         (prog1
4183             thread                      ; We return this thread.
4184           (unless dont-remove
4185             (if (stringp (car thread))
4186                 (progn
4187                   ;; If we use dummy roots, then we have to remove the
4188                   ;; dummy root as well.
4189                   (when (eq gnus-summary-make-false-root 'dummy)
4190                     ;; We go to the dummy root by going to
4191                     ;; the first sub-"thread", and then one line up.
4192                     (gnus-summary-goto-article
4193                      (mail-header-number (caadr thread)))
4194                     (forward-line -1)
4195                     (gnus-delete-line)
4196                     (gnus-data-compute-positions))
4197                   (setq thread (cdr thread))
4198                   (while thread
4199                     (gnus-remove-thread-1 (car thread))
4200                     (setq thread (cdr thread))))
4201               (gnus-remove-thread-1 thread))))))))
4202
4203 (defun gnus-remove-thread-1 (thread)
4204   "Remove the thread THREAD recursively."
4205   (let ((number (mail-header-number (pop thread)))
4206         d)
4207     (setq thread (reverse thread))
4208     (while thread
4209       (gnus-remove-thread-1 (pop thread)))
4210     (when (setq d (gnus-data-find number))
4211       (goto-char (gnus-data-pos d))
4212       (gnus-summary-show-thread)
4213       (gnus-data-remove
4214        number
4215        (- (gnus-point-at-bol)
4216           (prog1
4217               (1+ (gnus-point-at-eol))
4218             (gnus-delete-line)))))))
4219
4220 (defun gnus-sort-threads-1 (threads func)
4221   (sort (mapcar (lambda (thread)
4222                   (cons (car thread)
4223                         (and (cdr thread)
4224                              (gnus-sort-threads-1 (cdr thread) func))))
4225                 threads) func))
4226
4227 (defun gnus-sort-threads (threads)
4228   "Sort THREADS."
4229   (if (not gnus-thread-sort-functions)
4230       threads
4231     (gnus-message 8 "Sorting threads...")
4232     (prog1
4233         (gnus-sort-threads-1
4234          threads
4235          (gnus-make-sort-function gnus-thread-sort-functions))
4236       (gnus-message 8 "Sorting threads...done"))))
4237
4238 (defun gnus-sort-articles (articles)
4239   "Sort ARTICLES."
4240   (when gnus-article-sort-functions
4241     (gnus-message 7 "Sorting articles...")
4242     (prog1
4243         (setq gnus-newsgroup-headers
4244               (sort articles (gnus-make-sort-function
4245                               gnus-article-sort-functions)))
4246       (gnus-message 7 "Sorting articles...done"))))
4247
4248 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4249 (defmacro gnus-thread-header (thread)
4250   "Return header of first article in THREAD.
4251 Note that THREAD must never, ever be anything else than a variable -
4252 using some other form will lead to serious barfage."
4253   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4254   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4255   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4256         (vector thread) 2))
4257
4258 (defsubst gnus-article-sort-by-number (h1 h2)
4259   "Sort articles by article number."
4260   (< (mail-header-number h1)
4261      (mail-header-number h2)))
4262
4263 (defun gnus-thread-sort-by-number (h1 h2)
4264   "Sort threads by root article number."
4265   (gnus-article-sort-by-number
4266    (gnus-thread-header h1) (gnus-thread-header h2)))
4267
4268 (defsubst gnus-article-sort-by-random (h1 h2)
4269   "Sort articles by article number."
4270   (zerop (random 2)))
4271
4272 (defun gnus-thread-sort-by-random (h1 h2)
4273   "Sort threads by root article number."
4274   (gnus-article-sort-by-random
4275    (gnus-thread-header h1) (gnus-thread-header h2)))
4276
4277 (defsubst gnus-article-sort-by-lines (h1 h2)
4278   "Sort articles by article Lines header."
4279   (< (mail-header-lines h1)
4280      (mail-header-lines h2)))
4281
4282 (defun gnus-thread-sort-by-lines (h1 h2)
4283   "Sort threads by root article Lines header."
4284   (gnus-article-sort-by-lines
4285    (gnus-thread-header h1) (gnus-thread-header h2)))
4286
4287 (defsubst gnus-article-sort-by-chars (h1 h2)
4288   "Sort articles by octet length."
4289   (< (mail-header-chars h1)
4290      (mail-header-chars h2)))
4291
4292 (defun gnus-thread-sort-by-chars (h1 h2)
4293   "Sort threads by root article octet length."
4294   (gnus-article-sort-by-chars
4295    (gnus-thread-header h1) (gnus-thread-header h2)))
4296
4297 (defsubst gnus-article-sort-by-author (h1 h2)
4298   "Sort articles by root author."
4299   (string-lessp
4300    (let ((extract (funcall
4301                    gnus-extract-address-components
4302                    (mail-header-from h1))))
4303      (or (car extract) (cadr extract) ""))
4304    (let ((extract (funcall
4305                    gnus-extract-address-components
4306                    (mail-header-from h2))))
4307      (or (car extract) (cadr extract) ""))))
4308
4309 (defun gnus-thread-sort-by-author (h1 h2)
4310   "Sort threads by root author."
4311   (gnus-article-sort-by-author
4312    (gnus-thread-header h1)  (gnus-thread-header h2)))
4313
4314 (defsubst gnus-article-sort-by-subject (h1 h2)
4315   "Sort articles by root subject."
4316   (string-lessp
4317    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4318    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4319
4320 (defun gnus-thread-sort-by-subject (h1 h2)
4321   "Sort threads by root subject."
4322   (gnus-article-sort-by-subject
4323    (gnus-thread-header h1) (gnus-thread-header h2)))
4324
4325 (defsubst gnus-article-sort-by-date (h1 h2)
4326   "Sort articles by root article date."
4327   (time-less-p
4328    (gnus-date-get-time (mail-header-date h1))
4329    (gnus-date-get-time (mail-header-date h2))))
4330
4331 (defun gnus-thread-sort-by-date (h1 h2)
4332   "Sort threads by root article date."
4333   (gnus-article-sort-by-date
4334    (gnus-thread-header h1) (gnus-thread-header h2)))
4335
4336 (defsubst gnus-article-sort-by-score (h1 h2)
4337   "Sort articles by root article score.
4338 Unscored articles will be counted as having a score of zero."
4339   (> (or (cdr (assq (mail-header-number h1)
4340                     gnus-newsgroup-scored))
4341          gnus-summary-default-score 0)
4342      (or (cdr (assq (mail-header-number h2)
4343                     gnus-newsgroup-scored))
4344          gnus-summary-default-score 0)))
4345
4346 (defun gnus-thread-sort-by-score (h1 h2)
4347   "Sort threads by root article score."
4348   (gnus-article-sort-by-score
4349    (gnus-thread-header h1) (gnus-thread-header h2)))
4350
4351 (defun gnus-thread-sort-by-total-score (h1 h2)
4352   "Sort threads by the sum of all scores in the thread.
4353 Unscored articles will be counted as having a score of zero."
4354   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4355
4356 (defun gnus-thread-total-score (thread)
4357   ;; This function find the total score of THREAD.
4358   (cond
4359    ((null thread)
4360     0)
4361    ((consp thread)
4362     (if (stringp (car thread))
4363         (apply gnus-thread-score-function 0
4364                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4365       (gnus-thread-total-score-1 thread)))
4366    (t
4367     (gnus-thread-total-score-1 (list thread)))))
4368
4369 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4370   "Sort threads such that the thread with the most recently arrived article comes first."
4371   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4372
4373 (defun gnus-thread-highest-number (thread)
4374   "Return the highest article number in THREAD."
4375   (apply 'max (mapcar (lambda (header)
4376                         (mail-header-number header))
4377                       (message-flatten-list thread))))
4378
4379 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4380   "Sort threads such that the thread with the most recently dated article comes first."
4381   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4382
4383 (defun gnus-thread-latest-date (thread)
4384   "Return the highest article date in THREAD."
4385   (let ((previous-time 0))
4386     (apply 'max (mapcar
4387                  (lambda (header)
4388                    (setq previous-time
4389                          (time-to-seconds
4390                           (mail-header-parse-date
4391                            (condition-case ()
4392                                (mail-header-date header)
4393                              (error previous-time))))))
4394                  (sort
4395                   (message-flatten-list thread)
4396                   (lambda (h1 h2)
4397                     (< (mail-header-number h1)
4398                        (mail-header-number h2))))))))
4399
4400 (defun gnus-thread-total-score-1 (root)
4401   ;; This function find the total score of the thread below ROOT.
4402   (setq root (car root))
4403   (apply gnus-thread-score-function
4404          (or (append
4405               (mapcar 'gnus-thread-total-score
4406                       (cdr (gnus-id-to-thread (mail-header-id root))))
4407               (when (> (mail-header-number root) 0)
4408                 (list (or (cdr (assq (mail-header-number root)
4409                                      gnus-newsgroup-scored))
4410                           gnus-summary-default-score 0))))
4411              (list gnus-summary-default-score)
4412              '(0))))
4413
4414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4415 (defvar gnus-tmp-prev-subject nil)
4416 (defvar gnus-tmp-false-parent nil)
4417 (defvar gnus-tmp-root-expunged nil)
4418 (defvar gnus-tmp-dummy-line nil)
4419
4420 (eval-when-compile (defvar gnus-tmp-header))
4421 (defun gnus-extra-header (type &optional header)
4422   "Return the extra header of TYPE."
4423   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4424       ""))
4425
4426 (defvar gnus-tmp-thread-tree-header-string "")
4427
4428 (defcustom gnus-sum-thread-tree-root "> "
4429   "With %B spec, used for the root of a thread.
4430 If nil, use subject instead."
4431   :type 'string
4432   :group 'gnus-thread)
4433 (defcustom gnus-sum-thread-tree-single-indent ""
4434   "With %B spec, used for a thread with just one message.
4435 If nil, use subject instead."
4436   :type 'string
4437   :group 'gnus-thread)
4438 (defcustom gnus-sum-thread-tree-vertical "| "
4439   "With %B spec, used for drawing a vertical line."
4440   :type 'string
4441   :group 'gnus-thread)
4442 (defcustom gnus-sum-thread-tree-indent "  "
4443   "With %B spec, used for indenting."
4444   :type 'string
4445   :group 'gnus-thread)
4446 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4447   "With %B spec, used for a leaf with brothers."
4448   :type 'string
4449   :group 'gnus-thread)
4450 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4451   "With %B spec, used for a leaf without brothers."
4452   :type 'string
4453   :group 'gnus-thread)
4454
4455 (defun gnus-summary-prepare-threads (threads)
4456   "Prepare summary buffer from THREADS and indentation LEVEL.
4457 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4458 or a straight list of headers."
4459   (gnus-message 7 "Generating summary...")
4460
4461   (setq gnus-newsgroup-threads threads)
4462   (beginning-of-line)
4463
4464   (let ((gnus-tmp-level 0)
4465         (default-score (or gnus-summary-default-score 0))
4466         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4467         thread number subject stack state gnus-tmp-gathered beg-match
4468         new-roots gnus-tmp-new-adopts thread-end simp-subject
4469         gnus-tmp-header gnus-tmp-unread
4470         gnus-tmp-replied gnus-tmp-subject-or-nil
4471         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4472         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4473         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4474         tree-stack)
4475
4476     (setq gnus-tmp-prev-subject nil)
4477
4478     (if (vectorp (car threads))
4479         ;; If this is a straight (sic) list of headers, then a
4480         ;; threaded summary display isn't required, so we just create
4481         ;; an unthreaded one.
4482         (gnus-summary-prepare-unthreaded threads)
4483
4484       ;; Do the threaded display.
4485
4486       (while (or threads stack gnus-tmp-new-adopts new-roots)
4487
4488         (if (and (= gnus-tmp-level 0)
4489                  (or (not stack)
4490                      (= (caar stack) 0))
4491                  (not gnus-tmp-false-parent)
4492                  (or gnus-tmp-new-adopts new-roots))
4493             (if gnus-tmp-new-adopts
4494                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4495                       thread (list (car gnus-tmp-new-adopts))
4496                       gnus-tmp-header (caar thread)
4497                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4498               (when new-roots
4499                 (setq thread (list (car new-roots))
4500                       gnus-tmp-header (caar thread)
4501                       new-roots (cdr new-roots))))
4502
4503           (if threads
4504               ;; If there are some threads, we do them before the
4505               ;; threads on the stack.
4506               (setq thread threads
4507                     gnus-tmp-header (caar thread))
4508             ;; There were no current threads, so we pop something off
4509             ;; the stack.
4510             (setq state (car stack)
4511                   gnus-tmp-level (car state)
4512                   tree-stack (cadr state)
4513                   thread (caddr state)
4514                   stack (cdr stack)
4515                   gnus-tmp-header (caar thread))))
4516
4517         (setq gnus-tmp-false-parent nil)
4518         (setq gnus-tmp-root-expunged nil)
4519         (setq thread-end nil)
4520
4521         (if (stringp gnus-tmp-header)
4522             ;; The header is a dummy root.
4523             (cond
4524              ((eq gnus-summary-make-false-root 'adopt)
4525               ;; We let the first article adopt the rest.
4526               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4527                                                (cddar thread)))
4528               (setq gnus-tmp-gathered
4529                     (nconc (mapcar
4530                             (lambda (h) (mail-header-number (car h)))
4531                             (cddar thread))
4532                            gnus-tmp-gathered))
4533               (setq thread (cons (list (caar thread)
4534                                        (cadar thread))
4535                                  (cdr thread)))
4536               (setq gnus-tmp-level -1
4537                     gnus-tmp-false-parent t))
4538              ((eq gnus-summary-make-false-root 'empty)
4539               ;; We print adopted articles with empty subject fields.
4540               (setq gnus-tmp-gathered
4541                     (nconc (mapcar
4542                             (lambda (h) (mail-header-number (car h)))
4543                             (cddar thread))
4544                            gnus-tmp-gathered))
4545               (setq gnus-tmp-level -1))
4546              ((eq gnus-summary-make-false-root 'dummy)
4547               ;; We remember that we probably want to output a dummy
4548               ;; root.
4549               (setq gnus-tmp-dummy-line gnus-tmp-header)
4550               (setq gnus-tmp-prev-subject gnus-tmp-header))
4551              (t
4552               ;; We do not make a root for the gathered
4553               ;; sub-threads at all.
4554               (setq gnus-tmp-level -1)))
4555
4556           (setq number (mail-header-number gnus-tmp-header)
4557                 subject (mail-header-subject gnus-tmp-header)
4558                 simp-subject (gnus-simplify-subject-fully subject))
4559
4560           (cond
4561            ;; If the thread has changed subject, we might want to make
4562            ;; this subthread into a root.
4563            ((and (null gnus-thread-ignore-subject)
4564                  (not (zerop gnus-tmp-level))
4565                  gnus-tmp-prev-subject
4566                  (not (string= gnus-tmp-prev-subject simp-subject)))
4567             (setq new-roots (nconc new-roots (list (car thread)))
4568                   thread-end t
4569                   gnus-tmp-header nil))
4570            ;; If the article lies outside the current limit,
4571            ;; then we do not display it.
4572            ((not (memq number gnus-newsgroup-limit))
4573             (setq gnus-tmp-gathered
4574                   (nconc (mapcar
4575                           (lambda (h) (mail-header-number (car h)))
4576                           (cdar thread))
4577                          gnus-tmp-gathered))
4578             (setq gnus-tmp-new-adopts (if (cdar thread)
4579                                           (append gnus-tmp-new-adopts
4580                                                   (cdar thread))
4581                                         gnus-tmp-new-adopts)
4582                   thread-end t
4583                   gnus-tmp-header nil)
4584             (when (zerop gnus-tmp-level)
4585               (setq gnus-tmp-root-expunged t)))
4586            ;; Perhaps this article is to be marked as read?
4587            ((and gnus-summary-mark-below
4588                  (< (or (cdr (assq number gnus-newsgroup-scored))
4589                         default-score)
4590                     gnus-summary-mark-below)
4591                  ;; Don't touch sparse articles.
4592                  (not (gnus-summary-article-sparse-p number))
4593                  (not (gnus-summary-article-ancient-p number)))
4594             (setq gnus-newsgroup-unreads
4595                   (delq number gnus-newsgroup-unreads))
4596             (if gnus-newsgroup-auto-expire
4597                 (setq gnus-newsgroup-expirable
4598                       (gnus-add-to-sorted-list
4599                        gnus-newsgroup-expirable number))
4600               (push (cons number gnus-low-score-mark)
4601                     gnus-newsgroup-reads))))
4602
4603           (when gnus-tmp-header
4604             ;; We may have an old dummy line to output before this
4605             ;; article.
4606             (when (and gnus-tmp-dummy-line
4607                        (gnus-subject-equal
4608                         gnus-tmp-dummy-line
4609                         (mail-header-subject gnus-tmp-header)))
4610               (gnus-summary-insert-dummy-line
4611                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4612               (setq gnus-tmp-dummy-line nil))
4613
4614             ;; Compute the mark.
4615             (setq gnus-tmp-unread (gnus-article-mark number))
4616
4617             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4618                                   gnus-tmp-header gnus-tmp-level)
4619                   gnus-newsgroup-data)
4620
4621             ;; Actually insert the line.
4622             (setq
4623              gnus-tmp-subject-or-nil
4624              (cond
4625               ((and gnus-thread-ignore-subject
4626                     gnus-tmp-prev-subject
4627                     (not (string= gnus-tmp-prev-subject simp-subject)))
4628                subject)
4629               ((zerop gnus-tmp-level)
4630                (if (and (eq gnus-summary-make-false-root 'empty)
4631                         (memq number gnus-tmp-gathered)
4632                         gnus-tmp-prev-subject
4633                         (string= gnus-tmp-prev-subject simp-subject))
4634                    gnus-summary-same-subject
4635                  subject))
4636               (t gnus-summary-same-subject)))
4637             (if (and (eq gnus-summary-make-false-root 'adopt)
4638                      (= gnus-tmp-level 1)
4639                      (memq number gnus-tmp-gathered))
4640                 (setq gnus-tmp-opening-bracket ?\<
4641                       gnus-tmp-closing-bracket ?\>)
4642               (setq gnus-tmp-opening-bracket ?\[
4643                     gnus-tmp-closing-bracket ?\]))
4644             (setq
4645              gnus-tmp-indentation
4646              (aref gnus-thread-indent-array gnus-tmp-level)
4647              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4648              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4649                                 gnus-summary-default-score 0)
4650              gnus-tmp-score-char
4651              (if (or (null gnus-summary-default-score)
4652                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4653                          gnus-summary-zcore-fuzz))
4654                  ?                      ;Whitespace
4655                (if (< gnus-tmp-score gnus-summary-default-score)
4656                    gnus-score-below-mark gnus-score-over-mark))
4657              gnus-tmp-replied
4658              (cond ((memq number gnus-newsgroup-processable)
4659                     gnus-process-mark)
4660                    ((memq number gnus-newsgroup-cached)
4661                     gnus-cached-mark)
4662                    ((memq number gnus-newsgroup-replied)
4663                     gnus-replied-mark)
4664                    ((memq number gnus-newsgroup-forwarded)
4665                     gnus-forwarded-mark)
4666                    ((memq number gnus-newsgroup-saved)
4667                     gnus-saved-mark)
4668                    ((memq number gnus-newsgroup-recent)
4669                     gnus-recent-mark)
4670                    ((memq number gnus-newsgroup-unseen)
4671                     gnus-unseen-mark)
4672                    (t gnus-no-mark))
4673              gnus-tmp-from (mail-header-from gnus-tmp-header)
4674              gnus-tmp-name
4675              (cond
4676               ((string-match "<[^>]+> *$" gnus-tmp-from)
4677                (setq beg-match (match-beginning 0))
4678                (or (and (string-match "^\".+\"" gnus-tmp-from)
4679                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4680                    (substring gnus-tmp-from 0 beg-match)))
4681               ((string-match "(.+)" gnus-tmp-from)
4682                (substring gnus-tmp-from
4683                           (1+ (match-beginning 0)) (1- (match-end 0))))
4684               (t gnus-tmp-from))
4685              gnus-tmp-thread-tree-header-string
4686              (cond
4687               ((not gnus-show-threads) "")
4688               ((zerop gnus-tmp-level)
4689                (if (cdar thread)
4690                    (or gnus-sum-thread-tree-root subject)
4691                  (or gnus-sum-thread-tree-single-indent subject)))
4692               (t
4693                (concat (apply 'concat
4694                               (mapcar (lambda (item)
4695                                         (if (= item 1)
4696                                             gnus-sum-thread-tree-vertical
4697                                           gnus-sum-thread-tree-indent))
4698                                       (cdr (reverse tree-stack))))
4699                        (if (nth 1 thread)
4700                            gnus-sum-thread-tree-leaf-with-other
4701                          gnus-sum-thread-tree-single-leaf)))))
4702             (when (string= gnus-tmp-name "")
4703               (setq gnus-tmp-name gnus-tmp-from))
4704             (unless (numberp gnus-tmp-lines)
4705               (setq gnus-tmp-lines -1))
4706             (if (= gnus-tmp-lines -1)
4707                 (setq gnus-tmp-lines "?")
4708               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4709             (gnus-put-text-property
4710              (point)
4711              (progn (eval gnus-summary-line-format-spec) (point))
4712              'gnus-number number)
4713             (when gnus-visual-p
4714               (forward-line -1)
4715               (gnus-run-hooks 'gnus-summary-update-hook)
4716               (forward-line 1))
4717
4718             (setq gnus-tmp-prev-subject simp-subject)))
4719
4720         (when (nth 1 thread)
4721           (push (list (max 0 gnus-tmp-level)
4722                       (copy-list tree-stack)
4723                       (nthcdr 1 thread))
4724                 stack))
4725         (push (if (nth 1 thread) 1 0) tree-stack)
4726         (incf gnus-tmp-level)
4727         (setq threads (if thread-end nil (cdar thread)))
4728         (unless threads
4729           (setq gnus-tmp-level 0)))))
4730   (gnus-message 7 "Generating summary...done"))
4731
4732 (defun gnus-summary-prepare-unthreaded (headers)
4733   "Generate an unthreaded summary buffer based on HEADERS."
4734   (let (header number mark)
4735
4736     (beginning-of-line)
4737
4738     (while headers
4739       ;; We may have to root out some bad articles...
4740       (when (memq (setq number (mail-header-number
4741                                 (setq header (pop headers))))
4742                   gnus-newsgroup-limit)
4743         ;; Mark article as read when it has a low score.
4744         (when (and gnus-summary-mark-below
4745                    (< (or (cdr (assq number gnus-newsgroup-scored))
4746                           gnus-summary-default-score 0)
4747                       gnus-summary-mark-below)
4748                    (not (gnus-summary-article-ancient-p number)))
4749           (setq gnus-newsgroup-unreads
4750                 (delq number gnus-newsgroup-unreads))
4751           (if gnus-newsgroup-auto-expire
4752               (push number gnus-newsgroup-expirable)
4753             (push (cons number gnus-low-score-mark)
4754                   gnus-newsgroup-reads)))
4755
4756         (setq mark (gnus-article-mark number))
4757         (push (gnus-data-make number mark (1+ (point)) header 0)
4758               gnus-newsgroup-data)
4759         (gnus-summary-insert-line
4760          header 0 number
4761          mark (memq number gnus-newsgroup-replied)
4762          (memq number gnus-newsgroup-expirable)
4763          (mail-header-subject header) nil
4764          (cdr (assq number gnus-newsgroup-scored))
4765          (memq number gnus-newsgroup-processable))))))
4766
4767 (defun gnus-summary-remove-list-identifiers ()
4768   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4769   (let ((regexp (if (consp gnus-list-identifiers)
4770                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4771                   gnus-list-identifiers))
4772         changed subject)
4773     (when regexp
4774       (dolist (header gnus-newsgroup-headers)
4775         (setq subject (mail-header-subject header)
4776               changed nil)
4777         (while (string-match
4778                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4779                 subject)
4780           (setq subject
4781                 (concat (substring subject 0 (match-beginning 2))
4782                         (substring subject (match-end 0)))
4783                 changed t))
4784         (when (and changed
4785                    (string-match
4786                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4787           (setq subject
4788                 (concat (substring subject 0 (match-beginning 1))
4789                         (substring subject (match-end 1)))))
4790         (when changed
4791           (mail-header-set-subject header subject))))))
4792
4793 (defun gnus-fetch-headers (articles)
4794   "Fetch headers of ARTICLES."
4795   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4796     (gnus-message 5 "Fetching headers for %s..." name)
4797     (prog1
4798         (if (eq 'nov
4799                 (setq gnus-headers-retrieved-by
4800                       (gnus-retrieve-headers
4801                        articles gnus-newsgroup-name
4802                        ;; We might want to fetch old headers, but
4803                        ;; not if there is only 1 article.
4804                        (and (or (and
4805                                  (not (eq gnus-fetch-old-headers 'some))
4806                                  (not (numberp gnus-fetch-old-headers)))
4807                                 (> (length articles) 1))
4808                             gnus-fetch-old-headers))))
4809             (gnus-get-newsgroup-headers-xover
4810              articles nil nil gnus-newsgroup-name t)
4811           (gnus-get-newsgroup-headers))
4812       (gnus-message 5 "Fetching headers for %s...done" name))))
4813
4814 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4815   "Select newsgroup GROUP.
4816 If READ-ALL is non-nil, all articles in the group are selected.
4817 If SELECT-ARTICLES, only select those articles from GROUP."
4818   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4819          ;;!!! Dirty hack; should be removed.
4820          (gnus-summary-ignore-duplicates
4821           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4822               t
4823             gnus-summary-ignore-duplicates))
4824          (info (nth 2 entry))
4825          articles fetched-articles cached)
4826
4827     (unless (gnus-check-server
4828              (set (make-local-variable 'gnus-current-select-method)
4829                   (gnus-find-method-for-group group)))
4830       (error "Couldn't open server"))
4831
4832     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4833         (gnus-activate-group group)     ; Or we can activate it...
4834         (progn                          ; Or we bug out.
4835           (when (equal major-mode 'gnus-summary-mode)
4836             (kill-buffer (current-buffer)))
4837           (error "Couldn't activate group %s: %s"
4838                  group (gnus-status-message group))))
4839
4840     (unless (gnus-request-group group t)
4841       (when (equal major-mode 'gnus-summary-mode)
4842         (kill-buffer (current-buffer)))
4843       (error "Couldn't request group %s: %s"
4844              group (gnus-status-message group)))
4845
4846     (setq gnus-newsgroup-name group
4847           gnus-newsgroup-unselected nil
4848           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4849
4850     (let ((display (gnus-group-find-parameter group 'display)))
4851       (setq gnus-newsgroup-display
4852             (cond
4853              ((not (zerop (or (car-safe read-all) 0)))
4854               ;; The user entered the group with C-u SPC/RET, let's show
4855               ;; all articles.
4856               'gnus-not-ignore)
4857              ((eq display 'all)
4858               'gnus-not-ignore)
4859              ((arrayp display)
4860               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4861              ((numberp display)
4862               ;; The following is probably the "correct" solution, but
4863               ;; it makes Gnus fetch all headers and then limit the
4864               ;; articles (which is slow), so instead we hack the
4865               ;; select-articles parameter instead. -- Simon Josefsson
4866               ;; <jas@kth.se>
4867               ;;
4868               ;; (gnus-byte-compile
4869               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4870               ;;                         display)))))
4871               (setq select-articles
4872                     (gnus-uncompress-range
4873                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4874                              (if (> tmp 0)
4875                                  tmp
4876                                1))
4877                            (cdr (gnus-active group)))))
4878               nil)
4879              (t
4880               nil))))
4881
4882     (gnus-summary-setup-default-charset)
4883
4884     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4885     (when (gnus-virtual-group-p group)
4886       (setq cached gnus-newsgroup-cached))
4887
4888     (setq gnus-newsgroup-unreads
4889           (gnus-sorted-ndifference
4890            (gnus-sorted-ndifference gnus-newsgroup-unreads
4891                                     gnus-newsgroup-marked)
4892            gnus-newsgroup-dormant))
4893
4894     (setq gnus-newsgroup-processable nil)
4895
4896     (gnus-update-read-articles group gnus-newsgroup-unreads)
4897
4898     ;; Adjust and set lists of article marks.
4899     (when info
4900       (gnus-adjust-marked-articles info))
4901
4902     (if (setq articles select-articles)
4903         (setq gnus-newsgroup-unselected
4904               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4905       (setq articles (gnus-articles-to-read group read-all)))
4906
4907     (cond
4908      ((null articles)
4909       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4910       'quit)
4911      ((eq articles 0) nil)
4912      (t
4913       ;; Init the dependencies hash table.
4914       (setq gnus-newsgroup-dependencies
4915             (gnus-make-hashtable (length articles)))
4916       (gnus-set-global-variables)
4917       ;; Retrieve the headers and read them in.
4918       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4919
4920       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4921       (when cached
4922         (setq gnus-newsgroup-cached cached))
4923
4924       ;; Suppress duplicates?
4925       (when gnus-suppress-duplicates
4926         (gnus-dup-suppress-articles))
4927
4928       ;; Set the initial limit.
4929       (setq gnus-newsgroup-limit (copy-sequence articles))
4930       ;; Remove canceled articles from the list of unread articles.
4931       (setq fetched-articles
4932             (mapcar (lambda (headers) (mail-header-number headers))
4933                     gnus-newsgroup-headers))
4934       (setq gnus-newsgroup-articles fetched-articles)
4935       (setq gnus-newsgroup-unreads
4936             (gnus-sorted-nintersection
4937              gnus-newsgroup-unreads fetched-articles))
4938       (gnus-compute-unseen-list)
4939
4940       ;; Removed marked articles that do not exist.
4941       (gnus-update-missing-marks
4942        (gnus-sorted-difference articles fetched-articles))
4943       ;; We might want to build some more threads first.
4944       (when (and gnus-fetch-old-headers
4945                  (eq gnus-headers-retrieved-by 'nov))
4946         (if (eq gnus-fetch-old-headers 'invisible)
4947             (gnus-build-all-threads)
4948           (gnus-build-old-threads)))
4949       ;; Let the Gnus agent mark articles as read.
4950       (when gnus-agent
4951         (gnus-agent-get-undownloaded-list))
4952       ;; Remove list identifiers from subject
4953       (when gnus-list-identifiers
4954         (gnus-summary-remove-list-identifiers))
4955       ;; Check whether auto-expire is to be done in this group.
4956       (setq gnus-newsgroup-auto-expire
4957             (gnus-group-auto-expirable-p group))
4958       ;; Set up the article buffer now, if necessary.
4959       (unless gnus-single-article-buffer
4960         (gnus-article-setup-buffer))
4961       ;; First and last article in this newsgroup.
4962       (when gnus-newsgroup-headers
4963         (setq gnus-newsgroup-begin
4964               (mail-header-number (car gnus-newsgroup-headers))
4965               gnus-newsgroup-end
4966               (mail-header-number
4967                (gnus-last-element gnus-newsgroup-headers))))
4968       ;; GROUP is successfully selected.
4969       (or gnus-newsgroup-headers t)))))
4970
4971 (defun gnus-compute-unseen-list ()
4972   ;; The `seen' marks are treated specially.
4973   (if (not gnus-newsgroup-seen)
4974       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4975     (setq gnus-newsgroup-unseen
4976           (gnus-inverse-list-range-intersection
4977            gnus-newsgroup-articles gnus-newsgroup-seen))))
4978
4979 (defun gnus-summary-display-make-predicate (display)
4980   (require 'gnus-agent)
4981   (when (= (length display) 1)
4982     (setq display (car display)))
4983   (unless gnus-summary-display-cache
4984     (dolist (elem (append '((unread . unread)
4985                             (read . read)
4986                             (unseen . unseen))
4987                           gnus-article-mark-lists))
4988       (push (cons (cdr elem)
4989                   (gnus-byte-compile
4990                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4991             gnus-summary-display-cache)))
4992   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4993         (gnus-category-predicate-cache gnus-summary-display-cache))
4994     (gnus-get-predicate display)))
4995
4996 ;; Uses the dynamically bound `number' variable.
4997 (defvar number)
4998 (defun gnus-article-marked-p (type &optional article)
4999   (let ((article (or article number)))
5000     (cond
5001      ((eq type 'tick)
5002       (memq article gnus-newsgroup-marked))
5003      ((eq type 'spam)
5004       (memq article gnus-newsgroup-spam-marked))
5005      ((eq type 'unsend)
5006       (memq article gnus-newsgroup-unsendable))
5007      ((eq type 'undownload)
5008       (memq article gnus-newsgroup-undownloaded))
5009      ((eq type 'download)
5010       (memq article gnus-newsgroup-downloadable))
5011      ((eq type 'unread)
5012       (memq article gnus-newsgroup-unreads))
5013      ((eq type 'read)
5014       (memq article gnus-newsgroup-reads))
5015      ((eq type 'dormant)
5016       (memq article gnus-newsgroup-dormant) )
5017      ((eq type 'expire)
5018       (memq article gnus-newsgroup-expirable))
5019      ((eq type 'reply)
5020       (memq article gnus-newsgroup-replied))
5021      ((eq type 'killed)
5022       (memq article gnus-newsgroup-killed))
5023      ((eq type 'bookmark)
5024       (assq article gnus-newsgroup-bookmarks))
5025      ((eq type 'score)
5026       (assq article gnus-newsgroup-scored))
5027      ((eq type 'save)
5028       (memq article gnus-newsgroup-saved))
5029      ((eq type 'cache)
5030       (memq article gnus-newsgroup-cached))
5031      ((eq type 'forward)
5032       (memq article gnus-newsgroup-forwarded))
5033      ((eq type 'seen)
5034       (not (memq article gnus-newsgroup-unseen)))
5035      ((eq type 'recent)
5036       (memq article gnus-newsgroup-recent))
5037      (t t))))
5038
5039 (defun gnus-articles-to-read (group &optional read-all)
5040   "Find out what articles the user wants to read."
5041   (let* ((articles
5042           ;; Select all articles if `read-all' is non-nil, or if there
5043           ;; are no unread articles.
5044           (if (or read-all
5045                   (and (zerop (length gnus-newsgroup-marked))
5046                        (zerop (length gnus-newsgroup-unreads)))
5047                   ;; Fetch all if the predicate is non-nil.
5048                   gnus-newsgroup-display)
5049               ;; We want to select the headers for all the articles in
5050               ;; the group, so we select either all the active
5051               ;; articles in the group, or (if that's nil), the
5052               ;; articles in the cache.
5053               (or
5054                (gnus-uncompress-range (gnus-active group))
5055                (gnus-cache-articles-in-group group))
5056             ;; Select only the "normal" subset of articles.
5057             (gnus-sorted-nunion
5058              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5059              gnus-newsgroup-unreads)))
5060          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5061          (scored (length scored-list))
5062          (number (length articles))
5063          (marked (+ (length gnus-newsgroup-marked)
5064                     (length gnus-newsgroup-dormant)))
5065          (select
5066           (cond
5067            ((numberp read-all)
5068             read-all)
5069            ((numberp gnus-newsgroup-display)
5070             gnus-newsgroup-display)
5071            (t
5072             (condition-case ()
5073                 (cond
5074                  ((and (or (<= scored marked) (= scored number))
5075                        (numberp gnus-large-newsgroup)
5076                        (> number gnus-large-newsgroup))
5077                   (let* ((cursor-in-echo-area nil)
5078                          (initial (gnus-parameter-large-newsgroup-initial
5079                                    gnus-newsgroup-name))
5080                          (input
5081                           (read-string
5082                            (format
5083                             "How many articles from %s (%s %d): "
5084                             (gnus-limit-string
5085                              (gnus-group-decoded-name gnus-newsgroup-name)
5086                              35)
5087                             (if initial "max" "default")
5088                             number)
5089                            (if initial
5090                                (cons (number-to-string initial)
5091                                      0)))))
5092                     (if (string-match "^[ \t]*$" input) number input)))
5093                  ((and (> scored marked) (< scored number)
5094                        (> (- scored number) 20))
5095                   (let ((input
5096                          (read-string
5097                           (format "%s %s (%d scored, %d total): "
5098                                   "How many articles from"
5099                                   (gnus-group-decoded-name group)
5100                                   scored number))))
5101                     (if (string-match "^[ \t]*$" input)
5102                         number input)))
5103                  (t number))
5104               (quit
5105                (message "Quit getting the articles to read")
5106                nil))))))
5107     (setq select (if (stringp select) (string-to-number select) select))
5108     (if (or (null select) (zerop select))
5109         select
5110       (if (and (not (zerop scored)) (<= (abs select) scored))
5111           (progn
5112             (setq articles (sort scored-list '<))
5113             (setq number (length articles)))
5114         (setq articles (copy-sequence articles)))
5115
5116       (when (< (abs select) number)
5117         (if (< select 0)
5118             ;; Select the N oldest articles.
5119             (setcdr (nthcdr (1- (abs select)) articles) nil)
5120           ;; Select the N most recent articles.
5121           (setq articles (nthcdr (- number select) articles))))
5122       (setq gnus-newsgroup-unselected
5123             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5124       (when gnus-alter-articles-to-read-function
5125         (setq gnus-newsgroup-unreads
5126               (sort
5127                (funcall gnus-alter-articles-to-read-function
5128                         gnus-newsgroup-name gnus-newsgroup-unreads)
5129                '<)))
5130       articles)))
5131
5132 (defun gnus-killed-articles (killed articles)
5133   (let (out)
5134     (while articles
5135       (when (inline (gnus-member-of-range (car articles) killed))
5136         (push (car articles) out))
5137       (setq articles (cdr articles)))
5138     out))
5139
5140 (defun gnus-uncompress-marks (marks)
5141   "Uncompress the mark ranges in MARKS."
5142   (let ((uncompressed '(score bookmark))
5143         out)
5144     (while marks
5145       (if (memq (caar marks) uncompressed)
5146           (push (car marks) out)
5147         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5148       (setq marks (cdr marks)))
5149     out))
5150
5151 (defun gnus-article-mark-to-type (mark)
5152   "Return the type of MARK."
5153   (or (cadr (assq mark gnus-article-special-mark-lists))
5154       'list))
5155
5156 (defun gnus-article-unpropagatable-p (mark)
5157   "Return whether MARK should be propagated to backend."
5158   (memq mark gnus-article-unpropagated-mark-lists))
5159
5160 (defun gnus-adjust-marked-articles (info)
5161   "Set all article lists and remove all marks that are no longer valid."
5162   (let* ((marked-lists (gnus-info-marks info))
5163          (active (gnus-active (gnus-info-group info)))
5164          (min (car active))
5165          (max (cdr active))
5166          (types gnus-article-mark-lists)
5167          marks var articles article mark mark-type)
5168
5169     (dolist (marks marked-lists)
5170       (setq mark (car marks)
5171             mark-type (gnus-article-mark-to-type mark)
5172             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5173
5174       ;; We set the variable according to the type of the marks list,
5175       ;; and then adjust the marks to a subset of the active articles.
5176       (cond
5177        ;; Adjust "simple" lists.
5178        ((eq mark-type 'list)
5179         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5180         (when (memq mark '(tick dormant expire reply save))
5181           (while articles
5182             (when (or (< (setq article (pop articles)) min) (> article max))
5183               (set var (delq article (symbol-value var)))))))
5184        ;; Adjust assocs.
5185        ((eq mark-type 'tuple)
5186         (set var (setq articles (cdr marks)))
5187         (when (not (listp (cdr (symbol-value var))))
5188           (set var (list (symbol-value var))))
5189         (when (not (listp (cdr articles)))
5190           (setq articles (list articles)))
5191         (while articles
5192           (when (or (not (consp (setq article (pop articles))))
5193                     (< (car article) min)
5194                     (> (car article) max))
5195             (set var (delq article (symbol-value var))))))
5196        ;; Adjust ranges (sloppily).
5197        ((eq mark-type 'range)
5198         (cond
5199          ((eq mark 'seen)
5200           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5201           ;; It should be (seen (NUM1 . NUM2)).
5202           (when (numberp (cddr marks))
5203             (setcdr marks (list (cdr marks))))
5204           (setq articles (cdr marks))
5205           (while (and articles
5206                       (or (and (consp (car articles))
5207                                (> min (cdar articles)))
5208                           (and (numberp (car articles))
5209                                (> min (car articles)))))
5210             (pop articles))
5211           (set var articles))))))))
5212
5213 (defun gnus-update-missing-marks (missing)
5214   "Go through the list of MISSING articles and remove them from the mark lists."
5215   (when missing
5216     (let (var m)
5217       ;; Go through all types.
5218       (dolist (elem gnus-article-mark-lists)
5219         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5220           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5221           (when (symbol-value var)
5222             ;; This list has articles.  So we delete all missing
5223             ;; articles from it.
5224             (setq m missing)
5225             (while m
5226               (set var (delq (pop m) (symbol-value var))))))))))
5227
5228 (defun gnus-update-marks ()
5229   "Enter the various lists of marked articles into the newsgroup info list."
5230   (let ((types gnus-article-mark-lists)
5231         (info (gnus-get-info gnus-newsgroup-name))
5232         type list newmarked symbol delta-marks)
5233     (when info
5234       ;; Add all marks lists to the list of marks lists.
5235       (while (setq type (pop types))
5236         (setq list (symbol-value
5237                     (setq symbol
5238                           (intern (format "gnus-newsgroup-%s" (car type))))))
5239
5240         (when list
5241           ;; Get rid of the entries of the articles that have the
5242           ;; default score.
5243           (when (and (eq (cdr type) 'score)
5244                      gnus-save-score
5245                      list)
5246             (let* ((arts list)
5247                    (prev (cons nil list))
5248                    (all prev))
5249               (while arts
5250                 (if (or (not (consp (car arts)))
5251                         (= (cdar arts) gnus-summary-default-score))
5252                     (setcdr prev (cdr arts))
5253                   (setq prev arts))
5254                 (setq arts (cdr arts)))
5255               (setq list (cdr all)))))
5256
5257         (when (eq (cdr type) 'seen)
5258           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5259
5260         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5261           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5262
5263         (when (and (gnus-check-backend-function
5264                     'request-set-mark gnus-newsgroup-name)
5265                    (not (gnus-article-unpropagatable-p (cdr type))))
5266           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5267                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5268                  (add (gnus-remove-from-range
5269                        (gnus-copy-sequence list) old)))
5270             (when add
5271               (push (list add 'add (list (cdr type))) delta-marks))
5272             (when del
5273               (push (list del 'del (list (cdr type))) delta-marks))))
5274
5275         (when list
5276           (push (cons (cdr type) list) newmarked)))
5277
5278       (when delta-marks
5279         (unless (gnus-check-group gnus-newsgroup-name)
5280           (error "Can't open server for %s" gnus-newsgroup-name))
5281         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5282
5283       ;; Enter these new marks into the info of the group.
5284       (if (nthcdr 3 info)
5285           (setcar (nthcdr 3 info) newmarked)
5286         ;; Add the marks lists to the end of the info.
5287         (when newmarked
5288           (setcdr (nthcdr 2 info) (list newmarked))))
5289
5290       ;; Cut off the end of the info if there's nothing else there.
5291       (let ((i 5))
5292         (while (and (> i 2)
5293                     (not (nth i info)))
5294           (when (nthcdr (decf i) info)
5295             (setcdr (nthcdr i info) nil)))))))
5296
5297 (defun gnus-set-mode-line (where)
5298   "Set the mode line of the article or summary buffers.
5299 If WHERE is `summary', the summary mode line format will be used."
5300   ;; Is this mode line one we keep updated?
5301   (when (and (memq where gnus-updated-mode-lines)
5302              (symbol-value
5303               (intern (format "gnus-%s-mode-line-format-spec" where))))
5304     (let (mode-string)
5305       (save-excursion
5306         ;; We evaluate this in the summary buffer since these
5307         ;; variables are buffer-local to that buffer.
5308         (set-buffer gnus-summary-buffer)
5309        ;; We bind all these variables that are used in the `eval' form
5310         ;; below.
5311         (let* ((mformat (symbol-value
5312                          (intern
5313                           (format "gnus-%s-mode-line-format-spec" where))))
5314                (gnus-tmp-group-name (gnus-group-decoded-name
5315                                      gnus-newsgroup-name))
5316                (gnus-tmp-article-number (or gnus-current-article 0))
5317                (gnus-tmp-unread gnus-newsgroup-unreads)
5318                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5319                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5320                (gnus-tmp-unread-and-unselected
5321                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5322                             (zerop gnus-tmp-unselected))
5323                        "")
5324                       ((zerop gnus-tmp-unselected)
5325                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5326                       (t (format "{%d(+%d) more}"
5327                                  gnus-tmp-unread-and-unticked
5328                                  gnus-tmp-unselected))))
5329                (gnus-tmp-subject
5330                 (if (and gnus-current-headers
5331                          (vectorp gnus-current-headers))
5332                     (gnus-mode-string-quote
5333                      (mail-header-subject gnus-current-headers))
5334                   ""))
5335                bufname-length max-len
5336                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5337           (setq mode-string (eval mformat))
5338           (setq bufname-length (if (string-match "%b" mode-string)
5339                                    (- (length
5340                                        (buffer-name
5341                                         (if (eq where 'summary)
5342                                             nil
5343                                           (get-buffer gnus-article-buffer))))
5344                                       2)
5345                                  0))
5346           (setq max-len (max 4 (if gnus-mode-non-string-length
5347                                    (- (window-width)
5348                                       gnus-mode-non-string-length
5349                                       bufname-length)
5350                                  (length mode-string))))
5351           ;; We might have to chop a bit of the string off...
5352           (when (> (length mode-string) max-len)
5353             (setq mode-string
5354                   (concat (truncate-string-to-width mode-string (- max-len 3))
5355                           "...")))
5356           ;; Pad the mode string a bit.
5357           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5358       ;; Update the mode line.
5359       (setq mode-line-buffer-identification
5360             (gnus-mode-line-buffer-identification (list mode-string)))
5361       (set-buffer-modified-p t))))
5362
5363 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5364   "Go through the HEADERS list and add all Xrefs to a hash table.
5365 The resulting hash table is returned, or nil if no Xrefs were found."
5366   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5367          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5368          (xref-hashtb (gnus-make-hashtable))
5369          start group entry number xrefs header)
5370     (while headers
5371       (setq header (pop headers))
5372       (when (and (setq xrefs (mail-header-xref header))
5373                  (not (memq (setq number (mail-header-number header))
5374                             unreads)))
5375         (setq start 0)
5376         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5377           (setq start (match-end 0))
5378           (setq group (if prefix
5379                           (concat prefix (substring xrefs (match-beginning 1)
5380                                                     (match-end 1)))
5381                         (substring xrefs (match-beginning 1) (match-end 1))))
5382           (setq number
5383                 (string-to-int (substring xrefs (match-beginning 2)
5384                                           (match-end 2))))
5385           (if (setq entry (gnus-gethash group xref-hashtb))
5386               (setcdr entry (cons number (cdr entry)))
5387             (gnus-sethash group (cons number nil) xref-hashtb)))))
5388     (and start xref-hashtb)))
5389
5390 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5391   "Look through all the headers and mark the Xrefs as read."
5392   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5393         name entry info xref-hashtb idlist method nth4)
5394     (save-excursion
5395       (set-buffer gnus-group-buffer)
5396       (when (setq xref-hashtb
5397                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5398         (mapatoms
5399          (lambda (group)
5400            (unless (string= from-newsgroup (setq name (symbol-name group)))
5401              (setq idlist (symbol-value group))
5402              ;; Dead groups are not updated.
5403              (and (prog1
5404                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5405                             info (nth 2 entry))
5406                     (when (stringp (setq nth4 (gnus-info-method info)))
5407                       (setq nth4 (gnus-server-to-method nth4))))
5408                   ;; Only do the xrefs if the group has the same
5409                   ;; select method as the group we have just read.
5410                   (or (gnus-methods-equal-p
5411                        nth4 (gnus-find-method-for-group from-newsgroup))
5412                       virtual
5413                       (equal nth4 (setq method (gnus-find-method-for-group
5414                                                 from-newsgroup)))
5415                       (and (equal (car nth4) (car method))
5416                            (equal (nth 1 nth4) (nth 1 method))))
5417                   gnus-use-cross-reference
5418                   (or (not (eq gnus-use-cross-reference t))
5419                       virtual
5420                       ;; Only do cross-references on subscribed
5421                       ;; groups, if that is what is wanted.
5422                       (<= (gnus-info-level info) gnus-level-subscribed))
5423                   (gnus-group-make-articles-read name idlist))))
5424          xref-hashtb)))))
5425
5426 (defun gnus-compute-read-articles (group articles)
5427   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5428          (info (nth 2 entry))
5429          (active (gnus-active group))
5430          ninfo)
5431     (when entry
5432       ;; First peel off all invalid article numbers.
5433       (when active
5434         (let ((ids articles)
5435               id first)
5436           (while (setq id (pop ids))
5437             (when (and first (> id (cdr active)))
5438               ;; We'll end up in this situation in one particular
5439               ;; obscure situation.  If you re-scan a group and get
5440               ;; a new article that is cross-posted to a different
5441               ;; group that has not been re-scanned, you might get
5442               ;; crossposted article that has a higher number than
5443               ;; Gnus believes possible.  So we re-activate this
5444               ;; group as well.  This might mean doing the
5445               ;; crossposting thingy will *increase* the number
5446               ;; of articles in some groups.  Tsk, tsk.
5447               (setq active (or (gnus-activate-group group) active)))
5448             (when (or (> id (cdr active))
5449                       (< id (car active)))
5450               (setq articles (delq id articles))))))
5451       ;; If the read list is nil, we init it.
5452       (if (and active
5453                (null (gnus-info-read info))
5454                (> (car active) 1))
5455           (setq ninfo (cons 1 (1- (car active))))
5456         (setq ninfo (gnus-info-read info)))
5457       ;; Then we add the read articles to the range.
5458       (gnus-add-to-range
5459        ninfo (setq articles (sort articles '<))))))
5460
5461 (defun gnus-group-make-articles-read (group articles)
5462   "Update the info of GROUP to say that ARTICLES are read."
5463   (let* ((num 0)
5464          (entry (gnus-gethash group gnus-newsrc-hashtb))
5465          (info (nth 2 entry))
5466          (active (gnus-active group))
5467          range)
5468     (when entry
5469       (setq range (gnus-compute-read-articles group articles))
5470       (save-excursion
5471         (set-buffer gnus-group-buffer)
5472         (gnus-undo-register
5473           `(progn
5474              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5475              (gnus-info-set-read ',info ',(gnus-info-read info))
5476              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5477              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5478              (gnus-group-update-group ,group t))))
5479       ;; Add the read articles to the range.
5480       (gnus-info-set-read info range)
5481       (gnus-request-set-mark group (list (list range 'add '(read))))
5482       ;; Then we have to re-compute how many unread
5483       ;; articles there are in this group.
5484       (when active
5485         (cond
5486          ((not range)
5487           (setq num (- (1+ (cdr active)) (car active))))
5488          ((not (listp (cdr range)))
5489           (setq num (- (cdr active) (- (1+ (cdr range))
5490                                        (car range)))))
5491          (t
5492           (while range
5493             (if (numberp (car range))
5494                 (setq num (1+ num))
5495               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5496             (setq range (cdr range)))
5497           (setq num (- (cdr active) num))))
5498         ;; Update the number of unread articles.
5499         (setcar entry num)
5500         ;; Update the group buffer.
5501         (gnus-group-update-group group t)))))
5502
5503 (defvar gnus-newsgroup-none-id 0)
5504
5505 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5506   (let ((cur nntp-server-buffer)
5507         (dependencies
5508          (or dependencies
5509              (save-excursion (set-buffer gnus-summary-buffer)
5510                              gnus-newsgroup-dependencies)))
5511         headers id end ref
5512         (mail-parse-charset gnus-newsgroup-charset)
5513         (mail-parse-ignored-charsets
5514          (save-excursion (condition-case nil
5515                              (set-buffer gnus-summary-buffer)
5516                            (error))
5517                          gnus-newsgroup-ignored-charsets)))
5518     (save-excursion
5519       (set-buffer nntp-server-buffer)
5520       ;; Translate all TAB characters into SPACE characters.
5521       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5522       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5523       (gnus-run-hooks 'gnus-parse-headers-hook)
5524       (let ((case-fold-search t)
5525             in-reply-to header p lines chars)
5526         (goto-char (point-min))
5527         ;; Search to the beginning of the next header.  Error messages
5528         ;; do not begin with 2 or 3.
5529         (while (re-search-forward "^[23][0-9]+ " nil t)
5530           (setq id nil
5531                 ref nil)
5532           ;; This implementation of this function, with nine
5533           ;; search-forwards instead of the one re-search-forward and
5534           ;; a case (which basically was the old function) is actually
5535           ;; about twice as fast, even though it looks messier.  You
5536           ;; can't have everything, I guess.  Speed and elegance
5537           ;; doesn't always go hand in hand.
5538           (setq
5539            header
5540            (vector
5541             ;; Number.
5542             (prog1
5543                 (read cur)
5544               (end-of-line)
5545               (setq p (point))
5546               (narrow-to-region (point)
5547                                 (or (and (search-forward "\n.\n" nil t)
5548                                          (- (point) 2))
5549                                     (point))))
5550             ;; Subject.
5551             (progn
5552               (goto-char p)
5553               (if (search-forward "\nsubject:" nil t)
5554                   (funcall gnus-decode-encoded-word-function
5555                            (nnheader-header-value))
5556                 "(none)"))
5557             ;; From.
5558             (progn
5559               (goto-char p)
5560               (if (search-forward "\nfrom:" nil t)
5561                   (funcall gnus-decode-encoded-word-function
5562                            (nnheader-header-value))
5563                 "(nobody)"))
5564             ;; Date.
5565             (progn
5566               (goto-char p)
5567               (if (search-forward "\ndate:" nil t)
5568                   (nnheader-header-value) ""))
5569             ;; Message-ID.
5570             (progn
5571               (goto-char p)
5572               (setq id (if (re-search-forward
5573                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5574                            ;; We do it this way to make sure the Message-ID
5575                            ;; is (somewhat) syntactically valid.
5576                            (buffer-substring (match-beginning 1)
5577                                              (match-end 1))
5578                          ;; If there was no message-id, we just fake one
5579                          ;; to make subsequent routines simpler.
5580                          (nnheader-generate-fake-message-id))))
5581             ;; References.
5582             (progn
5583               (goto-char p)
5584               (if (search-forward "\nreferences:" nil t)
5585                   (progn
5586                     (setq end (point))
5587                     (prog1
5588                         (nnheader-header-value)
5589                       (setq ref
5590                             (buffer-substring
5591                              (progn
5592                                (end-of-line)
5593                                (search-backward ">" end t)
5594                                (1+ (point)))
5595                              (progn
5596                                (search-backward "<" end t)
5597                                (point))))))
5598                 ;; Get the references from the in-reply-to header if there
5599                 ;; were no references and the in-reply-to header looks
5600                 ;; promising.
5601                 (if (and (search-forward "\nin-reply-to:" nil t)
5602                          (setq in-reply-to (nnheader-header-value))
5603                          (string-match "<[^>]+>" in-reply-to))
5604                     (let (ref2)
5605                       (setq ref (substring in-reply-to (match-beginning 0)
5606                                            (match-end 0)))
5607                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5608                         (setq ref2 (substring in-reply-to (match-beginning 0)
5609                                               (match-end 0)))
5610                         (when (> (length ref2) (length ref))
5611                           (setq ref ref2)))
5612                       ref)
5613                   (setq ref nil))))
5614             ;; Chars.
5615             (progn
5616               (goto-char p)
5617               (if (search-forward "\nchars: " nil t)
5618                   (if (numberp (setq chars (ignore-errors (read cur))))
5619                       chars -1)
5620                 -1))
5621             ;; Lines.
5622             (progn
5623               (goto-char p)
5624               (if (search-forward "\nlines: " nil t)
5625                   (if (numberp (setq lines (ignore-errors (read cur))))
5626                       lines -1)
5627                 -1))
5628             ;; Xref.
5629             (progn
5630               (goto-char p)
5631               (and (search-forward "\nxref:" nil t)
5632                    (nnheader-header-value)))
5633             ;; Extra.
5634             (when gnus-extra-headers
5635               (let ((extra gnus-extra-headers)
5636                     out)
5637                 (while extra
5638                   (goto-char p)
5639                   (when (search-forward
5640                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5641                     (push (cons (car extra) (nnheader-header-value))
5642                           out))
5643                   (pop extra))
5644                 out))))
5645           (when (equal id ref)
5646             (setq ref nil))
5647
5648           (when gnus-alter-header-function
5649             (funcall gnus-alter-header-function header)
5650             (setq id (mail-header-id header)
5651                   ref (gnus-parent-id (mail-header-references header))))
5652
5653           (when (setq header
5654                       (gnus-dependencies-add-header
5655                        header dependencies force-new))
5656             (push header headers))
5657           (goto-char (point-max))
5658           (widen))
5659         (nreverse headers)))))
5660
5661 ;; Goes through the xover lines and returns a list of vectors
5662 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5663                                                   force-new dependencies
5664                                                   group also-fetch-heads)
5665   "Parse the news overview data in the server buffer.
5666 Return a list of headers that match SEQUENCE (see
5667 `nntp-retrieve-headers')."
5668   ;; Get the Xref when the users reads the articles since most/some
5669   ;; NNTP servers do not include Xrefs when using XOVER.
5670   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5671   (let ((mail-parse-charset gnus-newsgroup-charset)
5672         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5673         (cur nntp-server-buffer)
5674         (dependencies (or dependencies gnus-newsgroup-dependencies))
5675         (allp (cond
5676                ((eq gnus-read-all-available-headers t)
5677                 t)
5678                ((stringp gnus-read-all-available-headers)
5679                 (string-match gnus-read-all-available-headers group))
5680                (t
5681                 nil)))
5682         number headers header)
5683     (save-excursion
5684       (set-buffer nntp-server-buffer)
5685       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5686       ;; Allow the user to mangle the headers before parsing them.
5687       (gnus-run-hooks 'gnus-parse-headers-hook)
5688       (goto-char (point-min))
5689       (gnus-parse-without-error
5690         (while (and (or sequence allp)
5691                     (not (eobp)))
5692           (setq number (read cur))
5693           (when (not allp)
5694             (while (and sequence
5695                         (< (car sequence) number))
5696               (setq sequence (cdr sequence))))
5697           (when (and (or allp
5698                          (and sequence
5699                               (eq number (car sequence))))
5700                      (progn
5701                        (setq sequence (cdr sequence))
5702                        (setq header (inline
5703                                       (gnus-nov-parse-line
5704                                        number dependencies force-new)))))
5705             (push header headers))
5706           (forward-line 1)))
5707       ;; A common bug in inn is that if you have posted an article and
5708       ;; then retrieves the active file, it will answer correctly --
5709       ;; the new article is included.  However, a NOV entry for the
5710       ;; article may not have been generated yet, so this may fail.
5711       ;; We work around this problem by retrieving the last few
5712       ;; headers using HEAD.
5713       (if (or (not also-fetch-heads)
5714               (not sequence))
5715           ;; We (probably) got all the headers.
5716           (nreverse headers)
5717         (let ((gnus-nov-is-evil t))
5718           (nconc
5719            (nreverse headers)
5720            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5721              (gnus-get-newsgroup-headers))))))))
5722
5723 (defun gnus-article-get-xrefs ()
5724   "Fill in the Xref value in `gnus-current-headers', if necessary.
5725 This is meant to be called in `gnus-article-internal-prepare-hook'."
5726   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5727                                  gnus-current-headers)))
5728     (or (not gnus-use-cross-reference)
5729         (not headers)
5730         (and (mail-header-xref headers)
5731              (not (string= (mail-header-xref headers) "")))
5732         (let ((case-fold-search t)
5733               xref)
5734           (save-restriction
5735             (nnheader-narrow-to-headers)
5736             (goto-char (point-min))
5737             (when (or (and (not (eobp))
5738                            (eq (downcase (char-after)) ?x)
5739                            (looking-at "Xref:"))
5740                       (search-forward "\nXref:" nil t))
5741               (goto-char (1+ (match-end 0)))
5742               (setq xref (buffer-substring (point)
5743                                            (progn (end-of-line) (point))))
5744               (mail-header-set-xref headers xref)))))))
5745
5746 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5747   "Find article ID and insert the summary line for that article.
5748 OLD-HEADER can either be a header or a line number to insert
5749 the subject line on."
5750   (let* ((line (and (numberp old-header) old-header))
5751          (old-header (and (vectorp old-header) old-header))
5752          (header (cond ((and old-header use-old-header)
5753                         old-header)
5754                        ((and (numberp id)
5755                              (gnus-number-to-header id))
5756                         (gnus-number-to-header id))
5757                        (t
5758                         (gnus-read-header id))))
5759          (number (and (numberp id) id))
5760          d)
5761     (when header
5762       ;; Rebuild the thread that this article is part of and go to the
5763       ;; article we have fetched.
5764       (when (and (not gnus-show-threads)
5765                  old-header)
5766         (when (and number
5767                    (setq d (gnus-data-find (mail-header-number old-header))))
5768           (goto-char (gnus-data-pos d))
5769           (gnus-data-remove
5770            number
5771            (- (gnus-point-at-bol)
5772               (prog1
5773                   (1+ (gnus-point-at-eol))
5774                 (gnus-delete-line))))))
5775       (when old-header
5776         (mail-header-set-number header (mail-header-number old-header)))
5777       (setq gnus-newsgroup-sparse
5778             (delq (setq number (mail-header-number header))
5779                   gnus-newsgroup-sparse))
5780       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5781       (push number gnus-newsgroup-limit)
5782       (gnus-rebuild-thread (mail-header-id header) line)
5783       (gnus-summary-goto-subject number nil t))
5784     (when (and (numberp number)
5785                (> number 0))
5786       ;; We have to update the boundaries even if we can't fetch the
5787       ;; article if ID is a number -- so that the next `P' or `N'
5788       ;; command will fetch the previous (or next) article even
5789       ;; if the one we tried to fetch this time has been canceled.
5790       (when (> number gnus-newsgroup-end)
5791         (setq gnus-newsgroup-end number))
5792       (when (< number gnus-newsgroup-begin)
5793         (setq gnus-newsgroup-begin number))
5794       (setq gnus-newsgroup-unselected
5795             (delq number gnus-newsgroup-unselected)))
5796     ;; Report back a success?
5797     (and header (mail-header-number header))))
5798
5799 ;;; Process/prefix in the summary buffer
5800
5801 (defun gnus-summary-work-articles (n)
5802   "Return a list of articles to be worked upon.
5803 The prefix argument, the list of process marked articles, and the
5804 current article will be taken into consideration."
5805   (save-excursion
5806     (set-buffer gnus-summary-buffer)
5807     (cond
5808      (n
5809       ;; A numerical prefix has been given.
5810       (setq n (prefix-numeric-value n))
5811       (let ((backward (< n 0))
5812             (n (abs (prefix-numeric-value n)))
5813             articles article)
5814         (save-excursion
5815           (while
5816               (and (> n 0)
5817                    (push (setq article (gnus-summary-article-number))
5818                          articles)
5819                    (if backward
5820                        (gnus-summary-find-prev nil article)
5821                      (gnus-summary-find-next nil article)))
5822             (decf n)))
5823         (nreverse articles)))
5824      ((and (gnus-region-active-p) (mark))
5825       (message "region active")
5826       ;; Work on the region between point and mark.
5827       (let ((max (max (point) (mark)))
5828             articles article)
5829         (save-excursion
5830           (goto-char (min (min (point) (mark))))
5831           (while
5832               (and
5833                (push (setq article (gnus-summary-article-number)) articles)
5834                (gnus-summary-find-next nil article)
5835                (< (point) max)))
5836           (nreverse articles))))
5837      (gnus-newsgroup-processable
5838       ;; There are process-marked articles present.
5839       ;; Save current state.
5840       (gnus-summary-save-process-mark)
5841       ;; Return the list.
5842       (reverse gnus-newsgroup-processable))
5843      (t
5844       ;; Just return the current article.
5845       (list (gnus-summary-article-number))))))
5846
5847 (defmacro gnus-summary-iterate (arg &rest forms)
5848   "Iterate over the process/prefixed articles and do FORMS.
5849 ARG is the interactive prefix given to the command.  FORMS will be
5850 executed with point over the summary line of the articles."
5851   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5852     `(let ((,articles (gnus-summary-work-articles ,arg)))
5853        (while ,articles
5854          (gnus-summary-goto-subject (car ,articles))
5855          ,@forms
5856          (pop ,articles)))))
5857
5858 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5859 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5860
5861 (defun gnus-summary-save-process-mark ()
5862   "Push the current set of process marked articles on the stack."
5863   (interactive)
5864   (push (copy-sequence gnus-newsgroup-processable)
5865         gnus-newsgroup-process-stack))
5866
5867 (defun gnus-summary-kill-process-mark ()
5868   "Push the current set of process marked articles on the stack and unmark."
5869   (interactive)
5870   (gnus-summary-save-process-mark)
5871   (gnus-summary-unmark-all-processable))
5872
5873 (defun gnus-summary-yank-process-mark ()
5874   "Pop the last process mark state off the stack and restore it."
5875   (interactive)
5876   (unless gnus-newsgroup-process-stack
5877     (error "Empty mark stack"))
5878   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5879
5880 (defun gnus-summary-process-mark-set (set)
5881   "Make SET into the current process marked articles."
5882   (gnus-summary-unmark-all-processable)
5883   (while set
5884     (gnus-summary-set-process-mark (pop set))))
5885
5886 ;;; Searching and stuff
5887
5888 (defun gnus-summary-search-group (&optional backward use-level)
5889   "Search for next unread newsgroup.
5890 If optional argument BACKWARD is non-nil, search backward instead."
5891   (save-excursion
5892     (set-buffer gnus-group-buffer)
5893     (when (gnus-group-search-forward
5894            backward nil (if use-level (gnus-group-group-level) nil))
5895       (gnus-group-group-name))))
5896
5897 (defun gnus-summary-best-group (&optional exclude-group)
5898   "Find the name of the best unread group.
5899 If EXCLUDE-GROUP, do not go to this group."
5900   (save-excursion
5901     (set-buffer gnus-group-buffer)
5902     (save-excursion
5903       (gnus-group-best-unread-group exclude-group))))
5904
5905 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5906   (if backward (gnus-summary-find-prev)
5907     (let* ((dummy (gnus-summary-article-intangible-p))
5908            (article (or article (gnus-summary-article-number)))
5909            (arts (gnus-data-find-list article))
5910            result)
5911       (when (and (not dummy)
5912                  (or (not gnus-summary-check-current)
5913                      (not unread)
5914                      (not (gnus-data-unread-p (car arts)))))
5915         (setq arts (cdr arts)))
5916       (when (setq result
5917                   (if unread
5918                       (progn
5919                         (while arts
5920                           (when (or (and undownloaded
5921                                          (eq gnus-undownloaded-mark
5922                                              (gnus-data-mark (car arts))))
5923                                     (gnus-data-unread-p (car arts)))
5924                             (setq result (car arts)
5925                                   arts nil))
5926                           (setq arts (cdr arts)))
5927                         result)
5928                     (car arts)))
5929         (goto-char (gnus-data-pos result))
5930         (gnus-data-number result)))))
5931
5932 (defun gnus-summary-find-prev (&optional unread article)
5933   (let* ((eobp (eobp))
5934          (article (or article (gnus-summary-article-number)))
5935          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5936          result)
5937     (when (and (not eobp)
5938                (or (not gnus-summary-check-current)
5939                    (not unread)
5940                    (not (gnus-data-unread-p (car arts)))))
5941       (setq arts (cdr arts)))
5942     (when (setq result
5943                 (if unread
5944                     (progn
5945                       (while arts
5946                         (when (gnus-data-unread-p (car arts))
5947                           (setq result (car arts)
5948                                 arts nil))
5949                         (setq arts (cdr arts)))
5950                       result)
5951                   (car arts)))
5952       (goto-char (gnus-data-pos result))
5953       (gnus-data-number result))))
5954
5955 (defun gnus-summary-find-subject (subject &optional unread backward article)
5956   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5957          (article (or article (gnus-summary-article-number)))
5958          (articles (gnus-data-list backward))
5959          (arts (gnus-data-find-list article articles))
5960          result)
5961     (when (or (not gnus-summary-check-current)
5962               (not unread)
5963               (not (gnus-data-unread-p (car arts))))
5964       (setq arts (cdr arts)))
5965     (while arts
5966       (and (or (not unread)
5967                (gnus-data-unread-p (car arts)))
5968            (vectorp (gnus-data-header (car arts)))
5969            (gnus-subject-equal
5970             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5971            (setq result (car arts)
5972                  arts nil))
5973       (setq arts (cdr arts)))
5974     (and result
5975          (goto-char (gnus-data-pos result))
5976          (gnus-data-number result))))
5977
5978 (defun gnus-summary-search-forward (&optional unread subject backward)
5979   "Search forward for an article.
5980 If UNREAD, look for unread articles.  If SUBJECT, look for
5981 articles with that subject.  If BACKWARD, search backward instead."
5982   (cond (subject (gnus-summary-find-subject subject unread backward))
5983         (backward (gnus-summary-find-prev unread))
5984         (t (gnus-summary-find-next unread))))
5985
5986 (defun gnus-recenter (&optional n)
5987   "Center point in window and redisplay frame.
5988 Also do horizontal recentering."
5989   (interactive "P")
5990   (when (and gnus-auto-center-summary
5991              (not (eq gnus-auto-center-summary 'vertical)))
5992     (gnus-horizontal-recenter))
5993   (recenter n))
5994
5995 (defun gnus-summary-recenter ()
5996   "Center point in the summary window.
5997 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5998 displayed, no centering will be performed."
5999   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6000 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6001   (interactive)
6002   (let* ((top (cond ((< (window-height) 4) 0)
6003                     ((< (window-height) 7) 1)
6004                     (t (if (numberp gnus-auto-center-summary)
6005                            gnus-auto-center-summary
6006                          2))))
6007          (height (1- (window-height)))
6008          (bottom (save-excursion (goto-char (point-max))
6009                                  (forward-line (- height))
6010                                  (point)))
6011          (window (get-buffer-window (current-buffer))))
6012     ;; The user has to want it.
6013     (when gnus-auto-center-summary
6014       (when (get-buffer-window gnus-article-buffer)
6015         ;; Only do recentering when the article buffer is displayed,
6016       ;; Set the window start to either `bottom', which is the biggest
6017         ;; possible valid number, or the second line from the top,
6018         ;; whichever is the least.
6019         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6020           (if (> bottom top-pos)
6021               ;; Keep the second line from the top visible
6022               (set-window-start window top-pos t)
6023             ;; Try to keep the bottom line visible; if it's partially
6024             ;; obscured, either scroll one more line to make it fully
6025             ;; visible, or revert to using TOP-POS.
6026             (save-excursion
6027               (goto-char (point-max))
6028               (forward-line -1)
6029               (let ((last-line-start (point)))
6030                 (goto-char bottom)
6031                 (set-window-start window (point) t)
6032                 (when (not (pos-visible-in-window-p last-line-start window))
6033                   (forward-line 1)
6034                   (set-window-start window (min (point) top-pos) t)))))))
6035       ;; Do horizontal recentering while we're at it.
6036       (when (and (get-buffer-window (current-buffer) t)
6037                  (not (eq gnus-auto-center-summary 'vertical)))
6038         (let ((selected (selected-window)))
6039           (select-window (get-buffer-window (current-buffer) t))
6040           (gnus-summary-position-point)
6041           (gnus-horizontal-recenter)
6042           (select-window selected))))))
6043
6044 (defun gnus-summary-jump-to-group (newsgroup)
6045   "Move point to NEWSGROUP in group mode buffer."
6046   ;; Keep update point of group mode buffer if visible.
6047   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6048       (save-window-excursion
6049         ;; Take care of tree window mode.
6050         (when (get-buffer-window gnus-group-buffer)
6051           (pop-to-buffer gnus-group-buffer))
6052         (gnus-group-jump-to-group newsgroup))
6053     (save-excursion
6054       ;; Take care of tree window mode.
6055       (if (get-buffer-window gnus-group-buffer)
6056           (pop-to-buffer gnus-group-buffer)
6057         (set-buffer gnus-group-buffer))
6058       (gnus-group-jump-to-group newsgroup))))
6059
6060 ;; This function returns a list of article numbers based on the
6061 ;; difference between the ranges of read articles in this group and
6062 ;; the range of active articles.
6063 (defun gnus-list-of-unread-articles (group)
6064   (let* ((read (gnus-info-read (gnus-get-info group)))
6065          (active (or (gnus-active group) (gnus-activate-group group)))
6066          (last (cdr active))
6067          first nlast unread)
6068     ;; If none are read, then all are unread.
6069     (if (not read)
6070         (setq first (car active))
6071       ;; If the range of read articles is a single range, then the
6072       ;; first unread article is the article after the last read
6073       ;; article.  Sounds logical, doesn't it?
6074       (if (and (not (listp (cdr read)))
6075                (or (< (car read) (car active))
6076                    (progn (setq read (list read))
6077                           nil)))
6078           (setq first (max (car active) (1+ (cdr read))))
6079         ;; `read' is a list of ranges.
6080         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6081                                   (caar read)))
6082                   1)
6083           (setq first (car active)))
6084         (while read
6085           (when first
6086             (while (< first nlast)
6087               (push first unread)
6088               (setq first (1+ first))))
6089           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6090           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6091           (setq read (cdr read)))))
6092     ;; And add the last unread articles.
6093     (while (<= first last)
6094       (push first unread)
6095       (setq first (1+ first)))
6096     ;; Return the list of unread articles.
6097     (delq 0 (nreverse unread))))
6098
6099 (defun gnus-list-of-read-articles (group)
6100   "Return a list of unread, unticked and non-dormant articles."
6101   (let* ((info (gnus-get-info group))
6102          (marked (gnus-info-marks info))
6103          (active (gnus-active group)))
6104     (and info active
6105          (gnus-list-range-difference
6106           (gnus-list-range-difference
6107            (gnus-sorted-complement
6108             (gnus-uncompress-range active)
6109             (gnus-list-of-unread-articles group))
6110            (cdr (assq 'dormant marked)))
6111           (cdr (assq 'tick marked))))))
6112
6113 ;; Various summary commands
6114
6115 (defun gnus-summary-select-article-buffer ()
6116   "Reconfigure windows to show article buffer."
6117   (interactive)
6118   (if (not (gnus-buffer-live-p gnus-article-buffer))
6119       (error "There is no article buffer for this summary buffer")
6120     (gnus-configure-windows 'article)
6121     (select-window (get-buffer-window gnus-article-buffer))))
6122
6123 (defun gnus-summary-universal-argument (arg)
6124   "Perform any operation on all articles that are process/prefixed."
6125   (interactive "P")
6126   (let ((articles (gnus-summary-work-articles arg))
6127         func article)
6128     (if (eq
6129          (setq
6130           func
6131           (key-binding
6132            (read-key-sequence
6133             (substitute-command-keys
6134              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6135          'undefined)
6136         (gnus-error 1 "Undefined key")
6137       (save-excursion
6138         (while articles
6139           (gnus-summary-goto-subject (setq article (pop articles)))
6140           (let (gnus-newsgroup-processable)
6141             (command-execute func))
6142           (gnus-summary-remove-process-mark article)))))
6143   (gnus-summary-position-point))
6144
6145 (defun gnus-summary-toggle-truncation (&optional arg)
6146   "Toggle truncation of summary lines.
6147 With arg, turn line truncation on iff arg is positive."
6148   (interactive "P")
6149   (setq truncate-lines
6150         (if (null arg) (not truncate-lines)
6151           (> (prefix-numeric-value arg) 0)))
6152   (redraw-display))
6153
6154 (defun gnus-summary-reselect-current-group (&optional all rescan)
6155   "Exit and then reselect the current newsgroup.
6156 The prefix argument ALL means to select all articles."
6157   (interactive "P")
6158   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6159     (error "Ephemeral groups can't be reselected"))
6160   (let ((current-subject (gnus-summary-article-number))
6161         (group gnus-newsgroup-name))
6162     (setq gnus-newsgroup-begin nil)
6163     (gnus-summary-exit)
6164     ;; We have to adjust the point of group mode buffer because
6165     ;; point was moved to the next unread newsgroup by exiting.
6166     (gnus-summary-jump-to-group group)
6167     (when rescan
6168       (save-excursion
6169         (gnus-group-get-new-news-this-group 1)))
6170     (gnus-group-read-group all t)
6171     (gnus-summary-goto-subject current-subject nil t)))
6172
6173 (defun gnus-summary-rescan-group (&optional all)
6174   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6175   (interactive "P")
6176   (gnus-summary-reselect-current-group all t))
6177
6178 (defun gnus-summary-update-info (&optional non-destructive)
6179   (save-excursion
6180     (let ((group gnus-newsgroup-name))
6181       (when group
6182         (when gnus-newsgroup-kill-headers
6183           (setq gnus-newsgroup-killed
6184                 (gnus-compress-sequence
6185                  (gnus-sorted-union
6186                   (gnus-list-range-intersection
6187                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6188                   gnus-newsgroup-unreads)
6189                  t)))
6190         (unless (listp (cdr gnus-newsgroup-killed))
6191           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6192         (let ((headers gnus-newsgroup-headers))
6193           ;; Set the new ranges of read articles.
6194           (save-excursion
6195             (set-buffer gnus-group-buffer)
6196             (gnus-undo-force-boundary))
6197           (gnus-update-read-articles
6198            group (gnus-sorted-union
6199                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6200           ;; Set the current article marks.
6201           (let ((gnus-newsgroup-scored
6202                  (if (and (not gnus-save-score)
6203                           (not non-destructive))
6204                      nil
6205                    gnus-newsgroup-scored)))
6206             (save-excursion
6207               (gnus-update-marks)))
6208           ;; Do the cross-ref thing.
6209           (when gnus-use-cross-reference
6210             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6211           ;; Do not switch windows but change the buffer to work.
6212           (set-buffer gnus-group-buffer)
6213           (unless (gnus-ephemeral-group-p group)
6214             (gnus-group-update-group group)))))))
6215
6216 (defun gnus-summary-save-newsrc (&optional force)
6217   "Save the current number of read/marked articles in the dribble buffer.
6218 The dribble buffer will then be saved.
6219 If FORCE (the prefix), also save the .newsrc file(s)."
6220   (interactive "P")
6221   (gnus-summary-update-info t)
6222   (if force
6223       (gnus-save-newsrc-file)
6224     (gnus-dribble-save)))
6225
6226 (defun gnus-summary-exit (&optional temporary)
6227   "Exit reading current newsgroup, and then return to group selection mode.
6228 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6229   (interactive)
6230   (gnus-set-global-variables)
6231   (when (gnus-buffer-live-p gnus-article-buffer)
6232     (save-excursion
6233       (set-buffer gnus-article-buffer)
6234       (mm-destroy-parts gnus-article-mime-handles)
6235       ;; Set it to nil for safety reason.
6236       (setq gnus-article-mime-handle-alist nil)
6237       (setq gnus-article-mime-handles nil)))
6238   (gnus-kill-save-kill-buffer)
6239   (gnus-async-halt-prefetch)
6240   (let* ((group gnus-newsgroup-name)
6241          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6242          (mode major-mode)
6243          (group-point nil)
6244          (buf (current-buffer)))
6245     (unless quit-config
6246       ;; Do adaptive scoring, and possibly save score files.
6247       (when gnus-newsgroup-adaptive
6248         (gnus-score-adaptive))
6249       (when gnus-use-scoring
6250         (gnus-score-save)))
6251     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6252     ;; If we have several article buffers, we kill them at exit.
6253     (unless gnus-single-article-buffer
6254       (gnus-kill-buffer gnus-original-article-buffer)
6255       (setq gnus-article-current nil))
6256     (when gnus-use-cache
6257       (gnus-cache-possibly-remove-articles)
6258       (gnus-cache-save-buffers))
6259     (gnus-async-prefetch-remove-group group)
6260     (when gnus-suppress-duplicates
6261       (gnus-dup-enter-articles))
6262     (when gnus-use-trees
6263       (gnus-tree-close group))
6264     (when gnus-use-cache
6265       (gnus-cache-write-active))
6266     ;; Remove entries for this group.
6267     (nnmail-purge-split-history (gnus-group-real-name group))
6268     ;; Make all changes in this group permanent.
6269     (unless quit-config
6270       (gnus-run-hooks 'gnus-exit-group-hook)
6271       (gnus-summary-update-info))
6272     (gnus-close-group group)
6273     ;; Make sure where we were, and go to next newsgroup.
6274     (set-buffer gnus-group-buffer)
6275     (unless quit-config
6276       (gnus-group-jump-to-group group))
6277     (gnus-run-hooks 'gnus-summary-exit-hook)
6278     (unless (or quit-config
6279                 ;; If this group has disappeared from the summary
6280                 ;; buffer, don't skip forwards.
6281                 (not (string= group (gnus-group-group-name))))
6282       (gnus-group-next-unread-group 1))
6283     (setq group-point (point))
6284     (if temporary
6285         nil                             ;Nothing to do.
6286       ;; If we have several article buffers, we kill them at exit.
6287       (unless gnus-single-article-buffer
6288         (gnus-kill-buffer gnus-article-buffer)
6289         (gnus-kill-buffer gnus-original-article-buffer)
6290         (setq gnus-article-current nil))
6291       (set-buffer buf)
6292       (if (not gnus-kill-summary-on-exit)
6293           (progn
6294             (gnus-deaden-summary)
6295             (setq mode nil))
6296        ;; We set all buffer-local variables to nil.  It is unclear why
6297         ;; this is needed, but if we don't, buffer-local variables are
6298         ;; not garbage-collected, it seems.  This would the lead to en
6299         ;; ever-growing Emacs.
6300         (gnus-summary-clear-local-variables)
6301         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6302           (gnus-summary-clear-local-variables))
6303         (when (get-buffer gnus-article-buffer)
6304           (bury-buffer gnus-article-buffer))
6305         ;; We clear the global counterparts of the buffer-local
6306         ;; variables as well, just to be on the safe side.
6307         (set-buffer gnus-group-buffer)
6308         (gnus-summary-clear-local-variables)
6309         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6310           (gnus-summary-clear-local-variables)))
6311       (setq gnus-current-select-method gnus-select-method)
6312       (pop-to-buffer gnus-group-buffer)
6313       (if (not quit-config)
6314           (progn
6315             (goto-char group-point)
6316             (gnus-configure-windows 'group 'force))
6317         (gnus-handle-ephemeral-exit quit-config))
6318       ;; Return to group mode buffer.
6319       (when (eq mode 'gnus-summary-mode)
6320         (gnus-kill-buffer buf))
6321       ;; Clear the current group name.
6322       (unless quit-config
6323         (setq gnus-newsgroup-name nil)))))
6324
6325 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6326 (defun gnus-summary-exit-no-update (&optional no-questions)
6327   "Quit reading current newsgroup without updating read article info."
6328   (interactive)
6329   (let* ((group gnus-newsgroup-name)
6330          (quit-config (gnus-group-quit-config group)))
6331     (when (or no-questions
6332               gnus-expert-user
6333               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6334       (gnus-async-halt-prefetch)
6335       (mapcar 'funcall
6336               (delq 'gnus-summary-expire-articles
6337                     (copy-sequence gnus-summary-prepare-exit-hook)))
6338       (when (gnus-buffer-live-p gnus-article-buffer)
6339         (save-excursion
6340           (set-buffer gnus-article-buffer)
6341           (mm-destroy-parts gnus-article-mime-handles)
6342           ;; Set it to nil for safety reason.
6343           (setq gnus-article-mime-handle-alist nil)
6344           (setq gnus-article-mime-handles nil)))
6345       ;; If we have several article buffers, we kill them at exit.
6346       (unless gnus-single-article-buffer
6347         (gnus-kill-buffer gnus-article-buffer)
6348         (gnus-kill-buffer gnus-original-article-buffer)
6349         (setq gnus-article-current nil))
6350       (if (not gnus-kill-summary-on-exit)
6351           (gnus-deaden-summary)
6352         (gnus-close-group group)
6353         (gnus-summary-clear-local-variables)
6354         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6355           (gnus-summary-clear-local-variables))
6356         (set-buffer gnus-group-buffer)
6357         (gnus-summary-clear-local-variables)
6358         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6359           (gnus-summary-clear-local-variables))
6360         (when (get-buffer gnus-summary-buffer)
6361           (kill-buffer gnus-summary-buffer)))
6362       (unless gnus-single-article-buffer
6363         (setq gnus-article-current nil))
6364       (when gnus-use-trees
6365         (gnus-tree-close group))
6366       (gnus-async-prefetch-remove-group group)
6367       (when (get-buffer gnus-article-buffer)
6368         (bury-buffer gnus-article-buffer))
6369       ;; Return to the group buffer.
6370       (gnus-configure-windows 'group 'force)
6371       ;; Clear the current group name.
6372       (setq gnus-newsgroup-name nil)
6373       (when (equal (gnus-group-group-name) group)
6374         (gnus-group-next-unread-group 1))
6375       (when quit-config
6376         (gnus-handle-ephemeral-exit quit-config)))))
6377
6378 (defun gnus-handle-ephemeral-exit (quit-config)
6379   "Handle movement when leaving an ephemeral group.
6380 The state which existed when entering the ephemeral is reset."
6381   (if (not (buffer-name (car quit-config)))
6382       (gnus-configure-windows 'group 'force)
6383     (set-buffer (car quit-config))
6384     (cond ((eq major-mode 'gnus-summary-mode)
6385            (gnus-set-global-variables))
6386           ((eq major-mode 'gnus-article-mode)
6387            (save-excursion
6388              ;; The `gnus-summary-buffer' variable may point
6389              ;; to the old summary buffer when using a single
6390              ;; article buffer.
6391              (unless (gnus-buffer-live-p gnus-summary-buffer)
6392                (set-buffer gnus-group-buffer))
6393              (set-buffer gnus-summary-buffer)
6394              (gnus-set-global-variables))))
6395     (if (or (eq (cdr quit-config) 'article)
6396             (eq (cdr quit-config) 'pick))
6397         (progn
6398           ;; The current article may be from the ephemeral group
6399           ;; thus it is best that we reload this article
6400           (gnus-summary-show-article)
6401           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6402               (gnus-configure-windows 'pick 'force)
6403             (gnus-configure-windows (cdr quit-config) 'force)))
6404       (gnus-configure-windows (cdr quit-config) 'force))
6405     (when (eq major-mode 'gnus-summary-mode)
6406       (gnus-summary-next-subject 1 nil t)
6407       (gnus-summary-recenter)
6408       (gnus-summary-position-point))))
6409
6410 ;;; Dead summaries.
6411
6412 (defvar gnus-dead-summary-mode-map nil)
6413
6414 (unless gnus-dead-summary-mode-map
6415   (setq gnus-dead-summary-mode-map (make-keymap))
6416   (suppress-keymap gnus-dead-summary-mode-map)
6417   (substitute-key-definition
6418    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6419   (dolist (key '("\C-d" "\r" "\177" [delete]))
6420     (define-key gnus-dead-summary-mode-map
6421       key 'gnus-summary-wake-up-the-dead))
6422   (dolist (key '("q" "Q"))
6423     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6424
6425 (defvar gnus-dead-summary-mode nil
6426   "Minor mode for Gnus summary buffers.")
6427
6428 (defun gnus-dead-summary-mode (&optional arg)
6429   "Minor mode for Gnus summary buffers."
6430   (interactive "P")
6431   (when (eq major-mode 'gnus-summary-mode)
6432     (make-local-variable 'gnus-dead-summary-mode)
6433     (setq gnus-dead-summary-mode
6434           (if (null arg) (not gnus-dead-summary-mode)
6435             (> (prefix-numeric-value arg) 0)))
6436     (when gnus-dead-summary-mode
6437       (gnus-add-minor-mode
6438        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6439
6440 (defun gnus-deaden-summary ()
6441   "Make the current summary buffer into a dead summary buffer."
6442   ;; Kill any previous dead summary buffer.
6443   (when (and gnus-dead-summary
6444              (buffer-name gnus-dead-summary))
6445     (save-excursion
6446       (set-buffer gnus-dead-summary)
6447       (when gnus-dead-summary-mode
6448         (kill-buffer (current-buffer)))))
6449   ;; Make this the current dead summary.
6450   (setq gnus-dead-summary (current-buffer))
6451   (gnus-dead-summary-mode 1)
6452   (let ((name (buffer-name)))
6453     (when (string-match "Summary" name)
6454       (rename-buffer
6455        (concat (substring name 0 (match-beginning 0)) "Dead "
6456                (substring name (match-beginning 0)))
6457        t)
6458       (bury-buffer))))
6459
6460 (defun gnus-kill-or-deaden-summary (buffer)
6461   "Kill or deaden the summary BUFFER."
6462   (save-excursion
6463     (when (and (buffer-name buffer)
6464                (not gnus-single-article-buffer))
6465       (save-excursion
6466         (set-buffer buffer)
6467         (gnus-kill-buffer gnus-article-buffer)
6468         (gnus-kill-buffer gnus-original-article-buffer)))
6469     (cond
6470      ;; Kill the buffer.
6471      (gnus-kill-summary-on-exit
6472       (when (and gnus-use-trees
6473                  (gnus-buffer-exists-p buffer))
6474         (save-excursion
6475           (set-buffer buffer)
6476           (gnus-tree-close gnus-newsgroup-name)))
6477       (gnus-kill-buffer buffer))
6478      ;; Deaden the buffer.
6479      ((gnus-buffer-exists-p buffer)
6480       (save-excursion
6481         (set-buffer buffer)
6482         (gnus-deaden-summary))))))
6483
6484 (defun gnus-summary-wake-up-the-dead (&rest args)
6485   "Wake up the dead summary buffer."
6486   (interactive)
6487   (gnus-dead-summary-mode -1)
6488   (let ((name (buffer-name)))
6489     (when (string-match "Dead " name)
6490       (rename-buffer
6491        (concat (substring name 0 (match-beginning 0))
6492                (substring name (match-end 0)))
6493        t)))
6494   (gnus-message 3 "This dead summary is now alive again"))
6495
6496 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6497 (defun gnus-summary-fetch-faq (&optional faq-dir)
6498   "Fetch the FAQ for the current group.
6499 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6500 in."
6501   (interactive
6502    (list
6503     (when current-prefix-arg
6504       (completing-read
6505        "Faq dir: " (and (listp gnus-group-faq-directory)
6506                         (mapcar (lambda (file) (list file))
6507                                 gnus-group-faq-directory))))))
6508   (let (gnus-faq-buffer)
6509     (when (setq gnus-faq-buffer
6510                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6511       (gnus-configure-windows 'summary-faq))))
6512
6513 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6514 (defun gnus-summary-describe-group (&optional force)
6515   "Describe the current newsgroup."
6516   (interactive "P")
6517   (gnus-group-describe-group force gnus-newsgroup-name))
6518
6519 (defun gnus-summary-describe-briefly ()
6520   "Describe summary mode commands briefly."
6521   (interactive)
6522   (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")))
6523
6524 ;; Walking around group mode buffer from summary mode.
6525
6526 (defun gnus-summary-next-group (&optional no-article target-group backward)
6527   "Exit current newsgroup and then select next unread newsgroup.
6528 If prefix argument NO-ARTICLE is non-nil, no article is selected
6529 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6530 previous group instead."
6531   (interactive "P")
6532   ;; Stop pre-fetching.
6533   (gnus-async-halt-prefetch)
6534   (let ((current-group gnus-newsgroup-name)
6535         (current-buffer (current-buffer))
6536         entered)
6537     (while (not entered)
6538       ;; Then we find what group we are supposed to enter.
6539       (set-buffer gnus-group-buffer)
6540       (gnus-group-jump-to-group current-group)
6541       (setq target-group
6542             (or target-group
6543                 (if (eq gnus-keep-same-level 'best)
6544                     (gnus-summary-best-group gnus-newsgroup-name)
6545                   (gnus-summary-search-group backward gnus-keep-same-level))))
6546       (if (not target-group)
6547           ;; There are no further groups, so we return to the group
6548           ;; buffer.
6549           (progn
6550             (gnus-message 5 "Returning to the group buffer")
6551             (setq entered t)
6552             (when (gnus-buffer-live-p current-buffer)
6553               (set-buffer current-buffer)
6554               (gnus-summary-exit))
6555             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6556         ;; We try to enter the target group.
6557         (gnus-group-jump-to-group target-group)
6558         (let ((unreads (gnus-group-group-unread)))
6559           (if (and (or (eq t unreads)
6560                        (and unreads (not (zerop unreads))))
6561                    (progn
6562                      ;; Now we semi-exit this group to update Xrefs
6563                      ;; and all variables.  We can't do a real exit,
6564                      ;; because the window conf must remain the same
6565                      ;; in case the user is prompted for info, and we
6566                      ;; don't want the window conf to change before
6567                      ;; that...
6568                      (when (gnus-buffer-live-p current-buffer)
6569                        (set-buffer current-buffer)
6570                        (gnus-summary-exit t))
6571                      (gnus-summary-read-group
6572                       target-group nil no-article
6573                       (and (buffer-name current-buffer) current-buffer)
6574                       nil backward)))
6575               (setq entered t)
6576             (setq current-group target-group
6577                   target-group nil)))))))
6578
6579 (defun gnus-summary-prev-group (&optional no-article)
6580   "Exit current newsgroup and then select previous unread newsgroup.
6581 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6582   (interactive "P")
6583   (gnus-summary-next-group no-article nil t))
6584
6585 ;; Walking around summary lines.
6586
6587 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6588   "Go to the first unread subject.
6589 If UNREAD is non-nil, go to the first unread article.
6590 Returns the article selected or nil if there are no unread articles."
6591   (interactive "P")
6592   (prog1
6593       (cond
6594        ;; Empty summary.
6595        ((null gnus-newsgroup-data)
6596         (gnus-message 3 "No articles in the group")
6597         nil)
6598        ;; Pick the first article.
6599        ((not unread)
6600         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6601         (gnus-data-number (car gnus-newsgroup-data)))
6602        ;; No unread articles.
6603        ((null gnus-newsgroup-unreads)
6604         (gnus-message 3 "No more unread articles")
6605         nil)
6606        ;; Find the first unread article.
6607        (t
6608         (let ((data gnus-newsgroup-data))
6609           (while (and data
6610                       (and (not (and undownloaded
6611                                      (eq gnus-undownloaded-mark
6612                                          (gnus-data-mark (car data)))))
6613                            (if unseen
6614                                (or (not (memq
6615                                          (gnus-data-number (car data))
6616                                          gnus-newsgroup-unseen))
6617                                    (not (gnus-data-unread-p (car data))))
6618                              (not (gnus-data-unread-p (car data))))))
6619             (setq data (cdr data)))
6620           (when data
6621             (goto-char (gnus-data-pos (car data)))
6622             (gnus-data-number (car data))))))
6623     (gnus-summary-position-point)))
6624
6625 (defun gnus-summary-next-subject (n &optional unread dont-display)
6626   "Go to next N'th summary line.
6627 If N is negative, go to the previous N'th subject line.
6628 If UNREAD is non-nil, only unread articles are selected.
6629 The difference between N and the actual number of steps taken is
6630 returned."
6631   (interactive "p")
6632   (let ((backward (< n 0))
6633         (n (abs n)))
6634     (while (and (> n 0)
6635                 (if backward
6636                     (gnus-summary-find-prev unread)
6637                   (gnus-summary-find-next unread)))
6638       (unless (zerop (setq n (1- n)))
6639         (gnus-summary-show-thread)))
6640     (when (/= 0 n)
6641       (gnus-message 7 "No more%s articles"
6642                     (if unread " unread" "")))
6643     (unless dont-display
6644       (gnus-summary-recenter)
6645       (gnus-summary-position-point))
6646     n))
6647
6648 (defun gnus-summary-next-unread-subject (n)
6649   "Go to next N'th unread summary line."
6650   (interactive "p")
6651   (gnus-summary-next-subject n t))
6652
6653 (defun gnus-summary-prev-subject (n &optional unread)
6654   "Go to previous N'th summary line.
6655 If optional argument UNREAD is non-nil, only unread article is selected."
6656   (interactive "p")
6657   (gnus-summary-next-subject (- n) unread))
6658
6659 (defun gnus-summary-prev-unread-subject (n)
6660   "Go to previous N'th unread summary line."
6661   (interactive "p")
6662   (gnus-summary-next-subject (- n) t))
6663
6664 (defun gnus-summary-goto-subject (article &optional force silent)
6665   "Go the subject line of ARTICLE.
6666 If FORCE, also allow jumping to articles not currently shown."
6667   (interactive "nArticle number: ")
6668   (unless (numberp article)
6669     (error "Article %s is not a number" article))
6670   (let ((b (point))
6671         (data (gnus-data-find article)))
6672     ;; We read in the article if we have to.
6673     (and (not data)
6674          force
6675          (gnus-summary-insert-subject
6676           article
6677           (if (or (numberp force) (vectorp force)) force)
6678           t)
6679          (setq data (gnus-data-find article)))
6680     (goto-char b)
6681     (if (not data)
6682         (progn
6683           (unless silent
6684             (gnus-message 3 "Can't find article %d" article))
6685           nil)
6686       (let ((pt (gnus-data-pos data)))
6687         (goto-char pt)
6688         (gnus-summary-set-article-display-arrow pt))
6689       (gnus-summary-position-point)
6690       article)))
6691
6692 ;; Walking around summary lines with displaying articles.
6693
6694 (defun gnus-summary-expand-window (&optional arg)
6695   "Make the summary buffer take up the entire Emacs frame.
6696 Given a prefix, will force an `article' buffer configuration."
6697   (interactive "P")
6698   (if arg
6699       (gnus-configure-windows 'article 'force)
6700     (gnus-configure-windows 'summary 'force)))
6701
6702 (defun gnus-summary-display-article (article &optional all-header)
6703   "Display ARTICLE in article buffer."
6704   (when (gnus-buffer-live-p gnus-article-buffer)
6705     (with-current-buffer gnus-article-buffer
6706       (mm-enable-multibyte)))
6707   (gnus-set-global-variables)
6708   (when (gnus-buffer-live-p gnus-article-buffer)
6709     (with-current-buffer gnus-article-buffer
6710       (setq gnus-article-charset gnus-newsgroup-charset)
6711       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6712       (mm-enable-multibyte)))
6713   (if (null article)
6714       nil
6715     (prog1
6716         (if gnus-summary-display-article-function
6717             (funcall gnus-summary-display-article-function article all-header)
6718           (gnus-article-prepare article all-header))
6719       (gnus-run-hooks 'gnus-select-article-hook)
6720       (when (and gnus-current-article
6721                  (not (zerop gnus-current-article)))
6722         (gnus-summary-goto-subject gnus-current-article))
6723       (gnus-summary-recenter)
6724       (when (and gnus-use-trees gnus-show-threads)
6725         (gnus-possibly-generate-tree article)
6726         (gnus-highlight-selected-tree article))
6727       ;; Successfully display article.
6728       (gnus-article-set-window-start
6729        (cdr (assq article gnus-newsgroup-bookmarks))))))
6730
6731 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6732   "Select the current article.
6733 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6734 non-nil, the article will be re-fetched even if it already present in
6735 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6736 be displayed."
6737   ;; Make sure we are in the summary buffer to work around bbdb bug.
6738   (unless (eq major-mode 'gnus-summary-mode)
6739     (set-buffer gnus-summary-buffer))
6740   (let ((article (or article (gnus-summary-article-number)))
6741         (all-headers (not (not all-headers))) ;Must be t or nil.
6742         gnus-summary-display-article-function)
6743     (and (not pseudo)
6744          (gnus-summary-article-pseudo-p article)
6745          (error "This is a pseudo-article"))
6746     (save-excursion
6747       (set-buffer gnus-summary-buffer)
6748       (if (or (and gnus-single-article-buffer
6749                    (or (null gnus-current-article)
6750                        (null gnus-article-current)
6751                        (null (get-buffer gnus-article-buffer))
6752                        (not (eq article (cdr gnus-article-current)))
6753                        (not (equal (car gnus-article-current)
6754                                    gnus-newsgroup-name))))
6755               (and (not gnus-single-article-buffer)
6756                    (or (null gnus-current-article)
6757                        (not (eq gnus-current-article article))))
6758               force)
6759           ;; The requested article is different from the current article.
6760           (progn
6761             (gnus-summary-display-article article all-headers)
6762             (when (gnus-buffer-live-p gnus-article-buffer)
6763               (with-current-buffer gnus-article-buffer
6764                 (if (not gnus-article-decoded-p) ;; a local variable
6765                     (mm-disable-multibyte))))
6766             (gnus-article-set-window-start
6767              (cdr (assq article gnus-newsgroup-bookmarks)))
6768             article)
6769         'old))))
6770
6771 (defun gnus-summary-force-verify-and-decrypt ()
6772   (interactive)
6773   (let ((mm-verify-option 'known)
6774         (mm-decrypt-option 'known)
6775         (gnus-buttonized-mime-types (append (list "multipart/signed"
6776                                                   "multipart/encrypted")
6777                                             gnus-buttonized-mime-types)))
6778     (gnus-summary-select-article nil 'force)))
6779
6780 (defun gnus-summary-set-current-mark (&optional current-mark)
6781   "Obsolete function."
6782   nil)
6783
6784 (defun gnus-summary-next-article (&optional unread subject backward push)
6785   "Select the next article.
6786 If UNREAD, only unread articles are selected.
6787 If SUBJECT, only articles with SUBJECT are selected.
6788 If BACKWARD, the previous article is selected instead of the next."
6789   (interactive "P")
6790   (cond
6791    ;; Is there such an article?
6792    ((and (gnus-summary-search-forward unread subject backward)
6793          (or (gnus-summary-display-article (gnus-summary-article-number))
6794              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6795     (gnus-summary-position-point))
6796    ;; If not, we try the first unread, if that is wanted.
6797    ((and subject
6798          gnus-auto-select-same
6799          (gnus-summary-first-unread-article))
6800     (gnus-summary-position-point)
6801     (gnus-message 6 "Wrapped"))
6802    ;; Try to get next/previous article not displayed in this group.
6803    ((and gnus-auto-extend-newsgroup
6804          (not unread) (not subject))
6805     (gnus-summary-goto-article
6806      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6807      nil (count-lines (point-min) (point))))
6808    ;; Go to next/previous group.
6809    (t
6810     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6811       (gnus-summary-jump-to-group gnus-newsgroup-name))
6812     (let ((cmd last-command-char)
6813           (point
6814            (save-excursion
6815              (set-buffer gnus-group-buffer)
6816              (point)))
6817           (group
6818            (if (eq gnus-keep-same-level 'best)
6819                (gnus-summary-best-group gnus-newsgroup-name)
6820              (gnus-summary-search-group backward gnus-keep-same-level))))
6821       ;; For some reason, the group window gets selected.  We change
6822       ;; it back.
6823       (select-window (get-buffer-window (current-buffer)))
6824       ;; Select next unread newsgroup automagically.
6825       (cond
6826        ((or (not gnus-auto-select-next)
6827             (not cmd))
6828         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6829        ((or (eq gnus-auto-select-next 'quietly)
6830             (and (eq gnus-auto-select-next 'slightly-quietly)
6831                  push)
6832             (and (eq gnus-auto-select-next 'almost-quietly)
6833                  (gnus-summary-last-article-p)))
6834         ;; Select quietly.
6835         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6836             (gnus-summary-exit)
6837           (gnus-message 7 "No more%s articles (%s)..."
6838                         (if unread " unread" "")
6839                         (if group (concat "selecting " group)
6840                           "exiting"))
6841           (gnus-summary-next-group nil group backward)))
6842        (t
6843         (when (gnus-key-press-event-p last-input-event)
6844           (gnus-summary-walk-group-buffer
6845            gnus-newsgroup-name cmd unread backward point))))))))
6846
6847 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6848   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6849                       (?\C-p (gnus-group-prev-unread-group 1))))
6850         (cursor-in-echo-area t)
6851         keve key group ended)
6852     (save-excursion
6853       (set-buffer gnus-group-buffer)
6854       (goto-char start)
6855       (setq group
6856             (if (eq gnus-keep-same-level 'best)
6857                 (gnus-summary-best-group gnus-newsgroup-name)
6858               (gnus-summary-search-group backward gnus-keep-same-level))))
6859     (while (not ended)
6860       (gnus-message
6861        5 "No more%s articles%s" (if unread " unread" "")
6862        (if (and group
6863                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6864            (format " (Type %s for %s [%s])"
6865                    (single-key-description cmd) group
6866                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6867          (format " (Type %s to exit %s)"
6868                  (single-key-description cmd)
6869                  gnus-newsgroup-name)))
6870       ;; Confirm auto selection.
6871       (setq key (car (setq keve (gnus-read-event-char))))
6872       (setq ended t)
6873       (cond
6874        ((assq key keystrokes)
6875         (let ((obuf (current-buffer)))
6876           (switch-to-buffer gnus-group-buffer)
6877           (when group
6878             (gnus-group-jump-to-group group))
6879           (eval (cadr (assq key keystrokes)))
6880           (setq group (gnus-group-group-name))
6881           (switch-to-buffer obuf))
6882         (setq ended nil))
6883        ((equal key cmd)
6884         (if (or (not group)
6885                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6886             (gnus-summary-exit)
6887           (gnus-summary-next-group nil group backward)))
6888        (t
6889         (push (cdr keve) unread-command-events))))))
6890
6891 (defun gnus-summary-next-unread-article ()
6892   "Select unread article after current one."
6893   (interactive)
6894   (gnus-summary-next-article
6895    (or (not (eq gnus-summary-goto-unread 'never))
6896        (gnus-summary-last-article-p (gnus-summary-article-number)))
6897    (and gnus-auto-select-same
6898         (gnus-summary-article-subject))))
6899
6900 (defun gnus-summary-prev-article (&optional unread subject)
6901   "Select the article after the current one.
6902 If UNREAD is non-nil, only unread articles are selected."
6903   (interactive "P")
6904   (gnus-summary-next-article unread subject t))
6905
6906 (defun gnus-summary-prev-unread-article ()
6907   "Select unread article before current one."
6908   (interactive)
6909   (gnus-summary-prev-article
6910    (or (not (eq gnus-summary-goto-unread 'never))
6911        (gnus-summary-first-article-p (gnus-summary-article-number)))
6912    (and gnus-auto-select-same
6913         (gnus-summary-article-subject))))
6914
6915 (defun gnus-summary-next-page (&optional lines circular)
6916   "Show next page of the selected article.
6917 If at the end of the current article, select the next article.
6918 LINES says how many lines should be scrolled up.
6919
6920 If CIRCULAR is non-nil, go to the start of the article instead of
6921 selecting the next article when reaching the end of the current
6922 article."
6923   (interactive "P")
6924   (setq gnus-summary-buffer (current-buffer))
6925   (gnus-set-global-variables)
6926   (let ((article (gnus-summary-article-number))
6927         (article-window (get-buffer-window gnus-article-buffer t))
6928         endp)
6929     ;; If the buffer is empty, we have no article.
6930     (unless article
6931       (error "No article to select"))
6932     (gnus-configure-windows 'article)
6933     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6934         (if (and (eq gnus-summary-goto-unread 'never)
6935                  (not (gnus-summary-last-article-p article)))
6936             (gnus-summary-next-article)
6937           (gnus-summary-next-unread-article))
6938       (if (or (null gnus-current-article)
6939               (null gnus-article-current)
6940               (/= article (cdr gnus-article-current))
6941               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6942           ;; Selected subject is different from current article's.
6943           (gnus-summary-display-article article)
6944         (when article-window
6945           (gnus-eval-in-buffer-window gnus-article-buffer
6946             (setq endp (gnus-article-next-page lines)))
6947           (when endp
6948             (cond (circular
6949                    (gnus-summary-beginning-of-article))
6950                   (lines
6951                    (gnus-message 3 "End of message"))
6952                   ((null lines)
6953                    (if (and (eq gnus-summary-goto-unread 'never)
6954                             (not (gnus-summary-last-article-p article)))
6955                        (gnus-summary-next-article)
6956                      (gnus-summary-next-unread-article))))))))
6957     (gnus-summary-recenter)
6958     (gnus-summary-position-point)))
6959
6960 (defun gnus-summary-prev-page (&optional lines move)
6961   "Show previous page of selected article.
6962 Argument LINES specifies lines to be scrolled down.
6963 If MOVE, move to the previous unread article if point is at
6964 the beginning of the buffer."
6965   (interactive "P")
6966   (let ((article (gnus-summary-article-number))
6967         (article-window (get-buffer-window gnus-article-buffer t))
6968         endp)
6969     (gnus-configure-windows 'article)
6970     (if (or (null gnus-current-article)
6971             (null gnus-article-current)
6972             (/= article (cdr gnus-article-current))
6973             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6974         ;; Selected subject is different from current article's.
6975         (gnus-summary-display-article article)
6976       (gnus-summary-recenter)
6977       (when article-window
6978         (gnus-eval-in-buffer-window gnus-article-buffer
6979           (setq endp (gnus-article-prev-page lines)))
6980         (when (and move endp)
6981           (cond (lines
6982                  (gnus-message 3 "Beginning of message"))
6983                 ((null lines)
6984                  (if (and (eq gnus-summary-goto-unread 'never)
6985                           (not (gnus-summary-first-article-p article)))
6986                      (gnus-summary-prev-article)
6987                    (gnus-summary-prev-unread-article))))))))
6988   (gnus-summary-position-point))
6989
6990 (defun gnus-summary-prev-page-or-article (&optional lines)
6991   "Show previous page of selected article.
6992 Argument LINES specifies lines to be scrolled down.
6993 If at the beginning of the article, go to the next article."
6994   (interactive "P")
6995   (gnus-summary-prev-page lines t))
6996
6997 (defun gnus-summary-scroll-up (lines)
6998   "Scroll up (or down) one line current article.
6999 Argument LINES specifies lines to be scrolled up (or down if negative)."
7000   (interactive "p")
7001   (gnus-configure-windows 'article)
7002   (gnus-summary-show-thread)
7003   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7004     (gnus-eval-in-buffer-window gnus-article-buffer
7005       (cond ((> lines 0)
7006              (when (gnus-article-next-page lines)
7007                (gnus-message 3 "End of message")))
7008             ((< lines 0)
7009              (gnus-article-prev-page (- lines))))))
7010   (gnus-summary-recenter)
7011   (gnus-summary-position-point))
7012
7013 (defun gnus-summary-scroll-down (lines)
7014   "Scroll down (or up) one line current article.
7015 Argument LINES specifies lines to be scrolled down (or up if negative)."
7016   (interactive "p")
7017   (gnus-summary-scroll-up (- lines)))
7018
7019 (defun gnus-summary-next-same-subject ()
7020   "Select next article which has the same subject as current one."
7021   (interactive)
7022   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7023
7024 (defun gnus-summary-prev-same-subject ()
7025   "Select previous article which has the same subject as current one."
7026   (interactive)
7027   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7028
7029 (defun gnus-summary-next-unread-same-subject ()
7030   "Select next unread article which has the same subject as current one."
7031   (interactive)
7032   (gnus-summary-next-article t (gnus-summary-article-subject)))
7033
7034 (defun gnus-summary-prev-unread-same-subject ()
7035   "Select previous unread article which has the same subject as current one."
7036   (interactive)
7037   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7038
7039 (defun gnus-summary-first-unread-article ()
7040   "Select the first unread article.
7041 Return nil if there are no unread articles."
7042   (interactive)
7043   (prog1
7044       (when (gnus-summary-first-subject t)
7045         (gnus-summary-show-thread)
7046         (gnus-summary-first-subject t)
7047         (gnus-summary-display-article (gnus-summary-article-number)))
7048     (gnus-summary-position-point)))
7049
7050 (defun gnus-summary-first-unread-subject ()
7051   "Place the point on the subject line of the first unread article.
7052 Return nil if there are no unread articles."
7053   (interactive)
7054   (prog1
7055       (when (gnus-summary-first-subject t)
7056         (gnus-summary-show-thread)
7057         (gnus-summary-first-subject t))
7058     (gnus-summary-position-point)))
7059
7060 (defun gnus-summary-first-unseen-subject ()
7061   "Place the point on the subject line of the first unseen article.
7062 Return nil if there are no unseen articles."
7063   (interactive)
7064   (prog1
7065       (when (gnus-summary-first-subject t t t)
7066         (gnus-summary-show-thread)
7067         (gnus-summary-first-subject t t t))
7068     (gnus-summary-position-point)))
7069
7070 (defun gnus-summary-first-unseen-or-unread-subject ()
7071   "Place the point on the subject line of the first unseen article.
7072 Return nil if there are no unseen articles."
7073   (interactive)
7074   (prog1
7075       (unless (when (gnus-summary-first-subject t t t)
7076                 (gnus-summary-show-thread)
7077                 (gnus-summary-first-subject t t t))
7078         (when (gnus-summary-first-subject t)
7079           (gnus-summary-show-thread)
7080           (gnus-summary-first-subject t)))
7081     (gnus-summary-position-point)))
7082
7083 (defun gnus-summary-first-article ()
7084   "Select the first article.
7085 Return nil if there are no articles."
7086   (interactive)
7087   (prog1
7088       (when (gnus-summary-first-subject)
7089         (gnus-summary-show-thread)
7090         (gnus-summary-first-subject)
7091         (gnus-summary-display-article (gnus-summary-article-number)))
7092     (gnus-summary-position-point)))
7093
7094 (defun gnus-summary-best-unread-article (&optional arg)
7095   "Select the unread article with the highest score.
7096 If given a prefix argument, select the next unread article that has a
7097 score higher than the default score."
7098   (interactive "P")
7099   (let ((article (if arg
7100                      (gnus-summary-better-unread-subject)
7101                    (gnus-summary-best-unread-subject))))
7102     (if article
7103         (gnus-summary-goto-article article)
7104       (error "No unread articles"))))
7105
7106 (defun gnus-summary-best-unread-subject ()
7107   "Select the unread subject with the highest score."
7108   (interactive)
7109   (let ((best -1000000)
7110         (data gnus-newsgroup-data)
7111         article score)
7112     (while data
7113       (and (gnus-data-unread-p (car data))
7114            (> (setq score
7115                     (gnus-summary-article-score (gnus-data-number (car data))))
7116               best)
7117            (setq best score
7118                  article (gnus-data-number (car data))))
7119       (setq data (cdr data)))
7120     (when article
7121       (gnus-summary-goto-subject article))
7122     (gnus-summary-position-point)
7123     article))
7124
7125 (defun gnus-summary-better-unread-subject ()
7126   "Select the first unread subject that has a score over the default score."
7127   (interactive)
7128   (let ((data gnus-newsgroup-data)
7129         article score)
7130     (while (and (setq article (gnus-data-number (car data)))
7131                 (or (gnus-data-read-p (car data))
7132                     (not (> (gnus-summary-article-score article)
7133                             gnus-summary-default-score))))
7134       (setq data (cdr data)))
7135     (when article
7136       (gnus-summary-goto-subject article))
7137     (gnus-summary-position-point)
7138     article))
7139
7140 (defun gnus-summary-last-subject ()
7141   "Go to the last displayed subject line in the group."
7142   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7143     (when article
7144       (gnus-summary-goto-subject article))))
7145
7146 (defun gnus-summary-goto-article (article &optional all-headers force)
7147   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7148 If ALL-HEADERS is non-nil, no header lines are hidden.
7149 If FORCE, go to the article even if it isn't displayed.  If FORCE
7150 is a number, it is the line the article is to be displayed on."
7151   (interactive
7152    (list
7153     (completing-read
7154      "Article number or Message-ID: "
7155      (mapcar (lambda (number) (list (int-to-string number)))
7156              gnus-newsgroup-limit))
7157     current-prefix-arg
7158     t))
7159   (prog1
7160       (if (and (stringp article)
7161                (string-match "@" article))
7162           (gnus-summary-refer-article article)
7163         (when (stringp article)
7164           (setq article (string-to-number article)))
7165         (if (gnus-summary-goto-subject article force)
7166             (gnus-summary-display-article article all-headers)
7167           (gnus-message 4 "Couldn't go to article %s" article) nil))
7168     (gnus-summary-position-point)))
7169
7170 (defun gnus-summary-goto-last-article ()
7171   "Go to the previously read article."
7172   (interactive)
7173   (prog1
7174       (when gnus-last-article
7175         (gnus-summary-goto-article gnus-last-article nil t))
7176     (gnus-summary-position-point)))
7177
7178 (defun gnus-summary-pop-article (number)
7179   "Pop one article off the history and go to the previous.
7180 NUMBER articles will be popped off."
7181   (interactive "p")
7182   (let (to)
7183     (setq gnus-newsgroup-history
7184           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7185     (if to
7186         (gnus-summary-goto-article (car to) nil t)
7187       (error "Article history empty")))
7188   (gnus-summary-position-point))
7189
7190 ;; Summary commands and functions for limiting the summary buffer.
7191
7192 (defun gnus-summary-limit-to-articles (n)
7193   "Limit the summary buffer to the next N articles.
7194 If not given a prefix, use the process marked articles instead."
7195   (interactive "P")
7196   (prog1
7197       (let ((articles (gnus-summary-work-articles n)))
7198         (setq gnus-newsgroup-processable nil)
7199         (gnus-summary-limit articles))
7200     (gnus-summary-position-point)))
7201
7202 (defun gnus-summary-pop-limit (&optional total)
7203   "Restore the previous limit.
7204 If given a prefix, remove all limits."
7205   (interactive "P")
7206   (when total
7207     (setq gnus-newsgroup-limits
7208           (list (mapcar (lambda (h) (mail-header-number h))
7209                         gnus-newsgroup-headers))))
7210   (unless gnus-newsgroup-limits
7211     (error "No limit to pop"))
7212   (prog1
7213       (gnus-summary-limit nil 'pop)
7214     (gnus-summary-position-point)))
7215
7216 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7217   "Limit the summary buffer to articles that have subjects that match a regexp.
7218 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7219   (interactive
7220    (list (read-string (if current-prefix-arg
7221                           "Exclude subject (regexp): "
7222                         "Limit to subject (regexp): "))
7223          nil current-prefix-arg))
7224   (unless header
7225     (setq header "subject"))
7226   (when (not (equal "" subject))
7227     (prog1
7228         (let ((articles (gnus-summary-find-matching
7229                          (or header "subject") subject 'all nil nil
7230                          not-matching)))
7231           (unless articles
7232             (error "Found no matches for \"%s\"" subject))
7233           (gnus-summary-limit articles))
7234       (gnus-summary-position-point))))
7235
7236 (defun gnus-summary-limit-to-author (from &optional not-matching)
7237   "Limit the summary buffer to articles that have authors that match a regexp.
7238 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7239   (interactive
7240    (list (read-string (if current-prefix-arg
7241                           "Exclude author (regexp): "
7242                         "Limit to author (regexp): "))
7243          current-prefix-arg))
7244   (gnus-summary-limit-to-subject from "from" not-matching))
7245
7246 (defun gnus-summary-limit-to-age (age &optional younger-p)
7247   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7248 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7249 articles that are younger than AGE days."
7250   (interactive
7251    (let ((younger current-prefix-arg)
7252          (days-got nil)
7253          days)
7254      (while (not days-got)
7255        (setq days (if younger
7256                       (read-string "Limit to articles within (in days): ")
7257                     (read-string "Limit to articles older than (in days): ")))
7258        (when (> (length days) 0)
7259          (setq days (read days)))
7260        (if (numberp days)
7261            (progn
7262              (setq days-got t)
7263              (if (< days 0)
7264                  (progn
7265                    (setq younger (not younger))
7266                    (setq days (* days -1)))))
7267          (message "Please enter a number.")
7268          (sleep-for 1)))
7269      (list days younger)))
7270   (prog1
7271       (let ((data gnus-newsgroup-data)
7272             (cutoff (days-to-time age))
7273             articles d date is-younger)
7274         (while (setq d (pop data))
7275           (when (and (vectorp (gnus-data-header d))
7276                      (setq date (mail-header-date (gnus-data-header d))))
7277             (setq is-younger (time-less-p
7278                               (time-since (condition-case ()
7279                                               (date-to-time date)
7280                                             (error '(0 0))))
7281                               cutoff))
7282             (when (if younger-p
7283                       is-younger
7284                     (not is-younger))
7285               (push (gnus-data-number d) articles))))
7286         (gnus-summary-limit (nreverse articles)))
7287     (gnus-summary-position-point)))
7288
7289 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7290   "Limit the summary buffer to articles that match an 'extra' header."
7291   (interactive
7292    (let ((header
7293           (intern
7294            (gnus-completing-read-with-default
7295             (symbol-name (car gnus-extra-headers))
7296             (if current-prefix-arg
7297                 "Exclude extra header:"
7298               "Limit extra header:")
7299             (mapcar (lambda (x)
7300                       (cons (symbol-name x) x))
7301                     gnus-extra-headers)
7302             nil
7303             t))))
7304      (list header
7305            (read-string (format "%s header %s (regexp): "
7306                                 (if current-prefix-arg "Exclude" "Limit to")
7307                                 header))
7308            current-prefix-arg)))
7309   (when (not (equal "" regexp))
7310     (prog1
7311         (let ((articles (gnus-summary-find-matching
7312                          (cons 'extra header) regexp 'all nil nil
7313                          not-matching)))
7314           (unless articles
7315             (error "Found no matches for \"%s\"" regexp))
7316           (gnus-summary-limit articles))
7317       (gnus-summary-position-point))))
7318
7319 (defun gnus-summary-limit-to-display-predicate ()
7320   "Limit the summary buffer to the predicated in the `display' group parameter."
7321   (interactive)
7322   (unless gnus-newsgroup-display
7323     (error "There is no `display' group parameter"))
7324   (let (articles)
7325     (dolist (number gnus-newsgroup-articles)
7326       (when (funcall gnus-newsgroup-display)
7327         (push number articles)))
7328     (gnus-summary-limit articles))
7329   (gnus-summary-position-point))
7330
7331 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7332 (make-obsolete
7333  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7334
7335 (defun gnus-summary-limit-to-unread (&optional all)
7336   "Limit the summary buffer to articles that are not marked as read.
7337 If ALL is non-nil, limit strictly to unread articles."
7338   (interactive "P")
7339   (if all
7340       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7341     (gnus-summary-limit-to-marks
7342      ;; Concat all the marks that say that an article is read and have
7343      ;; those removed.
7344      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7345            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7346            gnus-low-score-mark gnus-expirable-mark
7347            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7348            gnus-duplicate-mark gnus-souped-mark)
7349      'reverse)))
7350
7351 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7352 (make-obsolete 'gnus-summary-delete-marked-with
7353                'gnus-summary-limit-exclude-marks)
7354
7355 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7356   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7357 If REVERSE, limit the summary buffer to articles that are marked
7358 with MARKS.  MARKS can either be a string of marks or a list of marks.
7359 Returns how many articles were removed."
7360   (interactive "sMarks: ")
7361   (gnus-summary-limit-to-marks marks t))
7362
7363 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7364   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7365 If REVERSE (the prefix), limit the summary buffer to articles that are
7366 not marked with MARKS.  MARKS can either be a string of marks or a
7367 list of marks.
7368 Returns how many articles were removed."
7369   (interactive "sMarks: \nP")
7370   (prog1
7371       (let ((data gnus-newsgroup-data)
7372             (marks (if (listp marks) marks
7373                      (append marks nil))) ; Transform to list.
7374             articles)
7375         (while data
7376           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7377                   (memq (gnus-data-mark (car data)) marks))
7378             (push (gnus-data-number (car data)) articles))
7379           (setq data (cdr data)))
7380         (gnus-summary-limit articles))
7381     (gnus-summary-position-point)))
7382
7383 (defun gnus-summary-limit-to-score (score)
7384   "Limit to articles with score at or above SCORE."
7385   (interactive "NLimit to articles with score of at least: ")
7386   (let ((data gnus-newsgroup-data)
7387         articles)
7388     (while data
7389       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7390                 score)
7391         (push (gnus-data-number (car data)) articles))
7392       (setq data (cdr data)))
7393     (prog1
7394         (gnus-summary-limit articles)
7395       (gnus-summary-position-point))))
7396
7397 (defun gnus-summary-limit-to-unseen ()
7398   "Limit to unseen articles."
7399   (interactive)
7400   (prog1
7401       (gnus-summary-limit gnus-newsgroup-unseen)
7402     (gnus-summary-position-point)))
7403
7404 (defun gnus-summary-limit-include-thread (id)
7405   "Display all the hidden articles that is in the thread with ID in it.
7406 When called interactively, ID is the Message-ID of the current
7407 article."
7408   (interactive (list (mail-header-id (gnus-summary-article-header))))
7409   (let ((articles (gnus-articles-in-thread
7410                    (gnus-id-to-thread (gnus-root-id id)))))
7411     (prog1
7412         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7413       (gnus-summary-limit-include-matching-articles
7414        "subject"
7415        (regexp-quote (gnus-simplify-subject-re
7416                       (mail-header-subject (gnus-id-to-header id)))))
7417       (gnus-summary-position-point))))
7418
7419 (defun gnus-summary-limit-include-matching-articles (header regexp)
7420   "Display all the hidden articles that have HEADERs that match REGEXP."
7421   (interactive (list (read-string "Match on header: ")
7422                      (read-string "Regexp: ")))
7423   (let ((articles (gnus-find-matching-articles header regexp)))
7424     (prog1
7425         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7426       (gnus-summary-position-point))))
7427
7428 (defun gnus-summary-limit-include-dormant ()
7429   "Display all the hidden articles that are marked as dormant.
7430 Note that this command only works on a subset of the articles currently
7431 fetched for this group."
7432   (interactive)
7433   (unless gnus-newsgroup-dormant
7434     (error "There are no dormant articles in this group"))
7435   (prog1
7436       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7437     (gnus-summary-position-point)))
7438
7439 (defun gnus-summary-limit-exclude-dormant ()
7440   "Hide all dormant articles."
7441   (interactive)
7442   (prog1
7443       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7444     (gnus-summary-position-point)))
7445
7446 (defun gnus-summary-limit-exclude-childless-dormant ()
7447   "Hide all dormant articles that have no children."
7448   (interactive)
7449   (let ((data (gnus-data-list t))
7450         articles d children)
7451     ;; Find all articles that are either not dormant or have
7452     ;; children.
7453     (while (setq d (pop data))
7454       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7455                 (and (setq children
7456                            (gnus-article-children (gnus-data-number d)))
7457                      (let (found)
7458                        (while children
7459                          (when (memq (car children) articles)
7460                            (setq children nil
7461                                  found t))
7462                          (pop children))
7463                        found)))
7464         (push (gnus-data-number d) articles)))
7465     ;; Do the limiting.
7466     (prog1
7467         (gnus-summary-limit articles)
7468       (gnus-summary-position-point))))
7469
7470 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7471   "Mark all unread excluded articles as read.
7472 If ALL, mark even excluded ticked and dormants as read."
7473   (interactive "P")
7474   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7475   (let ((articles (gnus-sorted-ndifference
7476                    (sort
7477                     (mapcar (lambda (h) (mail-header-number h))
7478                             gnus-newsgroup-headers)
7479                     '<)
7480                    gnus-newsgroup-limit))
7481         article)
7482     (setq gnus-newsgroup-unreads
7483           (gnus-sorted-intersection gnus-newsgroup-unreads
7484                                     gnus-newsgroup-limit))
7485     (if all
7486         (setq gnus-newsgroup-dormant nil
7487               gnus-newsgroup-marked nil
7488               gnus-newsgroup-reads
7489               (nconc
7490                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7491                gnus-newsgroup-reads))
7492       (while (setq article (pop articles))
7493         (unless (or (memq article gnus-newsgroup-dormant)
7494                     (memq article gnus-newsgroup-marked))
7495           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7496
7497 (defun gnus-summary-limit (articles &optional pop)
7498   (if pop
7499       ;; We pop the previous limit off the stack and use that.
7500       (setq articles (car gnus-newsgroup-limits)
7501             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7502     ;; We use the new limit, so we push the old limit on the stack.
7503     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7504   ;; Set the limit.
7505   (setq gnus-newsgroup-limit articles)
7506   (let ((total (length gnus-newsgroup-data))
7507         (data (gnus-data-find-list (gnus-summary-article-number)))
7508         (gnus-summary-mark-below nil)   ; Inhibit this.
7509         found)
7510     ;; This will do all the work of generating the new summary buffer
7511     ;; according to the new limit.
7512     (gnus-summary-prepare)
7513     ;; Hide any threads, possibly.
7514     (gnus-summary-maybe-hide-threads)
7515     ;; Try to return to the article you were at, or one in the
7516     ;; neighborhood.
7517     (when data
7518       ;; We try to find some article after the current one.
7519       (while data
7520         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7521           (setq data nil
7522                 found t))
7523         (setq data (cdr data))))
7524     (unless found
7525       ;; If there is no data, that means that we were after the last
7526       ;; article.  The same goes when we can't find any articles
7527       ;; after the current one.
7528       (goto-char (point-max))
7529       (gnus-summary-find-prev))
7530     (gnus-set-mode-line 'summary)
7531     ;; We return how many articles were removed from the summary
7532     ;; buffer as a result of the new limit.
7533     (- total (length gnus-newsgroup-data))))
7534
7535 (defsubst gnus-invisible-cut-children (threads)
7536   (let ((num 0))
7537     (while threads
7538       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7539         (incf num))
7540       (pop threads))
7541     (< num 2)))
7542
7543 (defsubst gnus-cut-thread (thread)
7544   "Go forwards in the thread until we find an article that we want to display."
7545   (when (or (eq gnus-fetch-old-headers 'some)
7546             (eq gnus-fetch-old-headers 'invisible)
7547             (numberp gnus-fetch-old-headers)
7548             (eq gnus-build-sparse-threads 'some)
7549             (eq gnus-build-sparse-threads 'more))
7550     ;; Deal with old-fetched headers and sparse threads.
7551     (while (and
7552             thread
7553             (or
7554              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7555              (gnus-summary-article-ancient-p
7556               (mail-header-number (car thread))))
7557             (if (or (<= (length (cdr thread)) 1)
7558                     (eq gnus-fetch-old-headers 'invisible))
7559                 (setq gnus-newsgroup-limit
7560                       (delq (mail-header-number (car thread))
7561                             gnus-newsgroup-limit)
7562                       thread (cadr thread))
7563               (when (gnus-invisible-cut-children (cdr thread))
7564                 (let ((th (cdr thread)))
7565                   (while th
7566                     (if (memq (mail-header-number (caar th))
7567                               gnus-newsgroup-limit)
7568                         (setq thread (car th)
7569                               th nil)
7570                       (setq th (cdr th))))))))))
7571   thread)
7572
7573 (defun gnus-cut-threads (threads)
7574   "Cut off all uninteresting articles from the beginning of threads."
7575   (when (or (eq gnus-fetch-old-headers 'some)
7576             (eq gnus-fetch-old-headers 'invisible)
7577             (numberp gnus-fetch-old-headers)
7578             (eq gnus-build-sparse-threads 'some)
7579             (eq gnus-build-sparse-threads 'more))
7580     (let ((th threads))
7581       (while th
7582         (setcar th (gnus-cut-thread (car th)))
7583         (setq th (cdr th)))))
7584   ;; Remove nixed out threads.
7585   (delq nil threads))
7586
7587 (defun gnus-summary-initial-limit (&optional show-if-empty)
7588   "Figure out what the initial limit is supposed to be on group entry.
7589 This entails weeding out unwanted dormants, low-scored articles,
7590 fetch-old-headers verbiage, and so on."
7591   ;; Most groups have nothing to remove.
7592   (if (or gnus-inhibit-limiting
7593           (and (null gnus-newsgroup-dormant)
7594                (eq gnus-newsgroup-display 'gnus-not-ignore)
7595                (not (eq gnus-fetch-old-headers 'some))
7596                (not (numberp gnus-fetch-old-headers))
7597                (not (eq gnus-fetch-old-headers 'invisible))
7598                (null gnus-summary-expunge-below)
7599                (not (eq gnus-build-sparse-threads 'some))
7600                (not (eq gnus-build-sparse-threads 'more))
7601                (null gnus-thread-expunge-below)
7602                (not gnus-use-nocem)))
7603       ()                                ; Do nothing.
7604     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7605     (setq gnus-newsgroup-limit nil)
7606     (mapatoms
7607      (lambda (node)
7608        (unless (car (symbol-value node))
7609          ;; These threads have no parents -- they are roots.
7610          (let ((nodes (cdr (symbol-value node)))
7611                thread)
7612            (while nodes
7613              (if (and gnus-thread-expunge-below
7614                       (< (gnus-thread-total-score (car nodes))
7615                          gnus-thread-expunge-below))
7616                  (gnus-expunge-thread (pop nodes))
7617                (setq thread (pop nodes))
7618                (gnus-summary-limit-children thread))))))
7619      gnus-newsgroup-dependencies)
7620     ;; If this limitation resulted in an empty group, we might
7621     ;; pop the previous limit and use it instead.
7622     (when (and (not gnus-newsgroup-limit)
7623                show-if-empty)
7624       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7625     gnus-newsgroup-limit))
7626
7627 (defun gnus-summary-limit-children (thread)
7628   "Return 1 if this subthread is visible and 0 if it is not."
7629   ;; First we get the number of visible children to this thread.  This
7630   ;; is done by recursing down the thread using this function, so this
7631   ;; will really go down to a leaf article first, before slowly
7632   ;; working its way up towards the root.
7633   (when thread
7634     (let ((children
7635            (if (cdr thread)
7636                (apply '+ (mapcar 'gnus-summary-limit-children
7637                                  (cdr thread)))
7638              0))
7639           (number (mail-header-number (car thread)))
7640           score)
7641       (if (and
7642            (not (memq number gnus-newsgroup-marked))
7643            (or
7644             ;; If this article is dormant and has absolutely no visible
7645             ;; children, then this article isn't visible.
7646             (and (memq number gnus-newsgroup-dormant)
7647                  (zerop children))
7648             ;; If this is "fetch-old-headered" and there is no
7649             ;; visible children, then we don't want this article.
7650             (and (or (eq gnus-fetch-old-headers 'some)
7651                      (numberp gnus-fetch-old-headers))
7652                  (gnus-summary-article-ancient-p number)
7653                  (zerop children))
7654             ;; If this is "fetch-old-headered" and `invisible', then
7655             ;; we don't want this article.
7656             (and (eq gnus-fetch-old-headers 'invisible)
7657                  (gnus-summary-article-ancient-p number))
7658             ;; If this is a sparsely inserted article with no children,
7659             ;; we don't want it.
7660             (and (eq gnus-build-sparse-threads 'some)
7661                  (gnus-summary-article-sparse-p number)
7662                  (zerop children))
7663             ;; If we use expunging, and this article is really
7664             ;; low-scored, then we don't want this article.
7665             (when (and gnus-summary-expunge-below
7666                        (< (setq score
7667                                 (or (cdr (assq number gnus-newsgroup-scored))
7668                                     gnus-summary-default-score))
7669                           gnus-summary-expunge-below))
7670               ;; We increase the expunge-tally here, but that has
7671               ;; nothing to do with the limits, really.
7672               (incf gnus-newsgroup-expunged-tally)
7673               ;; We also mark as read here, if that's wanted.
7674               (when (and gnus-summary-mark-below
7675                          (< score gnus-summary-mark-below))
7676                 (setq gnus-newsgroup-unreads
7677                       (delq number gnus-newsgroup-unreads))
7678                 (if gnus-newsgroup-auto-expire
7679                     (push number gnus-newsgroup-expirable)
7680                   (push (cons number gnus-low-score-mark)
7681                         gnus-newsgroup-reads)))
7682               t)
7683             ;; Do the `display' group parameter.
7684             (and gnus-newsgroup-display
7685                  (not (funcall gnus-newsgroup-display)))
7686             ;; Check NoCeM things.
7687             (if (and gnus-use-nocem
7688                      (gnus-nocem-unwanted-article-p
7689                       (mail-header-id (car thread))))
7690                 (progn
7691                   (setq gnus-newsgroup-unreads
7692                         (delq number gnus-newsgroup-unreads))
7693                   t))))
7694           ;; Nope, invisible article.
7695           0
7696         ;; Ok, this article is to be visible, so we add it to the limit
7697         ;; and return 1.
7698         (push number gnus-newsgroup-limit)
7699         1))))
7700
7701 (defun gnus-expunge-thread (thread)
7702   "Mark all articles in THREAD as read."
7703   (let* ((number (mail-header-number (car thread))))
7704     (incf gnus-newsgroup-expunged-tally)
7705     ;; We also mark as read here, if that's wanted.
7706     (setq gnus-newsgroup-unreads
7707           (delq number gnus-newsgroup-unreads))
7708     (if gnus-newsgroup-auto-expire
7709         (push number gnus-newsgroup-expirable)
7710       (push (cons number gnus-low-score-mark)
7711             gnus-newsgroup-reads)))
7712   ;; Go recursively through all subthreads.
7713   (mapcar 'gnus-expunge-thread (cdr thread)))
7714
7715 ;; Summary article oriented commands
7716
7717 (defun gnus-summary-refer-parent-article (n)
7718   "Refer parent article N times.
7719 If N is negative, go to ancestor -N instead.
7720 The difference between N and the number of articles fetched is returned."
7721   (interactive "p")
7722   (let ((skip 1)
7723         error header ref)
7724     (when (not (natnump n))
7725       (setq skip (abs n)
7726             n 1))
7727     (while (and (> n 0)
7728                 (not error))
7729       (setq header (gnus-summary-article-header))
7730       (if (and (eq (mail-header-number header)
7731                    (cdr gnus-article-current))
7732                (equal gnus-newsgroup-name
7733                       (car gnus-article-current)))
7734           ;; If we try to find the parent of the currently
7735           ;; displayed article, then we take a look at the actual
7736           ;; References header, since this is slightly more
7737           ;; reliable than the References field we got from the
7738           ;; server.
7739           (save-excursion
7740             (set-buffer gnus-original-article-buffer)
7741             (nnheader-narrow-to-headers)
7742             (unless (setq ref (message-fetch-field "references"))
7743               (setq ref (message-fetch-field "in-reply-to")))
7744             (widen))
7745         (setq ref
7746               ;; It's not the current article, so we take a bet on
7747               ;; the value we got from the server.
7748               (mail-header-references header)))
7749       (if (and ref
7750                (not (equal ref "")))
7751           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7752             (gnus-message 1 "Couldn't find parent"))
7753         (gnus-message 1 "No references in article %d"
7754                       (gnus-summary-article-number))
7755         (setq error t))
7756       (decf n))
7757     (gnus-summary-position-point)
7758     n))
7759
7760 (defun gnus-summary-refer-references ()
7761   "Fetch all articles mentioned in the References header.
7762 Return the number of articles fetched."
7763   (interactive)
7764   (let ((ref (mail-header-references (gnus-summary-article-header)))
7765         (current (gnus-summary-article-number))
7766         (n 0))
7767     (if (or (not ref)
7768             (equal ref ""))
7769         (error "No References in the current article")
7770       ;; For each Message-ID in the References header...
7771       (while (string-match "<[^>]*>" ref)
7772         (incf n)
7773         ;; ... fetch that article.
7774         (gnus-summary-refer-article
7775          (prog1 (match-string 0 ref)
7776            (setq ref (substring ref (match-end 0))))))
7777       (gnus-summary-goto-subject current)
7778       (gnus-summary-position-point)
7779       n)))
7780
7781 (defun gnus-summary-refer-thread (&optional limit)
7782   "Fetch all articles in the current thread.
7783 If LIMIT (the numerical prefix), fetch that many old headers instead
7784 of what's specified by the `gnus-refer-thread-limit' variable."
7785   (interactive "P")
7786   (let ((id (mail-header-id (gnus-summary-article-header)))
7787         (limit (if limit (prefix-numeric-value limit)
7788                  gnus-refer-thread-limit)))
7789     ;; We want to fetch LIMIT *old* headers, but we also have to
7790     ;; re-fetch all the headers in the current buffer, because many of
7791     ;; them may be undisplayed.  So we adjust LIMIT.
7792     (when (numberp limit)
7793       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7794     (unless (eq gnus-fetch-old-headers 'invisible)
7795       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7796       ;; Retrieve the headers and read them in.
7797       (if (eq (gnus-retrieve-headers
7798                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7799               'nov)
7800           (gnus-build-all-threads)
7801         (error "Can't fetch thread from backends that don't support NOV"))
7802       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7803     (gnus-summary-limit-include-thread id)))
7804
7805 (defun gnus-summary-refer-article (message-id)
7806   "Fetch an article specified by MESSAGE-ID."
7807   (interactive "sMessage-ID: ")
7808   (when (and (stringp message-id)
7809              (not (zerop (length message-id))))
7810     ;; Construct the correct Message-ID if necessary.
7811     ;; Suggested by tale@pawl.rpi.edu.
7812     (unless (string-match "^<" message-id)
7813       (setq message-id (concat "<" message-id)))
7814     (unless (string-match ">$" message-id)
7815       (setq message-id (concat message-id ">")))
7816     (let* ((header (gnus-id-to-header message-id))
7817            (sparse (and header
7818                         (gnus-summary-article-sparse-p
7819                          (mail-header-number header))
7820                         (memq (mail-header-number header)
7821                               gnus-newsgroup-limit)))
7822            number)
7823       (cond
7824        ;; If the article is present in the buffer we just go to it.
7825        ((and header
7826              (or (not (gnus-summary-article-sparse-p
7827                        (mail-header-number header)))
7828                  sparse))
7829         (prog1
7830             (gnus-summary-goto-article
7831              (mail-header-number header) nil t)
7832           (when sparse
7833             (gnus-summary-update-article (mail-header-number header)))))
7834        (t
7835         ;; We fetch the article.
7836         (catch 'found
7837           (dolist (gnus-override-method (gnus-refer-article-methods))
7838             (gnus-check-server gnus-override-method)
7839             ;; Fetch the header, and display the article.
7840             (when (setq number (gnus-summary-insert-subject message-id))
7841               (gnus-summary-select-article nil nil nil number)
7842               (throw 'found t)))
7843           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7844
7845 (defun gnus-refer-article-methods ()
7846   "Return a list of referrable methods."
7847   (cond
7848    ;; No method, so we default to current and native.
7849    ((null gnus-refer-article-method)
7850     (list gnus-current-select-method gnus-select-method))
7851    ;; Current.
7852    ((eq 'current gnus-refer-article-method)
7853     (list gnus-current-select-method))
7854    ;; List of select methods.
7855    ((not (and (symbolp (car gnus-refer-article-method))
7856               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7857     (let (out)
7858       (dolist (method gnus-refer-article-method)
7859         (push (if (eq 'current method)
7860                   gnus-current-select-method
7861                 method)
7862               out))
7863       (nreverse out)))
7864    ;; One single select method.
7865    (t
7866     (list gnus-refer-article-method))))
7867
7868 (defun gnus-summary-edit-parameters ()
7869   "Edit the group parameters of the current group."
7870   (interactive)
7871   (gnus-group-edit-group gnus-newsgroup-name 'params))
7872
7873 (defun gnus-summary-customize-parameters ()
7874   "Customize the group parameters of the current group."
7875   (interactive)
7876   (gnus-group-customize gnus-newsgroup-name))
7877
7878 (defun gnus-summary-enter-digest-group (&optional force)
7879   "Enter an nndoc group based on the current article.
7880 If FORCE, force a digest interpretation.  If not, try
7881 to guess what the document format is."
7882   (interactive "P")
7883   (let ((conf gnus-current-window-configuration))
7884     (save-excursion
7885       (gnus-summary-select-article))
7886     (setq gnus-current-window-configuration conf)
7887     (let* ((name (format "%s-%d"
7888                          (gnus-group-prefixed-name
7889                           gnus-newsgroup-name (list 'nndoc ""))
7890                          (save-excursion
7891                            (set-buffer gnus-summary-buffer)
7892                            gnus-current-article)))
7893            (ogroup gnus-newsgroup-name)
7894            (params (append (gnus-info-params (gnus-get-info ogroup))
7895                            (list (cons 'to-group ogroup))
7896                            (list (cons 'save-article-group ogroup))))
7897            (case-fold-search t)
7898            (buf (current-buffer))
7899            dig to-address)
7900       (save-excursion
7901         (set-buffer gnus-original-article-buffer)
7902         ;; Have the digest group inherit the main mail address of
7903         ;; the parent article.
7904         (when (setq to-address (or (gnus-fetch-field "reply-to")
7905                                    (gnus-fetch-field "from")))
7906           (setq params (append
7907                         (list (cons 'to-address
7908                                     (funcall gnus-decode-encoded-word-function
7909                                              to-address))))))
7910         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7911         (insert-buffer-substring gnus-original-article-buffer)
7912         ;; Remove lines that may lead nndoc to misinterpret the
7913         ;; document type.
7914         (narrow-to-region
7915          (goto-char (point-min))
7916          (or (search-forward "\n\n" nil t) (point)))
7917         (goto-char (point-min))
7918         (delete-matching-lines "^Path:\\|^From ")
7919         (widen))
7920       (unwind-protect
7921           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7922                     (gnus-newsgroup-ephemeral-ignored-charsets
7923                      gnus-newsgroup-ignored-charsets))
7924                 (gnus-group-read-ephemeral-group
7925                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7926                               (nndoc-article-type
7927                                ,(if force 'mbox 'guess)))
7928                  t nil nil nil
7929                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7930                                                         "ADAPT")))))
7931               ;; Make all postings to this group go to the parent group.
7932               (nconc (gnus-info-params (gnus-get-info name))
7933                      params)
7934             ;; Couldn't select this doc group.
7935             (switch-to-buffer buf)
7936             (gnus-set-global-variables)
7937             (gnus-configure-windows 'summary)
7938             (gnus-message 3 "Article couldn't be entered?"))
7939         (kill-buffer dig)))))
7940
7941 (defun gnus-summary-read-document (n)
7942   "Open a new group based on the current article(s).
7943 This will allow you to read digests and other similar
7944 documents as newsgroups.
7945 Obeys the standard process/prefix convention."
7946   (interactive "P")
7947   (let* ((articles (gnus-summary-work-articles n))
7948          (ogroup gnus-newsgroup-name)
7949          (params (append (gnus-info-params (gnus-get-info ogroup))
7950                          (list (cons 'to-group ogroup))))
7951          article group egroup groups vgroup)
7952     (while (setq article (pop articles))
7953       (setq group (format "%s-%d" gnus-newsgroup-name article))
7954       (gnus-summary-remove-process-mark article)
7955       (when (gnus-summary-display-article article)
7956         (save-excursion
7957           (with-temp-buffer
7958             (insert-buffer-substring gnus-original-article-buffer)
7959             ;; Remove some headers that may lead nndoc to make
7960             ;; the wrong guess.
7961             (message-narrow-to-head)
7962             (goto-char (point-min))
7963             (delete-matching-lines "^\\(Path\\):\\|^From ")
7964             (widen)
7965             (if (setq egroup
7966                       (gnus-group-read-ephemeral-group
7967                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7968                                      (nndoc-article-type guess))
7969                        t nil t))
7970                 (progn
7971             ;; Make all postings to this group go to the parent group.
7972                   (nconc (gnus-info-params (gnus-get-info egroup))
7973                          params)
7974                   (push egroup groups))
7975               ;; Couldn't select this doc group.
7976               (gnus-error 3 "Article couldn't be entered"))))))
7977     ;; Now we have selected all the documents.
7978     (cond
7979      ((not groups)
7980       (error "None of the articles could be interpreted as documents"))
7981      ((gnus-group-read-ephemeral-group
7982        (setq vgroup (format
7983                      "nnvirtual:%s-%s" gnus-newsgroup-name
7984                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7985        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7986        t
7987        (cons (current-buffer) 'summary)))
7988      (t
7989       (error "Couldn't select virtual nndoc group")))))
7990
7991 (defun gnus-summary-isearch-article (&optional regexp-p)
7992   "Do incremental search forward on the current article.
7993 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7994   (interactive "P")
7995   (gnus-summary-select-article)
7996   (gnus-configure-windows 'article)
7997   (gnus-eval-in-buffer-window gnus-article-buffer
7998     (save-restriction
7999       (widen)
8000       (isearch-forward regexp-p))))
8001
8002 (defun gnus-summary-search-article-forward (regexp &optional backward)
8003   "Search for an article containing REGEXP forward.
8004 If BACKWARD, search backward instead."
8005   (interactive
8006    (list (read-string
8007           (format "Search article %s (regexp%s): "
8008                   (if current-prefix-arg "backward" "forward")
8009                   (if gnus-last-search-regexp
8010                       (concat ", default " gnus-last-search-regexp)
8011                     "")))
8012          current-prefix-arg))
8013   (if (string-equal regexp "")
8014       (setq regexp (or gnus-last-search-regexp ""))
8015     (setq gnus-last-search-regexp regexp)
8016     (setq gnus-article-before-search gnus-current-article))
8017   ;; Intentionally set gnus-last-article.
8018   (setq gnus-last-article gnus-article-before-search)
8019   (let ((gnus-last-article gnus-last-article))
8020     (if (gnus-summary-search-article regexp backward)
8021         (gnus-summary-show-thread)
8022       (error "Search failed: \"%s\"" regexp))))
8023
8024 (defun gnus-summary-search-article-backward (regexp)
8025   "Search for an article containing REGEXP backward."
8026   (interactive
8027    (list (read-string
8028           (format "Search article backward (regexp%s): "
8029                   (if gnus-last-search-regexp
8030                       (concat ", default " gnus-last-search-regexp)
8031                     "")))))
8032   (gnus-summary-search-article-forward regexp 'backward))
8033
8034 (defun gnus-summary-search-article (regexp &optional backward)
8035   "Search for an article containing REGEXP.
8036 Optional argument BACKWARD means do search for backward.
8037 `gnus-select-article-hook' is not called during the search."
8038   ;; We have to require this here to make sure that the following
8039   ;; dynamic binding isn't shadowed by autoloading.
8040   (require 'gnus-async)
8041   (require 'gnus-art)
8042   (let ((gnus-select-article-hook nil)  ;Disable hook.
8043         (gnus-article-prepare-hook nil)
8044         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8045         (gnus-use-article-prefetch nil)
8046         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8047         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8048         (sum (current-buffer))
8049         (gnus-display-mime-function nil)
8050         (found nil)
8051         point)
8052     (gnus-save-hidden-threads
8053       (gnus-summary-select-article)
8054       (set-buffer gnus-article-buffer)
8055       (goto-char (window-point (get-buffer-window (current-buffer))))
8056       (when backward
8057         (forward-line -1))
8058       (while (not found)
8059         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8060         (if (if backward
8061                 (re-search-backward regexp nil t)
8062               (re-search-forward regexp nil t))
8063             ;; We found the regexp.
8064             (progn
8065               (setq found 'found)
8066               (beginning-of-line)
8067               (set-window-start
8068                (get-buffer-window (current-buffer))
8069                (point))
8070               (forward-line 1)
8071               (set-window-point
8072                (get-buffer-window (current-buffer))
8073                (point))
8074               (set-buffer sum)
8075               (setq point (point)))
8076           ;; We didn't find it, so we go to the next article.
8077           (set-buffer sum)
8078           (setq found 'not)
8079           (while (eq found 'not)
8080             (if (not (if backward (gnus-summary-find-prev)
8081                        (gnus-summary-find-next)))
8082                 ;; No more articles.
8083                 (setq found t)
8084               ;; Select the next article and adjust point.
8085               (unless (gnus-summary-article-sparse-p
8086                        (gnus-summary-article-number))
8087                 (setq found nil)
8088                 (gnus-summary-select-article)
8089                 (set-buffer gnus-article-buffer)
8090                 (widen)
8091                 (goto-char (if backward (point-max) (point-min))))))))
8092       (gnus-message 7 ""))
8093     ;; Return whether we found the regexp.
8094     (when (eq found 'found)
8095       (goto-char point)
8096       (gnus-summary-show-thread)
8097       (gnus-summary-goto-subject gnus-current-article)
8098       (gnus-summary-position-point)
8099       t)))
8100
8101 (defun gnus-find-matching-articles (header regexp)
8102   "Return a list of all articles that match REGEXP on HEADER.
8103 This search includes all articles in the current group that Gnus has
8104 fetched headers for, whether they are displayed or not."
8105   (let ((articles nil)
8106         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8107         (case-fold-search t))
8108     (dolist (header gnus-newsgroup-headers)
8109       (when (string-match regexp (funcall func header))
8110         (push (mail-header-number header) articles)))
8111     (nreverse articles)))
8112
8113 (defun gnus-summary-find-matching (header regexp &optional backward unread
8114                                           not-case-fold not-matching)
8115   "Return a list of all articles that match REGEXP on HEADER.
8116 The search stars on the current article and goes forwards unless
8117 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8118 If UNREAD is non-nil, only unread articles will
8119 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8120 in the comparisons. If NOT-MATCHING, return a list of all articles that
8121 not match REGEXP on HEADER."
8122   (let ((case-fold-search (not not-case-fold))
8123         articles d func)
8124     (if (consp header)
8125         (if (eq (car header) 'extra)
8126             (setq func
8127                   `(lambda (h)
8128                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8129                          "")))
8130           (error "%s is an invalid header" header))
8131       (unless (fboundp (intern (concat "mail-header-" header)))
8132         (error "%s is not a valid header" header))
8133       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8134     (dolist (d (if (eq backward 'all)
8135                    gnus-newsgroup-data
8136                  (gnus-data-find-list
8137                   (gnus-summary-article-number)
8138                   (gnus-data-list backward))))
8139       (when (and (or (not unread)       ; We want all articles...
8140                      (gnus-data-unread-p d)) ; Or just unreads.
8141                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8142                  (if not-matching
8143                      (not (string-match
8144                            regexp
8145                            (funcall func (gnus-data-header d))))
8146                    (string-match regexp
8147                                  (funcall func (gnus-data-header d)))))
8148         (push (gnus-data-number d) articles))) ; Success!
8149     (nreverse articles)))
8150
8151 (defun gnus-summary-execute-command (header regexp command &optional backward)
8152   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8153 If HEADER is an empty string (or nil), the match is done on the entire
8154 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8155   (interactive
8156    (list (let ((completion-ignore-case t))
8157            (completing-read
8158             "Header name: "
8159             (mapcar (lambda (header) (list (format "%s" header)))
8160                     (append
8161                      '("Number" "Subject" "From" "Lines" "Date"
8162                        "Message-ID" "Xref" "References" "Body")
8163                      gnus-extra-headers))
8164             nil 'require-match))
8165          (read-string "Regexp: ")
8166          (read-key-sequence "Command: ")
8167          current-prefix-arg))
8168   (when (equal header "Body")
8169     (setq header ""))
8170   ;; Hidden thread subtrees must be searched as well.
8171   (gnus-summary-show-all-threads)
8172   ;; We don't want to change current point nor window configuration.
8173   (save-excursion
8174     (save-window-excursion
8175       (let (gnus-visual
8176             gnus-treat-strip-trailing-blank-lines
8177             gnus-treat-strip-leading-blank-lines
8178             gnus-treat-strip-multiple-blank-lines
8179             gnus-treat-hide-boring-headers
8180             gnus-treat-fold-newsgroups
8181             gnus-article-prepare-hook)
8182         (gnus-message 6 "Executing %s..." (key-description command))
8183         ;; We'd like to execute COMMAND interactively so as to give arguments.
8184         (gnus-execute header regexp
8185                       `(call-interactively ',(key-binding command))
8186                       backward)
8187         (gnus-message 6 "Executing %s...done" (key-description command))))))
8188
8189 (defun gnus-summary-beginning-of-article ()
8190   "Scroll the article back to the beginning."
8191   (interactive)
8192   (gnus-summary-select-article)
8193   (gnus-configure-windows 'article)
8194   (gnus-eval-in-buffer-window gnus-article-buffer
8195     (widen)
8196     (goto-char (point-min))
8197     (when gnus-page-broken
8198       (gnus-narrow-to-page))))
8199
8200 (defun gnus-summary-end-of-article ()
8201   "Scroll to the end of the article."
8202   (interactive)
8203   (gnus-summary-select-article)
8204   (gnus-configure-windows 'article)
8205   (gnus-eval-in-buffer-window gnus-article-buffer
8206     (widen)
8207     (goto-char (point-max))
8208     (recenter -3)
8209     (when gnus-page-broken
8210       (gnus-narrow-to-page))))
8211
8212 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8213   "Truncate to LEN and quote all \"(\"'s in STRING."
8214   (gnus-replace-in-string (if (and len (> (length string) len))
8215                               (substring string 0 len)
8216                             string)
8217                           "[()]" "\\\\\\&"))
8218
8219 (defun gnus-summary-print-article (&optional filename n)
8220   "Generate and print a PostScript image of the N next (mail) articles.
8221
8222 If N is negative, print the N previous articles.  If N is nil and articles
8223 have been marked with the process mark, print these instead.
8224
8225 If the optional first argument FILENAME is nil, send the image to the
8226 printer.  If FILENAME is a string, save the PostScript image in a file with
8227 that name.  If FILENAME is a number, prompt the user for the name of the file
8228 to save in."
8229   (interactive (list (ps-print-preprint current-prefix-arg)))
8230   (dolist (article (gnus-summary-work-articles n))
8231     (gnus-summary-select-article nil nil 'pseudo article)
8232     (gnus-eval-in-buffer-window gnus-article-buffer
8233       (gnus-print-buffer))
8234     (gnus-summary-remove-process-mark article))
8235   (ps-despool filename))
8236
8237 (defun gnus-print-buffer ()
8238   (let ((buffer (generate-new-buffer " *print*")))
8239     (unwind-protect
8240         (progn
8241           (copy-to-buffer buffer (point-min) (point-max))
8242           (set-buffer buffer)
8243           (gnus-article-delete-invisible-text)
8244           (gnus-remove-text-with-property 'gnus-decoration)
8245           (when (gnus-visual-p 'article-highlight 'highlight)
8246             ;; Copy-to-buffer doesn't copy overlay.  So redo
8247             ;; highlight.
8248             (let ((gnus-article-buffer buffer))
8249               (gnus-article-highlight-citation t)
8250               (gnus-article-highlight-signature)))
8251           (let ((ps-left-header
8252                  (list
8253                   (concat "("
8254                           (gnus-summary-print-truncate-and-quote
8255                            (mail-header-subject gnus-current-headers)
8256                            66) ")")
8257                   (concat "("
8258                           (gnus-summary-print-truncate-and-quote
8259                            (mail-header-from gnus-current-headers)
8260                            45) ")")))
8261                 (ps-right-header
8262                  (list
8263                   "/pagenumberstring load"
8264                   (concat "("
8265                           (mail-header-date gnus-current-headers) ")"))))
8266             (gnus-run-hooks 'gnus-ps-print-hook)
8267             (save-excursion
8268               (if window-system
8269                   (ps-spool-buffer-with-faces)
8270                 (ps-spool-buffer)))))
8271       (kill-buffer buffer))))
8272
8273 (defun gnus-summary-show-article (&optional arg)
8274   "Force redisplaying of the current article.
8275 If ARG (the prefix) is a number, show the article with the charset
8276 defined in `gnus-summary-show-article-charset-alist', or the charset
8277 input.
8278 If ARG (the prefix) is non-nil and not a number, show the raw article
8279 without any article massaging functions being run.  Normally, the key strokes
8280 are `C-u g'."
8281   (interactive "P")
8282   (cond
8283    ((numberp arg)
8284     (gnus-summary-show-article t)
8285     (let ((gnus-newsgroup-charset
8286            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8287                (mm-read-coding-system
8288                 "View as charset: " ;; actually it is coding system.
8289                 (save-excursion
8290                   (set-buffer gnus-article-buffer)
8291                   (mm-detect-coding-region (point) (point-max))))))
8292           (gnus-newsgroup-ignored-charsets 'gnus-all))
8293       (gnus-summary-select-article nil 'force)
8294       (let ((deps gnus-newsgroup-dependencies)
8295             head header lines)
8296         (save-excursion
8297           (set-buffer gnus-original-article-buffer)
8298           (save-restriction
8299             (message-narrow-to-head)
8300             (setq head (buffer-string))
8301             (goto-char (point-min))
8302             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8303               (goto-char (point-max))
8304               (widen)
8305               (setq lines (1- (count-lines (point) (point-max))))))
8306           (with-temp-buffer
8307             (insert (format "211 %d Article retrieved.\n"
8308                             (cdr gnus-article-current)))
8309             (insert head)
8310             (if lines (insert (format "Lines: %d\n" lines)))
8311             (insert ".\n")
8312             (let ((nntp-server-buffer (current-buffer)))
8313               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8314         (gnus-data-set-header
8315          (gnus-data-find (cdr gnus-article-current))
8316          header)
8317         (gnus-summary-update-article-line
8318          (cdr gnus-article-current) header)
8319         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8320           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8321    ((not arg)
8322     ;; Select the article the normal way.
8323     (gnus-summary-select-article nil 'force))
8324    (t
8325     ;; We have to require this here to make sure that the following
8326     ;; dynamic binding isn't shadowed by autoloading.
8327     (require 'gnus-async)
8328     (require 'gnus-art)
8329     ;; Bind the article treatment functions to nil.
8330     (let ((gnus-have-all-headers t)
8331           gnus-article-prepare-hook
8332           gnus-article-decode-hook
8333           gnus-display-mime-function
8334           gnus-break-pages)
8335       ;; Destroy any MIME parts.
8336       (when (gnus-buffer-live-p gnus-article-buffer)
8337         (save-excursion
8338           (set-buffer gnus-article-buffer)
8339           (mm-destroy-parts gnus-article-mime-handles)
8340           ;; Set it to nil for safety reason.
8341           (setq gnus-article-mime-handle-alist nil)
8342           (setq gnus-article-mime-handles nil)))
8343       (gnus-summary-select-article nil 'force))))
8344   (gnus-summary-goto-subject gnus-current-article)
8345   (gnus-summary-position-point))
8346
8347 (defun gnus-summary-show-raw-article ()
8348   "Show the raw article without any article massaging functions being run."
8349   (interactive)
8350   (gnus-summary-show-article t))
8351
8352 (defun gnus-summary-verbose-headers (&optional arg)
8353   "Toggle permanent full header display.
8354 If ARG is a positive number, turn header display on.
8355 If ARG is a negative number, turn header display off."
8356   (interactive "P")
8357   (setq gnus-show-all-headers
8358         (cond ((or (not (numberp arg))
8359                    (zerop arg))
8360                (not gnus-show-all-headers))
8361               ((natnump arg)
8362                t)))
8363   (gnus-summary-show-article))
8364
8365 (defun gnus-summary-toggle-header (&optional arg)
8366   "Show the headers if they are hidden, or hide them if they are shown.
8367 If ARG is a positive number, show the entire header.
8368 If ARG is a negative number, hide the unwanted header lines."
8369   (interactive "P")
8370   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8371                      (get-buffer-window gnus-article-buffer t))))
8372     (with-current-buffer gnus-article-buffer
8373       (widen)
8374       (article-narrow-to-head)
8375       (let* ((buffer-read-only nil)
8376              (inhibit-point-motion-hooks t)
8377              (hidden (if (numberp arg)
8378                          (>= arg 0)
8379                        (gnus-article-hidden-text-p 'headers)))
8380              s e)
8381         (delete-region (point-min) (point-max))
8382         (with-current-buffer gnus-original-article-buffer
8383           (goto-char (setq s (point-min)))
8384           (setq e (if (search-forward "\n\n" nil t)
8385                       (1- (point))
8386                     (point-max))))
8387         (insert-buffer-substring gnus-original-article-buffer s e)
8388         (article-decode-encoded-words)
8389         (if hidden
8390             (let ((gnus-treat-hide-headers nil)
8391                   (gnus-treat-hide-boring-headers nil))
8392               (gnus-delete-wash-type 'headers)
8393               (gnus-treat-article 'head))
8394           (gnus-treat-article 'head))
8395         (widen)
8396         (if window
8397             (set-window-start window (goto-char (point-min))))
8398         (setq gnus-page-broken
8399               (when gnus-break-pages
8400                 (gnus-narrow-to-page)
8401                 t))
8402         (gnus-set-mode-line 'article)))))
8403
8404 (defun gnus-summary-show-all-headers ()
8405   "Make all header lines visible."
8406   (interactive)
8407   (gnus-summary-toggle-header 1))
8408
8409 (defun gnus-summary-caesar-message (&optional arg)
8410   "Caesar rotate the current article by 13.
8411 The numerical prefix specifies how many places to rotate each letter
8412 forward."
8413   (interactive "P")
8414   (gnus-summary-select-article)
8415   (let ((mail-header-separator ""))
8416     (gnus-eval-in-buffer-window gnus-article-buffer
8417       (save-restriction
8418         (widen)
8419         (let ((start (window-start))
8420               buffer-read-only)
8421           (message-caesar-buffer-body arg)
8422           (set-window-start (get-buffer-window (current-buffer)) start))))))
8423
8424 (defun gnus-summary-stop-page-breaking ()
8425   "Stop page breaking in the current article."
8426   (interactive)
8427   (gnus-summary-select-article)
8428   (gnus-eval-in-buffer-window gnus-article-buffer
8429     (widen)
8430     (when (gnus-visual-p 'page-marker)
8431       (let ((buffer-read-only nil))
8432         (gnus-remove-text-with-property 'gnus-prev)
8433         (gnus-remove-text-with-property 'gnus-next))
8434       (setq gnus-page-broken nil))))
8435
8436 (defun gnus-summary-move-article (&optional n to-newsgroup
8437                                             select-method action)
8438   "Move the current article to a different newsgroup.
8439 If N is a positive number, move the N next articles.
8440 If N is a negative number, move the N previous articles.
8441 If N is nil and any articles have been marked with the process mark,
8442 move those articles instead.
8443 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8444 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8445 re-spool using this method.
8446
8447 When called interactively with TO-NEWSGROUP being nil, the value of
8448 the variable `gnus-move-split-methods' is used for finding a default
8449 for the target newsgroup.
8450
8451 For this function to work, both the current newsgroup and the
8452 newsgroup that you want to move to have to support the `request-move'
8453 and `request-accept' functions.
8454
8455 ACTION can be either `move' (the default), `crosspost' or `copy'."
8456   (interactive "P")
8457   (unless action
8458     (setq action 'move))
8459   ;; Check whether the source group supports the required functions.
8460   (cond ((and (eq action 'move)
8461               (not (gnus-check-backend-function
8462                     'request-move-article gnus-newsgroup-name)))
8463          (error "The current group does not support article moving"))
8464         ((and (eq action 'crosspost)
8465               (not (gnus-check-backend-function
8466                     'request-replace-article gnus-newsgroup-name)))
8467          (error "The current group does not support article editing")))
8468   (let ((articles (gnus-summary-work-articles n))
8469         (prefix (if (gnus-check-backend-function
8470                      'request-move-article gnus-newsgroup-name)
8471                     (gnus-group-real-prefix gnus-newsgroup-name)
8472                   ""))
8473         (names '((move "Move" "Moving")
8474                  (copy "Copy" "Copying")
8475                  (crosspost "Crosspost" "Crossposting")))
8476         (copy-buf (save-excursion
8477                     (nnheader-set-temp-buffer " *copy article*")))
8478         art-group to-method new-xref article to-groups)
8479     (unless (assq action names)
8480       (error "Unknown action %s" action))
8481     ;; Read the newsgroup name.
8482     (when (and (not to-newsgroup)
8483                (not select-method))
8484       (if (and gnus-move-split-methods
8485                (not
8486                 (and (memq gnus-current-article articles)
8487                      (gnus-buffer-live-p gnus-original-article-buffer))))
8488           ;; When `gnus-move-split-methods' is non-nil, we have to
8489           ;; select an article to give `gnus-read-move-group-name' an
8490           ;; opportunity to suggest an appropriate default.  However,
8491           ;; we needn't render or mark the article.
8492           (let ((gnus-display-mime-function nil)
8493                 (gnus-article-prepare-hook nil)
8494                 (gnus-mark-article-hook nil))
8495             (gnus-summary-select-article nil nil nil (car articles))))
8496       (setq to-newsgroup
8497             (gnus-read-move-group-name
8498              (cadr (assq action names))
8499              (symbol-value (intern (format "gnus-current-%s-group" action)))
8500              articles prefix))
8501       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8502     (setq to-method (or select-method
8503                         (gnus-server-to-method
8504                          (gnus-group-method to-newsgroup))))
8505     ;; Check the method we are to move this article to...
8506     (unless (gnus-check-backend-function
8507              'request-accept-article (car to-method))
8508       (error "%s does not support article copying" (car to-method)))
8509     (unless (gnus-check-server to-method)
8510       (error "Can't open server %s" (car to-method)))
8511     (gnus-message 6 "%s to %s: %s..."
8512                   (caddr (assq action names))
8513                   (or (car select-method) to-newsgroup) articles)
8514     (while articles
8515       (setq article (pop articles))
8516       (setq
8517        art-group
8518        (cond
8519         ;; Move the article.
8520         ((eq action 'move)
8521          ;; Remove this article from future suppression.
8522          (gnus-dup-unsuppress-article article)
8523          (gnus-request-move-article
8524           article                       ; Article to move
8525           gnus-newsgroup-name           ; From newsgroup
8526           (nth 1 (gnus-find-method-for-group
8527                   gnus-newsgroup-name)) ; Server
8528           (list 'gnus-request-accept-article
8529                 to-newsgroup (list 'quote select-method)
8530                 (not articles) t)       ; Accept form
8531           (not articles)))              ; Only save nov last time
8532         ;; Copy the article.
8533         ((eq action 'copy)
8534          (save-excursion
8535            (set-buffer copy-buf)
8536            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8537              (gnus-request-accept-article
8538               to-newsgroup select-method (not articles) t))))
8539         ;; Crosspost the article.
8540         ((eq action 'crosspost)
8541          (let ((xref (message-tokenize-header
8542                       (mail-header-xref (gnus-summary-article-header article))
8543                       " ")))
8544            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8545                                   ":" (number-to-string article)))
8546            (unless xref
8547              (setq xref (list (system-name))))
8548            (setq new-xref
8549                  (concat
8550                   (mapconcat 'identity
8551                              (delete "Xref:" (delete new-xref xref))
8552                              " ")
8553                   " " new-xref))
8554            (save-excursion
8555              (set-buffer copy-buf)
8556              ;; First put the article in the destination group.
8557              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8558              (when (consp (setq art-group
8559                                 (gnus-request-accept-article
8560                                  to-newsgroup select-method (not articles))))
8561                (setq new-xref (concat new-xref " " (car art-group)
8562                                       ":"
8563                                       (number-to-string (cdr art-group))))
8564                ;; Now we have the new Xrefs header, so we insert
8565                ;; it and replace the new article.
8566                (nnheader-replace-header "Xref" new-xref)
8567                (gnus-request-replace-article
8568                 (cdr art-group) to-newsgroup (current-buffer))
8569                art-group))))))
8570       (cond
8571        ((not art-group)
8572         (gnus-message 1 "Couldn't %s article %s: %s"
8573                       (cadr (assq action names)) article
8574                       (nnheader-get-report (car to-method))))
8575        ((eq art-group 'junk)
8576         (when (eq action 'move)
8577           (gnus-summary-mark-article article gnus-canceled-mark)
8578           (gnus-message 4 "Deleted article %s" article)))
8579        (t
8580         (let* ((pto-group (gnus-group-prefixed-name
8581                            (car art-group) to-method))
8582                (entry
8583                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8584                (info (nth 2 entry))
8585                (to-group (gnus-info-group info))
8586                to-marks)
8587           ;; Update the group that has been moved to.
8588           (when (and info
8589                      (memq action '(move copy)))
8590             (unless (member to-group to-groups)
8591               (push to-group to-groups))
8592
8593             (unless (memq article gnus-newsgroup-unreads)
8594               (push 'read to-marks)
8595               (gnus-info-set-read
8596                info (gnus-add-to-range (gnus-info-read info)
8597                                        (list (cdr art-group)))))
8598
8599             ;; See whether the article is to be put in the cache.
8600             (let ((marks gnus-article-mark-lists)
8601                   (to-article (cdr art-group)))
8602
8603               ;; Enter the article into the cache in the new group,
8604               ;; if that is required.
8605               (when gnus-use-cache
8606                 (gnus-cache-possibly-enter-article
8607                  to-group to-article
8608                  (memq article gnus-newsgroup-marked)
8609                  (memq article gnus-newsgroup-dormant)
8610                  (memq article gnus-newsgroup-unreads)))
8611
8612               (when gnus-preserve-marks
8613                 ;; Copy any marks over to the new group.
8614                 (when (and (equal to-group gnus-newsgroup-name)
8615                            (not (memq article gnus-newsgroup-unreads)))
8616                   ;; Mark this article as read in this group.
8617                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8618                   (setcdr (gnus-active to-group) to-article)
8619                   (setcdr gnus-newsgroup-active to-article))
8620
8621                 (while marks
8622                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8623                     (when (memq article (symbol-value
8624                                          (intern (format "gnus-newsgroup-%s"
8625                                                          (caar marks)))))
8626                       (push (cdar marks) to-marks)
8627                       ;; If the other group is the same as this group,
8628                       ;; then we have to add the mark to the list.
8629                       (when (equal to-group gnus-newsgroup-name)
8630                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8631                              (cons to-article
8632                                    (symbol-value
8633                                     (intern (format "gnus-newsgroup-%s"
8634                                                     (caar marks)))))))
8635                       ;; Copy the marks to other group.
8636                       (gnus-add-marked-articles
8637                        to-group (cdar marks) (list to-article) info)))
8638                   (setq marks (cdr marks)))
8639
8640                 (gnus-request-set-mark to-group (list (list (list to-article)
8641                                                             'add
8642                                                             to-marks))))
8643
8644               (gnus-dribble-enter
8645                (concat "(gnus-group-set-info '"
8646                        (gnus-prin1-to-string (gnus-get-info to-group))
8647                        ")"))))
8648
8649           ;; Update the Xref header in this article to point to
8650           ;; the new crossposted article we have just created.
8651           (when (eq action 'crosspost)
8652             (save-excursion
8653               (set-buffer copy-buf)
8654               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8655               (nnheader-replace-header "Xref" new-xref)
8656               (gnus-request-replace-article
8657                article gnus-newsgroup-name (current-buffer)))))
8658
8659         ;;;!!!Why is this necessary?
8660         (set-buffer gnus-summary-buffer)
8661
8662         (gnus-summary-goto-subject article)
8663         (when (eq action 'move)
8664           (gnus-summary-mark-article article gnus-canceled-mark))))
8665       (gnus-summary-remove-process-mark article))
8666     ;; Re-activate all groups that have been moved to.
8667     (save-excursion
8668       (set-buffer gnus-group-buffer)
8669       (let ((gnus-group-marked to-groups))
8670         (gnus-group-get-new-news-this-group nil t)))
8671
8672     (gnus-kill-buffer copy-buf)
8673     (gnus-summary-position-point)
8674     (gnus-set-mode-line 'summary)))
8675
8676 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8677   "Move the current article to a different newsgroup.
8678 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8679 When called interactively, if TO-NEWSGROUP is nil, use the value of
8680 the variable `gnus-move-split-methods' for finding a default target
8681 newsgroup.
8682 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8683 re-spool using this method."
8684   (interactive "P")
8685   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8686
8687 (defun gnus-summary-crosspost-article (&optional n)
8688   "Crosspost the current article to some other group."
8689   (interactive "P")
8690   (gnus-summary-move-article n nil nil 'crosspost))
8691
8692 (defcustom gnus-summary-respool-default-method nil
8693   "Default method type for respooling an article.
8694 If nil, use to the current newsgroup method."
8695   :type 'symbol
8696   :group 'gnus-summary-mail)
8697
8698 (defun gnus-summary-respool-article (&optional n method)
8699   "Respool the current article.
8700 The article will be squeezed through the mail spooling process again,
8701 which means that it will be put in some mail newsgroup or other
8702 depending on `nnmail-split-methods'.
8703 If N is a positive number, respool the N next articles.
8704 If N is a negative number, respool the N previous articles.
8705 If N is nil and any articles have been marked with the process mark,
8706 respool those articles instead.
8707
8708 Respooling can be done both from mail groups and \"real\" newsgroups.
8709 In the former case, the articles in question will be moved from the
8710 current group into whatever groups they are destined to.  In the
8711 latter case, they will be copied into the relevant groups."
8712   (interactive
8713    (list current-prefix-arg
8714          (let* ((methods (gnus-methods-using 'respool))
8715                 (methname
8716                  (symbol-name (or gnus-summary-respool-default-method
8717                                   (car (gnus-find-method-for-group
8718                                         gnus-newsgroup-name)))))
8719                 (method
8720                  (gnus-completing-read-with-default
8721                   methname "What backend do you want to use when respooling?"
8722                   methods nil t nil 'gnus-mail-method-history))
8723                 ms)
8724            (cond
8725             ((zerop (length (setq ms (gnus-servers-using-backend
8726                                       (intern method)))))
8727              (list (intern method) ""))
8728             ((= 1 (length ms))
8729              (car ms))
8730             (t
8731              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8732                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8733                            ms-alist))))))))
8734   (unless method
8735     (error "No method given for respooling"))
8736   (if (assoc (symbol-name
8737               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8738              (gnus-methods-using 'respool))
8739       (gnus-summary-move-article n nil method)
8740     (gnus-summary-copy-article n nil method)))
8741
8742 (defun gnus-summary-import-article (file &optional edit)
8743   "Import an arbitrary file into a mail newsgroup."
8744   (interactive "fImport file: \nP")
8745   (let ((group gnus-newsgroup-name)
8746         (now (current-time))
8747         atts lines group-art)
8748     (unless (gnus-check-backend-function 'request-accept-article group)
8749       (error "%s does not support article importing" group))
8750     (or (file-readable-p file)
8751         (not (file-regular-p file))
8752         (error "Can't read %s" file))
8753     (save-excursion
8754       (set-buffer (gnus-get-buffer-create " *import file*"))
8755       (erase-buffer)
8756       (nnheader-insert-file-contents file)
8757       (goto-char (point-min))
8758       (if (nnheader-article-p)
8759           (save-restriction
8760             (goto-char (point-min))
8761             (search-forward "\n\n" nil t)
8762             (narrow-to-region (point-min) (1- (point)))
8763             (goto-char (point-min))
8764             (unless (re-search-forward "^date:" nil t)
8765               (goto-char (point-max))
8766               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8767        ;; This doesn't look like an article, so we fudge some headers.
8768         (setq atts (file-attributes file)
8769               lines (count-lines (point-min) (point-max)))
8770         (insert "From: " (read-string "From: ") "\n"
8771                 "Subject: " (read-string "Subject: ") "\n"
8772                 "Date: " (message-make-date (nth 5 atts)) "\n"
8773                 "Message-ID: " (message-make-message-id) "\n"
8774                 "Lines: " (int-to-string lines) "\n"
8775                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8776       (setq group-art (gnus-request-accept-article group nil t))
8777       (kill-buffer (current-buffer)))
8778     (setq gnus-newsgroup-active (gnus-activate-group group))
8779     (forward-line 1)
8780     (gnus-summary-goto-article (cdr group-art) nil t)
8781     (when edit
8782       (gnus-summary-edit-article))))
8783
8784 (defun gnus-summary-create-article ()
8785   "Create an article in a mail newsgroup."
8786   (interactive)
8787   (let ((group gnus-newsgroup-name)
8788         (now (current-time))
8789         group-art)
8790     (unless (gnus-check-backend-function 'request-accept-article group)
8791       (error "%s does not support article importing" group))
8792     (save-excursion
8793       (set-buffer (gnus-get-buffer-create " *import file*"))
8794       (erase-buffer)
8795       (goto-char (point-min))
8796       ;; This doesn't look like an article, so we fudge some headers.
8797       (insert "From: " (read-string "From: ") "\n"
8798               "Subject: " (read-string "Subject: ") "\n"
8799               "Date: " (message-make-date now) "\n"
8800               "Message-ID: " (message-make-message-id) "\n")
8801       (setq group-art (gnus-request-accept-article group nil t))
8802       (kill-buffer (current-buffer)))
8803     (setq gnus-newsgroup-active (gnus-activate-group group))
8804     (forward-line 1)
8805     (gnus-summary-goto-article (cdr group-art) nil t)
8806     (gnus-summary-edit-article)))
8807
8808 (defun gnus-summary-article-posted-p ()
8809   "Say whether the current (mail) article is available from news as well.
8810 This will be the case if the article has both been mailed and posted."
8811   (interactive)
8812   (let ((id (mail-header-references (gnus-summary-article-header)))
8813         (gnus-override-method (car (gnus-refer-article-methods))))
8814     (if (gnus-request-head id "")
8815         (gnus-message 2 "The current message was found on %s"
8816                       gnus-override-method)
8817       (gnus-message 2 "The current message couldn't be found on %s"
8818                     gnus-override-method)
8819       nil)))
8820
8821 (defun gnus-summary-expire-articles (&optional now)
8822   "Expire all articles that are marked as expirable in the current group."
8823   (interactive)
8824   (when (gnus-check-backend-function
8825          'request-expire-articles gnus-newsgroup-name)
8826     ;; This backend supports expiry.
8827     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8828            (expirable (if total
8829                           (progn
8830                             ;; We need to update the info for
8831                             ;; this group for `gnus-list-of-read-articles'
8832                             ;; to give us the right answer.
8833                             (gnus-run-hooks 'gnus-exit-group-hook)
8834                             (gnus-summary-update-info)
8835                             (gnus-list-of-read-articles gnus-newsgroup-name))
8836                         (setq gnus-newsgroup-expirable
8837                               (sort gnus-newsgroup-expirable '<))))
8838            (expiry-wait (if now 'immediate
8839                           (gnus-group-find-parameter
8840                            gnus-newsgroup-name 'expiry-wait)))
8841            (nnmail-expiry-target
8842             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8843                 nnmail-expiry-target))
8844            es)
8845       (when expirable
8846         ;; There are expirable articles in this group, so we run them
8847         ;; through the expiry process.
8848         (gnus-message 6 "Expiring articles...")
8849         (unless (gnus-check-group gnus-newsgroup-name)
8850           (error "Can't open server for %s" gnus-newsgroup-name))
8851         ;; The list of articles that weren't expired is returned.
8852         (save-excursion
8853           (if expiry-wait
8854               (let ((nnmail-expiry-wait-function nil)
8855                     (nnmail-expiry-wait expiry-wait))
8856                 (setq es (gnus-request-expire-articles
8857                           expirable gnus-newsgroup-name)))
8858             (setq es (gnus-request-expire-articles
8859                       expirable gnus-newsgroup-name)))
8860           (unless total
8861             (setq gnus-newsgroup-expirable es))
8862           ;; We go through the old list of expirable, and mark all
8863           ;; really expired articles as nonexistent.
8864           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8865             (let ((gnus-use-cache nil))
8866               (dolist (article expirable)
8867                 (when (and (not (memq article es))
8868                            (gnus-data-find article))
8869                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8870         (gnus-message 6 "Expiring articles...done")))))
8871
8872 (defun gnus-summary-expire-articles-now ()
8873   "Expunge all expirable articles in the current group.
8874 This means that *all* articles that are marked as expirable will be
8875 deleted forever, right now."
8876   (interactive)
8877   (or gnus-expert-user
8878       (gnus-yes-or-no-p
8879        "Are you really, really, really sure you want to delete all these messages? ")
8880       (error "Phew!"))
8881   (gnus-summary-expire-articles t))
8882
8883 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8884 (defun gnus-summary-delete-article (&optional n)
8885   "Delete the N next (mail) articles.
8886 This command actually deletes articles.  This is not a marking
8887 command.  The article will disappear forever from your life, never to
8888 return.
8889 If N is negative, delete backwards.
8890 If N is nil and articles have been marked with the process mark,
8891 delete these instead."
8892   (interactive "P")
8893   (unless (gnus-check-backend-function 'request-expire-articles
8894                                        gnus-newsgroup-name)
8895     (error "The current newsgroup does not support article deletion"))
8896   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8897     (error "Couldn't open server"))
8898   ;; Compute the list of articles to delete.
8899   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8900         (nnmail-expiry-target 'delete)
8901         not-deleted)
8902     (if (and gnus-novice-user
8903              (not (gnus-yes-or-no-p
8904                    (format "Do you really want to delete %s forever? "
8905                            (if (> (length articles) 1)
8906                                (format "these %s articles" (length articles))
8907                              "this article")))))
8908         ()
8909       ;; Delete the articles.
8910       (setq not-deleted (gnus-request-expire-articles
8911                          articles gnus-newsgroup-name 'force))
8912       (while articles
8913         (gnus-summary-remove-process-mark (car articles))
8914         ;; The backend might not have been able to delete the article
8915         ;; after all.
8916         (unless (memq (car articles) not-deleted)
8917           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8918         (setq articles (cdr articles)))
8919       (when not-deleted
8920         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8921     (gnus-summary-position-point)
8922     (gnus-set-mode-line 'summary)
8923     not-deleted))
8924
8925 (defun gnus-summary-edit-article (&optional arg)
8926   "Edit the current article.
8927 This will have permanent effect only in mail groups.
8928 If ARG is nil, edit the decoded articles.
8929 If ARG is 1, edit the raw articles.
8930 If ARG is 2, edit the raw articles even in read-only groups.
8931 If ARG is 3, edit the articles with the current handles.
8932 Otherwise, allow editing of articles even in read-only
8933 groups."
8934   (interactive "P")
8935   (let (force raw current-handles)
8936     (cond
8937      ((null arg))
8938      ((eq arg 1)
8939       (setq raw t))
8940      ((eq arg 2)
8941       (setq raw t
8942             force t))
8943      ((eq arg 3)
8944       (setq current-handles
8945             (and (gnus-buffer-live-p gnus-article-buffer)
8946                  (with-current-buffer gnus-article-buffer
8947                    (prog1
8948                        gnus-article-mime-handles
8949                      (setq gnus-article-mime-handles nil))))))
8950      (t
8951       (setq force t)))
8952     (when (and raw (not force)
8953                (member gnus-newsgroup-name '("nndraft:delayed"
8954                                              "nndraft:drafts"
8955                                              "nndraft:queue")))
8956       (error "Can't edit the raw article in group %s"
8957              gnus-newsgroup-name))
8958     (save-excursion
8959       (set-buffer gnus-summary-buffer)
8960       (let ((mail-parse-charset gnus-newsgroup-charset)
8961             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8962         (gnus-set-global-variables)
8963         (when (and (not force)
8964                    (gnus-group-read-only-p))
8965           (error "The current newsgroup does not support article editing"))
8966         (gnus-summary-show-article t)
8967         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8968           (with-current-buffer gnus-article-buffer
8969             (mm-enable-multibyte)))
8970         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
8971             (setq raw t))
8972         (gnus-article-edit-article
8973          (if raw 'ignore
8974            `(lambda ()
8975               (let ((mbl mml-buffer-list))
8976                 (setq mml-buffer-list nil)
8977                 (mime-to-mml ,'current-handles)
8978                 (let ((mbl1 mml-buffer-list))
8979                   (setq mml-buffer-list mbl)
8980                   (set (make-local-variable 'mml-buffer-list) mbl1))
8981                 (make-local-hook 'kill-buffer-hook)
8982                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8983          `(lambda (no-highlight)
8984             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8985                   (message-options message-options)
8986                   (message-options-set-recipient)
8987                   (mail-parse-ignored-charsets
8988                    ',gnus-newsgroup-ignored-charsets))
8989               ,(if (not raw) '(progn
8990                                 (mml-to-mime)
8991                                 (mml-destroy-buffers)
8992                                 (remove-hook 'kill-buffer-hook
8993                                              'mml-destroy-buffers t)
8994                                 (kill-local-variable 'mml-buffer-list)))
8995               (gnus-summary-edit-article-done
8996                ,(or (mail-header-references gnus-current-headers) "")
8997                ,(gnus-group-read-only-p)
8998                ,gnus-summary-buffer no-highlight))))))))
8999
9000 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9001
9002 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9003                                                  no-highlight)
9004   "Make edits to the current article permanent."
9005   (interactive)
9006   (save-excursion
9007    ;; The buffer restriction contains the entire article if it exists.
9008     (when (article-goto-body)
9009       (let ((lines (count-lines (point) (point-max)))
9010             (length (- (point-max) (point)))
9011             (case-fold-search t)
9012             (body (copy-marker (point))))
9013         (goto-char (point-min))
9014         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9015           (delete-region (match-beginning 1) (match-end 1))
9016           (insert (number-to-string length)))
9017         (goto-char (point-min))
9018         (when (re-search-forward
9019                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9020           (delete-region (match-beginning 1) (match-end 1))
9021           (insert (number-to-string length)))
9022         (goto-char (point-min))
9023         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9024           (delete-region (match-beginning 1) (match-end 1))
9025           (insert (number-to-string lines))))))
9026   ;; Replace the article.
9027   (let ((buf (current-buffer)))
9028     (with-temp-buffer
9029       (insert-buffer-substring buf)
9030
9031       (if (and (not read-only)
9032                (not (gnus-request-replace-article
9033                      (cdr gnus-article-current) (car gnus-article-current)
9034                      (current-buffer) t)))
9035           (error "Couldn't replace article")
9036         ;; Update the summary buffer.
9037         (if (and references
9038                  (equal (message-tokenize-header references " ")
9039                         (message-tokenize-header
9040                          (or (message-fetch-field "references") "") " ")))
9041             ;; We only have to update this line.
9042             (save-excursion
9043               (save-restriction
9044                 (message-narrow-to-head)
9045                 (let ((head (buffer-string))
9046                       header)
9047                   (with-temp-buffer
9048                     (insert (format "211 %d Article retrieved.\n"
9049                                     (cdr gnus-article-current)))
9050                     (insert head)
9051                     (insert ".\n")
9052                     (let ((nntp-server-buffer (current-buffer)))
9053                       (setq header (car (gnus-get-newsgroup-headers
9054                                          nil t))))
9055                     (save-excursion
9056                       (set-buffer gnus-summary-buffer)
9057                       (gnus-data-set-header
9058                        (gnus-data-find (cdr gnus-article-current))
9059                        header)
9060                       (gnus-summary-update-article-line
9061                        (cdr gnus-article-current) header)
9062                       (if (gnus-summary-goto-subject
9063                            (cdr gnus-article-current) nil t)
9064                           (gnus-summary-update-secondary-mark
9065                            (cdr gnus-article-current))))))))
9066           ;; Update threads.
9067           (set-buffer (or buffer gnus-summary-buffer))
9068           (gnus-summary-update-article (cdr gnus-article-current))
9069           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9070               (gnus-summary-update-secondary-mark
9071                (cdr gnus-article-current))))
9072         ;; Prettify the article buffer again.
9073         (unless no-highlight
9074           (save-excursion
9075             (set-buffer gnus-article-buffer)
9076             ;;;!!! Fix this -- article should be rehighlighted.
9077             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9078             (set-buffer gnus-original-article-buffer)
9079             (gnus-request-article
9080              (cdr gnus-article-current)
9081              (car gnus-article-current) (current-buffer))))
9082         ;; Prettify the summary buffer line.
9083         (when (gnus-visual-p 'summary-highlight 'highlight)
9084           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9085
9086 (defun gnus-summary-edit-wash (key)
9087   "Perform editing command KEY in the article buffer."
9088   (interactive
9089    (list
9090     (progn
9091       (message "%s" (concat (this-command-keys) "- "))
9092       (read-char))))
9093   (message "")
9094   (gnus-summary-edit-article)
9095   (execute-kbd-macro (concat (this-command-keys) key))
9096   (gnus-article-edit-done))
9097
9098 ;;; Respooling
9099
9100 (defun gnus-summary-respool-query (&optional silent trace)
9101   "Query where the respool algorithm would put this article."
9102   (interactive)
9103   (let (gnus-mark-article-hook)
9104     (gnus-summary-select-article)
9105     (save-excursion
9106       (set-buffer gnus-original-article-buffer)
9107       (save-restriction
9108         (message-narrow-to-head)
9109         (let ((groups (nnmail-article-group 'identity trace)))
9110           (unless silent
9111             (if groups
9112                 (message "This message would go to %s"
9113                          (mapconcat 'car groups ", "))
9114               (message "This message would go to no groups"))
9115             groups))))))
9116
9117 (defun gnus-summary-respool-trace ()
9118   "Trace where the respool algorithm would put this article.
9119 Display a buffer showing all fancy splitting patterns which matched."
9120   (interactive)
9121   (gnus-summary-respool-query nil t))
9122
9123 ;; Summary marking commands.
9124
9125 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9126   "Mark articles which has the same subject as read, and then select the next.
9127 If UNMARK is positive, remove any kind of mark.
9128 If UNMARK is negative, tick articles."
9129   (interactive "P")
9130   (when unmark
9131     (setq unmark (prefix-numeric-value unmark)))
9132   (let ((count
9133          (gnus-summary-mark-same-subject
9134           (gnus-summary-article-subject) unmark)))
9135     ;; Select next unread article.  If auto-select-same mode, should
9136     ;; select the first unread article.
9137     (gnus-summary-next-article t (and gnus-auto-select-same
9138                                       (gnus-summary-article-subject)))
9139     (gnus-message 7 "%d article%s marked as %s"
9140                   count (if (= count 1) " is" "s are")
9141                   (if unmark "unread" "read"))))
9142
9143 (defun gnus-summary-kill-same-subject (&optional unmark)
9144   "Mark articles which has the same subject as read.
9145 If UNMARK is positive, remove any kind of mark.
9146 If UNMARK is negative, tick articles."
9147   (interactive "P")
9148   (when unmark
9149     (setq unmark (prefix-numeric-value unmark)))
9150   (let ((count
9151          (gnus-summary-mark-same-subject
9152           (gnus-summary-article-subject) unmark)))
9153     ;; If marked as read, go to next unread subject.
9154     (when (null unmark)
9155       ;; Go to next unread subject.
9156       (gnus-summary-next-subject 1 t))
9157     (gnus-message 7 "%d articles are marked as %s"
9158                   count (if unmark "unread" "read"))))
9159
9160 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9161   "Mark articles with same SUBJECT as read, and return marked number.
9162 If optional argument UNMARK is positive, remove any kinds of marks.
9163 If optional argument UNMARK is negative, mark articles as unread instead."
9164   (let ((count 1))
9165     (save-excursion
9166       (cond
9167        ((null unmark)                   ; Mark as read.
9168         (while (and
9169                 (progn
9170                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9171                   (gnus-summary-show-thread) t)
9172                 (gnus-summary-find-subject subject))
9173           (setq count (1+ count))))
9174        ((> unmark 0)                    ; Tick.
9175         (while (and
9176                 (progn
9177                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9178                   (gnus-summary-show-thread) t)
9179                 (gnus-summary-find-subject subject))
9180           (setq count (1+ count))))
9181        (t                               ; Mark as unread.
9182         (while (and
9183                 (progn
9184                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9185                   (gnus-summary-show-thread) t)
9186                 (gnus-summary-find-subject subject))
9187           (setq count (1+ count)))))
9188       (gnus-set-mode-line 'summary)
9189       ;; Return the number of marked articles.
9190       count)))
9191
9192 (defun gnus-summary-mark-as-processable (n &optional unmark)
9193   "Set the process mark on the next N articles.
9194 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9195 the process mark instead.  The difference between N and the actual
9196 number of articles marked is returned."
9197   (interactive "P")
9198   (if (and (null n) (gnus-region-active-p))
9199       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9200     (setq n (prefix-numeric-value n))
9201     (let ((backward (< n 0))
9202           (n (abs n)))
9203       (while (and
9204               (> n 0)
9205               (if unmark
9206                   (gnus-summary-remove-process-mark
9207                    (gnus-summary-article-number))
9208                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9209               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9210         (setq n (1- n)))
9211       (when (/= 0 n)
9212         (gnus-message 7 "No more articles"))
9213       (gnus-summary-recenter)
9214       (gnus-summary-position-point)
9215       n)))
9216
9217 (defun gnus-summary-unmark-as-processable (n)
9218   "Remove the process mark from the next N articles.
9219 If N is negative, unmark backward instead.  The difference between N and
9220 the actual number of articles unmarked is returned."
9221   (interactive "P")
9222   (gnus-summary-mark-as-processable n t))
9223
9224 (defun gnus-summary-unmark-all-processable ()
9225   "Remove the process mark from all articles."
9226   (interactive)
9227   (save-excursion
9228     (while gnus-newsgroup-processable
9229       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9230   (gnus-summary-position-point))
9231
9232 (defun gnus-summary-add-mark (article type)
9233   "Mark ARTICLE with a mark of TYPE."
9234   (let ((vtype (car (assq type gnus-article-mark-lists)))
9235         var)
9236     (if (not vtype)
9237         (error "No such mark type: %s" type)
9238       (setq var (intern (format "gnus-newsgroup-%s" type)))
9239       (set var (cons article (symbol-value var)))
9240       (if (memq type '(processable cached replied forwarded recent saved))
9241           (gnus-summary-update-secondary-mark article)
9242         ;;; !!! This is bogus.  We should find out what primary
9243         ;;; !!! mark we want to set.
9244         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9245
9246 (defun gnus-summary-mark-as-expirable (n)
9247   "Mark N articles forward as expirable.
9248 If N is negative, mark backward instead.  The difference between N and
9249 the actual number of articles marked is returned."
9250   (interactive "p")
9251   (gnus-summary-mark-forward n gnus-expirable-mark))
9252
9253 (defun gnus-summary-mark-as-spam (n)
9254   "Mark N articles forward as spam.
9255 If N is negative, mark backward instead.  The difference between N and
9256 the actual number of articles marked is returned."
9257   (interactive "p")
9258   (gnus-summary-mark-forward n gnus-spam-mark))
9259
9260 (defun gnus-summary-mark-article-as-replied (article)
9261   "Mark ARTICLE as replied to and update the summary line.
9262 ARTICLE can also be a list of articles."
9263   (interactive (list (gnus-summary-article-number)))
9264   (let ((articles (if (listp article) article (list article))))
9265     (dolist (article articles)
9266       (push article gnus-newsgroup-replied)
9267       (let ((buffer-read-only nil))
9268         (when (gnus-summary-goto-subject article nil t)
9269           (gnus-summary-update-secondary-mark article))))))
9270
9271 (defun gnus-summary-mark-article-as-forwarded (article)
9272   "Mark ARTICLE as forwarded and update the summary line.
9273 ARTICLE can also be a list of articles."
9274   (let ((articles (if (listp article) article (list article))))
9275     (dolist (article articles)
9276       (push article gnus-newsgroup-forwarded)
9277       (let ((buffer-read-only nil))
9278         (when (gnus-summary-goto-subject article nil t)
9279           (gnus-summary-update-secondary-mark article))))))
9280
9281 (defun gnus-summary-set-bookmark (article)
9282   "Set a bookmark in current article."
9283   (interactive (list (gnus-summary-article-number)))
9284   (when (or (not (get-buffer gnus-article-buffer))
9285             (not gnus-current-article)
9286             (not gnus-article-current)
9287             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9288     (error "No current article selected"))
9289   ;; Remove old bookmark, if one exists.
9290   (let ((old (assq article gnus-newsgroup-bookmarks)))
9291     (when old
9292       (setq gnus-newsgroup-bookmarks
9293             (delq old gnus-newsgroup-bookmarks))))
9294   ;; Set the new bookmark, which is on the form
9295   ;; (article-number . line-number-in-body).
9296   (push
9297    (cons article
9298          (save-excursion
9299            (set-buffer gnus-article-buffer)
9300            (count-lines
9301             (min (point)
9302                  (save-excursion
9303                    (goto-char (point-min))
9304                    (search-forward "\n\n" nil t)
9305                    (point)))
9306             (point))))
9307    gnus-newsgroup-bookmarks)
9308   (gnus-message 6 "A bookmark has been added to the current article."))
9309
9310 (defun gnus-summary-remove-bookmark (article)
9311   "Remove the bookmark from the current article."
9312   (interactive (list (gnus-summary-article-number)))
9313   ;; Remove old bookmark, if one exists.
9314   (let ((old (assq article gnus-newsgroup-bookmarks)))
9315     (if old
9316         (progn
9317           (setq gnus-newsgroup-bookmarks
9318                 (delq old gnus-newsgroup-bookmarks))
9319           (gnus-message 6 "Removed bookmark."))
9320       (gnus-message 6 "No bookmark in current article."))))
9321
9322 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9323 (defun gnus-summary-mark-as-dormant (n)
9324   "Mark N articles forward as dormant.
9325 If N is negative, mark backward instead.  The difference between N and
9326 the actual number of articles marked is returned."
9327   (interactive "p")
9328   (gnus-summary-mark-forward n gnus-dormant-mark))
9329
9330 (defun gnus-summary-set-process-mark (article)
9331   "Set the process mark on ARTICLE and update the summary line."
9332   (setq gnus-newsgroup-processable
9333         (cons article
9334               (delq article gnus-newsgroup-processable)))
9335   (when (gnus-summary-goto-subject article)
9336     (gnus-summary-show-thread)
9337     (gnus-summary-goto-subject article)
9338     (gnus-summary-update-secondary-mark article)))
9339
9340 (defun gnus-summary-remove-process-mark (article)
9341   "Remove the process mark from ARTICLE and update the summary line."
9342   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9343   (when (gnus-summary-goto-subject article)
9344     (gnus-summary-show-thread)
9345     (gnus-summary-goto-subject article)
9346     (gnus-summary-update-secondary-mark article)))
9347
9348 (defun gnus-summary-set-saved-mark (article)
9349   "Set the process mark on ARTICLE and update the summary line."
9350   (push article gnus-newsgroup-saved)
9351   (when (gnus-summary-goto-subject article)
9352     (gnus-summary-update-secondary-mark article)))
9353
9354 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9355   "Mark N articles as read forwards.
9356 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9357 The difference between N and the actual number of articles marked is
9358 returned.
9359 Iff NO-EXPIRE, auto-expiry will be inhibited."
9360   (interactive "p")
9361   (gnus-summary-show-thread)
9362   (let ((backward (< n 0))
9363         (gnus-summary-goto-unread
9364          (and gnus-summary-goto-unread
9365               (not (eq gnus-summary-goto-unread 'never))
9366               (not (memq mark (list gnus-unread-mark
9367                                     gnus-ticked-mark gnus-dormant-mark)))))
9368         (n (abs n))
9369         (mark (or mark gnus-del-mark)))
9370     (while (and (> n 0)
9371                 (gnus-summary-mark-article nil mark no-expire)
9372                 (zerop (gnus-summary-next-subject
9373                         (if backward -1 1)
9374                         (and gnus-summary-goto-unread
9375                              (not (eq gnus-summary-goto-unread 'never)))
9376                         t)))
9377       (setq n (1- n)))
9378     (when (/= 0 n)
9379       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9380     (gnus-summary-recenter)
9381     (gnus-summary-position-point)
9382     (gnus-set-mode-line 'summary)
9383     n))
9384
9385 (defun gnus-summary-mark-article-as-read (mark)
9386   "Mark the current article quickly as read with MARK."
9387   (let ((article (gnus-summary-article-number)))
9388     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9389     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9390     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9391     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9392     (push (cons article mark) gnus-newsgroup-reads)
9393     ;; Possibly remove from cache, if that is used.
9394     (when gnus-use-cache
9395       (gnus-cache-enter-remove-article article))
9396     ;; Allow the backend to change the mark.
9397     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9398     ;; Check for auto-expiry.
9399     (when (and gnus-newsgroup-auto-expire
9400                (memq mark gnus-auto-expirable-marks))
9401       (setq mark gnus-expirable-mark)
9402       ;; Let the backend know about the mark change.
9403       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9404       (push article gnus-newsgroup-expirable))
9405     ;; Set the mark in the buffer.
9406     (gnus-summary-update-mark mark 'unread)
9407     t))
9408
9409 (defun gnus-summary-mark-article-as-unread (mark)
9410   "Mark the current article quickly as unread with MARK."
9411   (let* ((article (gnus-summary-article-number))
9412          (old-mark (gnus-summary-article-mark article)))
9413     ;; Allow the backend to change the mark.
9414     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9415     (if (eq mark old-mark)
9416         t
9417       (if (<= article 0)
9418           (progn
9419             (gnus-error 1 "Can't mark negative article numbers")
9420             nil)
9421         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9422         (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9423         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9424         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9425         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9426         (cond ((= mark gnus-ticked-mark)
9427                (setq gnus-newsgroup-marked
9428                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9429                                               article)))
9430               ((= mark gnus-spam-mark)
9431                (setq gnus-newsgroup-spam-marked
9432                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9433                                               article)))
9434               ((= mark gnus-dormant-mark)
9435                (setq gnus-newsgroup-dormant
9436                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9437                                               article)))
9438               (t
9439                (setq gnus-newsgroup-unreads
9440                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9441                                               article))))
9442         (gnus-pull article gnus-newsgroup-reads)
9443
9444         ;; See whether the article is to be put in the cache.
9445         (and gnus-use-cache
9446              (vectorp (gnus-summary-article-header article))
9447              (save-excursion
9448                (gnus-cache-possibly-enter-article
9449                 gnus-newsgroup-name article
9450                 (= mark gnus-ticked-mark)
9451                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9452
9453         ;; Fix the mark.
9454         (gnus-summary-update-mark mark 'unread)
9455         t))))
9456
9457 (defun gnus-summary-mark-article (&optional article mark no-expire)
9458   "Mark ARTICLE with MARK.  MARK can be any character.
9459 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9460 `??' (dormant) and `?E' (expirable).
9461 If MARK is nil, then the default character `?r' is used.
9462 If ARTICLE is nil, then the article on the current line will be
9463 marked.
9464 Iff NO-EXPIRE, auto-expiry will be inhibited."
9465   ;; The mark might be a string.
9466   (when (stringp mark)
9467     (setq mark (aref mark 0)))
9468   ;; If no mark is given, then we check auto-expiring.
9469   (when (null mark)
9470     (setq mark gnus-del-mark))
9471   (when (and (not no-expire)
9472              gnus-newsgroup-auto-expire
9473              (memq mark gnus-auto-expirable-marks))
9474     (setq mark gnus-expirable-mark))
9475   (let ((article (or article (gnus-summary-article-number)))
9476         (old-mark (gnus-summary-article-mark article)))
9477     ;; Allow the backend to change the mark.
9478     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9479     (if (eq mark old-mark)
9480         t
9481       (unless article
9482         (error "No article on current line"))
9483       (if (not (if (or (= mark gnus-unread-mark)
9484                        (= mark gnus-ticked-mark)
9485                        (= mark gnus-spam-mark)
9486                        (= mark gnus-dormant-mark))
9487                    (gnus-mark-article-as-unread article mark)
9488                  (gnus-mark-article-as-read article mark)))
9489           t
9490         ;; See whether the article is to be put in the cache.
9491         (and gnus-use-cache
9492              (not (= mark gnus-canceled-mark))
9493              (vectorp (gnus-summary-article-header article))
9494              (save-excursion
9495                (gnus-cache-possibly-enter-article
9496                 gnus-newsgroup-name article
9497                 (= mark gnus-ticked-mark)
9498                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9499
9500         (when (gnus-summary-goto-subject article nil t)
9501           (let ((buffer-read-only nil))
9502             (gnus-summary-show-thread)
9503             ;; Fix the mark.
9504             (gnus-summary-update-mark mark 'unread)
9505             t))))))
9506
9507 (defun gnus-summary-update-secondary-mark (article)
9508   "Update the secondary (read, process, cache) mark."
9509   (gnus-summary-update-mark
9510    (cond ((memq article gnus-newsgroup-processable)
9511           gnus-process-mark)
9512          ((memq article gnus-newsgroup-cached)
9513           gnus-cached-mark)
9514          ((memq article gnus-newsgroup-replied)
9515           gnus-replied-mark)
9516          ((memq article gnus-newsgroup-forwarded)
9517           gnus-forwarded-mark)
9518          ((memq article gnus-newsgroup-saved)
9519           gnus-saved-mark)
9520          ((memq article gnus-newsgroup-recent)
9521           gnus-recent-mark)
9522          ((memq article gnus-newsgroup-unseen)
9523           gnus-unseen-mark)
9524          (t gnus-no-mark))
9525    'replied)
9526   (when (gnus-visual-p 'summary-highlight 'highlight)
9527     (gnus-run-hooks 'gnus-summary-update-hook))
9528   t)
9529
9530 (defun gnus-summary-update-mark (mark type)
9531   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9532         (buffer-read-only nil))
9533     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9534     (when forward
9535       (when (looking-at "\r")
9536         (incf forward))
9537       (when (<= (+ forward (point)) (point-max))
9538         ;; Go to the right position on the line.
9539         (goto-char (+ forward (point)))
9540         ;; Replace the old mark with the new mark.
9541         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9542         ;; Optionally update the marks by some user rule.
9543         (when (eq type 'unread)
9544           (gnus-data-set-mark
9545            (gnus-data-find (gnus-summary-article-number)) mark)
9546           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9547
9548 (defun gnus-mark-article-as-read (article &optional mark)
9549   "Enter ARTICLE in the pertinent lists and remove it from others."
9550   ;; Make the article expirable.
9551   (let ((mark (or mark gnus-del-mark)))
9552     (setq gnus-newsgroup-expirable
9553           (if (= mark gnus-expirable-mark)
9554               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9555             (delq article gnus-newsgroup-expirable)))
9556     ;; Remove from unread and marked lists.
9557     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9558     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9559     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9560     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9561     (push (cons article mark) gnus-newsgroup-reads)
9562     ;; Possibly remove from cache, if that is used.
9563     (when gnus-use-cache
9564       (gnus-cache-enter-remove-article article))
9565     t))
9566
9567 (defun gnus-mark-article-as-unread (article &optional mark)
9568   "Enter ARTICLE in the pertinent lists and remove it from others."
9569   (let ((mark (or mark gnus-ticked-mark)))
9570     (if (<= article 0)
9571         (progn
9572           (gnus-error 1 "Can't mark negative article numbers")
9573           nil)
9574       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9575             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9576             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9577             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9578             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9579
9580       ;; Unsuppress duplicates?
9581       (when gnus-suppress-duplicates
9582         (gnus-dup-unsuppress-article article))
9583
9584       (cond ((= mark gnus-ticked-mark)
9585              (setq gnus-newsgroup-marked
9586                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9587             ((= mark gnus-spam-mark)
9588              (setq gnus-newsgroup-spam-marked
9589                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked article)))
9590             ((= mark gnus-dormant-mark)
9591              (setq gnus-newsgroup-dormant
9592                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9593             (t
9594              (setq gnus-newsgroup-unreads
9595                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9596       (gnus-pull article gnus-newsgroup-reads)
9597       t)))
9598
9599 (defalias 'gnus-summary-mark-as-unread-forward
9600   'gnus-summary-tick-article-forward)
9601 (make-obsolete 'gnus-summary-mark-as-unread-forward
9602                'gnus-summary-tick-article-forward)
9603 (defun gnus-summary-tick-article-forward (n)
9604   "Tick N articles forwards.
9605 If N is negative, tick backwards instead.
9606 The difference between N and the number of articles ticked is returned."
9607   (interactive "p")
9608   (gnus-summary-mark-forward n gnus-ticked-mark))
9609
9610 (defalias 'gnus-summary-mark-as-unread-backward
9611   'gnus-summary-tick-article-backward)
9612 (make-obsolete 'gnus-summary-mark-as-unread-backward
9613                'gnus-summary-tick-article-backward)
9614 (defun gnus-summary-tick-article-backward (n)
9615   "Tick N articles backwards.
9616 The difference between N and the number of articles ticked is returned."
9617   (interactive "p")
9618   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9619
9620 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9621 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9622 (defun gnus-summary-tick-article (&optional article clear-mark)
9623   "Mark current article as unread.
9624 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9625 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9626   (interactive)
9627   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9628                                        gnus-ticked-mark)))
9629
9630 (defun gnus-summary-mark-as-read-forward (n)
9631   "Mark N articles as read forwards.
9632 If N is negative, mark backwards instead.
9633 The difference between N and the actual number of articles marked is
9634 returned."
9635   (interactive "p")
9636   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9637
9638 (defun gnus-summary-mark-as-read-backward (n)
9639   "Mark the N articles as read backwards.
9640 The difference between N and the actual number of articles marked is
9641 returned."
9642   (interactive "p")
9643   (gnus-summary-mark-forward
9644    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9645
9646 (defun gnus-summary-mark-as-read (&optional article mark)
9647   "Mark current article as read.
9648 ARTICLE specifies the article to be marked as read.
9649 MARK specifies a string to be inserted at the beginning of the line."
9650   (gnus-summary-mark-article article mark))
9651
9652 (defun gnus-summary-clear-mark-forward (n)
9653   "Clear marks from N articles forward.
9654 If N is negative, clear backward instead.
9655 The difference between N and the number of marks cleared is returned."
9656   (interactive "p")
9657   (gnus-summary-mark-forward n gnus-unread-mark))
9658
9659 (defun gnus-summary-clear-mark-backward (n)
9660   "Clear marks from N articles backward.
9661 The difference between N and the number of marks cleared is returned."
9662   (interactive "p")
9663   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9664
9665 (defun gnus-summary-mark-unread-as-read ()
9666   "Intended to be used by `gnus-summary-mark-article-hook'."
9667   (when (memq gnus-current-article gnus-newsgroup-unreads)
9668     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9669
9670 (defun gnus-summary-mark-read-and-unread-as-read ()
9671   "Intended to be used by `gnus-summary-mark-article-hook'."
9672   (let ((mark (gnus-summary-article-mark)))
9673     (when (or (gnus-unread-mark-p mark)
9674               (gnus-read-mark-p mark))
9675       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9676
9677 (defun gnus-summary-mark-unread-as-ticked ()
9678   "Intended to be used by `gnus-summary-mark-article-hook'."
9679   (when (memq gnus-current-article gnus-newsgroup-unreads)
9680     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9681
9682 (defun gnus-summary-mark-region-as-read (point mark all)
9683   "Mark all unread articles between point and mark as read.
9684 If given a prefix, mark all articles between point and mark as read,
9685 even ticked and dormant ones."
9686   (interactive "r\nP")
9687   (save-excursion
9688     (let (article)
9689       (goto-char point)
9690       (beginning-of-line)
9691       (while (and
9692               (< (point) mark)
9693               (progn
9694                 (when (or all
9695                           (memq (setq article (gnus-summary-article-number))
9696                                 gnus-newsgroup-unreads))
9697                   (gnus-summary-mark-article article gnus-del-mark))
9698                 t)
9699               (gnus-summary-find-next))))))
9700
9701 (defun gnus-summary-mark-below (score mark)
9702   "Mark articles with score less than SCORE with MARK."
9703   (interactive "P\ncMark: ")
9704   (setq score (if score
9705                   (prefix-numeric-value score)
9706                 (or gnus-summary-default-score 0)))
9707   (save-excursion
9708     (set-buffer gnus-summary-buffer)
9709     (goto-char (point-min))
9710     (while
9711         (progn
9712           (and (< (gnus-summary-article-score) score)
9713                (gnus-summary-mark-article nil mark))
9714           (gnus-summary-find-next)))))
9715
9716 (defun gnus-summary-kill-below (&optional score)
9717   "Mark articles with score below SCORE as read."
9718   (interactive "P")
9719   (gnus-summary-mark-below score gnus-killed-mark))
9720
9721 (defun gnus-summary-clear-above (&optional score)
9722   "Clear all marks from articles with score above SCORE."
9723   (interactive "P")
9724   (gnus-summary-mark-above score gnus-unread-mark))
9725
9726 (defun gnus-summary-tick-above (&optional score)
9727   "Tick all articles with score above SCORE."
9728   (interactive "P")
9729   (gnus-summary-mark-above score gnus-ticked-mark))
9730
9731 (defun gnus-summary-mark-above (score mark)
9732   "Mark articles with score over SCORE with MARK."
9733   (interactive "P\ncMark: ")
9734   (setq score (if score
9735                   (prefix-numeric-value score)
9736                 (or gnus-summary-default-score 0)))
9737   (save-excursion
9738     (set-buffer gnus-summary-buffer)
9739     (goto-char (point-min))
9740     (while (and (progn
9741                   (when (> (gnus-summary-article-score) score)
9742                     (gnus-summary-mark-article nil mark))
9743                   t)
9744                 (gnus-summary-find-next)))))
9745
9746 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9747 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9748 (defun gnus-summary-limit-include-expunged (&optional no-error)
9749   "Display all the hidden articles that were expunged for low scores."
9750   (interactive)
9751   (let ((buffer-read-only nil))
9752     (let ((scored gnus-newsgroup-scored)
9753           headers h)
9754       (while scored
9755         (unless (gnus-summary-article-header (caar scored))
9756           (and (setq h (gnus-number-to-header (caar scored)))
9757                (< (cdar scored) gnus-summary-expunge-below)
9758                (push h headers)))
9759         (setq scored (cdr scored)))
9760       (if (not headers)
9761           (when (not no-error)
9762             (error "No expunged articles hidden"))
9763         (goto-char (point-min))
9764         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9765         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9766         (mapcar (lambda (x) (push (mail-header-number x)
9767                                   gnus-newsgroup-limit))
9768                 headers)
9769         (gnus-summary-prepare-unthreaded (nreverse headers))
9770         (goto-char (point-min))
9771         (gnus-summary-position-point)
9772         t))))
9773
9774 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9775   "Mark all unread articles in this newsgroup as read.
9776 If prefix argument ALL is non-nil, ticked and dormant articles will
9777 also be marked as read.
9778 If QUIETLY is non-nil, no questions will be asked.
9779 If TO-HERE is non-nil, it should be a point in the buffer.  All
9780 articles before (after, if REVERSE is set) this point will be marked as read.
9781 Note that this function will only catch up the unread article
9782 in the current summary buffer limitation.
9783 The number of articles marked as read is returned."
9784   (interactive "P")
9785   (prog1
9786       (save-excursion
9787         (when (or quietly
9788                   (not gnus-interactive-catchup) ;Without confirmation?
9789                   gnus-expert-user
9790                   (gnus-y-or-n-p
9791                    (if all
9792                        "Mark absolutely all articles as read? "
9793                      "Mark all unread articles as read? ")))
9794           (if (and not-mark
9795                    (not gnus-newsgroup-adaptive)
9796                    (not gnus-newsgroup-auto-expire)
9797                    (not gnus-suppress-duplicates)
9798                    (or (not gnus-use-cache)
9799                        (eq gnus-use-cache 'passive)))
9800               (progn
9801                 (when all
9802                   (setq gnus-newsgroup-marked nil
9803                         gnus-newsgroup-spam-marked nil
9804                         gnus-newsgroup-dormant nil))
9805                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9806             ;; We actually mark all articles as canceled, which we
9807             ;; have to do when using auto-expiry or adaptive scoring.
9808             (gnus-summary-show-all-threads)
9809             (if (and to-here reverse)
9810                 (progn
9811                   (goto-char to-here)
9812                   (while (and
9813                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9814                           (gnus-summary-find-next (not all) nil nil t))))
9815               (when (gnus-summary-first-subject (not all) t)
9816                 (while (and
9817                         (if to-here (< (point) to-here) t)
9818                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9819                         (gnus-summary-find-next (not all) nil nil t)))))
9820             (gnus-set-mode-line 'summary))
9821           t))
9822     (gnus-summary-position-point)))
9823
9824 (defun gnus-summary-catchup-to-here (&optional all)
9825   "Mark all unticked articles before the current one as read.
9826 If ALL is non-nil, also mark ticked and dormant articles as read."
9827   (interactive "P")
9828   (save-excursion
9829     (gnus-save-hidden-threads
9830       (let ((beg (point)))
9831         ;; We check that there are unread articles.
9832         (when (or all (gnus-summary-find-prev))
9833           (gnus-summary-catchup all t beg)))))
9834   (gnus-summary-position-point))
9835
9836 (defun gnus-summary-catchup-from-here (&optional all)
9837   "Mark all unticked articles after the current one as read.
9838 If ALL is non-nil, also mark ticked and dormant articles as read."
9839   (interactive "P")
9840   (save-excursion
9841     (gnus-save-hidden-threads
9842       (let ((beg (point)))
9843         ;; We check that there are unread articles.
9844         (when (or all (gnus-summary-find-next))
9845           (gnus-summary-catchup all t beg nil t)))))
9846
9847   (gnus-summary-position-point))
9848 (defun gnus-summary-catchup-all (&optional quietly)
9849   "Mark all articles in this newsgroup as read."
9850   (interactive "P")
9851   (gnus-summary-catchup t quietly))
9852
9853 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9854   "Mark all unread articles in this group as read, then exit.
9855 If prefix argument ALL is non-nil, all articles are marked as read.
9856 If QUIETLY is non-nil, no questions will be asked."
9857   (interactive "P")
9858   (when (gnus-summary-catchup all quietly nil 'fast)
9859     ;; Select next newsgroup or exit.
9860     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9861              (eq gnus-auto-select-next 'quietly))
9862         (gnus-summary-next-group nil)
9863       (gnus-summary-exit))))
9864
9865 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9866   "Mark all articles in this newsgroup as read, and then exit."
9867   (interactive "P")
9868   (gnus-summary-catchup-and-exit t quietly))
9869
9870 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9871   "Mark all articles in this group as read and select the next group.
9872 If given a prefix, mark all articles, unread as well as ticked, as
9873 read."
9874   (interactive "P")
9875   (save-excursion
9876     (gnus-summary-catchup all))
9877   (gnus-summary-next-group))
9878
9879 ;;;
9880 ;;; with article
9881 ;;;
9882
9883 (defmacro gnus-with-article (article &rest forms)
9884   "Select ARTICLE and perform FORMS in the original article buffer.
9885 Then replace the article with the result."
9886   `(progn
9887      ;; We don't want the article to be marked as read.
9888      (let (gnus-mark-article-hook)
9889        (gnus-summary-select-article t t nil ,article))
9890      (set-buffer gnus-original-article-buffer)
9891      ,@forms
9892      (if (not (gnus-check-backend-function
9893                'request-replace-article (car gnus-article-current)))
9894          (gnus-message 5 "Read-only group; not replacing")
9895        (unless (gnus-request-replace-article
9896                 ,article (car gnus-article-current)
9897                 (current-buffer) t)
9898          (error "Couldn't replace article")))
9899      ;; The cache and backlog have to be flushed somewhat.
9900      (when gnus-keep-backlog
9901        (gnus-backlog-remove-article
9902         (car gnus-article-current) (cdr gnus-article-current)))
9903      (when gnus-use-cache
9904        (gnus-cache-update-article
9905         (car gnus-article-current) (cdr gnus-article-current)))))
9906
9907 (put 'gnus-with-article 'lisp-indent-function 1)
9908 (put 'gnus-with-article 'edebug-form-spec '(form body))
9909
9910 ;; Thread-based commands.
9911
9912 (defun gnus-summary-articles-in-thread (&optional article)
9913   "Return a list of all articles in the current thread.
9914 If ARTICLE is non-nil, return all articles in the thread that starts
9915 with that article."
9916   (let* ((article (or article (gnus-summary-article-number)))
9917          (data (gnus-data-find-list article))
9918          (top-level (gnus-data-level (car data)))
9919          (top-subject
9920           (cond ((null gnus-thread-operation-ignore-subject)
9921                  (gnus-simplify-subject-re
9922                   (mail-header-subject (gnus-data-header (car data)))))
9923                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9924                  (gnus-simplify-subject-fuzzy
9925                   (mail-header-subject (gnus-data-header (car data)))))
9926                 (t nil)))
9927          (end-point (save-excursion
9928                       (if (gnus-summary-go-to-next-thread)
9929                           (point) (point-max))))
9930          articles)
9931     (while (and data
9932                 (< (gnus-data-pos (car data)) end-point))
9933       (when (or (not top-subject)
9934                 (string= top-subject
9935                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9936                              (gnus-simplify-subject-fuzzy
9937                               (mail-header-subject
9938                                (gnus-data-header (car data))))
9939                            (gnus-simplify-subject-re
9940                             (mail-header-subject
9941                              (gnus-data-header (car data)))))))
9942         (push (gnus-data-number (car data)) articles))
9943       (unless (and (setq data (cdr data))
9944                    (> (gnus-data-level (car data)) top-level))
9945         (setq data nil)))
9946     ;; Return the list of articles.
9947     (nreverse articles)))
9948
9949 (defun gnus-summary-rethread-current ()
9950   "Rethread the thread the current article is part of."
9951   (interactive)
9952   (let* ((gnus-show-threads t)
9953          (article (gnus-summary-article-number))
9954          (id (mail-header-id (gnus-summary-article-header)))
9955          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9956     (unless id
9957       (error "No article on the current line"))
9958     (gnus-rebuild-thread id)
9959     (gnus-summary-goto-subject article)))
9960
9961 (defun gnus-summary-reparent-thread ()
9962   "Make the current article child of the marked (or previous) article.
9963
9964 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9965 is non-nil or the Subject: of both articles are the same."
9966   (interactive)
9967   (unless (not (gnus-group-read-only-p))
9968     (error "The current newsgroup does not support article editing"))
9969   (unless (<= (length gnus-newsgroup-processable) 1)
9970     (error "No more than one article may be marked"))
9971   (save-window-excursion
9972     (let ((gnus-article-buffer " *reparent*")
9973           (current-article (gnus-summary-article-number))
9974           ;; First grab the marked article, otherwise one line up.
9975           (parent-article (if (not (null gnus-newsgroup-processable))
9976                               (car gnus-newsgroup-processable)
9977                             (save-excursion
9978                               (if (eq (forward-line -1) 0)
9979                                   (gnus-summary-article-number)
9980                                 (error "Beginning of summary buffer"))))))
9981       (unless (not (eq current-article parent-article))
9982         (error "An article may not be self-referential"))
9983       (let ((message-id (mail-header-id
9984                          (gnus-summary-article-header parent-article))))
9985         (unless (and message-id (not (equal message-id "")))
9986           (error "No message-id in desired parent"))
9987         (gnus-with-article current-article
9988           (save-restriction
9989             (goto-char (point-min))
9990             (message-narrow-to-head)
9991             (if (re-search-forward "^References: " nil t)
9992                 (progn
9993                   (re-search-forward "^[^ \t]" nil t)
9994                   (forward-line -1)
9995                   (end-of-line)
9996                   (insert " " message-id))
9997               (insert "References: " message-id "\n"))))
9998         (set-buffer gnus-summary-buffer)
9999         (gnus-summary-unmark-all-processable)
10000         (gnus-summary-update-article current-article)
10001         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10002             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10003         (gnus-summary-rethread-current)
10004         (gnus-message 3 "Article %d is now the child of article %d"
10005                       current-article parent-article)))))
10006
10007 (defun gnus-summary-toggle-threads (&optional arg)
10008   "Toggle showing conversation threads.
10009 If ARG is positive number, turn showing conversation threads on."
10010   (interactive "P")
10011   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10012     (setq gnus-show-threads
10013           (if (null arg) (not gnus-show-threads)
10014             (> (prefix-numeric-value arg) 0)))
10015     (gnus-summary-prepare)
10016     (gnus-summary-goto-subject current)
10017     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10018     (gnus-summary-position-point)))
10019
10020 (defun gnus-summary-show-all-threads ()
10021   "Show all threads."
10022   (interactive)
10023   (save-excursion
10024     (let ((buffer-read-only nil))
10025       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10026   (gnus-summary-position-point))
10027
10028 (defun gnus-summary-show-thread ()
10029   "Show thread subtrees.
10030 Returns nil if no thread was there to be shown."
10031   (interactive)
10032   (let ((buffer-read-only nil)
10033         (orig (point))
10034         ;; first goto end then to beg, to have point at beg after let
10035         (end (progn (end-of-line) (point)))
10036         (beg (progn (beginning-of-line) (point))))
10037     (prog1
10038         ;; Any hidden lines here?
10039         (search-forward "\r" end t)
10040       (subst-char-in-region beg end ?\^M ?\n t)
10041       (goto-char orig)
10042       (gnus-summary-position-point))))
10043
10044 (defun gnus-summary-maybe-hide-threads ()
10045   "If requested, hide the threads that should be hidden."
10046   (when (and gnus-show-threads
10047              gnus-thread-hide-subtree)
10048     (gnus-summary-hide-all-threads
10049      (if (or (consp gnus-thread-hide-subtree)
10050              (gnus-functionp gnus-thread-hide-subtree))
10051          (gnus-make-predicate gnus-thread-hide-subtree)
10052        nil))))
10053
10054 ;;; Hiding predicates.
10055
10056 (defun gnus-article-unread-p (header)
10057   (memq (mail-header-number header) gnus-newsgroup-unreads))
10058
10059 (defun gnus-article-unseen-p (header)
10060   (memq (mail-header-number header) gnus-newsgroup-unseen))
10061
10062 (defun gnus-map-articles (predicate articles)
10063   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10064   (apply 'gnus-or (mapcar predicate
10065                           (mapcar 'gnus-summary-article-header articles))))
10066
10067 (defun gnus-summary-hide-all-threads (&optional predicate)
10068   "Hide all thread subtrees.
10069 If PREDICATE is supplied, threads that satisfy this predicate
10070 will not be hidden."
10071   (interactive)
10072   (save-excursion
10073     (goto-char (point-min))
10074     (let ((end nil))
10075       (while (not end)
10076         (when (or (not predicate)
10077                   (gnus-map-articles
10078                    predicate (gnus-summary-article-children)))
10079             (gnus-summary-hide-thread))
10080         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10081   (gnus-summary-position-point))
10082
10083 (defun gnus-summary-hide-thread ()
10084   "Hide thread subtrees.
10085 If PREDICATE is supplied, threads that satisfy this predicate
10086 will not be hidden.
10087 Returns nil if no threads were there to be hidden."
10088   (interactive)
10089   (let ((buffer-read-only nil)
10090         (start (point))
10091         (article (gnus-summary-article-number)))
10092     (goto-char start)
10093     ;; Go forward until either the buffer ends or the subthread
10094     ;; ends.
10095     (when (and (not (eobp))
10096                (or (zerop (gnus-summary-next-thread 1 t))
10097                    (goto-char (point-max))))
10098       (prog1
10099           (if (and (> (point) start)
10100                    (search-backward "\n" start t))
10101               (progn
10102                 (subst-char-in-region start (point) ?\n ?\^M)
10103                 (gnus-summary-goto-subject article))
10104             (goto-char start)
10105             nil)))))
10106
10107 (defun gnus-summary-go-to-next-thread (&optional previous)
10108   "Go to the same level (or less) next thread.
10109 If PREVIOUS is non-nil, go to previous thread instead.
10110 Return the article number moved to, or nil if moving was impossible."
10111   (let ((level (gnus-summary-thread-level))
10112         (way (if previous -1 1))
10113         (beg (point)))
10114     (forward-line way)
10115     (while (and (not (eobp))
10116                 (< level (gnus-summary-thread-level)))
10117       (forward-line way))
10118     (if (eobp)
10119         (progn
10120           (goto-char beg)
10121           nil)
10122       (setq beg (point))
10123       (prog1
10124           (gnus-summary-article-number)
10125         (goto-char beg)))))
10126
10127 (defun gnus-summary-next-thread (n &optional silent)
10128   "Go to the same level next N'th thread.
10129 If N is negative, search backward instead.
10130 Returns the difference between N and the number of skips actually
10131 done.
10132
10133 If SILENT, don't output messages."
10134   (interactive "p")
10135   (let ((backward (< n 0))
10136         (n (abs n)))
10137     (while (and (> n 0)
10138                 (gnus-summary-go-to-next-thread backward))
10139       (decf n))
10140     (unless silent
10141       (gnus-summary-position-point))
10142     (when (and (not silent) (/= 0 n))
10143       (gnus-message 7 "No more threads"))
10144     n))
10145
10146 (defun gnus-summary-prev-thread (n)
10147   "Go to the same level previous N'th thread.
10148 Returns the difference between N and the number of skips actually
10149 done."
10150   (interactive "p")
10151   (gnus-summary-next-thread (- n)))
10152
10153 (defun gnus-summary-go-down-thread ()
10154   "Go down one level in the current thread."
10155   (let ((children (gnus-summary-article-children)))
10156     (when children
10157       (gnus-summary-goto-subject (car children)))))
10158
10159 (defun gnus-summary-go-up-thread ()
10160   "Go up one level in the current thread."
10161   (let ((parent (gnus-summary-article-parent)))
10162     (when parent
10163       (gnus-summary-goto-subject parent))))
10164
10165 (defun gnus-summary-down-thread (n)
10166   "Go down thread N steps.
10167 If N is negative, go up instead.
10168 Returns the difference between N and how many steps down that were
10169 taken."
10170   (interactive "p")
10171   (let ((up (< n 0))
10172         (n (abs n)))
10173     (while (and (> n 0)
10174                 (if up (gnus-summary-go-up-thread)
10175                   (gnus-summary-go-down-thread)))
10176       (setq n (1- n)))
10177     (gnus-summary-position-point)
10178     (when (/= 0 n)
10179       (gnus-message 7 "Can't go further"))
10180     n))
10181
10182 (defun gnus-summary-up-thread (n)
10183   "Go up thread N steps.
10184 If N is negative, go down instead.
10185 Returns the difference between N and how many steps down that were
10186 taken."
10187   (interactive "p")
10188   (gnus-summary-down-thread (- n)))
10189
10190 (defun gnus-summary-top-thread ()
10191   "Go to the top of the thread."
10192   (interactive)
10193   (while (gnus-summary-go-up-thread))
10194   (gnus-summary-article-number))
10195
10196 (defun gnus-summary-kill-thread (&optional unmark)
10197   "Mark articles under current thread as read.
10198 If the prefix argument is positive, remove any kinds of marks.
10199 If the prefix argument is negative, tick articles instead."
10200   (interactive "P")
10201   (when unmark
10202     (setq unmark (prefix-numeric-value unmark)))
10203   (let ((articles (gnus-summary-articles-in-thread)))
10204     (save-excursion
10205       ;; Expand the thread.
10206       (gnus-summary-show-thread)
10207       ;; Mark all the articles.
10208       (while articles
10209         (gnus-summary-goto-subject (car articles))
10210         (cond ((null unmark)
10211                (gnus-summary-mark-article-as-read gnus-killed-mark))
10212               ((> unmark 0)
10213                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10214               (t
10215                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10216         (setq articles (cdr articles))))
10217     ;; Hide killed subtrees.
10218     (and (null unmark)
10219          gnus-thread-hide-killed
10220          (gnus-summary-hide-thread))
10221     ;; If marked as read, go to next unread subject.
10222     (when (null unmark)
10223       ;; Go to next unread subject.
10224       (gnus-summary-next-subject 1 t)))
10225   (gnus-set-mode-line 'summary))
10226
10227 ;; Summary sorting commands
10228
10229 (defun gnus-summary-sort-by-number (&optional reverse)
10230   "Sort the summary buffer by article number.
10231 Argument REVERSE means reverse order."
10232   (interactive "P")
10233   (gnus-summary-sort 'number reverse))
10234
10235 (defun gnus-summary-sort-by-random (&optional reverse)
10236   "Randomize the order in the summary buffer.
10237 Argument REVERSE means to randomize in reverse order."
10238   (interactive "P")
10239   (gnus-summary-sort 'random reverse))
10240
10241 (defun gnus-summary-sort-by-author (&optional reverse)
10242   "Sort the summary buffer by author name alphabetically.
10243 If `case-fold-search' is non-nil, case of letters is ignored.
10244 Argument REVERSE means reverse order."
10245   (interactive "P")
10246   (gnus-summary-sort 'author reverse))
10247
10248 (defun gnus-summary-sort-by-subject (&optional reverse)
10249   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10250 If `case-fold-search' is non-nil, case of letters is ignored.
10251 Argument REVERSE means reverse order."
10252   (interactive "P")
10253   (gnus-summary-sort 'subject reverse))
10254
10255 (defun gnus-summary-sort-by-date (&optional reverse)
10256   "Sort the summary buffer by date.
10257 Argument REVERSE means reverse order."
10258   (interactive "P")
10259   (gnus-summary-sort 'date reverse))
10260
10261 (defun gnus-summary-sort-by-score (&optional reverse)
10262   "Sort the summary buffer by score.
10263 Argument REVERSE means reverse order."
10264   (interactive "P")
10265   (gnus-summary-sort 'score reverse))
10266
10267 (defun gnus-summary-sort-by-lines (&optional reverse)
10268   "Sort the summary buffer by the number of lines.
10269 Argument REVERSE means reverse order."
10270   (interactive "P")
10271   (gnus-summary-sort 'lines reverse))
10272
10273 (defun gnus-summary-sort-by-chars (&optional reverse)
10274   "Sort the summary buffer by article length.
10275 Argument REVERSE means reverse order."
10276   (interactive "P")
10277   (gnus-summary-sort 'chars reverse))
10278
10279 (defun gnus-summary-sort-by-original (&optional reverse)
10280   "Sort the summary buffer using the default sorting method.
10281 Argument REVERSE means reverse order."
10282   (interactive "P")
10283   (let* ((buffer-read-only)
10284          (gnus-summary-prepare-hook nil))
10285     ;; We do the sorting by regenerating the threads.
10286     (gnus-summary-prepare)
10287     ;; Hide subthreads if needed.
10288     (gnus-summary-maybe-hide-threads)))
10289
10290 (defun gnus-summary-sort (predicate reverse)
10291   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10292   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10293          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10294          (gnus-thread-sort-functions
10295           (if (not reverse)
10296               thread
10297             `(lambda (t1 t2)
10298                (,thread t2 t1))))
10299          (gnus-sort-gathered-threads-function
10300           gnus-thread-sort-functions)
10301          (gnus-article-sort-functions
10302           (if (not reverse)
10303               article
10304             `(lambda (t1 t2)
10305                (,article t2 t1))))
10306          (buffer-read-only)
10307          (gnus-summary-prepare-hook nil))
10308     ;; We do the sorting by regenerating the threads.
10309     (gnus-summary-prepare)
10310     ;; Hide subthreads if needed.
10311     (gnus-summary-maybe-hide-threads)))
10312
10313 ;; Summary saving commands.
10314
10315 (defun gnus-summary-save-article (&optional n not-saved)
10316   "Save the current article using the default saver function.
10317 If N is a positive number, save the N next articles.
10318 If N is a negative number, save the N previous articles.
10319 If N is nil and any articles have been marked with the process mark,
10320 save those articles instead.
10321 The variable `gnus-default-article-saver' specifies the saver function."
10322   (interactive "P")
10323   (let* ((articles (gnus-summary-work-articles n))
10324          (save-buffer (save-excursion
10325                         (nnheader-set-temp-buffer " *Gnus Save*")))
10326          (num (length articles))
10327          header file)
10328     (dolist (article articles)
10329       (setq header (gnus-summary-article-header article))
10330       (if (not (vectorp header))
10331           ;; This is a pseudo-article.
10332           (if (assq 'name header)
10333               (gnus-copy-file (cdr (assq 'name header)))
10334             (gnus-message 1 "Article %d is unsaveable" article))
10335         ;; This is a real article.
10336         (save-window-excursion
10337           (let ((gnus-display-mime-function nil)
10338                 (gnus-article-prepare-hook nil))
10339             (gnus-summary-select-article t nil nil article)))
10340         (save-excursion
10341           (set-buffer save-buffer)
10342           (erase-buffer)
10343           (insert-buffer-substring gnus-original-article-buffer))
10344         (setq file (gnus-article-save save-buffer file num))
10345         (gnus-summary-remove-process-mark article)
10346         (unless not-saved
10347           (gnus-summary-set-saved-mark article))))
10348     (gnus-kill-buffer save-buffer)
10349     (gnus-summary-position-point)
10350     (gnus-set-mode-line 'summary)
10351     n))
10352
10353 (defun gnus-summary-pipe-output (&optional arg)
10354   "Pipe the current article to a subprocess.
10355 If N is a positive number, pipe the N next articles.
10356 If N is a negative number, pipe the N previous articles.
10357 If N is nil and any articles have been marked with the process mark,
10358 pipe those articles instead."
10359   (interactive "P")
10360   (require 'gnus-art)
10361   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10362     (gnus-summary-save-article arg t))
10363   (let ((buffer (get-buffer "*Shell Command Output*")))
10364     (if (and buffer
10365              (with-current-buffer buffer (> (point-max) (point-min))))
10366         (gnus-configure-windows 'pipe))))
10367
10368 (defun gnus-summary-save-article-mail (&optional arg)
10369   "Append the current article to an mail file.
10370 If N is a positive number, save the N next articles.
10371 If N is a negative number, save the N previous articles.
10372 If N is nil and any articles have been marked with the process mark,
10373 save those articles instead."
10374   (interactive "P")
10375   (require 'gnus-art)
10376   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10377     (gnus-summary-save-article arg)))
10378
10379 (defun gnus-summary-save-article-rmail (&optional arg)
10380   "Append the current article to an rmail file.
10381 If N is a positive number, save the N next articles.
10382 If N is a negative number, save the N previous articles.
10383 If N is nil and any articles have been marked with the process mark,
10384 save those articles instead."
10385   (interactive "P")
10386   (require 'gnus-art)
10387   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10388     (gnus-summary-save-article arg)))
10389
10390 (defun gnus-summary-save-article-file (&optional arg)
10391   "Append the current article to a file.
10392 If N is a positive number, save the N next articles.
10393 If N is a negative number, save the N previous articles.
10394 If N is nil and any articles have been marked with the process mark,
10395 save those articles instead."
10396   (interactive "P")
10397   (require 'gnus-art)
10398   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10399     (gnus-summary-save-article arg)))
10400
10401 (defun gnus-summary-write-article-file (&optional arg)
10402   "Write the current article to a file, deleting the previous file.
10403 If N is a positive number, save the N next articles.
10404 If N is a negative number, save the N previous articles.
10405 If N is nil and any articles have been marked with the process mark,
10406 save those articles instead."
10407   (interactive "P")
10408   (require 'gnus-art)
10409   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10410     (gnus-summary-save-article arg)))
10411
10412 (defun gnus-summary-save-article-body-file (&optional arg)
10413   "Append the current article body to a file.
10414 If N is a positive number, save the N next articles.
10415 If N is a negative number, save the N previous articles.
10416 If N is nil and any articles have been marked with the process mark,
10417 save those articles instead."
10418   (interactive "P")
10419   (require 'gnus-art)
10420   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10421     (gnus-summary-save-article arg)))
10422
10423 (defun gnus-summary-muttprint (&optional arg)
10424   "Print the current article using Muttprint.
10425 If N is a positive number, save the N next articles.
10426 If N is a negative number, save the N previous articles.
10427 If N is nil and any articles have been marked with the process mark,
10428 save those articles instead."
10429   (interactive "P")
10430   (require 'gnus-art)
10431   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10432     (gnus-summary-save-article arg t)))
10433
10434 (defun gnus-summary-pipe-message (program)
10435   "Pipe the current article through PROGRAM."
10436   (interactive "sProgram: ")
10437   (gnus-summary-select-article)
10438   (let ((mail-header-separator ""))
10439     (gnus-eval-in-buffer-window gnus-article-buffer
10440       (save-restriction
10441         (widen)
10442         (let ((start (window-start))
10443               buffer-read-only)
10444           (message-pipe-buffer-body program)
10445           (set-window-start (get-buffer-window (current-buffer)) start))))))
10446
10447 (defun gnus-get-split-value (methods)
10448   "Return a value based on the split METHODS."
10449   (let (split-name method result match)
10450     (when methods
10451       (save-excursion
10452         (set-buffer gnus-original-article-buffer)
10453         (save-restriction
10454           (nnheader-narrow-to-headers)
10455           (while (and methods (not split-name))
10456             (goto-char (point-min))
10457             (setq method (pop methods))
10458             (setq match (car method))
10459             (when (cond
10460                    ((stringp match)
10461                     ;; Regular expression.
10462                     (ignore-errors
10463                       (re-search-forward match nil t)))
10464                    ((gnus-functionp match)
10465                     ;; Function.
10466                     (save-restriction
10467                       (widen)
10468                       (setq result (funcall match gnus-newsgroup-name))))
10469                    ((consp match)
10470                     ;; Form.
10471                     (save-restriction
10472                       (widen)
10473                       (setq result (eval match)))))
10474               (setq split-name (cdr method))
10475               (cond ((stringp result)
10476                      (push (expand-file-name
10477                             result gnus-article-save-directory)
10478                            split-name))
10479                     ((consp result)
10480                      (setq split-name (append result split-name)))))))))
10481     (nreverse split-name)))
10482
10483 (defun gnus-valid-move-group-p (group)
10484   (and (boundp group)
10485        (symbol-name group)
10486        (symbol-value group)
10487        (gnus-get-function (gnus-find-method-for-group
10488                            (symbol-name group)) 'request-accept-article t)))
10489
10490 (defun gnus-read-move-group-name (prompt default articles prefix)
10491   "Read a group name."
10492   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10493          (minibuffer-confirm-incomplete nil) ; XEmacs
10494          (prom
10495           (format "%s %s to:"
10496                   prompt
10497                   (if (> (length articles) 1)
10498                       (format "these %d articles" (length articles))
10499                     "this article")))
10500          (to-newsgroup
10501           (cond
10502            ((null split-name)
10503             (gnus-completing-read-with-default
10504              default prom
10505              gnus-active-hashtb
10506              'gnus-valid-move-group-p
10507              nil prefix
10508              'gnus-group-history))
10509            ((= 1 (length split-name))
10510             (gnus-completing-read-with-default
10511              (car split-name) prom
10512              gnus-active-hashtb
10513              'gnus-valid-move-group-p
10514              nil nil
10515              'gnus-group-history))
10516            (t
10517             (gnus-completing-read-with-default
10518              nil prom
10519              (mapcar (lambda (el) (list el))
10520                      (nreverse split-name))
10521              nil nil nil
10522              'gnus-group-history))))
10523          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10524     (when to-newsgroup
10525       (if (or (string= to-newsgroup "")
10526               (string= to-newsgroup prefix))
10527           (setq to-newsgroup default))
10528       (unless to-newsgroup
10529         (error "No group name entered"))
10530       (or (gnus-active to-newsgroup)
10531           (gnus-activate-group to-newsgroup nil nil to-method)
10532           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10533                                      to-newsgroup))
10534               (or (and (gnus-request-create-group to-newsgroup to-method)
10535                        (gnus-activate-group
10536                         to-newsgroup nil nil to-method)
10537                        (gnus-subscribe-group to-newsgroup))
10538                   (error "Couldn't create group %s" to-newsgroup)))
10539           (error "No such group: %s" to-newsgroup)))
10540     to-newsgroup))
10541
10542 (defun gnus-summary-save-parts (type dir n &optional reverse)
10543   "Save parts matching TYPE to DIR.
10544 If REVERSE, save parts that do not match TYPE."
10545   (interactive
10546    (list (read-string "Save parts of type: "
10547                       (or (car gnus-summary-save-parts-type-history)
10548                           gnus-summary-save-parts-default-mime)
10549                       'gnus-summary-save-parts-type-history)
10550          (setq gnus-summary-save-parts-last-directory
10551                (read-file-name "Save to directory: "
10552                                gnus-summary-save-parts-last-directory
10553                                nil t))
10554          current-prefix-arg))
10555   (gnus-summary-iterate n
10556     (let ((gnus-display-mime-function nil)
10557           (gnus-inhibit-treatment t))
10558       (gnus-summary-select-article))
10559     (save-excursion
10560       (set-buffer gnus-article-buffer)
10561       (let ((handles (or gnus-article-mime-handles
10562                          (mm-dissect-buffer nil gnus-article-loose-mime)
10563                          (mm-uu-dissect))))
10564         (when handles
10565           (gnus-summary-save-parts-1 type dir handles reverse)
10566           (unless gnus-article-mime-handles ;; Don't destroy this case.
10567             (mm-destroy-parts handles)))))))
10568
10569 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10570   (if (stringp (car handle))
10571       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10572               (cdr handle))
10573     (when (if reverse
10574               (not (string-match type (mm-handle-media-type handle)))
10575             (string-match type (mm-handle-media-type handle)))
10576       (let ((file (expand-file-name
10577                    (file-name-nondirectory
10578                     (or
10579                      (mail-content-type-get
10580                       (mm-handle-disposition handle) 'filename)
10581                      (concat gnus-newsgroup-name
10582                              "." (number-to-string
10583                                   (cdr gnus-article-current)))))
10584                    dir)))
10585         (unless (file-exists-p file)
10586           (mm-save-part-to-file handle file))))))
10587
10588 ;; Summary extract commands
10589
10590 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10591   (let ((buffer-read-only nil)
10592         (article (gnus-summary-article-number))
10593         after-article b e)
10594     (unless (gnus-summary-goto-subject article)
10595       (error "No such article: %d" article))
10596     (gnus-summary-position-point)
10597     ;; If all commands are to be bunched up on one line, we collect
10598     ;; them here.
10599     (unless gnus-view-pseudos-separately
10600       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10601             files action)
10602         (while ps
10603           (setq action (cdr (assq 'action (car ps))))
10604           (setq files (list (cdr (assq 'name (car ps)))))
10605           (while (and ps (cdr ps)
10606                       (string= (or action "1")
10607                                (or (cdr (assq 'action (cadr ps))) "2")))
10608             (push (cdr (assq 'name (cadr ps))) files)
10609             (setcdr ps (cddr ps)))
10610           (when files
10611             (when (not (string-match "%s" action))
10612               (push " " files))
10613             (push " " files)
10614             (when (assq 'execute (car ps))
10615               (setcdr (assq 'execute (car ps))
10616                       (funcall (if (string-match "%s" action)
10617                                    'format 'concat)
10618                                action
10619                                (mapconcat
10620                                 (lambda (f)
10621                                   (if (equal f " ")
10622                                       f
10623                                     (mm-quote-arg f)))
10624                                 files " ")))))
10625           (setq ps (cdr ps)))))
10626     (if (and gnus-view-pseudos (not not-view))
10627         (while pslist
10628           (when (assq 'execute (car pslist))
10629             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10630                                   (eq gnus-view-pseudos 'not-confirm)))
10631           (setq pslist (cdr pslist)))
10632       (save-excursion
10633         (while pslist
10634           (setq after-article (or (cdr (assq 'article (car pslist)))
10635                                   (gnus-summary-article-number)))
10636           (gnus-summary-goto-subject after-article)
10637           (forward-line 1)
10638           (setq b (point))
10639           (insert "    " (file-name-nondirectory
10640                           (cdr (assq 'name (car pslist))))
10641                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10642           (setq e (point))
10643           (forward-line -1)             ; back to `b'
10644           (gnus-add-text-properties
10645            b (1- e) (list 'gnus-number gnus-reffed-article-number
10646                           gnus-mouse-face-prop gnus-mouse-face))
10647           (gnus-data-enter
10648            after-article gnus-reffed-article-number
10649            gnus-unread-mark b (car pslist) 0 (- e b))
10650           (setq gnus-newsgroup-unreads
10651                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10652                                          gnus-reffed-article-number))
10653           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10654           (setq pslist (cdr pslist)))))))
10655
10656 (defun gnus-pseudos< (p1 p2)
10657   (let ((c1 (cdr (assq 'action p1)))
10658         (c2 (cdr (assq 'action p2))))
10659     (and c1 c2 (string< c1 c2))))
10660
10661 (defun gnus-request-pseudo-article (props)
10662   (cond ((assq 'execute props)
10663          (gnus-execute-command (cdr (assq 'execute props)))))
10664   (let ((gnus-current-article (gnus-summary-article-number)))
10665     (gnus-run-hooks 'gnus-mark-article-hook)))
10666
10667 (defun gnus-execute-command (command &optional automatic)
10668   (save-excursion
10669     (gnus-article-setup-buffer)
10670     (set-buffer gnus-article-buffer)
10671     (setq buffer-read-only nil)
10672     (let ((command (if automatic command
10673                      (read-string "Command: " (cons command 0)))))
10674       (erase-buffer)
10675       (insert "$ " command "\n\n")
10676       (if gnus-view-pseudo-asynchronously
10677           (start-process "gnus-execute" (current-buffer) shell-file-name
10678                          shell-command-switch command)
10679         (call-process shell-file-name nil t nil
10680                       shell-command-switch command)))))
10681
10682 ;; Summary kill commands.
10683
10684 (defun gnus-summary-edit-global-kill (article)
10685   "Edit the \"global\" kill file."
10686   (interactive (list (gnus-summary-article-number)))
10687   (gnus-group-edit-global-kill article))
10688
10689 (defun gnus-summary-edit-local-kill ()
10690   "Edit a local kill file applied to the current newsgroup."
10691   (interactive)
10692   (setq gnus-current-headers (gnus-summary-article-header))
10693   (gnus-group-edit-local-kill
10694    (gnus-summary-article-number) gnus-newsgroup-name))
10695
10696 ;;; Header reading.
10697
10698 (defun gnus-read-header (id &optional header)
10699   "Read the headers of article ID and enter them into the Gnus system."
10700   (let ((group gnus-newsgroup-name)
10701         (gnus-override-method
10702          (or
10703           gnus-override-method
10704           (and (gnus-news-group-p gnus-newsgroup-name)
10705                (car (gnus-refer-article-methods)))))
10706         where)
10707     ;; First we check to see whether the header in question is already
10708     ;; fetched.
10709     (if (stringp id)
10710         ;; This is a Message-ID.
10711         (setq header (or header (gnus-id-to-header id)))
10712       ;; This is an article number.
10713       (setq header (or header (gnus-summary-article-header id))))
10714     (if (and header
10715              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10716         ;; We have found the header.
10717         header
10718       ;; If this is a sparse article, we have to nix out its
10719       ;; previous entry in the thread hashtb.
10720       (when (and header
10721                  (gnus-summary-article-sparse-p (mail-header-number header)))
10722         (let* ((parent (gnus-parent-id (mail-header-references header)))
10723                (thread (and parent (gnus-id-to-thread parent))))
10724           (when thread
10725             (delq (assq header thread) thread))))
10726       ;; We have to really fetch the header to this article.
10727       (save-excursion
10728         (set-buffer nntp-server-buffer)
10729         (when (setq where (gnus-request-head id group))
10730           (nnheader-fold-continuation-lines)
10731           (goto-char (point-max))
10732           (insert ".\n")
10733           (goto-char (point-min))
10734           (insert "211 ")
10735           (princ (cond
10736                   ((numberp id) id)
10737                   ((cdr where) (cdr where))
10738                   (header (mail-header-number header))
10739                   (t gnus-reffed-article-number))
10740                  (current-buffer))
10741           (insert " Article retrieved.\n"))
10742         (if (or (not where)
10743                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10744             ()                          ; Malformed head.
10745           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10746             (when (and (stringp id)
10747                        (not (string= (gnus-group-real-name group)
10748                                      (car where))))
10749               ;; If we fetched by Message-ID and the article came
10750               ;; from a different group, we fudge some bogus article
10751               ;; numbers for this article.
10752               (mail-header-set-number header gnus-reffed-article-number))
10753             (save-excursion
10754               (set-buffer gnus-summary-buffer)
10755               (decf gnus-reffed-article-number)
10756               (gnus-remove-header (mail-header-number header))
10757               (push header gnus-newsgroup-headers)
10758               (setq gnus-current-headers header)
10759               (push (mail-header-number header) gnus-newsgroup-limit)))
10760           header)))))
10761
10762 (defun gnus-remove-header (number)
10763   "Remove header NUMBER from `gnus-newsgroup-headers'."
10764   (if (and gnus-newsgroup-headers
10765            (= number (mail-header-number (car gnus-newsgroup-headers))))
10766       (pop gnus-newsgroup-headers)
10767     (let ((headers gnus-newsgroup-headers))
10768       (while (and (cdr headers)
10769                   (not (= number (mail-header-number (cadr headers)))))
10770         (pop headers))
10771       (when (cdr headers)
10772         (setcdr headers (cddr headers))))))
10773
10774 ;;;
10775 ;;; summary highlights
10776 ;;;
10777
10778 (defun gnus-highlight-selected-summary ()
10779   "Highlight selected article in summary buffer."
10780   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10781   (when gnus-summary-selected-face
10782     (save-excursion
10783       (let* ((beg (progn (beginning-of-line) (point)))
10784              (end (progn (end-of-line) (point)))
10785              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10786              (from (if (get-text-property beg gnus-mouse-face-prop)
10787                        beg
10788                      (or (next-single-property-change
10789                           beg gnus-mouse-face-prop nil end)
10790                          beg)))
10791              (to
10792               (if (= from end)
10793                   (- from 2)
10794                 (or (next-single-property-change
10795                      from gnus-mouse-face-prop nil end)
10796                     end))))
10797         ;; If no mouse-face prop on line we will have to = from = end,
10798         ;; so we highlight the entire line instead.
10799         (when (= (+ to 2) from)
10800           (setq from beg)
10801           (setq to end))
10802         (if gnus-newsgroup-selected-overlay
10803             ;; Move old overlay.
10804             (gnus-move-overlay
10805              gnus-newsgroup-selected-overlay from to (current-buffer))
10806           ;; Create new overlay.
10807           (gnus-overlay-put
10808            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10809            'face gnus-summary-selected-face))))))
10810
10811 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10812 (defun gnus-summary-highlight-line ()
10813   "Highlight current line according to `gnus-summary-highlight'."
10814   (let* ((list gnus-summary-highlight)
10815          (beg (gnus-point-at-bol))
10816          (article (gnus-summary-article-number))
10817          (score (or (cdr (assq (or article gnus-current-article)
10818                                gnus-newsgroup-scored))
10819                     gnus-summary-default-score 0))
10820          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10821          (inhibit-read-only t)
10822          (default gnus-summary-default-score)
10823          (default-high gnus-summary-default-high-score)
10824          (default-low gnus-summary-default-low-score))
10825     ;; Eval the cars of the lists until we find a match.
10826     (while (and list
10827                 (not (eval (caar list))))
10828       (setq list (cdr list)))
10829     (let ((face (cdar list)))
10830       (unless (eq face (get-text-property beg 'face))
10831         (gnus-put-text-property-excluding-characters-with-faces
10832          beg (gnus-point-at-eol) 'face
10833          (setq face (if (boundp face) (symbol-value face) face)))
10834         (when gnus-summary-highlight-line-function
10835           (funcall gnus-summary-highlight-line-function article face))))))
10836
10837 (defun gnus-update-read-articles (group unread &optional compute)
10838   "Update the list of read articles in GROUP.
10839 UNREAD is a sorted list."
10840   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10841          (entry (gnus-gethash group gnus-newsrc-hashtb))
10842          (info (nth 2 entry))
10843          (prev 1)
10844          read)
10845     (if (or (not info) (not active))
10846         ;; There is no info on this group if it was, in fact,
10847         ;; killed.  Gnus stores no information on killed groups, so
10848         ;; there's nothing to be done.
10849         ;; One could store the information somewhere temporarily,
10850         ;; perhaps...  Hmmm...
10851         ()
10852       ;; Remove any negative articles numbers.
10853       (while (and unread (< (car unread) 0))
10854         (setq unread (cdr unread)))
10855       ;; Remove any expired article numbers
10856       (while (and unread (< (car unread) (car active)))
10857         (setq unread (cdr unread)))
10858       ;; Compute the ranges of read articles by looking at the list of
10859       ;; unread articles.
10860       (while unread
10861         (when (/= (car unread) prev)
10862           (push (if (= prev (1- (car unread))) prev
10863                   (cons prev (1- (car unread))))
10864                 read))
10865         (setq prev (1+ (car unread)))
10866         (setq unread (cdr unread)))
10867       (when (<= prev (cdr active))
10868         (push (cons prev (cdr active)) read))
10869       (setq read (if (> (length read) 1) (nreverse read) read))
10870       (if compute
10871           read
10872         (save-excursion
10873           (let (setmarkundo)
10874             ;; Propagate the read marks to the backend.
10875             (when (gnus-check-backend-function 'request-set-mark group)
10876               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10877                     (add (gnus-remove-from-range read (gnus-info-read info))))
10878                 (when (or add del)
10879                   (unless (gnus-check-group group)
10880                     (error "Can't open server for %s" group))
10881                   (gnus-request-set-mark
10882                    group (delq nil (list (if add (list add 'add '(read)))
10883                                          (if del (list del 'del '(read))))))
10884                   (setq setmarkundo
10885                         `(gnus-request-set-mark
10886                           ,group
10887                           ',(delq nil (list
10888                                        (if del (list del 'add '(read)))
10889                                        (if add (list add 'del '(read))))))))))
10890             (set-buffer gnus-group-buffer)
10891             (gnus-undo-register
10892               `(progn
10893                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10894                  (gnus-info-set-read ',info ',(gnus-info-read info))
10895                  (gnus-get-unread-articles-in-group ',info
10896                                                     (gnus-active ,group))
10897                  (gnus-group-update-group ,group t)
10898                  ,setmarkundo))))
10899         ;; Enter this list into the group info.
10900         (gnus-info-set-read info read)
10901         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10902         (gnus-get-unread-articles-in-group info (gnus-active group))
10903         t))))
10904
10905 (defun gnus-offer-save-summaries ()
10906   "Offer to save all active summary buffers."
10907   (let (buffers)
10908     ;; Go through all buffers and find all summaries.
10909     (dolist (buffer (buffer-list))
10910       (when (and (setq buffer (buffer-name buffer))
10911                  (string-match "Summary" buffer)
10912                  (save-excursion
10913                    (set-buffer buffer)
10914                    ;; We check that this is, indeed, a summary buffer.
10915                    (and (eq major-mode 'gnus-summary-mode)
10916                         ;; Also make sure this isn't bogus.
10917                         gnus-newsgroup-prepared
10918                         ;; Also make sure that this isn't a
10919                         ;; dead summary buffer.
10920                         (not gnus-dead-summary-mode))))
10921         (push buffer buffers)))
10922     ;; Go through all these summary buffers and offer to save them.
10923     (when buffers
10924       (save-excursion
10925         (map-y-or-n-p
10926          "Update summary buffer %s? "
10927          (lambda (buf)
10928            (switch-to-buffer buf)
10929            (gnus-summary-exit))
10930          buffers)))))
10931
10932 (defun gnus-summary-setup-default-charset ()
10933   "Setup newsgroup default charset."
10934   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10935       (setq gnus-newsgroup-charset nil)
10936     (let* ((ignored-charsets
10937             (or gnus-newsgroup-ephemeral-ignored-charsets
10938                 (append
10939                  (and gnus-newsgroup-name
10940                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10941                  gnus-newsgroup-ignored-charsets))))
10942       (setq gnus-newsgroup-charset
10943             (or gnus-newsgroup-ephemeral-charset
10944                 (and gnus-newsgroup-name
10945                      (gnus-parameter-charset gnus-newsgroup-name))
10946                 gnus-default-charset))
10947       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10948            ignored-charsets))))
10949
10950 ;;;
10951 ;;; Mime Commands
10952 ;;;
10953
10954 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10955   "Display the current article buffer fully MIME-buttonized.
10956 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10957 treated as multipart/mixed."
10958   (interactive "P")
10959   (require 'gnus-art)
10960   (let ((gnus-unbuttonized-mime-types nil)
10961         (gnus-mime-display-multipart-as-mixed show-all-parts))
10962     (gnus-summary-show-article)))
10963
10964 (defun gnus-summary-repair-multipart (article)
10965   "Add a Content-Type header to a multipart article without one."
10966   (interactive (list (gnus-summary-article-number)))
10967   (gnus-with-article article
10968     (message-narrow-to-head)
10969     (message-remove-header "Mime-Version")
10970     (goto-char (point-max))
10971     (insert "Mime-Version: 1.0\n")
10972     (widen)
10973     (when (search-forward "\n--" nil t)
10974       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10975         (message-narrow-to-head)
10976         (message-remove-header "Content-Type")
10977         (goto-char (point-max))
10978         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10979                         separator))
10980         (widen))))
10981   (let (gnus-mark-article-hook)
10982     (gnus-summary-select-article t t nil article)))
10983
10984 (defun gnus-summary-toggle-display-buttonized ()
10985   "Toggle the buttonizing of the article buffer."
10986   (interactive)
10987   (require 'gnus-art)
10988   (if (setq gnus-inhibit-mime-unbuttonizing
10989             (not gnus-inhibit-mime-unbuttonizing))
10990       (let ((gnus-unbuttonized-mime-types nil))
10991         (gnus-summary-show-article))
10992     (gnus-summary-show-article)))
10993
10994 ;;;
10995 ;;; Generic summary marking commands
10996 ;;;
10997
10998 (defvar gnus-summary-marking-alist
10999   '((read gnus-del-mark "d")
11000     (unread gnus-unread-mark "u")
11001     (ticked gnus-ticked-mark "!")
11002     (dormant gnus-dormant-mark "?")
11003     (expirable gnus-expirable-mark "e"))
11004   "An alist of names/marks/keystrokes.")
11005
11006 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11007 (defvar gnus-summary-mark-map)
11008
11009 (defun gnus-summary-make-all-marking-commands ()
11010   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11011   (dolist (elem gnus-summary-marking-alist)
11012     (apply 'gnus-summary-make-marking-command elem)))
11013
11014 (defun gnus-summary-make-marking-command (name mark keystroke)
11015   (let ((map (make-sparse-keymap)))
11016     (define-key gnus-summary-generic-mark-map keystroke map)
11017     (dolist (lway `((next "next" next nil "n")
11018                     (next-unread "next unread" next t "N")
11019                     (prev "previous" prev nil "p")
11020                     (prev-unread "previous unread" prev t "P")
11021                     (nomove "" nil nil ,keystroke)))
11022       (let ((func (gnus-summary-make-marking-command-1
11023                    mark (car lway) lway name)))
11024         (setq func (eval func))
11025         (define-key map (nth 4 lway) func)))))
11026
11027 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11028   `(defun ,(intern
11029             (format "gnus-summary-put-mark-as-%s%s"
11030                     name (if (eq way 'nomove)
11031                              ""
11032                            (concat "-" (symbol-name way)))))
11033      (n)
11034      ,(format
11035        "Mark the current article as %s%s.
11036 If N, the prefix, then repeat N times.
11037 If N is negative, move in reverse order.
11038 The difference between N and the actual number of articles marked is
11039 returned."
11040        name (cadr lway))
11041      (interactive "p")
11042      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11043
11044 (defun gnus-summary-generic-mark (n mark move unread)
11045   "Mark N articles with MARK."
11046   (unless (eq major-mode 'gnus-summary-mode)
11047     (error "This command can only be used in the summary buffer"))
11048   (gnus-summary-show-thread)
11049   (let ((nummove
11050          (cond
11051           ((eq move 'next) 1)
11052           ((eq move 'prev) -1)
11053           (t 0))))
11054     (if (zerop nummove)
11055         (setq n 1)
11056       (when (< n 0)
11057         (setq n (abs n)
11058               nummove (* -1 nummove))))
11059     (while (and (> n 0)
11060                 (gnus-summary-mark-article nil mark)
11061                 (zerop (gnus-summary-next-subject nummove unread t)))
11062       (setq n (1- n)))
11063     (when (/= 0 n)
11064       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11065     (gnus-summary-recenter)
11066     (gnus-summary-position-point)
11067     (gnus-set-mode-line 'summary)
11068     n))
11069
11070 (defun gnus-summary-insert-articles (articles)
11071   (when (setq articles
11072               (gnus-sorted-difference articles
11073                                       (mapcar (lambda (h)
11074                                                 (mail-header-number h))
11075                                               gnus-newsgroup-headers)))
11076     (setq gnus-newsgroup-headers
11077           (merge 'list
11078                  gnus-newsgroup-headers
11079                  (gnus-fetch-headers articles)
11080                  'gnus-article-sort-by-number))
11081     ;; Suppress duplicates?
11082     (when gnus-suppress-duplicates
11083       (gnus-dup-suppress-articles))
11084
11085     ;; We might want to build some more threads first.
11086     (when (and gnus-fetch-old-headers
11087                (eq gnus-headers-retrieved-by 'nov))
11088       (if (eq gnus-fetch-old-headers 'invisible)
11089           (gnus-build-all-threads)
11090         (gnus-build-old-threads)))
11091     ;; Let the Gnus agent mark articles as read.
11092     (when gnus-agent
11093       (gnus-agent-get-undownloaded-list))
11094     ;; Remove list identifiers from subject
11095     (when gnus-list-identifiers
11096       (gnus-summary-remove-list-identifiers))
11097     ;; First and last article in this newsgroup.
11098     (when gnus-newsgroup-headers
11099       (setq gnus-newsgroup-begin
11100             (mail-header-number (car gnus-newsgroup-headers))
11101             gnus-newsgroup-end
11102             (mail-header-number
11103              (gnus-last-element gnus-newsgroup-headers))))
11104     (when gnus-use-scoring
11105       (gnus-possibly-score-headers))))
11106
11107 (defun gnus-summary-insert-old-articles (&optional all)
11108   "Insert all old articles in this group.
11109 If ALL is non-nil, already read articles become readable.
11110 If ALL is a number, fetch this number of articles."
11111   (interactive "P")
11112   (prog1
11113       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11114             older len)
11115         (setq older
11116               (gnus-sorted-difference
11117                (gnus-uncompress-range (list gnus-newsgroup-active))
11118                old))
11119         (setq len (length older))
11120         (cond
11121          ((null older) nil)
11122          ((numberp all)
11123           (if (< all len)
11124               (setq older (last older all))))
11125          (all nil)
11126          (t
11127           (if (and (numberp gnus-large-newsgroup)
11128                    (> len gnus-large-newsgroup))
11129               (let* ((cursor-in-echo-area nil)
11130                      (initial (gnus-parameter-large-newsgroup-initial
11131                                gnus-newsgroup-name))
11132                      (input
11133                       (read-string
11134                        (format
11135                         "How many articles from %s (%s %d): "
11136                         (gnus-limit-string
11137                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11138                         (if initial "max" "default")
11139                         len)
11140                        (if initial
11141                            (cons (number-to-string initial)
11142                                  0)))))
11143                 (unless (string-match "^[ \t]*$" input)
11144                   (setq all (string-to-number input))
11145                   (if (< all len)
11146                       (setq older (last older all))))))))
11147         (if (not older)
11148             (message "No old news.")
11149           (gnus-summary-insert-articles older)
11150           (gnus-summary-limit (gnus-sorted-nunion old older))))
11151     (gnus-summary-position-point)))
11152
11153 (defun gnus-summary-insert-new-articles ()
11154   "Insert all new articles in this group."
11155   (interactive)
11156   (prog1
11157       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11158             (old-active gnus-newsgroup-active)
11159             (nnmail-fetched-sources (list t))
11160             i new)
11161         (setq gnus-newsgroup-active
11162               (gnus-activate-group gnus-newsgroup-name 'scan))
11163         (setq i (cdr gnus-newsgroup-active))
11164         (while (> i (cdr old-active))
11165           (push i new)
11166           (decf i))
11167         (if (not new)
11168             (message "No gnus is bad news.")
11169           (gnus-summary-insert-articles new)
11170           (setq gnus-newsgroup-unreads
11171                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11172           (gnus-summary-limit (gnus-sorted-nunion old new))))
11173     (gnus-summary-position-point)))
11174
11175 (gnus-summary-make-all-marking-commands)
11176
11177 (gnus-ems-redefine)
11178
11179 (provide 'gnus-sum)
11180
11181 (run-hooks 'gnus-sum-load-hook)
11182
11183 ;;; gnus-sum.el ends here