lisp/ChangeLog addition:
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-mode))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'gmm-utils)
42 (require 'mm-decode)
43 (require 'nnoo)
44
45 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
46 (autoload 'gnus-cache-write-active "gnus-cache")
47 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
48 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
49 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
52   "Deuglify broken Outlook (Express) articles and redisplay."
53   t)
54 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
55 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
56 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
57
58 (defcustom gnus-kill-summary-on-exit t
59   "*If non-nil, kill the summary buffer when you exit from it.
60 If nil, the summary will become a \"*Dead Summary*\" buffer, and
61 it will be killed sometime later."
62   :group 'gnus-summary-exit
63   :type 'boolean)
64
65 (defcustom gnus-fetch-old-headers nil
66   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
67 If an unread article in the group refers to an older, already
68 read (or just marked as read) article, the old article will not
69 normally be displayed in the Summary buffer.  If this variable is
70 t, Gnus will attempt to grab the headers to the old articles, and
71 thereby build complete threads.  If it has the value `some', all
72 old headers will be fetched but only enough headers to connect
73 otherwise loose threads will be displayed.  This variable can
74 also be a number.  In that case, no more than that number of old
75 headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work.
79
80 This feature can seriously impact performance it ignores all
81 locally cached header entries."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const :tag "on" t)
85                  (const some)
86                  (const invisible)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 500
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :version "22.1"
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :version "22.1"
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-zcore-fuzz 0
242   "*Fuzziness factor for the zcore in the summary buffer.
243 Articles with scores closer than this to `gnus-summary-default-score'
244 will not be marked."
245   :group 'gnus-summary-format
246   :type 'integer)
247
248 (defcustom gnus-simplify-subject-fuzzy-regexp nil
249   "*Strings to be removed when doing fuzzy matches.
250 This can either be a regular expression or list of regular expressions
251 that will be removed from subject strings if fuzzy subject
252 simplification is selected."
253   :group 'gnus-thread
254   :type '(repeat regexp))
255
256 (defcustom gnus-show-threads t
257   "*If non-nil, display threads in summary mode."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-hide-subtree nil
262   "*If non-nil, hide all threads initially.
263 This can be a predicate specifier which says which threads to hide.
264 If threads are hidden, you have to run the command
265 `gnus-summary-show-thread' by hand or select an article."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier")))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "If non-nil, select an article on group entry.
312 An article is selected automatically when entering a group
313 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
314 `gnus-summary-catchup-and-goto-next-group'.
315
316 Which article is selected is controlled by the variable
317 `gnus-auto-select-subject'.
318
319 If you want to prevent automatic selection of articles in some
320 newsgroups, set the variable to nil in `gnus-select-group-hook'."
321   ;; Commands include...
322   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
323   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
324   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
325   :group 'gnus-group-select
326   :type '(choice (const :tag "none" nil)
327                  (sexp :menu-tag "first" t)))
328
329 (defcustom gnus-auto-select-subject 'unread
330   "*Says what subject to place under point when entering a group.
331
332 This variable can either be the symbols `first' (place point on the
333 first subject), `unread' (place point on the subject line of the first
334 unread article), `best' (place point on the subject line of the
335 higest-scored article), `unseen' (place point on the subject line of
336 the first unseen article), `unseen-or-unread' (place point on the subject
337 line of the first unseen article or, if all article have been seen, on the
338 subject line of the first unread article), or a function to be called to
339 place point on some subject line."
340   :version "22.1"
341   :group 'gnus-group-select
342   :type '(choice (const best)
343                  (const unread)
344                  (const first)
345                  (const unseen)
346                  (const unseen-or-unread)))
347
348 (defcustom gnus-auto-select-next t
349   "*If non-nil, offer to go to the next group from the end of the previous.
350 If the value is t and the next newsgroup is empty, Gnus will exit
351 summary mode and go back to group mode.  If the value is neither nil
352 nor t, Gnus will select the following unread newsgroup.  In
353 particular, if the value is the symbol `quietly', the next unread
354 newsgroup will be selected without any confirmation, and if it is
355 `almost-quietly', the next group will be selected without any
356 confirmation if you are located on the last article in the group.
357 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
358 will go to the next group without confirmation."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "off" nil)
361                  (const quietly)
362                  (const almost-quietly)
363                  (const slightly-quietly)
364                  (sexp :menu-tag "on" t)))
365
366 (defcustom gnus-auto-select-same nil
367   "*If non-nil, select the next article with the same subject.
368 If there are no more articles with the same subject, go to
369 the first unread article."
370   :group 'gnus-summary-maneuvering
371   :type 'boolean)
372
373 (defcustom gnus-auto-goto-ignores 'unfetched
374   "*Says how to handle unfetched articles when maneuvering.
375
376 This variable can either be the symbols nil (maneuver to any
377 article), `undownloaded' (maneuvering while unplugged ignores articles
378 that have not been fetched), `always-undownloaded' (maneuvering always
379 ignores articles that have not been fetched), `unfetched' (maneuvering
380 ignores articles whose headers have not been fetched).
381
382 NOTE: The list of unfetched articles will always be nil when plugged
383 and, when unplugged, a subset of the undownloaded article list."
384   :version "22.1"
385   :group 'gnus-summary-maneuvering
386   :type '(choice (const :tag "None" nil)
387                  (const :tag "Undownloaded when unplugged" undownloaded)
388                  (const :tag "Undownloaded" always-undownloaded)
389                  (const :tag "Unfetched" unfetched)))
390
391 (defcustom gnus-summary-check-current nil
392   "*If non-nil, consider the current article when moving.
393 The \"unread\" movement commands will stay on the same line if the
394 current article is unread."
395   :group 'gnus-summary-maneuvering
396   :type 'boolean)
397
398 (defcustom gnus-auto-center-summary 2
399   "*If non-nil, always center the current summary buffer.
400 In particular, if `vertical' do only vertical recentering.  If non-nil
401 and non-`vertical', do both horizontal and vertical recentering."
402   :group 'gnus-summary-maneuvering
403   :type '(choice (const :tag "none" nil)
404                  (const vertical)
405                  (integer :tag "height")
406                  (sexp :menu-tag "both" t)))
407
408 (defvar gnus-auto-center-group t
409   "*If non-nil, always center the group buffer.")
410
411 (defcustom gnus-show-all-headers nil
412   "*If non-nil, don't hide any headers."
413   :group 'gnus-article-hiding
414   :group 'gnus-article-headers
415   :type 'boolean)
416
417 (defcustom gnus-summary-ignore-duplicates nil
418   "*If non-nil, ignore articles with identical Message-ID headers."
419   :group 'gnus-summary
420   :type 'boolean)
421
422 (defcustom gnus-single-article-buffer t
423   "*If non-nil, display all articles in the same buffer.
424 If nil, each group will get its own article buffer."
425   :group 'gnus-article-various
426   :type 'boolean)
427
428 (defcustom gnus-break-pages t
429   "*If non-nil, do page breaking on articles.
430 The page delimiter is specified by the `gnus-page-delimiter'
431 variable."
432   :group 'gnus-article-various
433   :type 'boolean)
434
435 (defcustom gnus-move-split-methods nil
436   "*Variable used to suggest where articles are to be moved to.
437 It uses the same syntax as the `gnus-split-methods' variable.
438 However, whereas `gnus-split-methods' specifies file names as targets,
439 this variable specifies group names."
440   :group 'gnus-summary-mail
441   :type '(repeat (choice (list :value (fun) function)
442                          (cons :value ("" "") regexp (repeat string))
443                          (sexp :value nil))))
444
445 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
446   "Function used to compute default prefix for article move/copy/etc prompts.
447 The function should take one argument, a group name, and return a
448 string with the suggested prefix."
449   :group 'gnus-summary-mail
450   :type 'function)
451
452 ;; FIXME: Although the custom type is `character' for the following variables,
453 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
454
455 (defcustom gnus-unread-mark ?           ;Whitespace
456   "*Mark used for unread articles."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-ticked-mark ?!
461   "*Mark used for ticked articles."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-dormant-mark ??
466   "*Mark used for dormant articles."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-del-mark ?r
471   "*Mark used for del'd articles."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-read-mark ?R
476   "*Mark used for read articles."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-expirable-mark ?E
481   "*Mark used for expirable articles."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-killed-mark ?K
486   "*Mark used for killed articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-spam-mark ?$
491   "*Mark used for spam articles."
492   :version "22.1"
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-souped-mark ?F
497   "*Mark used for souped articles."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-kill-file-mark ?X
502   "*Mark used for articles killed by kill files."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-low-score-mark ?Y
507   "*Mark used for articles with a low score."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-catchup-mark ?C
512   "*Mark used for articles that are caught up."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-replied-mark ?A
517   "*Mark used for articles that have been replied to."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-forwarded-mark ?F
522   "*Mark used for articles that have been forwarded."
523   :version "22.1"
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-recent-mark ?N
528   "*Mark used for articles that are recent."
529   :version "22.1"
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-cached-mark ?*
534   "*Mark used for articles that are in the cache."
535   :group 'gnus-summary-marks
536   :type 'character)
537
538 (defcustom gnus-saved-mark ?S
539   "*Mark used for articles that have been saved."
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-unseen-mark ?.
544   "*Mark used for articles that haven't been seen."
545   :version "22.1"
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-no-mark ?               ;Whitespace
550   "*Mark used for articles that have no other secondary mark."
551   :version "22.1"
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-ancient-mark ?O
556   "*Mark used for ancient articles."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-sparse-mark ?Q
561   "*Mark used for sparsely reffed articles."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-canceled-mark ?G
566   "*Mark used for canceled articles."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-duplicate-mark ?M
571   "*Mark used for duplicate articles."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-undownloaded-mark ?-
576   "*Mark used for articles that weren't downloaded."
577   :version "22.1"
578   :group 'gnus-summary-marks
579   :type 'character)
580
581 (defcustom gnus-downloaded-mark ?+
582   "*Mark used for articles that were downloaded."
583   :group 'gnus-summary-marks
584   :type 'character)
585
586 (defcustom gnus-downloadable-mark ?%
587   "*Mark used for articles that are to be downloaded."
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-unsendable-mark ?=
592   "*Mark used for articles that won't be sent."
593   :group 'gnus-summary-marks
594   :type 'character)
595
596 (defcustom gnus-score-over-mark ?+
597   "*Score mark used for articles with high scores."
598   :group 'gnus-summary-marks
599   :type 'character)
600
601 (defcustom gnus-score-below-mark ?-
602   "*Score mark used for articles with low scores."
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-empty-thread-mark ?     ;Whitespace
607   "*There is no thread under the article."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-not-empty-thread-mark ?=
612   "*There is a thread under the article."
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-view-pseudo-asynchronously nil
617   "*If non-nil, Gnus will view pseudo-articles asynchronously."
618   :group 'gnus-extract-view
619   :type 'boolean)
620
621 (defcustom gnus-auto-expirable-marks
622   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
623         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
624         gnus-souped-mark gnus-duplicate-mark)
625   "*The list of marks converted into expiration if a group is auto-expirable."
626   :version "21.1"
627   :group 'gnus-summary
628   :type '(repeat character))
629
630 (defcustom gnus-inhibit-user-auto-expire t
631   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
632   :version "21.1"
633   :group 'gnus-summary
634   :type 'boolean)
635
636 (defcustom gnus-view-pseudos nil
637   "*If `automatic', pseudo-articles will be viewed automatically.
638 If `not-confirm', pseudos will be viewed automatically, and the user
639 will not be asked to confirm the command."
640   :group 'gnus-extract-view
641   :type '(choice (const :tag "off" nil)
642                  (const automatic)
643                  (const not-confirm)))
644
645 (defcustom gnus-view-pseudos-separately t
646   "*If non-nil, one pseudo-article will be created for each file to be viewed.
647 If nil, all files that use the same viewing command will be given as a
648 list of parameters to that command."
649   :group 'gnus-extract-view
650   :type 'boolean)
651
652 (defcustom gnus-insert-pseudo-articles t
653   "*If non-nil, insert pseudo-articles when decoding articles."
654   :group 'gnus-extract-view
655   :type 'boolean)
656
657 (defcustom gnus-summary-dummy-line-format
658   "   %(:                             :%) %S\n"
659   "*The format specification for the dummy roots in the summary buffer.
660 It works along the same lines as a normal formatting string,
661 with some simple extensions.
662
663 %S  The subject
664
665 General format specifiers can also be used.
666 See `(gnus)Formatting Variables'."
667   :link '(custom-manual "(gnus)Formatting Variables")
668   :group 'gnus-threading
669   :type 'string)
670
671 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
672   "*The format specification for the summary mode line.
673 It works along the same lines as a normal formatting string,
674 with some simple extensions:
675
676 %G  Group name
677 %p  Unprefixed group name
678 %A  Current article number
679 %z  Current article score
680 %V  Gnus version
681 %U  Number of unread articles in the group
682 %e  Number of unselected articles in the group
683 %Z  A string with unread/unselected article counts
684 %g  Shortish group name
685 %S  Subject of the current article
686 %u  User-defined spec
687 %s  Current score file name
688 %d  Number of dormant articles
689 %r  Number of articles that have been marked as read in this session
690 %E  Number of articles expunged by the score files"
691   :group 'gnus-summary-format
692   :type 'string)
693
694 (defcustom gnus-list-identifiers nil
695   "Regexp that matches list identifiers to be removed from subject.
696 This can also be a list of regexps."
697   :version "21.1"
698   :group 'gnus-summary-format
699   :group 'gnus-article-hiding
700   :type '(choice (const :tag "none" nil)
701                  (regexp :value ".*")
702                  (repeat :value (".*") regexp)))
703
704 (defcustom gnus-summary-mark-below 0
705   "*Mark all articles with a score below this variable as read.
706 This variable is local to each summary buffer and usually set by the
707 score file."
708   :group 'gnus-score-default
709   :type 'integer)
710
711 (defun gnus-widget-reversible-match (widget value)
712   "Ignoring WIDGET, convert VALUE to internal form.
713 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
714   ;; (debug value)
715   (or (symbolp value)
716       (and (listp value)
717            (eq (length value) 2)
718            (eq (nth 0 value) 'not)
719            (symbolp (nth 1 value)))))
720
721 (defun gnus-widget-reversible-to-internal (widget value)
722   "Ignoring WIDGET, convert VALUE to internal form.
723 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
724 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
725   ;; (debug value)
726   (if (atom value)
727       (list value nil)
728     (list (nth 1 value) t)))
729
730 (defun gnus-widget-reversible-to-external (widget value)
731   "Ignoring WIDGET, convert VALUE to external form.
732 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
733 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
734   ;; (debug value)
735   (if (nth 1 value)
736       (list 'not (nth 0 value))
737     (nth 0 value)))
738
739 (define-widget 'gnus-widget-reversible 'group
740   "A `group' that convert values."
741   :match 'gnus-widget-reversible-match
742   :value-to-internal 'gnus-widget-reversible-to-internal
743   :value-to-external 'gnus-widget-reversible-to-external)
744
745 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
746   "*List of functions used for sorting articles in the summary buffer.
747
748 Each function takes two articles and returns non-nil if the first
749 article should be sorted before the other.  If you use more than one
750 function, the primary sort function should be the last.  You should
751 probably always include `gnus-article-sort-by-number' in the list of
752 sorting functions -- preferably first.  Also note that sorting by date
753 is often much slower than sorting by number, and the sorting order is
754 very similar.  (Sorting by date means sorting by the time the message
755 was sent, sorting by number means sorting by arrival time.)
756
757 Each item can also be a list `(not F)' where F is a function;
758 this reverses the sort order.
759
760 Ready-made functions include `gnus-article-sort-by-number',
761 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
762 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
763 and `gnus-article-sort-by-score'.
764
765 When threading is turned on, the variable `gnus-thread-sort-functions'
766 controls how articles are sorted."
767   :group 'gnus-summary-sort
768   :type '(repeat (gnus-widget-reversible
769                   (choice (function-item gnus-article-sort-by-number)
770                           (function-item gnus-article-sort-by-author)
771                           (function-item gnus-article-sort-by-subject)
772                           (function-item gnus-article-sort-by-date)
773                           (function-item gnus-article-sort-by-score)
774                           (function-item gnus-article-sort-by-random)
775                           (function :tag "other"))
776                   (boolean :tag "Reverse order"))))
777
778
779 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
780   "*List of functions used for sorting threads in the summary buffer.
781 By default, threads are sorted by article number.
782
783 Each function takes two threads and returns non-nil if the first
784 thread should be sorted before the other.  If you use more than one
785 function, the primary sort function should be the last.  You should
786 probably always include `gnus-thread-sort-by-number' in the list of
787 sorting functions -- preferably first.  Also note that sorting by date
788 is often much slower than sorting by number, and the sorting order is
789 very similar.  (Sorting by date means sorting by the time the message
790 was sent, sorting by number means sorting by arrival time.)
791
792 Each list item can also be a list `(not F)' where F is a
793 function; this specifies reversed sort order.
794
795 Ready-made functions include `gnus-thread-sort-by-number',
796 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
797 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
798 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
799 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
800 and `gnus-thread-sort-by-total-score' (see
801 `gnus-thread-score-function').
802
803 When threading is turned off, the variable
804 `gnus-article-sort-functions' controls how articles are sorted."
805   :group 'gnus-summary-sort
806   :type '(repeat
807           (gnus-widget-reversible
808            (choice (function-item gnus-thread-sort-by-number)
809                    (function-item gnus-thread-sort-by-author)
810                    (function-item gnus-thread-sort-by-recipient)
811                    (function-item gnus-thread-sort-by-subject)
812                    (function-item gnus-thread-sort-by-date)
813                    (function-item gnus-thread-sort-by-score)
814                    (function-item gnus-thread-sort-by-most-recent-number)
815                    (function-item gnus-thread-sort-by-most-recent-date)
816                    (function-item gnus-thread-sort-by-random)
817                    (function-item gnus-thread-sort-by-total-score)
818                    (function :tag "other"))
819            (boolean :tag "Reverse order"))))
820
821 (defcustom gnus-thread-score-function '+
822   "*Function used for calculating the total score of a thread.
823
824 The function is called with the scores of the article and each
825 subthread and should then return the score of the thread.
826
827 Some functions you can use are `+', `max', or `min'."
828   :group 'gnus-summary-sort
829   :type 'function)
830
831 (defcustom gnus-summary-expunge-below nil
832   "All articles that have a score less than this variable will be expunged.
833 This variable is local to the summary buffers."
834   :group 'gnus-score-default
835   :type '(choice (const :tag "off" nil)
836                  integer))
837
838 (defcustom gnus-thread-expunge-below nil
839   "All threads that have a total score less than this variable will be expunged.
840 See `gnus-thread-score-function' for en explanation of what a
841 \"thread score\" is.
842
843 This variable is local to the summary buffers."
844   :group 'gnus-threading
845   :group 'gnus-score-default
846   :type '(choice (const :tag "off" nil)
847                  integer))
848
849 (defcustom gnus-summary-mode-hook nil
850   "*A hook for Gnus summary mode.
851 This hook is run before any variables are set in the summary buffer."
852   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
853   :group 'gnus-summary-various
854   :type 'hook)
855
856 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
857 (when (featurep 'xemacs)
858   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
859   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
860   (add-hook 'gnus-summary-mode-hook
861             'gnus-xmas-switch-horizontal-scrollbar-off))
862
863 (defcustom gnus-summary-menu-hook nil
864   "*Hook run after the creation of the summary mode menu."
865   :group 'gnus-summary-visual
866   :type 'hook)
867
868 (defcustom gnus-summary-exit-hook nil
869   "*A hook called on exit from the summary buffer.
870 It will be called with point in the group buffer."
871   :group 'gnus-summary-exit
872   :type 'hook)
873
874 (defcustom gnus-summary-prepare-hook nil
875   "*A hook called after the summary buffer has been generated.
876 If you want to modify the summary buffer, you can use this hook."
877   :group 'gnus-summary-various
878   :type 'hook)
879
880 (defcustom gnus-summary-prepared-hook nil
881   "*A hook called as the last thing after the summary buffer has been generated."
882   :group 'gnus-summary-various
883   :type 'hook)
884
885 (defcustom gnus-summary-generate-hook nil
886   "*A hook run just before generating the summary buffer.
887 This hook is commonly used to customize threading variables and the
888 like."
889   :group 'gnus-summary-various
890   :type 'hook)
891
892 (defcustom gnus-select-group-hook nil
893   "*A hook called when a newsgroup is selected.
894
895 If you'd like to simplify subjects like the
896 `gnus-summary-next-same-subject' command does, you can use the
897 following hook:
898
899  (add-hook gnus-select-group-hook
900            (lambda ()
901              (mapcar (lambda (header)
902                        (mail-header-set-subject
903                         header
904                         (gnus-simplify-subject
905                          (mail-header-subject header) 're-only)))
906                      gnus-newsgroup-headers)))"
907   :group 'gnus-group-select
908   :type 'hook)
909
910 (defcustom gnus-select-article-hook nil
911   "*A hook called when an article is selected."
912   :group 'gnus-summary-choose
913   :options '(gnus-agent-fetch-selected-article)
914   :type 'hook)
915
916 (defcustom gnus-visual-mark-article-hook
917   (list 'gnus-highlight-selected-summary)
918   "*Hook run after selecting an article in the summary buffer.
919 It is meant to be used for highlighting the article in some way.  It
920 is not run if `gnus-visual' is nil."
921   :group 'gnus-summary-visual
922   :type 'hook)
923
924 (defcustom gnus-parse-headers-hook nil
925   "*A hook called before parsing the headers."
926   :group 'gnus-various
927   :type 'hook)
928
929 (defcustom gnus-exit-group-hook nil
930   "*A hook called when exiting summary mode.
931 This hook is not called from the non-updating exit commands like `Q'."
932   :group 'gnus-various
933   :type 'hook)
934
935 (defcustom gnus-summary-update-hook
936   (list 'gnus-summary-highlight-line)
937   "*A hook called when a summary line is changed.
938 The hook will not be called if `gnus-visual' is nil.
939
940 The default function `gnus-summary-highlight-line' will
941 highlight the line according to the `gnus-summary-highlight'
942 variable."
943   :group 'gnus-summary-visual
944   :type 'hook)
945
946 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
947   "*A hook called when an article is selected for the first time.
948 The hook is intended to mark an article as read (or unread)
949 automatically when it is selected."
950   :group 'gnus-summary-choose
951   :type 'hook)
952
953 (defcustom gnus-group-no-more-groups-hook nil
954   "*A hook run when returning to group mode having no more (unread) groups."
955   :group 'gnus-group-select
956   :type 'hook)
957
958 (defcustom gnus-ps-print-hook nil
959   "*A hook run before ps-printing something from Gnus."
960   :group 'gnus-summary
961   :type 'hook)
962
963 (defcustom gnus-summary-article-move-hook nil
964   "*A hook called after an article is moved, copied, respooled, or crossposted."
965   :version "22.1"
966   :group 'gnus-summary
967   :type 'hook)
968
969 (defcustom gnus-summary-article-delete-hook nil
970   "*A hook called after an article is deleted."
971   :version "22.1"
972   :group 'gnus-summary
973   :type 'hook)
974
975 (defcustom gnus-summary-article-expire-hook nil
976   "*A hook called after an article is expired."
977   :version "22.1"
978   :group 'gnus-summary
979   :type 'hook)
980
981 (defcustom gnus-summary-display-arrow
982   (and (fboundp 'display-graphic-p)
983        (display-graphic-p))
984   "*If non-nil, display an arrow highlighting the current article."
985   :version "22.1"
986   :group 'gnus-summary
987   :type 'boolean)
988
989 (defcustom gnus-summary-selected-face 'gnus-summary-selected
990   "Face used for highlighting the current article in the summary buffer."
991   :group 'gnus-summary-visual
992   :type 'face)
993
994 (defvar gnus-tmp-downloaded nil)
995
996 (defcustom gnus-summary-highlight
997   '(((eq mark gnus-canceled-mark)
998      . gnus-summary-cancelled)
999     ((and uncached (> score default-high))
1000      . gnus-summary-high-undownloaded)
1001     ((and uncached (< score default-low))
1002      . gnus-summary-low-undownloaded)
1003     (uncached
1004      . gnus-summary-normal-undownloaded)
1005     ((and (> score default-high)
1006           (or (eq mark gnus-dormant-mark)
1007               (eq mark gnus-ticked-mark)))
1008      . gnus-summary-high-ticked)
1009     ((and (< score default-low)
1010           (or (eq mark gnus-dormant-mark)
1011               (eq mark gnus-ticked-mark)))
1012      . gnus-summary-low-ticked)
1013     ((or (eq mark gnus-dormant-mark)
1014          (eq mark gnus-ticked-mark))
1015      . gnus-summary-normal-ticked)
1016     ((and (> score default-high) (eq mark gnus-ancient-mark))
1017      . gnus-summary-high-ancient)
1018     ((and (< score default-low) (eq mark gnus-ancient-mark))
1019      . gnus-summary-low-ancient)
1020     ((eq mark gnus-ancient-mark)
1021      . gnus-summary-normal-ancient)
1022     ((and (> score default-high) (eq mark gnus-unread-mark))
1023      . gnus-summary-high-unread)
1024     ((and (< score default-low) (eq mark gnus-unread-mark))
1025      . gnus-summary-low-unread)
1026     ((eq mark gnus-unread-mark)
1027      . gnus-summary-normal-unread)
1028     ((> score default-high)
1029      . gnus-summary-high-read)
1030     ((< score default-low)
1031      . gnus-summary-low-read)
1032     (t
1033      . gnus-summary-normal-read))
1034   "*Controls the highlighting of summary buffer lines.
1035
1036 A list of (FORM . FACE) pairs.  When deciding how a a particular
1037 summary line should be displayed, each form is evaluated.  The content
1038 of the face field after the first true form is used.  You can change
1039 how those summary lines are displayed, by editing the face field.
1040
1041 You can use the following variables in the FORM field.
1042
1043 score:        The article's score
1044 default:      The default article score.
1045 default-high: The default score for high scored articles.
1046 default-low:  The default score for low scored articles.
1047 below:        The score below which articles are automatically marked as read.
1048 mark:         The article's mark.
1049 uncached:     Non-nil if the article is uncached."
1050   :group 'gnus-summary-visual
1051   :type '(repeat (cons (sexp :tag "Form" nil)
1052                        face)))
1053
1054 (defcustom gnus-alter-header-function nil
1055   "Function called to allow alteration of article header structures.
1056 The function is called with one parameter, the article header vector,
1057 which it may alter in any way."
1058   :type '(choice (const :tag "None" nil)
1059                  function)
1060   :group 'gnus-summary)
1061
1062 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1063   "Function used to decode a string with encoded words.")
1064
1065 (defvar gnus-decode-encoded-address-function
1066   'mail-decode-encoded-address-string
1067   "Function used to decode addresses with encoded words.")
1068
1069 (defcustom gnus-extra-headers '(To Newsgroups)
1070   "*Extra headers to parse."
1071   :version "21.1"
1072   :group 'gnus-summary
1073   :type '(repeat symbol))
1074
1075 (defcustom gnus-ignored-from-addresses
1076   (and user-mail-address
1077        (not (string= user-mail-address ""))
1078        (regexp-quote user-mail-address))
1079   "*From headers that may be suppressed in favor of To headers.
1080 This can be a regexp or a list of regexps."
1081   :version "21.1"
1082   :group 'gnus-summary
1083   :type '(choice regexp
1084                  (repeat :tag "Regexp List" regexp)))
1085
1086 (defsubst gnus-ignored-from-addresses ()
1087   (gnus-orify-regexp gnus-ignored-from-addresses))
1088
1089 (defcustom gnus-summary-to-prefix "-> "
1090   "*String prefixed to the To field in the summary line when
1091 using `gnus-ignored-from-addresses'."
1092   :version "22.1"
1093   :group 'gnus-summary
1094   :type 'string)
1095
1096 (defcustom gnus-summary-newsgroup-prefix "=> "
1097   "*String prefixed to the Newsgroup field in the summary
1098 line when using `gnus-ignored-from-addresses'."
1099   :version "22.1"
1100   :group 'gnus-summary
1101   :type 'string)
1102
1103 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1104   "List of charsets that should be ignored.
1105 When these charsets are used in the \"charset\" parameter, the
1106 default charset will be used instead."
1107   :version "21.1"
1108   :type '(repeat symbol)
1109   :group 'gnus-charset)
1110
1111 (gnus-define-group-parameter
1112  ignored-charsets
1113  :type list
1114  :function-document
1115  "Return the ignored charsets of GROUP."
1116  :variable gnus-group-ignored-charsets-alist
1117  :variable-default
1118  '(("alt\\.chinese\\.text" iso-8859-1))
1119  :variable-document
1120  "Alist of regexps (to match group names) and charsets that should be ignored.
1121 When these charsets are used in the \"charset\" parameter, the
1122 default charset will be used instead."
1123  :variable-group gnus-charset
1124  :variable-type '(repeat (cons (regexp :tag "Group")
1125                                (repeat symbol)))
1126  :parameter-type '(choice :tag "Ignored charsets"
1127                           :value nil
1128                           (repeat (symbol)))
1129  :parameter-document       "\
1130 List of charsets that should be ignored.
1131
1132 When these charsets are used in the \"charset\" parameter, the
1133 default charset will be used instead.")
1134
1135 (defcustom gnus-group-highlight-words-alist nil
1136   "Alist of group regexps and highlight regexps.
1137 This variable uses the same syntax as `gnus-emphasis-alist'."
1138   :version "21.1"
1139   :type '(repeat (cons (regexp :tag "Group")
1140                        (repeat (list (regexp :tag "Highlight regexp")
1141                                      (number :tag "Group for entire word" 0)
1142                                      (number :tag "Group for displayed part" 0)
1143                                      (symbol :tag "Face"
1144                                              gnus-emphasis-highlight-words)))))
1145   :group 'gnus-summary-visual)
1146
1147 (defcustom gnus-summary-show-article-charset-alist
1148   nil
1149   "Alist of number and charset.
1150 The article will be shown with the charset corresponding to the
1151 numbered argument.
1152 For example: ((1 . cn-gb-2312) (2 . big5))."
1153   :version "21.1"
1154   :type '(repeat (cons (number :tag "Argument" 1)
1155                        (symbol :tag "Charset")))
1156   :group 'gnus-charset)
1157
1158 (defcustom gnus-preserve-marks t
1159   "Whether marks are preserved when moving, copying and respooling messages."
1160   :version "21.1"
1161   :type 'boolean
1162   :group 'gnus-summary-marks)
1163
1164 (defcustom gnus-alter-articles-to-read-function nil
1165   "Function to be called to alter the list of articles to be selected."
1166   :type '(choice (const nil) function)
1167   :group 'gnus-summary)
1168
1169 (defcustom gnus-orphan-score nil
1170   "*All orphans get this score added.  Set in the score file."
1171   :group 'gnus-score-default
1172   :type '(choice (const nil)
1173                  integer))
1174
1175 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1176   "*A regexp to match MIME parts when saving multiple parts of a
1177 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1178 This regexp will be used by default when prompting the user for which
1179 type of files to save."
1180   :group 'gnus-summary
1181   :type 'regexp)
1182
1183 (defcustom gnus-read-all-available-headers nil
1184   "Whether Gnus should parse all headers made available to it.
1185 This is mostly relevant for slow back ends where the user may
1186 wish to widen the summary buffer to include all headers
1187 that were fetched.  Say, for nnultimate groups."
1188   :version "22.1"
1189   :group 'gnus-summary
1190   :type '(choice boolean regexp))
1191
1192 (defcustom gnus-summary-muttprint-program "muttprint"
1193   "Command (and optional arguments) used to run Muttprint."
1194   :version "22.1"
1195   :group 'gnus-summary
1196   :type 'string)
1197
1198 (defcustom gnus-article-loose-mime t
1199   "If non-nil, don't require MIME-Version header.
1200 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1201 supply the MIME-Version header or deliberately strip it from the mail.
1202 If non-nil (the default), Gnus will treat some articles as MIME
1203 even if the MIME-Version header is missing."
1204   :version "22.1"
1205   :type 'boolean
1206   :group 'gnus-article-mime)
1207
1208 (defcustom gnus-article-emulate-mime t
1209   "If non-nil, use MIME emulation for uuencode and the like.
1210 This means that Gnus will search message bodies for text that look
1211 like uuencoded bits, yEncoded bits, and so on, and present that using
1212 the normal Gnus MIME machinery."
1213   :version "22.1"
1214   :type 'boolean
1215   :group 'gnus-article-mime)
1216
1217 ;;; Internal variables
1218
1219 (defvar gnus-summary-display-cache nil)
1220 (defvar gnus-article-mime-handles nil)
1221 (defvar gnus-article-decoded-p nil)
1222 (defvar gnus-article-charset nil)
1223 (defvar gnus-article-ignored-charsets nil)
1224 (defvar gnus-scores-exclude-files nil)
1225 (defvar gnus-page-broken nil)
1226
1227 (defvar gnus-original-article nil)
1228 (defvar gnus-article-internal-prepare-hook nil)
1229 (defvar gnus-newsgroup-process-stack nil)
1230
1231 (defvar gnus-thread-indent-array nil)
1232 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1233 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1234   "Function called to sort the articles within a thread after it has been gathered together.")
1235
1236 (defvar gnus-summary-save-parts-type-history nil)
1237 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1238
1239 ;; Avoid highlighting in kill files.
1240 (defvar gnus-summary-inhibit-highlight nil)
1241 (defvar gnus-newsgroup-selected-overlay nil)
1242 (defvar gnus-inhibit-limiting nil)
1243 (defvar gnus-newsgroup-adaptive-score-file nil)
1244 (defvar gnus-current-score-file nil)
1245 (defvar gnus-current-move-group nil)
1246 (defvar gnus-current-copy-group nil)
1247 (defvar gnus-current-crosspost-group nil)
1248 (defvar gnus-newsgroup-display nil)
1249
1250 (defvar gnus-newsgroup-dependencies nil)
1251 (defvar gnus-newsgroup-adaptive nil)
1252 (defvar gnus-summary-display-article-function nil)
1253 (defvar gnus-summary-highlight-line-function nil
1254   "Function called after highlighting a summary line.")
1255
1256 (defvar gnus-summary-line-format-alist
1257   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1258     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1259     (?s gnus-tmp-subject-or-nil ?s)
1260     (?n gnus-tmp-name ?s)
1261     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1262         ?s)
1263     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1264             gnus-tmp-from) ?s)
1265     (?F gnus-tmp-from ?s)
1266     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1267     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1268     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1269     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1270     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1271     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1272     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1273     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1274     (?L gnus-tmp-lines ?s)
1275     (?O gnus-tmp-downloaded ?c)
1276     (?I gnus-tmp-indentation ?s)
1277     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1278     (?R gnus-tmp-replied ?c)
1279     (?\[ gnus-tmp-opening-bracket ?c)
1280     (?\] gnus-tmp-closing-bracket ?c)
1281     (?\> (make-string gnus-tmp-level ? ) ?s)
1282     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1283     (?i gnus-tmp-score ?d)
1284     (?z gnus-tmp-score-char ?c)
1285     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1286     (?U gnus-tmp-unread ?c)
1287     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1288         ?s)
1289     (?t (gnus-summary-number-of-articles-in-thread
1290          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1291         ?d)
1292     (?e (gnus-summary-number-of-articles-in-thread
1293          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1294         ?c)
1295     (?u gnus-tmp-user-defined ?s)
1296     (?P (gnus-pick-line-number) ?d)
1297     (?B gnus-tmp-thread-tree-header-string ?s)
1298     (user-date (gnus-user-date
1299                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1300   "An alist of format specifications that can appear in summary lines.
1301 These are paired with what variables they correspond with, along with
1302 the type of the variable (string, integer, character, etc).")
1303
1304 (defvar gnus-summary-dummy-line-format-alist
1305   `((?S gnus-tmp-subject ?s)
1306     (?N gnus-tmp-number ?d)
1307     (?u gnus-tmp-user-defined ?s)))
1308
1309 (defvar gnus-summary-mode-line-format-alist
1310   `((?G gnus-tmp-group-name ?s)
1311     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1312     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1313     (?A gnus-tmp-article-number ?d)
1314     (?Z gnus-tmp-unread-and-unselected ?s)
1315     (?V gnus-version ?s)
1316     (?U gnus-tmp-unread-and-unticked ?d)
1317     (?S gnus-tmp-subject ?s)
1318     (?e gnus-tmp-unselected ?d)
1319     (?u gnus-tmp-user-defined ?s)
1320     (?d (length gnus-newsgroup-dormant) ?d)
1321     (?t (length gnus-newsgroup-marked) ?d)
1322     (?h (length gnus-newsgroup-spam-marked) ?d)
1323     (?r (length gnus-newsgroup-reads) ?d)
1324     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1325     (?E gnus-newsgroup-expunged-tally ?d)
1326     (?s (gnus-current-score-file-nondirectory) ?s)))
1327
1328 (defvar gnus-last-search-regexp nil
1329   "Default regexp for article search command.")
1330
1331 (defvar gnus-last-shell-command nil
1332   "Default shell command on article.")
1333
1334 (defvar gnus-newsgroup-agentized nil
1335   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1336 (defvar gnus-newsgroup-begin nil)
1337 (defvar gnus-newsgroup-end nil)
1338 (defvar gnus-newsgroup-last-rmail nil)
1339 (defvar gnus-newsgroup-last-mail nil)
1340 (defvar gnus-newsgroup-last-folder nil)
1341 (defvar gnus-newsgroup-last-file nil)
1342 (defvar gnus-newsgroup-last-directory nil)
1343 (defvar gnus-newsgroup-auto-expire nil)
1344 (defvar gnus-newsgroup-active nil)
1345
1346 (defvar gnus-newsgroup-data nil)
1347 (defvar gnus-newsgroup-data-reverse nil)
1348 (defvar gnus-newsgroup-limit nil)
1349 (defvar gnus-newsgroup-limits nil)
1350 (defvar gnus-summary-use-undownloaded-faces nil)
1351
1352 (defvar gnus-newsgroup-unreads nil
1353   "Sorted list of unread articles in the current newsgroup.")
1354
1355 (defvar gnus-newsgroup-unselected nil
1356   "Sorted list of unselected unread articles in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-reads nil
1359   "Alist of read articles and article marks in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-expunged-tally nil)
1362
1363 (defvar gnus-newsgroup-marked nil
1364   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1365
1366 (defvar gnus-newsgroup-spam-marked nil
1367   "List of ranges of articles that have been marked as spam.")
1368
1369 (defvar gnus-newsgroup-killed nil
1370   "List of ranges of articles that have been through the scoring process.")
1371
1372 (defvar gnus-newsgroup-cached nil
1373   "Sorted list of articles that come from the article cache.")
1374
1375 (defvar gnus-newsgroup-saved nil
1376   "List of articles that have been saved.")
1377
1378 (defvar gnus-newsgroup-kill-headers nil)
1379
1380 (defvar gnus-newsgroup-replied nil
1381   "List of articles that have been replied to in the current newsgroup.")
1382
1383 (defvar gnus-newsgroup-forwarded nil
1384   "List of articles that have been forwarded in the current newsgroup.")
1385
1386 (defvar gnus-newsgroup-recent nil
1387   "List of articles that have are recent in the current newsgroup.")
1388
1389 (defvar gnus-newsgroup-expirable nil
1390   "Sorted list of articles in the current newsgroup that can be expired.")
1391
1392 (defvar gnus-newsgroup-processable nil
1393   "List of articles in the current newsgroup that can be processed.")
1394
1395 (defvar gnus-newsgroup-downloadable nil
1396   "Sorted list of articles in the current newsgroup that can be processed.")
1397
1398 (defvar gnus-newsgroup-unfetched nil
1399   "Sorted list of articles in the current newsgroup whose headers have
1400 not been fetched into the agent.
1401
1402 This list will always be a subset of gnus-newsgroup-undownloaded.")
1403
1404 (defvar gnus-newsgroup-undownloaded nil
1405   "List of articles in the current newsgroup that haven't been downloaded.")
1406
1407 (defvar gnus-newsgroup-unsendable nil
1408   "List of articles in the current newsgroup that won't be sent.")
1409
1410 (defvar gnus-newsgroup-bookmarks nil
1411   "List of articles in the current newsgroup that have bookmarks.")
1412
1413 (defvar gnus-newsgroup-dormant nil
1414   "Sorted list of dormant articles in the current newsgroup.")
1415
1416 (defvar gnus-newsgroup-unseen nil
1417   "List of unseen articles in the current newsgroup.")
1418
1419 (defvar gnus-newsgroup-seen nil
1420   "Range of seen articles in the current newsgroup.")
1421
1422 (defvar gnus-newsgroup-articles nil
1423   "List of articles in the current newsgroup.")
1424
1425 (defvar gnus-newsgroup-scored nil
1426   "List of scored articles in the current newsgroup.")
1427
1428 (defvar gnus-newsgroup-headers nil
1429   "List of article headers in the current newsgroup.")
1430
1431 (defvar gnus-newsgroup-threads nil)
1432
1433 (defvar gnus-newsgroup-prepared nil
1434   "Whether the current group has been prepared properly.")
1435
1436 (defvar gnus-newsgroup-ancient nil
1437   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1438
1439 (defvar gnus-newsgroup-sparse nil)
1440
1441 (defvar gnus-current-article nil)
1442 (defvar gnus-article-current nil)
1443 (defvar gnus-current-headers nil)
1444 (defvar gnus-have-all-headers nil)
1445 (defvar gnus-last-article nil)
1446 (defvar gnus-newsgroup-history nil)
1447 (defvar gnus-newsgroup-charset nil)
1448 (defvar gnus-newsgroup-ephemeral-charset nil)
1449 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1450
1451 (defvar gnus-article-before-search nil)
1452
1453 (defvar gnus-summary-local-variables
1454   '(gnus-newsgroup-name
1455     gnus-newsgroup-begin gnus-newsgroup-end
1456     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1457     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1458     gnus-newsgroup-last-directory
1459     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1460     gnus-newsgroup-unselected gnus-newsgroup-marked
1461     gnus-newsgroup-spam-marked
1462     gnus-newsgroup-reads gnus-newsgroup-saved
1463     gnus-newsgroup-replied gnus-newsgroup-forwarded
1464     gnus-newsgroup-recent
1465     gnus-newsgroup-expirable
1466     gnus-newsgroup-processable gnus-newsgroup-killed
1467     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1468     gnus-newsgroup-unfetched
1469     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1470     gnus-newsgroup-seen gnus-newsgroup-articles
1471     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1472     gnus-newsgroup-headers gnus-newsgroup-threads
1473     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1474     gnus-current-article gnus-current-headers gnus-have-all-headers
1475     gnus-last-article gnus-article-internal-prepare-hook
1476     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1477     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1478     gnus-thread-expunge-below
1479     gnus-score-alist gnus-current-score-file
1480     (gnus-summary-expunge-below . global)
1481     (gnus-summary-mark-below . global)
1482     (gnus-orphan-score . global)
1483     gnus-newsgroup-active gnus-scores-exclude-files
1484     gnus-newsgroup-history gnus-newsgroup-ancient
1485     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1486     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1487     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1488     (gnus-newsgroup-expunged-tally . 0)
1489     gnus-cache-removable-articles gnus-newsgroup-cached
1490     gnus-newsgroup-data gnus-newsgroup-data-reverse
1491     gnus-newsgroup-limit gnus-newsgroup-limits
1492     gnus-newsgroup-charset gnus-newsgroup-display
1493     gnus-summary-use-undownloaded-faces)
1494   "Variables that are buffer-local to the summary buffers.")
1495
1496 (defvar gnus-newsgroup-variables nil
1497   "A list of variables that have separate values in different newsgroups.
1498 A list of newsgroup (summary buffer) local variables, or cons of
1499 variables and their default expressions to be evalled (when the default
1500 values are not nil), that should be made global while the summary buffer
1501 is active.
1502
1503 Note: The default expressions will be evaluated (using function `eval')
1504 before assignment to the local variable rather than just assigned to it.
1505 If the default expression is the symbol `global', that symbol will not
1506 be evaluated but the global value of the local variable will be used
1507 instead.
1508
1509 These variables can be used to set variables in the group parameters
1510 while still allowing them to affect operations done in other buffers.
1511 For example:
1512
1513 \(setq gnus-newsgroup-variables
1514      '(message-use-followup-to
1515        (gnus-visible-headers .
1516          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1517 ")
1518
1519 ;; Byte-compiler warning.
1520 (eval-when-compile
1521   ;; Bind features so that require will believe that gnus-sum has
1522   ;; already been loaded (avoids infinite recursion)
1523   (let ((features (cons 'gnus-sum features)))
1524     ;; Several of the declarations in gnus-sum are needed to load the
1525     ;; following files. Right now, these definitions have been
1526     ;; compiled but not defined (evaluated).  We could either do a
1527     ;; eval-and-compile about all of the declarations or evaluate the
1528     ;; source file.
1529     (if (boundp 'gnus-newsgroup-variables)
1530         nil
1531       (load "gnus-sum.el" t t t))
1532     (require 'gnus)
1533     (require 'gnus-art)))
1534
1535 ;; MIME stuff.
1536
1537 (defvar gnus-decode-encoded-word-methods
1538   '(mail-decode-encoded-word-string)
1539   "List of methods used to decode encoded words.
1540
1541 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1542 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1543 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1544 whose names match REGEXP.
1545
1546 For example:
1547 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1548  mail-decode-encoded-word-string
1549  (\"chinese\" . rfc1843-decode-string))")
1550
1551 (defvar gnus-decode-encoded-word-methods-cache nil)
1552
1553 (defun gnus-multi-decode-encoded-word-string (string)
1554   "Apply the functions from `gnus-encoded-word-methods' that match."
1555   (unless (and gnus-decode-encoded-word-methods-cache
1556                (eq gnus-newsgroup-name
1557                    (car gnus-decode-encoded-word-methods-cache)))
1558     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1559     (mapcar (lambda (x)
1560               (if (symbolp x)
1561                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1562                 (if (and gnus-newsgroup-name
1563                          (string-match (car x) gnus-newsgroup-name))
1564                     (nconc gnus-decode-encoded-word-methods-cache
1565                            (list (cdr x))))))
1566             gnus-decode-encoded-word-methods))
1567   (let ((xlist gnus-decode-encoded-word-methods-cache))
1568     (pop xlist)
1569     (while xlist
1570       (setq string (funcall (pop xlist) string))))
1571   string)
1572
1573 ;; Subject simplification.
1574
1575 (defun gnus-simplify-whitespace (str)
1576   "Remove excessive whitespace from STR."
1577   ;; Multiple spaces.
1578   (while (string-match "[ \t][ \t]+" str)
1579     (setq str (concat (substring str 0 (match-beginning 0))
1580                         " "
1581                         (substring str (match-end 0)))))
1582   ;; Leading spaces.
1583   (when (string-match "^[ \t]+" str)
1584     (setq str (substring str (match-end 0))))
1585   ;; Trailing spaces.
1586   (when (string-match "[ \t]+$" str)
1587     (setq str (substring str 0 (match-beginning 0))))
1588   str)
1589
1590 (defun gnus-simplify-all-whitespace (str)
1591   "Remove all whitespace from STR."
1592   (while (string-match "[ \t\n]+" str)
1593     (setq str (replace-match "" nil nil str)))
1594   str)
1595
1596 (defsubst gnus-simplify-subject-re (subject)
1597   "Remove \"Re:\" from subject lines."
1598   (if (string-match message-subject-re-regexp subject)
1599       (substring subject (match-end 0))
1600     subject))
1601
1602 (defun gnus-simplify-subject (subject &optional re-only)
1603   "Remove `Re:' and words in parentheses.
1604 If RE-ONLY is non-nil, strip leading `Re:'s only."
1605   (let ((case-fold-search t))           ;Ignore case.
1606     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1607     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1608       (setq subject (substring subject (match-end 0))))
1609     ;; Remove uninteresting prefixes.
1610     (when (and (not re-only)
1611                gnus-simplify-ignored-prefixes
1612                (string-match gnus-simplify-ignored-prefixes subject))
1613       (setq subject (substring subject (match-end 0))))
1614     ;; Remove words in parentheses from end.
1615     (unless re-only
1616       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1617         (setq subject (substring subject 0 (match-beginning 0)))))
1618     ;; Return subject string.
1619     subject))
1620
1621 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1622 ;; all whitespace.
1623 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1624   (goto-char (point-min))
1625   (while (re-search-forward regexp nil t)
1626     (replace-match (or newtext ""))))
1627
1628 (defun gnus-simplify-buffer-fuzzy ()
1629   "Simplify string in the buffer fuzzily.
1630 The string in the accessible portion of the current buffer is simplified.
1631 It is assumed to be a single-line subject.
1632 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1633 matter is removed.  Additional things can be deleted by setting
1634 `gnus-simplify-subject-fuzzy-regexp'."
1635   (let ((case-fold-search t)
1636         (modified-tick))
1637     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1638
1639     (while (not (eq modified-tick (buffer-modified-tick)))
1640       (setq modified-tick (buffer-modified-tick))
1641       (cond
1642        ((listp gnus-simplify-subject-fuzzy-regexp)
1643         (mapcar 'gnus-simplify-buffer-fuzzy-step
1644                 gnus-simplify-subject-fuzzy-regexp))
1645        (gnus-simplify-subject-fuzzy-regexp
1646         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1647       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1648       (gnus-simplify-buffer-fuzzy-step
1649        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1650       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1651
1652     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1653     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1654     (gnus-simplify-buffer-fuzzy-step " $")
1655     (gnus-simplify-buffer-fuzzy-step "^ +")))
1656
1657 (defun gnus-simplify-subject-fuzzy (subject)
1658   "Simplify a subject string fuzzily.
1659 See `gnus-simplify-buffer-fuzzy' for details."
1660   (save-excursion
1661     (gnus-set-work-buffer)
1662     (let ((case-fold-search t))
1663       ;; Remove uninteresting prefixes.
1664       (when (and gnus-simplify-ignored-prefixes
1665                  (string-match gnus-simplify-ignored-prefixes subject))
1666         (setq subject (substring subject (match-end 0))))
1667       (insert subject)
1668       (inline (gnus-simplify-buffer-fuzzy))
1669       (buffer-string))))
1670
1671 (defsubst gnus-simplify-subject-fully (subject)
1672   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1673   (cond
1674    (gnus-simplify-subject-functions
1675     (gnus-map-function gnus-simplify-subject-functions subject))
1676    ((null gnus-summary-gather-subject-limit)
1677     (gnus-simplify-subject-re subject))
1678    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1679     (gnus-simplify-subject-fuzzy subject))
1680    ((numberp gnus-summary-gather-subject-limit)
1681     (gnus-limit-string (gnus-simplify-subject-re subject)
1682                        gnus-summary-gather-subject-limit))
1683    (t
1684     subject)))
1685
1686 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1687   "Check whether two subjects are equal.
1688 If optional argument SIMPLE-FIRST is t, first argument is already
1689 simplified."
1690   (cond
1691    ((null simple-first)
1692     (equal (gnus-simplify-subject-fully s1)
1693            (gnus-simplify-subject-fully s2)))
1694    (t
1695     (equal s1
1696            (gnus-simplify-subject-fully s2)))))
1697
1698 (defun gnus-summary-bubble-group ()
1699   "Increase the score of the current group.
1700 This is a handy function to add to `gnus-summary-exit-hook' to
1701 increase the score of each group you read."
1702   (gnus-group-add-score gnus-newsgroup-name))
1703
1704 \f
1705 ;;;
1706 ;;; Gnus summary mode
1707 ;;;
1708
1709 (put 'gnus-summary-mode 'mode-class 'special)
1710
1711 (defvar gnus-article-commands-menu)
1712
1713 ;; Non-orthogonal keys
1714
1715 (gnus-define-keys gnus-summary-mode-map
1716   " " gnus-summary-next-page
1717   "\177" gnus-summary-prev-page
1718   [delete] gnus-summary-prev-page
1719   [backspace] gnus-summary-prev-page
1720   "\r" gnus-summary-scroll-up
1721   "\M-\r" gnus-summary-scroll-down
1722   "n" gnus-summary-next-unread-article
1723   "p" gnus-summary-prev-unread-article
1724   "N" gnus-summary-next-article
1725   "P" gnus-summary-prev-article
1726   "\M-\C-n" gnus-summary-next-same-subject
1727   "\M-\C-p" gnus-summary-prev-same-subject
1728   "\M-n" gnus-summary-next-unread-subject
1729   "\M-p" gnus-summary-prev-unread-subject
1730   "." gnus-summary-first-unread-article
1731   "," gnus-summary-best-unread-article
1732   "\M-s" gnus-summary-search-article-forward
1733   "\M-r" gnus-summary-search-article-backward
1734   "\M-S" gnus-summary-repeat-search-article-forward
1735   "\M-R" gnus-summary-repeat-search-article-backward
1736   "<" gnus-summary-beginning-of-article
1737   ">" gnus-summary-end-of-article
1738   "j" gnus-summary-goto-article
1739   "^" gnus-summary-refer-parent-article
1740   "\M-^" gnus-summary-refer-article
1741   "u" gnus-summary-tick-article-forward
1742   "!" gnus-summary-tick-article-forward
1743   "U" gnus-summary-tick-article-backward
1744   "d" gnus-summary-mark-as-read-forward
1745   "D" gnus-summary-mark-as-read-backward
1746   "E" gnus-summary-mark-as-expirable
1747   "\M-u" gnus-summary-clear-mark-forward
1748   "\M-U" gnus-summary-clear-mark-backward
1749   "k" gnus-summary-kill-same-subject-and-select
1750   "\C-k" gnus-summary-kill-same-subject
1751   "\M-\C-k" gnus-summary-kill-thread
1752   "\M-\C-l" gnus-summary-lower-thread
1753   "e" gnus-summary-edit-article
1754   "#" gnus-summary-mark-as-processable
1755   "\M-#" gnus-summary-unmark-as-processable
1756   "\M-\C-t" gnus-summary-toggle-threads
1757   "\M-\C-s" gnus-summary-show-thread
1758   "\M-\C-h" gnus-summary-hide-thread
1759   "\M-\C-f" gnus-summary-next-thread
1760   "\M-\C-b" gnus-summary-prev-thread
1761   [(meta down)] gnus-summary-next-thread
1762   [(meta up)] gnus-summary-prev-thread
1763   "\M-\C-u" gnus-summary-up-thread
1764   "\M-\C-d" gnus-summary-down-thread
1765   "&" gnus-summary-execute-command
1766   "c" gnus-summary-catchup-and-exit
1767   "\C-w" gnus-summary-mark-region-as-read
1768   "\C-t" gnus-summary-toggle-truncation
1769   "?" gnus-summary-mark-as-dormant
1770   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1771   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1772   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1773   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1774   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1775   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1776   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1777   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1778   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1779   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1780   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1781   "=" gnus-summary-expand-window
1782   "\C-x\C-s" gnus-summary-reselect-current-group
1783   "\M-g" gnus-summary-rescan-group
1784   "w" gnus-summary-stop-page-breaking
1785   "\C-c\C-r" gnus-summary-caesar-message
1786   "f" gnus-summary-followup
1787   "F" gnus-summary-followup-with-original
1788   "C" gnus-summary-cancel-article
1789   "r" gnus-summary-reply
1790   "R" gnus-summary-reply-with-original
1791   "\C-c\C-f" gnus-summary-mail-forward
1792   "o" gnus-summary-save-article
1793   "\C-o" gnus-summary-save-article-mail
1794   "|" gnus-summary-pipe-output
1795   "\M-k" gnus-summary-edit-local-kill
1796   "\M-K" gnus-summary-edit-global-kill
1797   ;; "V" gnus-version
1798   "\C-c\C-d" gnus-summary-describe-group
1799   "q" gnus-summary-exit
1800   "Q" gnus-summary-exit-no-update
1801   "\C-c\C-i" gnus-info-find-node
1802   gnus-mouse-2 gnus-mouse-pick-article
1803   [follow-link] mouse-face
1804   "m" gnus-summary-mail-other-window
1805   "a" gnus-summary-post-news
1806   "i" gnus-summary-news-other-window
1807   "x" gnus-summary-limit-to-unread
1808   "s" gnus-summary-isearch-article
1809   "t" gnus-summary-toggle-header
1810   "g" gnus-summary-show-article
1811   "l" gnus-summary-goto-last-article
1812   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1813   "\C-d" gnus-summary-enter-digest-group
1814   "\M-\C-d" gnus-summary-read-document
1815   "\M-\C-e" gnus-summary-edit-parameters
1816   "\M-\C-a" gnus-summary-customize-parameters
1817   "\C-c\C-b" gnus-bug
1818   "*" gnus-cache-enter-article
1819   "\M-*" gnus-cache-remove-article
1820   "\M-&" gnus-summary-universal-argument
1821   "\C-l" gnus-recenter
1822   "I" gnus-summary-increase-score
1823   "L" gnus-summary-lower-score
1824   "\M-i" gnus-symbolic-argument
1825   "h" gnus-summary-select-article-buffer
1826
1827   "b" gnus-article-view-part
1828   "\M-t" gnus-summary-toggle-display-buttonized
1829
1830   "V" gnus-summary-score-map
1831   "X" gnus-uu-extract-map
1832   "S" gnus-summary-send-map)
1833
1834 ;; Sort of orthogonal keymap
1835 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1836   "t" gnus-summary-tick-article-forward
1837   "!" gnus-summary-tick-article-forward
1838   "d" gnus-summary-mark-as-read-forward
1839   "r" gnus-summary-mark-as-read-forward
1840   "c" gnus-summary-clear-mark-forward
1841   " " gnus-summary-clear-mark-forward
1842   "e" gnus-summary-mark-as-expirable
1843   "x" gnus-summary-mark-as-expirable
1844   "?" gnus-summary-mark-as-dormant
1845   "b" gnus-summary-set-bookmark
1846   "B" gnus-summary-remove-bookmark
1847   "#" gnus-summary-mark-as-processable
1848   "\M-#" gnus-summary-unmark-as-processable
1849   "S" gnus-summary-limit-include-expunged
1850   "C" gnus-summary-catchup
1851   "H" gnus-summary-catchup-to-here
1852   "h" gnus-summary-catchup-from-here
1853   "\C-c" gnus-summary-catchup-all
1854   "k" gnus-summary-kill-same-subject-and-select
1855   "K" gnus-summary-kill-same-subject
1856   "P" gnus-uu-mark-map)
1857
1858 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1859   "c" gnus-summary-clear-above
1860   "u" gnus-summary-tick-above
1861   "m" gnus-summary-mark-above
1862   "k" gnus-summary-kill-below)
1863
1864 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1865   "/" gnus-summary-limit-to-subject
1866   "n" gnus-summary-limit-to-articles
1867   "b" gnus-summary-limit-to-bodies
1868   "h" gnus-summary-limit-to-headers
1869   "w" gnus-summary-pop-limit
1870   "s" gnus-summary-limit-to-subject
1871   "a" gnus-summary-limit-to-author
1872   "u" gnus-summary-limit-to-unread
1873   "m" gnus-summary-limit-to-marks
1874   "M" gnus-summary-limit-exclude-marks
1875   "v" gnus-summary-limit-to-score
1876   "*" gnus-summary-limit-include-cached
1877   "D" gnus-summary-limit-include-dormant
1878   "T" gnus-summary-limit-include-thread
1879   "d" gnus-summary-limit-exclude-dormant
1880   "t" gnus-summary-limit-to-age
1881   "." gnus-summary-limit-to-unseen
1882   "x" gnus-summary-limit-to-extra
1883   "p" gnus-summary-limit-to-display-predicate
1884   "E" gnus-summary-limit-include-expunged
1885   "c" gnus-summary-limit-exclude-childless-dormant
1886   "C" gnus-summary-limit-mark-excluded-as-read
1887   "o" gnus-summary-insert-old-articles
1888   "N" gnus-summary-insert-new-articles
1889   "S" gnus-summary-limit-to-singletons
1890   "r" gnus-summary-limit-to-replied
1891   "R" gnus-summary-limit-to-recipient)
1892
1893 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1894   "n" gnus-summary-next-unread-article
1895   "p" gnus-summary-prev-unread-article
1896   "N" gnus-summary-next-article
1897   "P" gnus-summary-prev-article
1898   "\C-n" gnus-summary-next-same-subject
1899   "\C-p" gnus-summary-prev-same-subject
1900   "\M-n" gnus-summary-next-unread-subject
1901   "\M-p" gnus-summary-prev-unread-subject
1902   "f" gnus-summary-first-unread-article
1903   "b" gnus-summary-best-unread-article
1904   "j" gnus-summary-goto-article
1905   "g" gnus-summary-goto-subject
1906   "l" gnus-summary-goto-last-article
1907   "o" gnus-summary-pop-article)
1908
1909 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1910   "k" gnus-summary-kill-thread
1911   "l" gnus-summary-lower-thread
1912   "i" gnus-summary-raise-thread
1913   "T" gnus-summary-toggle-threads
1914   "t" gnus-summary-rethread-current
1915   "^" gnus-summary-reparent-thread
1916   "\M-^" gnus-summary-reparent-children
1917   "s" gnus-summary-show-thread
1918   "S" gnus-summary-show-all-threads
1919   "h" gnus-summary-hide-thread
1920   "H" gnus-summary-hide-all-threads
1921   "n" gnus-summary-next-thread
1922   "p" gnus-summary-prev-thread
1923   "u" gnus-summary-up-thread
1924   "o" gnus-summary-top-thread
1925   "d" gnus-summary-down-thread
1926   "#" gnus-uu-mark-thread
1927   "\M-#" gnus-uu-unmark-thread)
1928
1929 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1930   "g" gnus-summary-prepare
1931   "c" gnus-summary-insert-cached-articles
1932   "d" gnus-summary-insert-dormant-articles
1933   "t" gnus-summary-insert-ticked-articles)
1934
1935 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1936   "c" gnus-summary-catchup-and-exit
1937   "C" gnus-summary-catchup-all-and-exit
1938   "E" gnus-summary-exit-no-update
1939   "Q" gnus-summary-exit
1940   "Z" gnus-summary-exit
1941   "n" gnus-summary-catchup-and-goto-next-group
1942   "p" gnus-summary-catchup-and-goto-prev-group
1943   "R" gnus-summary-reselect-current-group
1944   "G" gnus-summary-rescan-group
1945   "N" gnus-summary-next-group
1946   "s" gnus-summary-save-newsrc
1947   "P" gnus-summary-prev-group)
1948
1949 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1950   " " gnus-summary-next-page
1951   "n" gnus-summary-next-page
1952   "\177" gnus-summary-prev-page
1953   [delete] gnus-summary-prev-page
1954   "p" gnus-summary-prev-page
1955   "\r" gnus-summary-scroll-up
1956   "\M-\r" gnus-summary-scroll-down
1957   "<" gnus-summary-beginning-of-article
1958   ">" gnus-summary-end-of-article
1959   "b" gnus-summary-beginning-of-article
1960   "e" gnus-summary-end-of-article
1961   "^" gnus-summary-refer-parent-article
1962   "r" gnus-summary-refer-parent-article
1963   "D" gnus-summary-enter-digest-group
1964   "R" gnus-summary-refer-references
1965   "T" gnus-summary-refer-thread
1966   "g" gnus-summary-show-article
1967   "s" gnus-summary-isearch-article
1968   "P" gnus-summary-print-article
1969   "M" gnus-mailing-list-insinuate
1970   "t" gnus-article-babel)
1971
1972 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1973   "b" gnus-article-add-buttons
1974   "B" gnus-article-add-buttons-to-head
1975   "o" gnus-article-treat-overstrike
1976   "e" gnus-article-emphasize
1977   "w" gnus-article-fill-cited-article
1978   "Q" gnus-article-fill-long-lines
1979   "L" gnus-article-toggle-truncate-lines
1980   "C" gnus-article-capitalize-sentences
1981   "c" gnus-article-remove-cr
1982   "q" gnus-article-de-quoted-unreadable
1983   "6" gnus-article-de-base64-unreadable
1984   "Z" gnus-article-decode-HZ
1985   "A" gnus-article-treat-ansi-sequences
1986   "h" gnus-article-wash-html
1987   "u" gnus-article-unsplit-urls
1988   "s" gnus-summary-force-verify-and-decrypt
1989   "f" gnus-article-display-x-face
1990   "l" gnus-summary-stop-page-breaking
1991   "r" gnus-summary-caesar-message
1992   "m" gnus-summary-morse-message
1993   "t" gnus-summary-toggle-header
1994   "g" gnus-treat-smiley
1995   "v" gnus-summary-verbose-headers
1996   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1997   "p" gnus-article-verify-x-pgp-sig
1998   "d" gnus-article-treat-dumbquotes
1999   "i" gnus-summary-idna-message)
2000
2001 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2002   ;; mnemonic: deuglif*Y*
2003   "u" gnus-article-outlook-unwrap-lines
2004   "a" gnus-article-outlook-repair-attribution
2005   "c" gnus-article-outlook-rearrange-citation
2006   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2007
2008 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2009   "a" gnus-article-hide
2010   "h" gnus-article-hide-headers
2011   "b" gnus-article-hide-boring-headers
2012   "s" gnus-article-hide-signature
2013   "c" gnus-article-hide-citation
2014   "C" gnus-article-hide-citation-in-followups
2015   "l" gnus-article-hide-list-identifiers
2016   "B" gnus-article-strip-banner
2017   "P" gnus-article-hide-pem
2018   "\C-c" gnus-article-hide-citation-maybe)
2019
2020 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2021   "a" gnus-article-highlight
2022   "h" gnus-article-highlight-headers
2023   "c" gnus-article-highlight-citation
2024   "s" gnus-article-highlight-signature)
2025
2026 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2027   "f" gnus-article-treat-fold-headers
2028   "u" gnus-article-treat-unfold-headers
2029   "n" gnus-article-treat-fold-newsgroups)
2030
2031 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2032   "x" gnus-article-display-x-face
2033   "d" gnus-article-display-face
2034   "s" gnus-treat-smiley
2035   "D" gnus-article-remove-images
2036   "f" gnus-treat-from-picon
2037   "m" gnus-treat-mail-picon
2038   "n" gnus-treat-newsgroups-picon)
2039
2040 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2041   "w" gnus-article-decode-mime-words
2042   "c" gnus-article-decode-charset
2043   "v" gnus-mime-view-all-parts
2044   "b" gnus-article-view-part)
2045
2046 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2047   "z" gnus-article-date-ut
2048   "u" gnus-article-date-ut
2049   "l" gnus-article-date-local
2050   "p" gnus-article-date-english
2051   "e" gnus-article-date-lapsed
2052   "o" gnus-article-date-original
2053   "i" gnus-article-date-iso8601
2054   "s" gnus-article-date-user)
2055
2056 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2057   "t" gnus-article-remove-trailing-blank-lines
2058   "l" gnus-article-strip-leading-blank-lines
2059   "m" gnus-article-strip-multiple-blank-lines
2060   "a" gnus-article-strip-blank-lines
2061   "A" gnus-article-strip-all-blank-lines
2062   "s" gnus-article-strip-leading-space
2063   "e" gnus-article-strip-trailing-space
2064   "w" gnus-article-remove-leading-whitespace)
2065
2066 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2067   "v" gnus-version
2068   "f" gnus-summary-fetch-faq
2069   "d" gnus-summary-describe-group
2070   "h" gnus-summary-describe-briefly
2071   "i" gnus-info-find-node
2072   "c" gnus-group-fetch-charter
2073   "C" gnus-group-fetch-control)
2074
2075 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2076   "e" gnus-summary-expire-articles
2077   "\M-\C-e" gnus-summary-expire-articles-now
2078   "\177" gnus-summary-delete-article
2079   [delete] gnus-summary-delete-article
2080   [backspace] gnus-summary-delete-article
2081   "m" gnus-summary-move-article
2082   "r" gnus-summary-respool-article
2083   "w" gnus-summary-edit-article
2084   "c" gnus-summary-copy-article
2085   "B" gnus-summary-crosspost-article
2086   "q" gnus-summary-respool-query
2087   "t" gnus-summary-respool-trace
2088   "i" gnus-summary-import-article
2089   "I" gnus-summary-create-article
2090   "p" gnus-summary-article-posted-p)
2091
2092 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2093   "o" gnus-summary-save-article
2094   "m" gnus-summary-save-article-mail
2095   "F" gnus-summary-write-article-file
2096   "r" gnus-summary-save-article-rmail
2097   "f" gnus-summary-save-article-file
2098   "b" gnus-summary-save-article-body-file
2099   "B" gnus-summary-write-article-body-file
2100   "h" gnus-summary-save-article-folder
2101   "v" gnus-summary-save-article-vm
2102   "p" gnus-summary-pipe-output
2103   "P" gnus-summary-muttprint
2104   "s" gnus-soup-add-article)
2105
2106 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2107   "b" gnus-summary-display-buttonized
2108   "m" gnus-summary-repair-multipart
2109   "v" gnus-article-view-part
2110   "o" gnus-article-save-part
2111   "O" gnus-article-save-part-and-strip
2112   "r" gnus-article-replace-part
2113   "d" gnus-article-delete-part
2114   "t" gnus-article-view-part-as-type
2115   "j" gnus-article-jump-to-part
2116   "c" gnus-article-copy-part
2117   "C" gnus-article-view-part-as-charset
2118   "e" gnus-article-view-part-externally
2119   "H" gnus-article-browse-html-article
2120   "E" gnus-article-encrypt-body
2121   "i" gnus-article-inline-part
2122   "|" gnus-article-pipe-part)
2123
2124 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2125   "p" gnus-summary-mark-as-processable
2126   "u" gnus-summary-unmark-as-processable
2127   "U" gnus-summary-unmark-all-processable
2128   "v" gnus-uu-mark-over
2129   "s" gnus-uu-mark-series
2130   "r" gnus-uu-mark-region
2131   "g" gnus-uu-unmark-region
2132   "R" gnus-uu-mark-by-regexp
2133   "G" gnus-uu-unmark-by-regexp
2134   "t" gnus-uu-mark-thread
2135   "T" gnus-uu-unmark-thread
2136   "a" gnus-uu-mark-all
2137   "b" gnus-uu-mark-buffer
2138   "S" gnus-uu-mark-sparse
2139   "k" gnus-summary-kill-process-mark
2140   "y" gnus-summary-yank-process-mark
2141   "w" gnus-summary-save-process-mark
2142   "i" gnus-uu-invert-processable)
2143
2144 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2145   ;;"x" gnus-uu-extract-any
2146   "m" gnus-summary-save-parts
2147   "u" gnus-uu-decode-uu
2148   "U" gnus-uu-decode-uu-and-save
2149   "s" gnus-uu-decode-unshar
2150   "S" gnus-uu-decode-unshar-and-save
2151   "o" gnus-uu-decode-save
2152   "O" gnus-uu-decode-save
2153   "b" gnus-uu-decode-binhex
2154   "B" gnus-uu-decode-binhex
2155   "p" gnus-uu-decode-postscript
2156   "P" gnus-uu-decode-postscript-and-save)
2157
2158 (gnus-define-keys
2159     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2160   "u" gnus-uu-decode-uu-view
2161   "U" gnus-uu-decode-uu-and-save-view
2162   "s" gnus-uu-decode-unshar-view
2163   "S" gnus-uu-decode-unshar-and-save-view
2164   "o" gnus-uu-decode-save-view
2165   "O" gnus-uu-decode-save-view
2166   "b" gnus-uu-decode-binhex-view
2167   "B" gnus-uu-decode-binhex-view
2168   "p" gnus-uu-decode-postscript-view
2169   "P" gnus-uu-decode-postscript-and-save-view)
2170
2171 (defvar gnus-article-post-menu nil)
2172
2173 (defconst gnus-summary-menu-maxlen 20)
2174
2175 (defun gnus-summary-menu-split (menu)
2176   ;; If we have lots of elements, divide them into groups of 20
2177   ;; and make a pane (or submenu) for each one.
2178   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2179       (let ((menu menu) sublists next
2180             (i 1))
2181         (while menu
2182           ;; Pull off the next gnus-summary-menu-maxlen elements
2183           ;; and make them the next element of sublist.
2184           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2185           (if next
2186               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2187                       nil))
2188           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2189                                              (aref (car (last menu)) 0)) menu)
2190                                sublists))
2191           (setq i (1+ i))
2192           (setq menu next))
2193         (nreverse sublists))
2194     ;; Few elements--put them all in one pane.
2195     menu))
2196
2197 (defun gnus-summary-make-menu-bar ()
2198   (gnus-turn-off-edit-menu 'summary)
2199
2200   (unless (boundp 'gnus-summary-misc-menu)
2201
2202     (easy-menu-define
2203       gnus-summary-kill-menu gnus-summary-mode-map ""
2204       (cons
2205        "Score"
2206        (nconc
2207         (list
2208          ["Customize" gnus-score-customize t])
2209         (gnus-make-score-map 'increase)
2210         (gnus-make-score-map 'lower)
2211         '(("Mark"
2212            ["Kill below" gnus-summary-kill-below t]
2213            ["Mark above" gnus-summary-mark-above t]
2214            ["Tick above" gnus-summary-tick-above t]
2215            ["Clear above" gnus-summary-clear-above t])
2216           ["Current score" gnus-summary-current-score t]
2217           ["Set score" gnus-summary-set-score t]
2218           ["Switch current score file..." gnus-score-change-score-file t]
2219           ["Set mark below..." gnus-score-set-mark-below t]
2220           ["Set expunge below..." gnus-score-set-expunge-below t]
2221           ["Edit current score file" gnus-score-edit-current-scores t]
2222           ["Edit score file" gnus-score-edit-file t]
2223           ["Trace score" gnus-score-find-trace t]
2224           ["Find words" gnus-score-find-favourite-words t]
2225           ["Rescore buffer" gnus-summary-rescore t]
2226           ["Increase score..." gnus-summary-increase-score t]
2227           ["Lower score..." gnus-summary-lower-score t]))))
2228
2229     ;; Define both the Article menu in the summary buffer and the
2230     ;; equivalent Commands menu in the article buffer here for
2231     ;; consistency.
2232     (let ((innards
2233            `(("Hide"
2234               ["All" gnus-article-hide t]
2235               ["Headers" gnus-article-hide-headers t]
2236               ["Signature" gnus-article-hide-signature t]
2237               ["Citation" gnus-article-hide-citation t]
2238               ["List identifiers" gnus-article-hide-list-identifiers t]
2239               ["Banner" gnus-article-strip-banner t]
2240               ["Boring headers" gnus-article-hide-boring-headers t])
2241              ("Highlight"
2242               ["All" gnus-article-highlight t]
2243               ["Headers" gnus-article-highlight-headers t]
2244               ["Signature" gnus-article-highlight-signature t]
2245               ["Citation" gnus-article-highlight-citation t])
2246              ("MIME"
2247               ["Words" gnus-article-decode-mime-words t]
2248               ["Charset" gnus-article-decode-charset t]
2249               ["QP" gnus-article-de-quoted-unreadable t]
2250               ["Base64" gnus-article-de-base64-unreadable t]
2251               ["View MIME buttons" gnus-summary-display-buttonized t]
2252               ["View all" gnus-mime-view-all-parts t]
2253               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2254               ["Encrypt body" gnus-article-encrypt-body
2255                :active (not (gnus-group-read-only-p))
2256                ,@(if (featurep 'xemacs) nil
2257                    '(:help "Encrypt the message body on disk"))]
2258               ["Extract all parts..." gnus-summary-save-parts t]
2259               ("Multipart"
2260                ["Repair multipart" gnus-summary-repair-multipart t]
2261                ["Pipe part..." gnus-article-pipe-part t]
2262                ["Inline part" gnus-article-inline-part t]
2263                ["View part as type..." gnus-article-view-part-as-type t]
2264                ["Encrypt body" gnus-article-encrypt-body
2265                 :active (not (gnus-group-read-only-p))
2266                ,@(if (featurep 'xemacs) nil
2267                    '(:help "Encrypt the message body on disk"))]
2268                ["View part externally" gnus-article-view-part-externally t]
2269                ["View HTML parts in browser" gnus-article-browse-html-article t]
2270                ["View part with charset..." gnus-article-view-part-as-charset t]
2271                ["Copy part" gnus-article-copy-part t]
2272                ["Save part..." gnus-article-save-part t]
2273                ["View part" gnus-article-view-part t]))
2274              ("Date"
2275               ["Local" gnus-article-date-local t]
2276               ["ISO8601" gnus-article-date-iso8601 t]
2277               ["UT" gnus-article-date-ut t]
2278               ["Original" gnus-article-date-original t]
2279               ["Lapsed" gnus-article-date-lapsed t]
2280               ["User-defined" gnus-article-date-user t])
2281              ("Display"
2282               ["Remove images" gnus-article-remove-images t]
2283               ["Toggle smiley" gnus-treat-smiley t]
2284               ["Show X-Face" gnus-article-display-x-face t]
2285               ["Show picons in From" gnus-treat-from-picon t]
2286               ["Show picons in mail headers" gnus-treat-mail-picon t]
2287               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2288               ("View as different encoding"
2289                ,@(gnus-summary-menu-split
2290                   (mapcar
2291                    (lambda (cs)
2292                      ;; Since easymenu under Emacs doesn't allow
2293                      ;; lambda forms for menu commands, we should
2294                      ;; provide intern'ed function symbols.
2295                      (let ((command (intern (format "\
2296 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2297                        (fset command
2298                              `(lambda ()
2299                                 (interactive)
2300                                 (let ((gnus-summary-show-article-charset-alist
2301                                        '((1 . ,cs))))
2302                                   (gnus-summary-show-article 1))))
2303                        `[,(symbol-name cs) ,command t]))
2304                    (sort (if (fboundp 'coding-system-list)
2305                              (coding-system-list)
2306                            (mapcar 'car mm-mime-mule-charset-alist))
2307                          'string<)))))
2308              ("Washing"
2309               ("Remove Blanks"
2310                ["Leading" gnus-article-strip-leading-blank-lines t]
2311                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2312                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2313                ["All of the above" gnus-article-strip-blank-lines t]
2314                ["All" gnus-article-strip-all-blank-lines t]
2315                ["Leading space" gnus-article-strip-leading-space t]
2316                ["Trailing space" gnus-article-strip-trailing-space t]
2317                ["Leading space in headers"
2318                 gnus-article-remove-leading-whitespace t])
2319               ["Overstrike" gnus-article-treat-overstrike t]
2320               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2321               ["Emphasis" gnus-article-emphasize t]
2322               ["Word wrap" gnus-article-fill-cited-article t]
2323               ["Fill long lines" gnus-article-fill-long-lines t]
2324               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2325               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2326               ["Remove CR" gnus-article-remove-cr t]
2327               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2328               ["Base64" gnus-article-de-base64-unreadable t]
2329               ["Rot 13" gnus-summary-caesar-message
2330                ,@(if (featurep 'xemacs) '(t)
2331                    '(:help "\"Caesar rotate\" article by 13"))]
2332               ["De-IDNA" gnus-summary-idna-message t]
2333               ["Morse decode" gnus-summary-morse-message t]
2334               ["Unix pipe..." gnus-summary-pipe-message t]
2335               ["Add buttons" gnus-article-add-buttons t]
2336               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2337               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2338               ["Verbose header" gnus-summary-verbose-headers t]
2339               ["Toggle header" gnus-summary-toggle-header t]
2340               ["Unfold headers" gnus-article-treat-unfold-headers t]
2341               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2342               ["Html" gnus-article-wash-html t]
2343               ["Unsplit URLs" gnus-article-unsplit-urls t]
2344               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2345               ["Decode HZ" gnus-article-decode-HZ t]
2346               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2347               ("(Outlook) Deuglify"
2348                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2349                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2350                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2351                ["Full (Outlook) deuglify"
2352                 gnus-article-outlook-deuglify-article t])
2353               )
2354              ("Output"
2355               ["Save in default format..." gnus-summary-save-article
2356                ,@(if (featurep 'xemacs) '(t)
2357                    '(:help "Save article using default method"))]
2358               ["Save in file..." gnus-summary-save-article-file
2359                ,@(if (featurep 'xemacs) '(t)
2360                    '(:help "Save article in file"))]
2361               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2362               ["Save in MH folder..." gnus-summary-save-article-folder t]
2363               ["Save in VM folder..." gnus-summary-save-article-vm t]
2364               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2365               ["Save body in file..." gnus-summary-save-article-body-file t]
2366               ["Pipe through a filter..." gnus-summary-pipe-output t]
2367               ["Add to SOUP packet" gnus-soup-add-article t]
2368               ["Print with Muttprint..." gnus-summary-muttprint t]
2369               ["Print" gnus-summary-print-article
2370                ,@(if (featurep 'xemacs) '(t)
2371                    '(:help "Generate and print a PostScript image"))])
2372              ("Copy, move,... (Backend)"
2373               ,@(if (featurep 'xemacs) nil
2374                   '(:help "Copying, moving, expiring articles..."))
2375               ["Respool article..." gnus-summary-respool-article t]
2376               ["Move article..." gnus-summary-move-article
2377                (gnus-check-backend-function
2378                 'request-move-article gnus-newsgroup-name)]
2379               ["Copy article..." gnus-summary-copy-article t]
2380               ["Crosspost article..." gnus-summary-crosspost-article
2381                (gnus-check-backend-function
2382                 'request-replace-article gnus-newsgroup-name)]
2383               ["Import file..." gnus-summary-import-article
2384                (gnus-check-backend-function
2385                 'request-accept-article gnus-newsgroup-name)]
2386               ["Create article..." gnus-summary-create-article
2387                (gnus-check-backend-function
2388                 'request-accept-article gnus-newsgroup-name)]
2389               ["Check if posted" gnus-summary-article-posted-p t]
2390               ["Edit article" gnus-summary-edit-article
2391                (not (gnus-group-read-only-p))]
2392               ["Delete article" gnus-summary-delete-article
2393                (gnus-check-backend-function
2394                 'request-expire-articles gnus-newsgroup-name)]
2395               ["Query respool" gnus-summary-respool-query t]
2396               ["Trace respool" gnus-summary-respool-trace t]
2397               ["Delete expirable articles" gnus-summary-expire-articles-now
2398                (gnus-check-backend-function
2399                 'request-expire-articles gnus-newsgroup-name)])
2400              ("Extract"
2401               ["Uudecode" gnus-uu-decode-uu
2402                ,@(if (featurep 'xemacs) '(t)
2403                    '(:help "Decode uuencoded article(s)"))]
2404               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2405               ["Unshar" gnus-uu-decode-unshar t]
2406               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2407               ["Save" gnus-uu-decode-save t]
2408               ["Binhex" gnus-uu-decode-binhex t]
2409               ["Postscript" gnus-uu-decode-postscript t]
2410               ["All MIME parts" gnus-summary-save-parts t])
2411              ("Cache"
2412               ["Enter article" gnus-cache-enter-article t]
2413               ["Remove article" gnus-cache-remove-article t])
2414              ["Translate" gnus-article-babel t]
2415              ["Select article buffer" gnus-summary-select-article-buffer t]
2416              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2417              ["Isearch article..." gnus-summary-isearch-article t]
2418              ["Beginning of the article" gnus-summary-beginning-of-article t]
2419              ["End of the article" gnus-summary-end-of-article t]
2420              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2421              ["Fetch referenced articles" gnus-summary-refer-references t]
2422              ["Fetch current thread" gnus-summary-refer-thread t]
2423              ["Fetch article with id..." gnus-summary-refer-article t]
2424              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2425              ["Redisplay" gnus-summary-show-article t]
2426              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2427       (easy-menu-define
2428         gnus-summary-article-menu gnus-summary-mode-map ""
2429         (cons "Article" innards))
2430
2431       (if (not (keymapp gnus-summary-article-menu))
2432           (easy-menu-define
2433             gnus-article-commands-menu gnus-article-mode-map ""
2434             (cons "Commands" innards))
2435         ;; in Emacs, don't share menu.
2436         (setq gnus-article-commands-menu
2437               (copy-keymap gnus-summary-article-menu))
2438         (define-key gnus-article-mode-map [menu-bar commands]
2439           (cons "Commands" gnus-article-commands-menu))))
2440
2441     (easy-menu-define
2442       gnus-summary-thread-menu gnus-summary-mode-map ""
2443       '("Threads"
2444         ["Find all messages in thread" gnus-summary-refer-thread t]
2445         ["Toggle threading" gnus-summary-toggle-threads t]
2446         ["Hide threads" gnus-summary-hide-all-threads t]
2447         ["Show threads" gnus-summary-show-all-threads t]
2448         ["Hide thread" gnus-summary-hide-thread t]
2449         ["Show thread" gnus-summary-show-thread t]
2450         ["Go to next thread" gnus-summary-next-thread t]
2451         ["Go to previous thread" gnus-summary-prev-thread t]
2452         ["Go down thread" gnus-summary-down-thread t]
2453         ["Go up thread" gnus-summary-up-thread t]
2454         ["Top of thread" gnus-summary-top-thread t]
2455         ["Mark thread as read" gnus-summary-kill-thread t]
2456         ["Lower thread score" gnus-summary-lower-thread t]
2457         ["Raise thread score" gnus-summary-raise-thread t]
2458         ["Rethread current" gnus-summary-rethread-current t]))
2459
2460     (easy-menu-define
2461       gnus-summary-post-menu gnus-summary-mode-map ""
2462       `("Post"
2463         ["Send a message (mail or news)" gnus-summary-post-news
2464          ,@(if (featurep 'xemacs) '(t)
2465              '(:help "Compose a new message (mail or news)"))]
2466         ["Followup" gnus-summary-followup
2467          ,@(if (featurep 'xemacs) '(t)
2468              '(:help "Post followup to this article"))]
2469         ["Followup and yank" gnus-summary-followup-with-original
2470          ,@(if (featurep 'xemacs) '(t)
2471              '(:help "Post followup to this article, quoting its contents"))]
2472         ["Supersede article" gnus-summary-supersede-article t]
2473         ["Cancel article" gnus-summary-cancel-article
2474          ,@(if (featurep 'xemacs) '(t)
2475              '(:help "Cancel an article you posted"))]
2476         ["Reply" gnus-summary-reply t]
2477         ["Reply and yank" gnus-summary-reply-with-original t]
2478         ["Wide reply" gnus-summary-wide-reply t]
2479         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2480          ,@(if (featurep 'xemacs) '(t)
2481              '(:help "Mail a reply, quoting this article"))]
2482         ["Very wide reply" gnus-summary-very-wide-reply t]
2483         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2484          ,@(if (featurep 'xemacs) '(t)
2485              '(:help "Mail a very wide reply, quoting this article"))]
2486         ["Mail forward" gnus-summary-mail-forward t]
2487         ["Post forward" gnus-summary-post-forward t]
2488         ["Digest and mail" gnus-uu-digest-mail-forward t]
2489         ["Digest and post" gnus-uu-digest-post-forward t]
2490         ["Resend message" gnus-summary-resend-message t]
2491         ["Resend message edit" gnus-summary-resend-message-edit t]
2492         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2493         ["Send a mail" gnus-summary-mail-other-window t]
2494         ["Create a local message" gnus-summary-news-other-window t]
2495         ["Uuencode and post" gnus-uu-post-news
2496          ,@(if (featurep 'xemacs) '(t)
2497              '(:help "Post a uuencoded article"))]
2498         ["Followup via news" gnus-summary-followup-to-mail t]
2499         ["Followup via news and yank"
2500          gnus-summary-followup-to-mail-with-original t]
2501         ;;("Draft"
2502         ;;["Send" gnus-summary-send-draft t]
2503         ;;["Send bounced" gnus-resend-bounced-mail t])
2504         ))
2505
2506     (cond
2507      ((not (keymapp gnus-summary-post-menu))
2508       (setq gnus-article-post-menu gnus-summary-post-menu))
2509      ((not gnus-article-post-menu)
2510       ;; Don't share post menu.
2511       (setq gnus-article-post-menu
2512             (copy-keymap gnus-summary-post-menu))))
2513     (define-key gnus-article-mode-map [menu-bar post]
2514       (cons "Post" gnus-article-post-menu))
2515
2516     (easy-menu-define
2517       gnus-summary-misc-menu gnus-summary-mode-map ""
2518       `("Gnus"
2519         ("Mark Read"
2520          ["Mark as read" gnus-summary-mark-as-read-forward t]
2521          ["Mark same subject and select"
2522           gnus-summary-kill-same-subject-and-select t]
2523          ["Mark same subject" gnus-summary-kill-same-subject t]
2524          ["Catchup" gnus-summary-catchup
2525           ,@(if (featurep 'xemacs) '(t)
2526               '(:help "Mark unread articles in this group as read"))]
2527          ["Catchup all" gnus-summary-catchup-all t]
2528          ["Catchup to here" gnus-summary-catchup-to-here t]
2529          ["Catchup from here" gnus-summary-catchup-from-here t]
2530          ["Catchup region" gnus-summary-mark-region-as-read
2531           (gnus-mark-active-p)]
2532          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2533         ("Mark Various"
2534          ["Tick" gnus-summary-tick-article-forward t]
2535          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2536          ["Remove marks" gnus-summary-clear-mark-forward t]
2537          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2538          ["Set bookmark" gnus-summary-set-bookmark t]
2539          ["Remove bookmark" gnus-summary-remove-bookmark t])
2540         ("Limit to"
2541          ["Marks..." gnus-summary-limit-to-marks t]
2542          ["Subject..." gnus-summary-limit-to-subject t]
2543          ["Author..." gnus-summary-limit-to-author t]
2544          ["Recipient..." gnus-summary-limit-to-recipient t]
2545          ["Age..." gnus-summary-limit-to-age t]
2546          ["Extra..." gnus-summary-limit-to-extra t]
2547          ["Score..." gnus-summary-limit-to-score t]
2548          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2549          ["Unread" gnus-summary-limit-to-unread t]
2550          ["Unseen" gnus-summary-limit-to-unseen t]
2551          ["Singletons" gnus-summary-limit-to-singletons t]
2552          ["Replied" gnus-summary-limit-to-replied t]
2553          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2554          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2555          ["Pop limit" gnus-summary-pop-limit t]
2556          ["Show dormant" gnus-summary-limit-include-dormant t]
2557          ["Hide childless dormant"
2558           gnus-summary-limit-exclude-childless-dormant t]
2559          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2560          ["Hide marked" gnus-summary-limit-exclude-marks t]
2561          ["Show expunged" gnus-summary-limit-include-expunged t])
2562         ("Process Mark"
2563          ["Set mark" gnus-summary-mark-as-processable t]
2564          ["Remove mark" gnus-summary-unmark-as-processable t]
2565          ["Remove all marks" gnus-summary-unmark-all-processable t]
2566          ["Invert marks" gnus-uu-invert-processable t]
2567          ["Mark above" gnus-uu-mark-over t]
2568          ["Mark series" gnus-uu-mark-series t]
2569          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2570          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2571          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2572          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2573          ["Mark all" gnus-uu-mark-all t]
2574          ["Mark buffer" gnus-uu-mark-buffer t]
2575          ["Mark sparse" gnus-uu-mark-sparse t]
2576          ["Mark thread" gnus-uu-mark-thread t]
2577          ["Unmark thread" gnus-uu-unmark-thread t]
2578          ("Process Mark Sets"
2579           ["Kill" gnus-summary-kill-process-mark t]
2580           ["Yank" gnus-summary-yank-process-mark
2581            gnus-newsgroup-process-stack]
2582           ["Save" gnus-summary-save-process-mark t]
2583           ["Run command on marked..." gnus-summary-universal-argument t]))
2584         ("Scroll article"
2585          ["Page forward" gnus-summary-next-page
2586           ,@(if (featurep 'xemacs) '(t)
2587               '(:help "Show next page of article"))]
2588          ["Page backward" gnus-summary-prev-page
2589           ,@(if (featurep 'xemacs) '(t)
2590               '(:help "Show previous page of article"))]
2591          ["Line forward" gnus-summary-scroll-up t])
2592         ("Move"
2593          ["Next unread article" gnus-summary-next-unread-article t]
2594          ["Previous unread article" gnus-summary-prev-unread-article t]
2595          ["Next article" gnus-summary-next-article t]
2596          ["Previous article" gnus-summary-prev-article t]
2597          ["Next unread subject" gnus-summary-next-unread-subject t]
2598          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2599          ["Next article same subject" gnus-summary-next-same-subject t]
2600          ["Previous article same subject" gnus-summary-prev-same-subject t]
2601          ["First unread article" gnus-summary-first-unread-article t]
2602          ["Best unread article" gnus-summary-best-unread-article t]
2603          ["Go to subject number..." gnus-summary-goto-subject t]
2604          ["Go to article number..." gnus-summary-goto-article t]
2605          ["Go to the last article" gnus-summary-goto-last-article t]
2606          ["Pop article off history" gnus-summary-pop-article t])
2607         ("Sort"
2608          ["Sort by number" gnus-summary-sort-by-number t]
2609          ["Sort by author" gnus-summary-sort-by-author t]
2610          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2611          ["Sort by subject" gnus-summary-sort-by-subject t]
2612          ["Sort by date" gnus-summary-sort-by-date t]
2613          ["Sort by score" gnus-summary-sort-by-score t]
2614          ["Sort by lines" gnus-summary-sort-by-lines t]
2615          ["Sort by characters" gnus-summary-sort-by-chars t]
2616          ["Randomize" gnus-summary-sort-by-random t]
2617          ["Original sort" gnus-summary-sort-by-original t])
2618         ("Help"
2619          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2620          ["Describe group" gnus-summary-describe-group t]
2621          ["Fetch charter" gnus-group-fetch-charter
2622           ,@(if (featurep 'xemacs) nil
2623               '(:help "Display the charter of the current group"))]
2624          ["Fetch control message" gnus-group-fetch-control
2625           ,@(if (featurep 'xemacs) nil
2626               '(:help "Display the archived control message for the current group"))]
2627          ["Read manual" gnus-info-find-node t])
2628         ("Modes"
2629          ["Pick and read" gnus-pick-mode t]
2630          ["Binary" gnus-binary-mode t])
2631         ("Regeneration"
2632          ["Regenerate" gnus-summary-prepare t]
2633          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2634          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2635          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2636          ["Toggle threading" gnus-summary-toggle-threads t])
2637         ["See old articles" gnus-summary-insert-old-articles t]
2638         ["See new articles" gnus-summary-insert-new-articles t]
2639         ["Filter articles..." gnus-summary-execute-command t]
2640         ["Run command on articles..." gnus-summary-universal-argument t]
2641         ["Search articles forward..." gnus-summary-search-article-forward t]
2642         ["Search articles backward..." gnus-summary-search-article-backward t]
2643         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2644         ["Expand window" gnus-summary-expand-window t]
2645         ["Expire expirable articles" gnus-summary-expire-articles
2646          (gnus-check-backend-function
2647           'request-expire-articles gnus-newsgroup-name)]
2648         ["Edit local kill file" gnus-summary-edit-local-kill t]
2649         ["Edit main kill file" gnus-summary-edit-global-kill t]
2650         ["Edit group parameters" gnus-summary-edit-parameters t]
2651         ["Customize group parameters" gnus-summary-customize-parameters t]
2652         ["Send a bug report" gnus-bug t]
2653         ("Exit"
2654          ["Catchup and exit" gnus-summary-catchup-and-exit
2655           ,@(if (featurep 'xemacs) '(t)
2656               '(:help "Mark unread articles in this group as read, then exit"))]
2657          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2658          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2659          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2660          ["Exit group" gnus-summary-exit
2661           ,@(if (featurep 'xemacs) '(t)
2662               '(:help "Exit current group, return to group selection mode"))]
2663          ["Exit group without updating" gnus-summary-exit-no-update t]
2664          ["Exit and goto next group" gnus-summary-next-group t]
2665          ["Exit and goto prev group" gnus-summary-prev-group t]
2666          ["Reselect group" gnus-summary-reselect-current-group t]
2667          ["Rescan group" gnus-summary-rescan-group t]
2668          ["Update dribble" gnus-summary-save-newsrc t])))
2669
2670     (gnus-run-hooks 'gnus-summary-menu-hook)))
2671
2672 (defvar gnus-summary-tool-bar-map nil)
2673
2674 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2675 ;; affect _new_ message buffers.  We might add a function that walks thru all
2676 ;; summary-mode buffers and force the update.
2677 (defun gnus-summary-tool-bar-update (&optional symbol value)
2678   "Update summary mode toolbar.
2679 Setter function for custom variables."
2680   (setq-default gnus-summary-tool-bar-map nil)
2681   (when symbol
2682     ;; When used as ":set" function:
2683     (set-default symbol value))
2684   (when (gnus-buffer-live-p gnus-summary-buffer)
2685     (with-current-buffer gnus-summary-buffer
2686       (gnus-summary-make-tool-bar))))
2687
2688 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2689                                      'gnus-summary-tool-bar-gnome
2690                                    'gnus-summary-tool-bar-retro)
2691   "Specifies the Gnus summary tool bar.
2692
2693 It can be either a list or a symbol refering to a list.  See
2694 `gmm-tool-bar-from-list' for the format of the list.  The
2695 default key map is `gnus-summary-mode-map'.
2696
2697 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2698 `gnus-summary-tool-bar-retro'."
2699   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2700                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2701                  (repeat :tag "User defined list" gmm-tool-bar-item)
2702                  (symbol))
2703   :version "23.0" ;; No Gnus
2704   :initialize 'custom-initialize-default
2705   :set 'gnus-summary-tool-bar-update
2706   :group 'gnus-summary)
2707
2708 (defcustom gnus-summary-tool-bar-gnome
2709   '((gnus-summary-post-news "mail/compose" nil)
2710     (gnus-summary-insert-new-articles "mail/inbox" nil
2711                                       :visible (or (not gnus-agent)
2712                                                    gnus-plugged))
2713     (gnus-summary-reply-with-original "mail/reply")
2714     (gnus-summary-reply "mail/reply" nil :visible nil)
2715     (gnus-summary-followup-with-original "mail/reply-all")
2716     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2717     (gnus-summary-mail-forward "mail/forward")
2718     (gnus-summary-save-article "mail/save")
2719     (gnus-summary-search-article-forward "search" nil :visible nil)
2720     (gnus-summary-print-article "print")
2721     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2722     ;; Some new commands that may need more suitable icons:
2723     (gnus-summary-save-newsrc "save" nil :visible nil)
2724     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2725     (gnus-summary-prev-article "left-arrow")
2726     (gnus-summary-next-article "right-arrow")
2727     (gnus-summary-next-page "next-page")
2728     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2729     ;;
2730     ;; Maybe some sort-by-... could be added:
2731     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2732     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2733     (gnus-summary-mark-as-expirable
2734      "delete" nil
2735      :visible (gnus-check-backend-function 'request-expire-articles
2736                                            gnus-newsgroup-name))
2737     (gnus-summary-mark-as-spam
2738      "mail/spam" t
2739      :visible (and (fboundp 'spam-group-ham-contents-p)
2740                    (spam-group-ham-contents-p gnus-newsgroup-name))
2741      :help "Mark as spam")
2742     (gnus-summary-mark-as-read-forward
2743      "mail/not-spam" nil
2744      :visible (and (fboundp 'spam-group-spam-contents-p)
2745                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2746     ;;
2747     (gnus-summary-exit "exit")
2748     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2749     (gnus-info-find-node "help"))
2750   "List of functions for the summary tool bar (GNOME style).
2751
2752 See `gmm-tool-bar-from-list' for the format of the list."
2753   :type '(repeat gmm-tool-bar-item)
2754   :version "23.0" ;; No Gnus
2755   :initialize 'custom-initialize-default
2756   :set 'gnus-summary-tool-bar-update
2757   :group 'gnus-summary)
2758
2759 (defcustom gnus-summary-tool-bar-retro
2760   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2761     (gnus-summary-next-unread-article "gnus/next-ur")
2762     (gnus-summary-post-news "gnus/post")
2763     (gnus-summary-followup-with-original "gnus/fuwo")
2764     (gnus-summary-followup "gnus/followup")
2765     (gnus-summary-reply-with-original "gnus/reply-wo")
2766     (gnus-summary-reply "gnus/reply")
2767     (gnus-summary-caesar-message "gnus/rot13")
2768     (gnus-uu-decode-uu "gnus/uu-decode")
2769     (gnus-summary-save-article-file "gnus/save-aif")
2770     (gnus-summary-save-article "gnus/save-art")
2771     (gnus-uu-post-news "gnus/uu-post")
2772     (gnus-summary-catchup "gnus/catchup")
2773     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2774     (gnus-summary-exit "gnus/exit-summ")
2775     ;; Some new command that may need more suitable icons:
2776     (gnus-summary-print-article "gnus/print" nil :visible nil)
2777     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2778     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2779     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2780     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2781     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2782     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2783     ;;
2784     (gnus-info-find-node "gnus/help" nil :visible nil))
2785   "List of functions for the summary tool bar (retro look).
2786
2787 See `gmm-tool-bar-from-list' for the format of the list."
2788   :type '(repeat gmm-tool-bar-item)
2789   :version "23.0" ;; No Gnus
2790   :initialize 'custom-initialize-default
2791   :set 'gnus-summary-tool-bar-update
2792   :group 'gnus-summary)
2793
2794 (defcustom gnus-summary-tool-bar-zap-list t
2795   "List of icon items from the global tool bar.
2796 These items are not displayed in the Gnus summary mode tool bar.
2797
2798 See `gmm-tool-bar-from-list' for the format of the list."
2799   :type 'gmm-tool-bar-zap-list
2800   :version "23.0" ;; No Gnus
2801   :initialize 'custom-initialize-default
2802   :set 'gnus-summary-tool-bar-update
2803   :group 'gnus-summary)
2804
2805 (defvar image-load-path)
2806
2807 (defun gnus-summary-make-tool-bar (&optional force)
2808   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2809 When FORCE, rebuild the tool bar."
2810   (when (and (not (featurep 'xemacs))
2811              (boundp 'tool-bar-mode)
2812              tool-bar-mode
2813              (or (not gnus-summary-tool-bar-map) force))
2814     (let* ((load-path
2815             (gmm-image-load-path-for-library "gnus"
2816                                              "mail/save.xpm"
2817                                              nil t))
2818            (image-load-path (cons (car load-path)
2819                                   (when (boundp 'image-load-path)
2820                                     image-load-path)))
2821            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2822                                         gnus-summary-tool-bar-zap-list
2823                                         'gnus-summary-mode-map)))
2824       (when map
2825         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2826         ;; uses it's value.
2827         (setq gnus-summary-tool-bar-map map))))
2828   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2829
2830 (defun gnus-score-set-default (var value)
2831   "A version of set that updates the GNU Emacs menu-bar."
2832   (set var value)
2833   ;; It is the message that forces the active status to be updated.
2834   (message ""))
2835
2836 (defun gnus-make-score-map (type)
2837   "Make a summary score map of type TYPE."
2838   (if t
2839       nil
2840     (let ((headers '(("author" "from" string)
2841                      ("subject" "subject" string)
2842                      ("article body" "body" string)
2843                      ("article head" "head" string)
2844                      ("xref" "xref" string)
2845                      ("extra header" "extra" string)
2846                      ("lines" "lines" number)
2847                      ("followups to author" "followup" string)))
2848           (types '((number ("less than" <)
2849                            ("greater than" >)
2850                            ("equal" =))
2851                    (string ("substring" s)
2852                            ("exact string" e)
2853                            ("fuzzy string" f)
2854                            ("regexp" r))))
2855           (perms '(("temporary" (current-time-string))
2856                    ("permanent" nil)
2857                    ("immediate" now)))
2858           header)
2859       (list
2860        (apply
2861         'nconc
2862         (list
2863          (if (eq type 'lower)
2864              "Lower score"
2865            "Increase score"))
2866         (let (outh)
2867           (while headers
2868             (setq header (car headers))
2869             (setq outh
2870                   (cons
2871                    (apply
2872                     'nconc
2873                     (list (car header))
2874                     (let ((ts (cdr (assoc (nth 2 header) types)))
2875                           outt)
2876                       (while ts
2877                         (setq outt
2878                               (cons
2879                                (apply
2880                                 'nconc
2881                                 (list (caar ts))
2882                                 (let ((ps perms)
2883                                       outp)
2884                                   (while ps
2885                                     (setq outp
2886                                           (cons
2887                                            (vector
2888                                             (caar ps)
2889                                             (list
2890                                              'gnus-summary-score-entry
2891                                              (nth 1 header)
2892                                              (if (or (string= (nth 1 header)
2893                                                               "head")
2894                                                      (string= (nth 1 header)
2895                                                               "body"))
2896                                                  ""
2897                                                (list 'gnus-summary-header
2898                                                      (nth 1 header)))
2899                                              (list 'quote (nth 1 (car ts)))
2900                                              (list 'gnus-score-delta-default
2901                                                    nil)
2902                                              (nth 1 (car ps))
2903                                              t)
2904                                             t)
2905                                            outp))
2906                                     (setq ps (cdr ps)))
2907                                   (list (nreverse outp))))
2908                                outt))
2909                         (setq ts (cdr ts)))
2910                       (list (nreverse outt))))
2911                    outh))
2912             (setq headers (cdr headers)))
2913           (list (nreverse outh))))))))
2914
2915 \f
2916
2917 (defun gnus-summary-mode (&optional group)
2918   "Major mode for reading articles.
2919
2920 All normal editing commands are switched off.
2921 \\<gnus-summary-mode-map>
2922 Each line in this buffer represents one article.  To read an
2923 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2924 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2925 respectively.
2926
2927 You can also post articles and send mail from this buffer.  To
2928 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2929 of an article, type `\\[gnus-summary-reply]'.
2930
2931 There are approx. one gazillion commands you can execute in this
2932 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2933
2934 The following commands are available:
2935
2936 \\{gnus-summary-mode-map}"
2937   (interactive)
2938   (kill-all-local-variables)
2939   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2940     (gnus-summary-make-local-variables))
2941   (gnus-summary-make-local-variables)
2942   (setq gnus-newsgroup-name group)
2943   (when (gnus-visual-p 'summary-menu 'menu)
2944     (gnus-summary-make-menu-bar)
2945     (gnus-summary-make-tool-bar))
2946   (gnus-make-thread-indent-array)
2947   (gnus-simplify-mode-line)
2948   (setq major-mode 'gnus-summary-mode)
2949   (setq mode-name "Summary")
2950   (make-local-variable 'minor-mode-alist)
2951   (use-local-map gnus-summary-mode-map)
2952   (buffer-disable-undo)
2953   (setq buffer-read-only t              ;Disable modification
2954         show-trailing-whitespace nil)
2955   (setq truncate-lines t)
2956   (setq selective-display t)
2957   (setq selective-display-ellipses t)   ;Display `...'
2958   (gnus-summary-set-display-table)
2959   (gnus-set-default-directory)
2960   (make-local-variable 'gnus-summary-line-format)
2961   (make-local-variable 'gnus-summary-line-format-spec)
2962   (make-local-variable 'gnus-summary-dummy-line-format)
2963   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2964   (make-local-variable 'gnus-summary-mark-positions)
2965   (gnus-make-local-hook 'pre-command-hook)
2966   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2967   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2968   (turn-on-gnus-mailing-list-mode)
2969   (mm-enable-multibyte)
2970   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2971   (gnus-update-summary-mark-positions))
2972
2973 (defun gnus-summary-make-local-variables ()
2974   "Make all the local summary buffer variables."
2975   (let (global)
2976     (dolist (local gnus-summary-local-variables)
2977       (if (consp local)
2978           (progn
2979             (if (eq (cdr local) 'global)
2980                 ;; Copy the global value of the variable.
2981                 (setq global (symbol-value (car local)))
2982               ;; Use the value from the list.
2983               (setq global (eval (cdr local))))
2984             (set (make-local-variable (car local)) global))
2985         ;; Simple nil-valued local variable.
2986         (set (make-local-variable local) nil)))))
2987
2988 (defun gnus-summary-clear-local-variables ()
2989   (let ((locals gnus-summary-local-variables))
2990     (while locals
2991       (if (consp (car locals))
2992           (and (symbolp (caar locals))
2993                (set (caar locals) nil))
2994         (and (symbolp (car locals))
2995              (set (car locals) nil)))
2996       (setq locals (cdr locals)))))
2997
2998 ;; Summary data functions.
2999
3000 (defmacro gnus-data-number (data)
3001   `(car ,data))
3002
3003 (defmacro gnus-data-set-number (data number)
3004   `(setcar ,data ,number))
3005
3006 (defmacro gnus-data-mark (data)
3007   `(nth 1 ,data))
3008
3009 (defmacro gnus-data-set-mark (data mark)
3010   `(setcar (nthcdr 1 ,data) ,mark))
3011
3012 (defmacro gnus-data-pos (data)
3013   `(nth 2 ,data))
3014
3015 (defmacro gnus-data-set-pos (data pos)
3016   `(setcar (nthcdr 2 ,data) ,pos))
3017
3018 (defmacro gnus-data-header (data)
3019   `(nth 3 ,data))
3020
3021 (defmacro gnus-data-set-header (data header)
3022   `(setf (nth 3 ,data) ,header))
3023
3024 (defmacro gnus-data-level (data)
3025   `(nth 4 ,data))
3026
3027 (defmacro gnus-data-unread-p (data)
3028   `(= (nth 1 ,data) gnus-unread-mark))
3029
3030 (defmacro gnus-data-read-p (data)
3031   `(/= (nth 1 ,data) gnus-unread-mark))
3032
3033 (defmacro gnus-data-pseudo-p (data)
3034   `(consp (nth 3 ,data)))
3035
3036 (defmacro gnus-data-find (number)
3037   `(assq ,number gnus-newsgroup-data))
3038
3039 (defmacro gnus-data-find-list (number &optional data)
3040   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3041      (memq (assq ,number bdata)
3042            bdata)))
3043
3044 (defmacro gnus-data-make (number mark pos header level)
3045   `(list ,number ,mark ,pos ,header ,level))
3046
3047 (defun gnus-data-enter (after-article number mark pos header level offset)
3048   (let ((data (gnus-data-find-list after-article)))
3049     (unless data
3050       (error "No such article: %d" after-article))
3051     (setcdr data (cons (gnus-data-make number mark pos header level)
3052                        (cdr data)))
3053     (setq gnus-newsgroup-data-reverse nil)
3054     (gnus-data-update-list (cddr data) offset)))
3055
3056 (defun gnus-data-enter-list (after-article list &optional offset)
3057   (when list
3058     (let ((data (and after-article (gnus-data-find-list after-article)))
3059           (ilist list))
3060       (if (not (or data
3061                    after-article))
3062           (let ((odata gnus-newsgroup-data))
3063             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3064             (when offset
3065               (gnus-data-update-list odata offset)))
3066         ;; Find the last element in the list to be spliced into the main
3067         ;; list.
3068         (setq list (last list))
3069         (if (not data)
3070             (progn
3071               (setcdr list gnus-newsgroup-data)
3072               (setq gnus-newsgroup-data ilist)
3073               (when offset
3074                 (gnus-data-update-list (cdr list) offset)))
3075           (setcdr list (cdr data))
3076           (setcdr data ilist)
3077           (when offset
3078             (gnus-data-update-list (cdr list) offset))))
3079       (setq gnus-newsgroup-data-reverse nil))))
3080
3081 (defun gnus-data-remove (article &optional offset)
3082   (let ((data gnus-newsgroup-data))
3083     (if (= (gnus-data-number (car data)) article)
3084         (progn
3085           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3086                 gnus-newsgroup-data-reverse nil)
3087           (when offset
3088             (gnus-data-update-list gnus-newsgroup-data offset)))
3089       (while (cdr data)
3090         (when (= (gnus-data-number (cadr data)) article)
3091           (setcdr data (cddr data))
3092           (when offset
3093             (gnus-data-update-list (cdr data) offset))
3094           (setq data nil
3095                 gnus-newsgroup-data-reverse nil))
3096         (setq data (cdr data))))))
3097
3098 (defmacro gnus-data-list (backward)
3099   `(if ,backward
3100        (or gnus-newsgroup-data-reverse
3101            (setq gnus-newsgroup-data-reverse
3102                  (reverse gnus-newsgroup-data)))
3103      gnus-newsgroup-data))
3104
3105 (defun gnus-data-update-list (data offset)
3106   "Add OFFSET to the POS of all data entries in DATA."
3107   (setq gnus-newsgroup-data-reverse nil)
3108   (while data
3109     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3110     (setq data (cdr data))))
3111
3112 (defun gnus-summary-article-pseudo-p (article)
3113   "Say whether this article is a pseudo article or not."
3114   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3115
3116 (defmacro gnus-summary-article-sparse-p (article)
3117   "Say whether this article is a sparse article or not."
3118   `(memq ,article gnus-newsgroup-sparse))
3119
3120 (defmacro gnus-summary-article-ancient-p (article)
3121   "Say whether this article is a sparse article or not."
3122   `(memq ,article gnus-newsgroup-ancient))
3123
3124 (defun gnus-article-parent-p (number)
3125   "Say whether this article is a parent or not."
3126   (let ((data (gnus-data-find-list number)))
3127     (and (cdr data)              ; There has to be an article after...
3128          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3129             (gnus-data-level (nth 1 data))))))
3130
3131 (defun gnus-article-children (number)
3132   "Return a list of all children to NUMBER."
3133   (let* ((data (gnus-data-find-list number))
3134          (level (gnus-data-level (car data)))
3135          children)
3136     (setq data (cdr data))
3137     (while (and data
3138                 (= (gnus-data-level (car data)) (1+ level)))
3139       (push (gnus-data-number (car data)) children)
3140       (setq data (cdr data)))
3141     children))
3142
3143 (defmacro gnus-summary-skip-intangible ()
3144   "If the current article is intangible, then jump to a different article."
3145   '(let ((to (get-text-property (point) 'gnus-intangible)))
3146      (and to (gnus-summary-goto-subject to))))
3147
3148 (defmacro gnus-summary-article-intangible-p ()
3149   "Say whether this article is intangible or not."
3150   '(get-text-property (point) 'gnus-intangible))
3151
3152 (defun gnus-article-read-p (article)
3153   "Say whether ARTICLE is read or not."
3154   (not (or (memq article gnus-newsgroup-marked)
3155            (memq article gnus-newsgroup-spam-marked)
3156            (memq article gnus-newsgroup-unreads)
3157            (memq article gnus-newsgroup-unselected)
3158            (memq article gnus-newsgroup-dormant))))
3159
3160 ;; Some summary mode macros.
3161
3162 (defmacro gnus-summary-article-number ()
3163   "The article number of the article on the current line.
3164 If there isn't an article number here, then we return the current
3165 article number."
3166   '(progn
3167      (gnus-summary-skip-intangible)
3168      (or (get-text-property (point) 'gnus-number)
3169          (gnus-summary-last-subject))))
3170
3171 (defmacro gnus-summary-article-header (&optional number)
3172   "Return the header of article NUMBER."
3173   `(gnus-data-header (gnus-data-find
3174                       ,(or number '(gnus-summary-article-number)))))
3175
3176 (defmacro gnus-summary-thread-level (&optional number)
3177   "Return the level of thread that starts with article NUMBER."
3178   `(if (and (eq gnus-summary-make-false-root 'dummy)
3179             (get-text-property (point) 'gnus-intangible))
3180        0
3181      (gnus-data-level (gnus-data-find
3182                        ,(or number '(gnus-summary-article-number))))))
3183
3184 (defmacro gnus-summary-article-mark (&optional number)
3185   "Return the mark of article NUMBER."
3186   `(gnus-data-mark (gnus-data-find
3187                     ,(or number '(gnus-summary-article-number)))))
3188
3189 (defmacro gnus-summary-article-pos (&optional number)
3190   "Return the position of the line of article NUMBER."
3191   `(gnus-data-pos (gnus-data-find
3192                    ,(or number '(gnus-summary-article-number)))))
3193
3194 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3195 (defmacro gnus-summary-article-subject (&optional number)
3196   "Return current subject string or nil if nothing."
3197   `(let ((headers
3198           ,(if number
3199                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3200              '(gnus-data-header (assq (gnus-summary-article-number)
3201                                       gnus-newsgroup-data)))))
3202      (and headers
3203           (vectorp headers)
3204           (mail-header-subject headers))))
3205
3206 (defmacro gnus-summary-article-score (&optional number)
3207   "Return current article score."
3208   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3209                   gnus-newsgroup-scored))
3210        gnus-summary-default-score 0))
3211
3212 (defun gnus-summary-article-children (&optional number)
3213   "Return a list of article numbers that are children of article NUMBER."
3214   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3215          (level (gnus-data-level (car data)))
3216          l children)
3217     (while (and (setq data (cdr data))
3218                 (> (setq l (gnus-data-level (car data))) level))
3219       (and (= (1+ level) l)
3220            (push (gnus-data-number (car data))
3221                  children)))
3222     (nreverse children)))
3223
3224 (defun gnus-summary-article-parent (&optional number)
3225   "Return the article number of the parent of article NUMBER."
3226   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3227                                     (gnus-data-list t)))
3228          (level (gnus-data-level (car data))))
3229     (if (zerop level)
3230         ()                              ; This is a root.
3231       ;; We search until we find an article with a level less than
3232       ;; this one.  That function has to be the parent.
3233       (while (and (setq data (cdr data))
3234                   (not (< (gnus-data-level (car data)) level))))
3235       (and data (gnus-data-number (car data))))))
3236
3237 (defun gnus-unread-mark-p (mark)
3238   "Say whether MARK is the unread mark."
3239   (= mark gnus-unread-mark))
3240
3241 (defun gnus-read-mark-p (mark)
3242   "Say whether MARK is one of the marks that mark as read.
3243 This is all marks except unread, ticked, dormant, and expirable."
3244   (not (or (= mark gnus-unread-mark)
3245            (= mark gnus-ticked-mark)
3246            (= mark gnus-spam-mark)
3247            (= mark gnus-dormant-mark)
3248            (= mark gnus-expirable-mark))))
3249
3250 (defmacro gnus-article-mark (number)
3251   "Return the MARK of article NUMBER.
3252 This macro should only be used when computing the mark the \"first\"
3253 time; i.e., when generating the summary lines.  After that,
3254 `gnus-summary-article-mark' should be used to examine the
3255 marks of articles."
3256   `(cond
3257     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3258     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3259     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3260     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3261     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3262     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3263     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3264     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3265            gnus-ancient-mark))))
3266
3267 ;; Saving hidden threads.
3268
3269 (defmacro gnus-save-hidden-threads (&rest forms)
3270   "Save hidden threads, eval FORMS, and restore the hidden threads."
3271   (let ((config (make-symbol "config")))
3272     `(let ((,config (gnus-hidden-threads-configuration)))
3273        (unwind-protect
3274            (save-excursion
3275              ,@forms)
3276          (gnus-restore-hidden-threads-configuration ,config)))))
3277 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3278 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3279
3280 (defun gnus-data-compute-positions ()
3281   "Compute the positions of all articles."
3282   (setq gnus-newsgroup-data-reverse nil)
3283   (let ((data gnus-newsgroup-data))
3284     (save-excursion
3285       (gnus-save-hidden-threads
3286         (gnus-summary-show-all-threads)
3287         (goto-char (point-min))
3288         (while data
3289           (while (get-text-property (point) 'gnus-intangible)
3290             (forward-line 1))
3291           (gnus-data-set-pos (car data) (+ (point) 3))
3292           (setq data (cdr data))
3293           (forward-line 1))))))
3294
3295 (defun gnus-hidden-threads-configuration ()
3296   "Return the current hidden threads configuration."
3297   (save-excursion
3298     (let (config)
3299       (goto-char (point-min))
3300       (while (search-forward "\r" nil t)
3301         (push (1- (point)) config))
3302       config)))
3303
3304 (defun gnus-restore-hidden-threads-configuration (config)
3305   "Restore hidden threads configuration from CONFIG."
3306   (save-excursion
3307     (let (point buffer-read-only)
3308       (while (setq point (pop config))
3309         (when (and (< point (point-max))
3310                    (goto-char point)
3311                    (eq (char-after) ?\n))
3312           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3313
3314 ;; Various summary mode internalish functions.
3315
3316 (defun gnus-mouse-pick-article (e)
3317   (interactive "e")
3318   (mouse-set-point e)
3319   (gnus-summary-next-page nil t))
3320
3321 (defun gnus-summary-set-display-table ()
3322   "Change the display table.
3323 Odd characters have a tendency to mess
3324 up nicely formatted displays - we make all possible glyphs
3325 display only a single character."
3326
3327   ;; We start from the standard display table, if any.
3328   (let ((table (or (copy-sequence standard-display-table)
3329                    (make-display-table)))
3330         (i 32))
3331     ;; Nix out all the control chars...
3332     (while (>= (setq i (1- i)) 0)
3333       (aset table i [??]))
3334    ;; ... but not newline and cr, of course.  (cr is necessary for the
3335     ;; selective display).
3336     (aset table ?\n nil)
3337     (aset table ?\r nil)
3338     ;; We keep TAB as well.
3339     (aset table ?\t nil)
3340     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3341     ;; Emacs 23 (unicode), that are not set already.
3342     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3343                  160
3344                256)))
3345       (while (>= (setq i (1- i)) 127)
3346         ;; Only modify if the entry is nil.
3347         (unless (aref table i)
3348           (aset table i [??]))))
3349     (setq buffer-display-table table)))
3350
3351 (defun gnus-summary-set-article-display-arrow (pos)
3352   "Update the overlay arrow to point to line at position POS."
3353   (when (and gnus-summary-display-arrow
3354              (boundp 'overlay-arrow-position)
3355              (boundp 'overlay-arrow-string))
3356     (save-excursion
3357       (goto-char pos)
3358       (beginning-of-line)
3359       (unless overlay-arrow-position
3360         (setq overlay-arrow-position (make-marker)))
3361       (setq overlay-arrow-string "=>"
3362             overlay-arrow-position (set-marker overlay-arrow-position
3363                                                (point)
3364                                                (current-buffer))))))
3365
3366 (defun gnus-summary-setup-buffer (group)
3367   "Initialize summary buffer."
3368   (let ((buffer (gnus-summary-buffer-name group))
3369         (dead-name (concat "*Dead Summary "
3370                            (gnus-group-decoded-name group) "*")))
3371     ;; If a dead summary buffer exists, we kill it.
3372     (when (gnus-buffer-live-p dead-name)
3373       (gnus-kill-buffer dead-name))
3374     (if (get-buffer buffer)
3375         (progn
3376           (set-buffer buffer)
3377           (setq gnus-summary-buffer (current-buffer))
3378           (not gnus-newsgroup-prepared))
3379       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3380       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3381       (gnus-summary-mode group)
3382       (when gnus-carpal
3383         (gnus-carpal-setup-buffer 'summary))
3384       (when (gnus-group-quit-config group)
3385         (set (make-local-variable 'gnus-single-article-buffer) nil))
3386       (make-local-variable 'gnus-article-buffer)
3387       (make-local-variable 'gnus-article-current)
3388       (make-local-variable 'gnus-original-article-buffer)
3389       (setq gnus-newsgroup-name group)
3390       ;; Set any local variables in the group parameters.
3391       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3392       t)))
3393
3394 (defun gnus-set-global-variables ()
3395   "Set the global equivalents of the buffer-local variables.
3396 They are set to the latest values they had.  These reflect the summary
3397 buffer that was in action when the last article was fetched."
3398   (when (eq major-mode 'gnus-summary-mode)
3399     (setq gnus-summary-buffer (current-buffer))
3400     (let ((name gnus-newsgroup-name)
3401           (marked gnus-newsgroup-marked)
3402           (spam gnus-newsgroup-spam-marked)
3403           (unread gnus-newsgroup-unreads)
3404           (headers gnus-current-headers)
3405           (data gnus-newsgroup-data)
3406           (summary gnus-summary-buffer)
3407           (article-buffer gnus-article-buffer)
3408           (original gnus-original-article-buffer)
3409           (gac gnus-article-current)
3410           (reffed gnus-reffed-article-number)
3411           (score-file gnus-current-score-file)
3412           (default-charset gnus-newsgroup-charset)
3413           vlist)
3414       (let ((locals gnus-newsgroup-variables))
3415         (while locals
3416           (if (consp (car locals))
3417               (push (eval (caar locals)) vlist)
3418             (push (eval (car locals)) vlist))
3419           (setq locals (cdr locals)))
3420         (setq vlist (nreverse vlist)))
3421       (with-current-buffer gnus-group-buffer
3422         (setq gnus-newsgroup-name name
3423               gnus-newsgroup-marked marked
3424               gnus-newsgroup-spam-marked spam
3425               gnus-newsgroup-unreads unread
3426               gnus-current-headers headers
3427               gnus-newsgroup-data data
3428               gnus-article-current gac
3429               gnus-summary-buffer summary
3430               gnus-article-buffer article-buffer
3431               gnus-original-article-buffer original
3432               gnus-reffed-article-number reffed
3433               gnus-current-score-file score-file
3434               gnus-newsgroup-charset default-charset)
3435         (let ((locals gnus-newsgroup-variables))
3436           (while locals
3437             (if (consp (car locals))
3438                 (set (caar locals) (pop vlist))
3439               (set (car locals) (pop vlist)))
3440             (setq locals (cdr locals))))
3441         ;; The article buffer also has local variables.
3442         (when (gnus-buffer-live-p gnus-article-buffer)
3443           (set-buffer gnus-article-buffer)
3444           (setq gnus-summary-buffer summary))))))
3445
3446 (defun gnus-summary-article-unread-p (article)
3447   "Say whether ARTICLE is unread or not."
3448   (memq article gnus-newsgroup-unreads))
3449
3450 (defun gnus-summary-first-article-p (&optional article)
3451   "Return whether ARTICLE is the first article in the buffer."
3452   (if (not (setq article (or article (gnus-summary-article-number))))
3453       nil
3454     (eq article (caar gnus-newsgroup-data))))
3455
3456 (defun gnus-summary-last-article-p (&optional article)
3457   "Return whether ARTICLE is the last article in the buffer."
3458   (if (not (setq article (or article (gnus-summary-article-number))))
3459       ;; All non-existent numbers are the last article.  :-)
3460       t
3461     (not (cdr (gnus-data-find-list article)))))
3462
3463 (defun gnus-make-thread-indent-array (&optional n)
3464   (when (or n
3465             (progn (setq n 200) nil)
3466             (null gnus-thread-indent-array)
3467             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3468     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3469           gnus-thread-indent-array-level gnus-thread-indent-level)
3470     (while (>= n 0)
3471       (aset gnus-thread-indent-array n
3472             (make-string (* n gnus-thread-indent-level) ? ))
3473       (setq n (1- n)))))
3474
3475 (defun gnus-update-summary-mark-positions ()
3476   "Compute where the summary marks are to go."
3477   (save-excursion
3478     (when (gnus-buffer-exists-p gnus-summary-buffer)
3479       (set-buffer gnus-summary-buffer))
3480     (let ((spec gnus-summary-line-format-spec)
3481           pos)
3482       (save-excursion
3483         (gnus-set-work-buffer)
3484         (let ((gnus-tmp-unread ?Z)
3485               (gnus-replied-mark ?Z)
3486               (gnus-score-below-mark ?Z)
3487               (gnus-score-over-mark ?Z)
3488               (gnus-undownloaded-mark ?Z)
3489               (gnus-summary-line-format-spec spec)
3490               (gnus-newsgroup-downloadable '(0))
3491               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3492               case-fold-search ignores)
3493           ;; Here, all marks are bound to Z.
3494           (gnus-summary-insert-line header
3495                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3496           (goto-char (point-min))
3497           ;; Memorize the positions of the same characters as dummy marks.
3498           (while (re-search-forward "[A-D]" nil t)
3499             (push (point) ignores))
3500           (erase-buffer)
3501           ;; We use A-D as dummy marks in order to know column positions
3502           ;; where marks should be inserted.
3503           (setq gnus-tmp-unread ?A
3504                 gnus-replied-mark ?B
3505                 gnus-score-below-mark ?C
3506                 gnus-score-over-mark ?C
3507                 gnus-undownloaded-mark ?D)
3508           (gnus-summary-insert-line header
3509                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3510           ;; Ignore characters which aren't dummy marks.
3511           (dolist (p ignores)
3512             (delete-region (goto-char (1- p)) p)
3513             (insert ?Z))
3514           (goto-char (point-min))
3515           (setq pos (list (cons 'unread
3516                                 (and (search-forward "A" nil t)
3517                                      (- (point) (point-min) 1)))))
3518           (goto-char (point-min))
3519           (push (cons 'replied (and (search-forward "B" nil t)
3520                                     (- (point) (point-min) 1)))
3521                 pos)
3522           (goto-char (point-min))
3523           (push (cons 'score (and (search-forward "C" nil t)
3524                                   (- (point) (point-min) 1)))
3525                 pos)
3526           (goto-char (point-min))
3527           (push (cons 'download (and (search-forward "D" nil t)
3528                                      (- (point) (point-min) 1)))
3529                 pos)))
3530       (setq gnus-summary-mark-positions pos))))
3531
3532 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3533   "Insert a dummy root in the summary buffer."
3534   (beginning-of-line)
3535   (gnus-add-text-properties
3536    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3537    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3538
3539 (defun gnus-summary-extract-address-component (from)
3540   (or (car (funcall gnus-extract-address-components from))
3541       from))
3542
3543 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3544   (let ((mail-parse-charset gnus-newsgroup-charset)
3545         (ignored-from-addresses (gnus-ignored-from-addresses))
3546         ; Is it really necessary to do this next part for each summary line?
3547         ; Luckily, doesn't seem to slow things down much.
3548         (mail-parse-ignored-charsets
3549          (with-current-buffer gnus-summary-buffer
3550            gnus-newsgroup-ignored-charsets)))
3551     (or
3552      (and ignored-from-addresses
3553           (string-match ignored-from-addresses gnus-tmp-from)
3554           (let ((extra-headers (mail-header-extra header))
3555                 to
3556                 newsgroups)
3557             (cond
3558              ((setq to (cdr (assq 'To extra-headers)))
3559               (concat gnus-summary-to-prefix
3560                       (inline
3561                         (gnus-summary-extract-address-component
3562                          (funcall gnus-decode-encoded-address-function to)))))
3563              ((setq newsgroups
3564                     (or
3565                      (cdr (assq 'Newsgroups extra-headers))
3566                      (and
3567                       (memq 'Newsgroups gnus-extra-headers)
3568                       (eq (car (gnus-find-method-for-group
3569                                 gnus-newsgroup-name)) 'nntp)
3570                       (gnus-group-real-name gnus-newsgroup-name))))
3571               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3572      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3573
3574 (defun gnus-summary-insert-line (gnus-tmp-header
3575                                  gnus-tmp-level gnus-tmp-current
3576                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3577                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3578                                  &optional gnus-tmp-dummy gnus-tmp-score
3579                                  gnus-tmp-process)
3580   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3581       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3582                                           gnus-tmp-level)))
3583   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3584          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3585          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3586          (gnus-tmp-score-char
3587           (if (or (null gnus-summary-default-score)
3588                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3589                       gnus-summary-zcore-fuzz))
3590               ?                         ;Whitespace
3591             (if (< gnus-tmp-score gnus-summary-default-score)
3592                 gnus-score-below-mark gnus-score-over-mark)))
3593          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3594          (gnus-tmp-replied
3595           (cond (gnus-tmp-process gnus-process-mark)
3596                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3597                  gnus-cached-mark)
3598                 (gnus-tmp-replied gnus-replied-mark)
3599                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3600                  gnus-forwarded-mark)
3601                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3602                  gnus-saved-mark)
3603                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3604                  gnus-recent-mark)
3605                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3606                  gnus-unseen-mark)
3607                 (t gnus-no-mark)))
3608          (gnus-tmp-downloaded
3609           (cond (undownloaded
3610                  gnus-undownloaded-mark)
3611                 (gnus-newsgroup-agentized
3612                  gnus-downloaded-mark)
3613                 (t
3614                  gnus-no-mark)))
3615          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3616          (gnus-tmp-name
3617           (cond
3618            ((string-match "<[^>]+> *$" gnus-tmp-from)
3619             (let ((beg (match-beginning 0)))
3620               (or (and (string-match "^\".+\"" gnus-tmp-from)
3621                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3622                   (substring gnus-tmp-from 0 beg))))
3623            ((string-match "(.+)" gnus-tmp-from)
3624             (substring gnus-tmp-from
3625                        (1+ (match-beginning 0)) (1- (match-end 0))))
3626            (t gnus-tmp-from)))
3627          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3628          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3629          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3630          (buffer-read-only nil))
3631     (when (string= gnus-tmp-name "")
3632       (setq gnus-tmp-name gnus-tmp-from))
3633     (unless (numberp gnus-tmp-lines)
3634       (setq gnus-tmp-lines -1))
3635     (if (= gnus-tmp-lines -1)
3636         (setq gnus-tmp-lines "?")
3637       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3638       (gnus-put-text-property
3639      (point)
3640      (progn (eval gnus-summary-line-format-spec) (point))
3641        'gnus-number gnus-tmp-number)
3642     (when (gnus-visual-p 'summary-highlight 'highlight)
3643       (forward-line -1)
3644       (gnus-run-hooks 'gnus-summary-update-hook)
3645       (forward-line 1))))
3646
3647 (defun gnus-summary-update-line (&optional dont-update)
3648   "Update summary line after change."
3649   (when (and gnus-summary-default-score
3650              (not gnus-summary-inhibit-highlight))
3651     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3652            (article (gnus-summary-article-number))
3653            (score (gnus-summary-article-score article)))
3654       (unless dont-update
3655         (if (and gnus-summary-mark-below
3656                  (< (gnus-summary-article-score)
3657                     gnus-summary-mark-below))
3658             ;; This article has a low score, so we mark it as read.
3659             (when (memq article gnus-newsgroup-unreads)
3660               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3661           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3662             ;; This article was previously marked as read on account
3663             ;; of a low score, but now it has risen, so we mark it as
3664             ;; unread.
3665             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3666         (gnus-summary-update-mark
3667          (if (or (null gnus-summary-default-score)
3668                  (<= (abs (- score gnus-summary-default-score))
3669                      gnus-summary-zcore-fuzz))
3670              ?                          ;Whitespace
3671            (if (< score gnus-summary-default-score)
3672                gnus-score-below-mark gnus-score-over-mark))
3673          'score))
3674       ;; Do visual highlighting.
3675       (when (gnus-visual-p 'summary-highlight 'highlight)
3676         (gnus-run-hooks 'gnus-summary-update-hook)))))
3677
3678 (defvar gnus-tmp-new-adopts nil)
3679
3680 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3681   "Return the number of articles in THREAD.
3682 This may be 0 in some cases -- if none of the articles in
3683 the thread are to be displayed."
3684   (let* ((number
3685          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3686           (cond
3687            ((not (listp thread))
3688             1)
3689            ((and (consp thread) (cdr thread))
3690             (apply
3691              '+ 1 (mapcar
3692                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3693            ((null thread)
3694             1)
3695            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3696             1)
3697            (t 0))))
3698     (when (and level (zerop level) gnus-tmp-new-adopts)
3699       (incf number
3700             (apply '+ (mapcar
3701                        'gnus-summary-number-of-articles-in-thread
3702                        gnus-tmp-new-adopts))))
3703     (if char
3704         (if (> number 1) gnus-not-empty-thread-mark
3705           gnus-empty-thread-mark)
3706       number)))
3707
3708 (defsubst gnus-summary-line-message-size (head)
3709   "Return pretty-printed version of message size.
3710 This function is intended to be used in
3711 `gnus-summary-line-format-alist'."
3712   (let ((c (or (mail-header-chars head) -1)))
3713     (cond ((< c 0) "n/a")               ; chars not available
3714           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3715           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3716           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3717           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3718
3719
3720 (defun gnus-summary-set-local-parameters (group)
3721   "Go through the local params of GROUP and set all variable specs in that list."
3722   (let ((vars '(quit-config)))          ; Ignore quit-config.
3723     (dolist (elem (gnus-group-find-parameter group))
3724       (and (consp elem)                 ; Has to be a cons.
3725            (consp (cdr elem))           ; The cdr has to be a list.
3726            (symbolp (car elem))         ; Has to be a symbol in there.
3727            (not (memq (car elem) vars))
3728            (ignore-errors               ; So we set it.
3729              (push (car elem) vars)
3730              (make-local-variable (car elem))
3731              (set (car elem) (eval (nth 1 elem))))))))
3732
3733 (defun gnus-summary-read-group (group &optional show-all no-article
3734                                       kill-buffer no-display backward
3735                                       select-articles)
3736   "Start reading news in newsgroup GROUP.
3737 If SHOW-ALL is non-nil, already read articles are also listed.
3738 If NO-ARTICLE is non-nil, no article is selected initially.
3739 If NO-DISPLAY, don't generate a summary buffer."
3740   (let (result)
3741     (while (and group
3742                 (null (setq result
3743                             (let ((gnus-auto-select-next nil))
3744                               (or (gnus-summary-read-group-1
3745                                    group show-all no-article
3746                                    kill-buffer no-display
3747                                    select-articles)
3748                                   (setq show-all nil
3749                                         select-articles nil)))))
3750                 (eq gnus-auto-select-next 'quietly))
3751       (set-buffer gnus-group-buffer)
3752       ;; The entry function called above goes to the next
3753       ;; group automatically, so we go two groups back
3754       ;; if we are searching for the previous group.
3755       (when backward
3756         (gnus-group-prev-unread-group 2))
3757       (if (not (equal group (gnus-group-group-name)))
3758           (setq group (gnus-group-group-name))
3759         (setq group nil)))
3760     result))
3761
3762 (defun gnus-summary-read-group-1 (group show-all no-article
3763                                         kill-buffer no-display
3764                                         &optional select-articles)
3765   ;; Killed foreign groups can't be entered.
3766   ;;  (when (and (not (gnus-group-native-p group))
3767   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3768   ;;    (error "Dead non-native groups can't be entered"))
3769   (gnus-message 5 "Retrieving newsgroup: %s..."
3770                 (gnus-group-decoded-name group))
3771   (let* ((new-group (gnus-summary-setup-buffer group))
3772          (quit-config (gnus-group-quit-config group))
3773          (did-select (and new-group (gnus-select-newsgroup
3774                                      group show-all select-articles))))
3775     (cond
3776      ;; This summary buffer exists already, so we just select it.
3777      ((not new-group)
3778       (gnus-set-global-variables)
3779       (when kill-buffer
3780         (gnus-kill-or-deaden-summary kill-buffer))
3781       (gnus-configure-windows 'summary 'force)
3782       (gnus-set-mode-line 'summary)
3783       (gnus-summary-position-point)
3784       (message "")
3785       t)
3786      ;; We couldn't select this group.
3787      ((null did-select)
3788       (when (and (eq major-mode 'gnus-summary-mode)
3789                  (not (equal (current-buffer) kill-buffer)))
3790         (kill-buffer (current-buffer))
3791         (if (not quit-config)
3792             (progn
3793               ;; Update the info -- marks might need to be removed,
3794               ;; for instance.
3795               (gnus-summary-update-info)
3796               (set-buffer gnus-group-buffer)
3797               (gnus-group-jump-to-group group)
3798               (gnus-group-next-unread-group 1))
3799           (gnus-handle-ephemeral-exit quit-config)))
3800       (let ((grpinfo (gnus-get-info group)))
3801         (if (null (gnus-info-read grpinfo))
3802             (gnus-message 3 "Group %s contains no messages"
3803                           (gnus-group-decoded-name group))
3804           (gnus-message 3 "Can't select group")))
3805       nil)
3806      ;; The user did a `C-g' while prompting for number of articles,
3807      ;; so we exit this group.
3808      ((eq did-select 'quit)
3809       (and (eq major-mode 'gnus-summary-mode)
3810            (not (equal (current-buffer) kill-buffer))
3811            (kill-buffer (current-buffer)))
3812       (when kill-buffer
3813         (gnus-kill-or-deaden-summary kill-buffer))
3814       (if (not quit-config)
3815           (progn
3816             (set-buffer gnus-group-buffer)
3817             (gnus-group-jump-to-group group)
3818             (gnus-group-next-unread-group 1)
3819             (gnus-configure-windows 'group 'force))
3820         (gnus-handle-ephemeral-exit quit-config))
3821       ;; Finally signal the quit.
3822       (signal 'quit nil))
3823      ;; The group was successfully selected.
3824      (t
3825       (gnus-set-global-variables)
3826       ;; Save the active value in effect when the group was entered.
3827       (setq gnus-newsgroup-active
3828             (gnus-copy-sequence
3829              (gnus-active gnus-newsgroup-name)))
3830       ;; You can change the summary buffer in some way with this hook.
3831       (gnus-run-hooks 'gnus-select-group-hook)
3832       (when (memq 'summary (gnus-update-format-specifications
3833                             nil 'summary 'summary-mode 'summary-dummy))
3834         ;; The format specification for the summary line was updated,
3835         ;; so we need to update the mark positions as well.
3836         (gnus-update-summary-mark-positions))
3837       ;; Do score processing.
3838       (when gnus-use-scoring
3839         (gnus-possibly-score-headers))
3840       ;; Check whether to fill in the gaps in the threads.
3841       (when gnus-build-sparse-threads
3842         (gnus-build-sparse-threads))
3843       ;; Find the initial limit.
3844       (if show-all
3845           (let ((gnus-newsgroup-dormant nil))
3846             (gnus-summary-initial-limit show-all))
3847         (gnus-summary-initial-limit show-all))
3848       ;; Generate the summary buffer.
3849       (unless no-display
3850         (gnus-summary-prepare))
3851       (when gnus-use-trees
3852         (gnus-tree-open group)
3853         (setq gnus-summary-highlight-line-function
3854               'gnus-tree-highlight-article))
3855       ;; If the summary buffer is empty, but there are some low-scored
3856       ;; articles or some excluded dormants, we include these in the
3857       ;; buffer.
3858       (when (and (zerop (buffer-size))
3859                  (not no-display))
3860         (cond (gnus-newsgroup-dormant
3861                (gnus-summary-limit-include-dormant))
3862               ((and gnus-newsgroup-scored show-all)
3863                (gnus-summary-limit-include-expunged t))))
3864       ;; Function `gnus-apply-kill-file' must be called in this hook.
3865       (gnus-run-hooks 'gnus-apply-kill-hook)
3866       (if (and (zerop (buffer-size))
3867                (not no-display))
3868           (progn
3869             ;; This newsgroup is empty.
3870             (gnus-summary-catchup-and-exit nil t)
3871             (gnus-message 6 "No unread news")
3872             (when kill-buffer
3873               (gnus-kill-or-deaden-summary kill-buffer))
3874             ;; Return nil from this function.
3875             nil)
3876         ;; Hide conversation thread subtrees.  We cannot do this in
3877         ;; gnus-summary-prepare-hook since kill processing may not
3878         ;; work with hidden articles.
3879         (gnus-summary-maybe-hide-threads)
3880         (when kill-buffer
3881           (gnus-kill-or-deaden-summary kill-buffer))
3882         (gnus-summary-auto-select-subject)
3883         ;; Show first unread article if requested.
3884         (if (and (not no-article)
3885                  (not no-display)
3886                  gnus-newsgroup-unreads
3887                  gnus-auto-select-first)
3888             (progn
3889               (gnus-configure-windows 'summary)
3890               (let ((art (gnus-summary-article-number)))
3891                 (unless (and (not gnus-plugged)
3892                              (or (memq art gnus-newsgroup-undownloaded)
3893                                  (memq art gnus-newsgroup-downloadable)))
3894                   (gnus-summary-goto-article art))))
3895           ;; Don't select any articles.
3896           (gnus-summary-position-point)
3897           (gnus-configure-windows 'summary 'force)
3898           (gnus-set-mode-line 'summary))
3899         (when (and gnus-auto-center-group
3900                    (get-buffer-window gnus-group-buffer t))
3901           ;; Gotta use windows, because recenter does weird stuff if
3902           ;; the current buffer ain't the displayed window.
3903           (let ((owin (selected-window)))
3904             (select-window (get-buffer-window gnus-group-buffer t))
3905             (when (gnus-group-goto-group group)
3906               (recenter))
3907             (select-window owin)))
3908         ;; Mark this buffer as "prepared".
3909         (setq gnus-newsgroup-prepared t)
3910         (gnus-run-hooks 'gnus-summary-prepared-hook)
3911         (unless (gnus-ephemeral-group-p group)
3912           (gnus-group-update-group group))
3913         t)))))
3914
3915 (defun gnus-summary-auto-select-subject ()
3916   "Select the subject line on initial group entry."
3917   (goto-char (point-min))
3918   (cond
3919    ((eq gnus-auto-select-subject 'best)
3920     (gnus-summary-best-unread-subject))
3921    ((eq gnus-auto-select-subject 'unread)
3922     (gnus-summary-first-unread-subject))
3923    ((eq gnus-auto-select-subject 'unseen)
3924     (gnus-summary-first-unseen-subject))
3925    ((eq gnus-auto-select-subject 'unseen-or-unread)
3926     (gnus-summary-first-unseen-or-unread-subject))
3927    ((eq gnus-auto-select-subject 'first)
3928     ;; Do nothing.
3929     )
3930    ((functionp gnus-auto-select-subject)
3931     (funcall gnus-auto-select-subject))))
3932
3933 (defun gnus-summary-prepare ()
3934   "Generate the summary buffer."
3935   (interactive)
3936   (let ((buffer-read-only nil))
3937     (erase-buffer)
3938     (setq gnus-newsgroup-data nil
3939           gnus-newsgroup-data-reverse nil)
3940     (gnus-run-hooks 'gnus-summary-generate-hook)
3941     ;; Generate the buffer, either with threads or without.
3942     (when gnus-newsgroup-headers
3943       (gnus-summary-prepare-threads
3944        (if gnus-show-threads
3945            (gnus-sort-gathered-threads
3946             (funcall gnus-summary-thread-gathering-function
3947                      (gnus-sort-threads
3948                       (gnus-cut-threads (gnus-make-threads)))))
3949          ;; Unthreaded display.
3950          (gnus-sort-articles gnus-newsgroup-headers))))
3951     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3952     ;; Call hooks for modifying summary buffer.
3953     (goto-char (point-min))
3954     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3955
3956 (defsubst gnus-general-simplify-subject (subject)
3957   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3958   (setq subject
3959         (cond
3960          ;; Truncate the subject.
3961          (gnus-simplify-subject-functions
3962           (gnus-map-function gnus-simplify-subject-functions subject))
3963          ((numberp gnus-summary-gather-subject-limit)
3964           (setq subject (gnus-simplify-subject-re subject))
3965           (if (> (length subject) gnus-summary-gather-subject-limit)
3966               (substring subject 0 gnus-summary-gather-subject-limit)
3967             subject))
3968          ;; Fuzzily simplify it.
3969          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3970           (gnus-simplify-subject-fuzzy subject))
3971          ;; Just remove the leading "Re:".
3972          (t
3973           (gnus-simplify-subject-re subject))))
3974
3975   (if (and gnus-summary-gather-exclude-subject
3976            (string-match gnus-summary-gather-exclude-subject subject))
3977       nil                         ; This article shouldn't be gathered
3978     subject))
3979
3980 (defun gnus-summary-simplify-subject-query ()
3981   "Query where the respool algorithm would put this article."
3982   (interactive)
3983   (gnus-summary-select-article)
3984   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3985
3986 (defun gnus-gather-threads-by-subject (threads)
3987   "Gather threads by looking at Subject headers."
3988   (if (not gnus-summary-make-false-root)
3989       threads
3990     (let ((hashtb (gnus-make-hashtable 1024))
3991           (prev threads)
3992           (result threads)
3993           subject hthread whole-subject)
3994       (while threads
3995         (setq subject (gnus-general-simplify-subject
3996                        (setq whole-subject (mail-header-subject
3997                                             (caar threads)))))
3998         (when subject
3999           (if (setq hthread (gnus-gethash subject hashtb))
4000               (progn
4001                 ;; We enter a dummy root into the thread, if we
4002                 ;; haven't done that already.
4003                 (unless (stringp (caar hthread))
4004                   (setcar hthread (list whole-subject (car hthread))))
4005                 ;; We add this new gathered thread to this gathered
4006                 ;; thread.
4007                 (setcdr (car hthread)
4008                         (nconc (cdar hthread) (list (car threads))))
4009                 ;; Remove it from the list of threads.
4010                 (setcdr prev (cdr threads))
4011                 (setq threads prev))
4012             ;; Enter this thread into the hash table.
4013             (gnus-sethash subject
4014                           (if gnus-summary-make-false-root-always
4015                               (progn
4016                                 ;; If you want a dummy root above all
4017                                 ;; threads...
4018                                 (setcar threads (list whole-subject
4019                                                       (car threads)))
4020                                 threads)
4021                             threads)
4022                           hashtb)))
4023         (setq prev threads)
4024         (setq threads (cdr threads)))
4025       result)))
4026
4027 (defun gnus-gather-threads-by-references (threads)
4028   "Gather threads by looking at References headers."
4029   (let ((idhashtb (gnus-make-hashtable 1024))
4030         (thhashtb (gnus-make-hashtable 1024))
4031         (prev threads)
4032         (result threads)
4033         ids references id gthread gid entered ref)
4034     (while threads
4035       (when (setq references (mail-header-references (caar threads)))
4036         (setq id (mail-header-id (caar threads))
4037               ids (inline (gnus-split-references references))
4038               entered nil)
4039         (while (setq ref (pop ids))
4040           (setq ids (delete ref ids))
4041           (if (not (setq gid (gnus-gethash ref idhashtb)))
4042               (progn
4043                 (gnus-sethash ref id idhashtb)
4044                 (gnus-sethash id threads thhashtb))
4045             (setq gthread (gnus-gethash gid thhashtb))
4046             (unless entered
4047               ;; We enter a dummy root into the thread, if we
4048               ;; haven't done that already.
4049               (unless (stringp (caar gthread))
4050                 (setcar gthread (list (mail-header-subject (caar gthread))
4051                                       (car gthread))))
4052               ;; We add this new gathered thread to this gathered
4053               ;; thread.
4054               (setcdr (car gthread)
4055                       (nconc (cdar gthread) (list (car threads)))))
4056             ;; Add it into the thread hash table.
4057             (gnus-sethash id gthread thhashtb)
4058             (setq entered t)
4059             ;; Remove it from the list of threads.
4060             (setcdr prev (cdr threads))
4061             (setq threads prev))))
4062       (setq prev threads)
4063       (setq threads (cdr threads)))
4064     result))
4065
4066 (defun gnus-sort-gathered-threads (threads)
4067   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4068   (let ((result threads))
4069     (while threads
4070       (when (stringp (caar threads))
4071         (setcdr (car threads)
4072                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4073       (setq threads (cdr threads)))
4074     result))
4075
4076 (defun gnus-thread-loop-p (root thread)
4077   "Say whether ROOT is in THREAD."
4078   (let ((stack (list thread))
4079         (infloop 0)
4080         th)
4081     (while (setq thread (pop stack))
4082       (setq th (cdr thread))
4083       (while (and th
4084                   (not (eq (caar th) root)))
4085         (pop th))
4086       (if th
4087           ;; We have found a loop.
4088           (let (ref-dep)
4089             (setcdr thread (delq (car th) (cdr thread)))
4090             (if (boundp (setq ref-dep (intern "none"
4091                                               gnus-newsgroup-dependencies)))
4092                 (setcdr (symbol-value ref-dep)
4093                         (nconc (cdr (symbol-value ref-dep))
4094                                (list (car th))))
4095               (set ref-dep (list nil (car th))))
4096             (setq infloop 1
4097                   stack nil))
4098         ;; Push all the subthreads onto the stack.
4099         (push (cdr thread) stack)))
4100     infloop))
4101
4102 (defun gnus-make-threads ()
4103   "Go through the dependency hashtb and find the roots.  Return all threads."
4104   (let (threads)
4105     (while (catch 'infloop
4106              (mapatoms
4107               (lambda (refs)
4108                 ;; Deal with self-referencing References loops.
4109                 (when (and (car (symbol-value refs))
4110                            (not (zerop
4111                                  (apply
4112                                   '+
4113                                   (mapcar
4114                                    (lambda (thread)
4115                                      (gnus-thread-loop-p
4116                                       (car (symbol-value refs)) thread))
4117                                    (cdr (symbol-value refs)))))))
4118                   (setq threads nil)
4119                   (throw 'infloop t))
4120                 (unless (car (symbol-value refs))
4121                   ;; These threads do not refer back to any other
4122                   ;; articles, so they're roots.
4123                   (setq threads (append (cdr (symbol-value refs)) threads))))
4124               gnus-newsgroup-dependencies)))
4125     threads))
4126
4127 ;; Build the thread tree.
4128 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4129   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4130
4131 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4132 if it was already present.
4133
4134 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4135 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4136 Message-IDs will be renamed to a unique Message-ID before being
4137 entered.
4138
4139 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4140   (let* ((id (mail-header-id header))
4141          (id-dep (and id (intern id dependencies)))
4142          parent-id ref ref-dep ref-header replaced)
4143     ;; Enter this `header' in the `dependencies' table.
4144     (cond
4145      ((not id-dep)
4146       (setq header nil))
4147      ;; The first two cases do the normal part: enter a new `header'
4148      ;; in the `dependencies' table.
4149      ((not (boundp id-dep))
4150       (set id-dep (list header)))
4151      ((null (car (symbol-value id-dep)))
4152       (setcar (symbol-value id-dep) header))
4153
4154      ;; From here the `header' was already present in the
4155      ;; `dependencies' table.
4156      (force-new
4157       ;; Overrides an existing entry;
4158       ;; just set the header part of the entry.
4159       (setcar (symbol-value id-dep) header)
4160       (setq replaced t))
4161
4162      ;; Renames the existing `header' to a unique Message-ID.
4163      ((not gnus-summary-ignore-duplicates)
4164       ;; An article with this Message-ID has already been seen.
4165       ;; We rename the Message-ID.
4166       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4167            (list header))
4168       (mail-header-set-id header id))
4169
4170      ;; The last case ignores an existing entry, except it adds any
4171      ;; additional Xrefs (in case the two articles came from different
4172      ;; servers.
4173      ;; Also sets `header' to `nil' meaning that the `dependencies'
4174      ;; table was *not* modified.
4175      (t
4176       (mail-header-set-xref
4177        (car (symbol-value id-dep))
4178        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4179                    "")
4180                (or (mail-header-xref header) "")))
4181       (setq header nil)))
4182
4183     (when (and header (not replaced))
4184       ;; First check that we are not creating a References loop.
4185       (setq parent-id (gnus-parent-id (mail-header-references header)))
4186       (setq ref parent-id)
4187       (while (and ref
4188                   (setq ref-dep (intern-soft ref dependencies))
4189                   (boundp ref-dep)
4190                   (setq ref-header (car (symbol-value ref-dep))))
4191         (if (string= id ref)
4192             ;; Yuk!  This is a reference loop.  Make the article be a
4193             ;; root article.
4194             (progn
4195               (mail-header-set-references (car (symbol-value id-dep)) "none")
4196               (setq ref nil)
4197               (setq parent-id nil))
4198           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4199       (setq ref-dep (intern (or parent-id "none") dependencies))
4200       (if (boundp ref-dep)
4201           (setcdr (symbol-value ref-dep)
4202                   (nconc (cdr (symbol-value ref-dep))
4203                          (list (symbol-value id-dep))))
4204         (set ref-dep (list nil (symbol-value id-dep)))))
4205     header))
4206
4207 (defun gnus-extract-message-id-from-in-reply-to (string)
4208   (if (string-match "<[^>]+>" string)
4209       (substring string (match-beginning 0) (match-end 0))
4210     nil))
4211
4212 (defun gnus-build-sparse-threads ()
4213   (let ((headers gnus-newsgroup-headers)
4214         (mail-parse-charset gnus-newsgroup-charset)
4215         (gnus-summary-ignore-duplicates t)
4216         header references generation relations
4217         subject child end new-child date)
4218     ;; First we create an alist of generations/relations, where
4219     ;; generations is how much we trust the relation, and the relation
4220     ;; is parent/child.
4221     (gnus-message 7 "Making sparse threads...")
4222     (save-excursion
4223       (nnheader-set-temp-buffer " *gnus sparse threads*")
4224       (while (setq header (pop headers))
4225         (when (and (setq references (mail-header-references header))
4226                    (not (string= references "")))
4227           (insert references)
4228           (setq child (mail-header-id header)
4229                 subject (mail-header-subject header)
4230                 date (mail-header-date header)
4231                 generation 0)
4232           (while (search-backward ">" nil t)
4233             (setq end (1+ (point)))
4234             (when (search-backward "<" nil t)
4235               (setq new-child (buffer-substring (point) end))
4236               (push (list (incf generation)
4237                           child (setq child new-child)
4238                           subject date)
4239                     relations)))
4240           (when child
4241             (push (list (1+ generation) child nil subject) relations))
4242           (erase-buffer)))
4243       (kill-buffer (current-buffer)))
4244     ;; Sort over trustworthiness.
4245     (mapcar
4246      (lambda (relation)
4247        (when (gnus-dependencies-add-header
4248               (make-full-mail-header
4249                gnus-reffed-article-number
4250                (nth 3 relation) "" (or (nth 4 relation) "")
4251                (nth 1 relation)
4252                (or (nth 2 relation) "") 0 0 "")
4253               gnus-newsgroup-dependencies nil)
4254          (push gnus-reffed-article-number gnus-newsgroup-limit)
4255          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4256          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4257                gnus-newsgroup-reads)
4258          (decf gnus-reffed-article-number)))
4259      (sort relations 'car-less-than-car))
4260     (gnus-message 7 "Making sparse threads...done")))
4261
4262 (defun gnus-build-old-threads ()
4263   ;; Look at all the articles that refer back to old articles, and
4264   ;; fetch the headers for the articles that aren't there.  This will
4265   ;; build complete threads - if the roots haven't been expired by the
4266   ;; server, that is.
4267   (let ((mail-parse-charset gnus-newsgroup-charset)
4268         id heads)
4269     (mapatoms
4270      (lambda (refs)
4271        (when (not (car (symbol-value refs)))
4272          (setq heads (cdr (symbol-value refs)))
4273          (while heads
4274            (if (memq (mail-header-number (caar heads))
4275                      gnus-newsgroup-dormant)
4276                (setq heads (cdr heads))
4277              (setq id (symbol-name refs))
4278              (while (and (setq id (gnus-build-get-header id))
4279                          (not (car (gnus-id-to-thread id)))))
4280              (setq heads nil)))))
4281      gnus-newsgroup-dependencies)))
4282
4283 (defsubst gnus-remove-odd-characters (string)
4284   "Translate STRING into something that doesn't contain weird characters."
4285   (mm-subst-char-in-string
4286    ?\r ?\-
4287    (mm-subst-char-in-string ?\n ?\- string t) t))
4288
4289 ;; This function has to be called with point after the article number
4290 ;; on the beginning of the line.
4291 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4292   (let ((eol (point-at-eol))
4293         (buffer (current-buffer))
4294         header references in-reply-to)
4295
4296     ;; overview: [num subject from date id refs chars lines misc]
4297     (unwind-protect
4298         (let (x)
4299           (narrow-to-region (point) eol)
4300           (unless (eobp)
4301             (forward-char))
4302
4303           (setq header
4304                 (make-full-mail-header
4305                  number                 ; number
4306                  (condition-case ()     ; subject
4307                      (gnus-remove-odd-characters
4308                       (funcall gnus-decode-encoded-word-function
4309                                (setq x (nnheader-nov-field))))
4310                    (error x))
4311                  (condition-case ()     ; from
4312                      (gnus-remove-odd-characters
4313                       (funcall gnus-decode-encoded-address-function
4314                                (setq x (nnheader-nov-field))))
4315                    (error x))
4316                  (nnheader-nov-field)   ; date
4317                  (nnheader-nov-read-message-id number)  ; id
4318                  (setq references (nnheader-nov-field)) ; refs
4319                  (nnheader-nov-read-integer) ; chars
4320                  (nnheader-nov-read-integer) ; lines
4321                  (unless (eobp)
4322                    (if (looking-at "Xref: ")
4323                        (goto-char (match-end 0)))
4324                    (nnheader-nov-field)) ; Xref
4325                  (nnheader-nov-parse-extra)))) ; extra
4326
4327       (widen))
4328
4329     (when (and (string= references "")
4330                (setq in-reply-to (mail-header-extra header))
4331                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4332       (mail-header-set-references
4333        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4334
4335     (when gnus-alter-header-function
4336       (funcall gnus-alter-header-function header))
4337     (gnus-dependencies-add-header header dependencies force-new)))
4338
4339 (defun gnus-build-get-header (id)
4340   "Look through the buffer of NOV lines and find the header to ID.
4341 Enter this line into the dependencies hash table, and return
4342 the id of the parent article (if any)."
4343   (let ((deps gnus-newsgroup-dependencies)
4344         found header)
4345     (prog1
4346         (save-excursion
4347           (set-buffer nntp-server-buffer)
4348           (let ((case-fold-search nil))
4349             (goto-char (point-min))
4350             (while (and (not found)
4351                         (search-forward id nil t))
4352               (beginning-of-line)
4353               (setq found (looking-at
4354                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4355                                    (regexp-quote id))))
4356               (or found (beginning-of-line 2)))
4357             (when found
4358               (beginning-of-line)
4359               (and
4360                (setq header (gnus-nov-parse-line
4361                              (read (current-buffer)) deps))
4362                (gnus-parent-id (mail-header-references header))))))
4363       (when header
4364         (let ((number (mail-header-number header)))
4365           (push number gnus-newsgroup-limit)
4366           (push header gnus-newsgroup-headers)
4367           (if (memq number gnus-newsgroup-unselected)
4368               (progn
4369                 (setq gnus-newsgroup-unreads
4370                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4371                                                number))
4372                 (setq gnus-newsgroup-unselected
4373                       (delq number gnus-newsgroup-unselected)))
4374             (push number gnus-newsgroup-ancient)))))))
4375
4376 (defun gnus-build-all-threads ()
4377   "Read all the headers."
4378   (let ((gnus-summary-ignore-duplicates t)
4379         (mail-parse-charset gnus-newsgroup-charset)
4380         (dependencies gnus-newsgroup-dependencies)
4381         header article)
4382     (save-excursion
4383       (set-buffer nntp-server-buffer)
4384       (let ((case-fold-search nil))
4385         (goto-char (point-min))
4386         (while (not (eobp))
4387           (ignore-errors
4388             (setq article (read (current-buffer))
4389                   header (gnus-nov-parse-line article dependencies)))
4390           (when header
4391             (with-current-buffer gnus-summary-buffer
4392               (push header gnus-newsgroup-headers)
4393               (if (memq (setq article (mail-header-number header))
4394                         gnus-newsgroup-unselected)
4395                   (progn
4396                     (setq gnus-newsgroup-unreads
4397                           (gnus-add-to-sorted-list
4398                            gnus-newsgroup-unreads article))
4399                     (setq gnus-newsgroup-unselected
4400                           (delq article gnus-newsgroup-unselected)))
4401                 (push article gnus-newsgroup-ancient)))
4402             (forward-line 1)))))))
4403
4404 (defun gnus-summary-update-article-line (article header)
4405   "Update the line for ARTICLE using HEADER."
4406   (let* ((id (mail-header-id header))
4407          (thread (gnus-id-to-thread id)))
4408     (unless thread
4409       (error "Article in no thread"))
4410     ;; Update the thread.
4411     (setcar thread header)
4412     (gnus-summary-goto-subject article)
4413     (let* ((datal (gnus-data-find-list article))
4414            (data (car datal))
4415            (buffer-read-only nil)
4416            (level (gnus-summary-thread-level)))
4417       (gnus-delete-line)
4418       (let ((inserted (- (point)
4419                          (progn
4420                            (gnus-summary-insert-line
4421                             header level nil
4422                             (memq article gnus-newsgroup-undownloaded)
4423                             (gnus-article-mark article)
4424                             (memq article gnus-newsgroup-replied)
4425                             (memq article gnus-newsgroup-expirable)
4426                             ;; Only insert the Subject string when it's different
4427                             ;; from the previous Subject string.
4428                             (if (and
4429                                  gnus-show-threads
4430                                  (gnus-subject-equal
4431                                   (condition-case ()
4432                                       (mail-header-subject
4433                                        (gnus-data-header
4434                                         (cadr
4435                                          (gnus-data-find-list
4436                                           article
4437                                           (gnus-data-list t)))))
4438                                     ;; Error on the side of excessive subjects.
4439                                     (error ""))
4440                                   (mail-header-subject header)))
4441                                 ""
4442                               (mail-header-subject header))
4443                             nil (cdr (assq article gnus-newsgroup-scored))
4444                             (memq article gnus-newsgroup-processable))
4445                            (point)))))
4446         (when (cdr datal)
4447           (gnus-data-update-list
4448            (cdr datal)
4449            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4450
4451 (defun gnus-summary-update-article (article &optional iheader)
4452   "Update ARTICLE in the summary buffer."
4453   (set-buffer gnus-summary-buffer)
4454   (let* ((header (gnus-summary-article-header article))
4455          (id (mail-header-id header))
4456          (data (gnus-data-find article))
4457          (thread (gnus-id-to-thread id))
4458          (references (mail-header-references header))
4459          (parent
4460           (gnus-id-to-thread
4461            (or (gnus-parent-id
4462                 (when (and references
4463                            (not (equal "" references)))
4464                   references))
4465                "none")))
4466          (buffer-read-only nil)
4467          (old (car thread)))
4468     (when thread
4469       (unless iheader
4470         (setcar thread nil)
4471         (when parent
4472           (delq thread parent)))
4473       (if (gnus-summary-insert-subject id header)
4474           ;; Set the (possibly) new article number in the data structure.
4475           (gnus-data-set-number data (gnus-id-to-article id))
4476         (setcar thread old)
4477         nil))))
4478
4479 (defun gnus-rebuild-thread (id &optional line)
4480   "Rebuild the thread containing ID.
4481 If LINE, insert the rebuilt thread starting on line LINE."
4482   (let ((buffer-read-only nil)
4483         old-pos current thread data)
4484     (if (not gnus-show-threads)
4485         (setq thread (list (car (gnus-id-to-thread id))))
4486       ;; Get the thread this article is part of.
4487       (setq thread (gnus-remove-thread id)))
4488     (setq old-pos (point-at-bol))
4489     (setq current (save-excursion
4490                     (and (re-search-backward "[\r\n]" nil t)
4491                          (gnus-summary-article-number))))
4492     ;; If this is a gathered thread, we have to go some re-gathering.
4493     (when (stringp (car thread))
4494       (let ((subject (car thread))
4495             roots thr)
4496         (setq thread (cdr thread))
4497         (while thread
4498           (unless (memq (setq thr (gnus-id-to-thread
4499                                    (gnus-root-id
4500                                     (mail-header-id (caar thread)))))
4501                         roots)
4502             (push thr roots))
4503           (setq thread (cdr thread)))
4504         ;; We now have all (unique) roots.
4505         (if (= (length roots) 1)
4506             ;; All the loose roots are now one solid root.
4507             (setq thread (car roots))
4508           (setq thread (cons subject (gnus-sort-threads roots))))))
4509     (let (threads)
4510       ;; We then insert this thread into the summary buffer.
4511       (when line
4512         (goto-char (point-min))
4513         (forward-line (1- line)))
4514       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4515         (if gnus-show-threads
4516             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4517           (gnus-summary-prepare-unthreaded thread))
4518         (setq data (nreverse gnus-newsgroup-data))
4519         (setq threads gnus-newsgroup-threads))
4520       ;; We splice the new data into the data structure.
4521       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4522       ;;!!! then we want to insert at the beginning of the buffer.
4523       ;;!!! That happens to be true with Gnus now, but that may
4524       ;;!!! change in the future.  Perhaps.
4525       (gnus-data-enter-list
4526        (if line nil current) data (- (point) old-pos))
4527       (setq gnus-newsgroup-threads
4528             (nconc threads gnus-newsgroup-threads))
4529       (gnus-data-compute-positions))))
4530
4531 (defun gnus-number-to-header (number)
4532   "Return the header for article NUMBER."
4533   (let ((headers gnus-newsgroup-headers))
4534     (while (and headers
4535                 (not (= number (mail-header-number (car headers)))))
4536       (pop headers))
4537     (when headers
4538       (car headers))))
4539
4540 (defun gnus-parent-headers (in-headers &optional generation)
4541   "Return the headers of the GENERATIONeth parent of HEADERS."
4542   (unless generation
4543     (setq generation 1))
4544   (let ((parent t)
4545         (headers in-headers)
4546         references)
4547     (while (and parent
4548                 (not (zerop generation))
4549                 (setq references (mail-header-references headers)))
4550       (setq headers (if (and references
4551                              (setq parent (gnus-parent-id references)))
4552                         (car (gnus-id-to-thread parent))
4553                       nil))
4554       (decf generation))
4555     (and (not (eq headers in-headers))
4556          headers)))
4557
4558 (defun gnus-id-to-thread (id)
4559   "Return the (sub-)thread where ID appears."
4560   (gnus-gethash id gnus-newsgroup-dependencies))
4561
4562 (defun gnus-id-to-article (id)
4563   "Return the article number of ID."
4564   (let ((thread (gnus-id-to-thread id)))
4565     (when (and thread
4566                (car thread))
4567       (mail-header-number (car thread)))))
4568
4569 (defun gnus-id-to-header (id)
4570   "Return the article headers of ID."
4571   (car (gnus-id-to-thread id)))
4572
4573 (defun gnus-article-displayed-root-p (article)
4574   "Say whether ARTICLE is a root(ish) article."
4575   (let ((level (gnus-summary-thread-level article))
4576         (refs (mail-header-references  (gnus-summary-article-header article)))
4577         particle)
4578     (cond
4579      ((null level) nil)
4580      ((zerop level) t)
4581      ((null refs) t)
4582      ((null (gnus-parent-id refs)) t)
4583      ((and (= 1 level)
4584            (null (setq particle (gnus-id-to-article
4585                                  (gnus-parent-id refs))))
4586            (null (gnus-summary-thread-level particle)))))))
4587
4588 (defun gnus-root-id (id)
4589   "Return the id of the root of the thread where ID appears."
4590   (let (last-id prev)
4591     (while (and id (setq prev (car (gnus-id-to-thread id))))
4592       (setq last-id id
4593             id (gnus-parent-id (mail-header-references prev))))
4594     last-id))
4595
4596 (defun gnus-articles-in-thread (thread)
4597   "Return the list of articles in THREAD."
4598   (cons (mail-header-number (car thread))
4599         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4600
4601 (defun gnus-remove-thread (id &optional dont-remove)
4602   "Remove the thread that has ID in it."
4603   (let (headers thread last-id)
4604     ;; First go up in this thread until we find the root.
4605     (setq last-id (gnus-root-id id)
4606           headers (message-flatten-list (gnus-id-to-thread last-id)))
4607     ;; We have now found the real root of this thread.  It might have
4608     ;; been gathered into some loose thread, so we have to search
4609     ;; through the threads to find the thread we wanted.
4610     (let ((threads gnus-newsgroup-threads)
4611           sub)
4612       (while threads
4613         (setq sub (car threads))
4614         (if (stringp (car sub))
4615             ;; This is a gathered thread, so we look at the roots
4616             ;; below it to find whether this article is in this
4617             ;; gathered root.
4618             (progn
4619               (setq sub (cdr sub))
4620               (while sub
4621                 (when (member (caar sub) headers)
4622                   (setq thread (car threads)
4623                         threads nil
4624                         sub nil))
4625                 (setq sub (cdr sub))))
4626           ;; It's an ordinary thread, so we check it.
4627           (when (eq (car sub) (car headers))
4628             (setq thread sub
4629                   threads nil)))
4630         (setq threads (cdr threads)))
4631       ;; If this article is in no thread, then it's a root.
4632       (if thread
4633           (unless dont-remove
4634             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4635         (setq thread (gnus-id-to-thread last-id)))
4636       (when thread
4637         (prog1
4638             thread                      ; We return this thread.
4639           (unless dont-remove
4640             (if (stringp (car thread))
4641                 (progn
4642                   ;; If we use dummy roots, then we have to remove the
4643                   ;; dummy root as well.
4644                   (when (eq gnus-summary-make-false-root 'dummy)
4645                     ;; We go to the dummy root by going to
4646                     ;; the first sub-"thread", and then one line up.
4647                     (gnus-summary-goto-article
4648                      (mail-header-number (caadr thread)))
4649                     (forward-line -1)
4650                     (gnus-delete-line)
4651                     (gnus-data-compute-positions))
4652                   (setq thread (cdr thread))
4653                   (while thread
4654                     (gnus-remove-thread-1 (car thread))
4655                     (setq thread (cdr thread))))
4656               (gnus-remove-thread-1 thread))))))))
4657
4658 (defun gnus-remove-thread-1 (thread)
4659   "Remove the thread THREAD recursively."
4660   (let ((number (mail-header-number (pop thread)))
4661         d)
4662     (setq thread (reverse thread))
4663     (while thread
4664       (gnus-remove-thread-1 (pop thread)))
4665     (when (setq d (gnus-data-find number))
4666       (goto-char (gnus-data-pos d))
4667       (gnus-summary-show-thread)
4668       (gnus-data-remove
4669        number
4670        (- (point-at-bol)
4671           (prog1
4672               (1+ (point-at-eol))
4673             (gnus-delete-line)))))))
4674
4675 (defun gnus-sort-threads-recursive (threads func)
4676   (sort (mapcar (lambda (thread)
4677                   (cons (car thread)
4678                         (and (cdr thread)
4679                              (gnus-sort-threads-recursive (cdr thread) func))))
4680                 threads) func))
4681
4682 (defun gnus-sort-threads-loop (threads func)
4683   (let* ((superthread (cons nil threads))
4684          (stack (list (cons superthread threads)))
4685          remaining-threads thread)
4686     (while stack
4687       (setq remaining-threads (cdr (car stack)))
4688       (if remaining-threads
4689           (progn (setq thread (car remaining-threads))
4690                  (setcdr (car stack) (cdr remaining-threads))
4691                  (if (cdr thread)
4692                      (push (cons thread (cdr thread)) stack)))
4693         (setq thread (caar stack))
4694         (setcdr thread (sort (cdr thread) func))
4695         (pop stack)))
4696     (cdr superthread)))
4697
4698 (defun gnus-sort-threads (threads)
4699   "Sort THREADS."
4700   (if (not gnus-thread-sort-functions)
4701       threads
4702     (gnus-message 8 "Sorting threads...")
4703     (prog1
4704         (condition-case nil
4705             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4706               (gnus-sort-threads-recursive
4707                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4708           ;; Even after binding max-lisp-eval-depth, the recursive
4709           ;; sorter might fail for very long threads.  In that case,
4710           ;; try using a (less well-tested) non-recursive sorter.
4711           (error (gnus-sort-threads-loop
4712                   threads (gnus-make-sort-function
4713                            gnus-thread-sort-functions))))
4714       (gnus-message 8 "Sorting threads...done"))))
4715
4716 (defun gnus-sort-articles (articles)
4717   "Sort ARTICLES."
4718   (when gnus-article-sort-functions
4719     (gnus-message 7 "Sorting articles...")
4720     (prog1
4721         (setq gnus-newsgroup-headers
4722               (sort articles (gnus-make-sort-function
4723                               gnus-article-sort-functions)))
4724       (gnus-message 7 "Sorting articles...done"))))
4725
4726 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4727 (defmacro gnus-thread-header (thread)
4728   "Return header of first article in THREAD.
4729 Note that THREAD must never, ever be anything else than a variable -
4730 using some other form will lead to serious barfage."
4731   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4732   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4733   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4734         (vector thread) 2))
4735
4736 (defsubst gnus-article-sort-by-number (h1 h2)
4737   "Sort articles by article number."
4738   (< (mail-header-number h1)
4739      (mail-header-number h2)))
4740
4741 (defun gnus-thread-sort-by-number (h1 h2)
4742   "Sort threads by root article number."
4743   (gnus-article-sort-by-number
4744    (gnus-thread-header h1) (gnus-thread-header h2)))
4745
4746 (defsubst gnus-article-sort-by-random (h1 h2)
4747   "Sort articles by article number."
4748   (zerop (random 2)))
4749
4750 (defun gnus-thread-sort-by-random (h1 h2)
4751   "Sort threads by root article number."
4752   (gnus-article-sort-by-random
4753    (gnus-thread-header h1) (gnus-thread-header h2)))
4754
4755 (defsubst gnus-article-sort-by-lines (h1 h2)
4756   "Sort articles by article Lines header."
4757   (< (mail-header-lines h1)
4758      (mail-header-lines h2)))
4759
4760 (defun gnus-thread-sort-by-lines (h1 h2)
4761   "Sort threads by root article Lines header."
4762   (gnus-article-sort-by-lines
4763    (gnus-thread-header h1) (gnus-thread-header h2)))
4764
4765 (defsubst gnus-article-sort-by-chars (h1 h2)
4766   "Sort articles by octet length."
4767   (< (mail-header-chars h1)
4768      (mail-header-chars h2)))
4769
4770 (defun gnus-thread-sort-by-chars (h1 h2)
4771   "Sort threads by root article octet length."
4772   (gnus-article-sort-by-chars
4773    (gnus-thread-header h1) (gnus-thread-header h2)))
4774
4775 (defsubst gnus-article-sort-by-author (h1 h2)
4776   "Sort articles by root author."
4777   (string-lessp
4778    (let ((extract (funcall
4779                    gnus-extract-address-components
4780                    (mail-header-from h1))))
4781      (or (car extract) (cadr extract) ""))
4782    (let ((extract (funcall
4783                    gnus-extract-address-components
4784                    (mail-header-from h2))))
4785      (or (car extract) (cadr extract) ""))))
4786
4787 (defun gnus-thread-sort-by-author (h1 h2)
4788   "Sort threads by root author."
4789   (gnus-article-sort-by-author
4790    (gnus-thread-header h1)  (gnus-thread-header h2)))
4791
4792 (defsubst gnus-article-sort-by-recipient (h1 h2)
4793   "Sort articles by recipient."
4794   (string-lessp
4795    (let ((extract (funcall
4796                    gnus-extract-address-components
4797                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4798      (or (car extract) (cadr extract)))
4799    (let ((extract (funcall
4800                    gnus-extract-address-components
4801                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4802      (or (car extract) (cadr extract)))))
4803
4804 (defun gnus-thread-sort-by-recipient (h1 h2)
4805   "Sort threads by root recipient."
4806   (gnus-article-sort-by-recipient
4807    (gnus-thread-header h1) (gnus-thread-header h2)))
4808
4809 (defsubst gnus-article-sort-by-subject (h1 h2)
4810   "Sort articles by root subject."
4811   (string-lessp
4812    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4813    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4814
4815 (defun gnus-thread-sort-by-subject (h1 h2)
4816   "Sort threads by root subject."
4817   (gnus-article-sort-by-subject
4818    (gnus-thread-header h1) (gnus-thread-header h2)))
4819
4820 (defsubst gnus-article-sort-by-date (h1 h2)
4821   "Sort articles by root article date."
4822   (time-less-p
4823    (gnus-date-get-time (mail-header-date h1))
4824    (gnus-date-get-time (mail-header-date h2))))
4825
4826 (defun gnus-thread-sort-by-date (h1 h2)
4827   "Sort threads by root article date."
4828   (gnus-article-sort-by-date
4829    (gnus-thread-header h1) (gnus-thread-header h2)))
4830
4831 (defsubst gnus-article-sort-by-score (h1 h2)
4832   "Sort articles by root article score.
4833 Unscored articles will be counted as having a score of zero."
4834   (> (or (cdr (assq (mail-header-number h1)
4835                     gnus-newsgroup-scored))
4836          gnus-summary-default-score 0)
4837      (or (cdr (assq (mail-header-number h2)
4838                     gnus-newsgroup-scored))
4839          gnus-summary-default-score 0)))
4840
4841 (defun gnus-thread-sort-by-score (h1 h2)
4842   "Sort threads by root article score."
4843   (gnus-article-sort-by-score
4844    (gnus-thread-header h1) (gnus-thread-header h2)))
4845
4846 (defun gnus-thread-sort-by-total-score (h1 h2)
4847   "Sort threads by the sum of all scores in the thread.
4848 Unscored articles will be counted as having a score of zero."
4849   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4850
4851 (defun gnus-thread-total-score (thread)
4852   ;; This function find the total score of THREAD.
4853   (cond
4854    ((null thread)
4855     0)
4856    ((consp thread)
4857     (if (stringp (car thread))
4858         (apply gnus-thread-score-function 0
4859                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4860       (gnus-thread-total-score-1 thread)))
4861    (t
4862     (gnus-thread-total-score-1 (list thread)))))
4863
4864 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4865   "Sort threads such that the thread with the most recently arrived article comes first."
4866   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4867
4868 (defun gnus-thread-highest-number (thread)
4869   "Return the highest article number in THREAD."
4870   (apply 'max (mapcar (lambda (header)
4871                         (mail-header-number header))
4872                       (message-flatten-list thread))))
4873
4874 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4875   "Sort threads such that the thread with the most recently dated article comes first."
4876   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4877
4878 (defun gnus-thread-latest-date (thread)
4879   "Return the highest article date in THREAD."
4880   (let ((previous-time 0))
4881     (apply 'max
4882            (mapcar
4883             (lambda (header)
4884               (setq previous-time
4885                     (condition-case ()
4886                         (time-to-seconds (mail-header-parse-date
4887                                           (mail-header-date header)))
4888                       (error previous-time))))
4889             (sort
4890              (message-flatten-list thread)
4891              (lambda (h1 h2)
4892                (< (mail-header-number h1)
4893                   (mail-header-number h2))))))))
4894
4895 (defun gnus-thread-total-score-1 (root)
4896   ;; This function find the total score of the thread below ROOT.
4897   (setq root (car root))
4898   (apply gnus-thread-score-function
4899          (or (append
4900               (mapcar 'gnus-thread-total-score
4901                       (cdr (gnus-id-to-thread (mail-header-id root))))
4902               (when (> (mail-header-number root) 0)
4903                 (list (or (cdr (assq (mail-header-number root)
4904                                      gnus-newsgroup-scored))
4905                           gnus-summary-default-score 0))))
4906              (list gnus-summary-default-score)
4907              '(0))))
4908
4909 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4910 (defvar gnus-tmp-prev-subject nil)
4911 (defvar gnus-tmp-false-parent nil)
4912 (defvar gnus-tmp-root-expunged nil)
4913 (defvar gnus-tmp-dummy-line nil)
4914
4915 (eval-when-compile (defvar gnus-tmp-header))
4916 (defun gnus-extra-header (type &optional header)
4917   "Return the extra header of TYPE."
4918   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4919       ""))
4920
4921 (defvar gnus-tmp-thread-tree-header-string "")
4922
4923 (defcustom gnus-sum-thread-tree-root "> "
4924   "With %B spec, used for the root of a thread.
4925 If nil, use subject instead."
4926   :version "22.1"
4927   :type '(radio (const :format "%v  " nil) string)
4928   :group 'gnus-thread)
4929
4930 (defcustom gnus-sum-thread-tree-false-root "> "
4931   "With %B spec, used for a false root of a thread.
4932 If nil, use subject instead."
4933   :version "22.1"
4934   :type '(radio (const :format "%v  " nil) string)
4935   :group 'gnus-thread)
4936
4937 (defcustom gnus-sum-thread-tree-single-indent ""
4938   "With %B spec, used for a thread with just one message.
4939 If nil, use subject instead."
4940   :version "22.1"
4941   :type '(radio (const :format "%v  " nil) string)
4942   :group 'gnus-thread)
4943
4944 (defcustom gnus-sum-thread-tree-vertical "| "
4945   "With %B spec, used for drawing a vertical line."
4946   :version "22.1"
4947   :type 'string
4948   :group 'gnus-thread)
4949
4950 (defcustom gnus-sum-thread-tree-indent "  "
4951   "With %B spec, used for indenting."
4952   :version "22.1"
4953   :type 'string
4954   :group 'gnus-thread)
4955
4956 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4957   "With %B spec, used for a leaf with brothers."
4958   :version "22.1"
4959   :type 'string
4960   :group 'gnus-thread)
4961
4962 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4963   "With %B spec, used for a leaf without brothers."
4964   :version "22.1"
4965   :type 'string
4966   :group 'gnus-thread)
4967
4968 (defun gnus-summary-prepare-threads (threads)
4969   "Prepare summary buffer from THREADS and indentation LEVEL.
4970 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4971 or a straight list of headers."
4972   (gnus-message 7 "Generating summary...")
4973
4974   (setq gnus-newsgroup-threads threads)
4975   (beginning-of-line)
4976
4977   (let ((gnus-tmp-level 0)
4978         (default-score (or gnus-summary-default-score 0))
4979         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4980         (building-line-count gnus-summary-display-while-building)
4981         (building-count (integerp gnus-summary-display-while-building))
4982         thread number subject stack state gnus-tmp-gathered beg-match
4983         new-roots gnus-tmp-new-adopts thread-end simp-subject
4984         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4985         gnus-tmp-replied gnus-tmp-subject-or-nil
4986         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4987         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4988         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4989         tree-stack)
4990
4991     (setq gnus-tmp-prev-subject nil
4992           gnus-tmp-thread-tree-header-string "")
4993
4994     (if (vectorp (car threads))
4995         ;; If this is a straight (sic) list of headers, then a
4996         ;; threaded summary display isn't required, so we just create
4997         ;; an unthreaded one.
4998         (gnus-summary-prepare-unthreaded threads)
4999
5000       ;; Do the threaded display.
5001
5002       (if gnus-summary-display-while-building
5003           (switch-to-buffer (buffer-name)))
5004       (while (or threads stack gnus-tmp-new-adopts new-roots)
5005
5006         (if (and (= gnus-tmp-level 0)
5007                  (or (not stack)
5008                      (= (caar stack) 0))
5009                  (not gnus-tmp-false-parent)
5010                  (or gnus-tmp-new-adopts new-roots))
5011             (if gnus-tmp-new-adopts
5012                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5013                       thread (list (car gnus-tmp-new-adopts))
5014                       gnus-tmp-header (caar thread)
5015                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5016               (when new-roots
5017                 (setq thread (list (car new-roots))
5018                       gnus-tmp-header (caar thread)
5019                       new-roots (cdr new-roots))))
5020
5021           (if threads
5022               ;; If there are some threads, we do them before the
5023               ;; threads on the stack.
5024               (setq thread threads
5025                     gnus-tmp-header (caar thread))
5026             ;; There were no current threads, so we pop something off
5027             ;; the stack.
5028             (setq state (car stack)
5029                   gnus-tmp-level (car state)
5030                   tree-stack (cadr state)
5031                   thread (caddr state)
5032                   stack (cdr stack)
5033                   gnus-tmp-header (caar thread))))
5034
5035         (setq gnus-tmp-false-parent nil)
5036         (setq gnus-tmp-root-expunged nil)
5037         (setq thread-end nil)
5038
5039         (if (stringp gnus-tmp-header)
5040             ;; The header is a dummy root.
5041             (cond
5042              ((eq gnus-summary-make-false-root 'adopt)
5043               ;; We let the first article adopt the rest.
5044               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5045                                                (cddar thread)))
5046               (setq gnus-tmp-gathered
5047                     (nconc (mapcar
5048                             (lambda (h) (mail-header-number (car h)))
5049                             (cddar thread))
5050                            gnus-tmp-gathered))
5051               (setq thread (cons (list (caar thread)
5052                                        (cadar thread))
5053                                  (cdr thread)))
5054               (setq gnus-tmp-level -1
5055                     gnus-tmp-false-parent t))
5056              ((eq gnus-summary-make-false-root 'empty)
5057               ;; We print adopted articles with empty subject fields.
5058               (setq gnus-tmp-gathered
5059                     (nconc (mapcar
5060                             (lambda (h) (mail-header-number (car h)))
5061                             (cddar thread))
5062                            gnus-tmp-gathered))
5063               (setq gnus-tmp-level -1))
5064              ((eq gnus-summary-make-false-root 'dummy)
5065               ;; We remember that we probably want to output a dummy
5066               ;; root.
5067               (setq gnus-tmp-dummy-line gnus-tmp-header)
5068               (setq gnus-tmp-prev-subject gnus-tmp-header))
5069              (t
5070               ;; We do not make a root for the gathered
5071               ;; sub-threads at all.
5072               (setq gnus-tmp-level -1)))
5073
5074           (setq number (mail-header-number gnus-tmp-header)
5075                 subject (mail-header-subject gnus-tmp-header)
5076                 simp-subject (gnus-simplify-subject-fully subject))
5077
5078           (cond
5079            ;; If the thread has changed subject, we might want to make
5080            ;; this subthread into a root.
5081            ((and (null gnus-thread-ignore-subject)
5082                  (not (zerop gnus-tmp-level))
5083                  gnus-tmp-prev-subject
5084                  (not (string= gnus-tmp-prev-subject simp-subject)))
5085             (setq new-roots (nconc new-roots (list (car thread)))
5086                   thread-end t
5087                   gnus-tmp-header nil))
5088            ;; If the article lies outside the current limit,
5089            ;; then we do not display it.
5090            ((not (memq number gnus-newsgroup-limit))
5091             (setq gnus-tmp-gathered
5092                   (nconc (mapcar
5093                           (lambda (h) (mail-header-number (car h)))
5094                           (cdar thread))
5095                          gnus-tmp-gathered))
5096             (setq gnus-tmp-new-adopts (if (cdar thread)
5097                                           (append gnus-tmp-new-adopts
5098                                                   (cdar thread))
5099                                         gnus-tmp-new-adopts)
5100                   thread-end t
5101                   gnus-tmp-header nil)
5102             (when (zerop gnus-tmp-level)
5103               (setq gnus-tmp-root-expunged t)))
5104            ;; Perhaps this article is to be marked as read?
5105            ((and gnus-summary-mark-below
5106                  (< (or (cdr (assq number gnus-newsgroup-scored))
5107                         default-score)
5108                     gnus-summary-mark-below)
5109                  ;; Don't touch sparse articles.
5110                  (not (gnus-summary-article-sparse-p number))
5111                  (not (gnus-summary-article-ancient-p number)))
5112             (setq gnus-newsgroup-unreads
5113                   (delq number gnus-newsgroup-unreads))
5114             (if gnus-newsgroup-auto-expire
5115                 (setq gnus-newsgroup-expirable
5116                       (gnus-add-to-sorted-list
5117                        gnus-newsgroup-expirable number))
5118               (push (cons number gnus-low-score-mark)
5119                     gnus-newsgroup-reads))))
5120
5121           (when gnus-tmp-header
5122             ;; We may have an old dummy line to output before this
5123             ;; article.
5124             (when (and gnus-tmp-dummy-line
5125                        (gnus-subject-equal
5126                         gnus-tmp-dummy-line
5127                         (mail-header-subject gnus-tmp-header)))
5128               (gnus-summary-insert-dummy-line
5129                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5130               (setq gnus-tmp-dummy-line nil))
5131
5132             ;; Compute the mark.
5133             (setq gnus-tmp-unread (gnus-article-mark number))
5134
5135             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5136                                   gnus-tmp-header gnus-tmp-level)
5137                   gnus-newsgroup-data)
5138
5139             ;; Actually insert the line.
5140             (setq
5141              gnus-tmp-subject-or-nil
5142              (cond
5143               ((and gnus-thread-ignore-subject
5144                     gnus-tmp-prev-subject
5145                     (not (string= gnus-tmp-prev-subject simp-subject)))
5146                subject)
5147               ((zerop gnus-tmp-level)
5148                (if (and (eq gnus-summary-make-false-root 'empty)
5149                         (memq number gnus-tmp-gathered)
5150                         gnus-tmp-prev-subject
5151                         (string= gnus-tmp-prev-subject simp-subject))
5152                    gnus-summary-same-subject
5153                  subject))
5154               (t gnus-summary-same-subject)))
5155             (if (and (eq gnus-summary-make-false-root 'adopt)
5156                      (= gnus-tmp-level 1)
5157                      (memq number gnus-tmp-gathered))
5158                 (setq gnus-tmp-opening-bracket ?\<
5159                       gnus-tmp-closing-bracket ?\>)
5160               (setq gnus-tmp-opening-bracket ?\[
5161                     gnus-tmp-closing-bracket ?\]))
5162             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5163                 (gnus-make-thread-indent-array
5164                  (max (* 2 (length gnus-thread-indent-array))
5165                       gnus-tmp-level)))
5166             (setq
5167              gnus-tmp-indentation
5168              (aref gnus-thread-indent-array gnus-tmp-level)
5169              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5170              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5171                                 gnus-summary-default-score 0)
5172              gnus-tmp-score-char
5173              (if (or (null gnus-summary-default-score)
5174                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5175                          gnus-summary-zcore-fuzz))
5176                  ?                      ;Whitespace
5177                (if (< gnus-tmp-score gnus-summary-default-score)
5178                    gnus-score-below-mark gnus-score-over-mark))
5179              gnus-tmp-replied
5180              (cond ((memq number gnus-newsgroup-processable)
5181                     gnus-process-mark)
5182                    ((memq number gnus-newsgroup-cached)
5183                     gnus-cached-mark)
5184                    ((memq number gnus-newsgroup-replied)
5185                     gnus-replied-mark)
5186                    ((memq number gnus-newsgroup-forwarded)
5187                     gnus-forwarded-mark)
5188                    ((memq number gnus-newsgroup-saved)
5189                     gnus-saved-mark)
5190                    ((memq number gnus-newsgroup-recent)
5191                     gnus-recent-mark)
5192                    ((memq number gnus-newsgroup-unseen)
5193                     gnus-unseen-mark)
5194                    (t gnus-no-mark))
5195              gnus-tmp-downloaded
5196              (cond ((memq number gnus-newsgroup-undownloaded)
5197                     gnus-undownloaded-mark)
5198                    (gnus-newsgroup-agentized
5199                     gnus-downloaded-mark)
5200                    (t
5201                     gnus-no-mark))
5202              gnus-tmp-from (mail-header-from gnus-tmp-header)
5203              gnus-tmp-name
5204              (cond
5205               ((string-match "<[^>]+> *$" gnus-tmp-from)
5206                (setq beg-match (match-beginning 0))
5207                (or (and (string-match "^\".+\"" gnus-tmp-from)
5208                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5209                    (substring gnus-tmp-from 0 beg-match)))
5210               ((string-match "(.+)" gnus-tmp-from)
5211                (substring gnus-tmp-from
5212                           (1+ (match-beginning 0)) (1- (match-end 0))))
5213               (t gnus-tmp-from))
5214
5215              ;; Do the %B string
5216              gnus-tmp-thread-tree-header-string
5217              (cond
5218               ((not gnus-show-threads) "")
5219               ((zerop gnus-tmp-level)
5220                (cond ((cdar thread)
5221                       (or gnus-sum-thread-tree-root subject))
5222                      (gnus-tmp-new-adopts
5223                       (or gnus-sum-thread-tree-false-root subject))
5224                      (t
5225                       (or gnus-sum-thread-tree-single-indent subject))))
5226               (t
5227                (concat (apply 'concat
5228                               (mapcar (lambda (item)
5229                                         (if (= item 1)
5230                                             gnus-sum-thread-tree-vertical
5231                                           gnus-sum-thread-tree-indent))
5232                                       (cdr (reverse tree-stack))))
5233                        (if (nth 1 thread)
5234                            gnus-sum-thread-tree-leaf-with-other
5235                          gnus-sum-thread-tree-single-leaf)))))
5236             (when (string= gnus-tmp-name "")
5237               (setq gnus-tmp-name gnus-tmp-from))
5238             (unless (numberp gnus-tmp-lines)
5239               (setq gnus-tmp-lines -1))
5240             (if (= gnus-tmp-lines -1)
5241                 (setq gnus-tmp-lines "?")
5242               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5243               (gnus-put-text-property
5244              (point)
5245              (progn (eval gnus-summary-line-format-spec) (point))
5246                'gnus-number number)
5247             (when gnus-visual-p
5248               (forward-line -1)
5249               (gnus-run-hooks 'gnus-summary-update-hook)
5250               (forward-line 1))
5251
5252             (setq gnus-tmp-prev-subject simp-subject)))
5253
5254         (when (nth 1 thread)
5255           (push (list (max 0 gnus-tmp-level)
5256                       (copy-sequence tree-stack)
5257                       (nthcdr 1 thread))
5258                 stack))
5259         (push (if (nth 1 thread) 1 0) tree-stack)
5260         (incf gnus-tmp-level)
5261         (setq threads (if thread-end nil (cdar thread)))
5262         (if gnus-summary-display-while-building
5263             (if building-count
5264                 (progn
5265                   ;; use a set frequency
5266                   (setq building-line-count (1- building-line-count))
5267                   (when (= building-line-count 0)
5268                     (sit-for 0)
5269                     (setq building-line-count
5270                           gnus-summary-display-while-building)))
5271               ;; always
5272               (sit-for 0)))
5273         (unless threads
5274           (setq gnus-tmp-level 0)))))
5275   (gnus-message 7 "Generating summary...done"))
5276
5277 (defun gnus-summary-prepare-unthreaded (headers)
5278   "Generate an unthreaded summary buffer based on HEADERS."
5279   (let (header number mark)
5280
5281     (beginning-of-line)
5282
5283     (while headers
5284       ;; We may have to root out some bad articles...
5285       (when (memq (setq number (mail-header-number
5286                                 (setq header (pop headers))))
5287                   gnus-newsgroup-limit)
5288         ;; Mark article as read when it has a low score.
5289         (when (and gnus-summary-mark-below
5290                    (< (or (cdr (assq number gnus-newsgroup-scored))
5291                           gnus-summary-default-score 0)
5292                       gnus-summary-mark-below)
5293                    (not (gnus-summary-article-ancient-p number)))
5294           (setq gnus-newsgroup-unreads
5295                 (delq number gnus-newsgroup-unreads))
5296           (if gnus-newsgroup-auto-expire
5297               (push number gnus-newsgroup-expirable)
5298             (push (cons number gnus-low-score-mark)
5299                   gnus-newsgroup-reads)))
5300
5301         (setq mark (gnus-article-mark number))
5302         (push (gnus-data-make number mark (1+ (point)) header 0)
5303               gnus-newsgroup-data)
5304         (gnus-summary-insert-line
5305          header 0 number
5306          (memq number gnus-newsgroup-undownloaded)
5307          mark (memq number gnus-newsgroup-replied)
5308          (memq number gnus-newsgroup-expirable)
5309          (mail-header-subject header) nil
5310          (cdr (assq number gnus-newsgroup-scored))
5311          (memq number gnus-newsgroup-processable))))))
5312
5313 (defun gnus-summary-remove-list-identifiers ()
5314   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5315   (let ((regexp (if (consp gnus-list-identifiers)
5316                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5317                   gnus-list-identifiers))
5318         changed subject)
5319     (when regexp
5320       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5321       (dolist (header gnus-newsgroup-headers)
5322         (setq subject (mail-header-subject header)
5323               changed nil)
5324         (while (string-match regexp subject)
5325           (setq subject
5326                 (concat (substring subject 0 (match-beginning 1))
5327                         (substring subject (match-end 0)))
5328                 changed t))
5329         (when changed
5330           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5331             (setq subject
5332                   (concat (substring subject 0 (match-beginning 1))
5333                           (substring subject (match-end 1)))))
5334           (mail-header-set-subject header subject))))))
5335
5336 (defun gnus-fetch-headers (articles)
5337   "Fetch headers of ARTICLES."
5338   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5339     (gnus-message 5 "Fetching headers for %s..." name)
5340     (prog1
5341         (if (eq 'nov
5342                 (setq gnus-headers-retrieved-by
5343                       (gnus-retrieve-headers
5344                        articles gnus-newsgroup-name
5345                        ;; We might want to fetch old headers, but
5346                        ;; not if there is only 1 article.
5347                        (and (or (and
5348                                  (not (eq gnus-fetch-old-headers 'some))
5349                                  (not (numberp gnus-fetch-old-headers)))
5350                                 (> (length articles) 1))
5351                             gnus-fetch-old-headers))))
5352             (gnus-get-newsgroup-headers-xover
5353              articles nil nil gnus-newsgroup-name t)
5354           (gnus-get-newsgroup-headers))
5355       (gnus-message 5 "Fetching headers for %s...done" name))))
5356
5357 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5358   "Select newsgroup GROUP.
5359 If READ-ALL is non-nil, all articles in the group are selected.
5360 If SELECT-ARTICLES, only select those articles from GROUP."
5361   (let* ((entry (gnus-group-entry group))
5362          ;;!!! Dirty hack; should be removed.
5363          (gnus-summary-ignore-duplicates
5364           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5365               t
5366             gnus-summary-ignore-duplicates))
5367          (info (nth 2 entry))
5368          articles fetched-articles cached)
5369
5370     (unless (gnus-check-server
5371              (set (make-local-variable 'gnus-current-select-method)
5372                   (gnus-find-method-for-group group)))
5373       (error "Couldn't open server"))
5374
5375     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5376         (gnus-activate-group group)     ; Or we can activate it...
5377         (progn                          ; Or we bug out.
5378           (when (equal major-mode 'gnus-summary-mode)
5379             (gnus-kill-buffer (current-buffer)))
5380           (error "Couldn't activate group %s: %s"
5381                  (gnus-group-decoded-name group) (gnus-status-message group))))
5382
5383     (unless (gnus-request-group group t)
5384       (when (equal major-mode 'gnus-summary-mode)
5385         (gnus-kill-buffer (current-buffer)))
5386       (error "Couldn't request group %s: %s"
5387              (gnus-group-decoded-name group) (gnus-status-message group)))
5388
5389     (when gnus-agent
5390       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5391
5392       (setq gnus-summary-use-undownloaded-faces
5393             (gnus-agent-find-parameter
5394              group
5395              'agent-enable-undownloaded-faces)))
5396
5397     (setq gnus-newsgroup-name group
5398           gnus-newsgroup-unselected nil
5399           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5400
5401     (let ((display (gnus-group-find-parameter group 'display)))
5402       (setq gnus-newsgroup-display
5403             (cond
5404              ((not (zerop (or (car-safe read-all) 0)))
5405               ;; The user entered the group with C-u SPC/RET, let's show
5406               ;; all articles.
5407               'gnus-not-ignore)
5408              ((eq display 'all)
5409               'gnus-not-ignore)
5410              ((arrayp display)
5411               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5412              ((numberp display)
5413               ;; The following is probably the "correct" solution, but
5414               ;; it makes Gnus fetch all headers and then limit the
5415               ;; articles (which is slow), so instead we hack the
5416               ;; select-articles parameter instead. -- Simon Josefsson
5417               ;; <jas@kth.se>
5418               ;;
5419               ;; (gnus-byte-compile
5420               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5421               ;;                         display)))))
5422               (setq select-articles
5423                     (gnus-uncompress-range
5424                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5425                              (if (> tmp 0)
5426                                  tmp
5427                                1))
5428                            (cdr (gnus-active group)))))
5429               nil)
5430              (t
5431               nil))))
5432
5433     (gnus-summary-setup-default-charset)
5434
5435     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5436     (when (gnus-virtual-group-p group)
5437       (setq cached gnus-newsgroup-cached))
5438
5439     (setq gnus-newsgroup-unreads
5440           (gnus-sorted-ndifference
5441            (gnus-sorted-ndifference gnus-newsgroup-unreads
5442                                     gnus-newsgroup-marked)
5443            gnus-newsgroup-dormant))
5444
5445     (setq gnus-newsgroup-processable nil)
5446
5447     (gnus-update-read-articles group gnus-newsgroup-unreads)
5448
5449     ;; Adjust and set lists of article marks.
5450     (when info
5451       (gnus-adjust-marked-articles info))
5452     (if (setq articles select-articles)
5453         (setq gnus-newsgroup-unselected
5454               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5455       (setq articles (gnus-articles-to-read group read-all)))
5456
5457     (cond
5458      ((null articles)
5459       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5460       'quit)
5461      ((eq articles 0) nil)
5462      (t
5463       ;; Init the dependencies hash table.
5464       (setq gnus-newsgroup-dependencies
5465             (gnus-make-hashtable (length articles)))
5466       (gnus-set-global-variables)
5467       ;; Retrieve the headers and read them in.
5468
5469       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5470
5471       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5472       (when cached
5473         (setq gnus-newsgroup-cached cached))
5474
5475       ;; Suppress duplicates?
5476       (when gnus-suppress-duplicates
5477         (gnus-dup-suppress-articles))
5478
5479       ;; Set the initial limit.
5480       (setq gnus-newsgroup-limit (copy-sequence articles))
5481       ;; Remove canceled articles from the list of unread articles.
5482       (setq fetched-articles
5483             (mapcar (lambda (headers) (mail-header-number headers))
5484                     gnus-newsgroup-headers))
5485       (setq gnus-newsgroup-articles fetched-articles)
5486       (setq gnus-newsgroup-unreads
5487             (gnus-sorted-nintersection
5488              gnus-newsgroup-unreads fetched-articles))
5489       (gnus-compute-unseen-list)
5490
5491       ;; Removed marked articles that do not exist.
5492       (gnus-update-missing-marks
5493        (gnus-sorted-difference articles fetched-articles))
5494       ;; We might want to build some more threads first.
5495       (when (and gnus-fetch-old-headers
5496                  (eq gnus-headers-retrieved-by 'nov))
5497         (if (eq gnus-fetch-old-headers 'invisible)
5498             (gnus-build-all-threads)
5499           (gnus-build-old-threads)))
5500       ;; Let the Gnus agent mark articles as read.
5501       (when gnus-agent
5502         (gnus-agent-get-undownloaded-list))
5503       ;; Remove list identifiers from subject
5504       (when gnus-list-identifiers
5505         (gnus-summary-remove-list-identifiers))
5506       ;; Check whether auto-expire is to be done in this group.
5507       (setq gnus-newsgroup-auto-expire
5508             (gnus-group-auto-expirable-p group))
5509       ;; Set up the article buffer now, if necessary.
5510       (unless (and gnus-single-article-buffer
5511                    (equal gnus-article-buffer "*Article*"))
5512         (gnus-article-setup-buffer))
5513       ;; First and last article in this newsgroup.
5514       (when gnus-newsgroup-headers
5515         (setq gnus-newsgroup-begin
5516               (mail-header-number (car gnus-newsgroup-headers))
5517               gnus-newsgroup-end
5518               (mail-header-number
5519                (gnus-last-element gnus-newsgroup-headers))))
5520       ;; GROUP is successfully selected.
5521       (or gnus-newsgroup-headers t)))))
5522
5523 (defun gnus-compute-unseen-list ()
5524   ;; The `seen' marks are treated specially.
5525   (if (not gnus-newsgroup-seen)
5526       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5527     (setq gnus-newsgroup-unseen
5528           (gnus-inverse-list-range-intersection
5529            gnus-newsgroup-articles gnus-newsgroup-seen))))
5530
5531 (defun gnus-summary-display-make-predicate (display)
5532   (require 'gnus-agent)
5533   (when (= (length display) 1)
5534     (setq display (car display)))
5535   (unless gnus-summary-display-cache
5536     (dolist (elem (append '((unread . unread)
5537                             (read . read)
5538                             (unseen . unseen))
5539                           gnus-article-mark-lists))
5540       (push (cons (cdr elem)
5541                   (gnus-byte-compile
5542                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5543             gnus-summary-display-cache)))
5544   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5545         (gnus-category-predicate-cache gnus-summary-display-cache))
5546     (gnus-get-predicate display)))
5547
5548 ;; Uses the dynamically bound `number' variable.
5549 (eval-when-compile
5550   (defvar number))
5551 (defun gnus-article-marked-p (type &optional article)
5552   (let ((article (or article number)))
5553     (cond
5554      ((eq type 'tick)
5555       (memq article gnus-newsgroup-marked))
5556      ((eq type 'spam)
5557       (memq article gnus-newsgroup-spam-marked))
5558      ((eq type 'unsend)
5559       (memq article gnus-newsgroup-unsendable))
5560      ((eq type 'undownload)
5561       (memq article gnus-newsgroup-undownloaded))
5562      ((eq type 'download)
5563       (memq article gnus-newsgroup-downloadable))
5564      ((eq type 'unread)
5565       (memq article gnus-newsgroup-unreads))
5566      ((eq type 'read)
5567       (memq article gnus-newsgroup-reads))
5568      ((eq type 'dormant)
5569       (memq article gnus-newsgroup-dormant) )
5570      ((eq type 'expire)
5571       (memq article gnus-newsgroup-expirable))
5572      ((eq type 'reply)
5573       (memq article gnus-newsgroup-replied))
5574      ((eq type 'killed)
5575       (memq article gnus-newsgroup-killed))
5576      ((eq type 'bookmark)
5577       (assq article gnus-newsgroup-bookmarks))
5578      ((eq type 'score)
5579       (assq article gnus-newsgroup-scored))
5580      ((eq type 'save)
5581       (memq article gnus-newsgroup-saved))
5582      ((eq type 'cache)
5583       (memq article gnus-newsgroup-cached))
5584      ((eq type 'forward)
5585       (memq article gnus-newsgroup-forwarded))
5586      ((eq type 'seen)
5587       (not (memq article gnus-newsgroup-unseen)))
5588      ((eq type 'recent)
5589       (memq article gnus-newsgroup-recent))
5590      (t t))))
5591
5592 (defun gnus-articles-to-read (group &optional read-all)
5593   "Find out what articles the user wants to read."
5594   (let* ((articles
5595           ;; Select all articles if `read-all' is non-nil, or if there
5596           ;; are no unread articles.
5597           (if (or read-all
5598                   (and (zerop (length gnus-newsgroup-marked))
5599                        (zerop (length gnus-newsgroup-unreads)))
5600                   ;; Fetch all if the predicate is non-nil.
5601                   gnus-newsgroup-display)
5602               ;; We want to select the headers for all the articles in
5603               ;; the group, so we select either all the active
5604               ;; articles in the group, or (if that's nil), the
5605               ;; articles in the cache.
5606               (or
5607                (gnus-uncompress-range (gnus-active group))
5608                (gnus-cache-articles-in-group group))
5609             ;; Select only the "normal" subset of articles.
5610             (gnus-sorted-nunion
5611              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5612              gnus-newsgroup-unreads)))
5613          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5614          (scored (length scored-list))
5615          (number (length articles))
5616          (marked (+ (length gnus-newsgroup-marked)
5617                     (length gnus-newsgroup-dormant)))
5618          (select
5619           (cond
5620            ((numberp read-all)
5621             read-all)
5622            ((numberp gnus-newsgroup-display)
5623             gnus-newsgroup-display)
5624            (t
5625             (condition-case ()
5626                 (cond
5627                  ((and (or (<= scored marked) (= scored number))
5628                        (numberp gnus-large-newsgroup)
5629                        (> number gnus-large-newsgroup))
5630                   (let* ((cursor-in-echo-area nil)
5631                          (initial (gnus-parameter-large-newsgroup-initial
5632                                    gnus-newsgroup-name))
5633                          (input
5634                           (read-string
5635                            (format
5636                             "How many articles from %s (%s %d): "
5637                             (gnus-limit-string
5638                              (gnus-group-decoded-name gnus-newsgroup-name)
5639                              35)
5640                             (if initial "max" "default")
5641                             number)
5642                            (if initial
5643                                (cons (number-to-string initial)
5644                                      0)))))
5645                     (if (string-match "^[ \t]*$" input) number input)))
5646                  ((and (> scored marked) (< scored number)
5647                        (> (- scored number) 20))
5648                   (let ((input
5649                          (read-string
5650                           (format "%s %s (%d scored, %d total): "
5651                                   "How many articles from"
5652                                   (gnus-group-decoded-name group)
5653                                   scored number))))
5654                     (if (string-match "^[ \t]*$" input)
5655                         number input)))
5656                  (t number))
5657               (quit
5658                (message "Quit getting the articles to read")
5659                nil))))))
5660     (setq select (if (stringp select) (string-to-number select) select))
5661     (if (or (null select) (zerop select))
5662         select
5663       (if (and (not (zerop scored)) (<= (abs select) scored))
5664           (progn
5665             (setq articles (sort scored-list '<))
5666             (setq number (length articles)))
5667         (setq articles (copy-sequence articles)))
5668
5669       (when (< (abs select) number)
5670         (if (< select 0)
5671             ;; Select the N oldest articles.
5672             (setcdr (nthcdr (1- (abs select)) articles) nil)
5673           ;; Select the N most recent articles.
5674           (setq articles (nthcdr (- number select) articles))))
5675       (setq gnus-newsgroup-unselected
5676             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5677       (when gnus-alter-articles-to-read-function
5678         (setq articles
5679               (sort
5680                (funcall gnus-alter-articles-to-read-function
5681                         gnus-newsgroup-name articles)
5682                '<)))
5683       articles)))
5684
5685 (defun gnus-killed-articles (killed articles)
5686   (let (out)
5687     (while articles
5688       (when (inline (gnus-member-of-range (car articles) killed))
5689         (push (car articles) out))
5690       (setq articles (cdr articles)))
5691     out))
5692
5693 (defun gnus-uncompress-marks (marks)
5694   "Uncompress the mark ranges in MARKS."
5695   (let ((uncompressed '(score bookmark))
5696         out)
5697     (while marks
5698       (if (memq (caar marks) uncompressed)
5699           (push (car marks) out)
5700         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5701       (setq marks (cdr marks)))
5702     out))
5703
5704 (defun gnus-article-mark-to-type (mark)
5705   "Return the type of MARK."
5706   (or (cadr (assq mark gnus-article-special-mark-lists))
5707       'list))
5708
5709 (defun gnus-article-unpropagatable-p (mark)
5710   "Return whether MARK should be propagated to back end."
5711   (memq mark gnus-article-unpropagated-mark-lists))
5712
5713 (defun gnus-adjust-marked-articles (info)
5714   "Set all article lists and remove all marks that are no longer valid."
5715   (let* ((marked-lists (gnus-info-marks info))
5716          (active (gnus-active (gnus-info-group info)))
5717          (min (car active))
5718          (max (cdr active))
5719          (types gnus-article-mark-lists)
5720          marks var articles article mark mark-type
5721          bgn end)
5722
5723     (dolist (marks marked-lists)
5724       (setq mark (car marks)
5725             mark-type (gnus-article-mark-to-type mark)
5726             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5727
5728       ;; We set the variable according to the type of the marks list,
5729       ;; and then adjust the marks to a subset of the active articles.
5730       (cond
5731        ;; Adjust "simple" lists - compressed yet unsorted
5732        ((eq mark-type 'list)
5733         ;; Simultaneously uncompress and clip to active range
5734         ;; See gnus-uncompress-range for a description of possible marks
5735         (let (l lh)
5736           (if (not (cadr marks))
5737               (set var nil)
5738             (setq articles (if (numberp (cddr marks))
5739                                (list (cdr marks))
5740                              (cdr marks))
5741                   lh (cons nil nil)
5742                   l lh)
5743
5744             (while (setq article (pop articles))
5745               (cond ((consp article)
5746                      (setq bgn (max (car article) min)
5747                            end (min (cdr article) max))
5748                      (while (<= bgn end)
5749                        (setq l (setcdr l (cons bgn nil))
5750                              bgn (1+ bgn))))
5751                     ((and (<= min article)
5752                           (>= max article))
5753                      (setq l (setcdr l (cons article nil))))))
5754             (set var (cdr lh)))))
5755        ;; Adjust assocs.
5756        ((eq mark-type 'tuple)
5757         (set var (setq articles (cdr marks)))
5758         (when (not (listp (cdr (symbol-value var))))
5759           (set var (list (symbol-value var))))
5760         (when (not (listp (cdr articles)))
5761           (setq articles (list articles)))
5762         (while articles
5763           (when (or (not (consp (setq article (pop articles))))
5764                     (< (car article) min)
5765                     (> (car article) max))
5766             (set var (delq article (symbol-value var))))))
5767        ;; Adjust ranges (sloppily).
5768        ((eq mark-type 'range)
5769         (cond
5770          ((eq mark 'seen)
5771           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5772           ;; It should be (seen (NUM1 . NUM2)).
5773           (when (numberp (cddr marks))
5774             (setcdr marks (list (cdr marks))))
5775           (setq articles (cdr marks))
5776           (while (and articles
5777                       (or (and (consp (car articles))
5778                                (> min (cdar articles)))
5779                           (and (numberp (car articles))
5780                                (> min (car articles)))))
5781             (pop articles))
5782           (set var articles))))))))
5783
5784 (defun gnus-update-missing-marks (missing)
5785   "Go through the list of MISSING articles and remove them from the mark lists."
5786   (when missing
5787     (let (var m)
5788       ;; Go through all types.
5789       (dolist (elem gnus-article-mark-lists)
5790         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5791           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5792           (when (symbol-value var)
5793             ;; This list has articles.  So we delete all missing
5794             ;; articles from it.
5795             (setq m missing)
5796             (while m
5797               (set var (delq (pop m) (symbol-value var))))))))))
5798
5799 (defun gnus-update-marks ()
5800   "Enter the various lists of marked articles into the newsgroup info list."
5801   (let ((types gnus-article-mark-lists)
5802         (info (gnus-get-info gnus-newsgroup-name))
5803         type list newmarked symbol delta-marks)
5804     (when info
5805       ;; Add all marks lists to the list of marks lists.
5806       (while (setq type (pop types))
5807         (setq list (symbol-value
5808                     (setq symbol
5809                           (intern (format "gnus-newsgroup-%s" (car type))))))
5810
5811         (when list
5812           ;; Get rid of the entries of the articles that have the
5813           ;; default score.
5814           (when (and (eq (cdr type) 'score)
5815                      gnus-save-score
5816                      list)
5817             (let* ((arts list)
5818                    (prev (cons nil list))
5819                    (all prev))
5820               (while arts
5821                 (if (or (not (consp (car arts)))
5822                         (= (cdar arts) gnus-summary-default-score))
5823                     (setcdr prev (cdr arts))
5824                   (setq prev arts))
5825                 (setq arts (cdr arts)))
5826               (setq list (cdr all)))))
5827
5828         (when (eq (cdr type) 'seen)
5829           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5830
5831         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5832           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5833
5834         (when (and (gnus-check-backend-function
5835                     'request-set-mark gnus-newsgroup-name)
5836                    (not (gnus-article-unpropagatable-p (cdr type))))
5837           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5838                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5839                  (add (gnus-remove-from-range
5840                        (gnus-copy-sequence list) old)))
5841             (when add
5842               (push (list add 'add (list (cdr type))) delta-marks))
5843             (when del
5844               (push (list del 'del (list (cdr type))) delta-marks))))
5845
5846         (when list
5847           (push (cons (cdr type) list) newmarked)))
5848
5849       (when delta-marks
5850         (unless (gnus-check-group gnus-newsgroup-name)
5851           (error "Can't open server for %s" gnus-newsgroup-name))
5852         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5853
5854       ;; Enter these new marks into the info of the group.
5855       (if (nthcdr 3 info)
5856           (setcar (nthcdr 3 info) newmarked)
5857         ;; Add the marks lists to the end of the info.
5858         (when newmarked
5859           (setcdr (nthcdr 2 info) (list newmarked))))
5860
5861       ;; Cut off the end of the info if there's nothing else there.
5862       (let ((i 5))
5863         (while (and (> i 2)
5864                     (not (nth i info)))
5865           (when (nthcdr (decf i) info)
5866             (setcdr (nthcdr i info) nil)))))))
5867
5868 (defun gnus-set-mode-line (where)
5869   "Set the mode line of the article or summary buffers.
5870 If WHERE is `summary', the summary mode line format will be used."
5871   ;; Is this mode line one we keep updated?
5872   (when (and (memq where gnus-updated-mode-lines)
5873              (symbol-value
5874               (intern (format "gnus-%s-mode-line-format-spec" where))))
5875     (let (mode-string)
5876       (save-excursion
5877         ;; We evaluate this in the summary buffer since these
5878         ;; variables are buffer-local to that buffer.
5879         (set-buffer gnus-summary-buffer)
5880        ;; We bind all these variables that are used in the `eval' form
5881         ;; below.
5882         (let* ((mformat (symbol-value
5883                          (intern
5884                           (format "gnus-%s-mode-line-format-spec" where))))
5885                (gnus-tmp-group-name (gnus-mode-string-quote
5886                                      (gnus-group-decoded-name
5887                                       gnus-newsgroup-name)))
5888                (gnus-tmp-article-number (or gnus-current-article 0))
5889                (gnus-tmp-unread gnus-newsgroup-unreads)
5890                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5891                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5892                (gnus-tmp-unread-and-unselected
5893                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5894                             (zerop gnus-tmp-unselected))
5895                        "")
5896                       ((zerop gnus-tmp-unselected)
5897                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5898                       (t (format "{%d(+%d) more}"
5899                                  gnus-tmp-unread-and-unticked
5900                                  gnus-tmp-unselected))))
5901                (gnus-tmp-subject
5902                 (if (and gnus-current-headers
5903                          (vectorp gnus-current-headers))
5904                     (gnus-mode-string-quote
5905                      (mail-header-subject gnus-current-headers))
5906                   ""))
5907                bufname-length max-len
5908                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5909           (setq mode-string (eval mformat))
5910           (setq bufname-length (if (string-match "%b" mode-string)
5911                                    (- (length
5912                                        (buffer-name
5913                                         (if (eq where 'summary)
5914                                             nil
5915                                           (get-buffer gnus-article-buffer))))
5916                                       2)
5917                                  0))
5918           (setq max-len (max 4 (if gnus-mode-non-string-length
5919                                    (- (window-width)
5920                                       gnus-mode-non-string-length
5921                                       bufname-length)
5922                                  (length mode-string))))
5923           ;; We might have to chop a bit of the string off...
5924           (when (> (length mode-string) max-len)
5925             (setq mode-string
5926                   (concat (truncate-string-to-width mode-string (- max-len 3))
5927                           "...")))
5928           ;; Pad the mode string a bit.
5929           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5930       ;; Update the mode line.
5931       (setq mode-line-buffer-identification
5932             (gnus-mode-line-buffer-identification (list mode-string)))
5933       (set-buffer-modified-p t))))
5934
5935 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5936   "Go through the HEADERS list and add all Xrefs to a hash table.
5937 The resulting hash table is returned, or nil if no Xrefs were found."
5938   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5939          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5940          (xref-hashtb (gnus-make-hashtable))
5941          start group entry number xrefs header)
5942     (while headers
5943       (setq header (pop headers))
5944       (when (and (setq xrefs (mail-header-xref header))
5945                  (not (memq (setq number (mail-header-number header))
5946                             unreads)))
5947         (setq start 0)
5948         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5949           (setq start (match-end 0))
5950           (setq group (if prefix
5951                           (concat prefix (substring xrefs (match-beginning 1)
5952                                                     (match-end 1)))
5953                         (substring xrefs (match-beginning 1) (match-end 1))))
5954           (setq number
5955                 (string-to-number (substring xrefs (match-beginning 2)
5956                                           (match-end 2))))
5957           (if (setq entry (gnus-gethash group xref-hashtb))
5958               (setcdr entry (cons number (cdr entry)))
5959             (gnus-sethash group (cons number nil) xref-hashtb)))))
5960     (and start xref-hashtb)))
5961
5962 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5963   "Look through all the headers and mark the Xrefs as read."
5964   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5965         name info xref-hashtb idlist method nth4)
5966     (save-excursion
5967       (set-buffer gnus-group-buffer)
5968       (when (setq xref-hashtb
5969                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5970         (mapatoms
5971          (lambda (group)
5972            (unless (string= from-newsgroup (setq name (symbol-name group)))
5973              (setq idlist (symbol-value group))
5974              ;; Dead groups are not updated.
5975              (and (prog1
5976                       (setq info (gnus-get-info name))
5977                     (when (stringp (setq nth4 (gnus-info-method info)))
5978                       (setq nth4 (gnus-server-to-method nth4))))
5979                   ;; Only do the xrefs if the group has the same
5980                   ;; select method as the group we have just read.
5981                   (or (gnus-methods-equal-p
5982                        nth4 (gnus-find-method-for-group from-newsgroup))
5983                       virtual
5984                       (equal nth4 (setq method (gnus-find-method-for-group
5985                                                 from-newsgroup)))
5986                       (and (equal (car nth4) (car method))
5987                            (equal (nth 1 nth4) (nth 1 method))))
5988                   gnus-use-cross-reference
5989                   (or (not (eq gnus-use-cross-reference t))
5990                       virtual
5991                       ;; Only do cross-references on subscribed
5992                       ;; groups, if that is what is wanted.
5993                       (<= (gnus-info-level info) gnus-level-subscribed))
5994                   (gnus-group-make-articles-read name idlist))))
5995          xref-hashtb)))))
5996
5997 (defun gnus-compute-read-articles (group articles)
5998   (let* ((entry (gnus-group-entry group))
5999          (info (nth 2 entry))
6000          (active (gnus-active group))
6001          ninfo)
6002     (when entry
6003       ;; First peel off all invalid article numbers.
6004       (when active
6005         (let ((ids articles)
6006               id first)
6007           (while (setq id (pop ids))
6008             (when (and first (> id (cdr active)))
6009               ;; We'll end up in this situation in one particular
6010               ;; obscure situation.  If you re-scan a group and get
6011               ;; a new article that is cross-posted to a different
6012               ;; group that has not been re-scanned, you might get
6013               ;; crossposted article that has a higher number than
6014               ;; Gnus believes possible.  So we re-activate this
6015               ;; group as well.  This might mean doing the
6016               ;; crossposting thingy will *increase* the number
6017               ;; of articles in some groups.  Tsk, tsk.
6018               (setq active (or (gnus-activate-group group) active)))
6019             (when (or (> id (cdr active))
6020                       (< id (car active)))
6021               (setq articles (delq id articles))))))
6022       ;; If the read list is nil, we init it.
6023       (if (and active
6024                (null (gnus-info-read info))
6025                (> (car active) 1))
6026           (setq ninfo (cons 1 (1- (car active))))
6027         (setq ninfo (gnus-info-read info)))
6028       ;; Then we add the read articles to the range.
6029       (gnus-add-to-range
6030        ninfo (setq articles (sort articles '<))))))
6031
6032 (defun gnus-group-make-articles-read (group articles)
6033   "Update the info of GROUP to say that ARTICLES are read."
6034   (let* ((num 0)
6035          (entry (gnus-group-entry group))
6036          (info (nth 2 entry))
6037          (active (gnus-active group))
6038          range)
6039     (when entry
6040       (setq range (gnus-compute-read-articles group articles))
6041       (with-current-buffer gnus-group-buffer
6042         (gnus-undo-register
6043           `(progn
6044              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6045              (gnus-info-set-read ',info ',(gnus-info-read info))
6046              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6047              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6048              (gnus-group-update-group ,group t))))
6049       ;; Add the read articles to the range.
6050       (gnus-info-set-read info range)
6051       (gnus-request-set-mark group (list (list range 'add '(read))))
6052       ;; Then we have to re-compute how many unread
6053       ;; articles there are in this group.
6054       (when active
6055         (cond
6056          ((not range)
6057           (setq num (- (1+ (cdr active)) (car active))))
6058          ((not (listp (cdr range)))
6059           (setq num (- (cdr active) (- (1+ (cdr range))
6060                                        (car range)))))
6061          (t
6062           (while range
6063             (if (numberp (car range))
6064                 (setq num (1+ num))
6065               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6066             (setq range (cdr range)))
6067           (setq num (- (cdr active) num))))
6068         ;; Update the number of unread articles.
6069         (setcar entry num)
6070         ;; Update the group buffer.
6071         (unless (gnus-ephemeral-group-p group)
6072           (gnus-group-update-group group t))))))
6073
6074 (defvar gnus-newsgroup-none-id 0)
6075
6076 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6077   (let ((cur nntp-server-buffer)
6078         (dependencies
6079          (or dependencies
6080              (with-current-buffer gnus-summary-buffer
6081                gnus-newsgroup-dependencies)))
6082         headers id end ref number
6083         (mail-parse-charset gnus-newsgroup-charset)
6084         (mail-parse-ignored-charsets
6085          (save-excursion (condition-case nil
6086                              (set-buffer gnus-summary-buffer)
6087                            (error))
6088                          gnus-newsgroup-ignored-charsets)))
6089     (save-excursion
6090       (set-buffer nntp-server-buffer)
6091       ;; Translate all TAB characters into SPACE characters.
6092       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6093       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6094       (ietf-drums-unfold-fws)
6095       (gnus-run-hooks 'gnus-parse-headers-hook)
6096       (let ((case-fold-search t)
6097             in-reply-to header p lines chars)
6098         (goto-char (point-min))
6099         ;; Search to the beginning of the next header.  Error messages
6100         ;; do not begin with 2 or 3.
6101         (while (re-search-forward "^[23][0-9]+ " nil t)
6102           (setq id nil
6103                 ref nil)
6104           ;; This implementation of this function, with nine
6105           ;; search-forwards instead of the one re-search-forward and
6106           ;; a case (which basically was the old function) is actually
6107           ;; about twice as fast, even though it looks messier.  You
6108           ;; can't have everything, I guess.  Speed and elegance
6109           ;; doesn't always go hand in hand.
6110           (setq
6111            header
6112            (vector
6113             ;; Number.
6114             (prog1
6115                 (setq number (read cur))
6116               (end-of-line)
6117               (setq p (point))
6118               (narrow-to-region (point)
6119                                 (or (and (search-forward "\n.\n" nil t)
6120                                          (- (point) 2))
6121                                     (point))))
6122             ;; Subject.
6123             (progn
6124               (goto-char p)
6125               (if (search-forward "\nsubject:" nil t)
6126                   (funcall gnus-decode-encoded-word-function
6127                            (nnheader-header-value))
6128                 "(none)"))
6129             ;; From.
6130             (progn
6131               (goto-char p)
6132               (if (search-forward "\nfrom:" nil t)
6133                   (funcall gnus-decode-encoded-address-function
6134                            (nnheader-header-value))
6135                 "(nobody)"))
6136             ;; Date.
6137             (progn
6138               (goto-char p)
6139               (if (search-forward "\ndate:" nil t)
6140                   (nnheader-header-value) ""))
6141             ;; Message-ID.
6142             (progn
6143               (goto-char p)
6144               (setq id (if (re-search-forward
6145                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6146                            ;; We do it this way to make sure the Message-ID
6147                            ;; is (somewhat) syntactically valid.
6148                            (buffer-substring (match-beginning 1)
6149                                              (match-end 1))
6150                          ;; If there was no message-id, we just fake one
6151                          ;; to make subsequent routines simpler.
6152                          (nnheader-generate-fake-message-id number))))
6153             ;; References.
6154             (progn
6155               (goto-char p)
6156               (if (search-forward "\nreferences:" nil t)
6157                   (progn
6158                     (setq end (point))
6159                     (prog1
6160                         (nnheader-header-value)
6161                       (setq ref
6162                             (buffer-substring
6163                              (progn
6164                                (end-of-line)
6165                                (search-backward ">" end t)
6166                                (1+ (point)))
6167                              (progn
6168                                (search-backward "<" end t)
6169                                (point))))))
6170                 ;; Get the references from the in-reply-to header if there
6171                 ;; were no references and the in-reply-to header looks
6172                 ;; promising.
6173                 (if (and (search-forward "\nin-reply-to:" nil t)
6174                          (setq in-reply-to (nnheader-header-value))
6175                          (string-match "<[^>]+>" in-reply-to))
6176                     (let (ref2)
6177                       (setq ref (substring in-reply-to (match-beginning 0)
6178                                            (match-end 0)))
6179                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6180                         (setq ref2 (substring in-reply-to (match-beginning 0)
6181                                               (match-end 0)))
6182                         (when (> (length ref2) (length ref))
6183                           (setq ref ref2)))
6184                       ref)
6185                   (setq ref nil))))
6186             ;; Chars.
6187             (progn
6188               (goto-char p)
6189               (if (search-forward "\nchars: " nil t)
6190                   (if (numberp (setq chars (ignore-errors (read cur))))
6191                       chars -1)
6192                 -1))
6193             ;; Lines.
6194             (progn
6195               (goto-char p)
6196               (if (search-forward "\nlines: " nil t)
6197                   (if (numberp (setq lines (ignore-errors (read cur))))
6198                       lines -1)
6199                 -1))
6200             ;; Xref.
6201             (progn
6202               (goto-char p)
6203               (and (search-forward "\nxref:" nil t)
6204                    (nnheader-header-value)))
6205             ;; Extra.
6206             (when gnus-extra-headers
6207               (let ((extra gnus-extra-headers)
6208                     out)
6209                 (while extra
6210                   (goto-char p)
6211                   (when (search-forward
6212                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6213                     (push (cons (car extra) (nnheader-header-value))
6214                           out))
6215                   (pop extra))
6216                 out))))
6217           (when (equal id ref)
6218             (setq ref nil))
6219
6220           (when gnus-alter-header-function
6221             (funcall gnus-alter-header-function header)
6222             (setq id (mail-header-id header)
6223                   ref (gnus-parent-id (mail-header-references header))))
6224
6225           (when (setq header
6226                       (gnus-dependencies-add-header
6227                        header dependencies force-new))
6228             (push header headers))
6229           (goto-char (point-max))
6230           (widen))
6231         (nreverse headers)))))
6232
6233 ;; Goes through the xover lines and returns a list of vectors
6234 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6235                                                   force-new dependencies
6236                                                   group also-fetch-heads)
6237   "Parse the news overview data in the server buffer.
6238 Return a list of headers that match SEQUENCE (see
6239 `nntp-retrieve-headers')."
6240   ;; Get the Xref when the users reads the articles since most/some
6241   ;; NNTP servers do not include Xrefs when using XOVER.
6242   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6243   (let ((mail-parse-charset gnus-newsgroup-charset)
6244         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6245         (cur nntp-server-buffer)
6246         (dependencies (or dependencies gnus-newsgroup-dependencies))
6247         (allp (cond
6248                ((eq gnus-read-all-available-headers t)
6249                 t)
6250                ((and (stringp gnus-read-all-available-headers)
6251                      group)
6252                 (string-match gnus-read-all-available-headers group))
6253                (t
6254                 nil)))
6255         number headers header)
6256     (save-excursion
6257       (set-buffer nntp-server-buffer)
6258       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6259       ;; Allow the user to mangle the headers before parsing them.
6260       (gnus-run-hooks 'gnus-parse-headers-hook)
6261       (goto-char (point-min))
6262       (gnus-parse-without-error
6263         (while (and (or sequence allp)
6264                     (not (eobp)))
6265           (setq number (read cur))
6266           (when (not allp)
6267             (while (and sequence
6268                         (< (car sequence) number))
6269               (setq sequence (cdr sequence))))
6270           (when (and (or allp
6271                          (and sequence
6272                               (eq number (car sequence))))
6273                      (progn
6274                        (setq sequence (cdr sequence))
6275                        (setq header (inline
6276                                       (gnus-nov-parse-line
6277                                        number dependencies force-new)))))
6278             (push header headers))
6279           (forward-line 1)))
6280       ;; A common bug in inn is that if you have posted an article and
6281       ;; then retrieves the active file, it will answer correctly --
6282       ;; the new article is included.  However, a NOV entry for the
6283       ;; article may not have been generated yet, so this may fail.
6284       ;; We work around this problem by retrieving the last few
6285       ;; headers using HEAD.
6286       (if (or (not also-fetch-heads)
6287               (not sequence))
6288           ;; We (probably) got all the headers.
6289           (nreverse headers)
6290         (let ((gnus-nov-is-evil t))
6291           (nconc
6292            (nreverse headers)
6293            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6294              (gnus-get-newsgroup-headers))))))))
6295
6296 (defun gnus-article-get-xrefs ()
6297   "Fill in the Xref value in `gnus-current-headers', if necessary.
6298 This is meant to be called in `gnus-article-internal-prepare-hook'."
6299   (let ((headers (with-current-buffer gnus-summary-buffer
6300                    gnus-current-headers)))
6301     (or (not gnus-use-cross-reference)
6302         (not headers)
6303         (and (mail-header-xref headers)
6304              (not (string= (mail-header-xref headers) "")))
6305         (let ((case-fold-search t)
6306               xref)
6307           (save-restriction
6308             (nnheader-narrow-to-headers)
6309             (goto-char (point-min))
6310             (when (or (and (not (eobp))
6311                            (eq (downcase (char-after)) ?x)
6312                            (looking-at "Xref:"))
6313                       (search-forward "\nXref:" nil t))
6314               (goto-char (1+ (match-end 0)))
6315               (setq xref (buffer-substring (point) (point-at-eol)))
6316               (mail-header-set-xref headers xref)))))))
6317
6318 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6319   "Find article ID and insert the summary line for that article.
6320 OLD-HEADER can either be a header or a line number to insert
6321 the subject line on."
6322   (let* ((line (and (numberp old-header) old-header))
6323          (old-header (and (vectorp old-header) old-header))
6324          (header (cond ((and old-header use-old-header)
6325                         old-header)
6326                        ((and (numberp id)
6327                              (gnus-number-to-header id))
6328                         (gnus-number-to-header id))
6329                        (t
6330                         (gnus-read-header id))))
6331          (number (and (numberp id) id))
6332          d)
6333     (when header
6334       ;; Rebuild the thread that this article is part of and go to the
6335       ;; article we have fetched.
6336       (when (and (not gnus-show-threads)
6337                  old-header)
6338         (when (and number
6339                    (setq d (gnus-data-find (mail-header-number old-header))))
6340           (goto-char (gnus-data-pos d))
6341           (gnus-data-remove
6342            number
6343            (- (point-at-bol)
6344               (prog1
6345                   (1+ (point-at-eol))
6346                 (gnus-delete-line))))))
6347       ;; Remove list identifiers from subject.
6348       (when gnus-list-identifiers
6349         (let ((gnus-newsgroup-headers (list header)))
6350           (gnus-summary-remove-list-identifiers)))
6351       (when old-header
6352         (mail-header-set-number header (mail-header-number old-header)))
6353       (setq gnus-newsgroup-sparse
6354             (delq (setq number (mail-header-number header))
6355                   gnus-newsgroup-sparse))
6356       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6357       (push number gnus-newsgroup-limit)
6358       (gnus-rebuild-thread (mail-header-id header) line)
6359       (gnus-summary-goto-subject number nil t))
6360     (when (and (numberp number)
6361                (> number 0))
6362       ;; We have to update the boundaries even if we can't fetch the
6363       ;; article if ID is a number -- so that the next `P' or `N'
6364       ;; command will fetch the previous (or next) article even
6365       ;; if the one we tried to fetch this time has been canceled.
6366       (when (> number gnus-newsgroup-end)
6367         (setq gnus-newsgroup-end number))
6368       (when (< number gnus-newsgroup-begin)
6369         (setq gnus-newsgroup-begin number))
6370       (setq gnus-newsgroup-unselected
6371             (delq number gnus-newsgroup-unselected)))
6372     ;; Report back a success?
6373     (and header (mail-header-number header))))
6374
6375 ;;; Process/prefix in the summary buffer
6376
6377 (defun gnus-summary-work-articles (n)
6378   "Return a list of articles to be worked upon.
6379 The prefix argument, the list of process marked articles, and the
6380 current article will be taken into consideration."
6381   (save-excursion
6382     (set-buffer gnus-summary-buffer)
6383     (cond
6384      (n
6385       ;; A numerical prefix has been given.
6386       (setq n (prefix-numeric-value n))
6387       (let ((backward (< n 0))
6388             (n (abs (prefix-numeric-value n)))
6389             articles article)
6390         (save-excursion
6391           (while
6392               (and (> n 0)
6393                    (push (setq article (gnus-summary-article-number))
6394                          articles)
6395                    (if backward
6396                        (gnus-summary-find-prev nil article)
6397                      (gnus-summary-find-next nil article)))
6398             (decf n)))
6399         (nreverse articles)))
6400      ((and (gnus-region-active-p) (mark))
6401       (message "region active")
6402       ;; Work on the region between point and mark.
6403       (let ((max (max (point) (mark)))
6404             articles article)
6405         (save-excursion
6406           (goto-char (min (point) (mark)))
6407           (while
6408               (and
6409                (push (setq article (gnus-summary-article-number)) articles)
6410                (gnus-summary-find-next nil article)
6411                (< (point) max)))
6412           (nreverse articles))))
6413      (gnus-newsgroup-processable
6414       ;; There are process-marked articles present.
6415       ;; Save current state.
6416       (gnus-summary-save-process-mark)
6417       ;; Return the list.
6418       (reverse gnus-newsgroup-processable))
6419      (t
6420       ;; Just return the current article.
6421       (list (gnus-summary-article-number))))))
6422
6423 (defmacro gnus-summary-iterate (arg &rest forms)
6424   "Iterate over the process/prefixed articles and do FORMS.
6425 ARG is the interactive prefix given to the command.  FORMS will be
6426 executed with point over the summary line of the articles."
6427   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6428     `(let ((,articles (gnus-summary-work-articles ,arg)))
6429        (while ,articles
6430          (gnus-summary-goto-subject (car ,articles))
6431          ,@forms
6432          (pop ,articles)))))
6433
6434 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6435 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6436
6437 (defun gnus-summary-save-process-mark ()
6438   "Push the current set of process marked articles on the stack."
6439   (interactive)
6440   (push (copy-sequence gnus-newsgroup-processable)
6441         gnus-newsgroup-process-stack))
6442
6443 (defun gnus-summary-kill-process-mark ()
6444   "Push the current set of process marked articles on the stack and unmark."
6445   (interactive)
6446   (gnus-summary-save-process-mark)
6447   (gnus-summary-unmark-all-processable))
6448
6449 (defun gnus-summary-yank-process-mark ()
6450   "Pop the last process mark state off the stack and restore it."
6451   (interactive)
6452   (unless gnus-newsgroup-process-stack
6453     (error "Empty mark stack"))
6454   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6455
6456 (defun gnus-summary-process-mark-set (set)
6457   "Make SET into the current process marked articles."
6458   (gnus-summary-unmark-all-processable)
6459   (mapc 'gnus-summary-set-process-mark set))
6460
6461 ;;; Searching and stuff
6462
6463 (defun gnus-summary-search-group (&optional backward use-level)
6464   "Search for next unread newsgroup.
6465 If optional argument BACKWARD is non-nil, search backward instead."
6466   (save-excursion
6467     (set-buffer gnus-group-buffer)
6468     (when (gnus-group-search-forward
6469            backward nil (if use-level (gnus-group-group-level) nil))
6470       (gnus-group-group-name))))
6471
6472 (defun gnus-summary-best-group (&optional exclude-group)
6473   "Find the name of the best unread group.
6474 If EXCLUDE-GROUP, do not go to this group."
6475   (with-current-buffer gnus-group-buffer
6476     (save-excursion
6477       (gnus-group-best-unread-group exclude-group))))
6478
6479 (defun gnus-summary-find-next (&optional unread article backward)
6480   (if backward
6481       (gnus-summary-find-prev unread article)
6482     (let* ((dummy (gnus-summary-article-intangible-p))
6483            (article (or article (gnus-summary-article-number)))
6484            (data (gnus-data-find-list article))
6485            result)
6486       (when (and (not dummy)
6487                  (or (not gnus-summary-check-current)
6488                      (not unread)
6489                      (not (gnus-data-unread-p (car data)))))
6490         (setq data (cdr data)))
6491       (when (setq result
6492                   (if unread
6493                       (progn
6494                         (while data
6495                           (unless (memq (gnus-data-number (car data))
6496                                         (cond
6497                                          ((eq gnus-auto-goto-ignores
6498                                               'always-undownloaded)
6499                                           gnus-newsgroup-undownloaded)
6500                                          (gnus-plugged
6501                                           nil)
6502                                          ((eq gnus-auto-goto-ignores
6503                                               'unfetched)
6504                                           gnus-newsgroup-unfetched)
6505                                          ((eq gnus-auto-goto-ignores
6506                                               'undownloaded)
6507                                           gnus-newsgroup-undownloaded)))
6508                             (when (gnus-data-unread-p (car data))
6509                               (setq result (car data)
6510                                     data nil)))
6511                           (setq data (cdr data)))
6512                         result)
6513                     (car data)))
6514         (goto-char (gnus-data-pos result))
6515         (gnus-data-number result)))))
6516
6517 (defun gnus-summary-find-prev (&optional unread article)
6518   (let* ((eobp (eobp))
6519          (article (or article (gnus-summary-article-number)))
6520          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6521          result)
6522     (when (and (not eobp)
6523                (or (not gnus-summary-check-current)
6524                    (not unread)
6525                    (not (gnus-data-unread-p (car data)))))
6526       (setq data (cdr data)))
6527     (when (setq result
6528                 (if unread
6529                     (progn
6530                       (while data
6531                         (unless (memq (gnus-data-number (car data))
6532                                       (cond
6533                                        ((eq gnus-auto-goto-ignores
6534                                             'always-undownloaded)
6535                                         gnus-newsgroup-undownloaded)
6536                                        (gnus-plugged
6537                                         nil)
6538                                        ((eq gnus-auto-goto-ignores
6539                                             'unfetched)
6540                                         gnus-newsgroup-unfetched)
6541                                        ((eq gnus-auto-goto-ignores
6542                                             'undownloaded)
6543                                         gnus-newsgroup-undownloaded)))
6544                           (when (gnus-data-unread-p (car data))
6545                             (setq result (car data)
6546                                   data nil)))
6547                         (setq data (cdr data)))
6548                       result)
6549                   (car data)))
6550       (goto-char (gnus-data-pos result))
6551       (gnus-data-number result))))
6552
6553 (defun gnus-summary-find-subject (subject &optional unread backward article)
6554   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6555          (article (or article (gnus-summary-article-number)))
6556          (articles (gnus-data-list backward))
6557          (arts (gnus-data-find-list article articles))
6558          result)
6559     (when (or (not gnus-summary-check-current)
6560               (not unread)
6561               (not (gnus-data-unread-p (car arts))))
6562       (setq arts (cdr arts)))
6563     (while arts
6564       (and (or (not unread)
6565                (gnus-data-unread-p (car arts)))
6566            (vectorp (gnus-data-header (car arts)))
6567            (gnus-subject-equal
6568             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6569            (setq result (car arts)
6570                  arts nil))
6571       (setq arts (cdr arts)))
6572     (and result
6573          (goto-char (gnus-data-pos result))
6574          (gnus-data-number result))))
6575
6576 (defun gnus-summary-search-forward (&optional unread subject backward)
6577   "Search forward for an article.
6578 If UNREAD, look for unread articles.  If SUBJECT, look for
6579 articles with that subject.  If BACKWARD, search backward instead."
6580   (cond (subject (gnus-summary-find-subject subject unread backward))
6581         (backward (gnus-summary-find-prev unread))
6582         (t (gnus-summary-find-next unread))))
6583
6584 (defun gnus-recenter (&optional n)
6585   "Center point in window and redisplay frame.
6586 Also do horizontal recentering."
6587   (interactive "P")
6588   (when (and gnus-auto-center-summary
6589              (not (eq gnus-auto-center-summary 'vertical)))
6590     (gnus-horizontal-recenter))
6591   (recenter n))
6592
6593 (defun gnus-summary-recenter ()
6594   "Center point in the summary window.
6595 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6596 displayed, no centering will be performed."
6597   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6598   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6599   (interactive)
6600   ;; The user has to want it.
6601   (when gnus-auto-center-summary
6602     (let* ((top (cond ((< (window-height) 4) 0)
6603                       ((< (window-height) 7) 1)
6604                       (t (if (numberp gnus-auto-center-summary)
6605                              gnus-auto-center-summary
6606                            (/ (1- (window-height)) 2)))))
6607            (height (1- (window-height)))
6608            (bottom (save-excursion (goto-char (point-max))
6609                                    (forward-line (- height))
6610                                    (point)))
6611            (window (get-buffer-window (current-buffer))))
6612       (when (get-buffer-window gnus-article-buffer)
6613         ;; Only do recentering when the article buffer is displayed,
6614         ;; Set the window start to either `bottom', which is the biggest
6615         ;; possible valid number, or the second line from the top,
6616         ;; whichever is the least.
6617         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6618           (if (> bottom top-pos)
6619               ;; Keep the second line from the top visible
6620               (set-window-start window top-pos)
6621             ;; Try to keep the bottom line visible; if it's partially
6622             ;; obscured, either scroll one more line to make it fully
6623             ;; visible, or revert to using TOP-POS.
6624             (save-excursion
6625               (goto-char (point-max))
6626               (forward-line -1)
6627               (let ((last-line-start (point)))
6628                 (goto-char bottom)
6629                 (set-window-start window (point) t)
6630                 (when (not (pos-visible-in-window-p last-line-start window))
6631                   (forward-line 1)
6632                   (set-window-start window (min (point) top-pos) t)))))))
6633       ;; Do horizontal recentering while we're at it.
6634       (when (and (get-buffer-window (current-buffer) t)
6635                  (not (eq gnus-auto-center-summary 'vertical)))
6636         (let ((selected (selected-window)))
6637           (select-window (get-buffer-window (current-buffer) t))
6638           (gnus-summary-position-point)
6639           (gnus-horizontal-recenter)
6640           (select-window selected))))))
6641
6642 (defun gnus-summary-jump-to-group (newsgroup)
6643   "Move point to NEWSGROUP in group mode buffer."
6644   ;; Keep update point of group mode buffer if visible.
6645   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6646       (save-window-excursion
6647         ;; Take care of tree window mode.
6648         (when (get-buffer-window gnus-group-buffer)
6649           (pop-to-buffer gnus-group-buffer))
6650         (gnus-group-jump-to-group newsgroup))
6651     (save-excursion
6652       ;; Take care of tree window mode.
6653       (if (get-buffer-window gnus-group-buffer)
6654           (pop-to-buffer gnus-group-buffer)
6655         (set-buffer gnus-group-buffer))
6656       (gnus-group-jump-to-group newsgroup))))
6657
6658 ;; This function returns a list of article numbers based on the
6659 ;; difference between the ranges of read articles in this group and
6660 ;; the range of active articles.
6661 (defun gnus-list-of-unread-articles (group)
6662   (let* ((read (gnus-info-read (gnus-get-info group)))
6663          (active (or (gnus-active group) (gnus-activate-group group)))
6664          (last (or (cdr active)
6665                    (error "Group %s couldn't be activated " group)))
6666          first nlast unread)
6667     ;; If none are read, then all are unread.
6668     (if (not read)
6669         (setq first (car active))
6670       ;; If the range of read articles is a single range, then the
6671       ;; first unread article is the article after the last read
6672       ;; article.  Sounds logical, doesn't it?
6673       (if (and (not (listp (cdr read)))
6674                (or (< (car read) (car active))
6675                    (progn (setq read (list read))
6676                           nil)))
6677           (setq first (max (car active) (1+ (cdr read))))
6678         ;; `read' is a list of ranges.
6679         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6680                                   (caar read)))
6681                   1)
6682           (setq first (car active)))
6683         (while read
6684           (when first
6685             (while (< first nlast)
6686               (setq unread (cons first unread)
6687                     first (1+ first))))
6688           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6689           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6690           (setq read (cdr read)))))
6691     ;; And add the last unread articles.
6692     (while (<= first last)
6693       (setq unread (cons first unread)
6694             first (1+ first)))
6695     ;; Return the list of unread articles.
6696     (delq 0 (nreverse unread))))
6697
6698 (defun gnus-list-of-read-articles (group)
6699   "Return a list of unread, unticked and non-dormant articles."
6700   (let* ((info (gnus-get-info group))
6701          (marked (gnus-info-marks info))
6702          (active (gnus-active group)))
6703     (and info active
6704          (gnus-list-range-difference
6705           (gnus-list-range-difference
6706            (gnus-sorted-complement
6707             (gnus-uncompress-range active)
6708             (gnus-list-of-unread-articles group))
6709            (cdr (assq 'dormant marked)))
6710           (cdr (assq 'tick marked))))))
6711
6712 ;; This function returns a sequence of article numbers based on the
6713 ;; difference between the ranges of read articles in this group and
6714 ;; the range of active articles.
6715 (defun gnus-sequence-of-unread-articles (group)
6716   (let* ((read (gnus-info-read (gnus-get-info group)))
6717          (active (or (gnus-active group) (gnus-activate-group group)))
6718          (last (cdr active))
6719          first nlast unread)
6720     ;; If none are read, then all are unread.
6721     (if (not read)
6722         (setq first (car active))
6723       ;; If the range of read articles is a single range, then the
6724       ;; first unread article is the article after the last read
6725       ;; article.  Sounds logical, doesn't it?
6726       (if (and (not (listp (cdr read)))
6727                (or (< (car read) (car active))
6728                    (progn (setq read (list read))
6729                           nil)))
6730           (setq first (max (car active) (1+ (cdr read))))
6731         ;; `read' is a list of ranges.
6732         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6733                                   (caar read)))
6734                   1)
6735           (setq first (car active)))
6736         (while read
6737           (when first
6738             (push (cons first nlast) unread))
6739           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6740           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6741           (setq read (cdr read)))))
6742     ;; And add the last unread articles.
6743     (cond ((not (and first last))
6744            nil)
6745           ((< first last)
6746            (push (cons first last) unread))
6747           ((= first last)
6748            (push first unread)))
6749     ;; Return the sequence of unread articles.
6750     (delq 0 (nreverse unread))))
6751
6752 ;; Various summary commands
6753
6754 (defun gnus-summary-select-article-buffer ()
6755   "Reconfigure windows to show article buffer."
6756   (interactive)
6757   (if (not (gnus-buffer-live-p gnus-article-buffer))
6758       (error "There is no article buffer for this summary buffer")
6759     (gnus-configure-windows 'article)
6760     (select-window (get-buffer-window gnus-article-buffer))))
6761
6762 (defun gnus-summary-universal-argument (arg)
6763   "Perform any operation on all articles that are process/prefixed."
6764   (interactive "P")
6765   (let ((articles (gnus-summary-work-articles arg))
6766         func article)
6767     (if (eq
6768          (setq
6769           func
6770           (key-binding
6771            (read-key-sequence
6772             (substitute-command-keys
6773              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6774          'undefined)
6775         (gnus-error 1 "Undefined key")
6776       (save-excursion
6777         (while articles
6778           (gnus-summary-goto-subject (setq article (pop articles)))
6779           (let (gnus-newsgroup-processable)
6780             (command-execute func))
6781           (gnus-summary-remove-process-mark article)))))
6782   (gnus-summary-position-point))
6783
6784 (defun gnus-summary-toggle-truncation (&optional arg)
6785   "Toggle truncation of summary lines.
6786 With ARG, turn line truncation on if ARG is positive."
6787   (interactive "P")
6788   (setq truncate-lines
6789         (if (null arg) (not truncate-lines)
6790           (> (prefix-numeric-value arg) 0)))
6791   (redraw-display))
6792
6793 (defun gnus-summary-find-for-reselect ()
6794   "Return the number of an article to stay on across a reselect.
6795 The current article is considered, then following articles, then previous
6796 articles.  An article is sought which is not cancelled and isn't a temporary
6797 insertion from another group.  If there's no such then return a dummy 0."
6798   (let (found)
6799     (dolist (rev '(nil t))
6800       (unless found      ; don't demand the reverse list if we don't need it
6801         (let ((data (gnus-data-find-list
6802                      (gnus-summary-article-number) (gnus-data-list rev))))
6803           (while (and data (not found))
6804             (if (and (< 0 (gnus-data-number (car data)))
6805                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6806                 (setq found (gnus-data-number (car data))))
6807             (setq data (cdr data))))))
6808     (or found 0)))
6809
6810 (defun gnus-summary-reselect-current-group (&optional all rescan)
6811   "Exit and then reselect the current newsgroup.
6812 The prefix argument ALL means to select all articles."
6813   (interactive "P")
6814   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6815     (error "Ephemeral groups can't be reselected"))
6816   (let ((current-subject (gnus-summary-find-for-reselect))
6817         (group gnus-newsgroup-name))
6818     (setq gnus-newsgroup-begin nil)
6819     (gnus-summary-exit nil 'leave-hidden)
6820     ;; We have to adjust the point of group mode buffer because
6821     ;; point was moved to the next unread newsgroup by exiting.
6822     (gnus-summary-jump-to-group group)
6823     (when rescan
6824       (save-excursion
6825         (gnus-group-get-new-news-this-group 1)))
6826     (gnus-group-read-group all t)
6827     (gnus-summary-goto-subject current-subject nil t)))
6828
6829 (defun gnus-summary-rescan-group (&optional all)
6830   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6831   (interactive "P")
6832   (gnus-summary-reselect-current-group all t))
6833
6834 (defun gnus-summary-update-info (&optional non-destructive)
6835   (save-excursion
6836     (let ((group gnus-newsgroup-name))
6837       (when group
6838         (when gnus-newsgroup-kill-headers
6839           (setq gnus-newsgroup-killed
6840                 (gnus-compress-sequence
6841                  (gnus-sorted-union
6842                   (gnus-list-range-intersection
6843                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6844                   gnus-newsgroup-unreads)
6845                  t)))
6846         (unless (listp (cdr gnus-newsgroup-killed))
6847           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6848         (let ((headers gnus-newsgroup-headers))
6849           ;; Set the new ranges of read articles.
6850           (with-current-buffer gnus-group-buffer
6851             (gnus-undo-force-boundary))
6852           (gnus-update-read-articles
6853            group (gnus-sorted-union
6854                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6855           ;; Set the current article marks.
6856           (let ((gnus-newsgroup-scored
6857                  (if (and (not gnus-save-score)
6858                           (not non-destructive))
6859                      nil
6860                    gnus-newsgroup-scored)))
6861             (save-excursion
6862               (gnus-update-marks)))
6863           ;; Do the cross-ref thing.
6864           (when gnus-use-cross-reference
6865             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6866           ;; Do not switch windows but change the buffer to work.
6867           (set-buffer gnus-group-buffer)
6868           (unless (gnus-ephemeral-group-p group)
6869             (gnus-group-update-group group)))))))
6870
6871 (defun gnus-summary-save-newsrc (&optional force)
6872   "Save the current number of read/marked articles in the dribble buffer.
6873 The dribble buffer will then be saved.
6874 If FORCE (the prefix), also save the .newsrc file(s)."
6875   (interactive "P")
6876   (gnus-summary-update-info t)
6877   (if force
6878       (gnus-save-newsrc-file)
6879     (gnus-dribble-save)))
6880
6881 (defun gnus-summary-exit (&optional temporary leave-hidden)
6882   "Exit reading current newsgroup, and then return to group selection mode.
6883 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6884   (interactive)
6885   (gnus-set-global-variables)
6886   (when (gnus-buffer-live-p gnus-article-buffer)
6887     (save-excursion
6888       (set-buffer gnus-article-buffer)
6889       (mm-destroy-parts gnus-article-mime-handles)
6890       ;; Set it to nil for safety reason.
6891       (setq gnus-article-mime-handle-alist nil)
6892       (setq gnus-article-mime-handles nil)))
6893   (gnus-kill-save-kill-buffer)
6894   (gnus-async-halt-prefetch)
6895   (let* ((group gnus-newsgroup-name)
6896          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6897          (gnus-group-is-exiting-p t)
6898          (mode major-mode)
6899          (group-point nil)
6900          (buf (current-buffer)))
6901     (unless quit-config
6902       ;; Do adaptive scoring, and possibly save score files.
6903       (when gnus-newsgroup-adaptive
6904         (gnus-score-adaptive))
6905       (when gnus-use-scoring
6906         (gnus-score-save)))
6907     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6908     ;; If we have several article buffers, we kill them at exit.
6909     (unless gnus-single-article-buffer
6910       (gnus-kill-buffer gnus-article-buffer)
6911       (gnus-kill-buffer gnus-original-article-buffer)
6912       (setq gnus-article-current nil))
6913     (when gnus-use-cache
6914       (gnus-cache-possibly-remove-articles)
6915       (gnus-cache-save-buffers))
6916     (gnus-async-prefetch-remove-group group)
6917     (when gnus-suppress-duplicates
6918       (gnus-dup-enter-articles))
6919     (when gnus-use-trees
6920       (gnus-tree-close group))
6921     (when gnus-use-cache
6922       (gnus-cache-write-active))
6923     ;; Remove entries for this group.
6924     (nnmail-purge-split-history (gnus-group-real-name group))
6925     ;; Make all changes in this group permanent.
6926     (unless quit-config
6927       (gnus-run-hooks 'gnus-exit-group-hook)
6928       (gnus-summary-update-info))
6929     (gnus-close-group group)
6930     ;; Make sure where we were, and go to next newsgroup.
6931     (set-buffer gnus-group-buffer)
6932     (unless quit-config
6933       (gnus-group-jump-to-group group))
6934     (gnus-run-hooks 'gnus-summary-exit-hook)
6935     (unless (or quit-config
6936                 ;; If this group has disappeared from the summary
6937                 ;; buffer, don't skip forwards.
6938                 (not (string= group (gnus-group-group-name))))
6939       (gnus-group-next-unread-group 1))
6940     (setq group-point (point))
6941     (if temporary
6942         nil                             ;Nothing to do.
6943       ;; If we have several article buffers, we kill them at exit.
6944       (unless gnus-single-article-buffer
6945         (gnus-kill-buffer gnus-article-buffer)
6946         (gnus-kill-buffer gnus-original-article-buffer)
6947         (setq gnus-article-current nil))
6948       (set-buffer buf)
6949       (if (not gnus-kill-summary-on-exit)
6950           (progn
6951             (gnus-deaden-summary)
6952             (setq mode nil))
6953         ;; We set all buffer-local variables to nil.  It is unclear why
6954         ;; this is needed, but if we don't, buffer-local variables are
6955         ;; not garbage-collected, it seems.  This would the lead to en
6956         ;; ever-growing Emacs.
6957         (gnus-summary-clear-local-variables)
6958         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6959           (gnus-summary-clear-local-variables))
6960         (when (get-buffer gnus-article-buffer)
6961           (bury-buffer gnus-article-buffer))
6962         ;; Return to group mode buffer.
6963         (when (eq mode 'gnus-summary-mode)
6964           (gnus-kill-buffer buf)))
6965       (setq gnus-current-select-method gnus-select-method)
6966       (set-buffer gnus-group-buffer)
6967       (if quit-config
6968           (gnus-handle-ephemeral-exit quit-config)
6969         (goto-char group-point)
6970         ;; If gnus-group-buffer is already displayed, make sure we also move
6971         ;; the cursor in the window that displays it.
6972         (let ((win (get-buffer-window (current-buffer) 0)))
6973           (if win (set-window-point win (point))))
6974         (unless leave-hidden
6975           (gnus-configure-windows 'group 'force)))
6976       ;; Clear the current group name.
6977       (unless quit-config
6978         (setq gnus-newsgroup-name nil)))))
6979
6980 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6981 (defun gnus-summary-exit-no-update (&optional no-questions)
6982   "Quit reading current newsgroup without updating read article info."
6983   (interactive)
6984   (let* ((group gnus-newsgroup-name)
6985          (gnus-group-is-exiting-p t)
6986          (gnus-group-is-exiting-without-update-p t)
6987          (quit-config (gnus-group-quit-config group)))
6988     (when (or no-questions
6989               gnus-expert-user
6990               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6991       (gnus-async-halt-prefetch)
6992       (run-hooks 'gnus-summary-prepare-exit-hook)
6993       (when (gnus-buffer-live-p gnus-article-buffer)
6994         (save-excursion
6995           (set-buffer gnus-article-buffer)
6996           (mm-destroy-parts gnus-article-mime-handles)
6997           ;; Set it to nil for safety reason.
6998           (setq gnus-article-mime-handle-alist nil)
6999           (setq gnus-article-mime-handles nil)))
7000       ;; If we have several article buffers, we kill them at exit.
7001       (unless gnus-single-article-buffer
7002         (gnus-kill-buffer gnus-article-buffer)
7003         (gnus-kill-buffer gnus-original-article-buffer)
7004         (setq gnus-article-current nil))
7005       (if (not gnus-kill-summary-on-exit)
7006           (gnus-deaden-summary)
7007         (gnus-close-group group)
7008         (gnus-summary-clear-local-variables)
7009         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7010           (gnus-summary-clear-local-variables))
7011         (gnus-kill-buffer gnus-summary-buffer))
7012       (unless gnus-single-article-buffer
7013         (setq gnus-article-current nil))
7014       (when gnus-use-trees
7015         (gnus-tree-close group))
7016       (gnus-async-prefetch-remove-group group)
7017       (when (get-buffer gnus-article-buffer)
7018         (bury-buffer gnus-article-buffer))
7019       ;; Return to the group buffer.
7020       (gnus-configure-windows 'group 'force)
7021       ;; Clear the current group name.
7022       (setq gnus-newsgroup-name nil)
7023       (unless (gnus-ephemeral-group-p group)
7024         (gnus-group-update-group group))
7025       (when (equal (gnus-group-group-name) group)
7026         (gnus-group-next-unread-group 1))
7027       (when quit-config
7028         (gnus-handle-ephemeral-exit quit-config)))))
7029
7030 (defun gnus-handle-ephemeral-exit (quit-config)
7031   "Handle movement when leaving an ephemeral group.
7032 The state which existed when entering the ephemeral is reset."
7033   (if (not (buffer-name (car quit-config)))
7034       (gnus-configure-windows 'group 'force)
7035     (set-buffer (car quit-config))
7036     (cond ((eq major-mode 'gnus-summary-mode)
7037            (gnus-set-global-variables))
7038           ((eq major-mode 'gnus-article-mode)
7039            (save-excursion
7040              ;; The `gnus-summary-buffer' variable may point
7041              ;; to the old summary buffer when using a single
7042              ;; article buffer.
7043              (unless (gnus-buffer-live-p gnus-summary-buffer)
7044                (set-buffer gnus-group-buffer))
7045              (set-buffer gnus-summary-buffer)
7046              (gnus-set-global-variables))))
7047     (if (or (eq (cdr quit-config) 'article)
7048             (eq (cdr quit-config) 'pick))
7049         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7050             (gnus-configure-windows 'pick 'force)
7051           (gnus-configure-windows (cdr quit-config) 'force))
7052       (gnus-configure-windows (cdr quit-config) 'force))
7053     (when (eq major-mode 'gnus-summary-mode)
7054       (gnus-summary-recenter)
7055       (gnus-summary-position-point))))
7056
7057 ;;; Dead summaries.
7058
7059 (defvar gnus-dead-summary-mode-map nil)
7060
7061 (unless gnus-dead-summary-mode-map
7062   (setq gnus-dead-summary-mode-map (make-keymap))
7063   (suppress-keymap gnus-dead-summary-mode-map)
7064   (substitute-key-definition
7065    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7066   (dolist (key '("\C-d" "\r" "\177" [delete]))
7067     (define-key gnus-dead-summary-mode-map
7068       key 'gnus-summary-wake-up-the-dead))
7069   (dolist (key '("q" "Q"))
7070     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7071
7072 (defvar gnus-dead-summary-mode nil
7073   "Minor mode for Gnus summary buffers.")
7074
7075 (defun gnus-dead-summary-mode (&optional arg)
7076   "Minor mode for Gnus summary buffers."
7077   (interactive "P")
7078   (when (eq major-mode 'gnus-summary-mode)
7079     (make-local-variable 'gnus-dead-summary-mode)
7080     (setq gnus-dead-summary-mode
7081           (if (null arg) (not gnus-dead-summary-mode)
7082             (> (prefix-numeric-value arg) 0)))
7083     (when gnus-dead-summary-mode
7084       (add-minor-mode
7085        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7086
7087 (defun gnus-deaden-summary ()
7088   "Make the current summary buffer into a dead summary buffer."
7089   ;; Kill any previous dead summary buffer.
7090   (when (and gnus-dead-summary
7091              (buffer-name gnus-dead-summary))
7092     (with-current-buffer gnus-dead-summary
7093       (when gnus-dead-summary-mode
7094         (kill-buffer (current-buffer)))))
7095   ;; Make this the current dead summary.
7096   (setq gnus-dead-summary (current-buffer))
7097   (gnus-dead-summary-mode 1)
7098   (let ((name (buffer-name)))
7099     (when (string-match "Summary" name)
7100       (rename-buffer
7101        (concat (substring name 0 (match-beginning 0)) "Dead "
7102                (substring name (match-beginning 0)))
7103        t)
7104       (bury-buffer))))
7105
7106 (defun gnus-kill-or-deaden-summary (buffer)
7107   "Kill or deaden the summary BUFFER."
7108   (save-excursion
7109     (when (and (buffer-name buffer)
7110                (not gnus-single-article-buffer))
7111       (with-current-buffer buffer
7112         (gnus-kill-buffer gnus-article-buffer)
7113         (gnus-kill-buffer gnus-original-article-buffer)))
7114     (cond
7115      ;; Kill the buffer.
7116      (gnus-kill-summary-on-exit
7117       (when (and gnus-use-trees
7118                  (gnus-buffer-exists-p buffer))
7119         (save-excursion
7120           (set-buffer buffer)
7121           (gnus-tree-close gnus-newsgroup-name)))
7122       (gnus-kill-buffer buffer))
7123      ;; Deaden the buffer.
7124      ((gnus-buffer-exists-p buffer)
7125       (save-excursion
7126         (set-buffer buffer)
7127         (gnus-deaden-summary))))))
7128
7129 (defun gnus-summary-wake-up-the-dead (&rest args)
7130   "Wake up the dead summary buffer."
7131   (interactive)
7132   (gnus-dead-summary-mode -1)
7133   (let ((name (buffer-name)))
7134     (when (string-match "Dead " name)
7135       (rename-buffer
7136        (concat (substring name 0 (match-beginning 0))
7137                (substring name (match-end 0)))
7138        t)))
7139   (gnus-message 3 "This dead summary is now alive again"))
7140
7141 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7142 (defun gnus-summary-fetch-faq (&optional faq-dir)
7143   "Fetch the FAQ for the current group.
7144 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7145 in."
7146   (interactive
7147    (list
7148     (when current-prefix-arg
7149       (completing-read
7150        "FAQ dir: " (and (listp gnus-group-faq-directory)
7151                         (mapcar 'list
7152                                 gnus-group-faq-directory))))))
7153   (let (gnus-faq-buffer)
7154     (when (setq gnus-faq-buffer
7155                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7156       (gnus-configure-windows 'summary-faq))))
7157
7158 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7159 (defun gnus-summary-describe-group (&optional force)
7160   "Describe the current newsgroup."
7161   (interactive "P")
7162   (gnus-group-describe-group force gnus-newsgroup-name))
7163
7164 (defun gnus-summary-describe-briefly ()
7165   "Describe summary mode commands briefly."
7166   (interactive)
7167   (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")))
7168
7169 ;; Walking around group mode buffer from summary mode.
7170
7171 (defun gnus-summary-next-group (&optional no-article target-group backward)
7172   "Exit current newsgroup and then select next unread newsgroup.
7173 If prefix argument NO-ARTICLE is non-nil, no article is selected
7174 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7175 previous group instead."
7176   (interactive "P")
7177   ;; Stop pre-fetching.
7178   (gnus-async-halt-prefetch)
7179   (let ((current-group gnus-newsgroup-name)
7180         (current-buffer (current-buffer))
7181         entered)
7182     ;; First we semi-exit this group to update Xrefs and all variables.
7183     ;; We can't do a real exit, because the window conf must remain
7184     ;; the same in case the user is prompted for info, and we don't
7185     ;; want the window conf to change before that...
7186     (gnus-summary-exit t)
7187     (while (not entered)
7188       ;; Then we find what group we are supposed to enter.
7189       (set-buffer gnus-group-buffer)
7190       (gnus-group-jump-to-group current-group)
7191       (setq target-group
7192             (or target-group
7193                 (if (eq gnus-keep-same-level 'best)
7194                     (gnus-summary-best-group gnus-newsgroup-name)
7195                   (gnus-summary-search-group backward gnus-keep-same-level))))
7196       (if (not target-group)
7197           ;; There are no further groups, so we return to the group
7198           ;; buffer.
7199           (progn
7200             (gnus-message 5 "Returning to the group buffer")
7201             (setq entered t)
7202             (when (gnus-buffer-live-p current-buffer)
7203               (set-buffer current-buffer)
7204               (gnus-summary-exit))
7205             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7206         ;; We try to enter the target group.
7207         (gnus-group-jump-to-group target-group)
7208         (let ((unreads (gnus-group-group-unread)))
7209           (if (and (or (eq t unreads)
7210                        (and unreads (not (zerop unreads))))
7211                    (gnus-summary-read-group
7212                     target-group nil no-article
7213                     (and (buffer-name current-buffer) current-buffer)
7214                     nil backward))
7215               (setq entered t)
7216             (setq current-group target-group
7217                   target-group nil)))))))
7218
7219 (defun gnus-summary-prev-group (&optional no-article)
7220   "Exit current newsgroup and then select previous unread newsgroup.
7221 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7222   (interactive "P")
7223   (gnus-summary-next-group no-article nil t))
7224
7225 ;; Walking around summary lines.
7226
7227 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7228   "Go to the first subject satisfying any non-nil constraint.
7229 If UNREAD is non-nil, the article should be unread.
7230 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7231 If UNSEEN is non-nil, the article should be unseen.
7232 Returns the article selected or nil if there are no matching articles."
7233   (interactive "P")
7234   (cond
7235    ;; Empty summary.
7236    ((null gnus-newsgroup-data)
7237     (gnus-message 3 "No articles in the group")
7238     nil)
7239    ;; Pick the first article.
7240    ((not (or unread undownloaded unseen))
7241     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7242     (gnus-data-number (car gnus-newsgroup-data)))
7243    ;; Find the first unread article.
7244    (t
7245     (let ((data gnus-newsgroup-data))
7246       (while (and data
7247                   (let ((num (gnus-data-number (car data))))
7248                     (or (memq num gnus-newsgroup-unfetched)
7249                         (not (or (and unread
7250                                       (memq num gnus-newsgroup-unreads))
7251                                  (and undownloaded
7252                                       (memq num gnus-newsgroup-undownloaded))
7253                                  (and unseen
7254                                       (memq num gnus-newsgroup-unseen)))))))
7255         (setq data (cdr data)))
7256       (prog1
7257           (if data
7258               (progn
7259                 (goto-char (gnus-data-pos (car data)))
7260                 (gnus-data-number (car data)))
7261             (gnus-message 3 "No more%s articles"
7262                           (let* ((r (when unread " unread"))
7263                                  (d (when undownloaded " undownloaded"))
7264                                  (s (when unseen " unseen"))
7265                                  (l (delq nil (list r d s))))
7266                             (cond ((= 3 (length l))
7267                                    (concat r "," d ", or" s))
7268                                   ((= 2 (length l))
7269                                    (concat (car l) ", or" (cadr l)))
7270                                   ((= 1 (length l))
7271                                    (car l))
7272                                   (t
7273                                    ""))))
7274             nil
7275             )
7276         (gnus-summary-position-point))))))
7277
7278 (defun gnus-summary-next-subject (n &optional unread dont-display)
7279   "Go to next N'th summary line.
7280 If N is negative, go to the previous N'th subject line.
7281 If UNREAD is non-nil, only unread articles are selected.
7282 The difference between N and the actual number of steps taken is
7283 returned."
7284   (interactive "p")
7285   (let ((backward (< n 0))
7286         (n (abs n)))
7287     (while (and (> n 0)
7288                 (if backward
7289                     (gnus-summary-find-prev unread)
7290                   (gnus-summary-find-next unread)))
7291       (unless (zerop (setq n (1- n)))
7292         (gnus-summary-show-thread)))
7293     (when (/= 0 n)
7294       (gnus-message 7 "No more%s articles"
7295                     (if unread " unread" "")))
7296     (unless dont-display
7297       (gnus-summary-recenter)
7298       (gnus-summary-position-point))
7299     n))
7300
7301 (defun gnus-summary-next-unread-subject (n)
7302   "Go to next N'th unread summary line."
7303   (interactive "p")
7304   (gnus-summary-next-subject n t))
7305
7306 (defun gnus-summary-prev-subject (n &optional unread)
7307   "Go to previous N'th summary line.
7308 If optional argument UNREAD is non-nil, only unread article is selected."
7309   (interactive "p")
7310   (gnus-summary-next-subject (- n) unread))
7311
7312 (defun gnus-summary-prev-unread-subject (n)
7313   "Go to previous N'th unread summary line."
7314   (interactive "p")
7315   (gnus-summary-next-subject (- n) t))
7316
7317 (defun gnus-summary-goto-subjects (articles)
7318   "Insert the subject header for ARTICLES in the current buffer."
7319   (save-excursion
7320     (dolist (article articles)
7321       (gnus-summary-goto-subject article t)))
7322   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7323   (gnus-summary-position-point))
7324
7325 (defun gnus-summary-goto-subject (article &optional force silent)
7326   "Go the subject line of ARTICLE.
7327 If FORCE, also allow jumping to articles not currently shown."
7328   (interactive "nArticle number: ")
7329   (unless (numberp article)
7330     (error "Article %s is not a number" article))
7331   (let ((b (point))
7332         (data (gnus-data-find article)))
7333     ;; We read in the article if we have to.
7334     (and (not data)
7335          force
7336          (gnus-summary-insert-subject
7337           article
7338           (if (or (numberp force) (vectorp force)) force)
7339           t)
7340          (setq data (gnus-data-find article)))
7341     (goto-char b)
7342     (if (not data)
7343         (progn
7344           (unless silent
7345             (gnus-message 3 "Can't find article %d" article))
7346           nil)
7347       (let ((pt (gnus-data-pos data)))
7348         (goto-char pt)
7349         (gnus-summary-set-article-display-arrow pt))
7350       (gnus-summary-position-point)
7351       article)))
7352
7353 ;; Walking around summary lines with displaying articles.
7354
7355 (defun gnus-summary-expand-window (&optional arg)
7356   "Make the summary buffer take up the entire Emacs frame.
7357 Given a prefix, will force an `article' buffer configuration."
7358   (interactive "P")
7359   (if arg
7360       (gnus-configure-windows 'article 'force)
7361     (gnus-configure-windows 'summary 'force)))
7362
7363 (defun gnus-summary-display-article (article &optional all-header)
7364   "Display ARTICLE in article buffer."
7365   (when (gnus-buffer-live-p gnus-article-buffer)
7366     (with-current-buffer gnus-article-buffer
7367       (mm-enable-multibyte)))
7368   (gnus-set-global-variables)
7369   (when (gnus-buffer-live-p gnus-article-buffer)
7370     (with-current-buffer gnus-article-buffer
7371       (setq gnus-article-charset gnus-newsgroup-charset)
7372       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7373       (mm-enable-multibyte)))
7374   (if (null article)
7375       nil
7376     (prog1
7377         (if gnus-summary-display-article-function
7378             (funcall gnus-summary-display-article-function article all-header)
7379           (gnus-article-prepare article all-header))
7380       (gnus-run-hooks 'gnus-select-article-hook)
7381       (when (and gnus-current-article
7382                  (not (zerop gnus-current-article)))
7383         (gnus-summary-goto-subject gnus-current-article))
7384       (gnus-summary-recenter)
7385       (when (and gnus-use-trees gnus-show-threads)
7386         (gnus-possibly-generate-tree article)
7387         (gnus-highlight-selected-tree article))
7388       ;; Successfully display article.
7389       (gnus-article-set-window-start
7390        (cdr (assq article gnus-newsgroup-bookmarks))))))
7391
7392 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7393   "Select the current article.
7394 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7395 non-nil, the article will be re-fetched even if it already present in
7396 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7397 be displayed."
7398   ;; Make sure we are in the summary buffer to work around bbdb bug.
7399   (unless (eq major-mode 'gnus-summary-mode)
7400     (set-buffer gnus-summary-buffer))
7401   (let ((article (or article (gnus-summary-article-number)))
7402         (all-headers (not (not all-headers))) ;Must be t or nil.
7403         gnus-summary-display-article-function)
7404     (and (not pseudo)
7405          (gnus-summary-article-pseudo-p article)
7406          (error "This is a pseudo-article"))
7407     (save-excursion
7408       (set-buffer gnus-summary-buffer)
7409       (if (or (and gnus-single-article-buffer
7410                    (or (null gnus-current-article)
7411                        (null gnus-article-current)
7412                        (null (get-buffer gnus-article-buffer))
7413                        (not (eq article (cdr gnus-article-current)))
7414                        (not (equal (car gnus-article-current)
7415                                    gnus-newsgroup-name))))
7416               (and (not gnus-single-article-buffer)
7417                    (or (null gnus-current-article)
7418                        (not (eq gnus-current-article article))))
7419               force)
7420           ;; The requested article is different from the current article.
7421           (progn
7422             (gnus-summary-display-article article all-headers)
7423             (when (gnus-buffer-live-p gnus-article-buffer)
7424               (with-current-buffer gnus-article-buffer
7425                 (if (not gnus-article-decoded-p) ;; a local variable
7426                     (mm-disable-multibyte))))
7427             (gnus-article-set-window-start
7428              (cdr (assq article gnus-newsgroup-bookmarks)))
7429             article)
7430         'old))))
7431
7432 (defun gnus-summary-force-verify-and-decrypt ()
7433   "Display buttons for signed/encrypted parts and verify/decrypt them."
7434   (interactive)
7435   (let ((mm-verify-option 'known)
7436         (mm-decrypt-option 'known)
7437         (gnus-article-emulate-mime t)
7438         (gnus-buttonized-mime-types (append (list "multipart/signed"
7439                                                   "multipart/encrypted")
7440                                             gnus-buttonized-mime-types)))
7441     (gnus-summary-select-article nil 'force)))
7442
7443 (defun gnus-summary-set-current-mark (&optional current-mark)
7444   "Obsolete function."
7445   nil)
7446
7447 (defun gnus-summary-next-article (&optional unread subject backward push)
7448   "Select the next article.
7449 If UNREAD, only unread articles are selected.
7450 If SUBJECT, only articles with SUBJECT are selected.
7451 If BACKWARD, the previous article is selected instead of the next."
7452   (interactive "P")
7453   ;; Make sure we are in the summary buffer.
7454   (unless (eq major-mode 'gnus-summary-mode)
7455     (set-buffer gnus-summary-buffer))
7456   (cond
7457    ;; Is there such an article?
7458    ((and (gnus-summary-search-forward unread subject backward)
7459          (or (gnus-summary-display-article (gnus-summary-article-number))
7460              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7461     (gnus-summary-position-point))
7462    ;; If not, we try the first unread, if that is wanted.
7463    ((and subject
7464          gnus-auto-select-same
7465          (gnus-summary-first-unread-article))
7466     (gnus-summary-position-point)
7467     (gnus-message 6 "Wrapped"))
7468    ;; Try to get next/previous article not displayed in this group.
7469    ((and gnus-auto-extend-newsgroup
7470          (not unread) (not subject))
7471     (gnus-summary-goto-article
7472      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7473      nil (count-lines (point-min) (point))))
7474    ;; Go to next/previous group.
7475    (t
7476     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7477       (gnus-summary-jump-to-group gnus-newsgroup-name))
7478     (let ((cmd last-command-char)
7479           (point
7480            (with-current-buffer gnus-group-buffer
7481              (point)))
7482           (group
7483            (if (eq gnus-keep-same-level 'best)
7484                (gnus-summary-best-group gnus-newsgroup-name)
7485              (gnus-summary-search-group backward gnus-keep-same-level))))
7486       ;; For some reason, the group window gets selected.  We change
7487       ;; it back.
7488       (select-window (get-buffer-window (current-buffer)))
7489       ;; Select next unread newsgroup automagically.
7490       (cond
7491        ((or (not gnus-auto-select-next)
7492             (not cmd))
7493         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7494        ((or (eq gnus-auto-select-next 'quietly)
7495             (and (eq gnus-auto-select-next 'slightly-quietly)
7496                  push)
7497             (and (eq gnus-auto-select-next 'almost-quietly)
7498                  (gnus-summary-last-article-p)))
7499         ;; Select quietly.
7500         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7501             (gnus-summary-exit)
7502           (gnus-message 7 "No more%s articles (%s)..."
7503                         (if unread " unread" "")
7504                         (if group (concat "selecting " group)
7505                           "exiting"))
7506           (gnus-summary-next-group nil group backward)))
7507        (t
7508         (when (gnus-key-press-event-p last-input-event)
7509           (gnus-summary-walk-group-buffer
7510            gnus-newsgroup-name cmd unread backward point))))))))
7511
7512 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7513   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7514                       (?\C-p (gnus-group-prev-unread-group 1))))
7515         (cursor-in-echo-area t)
7516         keve key group ended prompt)
7517     (save-excursion
7518       (set-buffer gnus-group-buffer)
7519       (goto-char start)
7520       (setq group
7521             (if (eq gnus-keep-same-level 'best)
7522                 (gnus-summary-best-group gnus-newsgroup-name)
7523               (gnus-summary-search-group backward gnus-keep-same-level))))
7524     (while (not ended)
7525       (setq prompt
7526             (format
7527              "No more%s articles%s " (if unread " unread" "")
7528              (if (and group
7529                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7530                  (format " (Type %s for %s [%s])"
7531                          (single-key-description cmd)
7532                          (gnus-group-decoded-name group)
7533                          (gnus-group-unread group))
7534                (format " (Type %s to exit %s)"
7535                        (single-key-description cmd)
7536                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7537       ;; Confirm auto selection.
7538       (setq key (car (setq keve (gnus-read-event-char prompt)))
7539             ended t)
7540       (cond
7541        ((assq key keystrokes)
7542         (let ((obuf (current-buffer)))
7543           (switch-to-buffer gnus-group-buffer)
7544           (when group
7545             (gnus-group-jump-to-group group))
7546           (eval (cadr (assq key keystrokes)))
7547           (setq group (gnus-group-group-name))
7548           (switch-to-buffer obuf))
7549         (setq ended nil))
7550        ((equal key cmd)
7551         (if (or (not group)
7552                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7553             (gnus-summary-exit)
7554           (gnus-summary-next-group nil group backward)))
7555        (t
7556         (push (cdr keve) unread-command-events))))))
7557
7558 (defun gnus-summary-next-unread-article ()
7559   "Select unread article after current one."
7560   (interactive)
7561   (gnus-summary-next-article
7562    (or (not (eq gnus-summary-goto-unread 'never))
7563        (gnus-summary-last-article-p (gnus-summary-article-number)))
7564    (and gnus-auto-select-same
7565         (gnus-summary-article-subject))))
7566
7567 (defun gnus-summary-prev-article (&optional unread subject)
7568   "Select the article after the current one.
7569 If UNREAD is non-nil, only unread articles are selected."
7570   (interactive "P")
7571   (gnus-summary-next-article unread subject t))
7572
7573 (defun gnus-summary-prev-unread-article ()
7574   "Select unread article before current one."
7575   (interactive)
7576   (gnus-summary-prev-article
7577    (or (not (eq gnus-summary-goto-unread 'never))
7578        (gnus-summary-first-article-p (gnus-summary-article-number)))
7579    (and gnus-auto-select-same
7580         (gnus-summary-article-subject))))
7581
7582 (defun gnus-summary-next-page (&optional lines circular stop)
7583   "Show next page of the selected article.
7584 If at the end of the current article, select the next article.
7585 LINES says how many lines should be scrolled up.
7586
7587 If CIRCULAR is non-nil, go to the start of the article instead of
7588 selecting the next article when reaching the end of the current
7589 article.
7590
7591 If STOP is non-nil, just stop when reaching the end of the message.
7592
7593 Also see the variable `gnus-article-skip-boring'."
7594   (interactive "P")
7595   (setq gnus-summary-buffer (current-buffer))
7596   (gnus-set-global-variables)
7597   (let ((article (gnus-summary-article-number))
7598         (article-window (get-buffer-window gnus-article-buffer t))
7599         endp)
7600     ;; If the buffer is empty, we have no article.
7601     (unless article
7602       (error "No article to select"))
7603     (gnus-configure-windows 'article)
7604     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7605         (if (and (eq gnus-summary-goto-unread 'never)
7606                  (not (gnus-summary-last-article-p article)))
7607             (gnus-summary-next-article)
7608           (gnus-summary-next-unread-article))
7609       (if (or (null gnus-current-article)
7610               (null gnus-article-current)
7611               (/= article (cdr gnus-article-current))
7612               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7613           ;; Selected subject is different from current article's.
7614           (gnus-summary-display-article article)
7615         (when article-window
7616           (gnus-eval-in-buffer-window gnus-article-buffer
7617             (setq endp (or (gnus-article-next-page lines)
7618                            (gnus-article-only-boring-p))))
7619           (when endp
7620             (cond (stop
7621                    (gnus-message 3 "End of message"))
7622                   (circular
7623                    (gnus-summary-beginning-of-article))
7624                   (lines
7625                    (gnus-message 3 "End of message"))
7626                   ((null lines)
7627                    (if (and (eq gnus-summary-goto-unread 'never)
7628                             (not (gnus-summary-last-article-p article)))
7629                        (gnus-summary-next-article)
7630                      (gnus-summary-next-unread-article))))))))
7631     (gnus-summary-recenter)
7632     (gnus-summary-position-point)))
7633
7634 (defun gnus-summary-prev-page (&optional lines move)
7635   "Show previous page of selected article.
7636 Argument LINES specifies lines to be scrolled down.
7637 If MOVE, move to the previous unread article if point is at
7638 the beginning of the buffer."
7639   (interactive "P")
7640   (let ((article (gnus-summary-article-number))
7641         (article-window (get-buffer-window gnus-article-buffer t))
7642         endp)
7643     (gnus-configure-windows 'article)
7644     (if (or (null gnus-current-article)
7645             (null gnus-article-current)
7646             (/= article (cdr gnus-article-current))
7647             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7648         ;; Selected subject is different from current article's.
7649         (gnus-summary-display-article article)
7650       (gnus-summary-recenter)
7651       (when article-window
7652         (gnus-eval-in-buffer-window gnus-article-buffer
7653           (setq endp (gnus-article-prev-page lines)))
7654         (when (and move endp)
7655           (cond (lines
7656                  (gnus-message 3 "Beginning of message"))
7657                 ((null lines)
7658                  (if (and (eq gnus-summary-goto-unread 'never)
7659                           (not (gnus-summary-first-article-p article)))
7660                      (gnus-summary-prev-article)
7661                    (gnus-summary-prev-unread-article))))))))
7662   (gnus-summary-position-point))
7663
7664 (defun gnus-summary-prev-page-or-article (&optional lines)
7665   "Show previous page of selected article.
7666 Argument LINES specifies lines to be scrolled down.
7667 If at the beginning of the article, go to the next article."
7668   (interactive "P")
7669   (gnus-summary-prev-page lines t))
7670
7671 (defun gnus-summary-scroll-up (lines)
7672   "Scroll up (or down) one line current article.
7673 Argument LINES specifies lines to be scrolled up (or down if negative)."
7674   (interactive "p")
7675   (gnus-configure-windows 'article)
7676   (gnus-summary-show-thread)
7677   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7678     (gnus-eval-in-buffer-window gnus-article-buffer
7679       (cond ((> lines 0)
7680              (when (gnus-article-next-page lines)
7681                (gnus-message 3 "End of message")))
7682             ((< lines 0)
7683              (gnus-article-prev-page (- lines))))))
7684   (gnus-summary-recenter)
7685   (gnus-summary-position-point))
7686
7687 (defun gnus-summary-scroll-down (lines)
7688   "Scroll down (or up) one line current article.
7689 Argument LINES specifies lines to be scrolled down (or up if negative)."
7690   (interactive "p")
7691   (gnus-summary-scroll-up (- lines)))
7692
7693 (defun gnus-summary-next-same-subject ()
7694   "Select next article which has the same subject as current one."
7695   (interactive)
7696   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7697
7698 (defun gnus-summary-prev-same-subject ()
7699   "Select previous article which has the same subject as current one."
7700   (interactive)
7701   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7702
7703 (defun gnus-summary-next-unread-same-subject ()
7704   "Select next unread article which has the same subject as current one."
7705   (interactive)
7706   (gnus-summary-next-article t (gnus-summary-article-subject)))
7707
7708 (defun gnus-summary-prev-unread-same-subject ()
7709   "Select previous unread article which has the same subject as current one."
7710   (interactive)
7711   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7712
7713 (defun gnus-summary-first-unread-article ()
7714   "Select the first unread article.
7715 Return nil if there are no unread articles."
7716   (interactive)
7717   (prog1
7718       (when (gnus-summary-first-subject t)
7719         (gnus-summary-show-thread)
7720         (gnus-summary-first-subject t)
7721         (gnus-summary-display-article (gnus-summary-article-number)))
7722     (gnus-summary-position-point)))
7723
7724 (defun gnus-summary-first-unread-subject ()
7725   "Place the point on the subject line of the first unread article.
7726 Return nil if there are no unread articles."
7727   (interactive)
7728   (prog1
7729       (when (gnus-summary-first-subject t)
7730         (gnus-summary-show-thread)
7731         (gnus-summary-first-subject t))
7732     (gnus-summary-position-point)))
7733
7734 (defun gnus-summary-first-unseen-subject ()
7735   "Place the point on the subject line of the first unseen article.
7736 Return nil if there are no unseen articles."
7737   (interactive)
7738   (prog1
7739       (when (gnus-summary-first-subject nil nil t)
7740         (gnus-summary-show-thread)
7741         (gnus-summary-first-subject nil nil t))
7742     (gnus-summary-position-point)))
7743
7744 (defun gnus-summary-first-unseen-or-unread-subject ()
7745   "Place the point on the subject line of the first unseen article or,
7746 if all article have been seen, on the subject line of the first unread
7747 article."
7748   (interactive)
7749   (prog1
7750       (unless (when (gnus-summary-first-subject nil nil t)
7751                 (gnus-summary-show-thread)
7752                 (gnus-summary-first-subject nil nil t))
7753         (when (gnus-summary-first-subject t)
7754           (gnus-summary-show-thread)
7755           (gnus-summary-first-subject t)))
7756     (gnus-summary-position-point)))
7757
7758 (defun gnus-summary-first-article ()
7759   "Select the first article.
7760 Return nil if there are no articles."
7761   (interactive)
7762   (prog1
7763       (when (gnus-summary-first-subject)
7764         (gnus-summary-show-thread)
7765         (gnus-summary-first-subject)
7766         (gnus-summary-display-article (gnus-summary-article-number)))
7767     (gnus-summary-position-point)))
7768
7769 (defun gnus-summary-best-unread-article (&optional arg)
7770   "Select the unread article with the highest score.
7771 If given a prefix argument, select the next unread article that has a
7772 score higher than the default score."
7773   (interactive "P")
7774   (let ((article (if arg
7775                      (gnus-summary-better-unread-subject)
7776                    (gnus-summary-best-unread-subject))))
7777     (if article
7778         (gnus-summary-goto-article article)
7779       (error "No unread articles"))))
7780
7781 (defun gnus-summary-best-unread-subject ()
7782   "Select the unread subject with the highest score."
7783   (interactive)
7784   (let ((best -1000000)
7785         (data gnus-newsgroup-data)
7786         article score)
7787     (while data
7788       (and (gnus-data-unread-p (car data))
7789            (> (setq score
7790                     (gnus-summary-article-score (gnus-data-number (car data))))
7791               best)
7792            (setq best score
7793                  article (gnus-data-number (car data))))
7794       (setq data (cdr data)))
7795     (when article
7796       (gnus-summary-goto-subject article))
7797     (gnus-summary-position-point)
7798     article))
7799
7800 (defun gnus-summary-better-unread-subject ()
7801   "Select the first unread subject that has a score over the default score."
7802   (interactive)
7803   (let ((data gnus-newsgroup-data)
7804         article score)
7805     (while (and (setq article (gnus-data-number (car data)))
7806                 (or (gnus-data-read-p (car data))
7807                     (not (> (gnus-summary-article-score article)
7808                             gnus-summary-default-score))))
7809       (setq data (cdr data)))
7810     (when article
7811       (gnus-summary-goto-subject article))
7812     (gnus-summary-position-point)
7813     article))
7814
7815 (defun gnus-summary-last-subject ()
7816   "Go to the last displayed subject line in the group."
7817   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7818     (when article
7819       (gnus-summary-goto-subject article))))
7820
7821 (defun gnus-summary-goto-article (article &optional all-headers force)
7822   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7823 If ALL-HEADERS is non-nil, no header lines are hidden.
7824 If FORCE, go to the article even if it isn't displayed.  If FORCE
7825 is a number, it is the line the article is to be displayed on."
7826   (interactive
7827    (list
7828     (completing-read
7829      "Article number or Message-ID: "
7830      (mapcar (lambda (number) (list (int-to-string number)))
7831              gnus-newsgroup-limit))
7832     current-prefix-arg
7833     t))
7834   (prog1
7835       (if (and (stringp article)
7836                (string-match "@\\|%40" article))
7837           (gnus-summary-refer-article article)
7838         (when (stringp article)
7839           (setq article (string-to-number article)))
7840         (if (gnus-summary-goto-subject article force)
7841             (gnus-summary-display-article article all-headers)
7842           (gnus-message 4 "Couldn't go to article %s" article) nil))
7843     (gnus-summary-position-point)))
7844
7845 (defun gnus-summary-goto-last-article ()
7846   "Go to the previously read article."
7847   (interactive)
7848   (prog1
7849       (when gnus-last-article
7850         (gnus-summary-goto-article gnus-last-article nil t))
7851     (gnus-summary-position-point)))
7852
7853 (defun gnus-summary-pop-article (number)
7854   "Pop one article off the history and go to the previous.
7855 NUMBER articles will be popped off."
7856   (interactive "p")
7857   (let (to)
7858     (setq gnus-newsgroup-history
7859           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7860     (if to
7861         (gnus-summary-goto-article (car to) nil t)
7862       (error "Article history empty")))
7863   (gnus-summary-position-point))
7864
7865 ;; Summary commands and functions for limiting the summary buffer.
7866
7867 (defun gnus-summary-limit-to-articles (n)
7868   "Limit the summary buffer to the next N articles.
7869 If not given a prefix, use the process marked articles instead."
7870   (interactive "P")
7871   (prog1
7872       (let ((articles (gnus-summary-work-articles n)))
7873         (setq gnus-newsgroup-processable nil)
7874         (gnus-summary-limit articles))
7875     (gnus-summary-position-point)))
7876
7877 (defun gnus-summary-pop-limit (&optional total)
7878   "Restore the previous limit.
7879 If given a prefix, remove all limits."
7880   (interactive "P")
7881   (when total
7882     (setq gnus-newsgroup-limits
7883           (list (mapcar (lambda (h) (mail-header-number h))
7884                         gnus-newsgroup-headers))))
7885   (unless gnus-newsgroup-limits
7886     (error "No limit to pop"))
7887   (prog1
7888       (gnus-summary-limit nil 'pop)
7889     (gnus-summary-position-point)))
7890
7891 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7892   "Limit the summary buffer to articles that have subjects that match a regexp.
7893 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7894   (interactive
7895    (list (read-string (if current-prefix-arg
7896                           "Exclude subject (regexp): "
7897                         "Limit to subject (regexp): "))
7898          nil current-prefix-arg))
7899   (unless header
7900     (setq header "subject"))
7901   (when (not (equal "" subject))
7902     (prog1
7903         (let ((articles (gnus-summary-find-matching
7904                          (or header "subject") subject 'all nil nil
7905                          not-matching)))
7906           (unless articles
7907             (error "Found no matches for \"%s\"" subject))
7908           (gnus-summary-limit articles))
7909       (gnus-summary-position-point))))
7910
7911 (defun gnus-summary-limit-to-author (from &optional not-matching)
7912   "Limit the summary buffer to articles that have authors that match a regexp.
7913 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7914   (interactive
7915    (list (read-string (if current-prefix-arg
7916                           "Exclude author (regexp): "
7917                         "Limit to author (regexp): "))
7918          current-prefix-arg))
7919   (gnus-summary-limit-to-subject from "from" not-matching))
7920
7921 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7922   "Limit the summary buffer to articles with the given RECIPIENT.
7923
7924 If NOT-MATCHING, exclude RECIPIENT.
7925
7926 To and Cc headers are checked.  You need to include them in
7927 `nnmail-extra-headers'."
7928   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7929   (interactive
7930    (list (read-string (format "%s recipient (regexp): "
7931                               (if current-prefix-arg "Exclude" "Limit to")))
7932          current-prefix-arg))
7933   (when (not (equal "" recipient))
7934     (prog1 (let* ((to
7935                    (if (memq 'To nnmail-extra-headers)
7936                        (gnus-summary-find-matching
7937                         (cons 'extra 'To) recipient 'all nil nil
7938                         not-matching)
7939                      (gnus-message
7940                       1 "`To' isn't present in `nnmail-extra-headers'")
7941                      (sit-for 1)
7942                      nil))
7943                   (cc
7944                    (if (memq 'Cc nnmail-extra-headers)
7945                        (gnus-summary-find-matching
7946                         (cons 'extra 'Cc) recipient 'all nil nil
7947                         not-matching)
7948                      (gnus-message
7949                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7950                      (sit-for 1)
7951                      nil))
7952                   (articles
7953                    (if not-matching
7954                        ;; We need the numbers that are in both lists:
7955                        (mapcar (lambda (a)
7956                                  (and (memq a to) a))
7957                                cc)
7958                      (nconc to cc))))
7959              (unless articles
7960                (error "Found no matches for \"%s\"" recipient))
7961              (gnus-summary-limit articles))
7962       (gnus-summary-position-point))))
7963
7964 (defun gnus-summary-limit-strange-charsets-predicate (header)
7965   (let ((string (concat (mail-header-subject header)
7966                         (mail-header-from header)))
7967         charset found)
7968     (dotimes (i (1- (length string)))
7969       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
7970       (when (string-match "unicode\\|big\\|japanese" charset)
7971         (setq found t)))
7972     found))
7973
7974 (defun gnus-summary-limit-to-predicate (predicate)
7975   "Limit to articles where PREDICATE returns non-nil.
7976 PREDICATE will be called with the header structures of the
7977 articles."
7978   (let ((articles nil)
7979         (case-fold-search t))
7980     (dolist (header gnus-newsgroup-headers)
7981       (when (funcall predicate header)
7982         (push (mail-header-number header) articles)))
7983     (gnus-summary-limit (nreverse articles))))
7984
7985 (defun gnus-summary-limit-to-age (age &optional younger-p)
7986   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7987 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7988 articles that are younger than AGE days."
7989   (interactive
7990    (let ((younger current-prefix-arg)
7991          (days-got nil)
7992          days)
7993      (while (not days-got)
7994        (setq days (if younger
7995                       (read-string "Limit to articles younger than (in days, older when negative): ")
7996                     (read-string
7997                      "Limit to articles older than (in days, younger when negative): ")))
7998        (when (> (length days) 0)
7999          (setq days (read days)))
8000        (if (numberp days)
8001            (progn
8002              (setq days-got t)
8003              (when (< days 0)
8004                (setq younger (not younger))
8005                (setq days (* days -1))))
8006          (message "Please enter a number.")
8007          (sleep-for 1)))
8008      (list days younger)))
8009   (prog1
8010       (let ((data gnus-newsgroup-data)
8011             (cutoff (days-to-time age))
8012             articles d date is-younger)
8013         (while (setq d (pop data))
8014           (when (and (vectorp (gnus-data-header d))
8015                      (setq date (mail-header-date (gnus-data-header d))))
8016             (setq is-younger (time-less-p
8017                               (time-since (condition-case ()
8018                                               (date-to-time date)
8019                                             (error '(0 0))))
8020                               cutoff))
8021             (when (if younger-p
8022                       is-younger
8023                     (not is-younger))
8024               (push (gnus-data-number d) articles))))
8025         (gnus-summary-limit (nreverse articles)))
8026     (gnus-summary-position-point)))
8027
8028 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8029   "Limit the summary buffer to articles that match an 'extra' header."
8030   (interactive
8031    (let ((header
8032           (intern
8033            (gnus-completing-read-with-default
8034             (symbol-name (car gnus-extra-headers))
8035             (if current-prefix-arg
8036                 "Exclude extra header"
8037               "Limit extra header")
8038             (mapcar (lambda (x)
8039                       (cons (symbol-name x) x))
8040                     gnus-extra-headers)
8041             nil
8042             t))))
8043      (list header
8044            (read-string (format "%s header %s (regexp): "
8045                                 (if current-prefix-arg "Exclude" "Limit to")
8046                                 header))
8047            current-prefix-arg)))
8048   (when (not (equal "" regexp))
8049     (prog1
8050         (let ((articles (gnus-summary-find-matching
8051                          (cons 'extra header) regexp 'all nil nil
8052                          not-matching)))
8053           (unless articles
8054             (error "Found no matches for \"%s\"" regexp))
8055           (gnus-summary-limit articles))
8056       (gnus-summary-position-point))))
8057
8058 (defun gnus-summary-limit-to-display-predicate ()
8059   "Limit the summary buffer to the predicated in the `display' group parameter."
8060   (interactive)
8061   (unless gnus-newsgroup-display
8062     (error "There is no `display' group parameter"))
8063   (let (articles)
8064     (dolist (number gnus-newsgroup-articles)
8065       (when (funcall gnus-newsgroup-display)
8066         (push number articles)))
8067     (gnus-summary-limit articles))
8068   (gnus-summary-position-point))
8069
8070 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8071 (make-obsolete
8072  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8073
8074 (defun gnus-summary-limit-to-unread (&optional all)
8075   "Limit the summary buffer to articles that are not marked as read.
8076 If ALL is non-nil, limit strictly to unread articles."
8077   (interactive "P")
8078   (if all
8079       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8080     (gnus-summary-limit-to-marks
8081      ;; Concat all the marks that say that an article is read and have
8082      ;; those removed.
8083      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8084            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8085            gnus-low-score-mark gnus-expirable-mark
8086            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8087            gnus-duplicate-mark gnus-souped-mark)
8088      'reverse)))
8089
8090 (defun gnus-summary-limit-to-headers (match &optional reverse)
8091   "Limit the summary buffer to articles that have headers that match MATCH.
8092 If REVERSE (the prefix), limit to articles that don't match."
8093   (interactive "sMatch headers (regexp): \nP")
8094   (gnus-summary-limit-to-bodies match reverse t))
8095
8096 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8097   "Limit the summary buffer to articles that have bodies that match MATCH.
8098 If REVERSE (the prefix), limit to articles that don't match."
8099   (interactive "sMatch body (regexp): \nP")
8100   (let ((articles nil)
8101         (gnus-select-article-hook nil)  ;Disable hook.
8102         (gnus-article-prepare-hook nil)
8103         (gnus-use-article-prefetch nil)
8104         (gnus-keep-backlog nil)
8105         (gnus-break-pages nil)
8106         (gnus-summary-display-arrow nil)
8107         (gnus-updated-mode-lines nil)
8108         (gnus-auto-center-summary nil)
8109         (gnus-display-mime-function nil))
8110     (dolist (data gnus-newsgroup-data)
8111       (let (gnus-mark-article-hook)
8112         (gnus-summary-select-article t t nil (gnus-data-number data)))
8113       (save-excursion
8114         (set-buffer gnus-article-buffer)
8115         (article-goto-body)
8116         (let* ((case-fold-search t)
8117                (found (if headersp
8118                           (re-search-backward match nil t)
8119                         (re-search-forward match nil t))))
8120           (when (or (and found
8121                          (not reverse))
8122                     (and (not found)
8123                          reverse))
8124             (push (gnus-data-number data) articles)))))
8125     (if (not articles)
8126         (message "No messages matched")
8127       (gnus-summary-limit articles)))
8128   (gnus-summary-position-point))
8129
8130 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8131   "Limit the summary buffer to articles that aren't part on any thread.
8132 If THREADSP (the prefix), limit to articles that are in threads."
8133   (interactive "P")
8134   (let ((articles nil)
8135         thread-articles
8136         threads)
8137     (dolist (thread gnus-newsgroup-threads)
8138       (if (stringp (car thread))
8139           (dolist (thread (cdr thread))
8140             (push thread threads))
8141         (push thread threads)))
8142     (dolist (thread threads)
8143       (setq thread-articles (gnus-articles-in-thread thread))
8144       (when (or (and threadsp
8145                      (> (length thread-articles) 1))
8146                 (and (not threadsp)
8147                      (= (length thread-articles) 1)))
8148         (setq articles (nconc thread-articles articles))))
8149     (if (not articles)
8150         (message "No messages matched")
8151       (gnus-summary-limit articles))
8152     (gnus-summary-position-point)))
8153
8154 (defun gnus-summary-limit-to-replied (&optional unreplied)
8155   "Limit the summary buffer to replied articles.
8156 If UNREPLIED (the prefix), limit to unreplied articles."
8157   (interactive "P")
8158   (if unreplied
8159       (gnus-summary-limit
8160        (gnus-set-difference gnus-newsgroup-articles
8161         gnus-newsgroup-replied))
8162     (gnus-summary-limit gnus-newsgroup-replied))
8163   (gnus-summary-position-point))
8164
8165 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8166 (make-obsolete 'gnus-summary-delete-marked-with
8167                'gnus-summary-limit-exclude-marks)
8168
8169 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8170   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8171 If REVERSE, limit the summary buffer to articles that are marked
8172 with MARKS.  MARKS can either be a string of marks or a list of marks.
8173 Returns how many articles were removed."
8174   (interactive "sMarks: ")
8175   (gnus-summary-limit-to-marks marks t))
8176
8177 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8178   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8179 If REVERSE (the prefix), limit the summary buffer to articles that are
8180 not marked with MARKS.  MARKS can either be a string of marks or a
8181 list of marks.
8182 Returns how many articles were removed."
8183   (interactive "sMarks: \nP")
8184   (prog1
8185       (let ((data gnus-newsgroup-data)
8186             (marks (if (listp marks) marks
8187                      (append marks nil))) ; Transform to list.
8188             articles)
8189         (while data
8190           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8191                   (memq (gnus-data-mark (car data)) marks))
8192             (push (gnus-data-number (car data)) articles))
8193           (setq data (cdr data)))
8194         (gnus-summary-limit articles))
8195     (gnus-summary-position-point)))
8196
8197 (defun gnus-summary-limit-to-score (score)
8198   "Limit to articles with score at or above SCORE."
8199   (interactive "NLimit to articles with score of at least: ")
8200   (let ((data gnus-newsgroup-data)
8201         articles)
8202     (while data
8203       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8204                 score)
8205         (push (gnus-data-number (car data)) articles))
8206       (setq data (cdr data)))
8207     (prog1
8208         (gnus-summary-limit articles)
8209       (gnus-summary-position-point))))
8210
8211 (defun gnus-summary-limit-to-unseen ()
8212   "Limit to unseen articles."
8213   (interactive)
8214   (prog1
8215       (gnus-summary-limit gnus-newsgroup-unseen)
8216     (gnus-summary-position-point)))
8217
8218 (defun gnus-summary-limit-include-thread (id)
8219   "Display all the hidden articles that is in the thread with ID in it.
8220 When called interactively, ID is the Message-ID of the current
8221 article."
8222   (interactive (list (mail-header-id (gnus-summary-article-header))))
8223   (let ((articles (gnus-articles-in-thread
8224                    (gnus-id-to-thread (gnus-root-id id)))))
8225     (prog1
8226         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8227       (gnus-summary-limit-include-matching-articles
8228        "subject"
8229        (regexp-quote (gnus-simplify-subject-re
8230                       (mail-header-subject (gnus-id-to-header id)))))
8231       (gnus-summary-position-point))))
8232
8233 (defun gnus-summary-limit-include-matching-articles (header regexp)
8234   "Display all the hidden articles that have HEADERs that match REGEXP."
8235   (interactive (list (read-string "Match on header: ")
8236                      (read-string "Regexp: ")))
8237   (let ((articles (gnus-find-matching-articles header regexp)))
8238     (prog1
8239         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8240       (gnus-summary-position-point))))
8241
8242 (defun gnus-summary-insert-dormant-articles ()
8243   "Insert all the dormant articles for this group into the current buffer."
8244   (interactive)
8245   (let ((gnus-verbose (max 6 gnus-verbose)))
8246     (if (not gnus-newsgroup-dormant)
8247         (gnus-message 3 "No dormant articles for this group")
8248       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8249
8250 (defun gnus-summary-insert-ticked-articles ()
8251   "Insert ticked articles for this group into the current buffer."
8252   (interactive)
8253   (let ((gnus-verbose (max 6 gnus-verbose)))
8254     (if (not gnus-newsgroup-marked)
8255         (gnus-message 3 "No ticked articles for this group")
8256       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8257
8258 (defun gnus-summary-limit-include-dormant ()
8259   "Display all the hidden articles that are marked as dormant.
8260 Note that this command only works on a subset of the articles currently
8261 fetched for this group."
8262   (interactive)
8263   (unless gnus-newsgroup-dormant
8264     (error "There are no dormant articles in this group"))
8265   (prog1
8266       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8267     (gnus-summary-position-point)))
8268
8269 (defun gnus-summary-limit-exclude-dormant ()
8270   "Hide all dormant articles."
8271   (interactive)
8272   (prog1
8273       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8274     (gnus-summary-position-point)))
8275
8276 (defun gnus-summary-limit-exclude-childless-dormant ()
8277   "Hide all dormant articles that have no children."
8278   (interactive)
8279   (let ((data (gnus-data-list t))
8280         articles d children)
8281     ;; Find all articles that are either not dormant or have
8282     ;; children.
8283     (while (setq d (pop data))
8284       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8285                 (and (setq children
8286                            (gnus-article-children (gnus-data-number d)))
8287                      (let (found)
8288                        (while children
8289                          (when (memq (car children) articles)
8290                            (setq children nil
8291                                  found t))
8292                          (pop children))
8293                        found)))
8294         (push (gnus-data-number d) articles)))
8295     ;; Do the limiting.
8296     (prog1
8297         (gnus-summary-limit articles)
8298       (gnus-summary-position-point))))
8299
8300 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8301   "Mark all unread excluded articles as read.
8302 If ALL, mark even excluded ticked and dormants as read."
8303   (interactive "P")
8304   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8305   (let ((articles (gnus-sorted-ndifference
8306                    (sort
8307                     (mapcar (lambda (h) (mail-header-number h))
8308                             gnus-newsgroup-headers)
8309                     '<)
8310                    gnus-newsgroup-limit))
8311         article)
8312     (setq gnus-newsgroup-unreads
8313           (gnus-sorted-intersection gnus-newsgroup-unreads
8314                                     gnus-newsgroup-limit))
8315     (if all
8316         (setq gnus-newsgroup-dormant nil
8317               gnus-newsgroup-marked nil
8318               gnus-newsgroup-reads
8319               (nconc
8320                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8321                gnus-newsgroup-reads))
8322       (while (setq article (pop articles))
8323         (unless (or (memq article gnus-newsgroup-dormant)
8324                     (memq article gnus-newsgroup-marked))
8325           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8326
8327 (defun gnus-summary-limit (articles &optional pop)
8328   (if pop
8329       ;; We pop the previous limit off the stack and use that.
8330       (setq articles (car gnus-newsgroup-limits)
8331             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8332     ;; We use the new limit, so we push the old limit on the stack.
8333     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8334   ;; Set the limit.
8335   (setq gnus-newsgroup-limit articles)
8336   (let ((total (length gnus-newsgroup-data))
8337         (data (gnus-data-find-list (gnus-summary-article-number)))
8338         (gnus-summary-mark-below nil)   ; Inhibit this.
8339         found)
8340     ;; This will do all the work of generating the new summary buffer
8341     ;; according to the new limit.
8342     (gnus-summary-prepare)
8343     ;; Hide any threads, possibly.
8344     (gnus-summary-maybe-hide-threads)
8345     ;; Try to return to the article you were at, or one in the
8346     ;; neighborhood.
8347     (when data
8348       ;; We try to find some article after the current one.
8349       (while data
8350         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8351           (setq data nil
8352                 found t))
8353         (setq data (cdr data))))
8354     (unless found
8355       ;; If there is no data, that means that we were after the last
8356       ;; article.  The same goes when we can't find any articles
8357       ;; after the current one.
8358       (goto-char (point-max))
8359       (gnus-summary-find-prev))
8360     (gnus-set-mode-line 'summary)
8361     ;; We return how many articles were removed from the summary
8362     ;; buffer as a result of the new limit.
8363     (- total (length gnus-newsgroup-data))))
8364
8365 (defsubst gnus-invisible-cut-children (threads)
8366   (let ((num 0))
8367     (while threads
8368       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8369         (incf num))
8370       (pop threads))
8371     (< num 2)))
8372
8373 (defsubst gnus-cut-thread (thread)
8374   "Go forwards in the thread until we find an article that we want to display."
8375   (when (or (eq gnus-fetch-old-headers 'some)
8376             (eq gnus-fetch-old-headers 'invisible)
8377             (numberp gnus-fetch-old-headers)
8378             (eq gnus-build-sparse-threads 'some)
8379             (eq gnus-build-sparse-threads 'more))
8380     ;; Deal with old-fetched headers and sparse threads.
8381     (while (and
8382             thread
8383             (or
8384              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8385              (gnus-summary-article-ancient-p
8386               (mail-header-number (car thread))))
8387             (if (or (<= (length (cdr thread)) 1)
8388                     (eq gnus-fetch-old-headers 'invisible))
8389                 (setq gnus-newsgroup-limit
8390                       (delq (mail-header-number (car thread))
8391                             gnus-newsgroup-limit)
8392                       thread (cadr thread))
8393               (when (gnus-invisible-cut-children (cdr thread))
8394                 (let ((th (cdr thread)))
8395                   (while th
8396                     (if (memq (mail-header-number (caar th))
8397                               gnus-newsgroup-limit)
8398                         (setq thread (car th)
8399                               th nil)
8400                       (setq th (cdr th))))))))))
8401   thread)
8402
8403 (defun gnus-cut-threads (threads)
8404   "Cut off all uninteresting articles from the beginning of THREADS."
8405   (when (or (eq gnus-fetch-old-headers 'some)
8406             (eq gnus-fetch-old-headers 'invisible)
8407             (numberp gnus-fetch-old-headers)
8408             (eq gnus-build-sparse-threads 'some)
8409             (eq gnus-build-sparse-threads 'more))
8410     (let ((th threads))
8411       (while th
8412         (setcar th (gnus-cut-thread (car th)))
8413         (setq th (cdr th)))))
8414   ;; Remove nixed out threads.
8415   (delq nil threads))
8416
8417 (defun gnus-summary-initial-limit (&optional show-if-empty)
8418   "Figure out what the initial limit is supposed to be on group entry.
8419 This entails weeding out unwanted dormants, low-scored articles,
8420 fetch-old-headers verbiage, and so on."
8421   ;; Most groups have nothing to remove.
8422   (if (or gnus-inhibit-limiting
8423           (and (null gnus-newsgroup-dormant)
8424                (eq gnus-newsgroup-display 'gnus-not-ignore)
8425                (not (eq gnus-fetch-old-headers 'some))
8426                (not (numberp gnus-fetch-old-headers))
8427                (not (eq gnus-fetch-old-headers 'invisible))
8428                (null gnus-summary-expunge-below)
8429                (not (eq gnus-build-sparse-threads 'some))
8430                (not (eq gnus-build-sparse-threads 'more))
8431                (null gnus-thread-expunge-below)
8432                (not gnus-use-nocem)))
8433       ()                                ; Do nothing.
8434     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8435     (setq gnus-newsgroup-limit nil)
8436     (mapatoms
8437      (lambda (node)
8438        (unless (car (symbol-value node))
8439          ;; These threads have no parents -- they are roots.
8440          (let ((nodes (cdr (symbol-value node)))
8441                thread)
8442            (while nodes
8443              (if (and gnus-thread-expunge-below
8444                       (< (gnus-thread-total-score (car nodes))
8445                          gnus-thread-expunge-below))
8446                  (gnus-expunge-thread (pop nodes))
8447                (setq thread (pop nodes))
8448                (gnus-summary-limit-children thread))))))
8449      gnus-newsgroup-dependencies)
8450     ;; If this limitation resulted in an empty group, we might
8451     ;; pop the previous limit and use it instead.
8452     (when (and (not gnus-newsgroup-limit)
8453                show-if-empty)
8454       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8455     gnus-newsgroup-limit))
8456
8457 (defun gnus-summary-limit-children (thread)
8458   "Return 1 if this subthread is visible and 0 if it is not."
8459   ;; First we get the number of visible children to this thread.  This
8460   ;; is done by recursing down the thread using this function, so this
8461   ;; will really go down to a leaf article first, before slowly
8462   ;; working its way up towards the root.
8463   (when thread
8464     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8465            (children
8466            (if (cdr thread)
8467                (apply '+ (mapcar 'gnus-summary-limit-children
8468                                  (cdr thread)))
8469              0))
8470           (number (mail-header-number (car thread)))
8471           score)
8472       (if (and
8473            (not (memq number gnus-newsgroup-marked))
8474            (or
8475             ;; If this article is dormant and has absolutely no visible
8476             ;; children, then this article isn't visible.
8477             (and (memq number gnus-newsgroup-dormant)
8478                  (zerop children))
8479             ;; If this is "fetch-old-headered" and there is no
8480             ;; visible children, then we don't want this article.
8481             (and (or (eq gnus-fetch-old-headers 'some)
8482                      (numberp gnus-fetch-old-headers))
8483                  (gnus-summary-article-ancient-p number)
8484                  (zerop children))
8485             ;; If this is "fetch-old-headered" and `invisible', then
8486             ;; we don't want this article.
8487             (and (eq gnus-fetch-old-headers 'invisible)
8488                  (gnus-summary-article-ancient-p number))
8489             ;; If this is a sparsely inserted article with no children,
8490             ;; we don't want it.
8491             (and (eq gnus-build-sparse-threads 'some)
8492                  (gnus-summary-article-sparse-p number)
8493                  (zerop children))
8494             ;; If we use expunging, and this article is really
8495             ;; low-scored, then we don't want this article.
8496             (when (and gnus-summary-expunge-below
8497                        (< (setq score
8498                                 (or (cdr (assq number gnus-newsgroup-scored))
8499                                     gnus-summary-default-score))
8500                           gnus-summary-expunge-below))
8501               ;; We increase the expunge-tally here, but that has
8502               ;; nothing to do with the limits, really.
8503               (incf gnus-newsgroup-expunged-tally)
8504               ;; We also mark as read here, if that's wanted.
8505               (when (and gnus-summary-mark-below
8506                          (< score gnus-summary-mark-below))
8507                 (setq gnus-newsgroup-unreads
8508                       (delq number gnus-newsgroup-unreads))
8509                 (if gnus-newsgroup-auto-expire
8510                     (push number gnus-newsgroup-expirable)
8511                   (push (cons number gnus-low-score-mark)
8512                         gnus-newsgroup-reads)))
8513               t)
8514             ;; Do the `display' group parameter.
8515             (and gnus-newsgroup-display
8516                  (not (funcall gnus-newsgroup-display)))
8517             ;; Check NoCeM things.
8518             (when (and gnus-use-nocem
8519                        (gnus-nocem-unwanted-article-p
8520                         (mail-header-id (car thread))))
8521               (setq gnus-newsgroup-unreads
8522                     (delq number gnus-newsgroup-unreads))
8523               t)))
8524           ;; Nope, invisible article.
8525           0
8526         ;; Ok, this article is to be visible, so we add it to the limit
8527         ;; and return 1.
8528         (push number gnus-newsgroup-limit)
8529         1))))
8530
8531 (defun gnus-expunge-thread (thread)
8532   "Mark all articles in THREAD as read."
8533   (let* ((number (mail-header-number (car thread))))
8534     (incf gnus-newsgroup-expunged-tally)
8535     ;; We also mark as read here, if that's wanted.
8536     (setq gnus-newsgroup-unreads
8537           (delq number gnus-newsgroup-unreads))
8538     (if gnus-newsgroup-auto-expire
8539         (push number gnus-newsgroup-expirable)
8540       (push (cons number gnus-low-score-mark)
8541             gnus-newsgroup-reads)))
8542   ;; Go recursively through all subthreads.
8543   (mapcar 'gnus-expunge-thread (cdr thread)))
8544
8545 ;; Summary article oriented commands
8546
8547 (defun gnus-summary-refer-parent-article (n)
8548   "Refer parent article N times.
8549 If N is negative, go to ancestor -N instead.
8550 The difference between N and the number of articles fetched is returned."
8551   (interactive "p")
8552   (let ((skip 1)
8553         error header ref)
8554     (when (not (natnump n))
8555       (setq skip (abs n)
8556             n 1))
8557     (while (and (> n 0)
8558                 (not error))
8559       (setq header (gnus-summary-article-header))
8560       (if (and (eq (mail-header-number header)
8561                    (cdr gnus-article-current))
8562                (equal gnus-newsgroup-name
8563                       (car gnus-article-current)))
8564           ;; If we try to find the parent of the currently
8565           ;; displayed article, then we take a look at the actual
8566           ;; References header, since this is slightly more
8567           ;; reliable than the References field we got from the
8568           ;; server.
8569           (save-excursion
8570             (set-buffer gnus-original-article-buffer)
8571             (nnheader-narrow-to-headers)
8572             (unless (setq ref (message-fetch-field "references"))
8573               (when (setq ref (message-fetch-field "in-reply-to"))
8574                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8575             (widen))
8576         (setq ref
8577               ;; It's not the current article, so we take a bet on
8578               ;; the value we got from the server.
8579               (mail-header-references header)))
8580       (if (and ref
8581                (not (equal ref "")))
8582           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8583             (gnus-message 1 "Couldn't find parent"))
8584         (gnus-message 1 "No references in article %d"
8585                       (gnus-summary-article-number))
8586         (setq error t))
8587       (decf n))
8588     (gnus-summary-position-point)
8589     n))
8590
8591 (defun gnus-summary-refer-references ()
8592   "Fetch all articles mentioned in the References header.
8593 Return the number of articles fetched."
8594   (interactive)
8595   (let ((ref (mail-header-references (gnus-summary-article-header)))
8596         (current (gnus-summary-article-number))
8597         (n 0))
8598     (if (or (not ref)
8599             (equal ref ""))
8600         (error "No References in the current article")
8601       ;; For each Message-ID in the References header...
8602       (while (string-match "<[^>]*>" ref)
8603         (incf n)
8604         ;; ... fetch that article.
8605         (gnus-summary-refer-article
8606          (prog1 (match-string 0 ref)
8607            (setq ref (substring ref (match-end 0))))))
8608       (gnus-summary-goto-subject current)
8609       (gnus-summary-position-point)
8610       n)))
8611
8612 (defun gnus-summary-refer-thread (&optional limit)
8613   "Fetch all articles in the current thread.
8614 If LIMIT (the numerical prefix), fetch that many old headers instead
8615 of what's specified by the `gnus-refer-thread-limit' variable."
8616   (interactive "P")
8617   (let ((id (mail-header-id (gnus-summary-article-header)))
8618         (limit (if limit (prefix-numeric-value limit)
8619                  gnus-refer-thread-limit)))
8620     (unless (eq gnus-fetch-old-headers 'invisible)
8621       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8622       ;; Retrieve the headers and read them in.
8623       (if (eq (if (numberp limit)
8624                   (gnus-retrieve-headers
8625                    (list (min
8626                           (+ (mail-header-number
8627                               (gnus-summary-article-header))
8628                              limit)
8629                           gnus-newsgroup-end))
8630                    gnus-newsgroup-name (* limit 2))
8631                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8632                 ;; headers.
8633                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8634                                        gnus-newsgroup-name limit))
8635               'nov)
8636           (gnus-build-all-threads)
8637         (error "Can't fetch thread from back ends that don't support NOV"))
8638       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8639     (gnus-summary-limit-include-thread id)))
8640
8641 (defun gnus-summary-refer-article (message-id)
8642   "Fetch an article specified by MESSAGE-ID."
8643   (interactive "sMessage-ID: ")
8644   (when (and (stringp message-id)
8645              (not (zerop (length message-id))))
8646     (setq message-id (gnus-replace-in-string message-id " " ""))
8647     ;; Construct the correct Message-ID if necessary.
8648     ;; Suggested by tale@pawl.rpi.edu.
8649     (unless (string-match "^<" message-id)
8650       (setq message-id (concat "<" message-id)))
8651     (unless (string-match ">$" message-id)
8652       (setq message-id (concat message-id ">")))
8653     ;; People often post MIDs from URLs, so unhex it:
8654     (unless (string-match "@" message-id)
8655       (setq message-id (gnus-url-unhex-string message-id)))
8656     (let* ((header (gnus-id-to-header message-id))
8657            (sparse (and header
8658                         (gnus-summary-article-sparse-p
8659                          (mail-header-number header))
8660                         (memq (mail-header-number header)
8661                               gnus-newsgroup-limit)))
8662            number)
8663       (cond
8664        ;; If the article is present in the buffer we just go to it.
8665        ((and header
8666              (or (not (gnus-summary-article-sparse-p
8667                        (mail-header-number header)))
8668                  sparse))
8669         (prog1
8670             (gnus-summary-goto-article
8671              (mail-header-number header) nil t)
8672           (when sparse
8673             (gnus-summary-update-article (mail-header-number header)))))
8674        (t
8675         ;; We fetch the article.
8676         (catch 'found
8677           (dolist (gnus-override-method (gnus-refer-article-methods))
8678             (when (and (gnus-check-server gnus-override-method)
8679                        ;; Fetch the header,
8680                        (setq number (gnus-summary-insert-subject message-id)))
8681               ;; and display the article.
8682               (gnus-summary-select-article nil nil nil number)
8683               (throw 'found t)))
8684           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8685
8686 (defun gnus-refer-article-methods ()
8687   "Return a list of referable methods."
8688   (cond
8689    ;; No method, so we default to current and native.
8690    ((null gnus-refer-article-method)
8691     (list gnus-current-select-method gnus-select-method))
8692    ;; Current.
8693    ((eq 'current gnus-refer-article-method)
8694     (list gnus-current-select-method))
8695    ;; List of select methods.
8696    ((not (and (symbolp (car gnus-refer-article-method))
8697               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8698     (let (out)
8699       (dolist (method gnus-refer-article-method)
8700         (push (if (eq 'current method)
8701                   gnus-current-select-method
8702                 method)
8703               out))
8704       (nreverse out)))
8705    ;; One single select method.
8706    (t
8707     (list gnus-refer-article-method))))
8708
8709 (defun gnus-summary-edit-parameters ()
8710   "Edit the group parameters of the current group."
8711   (interactive)
8712   (gnus-group-edit-group gnus-newsgroup-name 'params))
8713
8714 (defun gnus-summary-customize-parameters ()
8715   "Customize the group parameters of the current group."
8716   (interactive)
8717   (gnus-group-customize gnus-newsgroup-name))
8718
8719 (defun gnus-summary-enter-digest-group (&optional force)
8720   "Enter an nndoc group based on the current article.
8721 If FORCE, force a digest interpretation.  If not, try
8722 to guess what the document format is."
8723   (interactive "P")
8724   (let ((conf gnus-current-window-configuration))
8725     (save-window-excursion
8726       (save-excursion
8727         (let (gnus-article-prepare-hook
8728               gnus-display-mime-function
8729               gnus-break-pages)
8730           (gnus-summary-select-article))))
8731     (setq gnus-current-window-configuration conf)
8732     (let* ((name (format "%s-%d"
8733                          (gnus-group-prefixed-name
8734                           gnus-newsgroup-name (list 'nndoc ""))
8735                          (with-current-buffer gnus-summary-buffer
8736                            gnus-current-article)))
8737            (ogroup gnus-newsgroup-name)
8738            (params (append (gnus-info-params (gnus-get-info ogroup))
8739                            (list (cons 'to-group ogroup))
8740                            (list (cons 'parent-group ogroup))
8741                            (list (cons 'save-article-group ogroup))))
8742            (case-fold-search t)
8743            (buf (current-buffer))
8744            dig to-address)
8745       (save-excursion
8746         (set-buffer gnus-original-article-buffer)
8747         ;; Have the digest group inherit the main mail address of
8748         ;; the parent article.
8749         (when (setq to-address (or (gnus-fetch-field "reply-to")
8750                                    (gnus-fetch-field "from")))
8751           (setq params
8752                 (append
8753                  (list (cons 'to-address
8754                              (funcall gnus-decode-encoded-address-function
8755                                       to-address))))))
8756         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8757         (insert-buffer-substring gnus-original-article-buffer)
8758         ;; Remove lines that may lead nndoc to misinterpret the
8759         ;; document type.
8760         (narrow-to-region
8761          (goto-char (point-min))
8762          (or (search-forward "\n\n" nil t) (point)))
8763         (goto-char (point-min))
8764         (delete-matching-lines "^Path:\\|^From ")
8765         (widen))
8766       (unwind-protect
8767           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8768                     (gnus-newsgroup-ephemeral-ignored-charsets
8769                      gnus-newsgroup-ignored-charsets))
8770                 (gnus-group-read-ephemeral-group
8771                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8772                               (nndoc-article-type
8773                                ,(if force 'mbox 'guess)))
8774                  t nil nil nil
8775                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8776                                                         "ADAPT")))))
8777               ;; Make all postings to this group go to the parent group.
8778               (nconc (gnus-info-params (gnus-get-info name))
8779                      params)
8780             ;; Couldn't select this doc group.
8781             (switch-to-buffer buf)
8782             (gnus-set-global-variables)
8783             (gnus-configure-windows 'summary)
8784             (gnus-message 3 "Article couldn't be entered?"))
8785         (kill-buffer dig)))))
8786
8787 (defun gnus-summary-read-document (n)
8788   "Open a new group based on the current article(s).
8789 This will allow you to read digests and other similar
8790 documents as newsgroups.
8791 Obeys the standard process/prefix convention."
8792   (interactive "P")
8793   (let* ((ogroup gnus-newsgroup-name)
8794          (params (append (gnus-info-params (gnus-get-info ogroup))
8795                          (list (cons 'to-group ogroup))))
8796          group egroup groups vgroup)
8797     (dolist (article (gnus-summary-work-articles n))
8798       (setq group (format "%s-%d" gnus-newsgroup-name article))
8799       (gnus-summary-remove-process-mark article)
8800       (when (gnus-summary-display-article article)
8801         (save-excursion
8802           (with-temp-buffer
8803             (insert-buffer-substring gnus-original-article-buffer)
8804             ;; Remove some headers that may lead nndoc to make
8805             ;; the wrong guess.
8806             (message-narrow-to-head)
8807             (goto-char (point-min))
8808             (delete-matching-lines "^Path:\\|^From ")
8809             (widen)
8810             (if (setq egroup
8811                       (gnus-group-read-ephemeral-group
8812                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8813                                      (nndoc-article-type guess))
8814                        t nil t))
8815                 (progn
8816             ;; Make all postings to this group go to the parent group.
8817                   (nconc (gnus-info-params (gnus-get-info egroup))
8818                          params)
8819                   (push egroup groups))
8820               ;; Couldn't select this doc group.
8821               (gnus-error 3 "Article couldn't be entered"))))))
8822     ;; Now we have selected all the documents.
8823     (cond
8824      ((not groups)
8825       (error "None of the articles could be interpreted as documents"))
8826      ((gnus-group-read-ephemeral-group
8827        (setq vgroup (format
8828                      "nnvirtual:%s-%s" gnus-newsgroup-name
8829                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8830        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8831        t
8832        (cons (current-buffer) 'summary)))
8833      (t
8834       (error "Couldn't select virtual nndoc group")))))
8835
8836 (defun gnus-summary-isearch-article (&optional regexp-p)
8837   "Do incremental search forward on the current article.
8838 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8839   (interactive "P")
8840   (gnus-summary-select-article)
8841   (gnus-configure-windows 'article)
8842   (gnus-eval-in-buffer-window gnus-article-buffer
8843     (save-restriction
8844       (widen)
8845       (isearch-forward regexp-p))))
8846
8847 (defun gnus-summary-repeat-search-article-forward ()
8848   "Repeat the previous search forwards."
8849   (interactive)
8850   (unless gnus-last-search-regexp
8851     (error "No previous search"))
8852   (gnus-summary-search-article-forward gnus-last-search-regexp))
8853
8854 (defun gnus-summary-repeat-search-article-backward ()
8855   "Repeat the previous search backwards."
8856   (interactive)
8857   (unless gnus-last-search-regexp
8858     (error "No previous search"))
8859   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8860
8861 (defun gnus-summary-search-article-forward (regexp &optional backward)
8862   "Search for an article containing REGEXP forward.
8863 If BACKWARD, search backward instead."
8864   (interactive
8865    (list (read-string
8866           (format "Search article %s (regexp%s): "
8867                   (if current-prefix-arg "backward" "forward")
8868                   (if gnus-last-search-regexp
8869                       (concat ", default " gnus-last-search-regexp)
8870                     "")))
8871          current-prefix-arg))
8872   (if (string-equal regexp "")
8873       (setq regexp (or gnus-last-search-regexp ""))
8874     (setq gnus-last-search-regexp regexp)
8875     (setq gnus-article-before-search gnus-current-article))
8876   ;; Intentionally set gnus-last-article.
8877   (setq gnus-last-article gnus-article-before-search)
8878   (let ((gnus-last-article gnus-last-article))
8879     (if (gnus-summary-search-article regexp backward)
8880         (gnus-summary-show-thread)
8881       (signal 'search-failed (list regexp)))))
8882
8883 (defun gnus-summary-search-article-backward (regexp)
8884   "Search for an article containing REGEXP backward."
8885   (interactive
8886    (list (read-string
8887           (format "Search article backward (regexp%s): "
8888                   (if gnus-last-search-regexp
8889                       (concat ", default " gnus-last-search-regexp)
8890                     "")))))
8891   (gnus-summary-search-article-forward regexp 'backward))
8892
8893 (defun gnus-summary-search-article (regexp &optional backward)
8894   "Search for an article containing REGEXP.
8895 Optional argument BACKWARD means do search for backward.
8896 `gnus-select-article-hook' is not called during the search."
8897   ;; We have to require this here to make sure that the following
8898   ;; dynamic binding isn't shadowed by autoloading.
8899   (require 'gnus-async)
8900   (require 'gnus-art)
8901   (let ((gnus-select-article-hook nil)  ;Disable hook.
8902         (gnus-article-prepare-hook nil)
8903         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8904         (gnus-use-article-prefetch nil)
8905         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8906         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8907         (gnus-visual nil)
8908         (gnus-keep-backlog nil)
8909         (gnus-break-pages nil)
8910         (gnus-summary-display-arrow nil)
8911         (gnus-updated-mode-lines nil)
8912         (gnus-auto-center-summary nil)
8913         (sum (current-buffer))
8914         (gnus-display-mime-function nil)
8915         (found nil)
8916         point)
8917     (gnus-save-hidden-threads
8918       (gnus-summary-select-article)
8919       (set-buffer gnus-article-buffer)
8920       (goto-char (window-point (get-buffer-window (current-buffer))))
8921       (when backward
8922         (forward-line -1))
8923       (while (not found)
8924         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8925         (if (if backward
8926                 (re-search-backward regexp nil t)
8927               (re-search-forward regexp nil t))
8928             ;; We found the regexp.
8929             (progn
8930               (setq found 'found)
8931               (beginning-of-line)
8932               (set-window-start
8933                (get-buffer-window (current-buffer))
8934                (point))
8935               (forward-line 1)
8936               (set-window-point
8937                (get-buffer-window (current-buffer))
8938                (point))
8939               (set-buffer sum)
8940               (setq point (point)))
8941           ;; We didn't find it, so we go to the next article.
8942           (set-buffer sum)
8943           (setq found 'not)
8944           (while (eq found 'not)
8945             (if (not (if backward (gnus-summary-find-prev)
8946                        (gnus-summary-find-next)))
8947                 ;; No more articles.
8948                 (setq found t)
8949               ;; Select the next article and adjust point.
8950               (unless (gnus-summary-article-sparse-p
8951                        (gnus-summary-article-number))
8952                 (setq found nil)
8953                 (gnus-summary-select-article)
8954                 (set-buffer gnus-article-buffer)
8955                 (widen)
8956                 (goto-char (if backward (point-max) (point-min))))))))
8957       (gnus-message 7 ""))
8958     ;; Return whether we found the regexp.
8959     (when (eq found 'found)
8960       (goto-char point)
8961       (gnus-summary-show-thread)
8962       (gnus-summary-goto-subject gnus-current-article)
8963       (gnus-summary-position-point)
8964       t)))
8965
8966 (defun gnus-find-matching-articles (header regexp)
8967   "Return a list of all articles that match REGEXP on HEADER.
8968 This search includes all articles in the current group that Gnus has
8969 fetched headers for, whether they are displayed or not."
8970   (let ((articles nil)
8971         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8972         (case-fold-search t))
8973     (dolist (header gnus-newsgroup-headers)
8974       (when (string-match regexp (funcall func header))
8975         (push (mail-header-number header) articles)))
8976     (nreverse articles)))
8977
8978 (defun gnus-summary-find-matching (header regexp &optional backward unread
8979                                           not-case-fold not-matching)
8980   "Return a list of all articles that match REGEXP on HEADER.
8981 The search stars on the current article and goes forwards unless
8982 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8983 If UNREAD is non-nil, only unread articles will
8984 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8985 in the comparisons. If NOT-MATCHING, return a list of all articles that
8986 not match REGEXP on HEADER."
8987   (let ((case-fold-search (not not-case-fold))
8988         articles d func)
8989     (if (consp header)
8990         (if (eq (car header) 'extra)
8991             (setq func
8992                   `(lambda (h)
8993                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8994                          "")))
8995           (error "%s is an invalid header" header))
8996       (unless (fboundp (intern (concat "mail-header-" header)))
8997         (error "%s is not a valid header" header))
8998       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8999     (dolist (d (if (eq backward 'all)
9000                    gnus-newsgroup-data
9001                  (gnus-data-find-list
9002                   (gnus-summary-article-number)
9003                   (gnus-data-list backward))))
9004       (when (and (or (not unread)       ; We want all articles...
9005                      (gnus-data-unread-p d)) ; Or just unreads.
9006                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9007                  (if not-matching
9008                      (not (string-match
9009                            regexp
9010                            (funcall func (gnus-data-header d))))
9011                    (string-match regexp
9012                                  (funcall func (gnus-data-header d)))))
9013         (push (gnus-data-number d) articles))) ; Success!
9014     (nreverse articles)))
9015
9016 (defun gnus-summary-execute-command (header regexp command &optional backward)
9017   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9018 If HEADER is an empty string (or nil), the match is done on the entire
9019 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9020   (interactive
9021    (list (let ((completion-ignore-case t))
9022            (completing-read
9023             "Header name: "
9024             (mapcar (lambda (header) (list (format "%s" header)))
9025                     (append
9026                      '("Number" "Subject" "From" "Lines" "Date"
9027                        "Message-ID" "Xref" "References" "Body")
9028                      gnus-extra-headers))
9029             nil 'require-match))
9030          (read-string "Regexp: ")
9031          (read-key-sequence "Command: ")
9032          current-prefix-arg))
9033   (when (equal header "Body")
9034     (setq header ""))
9035   ;; Hidden thread subtrees must be searched as well.
9036   (gnus-summary-show-all-threads)
9037   ;; We don't want to change current point nor window configuration.
9038   (save-excursion
9039     (save-window-excursion
9040       (let (gnus-visual
9041             gnus-treat-strip-trailing-blank-lines
9042             gnus-treat-strip-leading-blank-lines
9043             gnus-treat-strip-multiple-blank-lines
9044             gnus-treat-hide-boring-headers
9045             gnus-treat-fold-newsgroups
9046             gnus-article-prepare-hook)
9047         (gnus-message 6 "Executing %s..." (key-description command))
9048         ;; We'd like to execute COMMAND interactively so as to give arguments.
9049         (gnus-execute header regexp
9050                       `(call-interactively ',(key-binding command))
9051                       backward)
9052         (gnus-message 6 "Executing %s...done" (key-description command))))))
9053
9054 (defun gnus-summary-beginning-of-article ()
9055   "Scroll the article back to the beginning."
9056   (interactive)
9057   (gnus-summary-select-article)
9058   (gnus-configure-windows 'article)
9059   (gnus-eval-in-buffer-window gnus-article-buffer
9060     (widen)
9061     (goto-char (point-min))
9062     (when gnus-break-pages
9063       (gnus-narrow-to-page))))
9064
9065 (defun gnus-summary-end-of-article ()
9066   "Scroll to the end of the article."
9067   (interactive)
9068   (gnus-summary-select-article)
9069   (gnus-configure-windows 'article)
9070   (gnus-eval-in-buffer-window gnus-article-buffer
9071     (widen)
9072     (goto-char (point-max))
9073     (recenter -3)
9074     (when gnus-break-pages
9075       (when (re-search-backward page-delimiter nil t)
9076         (narrow-to-region (match-end 0) (point-max)))
9077       (gnus-narrow-to-page))))
9078
9079 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9080   "Truncate to LEN and quote all \"(\"'s in STRING."
9081   (gnus-replace-in-string (if (and len (> (length string) len))
9082                               (substring string 0 len)
9083                             string)
9084                           "[()]" "\\\\\\&"))
9085
9086 (defun gnus-summary-print-article (&optional filename n)
9087   "Generate and print a PostScript image of the process-marked (mail) articles.
9088
9089 If used interactively, print the current article if none are
9090 process-marked.  With prefix arg, prompt the user for the name of the
9091 file to save in.
9092
9093 When used from Lisp, accept two optional args FILENAME and N.  N means
9094 to print the next N articles.  If N is negative, print the N previous
9095 articles.  If N is nil and articles have been marked with the process
9096 mark, print these instead.
9097
9098 If the optional first argument FILENAME is nil, send the image to the
9099 printer.  If FILENAME is a string, save the PostScript image in a file with
9100 that name.  If FILENAME is a number, prompt the user for the name of the file
9101 to save in."
9102   (interactive (list (ps-print-preprint current-prefix-arg)))
9103   (dolist (article (gnus-summary-work-articles n))
9104     (gnus-summary-select-article nil nil 'pseudo article)
9105     (gnus-eval-in-buffer-window gnus-article-buffer
9106       (gnus-print-buffer))
9107     (gnus-summary-remove-process-mark article))
9108   (ps-despool filename))
9109
9110 (defun gnus-print-buffer ()
9111   (let ((buffer (generate-new-buffer " *print*")))
9112     (unwind-protect
9113         (progn
9114           (copy-to-buffer buffer (point-min) (point-max))
9115           (set-buffer buffer)
9116           (gnus-remove-text-with-property 'gnus-decoration)
9117           (when (gnus-visual-p 'article-highlight 'highlight)
9118             ;; Copy-to-buffer doesn't copy overlay.  So redo
9119             ;; highlight.
9120             (let ((gnus-article-buffer buffer))
9121               (gnus-article-highlight-citation t)
9122               (gnus-article-highlight-signature)
9123               (gnus-article-emphasize)
9124               (gnus-article-delete-invisible-text)))
9125           (let ((ps-left-header
9126                  (list
9127                   (concat "("
9128                           (gnus-summary-print-truncate-and-quote
9129                            (mail-header-subject gnus-current-headers)
9130                            66) ")")
9131                   (concat "("
9132                           (gnus-summary-print-truncate-and-quote
9133                            (mail-header-from gnus-current-headers)
9134                            45) ")")))
9135                 (ps-right-header
9136                  (list
9137                   "/pagenumberstring load"
9138                   (concat "("
9139                           (mail-header-date gnus-current-headers) ")"))))
9140             (gnus-run-hooks 'gnus-ps-print-hook)
9141             (save-excursion
9142               (if window-system
9143                   (ps-spool-buffer-with-faces)
9144                 (ps-spool-buffer)))))
9145       (kill-buffer buffer))))
9146
9147 (defun gnus-summary-show-article (&optional arg)
9148   "Force redisplaying of the current article.
9149 If ARG (the prefix) is a number, show the article with the charset
9150 defined in `gnus-summary-show-article-charset-alist', or the charset
9151 input.
9152 If ARG (the prefix) is non-nil and not a number, show the raw article
9153 without any article massaging functions being run.  Normally, the key
9154 strokes are `C-u g'."
9155   (interactive "P")
9156   (cond
9157    ((numberp arg)
9158     (gnus-summary-show-article t)
9159     (let ((gnus-newsgroup-charset
9160            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9161                (mm-read-coding-system
9162                 "View as charset: " ;; actually it is coding system.
9163                 (with-current-buffer gnus-article-buffer
9164                   (mm-detect-coding-region (point) (point-max))))))
9165           (gnus-newsgroup-ignored-charsets 'gnus-all))
9166       (gnus-summary-select-article nil 'force)
9167       (let ((deps gnus-newsgroup-dependencies)
9168             head header lines)
9169         (save-excursion
9170           (set-buffer gnus-original-article-buffer)
9171           (save-restriction
9172             (message-narrow-to-head)
9173             (setq head (buffer-string))
9174             (goto-char (point-min))
9175             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9176               (goto-char (point-max))
9177               (widen)
9178               (setq lines (1- (count-lines (point) (point-max))))))
9179           (with-temp-buffer
9180             (insert (format "211 %d Article retrieved.\n"
9181                             (cdr gnus-article-current)))
9182             (insert head)
9183             (if lines (insert (format "Lines: %d\n" lines)))
9184             (insert ".\n")
9185             (let ((nntp-server-buffer (current-buffer)))
9186               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9187         (gnus-data-set-header
9188          (gnus-data-find (cdr gnus-article-current))
9189          header)
9190         (gnus-summary-update-article-line
9191          (cdr gnus-article-current) header)
9192         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9193           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9194    ((not arg)
9195     ;; Select the article the normal way.
9196     (gnus-summary-select-article nil 'force))
9197    (t
9198     ;; We have to require this here to make sure that the following
9199     ;; dynamic binding isn't shadowed by autoloading.
9200     (require 'gnus-async)
9201     (require 'gnus-art)
9202     ;; Bind the article treatment functions to nil.
9203     (let ((gnus-have-all-headers t)
9204           gnus-article-prepare-hook
9205           gnus-article-decode-hook
9206           gnus-display-mime-function
9207           gnus-break-pages)
9208       ;; Destroy any MIME parts.
9209       (when (gnus-buffer-live-p gnus-article-buffer)
9210         (save-excursion
9211           (set-buffer gnus-article-buffer)
9212           (mm-destroy-parts gnus-article-mime-handles)
9213           ;; Set it to nil for safety reason.
9214           (setq gnus-article-mime-handle-alist nil)
9215           (setq gnus-article-mime-handles nil)))
9216       (gnus-summary-select-article nil 'force))))
9217   (gnus-summary-goto-subject gnus-current-article)
9218   (gnus-summary-position-point))
9219
9220 (defun gnus-summary-show-raw-article ()
9221   "Show the raw article without any article massaging functions being run."
9222   (interactive)
9223   (gnus-summary-show-article t))
9224
9225 (defun gnus-summary-verbose-headers (&optional arg)
9226   "Toggle permanent full header display.
9227 If ARG is a positive number, turn header display on.
9228 If ARG is a negative number, turn header display off."
9229   (interactive "P")
9230   (setq gnus-show-all-headers
9231         (cond ((or (not (numberp arg))
9232                    (zerop arg))
9233                (not gnus-show-all-headers))
9234               ((natnump arg)
9235                t)))
9236   (gnus-summary-show-article))
9237
9238 (defun gnus-summary-toggle-header (&optional arg)
9239   "Show the headers if they are hidden, or hide them if they are shown.
9240 If ARG is a positive number, show the entire header.
9241 If ARG is a negative number, hide the unwanted header lines."
9242   (interactive "P")
9243   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9244                      (get-buffer-window gnus-article-buffer t))))
9245     (with-current-buffer gnus-article-buffer
9246       (widen)
9247       (article-narrow-to-head)
9248       (let* ((buffer-read-only nil)
9249              (inhibit-point-motion-hooks t)
9250              (hidden (if (numberp arg)
9251                          (>= arg 0)
9252                        (or (not (looking-at "[^ \t\n]+:"))
9253                            (gnus-article-hidden-text-p 'headers))))
9254              s e)
9255         (delete-region (point-min) (point-max))
9256         (with-current-buffer gnus-original-article-buffer
9257           (goto-char (setq s (point-min)))
9258           (setq e (if (search-forward "\n\n" nil t)
9259                       (1- (point))
9260                     (point-max))))
9261         (insert-buffer-substring gnus-original-article-buffer s e)
9262         (run-hooks 'gnus-article-decode-hook)
9263         (if hidden
9264             (let ((gnus-treat-hide-headers nil)
9265                   (gnus-treat-hide-boring-headers nil))
9266               (gnus-delete-wash-type 'headers)
9267               (gnus-treat-article 'head))
9268           (gnus-treat-article 'head))
9269         (widen)
9270         (if window
9271             (set-window-start window (goto-char (point-min))))
9272         (if gnus-break-pages
9273             (gnus-narrow-to-page)
9274           (when (gnus-visual-p 'page-marker)
9275             (let ((buffer-read-only nil))
9276               (gnus-remove-text-with-property 'gnus-prev)
9277               (gnus-remove-text-with-property 'gnus-next))))
9278         (gnus-set-mode-line 'article)))))
9279
9280 (defun gnus-summary-show-all-headers ()
9281   "Make all header lines visible."
9282   (interactive)
9283   (gnus-summary-toggle-header 1))
9284
9285 (defun gnus-summary-caesar-message (&optional arg)
9286   "Caesar rotate the current article by 13.
9287 With a non-numerical prefix, also rotate headers.  A numerical
9288 prefix specifies how many places to rotate each letter forward."
9289   (interactive "P")
9290   (gnus-summary-select-article)
9291   (let ((mail-header-separator ""))
9292     (gnus-eval-in-buffer-window gnus-article-buffer
9293       (save-restriction
9294         (widen)
9295         (let ((start (window-start))
9296               buffer-read-only)
9297           (if (equal arg '(4))
9298               (message-caesar-buffer-body nil t)
9299             (message-caesar-buffer-body arg))
9300           (set-window-start (get-buffer-window (current-buffer)) start)))))
9301   ;; Create buttons and stuff...
9302   (gnus-treat-article nil))
9303
9304 (defun gnus-summary-idna-message (&optional arg)
9305   "Decode IDNA encoded domain names in the current articles.
9306 IDNA encoded domain names looks like `xn--bar'.  If a string
9307 remain unencoded after running this function, it is likely an
9308 invalid IDNA string (`xn--bar' is invalid).
9309
9310 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9311 installed for this command to work."
9312   (interactive "P")
9313   (if (not (and (condition-case nil (require 'idna)
9314                   (file-error))
9315                 (mm-coding-system-p 'utf-8)
9316                 (executable-find (symbol-value 'idna-program))))
9317       (gnus-message
9318        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9319     (gnus-summary-select-article)
9320     (let ((mail-header-separator ""))
9321       (gnus-eval-in-buffer-window gnus-article-buffer
9322         (save-restriction
9323           (widen)
9324           (let ((start (window-start))
9325                 buffer-read-only)
9326             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9327               (replace-match (idna-to-unicode (match-string 1))))
9328             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9329
9330 (defun gnus-summary-morse-message (&optional arg)
9331   "Morse decode the current article."
9332   (interactive "P")
9333   (gnus-summary-select-article)
9334   (let ((mail-header-separator ""))
9335     (gnus-eval-in-buffer-window gnus-article-buffer
9336       (save-excursion
9337         (save-restriction
9338           (widen)
9339           (let ((pos (window-start))
9340                 buffer-read-only)
9341             (goto-char (point-min))
9342             (when (message-goto-body)
9343               (gnus-narrow-to-body))
9344             (goto-char (point-min))
9345             (while (search-forward "·" (point-max) t)
9346               (replace-match "."))
9347             (unmorse-region (point-min) (point-max))
9348             (widen)
9349             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9350
9351 (defun gnus-summary-stop-page-breaking ()
9352   "Stop page breaking in the current article."
9353   (interactive)
9354   (gnus-summary-select-article)
9355   (gnus-eval-in-buffer-window gnus-article-buffer
9356     (widen)
9357     (when (gnus-visual-p 'page-marker)
9358       (let ((buffer-read-only nil))
9359         (gnus-remove-text-with-property 'gnus-prev)
9360         (gnus-remove-text-with-property 'gnus-next))
9361       (setq gnus-page-broken nil))))
9362
9363 (defun gnus-summary-move-article (&optional n to-newsgroup
9364                                             select-method action)
9365   "Move the current article to a different newsgroup.
9366 If N is a positive number, move the N next articles.
9367 If N is a negative number, move the N previous articles.
9368 If N is nil and any articles have been marked with the process mark,
9369 move those articles instead.
9370 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9371 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9372 re-spool using this method.
9373
9374 When called interactively with TO-NEWSGROUP being nil, the value of
9375 the variable `gnus-move-split-methods' is used for finding a default
9376 for the target newsgroup.
9377
9378 For this function to work, both the current newsgroup and the
9379 newsgroup that you want to move to have to support the `request-move'
9380 and `request-accept' functions.
9381
9382 ACTION can be either `move' (the default), `crosspost' or `copy'."
9383   (interactive "P")
9384   (unless action
9385     (setq action 'move))
9386   ;; Check whether the source group supports the required functions.
9387   (cond ((and (eq action 'move)
9388               (not (gnus-check-backend-function
9389                     'request-move-article gnus-newsgroup-name)))
9390          (error "The current group does not support article moving"))
9391         ((and (eq action 'crosspost)
9392               (not (gnus-check-backend-function
9393                     'request-replace-article gnus-newsgroup-name)))
9394          (error "The current group does not support article editing")))
9395   (let ((articles (gnus-summary-work-articles n))
9396         (prefix (if (gnus-check-backend-function
9397                      'request-move-article gnus-newsgroup-name)
9398                     (funcall gnus-move-group-prefix-function
9399                              gnus-newsgroup-name)
9400                   ""))
9401         (names '((move "Move" "Moving")
9402                  (copy "Copy" "Copying")
9403                  (crosspost "Crosspost" "Crossposting")))
9404         (copy-buf (save-excursion
9405                     (nnheader-set-temp-buffer " *copy article*")))
9406         art-group to-method new-xref article to-groups articles-to-update-marks)
9407     (unless (assq action names)
9408       (error "Unknown action %s" action))
9409     ;; Read the newsgroup name.
9410     (when (and (not to-newsgroup)
9411                (not select-method))
9412       (if (and gnus-move-split-methods
9413                (not
9414                 (and (memq gnus-current-article articles)
9415                      (gnus-buffer-live-p gnus-original-article-buffer))))
9416           ;; When `gnus-move-split-methods' is non-nil, we have to
9417           ;; select an article to give `gnus-read-move-group-name' an
9418           ;; opportunity to suggest an appropriate default.  However,
9419           ;; we needn't render or mark the article.
9420           (let ((gnus-display-mime-function nil)
9421                 (gnus-article-prepare-hook nil)
9422                 (gnus-mark-article-hook nil))
9423             (gnus-summary-select-article nil nil nil (car articles))))
9424       (setq to-newsgroup
9425             (gnus-read-move-group-name
9426              (cadr (assq action names))
9427              (symbol-value (intern (format "gnus-current-%s-group" action)))
9428              articles prefix))
9429       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9430     (setq to-method (or select-method
9431                         (gnus-server-to-method
9432                          (gnus-group-method to-newsgroup))))
9433     ;; Check the method we are to move this article to...
9434     (unless (gnus-check-backend-function
9435              'request-accept-article (car to-method))
9436       (error "%s does not support article copying" (car to-method)))
9437     (unless (gnus-check-server to-method)
9438       (error "Can't open server %s" (car to-method)))
9439     (gnus-message 6 "%s to %s: %s..."
9440                   (caddr (assq action names))
9441                   (or (car select-method) to-newsgroup) articles)
9442     (while articles
9443       (setq article (pop articles))
9444       (setq
9445        art-group
9446        (cond
9447         ;; Move the article.
9448         ((eq action 'move)
9449          ;; Remove this article from future suppression.
9450          (gnus-dup-unsuppress-article article)
9451          (let* ((from-method (gnus-find-method-for-group
9452                               gnus-newsgroup-name))
9453                 (to-method (gnus-find-method-for-group
9454                             to-newsgroup))
9455                 (move-is-internal (gnus-method-equal from-method to-method)))
9456            (gnus-request-move-article
9457             article                     ; Article to move
9458             gnus-newsgroup-name         ; From newsgroup
9459             (nth 1 (gnus-find-method-for-group
9460                     gnus-newsgroup-name)) ; Server
9461             (list 'gnus-request-accept-article
9462                   to-newsgroup (list 'quote select-method)
9463                   (not articles) t)     ; Accept form
9464             (not articles)              ; Only save nov last time
9465             move-is-internal)))         ; is this move internal?
9466         ;; Copy the article.
9467         ((eq action 'copy)
9468          (save-excursion
9469            (set-buffer copy-buf)
9470            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9471              (save-restriction
9472                (nnheader-narrow-to-headers)
9473                (dolist (hdr gnus-copy-article-ignored-headers)
9474                  (message-remove-header hdr t)))
9475              (gnus-request-accept-article
9476               to-newsgroup select-method (not articles) t))))
9477         ;; Crosspost the article.
9478         ((eq action 'crosspost)
9479          (let ((xref (message-tokenize-header
9480                       (mail-header-xref (gnus-summary-article-header article))
9481                       " ")))
9482            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9483                                   ":" (number-to-string article)))
9484            (unless xref
9485              (setq xref (list (system-name))))
9486            (setq new-xref
9487                  (concat
9488                   (mapconcat 'identity
9489                              (delete "Xref:" (delete new-xref xref))
9490                              " ")
9491                   " " new-xref))
9492            (save-excursion
9493              (set-buffer copy-buf)
9494              ;; First put the article in the destination group.
9495              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9496              (when (consp (setq art-group
9497                                 (gnus-request-accept-article
9498                                  to-newsgroup select-method (not articles) t)))
9499                (setq new-xref (concat new-xref " " (car art-group)
9500                                       ":"
9501                                       (number-to-string (cdr art-group))))
9502                ;; Now we have the new Xrefs header, so we insert
9503                ;; it and replace the new article.
9504                (nnheader-replace-header "Xref" new-xref)
9505                (gnus-request-replace-article
9506                 (cdr art-group) to-newsgroup (current-buffer) t)
9507                art-group))))))
9508       (cond
9509        ((not art-group)
9510         (gnus-message 1 "Couldn't %s article %s: %s"
9511                       (cadr (assq action names)) article
9512                       (nnheader-get-report (car to-method))))
9513        ((eq art-group 'junk)
9514         (when (eq action 'move)
9515           (gnus-summary-mark-article article gnus-canceled-mark)
9516           (gnus-message 4 "Deleted article %s" article)
9517           ;; run the delete hook
9518           (run-hook-with-args 'gnus-summary-article-delete-hook
9519                               action
9520                               (gnus-data-header
9521                                (assoc article (gnus-data-list nil)))
9522                               gnus-newsgroup-name nil
9523                               select-method)))
9524        (t
9525         (let* ((pto-group (gnus-group-prefixed-name
9526                            (car art-group) to-method))
9527                (info (gnus-get-info pto-group))
9528                (to-group (gnus-info-group info))
9529                to-marks)
9530           ;; Update the group that has been moved to.
9531           (when (and info
9532                      (memq action '(move copy)))
9533             (unless (member to-group to-groups)
9534               (push to-group to-groups))
9535
9536             (unless (memq article gnus-newsgroup-unreads)
9537               (push 'read to-marks)
9538               (gnus-info-set-read
9539                info (gnus-add-to-range (gnus-info-read info)
9540                                        (list (cdr art-group)))))
9541
9542             ;; See whether the article is to be put in the cache.
9543             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9544                              gnus-article-mark-lists
9545                            (delete '(expirable . expire)
9546                                    (copy-sequence gnus-article-mark-lists))))
9547                   (to-article (cdr art-group)))
9548
9549               ;; Enter the article into the cache in the new group,
9550               ;; if that is required.
9551               (when gnus-use-cache
9552                 (gnus-cache-possibly-enter-article
9553                  to-group to-article
9554                  (memq article gnus-newsgroup-marked)
9555                  (memq article gnus-newsgroup-dormant)
9556                  (memq article gnus-newsgroup-unreads)))
9557
9558               (when gnus-preserve-marks
9559                 ;; Copy any marks over to the new group.
9560                 (when (and (equal to-group gnus-newsgroup-name)
9561                            (not (memq article gnus-newsgroup-unreads)))
9562                   ;; Mark this article as read in this group.
9563                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9564                   (setcdr (gnus-active to-group) to-article)
9565                   (setcdr gnus-newsgroup-active to-article))
9566
9567                 (while marks
9568                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9569                     (when (memq article (symbol-value
9570                                          (intern (format "gnus-newsgroup-%s"
9571                                                          (caar marks)))))
9572                       (push (cdar marks) to-marks)
9573                       ;; If the other group is the same as this group,
9574                       ;; then we have to add the mark to the list.
9575                       (when (equal to-group gnus-newsgroup-name)
9576                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9577                              (cons to-article
9578                                    (symbol-value
9579                                     (intern (format "gnus-newsgroup-%s"
9580                                                     (caar marks)))))))
9581                       ;; Copy the marks to other group.
9582                       (gnus-add-marked-articles
9583                        to-group (cdar marks) (list to-article) info)))
9584                   (setq marks (cdr marks)))
9585
9586                 (gnus-request-set-mark
9587                  to-group (list (list (list to-article) 'add to-marks))))
9588
9589               (gnus-dribble-enter
9590                (concat "(gnus-group-set-info '"
9591                        (gnus-prin1-to-string (gnus-get-info to-group))
9592                        ")"))))
9593
9594           ;; Update the Xref header in this article to point to
9595           ;; the new crossposted article we have just created.
9596           (when (eq action 'crosspost)
9597             (save-excursion
9598               (set-buffer copy-buf)
9599               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9600               (nnheader-replace-header "Xref" new-xref)
9601               (gnus-request-replace-article
9602                article gnus-newsgroup-name (current-buffer) t)))
9603
9604           ;; run the move/copy/crosspost/respool hook
9605           (run-hook-with-args 'gnus-summary-article-move-hook
9606                               action
9607                               (gnus-data-header
9608                                (assoc article (gnus-data-list nil)))
9609                               gnus-newsgroup-name
9610                               to-newsgroup
9611                               select-method))
9612
9613         ;;;!!!Why is this necessary?
9614         (set-buffer gnus-summary-buffer)
9615
9616         (gnus-summary-goto-subject article)
9617         (when (eq action 'move)
9618           (gnus-summary-mark-article article gnus-canceled-mark))))
9619       (push article articles-to-update-marks))
9620
9621     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9622     ;; Re-activate all groups that have been moved to.
9623     (save-excursion
9624       (set-buffer gnus-group-buffer)
9625       (let ((gnus-group-marked to-groups))
9626         (gnus-group-get-new-news-this-group nil t)))
9627
9628     (gnus-kill-buffer copy-buf)
9629     (gnus-summary-position-point)
9630     (gnus-set-mode-line 'summary)))
9631
9632 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9633   "Copy the current article to some other group.
9634 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9635 When called interactively, if TO-NEWSGROUP is nil, use the value of
9636 the variable `gnus-move-split-methods' for finding a default target
9637 newsgroup.
9638 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9639 re-spool using this method."
9640   (interactive "P")
9641   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9642
9643 (defun gnus-summary-crosspost-article (&optional n)
9644   "Crosspost the current article to some other group."
9645   (interactive "P")
9646   (gnus-summary-move-article n nil nil 'crosspost))
9647
9648 (defcustom gnus-summary-respool-default-method nil
9649   "Default method type for respooling an article.
9650 If nil, use to the current newsgroup method."
9651   :type 'symbol
9652   :group 'gnus-summary-mail)
9653
9654 (defcustom gnus-summary-display-while-building nil
9655   "If non-nil, show and update the summary buffer as it's being built.
9656 If the value is t, update the buffer after every line is inserted.  If
9657 the value is an integer (N), update the display every N lines."
9658   :version "22.1"
9659   :group 'gnus-thread
9660   :type '(choice (const :tag "off" nil)
9661                  number
9662                  (const :tag "frequently" t)))
9663
9664 (defun gnus-summary-respool-article (&optional n method)
9665   "Respool the current article.
9666 The article will be squeezed through the mail spooling process again,
9667 which means that it will be put in some mail newsgroup or other
9668 depending on `nnmail-split-methods'.
9669 If N is a positive number, respool the N next articles.
9670 If N is a negative number, respool the N previous articles.
9671 If N is nil and any articles have been marked with the process mark,
9672 respool those articles instead.
9673
9674 Respooling can be done both from mail groups and \"real\" newsgroups.
9675 In the former case, the articles in question will be moved from the
9676 current group into whatever groups they are destined to.  In the
9677 latter case, they will be copied into the relevant groups."
9678   (interactive
9679    (list current-prefix-arg
9680          (let* ((methods (gnus-methods-using 'respool))
9681                 (methname
9682                  (symbol-name (or gnus-summary-respool-default-method
9683                                   (car (gnus-find-method-for-group
9684                                         gnus-newsgroup-name)))))
9685                 (method
9686                  (gnus-completing-read-with-default
9687                   methname "Backend to use when respooling"
9688                   methods nil t nil 'gnus-mail-method-history))
9689                 ms)
9690            (cond
9691             ((zerop (length (setq ms (gnus-servers-using-backend
9692                                       (intern method)))))
9693              (list (intern method) ""))
9694             ((= 1 (length ms))
9695              (car ms))
9696             (t
9697              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9698                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9699                            ms-alist))))))))
9700   (unless method
9701     (error "No method given for respooling"))
9702   (if (assoc (symbol-name
9703               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9704              (gnus-methods-using 'respool))
9705       (gnus-summary-move-article n nil method)
9706     (gnus-summary-copy-article n nil method)))
9707
9708 (defun gnus-summary-import-article (file &optional edit)
9709   "Import an arbitrary file into a mail newsgroup."
9710   (interactive "fImport file: \nP")
9711   (let ((group gnus-newsgroup-name)
9712         (now (current-time))
9713         atts lines group-art)
9714     (unless (gnus-check-backend-function 'request-accept-article group)
9715       (error "%s does not support article importing" group))
9716     (or (file-readable-p file)
9717         (not (file-regular-p file))
9718         (error "Can't read %s" file))
9719     (save-excursion
9720       (set-buffer (gnus-get-buffer-create " *import file*"))
9721       (erase-buffer)
9722       (nnheader-insert-file-contents file)
9723       (goto-char (point-min))
9724       (if (nnheader-article-p)
9725           (save-restriction
9726             (goto-char (point-min))
9727             (search-forward "\n\n" nil t)
9728             (narrow-to-region (point-min) (1- (point)))
9729             (goto-char (point-min))
9730             (unless (re-search-forward "^date:" nil t)
9731               (goto-char (point-max))
9732               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9733        ;; This doesn't look like an article, so we fudge some headers.
9734         (setq atts (file-attributes file)
9735               lines (count-lines (point-min) (point-max)))
9736         (insert "From: " (read-string "From: ") "\n"
9737                 "Subject: " (read-string "Subject: ") "\n"
9738                 "Date: " (message-make-date (nth 5 atts)) "\n"
9739                 "Message-ID: " (message-make-message-id) "\n"
9740                 "Lines: " (int-to-string lines) "\n"
9741                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9742       (setq group-art (gnus-request-accept-article group nil t))
9743       (kill-buffer (current-buffer)))
9744     (setq gnus-newsgroup-active (gnus-activate-group group))
9745     (forward-line 1)
9746     (gnus-summary-goto-article (cdr group-art) nil t)
9747     (when edit
9748       (gnus-summary-edit-article))))
9749
9750 (defun gnus-summary-create-article ()
9751   "Create an article in a mail newsgroup."
9752   (interactive)
9753   (let ((group gnus-newsgroup-name)
9754         (now (current-time))
9755         group-art)
9756     (unless (gnus-check-backend-function 'request-accept-article group)
9757       (error "%s does not support article importing" group))
9758     (save-excursion
9759       (set-buffer (gnus-get-buffer-create " *import file*"))
9760       (erase-buffer)
9761       (goto-char (point-min))
9762       ;; This doesn't look like an article, so we fudge some headers.
9763       (insert "From: " (read-string "From: ") "\n"
9764               "Subject: " (read-string "Subject: ") "\n"
9765               "Date: " (message-make-date now) "\n"
9766               "Message-ID: " (message-make-message-id) "\n")
9767       (setq group-art (gnus-request-accept-article group nil t))
9768       (kill-buffer (current-buffer)))
9769     (setq gnus-newsgroup-active (gnus-activate-group group))
9770     (forward-line 1)
9771     (gnus-summary-goto-article (cdr group-art) nil t)
9772     (gnus-summary-edit-article)))
9773
9774 (defun gnus-summary-article-posted-p ()
9775   "Say whether the current (mail) article is available from news as well.
9776 This will be the case if the article has both been mailed and posted."
9777   (interactive)
9778   (let ((id (mail-header-references (gnus-summary-article-header)))
9779         (gnus-override-method (car (gnus-refer-article-methods))))
9780     (if (gnus-request-head id "")
9781         (gnus-message 2 "The current message was found on %s"
9782                       gnus-override-method)
9783       (gnus-message 2 "The current message couldn't be found on %s"
9784                     gnus-override-method)
9785       nil)))
9786
9787 (defun gnus-summary-expire-articles (&optional now)
9788   "Expire all articles that are marked as expirable in the current group."
9789   (interactive)
9790   (when (and (not gnus-group-is-exiting-without-update-p)
9791              (gnus-check-backend-function
9792               'request-expire-articles gnus-newsgroup-name))
9793     ;; This backend supports expiry.
9794     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9795            (expirable (if total
9796                           (progn
9797                             ;; We need to update the info for
9798                             ;; this group for `gnus-list-of-read-articles'
9799                             ;; to give us the right answer.
9800                             (gnus-run-hooks 'gnus-exit-group-hook)
9801                             (gnus-summary-update-info)
9802                             (gnus-list-of-read-articles gnus-newsgroup-name))
9803                         (setq gnus-newsgroup-expirable
9804                               (sort gnus-newsgroup-expirable '<))))
9805            (expiry-wait (if now 'immediate
9806                           (gnus-group-find-parameter
9807                            gnus-newsgroup-name 'expiry-wait)))
9808            (nnmail-expiry-target
9809             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9810                 nnmail-expiry-target))
9811            es)
9812       (when expirable
9813         ;; There are expirable articles in this group, so we run them
9814         ;; through the expiry process.
9815         (gnus-message 6 "Expiring articles...")
9816         (unless (gnus-check-group gnus-newsgroup-name)
9817           (error "Can't open server for %s" gnus-newsgroup-name))
9818         ;; The list of articles that weren't expired is returned.
9819         (save-excursion
9820           (if expiry-wait
9821               (let ((nnmail-expiry-wait-function nil)
9822                     (nnmail-expiry-wait expiry-wait))
9823                 (setq es (gnus-request-expire-articles
9824                           expirable gnus-newsgroup-name)))
9825             (setq es (gnus-request-expire-articles
9826                       expirable gnus-newsgroup-name)))
9827           (unless total
9828             (setq gnus-newsgroup-expirable es))
9829           ;; We go through the old list of expirable, and mark all
9830           ;; really expired articles as nonexistent.
9831           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9832             (let ((gnus-use-cache nil))
9833               (dolist (article expirable)
9834                 (when (and (not (memq article es))
9835                            (gnus-data-find article))
9836                   (gnus-summary-mark-article article gnus-canceled-mark)
9837                   (run-hook-with-args 'gnus-summary-article-expire-hook
9838                                       'delete
9839                                       (gnus-data-header
9840                                        (assoc article (gnus-data-list nil)))
9841                                       gnus-newsgroup-name
9842                                       nil
9843                                       nil))))))
9844         (gnus-message 6 "Expiring articles...done")))))
9845
9846 (defun gnus-summary-expire-articles-now ()
9847   "Expunge all expirable articles in the current group.
9848 This means that *all* articles that are marked as expirable will be
9849 deleted forever, right now."
9850   (interactive)
9851   (or gnus-expert-user
9852       (gnus-yes-or-no-p
9853        "Are you really, really sure you want to delete all expirable messages? ")
9854       (error "Phew!"))
9855   (gnus-summary-expire-articles t))
9856
9857 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9858 (defun gnus-summary-delete-article (&optional n)
9859   "Delete the N next (mail) articles.
9860 This command actually deletes articles.  This is not a marking
9861 command.  The article will disappear forever from your life, never to
9862 return.
9863
9864 If N is negative, delete backwards.
9865 If N is nil and articles have been marked with the process mark,
9866 delete these instead.
9867
9868 If `gnus-novice-user' is non-nil you will be asked for
9869 confirmation before the articles are deleted."
9870   (interactive "P")
9871   (unless (gnus-check-backend-function 'request-expire-articles
9872                                        gnus-newsgroup-name)
9873     (error "The current newsgroup does not support article deletion"))
9874   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9875     (error "Couldn't open server"))
9876   ;; Compute the list of articles to delete.
9877   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9878         (nnmail-expiry-target 'delete)
9879         not-deleted)
9880     (if (and gnus-novice-user
9881              (not (gnus-yes-or-no-p
9882                    (format "Do you really want to delete %s forever? "
9883                            (if (> (length articles) 1)
9884                                (format "these %s articles" (length articles))
9885                              "this article")))))
9886         ()
9887       ;; Delete the articles.
9888       (setq not-deleted (gnus-request-expire-articles
9889                          articles gnus-newsgroup-name 'force))
9890       (while articles
9891         (gnus-summary-remove-process-mark (car articles))
9892         ;; The backend might not have been able to delete the article
9893         ;; after all.
9894         (unless (memq (car articles) not-deleted)
9895           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9896         (let* ((article (car articles))
9897                (ghead  (gnus-data-header
9898                                     (assoc article (gnus-data-list nil)))))
9899           (run-hook-with-args 'gnus-summary-article-delete-hook
9900                               'delete ghead gnus-newsgroup-name nil
9901                               nil))
9902         (setq articles (cdr articles)))
9903       (when not-deleted
9904         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9905     (gnus-summary-position-point)
9906     (gnus-set-mode-line 'summary)
9907     not-deleted))
9908
9909 (defun gnus-summary-edit-article (&optional arg)
9910   "Edit the current article.
9911 This will have permanent effect only in mail groups.
9912 If ARG is nil, edit the decoded articles.
9913 If ARG is 1, edit the raw articles.
9914 If ARG is 2, edit the raw articles even in read-only groups.
9915 If ARG is 3, edit the articles with the current handles.
9916 Otherwise, allow editing of articles even in read-only
9917 groups."
9918   (interactive "P")
9919   (let (force raw current-handles)
9920     (cond
9921      ((null arg))
9922      ((eq arg 1)
9923       (setq raw t))
9924      ((eq arg 2)
9925       (setq raw t
9926             force t))
9927      ((eq arg 3)
9928       (setq current-handles
9929             (and (gnus-buffer-live-p gnus-article-buffer)
9930                  (with-current-buffer gnus-article-buffer
9931                    (prog1
9932                        gnus-article-mime-handles
9933                      (setq gnus-article-mime-handles nil))))))
9934      (t
9935       (setq force t)))
9936     (when (and raw (not force)
9937                (member gnus-newsgroup-name '("nndraft:delayed"
9938                                              "nndraft:drafts"
9939                                              "nndraft:queue")))
9940       (error "Can't edit the raw article in group %s"
9941              gnus-newsgroup-name))
9942     (save-excursion
9943       (set-buffer gnus-summary-buffer)
9944       (let ((mail-parse-charset gnus-newsgroup-charset)
9945             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9946         (gnus-set-global-variables)
9947         (when (and (not force)
9948                    (gnus-group-read-only-p))
9949           (error "The current newsgroup does not support article editing"))
9950         (gnus-summary-show-article t)
9951         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9952           (with-current-buffer gnus-article-buffer
9953             (mm-enable-multibyte)))
9954         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9955             (setq raw t))
9956         (gnus-article-edit-article
9957          (if raw 'ignore
9958            `(lambda ()
9959               (let ((mbl mml-buffer-list))
9960                 (setq mml-buffer-list nil)
9961                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9962                   (mime-to-mml ,'current-handles))
9963                 (let ((mbl1 mml-buffer-list))
9964                   (setq mml-buffer-list mbl)
9965                   (set (make-local-variable 'mml-buffer-list) mbl1))
9966                 (gnus-make-local-hook 'kill-buffer-hook)
9967                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9968          `(lambda (no-highlight)
9969             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9970                   (message-options message-options)
9971                   (message-options-set-recipient)
9972                   (mail-parse-ignored-charsets
9973                    ',gnus-newsgroup-ignored-charsets))
9974               ,(if (not raw) '(progn
9975                                 (mml-to-mime)
9976                                 (mml-destroy-buffers)
9977                                 (remove-hook 'kill-buffer-hook
9978                                              'mml-destroy-buffers t)
9979                                 (kill-local-variable 'mml-buffer-list)))
9980               (gnus-summary-edit-article-done
9981                ,(or (mail-header-references gnus-current-headers) "")
9982                ,(gnus-group-read-only-p)
9983                ,gnus-summary-buffer no-highlight))))))))
9984
9985 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9986
9987 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9988                                                  no-highlight)
9989   "Make edits to the current article permanent."
9990   (interactive)
9991   (save-excursion
9992    ;; The buffer restriction contains the entire article if it exists.
9993     (when (article-goto-body)
9994       (let ((lines (count-lines (point) (point-max)))
9995             (length (- (point-max) (point)))
9996             (case-fold-search t)
9997             (body (copy-marker (point))))
9998         (goto-char (point-min))
9999         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10000           (delete-region (match-beginning 1) (match-end 1))
10001           (insert (number-to-string length)))
10002         (goto-char (point-min))
10003         (when (re-search-forward
10004                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10005           (delete-region (match-beginning 1) (match-end 1))
10006           (insert (number-to-string length)))
10007         (goto-char (point-min))
10008         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10009           (delete-region (match-beginning 1) (match-end 1))
10010           (insert (number-to-string lines))))))
10011   ;; Replace the article.
10012   (let ((buf (current-buffer)))
10013     (with-temp-buffer
10014       (insert-buffer-substring buf)
10015
10016       (if (and (not read-only)
10017                (not (gnus-request-replace-article
10018                      (cdr gnus-article-current) (car gnus-article-current)
10019                      (current-buffer) t)))
10020           (error "Couldn't replace article")
10021         ;; Update the summary buffer.
10022         (if (and references
10023                  (equal (message-tokenize-header references " ")
10024                         (message-tokenize-header
10025                          (or (message-fetch-field "references") "") " ")))
10026             ;; We only have to update this line.
10027             (save-excursion
10028               (save-restriction
10029                 (message-narrow-to-head)
10030                 (let ((head (buffer-substring-no-properties
10031                              (point-min) (point-max)))
10032                       header)
10033                   (with-temp-buffer
10034                     (insert (format "211 %d Article retrieved.\n"
10035                                     (cdr gnus-article-current)))
10036                     (insert head)
10037                     (insert ".\n")
10038                     (let ((nntp-server-buffer (current-buffer)))
10039                       (setq header (car (gnus-get-newsgroup-headers
10040                                          nil t))))
10041                     (save-excursion
10042                       (set-buffer gnus-summary-buffer)
10043                       (gnus-data-set-header
10044                        (gnus-data-find (cdr gnus-article-current))
10045                        header)
10046                       (gnus-summary-update-article-line
10047                        (cdr gnus-article-current) header)
10048                       (if (gnus-summary-goto-subject
10049                            (cdr gnus-article-current) nil t)
10050                           (gnus-summary-update-secondary-mark
10051                            (cdr gnus-article-current))))))))
10052           ;; Update threads.
10053           (set-buffer (or buffer gnus-summary-buffer))
10054           (gnus-summary-update-article (cdr gnus-article-current))
10055           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10056               (gnus-summary-update-secondary-mark
10057                (cdr gnus-article-current))))
10058         ;; Prettify the article buffer again.
10059         (unless no-highlight
10060           (save-excursion
10061             (set-buffer gnus-article-buffer)
10062             ;;;!!! Fix this -- article should be rehighlighted.
10063             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10064             (set-buffer gnus-original-article-buffer)
10065             (gnus-request-article
10066              (cdr gnus-article-current)
10067              (car gnus-article-current) (current-buffer))))
10068         ;; Prettify the summary buffer line.
10069         (when (gnus-visual-p 'summary-highlight 'highlight)
10070           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10071
10072 (defun gnus-summary-edit-wash (key)
10073   "Perform editing command KEY in the article buffer."
10074   (interactive
10075    (list
10076     (progn
10077       (message "%s" (concat (this-command-keys) "- "))
10078       (read-char))))
10079   (message "")
10080   (gnus-summary-edit-article)
10081   (execute-kbd-macro (concat (this-command-keys) key))
10082   (gnus-article-edit-done))
10083
10084 ;;; Respooling
10085
10086 (defun gnus-summary-respool-query (&optional silent trace)
10087   "Query where the respool algorithm would put this article."
10088   (interactive)
10089   (let (gnus-mark-article-hook)
10090     (gnus-summary-select-article)
10091     (save-excursion
10092       (set-buffer gnus-original-article-buffer)
10093       (let ((groups (nnmail-article-group 'identity trace)))
10094         (unless silent
10095           (if groups
10096               (message "This message would go to %s"
10097                        (mapconcat 'car groups ", "))
10098             (message "This message would go to no groups"))
10099           groups)))))
10100
10101 (defun gnus-summary-respool-trace ()
10102   "Trace where the respool algorithm would put this article.
10103 Display a buffer showing all fancy splitting patterns which matched."
10104   (interactive)
10105   (gnus-summary-respool-query nil t))
10106
10107 ;; Summary marking commands.
10108
10109 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10110   "Mark articles which has the same subject as read, and then select the next.
10111 If UNMARK is positive, remove any kind of mark.
10112 If UNMARK is negative, tick articles."
10113   (interactive "P")
10114   (when unmark
10115     (setq unmark (prefix-numeric-value unmark)))
10116   (let ((count
10117          (gnus-summary-mark-same-subject
10118           (gnus-summary-article-subject) unmark)))
10119     ;; Select next unread article.  If auto-select-same mode, should
10120     ;; select the first unread article.
10121     (gnus-summary-next-article t (and gnus-auto-select-same
10122                                       (gnus-summary-article-subject)))
10123     (gnus-message 7 "%d article%s marked as %s"
10124                   count (if (= count 1) " is" "s are")
10125                   (if unmark "unread" "read"))))
10126
10127 (defun gnus-summary-kill-same-subject (&optional unmark)
10128   "Mark articles which has the same subject as read.
10129 If UNMARK is positive, remove any kind of mark.
10130 If UNMARK is negative, tick articles."
10131   (interactive "P")
10132   (when unmark
10133     (setq unmark (prefix-numeric-value unmark)))
10134   (let ((count
10135          (gnus-summary-mark-same-subject
10136           (gnus-summary-article-subject) unmark)))
10137     ;; If marked as read, go to next unread subject.
10138     (when (null unmark)
10139       ;; Go to next unread subject.
10140       (gnus-summary-next-subject 1 t))
10141     (gnus-message 7 "%d articles are marked as %s"
10142                   count (if unmark "unread" "read"))))
10143
10144 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10145   "Mark articles with same SUBJECT as read, and return marked number.
10146 If optional argument UNMARK is positive, remove any kinds of marks.
10147 If optional argument UNMARK is negative, mark articles as unread instead."
10148   (let ((count 1))
10149     (save-excursion
10150       (cond
10151        ((null unmark)                   ; Mark as read.
10152         (while (and
10153                 (progn
10154                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10155                   (gnus-summary-show-thread) t)
10156                 (gnus-summary-find-subject subject))
10157           (setq count (1+ count))))
10158        ((> unmark 0)                    ; Tick.
10159         (while (and
10160                 (progn
10161                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10162                   (gnus-summary-show-thread) t)
10163                 (gnus-summary-find-subject subject))
10164           (setq count (1+ count))))
10165        (t                               ; Mark as unread.
10166         (while (and
10167                 (progn
10168                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10169                   (gnus-summary-show-thread) t)
10170                 (gnus-summary-find-subject subject))
10171           (setq count (1+ count)))))
10172       (gnus-set-mode-line 'summary)
10173       ;; Return the number of marked articles.
10174       count)))
10175
10176 (defun gnus-summary-mark-as-processable (n &optional unmark)
10177   "Set the process mark on the next N articles.
10178 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10179 the process mark instead.  The difference between N and the actual
10180 number of articles marked is returned."
10181   (interactive "P")
10182   (if (and (null n) (gnus-region-active-p))
10183       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10184     (setq n (prefix-numeric-value n))
10185     (let ((backward (< n 0))
10186           (n (abs n)))
10187       (while (and
10188               (> n 0)
10189               (if unmark
10190                   (gnus-summary-remove-process-mark
10191                    (gnus-summary-article-number))
10192                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10193               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10194         (setq n (1- n)))
10195       (when (/= 0 n)
10196         (gnus-message 7 "No more articles"))
10197       (gnus-summary-recenter)
10198       (gnus-summary-position-point)
10199       n)))
10200
10201 (defun gnus-summary-unmark-as-processable (n)
10202   "Remove the process mark from the next N articles.
10203 If N is negative, unmark backward instead.  The difference between N and
10204 the actual number of articles unmarked is returned."
10205   (interactive "P")
10206   (gnus-summary-mark-as-processable n t))
10207
10208 (defun gnus-summary-unmark-all-processable ()
10209   "Remove the process mark from all articles."
10210   (interactive)
10211   (save-excursion
10212     (while gnus-newsgroup-processable
10213       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10214   (gnus-summary-position-point))
10215
10216 (defun gnus-summary-add-mark (article type)
10217   "Mark ARTICLE with a mark of TYPE."
10218   (let ((vtype (car (assq type gnus-article-mark-lists)))
10219         var)
10220     (if (not vtype)
10221         (error "No such mark type: %s" type)
10222       (setq var (intern (format "gnus-newsgroup-%s" type)))
10223       (set var (cons article (symbol-value var)))
10224       (if (memq type '(processable cached replied forwarded recent saved))
10225           (gnus-summary-update-secondary-mark article)
10226         ;;; !!! This is bogus.  We should find out what primary
10227         ;;; !!! mark we want to set.
10228         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10229
10230 (defun gnus-summary-mark-as-expirable (n)
10231   "Mark N articles forward as expirable.
10232 If N is negative, mark backward instead.  The difference between N and
10233 the actual number of articles marked is returned."
10234   (interactive "p")
10235   (gnus-summary-mark-forward n gnus-expirable-mark))
10236
10237 (defun gnus-summary-mark-as-spam (n)
10238   "Mark N articles forward as spam.
10239 If N is negative, mark backward instead.  The difference between N and
10240 the actual number of articles marked is returned."
10241   (interactive "p")
10242   (gnus-summary-mark-forward n gnus-spam-mark))
10243
10244 (defun gnus-summary-mark-article-as-replied (article)
10245   "Mark ARTICLE as replied to and update the summary line.
10246 ARTICLE can also be a list of articles."
10247   (interactive (list (gnus-summary-article-number)))
10248   (let ((articles (if (listp article) article (list article))))
10249     (dolist (article articles)
10250       (unless (numberp article)
10251         (error "%s is not a number" article))
10252       (push article gnus-newsgroup-replied)
10253       (let ((buffer-read-only nil))
10254         (when (gnus-summary-goto-subject article nil t)
10255           (gnus-summary-update-secondary-mark article))))))
10256
10257 (defun gnus-summary-mark-article-as-forwarded (article)
10258   "Mark ARTICLE as forwarded and update the summary line.
10259 ARTICLE can also be a list of articles."
10260   (let ((articles (if (listp article) article (list article))))
10261     (dolist (article articles)
10262       (push article gnus-newsgroup-forwarded)
10263       (let ((buffer-read-only nil))
10264         (when (gnus-summary-goto-subject article nil t)
10265           (gnus-summary-update-secondary-mark article))))))
10266
10267 (defun gnus-summary-set-bookmark (article)
10268   "Set a bookmark in current article."
10269   (interactive (list (gnus-summary-article-number)))
10270   (when (or (not (get-buffer gnus-article-buffer))
10271             (not gnus-current-article)
10272             (not gnus-article-current)
10273             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10274     (error "No current article selected"))
10275   ;; Remove old bookmark, if one exists.
10276   (gnus-pull article gnus-newsgroup-bookmarks)
10277   ;; Set the new bookmark, which is on the form
10278   ;; (article-number . line-number-in-body).
10279   (push
10280    (cons article
10281          (with-current-buffer gnus-article-buffer
10282            (count-lines
10283             (min (point)
10284                  (save-excursion
10285                    (article-goto-body)
10286                    (point)))
10287             (point))))
10288    gnus-newsgroup-bookmarks)
10289   (gnus-message 6 "A bookmark has been added to the current article."))
10290
10291 (defun gnus-summary-remove-bookmark (article)
10292   "Remove the bookmark from the current article."
10293   (interactive (list (gnus-summary-article-number)))
10294   ;; Remove old bookmark, if one exists.
10295   (if (not (assq article gnus-newsgroup-bookmarks))
10296       (gnus-message 6 "No bookmark in current article.")
10297     (gnus-pull article gnus-newsgroup-bookmarks)
10298     (gnus-message 6 "Removed bookmark.")))
10299
10300 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10301 (defun gnus-summary-mark-as-dormant (n)
10302   "Mark N articles forward as dormant.
10303 If N is negative, mark backward instead.  The difference between N and
10304 the actual number of articles marked is returned."
10305   (interactive "p")
10306   (gnus-summary-mark-forward n gnus-dormant-mark))
10307
10308 (defun gnus-summary-set-process-mark (article)
10309   "Set the process mark on ARTICLE and update the summary line."
10310   (setq gnus-newsgroup-processable
10311         (cons article
10312               (delq article gnus-newsgroup-processable)))
10313   (when (gnus-summary-goto-subject article)
10314     (gnus-summary-show-thread)
10315     (gnus-summary-goto-subject article)
10316     (gnus-summary-update-secondary-mark article)))
10317
10318 (defun gnus-summary-remove-process-mark (&rest articles)
10319   "Remove the process mark from ARTICLES and update the summary line."
10320   (dolist (article articles)
10321     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10322     (when (gnus-summary-goto-subject article)
10323       (gnus-summary-show-thread)
10324       (gnus-summary-goto-subject article)
10325       (gnus-summary-update-secondary-mark article)))
10326   t)
10327
10328 (defun gnus-summary-set-saved-mark (article)
10329   "Set the process mark on ARTICLE and update the summary line."
10330   (push article gnus-newsgroup-saved)
10331   (when (gnus-summary-goto-subject article)
10332     (gnus-summary-update-secondary-mark article)))
10333
10334 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10335   "Mark N articles as read forwards.
10336 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10337 The difference between N and the actual number of articles marked is
10338 returned.
10339 If NO-EXPIRE, auto-expiry will be inhibited."
10340   (interactive "p")
10341   (gnus-summary-show-thread)
10342   (let ((backward (< n 0))
10343         (gnus-summary-goto-unread
10344          (and gnus-summary-goto-unread
10345               (not (eq gnus-summary-goto-unread 'never))
10346               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10347                                     gnus-ticked-mark gnus-dormant-mark)))))
10348         (n (abs n))
10349         (mark (or mark gnus-del-mark)))
10350     (while (and (> n 0)
10351                 (gnus-summary-mark-article nil mark no-expire)
10352                 (zerop (gnus-summary-next-subject
10353                         (if backward -1 1)
10354                         (and gnus-summary-goto-unread
10355                              (not (eq gnus-summary-goto-unread 'never)))
10356                         t)))
10357       (setq n (1- n)))
10358     (when (/= 0 n)
10359       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10360     (gnus-summary-recenter)
10361     (gnus-summary-position-point)
10362     (gnus-set-mode-line 'summary)
10363     n))
10364
10365 (defun gnus-summary-mark-article-as-read (mark)
10366   "Mark the current article quickly as read with MARK."
10367   (let ((article (gnus-summary-article-number)))
10368     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10369     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10370     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10371     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10372     (push (cons article mark) gnus-newsgroup-reads)
10373     ;; Possibly remove from cache, if that is used.
10374     (when gnus-use-cache
10375       (gnus-cache-enter-remove-article article))
10376     ;; Allow the backend to change the mark.
10377     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10378     ;; Check for auto-expiry.
10379     (when (and gnus-newsgroup-auto-expire
10380                (memq mark gnus-auto-expirable-marks))
10381       (setq mark gnus-expirable-mark)
10382       ;; Let the backend know about the mark change.
10383       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10384       (push article gnus-newsgroup-expirable))
10385     ;; Set the mark in the buffer.
10386     (gnus-summary-update-mark mark 'unread)
10387     t))
10388
10389 (defun gnus-summary-mark-article-as-unread (mark)
10390   "Mark the current article quickly as unread with MARK."
10391   (let* ((article (gnus-summary-article-number))
10392          (old-mark (gnus-summary-article-mark article)))
10393     ;; Allow the backend to change the mark.
10394     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10395     (if (eq mark old-mark)
10396         t
10397       (if (<= article 0)
10398           (progn
10399             (gnus-error 1 "Can't mark negative article numbers")
10400             nil)
10401         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10402         (setq gnus-newsgroup-spam-marked
10403               (delq article gnus-newsgroup-spam-marked))
10404         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10405         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10406         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10407         (cond ((= mark gnus-ticked-mark)
10408                (setq gnus-newsgroup-marked
10409                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10410                                               article)))
10411               ((= mark gnus-spam-mark)
10412                (setq gnus-newsgroup-spam-marked
10413                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10414                                               article)))
10415               ((= mark gnus-dormant-mark)
10416                (setq gnus-newsgroup-dormant
10417                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10418                                               article)))
10419               (t
10420                (setq gnus-newsgroup-unreads
10421                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10422                                               article))))
10423         (gnus-pull article gnus-newsgroup-reads)
10424
10425         ;; See whether the article is to be put in the cache.
10426         (and gnus-use-cache
10427              (vectorp (gnus-summary-article-header article))
10428              (save-excursion
10429                (gnus-cache-possibly-enter-article
10430                 gnus-newsgroup-name article
10431                 (= mark gnus-ticked-mark)
10432                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10433
10434         ;; Fix the mark.
10435         (gnus-summary-update-mark mark 'unread)
10436         t))))
10437
10438 (defun gnus-summary-mark-article (&optional article mark no-expire)
10439   "Mark ARTICLE with MARK.  MARK can be any character.
10440 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10441 `??' (dormant) and `?E' (expirable).
10442 If MARK is nil, then the default character `?r' is used.
10443 If ARTICLE is nil, then the article on the current line will be
10444 marked.
10445 If NO-EXPIRE, auto-expiry will be inhibited."
10446   ;; The mark might be a string.
10447   (when (stringp mark)
10448     (setq mark (aref mark 0)))
10449   ;; If no mark is given, then we check auto-expiring.
10450   (when (null mark)
10451     (setq mark gnus-del-mark))
10452   (when (and (not no-expire)
10453              gnus-newsgroup-auto-expire
10454              (memq mark gnus-auto-expirable-marks))
10455     (setq mark gnus-expirable-mark))
10456   (let ((article (or article (gnus-summary-article-number)))
10457         (old-mark (gnus-summary-article-mark article)))
10458     ;; Allow the backend to change the mark.
10459     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10460     (if (eq mark old-mark)
10461         t
10462       (unless article
10463         (error "No article on current line"))
10464       (if (not (if (or (= mark gnus-unread-mark)
10465                        (= mark gnus-ticked-mark)
10466                        (= mark gnus-spam-mark)
10467                        (= mark gnus-dormant-mark))
10468                    (gnus-mark-article-as-unread article mark)
10469                  (gnus-mark-article-as-read article mark)))
10470           t
10471         ;; See whether the article is to be put in the cache.
10472         (and gnus-use-cache
10473              (not (= mark gnus-canceled-mark))
10474              (vectorp (gnus-summary-article-header article))
10475              (save-excursion
10476                (gnus-cache-possibly-enter-article
10477                 gnus-newsgroup-name article
10478                 (= mark gnus-ticked-mark)
10479                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10480
10481         (when (gnus-summary-goto-subject article nil t)
10482           (let ((buffer-read-only nil))
10483             (gnus-summary-show-thread)
10484             ;; Fix the mark.
10485             (gnus-summary-update-mark mark 'unread)
10486             t))))))
10487
10488 (defun gnus-summary-update-secondary-mark (article)
10489   "Update the secondary (read, process, cache) mark."
10490   (gnus-summary-update-mark
10491    (cond ((memq article gnus-newsgroup-processable)
10492           gnus-process-mark)
10493          ((memq article gnus-newsgroup-cached)
10494           gnus-cached-mark)
10495          ((memq article gnus-newsgroup-replied)
10496           gnus-replied-mark)
10497          ((memq article gnus-newsgroup-forwarded)
10498           gnus-forwarded-mark)
10499          ((memq article gnus-newsgroup-saved)
10500           gnus-saved-mark)
10501          ((memq article gnus-newsgroup-recent)
10502           gnus-recent-mark)
10503          ((memq article gnus-newsgroup-unseen)
10504           gnus-unseen-mark)
10505          (t gnus-no-mark))
10506    'replied)
10507   (when (gnus-visual-p 'summary-highlight 'highlight)
10508     (gnus-run-hooks 'gnus-summary-update-hook))
10509   t)
10510
10511 (defun gnus-summary-update-download-mark (article)
10512   "Update the download mark."
10513   (gnus-summary-update-mark
10514    (cond ((memq article gnus-newsgroup-undownloaded)
10515           gnus-undownloaded-mark)
10516          (gnus-newsgroup-agentized
10517           gnus-downloaded-mark)
10518          (t
10519           gnus-no-mark))
10520    'download)
10521   (gnus-summary-update-line t)
10522   t)
10523
10524 (defun gnus-summary-update-mark (mark type)
10525   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10526         (buffer-read-only nil))
10527     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10528     (when forward
10529       (when (looking-at "\r")
10530         (incf forward))
10531       (when (<= (+ forward (point)) (point-max))
10532         ;; Go to the right position on the line.
10533         (goto-char (+ forward (point)))
10534         ;; Replace the old mark with the new mark.
10535         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10536         ;; Optionally update the marks by some user rule.
10537         (when (eq type 'unread)
10538           (gnus-data-set-mark
10539            (gnus-data-find (gnus-summary-article-number)) mark)
10540           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10541
10542 (defun gnus-mark-article-as-read (article &optional mark)
10543   "Enter ARTICLE in the pertinent lists and remove it from others."
10544   ;; Make the article expirable.
10545   (let ((mark (or mark gnus-del-mark)))
10546     (setq gnus-newsgroup-expirable
10547           (if (= mark gnus-expirable-mark)
10548               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10549             (delq article gnus-newsgroup-expirable)))
10550     ;; Remove from unread and marked lists.
10551     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10552     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10553     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10554     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10555     (push (cons article mark) gnus-newsgroup-reads)
10556     ;; Possibly remove from cache, if that is used.
10557     (when gnus-use-cache
10558       (gnus-cache-enter-remove-article article))
10559     t))
10560
10561 (defun gnus-mark-article-as-unread (article &optional mark)
10562   "Enter ARTICLE in the pertinent lists and remove it from others."
10563   (let ((mark (or mark gnus-ticked-mark)))
10564     (if (<= article 0)
10565         (progn
10566           (gnus-error 1 "Can't mark negative article numbers")
10567           nil)
10568       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10569             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10570             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10571             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10572             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10573
10574       ;; Unsuppress duplicates?
10575       (when gnus-suppress-duplicates
10576         (gnus-dup-unsuppress-article article))
10577
10578       (cond ((= mark gnus-ticked-mark)
10579              (setq gnus-newsgroup-marked
10580                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10581             ((= mark gnus-spam-mark)
10582              (setq gnus-newsgroup-spam-marked
10583                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10584                                             article)))
10585             ((= mark gnus-dormant-mark)
10586              (setq gnus-newsgroup-dormant
10587                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10588             (t
10589              (setq gnus-newsgroup-unreads
10590                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10591       (gnus-pull article gnus-newsgroup-reads)
10592       t)))
10593
10594 (defalias 'gnus-summary-mark-as-unread-forward
10595   'gnus-summary-tick-article-forward)
10596 (make-obsolete 'gnus-summary-mark-as-unread-forward
10597                'gnus-summary-tick-article-forward)
10598 (defun gnus-summary-tick-article-forward (n)
10599   "Tick N articles forwards.
10600 If N is negative, tick backwards instead.
10601 The difference between N and the number of articles ticked is returned."
10602   (interactive "p")
10603   (gnus-summary-mark-forward n gnus-ticked-mark))
10604
10605 (defalias 'gnus-summary-mark-as-unread-backward
10606   'gnus-summary-tick-article-backward)
10607 (make-obsolete 'gnus-summary-mark-as-unread-backward
10608                'gnus-summary-tick-article-backward)
10609 (defun gnus-summary-tick-article-backward (n)
10610   "Tick N articles backwards.
10611 The difference between N and the number of articles ticked is returned."
10612   (interactive "p")
10613   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10614
10615 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10616 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10617 (defun gnus-summary-tick-article (&optional article clear-mark)
10618   "Mark current article as unread.
10619 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10620 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10621   (interactive)
10622   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10623                                        gnus-ticked-mark)))
10624
10625 (defun gnus-summary-mark-as-read-forward (n)
10626   "Mark N articles as read forwards.
10627 If N is negative, mark backwards instead.
10628 The difference between N and the actual number of articles marked is
10629 returned."
10630   (interactive "p")
10631   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10632
10633 (defun gnus-summary-mark-as-read-backward (n)
10634   "Mark the N articles as read backwards.
10635 The difference between N and the actual number of articles marked is
10636 returned."
10637   (interactive "p")
10638   (gnus-summary-mark-forward
10639    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10640
10641 (defun gnus-summary-mark-as-read (&optional article mark)
10642   "Mark current article as read.
10643 ARTICLE specifies the article to be marked as read.
10644 MARK specifies a string to be inserted at the beginning of the line."
10645   (gnus-summary-mark-article article mark))
10646
10647 (defun gnus-summary-clear-mark-forward (n)
10648   "Clear marks from N articles forward.
10649 If N is negative, clear backward instead.
10650 The difference between N and the number of marks cleared is returned."
10651   (interactive "p")
10652   (gnus-summary-mark-forward n gnus-unread-mark))
10653
10654 (defun gnus-summary-clear-mark-backward (n)
10655   "Clear marks from N articles backward.
10656 The difference between N and the number of marks cleared is returned."
10657   (interactive "p")
10658   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10659
10660 (defun gnus-summary-mark-unread-as-read ()
10661   "Intended to be used by `gnus-summary-mark-article-hook'."
10662   (when (memq gnus-current-article gnus-newsgroup-unreads)
10663     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10664
10665 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10666   "Intended to be used by `gnus-summary-mark-article-hook'."
10667   (let ((mark (gnus-summary-article-mark)))
10668     (when (or (gnus-unread-mark-p mark)
10669               (gnus-read-mark-p mark))
10670       (gnus-summary-mark-article gnus-current-article
10671                                  (or new-mark gnus-read-mark)))))
10672
10673 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10674   "Intended to be used by `gnus-summary-mark-article-hook'."
10675   (let ((mark (gnus-summary-article-mark)))
10676     (when (or (gnus-unread-mark-p mark)
10677               (gnus-read-mark-p mark))
10678       (gnus-summary-mark-article (gnus-summary-article-number)
10679                                  (or new-mark gnus-read-mark)))))
10680
10681 (defun gnus-summary-mark-unread-as-ticked ()
10682   "Intended to be used by `gnus-summary-mark-article-hook'."
10683   (when (memq gnus-current-article gnus-newsgroup-unreads)
10684     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10685
10686 (defun gnus-summary-mark-region-as-read (point mark all)
10687   "Mark all unread articles between point and mark as read.
10688 If given a prefix, mark all articles between point and mark as read,
10689 even ticked and dormant ones."
10690   (interactive "r\nP")
10691   (save-excursion
10692     (let (article)
10693       (goto-char point)
10694       (beginning-of-line)
10695       (while (and
10696               (< (point) mark)
10697               (progn
10698                 (when (or all
10699                           (memq (setq article (gnus-summary-article-number))
10700                                 gnus-newsgroup-unreads))
10701                   (gnus-summary-mark-article article gnus-del-mark))
10702                 t)
10703               (gnus-summary-find-next))))))
10704
10705 (defun gnus-summary-mark-below (score mark)
10706   "Mark articles with score less than SCORE with MARK."
10707   (interactive "P\ncMark: ")
10708   (setq score (if score
10709                   (prefix-numeric-value score)
10710                 (or gnus-summary-default-score 0)))
10711   (save-excursion
10712     (set-buffer gnus-summary-buffer)
10713     (goto-char (point-min))
10714     (while
10715         (progn
10716           (and (< (gnus-summary-article-score) score)
10717                (gnus-summary-mark-article nil mark))
10718           (gnus-summary-find-next)))))
10719
10720 (defun gnus-summary-kill-below (&optional score)
10721   "Mark articles with score below SCORE as read."
10722   (interactive "P")
10723   (gnus-summary-mark-below score gnus-killed-mark))
10724
10725 (defun gnus-summary-clear-above (&optional score)
10726   "Clear all marks from articles with score above SCORE."
10727   (interactive "P")
10728   (gnus-summary-mark-above score gnus-unread-mark))
10729
10730 (defun gnus-summary-tick-above (&optional score)
10731   "Tick all articles with score above SCORE."
10732   (interactive "P")
10733   (gnus-summary-mark-above score gnus-ticked-mark))
10734
10735 (defun gnus-summary-mark-above (score mark)
10736   "Mark articles with score over SCORE with MARK."
10737   (interactive "P\ncMark: ")
10738   (setq score (if score
10739                   (prefix-numeric-value score)
10740                 (or gnus-summary-default-score 0)))
10741   (save-excursion
10742     (set-buffer gnus-summary-buffer)
10743     (goto-char (point-min))
10744     (while (and (progn
10745                   (when (> (gnus-summary-article-score) score)
10746                     (gnus-summary-mark-article nil mark))
10747                   t)
10748                 (gnus-summary-find-next)))))
10749
10750 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10751 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10752 (defun gnus-summary-limit-include-expunged (&optional no-error)
10753   "Display all the hidden articles that were expunged for low scores."
10754   (interactive)
10755   (let ((buffer-read-only nil))
10756     (let ((scored gnus-newsgroup-scored)
10757           headers h)
10758       (while scored
10759         (unless (gnus-summary-article-header (caar scored))
10760           (and (setq h (gnus-number-to-header (caar scored)))
10761                (< (cdar scored) gnus-summary-expunge-below)
10762                (push h headers)))
10763         (setq scored (cdr scored)))
10764       (if (not headers)
10765           (when (not no-error)
10766             (error "No expunged articles hidden"))
10767         (goto-char (point-min))
10768         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10769         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10770         (mapcar (lambda (x) (push (mail-header-number x)
10771                                   gnus-newsgroup-limit))
10772                 headers)
10773         (gnus-summary-prepare-unthreaded (nreverse headers))
10774         (goto-char (point-min))
10775         (gnus-summary-position-point)
10776         t))))
10777
10778 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10779   "Mark all unread articles in this newsgroup as read.
10780 If prefix argument ALL is non-nil, ticked and dormant articles will
10781 also be marked as read.
10782 If QUIETLY is non-nil, no questions will be asked.
10783
10784 If TO-HERE is non-nil, it should be a point in the buffer.  All
10785 articles before (after, if REVERSE is set) this point will be marked
10786 as read.
10787
10788 Note that this function will only catch up the unread article
10789 in the current summary buffer limitation.
10790
10791 The number of articles marked as read is returned."
10792   (interactive "P")
10793   (prog1
10794       (save-excursion
10795         (when (or quietly
10796                   (not gnus-interactive-catchup) ;Without confirmation?
10797                   gnus-expert-user
10798                   (gnus-y-or-n-p
10799                    (if all
10800                        "Mark absolutely all articles as read? "
10801                      "Mark all unread articles as read? ")))
10802           (if (and not-mark
10803                    (not gnus-newsgroup-adaptive)
10804                    (not gnus-newsgroup-auto-expire)
10805                    (not gnus-suppress-duplicates)
10806                    (or (not gnus-use-cache)
10807                        (eq gnus-use-cache 'passive)))
10808               (progn
10809                 (when all
10810                   (setq gnus-newsgroup-marked nil
10811                         gnus-newsgroup-spam-marked nil
10812                         gnus-newsgroup-dormant nil))
10813                 (setq gnus-newsgroup-unreads
10814                       (gnus-sorted-nunion
10815                        (gnus-sorted-intersection gnus-newsgroup-unreads
10816                                                  gnus-newsgroup-downloadable)
10817                        gnus-newsgroup-unfetched)))
10818             ;; We actually mark all articles as canceled, which we
10819             ;; have to do when using auto-expiry or adaptive scoring.
10820             (gnus-summary-show-all-threads)
10821             (if (and to-here reverse)
10822                 (progn
10823                   (goto-char to-here)
10824                   (gnus-summary-mark-current-read-and-unread-as-read
10825                    gnus-catchup-mark)
10826                   (while (gnus-summary-find-next (not all))
10827                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10828               (when (gnus-summary-first-subject (not all))
10829                 (while (and
10830                         (if to-here (< (point) to-here) t)
10831                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10832                         (gnus-summary-find-next (not all))))))
10833             (gnus-set-mode-line 'summary))
10834           t))
10835     (gnus-summary-position-point)))
10836
10837 (defun gnus-summary-catchup-to-here (&optional all)
10838   "Mark all unticked articles before the current one as read.
10839 If ALL is non-nil, also mark ticked and dormant articles as read."
10840   (interactive "P")
10841   (save-excursion
10842     (gnus-save-hidden-threads
10843       (let ((beg (point)))
10844         ;; We check that there are unread articles.
10845         (when (or all (gnus-summary-find-prev))
10846           (gnus-summary-catchup all t beg)))))
10847   (gnus-summary-position-point))
10848
10849 (defun gnus-summary-catchup-from-here (&optional all)
10850   "Mark all unticked articles after (and including) the current one as read.
10851 If ALL is non-nil, also mark ticked and dormant articles as read."
10852   (interactive "P")
10853   (save-excursion
10854     (gnus-save-hidden-threads
10855       (let ((beg (point)))
10856         ;; We check that there are unread articles.
10857         (when (or all (gnus-summary-find-next))
10858           (gnus-summary-catchup all t beg nil t)))))
10859   (gnus-summary-position-point))
10860
10861 (defun gnus-summary-catchup-all (&optional quietly)
10862   "Mark all articles in this newsgroup as read.
10863 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10864 instead, which marks only unread articles as read."
10865   (interactive "P")
10866   (gnus-summary-catchup t quietly))
10867
10868 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10869   "Mark all unread articles in this group as read, then exit.
10870 If prefix argument ALL is non-nil, all articles are marked as read.
10871 If QUIETLY is non-nil, no questions will be asked."
10872   (interactive "P")
10873   (when (gnus-summary-catchup all quietly nil 'fast)
10874     ;; Select next newsgroup or exit.
10875     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10876              (eq gnus-auto-select-next 'quietly))
10877         (gnus-summary-next-group nil)
10878       (gnus-summary-exit))))
10879
10880 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10881   "Mark all articles in this newsgroup as read, and then exit.
10882 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10883 instead, which marks only unread articles as read."
10884   (interactive "P")
10885   (gnus-summary-catchup-and-exit t quietly))
10886
10887 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10888   "Mark all articles in this group as read and select the next group.
10889 If given a prefix, mark all articles, unread as well as ticked, as
10890 read."
10891   (interactive "P")
10892   (save-excursion
10893     (gnus-summary-catchup all))
10894   (gnus-summary-next-group))
10895
10896 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10897   "Mark all articles in this group as read and select the previous group.
10898 If given a prefix, mark all articles, unread as well as ticked, as
10899 read."
10900   (interactive "P")
10901   (save-excursion
10902     (gnus-summary-catchup all))
10903   (gnus-summary-next-group nil nil t))
10904
10905 ;;;
10906 ;;; with article
10907 ;;;
10908
10909 (defmacro gnus-with-article (article &rest forms)
10910   "Select ARTICLE and perform FORMS in the original article buffer.
10911 Then replace the article with the result."
10912   `(progn
10913      ;; We don't want the article to be marked as read.
10914      (let (gnus-mark-article-hook)
10915        (gnus-summary-select-article t t nil ,article))
10916      (set-buffer gnus-original-article-buffer)
10917      ,@forms
10918      (if (not (gnus-check-backend-function
10919                'request-replace-article (car gnus-article-current)))
10920          (gnus-message 5 "Read-only group; not replacing")
10921        (unless (gnus-request-replace-article
10922                 ,article (car gnus-article-current)
10923                 (current-buffer) t)
10924          (error "Couldn't replace article")))
10925      ;; The cache and backlog have to be flushed somewhat.
10926      (when gnus-keep-backlog
10927        (gnus-backlog-remove-article
10928         (car gnus-article-current) (cdr gnus-article-current)))
10929      (when gnus-use-cache
10930        (gnus-cache-update-article
10931         (car gnus-article-current) (cdr gnus-article-current)))))
10932
10933 (put 'gnus-with-article 'lisp-indent-function 1)
10934 (put 'gnus-with-article 'edebug-form-spec '(form body))
10935
10936 ;; Thread-based commands.
10937
10938 (defun gnus-summary-articles-in-thread (&optional article)
10939   "Return a list of all articles in the current thread.
10940 If ARTICLE is non-nil, return all articles in the thread that starts
10941 with that article."
10942   (let* ((article (or article (gnus-summary-article-number)))
10943          (data (gnus-data-find-list article))
10944          (top-level (gnus-data-level (car data)))
10945          (top-subject
10946           (cond ((null gnus-thread-operation-ignore-subject)
10947                  (gnus-simplify-subject-re
10948                   (mail-header-subject (gnus-data-header (car data)))))
10949                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10950                  (gnus-simplify-subject-fuzzy
10951                   (mail-header-subject (gnus-data-header (car data)))))
10952                 (t nil)))
10953          (end-point (save-excursion
10954                       (if (gnus-summary-go-to-next-thread)
10955                           (point) (point-max))))
10956          articles)
10957     (while (and data
10958                 (< (gnus-data-pos (car data)) end-point))
10959       (when (or (not top-subject)
10960                 (string= top-subject
10961                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10962                              (gnus-simplify-subject-fuzzy
10963                               (mail-header-subject
10964                                (gnus-data-header (car data))))
10965                            (gnus-simplify-subject-re
10966                             (mail-header-subject
10967                              (gnus-data-header (car data)))))))
10968         (push (gnus-data-number (car data)) articles))
10969       (unless (and (setq data (cdr data))
10970                    (> (gnus-data-level (car data)) top-level))
10971         (setq data nil)))
10972     ;; Return the list of articles.
10973     (nreverse articles)))
10974
10975 (defun gnus-summary-rethread-current ()
10976   "Rethread the thread the current article is part of."
10977   (interactive)
10978   (let* ((gnus-show-threads t)
10979          (article (gnus-summary-article-number))
10980          (id (mail-header-id (gnus-summary-article-header)))
10981          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10982     (unless id
10983       (error "No article on the current line"))
10984     (gnus-rebuild-thread id)
10985     (gnus-summary-goto-subject article)))
10986
10987 (defun gnus-summary-reparent-thread ()
10988   "Make the current article child of the marked (or previous) article.
10989
10990 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10991 is non-nil or the Subject: of both articles are the same."
10992   (interactive)
10993   (unless (not (gnus-group-read-only-p))
10994     (error "The current newsgroup does not support article editing"))
10995   (unless (<= (length gnus-newsgroup-processable) 1)
10996     (error "No more than one article may be marked"))
10997   (let ((child (gnus-summary-article-number))
10998         ;; First grab the marked article, otherwise one line up.
10999         (parent (if (not (null gnus-newsgroup-processable))
11000                     (car gnus-newsgroup-processable)
11001                   (save-excursion
11002                     (if (eq (forward-line -1) 0)
11003                         (gnus-summary-article-number)
11004                       (error "Beginning of summary buffer"))))))
11005     (gnus-summary-reparent-children parent (list child))))
11006
11007 (defun gnus-summary-reparent-children (parent children)
11008   "Make PARENT the parent of CHILDREN.
11009 When called interactively, PARENT is the current article and CHILDREN
11010 are the process-marked articles."
11011   (interactive
11012    (list (gnus-summary-article-number)
11013          (gnus-summary-work-articles nil)))
11014   (dolist (child children)
11015     (save-window-excursion
11016       (let ((gnus-article-buffer " *reparent*"))
11017         (unless (not (eq parent child))
11018           (error "An article may not be self-referential"))
11019         (let ((message-id (mail-header-id
11020                            (gnus-summary-article-header parent))))
11021           (unless (and message-id (not (equal message-id "")))
11022             (error "No message-id in desired parent"))
11023           (gnus-with-article child
11024             (save-restriction
11025               (goto-char (point-min))
11026               (message-narrow-to-head)
11027               (if (re-search-forward "^References: " nil t)
11028                   (progn
11029                     (re-search-forward "^[^ \t]" nil t)
11030                     (forward-line -1)
11031                     (end-of-line)
11032                     (insert " " message-id))
11033                 (insert "References: " message-id "\n"))))
11034           (set-buffer gnus-summary-buffer)
11035           (gnus-summary-unmark-all-processable)
11036           (gnus-summary-update-article child)
11037           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11038             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11039           (gnus-summary-rethread-current)
11040           (gnus-message 3 "Article %d is now the child of article %d"
11041                         child parent))))))
11042
11043 (defun gnus-summary-toggle-threads (&optional arg)
11044   "Toggle showing conversation threads.
11045 If ARG is positive number, turn showing conversation threads on."
11046   (interactive "P")
11047   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11048     (setq gnus-show-threads
11049           (if (null arg) (not gnus-show-threads)
11050             (> (prefix-numeric-value arg) 0)))
11051     (gnus-summary-prepare)
11052     (gnus-summary-goto-subject current)
11053     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11054     (gnus-summary-position-point)))
11055
11056 (defun gnus-summary-show-all-threads ()
11057   "Show all threads."
11058   (interactive)
11059   (save-excursion
11060     (let ((buffer-read-only nil))
11061       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11062   (gnus-summary-position-point))
11063
11064 (defun gnus-summary-show-thread ()
11065   "Show thread subtrees.
11066 Returns nil if no thread was there to be shown."
11067   (interactive)
11068   (let ((buffer-read-only nil)
11069         (orig (point))
11070         (end (point-at-eol))
11071         ;; Leave point at bol
11072         (beg (progn (beginning-of-line) (point))))
11073     (prog1
11074         ;; Any hidden lines here?
11075         (search-forward "\r" end t)
11076       (subst-char-in-region beg end ?\^M ?\n t)
11077       (goto-char orig)
11078       (gnus-summary-position-point))))
11079
11080 (defun gnus-summary-maybe-hide-threads ()
11081   "If requested, hide the threads that should be hidden."
11082   (when (and gnus-show-threads
11083              gnus-thread-hide-subtree)
11084     (gnus-summary-hide-all-threads
11085      (if (or (consp gnus-thread-hide-subtree)
11086              (functionp gnus-thread-hide-subtree))
11087          (gnus-make-predicate gnus-thread-hide-subtree)
11088        nil))))
11089
11090 ;;; Hiding predicates.
11091
11092 (defun gnus-article-unread-p (header)
11093   (memq (mail-header-number header) gnus-newsgroup-unreads))
11094
11095 (defun gnus-article-unseen-p (header)
11096   (memq (mail-header-number header) gnus-newsgroup-unseen))
11097
11098 (defun gnus-map-articles (predicate articles)
11099   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11100   (apply 'gnus-or (mapcar predicate
11101                           (mapcar (lambda (number)
11102                                     (gnus-summary-article-header number))
11103                                   articles))))
11104
11105 (defun gnus-summary-hide-all-threads (&optional predicate)
11106   "Hide all thread subtrees.
11107 If PREDICATE is supplied, threads that satisfy this predicate
11108 will not be hidden."
11109   (interactive)
11110   (save-excursion
11111     (goto-char (point-min))
11112     (let ((end nil))
11113       (while (not end)
11114         (when (or (not predicate)
11115                   (gnus-map-articles
11116                    predicate (gnus-summary-article-children)))
11117             (gnus-summary-hide-thread))
11118         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11119   (gnus-summary-position-point))
11120
11121 (defun gnus-summary-hide-thread ()
11122   "Hide thread subtrees.
11123 If PREDICATE is supplied, threads that satisfy this predicate
11124 will not be hidden.
11125 Returns nil if no threads were there to be hidden."
11126   (interactive)
11127   (let ((buffer-read-only nil)
11128         (start (point))
11129         (article (gnus-summary-article-number)))
11130     (goto-char start)
11131     ;; Go forward until either the buffer ends or the subthread
11132     ;; ends.
11133     (when (and (not (eobp))
11134                (or (zerop (gnus-summary-next-thread 1 t))
11135                    (goto-char (point-max))))
11136       (prog1
11137           (if (and (> (point) start)
11138                    (search-backward "\n" start t))
11139               (progn
11140                 (subst-char-in-region start (point) ?\n ?\^M)
11141                 (gnus-summary-goto-subject article))
11142             (goto-char start)
11143             nil)))))
11144
11145 (defun gnus-summary-go-to-next-thread (&optional previous)
11146   "Go to the same level (or less) next thread.
11147 If PREVIOUS is non-nil, go to previous thread instead.
11148 Return the article number moved to, or nil if moving was impossible."
11149   (let ((level (gnus-summary-thread-level))
11150         (way (if previous -1 1))
11151         (beg (point)))
11152     (forward-line way)
11153     (while (and (not (eobp))
11154                 (< level (gnus-summary-thread-level)))
11155       (forward-line way))
11156     (if (eobp)
11157         (progn
11158           (goto-char beg)
11159           nil)
11160       (setq beg (point))
11161       (prog1
11162           (gnus-summary-article-number)
11163         (goto-char beg)))))
11164
11165 (defun gnus-summary-next-thread (n &optional silent)
11166   "Go to the same level next N'th thread.
11167 If N is negative, search backward instead.
11168 Returns the difference between N and the number of skips actually
11169 done.
11170
11171 If SILENT, don't output messages."
11172   (interactive "p")
11173   (let ((backward (< n 0))
11174         (n (abs n)))
11175     (while (and (> n 0)
11176                 (gnus-summary-go-to-next-thread backward))
11177       (decf n))
11178     (unless silent
11179       (gnus-summary-position-point))
11180     (when (and (not silent) (/= 0 n))
11181       (gnus-message 7 "No more threads"))
11182     n))
11183
11184 (defun gnus-summary-prev-thread (n)
11185   "Go to the same level previous N'th thread.
11186 Returns the difference between N and the number of skips actually
11187 done."
11188   (interactive "p")
11189   (gnus-summary-next-thread (- n)))
11190
11191 (defun gnus-summary-go-down-thread ()
11192   "Go down one level in the current thread."
11193   (let ((children (gnus-summary-article-children)))
11194     (when children
11195       (gnus-summary-goto-subject (car children)))))
11196
11197 (defun gnus-summary-go-up-thread ()
11198   "Go up one level in the current thread."
11199   (let ((parent (gnus-summary-article-parent)))
11200     (when parent
11201       (gnus-summary-goto-subject parent))))
11202
11203 (defun gnus-summary-down-thread (n)
11204   "Go down thread N steps.
11205 If N is negative, go up instead.
11206 Returns the difference between N and how many steps down that were
11207 taken."
11208   (interactive "p")
11209   (let ((up (< n 0))
11210         (n (abs n)))
11211     (while (and (> n 0)
11212                 (if up (gnus-summary-go-up-thread)
11213                   (gnus-summary-go-down-thread)))
11214       (setq n (1- n)))
11215     (gnus-summary-position-point)
11216     (when (/= 0 n)
11217       (gnus-message 7 "Can't go further"))
11218     n))
11219
11220 (defun gnus-summary-up-thread (n)
11221   "Go up thread N steps.
11222 If N is negative, go down instead.
11223 Returns the difference between N and how many steps down that were
11224 taken."
11225   (interactive "p")
11226   (gnus-summary-down-thread (- n)))
11227
11228 (defun gnus-summary-top-thread ()
11229   "Go to the top of the thread."
11230   (interactive)
11231   (while (gnus-summary-go-up-thread))
11232   (gnus-summary-article-number))
11233
11234 (defun gnus-summary-kill-thread (&optional unmark)
11235   "Mark articles under current thread as read.
11236 If the prefix argument is positive, remove any kinds of marks.
11237 If the prefix argument is negative, tick articles instead."
11238   (interactive "P")
11239   (when unmark
11240     (setq unmark (prefix-numeric-value unmark)))
11241   (let ((articles (gnus-summary-articles-in-thread)))
11242     (save-excursion
11243       ;; Expand the thread.
11244       (gnus-summary-show-thread)
11245       ;; Mark all the articles.
11246       (while articles
11247         (gnus-summary-goto-subject (car articles))
11248         (cond ((null unmark)
11249                (gnus-summary-mark-article-as-read gnus-killed-mark))
11250               ((> unmark 0)
11251                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11252               (t
11253                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11254         (setq articles (cdr articles))))
11255     ;; Hide killed subtrees.
11256     (and (null unmark)
11257          gnus-thread-hide-killed
11258          (gnus-summary-hide-thread))
11259     ;; If marked as read, go to next unread subject.
11260     (when (null unmark)
11261       ;; Go to next unread subject.
11262       (gnus-summary-next-subject 1 t)))
11263   (gnus-set-mode-line 'summary))
11264
11265 ;; Summary sorting commands
11266
11267 (defun gnus-summary-sort-by-number (&optional reverse)
11268   "Sort the summary buffer by article number.
11269 Argument REVERSE means reverse order."
11270   (interactive "P")
11271   (gnus-summary-sort 'number reverse))
11272
11273 (defun gnus-summary-sort-by-random (&optional reverse)
11274   "Randomize the order in the summary buffer.
11275 Argument REVERSE means to randomize in reverse order."
11276   (interactive "P")
11277   (gnus-summary-sort 'random reverse))
11278
11279 (defun gnus-summary-sort-by-author (&optional reverse)
11280   "Sort the summary buffer by author name alphabetically.
11281 If `case-fold-search' is non-nil, case of letters is ignored.
11282 Argument REVERSE means reverse order."
11283   (interactive "P")
11284   (gnus-summary-sort 'author reverse))
11285
11286 (defun gnus-summary-sort-by-recipient (&optional reverse)
11287   "Sort the summary buffer by recipient name alphabetically.
11288 If `case-fold-search' is non-nil, case of letters is ignored.
11289 Argument REVERSE means reverse order."
11290   (interactive "P")
11291   (gnus-summary-sort 'recipient reverse))
11292
11293 (defun gnus-summary-sort-by-subject (&optional reverse)
11294   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11295 If `case-fold-search' is non-nil, case of letters is ignored.
11296 Argument REVERSE means reverse order."
11297   (interactive "P")
11298   (gnus-summary-sort 'subject reverse))
11299
11300 (defun gnus-summary-sort-by-date (&optional reverse)
11301   "Sort the summary buffer by date.
11302 Argument REVERSE means reverse order."
11303   (interactive "P")
11304   (gnus-summary-sort 'date reverse))
11305
11306 (defun gnus-summary-sort-by-score (&optional reverse)
11307   "Sort the summary buffer by score.
11308 Argument REVERSE means reverse order."
11309   (interactive "P")
11310   (gnus-summary-sort 'score reverse))
11311
11312 (defun gnus-summary-sort-by-lines (&optional reverse)
11313   "Sort the summary buffer by the number of lines.
11314 Argument REVERSE means reverse order."
11315   (interactive "P")
11316   (gnus-summary-sort 'lines reverse))
11317
11318 (defun gnus-summary-sort-by-chars (&optional reverse)
11319   "Sort the summary buffer by article length.
11320 Argument REVERSE means reverse order."
11321   (interactive "P")
11322   (gnus-summary-sort 'chars reverse))
11323
11324 (defun gnus-summary-sort-by-original (&optional reverse)
11325   "Sort the summary buffer using the default sorting method.
11326 Argument REVERSE means reverse order."
11327   (interactive "P")
11328   (let* ((buffer-read-only)
11329          (gnus-summary-prepare-hook nil))
11330     ;; We do the sorting by regenerating the threads.
11331     (gnus-summary-prepare)
11332     ;; Hide subthreads if needed.
11333     (gnus-summary-maybe-hide-threads)))
11334
11335 (defun gnus-summary-sort (predicate reverse)
11336   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11337   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11338          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11339          (gnus-thread-sort-functions
11340           (if (not reverse)
11341               thread
11342             `(lambda (t1 t2)
11343                (,thread t2 t1))))
11344          (gnus-sort-gathered-threads-function
11345           gnus-thread-sort-functions)
11346          (gnus-article-sort-functions
11347           (if (not reverse)
11348               article
11349             `(lambda (t1 t2)
11350                (,article t2 t1))))
11351          (buffer-read-only)
11352          (gnus-summary-prepare-hook nil))
11353     ;; We do the sorting by regenerating the threads.
11354     (gnus-summary-prepare)
11355     ;; Hide subthreads if needed.
11356     (gnus-summary-maybe-hide-threads)))
11357
11358 ;; Summary saving commands.
11359
11360 (defun gnus-summary-save-article (&optional n not-saved)
11361   "Save the current article using the default saver function.
11362 If N is a positive number, save the N next articles.
11363 If N is a negative number, save the N previous articles.
11364 If N is nil and any articles have been marked with the process mark,
11365 save those articles instead.
11366 The variable `gnus-default-article-saver' specifies the saver function.
11367
11368 If the optional second argument NOT-SAVED is non-nil, articles saved
11369 will not be marked as saved."
11370   (interactive "P")
11371   (require 'gnus-art)
11372   (let* ((articles (gnus-summary-work-articles n))
11373          (save-buffer (save-excursion
11374                         (nnheader-set-temp-buffer " *Gnus Save*")))
11375          (num (length articles))
11376          ;; Whether to save decoded articles or raw articles.
11377          (decode (when gnus-article-save-coding-system
11378                    (get gnus-default-article-saver :decode)))
11379          ;; When saving many articles in a single file, use the other
11380          ;; function to save articles other than the first one.
11381          (saver2 (get gnus-default-article-saver :function))
11382          (gnus-prompt-before-saving (if saver2
11383                                         t
11384                                       gnus-prompt-before-saving))
11385          (gnus-default-article-saver gnus-default-article-saver)
11386          header file)
11387     (dolist (article articles)
11388       (setq header (gnus-summary-article-header article))
11389       (if (not (vectorp header))
11390           ;; This is a pseudo-article.
11391           (if (assq 'name header)
11392               (gnus-copy-file (cdr (assq 'name header)))
11393             (gnus-message 1 "Article %d is unsaveable" article))
11394         ;; This is a real article.
11395         (save-window-excursion
11396           (let ((gnus-display-mime-function (when decode
11397                                               gnus-display-mime-function))
11398                 (gnus-article-prepare-hook (when decode
11399                                              gnus-article-prepare-hook)))
11400             (gnus-summary-select-article t nil nil article)
11401             (gnus-summary-goto-subject article)))
11402         (save-excursion
11403           (set-buffer save-buffer)
11404           (erase-buffer)
11405           (insert-buffer-substring (if decode
11406                                        gnus-article-buffer
11407                                      gnus-original-article-buffer)))
11408         (setq file (gnus-article-save save-buffer file num))
11409         (gnus-summary-remove-process-mark article)
11410         (unless not-saved
11411           (gnus-summary-set-saved-mark article)))
11412       (when saver2
11413         (setq gnus-default-article-saver saver2
11414               saver2 nil)))
11415     (gnus-kill-buffer save-buffer)
11416     (gnus-summary-position-point)
11417     (gnus-set-mode-line 'summary)
11418     n))
11419
11420 (defun gnus-summary-pipe-output (&optional arg headers)
11421   "Pipe the current article to a subprocess.
11422 If N is a positive number, pipe the N next articles.
11423 If N is a negative number, pipe the N previous articles.
11424 If N is nil and any articles have been marked with the process mark,
11425 pipe those articles instead.
11426 If HEADERS (the symbolic prefix), include the headers, too."
11427   (interactive (gnus-interactive "P\ny"))
11428   (require 'gnus-art)
11429   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11430         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11431     (gnus-summary-save-article arg t))
11432   (let ((buffer (get-buffer "*Shell Command Output*")))
11433     (when (and buffer
11434                (not (zerop (buffer-size buffer))))
11435       (gnus-configure-windows 'pipe))))
11436
11437 (defun gnus-summary-save-article-mail (&optional arg)
11438   "Append the current article to a Unix mail box file.
11439 If N is a positive number, save the N next articles.
11440 If N is a negative number, save the N previous articles.
11441 If N is nil and any articles have been marked with the process mark,
11442 save those articles instead."
11443   (interactive "P")
11444   (require 'gnus-art)
11445   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11446     (gnus-summary-save-article arg)))
11447
11448 (defun gnus-summary-save-article-rmail (&optional arg)
11449   "Append the current article to an rmail file.
11450 If N is a positive number, save the N next articles.
11451 If N is a negative number, save the N previous articles.
11452 If N is nil and any articles have been marked with the process mark,
11453 save those articles instead."
11454   (interactive "P")
11455   (require 'gnus-art)
11456   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11457     (gnus-summary-save-article arg)))
11458
11459 (defun gnus-summary-save-article-file (&optional arg)
11460   "Append the current article to a file.
11461 If N is a positive number, save the N next articles.
11462 If N is a negative number, save the N previous articles.
11463 If N is nil and any articles have been marked with the process mark,
11464 save those articles instead."
11465   (interactive "P")
11466   (require 'gnus-art)
11467   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11468     (gnus-summary-save-article arg)))
11469
11470 (defun gnus-summary-write-article-file (&optional arg)
11471   "Write the current article to a file, deleting the previous file.
11472 If N is a positive number, save the N next articles.
11473 If N is a negative number, save the N previous articles.
11474 If N is nil and any articles have been marked with the process mark,
11475 save those articles instead."
11476   (interactive "P")
11477   (require 'gnus-art)
11478   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11479     (gnus-summary-save-article arg)))
11480
11481 (defun gnus-summary-save-article-body-file (&optional arg)
11482   "Append the current article body to a file.
11483 If N is a positive number, save the N next articles.
11484 If N is a negative number, save the N previous articles.
11485 If N is nil and any articles have been marked with the process mark,
11486 save those articles instead."
11487   (interactive "P")
11488   (require 'gnus-art)
11489   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11490     (gnus-summary-save-article arg)))
11491
11492 (defun gnus-summary-write-article-body-file (&optional arg)
11493   "Write the current article body to a file, deleting the previous file.
11494 If N is a positive number, save the N next articles.
11495 If N is a negative number, save the N previous articles.
11496 If N is nil and any articles have been marked with the process mark,
11497 save those articles instead."
11498   (interactive "P")
11499   (require 'gnus-art)
11500   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11501     (gnus-summary-save-article arg)))
11502
11503 (defun gnus-summary-muttprint (&optional arg)
11504   "Print the current article using Muttprint.
11505 If N is a positive number, save the N next articles.
11506 If N is a negative number, save the N previous articles.
11507 If N is nil and any articles have been marked with the process mark,
11508 save those articles instead."
11509   (interactive "P")
11510   (require 'gnus-art)
11511   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11512     (gnus-summary-save-article arg t)))
11513
11514 (defun gnus-summary-pipe-message (program)
11515   "Pipe the current article through PROGRAM."
11516   (interactive "sProgram: ")
11517   (gnus-summary-select-article)
11518   (let ((mail-header-separator ""))
11519     (gnus-eval-in-buffer-window gnus-article-buffer
11520       (save-restriction
11521         (widen)
11522         (let ((start (window-start))
11523               buffer-read-only)
11524           (message-pipe-buffer-body program)
11525           (set-window-start (get-buffer-window (current-buffer)) start))))))
11526
11527 (defun gnus-get-split-value (methods)
11528   "Return a value based on the split METHODS."
11529   (let (split-name method result match)
11530     (when methods
11531       (save-excursion
11532         (set-buffer gnus-original-article-buffer)
11533         (save-restriction
11534           (nnheader-narrow-to-headers)
11535           (while (and methods (not split-name))
11536             (goto-char (point-min))
11537             (setq method (pop methods))
11538             (setq match (car method))
11539             (when (cond
11540                    ((stringp match)
11541                     ;; Regular expression.
11542                     (ignore-errors
11543                       (re-search-forward match nil t)))
11544                    ((functionp match)
11545                     ;; Function.
11546                     (save-restriction
11547                       (widen)
11548                       (setq result (funcall match gnus-newsgroup-name))))
11549                    ((consp match)
11550                     ;; Form.
11551                     (save-restriction
11552                       (widen)
11553                       (setq result (eval match)))))
11554               (setq split-name (cdr method))
11555               (cond ((stringp result)
11556                      (push (expand-file-name
11557                             result gnus-article-save-directory)
11558                            split-name))
11559                     ((consp result)
11560                      (setq split-name (append result split-name)))))))))
11561     (nreverse split-name)))
11562
11563 (defun gnus-valid-move-group-p (group)
11564   (and (boundp group)
11565        (symbol-name group)
11566        (symbol-value group)
11567        (gnus-get-function (gnus-find-method-for-group
11568                            (symbol-name group)) 'request-accept-article t)))
11569
11570 (defun gnus-read-move-group-name (prompt default articles prefix)
11571   "Read a group name."
11572   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11573          (minibuffer-confirm-incomplete nil) ; XEmacs
11574          (prom
11575           (format "%s %s to"
11576                   prompt
11577                   (if (> (length articles) 1)
11578                       (format "these %d articles" (length articles))
11579                     "this article")))
11580          (to-newsgroup
11581           (cond
11582            ((null split-name)
11583             (gnus-completing-read-with-default
11584              default prom
11585              gnus-active-hashtb
11586              'gnus-valid-move-group-p
11587              nil prefix
11588              'gnus-group-history))
11589            ((= 1 (length split-name))
11590             (gnus-completing-read-with-default
11591              (car split-name) prom
11592              gnus-active-hashtb
11593              'gnus-valid-move-group-p
11594              nil nil
11595              'gnus-group-history))
11596            (t
11597             (gnus-completing-read-with-default
11598              nil prom
11599              (mapcar 'list (nreverse split-name))
11600              nil nil nil
11601              'gnus-group-history))))
11602          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11603     (when to-newsgroup
11604       (if (or (string= to-newsgroup "")
11605               (string= to-newsgroup prefix))
11606           (setq to-newsgroup default))
11607       (unless to-newsgroup
11608         (error "No group name entered"))
11609       (or (gnus-active to-newsgroup)
11610           (gnus-activate-group to-newsgroup nil nil to-method)
11611           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11612                                      to-newsgroup))
11613               (or (and (gnus-request-create-group to-newsgroup to-method)
11614                        (gnus-activate-group
11615                         to-newsgroup nil nil to-method)
11616                        (gnus-subscribe-group to-newsgroup))
11617                   (error "Couldn't create group %s" to-newsgroup)))
11618           (error "No such group: %s" to-newsgroup)))
11619     to-newsgroup))
11620
11621 (defvar gnus-summary-save-parts-counter)
11622
11623 (defun gnus-summary-save-parts (type dir n &optional reverse)
11624   "Save parts matching TYPE to DIR.
11625 If REVERSE, save parts that do not match TYPE."
11626   (interactive
11627    (list (read-string "Save parts of type: "
11628                       (or (car gnus-summary-save-parts-type-history)
11629                           gnus-summary-save-parts-default-mime)
11630                       'gnus-summary-save-parts-type-history)
11631          (setq gnus-summary-save-parts-last-directory
11632                (read-file-name "Save to directory: "
11633                                gnus-summary-save-parts-last-directory
11634                                nil t))
11635          current-prefix-arg))
11636   (gnus-summary-iterate n
11637     (let ((gnus-display-mime-function nil)
11638           (gnus-inhibit-treatment t))
11639       (gnus-summary-select-article))
11640     (save-excursion
11641       (set-buffer gnus-article-buffer)
11642       (let ((handles (or gnus-article-mime-handles
11643                          (mm-dissect-buffer nil gnus-article-loose-mime)
11644                          (and gnus-article-emulate-mime
11645                               (mm-uu-dissect))))
11646             (gnus-summary-save-parts-counter 1))
11647         (when handles
11648           (gnus-summary-save-parts-1 type dir handles reverse)
11649           (unless gnus-article-mime-handles ;; Don't destroy this case.
11650             (mm-destroy-parts handles)))))))
11651
11652 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11653   (if (stringp (car handle))
11654       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11655               (cdr handle))
11656     (when (if reverse
11657               (not (string-match type (mm-handle-media-type handle)))
11658             (string-match type (mm-handle-media-type handle)))
11659       (let ((file (expand-file-name
11660                    (gnus-map-function
11661                     mm-file-name-rewrite-functions
11662                     (file-name-nondirectory
11663                      (or
11664                       (mail-content-type-get
11665                        (mm-handle-disposition handle) 'filename)
11666                       (mail-content-type-get
11667                        (mm-handle-type handle) 'name)
11668                       (format "%s.%d.%d" gnus-newsgroup-name
11669                               (cdr gnus-article-current)
11670                               gnus-summary-save-parts-counter))))
11671                    dir)))
11672         (incf gnus-summary-save-parts-counter)
11673         (unless (file-exists-p file)
11674           (mm-save-part-to-file handle file))))))
11675
11676 ;; Summary extract commands
11677
11678 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11679   (let ((buffer-read-only nil)
11680         (article (gnus-summary-article-number))
11681         after-article b e)
11682     (unless (gnus-summary-goto-subject article)
11683       (error "No such article: %d" article))
11684     (gnus-summary-position-point)
11685     ;; If all commands are to be bunched up on one line, we collect
11686     ;; them here.
11687     (unless gnus-view-pseudos-separately
11688       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11689             files action)
11690         (while ps
11691           (setq action (cdr (assq 'action (car ps))))
11692           (setq files (list (cdr (assq 'name (car ps)))))
11693           (while (and ps (cdr ps)
11694                       (string= (or action "1")
11695                                (or (cdr (assq 'action (cadr ps))) "2")))
11696             (push (cdr (assq 'name (cadr ps))) files)
11697             (setcdr ps (cddr ps)))
11698           (when files
11699             (when (not (string-match "%s" action))
11700               (push " " files))
11701             (push " " files)
11702             (when (assq 'execute (car ps))
11703               (setcdr (assq 'execute (car ps))
11704                       (funcall (if (string-match "%s" action)
11705                                    'format 'concat)
11706                                action
11707                                (mapconcat
11708                                 (lambda (f)
11709                                   (if (equal f " ")
11710                                       f
11711                                     (shell-quote-argument f)))
11712                                 files " ")))))
11713           (setq ps (cdr ps)))))
11714     (if (and gnus-view-pseudos (not not-view))
11715         (while pslist
11716           (when (assq 'execute (car pslist))
11717             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11718                                   (eq gnus-view-pseudos 'not-confirm)))
11719           (setq pslist (cdr pslist)))
11720       (save-excursion
11721         (while pslist
11722           (setq after-article (or (cdr (assq 'article (car pslist)))
11723                                   (gnus-summary-article-number)))
11724           (gnus-summary-goto-subject after-article)
11725           (forward-line 1)
11726           (setq b (point))
11727           (insert "    " (file-name-nondirectory
11728                           (cdr (assq 'name (car pslist))))
11729                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11730           (setq e (point))
11731           (forward-line -1)             ; back to `b'
11732           (gnus-add-text-properties
11733            b (1- e) (list 'gnus-number gnus-reffed-article-number
11734                           gnus-mouse-face-prop gnus-mouse-face))
11735           (gnus-data-enter
11736            after-article gnus-reffed-article-number
11737            gnus-unread-mark b (car pslist) 0 (- e b))
11738           (setq gnus-newsgroup-unreads
11739                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11740                                          gnus-reffed-article-number))
11741           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11742           (setq pslist (cdr pslist)))))))
11743
11744 (defun gnus-pseudos< (p1 p2)
11745   (let ((c1 (cdr (assq 'action p1)))
11746         (c2 (cdr (assq 'action p2))))
11747     (and c1 c2 (string< c1 c2))))
11748
11749 (defun gnus-request-pseudo-article (props)
11750   (cond ((assq 'execute props)
11751          (gnus-execute-command (cdr (assq 'execute props)))))
11752   (let ((gnus-current-article (gnus-summary-article-number)))
11753     (gnus-run-hooks 'gnus-mark-article-hook)))
11754
11755 (defun gnus-execute-command (command &optional automatic)
11756   (save-excursion
11757     (gnus-article-setup-buffer)
11758     (set-buffer gnus-article-buffer)
11759     (setq buffer-read-only nil)
11760     (let ((command (if automatic command
11761                      (read-string "Command: " (cons command 0)))))
11762       (erase-buffer)
11763       (insert "$ " command "\n\n")
11764       (if gnus-view-pseudo-asynchronously
11765           (start-process "gnus-execute" (current-buffer) shell-file-name
11766                          shell-command-switch command)
11767         (call-process shell-file-name nil t nil
11768                       shell-command-switch command)))))
11769
11770 ;; Summary kill commands.
11771
11772 (defun gnus-summary-edit-global-kill (article)
11773   "Edit the \"global\" kill file."
11774   (interactive (list (gnus-summary-article-number)))
11775   (gnus-group-edit-global-kill article))
11776
11777 (defun gnus-summary-edit-local-kill ()
11778   "Edit a local kill file applied to the current newsgroup."
11779   (interactive)
11780   (setq gnus-current-headers (gnus-summary-article-header))
11781   (gnus-group-edit-local-kill
11782    (gnus-summary-article-number) gnus-newsgroup-name))
11783
11784 ;;; Header reading.
11785
11786 (defun gnus-read-header (id &optional header)
11787   "Read the headers of article ID and enter them into the Gnus system."
11788   (let ((group gnus-newsgroup-name)
11789         (gnus-override-method
11790          (or
11791           gnus-override-method
11792           (and (gnus-news-group-p gnus-newsgroup-name)
11793                (car (gnus-refer-article-methods)))))
11794         where)
11795     ;; First we check to see whether the header in question is already
11796     ;; fetched.
11797     (if (stringp id)
11798         ;; This is a Message-ID.
11799         (setq header (or header (gnus-id-to-header id)))
11800       ;; This is an article number.
11801       (setq header (or header (gnus-summary-article-header id))))
11802     (if (and header
11803              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11804         ;; We have found the header.
11805         header
11806       ;; We have to really fetch the header to this article.
11807       (save-excursion
11808         (set-buffer nntp-server-buffer)
11809         (when (setq where (gnus-request-head id group))
11810           (nnheader-fold-continuation-lines)
11811           (goto-char (point-max))
11812           (insert ".\n")
11813           (goto-char (point-min))
11814           (insert "211 ")
11815           (princ (cond
11816                   ((numberp id) id)
11817                   ((cdr where) (cdr where))
11818                   (header (mail-header-number header))
11819                   (t gnus-reffed-article-number))
11820                  (current-buffer))
11821           (insert " Article retrieved.\n"))
11822         (if (or (not where)
11823                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11824             ()                          ; Malformed head.
11825           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11826             (when (and (stringp id)
11827                        (or
11828                         (not (string= (gnus-group-real-name group)
11829                                       (car where)))
11830                         (not (gnus-server-equal gnus-override-method
11831                                                 (gnus-group-method group)))))
11832               ;; If we fetched by Message-ID and the article came from
11833               ;; a different group (or server), we fudge some bogus
11834               ;; article numbers for this article.
11835               (mail-header-set-number header gnus-reffed-article-number))
11836             (save-excursion
11837               (set-buffer gnus-summary-buffer)
11838               (decf gnus-reffed-article-number)
11839               (gnus-remove-header (mail-header-number header))
11840               (push header gnus-newsgroup-headers)
11841               (setq gnus-current-headers header)
11842               (push (mail-header-number header) gnus-newsgroup-limit)))
11843           header)))))
11844
11845 (defun gnus-remove-header (number)
11846   "Remove header NUMBER from `gnus-newsgroup-headers'."
11847   (if (and gnus-newsgroup-headers
11848            (= number (mail-header-number (car gnus-newsgroup-headers))))
11849       (pop gnus-newsgroup-headers)
11850     (let ((headers gnus-newsgroup-headers))
11851       (while (and (cdr headers)
11852                   (not (= number (mail-header-number (cadr headers)))))
11853         (pop headers))
11854       (when (cdr headers)
11855         (setcdr headers (cddr headers))))))
11856
11857 ;;;
11858 ;;; summary highlights
11859 ;;;
11860
11861 (defun gnus-highlight-selected-summary ()
11862   "Highlight selected article in summary buffer."
11863   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11864   (when gnus-summary-selected-face
11865     (save-excursion
11866       (let* ((beg (point-at-bol))
11867              (end (point-at-eol))
11868              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11869              (from (if (get-text-property beg gnus-mouse-face-prop)
11870                        beg
11871                      (or (next-single-property-change
11872                           beg gnus-mouse-face-prop nil end)
11873                          beg)))
11874              (to
11875               (if (= from end)
11876                   (- from 2)
11877                 (or (next-single-property-change
11878                      from gnus-mouse-face-prop nil end)
11879                     end))))
11880         ;; If no mouse-face prop on line we will have to = from = end,
11881         ;; so we highlight the entire line instead.
11882         (when (= (+ to 2) from)
11883           (setq from beg)
11884           (setq to end))
11885         (if gnus-newsgroup-selected-overlay
11886             ;; Move old overlay.
11887             (gnus-move-overlay
11888              gnus-newsgroup-selected-overlay from to (current-buffer))
11889           ;; Create new overlay.
11890           (gnus-overlay-put
11891            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11892            'face gnus-summary-selected-face))))))
11893
11894 (defvar gnus-summary-highlight-line-cached nil)
11895 (defvar gnus-summary-highlight-line-trigger nil)
11896
11897 (defun gnus-summary-highlight-line-0 ()
11898   (if (and (eq gnus-summary-highlight-line-trigger
11899                gnus-summary-highlight)
11900            gnus-summary-highlight-line-cached)
11901       gnus-summary-highlight-line-cached
11902     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11903           gnus-summary-highlight-line-cached
11904           (let* ((cond (list 'cond))
11905                  (c cond)
11906                  (list gnus-summary-highlight))
11907             (while list
11908               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11909                               nil))
11910               (setq c (cdr c)
11911                     list (cdr list)))
11912             (gnus-byte-compile (list 'lambda nil cond))))))
11913
11914 (defun gnus-summary-highlight-line ()
11915   "Highlight current line according to `gnus-summary-highlight'."
11916   (let* ((beg (point-at-bol))
11917          (article (or (gnus-summary-article-number) gnus-current-article))
11918          (score (or (cdr (assq article
11919                                gnus-newsgroup-scored))
11920                     gnus-summary-default-score 0))
11921          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11922          (inhibit-read-only t)
11923          (default gnus-summary-default-score)
11924          (default-high gnus-summary-default-high-score)
11925          (default-low gnus-summary-default-low-score)
11926          (uncached (and gnus-summary-use-undownloaded-faces
11927                         (memq article gnus-newsgroup-undownloaded)
11928                         (not (memq article gnus-newsgroup-cached)))))
11929     (let ((face (funcall (gnus-summary-highlight-line-0))))
11930       (unless (eq face (get-text-property beg 'face))
11931         (gnus-put-text-property-excluding-characters-with-faces
11932          beg (point-at-eol) 'face
11933          (setq face (if (boundp face) (symbol-value face) face)))
11934         (when gnus-summary-highlight-line-function
11935           (funcall gnus-summary-highlight-line-function article face))))))
11936
11937 (defun gnus-update-read-articles (group unread &optional compute)
11938   "Update the list of read articles in GROUP.
11939 UNREAD is a sorted list."
11940   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11941         (info (gnus-get-info group))
11942         (prev 1)
11943         read)
11944     (if (or (not info) (not active))
11945         ;; There is no info on this group if it was, in fact,
11946         ;; killed.  Gnus stores no information on killed groups, so
11947         ;; there's nothing to be done.
11948         ;; One could store the information somewhere temporarily,
11949         ;; perhaps...  Hmmm...
11950         ()
11951       ;; Remove any negative articles numbers.
11952       (while (and unread (< (car unread) 0))
11953         (setq unread (cdr unread)))
11954       ;; Remove any expired article numbers
11955       (while (and unread (< (car unread) (car active)))
11956         (setq unread (cdr unread)))
11957       ;; Compute the ranges of read articles by looking at the list of
11958       ;; unread articles.
11959       (while unread
11960         (when (/= (car unread) prev)
11961           (push (if (= prev (1- (car unread))) prev
11962                   (cons prev (1- (car unread))))
11963                 read))
11964         (setq prev (1+ (car unread)))
11965         (setq unread (cdr unread)))
11966       (when (<= prev (cdr active))
11967         (push (cons prev (cdr active)) read))
11968       (setq read (if (> (length read) 1) (nreverse read) read))
11969       (if compute
11970           read
11971         (save-excursion
11972           (let (setmarkundo)
11973             ;; Propagate the read marks to the backend.
11974             (when (gnus-check-backend-function 'request-set-mark group)
11975               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11976                     (add (gnus-remove-from-range read (gnus-info-read info))))
11977                 (when (or add del)
11978                   (unless (gnus-check-group group)
11979                     (error "Can't open server for %s" group))
11980                   (gnus-request-set-mark
11981                    group (delq nil (list (if add (list add 'add '(read)))
11982                                          (if del (list del 'del '(read))))))
11983                   (setq setmarkundo
11984                         `(gnus-request-set-mark
11985                           ,group
11986                           ',(delq nil (list
11987                                        (if del (list del 'add '(read)))
11988                                        (if add (list add 'del '(read))))))))))
11989             (set-buffer gnus-group-buffer)
11990             (gnus-undo-register
11991               `(progn
11992                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11993                  (gnus-info-set-read ',info ',(gnus-info-read info))
11994                  (gnus-get-unread-articles-in-group ',info
11995                                                     (gnus-active ,group))
11996                  (gnus-group-update-group ,group t)
11997                  ,setmarkundo))))
11998         ;; Enter this list into the group info.
11999         (gnus-info-set-read info read)
12000         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12001         (gnus-get-unread-articles-in-group info (gnus-active group))
12002         t))))
12003
12004 (defun gnus-offer-save-summaries ()
12005   "Offer to save all active summary buffers."
12006   (let (buffers)
12007     ;; Go through all buffers and find all summaries.
12008     (dolist (buffer (buffer-list))
12009       (when (and (setq buffer (buffer-name buffer))
12010                  (string-match "Summary" buffer)
12011                  (with-current-buffer buffer
12012                    ;; We check that this is, indeed, a summary buffer.
12013                    (and (eq major-mode 'gnus-summary-mode)
12014                         ;; Also make sure this isn't bogus.
12015                         gnus-newsgroup-prepared
12016                         ;; Also make sure that this isn't a
12017                         ;; dead summary buffer.
12018                         (not gnus-dead-summary-mode))))
12019         (push buffer buffers)))
12020     ;; Go through all these summary buffers and offer to save them.
12021     (when buffers
12022       (save-excursion
12023         (map-y-or-n-p
12024          "Update summary buffer %s? "
12025          (lambda (buf)
12026            (switch-to-buffer buf)
12027            (gnus-summary-exit))
12028          buffers)))))
12029
12030 (defun gnus-summary-setup-default-charset ()
12031   "Setup newsgroup default charset."
12032   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12033       (setq gnus-newsgroup-charset nil)
12034     (let* ((ignored-charsets
12035             (or gnus-newsgroup-ephemeral-ignored-charsets
12036                 (append
12037                  (and gnus-newsgroup-name
12038                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12039                  gnus-newsgroup-ignored-charsets))))
12040       (setq gnus-newsgroup-charset
12041             (or gnus-newsgroup-ephemeral-charset
12042                 (and gnus-newsgroup-name
12043                      (gnus-parameter-charset gnus-newsgroup-name))
12044                 gnus-default-charset))
12045       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12046            ignored-charsets))))
12047
12048 ;;;
12049 ;;; Mime Commands
12050 ;;;
12051
12052 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12053   "Display the current article buffer fully MIME-buttonized.
12054 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12055 treated as multipart/mixed."
12056   (interactive "P")
12057   (require 'gnus-art)
12058   (let ((gnus-unbuttonized-mime-types nil)
12059         (gnus-mime-display-multipart-as-mixed show-all-parts))
12060     (gnus-summary-show-article)))
12061
12062 (defun gnus-summary-repair-multipart (article)
12063   "Add a Content-Type header to a multipart article without one."
12064   (interactive (list (gnus-summary-article-number)))
12065   (gnus-with-article article
12066     (message-narrow-to-head)
12067     (message-remove-header "Mime-Version")
12068     (goto-char (point-max))
12069     (insert "Mime-Version: 1.0\n")
12070     (widen)
12071     (when (search-forward "\n--" nil t)
12072       (let ((separator (buffer-substring (point) (point-at-eol))))
12073         (message-narrow-to-head)
12074         (message-remove-header "Content-Type")
12075         (goto-char (point-max))
12076         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12077                         separator))
12078         (widen))))
12079   (let (gnus-mark-article-hook)
12080     (gnus-summary-select-article t t nil article)))
12081
12082 (defun gnus-summary-toggle-display-buttonized ()
12083   "Toggle the buttonizing of the article buffer."
12084   (interactive)
12085   (require 'gnus-art)
12086   (if (setq gnus-inhibit-mime-unbuttonizing
12087             (not gnus-inhibit-mime-unbuttonizing))
12088       (let ((gnus-unbuttonized-mime-types nil))
12089         (gnus-summary-show-article))
12090     (gnus-summary-show-article)))
12091
12092 ;;;
12093 ;;; Generic summary marking commands
12094 ;;;
12095
12096 (defvar gnus-summary-marking-alist
12097   '((read gnus-del-mark "d")
12098     (unread gnus-unread-mark "u")
12099     (ticked gnus-ticked-mark "!")
12100     (dormant gnus-dormant-mark "?")
12101     (expirable gnus-expirable-mark "e"))
12102   "An alist of names/marks/keystrokes.")
12103
12104 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12105 (defvar gnus-summary-mark-map)
12106
12107 (defun gnus-summary-make-all-marking-commands ()
12108   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12109   (dolist (elem gnus-summary-marking-alist)
12110     (apply 'gnus-summary-make-marking-command elem)))
12111
12112 (defun gnus-summary-make-marking-command (name mark keystroke)
12113   (let ((map (make-sparse-keymap)))
12114     (define-key gnus-summary-generic-mark-map keystroke map)
12115     (dolist (lway `((next "next" next nil "n")
12116                     (next-unread "next unread" next t "N")
12117                     (prev "previous" prev nil "p")
12118                     (prev-unread "previous unread" prev t "P")
12119                     (nomove "" nil nil ,keystroke)))
12120       (let ((func (gnus-summary-make-marking-command-1
12121                    mark (car lway) lway name)))
12122         (setq func (eval func))
12123         (define-key map (nth 4 lway) func)))))
12124
12125 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12126   `(defun ,(intern
12127             (format "gnus-summary-put-mark-as-%s%s"
12128                     name (if (eq way 'nomove)
12129                              ""
12130                            (concat "-" (symbol-name way)))))
12131      (n)
12132      ,(format
12133        "Mark the current article as %s%s.
12134 If N, the prefix, then repeat N times.
12135 If N is negative, move in reverse order.
12136 The difference between N and the actual number of articles marked is
12137 returned."
12138        name (cadr lway))
12139      (interactive "p")
12140      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12141
12142 (defun gnus-summary-generic-mark (n mark move unread)
12143   "Mark N articles with MARK."
12144   (unless (eq major-mode 'gnus-summary-mode)
12145     (error "This command can only be used in the summary buffer"))
12146   (gnus-summary-show-thread)
12147   (let ((nummove
12148          (cond
12149           ((eq move 'next) 1)
12150           ((eq move 'prev) -1)
12151           (t 0))))
12152     (if (zerop nummove)
12153         (setq n 1)
12154       (when (< n 0)
12155         (setq n (abs n)
12156               nummove (* -1 nummove))))
12157     (while (and (> n 0)
12158                 (gnus-summary-mark-article nil mark)
12159                 (zerop (gnus-summary-next-subject nummove unread t)))
12160       (setq n (1- n)))
12161     (when (/= 0 n)
12162       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12163     (gnus-summary-recenter)
12164     (gnus-summary-position-point)
12165     (gnus-set-mode-line 'summary)
12166     n))
12167
12168 (defun gnus-summary-insert-articles (articles)
12169   (when (setq articles
12170               (gnus-sorted-difference articles
12171                                       (mapcar (lambda (h)
12172                                                 (mail-header-number h))
12173                                               gnus-newsgroup-headers)))
12174     (setq gnus-newsgroup-headers
12175           (gnus-merge 'list
12176                       gnus-newsgroup-headers
12177                       (gnus-fetch-headers articles)
12178                       'gnus-article-sort-by-number))
12179     ;; Suppress duplicates?
12180     (when gnus-suppress-duplicates
12181       (gnus-dup-suppress-articles))
12182
12183     ;; We might want to build some more threads first.
12184     (when (and gnus-fetch-old-headers
12185                (eq gnus-headers-retrieved-by 'nov))
12186       (if (eq gnus-fetch-old-headers 'invisible)
12187           (gnus-build-all-threads)
12188         (gnus-build-old-threads)))
12189     ;; Let the Gnus agent mark articles as read.
12190     (when gnus-agent
12191       (gnus-agent-get-undownloaded-list))
12192     ;; Remove list identifiers from subject
12193     (when gnus-list-identifiers
12194       (gnus-summary-remove-list-identifiers))
12195     ;; First and last article in this newsgroup.
12196     (when gnus-newsgroup-headers
12197       (setq gnus-newsgroup-begin
12198             (mail-header-number (car gnus-newsgroup-headers))
12199             gnus-newsgroup-end
12200             (mail-header-number
12201              (gnus-last-element gnus-newsgroup-headers))))
12202     (when gnus-use-scoring
12203       (gnus-possibly-score-headers))))
12204
12205 (defun gnus-summary-insert-old-articles (&optional all)
12206   "Insert all old articles in this group.
12207 If ALL is non-nil, already read articles become readable.
12208 If ALL is a number, fetch this number of articles."
12209   (interactive "P")
12210   (prog1
12211       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12212             older len)
12213         (setq older
12214               ;; Some nntp servers lie about their active range.  When
12215               ;; this happens, the active range can be in the millions.
12216               ;; Use a compressed range to avoid creating a huge list.
12217               (gnus-range-difference (list gnus-newsgroup-active) old))
12218         (setq len (gnus-range-length older))
12219         (cond
12220          ((null older) nil)
12221          ((numberp all)
12222           (if (< all len)
12223               (let ((older-range (nreverse older)))
12224                 (setq older nil)
12225
12226                 (while (> all 0)
12227                   (let* ((r (pop older-range))
12228                          (min (if (numberp r) r (car r)))
12229                          (max (if (numberp r) r (cdr r))))
12230                     (while (and (<= min max)
12231                                 (> all 0))
12232                       (push max older)
12233                       (setq all (1- all)
12234                             max (1- max))))))
12235             (setq older (gnus-uncompress-range older))))
12236          (all
12237           (setq older (gnus-uncompress-range older)))
12238          (t
12239           (when (and (numberp gnus-large-newsgroup)
12240                    (> len gnus-large-newsgroup))
12241               (let* ((cursor-in-echo-area nil)
12242                      (initial (gnus-parameter-large-newsgroup-initial
12243                                gnus-newsgroup-name))
12244                      (input
12245                       (read-string
12246                        (format
12247                         "How many articles from %s (%s %d): "
12248                         (gnus-limit-string
12249                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12250                         (if initial "max" "default")
12251                         len)
12252                        (if initial
12253                            (cons (number-to-string initial)
12254                                  0)))))
12255                 (unless (string-match "^[ \t]*$" input)
12256                   (setq all (string-to-number input))
12257                   (if (< all len)
12258                       (let ((older-range (nreverse older)))
12259                         (setq older nil)
12260
12261                         (while (> all 0)
12262                           (let* ((r (pop older-range))
12263                                  (min (if (numberp r) r (car r)))
12264                                  (max (if (numberp r) r (cdr r))))
12265                             (while (and (<= min max)
12266                                         (> all 0))
12267                               (push max older)
12268                               (setq all (1- all)
12269                                     max (1- max))))))))))
12270           (setq older (gnus-uncompress-range older))))
12271         (if (not older)
12272             (message "No old news.")
12273           (gnus-summary-insert-articles older)
12274           (gnus-summary-limit (gnus-sorted-nunion old older))))
12275     (gnus-summary-position-point)))
12276
12277 (defun gnus-summary-insert-new-articles ()
12278   "Insert all new articles in this group."
12279   (interactive)
12280   (prog1
12281       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12282             (old-active gnus-newsgroup-active)
12283             (nnmail-fetched-sources (list t))
12284             i new)
12285         (setq gnus-newsgroup-active
12286               (gnus-activate-group gnus-newsgroup-name 'scan))
12287         (setq i (cdr gnus-newsgroup-active))
12288         (while (> i (cdr old-active))
12289           (push i new)
12290           (decf i))
12291         (if (not new)
12292             (message "No gnus is bad news")
12293           (gnus-summary-insert-articles new)
12294           (setq gnus-newsgroup-unreads
12295                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12296           (gnus-summary-limit (gnus-sorted-nunion old new))))
12297     (gnus-summary-position-point)))
12298
12299 (gnus-summary-make-all-marking-commands)
12300
12301 (gnus-ems-redefine)
12302
12303 (provide 'gnus-sum)
12304
12305 (run-hooks 'gnus-sum-load-hook)
12306
12307 ;; Local Variables:
12308 ;; coding: iso-8859-1
12309 ;; End:
12310
12311 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12312 ;;; gnus-sum.el ends here