(gnus-summary-set-local-parameters): Don't bind parameters that haven't existed
[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, 2008 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 3 of the License, or
14 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33
34 (defvar tool-bar-mode)
35 (defvar gnus-tmp-header)
36
37 (require 'gnus)
38 (require 'gnus-group)
39 (require 'gnus-spec)
40 (require 'gnus-range)
41 (require 'gnus-int)
42 (require 'gnus-undo)
43 (require 'gnus-util)
44 (require 'gmm-utils)
45 (require 'mm-decode)
46 (require 'nnoo)
47
48 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
49 (autoload 'gnus-cache-write-active "gnus-cache")
50 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
51 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
52 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
53 (autoload 'mm-uu-dissect "mm-uu")
54 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
55   "Deuglify broken Outlook (Express) articles and redisplay."
56   t)
57 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
58 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
59 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-summary-next-group-on-exit t
69   "If non-nil, go to the next unread newsgroup on summary exit.
70 See `gnus-group-goto-unread'."
71   :link '(custom-manual "(gnus)Group Maneuvering")
72   :group 'gnus-summary-exit
73   :version "23.1" ;; No Gnus
74   :type 'boolean)
75
76 (defcustom gnus-fetch-old-headers nil
77   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
78 If an unread article in the group refers to an older, already
79 read (or just marked as read) article, the old article will not
80 normally be displayed in the Summary buffer.  If this variable is
81 t, Gnus will attempt to grab the headers to the old articles, and
82 thereby build complete threads.  If it has the value `some', all
83 old headers will be fetched but only enough headers to connect
84 otherwise loose threads will be displayed.  This variable can
85 also be a number.  In that case, no more than that number of old
86 headers will be fetched.  If it has the value `invisible', all
87 old headers will be fetched, but none will be displayed.
88
89 The server has to support NOV for any of this to work.
90
91 This feature can seriously impact performance it ignores all
92 locally cached header entries.  Setting it to t for groups for a
93 server that doesn't expire articles (such as news.gmane.org),
94 leads to very slow summary generation."
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const :tag "on" t)
98                  (const some)
99                  (const invisible)
100                  number
101                  (sexp :menu-tag "other" t)))
102
103 (defcustom gnus-refer-thread-limit 500
104   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
105 If t, fetch all the available old headers."
106   :group 'gnus-thread
107   :type '(choice number
108                  (sexp :menu-tag "other" t)))
109
110 (defcustom gnus-summary-make-false-root 'adopt
111   "*nil means that Gnus won't gather loose threads.
112 If the root of a thread has expired or been read in a previous
113 session, the information necessary to build a complete thread has been
114 lost.  Instead of having many small sub-threads from this original thread
115 scattered all over the summary buffer, Gnus can gather them.
116
117 If non-nil, Gnus will try to gather all loose sub-threads from an
118 original thread into one large thread.
119
120 If this variable is non-nil, it should be one of `none', `adopt',
121 `dummy' or `empty'.
122
123 If this variable is `none', Gnus will not make a false root, but just
124 present the sub-threads after another.
125 If this variable is `dummy', Gnus will create a dummy root that will
126 have all the sub-threads as children.
127 If this variable is `adopt', Gnus will make one of the \"children\"
128 the parent and mark all the step-children as such.
129 If this variable is `empty', the \"children\" are printed with empty
130 subject fields.  (Or rather, they will be printed with a string
131 given by the `gnus-summary-same-subject' variable.)"
132   :group 'gnus-thread
133   :type '(choice (const :tag "off" nil)
134                  (const none)
135                  (const dummy)
136                  (const adopt)
137                  (const empty)))
138
139 (defcustom gnus-summary-make-false-root-always nil
140   "Always make a false dummy root."
141   :version "22.1"
142   :group 'gnus-thread
143   :type 'boolean)
144
145 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
146   "*A regexp to match subjects to be excluded from loose thread gathering.
147 As loose thread gathering is done on subjects only, that means that
148 there can be many false gatherings performed.  By rooting out certain
149 common subjects, gathering might become saner."
150   :group 'gnus-thread
151   :type 'regexp)
152
153 (defcustom gnus-summary-gather-subject-limit nil
154   "*Maximum length of subject comparisons when gathering loose threads.
155 Use nil to compare full subjects.  Setting this variable to a low
156 number will help gather threads that have been corrupted by
157 newsreaders chopping off subject lines, but it might also mean that
158 unrelated articles that have subject that happen to begin with the
159 same few characters will be incorrectly gathered.
160
161 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
162 comparing subjects."
163   :group 'gnus-thread
164   :type '(choice (const :tag "off" nil)
165                  (const fuzzy)
166                  (sexp :menu-tag "on" t)))
167
168 (defcustom gnus-simplify-subject-functions nil
169   "List of functions taking a string argument that simplify subjects.
170 The functions are applied recursively.
171
172 Useful functions to put in this list include:
173 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
174 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
175   :group 'gnus-thread
176   :type '(repeat function))
177
178 (defcustom gnus-simplify-ignored-prefixes nil
179   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
180   :group 'gnus-thread
181   :type '(choice (const :tag "off" nil)
182                  regexp))
183
184 (defcustom gnus-build-sparse-threads nil
185   "*If non-nil, fill in the gaps in threads.
186 If `some', only fill in the gaps that are needed to tie loose threads
187 together.  If `more', fill in all leaf nodes that Gnus can find.  If
188 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
189   :group 'gnus-thread
190   :type '(choice (const :tag "off" nil)
191                  (const some)
192                  (const more)
193                  (sexp :menu-tag "all" t)))
194
195 (defcustom gnus-summary-thread-gathering-function
196   'gnus-gather-threads-by-subject
197   "*Function used for gathering loose threads.
198 There are two pre-defined functions: `gnus-gather-threads-by-subject',
199 which only takes Subjects into consideration; and
200 `gnus-gather-threads-by-references', which compared the References
201 headers of the articles to find matches."
202   :group 'gnus-thread
203   :type '(radio (function-item gnus-gather-threads-by-subject)
204                 (function-item gnus-gather-threads-by-references)
205                 (function :tag "other")))
206
207 (defcustom gnus-summary-same-subject ""
208   "*String indicating that the current article has the same subject as the previous.
209 This variable will only be used if the value of
210 `gnus-summary-make-false-root' is `empty'."
211   :group 'gnus-summary-format
212   :type 'string)
213
214 (defcustom gnus-summary-goto-unread t
215   "*If t, many commands will go to the next unread article.
216 This applies to marking commands as well as other commands that
217 \"naturally\" select the next article, like, for instance, `SPC' at
218 the end of an article.
219
220 If nil, the marking commands do NOT go to the next unread article
221 \(they go to the next article instead).  If `never', commands that
222 usually go to the next unread article, will go to the next article,
223 whether it is read or not."
224   :group 'gnus-summary-marks
225   :link '(custom-manual "(gnus)Setting Marks")
226   :type '(choice (const :tag "off" nil)
227                  (const never)
228                  (sexp :menu-tag "on" t)))
229
230 (defcustom gnus-summary-default-score 0
231   "*Default article score level.
232 All scores generated by the score files will be added to this score.
233 If this variable is nil, scoring will be disabled."
234   :group 'gnus-score-default
235   :type '(choice (const :tag "disable")
236                  integer))
237
238 (defcustom gnus-summary-default-high-score 0
239   "*Default threshold for a high scored article.
240 An article will be highlighted as high scored if its score is greater
241 than this score."
242   :version "22.1"
243   :group 'gnus-score-default
244   :type 'integer)
245
246 (defcustom gnus-summary-default-low-score 0
247   "*Default threshold for a low scored article.
248 An article will be highlighted as low scored if its score is smaller
249 than this score."
250   :version "22.1"
251   :group 'gnus-score-default
252   :type 'integer)
253
254 (defcustom gnus-summary-zcore-fuzz 0
255   "*Fuzziness factor for the zcore in the summary buffer.
256 Articles with scores closer than this to `gnus-summary-default-score'
257 will not be marked."
258   :group 'gnus-summary-format
259   :type 'integer)
260
261 (defcustom gnus-simplify-subject-fuzzy-regexp nil
262   "*Strings to be removed when doing fuzzy matches.
263 This can either be a regular expression or list of regular expressions
264 that will be removed from subject strings if fuzzy subject
265 simplification is selected."
266   :group 'gnus-thread
267   :type '(repeat regexp))
268
269 (defcustom gnus-show-threads t
270   "*If non-nil, display threads in summary mode."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-hide-subtree nil
275   "*If non-nil, hide all threads initially.
276 This can be a predicate specifier which says which threads to hide.
277 If threads are hidden, you have to run the command
278 `gnus-summary-show-thread' by hand or select an article."
279   :group 'gnus-thread
280   :type '(radio (sexp :format "Non-nil\n"
281                       :match (lambda (widget value)
282                                (not (or (consp value) (functionp value))))
283                       :value t)
284                 (const nil)
285                 (sexp :tag "Predicate specifier")))
286
287 (defcustom gnus-thread-hide-killed t
288   "*If non-nil, hide killed threads automatically."
289   :group 'gnus-thread
290   :type 'boolean)
291
292 (defcustom gnus-thread-ignore-subject t
293   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
294 If nil, articles that have different subjects from their parents will
295 start separate threads."
296   :group 'gnus-thread
297   :type 'boolean)
298
299 (defcustom gnus-thread-operation-ignore-subject t
300   "*If non-nil, subjects will be ignored when doing thread commands.
301 This affects commands like `gnus-summary-kill-thread' and
302 `gnus-summary-lower-thread'.
303
304 If this variable is nil, articles in the same thread with different
305 subjects will not be included in the operation in question.  If this
306 variable is `fuzzy', only articles that have subjects that are fuzzily
307 equal will be included."
308   :group 'gnus-thread
309   :type '(choice (const :tag "off" nil)
310                  (const fuzzy)
311                  (sexp :tag "on" t)))
312
313 (defcustom gnus-thread-indent-level 4
314   "*Number that says how much each sub-thread should be indented."
315   :group 'gnus-thread
316   :type 'integer)
317
318 (defcustom gnus-auto-extend-newsgroup t
319   "*If non-nil, extend newsgroup forward and backward when requested."
320   :group 'gnus-summary-choose
321   :type 'boolean)
322
323 (defcustom gnus-auto-select-first t
324   "If non-nil, select an article on group entry.
325 An article is selected automatically when entering a group
326 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
327 `gnus-summary-catchup-and-goto-next-group'.
328
329 Which article is selected is controlled by the variable
330 `gnus-auto-select-subject'.
331
332 If you want to prevent automatic selection of articles in some
333 newsgroups, set the variable to nil in `gnus-select-group-hook'."
334   ;; Commands include...
335   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
336   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
337   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
338   :group 'gnus-group-select
339   :type '(choice (const :tag "none" nil)
340                  (sexp :menu-tag "first" t)))
341
342 (defcustom gnus-auto-select-subject 'unread
343   "*Says what subject to place under point when entering a group.
344
345 This variable can either be the symbols `first' (place point on the
346 first subject), `unread' (place point on the subject line of the first
347 unread article), `best' (place point on the subject line of the
348 higest-scored article), `unseen' (place point on the subject line of
349 the first unseen article), `unseen-or-unread' (place point on the subject
350 line of the first unseen article or, if all article have been seen, on the
351 subject line of the first unread article), or a function to be called to
352 place point on some subject line."
353   :version "22.1"
354   :group 'gnus-group-select
355   :type '(choice (const best)
356                  (const unread)
357                  (const first)
358                  (const unseen)
359                  (const unseen-or-unread)))
360
361 (defcustom gnus-auto-select-next t
362   "*If non-nil, offer to go to the next group from the end of the previous.
363 If the value is t and the next newsgroup is empty, Gnus will exit
364 summary mode and go back to group mode.  If the value is neither nil
365 nor t, Gnus will select the following unread newsgroup.  In
366 particular, if the value is the symbol `quietly', the next unread
367 newsgroup will be selected without any confirmation, and if it is
368 `almost-quietly', the next group will be selected without any
369 confirmation if you are located on the last article in the group.
370 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
371 will go to the next group without confirmation."
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "off" nil)
374                  (const quietly)
375                  (const almost-quietly)
376                  (const slightly-quietly)
377                  (sexp :menu-tag "on" t)))
378
379 (defcustom gnus-auto-select-same nil
380   "*If non-nil, select the next article with the same subject.
381 If there are no more articles with the same subject, go to
382 the first unread article."
383   :group 'gnus-summary-maneuvering
384   :type 'boolean)
385
386 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
387   "What article should be selected after exiting an ephemeral group.
388 Valid values include:
389
390 `next'
391   Select the next article.
392 `next-unread'
393   Select the next unread article.
394 `next-noselect'
395   Move the cursor to the next article.  This is the default.
396 `next-unread-noselect'
397   Move the cursor to the next unread article.
398
399 If it has any other value or there is no next (unread) article, the
400 article selected before entering to the ephemeral group will appear."
401   :version "23.1" ;; No Gnus
402   :group 'gnus-summary-maneuvering
403   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
404                  (const next) (const next-unread)
405                  (const next-noselect) (const next-unread-noselect)
406                  (sexp :tag "other" :value nil)))
407
408 (defcustom gnus-auto-goto-ignores 'unfetched
409   "*Says how to handle unfetched articles when maneuvering.
410
411 This variable can either be the symbols nil (maneuver to any
412 article), `undownloaded' (maneuvering while unplugged ignores articles
413 that have not been fetched), `always-undownloaded' (maneuvering always
414 ignores articles that have not been fetched), `unfetched' (maneuvering
415 ignores articles whose headers have not been fetched).
416
417 NOTE: The list of unfetched articles will always be nil when plugged
418 and, when unplugged, a subset of the undownloaded article list."
419   :version "22.1"
420   :group 'gnus-summary-maneuvering
421   :type '(choice (const :tag "None" nil)
422                  (const :tag "Undownloaded when unplugged" undownloaded)
423                  (const :tag "Undownloaded" always-undownloaded)
424                  (const :tag "Unfetched" unfetched)))
425
426 (defcustom gnus-summary-check-current nil
427   "*If non-nil, consider the current article when moving.
428 The \"unread\" movement commands will stay on the same line if the
429 current article is unread."
430   :group 'gnus-summary-maneuvering
431   :type 'boolean)
432
433 (defcustom gnus-auto-center-summary 2
434   "*If non-nil, always center the current summary buffer.
435 In particular, if `vertical' do only vertical recentering.  If non-nil
436 and non-`vertical', do both horizontal and vertical recentering."
437   :group 'gnus-summary-maneuvering
438   :type '(choice (const :tag "none" nil)
439                  (const vertical)
440                  (integer :tag "height")
441                  (sexp :menu-tag "both" t)))
442
443 (defvar gnus-auto-center-group t
444   "*If non-nil, always center the group buffer.")
445
446 (defcustom gnus-show-all-headers nil
447   "*If non-nil, don't hide any headers."
448   :group 'gnus-article-hiding
449   :group 'gnus-article-headers
450   :type 'boolean)
451
452 (defcustom gnus-summary-ignore-duplicates nil
453   "*If non-nil, ignore articles with identical Message-ID headers."
454   :group 'gnus-summary
455   :type 'boolean)
456
457 (defcustom gnus-single-article-buffer t
458   "*If non-nil, display all articles in the same buffer.
459 If nil, each group will get its own article buffer."
460   :group 'gnus-article-various
461   :type 'boolean)
462
463 (defcustom gnus-break-pages t
464   "*If non-nil, do page breaking on articles.
465 The page delimiter is specified by the `gnus-page-delimiter'
466 variable."
467   :group 'gnus-article-various
468   :type 'boolean)
469
470 (defcustom gnus-move-split-methods nil
471   "*Variable used to suggest where articles are to be moved to.
472 It uses the same syntax as the `gnus-split-methods' variable.
473 However, whereas `gnus-split-methods' specifies file names as targets,
474 this variable specifies group names."
475   :group 'gnus-summary-mail
476   :type '(repeat (choice (list :value (fun) function)
477                          (cons :value ("" "") regexp (repeat string))
478                          (sexp :value nil))))
479
480 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
481   "Function used to compute default prefix for article move/copy/etc prompts.
482 The function should take one argument, a group name, and return a
483 string with the suggested prefix."
484   :group 'gnus-summary-mail
485   :type 'function)
486
487 ;; FIXME: Although the custom type is `character' for the following variables,
488 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
489
490 (defcustom gnus-unread-mark ?           ;Whitespace
491   "*Mark used for unread articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ticked-mark ?!
496   "*Mark used for ticked articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-dormant-mark ??
501   "*Mark used for dormant articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-del-mark ?r
506   "*Mark used for del'd articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-read-mark ?R
511   "*Mark used for read articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-expirable-mark ?E
516   "*Mark used for expirable articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-killed-mark ?K
521   "*Mark used for killed articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-spam-mark ?$
526   "*Mark used for spam articles."
527   :version "22.1"
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-souped-mark ?F
532   "*Mark used for souped articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-kill-file-mark ?X
537   "*Mark used for articles killed by kill files."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-low-score-mark ?Y
542   "*Mark used for articles with a low score."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-catchup-mark ?C
547   "*Mark used for articles that are caught up."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-replied-mark ?A
552   "*Mark used for articles that have been replied to."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-forwarded-mark ?F
557   "*Mark used for articles that have been forwarded."
558   :version "22.1"
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-recent-mark ?N
563   "*Mark used for articles that are recent."
564   :version "22.1"
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-cached-mark ?*
569   "*Mark used for articles that are in the cache."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-saved-mark ?S
574   "*Mark used for articles that have been saved."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-unseen-mark ?.
579   "*Mark used for articles that haven't been seen."
580   :version "22.1"
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-no-mark ?               ;Whitespace
585   "*Mark used for articles that have no other secondary mark."
586   :version "22.1"
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-ancient-mark ?O
591   "*Mark used for ancient articles."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-sparse-mark ?Q
596   "*Mark used for sparsely reffed articles."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-canceled-mark ?G
601   "*Mark used for canceled articles."
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-duplicate-mark ?M
606   "*Mark used for duplicate articles."
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-undownloaded-mark ?-
611   "*Mark used for articles that weren't downloaded."
612   :version "22.1"
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-downloaded-mark ?+
617   "*Mark used for articles that were downloaded."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-downloadable-mark ?%
622   "*Mark used for articles that are to be downloaded."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-unsendable-mark ?=
627   "*Mark used for articles that won't be sent."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-score-over-mark ?+
632   "*Score mark used for articles with high scores."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-score-below-mark ?-
637   "*Score mark used for articles with low scores."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-empty-thread-mark ?     ;Whitespace
642   "*There is no thread under the article."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-not-empty-thread-mark ?=
647   "*There is a thread under the article."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-view-pseudo-asynchronously nil
652   "*If non-nil, Gnus will view pseudo-articles asynchronously."
653   :group 'gnus-extract-view
654   :type 'boolean)
655
656 (defcustom gnus-auto-expirable-marks
657   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
658         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
659         gnus-souped-mark gnus-duplicate-mark)
660   "*The list of marks converted into expiration if a group is auto-expirable."
661   :version "21.1"
662   :group 'gnus-summary
663   :type '(repeat character))
664
665 (defcustom gnus-inhibit-user-auto-expire t
666   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
667   :version "21.1"
668   :group 'gnus-summary
669   :type 'boolean)
670
671 (defcustom gnus-view-pseudos nil
672   "*If `automatic', pseudo-articles will be viewed automatically.
673 If `not-confirm', pseudos will be viewed automatically, and the user
674 will not be asked to confirm the command."
675   :group 'gnus-extract-view
676   :type '(choice (const :tag "off" nil)
677                  (const automatic)
678                  (const not-confirm)))
679
680 (defcustom gnus-view-pseudos-separately t
681   "*If non-nil, one pseudo-article will be created for each file to be viewed.
682 If nil, all files that use the same viewing command will be given as a
683 list of parameters to that command."
684   :group 'gnus-extract-view
685   :type 'boolean)
686
687 (defcustom gnus-insert-pseudo-articles t
688   "*If non-nil, insert pseudo-articles when decoding articles."
689   :group 'gnus-extract-view
690   :type 'boolean)
691
692 (defcustom gnus-summary-dummy-line-format
693   "   %(:                             :%) %S\n"
694   "*The format specification for the dummy roots in the summary buffer.
695 It works along the same lines as a normal formatting string,
696 with some simple extensions.
697
698 %S  The subject
699
700 General format specifiers can also be used.
701 See `(gnus)Formatting Variables'."
702   :link '(custom-manual "(gnus)Formatting Variables")
703   :group 'gnus-threading
704   :type 'string)
705
706 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
707   "*The format specification for the summary mode line.
708 It works along the same lines as a normal formatting string,
709 with some simple extensions:
710
711 %G  Group name
712 %p  Unprefixed group name
713 %A  Current article number
714 %z  Current article score
715 %V  Gnus version
716 %U  Number of unread articles in the group
717 %e  Number of unselected articles in the group
718 %Z  A string with unread/unselected article counts
719 %g  Shortish group name
720 %S  Subject of the current article
721 %u  User-defined spec
722 %s  Current score file name
723 %d  Number of dormant articles
724 %r  Number of articles that have been marked as read in this session
725 %E  Number of articles expunged by the score files"
726   :group 'gnus-summary-format
727   :type 'string)
728
729 (defcustom gnus-list-identifiers nil
730   "Regexp that matches list identifiers to be removed from subject.
731 This can also be a list of regexps."
732   :version "21.1"
733   :group 'gnus-summary-format
734   :group 'gnus-article-hiding
735   :type '(choice (const :tag "none" nil)
736                  (regexp :value ".*")
737                  (repeat :value (".*") regexp)))
738
739 (defcustom gnus-summary-mark-below 0
740   "*Mark all articles with a score below this variable as read.
741 This variable is local to each summary buffer and usually set by the
742 score file."
743   :group 'gnus-score-default
744   :type 'integer)
745
746 (defun gnus-widget-reversible-match (widget value)
747   "Ignoring WIDGET, convert VALUE to internal form.
748 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
749   ;; (debug value)
750   (or (symbolp value)
751       (and (listp value)
752            (eq (length value) 2)
753            (eq (nth 0 value) 'not)
754            (symbolp (nth 1 value)))))
755
756 (defun gnus-widget-reversible-to-internal (widget value)
757   "Ignoring WIDGET, convert VALUE to internal form.
758 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
759 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
760   ;; (debug value)
761   (if (atom value)
762       (list value nil)
763     (list (nth 1 value) t)))
764
765 (defun gnus-widget-reversible-to-external (widget value)
766   "Ignoring WIDGET, convert VALUE to external form.
767 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
768 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
769   ;; (debug value)
770   (if (nth 1 value)
771       (list 'not (nth 0 value))
772     (nth 0 value)))
773
774 (define-widget 'gnus-widget-reversible 'group
775   "A `group' that convert values."
776   :match 'gnus-widget-reversible-match
777   :value-to-internal 'gnus-widget-reversible-to-internal
778   :value-to-external 'gnus-widget-reversible-to-external)
779
780 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
781   "*List of functions used for sorting articles in the summary buffer.
782
783 Each function takes two articles and returns non-nil if the first
784 article 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-article-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 item can also be a list `(not F)' where F is a function;
793 this reverses the sort order.
794
795 Ready-made functions include `gnus-article-sort-by-number',
796 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
797 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
798 and `gnus-article-sort-by-score'.
799
800 When threading is turned on, the variable `gnus-thread-sort-functions'
801 controls how articles are sorted."
802   :group 'gnus-summary-sort
803   :type '(repeat (gnus-widget-reversible
804                   (choice (function-item gnus-article-sort-by-number)
805                           (function-item gnus-article-sort-by-author)
806                           (function-item gnus-article-sort-by-subject)
807                           (function-item gnus-article-sort-by-date)
808                           (function-item gnus-article-sort-by-score)
809                           (function-item gnus-article-sort-by-random)
810                           (function :tag "other"))
811                   (boolean :tag "Reverse order"))))
812
813
814 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
815   "*List of functions used for sorting threads in the summary buffer.
816 By default, threads are sorted by article number.
817
818 Each function takes two threads and returns non-nil if the first
819 thread should be sorted before the other.  If you use more than one
820 function, the primary sort function should be the last.  You should
821 probably always include `gnus-thread-sort-by-number' in the list of
822 sorting functions -- preferably first.  Also note that sorting by date
823 is often much slower than sorting by number, and the sorting order is
824 very similar.  (Sorting by date means sorting by the time the message
825 was sent, sorting by number means sorting by arrival time.)
826
827 Each list item can also be a list `(not F)' where F is a
828 function; this specifies reversed sort order.
829
830 Ready-made functions include `gnus-thread-sort-by-number',
831 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
832 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
833 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
834 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
835 and `gnus-thread-sort-by-total-score' (see
836 `gnus-thread-score-function').
837
838 When threading is turned off, the variable
839 `gnus-article-sort-functions' controls how articles are sorted."
840   :group 'gnus-summary-sort
841   :type '(repeat
842           (gnus-widget-reversible
843            (choice (function-item gnus-thread-sort-by-number)
844                    (function-item gnus-thread-sort-by-author)
845                    (function-item gnus-thread-sort-by-recipient)
846                    (function-item gnus-thread-sort-by-subject)
847                    (function-item gnus-thread-sort-by-date)
848                    (function-item gnus-thread-sort-by-score)
849                    (function-item gnus-thread-sort-by-most-recent-number)
850                    (function-item gnus-thread-sort-by-most-recent-date)
851                    (function-item gnus-thread-sort-by-random)
852                    (function-item gnus-thread-sort-by-total-score)
853                    (function :tag "other"))
854            (boolean :tag "Reverse order"))))
855
856 (defcustom gnus-thread-score-function '+
857   "*Function used for calculating the total score of a thread.
858
859 The function is called with the scores of the article and each
860 subthread and should then return the score of the thread.
861
862 Some functions you can use are `+', `max', or `min'."
863   :group 'gnus-summary-sort
864   :type 'function)
865
866 (defcustom gnus-summary-expunge-below nil
867   "All articles that have a score less than this variable will be expunged.
868 This variable is local to the summary buffers."
869   :group 'gnus-score-default
870   :type '(choice (const :tag "off" nil)
871                  integer))
872
873 (defcustom gnus-thread-expunge-below nil
874   "All threads that have a total score less than this variable will be expunged.
875 See `gnus-thread-score-function' for en explanation of what a
876 \"thread score\" is.
877
878 This variable is local to the summary buffers."
879   :group 'gnus-threading
880   :group 'gnus-score-default
881   :type '(choice (const :tag "off" nil)
882                  integer))
883
884 (defcustom gnus-summary-mode-hook nil
885   "*A hook for Gnus summary mode.
886 This hook is run before any variables are set in the summary buffer."
887   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
888   :group 'gnus-summary-various
889   :type 'hook)
890
891 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
892 (when (featurep 'xemacs)
893   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
894   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
895   (add-hook 'gnus-summary-mode-hook
896             'gnus-xmas-switch-horizontal-scrollbar-off))
897
898 (defcustom gnus-summary-menu-hook nil
899   "*Hook run after the creation of the summary mode menu."
900   :group 'gnus-summary-visual
901   :type 'hook)
902
903 (defcustom gnus-summary-exit-hook nil
904   "*A hook called on exit from the summary buffer.
905 It will be called with point in the group buffer."
906   :group 'gnus-summary-exit
907   :type 'hook)
908
909 (defcustom gnus-summary-prepare-hook nil
910   "*A hook called after the summary buffer has been generated.
911 If you want to modify the summary buffer, you can use this hook."
912   :group 'gnus-summary-various
913   :type 'hook)
914
915 (defcustom gnus-summary-prepared-hook nil
916   "*A hook called as the last thing after the summary buffer has been generated."
917   :group 'gnus-summary-various
918   :type 'hook)
919
920 (defcustom gnus-summary-generate-hook nil
921   "*A hook run just before generating the summary buffer.
922 This hook is commonly used to customize threading variables and the
923 like."
924   :group 'gnus-summary-various
925   :type 'hook)
926
927 (defcustom gnus-select-group-hook nil
928   "*A hook called when a newsgroup is selected.
929
930 If you'd like to simplify subjects like the
931 `gnus-summary-next-same-subject' command does, you can use the
932 following hook:
933
934  (add-hook gnus-select-group-hook
935            (lambda ()
936              (mapcar (lambda (header)
937                        (mail-header-set-subject
938                         header
939                         (gnus-simplify-subject
940                          (mail-header-subject header) 're-only)))
941                      gnus-newsgroup-headers)))"
942   :group 'gnus-group-select
943   :type 'hook)
944
945 (defcustom gnus-select-article-hook nil
946   "*A hook called when an article is selected."
947   :group 'gnus-summary-choose
948   :options '(gnus-agent-fetch-selected-article)
949   :type 'hook)
950
951 (defcustom gnus-visual-mark-article-hook
952   (list 'gnus-highlight-selected-summary)
953   "*Hook run after selecting an article in the summary buffer.
954 It is meant to be used for highlighting the article in some way.  It
955 is not run if `gnus-visual' is nil."
956   :group 'gnus-summary-visual
957   :type 'hook)
958
959 (defcustom gnus-parse-headers-hook nil
960   "*A hook called before parsing the headers."
961   :group 'gnus-various
962   :type 'hook)
963
964 (defcustom gnus-exit-group-hook nil
965   "*A hook called when exiting summary mode.
966 This hook is not called from the non-updating exit commands like `Q'."
967   :group 'gnus-various
968   :type 'hook)
969
970 (defcustom gnus-summary-update-hook
971   (list 'gnus-summary-highlight-line)
972   "*A hook called when a summary line is changed.
973 The hook will not be called if `gnus-visual' is nil.
974
975 The default function `gnus-summary-highlight-line' will
976 highlight the line according to the `gnus-summary-highlight'
977 variable."
978   :group 'gnus-summary-visual
979   :type 'hook)
980
981 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
982   "*A hook called when an article is selected for the first time.
983 The hook is intended to mark an article as read (or unread)
984 automatically when it is selected."
985   :group 'gnus-summary-choose
986   :type 'hook)
987
988 (defcustom gnus-group-no-more-groups-hook nil
989   "*A hook run when returning to group mode having no more (unread) groups."
990   :group 'gnus-group-select
991   :type 'hook)
992
993 (defcustom gnus-ps-print-hook nil
994   "*A hook run before ps-printing something from Gnus."
995   :group 'gnus-summary
996   :type 'hook)
997
998 (defcustom gnus-summary-article-move-hook nil
999   "*A hook called after an article is moved, copied, respooled, or crossposted."
1000   :version "22.1"
1001   :group 'gnus-summary
1002   :type 'hook)
1003
1004 (defcustom gnus-summary-article-delete-hook nil
1005   "*A hook called after an article is deleted."
1006   :version "22.1"
1007   :group 'gnus-summary
1008   :type 'hook)
1009
1010 (defcustom gnus-summary-article-expire-hook nil
1011   "*A hook called after an article is expired."
1012   :version "22.1"
1013   :group 'gnus-summary
1014   :type 'hook)
1015
1016 (defcustom gnus-summary-display-arrow
1017   (and (fboundp 'display-graphic-p)
1018        (display-graphic-p))
1019   "*If non-nil, display an arrow highlighting the current article."
1020   :version "22.1"
1021   :group 'gnus-summary
1022   :type 'boolean)
1023
1024 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1025   "Face used for highlighting the current article in the summary buffer."
1026   :group 'gnus-summary-visual
1027   :type 'face)
1028
1029 (defvar gnus-tmp-downloaded nil)
1030
1031 (defcustom gnus-summary-highlight
1032   '(((eq mark gnus-canceled-mark)
1033      . gnus-summary-cancelled)
1034     ((and uncached (> score default-high))
1035      . gnus-summary-high-undownloaded)
1036     ((and uncached (< score default-low))
1037      . gnus-summary-low-undownloaded)
1038     (uncached
1039      . gnus-summary-normal-undownloaded)
1040     ((and (> score default-high)
1041           (or (eq mark gnus-dormant-mark)
1042               (eq mark gnus-ticked-mark)))
1043      . gnus-summary-high-ticked)
1044     ((and (< score default-low)
1045           (or (eq mark gnus-dormant-mark)
1046               (eq mark gnus-ticked-mark)))
1047      . gnus-summary-low-ticked)
1048     ((or (eq mark gnus-dormant-mark)
1049          (eq mark gnus-ticked-mark))
1050      . gnus-summary-normal-ticked)
1051     ((and (> score default-high) (eq mark gnus-ancient-mark))
1052      . gnus-summary-high-ancient)
1053     ((and (< score default-low) (eq mark gnus-ancient-mark))
1054      . gnus-summary-low-ancient)
1055     ((eq mark gnus-ancient-mark)
1056      . gnus-summary-normal-ancient)
1057     ((and (> score default-high) (eq mark gnus-unread-mark))
1058      . gnus-summary-high-unread)
1059     ((and (< score default-low) (eq mark gnus-unread-mark))
1060      . gnus-summary-low-unread)
1061     ((eq mark gnus-unread-mark)
1062      . gnus-summary-normal-unread)
1063     ((> score default-high)
1064      . gnus-summary-high-read)
1065     ((< score default-low)
1066      . gnus-summary-low-read)
1067     (t
1068      . gnus-summary-normal-read))
1069   "*Controls the highlighting of summary buffer lines.
1070
1071 A list of (FORM . FACE) pairs.  When deciding how a particular
1072 summary line should be displayed, each form is evaluated.  The content
1073 of the face field after the first true form is used.  You can change
1074 how those summary lines are displayed, by editing the face field.
1075
1076 You can use the following variables in the FORM field.
1077
1078 score:        The article's score.
1079 default:      The default article score.
1080 default-high: The default score for high scored articles.
1081 default-low:  The default score for low scored articles.
1082 below:        The score below which articles are automatically marked as read.
1083 mark:         The article's mark.
1084 uncached:     Non-nil if the article is uncached."
1085   :group 'gnus-summary-visual
1086   :type '(repeat (cons (sexp :tag "Form" nil)
1087                        face)))
1088 (put 'gnus-summary-highlight 'risky-local-variable t)
1089
1090 (defcustom gnus-alter-header-function nil
1091   "Function called to allow alteration of article header structures.
1092 The function is called with one parameter, the article header vector,
1093 which it may alter in any way."
1094   :type '(choice (const :tag "None" nil)
1095                  function)
1096   :group 'gnus-summary)
1097
1098 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1099   "Function used to decode a string with encoded words.")
1100
1101 (defvar gnus-decode-encoded-address-function
1102   'mail-decode-encoded-address-string
1103   "Function used to decode addresses with encoded words.")
1104
1105 (defcustom gnus-extra-headers '(To Newsgroups)
1106   "*Extra headers to parse."
1107   :version "21.1"
1108   :group 'gnus-summary
1109   :type '(repeat symbol))
1110
1111 (defcustom gnus-ignored-from-addresses
1112   (and user-mail-address
1113        (not (string= user-mail-address ""))
1114        (regexp-quote user-mail-address))
1115   "*From headers that may be suppressed in favor of To headers.
1116 This can be a regexp or a list of regexps."
1117   :version "21.1"
1118   :group 'gnus-summary
1119   :type '(choice regexp
1120                  (repeat :tag "Regexp List" regexp)))
1121
1122 (defsubst gnus-ignored-from-addresses ()
1123   (gmm-regexp-concat gnus-ignored-from-addresses))
1124
1125 (defcustom gnus-summary-to-prefix "-> "
1126   "*String prefixed to the To field in the summary line when
1127 using `gnus-ignored-from-addresses'."
1128   :version "22.1"
1129   :group 'gnus-summary
1130   :type 'string)
1131
1132 (defcustom gnus-summary-newsgroup-prefix "=> "
1133   "*String prefixed to the Newsgroup field in the summary
1134 line when using `gnus-ignored-from-addresses'."
1135   :version "22.1"
1136   :group 'gnus-summary
1137   :type 'string)
1138
1139 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1140   "List of charsets that should be ignored.
1141 When these charsets are used in the \"charset\" parameter, the
1142 default charset will be used instead."
1143   :version "21.1"
1144   :type '(repeat symbol)
1145   :group 'gnus-charset)
1146
1147 (defcustom gnus-newsgroup-maximum-articles nil
1148   "The maximum number of articles a newsgroup.
1149 If this is a number, old articles in a newsgroup exceeding this number
1150 are silently ignored.  If it is nil, no article is ignored.  Note that
1151 setting this variable to a number might prevent you from reading very
1152 old articles."
1153   :group 'gnus-group-select
1154   :version "22.2"
1155   :type '(choice (const :tag "No limit" nil)
1156                  integer))
1157
1158 (gnus-define-group-parameter
1159  ignored-charsets
1160  :type list
1161  :function-document
1162  "Return the ignored charsets of GROUP."
1163  :variable gnus-group-ignored-charsets-alist
1164  :variable-default
1165  '(("alt\\.chinese\\.text" iso-8859-1))
1166  :variable-document
1167  "Alist of regexps (to match group names) and charsets that should be ignored.
1168 When these charsets are used in the \"charset\" parameter, the
1169 default charset will be used instead."
1170  :variable-group gnus-charset
1171  :variable-type '(repeat (cons (regexp :tag "Group")
1172                                (repeat symbol)))
1173  :parameter-type '(choice :tag "Ignored charsets"
1174                           :value nil
1175                           (repeat (symbol)))
1176  :parameter-document       "\
1177 List of charsets that should be ignored.
1178
1179 When these charsets are used in the \"charset\" parameter, the
1180 default charset will be used instead.")
1181
1182 (defcustom gnus-group-highlight-words-alist nil
1183   "Alist of group regexps and highlight regexps.
1184 This variable uses the same syntax as `gnus-emphasis-alist'."
1185   :version "21.1"
1186   :type '(repeat (cons (regexp :tag "Group")
1187                        (repeat (list (regexp :tag "Highlight regexp")
1188                                      (number :tag "Group for entire word" 0)
1189                                      (number :tag "Group for displayed part" 0)
1190                                      (symbol :tag "Face"
1191                                              gnus-emphasis-highlight-words)))))
1192   :group 'gnus-summary-visual)
1193
1194 (defcustom gnus-summary-show-article-charset-alist
1195   nil
1196   "Alist of number and charset.
1197 The article will be shown with the charset corresponding to the
1198 numbered argument.
1199 For example: ((1 . cn-gb-2312) (2 . big5))."
1200   :version "21.1"
1201   :type '(repeat (cons (number :tag "Argument" 1)
1202                        (symbol :tag "Charset")))
1203   :group 'gnus-charset)
1204
1205 (defcustom gnus-preserve-marks t
1206   "Whether marks are preserved when moving, copying and respooling messages."
1207   :version "21.1"
1208   :type 'boolean
1209   :group 'gnus-summary-marks)
1210
1211 (defcustom gnus-propagate-marks t
1212   "If non-nil, do not propagate marks to the backends."
1213   :version "23.1" ;; No Gnus
1214   :type 'boolean
1215   :group 'gnus-summary-marks)
1216
1217 (defcustom gnus-alter-articles-to-read-function nil
1218   "Function to be called to alter the list of articles to be selected."
1219   :type '(choice (const nil) function)
1220   :group 'gnus-summary)
1221
1222 (defcustom gnus-orphan-score nil
1223   "*All orphans get this score added.  Set in the score file."
1224   :group 'gnus-score-default
1225   :type '(choice (const nil)
1226                  integer))
1227
1228 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1229   "*A regexp to match MIME parts when saving multiple parts of a
1230 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1231 This regexp will be used by default when prompting the user for which
1232 type of files to save."
1233   :group 'gnus-summary
1234   :type 'regexp)
1235
1236 (defcustom gnus-read-all-available-headers nil
1237   "Whether Gnus should parse all headers made available to it.
1238 This is mostly relevant for slow back ends where the user may
1239 wish to widen the summary buffer to include all headers
1240 that were fetched.  Say, for nnultimate groups."
1241   :version "22.1"
1242   :group 'gnus-summary
1243   :type '(choice boolean regexp))
1244
1245 (defcustom gnus-summary-pipe-output-default-command nil
1246   "Command (and optional arguments) used to pipe article to subprocess.
1247 This will be used as the default command if it is non-nil.  The value
1248 will be updated if you modify it when executing the command
1249 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1250   :version "23.1" ;; No Gnus
1251   :group 'gnus-summary
1252   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1253
1254 (defcustom gnus-summary-muttprint-program "muttprint"
1255   "Command (and optional arguments) used to run Muttprint.
1256 The value will be updated if you modify it when executing the command
1257 `gnus-summary-muttprint'."
1258   :version "22.1"
1259   :group 'gnus-summary
1260   :type 'string)
1261
1262 (defcustom gnus-article-loose-mime t
1263   "If non-nil, don't require MIME-Version header.
1264 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1265 supply the MIME-Version header or deliberately strip it from the mail.
1266 If non-nil (the default), Gnus will treat some articles as MIME
1267 even if the MIME-Version header is missing."
1268   :version "22.1"
1269   :type 'boolean
1270   :group 'gnus-article-mime)
1271
1272 (defcustom gnus-article-emulate-mime t
1273   "If non-nil, use MIME emulation for uuencode and the like.
1274 This means that Gnus will search message bodies for text that look
1275 like uuencoded bits, yEncoded bits, and so on, and present that using
1276 the normal Gnus MIME machinery."
1277   :version "22.1"
1278   :type 'boolean
1279   :group 'gnus-article-mime)
1280
1281 ;;; Internal variables
1282
1283 (defvar gnus-summary-display-cache nil)
1284 (defvar gnus-article-mime-handles nil)
1285 (defvar gnus-article-decoded-p nil)
1286 (defvar gnus-article-charset nil)
1287 (defvar gnus-article-ignored-charsets nil)
1288 (defvar gnus-scores-exclude-files nil)
1289 (defvar gnus-page-broken nil)
1290
1291 (defvar gnus-original-article nil)
1292 (defvar gnus-article-internal-prepare-hook nil)
1293 (defvar gnus-newsgroup-process-stack nil)
1294
1295 (defvar gnus-thread-indent-array nil)
1296 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1297 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1298   "Function called to sort the articles within a thread after it has been gathered together.")
1299
1300 (defvar gnus-summary-save-parts-type-history nil)
1301 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1302
1303 ;; Avoid highlighting in kill files.
1304 (defvar gnus-summary-inhibit-highlight nil)
1305 (defvar gnus-newsgroup-selected-overlay nil)
1306 (defvar gnus-inhibit-limiting nil)
1307 (defvar gnus-newsgroup-adaptive-score-file nil)
1308 (defvar gnus-current-score-file nil)
1309 (defvar gnus-current-move-group nil)
1310 (defvar gnus-current-copy-group nil)
1311 (defvar gnus-current-crosspost-group nil)
1312 (defvar gnus-newsgroup-display nil)
1313
1314 (defvar gnus-newsgroup-dependencies nil)
1315 (defvar gnus-newsgroup-adaptive nil)
1316 (defvar gnus-summary-display-article-function nil)
1317 (defvar gnus-summary-highlight-line-function nil
1318   "Function called after highlighting a summary line.")
1319
1320 (defvar gnus-summary-line-format-alist
1321   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1322     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1323     (?s gnus-tmp-subject-or-nil ?s)
1324     (?n gnus-tmp-name ?s)
1325     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1326         ?s)
1327     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1328             gnus-tmp-from) ?s)
1329     (?F gnus-tmp-from ?s)
1330     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1331     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1332     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1333     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1334     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1335     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1336     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1337     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1338     (?L gnus-tmp-lines ?s)
1339     (?O gnus-tmp-downloaded ?c)
1340     (?I gnus-tmp-indentation ?s)
1341     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1342     (?R gnus-tmp-replied ?c)
1343     (?\[ gnus-tmp-opening-bracket ?c)
1344     (?\] gnus-tmp-closing-bracket ?c)
1345     (?\> (make-string gnus-tmp-level ? ) ?s)
1346     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1347     (?i gnus-tmp-score ?d)
1348     (?z gnus-tmp-score-char ?c)
1349     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1350     (?U gnus-tmp-unread ?c)
1351     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1352         ?s)
1353     (?t (gnus-summary-number-of-articles-in-thread
1354          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1355         ?d)
1356     (?e (gnus-summary-number-of-articles-in-thread
1357          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1358         ?c)
1359     (?u gnus-tmp-user-defined ?s)
1360     (?P (gnus-pick-line-number) ?d)
1361     (?B gnus-tmp-thread-tree-header-string ?s)
1362     (user-date (gnus-user-date
1363                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1364   "An alist of format specifications that can appear in summary lines.
1365 These are paired with what variables they correspond with, along with
1366 the type of the variable (string, integer, character, etc).")
1367
1368 (defvar gnus-summary-dummy-line-format-alist
1369   `((?S gnus-tmp-subject ?s)
1370     (?N gnus-tmp-number ?d)
1371     (?u gnus-tmp-user-defined ?s)))
1372
1373 (defvar gnus-summary-mode-line-format-alist
1374   `((?G gnus-tmp-group-name ?s)
1375     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1376     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1377     (?A gnus-tmp-article-number ?d)
1378     (?Z gnus-tmp-unread-and-unselected ?s)
1379     (?V gnus-version ?s)
1380     (?U gnus-tmp-unread-and-unticked ?d)
1381     (?S gnus-tmp-subject ?s)
1382     (?e gnus-tmp-unselected ?d)
1383     (?u gnus-tmp-user-defined ?s)
1384     (?d (length gnus-newsgroup-dormant) ?d)
1385     (?t (length gnus-newsgroup-marked) ?d)
1386     (?h (length gnus-newsgroup-spam-marked) ?d)
1387     (?r (length gnus-newsgroup-reads) ?d)
1388     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1389     (?E gnus-newsgroup-expunged-tally ?d)
1390     (?s (gnus-current-score-file-nondirectory) ?s)))
1391
1392 ;; This is here rather than in gnus-art for compilation reasons.
1393 (defvar gnus-article-mode-line-format-alist
1394   (nconc '((?w (gnus-article-wash-status) ?s)
1395            (?m (gnus-article-mime-part-status) ?s))
1396          gnus-summary-mode-line-format-alist))
1397
1398 (defvar gnus-last-search-regexp nil
1399   "Default regexp for article search command.")
1400
1401 (defvar gnus-last-shell-command nil
1402   "Default shell command on article.")
1403
1404 (defvar gnus-newsgroup-agentized nil
1405   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1406 (defvar gnus-newsgroup-begin nil)
1407 (defvar gnus-newsgroup-end nil)
1408 (defvar gnus-newsgroup-last-rmail nil)
1409 (defvar gnus-newsgroup-last-mail nil)
1410 (defvar gnus-newsgroup-last-folder nil)
1411 (defvar gnus-newsgroup-last-file nil)
1412 (defvar gnus-newsgroup-last-directory nil)
1413 (defvar gnus-newsgroup-auto-expire nil)
1414 (defvar gnus-newsgroup-active nil)
1415
1416 (defvar gnus-newsgroup-data nil)
1417 (defvar gnus-newsgroup-data-reverse nil)
1418 (defvar gnus-newsgroup-limit nil)
1419 (defvar gnus-newsgroup-limits nil)
1420 (defvar gnus-summary-use-undownloaded-faces nil)
1421
1422 (defvar gnus-newsgroup-unreads nil
1423   "Sorted list of unread articles in the current newsgroup.")
1424
1425 (defvar gnus-newsgroup-unselected nil
1426   "Sorted list of unselected unread articles in the current newsgroup.")
1427
1428 (defvar gnus-newsgroup-reads nil
1429   "Alist of read articles and article marks in the current newsgroup.")
1430
1431 (defvar gnus-newsgroup-expunged-tally nil)
1432
1433 (defvar gnus-newsgroup-marked nil
1434   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1435
1436 (defvar gnus-newsgroup-spam-marked nil
1437   "List of ranges of articles that have been marked as spam.")
1438
1439 (defvar gnus-newsgroup-killed nil
1440   "List of ranges of articles that have been through the scoring process.")
1441
1442 (defvar gnus-newsgroup-cached nil
1443   "Sorted list of articles that come from the article cache.")
1444
1445 (defvar gnus-newsgroup-saved nil
1446   "List of articles that have been saved.")
1447
1448 (defvar gnus-newsgroup-kill-headers nil)
1449
1450 (defvar gnus-newsgroup-replied nil
1451   "List of articles that have been replied to in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-forwarded nil
1454   "List of articles that have been forwarded in the current newsgroup.")
1455
1456 (defvar gnus-newsgroup-recent nil
1457   "List of articles that have are recent in the current newsgroup.")
1458
1459 (defvar gnus-newsgroup-expirable nil
1460   "Sorted list of articles in the current newsgroup that can be expired.")
1461
1462 (defvar gnus-newsgroup-processable nil
1463   "List of articles in the current newsgroup that can be processed.")
1464
1465 (defvar gnus-newsgroup-downloadable nil
1466   "Sorted list of articles in the current newsgroup that can be processed.")
1467
1468 (defvar gnus-newsgroup-unfetched nil
1469   "Sorted list of articles in the current newsgroup whose headers have
1470 not been fetched into the agent.
1471
1472 This list will always be a subset of gnus-newsgroup-undownloaded.")
1473
1474 (defvar gnus-newsgroup-undownloaded nil
1475   "List of articles in the current newsgroup that haven't been downloaded.")
1476
1477 (defvar gnus-newsgroup-unsendable nil
1478   "List of articles in the current newsgroup that won't be sent.")
1479
1480 (defvar gnus-newsgroup-bookmarks nil
1481   "List of articles in the current newsgroup that have bookmarks.")
1482
1483 (defvar gnus-newsgroup-dormant nil
1484   "Sorted list of dormant articles in the current newsgroup.")
1485
1486 (defvar gnus-newsgroup-unseen nil
1487   "List of unseen articles in the current newsgroup.")
1488
1489 (defvar gnus-newsgroup-seen nil
1490   "Range of seen articles in the current newsgroup.")
1491
1492 (defvar gnus-newsgroup-articles nil
1493   "List of articles in the current newsgroup.")
1494
1495 (defvar gnus-newsgroup-scored nil
1496   "List of scored articles in the current newsgroup.")
1497
1498 (defvar gnus-newsgroup-headers nil
1499   "List of article headers in the current newsgroup.")
1500
1501 (defvar gnus-newsgroup-threads nil)
1502
1503 (defvar gnus-newsgroup-prepared nil
1504   "Whether the current group has been prepared properly.")
1505
1506 (defvar gnus-newsgroup-ancient nil
1507   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-sparse nil)
1510
1511 (defvar gnus-current-article nil)
1512 (defvar gnus-article-current nil)
1513 (defvar gnus-current-headers nil)
1514 (defvar gnus-have-all-headers nil)
1515 (defvar gnus-last-article nil)
1516 (defvar gnus-newsgroup-history nil)
1517 (defvar gnus-newsgroup-charset nil)
1518 (defvar gnus-newsgroup-ephemeral-charset nil)
1519 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1520
1521 (defvar gnus-article-before-search nil)
1522
1523 (defvar gnus-summary-local-variables
1524   '(gnus-newsgroup-name
1525     gnus-newsgroup-begin gnus-newsgroup-end
1526     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1527     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1528     gnus-newsgroup-last-directory
1529     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1530     gnus-newsgroup-unselected gnus-newsgroup-marked
1531     gnus-newsgroup-spam-marked
1532     gnus-newsgroup-reads gnus-newsgroup-saved
1533     gnus-newsgroup-replied gnus-newsgroup-forwarded
1534     gnus-newsgroup-recent
1535     gnus-newsgroup-expirable
1536     gnus-newsgroup-processable gnus-newsgroup-killed
1537     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1538     gnus-newsgroup-unfetched
1539     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1540     gnus-newsgroup-seen gnus-newsgroup-articles
1541     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1542     gnus-newsgroup-headers gnus-newsgroup-threads
1543     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1544     gnus-current-article gnus-current-headers gnus-have-all-headers
1545     gnus-last-article gnus-article-internal-prepare-hook
1546     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1547     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1548     gnus-thread-expunge-below
1549     gnus-score-alist gnus-current-score-file
1550     (gnus-summary-expunge-below . global)
1551     (gnus-summary-mark-below . global)
1552     (gnus-orphan-score . global)
1553     gnus-newsgroup-active gnus-scores-exclude-files
1554     gnus-newsgroup-history gnus-newsgroup-ancient
1555     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1556     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1557     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1558     (gnus-newsgroup-expunged-tally . 0)
1559     gnus-cache-removable-articles gnus-newsgroup-cached
1560     gnus-newsgroup-data gnus-newsgroup-data-reverse
1561     gnus-newsgroup-limit gnus-newsgroup-limits
1562     gnus-newsgroup-charset gnus-newsgroup-display
1563     gnus-summary-use-undownloaded-faces)
1564   "Variables that are buffer-local to the summary buffers.")
1565
1566 (defvar gnus-newsgroup-variables nil
1567   "A list of variables that have separate values in different newsgroups.
1568 A list of newsgroup (summary buffer) local variables, or cons of
1569 variables and their default expressions to be evalled (when the default
1570 values are not nil), that should be made global while the summary buffer
1571 is active.
1572
1573 Note: The default expressions will be evaluated (using function `eval')
1574 before assignment to the local variable rather than just assigned to it.
1575 If the default expression is the symbol `global', that symbol will not
1576 be evaluated but the global value of the local variable will be used
1577 instead.
1578
1579 These variables can be used to set variables in the group parameters
1580 while still allowing them to affect operations done in other buffers.
1581 For example:
1582
1583 \(setq gnus-newsgroup-variables
1584      '(message-use-followup-to
1585        (gnus-visible-headers .
1586          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1587 ")
1588
1589 (eval-when-compile
1590   ;; Bind features so that require will believe that gnus-sum has
1591   ;; already been loaded (avoids infinite recursion)
1592   (let ((features (cons 'gnus-sum features)))
1593     (require 'gnus-art)))
1594
1595 ;; MIME stuff.
1596
1597 (defvar gnus-decode-encoded-word-methods
1598   '(mail-decode-encoded-word-string)
1599   "List of methods used to decode encoded words.
1600
1601 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1602 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1603 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1604 whose names match REGEXP.
1605
1606 For example:
1607 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1608  mail-decode-encoded-word-string
1609  (\"chinese\" . rfc1843-decode-string))")
1610
1611 (defvar gnus-decode-encoded-word-methods-cache nil)
1612
1613 (defun gnus-multi-decode-encoded-word-string (string)
1614   "Apply the functions from `gnus-encoded-word-methods' that match."
1615   (unless (and gnus-decode-encoded-word-methods-cache
1616                (eq gnus-newsgroup-name
1617                    (car gnus-decode-encoded-word-methods-cache)))
1618     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1619     (dolist (method gnus-decode-encoded-word-methods)
1620       (if (symbolp method)
1621           (nconc gnus-decode-encoded-word-methods-cache (list method))
1622         (if (and gnus-newsgroup-name
1623                  (string-match (car method) gnus-newsgroup-name))
1624             (nconc gnus-decode-encoded-word-methods-cache
1625                    (list (cdr method)))))))
1626   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1627     (setq string (funcall method string))))
1628
1629 ;; Subject simplification.
1630
1631 (defun gnus-simplify-whitespace (str)
1632   "Remove excessive whitespace from STR."
1633   ;; Multiple spaces.
1634   (while (string-match "[ \t][ \t]+" str)
1635     (setq str (concat (substring str 0 (match-beginning 0))
1636                         " "
1637                         (substring str (match-end 0)))))
1638   ;; Leading spaces.
1639   (when (string-match "^[ \t]+" str)
1640     (setq str (substring str (match-end 0))))
1641   ;; Trailing spaces.
1642   (when (string-match "[ \t]+$" str)
1643     (setq str (substring str 0 (match-beginning 0))))
1644   str)
1645
1646 (defun gnus-simplify-all-whitespace (str)
1647   "Remove all whitespace from STR."
1648   (while (string-match "[ \t\n]+" str)
1649     (setq str (replace-match "" nil nil str)))
1650   str)
1651
1652 (defsubst gnus-simplify-subject-re (subject)
1653   "Remove \"Re:\" from subject lines."
1654   (if (string-match message-subject-re-regexp subject)
1655       (substring subject (match-end 0))
1656     subject))
1657
1658 (defun gnus-simplify-subject (subject &optional re-only)
1659   "Remove `Re:' and words in parentheses.
1660 If RE-ONLY is non-nil, strip leading `Re:'s only."
1661   (let ((case-fold-search t))           ;Ignore case.
1662     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1663     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1664       (setq subject (substring subject (match-end 0))))
1665     ;; Remove uninteresting prefixes.
1666     (when (and (not re-only)
1667                gnus-simplify-ignored-prefixes
1668                (string-match gnus-simplify-ignored-prefixes subject))
1669       (setq subject (substring subject (match-end 0))))
1670     ;; Remove words in parentheses from end.
1671     (unless re-only
1672       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1673         (setq subject (substring subject 0 (match-beginning 0)))))
1674     ;; Return subject string.
1675     subject))
1676
1677 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1678 ;; all whitespace.
1679 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1680   (goto-char (point-min))
1681   (while (re-search-forward regexp nil t)
1682     (replace-match (or newtext ""))))
1683
1684 (defun gnus-simplify-buffer-fuzzy ()
1685   "Simplify string in the buffer fuzzily.
1686 The string in the accessible portion of the current buffer is simplified.
1687 It is assumed to be a single-line subject.
1688 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1689 matter is removed.  Additional things can be deleted by setting
1690 `gnus-simplify-subject-fuzzy-regexp'."
1691   (let ((case-fold-search t)
1692         (modified-tick))
1693     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1694
1695     (while (not (eq modified-tick (buffer-modified-tick)))
1696       (setq modified-tick (buffer-modified-tick))
1697       (cond
1698        ((listp gnus-simplify-subject-fuzzy-regexp)
1699         (mapc 'gnus-simplify-buffer-fuzzy-step
1700               gnus-simplify-subject-fuzzy-regexp))
1701        (gnus-simplify-subject-fuzzy-regexp
1702         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1703       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1704       (gnus-simplify-buffer-fuzzy-step
1705        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1706       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1707
1708     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1709     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1710     (gnus-simplify-buffer-fuzzy-step " $")
1711     (gnus-simplify-buffer-fuzzy-step "^ +")))
1712
1713 (defun gnus-simplify-subject-fuzzy (subject)
1714   "Simplify a subject string fuzzily.
1715 See `gnus-simplify-buffer-fuzzy' for details."
1716   (save-excursion
1717     (gnus-set-work-buffer)
1718     (let ((case-fold-search t))
1719       ;; Remove uninteresting prefixes.
1720       (when (and gnus-simplify-ignored-prefixes
1721                  (string-match gnus-simplify-ignored-prefixes subject))
1722         (setq subject (substring subject (match-end 0))))
1723       (insert subject)
1724       (inline (gnus-simplify-buffer-fuzzy))
1725       (buffer-string))))
1726
1727 (defsubst gnus-simplify-subject-fully (subject)
1728   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1729   (cond
1730    (gnus-simplify-subject-functions
1731     (gnus-map-function gnus-simplify-subject-functions subject))
1732    ((null gnus-summary-gather-subject-limit)
1733     (gnus-simplify-subject-re subject))
1734    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1735     (gnus-simplify-subject-fuzzy subject))
1736    ((numberp gnus-summary-gather-subject-limit)
1737     (truncate-string-to-width (gnus-simplify-subject-re subject)
1738                               gnus-summary-gather-subject-limit))
1739    (t
1740     subject)))
1741
1742 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1743   "Check whether two subjects are equal.
1744 If optional argument SIMPLE-FIRST is t, first argument is already
1745 simplified."
1746   (cond
1747    ((null simple-first)
1748     (equal (gnus-simplify-subject-fully s1)
1749            (gnus-simplify-subject-fully s2)))
1750    (t
1751     (equal s1
1752            (gnus-simplify-subject-fully s2)))))
1753
1754 (defun gnus-summary-bubble-group ()
1755   "Increase the score of the current group.
1756 This is a handy function to add to `gnus-summary-exit-hook' to
1757 increase the score of each group you read."
1758   (gnus-group-add-score gnus-newsgroup-name))
1759
1760 \f
1761 ;;;
1762 ;;; Gnus summary mode
1763 ;;;
1764
1765 (put 'gnus-summary-mode 'mode-class 'special)
1766
1767 (defvar gnus-article-commands-menu)
1768
1769 ;; Non-orthogonal keys
1770
1771 (gnus-define-keys gnus-summary-mode-map
1772   " " gnus-summary-next-page
1773   "\177" gnus-summary-prev-page
1774   [delete] gnus-summary-prev-page
1775   [backspace] gnus-summary-prev-page
1776   "\r" gnus-summary-scroll-up
1777   "\M-\r" gnus-summary-scroll-down
1778   "n" gnus-summary-next-unread-article
1779   "p" gnus-summary-prev-unread-article
1780   "N" gnus-summary-next-article
1781   "P" gnus-summary-prev-article
1782   "\M-\C-n" gnus-summary-next-same-subject
1783   "\M-\C-p" gnus-summary-prev-same-subject
1784   "\M-n" gnus-summary-next-unread-subject
1785   "\M-p" gnus-summary-prev-unread-subject
1786   "." gnus-summary-first-unread-article
1787   "," gnus-summary-best-unread-article
1788   "\M-s" gnus-summary-search-article-forward
1789   "\M-r" gnus-summary-search-article-backward
1790   "\M-S" gnus-summary-repeat-search-article-forward
1791   "\M-R" gnus-summary-repeat-search-article-backward
1792   "<" gnus-summary-beginning-of-article
1793   ">" gnus-summary-end-of-article
1794   "j" gnus-summary-goto-article
1795   "^" gnus-summary-refer-parent-article
1796   "\M-^" gnus-summary-refer-article
1797   "u" gnus-summary-tick-article-forward
1798   "!" gnus-summary-tick-article-forward
1799   "U" gnus-summary-tick-article-backward
1800   "d" gnus-summary-mark-as-read-forward
1801   "D" gnus-summary-mark-as-read-backward
1802   "E" gnus-summary-mark-as-expirable
1803   "\M-u" gnus-summary-clear-mark-forward
1804   "\M-U" gnus-summary-clear-mark-backward
1805   "k" gnus-summary-kill-same-subject-and-select
1806   "\C-k" gnus-summary-kill-same-subject
1807   "\M-\C-k" gnus-summary-kill-thread
1808   "\M-\C-l" gnus-summary-lower-thread
1809   "e" gnus-summary-edit-article
1810   "#" gnus-summary-mark-as-processable
1811   "\M-#" gnus-summary-unmark-as-processable
1812   "\M-\C-t" gnus-summary-toggle-threads
1813   "\M-\C-s" gnus-summary-show-thread
1814   "\M-\C-h" gnus-summary-hide-thread
1815   "\M-\C-f" gnus-summary-next-thread
1816   "\M-\C-b" gnus-summary-prev-thread
1817   [(meta down)] gnus-summary-next-thread
1818   [(meta up)] gnus-summary-prev-thread
1819   "\M-\C-u" gnus-summary-up-thread
1820   "\M-\C-d" gnus-summary-down-thread
1821   "&" gnus-summary-execute-command
1822   "c" gnus-summary-catchup-and-exit
1823   "\C-w" gnus-summary-mark-region-as-read
1824   "\C-t" gnus-summary-toggle-truncation
1825   "?" gnus-summary-mark-as-dormant
1826   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1827   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1828   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1829   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1830   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1831   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1832   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1833   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1834   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1835   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1836   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1837   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1838   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1839   "=" gnus-summary-expand-window
1840   "\C-x\C-s" gnus-summary-reselect-current-group
1841   "\M-g" gnus-summary-rescan-group
1842   "w" gnus-summary-stop-page-breaking
1843   "\C-c\C-r" gnus-summary-caesar-message
1844   "f" gnus-summary-followup
1845   "F" gnus-summary-followup-with-original
1846   "C" gnus-summary-cancel-article
1847   "r" gnus-summary-reply
1848   "R" gnus-summary-reply-with-original
1849   "\C-c\C-f" gnus-summary-mail-forward
1850   "o" gnus-summary-save-article
1851   "\C-o" gnus-summary-save-article-mail
1852   "|" gnus-summary-pipe-output
1853   "\M-k" gnus-summary-edit-local-kill
1854   "\M-K" gnus-summary-edit-global-kill
1855   ;; "V" gnus-version
1856   "\C-c\C-d" gnus-summary-describe-group
1857   "q" gnus-summary-exit
1858   "Q" gnus-summary-exit-no-update
1859   "\C-c\C-i" gnus-info-find-node
1860   gnus-mouse-2 gnus-mouse-pick-article
1861   [follow-link] mouse-face
1862   "m" gnus-summary-mail-other-window
1863   "a" gnus-summary-post-news
1864   "i" gnus-summary-news-other-window
1865   "x" gnus-summary-limit-to-unread
1866   "s" gnus-summary-isearch-article
1867   "t" gnus-summary-toggle-header
1868   "g" gnus-summary-show-article
1869   "l" gnus-summary-goto-last-article
1870   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1871   "\C-d" gnus-summary-enter-digest-group
1872   "\M-\C-d" gnus-summary-read-document
1873   "\M-\C-e" gnus-summary-edit-parameters
1874   "\M-\C-a" gnus-summary-customize-parameters
1875   "\C-c\C-b" gnus-bug
1876   "*" gnus-cache-enter-article
1877   "\M-*" gnus-cache-remove-article
1878   "\M-&" gnus-summary-universal-argument
1879   "\C-l" gnus-recenter
1880   "I" gnus-summary-increase-score
1881   "L" gnus-summary-lower-score
1882   "\M-i" gnus-symbolic-argument
1883   "h" gnus-summary-select-article-buffer
1884
1885   "b" gnus-article-view-part
1886   "\M-t" gnus-summary-toggle-display-buttonized
1887
1888   "V" gnus-summary-score-map
1889   "X" gnus-uu-extract-map
1890   "S" gnus-summary-send-map)
1891
1892 ;; Sort of orthogonal keymap
1893 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1894   "t" gnus-summary-tick-article-forward
1895   "!" gnus-summary-tick-article-forward
1896   "d" gnus-summary-mark-as-read-forward
1897   "r" gnus-summary-mark-as-read-forward
1898   "c" gnus-summary-clear-mark-forward
1899   " " gnus-summary-clear-mark-forward
1900   "e" gnus-summary-mark-as-expirable
1901   "x" gnus-summary-mark-as-expirable
1902   "?" gnus-summary-mark-as-dormant
1903   "b" gnus-summary-set-bookmark
1904   "B" gnus-summary-remove-bookmark
1905   "#" gnus-summary-mark-as-processable
1906   "\M-#" gnus-summary-unmark-as-processable
1907   "S" gnus-summary-limit-include-expunged
1908   "C" gnus-summary-catchup
1909   "H" gnus-summary-catchup-to-here
1910   "h" gnus-summary-catchup-from-here
1911   "\C-c" gnus-summary-catchup-all
1912   "k" gnus-summary-kill-same-subject-and-select
1913   "K" gnus-summary-kill-same-subject
1914   "P" gnus-uu-mark-map)
1915
1916 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1917   "c" gnus-summary-clear-above
1918   "u" gnus-summary-tick-above
1919   "m" gnus-summary-mark-above
1920   "k" gnus-summary-kill-below)
1921
1922 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1923   "/" gnus-summary-limit-to-subject
1924   "n" gnus-summary-limit-to-articles
1925   "b" gnus-summary-limit-to-bodies
1926   "h" gnus-summary-limit-to-headers
1927   "w" gnus-summary-pop-limit
1928   "s" gnus-summary-limit-to-subject
1929   "a" gnus-summary-limit-to-author
1930   "u" gnus-summary-limit-to-unread
1931   "m" gnus-summary-limit-to-marks
1932   "M" gnus-summary-limit-exclude-marks
1933   "v" gnus-summary-limit-to-score
1934   "*" gnus-summary-limit-include-cached
1935   "D" gnus-summary-limit-include-dormant
1936   "T" gnus-summary-limit-include-thread
1937   "d" gnus-summary-limit-exclude-dormant
1938   "t" gnus-summary-limit-to-age
1939   "." gnus-summary-limit-to-unseen
1940   "x" gnus-summary-limit-to-extra
1941   "p" gnus-summary-limit-to-display-predicate
1942   "E" gnus-summary-limit-include-expunged
1943   "c" gnus-summary-limit-exclude-childless-dormant
1944   "C" gnus-summary-limit-mark-excluded-as-read
1945   "o" gnus-summary-insert-old-articles
1946   "N" gnus-summary-insert-new-articles
1947   "S" gnus-summary-limit-to-singletons
1948   "r" gnus-summary-limit-to-replied
1949   "R" gnus-summary-limit-to-recipient
1950   "A" gnus-summary-limit-to-address)
1951
1952 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1953   "n" gnus-summary-next-unread-article
1954   "p" gnus-summary-prev-unread-article
1955   "N" gnus-summary-next-article
1956   "P" gnus-summary-prev-article
1957   "\C-n" gnus-summary-next-same-subject
1958   "\C-p" gnus-summary-prev-same-subject
1959   "\M-n" gnus-summary-next-unread-subject
1960   "\M-p" gnus-summary-prev-unread-subject
1961   "f" gnus-summary-first-unread-article
1962   "b" gnus-summary-best-unread-article
1963   "j" gnus-summary-goto-article
1964   "g" gnus-summary-goto-subject
1965   "l" gnus-summary-goto-last-article
1966   "o" gnus-summary-pop-article)
1967
1968 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1969   "k" gnus-summary-kill-thread
1970   "E" gnus-summary-expire-thread
1971   "l" gnus-summary-lower-thread
1972   "i" gnus-summary-raise-thread
1973   "T" gnus-summary-toggle-threads
1974   "t" gnus-summary-rethread-current
1975   "^" gnus-summary-reparent-thread
1976   "\M-^" gnus-summary-reparent-children
1977   "s" gnus-summary-show-thread
1978   "S" gnus-summary-show-all-threads
1979   "h" gnus-summary-hide-thread
1980   "H" gnus-summary-hide-all-threads
1981   "n" gnus-summary-next-thread
1982   "p" gnus-summary-prev-thread
1983   "u" gnus-summary-up-thread
1984   "o" gnus-summary-top-thread
1985   "d" gnus-summary-down-thread
1986   "#" gnus-uu-mark-thread
1987   "\M-#" gnus-uu-unmark-thread)
1988
1989 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1990   "g" gnus-summary-prepare
1991   "c" gnus-summary-insert-cached-articles
1992   "d" gnus-summary-insert-dormant-articles
1993   "t" gnus-summary-insert-ticked-articles)
1994
1995 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1996   "c" gnus-summary-catchup-and-exit
1997   "C" gnus-summary-catchup-all-and-exit
1998   "E" gnus-summary-exit-no-update
1999   "Q" gnus-summary-exit
2000   "Z" gnus-summary-exit
2001   "n" gnus-summary-catchup-and-goto-next-group
2002   "p" gnus-summary-catchup-and-goto-prev-group
2003   "R" gnus-summary-reselect-current-group
2004   "G" gnus-summary-rescan-group
2005   "N" gnus-summary-next-group
2006   "s" gnus-summary-save-newsrc
2007   "P" gnus-summary-prev-group)
2008
2009 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2010   " " gnus-summary-next-page
2011   "n" gnus-summary-next-page
2012   "\177" gnus-summary-prev-page
2013   [delete] gnus-summary-prev-page
2014   "p" gnus-summary-prev-page
2015   "\r" gnus-summary-scroll-up
2016   "\M-\r" gnus-summary-scroll-down
2017   "<" gnus-summary-beginning-of-article
2018   ">" gnus-summary-end-of-article
2019   "b" gnus-summary-beginning-of-article
2020   "e" gnus-summary-end-of-article
2021   "^" gnus-summary-refer-parent-article
2022   "r" gnus-summary-refer-parent-article
2023   "D" gnus-summary-enter-digest-group
2024   "R" gnus-summary-refer-references
2025   "T" gnus-summary-refer-thread
2026   "g" gnus-summary-show-article
2027   "s" gnus-summary-isearch-article
2028   "P" gnus-summary-print-article
2029   "S" gnus-sticky-article
2030   "M" gnus-mailing-list-insinuate
2031   "t" gnus-article-babel)
2032
2033 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2034   "b" gnus-article-add-buttons
2035   "B" gnus-article-add-buttons-to-head
2036   "o" gnus-article-treat-overstrike
2037   "e" gnus-article-emphasize
2038   "w" gnus-article-fill-cited-article
2039   "Q" gnus-article-fill-long-lines
2040   "L" gnus-article-toggle-truncate-lines
2041   "C" gnus-article-capitalize-sentences
2042   "c" gnus-article-remove-cr
2043   "q" gnus-article-de-quoted-unreadable
2044   "6" gnus-article-de-base64-unreadable
2045   "Z" gnus-article-decode-HZ
2046   "A" gnus-article-treat-ansi-sequences
2047   "h" gnus-article-wash-html
2048   "u" gnus-article-unsplit-urls
2049   "s" gnus-summary-force-verify-and-decrypt
2050   "f" gnus-article-display-x-face
2051   "l" gnus-summary-stop-page-breaking
2052   "r" gnus-summary-caesar-message
2053   "m" gnus-summary-morse-message
2054   "t" gnus-summary-toggle-header
2055   "g" gnus-treat-smiley
2056   "v" gnus-summary-verbose-headers
2057   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2058   "p" gnus-article-verify-x-pgp-sig
2059   "d" gnus-article-treat-dumbquotes
2060   "i" gnus-summary-idna-message)
2061
2062 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2063   ;; mnemonic: deuglif*Y*
2064   "u" gnus-article-outlook-unwrap-lines
2065   "a" gnus-article-outlook-repair-attribution
2066   "c" gnus-article-outlook-rearrange-citation
2067   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2068
2069 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2070   "a" gnus-article-hide
2071   "h" gnus-article-hide-headers
2072   "b" gnus-article-hide-boring-headers
2073   "s" gnus-article-hide-signature
2074   "c" gnus-article-hide-citation
2075   "C" gnus-article-hide-citation-in-followups
2076   "l" gnus-article-hide-list-identifiers
2077   "B" gnus-article-strip-banner
2078   "P" gnus-article-hide-pem
2079   "\C-c" gnus-article-hide-citation-maybe)
2080
2081 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2082   "a" gnus-article-highlight
2083   "h" gnus-article-highlight-headers
2084   "c" gnus-article-highlight-citation
2085   "s" gnus-article-highlight-signature)
2086
2087 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2088   "f" gnus-article-treat-fold-headers
2089   "u" gnus-article-treat-unfold-headers
2090   "n" gnus-article-treat-fold-newsgroups)
2091
2092 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2093   "x" gnus-article-display-x-face
2094   "d" gnus-article-display-face
2095   "s" gnus-treat-smiley
2096   "D" gnus-article-remove-images
2097   "f" gnus-treat-from-picon
2098   "m" gnus-treat-mail-picon
2099   "n" gnus-treat-newsgroups-picon)
2100
2101 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2102   "w" gnus-article-decode-mime-words
2103   "c" gnus-article-decode-charset
2104   "v" gnus-mime-view-all-parts
2105   "b" gnus-article-view-part)
2106
2107 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2108   "z" gnus-article-date-ut
2109   "u" gnus-article-date-ut
2110   "l" gnus-article-date-local
2111   "p" gnus-article-date-english
2112   "e" gnus-article-date-lapsed
2113   "o" gnus-article-date-original
2114   "i" gnus-article-date-iso8601
2115   "s" gnus-article-date-user)
2116
2117 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2118   "t" gnus-article-remove-trailing-blank-lines
2119   "l" gnus-article-strip-leading-blank-lines
2120   "m" gnus-article-strip-multiple-blank-lines
2121   "a" gnus-article-strip-blank-lines
2122   "A" gnus-article-strip-all-blank-lines
2123   "s" gnus-article-strip-leading-space
2124   "e" gnus-article-strip-trailing-space
2125   "w" gnus-article-remove-leading-whitespace)
2126
2127 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2128   "v" gnus-version
2129   "f" gnus-summary-fetch-faq
2130   "d" gnus-summary-describe-group
2131   "h" gnus-summary-describe-briefly
2132   "i" gnus-info-find-node
2133   "c" gnus-group-fetch-charter
2134   "C" gnus-group-fetch-control)
2135
2136 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2137   "e" gnus-summary-expire-articles
2138   "\M-\C-e" gnus-summary-expire-articles-now
2139   "\177" gnus-summary-delete-article
2140   [delete] gnus-summary-delete-article
2141   [backspace] gnus-summary-delete-article
2142   "m" gnus-summary-move-article
2143   "r" gnus-summary-respool-article
2144   "w" gnus-summary-edit-article
2145   "c" gnus-summary-copy-article
2146   "B" gnus-summary-crosspost-article
2147   "q" gnus-summary-respool-query
2148   "t" gnus-summary-respool-trace
2149   "i" gnus-summary-import-article
2150   "I" gnus-summary-create-article
2151   "p" gnus-summary-article-posted-p)
2152
2153 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2154   "o" gnus-summary-save-article
2155   "m" gnus-summary-save-article-mail
2156   "F" gnus-summary-write-article-file
2157   "r" gnus-summary-save-article-rmail
2158   "f" gnus-summary-save-article-file
2159   "b" gnus-summary-save-article-body-file
2160   "B" gnus-summary-write-article-body-file
2161   "h" gnus-summary-save-article-folder
2162   "v" gnus-summary-save-article-vm
2163   "p" gnus-summary-pipe-output
2164   "P" gnus-summary-muttprint
2165   "s" gnus-soup-add-article)
2166
2167 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2168   "b" gnus-summary-display-buttonized
2169   "m" gnus-summary-repair-multipart
2170   "v" gnus-article-view-part
2171   "o" gnus-article-save-part
2172   "O" gnus-article-save-part-and-strip
2173   "r" gnus-article-replace-part
2174   "d" gnus-article-delete-part
2175   "t" gnus-article-view-part-as-type
2176   "j" gnus-article-jump-to-part
2177   "c" gnus-article-copy-part
2178   "C" gnus-article-view-part-as-charset
2179   "e" gnus-article-view-part-externally
2180   "H" gnus-article-browse-html-article
2181   "E" gnus-article-encrypt-body
2182   "i" gnus-article-inline-part
2183   "|" gnus-article-pipe-part)
2184
2185 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2186   "p" gnus-summary-mark-as-processable
2187   "u" gnus-summary-unmark-as-processable
2188   "U" gnus-summary-unmark-all-processable
2189   "v" gnus-uu-mark-over
2190   "s" gnus-uu-mark-series
2191   "r" gnus-uu-mark-region
2192   "g" gnus-uu-unmark-region
2193   "R" gnus-uu-mark-by-regexp
2194   "G" gnus-uu-unmark-by-regexp
2195   "t" gnus-uu-mark-thread
2196   "T" gnus-uu-unmark-thread
2197   "a" gnus-uu-mark-all
2198   "b" gnus-uu-mark-buffer
2199   "S" gnus-uu-mark-sparse
2200   "k" gnus-summary-kill-process-mark
2201   "y" gnus-summary-yank-process-mark
2202   "w" gnus-summary-save-process-mark
2203   "i" gnus-uu-invert-processable)
2204
2205 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2206   ;;"x" gnus-uu-extract-any
2207   "m" gnus-summary-save-parts
2208   "u" gnus-uu-decode-uu
2209   "U" gnus-uu-decode-uu-and-save
2210   "s" gnus-uu-decode-unshar
2211   "S" gnus-uu-decode-unshar-and-save
2212   "o" gnus-uu-decode-save
2213   "O" gnus-uu-decode-save
2214   "b" gnus-uu-decode-binhex
2215   "B" gnus-uu-decode-binhex
2216   "Y" gnus-uu-decode-yenc
2217   "p" gnus-uu-decode-postscript
2218   "P" gnus-uu-decode-postscript-and-save)
2219
2220 (gnus-define-keys
2221     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2222   "u" gnus-uu-decode-uu-view
2223   "U" gnus-uu-decode-uu-and-save-view
2224   "s" gnus-uu-decode-unshar-view
2225   "S" gnus-uu-decode-unshar-and-save-view
2226   "o" gnus-uu-decode-save-view
2227   "O" gnus-uu-decode-save-view
2228   "b" gnus-uu-decode-binhex-view
2229   "B" gnus-uu-decode-binhex-view
2230   "p" gnus-uu-decode-postscript-view
2231   "P" gnus-uu-decode-postscript-and-save-view)
2232
2233 (defvar gnus-article-post-menu nil)
2234
2235 (defconst gnus-summary-menu-maxlen 20)
2236
2237 (defun gnus-summary-menu-split (menu)
2238   ;; If we have lots of elements, divide them into groups of 20
2239   ;; and make a pane (or submenu) for each one.
2240   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2241       (let ((menu menu) sublists next
2242             (i 1))
2243         (while menu
2244           ;; Pull off the next gnus-summary-menu-maxlen elements
2245           ;; and make them the next element of sublist.
2246           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2247           (if next
2248               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2249                       nil))
2250           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2251                                              (aref (car (last menu)) 0)) menu)
2252                                sublists))
2253           (setq i (1+ i))
2254           (setq menu next))
2255         (nreverse sublists))
2256     ;; Few elements--put them all in one pane.
2257     menu))
2258
2259 (defun gnus-summary-make-menu-bar ()
2260   (gnus-turn-off-edit-menu 'summary)
2261
2262   (unless (boundp 'gnus-summary-misc-menu)
2263
2264     (easy-menu-define
2265       gnus-summary-kill-menu gnus-summary-mode-map ""
2266       (cons
2267        "Score"
2268        (nconc
2269         (list
2270          ["Customize" gnus-score-customize t])
2271         (gnus-make-score-map 'increase)
2272         (gnus-make-score-map 'lower)
2273         '(("Mark"
2274            ["Kill below" gnus-summary-kill-below t]
2275            ["Mark above" gnus-summary-mark-above t]
2276            ["Tick above" gnus-summary-tick-above t]
2277            ["Clear above" gnus-summary-clear-above t])
2278           ["Current score" gnus-summary-current-score t]
2279           ["Set score" gnus-summary-set-score t]
2280           ["Switch current score file..." gnus-score-change-score-file t]
2281           ["Set mark below..." gnus-score-set-mark-below t]
2282           ["Set expunge below..." gnus-score-set-expunge-below t]
2283           ["Edit current score file" gnus-score-edit-current-scores t]
2284           ["Edit score file..." gnus-score-edit-file t]
2285           ["Trace score" gnus-score-find-trace t]
2286           ["Find words" gnus-score-find-favourite-words t]
2287           ["Rescore buffer" gnus-summary-rescore t]
2288           ["Increase score..." gnus-summary-increase-score t]
2289           ["Lower score..." gnus-summary-lower-score t]))))
2290
2291     ;; Define both the Article menu in the summary buffer and the
2292     ;; equivalent Commands menu in the article buffer here for
2293     ;; consistency.
2294     (let ((innards
2295            `(("Hide"
2296               ["All" gnus-article-hide t]
2297               ["Headers" gnus-article-hide-headers t]
2298               ["Signature" gnus-article-hide-signature t]
2299               ["Citation" gnus-article-hide-citation t]
2300               ["List identifiers" gnus-article-hide-list-identifiers t]
2301               ["Banner" gnus-article-strip-banner t]
2302               ["Boring headers" gnus-article-hide-boring-headers t])
2303              ("Highlight"
2304               ["All" gnus-article-highlight t]
2305               ["Headers" gnus-article-highlight-headers t]
2306               ["Signature" gnus-article-highlight-signature t]
2307               ["Citation" gnus-article-highlight-citation t])
2308              ("MIME"
2309               ["Words" gnus-article-decode-mime-words t]
2310               ["Charset" gnus-article-decode-charset t]
2311               ["QP" gnus-article-de-quoted-unreadable t]
2312               ["Base64" gnus-article-de-base64-unreadable t]
2313               ["View MIME buttons" gnus-summary-display-buttonized t]
2314               ["View all" gnus-mime-view-all-parts t]
2315               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2316               ["Encrypt body" gnus-article-encrypt-body
2317                :active (not (gnus-group-read-only-p))
2318                ,@(if (featurep 'xemacs) nil
2319                    '(:help "Encrypt the message body on disk"))]
2320               ["Extract all parts..." gnus-summary-save-parts t]
2321               ("Multipart"
2322                ["Repair multipart" gnus-summary-repair-multipart t]
2323                ["Pipe part..." gnus-article-pipe-part t]
2324                ["Inline part" gnus-article-inline-part t]
2325                ["View part as type..." gnus-article-view-part-as-type t]
2326                ["Encrypt body" gnus-article-encrypt-body
2327                 :active (not (gnus-group-read-only-p))
2328                ,@(if (featurep 'xemacs) nil
2329                    '(:help "Encrypt the message body on disk"))]
2330                ["View part externally" gnus-article-view-part-externally t]
2331                ["View HTML parts in browser" gnus-article-browse-html-article t]
2332                ["View part with charset..." gnus-article-view-part-as-charset t]
2333                ["Copy part" gnus-article-copy-part t]
2334                ["Save part..." gnus-article-save-part t]
2335                ["View part" gnus-article-view-part t]))
2336              ("Date"
2337               ["Local" gnus-article-date-local t]
2338               ["ISO8601" gnus-article-date-iso8601 t]
2339               ["UT" gnus-article-date-ut t]
2340               ["Original" gnus-article-date-original t]
2341               ["Lapsed" gnus-article-date-lapsed t]
2342               ["User-defined" gnus-article-date-user t])
2343              ("Display"
2344               ["Remove images" gnus-article-remove-images t]
2345               ["Toggle smiley" gnus-treat-smiley t]
2346               ["Show X-Face" gnus-article-display-x-face t]
2347               ["Show picons in From" gnus-treat-from-picon t]
2348               ["Show picons in mail headers" gnus-treat-mail-picon t]
2349               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2350               ("View as different encoding"
2351                ,@(gnus-summary-menu-split
2352                   (mapcar
2353                    (lambda (cs)
2354                      ;; Since easymenu under Emacs doesn't allow
2355                      ;; lambda forms for menu commands, we should
2356                      ;; provide intern'ed function symbols.
2357                      (let ((command (intern (format "\
2358 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2359                        (fset command
2360                              `(lambda ()
2361                                 (interactive)
2362                                 (let ((gnus-summary-show-article-charset-alist
2363                                        '((1 . ,cs))))
2364                                   (gnus-summary-show-article 1))))
2365                        `[,(symbol-name cs) ,command t]))
2366                    (sort (if (fboundp 'coding-system-list)
2367                              (coding-system-list)
2368                            (mapcar 'car mm-mime-mule-charset-alist))
2369                          'string<)))))
2370              ("Washing"
2371               ("Remove Blanks"
2372                ["Leading" gnus-article-strip-leading-blank-lines t]
2373                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2374                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2375                ["All of the above" gnus-article-strip-blank-lines t]
2376                ["All" gnus-article-strip-all-blank-lines t]
2377                ["Leading space" gnus-article-strip-leading-space t]
2378                ["Trailing space" gnus-article-strip-trailing-space t]
2379                ["Leading space in headers"
2380                 gnus-article-remove-leading-whitespace t])
2381               ["Overstrike" gnus-article-treat-overstrike t]
2382               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2383               ["Emphasis" gnus-article-emphasize t]
2384               ["Word wrap" gnus-article-fill-cited-article t]
2385               ["Fill long lines" gnus-article-fill-long-lines t]
2386               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2387               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2388               ["Remove CR" gnus-article-remove-cr t]
2389               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2390               ["Base64" gnus-article-de-base64-unreadable t]
2391               ["Rot 13" gnus-summary-caesar-message
2392                ,@(if (featurep 'xemacs) '(t)
2393                    '(:help "\"Caesar rotate\" article by 13"))]
2394               ["De-IDNA" gnus-summary-idna-message t]
2395               ["Morse decode" gnus-summary-morse-message t]
2396               ["Unix pipe..." gnus-summary-pipe-message t]
2397               ["Add buttons" gnus-article-add-buttons t]
2398               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2399               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2400               ["Verbose header" gnus-summary-verbose-headers t]
2401               ["Toggle header" gnus-summary-toggle-header t]
2402               ["Unfold headers" gnus-article-treat-unfold-headers t]
2403               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2404               ["Html" gnus-article-wash-html t]
2405               ["Unsplit URLs" gnus-article-unsplit-urls t]
2406               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2407               ["Decode HZ" gnus-article-decode-HZ t]
2408               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2409               ("(Outlook) Deuglify"
2410                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2411                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2412                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2413                ["Full (Outlook) deuglify"
2414                 gnus-article-outlook-deuglify-article t])
2415               )
2416              ("Output"
2417               ["Save in default format..." gnus-summary-save-article
2418                ,@(if (featurep 'xemacs) '(t)
2419                    '(:help "Save article using default method"))]
2420               ["Save in file..." gnus-summary-save-article-file
2421                ,@(if (featurep 'xemacs) '(t)
2422                    '(:help "Save article in file"))]
2423               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2424               ["Save in MH folder..." gnus-summary-save-article-folder t]
2425               ["Save in VM folder..." gnus-summary-save-article-vm t]
2426               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2427               ["Save body in file..." gnus-summary-save-article-body-file t]
2428               ["Pipe through a filter..." gnus-summary-pipe-output t]
2429               ["Add to SOUP packet" gnus-soup-add-article t]
2430               ["Print with Muttprint..." gnus-summary-muttprint t]
2431               ["Print" gnus-summary-print-article
2432                ,@(if (featurep 'xemacs) '(t)
2433                    '(:help "Generate and print a PostScript image"))])
2434              ("Copy, move,... (Backend)"
2435               ,@(if (featurep 'xemacs) nil
2436                   '(:help "Copying, moving, expiring articles..."))
2437               ["Respool article..." gnus-summary-respool-article t]
2438               ["Move article..." gnus-summary-move-article
2439                (gnus-check-backend-function
2440                 'request-move-article gnus-newsgroup-name)]
2441               ["Copy article..." gnus-summary-copy-article t]
2442               ["Crosspost article..." gnus-summary-crosspost-article
2443                (gnus-check-backend-function
2444                 'request-replace-article gnus-newsgroup-name)]
2445               ["Import file..." gnus-summary-import-article
2446                (gnus-check-backend-function
2447                 'request-accept-article gnus-newsgroup-name)]
2448               ["Create article..." gnus-summary-create-article
2449                (gnus-check-backend-function
2450                 'request-accept-article gnus-newsgroup-name)]
2451               ["Check if posted" gnus-summary-article-posted-p t]
2452               ["Edit article" gnus-summary-edit-article
2453                (not (gnus-group-read-only-p))]
2454               ["Delete article" gnus-summary-delete-article
2455                (gnus-check-backend-function
2456                 'request-expire-articles gnus-newsgroup-name)]
2457               ["Query respool" gnus-summary-respool-query t]
2458               ["Trace respool" gnus-summary-respool-trace t]
2459               ["Delete expirable articles" gnus-summary-expire-articles-now
2460                (gnus-check-backend-function
2461                 'request-expire-articles gnus-newsgroup-name)])
2462              ("Extract"
2463               ["Uudecode" gnus-uu-decode-uu
2464                ,@(if (featurep 'xemacs) '(t)
2465                    '(:help "Decode uuencoded article(s)"))]
2466               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2467               ["Unshar" gnus-uu-decode-unshar t]
2468               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2469               ["Save" gnus-uu-decode-save t]
2470               ["Binhex" gnus-uu-decode-binhex t]
2471               ["Postscript" gnus-uu-decode-postscript t]
2472               ["All MIME parts" gnus-summary-save-parts t])
2473              ("Cache"
2474               ["Enter article" gnus-cache-enter-article t]
2475               ["Remove article" gnus-cache-remove-article t])
2476              ["Translate" gnus-article-babel t]
2477              ["Select article buffer" gnus-summary-select-article-buffer t]
2478              ["Make article buffer sticky" gnus-sticky-article t]
2479              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2480              ["Isearch article..." gnus-summary-isearch-article t]
2481              ["Beginning of the article" gnus-summary-beginning-of-article t]
2482              ["End of the article" gnus-summary-end-of-article t]
2483              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2484              ["Fetch referenced articles" gnus-summary-refer-references t]
2485              ["Fetch current thread" gnus-summary-refer-thread t]
2486              ["Fetch article with id..." gnus-summary-refer-article t]
2487              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2488              ["Redisplay" gnus-summary-show-article t]
2489              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2490       (easy-menu-define
2491         gnus-summary-article-menu gnus-summary-mode-map ""
2492         (cons "Article" innards))
2493
2494       (if (not (keymapp gnus-summary-article-menu))
2495           (easy-menu-define
2496             gnus-article-commands-menu gnus-article-mode-map ""
2497             (cons "Commands" innards))
2498         ;; in Emacs, don't share menu.
2499         (setq gnus-article-commands-menu
2500               (copy-keymap gnus-summary-article-menu))
2501         (define-key gnus-article-mode-map [menu-bar commands]
2502           (cons "Commands" gnus-article-commands-menu))))
2503
2504     (easy-menu-define
2505       gnus-summary-thread-menu gnus-summary-mode-map ""
2506       '("Threads"
2507         ["Find all messages in thread" gnus-summary-refer-thread t]
2508         ["Toggle threading" gnus-summary-toggle-threads t]
2509         ["Hide threads" gnus-summary-hide-all-threads t]
2510         ["Show threads" gnus-summary-show-all-threads t]
2511         ["Hide thread" gnus-summary-hide-thread t]
2512         ["Show thread" gnus-summary-show-thread t]
2513         ["Go to next thread" gnus-summary-next-thread t]
2514         ["Go to previous thread" gnus-summary-prev-thread t]
2515         ["Go down thread" gnus-summary-down-thread t]
2516         ["Go up thread" gnus-summary-up-thread t]
2517         ["Top of thread" gnus-summary-top-thread t]
2518         ["Mark thread as read" gnus-summary-kill-thread t]
2519         ["Mark thread as expired" gnus-summary-expire-thread t]
2520         ["Lower thread score" gnus-summary-lower-thread t]
2521         ["Raise thread score" gnus-summary-raise-thread t]
2522         ["Rethread current" gnus-summary-rethread-current t]))
2523
2524     (easy-menu-define
2525       gnus-summary-post-menu gnus-summary-mode-map ""
2526       `("Post"
2527         ["Send a message (mail or news)" gnus-summary-post-news
2528          ,@(if (featurep 'xemacs) '(t)
2529              '(:help "Compose a new message (mail or news)"))]
2530         ["Followup" gnus-summary-followup
2531          ,@(if (featurep 'xemacs) '(t)
2532              '(:help "Post followup to this article"))]
2533         ["Followup and yank" gnus-summary-followup-with-original
2534          ,@(if (featurep 'xemacs) '(t)
2535              '(:help "Post followup to this article, quoting its contents"))]
2536         ["Supersede article" gnus-summary-supersede-article t]
2537         ["Cancel article" gnus-summary-cancel-article
2538          ,@(if (featurep 'xemacs) '(t)
2539              '(:help "Cancel an article you posted"))]
2540         ["Reply" gnus-summary-reply t]
2541         ["Reply and yank" gnus-summary-reply-with-original t]
2542         ["Wide reply" gnus-summary-wide-reply t]
2543         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2544          ,@(if (featurep 'xemacs) '(t)
2545              '(:help "Mail a reply, quoting this article"))]
2546         ["Very wide reply" gnus-summary-very-wide-reply t]
2547         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2548          ,@(if (featurep 'xemacs) '(t)
2549              '(:help "Mail a very wide reply, quoting this article"))]
2550         ["Mail forward" gnus-summary-mail-forward t]
2551         ["Post forward" gnus-summary-post-forward t]
2552         ["Digest and mail" gnus-uu-digest-mail-forward t]
2553         ["Digest and post" gnus-uu-digest-post-forward t]
2554         ["Resend message" gnus-summary-resend-message t]
2555         ["Resend message edit" gnus-summary-resend-message-edit t]
2556         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2557         ["Send a mail" gnus-summary-mail-other-window t]
2558         ["Create a local message" gnus-summary-news-other-window t]
2559         ["Uuencode and post" gnus-uu-post-news
2560          ,@(if (featurep 'xemacs) '(t)
2561              '(:help "Post a uuencoded article"))]
2562         ["Followup via news" gnus-summary-followup-to-mail t]
2563         ["Followup via news and yank"
2564          gnus-summary-followup-to-mail-with-original t]
2565         ["Strip signature on reply"
2566          (lambda ()
2567            (interactive)
2568            (if (not (memq message-cite-function
2569                           '(message-cite-original-without-signature
2570                             message-cite-original)))
2571                ;; Stupid workaround for XEmacs not honoring :visible.
2572                (message "Can't toggle this value of `message-cite-function'")
2573              (setq message-cite-function
2574                    (if (eq message-cite-function
2575                            'message-cite-original-without-signature)
2576                        'message-cite-original
2577                      'message-cite-original-without-signature))))
2578          ;; XEmacs barfs on :visible.
2579          ,@(if (featurep 'xemacs) nil
2580              '(:visible (memq message-cite-function
2581                               '(message-cite-original-without-signature
2582                                 message-cite-original))))
2583          :style toggle
2584          :selected (eq message-cite-function
2585                        'message-cite-original-without-signature)
2586          ,@(if (featurep 'xemacs) nil
2587              '(:help "Strip signature from cited article when replying."))]
2588         ;;("Draft"
2589         ;;["Send" gnus-summary-send-draft t]
2590         ;;["Send bounced" gnus-resend-bounced-mail t])
2591         ))
2592
2593     (cond
2594      ((not (keymapp gnus-summary-post-menu))
2595       (setq gnus-article-post-menu gnus-summary-post-menu))
2596      ((not gnus-article-post-menu)
2597       ;; Don't share post menu.
2598       (setq gnus-article-post-menu
2599             (copy-keymap gnus-summary-post-menu))))
2600     (define-key gnus-article-mode-map [menu-bar post]
2601       (cons "Post" gnus-article-post-menu))
2602
2603     (easy-menu-define
2604       gnus-summary-misc-menu gnus-summary-mode-map ""
2605       `("Gnus"
2606         ("Mark Read"
2607          ["Mark as read" gnus-summary-mark-as-read-forward t]
2608          ["Mark same subject and select"
2609           gnus-summary-kill-same-subject-and-select t]
2610          ["Mark same subject" gnus-summary-kill-same-subject t]
2611          ["Catchup" gnus-summary-catchup
2612           ,@(if (featurep 'xemacs) '(t)
2613               '(:help "Mark unread articles in this group as read"))]
2614          ["Catchup all" gnus-summary-catchup-all t]
2615          ["Catchup to here" gnus-summary-catchup-to-here t]
2616          ["Catchup from here" gnus-summary-catchup-from-here t]
2617          ["Catchup region" gnus-summary-mark-region-as-read
2618           (gnus-mark-active-p)]
2619          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2620         ("Mark Various"
2621          ["Tick" gnus-summary-tick-article-forward t]
2622          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2623          ["Remove marks" gnus-summary-clear-mark-forward t]
2624          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2625          ["Set bookmark" gnus-summary-set-bookmark t]
2626          ["Remove bookmark" gnus-summary-remove-bookmark t])
2627         ("Registry Mark"
2628          ["Important"     gnus-registry-set-article-Important-mark    t]
2629          ["Not Important" gnus-registry-remove-article-Important-mark t]
2630          ["Work"          gnus-registry-set-article-Work-mark         t]
2631          ["Not Work"      gnus-registry-remove-article-Work-mark      t]
2632          ["Later"         gnus-registry-set-article-Later-mark        t]
2633          ["Not Later"     gnus-registry-remove-article-Later-mark     t]
2634          ["Personal"      gnus-registry-set-article-Personal-mark     t]
2635          ["Not Personal"  gnus-registry-remove-article-Personal-mark  t]
2636          ["To Do"         gnus-registry-set-article-To-Do-mark        t]
2637          ["Not To Do"     gnus-registry-remove-article-To-Do-mark     t])
2638         ("Limit to"
2639          ["Marks..." gnus-summary-limit-to-marks t]
2640          ["Subject..." gnus-summary-limit-to-subject t]
2641          ["Author..." gnus-summary-limit-to-author t]
2642          ["Recipient..." gnus-summary-limit-to-recipient t]
2643          ["Address..." gnus-summary-limit-to-address t]
2644          ["Age..." gnus-summary-limit-to-age t]
2645          ["Extra..." gnus-summary-limit-to-extra t]
2646          ["Score..." gnus-summary-limit-to-score t]
2647          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2648          ["Unread" gnus-summary-limit-to-unread t]
2649          ["Unseen" gnus-summary-limit-to-unseen t]
2650          ["Singletons" gnus-summary-limit-to-singletons t]
2651          ["Replied" gnus-summary-limit-to-replied t]
2652          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2653          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2654          ["Pop limit" gnus-summary-pop-limit t]
2655          ["Show dormant" gnus-summary-limit-include-dormant t]
2656          ["Hide childless dormant"
2657           gnus-summary-limit-exclude-childless-dormant t]
2658          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2659          ["Hide marked" gnus-summary-limit-exclude-marks t]
2660          ["Show expunged" gnus-summary-limit-include-expunged t])
2661         ("Process Mark"
2662          ["Set mark" gnus-summary-mark-as-processable t]
2663          ["Remove mark" gnus-summary-unmark-as-processable t]
2664          ["Remove all marks" gnus-summary-unmark-all-processable t]
2665          ["Invert marks" gnus-uu-invert-processable t]
2666          ["Mark above" gnus-uu-mark-over t]
2667          ["Mark series" gnus-uu-mark-series t]
2668          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2669          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2670          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2671          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2672          ["Mark all" gnus-uu-mark-all t]
2673          ["Mark buffer" gnus-uu-mark-buffer t]
2674          ["Mark sparse" gnus-uu-mark-sparse t]
2675          ["Mark thread" gnus-uu-mark-thread t]
2676          ["Unmark thread" gnus-uu-unmark-thread t]
2677          ("Process Mark Sets"
2678           ["Kill" gnus-summary-kill-process-mark t]
2679           ["Yank" gnus-summary-yank-process-mark
2680            gnus-newsgroup-process-stack]
2681           ["Save" gnus-summary-save-process-mark t]
2682           ["Run command on marked..." gnus-summary-universal-argument t]))
2683         ("Scroll article"
2684          ["Page forward" gnus-summary-next-page
2685           ,@(if (featurep 'xemacs) '(t)
2686               '(:help "Show next page of article"))]
2687          ["Page backward" gnus-summary-prev-page
2688           ,@(if (featurep 'xemacs) '(t)
2689               '(:help "Show previous page of article"))]
2690          ["Line forward" gnus-summary-scroll-up t])
2691         ("Move"
2692          ["Next unread article" gnus-summary-next-unread-article t]
2693          ["Previous unread article" gnus-summary-prev-unread-article t]
2694          ["Next article" gnus-summary-next-article t]
2695          ["Previous article" gnus-summary-prev-article t]
2696          ["Next unread subject" gnus-summary-next-unread-subject t]
2697          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2698          ["Next article same subject" gnus-summary-next-same-subject t]
2699          ["Previous article same subject" gnus-summary-prev-same-subject t]
2700          ["First unread article" gnus-summary-first-unread-article t]
2701          ["Best unread article" gnus-summary-best-unread-article t]
2702          ["Go to subject number..." gnus-summary-goto-subject t]
2703          ["Go to article number..." gnus-summary-goto-article t]
2704          ["Go to the last article" gnus-summary-goto-last-article t]
2705          ["Pop article off history" gnus-summary-pop-article t])
2706         ("Sort"
2707          ["Sort by number" gnus-summary-sort-by-number t]
2708          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2709          ["Sort by author" gnus-summary-sort-by-author t]
2710          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2711          ["Sort by subject" gnus-summary-sort-by-subject t]
2712          ["Sort by date" gnus-summary-sort-by-date t]
2713          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2714          ["Sort by score" gnus-summary-sort-by-score t]
2715          ["Sort by lines" gnus-summary-sort-by-lines t]
2716          ["Sort by characters" gnus-summary-sort-by-chars t]
2717          ["Randomize" gnus-summary-sort-by-random t]
2718          ["Original sort" gnus-summary-sort-by-original t])
2719         ("Help"
2720          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2721          ["Describe group" gnus-summary-describe-group t]
2722          ["Fetch charter" gnus-group-fetch-charter
2723           ,@(if (featurep 'xemacs) nil
2724               '(:help "Display the charter of the current group"))]
2725          ["Fetch control message" gnus-group-fetch-control
2726           ,@(if (featurep 'xemacs) nil
2727               '(:help "Display the archived control message for the current group"))]
2728          ["Read manual" gnus-info-find-node t])
2729         ("Modes"
2730          ["Pick and read" gnus-pick-mode t]
2731          ["Binary" gnus-binary-mode t])
2732         ("Regeneration"
2733          ["Regenerate" gnus-summary-prepare t]
2734          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2735          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2736          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2737          ["Toggle threading" gnus-summary-toggle-threads t])
2738         ["See old articles" gnus-summary-insert-old-articles t]
2739         ["See new articles" gnus-summary-insert-new-articles t]
2740         ["Filter articles..." gnus-summary-execute-command t]
2741         ["Run command on articles..." gnus-summary-universal-argument t]
2742         ["Search articles forward..." gnus-summary-search-article-forward t]
2743         ["Search articles backward..." gnus-summary-search-article-backward t]
2744         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2745         ["Expand window" gnus-summary-expand-window t]
2746         ["Expire expirable articles" gnus-summary-expire-articles
2747          (gnus-check-backend-function
2748           'request-expire-articles gnus-newsgroup-name)]
2749         ["Edit local kill file" gnus-summary-edit-local-kill t]
2750         ["Edit main kill file" gnus-summary-edit-global-kill t]
2751         ["Edit group parameters" gnus-summary-edit-parameters t]
2752         ["Customize group parameters" gnus-summary-customize-parameters t]
2753         ["Send a bug report" gnus-bug t]
2754         ("Exit"
2755          ["Catchup and exit" gnus-summary-catchup-and-exit
2756           ,@(if (featurep 'xemacs) '(t)
2757               '(:help "Mark unread articles in this group as read, then exit"))]
2758          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2759          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2760          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2761          ["Exit group" gnus-summary-exit
2762           ,@(if (featurep 'xemacs) '(t)
2763               '(:help "Exit current group, return to group selection mode"))]
2764          ["Exit group without updating" gnus-summary-exit-no-update t]
2765          ["Exit and goto next group" gnus-summary-next-group t]
2766          ["Exit and goto prev group" gnus-summary-prev-group t]
2767          ["Reselect group" gnus-summary-reselect-current-group t]
2768          ["Rescan group" gnus-summary-rescan-group t]
2769          ["Update dribble" gnus-summary-save-newsrc t])))
2770
2771     (gnus-run-hooks 'gnus-summary-menu-hook)))
2772
2773 (defvar gnus-summary-tool-bar-map nil)
2774
2775 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2776 ;; affect _new_ message buffers.  We might add a function that walks thru all
2777 ;; summary-mode buffers and force the update.
2778 (defun gnus-summary-tool-bar-update (&optional symbol value)
2779   "Update summary mode toolbar.
2780 Setter function for custom variables."
2781   (setq-default gnus-summary-tool-bar-map nil)
2782   (when symbol
2783     ;; When used as ":set" function:
2784     (set-default symbol value))
2785   (when (gnus-buffer-live-p gnus-summary-buffer)
2786     (with-current-buffer gnus-summary-buffer
2787       (gnus-summary-make-tool-bar))))
2788
2789 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2790                                      'gnus-summary-tool-bar-gnome
2791                                    'gnus-summary-tool-bar-retro)
2792   "Specifies the Gnus summary tool bar.
2793
2794 It can be either a list or a symbol refering to a list.  See
2795 `gmm-tool-bar-from-list' for the format of the list.  The
2796 default key map is `gnus-summary-mode-map'.
2797
2798 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2799 `gnus-summary-tool-bar-retro'."
2800   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2801                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2802                  (repeat :tag "User defined list" gmm-tool-bar-item)
2803                  (symbol))
2804   :version "23.1" ;; No Gnus
2805   :initialize 'custom-initialize-default
2806   :set 'gnus-summary-tool-bar-update
2807   :group 'gnus-summary)
2808
2809 (defcustom gnus-summary-tool-bar-gnome
2810   '((gnus-summary-post-news "mail/compose" nil)
2811     (gnus-summary-insert-new-articles "mail/inbox" nil
2812                                       :visible (or (not gnus-agent)
2813                                                    gnus-plugged))
2814     (gnus-summary-reply-with-original "mail/reply")
2815     (gnus-summary-reply "mail/reply" nil :visible nil)
2816     (gnus-summary-followup-with-original "mail/reply-all")
2817     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2818     (gnus-summary-mail-forward "mail/forward")
2819     (gnus-summary-save-article "mail/save")
2820     (gnus-summary-search-article-forward "search" nil :visible nil)
2821     (gnus-summary-print-article "print")
2822     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2823     ;; Some new commands that may need more suitable icons:
2824     (gnus-summary-save-newsrc "save" nil :visible nil)
2825     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2826     (gnus-summary-prev-article "left-arrow")
2827     (gnus-summary-next-article "right-arrow")
2828     (gnus-summary-next-page "next-page")
2829     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2830     ;;
2831     ;; Maybe some sort-by-... could be added:
2832     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2833     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2834     (gnus-summary-mark-as-expirable
2835      "delete" nil
2836      :visible (gnus-check-backend-function 'request-expire-articles
2837                                            gnus-newsgroup-name))
2838     (gnus-summary-mark-as-spam
2839      "mail/spam" t
2840      :visible (and (fboundp 'spam-group-ham-contents-p)
2841                    (spam-group-ham-contents-p gnus-newsgroup-name))
2842      :help "Mark as spam")
2843     (gnus-summary-mark-as-read-forward
2844      "mail/not-spam" nil
2845      :visible (and (fboundp 'spam-group-spam-contents-p)
2846                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2847     ;;
2848     (gnus-summary-exit "exit")
2849     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2850     (gnus-info-find-node "help"))
2851   "List of functions for the summary tool bar (GNOME style).
2852
2853 See `gmm-tool-bar-from-list' for the format of the list."
2854   :type '(repeat gmm-tool-bar-item)
2855   :version "23.1" ;; No Gnus
2856   :initialize 'custom-initialize-default
2857   :set 'gnus-summary-tool-bar-update
2858   :group 'gnus-summary)
2859
2860 (defcustom gnus-summary-tool-bar-retro
2861   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2862     (gnus-summary-next-unread-article "gnus/next-ur")
2863     (gnus-summary-post-news "gnus/post")
2864     (gnus-summary-followup-with-original "gnus/fuwo")
2865     (gnus-summary-followup "gnus/followup")
2866     (gnus-summary-reply-with-original "gnus/reply-wo")
2867     (gnus-summary-reply "gnus/reply")
2868     (gnus-summary-caesar-message "gnus/rot13")
2869     (gnus-uu-decode-uu "gnus/uu-decode")
2870     (gnus-summary-save-article-file "gnus/save-aif")
2871     (gnus-summary-save-article "gnus/save-art")
2872     (gnus-uu-post-news "gnus/uu-post")
2873     (gnus-summary-catchup "gnus/catchup")
2874     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2875     (gnus-summary-exit "gnus/exit-summ")
2876     ;; Some new command that may need more suitable icons:
2877     (gnus-summary-print-article "gnus/print" nil :visible nil)
2878     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2879     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2880     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2881     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2882     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2883     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2884     ;;
2885     (gnus-info-find-node "gnus/help" nil :visible nil))
2886   "List of functions for the summary tool bar (retro look).
2887
2888 See `gmm-tool-bar-from-list' for the format of the list."
2889   :type '(repeat gmm-tool-bar-item)
2890   :version "23.1" ;; No Gnus
2891   :initialize 'custom-initialize-default
2892   :set 'gnus-summary-tool-bar-update
2893   :group 'gnus-summary)
2894
2895 (defcustom gnus-summary-tool-bar-zap-list t
2896   "List of icon items from the global tool bar.
2897 These items are not displayed in the Gnus summary mode tool bar.
2898
2899 See `gmm-tool-bar-from-list' for the format of the list."
2900   :type 'gmm-tool-bar-zap-list
2901   :version "23.1" ;; No Gnus
2902   :initialize 'custom-initialize-default
2903   :set 'gnus-summary-tool-bar-update
2904   :group 'gnus-summary)
2905
2906 (defvar image-load-path)
2907 (defvar tool-bar-map)
2908
2909 (defun gnus-summary-make-tool-bar (&optional force)
2910   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2911 When FORCE, rebuild the tool bar."
2912   (when (and (not (featurep 'xemacs))
2913              (boundp 'tool-bar-mode)
2914              tool-bar-mode
2915              (or (not gnus-summary-tool-bar-map) force))
2916     (let* ((load-path
2917             (gmm-image-load-path-for-library "gnus"
2918                                              "mail/save.xpm"
2919                                              nil t))
2920            (image-load-path (cons (car load-path)
2921                                   (when (boundp 'image-load-path)
2922                                     image-load-path)))
2923            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2924                                         gnus-summary-tool-bar-zap-list
2925                                         'gnus-summary-mode-map)))
2926       (when map
2927         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2928         ;; uses it's value.
2929         (setq gnus-summary-tool-bar-map map))))
2930   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2931
2932 (defun gnus-score-set-default (var value)
2933   "A version of set that updates the GNU Emacs menu-bar."
2934   (set var value)
2935   ;; It is the message that forces the active status to be updated.
2936   (message ""))
2937
2938 (defun gnus-make-score-map (type)
2939   "Make a summary score map of type TYPE."
2940   (if t
2941       nil
2942     (let ((headers '(("author" "from" string)
2943                      ("subject" "subject" string)
2944                      ("article body" "body" string)
2945                      ("article head" "head" string)
2946                      ("xref" "xref" string)
2947                      ("extra header" "extra" string)
2948                      ("lines" "lines" number)
2949                      ("followups to author" "followup" string)))
2950           (types '((number ("less than" <)
2951                            ("greater than" >)
2952                            ("equal" =))
2953                    (string ("substring" s)
2954                            ("exact string" e)
2955                            ("fuzzy string" f)
2956                            ("regexp" r))))
2957           (perms '(("temporary" (current-time-string))
2958                    ("permanent" nil)
2959                    ("immediate" now)))
2960           header)
2961       (list
2962        (apply
2963         'nconc
2964         (list
2965          (if (eq type 'lower)
2966              "Lower score"
2967            "Increase score"))
2968         (let (outh)
2969           (while headers
2970             (setq header (car headers))
2971             (setq outh
2972                   (cons
2973                    (apply
2974                     'nconc
2975                     (list (car header))
2976                     (let ((ts (cdr (assoc (nth 2 header) types)))
2977                           outt)
2978                       (while ts
2979                         (setq outt
2980                               (cons
2981                                (apply
2982                                 'nconc
2983                                 (list (caar ts))
2984                                 (let ((ps perms)
2985                                       outp)
2986                                   (while ps
2987                                     (setq outp
2988                                           (cons
2989                                            (vector
2990                                             (caar ps)
2991                                             (list
2992                                              'gnus-summary-score-entry
2993                                              (nth 1 header)
2994                                              (if (or (string= (nth 1 header)
2995                                                               "head")
2996                                                      (string= (nth 1 header)
2997                                                               "body"))
2998                                                  ""
2999                                                (list 'gnus-summary-header
3000                                                      (nth 1 header)))
3001                                              (list 'quote (nth 1 (car ts)))
3002                                              (list 'gnus-score-delta-default
3003                                                    nil)
3004                                              (nth 1 (car ps))
3005                                              t)
3006                                             t)
3007                                            outp))
3008                                     (setq ps (cdr ps)))
3009                                   (list (nreverse outp))))
3010                                outt))
3011                         (setq ts (cdr ts)))
3012                       (list (nreverse outt))))
3013                    outh))
3014             (setq headers (cdr headers)))
3015           (list (nreverse outh))))))))
3016
3017
3018 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3019
3020 \f
3021
3022 (defun gnus-summary-mode (&optional group)
3023   "Major mode for reading articles.
3024
3025 All normal editing commands are switched off.
3026 \\<gnus-summary-mode-map>
3027 Each line in this buffer represents one article.  To read an
3028 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3029 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3030 respectively.
3031
3032 You can also post articles and send mail from this buffer.  To
3033 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3034 of an article, type `\\[gnus-summary-reply]'.
3035
3036 There are approx. one gazillion commands you can execute in this
3037 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3038
3039 The following commands are available:
3040
3041 \\{gnus-summary-mode-map}"
3042   (interactive)
3043   (kill-all-local-variables)
3044   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3045     (gnus-summary-make-local-variables))
3046   (gnus-summary-make-local-variables)
3047   (setq gnus-newsgroup-name group)
3048   (when (gnus-visual-p 'summary-menu 'menu)
3049     (gnus-summary-make-menu-bar)
3050     (gnus-summary-make-tool-bar))
3051   (gnus-make-thread-indent-array)
3052   (gnus-simplify-mode-line)
3053   (setq major-mode 'gnus-summary-mode)
3054   (setq mode-name "Summary")
3055   (make-local-variable 'minor-mode-alist)
3056   (use-local-map gnus-summary-mode-map)
3057   (buffer-disable-undo)
3058   (setq buffer-read-only t              ;Disable modification
3059         show-trailing-whitespace nil)
3060   (setq truncate-lines t)
3061   (setq selective-display t)
3062   (setq selective-display-ellipses t)   ;Display `...'
3063   (gnus-summary-set-display-table)
3064   (gnus-set-default-directory)
3065   (make-local-variable 'gnus-summary-line-format)
3066   (make-local-variable 'gnus-summary-line-format-spec)
3067   (make-local-variable 'gnus-summary-dummy-line-format)
3068   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3069   (make-local-variable 'gnus-summary-mark-positions)
3070   (gnus-make-local-hook 'pre-command-hook)
3071   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3072   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3073   (turn-on-gnus-mailing-list-mode)
3074   (mm-enable-multibyte)
3075   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3076   (gnus-update-summary-mark-positions))
3077
3078 (defun gnus-summary-make-local-variables ()
3079   "Make all the local summary buffer variables."
3080   (let (global)
3081     (dolist (local gnus-summary-local-variables)
3082       (if (consp local)
3083           (progn
3084             (if (eq (cdr local) 'global)
3085                 ;; Copy the global value of the variable.
3086                 (setq global (symbol-value (car local)))
3087               ;; Use the value from the list.
3088               (setq global (eval (cdr local))))
3089             (set (make-local-variable (car local)) global))
3090         ;; Simple nil-valued local variable.
3091         (set (make-local-variable local) nil)))))
3092
3093 (defun gnus-summary-clear-local-variables ()
3094   (let ((locals gnus-summary-local-variables))
3095     (while locals
3096       (if (consp (car locals))
3097           (and (symbolp (caar locals))
3098                (set (caar locals) nil))
3099         (and (symbolp (car locals))
3100              (set (car locals) nil)))
3101       (setq locals (cdr locals)))))
3102
3103 ;; Summary data functions.
3104
3105 (defmacro gnus-data-number (data)
3106   `(car ,data))
3107
3108 (defmacro gnus-data-set-number (data number)
3109   `(setcar ,data ,number))
3110
3111 (defmacro gnus-data-mark (data)
3112   `(nth 1 ,data))
3113
3114 (defmacro gnus-data-set-mark (data mark)
3115   `(setcar (nthcdr 1 ,data) ,mark))
3116
3117 (defmacro gnus-data-pos (data)
3118   `(nth 2 ,data))
3119
3120 (defmacro gnus-data-set-pos (data pos)
3121   `(setcar (nthcdr 2 ,data) ,pos))
3122
3123 (defmacro gnus-data-header (data)
3124   `(nth 3 ,data))
3125
3126 (defmacro gnus-data-set-header (data header)
3127   `(setf (nth 3 ,data) ,header))
3128
3129 (defmacro gnus-data-level (data)
3130   `(nth 4 ,data))
3131
3132 (defmacro gnus-data-unread-p (data)
3133   `(= (nth 1 ,data) gnus-unread-mark))
3134
3135 (defmacro gnus-data-read-p (data)
3136   `(/= (nth 1 ,data) gnus-unread-mark))
3137
3138 (defmacro gnus-data-pseudo-p (data)
3139   `(consp (nth 3 ,data)))
3140
3141 (defmacro gnus-data-find (number)
3142   `(assq ,number gnus-newsgroup-data))
3143
3144 (defmacro gnus-data-find-list (number &optional data)
3145   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3146      (memq (assq ,number bdata)
3147            bdata)))
3148
3149 (defmacro gnus-data-make (number mark pos header level)
3150   `(list ,number ,mark ,pos ,header ,level))
3151
3152 (defun gnus-data-enter (after-article number mark pos header level offset)
3153   (let ((data (gnus-data-find-list after-article)))
3154     (unless data
3155       (error "No such article: %d" after-article))
3156     (setcdr data (cons (gnus-data-make number mark pos header level)
3157                        (cdr data)))
3158     (setq gnus-newsgroup-data-reverse nil)
3159     (gnus-data-update-list (cddr data) offset)))
3160
3161 (defun gnus-data-enter-list (after-article list &optional offset)
3162   (when list
3163     (let ((data (and after-article (gnus-data-find-list after-article)))
3164           (ilist list))
3165       (if (not (or data
3166                    after-article))
3167           (let ((odata gnus-newsgroup-data))
3168             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3169             (when offset
3170               (gnus-data-update-list odata offset)))
3171         ;; Find the last element in the list to be spliced into the main
3172         ;; list.
3173         (setq list (last list))
3174         (if (not data)
3175             (progn
3176               (setcdr list gnus-newsgroup-data)
3177               (setq gnus-newsgroup-data ilist)
3178               (when offset
3179                 (gnus-data-update-list (cdr list) offset)))
3180           (setcdr list (cdr data))
3181           (setcdr data ilist)
3182           (when offset
3183             (gnus-data-update-list (cdr list) offset))))
3184       (setq gnus-newsgroup-data-reverse nil))))
3185
3186 (defun gnus-data-remove (article &optional offset)
3187   (let ((data gnus-newsgroup-data))
3188     (if (= (gnus-data-number (car data)) article)
3189         (progn
3190           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3191                 gnus-newsgroup-data-reverse nil)
3192           (when offset
3193             (gnus-data-update-list gnus-newsgroup-data offset)))
3194       (while (cdr data)
3195         (when (= (gnus-data-number (cadr data)) article)
3196           (setcdr data (cddr data))
3197           (when offset
3198             (gnus-data-update-list (cdr data) offset))
3199           (setq data nil
3200                 gnus-newsgroup-data-reverse nil))
3201         (setq data (cdr data))))))
3202
3203 (defmacro gnus-data-list (backward)
3204   `(if ,backward
3205        (or gnus-newsgroup-data-reverse
3206            (setq gnus-newsgroup-data-reverse
3207                  (reverse gnus-newsgroup-data)))
3208      gnus-newsgroup-data))
3209
3210 (defun gnus-data-update-list (data offset)
3211   "Add OFFSET to the POS of all data entries in DATA."
3212   (setq gnus-newsgroup-data-reverse nil)
3213   (while data
3214     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3215     (setq data (cdr data))))
3216
3217 (defun gnus-summary-article-pseudo-p (article)
3218   "Say whether this article is a pseudo article or not."
3219   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3220
3221 (defmacro gnus-summary-article-sparse-p (article)
3222   "Say whether this article is a sparse article or not."
3223   `(memq ,article gnus-newsgroup-sparse))
3224
3225 (defmacro gnus-summary-article-ancient-p (article)
3226   "Say whether this article is a sparse article or not."
3227   `(memq ,article gnus-newsgroup-ancient))
3228
3229 (defun gnus-article-parent-p (number)
3230   "Say whether this article is a parent or not."
3231   (let ((data (gnus-data-find-list number)))
3232     (and (cdr data)              ; There has to be an article after...
3233          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3234             (gnus-data-level (nth 1 data))))))
3235
3236 (defun gnus-article-children (number)
3237   "Return a list of all children to NUMBER."
3238   (let* ((data (gnus-data-find-list number))
3239          (level (gnus-data-level (car data)))
3240          children)
3241     (setq data (cdr data))
3242     (while (and data
3243                 (= (gnus-data-level (car data)) (1+ level)))
3244       (push (gnus-data-number (car data)) children)
3245       (setq data (cdr data)))
3246     children))
3247
3248 (defmacro gnus-summary-skip-intangible ()
3249   "If the current article is intangible, then jump to a different article."
3250   '(let ((to (get-text-property (point) 'gnus-intangible)))
3251      (and to (gnus-summary-goto-subject to))))
3252
3253 (defmacro gnus-summary-article-intangible-p ()
3254   "Say whether this article is intangible or not."
3255   '(get-text-property (point) 'gnus-intangible))
3256
3257 (defun gnus-article-read-p (article)
3258   "Say whether ARTICLE is read or not."
3259   (not (or (memq article gnus-newsgroup-marked)
3260            (memq article gnus-newsgroup-spam-marked)
3261            (memq article gnus-newsgroup-unreads)
3262            (memq article gnus-newsgroup-unselected)
3263            (memq article gnus-newsgroup-dormant))))
3264
3265 ;; Some summary mode macros.
3266
3267 (defmacro gnus-summary-article-number ()
3268   "The article number of the article on the current line.
3269 If there isn't an article number here, then we return the current
3270 article number."
3271   '(progn
3272      (gnus-summary-skip-intangible)
3273      (or (get-text-property (point) 'gnus-number)
3274          (gnus-summary-last-subject))))
3275
3276 (defmacro gnus-summary-article-header (&optional number)
3277   "Return the header of article NUMBER."
3278   `(gnus-data-header (gnus-data-find
3279                       ,(or number '(gnus-summary-article-number)))))
3280
3281 (defmacro gnus-summary-thread-level (&optional number)
3282   "Return the level of thread that starts with article NUMBER."
3283   `(if (and (eq gnus-summary-make-false-root 'dummy)
3284             (get-text-property (point) 'gnus-intangible))
3285        0
3286      (gnus-data-level (gnus-data-find
3287                        ,(or number '(gnus-summary-article-number))))))
3288
3289 (defmacro gnus-summary-article-mark (&optional number)
3290   "Return the mark of article NUMBER."
3291   `(gnus-data-mark (gnus-data-find
3292                     ,(or number '(gnus-summary-article-number)))))
3293
3294 (defmacro gnus-summary-article-pos (&optional number)
3295   "Return the position of the line of article NUMBER."
3296   `(gnus-data-pos (gnus-data-find
3297                    ,(or number '(gnus-summary-article-number)))))
3298
3299 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3300 (defmacro gnus-summary-article-subject (&optional number)
3301   "Return current subject string or nil if nothing."
3302   `(let ((headers
3303           ,(if number
3304                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3305              '(gnus-data-header (assq (gnus-summary-article-number)
3306                                       gnus-newsgroup-data)))))
3307      (and headers
3308           (vectorp headers)
3309           (mail-header-subject headers))))
3310
3311 (defmacro gnus-summary-article-score (&optional number)
3312   "Return current article score."
3313   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3314                   gnus-newsgroup-scored))
3315        gnus-summary-default-score 0))
3316
3317 (defun gnus-summary-article-children (&optional number)
3318   "Return a list of article numbers that are children of article NUMBER."
3319   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3320          (level (gnus-data-level (car data)))
3321          l children)
3322     (while (and (setq data (cdr data))
3323                 (> (setq l (gnus-data-level (car data))) level))
3324       (and (= (1+ level) l)
3325            (push (gnus-data-number (car data))
3326                  children)))
3327     (nreverse children)))
3328
3329 (defun gnus-summary-article-parent (&optional number)
3330   "Return the article number of the parent of article NUMBER."
3331   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3332                                     (gnus-data-list t)))
3333          (level (gnus-data-level (car data))))
3334     (if (zerop level)
3335         ()                              ; This is a root.
3336       ;; We search until we find an article with a level less than
3337       ;; this one.  That function has to be the parent.
3338       (while (and (setq data (cdr data))
3339                   (not (< (gnus-data-level (car data)) level))))
3340       (and data (gnus-data-number (car data))))))
3341
3342 (defun gnus-unread-mark-p (mark)
3343   "Say whether MARK is the unread mark."
3344   (= mark gnus-unread-mark))
3345
3346 (defun gnus-read-mark-p (mark)
3347   "Say whether MARK is one of the marks that mark as read.
3348 This is all marks except unread, ticked, dormant, and expirable."
3349   (not (or (= mark gnus-unread-mark)
3350            (= mark gnus-ticked-mark)
3351            (= mark gnus-spam-mark)
3352            (= mark gnus-dormant-mark)
3353            (= mark gnus-expirable-mark))))
3354
3355 (defmacro gnus-article-mark (number)
3356   "Return the MARK of article NUMBER.
3357 This macro should only be used when computing the mark the \"first\"
3358 time; i.e., when generating the summary lines.  After that,
3359 `gnus-summary-article-mark' should be used to examine the
3360 marks of articles."
3361   `(cond
3362     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3363     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3364     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3365     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3366     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3367     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3368     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3369     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3370            gnus-ancient-mark))))
3371
3372 ;; Saving hidden threads.
3373
3374 (defmacro gnus-save-hidden-threads (&rest forms)
3375   "Save hidden threads, eval FORMS, and restore the hidden threads."
3376   (let ((config (make-symbol "config")))
3377     `(let ((,config (gnus-hidden-threads-configuration)))
3378        (unwind-protect
3379            (save-excursion
3380              ,@forms)
3381          (gnus-restore-hidden-threads-configuration ,config)))))
3382 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3383 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3384
3385 (defun gnus-data-compute-positions ()
3386   "Compute the positions of all articles."
3387   (setq gnus-newsgroup-data-reverse nil)
3388   (let ((data gnus-newsgroup-data))
3389     (save-excursion
3390       (gnus-save-hidden-threads
3391         (gnus-summary-show-all-threads)
3392         (goto-char (point-min))
3393         (while data
3394           (while (get-text-property (point) 'gnus-intangible)
3395             (forward-line 1))
3396           (gnus-data-set-pos (car data) (+ (point) 3))
3397           (setq data (cdr data))
3398           (forward-line 1))))))
3399
3400 (defun gnus-hidden-threads-configuration ()
3401   "Return the current hidden threads configuration."
3402   (save-excursion
3403     (let (config)
3404       (goto-char (point-min))
3405       (while (search-forward "\r" nil t)
3406         (push (1- (point)) config))
3407       config)))
3408
3409 (defun gnus-restore-hidden-threads-configuration (config)
3410   "Restore hidden threads configuration from CONFIG."
3411   (save-excursion
3412     (let (point (inhibit-read-only t))
3413       (while (setq point (pop config))
3414         (when (and (< point (point-max))
3415                    (goto-char point)
3416                    (eq (char-after) ?\n))
3417           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3418
3419 ;; Various summary mode internalish functions.
3420
3421 (defun gnus-mouse-pick-article (e)
3422   (interactive "e")
3423   (mouse-set-point e)
3424   (gnus-summary-next-page nil t))
3425
3426 (defun gnus-summary-set-display-table ()
3427   "Change the display table.
3428 Odd characters have a tendency to mess
3429 up nicely formatted displays - we make all possible glyphs
3430 display only a single character."
3431
3432   ;; We start from the standard display table, if any.
3433   (let ((table (or (copy-sequence standard-display-table)
3434                    (make-display-table)))
3435         (i 32))
3436     ;; Nix out all the control chars...
3437     (while (>= (setq i (1- i)) 0)
3438       (gnus-put-display-table i [??] table))
3439    ;; ... but not newline and cr, of course.  (cr is necessary for the
3440     ;; selective display).
3441     (gnus-put-display-table ?\n nil table)
3442     (gnus-put-display-table ?\r nil table)
3443     ;; We keep TAB as well.
3444     (gnus-put-display-table ?\t nil table)
3445     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3446     ;; Emacs 23 (unicode), that are not set already.
3447     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3448                  160
3449                256)))
3450       (while (>= (setq i (1- i)) 127)
3451         ;; Only modify if the entry is nil.
3452         (unless (gnus-get-display-table i table)
3453           (gnus-put-display-table i [??] table))))
3454     (setq buffer-display-table table)))
3455
3456 (defun gnus-summary-set-article-display-arrow (pos)
3457   "Update the overlay arrow to point to line at position POS."
3458   (when (and gnus-summary-display-arrow
3459              (boundp 'overlay-arrow-position)
3460              (boundp 'overlay-arrow-string))
3461     (save-excursion
3462       (goto-char pos)
3463       (beginning-of-line)
3464       (unless overlay-arrow-position
3465         (setq overlay-arrow-position (make-marker)))
3466       (setq overlay-arrow-string "=>"
3467             overlay-arrow-position (set-marker overlay-arrow-position
3468                                                (point)
3469                                                (current-buffer))))))
3470
3471 (defun gnus-summary-setup-buffer (group)
3472   "Initialize summary buffer."
3473   (let ((buffer (gnus-summary-buffer-name group))
3474         (dead-name (concat "*Dead Summary "
3475                            (gnus-group-decoded-name group) "*")))
3476     ;; If a dead summary buffer exists, we kill it.
3477     (when (gnus-buffer-live-p dead-name)
3478       (gnus-kill-buffer dead-name))
3479     (if (get-buffer buffer)
3480         (progn
3481           (set-buffer buffer)
3482           (setq gnus-summary-buffer (current-buffer))
3483           (not gnus-newsgroup-prepared))
3484       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3485       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3486       (gnus-summary-mode group)
3487       (when gnus-carpal
3488         (gnus-carpal-setup-buffer 'summary))
3489       (when (gnus-group-quit-config group)
3490         (set (make-local-variable 'gnus-single-article-buffer) nil))
3491       (make-local-variable 'gnus-article-buffer)
3492       (make-local-variable 'gnus-article-current)
3493       (make-local-variable 'gnus-original-article-buffer)
3494       (setq gnus-newsgroup-name group)
3495       ;; Set any local variables in the group parameters.
3496       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3497       t)))
3498
3499 (defun gnus-set-global-variables ()
3500   "Set the global equivalents of the buffer-local variables.
3501 They are set to the latest values they had.  These reflect the summary
3502 buffer that was in action when the last article was fetched."
3503   (when (eq major-mode 'gnus-summary-mode)
3504     (setq gnus-summary-buffer (current-buffer))
3505     (let ((name gnus-newsgroup-name)
3506           (marked gnus-newsgroup-marked)
3507           (spam gnus-newsgroup-spam-marked)
3508           (unread gnus-newsgroup-unreads)
3509           (headers gnus-current-headers)
3510           (data gnus-newsgroup-data)
3511           (summary gnus-summary-buffer)
3512           (article-buffer gnus-article-buffer)
3513           (original gnus-original-article-buffer)
3514           (gac gnus-article-current)
3515           (reffed gnus-reffed-article-number)
3516           (score-file gnus-current-score-file)
3517           (default-charset gnus-newsgroup-charset)
3518           vlist)
3519       (let ((locals gnus-newsgroup-variables))
3520         (while locals
3521           (if (consp (car locals))
3522               (push (eval (caar locals)) vlist)
3523             (push (eval (car locals)) vlist))
3524           (setq locals (cdr locals)))
3525         (setq vlist (nreverse vlist)))
3526       (with-current-buffer gnus-group-buffer
3527         (setq gnus-newsgroup-name name
3528               gnus-newsgroup-marked marked
3529               gnus-newsgroup-spam-marked spam
3530               gnus-newsgroup-unreads unread
3531               gnus-current-headers headers
3532               gnus-newsgroup-data data
3533               gnus-article-current gac
3534               gnus-summary-buffer summary
3535               gnus-article-buffer article-buffer
3536               gnus-original-article-buffer original
3537               gnus-reffed-article-number reffed
3538               gnus-current-score-file score-file
3539               gnus-newsgroup-charset default-charset)
3540         (let ((locals gnus-newsgroup-variables))
3541           (while locals
3542             (if (consp (car locals))
3543                 (set (caar locals) (pop vlist))
3544               (set (car locals) (pop vlist)))
3545             (setq locals (cdr locals))))
3546         ;; The article buffer also has local variables.
3547         (when (gnus-buffer-live-p gnus-article-buffer)
3548           (set-buffer gnus-article-buffer)
3549           (setq gnus-summary-buffer summary))))))
3550
3551 (defun gnus-summary-article-unread-p (article)
3552   "Say whether ARTICLE is unread or not."
3553   (memq article gnus-newsgroup-unreads))
3554
3555 (defun gnus-summary-first-article-p (&optional article)
3556   "Return whether ARTICLE is the first article in the buffer."
3557   (if (not (setq article (or article (gnus-summary-article-number))))
3558       nil
3559     (eq article (caar gnus-newsgroup-data))))
3560
3561 (defun gnus-summary-last-article-p (&optional article)
3562   "Return whether ARTICLE is the last article in the buffer."
3563   (if (not (setq article (or article (gnus-summary-article-number))))
3564       ;; All non-existent numbers are the last article.  :-)
3565       t
3566     (not (cdr (gnus-data-find-list article)))))
3567
3568 (defun gnus-make-thread-indent-array (&optional n)
3569   (when (or n
3570             (progn (setq n 200) nil)
3571             (null gnus-thread-indent-array)
3572             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3573     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3574           gnus-thread-indent-array-level gnus-thread-indent-level)
3575     (while (>= n 0)
3576       (aset gnus-thread-indent-array n
3577             (make-string (* n gnus-thread-indent-level) ? ))
3578       (setq n (1- n)))))
3579
3580 (defun gnus-update-summary-mark-positions ()
3581   "Compute where the summary marks are to go."
3582   (save-excursion
3583     (when (gnus-buffer-exists-p gnus-summary-buffer)
3584       (set-buffer gnus-summary-buffer))
3585     (let ((spec gnus-summary-line-format-spec)
3586           pos)
3587       (save-excursion
3588         (gnus-set-work-buffer)
3589         (let ((gnus-tmp-unread ?Z)
3590               (gnus-replied-mark ?Z)
3591               (gnus-score-below-mark ?Z)
3592               (gnus-score-over-mark ?Z)
3593               (gnus-undownloaded-mark ?Z)
3594               (gnus-summary-line-format-spec spec)
3595               (gnus-newsgroup-downloadable '(0))
3596               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3597               case-fold-search ignores)
3598           ;; Here, all marks are bound to Z.
3599           (gnus-summary-insert-line header
3600                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3601           (goto-char (point-min))
3602           ;; Memorize the positions of the same characters as dummy marks.
3603           (while (re-search-forward "[A-D]" nil t)
3604             (push (point) ignores))
3605           (erase-buffer)
3606           ;; We use A-D as dummy marks in order to know column positions
3607           ;; where marks should be inserted.
3608           (setq gnus-tmp-unread ?A
3609                 gnus-replied-mark ?B
3610                 gnus-score-below-mark ?C
3611                 gnus-score-over-mark ?C
3612                 gnus-undownloaded-mark ?D)
3613           (gnus-summary-insert-line header
3614                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3615           ;; Ignore characters which aren't dummy marks.
3616           (dolist (p ignores)
3617             (delete-region (goto-char (1- p)) p)
3618             (insert ?Z))
3619           (goto-char (point-min))
3620           (setq pos (list (cons 'unread
3621                                 (and (search-forward "A" nil t)
3622                                      (- (point) (point-min) 1)))))
3623           (goto-char (point-min))
3624           (push (cons 'replied (and (search-forward "B" nil t)
3625                                     (- (point) (point-min) 1)))
3626                 pos)
3627           (goto-char (point-min))
3628           (push (cons 'score (and (search-forward "C" nil t)
3629                                   (- (point) (point-min) 1)))
3630                 pos)
3631           (goto-char (point-min))
3632           (push (cons 'download (and (search-forward "D" nil t)
3633                                      (- (point) (point-min) 1)))
3634                 pos)))
3635       (setq gnus-summary-mark-positions pos))))
3636
3637 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3638   "Insert a dummy root in the summary buffer."
3639   (beginning-of-line)
3640   (gnus-add-text-properties
3641    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3642    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3643
3644 (defun gnus-summary-extract-address-component (from)
3645   (or (car (funcall gnus-extract-address-components from))
3646       from))
3647
3648 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3649   (let ((mail-parse-charset gnus-newsgroup-charset)
3650         (ignored-from-addresses (gnus-ignored-from-addresses))
3651         ; Is it really necessary to do this next part for each summary line?
3652         ; Luckily, doesn't seem to slow things down much.
3653         (mail-parse-ignored-charsets
3654          (with-current-buffer gnus-summary-buffer
3655            gnus-newsgroup-ignored-charsets)))
3656     (or
3657      (and ignored-from-addresses
3658           (string-match ignored-from-addresses gnus-tmp-from)
3659           (let ((extra-headers (mail-header-extra header))
3660                 to
3661                 newsgroups)
3662             (cond
3663              ((setq to (cdr (assq 'To extra-headers)))
3664               (concat gnus-summary-to-prefix
3665                       (inline
3666                         (gnus-summary-extract-address-component
3667                          (funcall gnus-decode-encoded-address-function to)))))
3668              ((setq newsgroups
3669                     (or
3670                      (cdr (assq 'Newsgroups extra-headers))
3671                      (and
3672                       (memq 'Newsgroups gnus-extra-headers)
3673                       (eq (car (gnus-find-method-for-group
3674                                 gnus-newsgroup-name)) 'nntp)
3675                       (gnus-group-real-name gnus-newsgroup-name))))
3676               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3677      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3678
3679 (defun gnus-summary-insert-line (gnus-tmp-header
3680                                  gnus-tmp-level gnus-tmp-current
3681                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3682                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3683                                  &optional gnus-tmp-dummy gnus-tmp-score
3684                                  gnus-tmp-process)
3685   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3686       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3687                                           gnus-tmp-level)))
3688   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3689          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3690          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3691          (gnus-tmp-score-char
3692           (if (or (null gnus-summary-default-score)
3693                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3694                       gnus-summary-zcore-fuzz))
3695               ?                         ;Whitespace
3696             (if (< gnus-tmp-score gnus-summary-default-score)
3697                 gnus-score-below-mark gnus-score-over-mark)))
3698          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3699          (gnus-tmp-replied
3700           (cond (gnus-tmp-process gnus-process-mark)
3701                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3702                  gnus-cached-mark)
3703                 (gnus-tmp-replied gnus-replied-mark)
3704                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3705                  gnus-forwarded-mark)
3706                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3707                  gnus-saved-mark)
3708                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3709                  gnus-recent-mark)
3710                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3711                  gnus-unseen-mark)
3712                 (t gnus-no-mark)))
3713          (gnus-tmp-downloaded
3714           (cond (undownloaded
3715                  gnus-undownloaded-mark)
3716                 (gnus-newsgroup-agentized
3717                  gnus-downloaded-mark)
3718                 (t
3719                  gnus-no-mark)))
3720          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3721          (gnus-tmp-name
3722           (cond
3723            ((string-match "<[^>]+> *$" gnus-tmp-from)
3724             (let ((beg (match-beginning 0)))
3725               (or (and (string-match "^\".+\"" gnus-tmp-from)
3726                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3727                   (substring gnus-tmp-from 0 beg))))
3728            ((string-match "(.+)" gnus-tmp-from)
3729             (substring gnus-tmp-from
3730                        (1+ (match-beginning 0)) (1- (match-end 0))))
3731            (t gnus-tmp-from)))
3732          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3733          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3734          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3735          (inhibit-read-only t))
3736     (when (string= gnus-tmp-name "")
3737       (setq gnus-tmp-name gnus-tmp-from))
3738     (unless (numberp gnus-tmp-lines)
3739       (setq gnus-tmp-lines -1))
3740     (if (= gnus-tmp-lines -1)
3741         (setq gnus-tmp-lines "?")
3742       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3743     (condition-case ()
3744         (gnus-put-text-property
3745          (point)
3746          (progn (eval gnus-summary-line-format-spec) (point))
3747          'gnus-number gnus-tmp-number)
3748       (error (gnus-message 5 "Error updating the summary line")))
3749     (when (gnus-visual-p 'summary-highlight 'highlight)
3750       (forward-line -1)
3751       (gnus-run-hooks 'gnus-summary-update-hook)
3752       (forward-line 1))))
3753
3754 (defun gnus-summary-update-line (&optional dont-update)
3755   "Update summary line after change."
3756   (when (and gnus-summary-default-score
3757              (not gnus-summary-inhibit-highlight))
3758     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3759            (article (gnus-summary-article-number))
3760            (score (gnus-summary-article-score article)))
3761       (unless dont-update
3762         (if (and gnus-summary-mark-below
3763                  (< (gnus-summary-article-score)
3764                     gnus-summary-mark-below))
3765             ;; This article has a low score, so we mark it as read.
3766             (when (memq article gnus-newsgroup-unreads)
3767               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3768           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3769             ;; This article was previously marked as read on account
3770             ;; of a low score, but now it has risen, so we mark it as
3771             ;; unread.
3772             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3773         (gnus-summary-update-mark
3774          (if (or (null gnus-summary-default-score)
3775                  (<= (abs (- score gnus-summary-default-score))
3776                      gnus-summary-zcore-fuzz))
3777              ?                          ;Whitespace
3778            (if (< score gnus-summary-default-score)
3779                gnus-score-below-mark gnus-score-over-mark))
3780          'score))
3781       ;; Do visual highlighting.
3782       (when (gnus-visual-p 'summary-highlight 'highlight)
3783         (gnus-run-hooks 'gnus-summary-update-hook)))))
3784
3785 (defvar gnus-tmp-new-adopts nil)
3786
3787 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3788   "Return the number of articles in THREAD.
3789 This may be 0 in some cases -- if none of the articles in
3790 the thread are to be displayed."
3791   (let* ((number
3792          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3793           (cond
3794            ((not (listp thread))
3795             1)
3796            ((and (consp thread) (cdr thread))
3797             (apply
3798              '+ 1 (mapcar
3799                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3800            ((null thread)
3801             1)
3802            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3803             1)
3804            (t 0))))
3805     (when (and level (zerop level) gnus-tmp-new-adopts)
3806       (incf number
3807             (apply '+ (mapcar
3808                        'gnus-summary-number-of-articles-in-thread
3809                        gnus-tmp-new-adopts))))
3810     (if char
3811         (if (> number 1) gnus-not-empty-thread-mark
3812           gnus-empty-thread-mark)
3813       number)))
3814
3815 (defsubst gnus-summary-line-message-size (head)
3816   "Return pretty-printed version of message size.
3817 This function is intended to be used in
3818 `gnus-summary-line-format-alist'."
3819   (let ((c (or (mail-header-chars head) -1)))
3820     (cond ((< c 0) "n/a")               ; chars not available
3821           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3822           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3823           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3824           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3825
3826
3827 (defun gnus-summary-set-local-parameters (group)
3828   "Go through the local params of GROUP and set all variable specs in that list."
3829   (let ((vars '(quit-config)))          ; Ignore quit-config.
3830     (dolist (elem (gnus-group-find-parameter group))
3831       (and (consp elem)                 ; Has to be a cons.
3832            (consp (cdr elem))           ; The cdr has to be a list.
3833            (symbolp (car elem))         ; Has to be a symbol in there.
3834
3835            ;; Variables like `gnus-show-threads' that are globally bound,
3836            ;; if used as group parameters, need to get to be buffer-local,
3837            ;; whereas just parameters like `gcc-self', `timestamp', etc.
3838            ;; should not be bound as variables.
3839            (boundp (car elem))          ; Has to be already bound
3840
3841            (not (memq (car elem) vars))
3842            (ignore-errors               ; So we set it.
3843              (push (car elem) vars)
3844              (make-local-variable (car elem))
3845              (set (car elem) (eval (nth 1 elem))))))))
3846
3847 (defun gnus-summary-read-group (group &optional show-all no-article
3848                                       kill-buffer no-display backward
3849                                       select-articles)
3850   "Start reading news in newsgroup GROUP.
3851 If SHOW-ALL is non-nil, already read articles are also listed.
3852 If NO-ARTICLE is non-nil, no article is selected initially.
3853 If NO-DISPLAY, don't generate a summary buffer."
3854   (let (result)
3855     (while (and group
3856                 (null (setq result
3857                             (let ((gnus-auto-select-next nil))
3858                               (or (gnus-summary-read-group-1
3859                                    group show-all no-article
3860                                    kill-buffer no-display
3861                                    select-articles)
3862                                   (setq show-all nil
3863                                         select-articles nil)))))
3864                 (eq gnus-auto-select-next 'quietly))
3865       (set-buffer gnus-group-buffer)
3866       ;; The entry function called above goes to the next
3867       ;; group automatically, so we go two groups back
3868       ;; if we are searching for the previous group.
3869       (when backward
3870         (gnus-group-prev-unread-group 2))
3871       (if (not (equal group (gnus-group-group-name)))
3872           (setq group (gnus-group-group-name))
3873         (setq group nil)))
3874     result))
3875
3876 (defun gnus-summary-read-group-1 (group show-all no-article
3877                                         kill-buffer no-display
3878                                         &optional select-articles)
3879   ;; Killed foreign groups can't be entered.
3880   ;;  (when (and (not (gnus-group-native-p group))
3881   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3882   ;;    (error "Dead non-native groups can't be entered"))
3883   (gnus-message 5 "Retrieving newsgroup: %s..."
3884                 (gnus-group-decoded-name group))
3885   (let* ((new-group (gnus-summary-setup-buffer group))
3886          (quit-config (gnus-group-quit-config group))
3887          (did-select (and new-group (gnus-select-newsgroup
3888                                      group show-all select-articles))))
3889     (cond
3890      ;; This summary buffer exists already, so we just select it.
3891      ((not new-group)
3892       (gnus-set-global-variables)
3893       (when kill-buffer
3894         (gnus-kill-or-deaden-summary kill-buffer))
3895       (gnus-configure-windows 'summary 'force)
3896       (gnus-set-mode-line 'summary)
3897       (gnus-summary-position-point)
3898       (message "")
3899       t)
3900      ;; We couldn't select this group.
3901      ((null did-select)
3902       (when (and (eq major-mode 'gnus-summary-mode)
3903                  (not (equal (current-buffer) kill-buffer)))
3904         (kill-buffer (current-buffer))
3905         (if (not quit-config)
3906             (progn
3907               ;; Update the info -- marks might need to be removed,
3908               ;; for instance.
3909               (gnus-summary-update-info)
3910               (set-buffer gnus-group-buffer)
3911               (gnus-group-jump-to-group group)
3912               (gnus-group-next-unread-group 1))
3913           (gnus-handle-ephemeral-exit quit-config)))
3914       (let ((grpinfo (gnus-get-info group)))
3915         (if (null (gnus-info-read grpinfo))
3916             (gnus-message 3 "Group %s contains no messages"
3917                           (gnus-group-decoded-name group))
3918           (gnus-message 3 "Can't select group")))
3919       nil)
3920      ;; The user did a `C-g' while prompting for number of articles,
3921      ;; so we exit this group.
3922      ((eq did-select 'quit)
3923       (and (eq major-mode 'gnus-summary-mode)
3924            (not (equal (current-buffer) kill-buffer))
3925            (kill-buffer (current-buffer)))
3926       (when kill-buffer
3927         (gnus-kill-or-deaden-summary kill-buffer))
3928       (if (not quit-config)
3929           (progn
3930             (set-buffer gnus-group-buffer)
3931             (gnus-group-jump-to-group group)
3932             (gnus-group-next-unread-group 1)
3933             (gnus-configure-windows 'group 'force))
3934         (gnus-handle-ephemeral-exit quit-config))
3935       ;; Finally signal the quit.
3936       (signal 'quit nil))
3937      ;; The group was successfully selected.
3938      (t
3939       (gnus-set-global-variables)
3940       ;; Save the active value in effect when the group was entered.
3941       (setq gnus-newsgroup-active
3942             (gnus-copy-sequence
3943              (gnus-active gnus-newsgroup-name)))
3944       ;; You can change the summary buffer in some way with this hook.
3945       (gnus-run-hooks 'gnus-select-group-hook)
3946       (when (memq 'summary (gnus-update-format-specifications
3947                             nil 'summary 'summary-mode 'summary-dummy))
3948         ;; The format specification for the summary line was updated,
3949         ;; so we need to update the mark positions as well.
3950         (gnus-update-summary-mark-positions))
3951       ;; Do score processing.
3952       (when gnus-use-scoring
3953         (gnus-possibly-score-headers))
3954       ;; Check whether to fill in the gaps in the threads.
3955       (when gnus-build-sparse-threads
3956         (gnus-build-sparse-threads))
3957       ;; Find the initial limit.
3958       (if show-all
3959           (let ((gnus-newsgroup-dormant nil))
3960             (gnus-summary-initial-limit show-all))
3961         (gnus-summary-initial-limit show-all))
3962       ;; Generate the summary buffer.
3963       (unless no-display
3964         (gnus-summary-prepare))
3965       (when gnus-use-trees
3966         (gnus-tree-open group)
3967         (setq gnus-summary-highlight-line-function
3968               'gnus-tree-highlight-article))
3969       ;; If the summary buffer is empty, but there are some low-scored
3970       ;; articles or some excluded dormants, we include these in the
3971       ;; buffer.
3972       (when (and (zerop (buffer-size))
3973                  (not no-display))
3974         (cond (gnus-newsgroup-dormant
3975                (gnus-summary-limit-include-dormant))
3976               ((and gnus-newsgroup-scored show-all)
3977                (gnus-summary-limit-include-expunged t))))
3978       ;; Function `gnus-apply-kill-file' must be called in this hook.
3979       (gnus-run-hooks 'gnus-apply-kill-hook)
3980       (if (and (zerop (buffer-size))
3981                (not no-display))
3982           (progn
3983             ;; This newsgroup is empty.
3984             (gnus-summary-catchup-and-exit nil t)
3985             (gnus-message 6 "No unread news")
3986             (when kill-buffer
3987               (gnus-kill-or-deaden-summary kill-buffer))
3988             ;; Return nil from this function.
3989             nil)
3990         ;; Hide conversation thread subtrees.  We cannot do this in
3991         ;; gnus-summary-prepare-hook since kill processing may not
3992         ;; work with hidden articles.
3993         (gnus-summary-maybe-hide-threads)
3994         (when kill-buffer
3995           (gnus-kill-or-deaden-summary kill-buffer))
3996         (gnus-summary-auto-select-subject)
3997         ;; Show first unread article if requested.
3998         (if (and (not no-article)
3999                  (not no-display)
4000                  gnus-newsgroup-unreads
4001                  gnus-auto-select-first)
4002             (progn
4003               (gnus-configure-windows 'summary)
4004               (let ((art (gnus-summary-article-number)))
4005                 (unless (and (not gnus-plugged)
4006                              (or (memq art gnus-newsgroup-undownloaded)
4007                                  (memq art gnus-newsgroup-downloadable)))
4008                   (gnus-summary-goto-article art))))
4009           ;; Don't select any articles.
4010           (gnus-summary-position-point)
4011           (gnus-configure-windows 'summary 'force)
4012           (gnus-set-mode-line 'summary))
4013         (when (and gnus-auto-center-group
4014                    (get-buffer-window gnus-group-buffer t))
4015           ;; Gotta use windows, because recenter does weird stuff if
4016           ;; the current buffer ain't the displayed window.
4017           (let ((owin (selected-window)))
4018             (select-window (get-buffer-window gnus-group-buffer t))
4019             (when (gnus-group-goto-group group)
4020               (recenter))
4021             (select-window owin)))
4022         ;; Mark this buffer as "prepared".
4023         (setq gnus-newsgroup-prepared t)
4024         (gnus-run-hooks 'gnus-summary-prepared-hook)
4025         (unless (gnus-ephemeral-group-p group)
4026           (gnus-group-update-group group))
4027         t)))))
4028
4029 (defun gnus-summary-auto-select-subject ()
4030   "Select the subject line on initial group entry."
4031   (goto-char (point-min))
4032   (cond
4033    ((eq gnus-auto-select-subject 'best)
4034     (gnus-summary-best-unread-subject))
4035    ((eq gnus-auto-select-subject 'unread)
4036     (gnus-summary-first-unread-subject))
4037    ((eq gnus-auto-select-subject 'unseen)
4038     (gnus-summary-first-unseen-subject))
4039    ((eq gnus-auto-select-subject 'unseen-or-unread)
4040     (gnus-summary-first-unseen-or-unread-subject))
4041    ((eq gnus-auto-select-subject 'first)
4042     ;; Do nothing.
4043     )
4044    ((functionp gnus-auto-select-subject)
4045     (funcall gnus-auto-select-subject))))
4046
4047 (defun gnus-summary-prepare ()
4048   "Generate the summary buffer."
4049   (interactive)
4050   (let ((inhibit-read-only t))
4051     (erase-buffer)
4052     (setq gnus-newsgroup-data nil
4053           gnus-newsgroup-data-reverse nil)
4054     (gnus-run-hooks 'gnus-summary-generate-hook)
4055     ;; Generate the buffer, either with threads or without.
4056     (when gnus-newsgroup-headers
4057       (gnus-summary-prepare-threads
4058        (if gnus-show-threads
4059            (gnus-sort-gathered-threads
4060             (funcall gnus-summary-thread-gathering-function
4061                      (gnus-sort-threads
4062                       (gnus-cut-threads (gnus-make-threads)))))
4063          ;; Unthreaded display.
4064          (gnus-sort-articles gnus-newsgroup-headers))))
4065     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4066     ;; Call hooks for modifying summary buffer.
4067     (goto-char (point-min))
4068     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4069
4070 (defsubst gnus-general-simplify-subject (subject)
4071   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4072   (setq subject
4073         (cond
4074          ;; Truncate the subject.
4075          (gnus-simplify-subject-functions
4076           (gnus-map-function gnus-simplify-subject-functions subject))
4077          ((numberp gnus-summary-gather-subject-limit)
4078           (setq subject (gnus-simplify-subject-re subject))
4079           (if (> (length subject) gnus-summary-gather-subject-limit)
4080               (substring subject 0 gnus-summary-gather-subject-limit)
4081             subject))
4082          ;; Fuzzily simplify it.
4083          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4084           (gnus-simplify-subject-fuzzy subject))
4085          ;; Just remove the leading "Re:".
4086          (t
4087           (gnus-simplify-subject-re subject))))
4088
4089   (if (and gnus-summary-gather-exclude-subject
4090            (string-match gnus-summary-gather-exclude-subject subject))
4091       nil                         ; This article shouldn't be gathered
4092     subject))
4093
4094 (defun gnus-summary-simplify-subject-query ()
4095   "Query where the respool algorithm would put this article."
4096   (interactive)
4097   (gnus-summary-select-article)
4098   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4099
4100 (defun gnus-gather-threads-by-subject (threads)
4101   "Gather threads by looking at Subject headers."
4102   (if (not gnus-summary-make-false-root)
4103       threads
4104     (let ((hashtb (gnus-make-hashtable 1024))
4105           (prev threads)
4106           (result threads)
4107           subject hthread whole-subject)
4108       (while threads
4109         (setq subject (gnus-general-simplify-subject
4110                        (setq whole-subject (mail-header-subject
4111                                             (caar threads)))))
4112         (when subject
4113           (if (setq hthread (gnus-gethash subject hashtb))
4114               (progn
4115                 ;; We enter a dummy root into the thread, if we
4116                 ;; haven't done that already.
4117                 (unless (stringp (caar hthread))
4118                   (setcar hthread (list whole-subject (car hthread))))
4119                 ;; We add this new gathered thread to this gathered
4120                 ;; thread.
4121                 (setcdr (car hthread)
4122                         (nconc (cdar hthread) (list (car threads))))
4123                 ;; Remove it from the list of threads.
4124                 (setcdr prev (cdr threads))
4125                 (setq threads prev))
4126             ;; Enter this thread into the hash table.
4127             (gnus-sethash subject
4128                           (if gnus-summary-make-false-root-always
4129                               (progn
4130                                 ;; If you want a dummy root above all
4131                                 ;; threads...
4132                                 (setcar threads (list whole-subject
4133                                                       (car threads)))
4134                                 threads)
4135                             threads)
4136                           hashtb)))
4137         (setq prev threads)
4138         (setq threads (cdr threads)))
4139       result)))
4140
4141 (defun gnus-gather-threads-by-references (threads)
4142   "Gather threads by looking at References headers."
4143   (let ((idhashtb (gnus-make-hashtable 1024))
4144         (thhashtb (gnus-make-hashtable 1024))
4145         (prev threads)
4146         (result threads)
4147         ids references id gthread gid entered ref)
4148     (while threads
4149       (when (setq references (mail-header-references (caar threads)))
4150         (setq id (mail-header-id (caar threads))
4151               ids (inline (gnus-split-references references))
4152               entered nil)
4153         (while (setq ref (pop ids))
4154           (setq ids (delete ref ids))
4155           (if (not (setq gid (gnus-gethash ref idhashtb)))
4156               (progn
4157                 (gnus-sethash ref id idhashtb)
4158                 (gnus-sethash id threads thhashtb))
4159             (setq gthread (gnus-gethash gid thhashtb))
4160             (unless entered
4161               ;; We enter a dummy root into the thread, if we
4162               ;; haven't done that already.
4163               (unless (stringp (caar gthread))
4164                 (setcar gthread (list (mail-header-subject (caar gthread))
4165                                       (car gthread))))
4166               ;; We add this new gathered thread to this gathered
4167               ;; thread.
4168               (setcdr (car gthread)
4169                       (nconc (cdar gthread) (list (car threads)))))
4170             ;; Add it into the thread hash table.
4171             (gnus-sethash id gthread thhashtb)
4172             (setq entered t)
4173             ;; Remove it from the list of threads.
4174             (setcdr prev (cdr threads))
4175             (setq threads prev))))
4176       (setq prev threads)
4177       (setq threads (cdr threads)))
4178     result))
4179
4180 (defun gnus-sort-gathered-threads (threads)
4181   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4182   (let ((result threads))
4183     (while threads
4184       (when (stringp (caar threads))
4185         (setcdr (car threads)
4186                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4187       (setq threads (cdr threads)))
4188     result))
4189
4190 (defun gnus-thread-loop-p (root thread)
4191   "Say whether ROOT is in THREAD."
4192   (let ((stack (list thread))
4193         (infloop 0)
4194         th)
4195     (while (setq thread (pop stack))
4196       (setq th (cdr thread))
4197       (while (and th
4198                   (not (eq (caar th) root)))
4199         (pop th))
4200       (if th
4201           ;; We have found a loop.
4202           (let (ref-dep)
4203             (setcdr thread (delq (car th) (cdr thread)))
4204             (if (boundp (setq ref-dep (intern "none"
4205                                               gnus-newsgroup-dependencies)))
4206                 (setcdr (symbol-value ref-dep)
4207                         (nconc (cdr (symbol-value ref-dep))
4208                                (list (car th))))
4209               (set ref-dep (list nil (car th))))
4210             (setq infloop 1
4211                   stack nil))
4212         ;; Push all the subthreads onto the stack.
4213         (push (cdr thread) stack)))
4214     infloop))
4215
4216 (defun gnus-make-threads ()
4217   "Go through the dependency hashtb and find the roots.  Return all threads."
4218   (let (threads)
4219     (while (catch 'infloop
4220              (mapatoms
4221               (lambda (refs)
4222                 ;; Deal with self-referencing References loops.
4223                 (when (and (car (symbol-value refs))
4224                            (not (zerop
4225                                  (apply
4226                                   '+
4227                                   (mapcar
4228                                    (lambda (thread)
4229                                      (gnus-thread-loop-p
4230                                       (car (symbol-value refs)) thread))
4231                                    (cdr (symbol-value refs)))))))
4232                   (setq threads nil)
4233                   (throw 'infloop t))
4234                 (unless (car (symbol-value refs))
4235                   ;; These threads do not refer back to any other
4236                   ;; articles, so they're roots.
4237                   (setq threads (append (cdr (symbol-value refs)) threads))))
4238               gnus-newsgroup-dependencies)))
4239     threads))
4240
4241 ;; Build the thread tree.
4242 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4243   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4244
4245 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4246 if it was already present.
4247
4248 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4249 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4250 Message-IDs will be renamed to a unique Message-ID before being
4251 entered.
4252
4253 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4254   (let* ((id (mail-header-id header))
4255          (id-dep (and id (intern id dependencies)))
4256          parent-id ref ref-dep ref-header replaced)
4257     ;; Enter this `header' in the `dependencies' table.
4258     (cond
4259      ((not id-dep)
4260       (setq header nil))
4261      ;; The first two cases do the normal part: enter a new `header'
4262      ;; in the `dependencies' table.
4263      ((not (boundp id-dep))
4264       (set id-dep (list header)))
4265      ((null (car (symbol-value id-dep)))
4266       (setcar (symbol-value id-dep) header))
4267
4268      ;; From here the `header' was already present in the
4269      ;; `dependencies' table.
4270      (force-new
4271       ;; Overrides an existing entry;
4272       ;; just set the header part of the entry.
4273       (setcar (symbol-value id-dep) header)
4274       (setq replaced t))
4275
4276      ;; Renames the existing `header' to a unique Message-ID.
4277      ((not gnus-summary-ignore-duplicates)
4278       ;; An article with this Message-ID has already been seen.
4279       ;; We rename the Message-ID.
4280       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4281            (list header))
4282       (mail-header-set-id header id))
4283
4284      ;; The last case ignores an existing entry, except it adds any
4285      ;; additional Xrefs (in case the two articles came from different
4286      ;; servers.
4287      ;; Also sets `header' to `nil' meaning that the `dependencies'
4288      ;; table was *not* modified.
4289      (t
4290       (mail-header-set-xref
4291        (car (symbol-value id-dep))
4292        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4293                    "")
4294                (or (mail-header-xref header) "")))
4295       (setq header nil)))
4296
4297     (when (and header (not replaced))
4298       ;; First check that we are not creating a References loop.
4299       (setq parent-id (gnus-parent-id (mail-header-references header)))
4300       (setq ref parent-id)
4301       (while (and ref
4302                   (setq ref-dep (intern-soft ref dependencies))
4303                   (boundp ref-dep)
4304                   (setq ref-header (car (symbol-value ref-dep))))
4305         (if (string= id ref)
4306             ;; Yuk!  This is a reference loop.  Make the article be a
4307             ;; root article.
4308             (progn
4309               (mail-header-set-references (car (symbol-value id-dep)) "none")
4310               (setq ref nil)
4311               (setq parent-id nil))
4312           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4313       (setq ref-dep (intern (or parent-id "none") dependencies))
4314       (if (boundp ref-dep)
4315           (setcdr (symbol-value ref-dep)
4316                   (nconc (cdr (symbol-value ref-dep))
4317                          (list (symbol-value id-dep))))
4318         (set ref-dep (list nil (symbol-value id-dep)))))
4319     header))
4320
4321 (defun gnus-extract-message-id-from-in-reply-to (string)
4322   (if (string-match "<[^>]+>" string)
4323       (substring string (match-beginning 0) (match-end 0))
4324     nil))
4325
4326 (defun gnus-build-sparse-threads ()
4327   (let ((headers gnus-newsgroup-headers)
4328         (mail-parse-charset gnus-newsgroup-charset)
4329         (gnus-summary-ignore-duplicates t)
4330         header references generation relations
4331         subject child end new-child date)
4332     ;; First we create an alist of generations/relations, where
4333     ;; generations is how much we trust the relation, and the relation
4334     ;; is parent/child.
4335     (gnus-message 7 "Making sparse threads...")
4336     (save-excursion
4337       (nnheader-set-temp-buffer " *gnus sparse threads*")
4338       (while (setq header (pop headers))
4339         (when (and (setq references (mail-header-references header))
4340                    (not (string= references "")))
4341           (insert references)
4342           (setq child (mail-header-id header)
4343                 subject (mail-header-subject header)
4344                 date (mail-header-date header)
4345                 generation 0)
4346           (while (search-backward ">" nil t)
4347             (setq end (1+ (point)))
4348             (when (search-backward "<" nil t)
4349               (setq new-child (buffer-substring (point) end))
4350               (push (list (incf generation)
4351                           child (setq child new-child)
4352                           subject date)
4353                     relations)))
4354           (when child
4355             (push (list (1+ generation) child nil subject) relations))
4356           (erase-buffer)))
4357       (kill-buffer (current-buffer)))
4358     ;; Sort over trustworthiness.
4359     (dolist (relation (sort relations 'car-less-than-car))
4360       (when (gnus-dependencies-add-header
4361              (make-full-mail-header
4362               gnus-reffed-article-number
4363               (nth 3 relation) "" (or (nth 4 relation) "")
4364               (nth 1 relation)
4365               (or (nth 2 relation) "") 0 0 "")
4366              gnus-newsgroup-dependencies nil)
4367         (push gnus-reffed-article-number gnus-newsgroup-limit)
4368         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4369         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4370               gnus-newsgroup-reads)
4371         (decf gnus-reffed-article-number)))
4372     (gnus-message 7 "Making sparse threads...done")))
4373
4374 (defun gnus-build-old-threads ()
4375   ;; Look at all the articles that refer back to old articles, and
4376   ;; fetch the headers for the articles that aren't there.  This will
4377   ;; build complete threads - if the roots haven't been expired by the
4378   ;; server, that is.
4379   (let ((mail-parse-charset gnus-newsgroup-charset)
4380         id heads)
4381     (mapatoms
4382      (lambda (refs)
4383        (when (not (car (symbol-value refs)))
4384          (setq heads (cdr (symbol-value refs)))
4385          (while heads
4386            (if (memq (mail-header-number (caar heads))
4387                      gnus-newsgroup-dormant)
4388                (setq heads (cdr heads))
4389              (setq id (symbol-name refs))
4390              (while (and (setq id (gnus-build-get-header id))
4391                          (not (car (gnus-id-to-thread id)))))
4392              (setq heads nil)))))
4393      gnus-newsgroup-dependencies)))
4394
4395 (defsubst gnus-remove-odd-characters (string)
4396   "Translate STRING into something that doesn't contain weird characters."
4397   (mm-subst-char-in-string
4398    ?\r ?\-
4399    (mm-subst-char-in-string ?\n ?\- string t) t))
4400
4401 ;; This function has to be called with point after the article number
4402 ;; on the beginning of the line.
4403 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4404   (let ((eol (point-at-eol))
4405         (buffer (current-buffer))
4406         header references in-reply-to)
4407
4408     ;; overview: [num subject from date id refs chars lines misc]
4409     (unwind-protect
4410         (let (x)
4411           (narrow-to-region (point) eol)
4412           (unless (eobp)
4413             (forward-char))
4414
4415           (setq header
4416                 (make-full-mail-header
4417                  number                 ; number
4418                  (condition-case ()     ; subject
4419                      (gnus-remove-odd-characters
4420                       (funcall gnus-decode-encoded-word-function
4421                                (setq x (nnheader-nov-field))))
4422                    (error x))
4423                  (condition-case ()     ; from
4424                      (gnus-remove-odd-characters
4425                       (funcall gnus-decode-encoded-address-function
4426                                (setq x (nnheader-nov-field))))
4427                    (error x))
4428                  (nnheader-nov-field)   ; date
4429                  (nnheader-nov-read-message-id number)  ; id
4430                  (setq references (nnheader-nov-field)) ; refs
4431                  (nnheader-nov-read-integer) ; chars
4432                  (nnheader-nov-read-integer) ; lines
4433                  (unless (eobp)
4434                    (if (looking-at "Xref: ")
4435                        (goto-char (match-end 0)))
4436                    (nnheader-nov-field)) ; Xref
4437                  (nnheader-nov-parse-extra)))) ; extra
4438
4439       (widen))
4440
4441     (when (and (string= references "")
4442                (setq in-reply-to (mail-header-extra header))
4443                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4444       (mail-header-set-references
4445        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4446
4447     (when gnus-alter-header-function
4448       (funcall gnus-alter-header-function header))
4449     (gnus-dependencies-add-header header dependencies force-new)))
4450
4451 (defun gnus-build-get-header (id)
4452   "Look through the buffer of NOV lines and find the header to ID.
4453 Enter this line into the dependencies hash table, and return
4454 the id of the parent article (if any)."
4455   (let ((deps gnus-newsgroup-dependencies)
4456         found header)
4457     (prog1
4458         (with-current-buffer nntp-server-buffer
4459           (let ((case-fold-search nil))
4460             (goto-char (point-min))
4461             (while (and (not found)
4462                         (search-forward id nil t))
4463               (beginning-of-line)
4464               (setq found (looking-at
4465                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4466                                    (regexp-quote id))))
4467               (or found (beginning-of-line 2)))
4468             (when found
4469               (beginning-of-line)
4470               (and
4471                (setq header (gnus-nov-parse-line
4472                              (read (current-buffer)) deps))
4473                (gnus-parent-id (mail-header-references header))))))
4474       (when header
4475         (let ((number (mail-header-number header)))
4476           (push number gnus-newsgroup-limit)
4477           (push header gnus-newsgroup-headers)
4478           (if (memq number gnus-newsgroup-unselected)
4479               (progn
4480                 (setq gnus-newsgroup-unreads
4481                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4482                                                number))
4483                 (setq gnus-newsgroup-unselected
4484                       (delq number gnus-newsgroup-unselected)))
4485             (push number gnus-newsgroup-ancient)))))))
4486
4487 (defun gnus-build-all-threads ()
4488   "Read all the headers."
4489   (let ((gnus-summary-ignore-duplicates t)
4490         (mail-parse-charset gnus-newsgroup-charset)
4491         (dependencies gnus-newsgroup-dependencies)
4492         header article)
4493     (with-current-buffer nntp-server-buffer
4494       (let ((case-fold-search nil))
4495         (goto-char (point-min))
4496         (while (not (eobp))
4497           (ignore-errors
4498             (setq article (read (current-buffer))
4499                   header (gnus-nov-parse-line article dependencies)))
4500           (when header
4501             (with-current-buffer gnus-summary-buffer
4502               (push header gnus-newsgroup-headers)
4503               (if (memq (setq article (mail-header-number header))
4504                         gnus-newsgroup-unselected)
4505                   (progn
4506                     (setq gnus-newsgroup-unreads
4507                           (gnus-add-to-sorted-list
4508                            gnus-newsgroup-unreads article))
4509                     (setq gnus-newsgroup-unselected
4510                           (delq article gnus-newsgroup-unselected)))
4511                 (push article gnus-newsgroup-ancient)))
4512             (forward-line 1)))))))
4513
4514 (defun gnus-summary-update-article-line (article header)
4515   "Update the line for ARTICLE using HEADER."
4516   (let* ((id (mail-header-id header))
4517          (thread (gnus-id-to-thread id)))
4518     (unless thread
4519       (error "Article in no thread"))
4520     ;; Update the thread.
4521     (setcar thread header)
4522     (gnus-summary-goto-subject article)
4523     (let* ((datal (gnus-data-find-list article))
4524            (data (car datal))
4525            (inhibit-read-only t)
4526            (level (gnus-summary-thread-level)))
4527       (gnus-delete-line)
4528       (let ((inserted (- (point)
4529                          (progn
4530                            (gnus-summary-insert-line
4531                             header level nil
4532                             (memq article gnus-newsgroup-undownloaded)
4533                             (gnus-article-mark article)
4534                             (memq article gnus-newsgroup-replied)
4535                             (memq article gnus-newsgroup-expirable)
4536                             ;; Only insert the Subject string when it's different
4537                             ;; from the previous Subject string.
4538                             (if (and
4539                                  gnus-show-threads
4540                                  (gnus-subject-equal
4541                                   (condition-case ()
4542                                       (mail-header-subject
4543                                        (gnus-data-header
4544                                         (cadr
4545                                          (gnus-data-find-list
4546                                           article
4547                                           (gnus-data-list t)))))
4548                                     ;; Error on the side of excessive subjects.
4549                                     (error ""))
4550                                   (mail-header-subject header)))
4551                                 ""
4552                               (mail-header-subject header))
4553                             nil (cdr (assq article gnus-newsgroup-scored))
4554                             (memq article gnus-newsgroup-processable))
4555                            (point)))))
4556         (when (cdr datal)
4557           (gnus-data-update-list
4558            (cdr datal)
4559            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4560
4561 (defun gnus-summary-update-article (article &optional iheader)
4562   "Update ARTICLE in the summary buffer."
4563   (set-buffer gnus-summary-buffer)
4564   (let* ((header (gnus-summary-article-header article))
4565          (id (mail-header-id header))
4566          (data (gnus-data-find article))
4567          (thread (gnus-id-to-thread id))
4568          (references (mail-header-references header))
4569          (parent
4570           (gnus-id-to-thread
4571            (or (gnus-parent-id
4572                 (when (and references
4573                            (not (equal "" references)))
4574                   references))
4575                "none")))
4576          (inhibit-read-only t)
4577          (old (car thread)))
4578     (when thread
4579       (unless iheader
4580         (setcar thread nil)
4581         (when parent
4582           (delq thread parent)))
4583       (if (gnus-summary-insert-subject id header)
4584           ;; Set the (possibly) new article number in the data structure.
4585           (gnus-data-set-number data (gnus-id-to-article id))
4586         (setcar thread old)
4587         nil))))
4588
4589 (defun gnus-rebuild-thread (id &optional line)
4590   "Rebuild the thread containing ID.
4591 If LINE, insert the rebuilt thread starting on line LINE."
4592   (let ((inhibit-read-only t)
4593         old-pos current thread data)
4594     (if (not gnus-show-threads)
4595         (setq thread (list (car (gnus-id-to-thread id))))
4596       ;; Get the thread this article is part of.
4597       (setq thread (gnus-remove-thread id)))
4598     (setq old-pos (point-at-bol))
4599     (setq current (save-excursion
4600                     (and (re-search-backward "[\r\n]" nil t)
4601                          (gnus-summary-article-number))))
4602     ;; If this is a gathered thread, we have to go some re-gathering.
4603     (when (stringp (car thread))
4604       (let ((subject (car thread))
4605             roots thr)
4606         (setq thread (cdr thread))
4607         (while thread
4608           (unless (memq (setq thr (gnus-id-to-thread
4609                                    (gnus-root-id
4610                                     (mail-header-id (caar thread)))))
4611                         roots)
4612             (push thr roots))
4613           (setq thread (cdr thread)))
4614         ;; We now have all (unique) roots.
4615         (if (= (length roots) 1)
4616             ;; All the loose roots are now one solid root.
4617             (setq thread (car roots))
4618           (setq thread (cons subject (gnus-sort-threads roots))))))
4619     (let (threads)
4620       ;; We then insert this thread into the summary buffer.
4621       (when line
4622         (goto-char (point-min))
4623         (forward-line (1- line)))
4624       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4625         (if gnus-show-threads
4626             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4627           (gnus-summary-prepare-unthreaded thread))
4628         (setq data (nreverse gnus-newsgroup-data))
4629         (setq threads gnus-newsgroup-threads))
4630       ;; We splice the new data into the data structure.
4631       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4632       ;;!!! then we want to insert at the beginning of the buffer.
4633       ;;!!! That happens to be true with Gnus now, but that may
4634       ;;!!! change in the future.  Perhaps.
4635       (gnus-data-enter-list
4636        (if line nil current) data (- (point) old-pos))
4637       (setq gnus-newsgroup-threads
4638             (nconc threads gnus-newsgroup-threads))
4639       (gnus-data-compute-positions))))
4640
4641 (defun gnus-number-to-header (number)
4642   "Return the header for article NUMBER."
4643   (let ((headers gnus-newsgroup-headers))
4644     (while (and headers
4645                 (not (= number (mail-header-number (car headers)))))
4646       (pop headers))
4647     (when headers
4648       (car headers))))
4649
4650 (defun gnus-parent-headers (in-headers &optional generation)
4651   "Return the headers of the GENERATIONeth parent of HEADERS."
4652   (unless generation
4653     (setq generation 1))
4654   (let ((parent t)
4655         (headers in-headers)
4656         references)
4657     (while (and parent
4658                 (not (zerop generation))
4659                 (setq references (mail-header-references headers)))
4660       (setq headers (if (and references
4661                              (setq parent (gnus-parent-id references)))
4662                         (car (gnus-id-to-thread parent))
4663                       nil))
4664       (decf generation))
4665     (and (not (eq headers in-headers))
4666          headers)))
4667
4668 (defun gnus-id-to-thread (id)
4669   "Return the (sub-)thread where ID appears."
4670   (gnus-gethash id gnus-newsgroup-dependencies))
4671
4672 (defun gnus-id-to-article (id)
4673   "Return the article number of ID."
4674   (let ((thread (gnus-id-to-thread id)))
4675     (when (and thread
4676                (car thread))
4677       (mail-header-number (car thread)))))
4678
4679 (defun gnus-id-to-header (id)
4680   "Return the article headers of ID."
4681   (car (gnus-id-to-thread id)))
4682
4683 (defun gnus-article-displayed-root-p (article)
4684   "Say whether ARTICLE is a root(ish) article."
4685   (let ((level (gnus-summary-thread-level article))
4686         (refs (mail-header-references  (gnus-summary-article-header article)))
4687         particle)
4688     (cond
4689      ((null level) nil)
4690      ((zerop level) t)
4691      ((null refs) t)
4692      ((null (gnus-parent-id refs)) t)
4693      ((and (= 1 level)
4694            (null (setq particle (gnus-id-to-article
4695                                  (gnus-parent-id refs))))
4696            (null (gnus-summary-thread-level particle)))))))
4697
4698 (defun gnus-root-id (id)
4699   "Return the id of the root of the thread where ID appears."
4700   (let (last-id prev)
4701     (while (and id (setq prev (car (gnus-id-to-thread id))))
4702       (setq last-id id
4703             id (gnus-parent-id (mail-header-references prev))))
4704     last-id))
4705
4706 (defun gnus-articles-in-thread (thread)
4707   "Return the list of articles in THREAD."
4708   (cons (mail-header-number (car thread))
4709         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4710
4711 (defun gnus-remove-thread (id &optional dont-remove)
4712   "Remove the thread that has ID in it."
4713   (let (headers thread last-id)
4714     ;; First go up in this thread until we find the root.
4715     (setq last-id (gnus-root-id id)
4716           headers (message-flatten-list (gnus-id-to-thread last-id)))
4717     ;; We have now found the real root of this thread.  It might have
4718     ;; been gathered into some loose thread, so we have to search
4719     ;; through the threads to find the thread we wanted.
4720     (let ((threads gnus-newsgroup-threads)
4721           sub)
4722       (while threads
4723         (setq sub (car threads))
4724         (if (stringp (car sub))
4725             ;; This is a gathered thread, so we look at the roots
4726             ;; below it to find whether this article is in this
4727             ;; gathered root.
4728             (progn
4729               (setq sub (cdr sub))
4730               (while sub
4731                 (when (member (caar sub) headers)
4732                   (setq thread (car threads)
4733                         threads nil
4734                         sub nil))
4735                 (setq sub (cdr sub))))
4736           ;; It's an ordinary thread, so we check it.
4737           (when (eq (car sub) (car headers))
4738             (setq thread sub
4739                   threads nil)))
4740         (setq threads (cdr threads)))
4741       ;; If this article is in no thread, then it's a root.
4742       (if thread
4743           (unless dont-remove
4744             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4745         (setq thread (gnus-id-to-thread last-id)))
4746       (when thread
4747         (prog1
4748             thread                      ; We return this thread.
4749           (unless dont-remove
4750             (if (stringp (car thread))
4751                 (progn
4752                   ;; If we use dummy roots, then we have to remove the
4753                   ;; dummy root as well.
4754                   (when (eq gnus-summary-make-false-root 'dummy)
4755                     ;; We go to the dummy root by going to
4756                     ;; the first sub-"thread", and then one line up.
4757                     (gnus-summary-goto-article
4758                      (mail-header-number (caadr thread)))
4759                     (forward-line -1)
4760                     (gnus-delete-line)
4761                     (gnus-data-compute-positions))
4762                   (setq thread (cdr thread))
4763                   (while thread
4764                     (gnus-remove-thread-1 (car thread))
4765                     (setq thread (cdr thread))))
4766               (gnus-remove-thread-1 thread))))))))
4767
4768 (defun gnus-remove-thread-1 (thread)
4769   "Remove the thread THREAD recursively."
4770   (let ((number (mail-header-number (pop thread)))
4771         d)
4772     (setq thread (reverse thread))
4773     (while thread
4774       (gnus-remove-thread-1 (pop thread)))
4775     (when (setq d (gnus-data-find number))
4776       (goto-char (gnus-data-pos d))
4777       (gnus-summary-show-thread)
4778       (gnus-data-remove
4779        number
4780        (- (point-at-bol)
4781           (prog1
4782               (1+ (point-at-eol))
4783             (gnus-delete-line)))))))
4784
4785 (defun gnus-sort-threads-recursive (threads func)
4786   (sort (mapcar (lambda (thread)
4787                   (cons (car thread)
4788                         (and (cdr thread)
4789                              (gnus-sort-threads-recursive (cdr thread) func))))
4790                 threads) func))
4791
4792 (defun gnus-sort-threads-loop (threads func)
4793   (let* ((superthread (cons nil threads))
4794          (stack (list (cons superthread threads)))
4795          remaining-threads thread)
4796     (while stack
4797       (setq remaining-threads (cdr (car stack)))
4798       (if remaining-threads
4799           (progn (setq thread (car remaining-threads))
4800                  (setcdr (car stack) (cdr remaining-threads))
4801                  (if (cdr thread)
4802                      (push (cons thread (cdr thread)) stack)))
4803         (setq thread (caar stack))
4804         (setcdr thread (sort (cdr thread) func))
4805         (pop stack)))
4806     (cdr superthread)))
4807
4808 (defun gnus-sort-threads (threads)
4809   "Sort THREADS."
4810   (if (not gnus-thread-sort-functions)
4811       threads
4812     (gnus-message 8 "Sorting threads...")
4813     (prog1
4814         (condition-case nil
4815             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4816               (gnus-sort-threads-recursive
4817                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4818           ;; Even after binding max-lisp-eval-depth, the recursive
4819           ;; sorter might fail for very long threads.  In that case,
4820           ;; try using a (less well-tested) non-recursive sorter.
4821           (error (gnus-sort-threads-loop
4822                   threads (gnus-make-sort-function
4823                            gnus-thread-sort-functions))))
4824       (gnus-message 8 "Sorting threads...done"))))
4825
4826 (defun gnus-sort-articles (articles)
4827   "Sort ARTICLES."
4828   (when gnus-article-sort-functions
4829     (gnus-message 7 "Sorting articles...")
4830     (prog1
4831         (setq gnus-newsgroup-headers
4832               (sort articles (gnus-make-sort-function
4833                               gnus-article-sort-functions)))
4834       (gnus-message 7 "Sorting articles...done"))))
4835
4836 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4837 (defmacro gnus-thread-header (thread)
4838   "Return header of first article in THREAD.
4839 Note that THREAD must never, ever be anything else than a variable -
4840 using some other form will lead to serious barfage."
4841   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4842   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4843   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4844         (vector thread) 2))
4845
4846 (defsubst gnus-article-sort-by-number (h1 h2)
4847   "Sort articles by article number."
4848   (< (mail-header-number h1)
4849      (mail-header-number h2)))
4850
4851 (defun gnus-thread-sort-by-number (h1 h2)
4852   "Sort threads by root article number."
4853   (gnus-article-sort-by-number
4854    (gnus-thread-header h1) (gnus-thread-header h2)))
4855
4856 (defsubst gnus-article-sort-by-random (h1 h2)
4857   "Sort articles randomly."
4858   (zerop (random 2)))
4859
4860 (defun gnus-thread-sort-by-random (h1 h2)
4861   "Sort threads randomly."
4862   (gnus-article-sort-by-random
4863    (gnus-thread-header h1) (gnus-thread-header h2)))
4864
4865 (defsubst gnus-article-sort-by-lines (h1 h2)
4866   "Sort articles by article Lines header."
4867   (< (mail-header-lines h1)
4868      (mail-header-lines h2)))
4869
4870 (defun gnus-thread-sort-by-lines (h1 h2)
4871   "Sort threads by root article Lines header."
4872   (gnus-article-sort-by-lines
4873    (gnus-thread-header h1) (gnus-thread-header h2)))
4874
4875 (defsubst gnus-article-sort-by-chars (h1 h2)
4876   "Sort articles by octet length."
4877   (< (mail-header-chars h1)
4878      (mail-header-chars h2)))
4879
4880 (defun gnus-thread-sort-by-chars (h1 h2)
4881   "Sort threads by root article octet length."
4882   (gnus-article-sort-by-chars
4883    (gnus-thread-header h1) (gnus-thread-header h2)))
4884
4885 (defsubst gnus-article-sort-by-author (h1 h2)
4886   "Sort articles by root author."
4887   (gnus-string<
4888    (let ((extract (funcall
4889                    gnus-extract-address-components
4890                    (mail-header-from h1))))
4891      (or (car extract) (cadr extract) ""))
4892    (let ((extract (funcall
4893                    gnus-extract-address-components
4894                    (mail-header-from h2))))
4895      (or (car extract) (cadr extract) ""))))
4896
4897 (defun gnus-thread-sort-by-author (h1 h2)
4898   "Sort threads by root author."
4899   (gnus-article-sort-by-author
4900    (gnus-thread-header h1)  (gnus-thread-header h2)))
4901
4902 (defsubst gnus-article-sort-by-recipient (h1 h2)
4903   "Sort articles by recipient."
4904   (gnus-string<
4905    (let ((extract (funcall
4906                    gnus-extract-address-components
4907                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4908      (or (car extract) (cadr extract)))
4909    (let ((extract (funcall
4910                    gnus-extract-address-components
4911                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4912      (or (car extract) (cadr extract)))))
4913
4914 (defun gnus-thread-sort-by-recipient (h1 h2)
4915   "Sort threads by root recipient."
4916   (gnus-article-sort-by-recipient
4917    (gnus-thread-header h1) (gnus-thread-header h2)))
4918
4919 (defsubst gnus-article-sort-by-subject (h1 h2)
4920   "Sort articles by root subject."
4921   (gnus-string<
4922    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4923    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4924
4925 (defun gnus-thread-sort-by-subject (h1 h2)
4926   "Sort threads by root subject."
4927   (gnus-article-sort-by-subject
4928    (gnus-thread-header h1) (gnus-thread-header h2)))
4929
4930 (defsubst gnus-article-sort-by-date (h1 h2)
4931   "Sort articles by root article date."
4932   (time-less-p
4933    (gnus-date-get-time (mail-header-date h1))
4934    (gnus-date-get-time (mail-header-date h2))))
4935
4936 (defun gnus-thread-sort-by-date (h1 h2)
4937   "Sort threads by root article date."
4938   (gnus-article-sort-by-date
4939    (gnus-thread-header h1) (gnus-thread-header h2)))
4940
4941 (defsubst gnus-article-sort-by-score (h1 h2)
4942   "Sort articles by root article score.
4943 Unscored articles will be counted as having a score of zero."
4944   (> (or (cdr (assq (mail-header-number h1)
4945                     gnus-newsgroup-scored))
4946          gnus-summary-default-score 0)
4947      (or (cdr (assq (mail-header-number h2)
4948                     gnus-newsgroup-scored))
4949          gnus-summary-default-score 0)))
4950
4951 (defun gnus-thread-sort-by-score (h1 h2)
4952   "Sort threads by root article score."
4953   (gnus-article-sort-by-score
4954    (gnus-thread-header h1) (gnus-thread-header h2)))
4955
4956 (defun gnus-thread-sort-by-total-score (h1 h2)
4957   "Sort threads by the sum of all scores in the thread.
4958 Unscored articles will be counted as having a score of zero."
4959   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4960
4961 (defun gnus-thread-total-score (thread)
4962   ;; This function find the total score of THREAD.
4963   (cond
4964    ((null thread)
4965     0)
4966    ((consp thread)
4967     (if (stringp (car thread))
4968         (apply gnus-thread-score-function 0
4969                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4970       (gnus-thread-total-score-1 thread)))
4971    (t
4972     (gnus-thread-total-score-1 (list thread)))))
4973
4974 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4975   "Sort threads such that the thread with the most recently arrived article comes first."
4976   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4977
4978 (defun gnus-thread-highest-number (thread)
4979   "Return the highest article number in THREAD."
4980   (apply 'max (mapcar (lambda (header)
4981                         (mail-header-number header))
4982                       (message-flatten-list thread))))
4983
4984 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4985   "Sort threads such that the thread with the most recently dated article comes first."
4986   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4987
4988 (defun gnus-thread-latest-date (thread)
4989   "Return the highest article date in THREAD."
4990   (let ((previous-time 0))
4991     (apply 'max
4992            (mapcar
4993             (lambda (header)
4994               (setq previous-time
4995                     (condition-case ()
4996                         (time-to-seconds (mail-header-parse-date
4997                                           (mail-header-date header)))
4998                       (error previous-time))))
4999             (sort
5000              (message-flatten-list thread)
5001              (lambda (h1 h2)
5002                (< (mail-header-number h1)
5003                   (mail-header-number h2))))))))
5004
5005 (defun gnus-thread-total-score-1 (root)
5006   ;; This function find the total score of the thread below ROOT.
5007   (setq root (car root))
5008   (apply gnus-thread-score-function
5009          (or (append
5010               (mapcar 'gnus-thread-total-score
5011                       (cdr (gnus-id-to-thread (mail-header-id root))))
5012               (when (> (mail-header-number root) 0)
5013                 (list (or (cdr (assq (mail-header-number root)
5014                                      gnus-newsgroup-scored))
5015                           gnus-summary-default-score 0))))
5016              (list gnus-summary-default-score)
5017              '(0))))
5018
5019 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5020 (defvar gnus-tmp-prev-subject nil)
5021 (defvar gnus-tmp-false-parent nil)
5022 (defvar gnus-tmp-root-expunged nil)
5023 (defvar gnus-tmp-dummy-line nil)
5024
5025 (defun gnus-extra-header (type &optional header)
5026   "Return the extra header of TYPE."
5027   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5028       ""))
5029
5030 (defvar gnus-tmp-thread-tree-header-string "")
5031
5032 (defcustom gnus-sum-thread-tree-root "> "
5033   "With %B spec, used for the root of a thread.
5034 If nil, use subject instead."
5035   :version "22.1"
5036   :type '(radio (const :format "%v  " nil) string)
5037   :group 'gnus-thread)
5038
5039 (defcustom gnus-sum-thread-tree-false-root "> "
5040   "With %B spec, used for a false root of a thread.
5041 If nil, use subject instead."
5042   :version "22.1"
5043   :type '(radio (const :format "%v  " nil) string)
5044   :group 'gnus-thread)
5045
5046 (defcustom gnus-sum-thread-tree-single-indent ""
5047   "With %B spec, used for a thread with just one message.
5048 If nil, use subject instead."
5049   :version "22.1"
5050   :type '(radio (const :format "%v  " nil) string)
5051   :group 'gnus-thread)
5052
5053 (defcustom gnus-sum-thread-tree-vertical "| "
5054   "With %B spec, used for drawing a vertical line."
5055   :version "22.1"
5056   :type 'string
5057   :group 'gnus-thread)
5058
5059 (defcustom gnus-sum-thread-tree-indent "  "
5060   "With %B spec, used for indenting."
5061   :version "22.1"
5062   :type 'string
5063   :group 'gnus-thread)
5064
5065 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5066   "With %B spec, used for a leaf with brothers."
5067   :version "22.1"
5068   :type 'string
5069   :group 'gnus-thread)
5070
5071 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5072   "With %B spec, used for a leaf without brothers."
5073   :version "22.1"
5074   :type 'string
5075   :group 'gnus-thread)
5076
5077 (defcustom gnus-summary-display-while-building nil
5078   "If non-nil, show and update the summary buffer as it's being built.
5079 If the value is t, update the buffer after every line is inserted.  If
5080 the value is an integer (N), update the display every N lines."
5081   :version "22.1"
5082   :group 'gnus-thread
5083   :type '(choice (const :tag "off" nil)
5084                  number
5085                  (const :tag "frequently" t)))
5086
5087 (defun gnus-summary-prepare-threads (threads)
5088   "Prepare summary buffer from THREADS and indentation LEVEL.
5089 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5090 or a straight list of headers."
5091   (gnus-message 7 "Generating summary...")
5092
5093   (setq gnus-newsgroup-threads threads)
5094   (beginning-of-line)
5095
5096   (let ((gnus-tmp-level 0)
5097         (default-score (or gnus-summary-default-score 0))
5098         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5099         (building-line-count gnus-summary-display-while-building)
5100         (building-count (integerp gnus-summary-display-while-building))
5101         thread number subject stack state gnus-tmp-gathered beg-match
5102         new-roots gnus-tmp-new-adopts thread-end simp-subject
5103         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5104         gnus-tmp-replied gnus-tmp-subject-or-nil
5105         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5106         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5107         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5108         tree-stack)
5109
5110     (setq gnus-tmp-prev-subject nil
5111           gnus-tmp-thread-tree-header-string "")
5112
5113     (if (vectorp (car threads))
5114         ;; If this is a straight (sic) list of headers, then a
5115         ;; threaded summary display isn't required, so we just create
5116         ;; an unthreaded one.
5117         (gnus-summary-prepare-unthreaded threads)
5118
5119       ;; Do the threaded display.
5120
5121       (if gnus-summary-display-while-building
5122           (switch-to-buffer (buffer-name)))
5123       (while (or threads stack gnus-tmp-new-adopts new-roots)
5124
5125         (if (and (= gnus-tmp-level 0)
5126                  (or (not stack)
5127                      (= (caar stack) 0))
5128                  (not gnus-tmp-false-parent)
5129                  (or gnus-tmp-new-adopts new-roots))
5130             (if gnus-tmp-new-adopts
5131                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5132                       thread (list (car gnus-tmp-new-adopts))
5133                       gnus-tmp-header (caar thread)
5134                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5135               (when new-roots
5136                 (setq thread (list (car new-roots))
5137                       gnus-tmp-header (caar thread)
5138                       new-roots (cdr new-roots))))
5139
5140           (if threads
5141               ;; If there are some threads, we do them before the
5142               ;; threads on the stack.
5143               (setq thread threads
5144                     gnus-tmp-header (caar thread))
5145             ;; There were no current threads, so we pop something off
5146             ;; the stack.
5147             (setq state (car stack)
5148                   gnus-tmp-level (car state)
5149                   tree-stack (cadr state)
5150                   thread (caddr state)
5151                   stack (cdr stack)
5152                   gnus-tmp-header (caar thread))))
5153
5154         (setq gnus-tmp-false-parent nil)
5155         (setq gnus-tmp-root-expunged nil)
5156         (setq thread-end nil)
5157
5158         (if (stringp gnus-tmp-header)
5159             ;; The header is a dummy root.
5160             (cond
5161              ((eq gnus-summary-make-false-root 'adopt)
5162               ;; We let the first article adopt the rest.
5163               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5164                                                (cddar thread)))
5165               (setq gnus-tmp-gathered
5166                     (nconc (mapcar
5167                             (lambda (h) (mail-header-number (car h)))
5168                             (cddar thread))
5169                            gnus-tmp-gathered))
5170               (setq thread (cons (list (caar thread)
5171                                        (cadar thread))
5172                                  (cdr thread)))
5173               (setq gnus-tmp-level -1
5174                     gnus-tmp-false-parent t))
5175              ((eq gnus-summary-make-false-root 'empty)
5176               ;; We print adopted articles with empty subject fields.
5177               (setq gnus-tmp-gathered
5178                     (nconc (mapcar
5179                             (lambda (h) (mail-header-number (car h)))
5180                             (cddar thread))
5181                            gnus-tmp-gathered))
5182               (setq gnus-tmp-level -1))
5183              ((eq gnus-summary-make-false-root 'dummy)
5184               ;; We remember that we probably want to output a dummy
5185               ;; root.
5186               (setq gnus-tmp-dummy-line gnus-tmp-header)
5187               (setq gnus-tmp-prev-subject gnus-tmp-header))
5188              (t
5189               ;; We do not make a root for the gathered
5190               ;; sub-threads at all.
5191               (setq gnus-tmp-level -1)))
5192
5193           (setq number (mail-header-number gnus-tmp-header)
5194                 subject (mail-header-subject gnus-tmp-header)
5195                 simp-subject (gnus-simplify-subject-fully subject))
5196
5197           (cond
5198            ;; If the thread has changed subject, we might want to make
5199            ;; this subthread into a root.
5200            ((and (null gnus-thread-ignore-subject)
5201                  (not (zerop gnus-tmp-level))
5202                  gnus-tmp-prev-subject
5203                  (not (string= gnus-tmp-prev-subject simp-subject)))
5204             (setq new-roots (nconc new-roots (list (car thread)))
5205                   thread-end t
5206                   gnus-tmp-header nil))
5207            ;; If the article lies outside the current limit,
5208            ;; then we do not display it.
5209            ((not (memq number gnus-newsgroup-limit))
5210             (setq gnus-tmp-gathered
5211                   (nconc (mapcar
5212                           (lambda (h) (mail-header-number (car h)))
5213                           (cdar thread))
5214                          gnus-tmp-gathered))
5215             (setq gnus-tmp-new-adopts (if (cdar thread)
5216                                           (append gnus-tmp-new-adopts
5217                                                   (cdar thread))
5218                                         gnus-tmp-new-adopts)
5219                   thread-end t
5220                   gnus-tmp-header nil)
5221             (when (zerop gnus-tmp-level)
5222               (setq gnus-tmp-root-expunged t)))
5223            ;; Perhaps this article is to be marked as read?
5224            ((and gnus-summary-mark-below
5225                  (< (or (cdr (assq number gnus-newsgroup-scored))
5226                         default-score)
5227                     gnus-summary-mark-below)
5228                  ;; Don't touch sparse articles.
5229                  (not (gnus-summary-article-sparse-p number))
5230                  (not (gnus-summary-article-ancient-p number)))
5231             (setq gnus-newsgroup-unreads
5232                   (delq number gnus-newsgroup-unreads))
5233             (if gnus-newsgroup-auto-expire
5234                 (setq gnus-newsgroup-expirable
5235                       (gnus-add-to-sorted-list
5236                        gnus-newsgroup-expirable number))
5237               (push (cons number gnus-low-score-mark)
5238                     gnus-newsgroup-reads))))
5239
5240           (when gnus-tmp-header
5241             ;; We may have an old dummy line to output before this
5242             ;; article.
5243             (when (and gnus-tmp-dummy-line
5244                        (gnus-subject-equal
5245                         gnus-tmp-dummy-line
5246                         (mail-header-subject gnus-tmp-header)))
5247               (gnus-summary-insert-dummy-line
5248                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5249               (setq gnus-tmp-dummy-line nil))
5250
5251             ;; Compute the mark.
5252             (setq gnus-tmp-unread (gnus-article-mark number))
5253
5254             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5255                                   gnus-tmp-header gnus-tmp-level)
5256                   gnus-newsgroup-data)
5257
5258             ;; Actually insert the line.
5259             (setq
5260              gnus-tmp-subject-or-nil
5261              (cond
5262               ((and gnus-thread-ignore-subject
5263                     gnus-tmp-prev-subject
5264                     (not (string= gnus-tmp-prev-subject simp-subject)))
5265                subject)
5266               ((zerop gnus-tmp-level)
5267                (if (and (eq gnus-summary-make-false-root 'empty)
5268                         (memq number gnus-tmp-gathered)
5269                         gnus-tmp-prev-subject
5270                         (string= gnus-tmp-prev-subject simp-subject))
5271                    gnus-summary-same-subject
5272                  subject))
5273               (t gnus-summary-same-subject)))
5274             (if (and (eq gnus-summary-make-false-root 'adopt)
5275                      (= gnus-tmp-level 1)
5276                      (memq number gnus-tmp-gathered))
5277                 (setq gnus-tmp-opening-bracket ?\<
5278                       gnus-tmp-closing-bracket ?\>)
5279               (setq gnus-tmp-opening-bracket ?\[
5280                     gnus-tmp-closing-bracket ?\]))
5281             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5282                 (gnus-make-thread-indent-array
5283                  (max (* 2 (length gnus-thread-indent-array))
5284                       gnus-tmp-level)))
5285             (setq
5286              gnus-tmp-indentation
5287              (aref gnus-thread-indent-array gnus-tmp-level)
5288              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5289              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5290                                 gnus-summary-default-score 0)
5291              gnus-tmp-score-char
5292              (if (or (null gnus-summary-default-score)
5293                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5294                          gnus-summary-zcore-fuzz))
5295                  ?                      ;Whitespace
5296                (if (< gnus-tmp-score gnus-summary-default-score)
5297                    gnus-score-below-mark gnus-score-over-mark))
5298              gnus-tmp-replied
5299              (cond ((memq number gnus-newsgroup-processable)
5300                     gnus-process-mark)
5301                    ((memq number gnus-newsgroup-cached)
5302                     gnus-cached-mark)
5303                    ((memq number gnus-newsgroup-replied)
5304                     gnus-replied-mark)
5305                    ((memq number gnus-newsgroup-forwarded)
5306                     gnus-forwarded-mark)
5307                    ((memq number gnus-newsgroup-saved)
5308                     gnus-saved-mark)
5309                    ((memq number gnus-newsgroup-recent)
5310                     gnus-recent-mark)
5311                    ((memq number gnus-newsgroup-unseen)
5312                     gnus-unseen-mark)
5313                    (t gnus-no-mark))
5314              gnus-tmp-downloaded
5315              (cond ((memq number gnus-newsgroup-undownloaded)
5316                     gnus-undownloaded-mark)
5317                    (gnus-newsgroup-agentized
5318                     gnus-downloaded-mark)
5319                    (t
5320                     gnus-no-mark))
5321              gnus-tmp-from (mail-header-from gnus-tmp-header)
5322              gnus-tmp-name
5323              (cond
5324               ((string-match "<[^>]+> *$" gnus-tmp-from)
5325                (setq beg-match (match-beginning 0))
5326                (or (and (string-match "^\".+\"" gnus-tmp-from)
5327                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5328                    (substring gnus-tmp-from 0 beg-match)))
5329               ((string-match "(.+)" gnus-tmp-from)
5330                (substring gnus-tmp-from
5331                           (1+ (match-beginning 0)) (1- (match-end 0))))
5332               (t gnus-tmp-from))
5333
5334              ;; Do the %B string
5335              gnus-tmp-thread-tree-header-string
5336              (cond
5337               ((not gnus-show-threads) "")
5338               ((zerop gnus-tmp-level)
5339                (cond ((cdar thread)
5340                       (or gnus-sum-thread-tree-root subject))
5341                      (gnus-tmp-new-adopts
5342                       (or gnus-sum-thread-tree-false-root subject))
5343                      (t
5344                       (or gnus-sum-thread-tree-single-indent subject))))
5345               (t
5346                (concat (apply 'concat
5347                               (mapcar (lambda (item)
5348                                         (if (= item 1)
5349                                             gnus-sum-thread-tree-vertical
5350                                           gnus-sum-thread-tree-indent))
5351                                       (cdr (reverse tree-stack))))
5352                        (if (nth 1 thread)
5353                            gnus-sum-thread-tree-leaf-with-other
5354                          gnus-sum-thread-tree-single-leaf)))))
5355             (when (string= gnus-tmp-name "")
5356               (setq gnus-tmp-name gnus-tmp-from))
5357             (unless (numberp gnus-tmp-lines)
5358               (setq gnus-tmp-lines -1))
5359             (if (= gnus-tmp-lines -1)
5360                 (setq gnus-tmp-lines "?")
5361               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5362               (gnus-put-text-property
5363                (point)
5364                (progn (eval gnus-summary-line-format-spec) (point))
5365                'gnus-number number)
5366               (when gnus-visual-p
5367                 (forward-line -1)
5368                 (gnus-run-hooks 'gnus-summary-update-hook)
5369                 (forward-line 1))
5370
5371               (setq gnus-tmp-prev-subject simp-subject)))
5372
5373         (when (nth 1 thread)
5374           (push (list (max 0 gnus-tmp-level)
5375                       (copy-sequence tree-stack)
5376                       (nthcdr 1 thread))
5377                 stack))
5378         (push (if (nth 1 thread) 1 0) tree-stack)
5379         (incf gnus-tmp-level)
5380         (setq threads (if thread-end nil (cdar thread)))
5381         (if gnus-summary-display-while-building
5382             (if building-count
5383                 (progn
5384                   ;; use a set frequency
5385                   (setq building-line-count (1- building-line-count))
5386                   (when (= building-line-count 0)
5387                     (sit-for 0)
5388                     (setq building-line-count
5389                           gnus-summary-display-while-building)))
5390               ;; always
5391               (sit-for 0)))
5392         (unless threads
5393           (setq gnus-tmp-level 0)))))
5394   (gnus-message 7 "Generating summary...done"))
5395
5396 (defun gnus-summary-prepare-unthreaded (headers)
5397   "Generate an unthreaded summary buffer based on HEADERS."
5398   (let (header number mark)
5399
5400     (beginning-of-line)
5401
5402     (while headers
5403       ;; We may have to root out some bad articles...
5404       (when (memq (setq number (mail-header-number
5405                                 (setq header (pop headers))))
5406                   gnus-newsgroup-limit)
5407         ;; Mark article as read when it has a low score.
5408         (when (and gnus-summary-mark-below
5409                    (< (or (cdr (assq number gnus-newsgroup-scored))
5410                           gnus-summary-default-score 0)
5411                       gnus-summary-mark-below)
5412                    (not (gnus-summary-article-ancient-p number)))
5413           (setq gnus-newsgroup-unreads
5414                 (delq number gnus-newsgroup-unreads))
5415           (if gnus-newsgroup-auto-expire
5416               (push number gnus-newsgroup-expirable)
5417             (push (cons number gnus-low-score-mark)
5418                   gnus-newsgroup-reads)))
5419
5420         (setq mark (gnus-article-mark number))
5421         (push (gnus-data-make number mark (1+ (point)) header 0)
5422               gnus-newsgroup-data)
5423         (gnus-summary-insert-line
5424          header 0 number
5425          (memq number gnus-newsgroup-undownloaded)
5426          mark (memq number gnus-newsgroup-replied)
5427          (memq number gnus-newsgroup-expirable)
5428          (mail-header-subject header) nil
5429          (cdr (assq number gnus-newsgroup-scored))
5430          (memq number gnus-newsgroup-processable))))))
5431
5432 (defun gnus-summary-remove-list-identifiers ()
5433   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5434   (let ((regexp (if (consp gnus-list-identifiers)
5435                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5436                   gnus-list-identifiers))
5437         changed subject)
5438     (when regexp
5439       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5440       (dolist (header gnus-newsgroup-headers)
5441         (setq subject (mail-header-subject header)
5442               changed nil)
5443         (while (string-match regexp subject)
5444           (setq subject
5445                 (concat (substring subject 0 (match-beginning 1))
5446                         (substring subject (match-end 0)))
5447                 changed t))
5448         (when changed
5449           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5450             (setq subject
5451                   (concat (substring subject 0 (match-beginning 1))
5452                           (substring subject (match-end 1)))))
5453           (mail-header-set-subject header subject))))))
5454
5455 (defun gnus-fetch-headers (articles)
5456   "Fetch headers of ARTICLES."
5457   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5458     (gnus-message 5 "Fetching headers for %s..." name)
5459     (prog1
5460         (if (eq 'nov
5461                 (setq gnus-headers-retrieved-by
5462                       (gnus-retrieve-headers
5463                        articles gnus-newsgroup-name
5464                        ;; We might want to fetch old headers, but
5465                        ;; not if there is only 1 article.
5466                        (and (or (and
5467                                  (not (eq gnus-fetch-old-headers 'some))
5468                                  (not (numberp gnus-fetch-old-headers)))
5469                                 (> (length articles) 1))
5470                             gnus-fetch-old-headers))))
5471             (gnus-get-newsgroup-headers-xover
5472              articles nil nil gnus-newsgroup-name t)
5473           (gnus-get-newsgroup-headers))
5474       (gnus-message 5 "Fetching headers for %s...done" name))))
5475
5476 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5477   "Select newsgroup GROUP.
5478 If READ-ALL is non-nil, all articles in the group are selected.
5479 If SELECT-ARTICLES, only select those articles from GROUP."
5480   (let* ((entry (gnus-group-entry group))
5481          ;;!!! Dirty hack; should be removed.
5482          (gnus-summary-ignore-duplicates
5483           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5484               t
5485             gnus-summary-ignore-duplicates))
5486          (info (nth 2 entry))
5487          charset articles fetched-articles cached)
5488
5489     (unless (gnus-check-server
5490              (set (make-local-variable 'gnus-current-select-method)
5491                   (gnus-find-method-for-group group)))
5492       (error "Couldn't open server"))
5493     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5494
5495     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5496         (gnus-activate-group group)     ; Or we can activate it...
5497         (progn                          ; Or we bug out.
5498           (when (equal major-mode 'gnus-summary-mode)
5499             (gnus-kill-buffer (current-buffer)))
5500           (error
5501            "Couldn't activate group %s: %s"
5502            (mm-decode-coding-string group charset)
5503            (mm-decode-coding-string (gnus-status-message group) charset))))
5504
5505     (unless (gnus-request-group group t)
5506         (when (equal major-mode 'gnus-summary-mode)
5507           (gnus-kill-buffer (current-buffer)))
5508         (error "Couldn't request group %s: %s"
5509                (mm-decode-coding-string group charset)
5510                (mm-decode-coding-string (gnus-status-message group) charset)))
5511
5512     (when gnus-agent
5513       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5514
5515       (setq gnus-summary-use-undownloaded-faces
5516             (gnus-agent-find-parameter
5517              group
5518              'agent-enable-undownloaded-faces)))
5519
5520     (setq gnus-newsgroup-name group
5521           gnus-newsgroup-unselected nil
5522           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5523
5524     (let ((display (gnus-group-find-parameter group 'display)))
5525       (setq gnus-newsgroup-display
5526             (cond
5527              ((not (zerop (or (car-safe read-all) 0)))
5528               ;; The user entered the group with C-u SPC/RET, let's show
5529               ;; all articles.
5530               'gnus-not-ignore)
5531              ((eq display 'all)
5532               'gnus-not-ignore)
5533              ((arrayp display)
5534               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5535              ((numberp display)
5536               ;; The following is probably the "correct" solution, but
5537               ;; it makes Gnus fetch all headers and then limit the
5538               ;; articles (which is slow), so instead we hack the
5539               ;; select-articles parameter instead. -- Simon Josefsson
5540               ;; <jas@kth.se>
5541               ;;
5542               ;; (gnus-byte-compile
5543               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5544               ;;                         display)))))
5545               (setq select-articles
5546                     (gnus-uncompress-range
5547                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5548                              (if (> tmp 0)
5549                                  tmp
5550                                1))
5551                            (cdr (gnus-active group)))))
5552               nil)
5553              (t
5554               nil))))
5555
5556     (gnus-summary-setup-default-charset)
5557
5558     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5559     (when (gnus-virtual-group-p group)
5560       (setq cached gnus-newsgroup-cached))
5561
5562     (setq gnus-newsgroup-unreads
5563           (gnus-sorted-ndifference
5564            (gnus-sorted-ndifference gnus-newsgroup-unreads
5565                                     gnus-newsgroup-marked)
5566            gnus-newsgroup-dormant))
5567
5568     (setq gnus-newsgroup-processable nil)
5569
5570     (gnus-update-read-articles group gnus-newsgroup-unreads)
5571
5572     ;; Adjust and set lists of article marks.
5573     (when info
5574       (gnus-adjust-marked-articles info))
5575     (if (setq articles select-articles)
5576         (setq gnus-newsgroup-unselected
5577               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5578       (setq articles (gnus-articles-to-read group read-all)))
5579
5580     (cond
5581      ((null articles)
5582       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5583       'quit)
5584      ((eq articles 0) nil)
5585      (t
5586       ;; Init the dependencies hash table.
5587       (setq gnus-newsgroup-dependencies
5588             (gnus-make-hashtable (length articles)))
5589       (gnus-set-global-variables)
5590       ;; Retrieve the headers and read them in.
5591
5592       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5593
5594       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5595       (when cached
5596         (setq gnus-newsgroup-cached cached))
5597
5598       ;; Suppress duplicates?
5599       (when gnus-suppress-duplicates
5600         (gnus-dup-suppress-articles))
5601
5602       ;; Set the initial limit.
5603       (setq gnus-newsgroup-limit (copy-sequence articles))
5604       ;; Remove canceled articles from the list of unread articles.
5605       (setq fetched-articles
5606             (mapcar (lambda (headers) (mail-header-number headers))
5607                     gnus-newsgroup-headers))
5608       (setq gnus-newsgroup-articles fetched-articles)
5609       (setq gnus-newsgroup-unreads
5610             (gnus-sorted-nintersection
5611              gnus-newsgroup-unreads fetched-articles))
5612       (gnus-compute-unseen-list)
5613
5614       ;; Removed marked articles that do not exist.
5615       (gnus-update-missing-marks
5616        (gnus-sorted-difference articles fetched-articles))
5617       ;; We might want to build some more threads first.
5618       (when (and gnus-fetch-old-headers
5619                  (eq gnus-headers-retrieved-by 'nov))
5620         (if (eq gnus-fetch-old-headers 'invisible)
5621             (gnus-build-all-threads)
5622           (gnus-build-old-threads)))
5623       ;; Let the Gnus agent mark articles as read.
5624       (when gnus-agent
5625         (gnus-agent-get-undownloaded-list))
5626       ;; Remove list identifiers from subject
5627       (when gnus-list-identifiers
5628         (gnus-summary-remove-list-identifiers))
5629       ;; Check whether auto-expire is to be done in this group.
5630       (setq gnus-newsgroup-auto-expire
5631             (gnus-group-auto-expirable-p group))
5632       ;; Set up the article buffer now, if necessary.
5633       (unless (and gnus-single-article-buffer
5634                    (equal gnus-article-buffer "*Article*"))
5635         (gnus-article-setup-buffer))
5636       ;; First and last article in this newsgroup.
5637       (when gnus-newsgroup-headers
5638         (setq gnus-newsgroup-begin
5639               (mail-header-number (car gnus-newsgroup-headers))
5640               gnus-newsgroup-end
5641               (mail-header-number
5642                (gnus-last-element gnus-newsgroup-headers))))
5643       ;; GROUP is successfully selected.
5644       (or gnus-newsgroup-headers t)))))
5645
5646 (defun gnus-compute-unseen-list ()
5647   ;; The `seen' marks are treated specially.
5648   (if (not gnus-newsgroup-seen)
5649       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5650     (setq gnus-newsgroup-unseen
5651           (gnus-inverse-list-range-intersection
5652            gnus-newsgroup-articles gnus-newsgroup-seen))))
5653
5654 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5655
5656 (defun gnus-summary-display-make-predicate (display)
5657   (require 'gnus-agent)
5658   (when (= (length display) 1)
5659     (setq display (car display)))
5660   (unless gnus-summary-display-cache
5661     (dolist (elem (append '((unread . unread)
5662                             (read . read)
5663                             (unseen . unseen))
5664                           gnus-article-mark-lists))
5665       (push (cons (cdr elem)
5666                   (gnus-byte-compile
5667                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5668             gnus-summary-display-cache)))
5669   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5670         (gnus-category-predicate-cache gnus-summary-display-cache))
5671     (gnus-get-predicate display)))
5672
5673 ;; Uses the dynamically bound `number' variable.
5674 (defvar number)
5675 (defun gnus-article-marked-p (type &optional article)
5676   (let ((article (or article number)))
5677     (cond
5678      ((eq type 'tick)
5679       (memq article gnus-newsgroup-marked))
5680      ((eq type 'spam)
5681       (memq article gnus-newsgroup-spam-marked))
5682      ((eq type 'unsend)
5683       (memq article gnus-newsgroup-unsendable))
5684      ((eq type 'undownload)
5685       (memq article gnus-newsgroup-undownloaded))
5686      ((eq type 'download)
5687       (memq article gnus-newsgroup-downloadable))
5688      ((eq type 'unread)
5689       (memq article gnus-newsgroup-unreads))
5690      ((eq type 'read)
5691       (memq article gnus-newsgroup-reads))
5692      ((eq type 'dormant)
5693       (memq article gnus-newsgroup-dormant) )
5694      ((eq type 'expire)
5695       (memq article gnus-newsgroup-expirable))
5696      ((eq type 'reply)
5697       (memq article gnus-newsgroup-replied))
5698      ((eq type 'killed)
5699       (memq article gnus-newsgroup-killed))
5700      ((eq type 'bookmark)
5701       (assq article gnus-newsgroup-bookmarks))
5702      ((eq type 'score)
5703       (assq article gnus-newsgroup-scored))
5704      ((eq type 'save)
5705       (memq article gnus-newsgroup-saved))
5706      ((eq type 'cache)
5707       (memq article gnus-newsgroup-cached))
5708      ((eq type 'forward)
5709       (memq article gnus-newsgroup-forwarded))
5710      ((eq type 'seen)
5711       (not (memq article gnus-newsgroup-unseen)))
5712      ((eq type 'recent)
5713       (memq article gnus-newsgroup-recent))
5714      (t t))))
5715
5716 (defun gnus-articles-to-read (group &optional read-all)
5717   "Find out what articles the user wants to read."
5718   (let* ((articles
5719           ;; Select all articles if `read-all' is non-nil, or if there
5720           ;; are no unread articles.
5721           (if (or read-all
5722                   (and (zerop (length gnus-newsgroup-marked))
5723                        (zerop (length gnus-newsgroup-unreads)))
5724                   ;; Fetch all if the predicate is non-nil.
5725                   gnus-newsgroup-display)
5726               ;; We want to select the headers for all the articles in
5727               ;; the group, so we select either all the active
5728               ;; articles in the group, or (if that's nil), the
5729               ;; articles in the cache.
5730               (or
5731                (if gnus-newsgroup-maximum-articles
5732                    (let ((active (gnus-active group)))
5733                      (gnus-uncompress-range
5734                       (cons (max (car active)
5735                                  (- (cdr active)
5736                                     gnus-newsgroup-maximum-articles
5737                                     -1))
5738                             (cdr active))))
5739                  (gnus-uncompress-range (gnus-active group)))
5740                (gnus-cache-articles-in-group group))
5741             ;; Select only the "normal" subset of articles.
5742             (gnus-sorted-nunion
5743              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5744              gnus-newsgroup-unreads)))
5745          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5746          (scored (length scored-list))
5747          (number (length articles))
5748          (marked (+ (length gnus-newsgroup-marked)
5749                     (length gnus-newsgroup-dormant)))
5750          (select
5751           (cond
5752            ((numberp read-all)
5753             read-all)
5754            ((numberp gnus-newsgroup-display)
5755             gnus-newsgroup-display)
5756            (t
5757             (condition-case ()
5758                 (cond
5759                  ((and (or (<= scored marked) (= scored number))
5760                        (numberp gnus-large-newsgroup)
5761                        (> number gnus-large-newsgroup))
5762                   (let* ((cursor-in-echo-area nil)
5763                          (initial (gnus-parameter-large-newsgroup-initial
5764                                    gnus-newsgroup-name))
5765                          (input
5766                           (read-string
5767                            (format
5768                             "How many articles from %s (%s %d): "
5769                             (gnus-group-decoded-name gnus-newsgroup-name)
5770                             (if initial "max" "default")
5771                             number)
5772                            (if initial
5773                                (cons (number-to-string initial)
5774                                      0)))))
5775                     (if (string-match "^[ \t]*$" input) number input)))
5776                  ((and (> scored marked) (< scored number)
5777                        (> (- scored number) 20))
5778                   (let ((input
5779                          (read-string
5780                           (format "%s %s (%d scored, %d total): "
5781                                   "How many articles from"
5782                                   (gnus-group-decoded-name group)
5783                                   scored number))))
5784                     (if (string-match "^[ \t]*$" input)
5785                         number input)))
5786                  (t number))
5787               (quit
5788                (message "Quit getting the articles to read")
5789                nil))))))
5790     (setq select (if (stringp select) (string-to-number select) select))
5791     (if (or (null select) (zerop select))
5792         select
5793       (if (and (not (zerop scored)) (<= (abs select) scored))
5794           (progn
5795             (setq articles (sort scored-list '<))
5796             (setq number (length articles)))
5797         (setq articles (copy-sequence articles)))
5798
5799       (when (< (abs select) number)
5800         (if (< select 0)
5801             ;; Select the N oldest articles.
5802             (setcdr (nthcdr (1- (abs select)) articles) nil)
5803           ;; Select the N most recent articles.
5804           (setq articles (nthcdr (- number select) articles))))
5805       (setq gnus-newsgroup-unselected
5806             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5807       (when gnus-alter-articles-to-read-function
5808         (setq articles
5809               (sort
5810                (funcall gnus-alter-articles-to-read-function
5811                         gnus-newsgroup-name articles)
5812                '<)))
5813       articles)))
5814
5815 (defun gnus-killed-articles (killed articles)
5816   (let (out)
5817     (while articles
5818       (when (inline (gnus-member-of-range (car articles) killed))
5819         (push (car articles) out))
5820       (setq articles (cdr articles)))
5821     out))
5822
5823 (defun gnus-uncompress-marks (marks)
5824   "Uncompress the mark ranges in MARKS."
5825   (let ((uncompressed '(score bookmark))
5826         out)
5827     (while marks
5828       (if (memq (caar marks) uncompressed)
5829           (push (car marks) out)
5830         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5831       (setq marks (cdr marks)))
5832     out))
5833
5834 (defun gnus-article-mark-to-type (mark)
5835   "Return the type of MARK."
5836   (or (cadr (assq mark gnus-article-special-mark-lists))
5837       'list))
5838
5839 (defun gnus-article-unpropagatable-p (mark)
5840   "Return whether MARK should be propagated to back end."
5841   (memq mark gnus-article-unpropagated-mark-lists))
5842
5843 (defun gnus-adjust-marked-articles (info)
5844   "Set all article lists and remove all marks that are no longer valid."
5845   (let* ((marked-lists (gnus-info-marks info))
5846          (active (gnus-active (gnus-info-group info)))
5847          (min (car active))
5848          (max (cdr active))
5849          (types gnus-article-mark-lists)
5850          marks var articles article mark mark-type
5851          bgn end)
5852
5853     (dolist (marks marked-lists)
5854       (setq mark (car marks)
5855             mark-type (gnus-article-mark-to-type mark)
5856             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5857
5858       ;; We set the variable according to the type of the marks list,
5859       ;; and then adjust the marks to a subset of the active articles.
5860       (cond
5861        ;; Adjust "simple" lists - compressed yet unsorted
5862        ((eq mark-type 'list)
5863         ;; Simultaneously uncompress and clip to active range
5864         ;; See gnus-uncompress-range for a description of possible marks
5865         (let (l lh)
5866           (if (not (cadr marks))
5867               (set var nil)
5868             (setq articles (if (numberp (cddr marks))
5869                                (list (cdr marks))
5870                              (cdr marks))
5871                   lh (cons nil nil)
5872                   l lh)
5873
5874             (while (setq article (pop articles))
5875               (cond ((consp article)
5876                      (setq bgn (max (car article) min)
5877                            end (min (cdr article) max))
5878                      (while (<= bgn end)
5879                        (setq l (setcdr l (cons bgn nil))
5880                              bgn (1+ bgn))))
5881                     ((and (<= min article)
5882                           (>= max article))
5883                      (setq l (setcdr l (cons article nil))))))
5884             (set var (cdr lh)))))
5885        ;; Adjust assocs.
5886        ((eq mark-type 'tuple)
5887         (set var (setq articles (cdr marks)))
5888         (when (not (listp (cdr (symbol-value var))))
5889           (set var (list (symbol-value var))))
5890         (when (not (listp (cdr articles)))
5891           (setq articles (list articles)))
5892         (while articles
5893           (when (or (not (consp (setq article (pop articles))))
5894                     (< (car article) min)
5895                     (> (car article) max))
5896             (set var (delq article (symbol-value var))))))
5897        ;; Adjust ranges (sloppily).
5898        ((eq mark-type 'range)
5899         (cond
5900          ((eq mark 'seen)
5901           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5902           ;; It should be (seen (NUM1 . NUM2)).
5903           (when (numberp (cddr marks))
5904             (setcdr marks (list (cdr marks))))
5905           (setq articles (cdr marks))
5906           (while (and articles
5907                       (or (and (consp (car articles))
5908                                (> min (cdar articles)))
5909                           (and (numberp (car articles))
5910                                (> min (car articles)))))
5911             (pop articles))
5912           (set var articles))))))))
5913
5914 (defun gnus-update-missing-marks (missing)
5915   "Go through the list of MISSING articles and remove them from the mark lists."
5916   (when missing
5917     (let (var m)
5918       ;; Go through all types.
5919       (dolist (elem gnus-article-mark-lists)
5920         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5921           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5922           (when (symbol-value var)
5923             ;; This list has articles.  So we delete all missing
5924             ;; articles from it.
5925             (setq m missing)
5926             (while m
5927               (set var (delq (pop m) (symbol-value var))))))))))
5928
5929 (defun gnus-update-marks ()
5930   "Enter the various lists of marked articles into the newsgroup info list."
5931   (let ((types gnus-article-mark-lists)
5932         (info (gnus-get-info gnus-newsgroup-name))
5933         type list newmarked symbol delta-marks)
5934     (when info
5935       ;; Add all marks lists to the list of marks lists.
5936       (while (setq type (pop types))
5937         (setq list (symbol-value
5938                     (setq symbol
5939                           (intern (format "gnus-newsgroup-%s" (car type))))))
5940
5941         (when list
5942           ;; Get rid of the entries of the articles that have the
5943           ;; default score.
5944           (when (and (eq (cdr type) 'score)
5945                      gnus-save-score
5946                      list)
5947             (let* ((arts list)
5948                    (prev (cons nil list))
5949                    (all prev))
5950               (while arts
5951                 (if (or (not (consp (car arts)))
5952                         (= (cdar arts) gnus-summary-default-score))
5953                     (setcdr prev (cdr arts))
5954                   (setq prev arts))
5955                 (setq arts (cdr arts)))
5956               (setq list (cdr all)))))
5957
5958         (when (eq (cdr type) 'seen)
5959           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5960
5961         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5962           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5963
5964         (when (and (gnus-check-backend-function
5965                     'request-set-mark gnus-newsgroup-name)
5966                    (not (gnus-article-unpropagatable-p (cdr type))))
5967           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5968                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5969                  (add (gnus-remove-from-range
5970                        (gnus-copy-sequence list) old)))
5971             (when add
5972               (push (list add 'add (list (cdr type))) delta-marks))
5973             (when del
5974               (push (list del 'del (list (cdr type))) delta-marks))))
5975
5976         (when list
5977           (push (cons (cdr type) list) newmarked)))
5978
5979       (when delta-marks
5980         (unless (gnus-check-group gnus-newsgroup-name)
5981           (error "Can't open server for %s" gnus-newsgroup-name))
5982         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5983
5984       ;; Enter these new marks into the info of the group.
5985       (if (nthcdr 3 info)
5986           (setcar (nthcdr 3 info) newmarked)
5987         ;; Add the marks lists to the end of the info.
5988         (when newmarked
5989           (setcdr (nthcdr 2 info) (list newmarked))))
5990
5991       ;; Cut off the end of the info if there's nothing else there.
5992       (let ((i 5))
5993         (while (and (> i 2)
5994                     (not (nth i info)))
5995           (when (nthcdr (decf i) info)
5996             (setcdr (nthcdr i info) nil)))))))
5997
5998 (defun gnus-set-mode-line (where)
5999   "Set the mode line of the article or summary buffers.
6000 If WHERE is `summary', the summary mode line format will be used."
6001   ;; Is this mode line one we keep updated?
6002   (when (and (memq where gnus-updated-mode-lines)
6003              (symbol-value
6004               (intern (format "gnus-%s-mode-line-format-spec" where))))
6005     (let (mode-string)
6006       ;; We evaluate this in the summary buffer since these
6007       ;; variables are buffer-local to that buffer.
6008       (with-current-buffer gnus-summary-buffer
6009         ;; We bind all these variables that are used in the `eval' form
6010         ;; below.
6011         (let* ((mformat (symbol-value
6012                          (intern
6013                           (format "gnus-%s-mode-line-format-spec" where))))
6014                (gnus-tmp-group-name (gnus-mode-string-quote
6015                                      (gnus-group-decoded-name
6016                                       gnus-newsgroup-name)))
6017                (gnus-tmp-article-number (or gnus-current-article 0))
6018                (gnus-tmp-unread gnus-newsgroup-unreads)
6019                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6020                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6021                (gnus-tmp-unread-and-unselected
6022                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6023                             (zerop gnus-tmp-unselected))
6024                        "")
6025                       ((zerop gnus-tmp-unselected)
6026                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6027                       (t (format "{%d(+%d) more}"
6028                                  gnus-tmp-unread-and-unticked
6029                                  gnus-tmp-unselected))))
6030                (gnus-tmp-subject
6031                 (if (and gnus-current-headers
6032                          (vectorp gnus-current-headers))
6033                     (gnus-mode-string-quote
6034                      (mail-header-subject gnus-current-headers))
6035                   ""))
6036                bufname-length max-len
6037                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6038           (setq mode-string (eval mformat))
6039           (setq bufname-length (if (string-match "%b" mode-string)
6040                                    (- (length
6041                                        (buffer-name
6042                                         (if (eq where 'summary)
6043                                             nil
6044                                           (get-buffer gnus-article-buffer))))
6045                                       2)
6046                                  0))
6047           (setq max-len (max 4 (if gnus-mode-non-string-length
6048                                    (- (window-width)
6049                                       gnus-mode-non-string-length
6050                                       bufname-length)
6051                                  (length mode-string))))
6052           ;; We might have to chop a bit of the string off...
6053           (when (> (length mode-string) max-len)
6054             (setq mode-string
6055                   (concat (truncate-string-to-width mode-string (- max-len 3))
6056                           "...")))
6057           ;; Pad the mode string a bit.
6058           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6059       ;; Update the mode line.
6060       (setq mode-line-buffer-identification
6061             (gnus-mode-line-buffer-identification (list mode-string)))
6062       (set-buffer-modified-p t))))
6063
6064 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6065   "Go through the HEADERS list and add all Xrefs to a hash table.
6066 The resulting hash table is returned, or nil if no Xrefs were found."
6067   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6068          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6069          (xref-hashtb (gnus-make-hashtable))
6070          start group entry number xrefs header)
6071     (while headers
6072       (setq header (pop headers))
6073       (when (and (setq xrefs (mail-header-xref header))
6074                  (not (memq (setq number (mail-header-number header))
6075                             unreads)))
6076         (setq start 0)
6077         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6078           (setq start (match-end 0))
6079           (setq group (if prefix
6080                           (concat prefix (substring xrefs (match-beginning 1)
6081                                                     (match-end 1)))
6082                         (substring xrefs (match-beginning 1) (match-end 1))))
6083           (setq number
6084                 (string-to-number (substring xrefs (match-beginning 2)
6085                                           (match-end 2))))
6086           (if (setq entry (gnus-gethash group xref-hashtb))
6087               (setcdr entry (cons number (cdr entry)))
6088             (gnus-sethash group (cons number nil) xref-hashtb)))))
6089     (and start xref-hashtb)))
6090
6091 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6092   "Look through all the headers and mark the Xrefs as read."
6093   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6094         name info xref-hashtb idlist method nth4)
6095     (save-excursion
6096       (set-buffer gnus-group-buffer)
6097       (when (setq xref-hashtb
6098                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6099         (mapatoms
6100          (lambda (group)
6101            (unless (string= from-newsgroup (setq name (symbol-name group)))
6102              (setq idlist (symbol-value group))
6103              ;; Dead groups are not updated.
6104              (and (prog1
6105                       (setq info (gnus-get-info name))
6106                     (when (stringp (setq nth4 (gnus-info-method info)))
6107                       (setq nth4 (gnus-server-to-method nth4))))
6108                   ;; Only do the xrefs if the group has the same
6109                   ;; select method as the group we have just read.
6110                   (or (gnus-methods-equal-p
6111                        nth4 (gnus-find-method-for-group from-newsgroup))
6112                       virtual
6113                       (equal nth4 (setq method (gnus-find-method-for-group
6114                                                 from-newsgroup)))
6115                       (and (equal (car nth4) (car method))
6116                            (equal (nth 1 nth4) (nth 1 method))))
6117                   gnus-use-cross-reference
6118                   (or (not (eq gnus-use-cross-reference t))
6119                       virtual
6120                       ;; Only do cross-references on subscribed
6121                       ;; groups, if that is what is wanted.
6122                       (<= (gnus-info-level info) gnus-level-subscribed))
6123                   (gnus-group-make-articles-read name idlist))))
6124          xref-hashtb)))))
6125
6126 (defun gnus-compute-read-articles (group articles)
6127   (let* ((entry (gnus-group-entry group))
6128          (info (nth 2 entry))
6129          (active (gnus-active group))
6130          ninfo)
6131     (when entry
6132       ;; First peel off all invalid article numbers.
6133       (when active
6134         (let ((ids articles)
6135               id first)
6136           (while (setq id (pop ids))
6137             (when (and first (> id (cdr active)))
6138               ;; We'll end up in this situation in one particular
6139               ;; obscure situation.  If you re-scan a group and get
6140               ;; a new article that is cross-posted to a different
6141               ;; group that has not been re-scanned, you might get
6142               ;; crossposted article that has a higher number than
6143               ;; Gnus believes possible.  So we re-activate this
6144               ;; group as well.  This might mean doing the
6145               ;; crossposting thingy will *increase* the number
6146               ;; of articles in some groups.  Tsk, tsk.
6147               (setq active (or (gnus-activate-group group) active)))
6148             (when (or (> id (cdr active))
6149                       (< id (car active)))
6150               (setq articles (delq id articles))))))
6151       ;; If the read list is nil, we init it.
6152       (if (and active
6153                (null (gnus-info-read info))
6154                (> (car active) 1))
6155           (setq ninfo (cons 1 (1- (car active))))
6156         (setq ninfo (gnus-info-read info)))
6157       ;; Then we add the read articles to the range.
6158       (gnus-add-to-range
6159        ninfo (setq articles (sort articles '<))))))
6160
6161 (defun gnus-group-make-articles-read (group articles)
6162   "Update the info of GROUP to say that ARTICLES are read."
6163   (let* ((num 0)
6164          (entry (gnus-group-entry group))
6165          (info (nth 2 entry))
6166          (active (gnus-active group))
6167          range)
6168     (when entry
6169       (setq range (gnus-compute-read-articles group articles))
6170       (with-current-buffer gnus-group-buffer
6171         (gnus-undo-register
6172           `(progn
6173              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6174              (gnus-info-set-read ',info ',(gnus-info-read info))
6175              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6176              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6177              (gnus-group-update-group ,group t))))
6178       ;; Add the read articles to the range.
6179       (gnus-info-set-read info range)
6180       (gnus-request-set-mark group (list (list range 'add '(read))))
6181       ;; Then we have to re-compute how many unread
6182       ;; articles there are in this group.
6183       (when active
6184         (cond
6185          ((not range)
6186           (setq num (- (1+ (cdr active)) (car active))))
6187          ((not (listp (cdr range)))
6188           (setq num (- (cdr active) (- (1+ (cdr range))
6189                                        (car range)))))
6190          (t
6191           (while range
6192             (if (numberp (car range))
6193                 (setq num (1+ num))
6194               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6195             (setq range (cdr range)))
6196           (setq num (- (cdr active) num))))
6197         ;; Update the number of unread articles.
6198         (setcar entry num)
6199         ;; Update the group buffer.
6200         (unless (gnus-ephemeral-group-p group)
6201           (gnus-group-update-group group t))))))
6202
6203 (defvar gnus-newsgroup-none-id 0)
6204
6205 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6206   (let ((cur nntp-server-buffer)
6207         (dependencies
6208          (or dependencies
6209              (with-current-buffer gnus-summary-buffer
6210                gnus-newsgroup-dependencies)))
6211         headers id end ref number
6212         (mail-parse-charset gnus-newsgroup-charset)
6213         (mail-parse-ignored-charsets
6214          (save-current-buffer (condition-case nil
6215                                   (set-buffer gnus-summary-buffer)
6216                                 (error))
6217                               gnus-newsgroup-ignored-charsets)))
6218     (with-current-buffer nntp-server-buffer
6219       ;; Translate all TAB characters into SPACE characters.
6220       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6221       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6222       (ietf-drums-unfold-fws)
6223       (gnus-run-hooks 'gnus-parse-headers-hook)
6224       (let ((case-fold-search t)
6225             in-reply-to header p lines chars)
6226         (goto-char (point-min))
6227         ;; Search to the beginning of the next header.  Error messages
6228         ;; do not begin with 2 or 3.
6229         (while (re-search-forward "^[23][0-9]+ " nil t)
6230           (setq id nil
6231                 ref nil)
6232           ;; This implementation of this function, with nine
6233           ;; search-forwards instead of the one re-search-forward and
6234           ;; a case (which basically was the old function) is actually
6235           ;; about twice as fast, even though it looks messier.  You
6236           ;; can't have everything, I guess.  Speed and elegance
6237           ;; doesn't always go hand in hand.
6238           (setq
6239            header
6240            (vector
6241             ;; Number.
6242             (prog1
6243                 (setq number (read cur))
6244               (end-of-line)
6245               (setq p (point))
6246               (narrow-to-region (point)
6247                                 (or (and (search-forward "\n.\n" nil t)
6248                                          (- (point) 2))
6249                                     (point))))
6250             ;; Subject.
6251             (progn
6252               (goto-char p)
6253               (if (search-forward "\nsubject:" nil t)
6254                   (funcall gnus-decode-encoded-word-function
6255                            (nnheader-header-value))
6256                 "(none)"))
6257             ;; From.
6258             (progn
6259               (goto-char p)
6260               (if (search-forward "\nfrom:" nil t)
6261                   (funcall gnus-decode-encoded-address-function
6262                            (nnheader-header-value))
6263                 "(nobody)"))
6264             ;; Date.
6265             (progn
6266               (goto-char p)
6267               (if (search-forward "\ndate:" nil t)
6268                   (nnheader-header-value) ""))
6269             ;; Message-ID.
6270             (progn
6271               (goto-char p)
6272               (setq id (if (re-search-forward
6273                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6274                            ;; We do it this way to make sure the Message-ID
6275                            ;; is (somewhat) syntactically valid.
6276                            (buffer-substring (match-beginning 1)
6277                                              (match-end 1))
6278                          ;; If there was no message-id, we just fake one
6279                          ;; to make subsequent routines simpler.
6280                          (nnheader-generate-fake-message-id number))))
6281             ;; References.
6282             (progn
6283               (goto-char p)
6284               (if (search-forward "\nreferences:" nil t)
6285                   (progn
6286                     (setq end (point))
6287                     (prog1
6288                         (nnheader-header-value)
6289                       (setq ref
6290                             (buffer-substring
6291                              (progn
6292                                (end-of-line)
6293                                (search-backward ">" end t)
6294                                (1+ (point)))
6295                              (progn
6296                                (search-backward "<" end t)
6297                                (point))))))
6298                 ;; Get the references from the in-reply-to header if there
6299                 ;; were no references and the in-reply-to header looks
6300                 ;; promising.
6301                 (if (and (search-forward "\nin-reply-to:" nil t)
6302                          (setq in-reply-to (nnheader-header-value))
6303                          (string-match "<[^>]+>" in-reply-to))
6304                     (let (ref2)
6305                       (setq ref (substring in-reply-to (match-beginning 0)
6306                                            (match-end 0)))
6307                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6308                         (setq ref2 (substring in-reply-to (match-beginning 0)
6309                                               (match-end 0)))
6310                         (when (> (length ref2) (length ref))
6311                           (setq ref ref2)))
6312                       ref)
6313                   (setq ref nil))))
6314             ;; Chars.
6315             (progn
6316               (goto-char p)
6317               (if (search-forward "\nchars: " nil t)
6318                   (if (numberp (setq chars (ignore-errors (read cur))))
6319                       chars -1)
6320                 -1))
6321             ;; Lines.
6322             (progn
6323               (goto-char p)
6324               (if (search-forward "\nlines: " nil t)
6325                   (if (numberp (setq lines (ignore-errors (read cur))))
6326                       lines -1)
6327                 -1))
6328             ;; Xref.
6329             (progn
6330               (goto-char p)
6331               (and (search-forward "\nxref:" nil t)
6332                    (nnheader-header-value)))
6333             ;; Extra.
6334             (when gnus-extra-headers
6335               (let ((extra gnus-extra-headers)
6336                     out)
6337                 (while extra
6338                   (goto-char p)
6339                   (when (search-forward
6340                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6341                     (push (cons (car extra) (nnheader-header-value))
6342                           out))
6343                   (pop extra))
6344                 out))))
6345           (when (equal id ref)
6346             (setq ref nil))
6347
6348           (when gnus-alter-header-function
6349             (funcall gnus-alter-header-function header)
6350             (setq id (mail-header-id header)
6351                   ref (gnus-parent-id (mail-header-references header))))
6352
6353           (when (setq header
6354                       (gnus-dependencies-add-header
6355                        header dependencies force-new))
6356             (push header headers))
6357           (goto-char (point-max))
6358           (widen))
6359         (nreverse headers)))))
6360
6361 ;; Goes through the xover lines and returns a list of vectors
6362 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6363                                                   force-new dependencies
6364                                                   group also-fetch-heads)
6365   "Parse the news overview data in the server buffer.
6366 Return a list of headers that match SEQUENCE (see
6367 `nntp-retrieve-headers')."
6368   ;; Get the Xref when the users reads the articles since most/some
6369   ;; NNTP servers do not include Xrefs when using XOVER.
6370   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6371   (let ((mail-parse-charset gnus-newsgroup-charset)
6372         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6373         (cur nntp-server-buffer)
6374         (dependencies (or dependencies gnus-newsgroup-dependencies))
6375         (allp (cond
6376                ((eq gnus-read-all-available-headers t)
6377                 t)
6378                ((and (stringp gnus-read-all-available-headers)
6379                      group)
6380                 (string-match gnus-read-all-available-headers group))
6381                (t
6382                 nil)))
6383         number headers header)
6384     (with-current-buffer nntp-server-buffer
6385       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6386       ;; Allow the user to mangle the headers before parsing them.
6387       (gnus-run-hooks 'gnus-parse-headers-hook)
6388       (goto-char (point-min))
6389       (gnus-parse-without-error
6390         (while (and (or sequence allp)
6391                     (not (eobp)))
6392           (setq number (read cur))
6393           (when (not allp)
6394             (while (and sequence
6395                         (< (car sequence) number))
6396               (setq sequence (cdr sequence))))
6397           (when (and (or allp
6398                          (and sequence
6399                               (eq number (car sequence))))
6400                      (progn
6401                        (setq sequence (cdr sequence))
6402                        (setq header (inline
6403                                       (gnus-nov-parse-line
6404                                        number dependencies force-new)))))
6405             (push header headers))
6406           (forward-line 1)))
6407       ;; A common bug in inn is that if you have posted an article and
6408       ;; then retrieves the active file, it will answer correctly --
6409       ;; the new article is included.  However, a NOV entry for the
6410       ;; article may not have been generated yet, so this may fail.
6411       ;; We work around this problem by retrieving the last few
6412       ;; headers using HEAD.
6413       (if (or (not also-fetch-heads)
6414               (not sequence))
6415           ;; We (probably) got all the headers.
6416           (nreverse headers)
6417         (let ((gnus-nov-is-evil t))
6418           (nconc
6419            (nreverse headers)
6420            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6421              (gnus-get-newsgroup-headers))))))))
6422
6423 (defun gnus-article-get-xrefs ()
6424   "Fill in the Xref value in `gnus-current-headers', if necessary.
6425 This is meant to be called in `gnus-article-internal-prepare-hook'."
6426   (let ((headers (with-current-buffer gnus-summary-buffer
6427                    gnus-current-headers)))
6428     (or (not gnus-use-cross-reference)
6429         (not headers)
6430         (and (mail-header-xref headers)
6431              (not (string= (mail-header-xref headers) "")))
6432         (let ((case-fold-search t)
6433               xref)
6434           (save-restriction
6435             (nnheader-narrow-to-headers)
6436             (goto-char (point-min))
6437             (when (or (and (not (eobp))
6438                            (eq (downcase (char-after)) ?x)
6439                            (looking-at "Xref:"))
6440                       (search-forward "\nXref:" nil t))
6441               (goto-char (1+ (match-end 0)))
6442               (setq xref (buffer-substring (point) (point-at-eol)))
6443               (mail-header-set-xref headers xref)))))))
6444
6445 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6446   "Find article ID and insert the summary line for that article.
6447 OLD-HEADER can either be a header or a line number to insert
6448 the subject line on."
6449   (let* ((line (and (numberp old-header) old-header))
6450          (old-header (and (vectorp old-header) old-header))
6451          (header (cond ((and old-header use-old-header)
6452                         old-header)
6453                        ((and (numberp id)
6454                              (gnus-number-to-header id))
6455                         (gnus-number-to-header id))
6456                        (t
6457                         (gnus-read-header id))))
6458          (number (and (numberp id) id))
6459          d)
6460     (when header
6461       ;; Rebuild the thread that this article is part of and go to the
6462       ;; article we have fetched.
6463       (when (and (not gnus-show-threads)
6464                  old-header)
6465         (when (and number
6466                    (setq d (gnus-data-find (mail-header-number old-header))))
6467           (goto-char (gnus-data-pos d))
6468           (gnus-data-remove
6469            number
6470            (- (point-at-bol)
6471               (prog1
6472                   (1+ (point-at-eol))
6473                 (gnus-delete-line))))))
6474       ;; Remove list identifiers from subject.
6475       (when gnus-list-identifiers
6476         (let ((gnus-newsgroup-headers (list header)))
6477           (gnus-summary-remove-list-identifiers)))
6478       (when old-header
6479         (mail-header-set-number header (mail-header-number old-header)))
6480       (setq gnus-newsgroup-sparse
6481             (delq (setq number (mail-header-number header))
6482                   gnus-newsgroup-sparse))
6483       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6484       (push number gnus-newsgroup-limit)
6485       (gnus-rebuild-thread (mail-header-id header) line)
6486       (gnus-summary-goto-subject number nil t))
6487     (when (and (numberp number)
6488                (> number 0))
6489       ;; We have to update the boundaries even if we can't fetch the
6490       ;; article if ID is a number -- so that the next `P' or `N'
6491       ;; command will fetch the previous (or next) article even
6492       ;; if the one we tried to fetch this time has been canceled.
6493       (when (> number gnus-newsgroup-end)
6494         (setq gnus-newsgroup-end number))
6495       (when (< number gnus-newsgroup-begin)
6496         (setq gnus-newsgroup-begin number))
6497       (setq gnus-newsgroup-unselected
6498             (delq number gnus-newsgroup-unselected)))
6499     ;; Report back a success?
6500     (and header (mail-header-number header))))
6501
6502 ;;; Process/prefix in the summary buffer
6503
6504 (defun gnus-summary-work-articles (n)
6505   "Return a list of articles to be worked upon.
6506 The prefix argument, the list of process marked articles, and the
6507 current article will be taken into consideration."
6508   (with-current-buffer gnus-summary-buffer
6509     (cond
6510      (n
6511       ;; A numerical prefix has been given.
6512       (setq n (prefix-numeric-value n))
6513       (let ((backward (< n 0))
6514             (n (abs (prefix-numeric-value n)))
6515             articles article)
6516         (save-excursion
6517           (while
6518               (and (> n 0)
6519                    (push (setq article (gnus-summary-article-number))
6520                          articles)
6521                    (if backward
6522                        (gnus-summary-find-prev nil article)
6523                      (gnus-summary-find-next nil article)))
6524             (decf n)))
6525         (nreverse articles)))
6526      ((and (gnus-region-active-p) (mark))
6527       (message "region active")
6528       ;; Work on the region between point and mark.
6529       (let ((max (max (point) (mark)))
6530             articles article)
6531         (save-excursion
6532           (goto-char (min (point) (mark)))
6533           (while
6534               (and
6535                (push (setq article (gnus-summary-article-number)) articles)
6536                (gnus-summary-find-next nil article)
6537                (< (point) max)))
6538           (nreverse articles))))
6539      (gnus-newsgroup-processable
6540       ;; There are process-marked articles present.
6541       ;; Save current state.
6542       (gnus-summary-save-process-mark)
6543       ;; Return the list.
6544       (reverse gnus-newsgroup-processable))
6545      (t
6546       ;; Just return the current article.
6547       (list (gnus-summary-article-number))))))
6548
6549 (defmacro gnus-summary-iterate (arg &rest forms)
6550   "Iterate over the process/prefixed articles and do FORMS.
6551 ARG is the interactive prefix given to the command.  FORMS will be
6552 executed with point over the summary line of the articles."
6553   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6554     `(let ((,articles (gnus-summary-work-articles ,arg)))
6555        (while ,articles
6556          (gnus-summary-goto-subject (car ,articles))
6557          ,@forms
6558          (pop ,articles)))))
6559
6560 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6561 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6562
6563 (defun gnus-summary-save-process-mark ()
6564   "Push the current set of process marked articles on the stack."
6565   (interactive)
6566   (push (copy-sequence gnus-newsgroup-processable)
6567         gnus-newsgroup-process-stack))
6568
6569 (defun gnus-summary-kill-process-mark ()
6570   "Push the current set of process marked articles on the stack and unmark."
6571   (interactive)
6572   (gnus-summary-save-process-mark)
6573   (gnus-summary-unmark-all-processable))
6574
6575 (defun gnus-summary-yank-process-mark ()
6576   "Pop the last process mark state off the stack and restore it."
6577   (interactive)
6578   (unless gnus-newsgroup-process-stack
6579     (error "Empty mark stack"))
6580   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6581
6582 (defun gnus-summary-process-mark-set (set)
6583   "Make SET into the current process marked articles."
6584   (gnus-summary-unmark-all-processable)
6585   (mapc 'gnus-summary-set-process-mark set))
6586
6587 ;;; Searching and stuff
6588
6589 (defun gnus-summary-search-group (&optional backward use-level)
6590   "Search for next unread newsgroup.
6591 If optional argument BACKWARD is non-nil, search backward instead."
6592   (with-current-buffer gnus-group-buffer
6593     (when (gnus-group-search-forward
6594            backward nil (if use-level (gnus-group-group-level) nil))
6595       (gnus-group-group-name))))
6596
6597 (defun gnus-summary-best-group (&optional exclude-group)
6598   "Find the name of the best unread group.
6599 If EXCLUDE-GROUP, do not go to this group."
6600   (with-current-buffer gnus-group-buffer
6601     (save-excursion
6602       (gnus-group-best-unread-group exclude-group))))
6603
6604 (defun gnus-summary-find-next (&optional unread article backward)
6605   (if backward
6606       (gnus-summary-find-prev unread article)
6607     (let* ((dummy (gnus-summary-article-intangible-p))
6608            (article (or article (gnus-summary-article-number)))
6609            (data (gnus-data-find-list article))
6610            result)
6611       (when (and (not dummy)
6612                  (or (not gnus-summary-check-current)
6613                      (not unread)
6614                      (not (gnus-data-unread-p (car data)))))
6615         (setq data (cdr data)))
6616       (when (setq result
6617                   (if unread
6618                       (progn
6619                         (while data
6620                           (unless (memq (gnus-data-number (car data))
6621                                         (cond
6622                                          ((eq gnus-auto-goto-ignores
6623                                               'always-undownloaded)
6624                                           gnus-newsgroup-undownloaded)
6625                                          (gnus-plugged
6626                                           nil)
6627                                          ((eq gnus-auto-goto-ignores
6628                                               'unfetched)
6629                                           gnus-newsgroup-unfetched)
6630                                          ((eq gnus-auto-goto-ignores
6631                                               'undownloaded)
6632                                           gnus-newsgroup-undownloaded)))
6633                             (when (gnus-data-unread-p (car data))
6634                               (setq result (car data)
6635                                     data nil)))
6636                           (setq data (cdr data)))
6637                         result)
6638                     (car data)))
6639         (goto-char (gnus-data-pos result))
6640         (gnus-data-number result)))))
6641
6642 (defun gnus-summary-find-prev (&optional unread article)
6643   (let* ((eobp (eobp))
6644          (article (or article (gnus-summary-article-number)))
6645          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6646          result)
6647     (when (and (not eobp)
6648                (or (not gnus-summary-check-current)
6649                    (not unread)
6650                    (not (gnus-data-unread-p (car data)))))
6651       (setq data (cdr data)))
6652     (when (setq result
6653                 (if unread
6654                     (progn
6655                       (while data
6656                         (unless (memq (gnus-data-number (car data))
6657                                       (cond
6658                                        ((eq gnus-auto-goto-ignores
6659                                             'always-undownloaded)
6660                                         gnus-newsgroup-undownloaded)
6661                                        (gnus-plugged
6662                                         nil)
6663                                        ((eq gnus-auto-goto-ignores
6664                                             'unfetched)
6665                                         gnus-newsgroup-unfetched)
6666                                        ((eq gnus-auto-goto-ignores
6667                                             'undownloaded)
6668                                         gnus-newsgroup-undownloaded)))
6669                           (when (gnus-data-unread-p (car data))
6670                             (setq result (car data)
6671                                   data nil)))
6672                         (setq data (cdr data)))
6673                       result)
6674                   (car data)))
6675       (goto-char (gnus-data-pos result))
6676       (gnus-data-number result))))
6677
6678 (defun gnus-summary-find-subject (subject &optional unread backward article)
6679   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6680          (article (or article (gnus-summary-article-number)))
6681          (articles (gnus-data-list backward))
6682          (arts (gnus-data-find-list article articles))
6683          result)
6684     (when (or (not gnus-summary-check-current)
6685               (not unread)
6686               (not (gnus-data-unread-p (car arts))))
6687       (setq arts (cdr arts)))
6688     (while arts
6689       (and (or (not unread)
6690                (gnus-data-unread-p (car arts)))
6691            (vectorp (gnus-data-header (car arts)))
6692            (gnus-subject-equal
6693             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6694            (setq result (car arts)
6695                  arts nil))
6696       (setq arts (cdr arts)))
6697     (and result
6698          (goto-char (gnus-data-pos result))
6699          (gnus-data-number result))))
6700
6701 (defun gnus-summary-search-forward (&optional unread subject backward)
6702   "Search forward for an article.
6703 If UNREAD, look for unread articles.  If SUBJECT, look for
6704 articles with that subject.  If BACKWARD, search backward instead."
6705   (cond (subject (gnus-summary-find-subject subject unread backward))
6706         (backward (gnus-summary-find-prev unread))
6707         (t (gnus-summary-find-next unread))))
6708
6709 (defun gnus-recenter (&optional n)
6710   "Center point in window and redisplay frame.
6711 Also do horizontal recentering."
6712   (interactive "P")
6713   (when (and gnus-auto-center-summary
6714              (not (eq gnus-auto-center-summary 'vertical)))
6715     (gnus-horizontal-recenter))
6716   (recenter n))
6717
6718 (defun gnus-summary-recenter ()
6719   "Center point in the summary window.
6720 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6721 displayed, no centering will be performed."
6722   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6723   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6724   (interactive)
6725   ;; The user has to want it.
6726   (when gnus-auto-center-summary
6727     (let* ((top (cond ((< (window-height) 4) 0)
6728                       ((< (window-height) 7) 1)
6729                       (t (if (numberp gnus-auto-center-summary)
6730                              gnus-auto-center-summary
6731                            (/ (1- (window-height)) 2)))))
6732            (height (1- (window-height)))
6733            (bottom (save-excursion (goto-char (point-max))
6734                                    (forward-line (- height))
6735                                    (point)))
6736            (window (get-buffer-window (current-buffer))))
6737       (when (get-buffer-window gnus-article-buffer)
6738         ;; Only do recentering when the article buffer is displayed,
6739         ;; Set the window start to either `bottom', which is the biggest
6740         ;; possible valid number, or the second line from the top,
6741         ;; whichever is the least.
6742         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6743           (if (> bottom top-pos)
6744               ;; Keep the second line from the top visible
6745               (set-window-start window top-pos)
6746             ;; Try to keep the bottom line visible; if it's partially
6747             ;; obscured, either scroll one more line to make it fully
6748             ;; visible, or revert to using TOP-POS.
6749             (save-excursion
6750               (goto-char (point-max))
6751               (forward-line -1)
6752               (let ((last-line-start (point)))
6753                 (goto-char bottom)
6754                 (set-window-start window (point) t)
6755                 (when (not (pos-visible-in-window-p last-line-start window))
6756                   (forward-line 1)
6757                   (set-window-start window (min (point) top-pos) t)))))))
6758       ;; Do horizontal recentering while we're at it.
6759       (when (and (get-buffer-window (current-buffer) t)
6760                  (not (eq gnus-auto-center-summary 'vertical)))
6761         (let ((selected (selected-window)))
6762           (select-window (get-buffer-window (current-buffer) t))
6763           (gnus-summary-position-point)
6764           (gnus-horizontal-recenter)
6765           (select-window selected))))))
6766
6767 (defun gnus-summary-jump-to-group (newsgroup)
6768   "Move point to NEWSGROUP in group mode buffer."
6769   ;; Keep update point of group mode buffer if visible.
6770   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6771       (save-window-excursion
6772         ;; Take care of tree window mode.
6773         (when (get-buffer-window gnus-group-buffer)
6774           (pop-to-buffer gnus-group-buffer))
6775         (gnus-group-jump-to-group newsgroup))
6776     (save-excursion
6777       ;; Take care of tree window mode.
6778       (if (get-buffer-window gnus-group-buffer 0)
6779           (pop-to-buffer gnus-group-buffer)
6780         (set-buffer gnus-group-buffer))
6781       (gnus-group-jump-to-group newsgroup))))
6782
6783 ;; This function returns a list of article numbers based on the
6784 ;; difference between the ranges of read articles in this group and
6785 ;; the range of active articles.
6786 (defun gnus-list-of-unread-articles (group)
6787   (let* ((read (gnus-info-read (gnus-get-info group)))
6788          (active (or (gnus-active group) (gnus-activate-group group)))
6789          (last (or (cdr active)
6790                    (error "Group %s couldn't be activated " group)))
6791          (bottom (if gnus-newsgroup-maximum-articles
6792                      (max (car active)
6793                           (- last gnus-newsgroup-maximum-articles -1))
6794                    (car active)))
6795          first nlast unread)
6796     ;; If none are read, then all are unread.
6797     (if (not read)
6798         (setq first bottom)
6799       ;; If the range of read articles is a single range, then the
6800       ;; first unread article is the article after the last read
6801       ;; article.  Sounds logical, doesn't it?
6802       (if (and (not (listp (cdr read)))
6803                (or (< (car read) bottom)
6804                    (progn (setq read (list read))
6805                           nil)))
6806           (setq first (max bottom (1+ (cdr read))))
6807         ;; `read' is a list of ranges.
6808         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6809                                   (caar read)))
6810                   1)
6811           (setq first bottom))
6812         (while read
6813           (when first
6814             (while (< first nlast)
6815               (setq unread (cons first unread)
6816                     first (1+ first))))
6817           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6818           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6819           (setq read (cdr read)))))
6820     ;; And add the last unread articles.
6821     (while (<= first last)
6822       (setq unread (cons first unread)
6823             first (1+ first)))
6824     ;; Return the list of unread articles.
6825     (delq 0 (nreverse unread))))
6826
6827 (defun gnus-list-of-read-articles (group)
6828   "Return a list of unread, unticked and non-dormant articles."
6829   (let* ((info (gnus-get-info group))
6830          (marked (gnus-info-marks info))
6831          (active (gnus-active group)))
6832     (and info active
6833          (gnus-list-range-difference
6834           (gnus-list-range-difference
6835            (gnus-sorted-complement
6836             (gnus-uncompress-range
6837              (if gnus-newsgroup-maximum-articles
6838                  (cons (max (car active)
6839                             (- (cdr active)
6840                                gnus-newsgroup-maximum-articles
6841                                -1))
6842                        (cdr active))
6843                active))
6844             (gnus-list-of-unread-articles group))
6845            (cdr (assq 'dormant marked)))
6846           (cdr (assq 'tick marked))))))
6847
6848 ;; This function returns a sequence of article numbers based on the
6849 ;; difference between the ranges of read articles in this group and
6850 ;; the range of active articles.
6851 (defun gnus-sequence-of-unread-articles (group)
6852   (let* ((read (gnus-info-read (gnus-get-info group)))
6853          (active (or (gnus-active group) (gnus-activate-group group)))
6854          (last (cdr active))
6855          (bottom (if gnus-newsgroup-maximum-articles
6856                      (max (car active)
6857                           (- last gnus-newsgroup-maximum-articles -1))
6858                    (car active)))
6859          first nlast unread)
6860     ;; If none are read, then all are unread.
6861     (if (not read)
6862         (setq first bottom)
6863       ;; If the range of read articles is a single range, then the
6864       ;; first unread article is the article after the last read
6865       ;; article.  Sounds logical, doesn't it?
6866       (if (and (not (listp (cdr read)))
6867                (or (< (car read) bottom)
6868                    (progn (setq read (list read))
6869                           nil)))
6870           (setq first (max bottom (1+ (cdr read))))
6871         ;; `read' is a list of ranges.
6872         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6873                                   (caar read)))
6874                   1)
6875           (setq first bottom))
6876         (while read
6877           (when first
6878             (push (cons first nlast) unread))
6879           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6880           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6881           (setq read (cdr read)))))
6882     ;; And add the last unread articles.
6883     (cond ((not (and first last))
6884            nil)
6885           ((< first last)
6886            (push (cons first last) unread))
6887           ((= first last)
6888            (push first unread)))
6889     ;; Return the sequence of unread articles.
6890     (delq 0 (nreverse unread))))
6891
6892 ;; Various summary commands
6893
6894 (defun gnus-summary-select-article-buffer ()
6895   "Reconfigure windows to show article buffer."
6896   (interactive)
6897   (if (not (gnus-buffer-live-p gnus-article-buffer))
6898       (error "There is no article buffer for this summary buffer")
6899     (gnus-configure-windows 'article)
6900     (select-window (get-buffer-window gnus-article-buffer))))
6901
6902 (defun gnus-summary-universal-argument (arg)
6903   "Perform any operation on all articles that are process/prefixed."
6904   (interactive "P")
6905   (let ((articles (gnus-summary-work-articles arg))
6906         func article)
6907     (if (eq
6908          (setq
6909           func
6910           (key-binding
6911            (read-key-sequence
6912             (substitute-command-keys
6913              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6914          'undefined)
6915         (gnus-error 1 "Undefined key")
6916       (save-excursion
6917         (while articles
6918           (gnus-summary-goto-subject (setq article (pop articles)))
6919           (let (gnus-newsgroup-processable)
6920             (command-execute func))
6921           (gnus-summary-remove-process-mark article)))))
6922   (gnus-summary-position-point))
6923
6924 (defun gnus-summary-toggle-truncation (&optional arg)
6925   "Toggle truncation of summary lines.
6926 With ARG, turn line truncation on if ARG is positive."
6927   (interactive "P")
6928   (setq truncate-lines
6929         (if (null arg) (not truncate-lines)
6930           (> (prefix-numeric-value arg) 0)))
6931   (redraw-display))
6932
6933 (defun gnus-summary-find-for-reselect ()
6934   "Return the number of an article to stay on across a reselect.
6935 The current article is considered, then following articles, then previous
6936 articles.  An article is sought which is not cancelled and isn't a temporary
6937 insertion from another group.  If there's no such then return a dummy 0."
6938   (let (found)
6939     (dolist (rev '(nil t))
6940       (unless found      ; don't demand the reverse list if we don't need it
6941         (let ((data (gnus-data-find-list
6942                      (gnus-summary-article-number) (gnus-data-list rev))))
6943           (while (and data (not found))
6944             (if (and (< 0 (gnus-data-number (car data)))
6945                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6946                 (setq found (gnus-data-number (car data))))
6947             (setq data (cdr data))))))
6948     (or found 0)))
6949
6950 (defun gnus-summary-reselect-current-group (&optional all rescan)
6951   "Exit and then reselect the current newsgroup.
6952 The prefix argument ALL means to select all articles."
6953   (interactive "P")
6954   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6955     (error "Ephemeral groups can't be reselected"))
6956   (let ((current-subject (gnus-summary-find-for-reselect))
6957         (group gnus-newsgroup-name))
6958     (setq gnus-newsgroup-begin nil)
6959     (gnus-summary-exit nil 'leave-hidden)
6960     ;; We have to adjust the point of group mode buffer because
6961     ;; point was moved to the next unread newsgroup by exiting.
6962     (gnus-summary-jump-to-group group)
6963     (when rescan
6964       (save-excursion
6965         (gnus-group-get-new-news-this-group 1)))
6966     (gnus-group-read-group all t)
6967     (gnus-summary-goto-subject current-subject nil t)))
6968
6969 (defun gnus-summary-rescan-group (&optional all)
6970   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6971   (interactive "P")
6972   (gnus-summary-reselect-current-group all t))
6973
6974 (defun gnus-summary-update-info (&optional non-destructive)
6975   (save-excursion
6976     (let ((group gnus-newsgroup-name))
6977       (when group
6978         (when gnus-newsgroup-kill-headers
6979           (setq gnus-newsgroup-killed
6980                 (gnus-compress-sequence
6981                  (gnus-sorted-union
6982                   (gnus-list-range-intersection
6983                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6984                   gnus-newsgroup-unreads)
6985                  t)))
6986         (unless (listp (cdr gnus-newsgroup-killed))
6987           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6988         (let ((headers gnus-newsgroup-headers))
6989           ;; Set the new ranges of read articles.
6990           (with-current-buffer gnus-group-buffer
6991             (gnus-undo-force-boundary))
6992           (gnus-update-read-articles
6993            group (gnus-sorted-union
6994                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6995           ;; Set the current article marks.
6996           (let ((gnus-newsgroup-scored
6997                  (if (and (not gnus-save-score)
6998                           (not non-destructive))
6999                      nil
7000                    gnus-newsgroup-scored)))
7001             (save-excursion
7002               (gnus-update-marks)))
7003           ;; Do the cross-ref thing.
7004           (when gnus-use-cross-reference
7005             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7006           ;; Do not switch windows but change the buffer to work.
7007           (set-buffer gnus-group-buffer)
7008           (unless (gnus-ephemeral-group-p group)
7009             (gnus-group-update-group group)))))))
7010
7011 (defun gnus-summary-save-newsrc (&optional force)
7012   "Save the current number of read/marked articles in the dribble buffer.
7013 The dribble buffer will then be saved.
7014 If FORCE (the prefix), also save the .newsrc file(s)."
7015   (interactive "P")
7016   (gnus-summary-update-info t)
7017   (if force
7018       (gnus-save-newsrc-file)
7019     (gnus-dribble-save)))
7020
7021 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7022
7023 (defun gnus-summary-exit (&optional temporary leave-hidden)
7024   "Exit reading current newsgroup, and then return to group selection mode.
7025 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7026   (interactive)
7027   (gnus-set-global-variables)
7028   (when (gnus-buffer-live-p gnus-article-buffer)
7029     (with-current-buffer gnus-article-buffer
7030       (mm-destroy-parts gnus-article-mime-handles)
7031       ;; Set it to nil for safety reason.
7032       (setq gnus-article-mime-handle-alist nil)
7033       (setq gnus-article-mime-handles nil)))
7034   (gnus-kill-save-kill-buffer)
7035   (gnus-async-halt-prefetch)
7036   (let* ((group gnus-newsgroup-name)
7037          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7038          (gnus-group-is-exiting-p t)
7039          (mode major-mode)
7040          (group-point nil)
7041          (buf (current-buffer)))
7042     (unless quit-config
7043       ;; Do adaptive scoring, and possibly save score files.
7044       (when gnus-newsgroup-adaptive
7045         (gnus-score-adaptive))
7046       (when gnus-use-scoring
7047         (gnus-score-save)))
7048     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7049     ;; If we have several article buffers, we kill them at exit.
7050     (unless gnus-single-article-buffer
7051       (when (gnus-buffer-live-p gnus-article-buffer)
7052         (with-current-buffer gnus-article-buffer
7053           ;; Don't kill sticky article buffers
7054           (unless (eq major-mode 'gnus-sticky-article-mode)
7055             (gnus-kill-buffer gnus-article-buffer)
7056             (setq gnus-article-current nil))))
7057       (gnus-kill-buffer gnus-original-article-buffer))
7058     (when gnus-use-cache
7059       (gnus-cache-possibly-remove-articles)
7060       (gnus-cache-save-buffers))
7061     (gnus-async-prefetch-remove-group group)
7062     (when gnus-suppress-duplicates
7063       (gnus-dup-enter-articles))
7064     (when gnus-use-trees
7065       (gnus-tree-close group))
7066     (when gnus-use-cache
7067       (gnus-cache-write-active))
7068     ;; Remove entries for this group.
7069     (nnmail-purge-split-history (gnus-group-real-name group))
7070     ;; Make all changes in this group permanent.
7071     (unless quit-config
7072       (gnus-run-hooks 'gnus-exit-group-hook)
7073       (gnus-summary-update-info))
7074     (gnus-close-group group)
7075     ;; Make sure where we were, and go to next newsgroup.
7076     (set-buffer gnus-group-buffer)
7077     (unless quit-config
7078       (gnus-group-jump-to-group group))
7079     (gnus-run-hooks 'gnus-summary-exit-hook)
7080     (unless (or quit-config
7081                 (not gnus-summary-next-group-on-exit)
7082                 ;; If this group has disappeared from the summary
7083                 ;; buffer, don't skip forwards.
7084                 (not (string= group (gnus-group-group-name))))
7085       (gnus-group-next-unread-group 1))
7086     (setq group-point (point))
7087     (if temporary
7088         nil                             ;Nothing to do.
7089       (set-buffer buf)
7090       (if (not gnus-kill-summary-on-exit)
7091           (progn
7092             (gnus-deaden-summary)
7093             (setq mode nil))
7094         ;; We set all buffer-local variables to nil.  It is unclear why
7095         ;; this is needed, but if we don't, buffer-local variables are
7096         ;; not garbage-collected, it seems.  This would the lead to en
7097         ;; ever-growing Emacs.
7098         (gnus-summary-clear-local-variables)
7099         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7100           (gnus-summary-clear-local-variables))
7101         (when (get-buffer gnus-article-buffer)
7102           (bury-buffer gnus-article-buffer))
7103         ;; Return to group mode buffer.
7104         (when (eq mode 'gnus-summary-mode)
7105           (gnus-kill-buffer buf)))
7106       (setq gnus-current-select-method gnus-select-method)
7107       (set-buffer gnus-group-buffer)
7108       (if quit-config
7109           (gnus-handle-ephemeral-exit quit-config)
7110         (goto-char group-point)
7111         ;; If gnus-group-buffer is already displayed, make sure we also move
7112         ;; the cursor in the window that displays it.
7113         (let ((win (get-buffer-window (current-buffer) 0)))
7114           (if win (set-window-point win (point))))
7115         (unless leave-hidden
7116           (gnus-configure-windows 'group 'force)))
7117       ;; Clear the current group name.
7118       (unless quit-config
7119         (setq gnus-newsgroup-name nil)))))
7120
7121 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7122 (defun gnus-summary-exit-no-update (&optional no-questions)
7123   "Quit reading current newsgroup without updating read article info."
7124   (interactive)
7125   (let* ((group gnus-newsgroup-name)
7126          (gnus-group-is-exiting-p t)
7127          (gnus-group-is-exiting-without-update-p t)
7128          (quit-config (gnus-group-quit-config group)))
7129     (when (or no-questions
7130               gnus-expert-user
7131               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7132       (gnus-async-halt-prefetch)
7133       (run-hooks 'gnus-summary-prepare-exit-hook)
7134       (when (gnus-buffer-live-p gnus-article-buffer)
7135         (with-current-buffer gnus-article-buffer
7136           (mm-destroy-parts gnus-article-mime-handles)
7137           ;; Set it to nil for safety reason.
7138           (setq gnus-article-mime-handle-alist nil)
7139           (setq gnus-article-mime-handles nil)))
7140       ;; If we have several article buffers, we kill them at exit.
7141       (unless gnus-single-article-buffer
7142         (gnus-kill-buffer gnus-article-buffer)
7143         (gnus-kill-buffer gnus-original-article-buffer)
7144         (setq gnus-article-current nil))
7145       (if (not gnus-kill-summary-on-exit)
7146           (gnus-deaden-summary)
7147         (gnus-close-group group)
7148         (gnus-summary-clear-local-variables)
7149         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7150           (gnus-summary-clear-local-variables))
7151         (gnus-kill-buffer gnus-summary-buffer))
7152       (unless gnus-single-article-buffer
7153         (setq gnus-article-current nil))
7154       (when gnus-use-trees
7155         (gnus-tree-close group))
7156       (gnus-async-prefetch-remove-group group)
7157       (when (get-buffer gnus-article-buffer)
7158         (bury-buffer gnus-article-buffer))
7159       ;; Return to the group buffer.
7160       (gnus-configure-windows 'group 'force)
7161       ;; Clear the current group name.
7162       (setq gnus-newsgroup-name nil)
7163       (unless (gnus-ephemeral-group-p group)
7164         (gnus-group-update-group group))
7165       (when (equal (gnus-group-group-name) group)
7166         (gnus-group-next-unread-group 1))
7167       (when quit-config
7168         (gnus-handle-ephemeral-exit quit-config)))))
7169
7170 (defun gnus-handle-ephemeral-exit (quit-config)
7171   "Handle movement when leaving an ephemeral group.
7172 The state which existed when entering the ephemeral is reset."
7173   (if (not (buffer-name (car quit-config)))
7174       (gnus-configure-windows 'group 'force)
7175     (set-buffer (car quit-config))
7176     (cond ((eq major-mode 'gnus-summary-mode)
7177            (gnus-set-global-variables))
7178           ((eq major-mode 'gnus-article-mode)
7179            (save-current-buffer
7180              ;; The `gnus-summary-buffer' variable may point
7181              ;; to the old summary buffer when using a single
7182              ;; article buffer.
7183              (unless (gnus-buffer-live-p gnus-summary-buffer)
7184                (set-buffer gnus-group-buffer))
7185              (set-buffer gnus-summary-buffer)
7186              (gnus-set-global-variables))))
7187     (if (or (eq (cdr quit-config) 'article)
7188             (eq (cdr quit-config) 'pick))
7189         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7190             (gnus-configure-windows 'pick 'force)
7191           (gnus-configure-windows (cdr quit-config) 'force))
7192       (gnus-configure-windows (cdr quit-config) 'force))
7193     (when (eq major-mode 'gnus-summary-mode)
7194       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7195                                                      next-unread-noselect))
7196           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7197                                   'next-noselect)
7198                               (gnus-summary-next-subject 1 nil t))
7199                              ((eq gnus-auto-select-on-ephemeral-exit
7200                                   'next-unread-noselect)
7201                               (gnus-summary-next-subject 1 t t))))
7202             ;; Hide the article buffer which displays the article different
7203             ;; from the one that the cursor points to in the summary buffer.
7204             (gnus-configure-windows 'summary 'force))
7205         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7206                (gnus-summary-next-subject 1))
7207               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7208                (gnus-summary-next-subject 1 t))))
7209       (gnus-summary-recenter)
7210       (gnus-summary-position-point))))
7211
7212 ;;; Dead summaries.
7213
7214 (defvar gnus-dead-summary-mode-map nil)
7215
7216 (unless gnus-dead-summary-mode-map
7217   (setq gnus-dead-summary-mode-map (make-keymap))
7218   (suppress-keymap gnus-dead-summary-mode-map)
7219   (substitute-key-definition
7220    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7221   (dolist (key '("\C-d" "\r" "\177" [delete]))
7222     (define-key gnus-dead-summary-mode-map
7223       key 'gnus-summary-wake-up-the-dead))
7224   (dolist (key '("q" "Q"))
7225     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7226
7227 (defvar gnus-dead-summary-mode nil
7228   "Minor mode for Gnus summary buffers.")
7229
7230 (defun gnus-dead-summary-mode (&optional arg)
7231   "Minor mode for Gnus summary buffers."
7232   (interactive "P")
7233   (when (eq major-mode 'gnus-summary-mode)
7234     (make-local-variable 'gnus-dead-summary-mode)
7235     (setq gnus-dead-summary-mode
7236           (if (null arg) (not gnus-dead-summary-mode)
7237             (> (prefix-numeric-value arg) 0)))
7238     (when gnus-dead-summary-mode
7239       (add-minor-mode
7240        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7241
7242 (defun gnus-deaden-summary ()
7243   "Make the current summary buffer into a dead summary buffer."
7244   ;; Kill any previous dead summary buffer.
7245   (when (and gnus-dead-summary
7246              (buffer-name gnus-dead-summary))
7247     (with-current-buffer gnus-dead-summary
7248       (when gnus-dead-summary-mode
7249         (kill-buffer (current-buffer)))))
7250   ;; Make this the current dead summary.
7251   (setq gnus-dead-summary (current-buffer))
7252   (gnus-dead-summary-mode 1)
7253   (let ((name (buffer-name)))
7254     (when (string-match "Summary" name)
7255       (rename-buffer
7256        (concat (substring name 0 (match-beginning 0)) "Dead "
7257                (substring name (match-beginning 0)))
7258        t)
7259       (bury-buffer))))
7260
7261 (defun gnus-kill-or-deaden-summary (buffer)
7262   "Kill or deaden the summary BUFFER."
7263   (save-excursion
7264     (when (and (buffer-name buffer)
7265                (not gnus-single-article-buffer))
7266       (with-current-buffer buffer
7267         (gnus-kill-buffer gnus-article-buffer)
7268         (gnus-kill-buffer gnus-original-article-buffer)))
7269     (cond
7270      ;; Kill the buffer.
7271      (gnus-kill-summary-on-exit
7272       (when (and gnus-use-trees
7273                  (gnus-buffer-exists-p buffer))
7274         (with-current-buffer buffer
7275           (gnus-tree-close gnus-newsgroup-name)))
7276       (gnus-kill-buffer buffer))
7277      ;; Deaden the buffer.
7278      ((gnus-buffer-exists-p buffer)
7279       (with-current-buffer buffer
7280         (gnus-deaden-summary))))))
7281
7282 (defun gnus-summary-wake-up-the-dead (&rest args)
7283   "Wake up the dead summary buffer."
7284   (interactive)
7285   (gnus-dead-summary-mode -1)
7286   (let ((name (buffer-name)))
7287     (when (string-match "Dead " name)
7288       (rename-buffer
7289        (concat (substring name 0 (match-beginning 0))
7290                (substring name (match-end 0)))
7291        t)))
7292   (gnus-message 3 "This dead summary is now alive again"))
7293
7294 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7295 (defun gnus-summary-fetch-faq (&optional faq-dir)
7296   "Fetch the FAQ for the current group.
7297 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7298 in."
7299   (interactive
7300    (list
7301     (when current-prefix-arg
7302       (completing-read
7303        "FAQ dir: " (and (listp gnus-group-faq-directory)
7304                         (mapcar 'list
7305                                 gnus-group-faq-directory))))))
7306   (let (gnus-faq-buffer)
7307     (when (setq gnus-faq-buffer
7308                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7309       (gnus-configure-windows 'summary-faq))))
7310
7311 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7312 (defun gnus-summary-describe-group (&optional force)
7313   "Describe the current newsgroup."
7314   (interactive "P")
7315   (gnus-group-describe-group force gnus-newsgroup-name))
7316
7317 (defun gnus-summary-describe-briefly ()
7318   "Describe summary mode commands briefly."
7319   (interactive)
7320   (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")))
7321
7322 ;; Walking around group mode buffer from summary mode.
7323
7324 (defun gnus-summary-next-group (&optional no-article target-group backward)
7325   "Exit current newsgroup and then select next unread newsgroup.
7326 If prefix argument NO-ARTICLE is non-nil, no article is selected
7327 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7328 previous group instead."
7329   (interactive "P")
7330   ;; Stop pre-fetching.
7331   (gnus-async-halt-prefetch)
7332   (let ((current-group gnus-newsgroup-name)
7333         (current-buffer (current-buffer))
7334         entered)
7335     ;; First we semi-exit this group to update Xrefs and all variables.
7336     ;; We can't do a real exit, because the window conf must remain
7337     ;; the same in case the user is prompted for info, and we don't
7338     ;; want the window conf to change before that...
7339     (gnus-summary-exit t)
7340     (while (not entered)
7341       ;; Then we find what group we are supposed to enter.
7342       (set-buffer gnus-group-buffer)
7343       (gnus-group-jump-to-group current-group)
7344       (setq target-group
7345             (or target-group
7346                 (if (eq gnus-keep-same-level 'best)
7347                     (gnus-summary-best-group gnus-newsgroup-name)
7348                   (gnus-summary-search-group backward gnus-keep-same-level))))
7349       (if (not target-group)
7350           ;; There are no further groups, so we return to the group
7351           ;; buffer.
7352           (progn
7353             (gnus-message 5 "Returning to the group buffer")
7354             (setq entered t)
7355             (when (gnus-buffer-live-p current-buffer)
7356               (set-buffer current-buffer)
7357               (gnus-summary-exit))
7358             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7359         ;; We try to enter the target group.
7360         (gnus-group-jump-to-group target-group)
7361         (let ((unreads (gnus-group-group-unread)))
7362           (if (and (or (eq t unreads)
7363                        (and unreads (not (zerop unreads))))
7364                    (gnus-summary-read-group
7365                     target-group nil no-article
7366                     (and (buffer-name current-buffer) current-buffer)
7367                     nil backward))
7368               (setq entered t)
7369             (setq current-group target-group
7370                   target-group nil)))))))
7371
7372 (defun gnus-summary-prev-group (&optional no-article)
7373   "Exit current newsgroup and then select previous unread newsgroup.
7374 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7375   (interactive "P")
7376   (gnus-summary-next-group no-article nil t))
7377
7378 ;; Walking around summary lines.
7379
7380 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7381   "Go to the first subject satisfying any non-nil constraint.
7382 If UNREAD is non-nil, the article should be unread.
7383 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7384 If UNSEEN is non-nil, the article should be unseen.
7385 Returns the article selected or nil if there are no matching articles."
7386   (interactive "P")
7387   (cond
7388    ;; Empty summary.
7389    ((null gnus-newsgroup-data)
7390     (gnus-message 3 "No articles in the group")
7391     nil)
7392    ;; Pick the first article.
7393    ((not (or unread undownloaded unseen))
7394     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7395     (gnus-data-number (car gnus-newsgroup-data)))
7396    ;; Find the first unread article.
7397    (t
7398     (let ((data gnus-newsgroup-data))
7399       (while (and data
7400                   (let ((num (gnus-data-number (car data))))
7401                     (or (memq num gnus-newsgroup-unfetched)
7402                         (not (or (and unread
7403                                       (memq num gnus-newsgroup-unreads))
7404                                  (and undownloaded
7405                                       (memq num gnus-newsgroup-undownloaded))
7406                                  (and unseen
7407                                       (memq num gnus-newsgroup-unseen)))))))
7408         (setq data (cdr data)))
7409       (prog1
7410           (if data
7411               (progn
7412                 (goto-char (gnus-data-pos (car data)))
7413                 (gnus-data-number (car data)))
7414             (gnus-message 3 "No more%s articles"
7415                           (let* ((r (when unread " unread"))
7416                                  (d (when undownloaded " undownloaded"))
7417                                  (s (when unseen " unseen"))
7418                                  (l (delq nil (list r d s))))
7419                             (cond ((= 3 (length l))
7420                                    (concat r "," d ", or" s))
7421                                   ((= 2 (length l))
7422                                    (concat (car l) ", or" (cadr l)))
7423                                   ((= 1 (length l))
7424                                    (car l))
7425                                   (t
7426                                    ""))))
7427             nil
7428             )
7429         (gnus-summary-position-point))))))
7430
7431 (defun gnus-summary-next-subject (n &optional unread dont-display)
7432   "Go to next N'th summary line.
7433 If N is negative, go to the previous N'th subject line.
7434 If UNREAD is non-nil, only unread articles are selected.
7435 The difference between N and the actual number of steps taken is
7436 returned."
7437   (interactive "p")
7438   (let ((backward (< n 0))
7439         (n (abs n)))
7440     (while (and (> n 0)
7441                 (if backward
7442                     (gnus-summary-find-prev unread)
7443                   (gnus-summary-find-next unread)))
7444       (unless (zerop (setq n (1- n)))
7445         (gnus-summary-show-thread)))
7446     (when (/= 0 n)
7447       (gnus-message 7 "No more%s articles"
7448                     (if unread " unread" "")))
7449     (unless dont-display
7450       (gnus-summary-recenter)
7451       (gnus-summary-position-point))
7452     n))
7453
7454 (defun gnus-summary-next-unread-subject (n)
7455   "Go to next N'th unread summary line."
7456   (interactive "p")
7457   (gnus-summary-next-subject n t))
7458
7459 (defun gnus-summary-prev-subject (n &optional unread)
7460   "Go to previous N'th summary line.
7461 If optional argument UNREAD is non-nil, only unread article is selected."
7462   (interactive "p")
7463   (gnus-summary-next-subject (- n) unread))
7464
7465 (defun gnus-summary-prev-unread-subject (n)
7466   "Go to previous N'th unread summary line."
7467   (interactive "p")
7468   (gnus-summary-next-subject (- n) t))
7469
7470 (defun gnus-summary-goto-subjects (articles)
7471   "Insert the subject header for ARTICLES in the current buffer."
7472   (save-excursion
7473     (dolist (article articles)
7474       (gnus-summary-goto-subject article t)))
7475   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7476   (gnus-summary-position-point))
7477
7478 (defun gnus-summary-goto-subject (article &optional force silent)
7479   "Go to the subject line of ARTICLE.
7480 If FORCE, also allow jumping to articles not currently shown."
7481   (interactive "nArticle number: ")
7482   (unless (numberp article)
7483     (error "Article %s is not a number" article))
7484   (let ((b (point))
7485         (data (gnus-data-find article)))
7486     ;; We read in the article if we have to.
7487     (and (not data)
7488          force
7489          (gnus-summary-insert-subject
7490           article
7491           (if (or (numberp force) (vectorp force)) force)
7492           t)
7493          (setq data (gnus-data-find article)))
7494     (goto-char b)
7495     (if (not data)
7496         (progn
7497           (unless silent
7498             (gnus-message 3 "Can't find article %d" article))
7499           nil)
7500       (let ((pt (gnus-data-pos data)))
7501         (goto-char pt)
7502         (gnus-summary-set-article-display-arrow pt))
7503       (gnus-summary-position-point)
7504       article)))
7505
7506 ;; Walking around summary lines with displaying articles.
7507
7508 (defun gnus-summary-expand-window (&optional arg)
7509   "Make the summary buffer take up the entire Emacs frame.
7510 Given a prefix, will force an `article' buffer configuration."
7511   (interactive "P")
7512   (if arg
7513       (gnus-configure-windows 'article 'force)
7514     (gnus-configure-windows 'summary 'force)))
7515
7516 (defun gnus-summary-display-article (article &optional all-header)
7517   "Display ARTICLE in article buffer."
7518   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7519                (with-current-buffer gnus-article-buffer
7520                  (eq major-mode 'gnus-article-mode)))
7521     (gnus-article-setup-buffer))
7522   (gnus-set-global-variables)
7523   (with-current-buffer gnus-article-buffer
7524     (setq gnus-article-charset gnus-newsgroup-charset)
7525     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7526     (mm-enable-multibyte))
7527   (if (null article)
7528       nil
7529     (prog1
7530         (if gnus-summary-display-article-function
7531             (funcall gnus-summary-display-article-function article all-header)
7532           (gnus-article-prepare article all-header))
7533       (gnus-run-hooks 'gnus-select-article-hook)
7534       (when (and gnus-current-article
7535                  (not (zerop gnus-current-article)))
7536         (gnus-summary-goto-subject gnus-current-article))
7537       (gnus-summary-recenter)
7538       (when (and gnus-use-trees gnus-show-threads)
7539         (gnus-possibly-generate-tree article)
7540         (gnus-highlight-selected-tree article))
7541       ;; Successfully display article.
7542       (gnus-article-set-window-start
7543        (cdr (assq article gnus-newsgroup-bookmarks))))))
7544
7545 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7546   "Select the current article.
7547 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7548 non-nil, the article will be re-fetched even if it already present in
7549 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7550 be displayed."
7551   ;; Make sure we are in the summary buffer to work around bbdb bug.
7552   (unless (eq major-mode 'gnus-summary-mode)
7553     (set-buffer gnus-summary-buffer))
7554   (let ((article (or article (gnus-summary-article-number)))
7555         (all-headers (not (not all-headers))) ;Must be t or nil.
7556         gnus-summary-display-article-function)
7557     (and (not pseudo)
7558          (gnus-summary-article-pseudo-p article)
7559          (error "This is a pseudo-article"))
7560     (with-current-buffer gnus-summary-buffer
7561       (if (or (and gnus-single-article-buffer
7562                    (or (null gnus-current-article)
7563                        (null gnus-article-current)
7564                        (null (get-buffer gnus-article-buffer))
7565                        (not (eq article (cdr gnus-article-current)))
7566                        (not (equal (car gnus-article-current)
7567                                    gnus-newsgroup-name))))
7568               (and (not gnus-single-article-buffer)
7569                    (or (null gnus-current-article)
7570                        (not (eq gnus-current-article article))))
7571               force)
7572           ;; The requested article is different from the current article.
7573           (progn
7574             (gnus-summary-display-article article all-headers)
7575             (when (gnus-buffer-live-p gnus-article-buffer)
7576               (with-current-buffer gnus-article-buffer
7577                 (if (not gnus-article-decoded-p) ;; a local variable
7578                     (mm-disable-multibyte))))
7579             (gnus-article-set-window-start
7580              (cdr (assq article gnus-newsgroup-bookmarks)))
7581             article)
7582         'old))))
7583
7584 (defun gnus-summary-force-verify-and-decrypt ()
7585   "Display buttons for signed/encrypted parts and verify/decrypt them."
7586   (interactive)
7587   (let ((mm-verify-option 'known)
7588         (mm-decrypt-option 'known)
7589         (gnus-article-emulate-mime t)
7590         (gnus-buttonized-mime-types (append (list "multipart/signed"
7591                                                   "multipart/encrypted")
7592                                             gnus-buttonized-mime-types)))
7593     (gnus-summary-select-article nil 'force)))
7594
7595 (defun gnus-summary-set-current-mark (&optional current-mark)
7596   "Obsolete function."
7597   nil)
7598
7599 (defun gnus-summary-next-article (&optional unread subject backward push)
7600   "Select the next article.
7601 If UNREAD, only unread articles are selected.
7602 If SUBJECT, only articles with SUBJECT are selected.
7603 If BACKWARD, the previous article is selected instead of the next."
7604   (interactive "P")
7605   ;; Make sure we are in the summary buffer.
7606   (unless (eq major-mode 'gnus-summary-mode)
7607     (set-buffer gnus-summary-buffer))
7608   (cond
7609    ;; Is there such an article?
7610    ((and (gnus-summary-search-forward unread subject backward)
7611          (or (gnus-summary-display-article (gnus-summary-article-number))
7612              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7613     (gnus-summary-position-point))
7614    ;; If not, we try the first unread, if that is wanted.
7615    ((and subject
7616          gnus-auto-select-same
7617          (gnus-summary-first-unread-article))
7618     (gnus-summary-position-point)
7619     (gnus-message 6 "Wrapped"))
7620    ;; Try to get next/previous article not displayed in this group.
7621    ((and gnus-auto-extend-newsgroup
7622          (not unread) (not subject))
7623     (gnus-summary-goto-article
7624      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7625      nil (count-lines (point-min) (point))))
7626    ;; Go to next/previous group.
7627    (t
7628     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7629       (gnus-summary-jump-to-group gnus-newsgroup-name))
7630     (let ((cmd last-command-char)
7631           (point
7632            (with-current-buffer gnus-group-buffer
7633              (point)))
7634           (group
7635            (if (eq gnus-keep-same-level 'best)
7636                (gnus-summary-best-group gnus-newsgroup-name)
7637              (gnus-summary-search-group backward gnus-keep-same-level))))
7638       ;; For some reason, the group window gets selected.  We change
7639       ;; it back.
7640       (select-window (get-buffer-window (current-buffer)))
7641       ;; Select next unread newsgroup automagically.
7642       (cond
7643        ((or (not gnus-auto-select-next)
7644             (not cmd))
7645         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7646        ((or (eq gnus-auto-select-next 'quietly)
7647             (and (eq gnus-auto-select-next 'slightly-quietly)
7648                  push)
7649             (and (eq gnus-auto-select-next 'almost-quietly)
7650                  (gnus-summary-last-article-p)))
7651         ;; Select quietly.
7652         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7653             (gnus-summary-exit)
7654           (gnus-message 7 "No more%s articles (%s)..."
7655                         (if unread " unread" "")
7656                         (if group (concat "selecting " group)
7657                           "exiting"))
7658           (gnus-summary-next-group nil group backward)))
7659        (t
7660         (when (gnus-key-press-event-p last-input-event)
7661           (gnus-summary-walk-group-buffer
7662            gnus-newsgroup-name cmd unread backward point))))))))
7663
7664 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7665   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7666                       (?\C-p (gnus-group-prev-unread-group 1))))
7667         (cursor-in-echo-area t)
7668         keve key group ended prompt)
7669     (with-current-buffer gnus-group-buffer
7670       (goto-char start)
7671       (setq group
7672             (if (eq gnus-keep-same-level 'best)
7673                 (gnus-summary-best-group gnus-newsgroup-name)
7674               (gnus-summary-search-group backward gnus-keep-same-level))))
7675     (while (not ended)
7676       (setq prompt
7677             (format
7678              "No more%s articles%s " (if unread " unread" "")
7679              (if (and group
7680                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7681                  (format " (Type %s for %s [%s])"
7682                          (single-key-description cmd)
7683                          (gnus-group-decoded-name group)
7684                          (gnus-group-unread group))
7685                (format " (Type %s to exit %s)"
7686                        (single-key-description cmd)
7687                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7688       ;; Confirm auto selection.
7689       (setq key (car (setq keve (gnus-read-event-char prompt)))
7690             ended t)
7691       (cond
7692        ((assq key keystrokes)
7693         (let ((obuf (current-buffer)))
7694           (switch-to-buffer gnus-group-buffer)
7695           (when group
7696             (gnus-group-jump-to-group group))
7697           (eval (cadr (assq key keystrokes)))
7698           (setq group (gnus-group-group-name))
7699           (switch-to-buffer obuf))
7700         (setq ended nil))
7701        ((equal key cmd)
7702         (if (or (not group)
7703                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7704             (gnus-summary-exit)
7705           (gnus-summary-next-group nil group backward)))
7706        (t
7707         (push (cdr keve) unread-command-events))))))
7708
7709 (defun gnus-summary-next-unread-article ()
7710   "Select unread article after current one."
7711   (interactive)
7712   (gnus-summary-next-article
7713    (or (not (eq gnus-summary-goto-unread 'never))
7714        (gnus-summary-last-article-p (gnus-summary-article-number)))
7715    (and gnus-auto-select-same
7716         (gnus-summary-article-subject))))
7717
7718 (defun gnus-summary-prev-article (&optional unread subject)
7719   "Select the article before the current one.
7720 If UNREAD is non-nil, only unread articles are selected."
7721   (interactive "P")
7722   (gnus-summary-next-article unread subject t))
7723
7724 (defun gnus-summary-prev-unread-article ()
7725   "Select unread article before current one."
7726   (interactive)
7727   (gnus-summary-prev-article
7728    (or (not (eq gnus-summary-goto-unread 'never))
7729        (gnus-summary-first-article-p (gnus-summary-article-number)))
7730    (and gnus-auto-select-same
7731         (gnus-summary-article-subject))))
7732
7733 (defun gnus-summary-next-page (&optional lines circular stop)
7734   "Show next page of the selected article.
7735 If at the end of the current article, select the next article.
7736 LINES says how many lines should be scrolled up.
7737
7738 If CIRCULAR is non-nil, go to the start of the article instead of
7739 selecting the next article when reaching the end of the current
7740 article.
7741
7742 If STOP is non-nil, just stop when reaching the end of the message.
7743
7744 Also see the variable `gnus-article-skip-boring'."
7745   (interactive "P")
7746   (setq gnus-summary-buffer (current-buffer))
7747   (gnus-set-global-variables)
7748   (let ((article (gnus-summary-article-number))
7749         (article-window (get-buffer-window gnus-article-buffer t))
7750         endp)
7751     ;; If the buffer is empty, we have no article.
7752     (unless article
7753       (error "No article to select"))
7754     (gnus-configure-windows 'article)
7755     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7756         (if (and (eq gnus-summary-goto-unread 'never)
7757                  (not (gnus-summary-last-article-p article)))
7758             (gnus-summary-next-article)
7759           (gnus-summary-next-unread-article))
7760       (if (or (null gnus-current-article)
7761               (null gnus-article-current)
7762               (/= article (cdr gnus-article-current))
7763               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7764           ;; Selected subject is different from current article's.
7765           (gnus-summary-display-article article)
7766         (when article-window
7767           (gnus-eval-in-buffer-window gnus-article-buffer
7768             (setq endp (or (gnus-article-next-page lines)
7769                            (gnus-article-only-boring-p))))
7770           (when endp
7771             (cond (stop
7772                    (gnus-message 3 "End of message"))
7773                   (circular
7774                    (gnus-summary-beginning-of-article))
7775                   (lines
7776                    (gnus-message 3 "End of message"))
7777                   ((null lines)
7778                    (if (and (eq gnus-summary-goto-unread 'never)
7779                             (not (gnus-summary-last-article-p article)))
7780                        (gnus-summary-next-article)
7781                      (gnus-summary-next-unread-article))))))))
7782     (gnus-summary-recenter)
7783     (gnus-summary-position-point)))
7784
7785 (defun gnus-summary-prev-page (&optional lines move)
7786   "Show previous page of selected article.
7787 Argument LINES specifies lines to be scrolled down.
7788 If MOVE, move to the previous unread article if point is at
7789 the beginning of the buffer."
7790   (interactive "P")
7791   (let ((article (gnus-summary-article-number))
7792         (article-window (get-buffer-window gnus-article-buffer t))
7793         endp)
7794     (gnus-configure-windows 'article)
7795     (if (or (null gnus-current-article)
7796             (null gnus-article-current)
7797             (/= article (cdr gnus-article-current))
7798             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7799         ;; Selected subject is different from current article's.
7800         (gnus-summary-display-article article)
7801       (gnus-summary-recenter)
7802       (when article-window
7803         (gnus-eval-in-buffer-window gnus-article-buffer
7804           (setq endp (gnus-article-prev-page lines)))
7805         (when (and move endp)
7806           (cond (lines
7807                  (gnus-message 3 "Beginning of message"))
7808                 ((null lines)
7809                  (if (and (eq gnus-summary-goto-unread 'never)
7810                           (not (gnus-summary-first-article-p article)))
7811                      (gnus-summary-prev-article)
7812                    (gnus-summary-prev-unread-article))))))))
7813   (gnus-summary-position-point))
7814
7815 (defun gnus-summary-prev-page-or-article (&optional lines)
7816   "Show previous page of selected article.
7817 Argument LINES specifies lines to be scrolled down.
7818 If at the beginning of the article, go to the next article."
7819   (interactive "P")
7820   (gnus-summary-prev-page lines t))
7821
7822 (defun gnus-summary-scroll-up (lines)
7823   "Scroll up (or down) one line current article.
7824 Argument LINES specifies lines to be scrolled up (or down if negative)."
7825   (interactive "p")
7826   (gnus-configure-windows 'article)
7827   (gnus-summary-show-thread)
7828   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7829     (gnus-eval-in-buffer-window gnus-article-buffer
7830       (cond ((> lines 0)
7831              (when (gnus-article-next-page lines)
7832                (gnus-message 3 "End of message")))
7833             ((< lines 0)
7834              (gnus-article-prev-page (- lines))))))
7835   (gnus-summary-recenter)
7836   (gnus-summary-position-point))
7837
7838 (defun gnus-summary-scroll-down (lines)
7839   "Scroll down (or up) one line current article.
7840 Argument LINES specifies lines to be scrolled down (or up if negative)."
7841   (interactive "p")
7842   (gnus-summary-scroll-up (- lines)))
7843
7844 (defun gnus-summary-next-same-subject ()
7845   "Select next article which has the same subject as current one."
7846   (interactive)
7847   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7848
7849 (defun gnus-summary-prev-same-subject ()
7850   "Select previous article which has the same subject as current one."
7851   (interactive)
7852   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7853
7854 (defun gnus-summary-next-unread-same-subject ()
7855   "Select next unread article which has the same subject as current one."
7856   (interactive)
7857   (gnus-summary-next-article t (gnus-summary-article-subject)))
7858
7859 (defun gnus-summary-prev-unread-same-subject ()
7860   "Select previous unread article which has the same subject as current one."
7861   (interactive)
7862   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7863
7864 (defun gnus-summary-first-unread-article ()
7865   "Select the first unread article.
7866 Return nil if there are no unread articles."
7867   (interactive)
7868   (prog1
7869       (when (gnus-summary-first-subject t)
7870         (gnus-summary-show-thread)
7871         (gnus-summary-first-subject t)
7872         (gnus-summary-display-article (gnus-summary-article-number)))
7873     (gnus-summary-position-point)))
7874
7875 (defun gnus-summary-first-unread-subject ()
7876   "Place the point on the subject line of the first unread article.
7877 Return nil if there are no unread articles."
7878   (interactive)
7879   (prog1
7880       (when (gnus-summary-first-subject t)
7881         (gnus-summary-show-thread)
7882         (gnus-summary-first-subject t))
7883     (gnus-summary-position-point)))
7884
7885 (defun gnus-summary-first-unseen-subject ()
7886   "Place the point on the subject line of the first unseen article.
7887 Return nil if there are no unseen articles."
7888   (interactive)
7889   (prog1
7890       (when (gnus-summary-first-subject nil nil t)
7891         (gnus-summary-show-thread)
7892         (gnus-summary-first-subject nil nil t))
7893     (gnus-summary-position-point)))
7894
7895 (defun gnus-summary-first-unseen-or-unread-subject ()
7896   "Place the point on the subject line of the first unseen article or,
7897 if all article have been seen, on the subject line of the first unread
7898 article."
7899   (interactive)
7900   (prog1
7901       (unless (when (gnus-summary-first-subject nil nil t)
7902                 (gnus-summary-show-thread)
7903                 (gnus-summary-first-subject nil nil t))
7904         (when (gnus-summary-first-subject t)
7905           (gnus-summary-show-thread)
7906           (gnus-summary-first-subject t)))
7907     (gnus-summary-position-point)))
7908
7909 (defun gnus-summary-first-article ()
7910   "Select the first article.
7911 Return nil if there are no articles."
7912   (interactive)
7913   (prog1
7914       (when (gnus-summary-first-subject)
7915         (gnus-summary-show-thread)
7916         (gnus-summary-first-subject)
7917         (gnus-summary-display-article (gnus-summary-article-number)))
7918     (gnus-summary-position-point)))
7919
7920 (defun gnus-summary-best-unread-article (&optional arg)
7921   "Select the unread article with the highest score.
7922 If given a prefix argument, select the next unread article that has a
7923 score higher than the default score."
7924   (interactive "P")
7925   (let ((article (if arg
7926                      (gnus-summary-better-unread-subject)
7927                    (gnus-summary-best-unread-subject))))
7928     (if article
7929         (gnus-summary-goto-article article)
7930       (error "No unread articles"))))
7931
7932 (defun gnus-summary-best-unread-subject ()
7933   "Select the unread subject with the highest score."
7934   (interactive)
7935   (let ((best -1000000)
7936         (data gnus-newsgroup-data)
7937         article score)
7938     (while data
7939       (and (gnus-data-unread-p (car data))
7940            (> (setq score
7941                     (gnus-summary-article-score (gnus-data-number (car data))))
7942               best)
7943            (setq best score
7944                  article (gnus-data-number (car data))))
7945       (setq data (cdr data)))
7946     (when article
7947       (gnus-summary-goto-subject article))
7948     (gnus-summary-position-point)
7949     article))
7950
7951 (defun gnus-summary-better-unread-subject ()
7952   "Select the first unread subject that has a score over the default score."
7953   (interactive)
7954   (let ((data gnus-newsgroup-data)
7955         article score)
7956     (while (and (setq article (gnus-data-number (car data)))
7957                 (or (gnus-data-read-p (car data))
7958                     (not (> (gnus-summary-article-score article)
7959                             gnus-summary-default-score))))
7960       (setq data (cdr data)))
7961     (when article
7962       (gnus-summary-goto-subject article))
7963     (gnus-summary-position-point)
7964     article))
7965
7966 (defun gnus-summary-last-subject ()
7967   "Go to the last displayed subject line in the group."
7968   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7969     (when article
7970       (gnus-summary-goto-subject article))))
7971
7972 (defun gnus-summary-goto-article (article &optional all-headers force)
7973   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7974 If ALL-HEADERS is non-nil, no header lines are hidden.
7975 If FORCE, go to the article even if it isn't displayed.  If FORCE
7976 is a number, it is the line the article is to be displayed on."
7977   (interactive
7978    (list
7979     (completing-read
7980      "Article number or Message-ID: "
7981      (mapcar (lambda (number) (list (int-to-string number)))
7982              gnus-newsgroup-limit))
7983     current-prefix-arg
7984     t))
7985   (prog1
7986       (if (and (stringp article)
7987                (string-match "@\\|%40" article))
7988           (gnus-summary-refer-article article)
7989         (when (stringp article)
7990           (setq article (string-to-number article)))
7991         (if (gnus-summary-goto-subject article force)
7992             (gnus-summary-display-article article all-headers)
7993           (gnus-message 4 "Couldn't go to article %s" article) nil))
7994     (gnus-summary-position-point)))
7995
7996 (defun gnus-summary-goto-last-article ()
7997   "Go to the previously read article."
7998   (interactive)
7999   (prog1
8000       (when gnus-last-article
8001         (gnus-summary-goto-article gnus-last-article nil t))
8002     (gnus-summary-position-point)))
8003
8004 (defun gnus-summary-pop-article (number)
8005   "Pop one article off the history and go to the previous.
8006 NUMBER articles will be popped off."
8007   (interactive "p")
8008   (let (to)
8009     (setq gnus-newsgroup-history
8010           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8011     (if to
8012         (gnus-summary-goto-article (car to) nil t)
8013       (error "Article history empty")))
8014   (gnus-summary-position-point))
8015
8016 ;; Summary commands and functions for limiting the summary buffer.
8017
8018 (defun gnus-summary-limit-to-articles (n)
8019   "Limit the summary buffer to the next N articles.
8020 If not given a prefix, use the process marked articles instead."
8021   (interactive "P")
8022   (prog1
8023       (let ((articles (gnus-summary-work-articles n)))
8024         (setq gnus-newsgroup-processable nil)
8025         (gnus-summary-limit articles))
8026     (gnus-summary-position-point)))
8027
8028 (defun gnus-summary-pop-limit (&optional total)
8029   "Restore the previous limit.
8030 If given a prefix, remove all limits."
8031   (interactive "P")
8032   (when total
8033     (setq gnus-newsgroup-limits
8034           (list (mapcar (lambda (h) (mail-header-number h))
8035                         gnus-newsgroup-headers))))
8036   (unless gnus-newsgroup-limits
8037     (error "No limit to pop"))
8038   (prog1
8039       (gnus-summary-limit nil 'pop)
8040     (gnus-summary-position-point)))
8041
8042 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8043   "Limit the summary buffer to articles that have subjects that match a regexp.
8044 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8045   (interactive
8046    (list (read-string (if current-prefix-arg
8047                           "Exclude subject (regexp): "
8048                         "Limit to subject (regexp): "))
8049          nil current-prefix-arg))
8050   (unless header
8051     (setq header "subject"))
8052   (when (not (equal "" subject))
8053     (prog1
8054         (let ((articles (gnus-summary-find-matching
8055                          (or header "subject") subject 'all nil nil
8056                          not-matching)))
8057           (unless articles
8058             (error "Found no matches for \"%s\"" subject))
8059           (gnus-summary-limit articles))
8060       (gnus-summary-position-point))))
8061
8062 (defun gnus-summary-limit-to-author (from &optional not-matching)
8063   "Limit the summary buffer to articles that have authors that match a regexp.
8064 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8065   (interactive
8066    (list (read-string (if current-prefix-arg
8067                           "Exclude author (regexp): "
8068                         "Limit to author (regexp): "))
8069          current-prefix-arg))
8070   (gnus-summary-limit-to-subject from "from" not-matching))
8071
8072 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8073   "Limit the summary buffer to articles with the given RECIPIENT.
8074
8075 If NOT-MATCHING, exclude RECIPIENT.
8076
8077 To and Cc headers are checked.  You need to include them in
8078 `nnmail-extra-headers'."
8079   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8080   (interactive
8081    (list (read-string (format "%s recipient (regexp): "
8082                               (if current-prefix-arg "Exclude" "Limit to")))
8083          current-prefix-arg))
8084   (when (not (equal "" recipient))
8085     (prog1 (let* ((to
8086                    (if (memq 'To nnmail-extra-headers)
8087                        (gnus-summary-find-matching
8088                         (cons 'extra 'To) recipient 'all nil nil
8089                         not-matching)
8090                      (gnus-message
8091                       1 "`To' isn't present in `nnmail-extra-headers'")
8092                      (sit-for 1)
8093                      nil))
8094                   (cc
8095                    (if (memq 'Cc nnmail-extra-headers)
8096                        (gnus-summary-find-matching
8097                         (cons 'extra 'Cc) recipient 'all nil nil
8098                         not-matching)
8099                      (gnus-message
8100                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8101                      (sit-for 1)
8102                      nil))
8103                   (articles
8104                    (if not-matching
8105                        ;; We need the numbers that are in both lists:
8106                        (mapcar (lambda (a)
8107                                  (and (memq a to) a))
8108                                cc)
8109                      (nconc to cc))))
8110              (unless articles
8111                (error "Found no matches for \"%s\"" recipient))
8112              (gnus-summary-limit articles))
8113       (gnus-summary-position-point))))
8114
8115 (defun gnus-summary-limit-to-address (address &optional not-matching)
8116   "Limit the summary buffer to articles with the given ADDRESS.
8117
8118 If NOT-MATCHING, exclude ADDRESS.
8119
8120 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8121 in `nnmail-extra-headers'."
8122   (interactive
8123    (list (read-string (format "%s address (regexp): "
8124                               (if current-prefix-arg "Exclude" "Limit to")))
8125          current-prefix-arg))
8126   (when (not (equal "" address))
8127     (prog1 (let* ((to
8128                    (if (memq 'To nnmail-extra-headers)
8129                        (gnus-summary-find-matching
8130                         (cons 'extra 'To) address 'all nil nil
8131                         not-matching)
8132                      (gnus-message
8133                       1 "`To' isn't present in `nnmail-extra-headers'")
8134                      (sit-for 1)
8135                      t))
8136                   (cc
8137                    (if (memq 'Cc nnmail-extra-headers)
8138                        (gnus-summary-find-matching
8139                         (cons 'extra 'Cc) address 'all nil nil
8140                         not-matching)
8141                      (gnus-message
8142                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8143                      (sit-for 1)
8144                      t))
8145                   (from
8146                    (gnus-summary-find-matching "from" address
8147                                                'all nil nil not-matching))
8148                   (articles
8149                    (if not-matching
8150                        ;; We need the numbers that are in all lists:
8151                        (if (eq cc t)
8152                            (if (eq to t)
8153                                from
8154                              (mapcar (lambda (a) (car (memq a from))) to))
8155                          (if (eq to t)
8156                              (mapcar (lambda (a) (car (memq a from))) cc)
8157                            (mapcar (lambda (a) (car (memq a from)))
8158                                    (mapcar (lambda (a) (car (memq a to)))
8159                                            cc))))
8160                      (nconc (if (eq to t) nil to)
8161                             (if (eq cc t) nil cc)
8162                             from))))
8163              (unless articles
8164                (error "Found no matches for \"%s\"" address))
8165              (gnus-summary-limit articles))
8166       (gnus-summary-position-point))))
8167
8168 (defun gnus-summary-limit-strange-charsets-predicate (header)
8169   (let ((string (concat (mail-header-subject header)
8170                         (mail-header-from header)))
8171         charset found)
8172     (dotimes (i (1- (length string)))
8173       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8174       (when (string-match "unicode\\|big\\|japanese" charset)
8175         (setq found t)))
8176     found))
8177
8178 (defun gnus-summary-limit-to-predicate (predicate)
8179   "Limit to articles where PREDICATE returns non-nil.
8180 PREDICATE will be called with the header structures of the
8181 articles."
8182   (let ((articles nil)
8183         (case-fold-search t))
8184     (dolist (header gnus-newsgroup-headers)
8185       (when (funcall predicate header)
8186         (push (mail-header-number header) articles)))
8187     (gnus-summary-limit (nreverse articles))))
8188
8189 (defun gnus-summary-limit-to-age (age &optional younger-p)
8190   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8191 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8192 articles that are younger than AGE days."
8193   (interactive
8194    (let ((younger current-prefix-arg)
8195          (days-got nil)
8196          days)
8197      (while (not days-got)
8198        (setq days (if younger
8199                       (read-string "Limit to articles younger than (in days, older when negative): ")
8200                     (read-string
8201                      "Limit to articles older than (in days, younger when negative): ")))
8202        (when (> (length days) 0)
8203          (setq days (read days)))
8204        (if (numberp days)
8205            (progn
8206              (setq days-got t)
8207              (when (< days 0)
8208                (setq younger (not younger))
8209                (setq days (* days -1))))
8210          (message "Please enter a number.")
8211          (sleep-for 1)))
8212      (list days younger)))
8213   (prog1
8214       (let ((data gnus-newsgroup-data)
8215             (cutoff (days-to-time age))
8216             articles d date is-younger)
8217         (while (setq d (pop data))
8218           (when (and (vectorp (gnus-data-header d))
8219                      (setq date (mail-header-date (gnus-data-header d))))
8220             (setq is-younger (time-less-p
8221                               (time-since (condition-case ()
8222                                               (date-to-time date)
8223                                             (error '(0 0))))
8224                               cutoff))
8225             (when (if younger-p
8226                       is-younger
8227                     (not is-younger))
8228               (push (gnus-data-number d) articles))))
8229         (gnus-summary-limit (nreverse articles)))
8230     (gnus-summary-position-point)))
8231
8232 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8233   "Limit the summary buffer to articles that match an 'extra' header."
8234   (interactive
8235    (let ((header
8236           (intern
8237            (gnus-completing-read-with-default
8238             (symbol-name (car gnus-extra-headers))
8239             (if current-prefix-arg
8240                 "Exclude extra header"
8241               "Limit extra header")
8242             (mapcar (lambda (x)
8243                       (cons (symbol-name x) x))
8244                     gnus-extra-headers)
8245             nil
8246             t))))
8247      (list header
8248            (read-string (format "%s header %s (regexp): "
8249                                 (if current-prefix-arg "Exclude" "Limit to")
8250                                 header))
8251            current-prefix-arg)))
8252   (when (not (equal "" regexp))
8253     (prog1
8254         (let ((articles (gnus-summary-find-matching
8255                          (cons 'extra header) regexp 'all nil nil
8256                          not-matching)))
8257           (unless articles
8258             (error "Found no matches for \"%s\"" regexp))
8259           (gnus-summary-limit articles))
8260       (gnus-summary-position-point))))
8261
8262 (defun gnus-summary-limit-to-display-predicate ()
8263   "Limit the summary buffer to the predicated in the `display' group parameter."
8264   (interactive)
8265   (unless gnus-newsgroup-display
8266     (error "There is no `display' group parameter"))
8267   (let (articles)
8268     (dolist (number gnus-newsgroup-articles)
8269       (when (funcall gnus-newsgroup-display)
8270         (push number articles)))
8271     (gnus-summary-limit articles))
8272   (gnus-summary-position-point))
8273
8274 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8275 (make-obsolete
8276  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8277
8278 (defun gnus-summary-limit-to-unread (&optional all)
8279   "Limit the summary buffer to articles that are not marked as read.
8280 If ALL is non-nil, limit strictly to unread articles."
8281   (interactive "P")
8282   (if all
8283       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8284     (gnus-summary-limit-to-marks
8285      ;; Concat all the marks that say that an article is read and have
8286      ;; those removed.
8287      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8288            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8289            gnus-low-score-mark gnus-expirable-mark
8290            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8291            gnus-duplicate-mark gnus-souped-mark)
8292      'reverse)))
8293
8294 (defun gnus-summary-limit-to-headers (match &optional reverse)
8295   "Limit the summary buffer to articles that have headers that match MATCH.
8296 If REVERSE (the prefix), limit to articles that don't match."
8297   (interactive "sMatch headers (regexp): \nP")
8298   (gnus-summary-limit-to-bodies match reverse t))
8299
8300 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8301   "Limit the summary buffer to articles that have bodies that match MATCH.
8302 If REVERSE (the prefix), limit to articles that don't match."
8303   (interactive "sMatch body (regexp): \nP")
8304   (let ((articles nil)
8305         (gnus-select-article-hook nil)  ;Disable hook.
8306         (gnus-article-prepare-hook nil)
8307         (gnus-use-article-prefetch nil)
8308         (gnus-keep-backlog nil)
8309         (gnus-break-pages nil)
8310         (gnus-summary-display-arrow nil)
8311         (gnus-updated-mode-lines nil)
8312         (gnus-auto-center-summary nil)
8313         (gnus-display-mime-function nil))
8314     (dolist (data gnus-newsgroup-data)
8315       (let (gnus-mark-article-hook)
8316         (gnus-summary-select-article t t nil (gnus-data-number data)))
8317       (save-excursion
8318         (set-buffer gnus-article-buffer)
8319         (article-goto-body)
8320         (let* ((case-fold-search t)
8321                (found (if headersp
8322                           (re-search-backward match nil t)
8323                         (re-search-forward match nil t))))
8324           (when (or (and found
8325                          (not reverse))
8326                     (and (not found)
8327                          reverse))
8328             (push (gnus-data-number data) articles)))))
8329     (if (not articles)
8330         (message "No messages matched")
8331       (gnus-summary-limit articles)))
8332   (gnus-summary-position-point))
8333
8334 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8335   "Limit the summary buffer to articles that aren't part on any thread.
8336 If THREADSP (the prefix), limit to articles that are in threads."
8337   (interactive "P")
8338   (let ((articles nil)
8339         thread-articles
8340         threads)
8341     (dolist (thread gnus-newsgroup-threads)
8342       (if (stringp (car thread))
8343           (dolist (thread (cdr thread))
8344             (push thread threads))
8345         (push thread threads)))
8346     (dolist (thread threads)
8347       (setq thread-articles (gnus-articles-in-thread thread))
8348       (when (or (and threadsp
8349                      (> (length thread-articles) 1))
8350                 (and (not threadsp)
8351                      (= (length thread-articles) 1)))
8352         (setq articles (nconc thread-articles articles))))
8353     (if (not articles)
8354         (message "No messages matched")
8355       (gnus-summary-limit articles))
8356     (gnus-summary-position-point)))
8357
8358 (defun gnus-summary-limit-to-replied (&optional unreplied)
8359   "Limit the summary buffer to replied articles.
8360 If UNREPLIED (the prefix), limit to unreplied articles."
8361   (interactive "P")
8362   (if unreplied
8363       (gnus-summary-limit
8364        (gnus-set-difference gnus-newsgroup-articles
8365         gnus-newsgroup-replied))
8366     (gnus-summary-limit gnus-newsgroup-replied))
8367   (gnus-summary-position-point))
8368
8369 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8370 (make-obsolete 'gnus-summary-delete-marked-with
8371                'gnus-summary-limit-exclude-marks)
8372
8373 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8374   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8375 If REVERSE, limit the summary buffer to articles that are marked
8376 with MARKS.  MARKS can either be a string of marks or a list of marks.
8377 Returns how many articles were removed."
8378   (interactive "sMarks: ")
8379   (gnus-summary-limit-to-marks marks t))
8380
8381 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8382   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8383 If REVERSE (the prefix), limit the summary buffer to articles that are
8384 not marked with MARKS.  MARKS can either be a string of marks or a
8385 list of marks.
8386 Returns how many articles were removed."
8387   (interactive "sMarks: \nP")
8388   (prog1
8389       (let ((data gnus-newsgroup-data)
8390             (marks (if (listp marks) marks
8391                      (append marks nil))) ; Transform to list.
8392             articles)
8393         (while data
8394           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8395                   (memq (gnus-data-mark (car data)) marks))
8396             (push (gnus-data-number (car data)) articles))
8397           (setq data (cdr data)))
8398         (gnus-summary-limit articles))
8399     (gnus-summary-position-point)))
8400
8401 (defun gnus-summary-limit-to-score (score)
8402   "Limit to articles with score at or above SCORE."
8403   (interactive "NLimit to articles with score of at least: ")
8404   (let ((data gnus-newsgroup-data)
8405         articles)
8406     (while data
8407       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8408                 score)
8409         (push (gnus-data-number (car data)) articles))
8410       (setq data (cdr data)))
8411     (prog1
8412         (gnus-summary-limit articles)
8413       (gnus-summary-position-point))))
8414
8415 (defun gnus-summary-limit-to-unseen ()
8416   "Limit to unseen articles."
8417   (interactive)
8418   (prog1
8419       (gnus-summary-limit gnus-newsgroup-unseen)
8420     (gnus-summary-position-point)))
8421
8422 (defun gnus-summary-limit-include-thread (id)
8423   "Display all the hidden articles that is in the thread with ID in it.
8424 When called interactively, ID is the Message-ID of the current
8425 article."
8426   (interactive (list (mail-header-id (gnus-summary-article-header))))
8427   (let ((articles (gnus-articles-in-thread
8428                    (gnus-id-to-thread (gnus-root-id id)))))
8429     (prog1
8430         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8431       (gnus-summary-limit-include-matching-articles
8432        "subject"
8433        (regexp-quote (gnus-simplify-subject-re
8434                       (mail-header-subject (gnus-id-to-header id)))))
8435       (gnus-summary-position-point))))
8436
8437 (defun gnus-summary-limit-include-matching-articles (header regexp)
8438   "Display all the hidden articles that have HEADERs that match REGEXP."
8439   (interactive (list (read-string "Match on header: ")
8440                      (read-string "Regexp: ")))
8441   (let ((articles (gnus-find-matching-articles header regexp)))
8442     (prog1
8443         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8444       (gnus-summary-position-point))))
8445
8446 (defun gnus-summary-insert-dormant-articles ()
8447   "Insert all the dormant articles for this group into the current buffer."
8448   (interactive)
8449   (let ((gnus-verbose (max 6 gnus-verbose)))
8450     (if (not gnus-newsgroup-dormant)
8451         (gnus-message 3 "No dormant articles for this group")
8452       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8453
8454 (defun gnus-summary-insert-ticked-articles ()
8455   "Insert ticked articles for this group into the current buffer."
8456   (interactive)
8457   (let ((gnus-verbose (max 6 gnus-verbose)))
8458     (if (not gnus-newsgroup-marked)
8459         (gnus-message 3 "No ticked articles for this group")
8460       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8461
8462 (defun gnus-summary-limit-include-dormant ()
8463   "Display all the hidden articles that are marked as dormant.
8464 Note that this command only works on a subset of the articles currently
8465 fetched for this group."
8466   (interactive)
8467   (unless gnus-newsgroup-dormant
8468     (error "There are no dormant articles in this group"))
8469   (prog1
8470       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8471     (gnus-summary-position-point)))
8472
8473 (defun gnus-summary-limit-exclude-dormant ()
8474   "Hide all dormant articles."
8475   (interactive)
8476   (prog1
8477       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8478     (gnus-summary-position-point)))
8479
8480 (defun gnus-summary-limit-exclude-childless-dormant ()
8481   "Hide all dormant articles that have no children."
8482   (interactive)
8483   (let ((data (gnus-data-list t))
8484         articles d children)
8485     ;; Find all articles that are either not dormant or have
8486     ;; children.
8487     (while (setq d (pop data))
8488       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8489                 (and (setq children
8490                            (gnus-article-children (gnus-data-number d)))
8491                      (let (found)
8492                        (while children
8493                          (when (memq (car children) articles)
8494                            (setq children nil
8495                                  found t))
8496                          (pop children))
8497                        found)))
8498         (push (gnus-data-number d) articles)))
8499     ;; Do the limiting.
8500     (prog1
8501         (gnus-summary-limit articles)
8502       (gnus-summary-position-point))))
8503
8504 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8505   "Mark all unread excluded articles as read.
8506 If ALL, mark even excluded ticked and dormants as read."
8507   (interactive "P")
8508   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8509   (let ((articles (gnus-sorted-ndifference
8510                    (sort
8511                     (mapcar (lambda (h) (mail-header-number h))
8512                             gnus-newsgroup-headers)
8513                     '<)
8514                    gnus-newsgroup-limit))
8515         article)
8516     (setq gnus-newsgroup-unreads
8517           (gnus-sorted-intersection gnus-newsgroup-unreads
8518                                     gnus-newsgroup-limit))
8519     (if all
8520         (setq gnus-newsgroup-dormant nil
8521               gnus-newsgroup-marked nil
8522               gnus-newsgroup-reads
8523               (nconc
8524                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8525                gnus-newsgroup-reads))
8526       (while (setq article (pop articles))
8527         (unless (or (memq article gnus-newsgroup-dormant)
8528                     (memq article gnus-newsgroup-marked))
8529           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8530
8531 (defun gnus-summary-limit (articles &optional pop)
8532   (if pop
8533       ;; We pop the previous limit off the stack and use that.
8534       (setq articles (car gnus-newsgroup-limits)
8535             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8536     ;; We use the new limit, so we push the old limit on the stack.
8537     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8538   ;; Set the limit.
8539   (setq gnus-newsgroup-limit articles)
8540   (let ((total (length gnus-newsgroup-data))
8541         (data (gnus-data-find-list (gnus-summary-article-number)))
8542         (gnus-summary-mark-below nil)   ; Inhibit this.
8543         found)
8544     ;; This will do all the work of generating the new summary buffer
8545     ;; according to the new limit.
8546     (gnus-summary-prepare)
8547     ;; Hide any threads, possibly.
8548     (gnus-summary-maybe-hide-threads)
8549     ;; Try to return to the article you were at, or one in the
8550     ;; neighborhood.
8551     (when data
8552       ;; We try to find some article after the current one.
8553       (while data
8554         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8555           (setq data nil
8556                 found t))
8557         (setq data (cdr data))))
8558     (unless found
8559       ;; If there is no data, that means that we were after the last
8560       ;; article.  The same goes when we can't find any articles
8561       ;; after the current one.
8562       (goto-char (point-max))
8563       (gnus-summary-find-prev))
8564     (gnus-set-mode-line 'summary)
8565     ;; We return how many articles were removed from the summary
8566     ;; buffer as a result of the new limit.
8567     (- total (length gnus-newsgroup-data))))
8568
8569 (defsubst gnus-invisible-cut-children (threads)
8570   (let ((num 0))
8571     (while threads
8572       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8573         (incf num))
8574       (pop threads))
8575     (< num 2)))
8576
8577 (defsubst gnus-cut-thread (thread)
8578   "Go forwards in the thread until we find an article that we want to display."
8579   (when (or (eq gnus-fetch-old-headers 'some)
8580             (eq gnus-fetch-old-headers 'invisible)
8581             (numberp gnus-fetch-old-headers)
8582             (eq gnus-build-sparse-threads 'some)
8583             (eq gnus-build-sparse-threads 'more))
8584     ;; Deal with old-fetched headers and sparse threads.
8585     (while (and
8586             thread
8587             (or
8588              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8589              (gnus-summary-article-ancient-p
8590               (mail-header-number (car thread))))
8591             (if (or (<= (length (cdr thread)) 1)
8592                     (eq gnus-fetch-old-headers 'invisible))
8593                 (setq gnus-newsgroup-limit
8594                       (delq (mail-header-number (car thread))
8595                             gnus-newsgroup-limit)
8596                       thread (cadr thread))
8597               (when (gnus-invisible-cut-children (cdr thread))
8598                 (let ((th (cdr thread)))
8599                   (while th
8600                     (if (memq (mail-header-number (caar th))
8601                               gnus-newsgroup-limit)
8602                         (setq thread (car th)
8603                               th nil)
8604                       (setq th (cdr th))))))))))
8605   thread)
8606
8607 (defun gnus-cut-threads (threads)
8608   "Cut off all uninteresting articles from the beginning of THREADS."
8609   (when (or (eq gnus-fetch-old-headers 'some)
8610             (eq gnus-fetch-old-headers 'invisible)
8611             (numberp gnus-fetch-old-headers)
8612             (eq gnus-build-sparse-threads 'some)
8613             (eq gnus-build-sparse-threads 'more))
8614     (let ((th threads))
8615       (while th
8616         (setcar th (gnus-cut-thread (car th)))
8617         (setq th (cdr th)))))
8618   ;; Remove nixed out threads.
8619   (delq nil threads))
8620
8621 (defun gnus-summary-initial-limit (&optional show-if-empty)
8622   "Figure out what the initial limit is supposed to be on group entry.
8623 This entails weeding out unwanted dormants, low-scored articles,
8624 fetch-old-headers verbiage, and so on."
8625   ;; Most groups have nothing to remove.
8626   (unless (or gnus-inhibit-limiting
8627               (and (null gnus-newsgroup-dormant)
8628                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8629                    (not (eq gnus-fetch-old-headers 'some))
8630                    (not (numberp gnus-fetch-old-headers))
8631                    (not (eq gnus-fetch-old-headers 'invisible))
8632                    (null gnus-summary-expunge-below)
8633                    (not (eq gnus-build-sparse-threads 'some))
8634                    (not (eq gnus-build-sparse-threads 'more))
8635                    (null gnus-thread-expunge-below)
8636                    (not gnus-use-nocem)))
8637     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8638     (setq gnus-newsgroup-limit nil)
8639     (mapatoms
8640      (lambda (node)
8641        (unless (car (symbol-value node))
8642          ;; These threads have no parents -- they are roots.
8643          (let ((nodes (cdr (symbol-value node)))
8644                thread)
8645            (while nodes
8646              (if (and gnus-thread-expunge-below
8647                       (< (gnus-thread-total-score (car nodes))
8648                          gnus-thread-expunge-below))
8649                  (gnus-expunge-thread (pop nodes))
8650                (setq thread (pop nodes))
8651                (gnus-summary-limit-children thread))))))
8652      gnus-newsgroup-dependencies)
8653     ;; If this limitation resulted in an empty group, we might
8654     ;; pop the previous limit and use it instead.
8655     (when (and (not gnus-newsgroup-limit)
8656                show-if-empty)
8657       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8658     gnus-newsgroup-limit))
8659
8660 (defun gnus-summary-limit-children (thread)
8661   "Return 1 if this subthread is visible and 0 if it is not."
8662   ;; First we get the number of visible children to this thread.  This
8663   ;; is done by recursing down the thread using this function, so this
8664   ;; will really go down to a leaf article first, before slowly
8665   ;; working its way up towards the root.
8666   (when thread
8667     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8668            (children
8669            (if (cdr thread)
8670                (apply '+ (mapcar 'gnus-summary-limit-children
8671                                  (cdr thread)))
8672              0))
8673           (number (mail-header-number (car thread)))
8674           score)
8675       (if (and
8676            (not (memq number gnus-newsgroup-marked))
8677            (or
8678             ;; If this article is dormant and has absolutely no visible
8679             ;; children, then this article isn't visible.
8680             (and (memq number gnus-newsgroup-dormant)
8681                  (zerop children))
8682             ;; If this is "fetch-old-headered" and there is no
8683             ;; visible children, then we don't want this article.
8684             (and (or (eq gnus-fetch-old-headers 'some)
8685                      (numberp gnus-fetch-old-headers))
8686                  (gnus-summary-article-ancient-p number)
8687                  (zerop children))
8688             ;; If this is "fetch-old-headered" and `invisible', then
8689             ;; we don't want this article.
8690             (and (eq gnus-fetch-old-headers 'invisible)
8691                  (gnus-summary-article-ancient-p number))
8692             ;; If this is a sparsely inserted article with no children,
8693             ;; we don't want it.
8694             (and (eq gnus-build-sparse-threads 'some)
8695                  (gnus-summary-article-sparse-p number)
8696                  (zerop children))
8697             ;; If we use expunging, and this article is really
8698             ;; low-scored, then we don't want this article.
8699             (when (and gnus-summary-expunge-below
8700                        (< (setq score
8701                                 (or (cdr (assq number gnus-newsgroup-scored))
8702                                     gnus-summary-default-score))
8703                           gnus-summary-expunge-below))
8704               ;; We increase the expunge-tally here, but that has
8705               ;; nothing to do with the limits, really.
8706               (incf gnus-newsgroup-expunged-tally)
8707               ;; We also mark as read here, if that's wanted.
8708               (when (and gnus-summary-mark-below
8709                          (< score gnus-summary-mark-below))
8710                 (setq gnus-newsgroup-unreads
8711                       (delq number gnus-newsgroup-unreads))
8712                 (if gnus-newsgroup-auto-expire
8713                     (push number gnus-newsgroup-expirable)
8714                   (push (cons number gnus-low-score-mark)
8715                         gnus-newsgroup-reads)))
8716               t)
8717             ;; Do the `display' group parameter.
8718             (and gnus-newsgroup-display
8719                  (not (funcall gnus-newsgroup-display)))
8720             ;; Check NoCeM things.
8721             (when (and gnus-use-nocem
8722                        (gnus-nocem-unwanted-article-p
8723                         (mail-header-id (car thread))))
8724               (setq gnus-newsgroup-unreads
8725                     (delq number gnus-newsgroup-unreads))
8726               t)))
8727           ;; Nope, invisible article.
8728           0
8729         ;; Ok, this article is to be visible, so we add it to the limit
8730         ;; and return 1.
8731         (push number gnus-newsgroup-limit)
8732         1))))
8733
8734 (defun gnus-expunge-thread (thread)
8735   "Mark all articles in THREAD as read."
8736   (let* ((number (mail-header-number (car thread))))
8737     (incf gnus-newsgroup-expunged-tally)
8738     ;; We also mark as read here, if that's wanted.
8739     (setq gnus-newsgroup-unreads
8740           (delq number gnus-newsgroup-unreads))
8741     (if gnus-newsgroup-auto-expire
8742         (push number gnus-newsgroup-expirable)
8743       (push (cons number gnus-low-score-mark)
8744             gnus-newsgroup-reads)))
8745   ;; Go recursively through all subthreads.
8746   (mapcar 'gnus-expunge-thread (cdr thread)))
8747
8748 ;; Summary article oriented commands
8749
8750 (defun gnus-summary-refer-parent-article (n)
8751   "Refer parent article N times.
8752 If N is negative, go to ancestor -N instead.
8753 The difference between N and the number of articles fetched is returned."
8754   (interactive "p")
8755   (let ((skip 1)
8756         error header ref)
8757     (when (not (natnump n))
8758       (setq skip (abs n)
8759             n 1))
8760     (while (and (> n 0)
8761                 (not error))
8762       (setq header (gnus-summary-article-header))
8763       (if (and (eq (mail-header-number header)
8764                    (cdr gnus-article-current))
8765                (equal gnus-newsgroup-name
8766                       (car gnus-article-current)))
8767           ;; If we try to find the parent of the currently
8768           ;; displayed article, then we take a look at the actual
8769           ;; References header, since this is slightly more
8770           ;; reliable than the References field we got from the
8771           ;; server.
8772           (with-current-buffer gnus-original-article-buffer
8773             (nnheader-narrow-to-headers)
8774             (unless (setq ref (message-fetch-field "references"))
8775               (when (setq ref (message-fetch-field "in-reply-to"))
8776                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8777             (widen))
8778         (setq ref
8779               ;; It's not the current article, so we take a bet on
8780               ;; the value we got from the server.
8781               (mail-header-references header)))
8782       (if (and ref
8783                (not (equal ref "")))
8784           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8785             (gnus-message 1 "Couldn't find parent"))
8786         (gnus-message 1 "No references in article %d"
8787                       (gnus-summary-article-number))
8788         (setq error t))
8789       (decf n))
8790     (gnus-summary-position-point)
8791     n))
8792
8793 (defun gnus-summary-refer-references ()
8794   "Fetch all articles mentioned in the References header.
8795 Return the number of articles fetched."
8796   (interactive)
8797   (let ((ref (mail-header-references (gnus-summary-article-header)))
8798         (current (gnus-summary-article-number))
8799         (n 0))
8800     (if (or (not ref)
8801             (equal ref ""))
8802         (error "No References in the current article")
8803       ;; For each Message-ID in the References header...
8804       (while (string-match "<[^>]*>" ref)
8805         (incf n)
8806         ;; ... fetch that article.
8807         (gnus-summary-refer-article
8808          (prog1 (match-string 0 ref)
8809            (setq ref (substring ref (match-end 0))))))
8810       (gnus-summary-goto-subject current)
8811       (gnus-summary-position-point)
8812       n)))
8813
8814 (defun gnus-summary-refer-thread (&optional limit)
8815   "Fetch all articles in the current thread.
8816 If LIMIT (the numerical prefix), fetch that many old headers instead
8817 of what's specified by the `gnus-refer-thread-limit' variable."
8818   (interactive "P")
8819   (let ((id (mail-header-id (gnus-summary-article-header)))
8820         (limit (if limit (prefix-numeric-value limit)
8821                  gnus-refer-thread-limit)))
8822     (unless (eq gnus-fetch-old-headers 'invisible)
8823       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8824       ;; Retrieve the headers and read them in.
8825       (if (eq (if (numberp limit)
8826                   (gnus-retrieve-headers
8827                    (list (min
8828                           (+ (mail-header-number
8829                               (gnus-summary-article-header))
8830                              limit)
8831                           gnus-newsgroup-end))
8832                    gnus-newsgroup-name (* limit 2))
8833                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8834                 ;; headers.
8835                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8836                                        gnus-newsgroup-name limit))
8837               'nov)
8838           (gnus-build-all-threads)
8839         (error "Can't fetch thread from back ends that don't support NOV"))
8840       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8841     (gnus-summary-limit-include-thread id)))
8842
8843 (defun gnus-summary-refer-article (message-id)
8844   "Fetch an article specified by MESSAGE-ID."
8845   (interactive "sMessage-ID: ")
8846   (when (and (stringp message-id)
8847              (not (zerop (length message-id))))
8848     (setq message-id (gnus-replace-in-string message-id " " ""))
8849     ;; Construct the correct Message-ID if necessary.
8850     ;; Suggested by tale@pawl.rpi.edu.
8851     (unless (string-match "^<" message-id)
8852       (setq message-id (concat "<" message-id)))
8853     (unless (string-match ">$" message-id)
8854       (setq message-id (concat message-id ">")))
8855     ;; People often post MIDs from URLs, so unhex it:
8856     (unless (string-match "@" message-id)
8857       (setq message-id (gnus-url-unhex-string message-id)))
8858     (let* ((header (gnus-id-to-header message-id))
8859            (sparse (and header
8860                         (gnus-summary-article-sparse-p
8861                          (mail-header-number header))
8862                         (memq (mail-header-number header)
8863                               gnus-newsgroup-limit)))
8864            number)
8865       (cond
8866        ;; If the article is present in the buffer we just go to it.
8867        ((and header
8868              (or (not (gnus-summary-article-sparse-p
8869                        (mail-header-number header)))
8870                  sparse))
8871         (prog1
8872             (gnus-summary-goto-article
8873              (mail-header-number header) nil t)
8874           (when sparse
8875             (gnus-summary-update-article (mail-header-number header)))))
8876        (t
8877         ;; We fetch the article.
8878         (catch 'found
8879           (dolist (gnus-override-method (gnus-refer-article-methods))
8880             (when (and (gnus-check-server gnus-override-method)
8881                        ;; Fetch the header,
8882                        (setq number (gnus-summary-insert-subject message-id)))
8883               ;; and display the article.
8884               (gnus-summary-select-article nil nil nil number)
8885               (throw 'found t)))
8886           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8887
8888 (defun gnus-refer-article-methods ()
8889   "Return a list of referable methods."
8890   (cond
8891    ;; No method, so we default to current and native.
8892    ((null gnus-refer-article-method)
8893     (list gnus-current-select-method gnus-select-method))
8894    ;; Current.
8895    ((eq 'current gnus-refer-article-method)
8896     (list gnus-current-select-method))
8897    ;; List of select methods.
8898    ((not (and (symbolp (car gnus-refer-article-method))
8899               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8900     (let (out)
8901       (dolist (method gnus-refer-article-method)
8902         (push (if (eq 'current method)
8903                   gnus-current-select-method
8904                 method)
8905               out))
8906       (nreverse out)))
8907    ;; One single select method.
8908    (t
8909     (list gnus-refer-article-method))))
8910
8911 (defun gnus-summary-edit-parameters ()
8912   "Edit the group parameters of the current group."
8913   (interactive)
8914   (gnus-group-edit-group gnus-newsgroup-name 'params))
8915
8916 (defun gnus-summary-customize-parameters ()
8917   "Customize the group parameters of the current group."
8918   (interactive)
8919   (gnus-group-customize gnus-newsgroup-name))
8920
8921 (defun gnus-summary-enter-digest-group (&optional force)
8922   "Enter an nndoc group based on the current article.
8923 If FORCE, force a digest interpretation.  If not, try
8924 to guess what the document format is."
8925   (interactive "P")
8926   (let ((conf gnus-current-window-configuration))
8927     (save-window-excursion
8928       (save-excursion
8929         (let (gnus-article-prepare-hook
8930               gnus-display-mime-function
8931               gnus-break-pages)
8932           (gnus-summary-select-article))))
8933     (setq gnus-current-window-configuration conf)
8934     (let* ((name (format "%s-%d"
8935                          (gnus-group-prefixed-name
8936                           gnus-newsgroup-name (list 'nndoc ""))
8937                          (with-current-buffer gnus-summary-buffer
8938                            gnus-current-article)))
8939            (ogroup gnus-newsgroup-name)
8940            (params (append (gnus-info-params (gnus-get-info ogroup))
8941                            (list (cons 'to-group ogroup))
8942                            (list (cons 'parent-group ogroup))
8943                            (list (cons 'save-article-group ogroup))))
8944            (case-fold-search t)
8945            (buf (current-buffer))
8946            dig to-address)
8947       (with-current-buffer gnus-original-article-buffer
8948         ;; Have the digest group inherit the main mail address of
8949         ;; the parent article.
8950         (when (setq to-address (or (gnus-fetch-field "reply-to")
8951                                    (gnus-fetch-field "from")))
8952           (setq params
8953                 (append
8954                  (list (cons 'to-address
8955                              (funcall gnus-decode-encoded-address-function
8956                                       to-address))))))
8957         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8958         (insert-buffer-substring gnus-original-article-buffer)
8959         ;; Remove lines that may lead nndoc to misinterpret the
8960         ;; document type.
8961         (narrow-to-region
8962          (goto-char (point-min))
8963          (or (search-forward "\n\n" nil t) (point)))
8964         (goto-char (point-min))
8965         (delete-matching-lines "^Path:\\|^From ")
8966         (widen))
8967       (unwind-protect
8968           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8969                     (gnus-newsgroup-ephemeral-ignored-charsets
8970                      gnus-newsgroup-ignored-charsets))
8971                 (gnus-group-read-ephemeral-group
8972                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8973                               (nndoc-article-type
8974                                ,(if force 'mbox 'guess)))
8975                  t nil nil nil
8976                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8977                                                         "ADAPT")))))
8978               ;; Make all postings to this group go to the parent group.
8979               (nconc (gnus-info-params (gnus-get-info name))
8980                      params)
8981             ;; Couldn't select this doc group.
8982             (switch-to-buffer buf)
8983             (gnus-set-global-variables)
8984             (gnus-configure-windows 'summary)
8985             (gnus-message 3 "Article couldn't be entered?"))
8986         (kill-buffer dig)))))
8987
8988 (defun gnus-summary-read-document (n)
8989   "Open a new group based on the current article(s).
8990 This will allow you to read digests and other similar
8991 documents as newsgroups.
8992 Obeys the standard process/prefix convention."
8993   (interactive "P")
8994   (let* ((ogroup gnus-newsgroup-name)
8995          (params (append (gnus-info-params (gnus-get-info ogroup))
8996                          (list (cons 'to-group ogroup))))
8997          group egroup groups vgroup)
8998     (dolist (article (gnus-summary-work-articles n))
8999       (setq group (format "%s-%d" gnus-newsgroup-name article))
9000       (gnus-summary-remove-process-mark article)
9001       (when (gnus-summary-display-article article)
9002         (save-excursion
9003           (with-temp-buffer
9004             (insert-buffer-substring gnus-original-article-buffer)
9005             ;; Remove some headers that may lead nndoc to make
9006             ;; the wrong guess.
9007             (message-narrow-to-head)
9008             (goto-char (point-min))
9009             (delete-matching-lines "^Path:\\|^From ")
9010             (widen)
9011             (if (setq egroup
9012                       (gnus-group-read-ephemeral-group
9013                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9014                                      (nndoc-article-type guess))
9015                        t nil t))
9016                 (progn
9017                   ;; Make all postings to this group go to the parent group.
9018                   (nconc (gnus-info-params (gnus-get-info egroup))
9019                          params)
9020                   (push egroup groups))
9021               ;; Couldn't select this doc group.
9022               (gnus-error 3 "Article couldn't be entered"))))))
9023     ;; Now we have selected all the documents.
9024     (cond
9025      ((not groups)
9026       (error "None of the articles could be interpreted as documents"))
9027      ((gnus-group-read-ephemeral-group
9028        (setq vgroup (format
9029                      "nnvirtual:%s-%s" gnus-newsgroup-name
9030                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9031        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9032        t
9033        (cons (current-buffer) 'summary)))
9034      (t
9035       (error "Couldn't select virtual nndoc group")))))
9036
9037 (defun gnus-summary-isearch-article (&optional regexp-p)
9038   "Do incremental search forward on the current article.
9039 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9040   (interactive "P")
9041   (gnus-summary-select-article)
9042   (gnus-configure-windows 'article)
9043   (gnus-eval-in-buffer-window gnus-article-buffer
9044     (save-restriction
9045       (widen)
9046       (isearch-forward regexp-p))))
9047
9048 (defun gnus-summary-repeat-search-article-forward ()
9049   "Repeat the previous search forwards."
9050   (interactive)
9051   (unless gnus-last-search-regexp
9052     (error "No previous search"))
9053   (gnus-summary-search-article-forward gnus-last-search-regexp))
9054
9055 (defun gnus-summary-repeat-search-article-backward ()
9056   "Repeat the previous search backwards."
9057   (interactive)
9058   (unless gnus-last-search-regexp
9059     (error "No previous search"))
9060   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9061
9062 (defun gnus-summary-search-article-forward (regexp &optional backward)
9063   "Search for an article containing REGEXP forward.
9064 If BACKWARD, search backward instead."
9065   (interactive
9066    (list (read-string
9067           (format "Search article %s (regexp%s): "
9068                   (if current-prefix-arg "backward" "forward")
9069                   (if gnus-last-search-regexp
9070                       (concat ", default " gnus-last-search-regexp)
9071                     "")))
9072          current-prefix-arg))
9073   (if (string-equal regexp "")
9074       (setq regexp (or gnus-last-search-regexp ""))
9075     (setq gnus-last-search-regexp regexp)
9076     (setq gnus-article-before-search gnus-current-article))
9077   ;; Intentionally set gnus-last-article.
9078   (setq gnus-last-article gnus-article-before-search)
9079   (let ((gnus-last-article gnus-last-article))
9080     (if (gnus-summary-search-article regexp backward)
9081         (gnus-summary-show-thread)
9082       (signal 'search-failed (list regexp)))))
9083
9084 (defun gnus-summary-search-article-backward (regexp)
9085   "Search for an article containing REGEXP backward."
9086   (interactive
9087    (list (read-string
9088           (format "Search article backward (regexp%s): "
9089                   (if gnus-last-search-regexp
9090                       (concat ", default " gnus-last-search-regexp)
9091                     "")))))
9092   (gnus-summary-search-article-forward regexp 'backward))
9093
9094 (defun gnus-summary-search-article (regexp &optional backward)
9095   "Search for an article containing REGEXP.
9096 Optional argument BACKWARD means do search for backward.
9097 `gnus-select-article-hook' is not called during the search."
9098   ;; We have to require this here to make sure that the following
9099   ;; dynamic binding isn't shadowed by autoloading.
9100   (require 'gnus-async)
9101   (require 'gnus-art)
9102   (let ((gnus-select-article-hook nil)  ;Disable hook.
9103         (gnus-article-prepare-hook nil)
9104         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9105         (gnus-use-article-prefetch nil)
9106         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9107         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9108         (gnus-visual nil)
9109         (gnus-keep-backlog nil)
9110         (gnus-break-pages nil)
9111         (gnus-summary-display-arrow nil)
9112         (gnus-updated-mode-lines nil)
9113         (gnus-auto-center-summary nil)
9114         (sum (current-buffer))
9115         (gnus-display-mime-function nil)
9116         (found nil)
9117         point)
9118     (gnus-save-hidden-threads
9119       (gnus-summary-select-article)
9120       (set-buffer gnus-article-buffer)
9121       (goto-char (window-point (get-buffer-window (current-buffer))))
9122       (when backward
9123         (forward-line -1))
9124       (while (not found)
9125         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9126         (if (if backward
9127                 (re-search-backward regexp nil t)
9128               (re-search-forward regexp nil t))
9129             ;; We found the regexp.
9130             (progn
9131               (setq found 'found)
9132               (beginning-of-line)
9133               (set-window-start
9134                (get-buffer-window (current-buffer))
9135                (point))
9136               (forward-line 1)
9137               (set-window-point
9138                (get-buffer-window (current-buffer))
9139                (point))
9140               (set-buffer sum)
9141               (setq point (point)))
9142           ;; We didn't find it, so we go to the next article.
9143           (set-buffer sum)
9144           (setq found 'not)
9145           (while (eq found 'not)
9146             (if (not (if backward (gnus-summary-find-prev)
9147                        (gnus-summary-find-next)))
9148                 ;; No more articles.
9149                 (setq found t)
9150               ;; Select the next article and adjust point.
9151               (unless (gnus-summary-article-sparse-p
9152                        (gnus-summary-article-number))
9153                 (setq found nil)
9154                 (gnus-summary-select-article)
9155                 (set-buffer gnus-article-buffer)
9156                 (widen)
9157                 (goto-char (if backward (point-max) (point-min))))))))
9158       (gnus-message 7 ""))
9159     ;; Return whether we found the regexp.
9160     (when (eq found 'found)
9161       (goto-char point)
9162       (gnus-summary-show-thread)
9163       (gnus-summary-goto-subject gnus-current-article)
9164       (gnus-summary-position-point)
9165       t)))
9166
9167 (defun gnus-find-matching-articles (header regexp)
9168   "Return a list of all articles that match REGEXP on HEADER.
9169 This search includes all articles in the current group that Gnus has
9170 fetched headers for, whether they are displayed or not."
9171   (let ((articles nil)
9172         ;; Can't eta-reduce because it's a macro.
9173         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9174         (case-fold-search t))
9175     (dolist (header gnus-newsgroup-headers)
9176       (when (string-match regexp (funcall func header))
9177         (push (mail-header-number header) articles)))
9178     (nreverse articles)))
9179
9180 (defun gnus-summary-find-matching (header regexp &optional backward unread
9181                                           not-case-fold not-matching)
9182   "Return a list of all articles that match REGEXP on HEADER.
9183 The search stars on the current article and goes forwards unless
9184 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9185 If UNREAD is non-nil, only unread articles will
9186 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9187 in the comparisons. If NOT-MATCHING, return a list of all articles that
9188 not match REGEXP on HEADER."
9189   (let ((case-fold-search (not not-case-fold))
9190         articles d func)
9191     (if (consp header)
9192         (if (eq (car header) 'extra)
9193             (setq func
9194                   `(lambda (h)
9195                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9196                          "")))
9197           (error "%s is an invalid header" header))
9198       (unless (fboundp (intern (concat "mail-header-" header)))
9199         (error "%s is not a valid header" header))
9200       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9201     (dolist (d (if (eq backward 'all)
9202                    gnus-newsgroup-data
9203                  (gnus-data-find-list
9204                   (gnus-summary-article-number)
9205                   (gnus-data-list backward))))
9206       (when (and (or (not unread)       ; We want all articles...
9207                      (gnus-data-unread-p d)) ; Or just unreads.
9208                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9209                  (if not-matching
9210                      (not (string-match
9211                            regexp
9212                            (funcall func (gnus-data-header d))))
9213                    (string-match regexp
9214                                  (funcall func (gnus-data-header d)))))
9215         (push (gnus-data-number d) articles))) ; Success!
9216     (nreverse articles)))
9217
9218 (defun gnus-summary-execute-command (header regexp command &optional backward)
9219   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9220 If HEADER is an empty string (or nil), the match is done on the entire
9221 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9222   (interactive
9223    (list (let ((completion-ignore-case t))
9224            (completing-read
9225             "Header name: "
9226             (mapcar (lambda (header) (list (format "%s" header)))
9227                     (append
9228                      '("Number" "Subject" "From" "Lines" "Date"
9229                        "Message-ID" "Xref" "References" "Body")
9230                      gnus-extra-headers))
9231             nil 'require-match))
9232          (read-string "Regexp: ")
9233          (read-key-sequence "Command: ")
9234          current-prefix-arg))
9235   (when (equal header "Body")
9236     (setq header ""))
9237   ;; Hidden thread subtrees must be searched as well.
9238   (gnus-summary-show-all-threads)
9239   ;; We don't want to change current point nor window configuration.
9240   (save-excursion
9241     (save-window-excursion
9242       (let (gnus-visual
9243             gnus-treat-strip-trailing-blank-lines
9244             gnus-treat-strip-leading-blank-lines
9245             gnus-treat-strip-multiple-blank-lines
9246             gnus-treat-hide-boring-headers
9247             gnus-treat-fold-newsgroups
9248             gnus-article-prepare-hook)
9249         (gnus-message 6 "Executing %s..." (key-description command))
9250         ;; We'd like to execute COMMAND interactively so as to give arguments.
9251         (gnus-execute header regexp
9252                       `(call-interactively ',(key-binding command))
9253                       backward)
9254         (gnus-message 6 "Executing %s...done" (key-description command))))))
9255
9256 (defun gnus-summary-beginning-of-article ()
9257   "Scroll the article back to the beginning."
9258   (interactive)
9259   (gnus-summary-select-article)
9260   (gnus-configure-windows 'article)
9261   (gnus-eval-in-buffer-window gnus-article-buffer
9262     (widen)
9263     (goto-char (point-min))
9264     (when gnus-break-pages
9265       (gnus-narrow-to-page))))
9266
9267 (defun gnus-summary-end-of-article ()
9268   "Scroll to the end of the article."
9269   (interactive)
9270   (gnus-summary-select-article)
9271   (gnus-configure-windows 'article)
9272   (gnus-eval-in-buffer-window gnus-article-buffer
9273     (widen)
9274     (goto-char (point-max))
9275     (recenter -3)
9276     (when gnus-break-pages
9277       (gnus-narrow-to-page))))
9278
9279 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9280   "Truncate to LEN and quote all \"(\"'s in STRING."
9281   (gnus-replace-in-string (if (and len (> (length string) len))
9282                               (substring string 0 len)
9283                             string)
9284                           "[()]" "\\\\\\&"))
9285
9286 (defun gnus-summary-print-article (&optional filename n)
9287   "Generate and print a PostScript image of the process-marked (mail) articles.
9288
9289 If used interactively, print the current article if none are
9290 process-marked.  With prefix arg, prompt the user for the name of the
9291 file to save in.
9292
9293 When used from Lisp, accept two optional args FILENAME and N.  N means
9294 to print the next N articles.  If N is negative, print the N previous
9295 articles.  If N is nil and articles have been marked with the process
9296 mark, print these instead.
9297
9298 If the optional first argument FILENAME is nil, send the image to the
9299 printer.  If FILENAME is a string, save the PostScript image in a file with
9300 that name.  If FILENAME is a number, prompt the user for the name of the file
9301 to save in."
9302   (interactive (list (ps-print-preprint current-prefix-arg)))
9303   (dolist (article (gnus-summary-work-articles n))
9304     (gnus-summary-select-article nil nil 'pseudo article)
9305     (gnus-eval-in-buffer-window gnus-article-buffer
9306       (gnus-print-buffer))
9307     (gnus-summary-remove-process-mark article))
9308   (ps-despool filename))
9309
9310 (defun gnus-print-buffer ()
9311   (let ((buffer (generate-new-buffer " *print*")))
9312     (unwind-protect
9313         (progn
9314           (copy-to-buffer buffer (point-min) (point-max))
9315           (set-buffer buffer)
9316           (gnus-remove-text-with-property 'gnus-decoration)
9317           (when (gnus-visual-p 'article-highlight 'highlight)
9318             ;; Copy-to-buffer doesn't copy overlay.  So redo
9319             ;; highlight.
9320             (let ((gnus-article-buffer buffer))
9321               (gnus-article-highlight-citation t)
9322               (gnus-article-highlight-signature)
9323               (gnus-article-emphasize)
9324               (gnus-article-delete-invisible-text)))
9325           (let ((ps-left-header
9326                  (list
9327                   (concat "("
9328                           (gnus-summary-print-truncate-and-quote
9329                            (mail-header-subject gnus-current-headers)
9330                            66) ")")
9331                   (concat "("
9332                           (gnus-summary-print-truncate-and-quote
9333                            (mail-header-from gnus-current-headers)
9334                            45) ")")))
9335                 (ps-right-header
9336                  (list
9337                   "/pagenumberstring load"
9338                   (concat "("
9339                           (mail-header-date gnus-current-headers) ")"))))
9340             (gnus-run-hooks 'gnus-ps-print-hook)
9341             (save-excursion
9342               (if ps-print-color-p
9343                   (ps-spool-buffer-with-faces)
9344                 (ps-spool-buffer)))))
9345       (kill-buffer buffer))))
9346
9347 (defun gnus-summary-show-article (&optional arg)
9348   "Force redisplaying of the current article.
9349 If ARG (the prefix) is a number, show the article with the charset
9350 defined in `gnus-summary-show-article-charset-alist', or the charset
9351 input.
9352 If ARG (the prefix) is non-nil and not a number, show the raw article
9353 without any article massaging functions being run.  Normally, the key
9354 strokes are `C-u g'."
9355   (interactive "P")
9356   (cond
9357    ((numberp arg)
9358     (gnus-summary-show-article t)
9359     (let ((gnus-newsgroup-charset
9360            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9361                (mm-read-coding-system
9362                 "View as charset: " ;; actually it is coding system.
9363                 (with-current-buffer gnus-article-buffer
9364                   (mm-detect-coding-region (point) (point-max))))))
9365           (gnus-newsgroup-ignored-charsets 'gnus-all))
9366       (gnus-summary-select-article nil 'force)
9367       (let ((deps gnus-newsgroup-dependencies)
9368             head header lines)
9369         (with-current-buffer gnus-original-article-buffer
9370           (save-restriction
9371             (message-narrow-to-head)
9372             (setq head (buffer-string))
9373             (goto-char (point-min))
9374             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9375               (goto-char (point-max))
9376               (widen)
9377               (setq lines (1- (count-lines (point) (point-max))))))
9378           (with-temp-buffer
9379             (insert (format "211 %d Article retrieved.\n"
9380                             (cdr gnus-article-current)))
9381             (insert head)
9382             (if lines (insert (format "Lines: %d\n" lines)))
9383             (insert ".\n")
9384             (let ((nntp-server-buffer (current-buffer)))
9385               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9386         (gnus-data-set-header
9387          (gnus-data-find (cdr gnus-article-current))
9388          header)
9389         (gnus-summary-update-article-line
9390          (cdr gnus-article-current) header)
9391         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9392           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9393    ((not arg)
9394     ;; Select the article the normal way.
9395     (gnus-summary-select-article nil 'force))
9396    (t
9397     ;; We have to require this here to make sure that the following
9398     ;; dynamic binding isn't shadowed by autoloading.
9399     (require 'gnus-async)
9400     (require 'gnus-art)
9401     ;; Bind the article treatment functions to nil.
9402     (let ((gnus-have-all-headers t)
9403           gnus-article-prepare-hook
9404           gnus-article-decode-hook
9405           gnus-display-mime-function
9406           gnus-break-pages)
9407       ;; Destroy any MIME parts.
9408       (when (gnus-buffer-live-p gnus-article-buffer)
9409         (with-current-buffer gnus-article-buffer
9410           (mm-destroy-parts gnus-article-mime-handles)
9411           ;; Set it to nil for safety reason.
9412           (setq gnus-article-mime-handle-alist nil)
9413           (setq gnus-article-mime-handles nil)))
9414       (gnus-summary-select-article nil 'force))))
9415   (gnus-summary-goto-subject gnus-current-article)
9416   (gnus-summary-position-point))
9417
9418 (defun gnus-summary-show-raw-article ()
9419   "Show the raw article without any article massaging functions being run."
9420   (interactive)
9421   (gnus-summary-show-article t))
9422
9423 (defun gnus-summary-verbose-headers (&optional arg)
9424   "Toggle permanent full header display.
9425 If ARG is a positive number, turn header display on.
9426 If ARG is a negative number, turn header display off."
9427   (interactive "P")
9428   (setq gnus-show-all-headers
9429         (cond ((or (not (numberp arg))
9430                    (zerop arg))
9431                (not gnus-show-all-headers))
9432               ((natnump arg)
9433                t)))
9434   (gnus-summary-show-article))
9435
9436 (defun gnus-summary-toggle-header (&optional arg)
9437   "Show the headers if they are hidden, or hide them if they are shown.
9438 If ARG is a positive number, show the entire header.
9439 If ARG is a negative number, hide the unwanted header lines."
9440   (interactive "P")
9441   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9442                      (get-buffer-window gnus-article-buffer t))))
9443     (with-current-buffer gnus-article-buffer
9444       (widen)
9445       (article-narrow-to-head)
9446       (let* ((inhibit-read-only t)
9447              (inhibit-point-motion-hooks t)
9448              (hidden (if (numberp arg)
9449                          (>= arg 0)
9450                        (or (not (looking-at "[^ \t\n]+:"))
9451                            (gnus-article-hidden-text-p 'headers))))
9452              s e)
9453         (delete-region (point-min) (point-max))
9454         (with-current-buffer gnus-original-article-buffer
9455           (goto-char (setq s (point-min)))
9456           (setq e (if (search-forward "\n\n" nil t)
9457                       (1- (point))
9458                     (point-max))))
9459         (insert-buffer-substring gnus-original-article-buffer s e)
9460         (run-hooks 'gnus-article-decode-hook)
9461         (if hidden
9462             (let ((gnus-treat-hide-headers nil)
9463                   (gnus-treat-hide-boring-headers nil))
9464               (gnus-delete-wash-type 'headers)
9465               (gnus-treat-article 'head))
9466           (gnus-treat-article 'head))
9467         (widen)
9468         (if window
9469             (set-window-start window (goto-char (point-min))))
9470         (if gnus-break-pages
9471             (gnus-narrow-to-page)
9472           (when (gnus-visual-p 'page-marker)
9473             (let ((inhibit-read-only t))
9474               (gnus-remove-text-with-property 'gnus-prev)
9475               (gnus-remove-text-with-property 'gnus-next))))
9476         (gnus-set-mode-line 'article)))))
9477
9478 (defun gnus-summary-show-all-headers ()
9479   "Make all header lines visible."
9480   (interactive)
9481   (gnus-summary-toggle-header 1))
9482
9483 (defun gnus-summary-caesar-message (&optional arg)
9484   "Caesar rotate the current article by 13.
9485 With a non-numerical prefix, also rotate headers.  A numerical
9486 prefix specifies how many places to rotate each letter forward."
9487   (interactive "P")
9488   (gnus-summary-select-article)
9489   (let ((mail-header-separator ""))
9490     (gnus-eval-in-buffer-window gnus-article-buffer
9491       (save-restriction
9492         (widen)
9493         (let ((start (window-start))
9494               (inhibit-read-only t))
9495           (if (equal arg '(4))
9496               (message-caesar-buffer-body nil t)
9497             (message-caesar-buffer-body arg))
9498           (set-window-start (get-buffer-window (current-buffer)) start)))))
9499   ;; Create buttons and stuff...
9500   (gnus-treat-article nil))
9501
9502 (declare-function idna-to-unicode "ext:idna" (str))
9503
9504 (defun gnus-summary-idna-message (&optional arg)
9505   "Decode IDNA encoded domain names in the current articles.
9506 IDNA encoded domain names looks like `xn--bar'.  If a string
9507 remain unencoded after running this function, it is likely an
9508 invalid IDNA string (`xn--bar' is invalid).
9509
9510 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9511 installed for this command to work."
9512   (interactive "P")
9513   (if (not (and (condition-case nil (require 'idna)
9514                   (file-error))
9515                 (mm-coding-system-p 'utf-8)
9516                 (executable-find (symbol-value 'idna-program))))
9517       (gnus-message
9518        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9519     (gnus-summary-select-article)
9520     (let ((mail-header-separator ""))
9521       (gnus-eval-in-buffer-window gnus-article-buffer
9522         (save-restriction
9523           (widen)
9524           (let ((start (window-start))
9525                 buffer-read-only)
9526             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9527               (replace-match (idna-to-unicode (match-string 1))))
9528             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9529
9530 (defun gnus-summary-morse-message (&optional arg)
9531   "Morse decode the current article."
9532   (interactive "P")
9533   (gnus-summary-select-article)
9534   (let ((mail-header-separator ""))
9535     (gnus-eval-in-buffer-window gnus-article-buffer
9536       (save-excursion
9537         (save-restriction
9538           (widen)
9539           (let ((pos (window-start))
9540                 (inhibit-read-only t))
9541             (goto-char (point-min))
9542             (when (message-goto-body)
9543               (gnus-narrow-to-body))
9544             (goto-char (point-min))
9545             (while (search-forward "·" (point-max) t)
9546               (replace-match "."))
9547             (unmorse-region (point-min) (point-max))
9548             (widen)
9549             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9550
9551 (defun gnus-summary-stop-page-breaking ()
9552   "Stop page breaking in the current article."
9553   (interactive)
9554   (gnus-summary-select-article)
9555   (gnus-eval-in-buffer-window gnus-article-buffer
9556     (widen)
9557     (when (gnus-visual-p 'page-marker)
9558       (let ((inhibit-read-only t))
9559         (gnus-remove-text-with-property 'gnus-prev)
9560         (gnus-remove-text-with-property 'gnus-next))
9561       (setq gnus-page-broken nil))))
9562
9563 (defun gnus-summary-move-article (&optional n to-newsgroup
9564                                             select-method action)
9565   "Move the current article to a different newsgroup.
9566 If N is a positive number, move the N next articles.
9567 If N is a negative number, move the N previous articles.
9568 If N is nil and any articles have been marked with the process mark,
9569 move those articles instead.
9570 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9571 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9572 re-spool using this method.
9573
9574 When called interactively with TO-NEWSGROUP being nil, the value of
9575 the variable `gnus-move-split-methods' is used for finding a default
9576 for the target newsgroup.
9577
9578 For this function to work, both the current newsgroup and the
9579 newsgroup that you want to move to have to support the `request-move'
9580 and `request-accept' functions.
9581
9582 ACTION can be either `move' (the default), `crosspost' or `copy'."
9583   (interactive "P")
9584   (unless action
9585     (setq action 'move))
9586   ;; Check whether the source group supports the required functions.
9587   (cond ((and (eq action 'move)
9588               (not (gnus-check-backend-function
9589                     'request-move-article gnus-newsgroup-name)))
9590          (error "The current group does not support article moving"))
9591         ((and (eq action 'crosspost)
9592               (not (gnus-check-backend-function
9593                     'request-replace-article gnus-newsgroup-name)))
9594          (error "The current group does not support article editing")))
9595   (let ((articles (gnus-summary-work-articles n))
9596         (prefix (if (gnus-check-backend-function
9597                      'request-move-article gnus-newsgroup-name)
9598                     (funcall gnus-move-group-prefix-function
9599                              gnus-newsgroup-name)
9600                   ""))
9601         (names '((move "Move" "Moving")
9602                  (copy "Copy" "Copying")
9603                  (crosspost "Crosspost" "Crossposting")))
9604         (copy-buf (save-excursion
9605                     (nnheader-set-temp-buffer " *copy article*")))
9606         art-group to-method new-xref article to-groups
9607         articles-to-update-marks encoded)
9608     (unless (assq action names)
9609       (error "Unknown action %s" action))
9610     ;; Read the newsgroup name.
9611     (when (and (not to-newsgroup)
9612                (not select-method))
9613       (if (and gnus-move-split-methods
9614                (not
9615                 (and (memq gnus-current-article articles)
9616                      (gnus-buffer-live-p gnus-original-article-buffer))))
9617           ;; When `gnus-move-split-methods' is non-nil, we have to
9618           ;; select an article to give `gnus-read-move-group-name' an
9619           ;; opportunity to suggest an appropriate default.  However,
9620           ;; we needn't render or mark the article.
9621           (let ((gnus-display-mime-function nil)
9622                 (gnus-article-prepare-hook nil)
9623                 (gnus-mark-article-hook nil))
9624             (gnus-summary-select-article nil nil nil (car articles))))
9625       (setq to-newsgroup (gnus-read-move-group-name
9626                           (cadr (assq action names))
9627                           (symbol-value
9628                            (intern (format "gnus-current-%s-group" action)))
9629                           articles prefix)
9630             encoded to-newsgroup
9631             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9632       (set (intern (format "gnus-current-%s-group" action))
9633            (mm-decode-coding-string
9634             to-newsgroup
9635             (gnus-group-name-charset to-method to-newsgroup))))
9636     (unless to-method
9637       (setq to-method (or select-method
9638                           (gnus-server-to-method
9639                            (gnus-group-method to-newsgroup)))))
9640     (setq to-newsgroup
9641           (or encoded
9642               (and to-newsgroup
9643                    (mm-encode-coding-string
9644                     to-newsgroup
9645                     (gnus-group-name-charset to-method to-newsgroup)))))
9646     ;; Check the method we are to move this article to...
9647     (unless (gnus-check-backend-function
9648              'request-accept-article (car to-method))
9649       (error "%s does not support article copying" (car to-method)))
9650     (unless (gnus-check-server to-method)
9651       (error "Can't open server %s" (car to-method)))
9652     (gnus-message 6 "%s to %s: %s..."
9653                   (caddr (assq action names))
9654                   (or (car select-method)
9655                       (gnus-group-decoded-name to-newsgroup))
9656                   articles)
9657     (while articles
9658       (setq article (pop articles))
9659       (setq
9660        art-group
9661        (cond
9662         ;; Move the article.
9663         ((eq action 'move)
9664          ;; Remove this article from future suppression.
9665          (gnus-dup-unsuppress-article article)
9666          (let* ((from-method (gnus-find-method-for-group
9667                               gnus-newsgroup-name))
9668                 (to-method (or select-method
9669                                (gnus-find-method-for-group to-newsgroup)))
9670                 (move-is-internal (gnus-method-equal from-method to-method)))
9671            (gnus-request-move-article
9672             article                     ; Article to move
9673             gnus-newsgroup-name         ; From newsgroup
9674             (nth 1 (gnus-find-method-for-group
9675                     gnus-newsgroup-name)) ; Server
9676             (list 'gnus-request-accept-article
9677                   to-newsgroup (list 'quote select-method)
9678                   (not articles) t)     ; Accept form
9679             (not articles)              ; Only save nov last time
9680             move-is-internal)))         ; is this move internal?
9681         ;; Copy the article.
9682         ((eq action 'copy)
9683          (with-current-buffer copy-buf
9684            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9685              (save-restriction
9686                (nnheader-narrow-to-headers)
9687                (dolist (hdr gnus-copy-article-ignored-headers)
9688                  (message-remove-header hdr t)))
9689              (gnus-request-accept-article
9690               to-newsgroup select-method (not articles) t))))
9691         ;; Crosspost the article.
9692         ((eq action 'crosspost)
9693          (let ((xref (message-tokenize-header
9694                       (mail-header-xref (gnus-summary-article-header article))
9695                       " ")))
9696            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9697                                   ":" (number-to-string article)))
9698            (unless xref
9699              (setq xref (list (system-name))))
9700            (setq new-xref
9701                  (concat
9702                   (mapconcat 'identity
9703                              (delete "Xref:" (delete new-xref xref))
9704                              " ")
9705                   " " new-xref))
9706            (with-current-buffer copy-buf
9707              ;; First put the article in the destination group.
9708              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9709              (when (consp (setq art-group
9710                                 (gnus-request-accept-article
9711                                  to-newsgroup select-method (not articles) t)))
9712                (setq new-xref (concat new-xref " " (car art-group)
9713                                       ":"
9714                                       (number-to-string (cdr art-group))))
9715                ;; Now we have the new Xrefs header, so we insert
9716                ;; it and replace the new article.
9717                (nnheader-replace-header "Xref" new-xref)
9718                (gnus-request-replace-article
9719                 (cdr art-group) to-newsgroup (current-buffer) t)
9720                art-group))))))
9721       (cond
9722        ((not art-group)
9723         (gnus-message 1 "Couldn't %s article %s: %s"
9724                       (cadr (assq action names)) article
9725                       (nnheader-get-report (car to-method))))
9726        ((eq art-group 'junk)
9727         (when (eq action 'move)
9728           (gnus-summary-mark-article article gnus-canceled-mark)
9729           (gnus-message 4 "Deleted article %s" article)
9730           ;; run the delete hook
9731           (run-hook-with-args 'gnus-summary-article-delete-hook
9732                               action
9733                               (gnus-data-header
9734                                (assoc article (gnus-data-list nil)))
9735                               gnus-newsgroup-name nil
9736                               select-method)))
9737        (t
9738         (let* ((pto-group (gnus-group-prefixed-name
9739                            (car art-group) to-method))
9740                (info (gnus-get-info pto-group))
9741                (to-group (gnus-info-group info))
9742                to-marks)
9743           ;; Update the group that has been moved to.
9744           (when (and info
9745                      (memq action '(move copy)))
9746             (unless (member to-group to-groups)
9747               (push to-group to-groups))
9748
9749             (unless (memq article gnus-newsgroup-unreads)
9750               (push 'read to-marks)
9751               (gnus-info-set-read
9752                info (gnus-add-to-range (gnus-info-read info)
9753                                        (list (cdr art-group)))))
9754
9755             ;; See whether the article is to be put in the cache.
9756             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9757                              gnus-article-mark-lists
9758                            (delete '(expirable . expire)
9759                                    (copy-sequence gnus-article-mark-lists))))
9760                   (to-article (cdr art-group)))
9761
9762               ;; Enter the article into the cache in the new group,
9763               ;; if that is required.
9764               (when gnus-use-cache
9765                 (gnus-cache-possibly-enter-article
9766                  to-group to-article
9767                  (memq article gnus-newsgroup-marked)
9768                  (memq article gnus-newsgroup-dormant)
9769                  (memq article gnus-newsgroup-unreads)))
9770
9771               (when gnus-preserve-marks
9772                 ;; Copy any marks over to the new group.
9773                 (when (and (equal to-group gnus-newsgroup-name)
9774                            (not (memq article gnus-newsgroup-unreads)))
9775                   ;; Mark this article as read in this group.
9776                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9777                   (setcdr (gnus-active to-group) to-article)
9778                   (setcdr gnus-newsgroup-active to-article))
9779
9780                 (while marks
9781                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9782                     (when (memq article (symbol-value
9783                                          (intern (format "gnus-newsgroup-%s"
9784                                                          (caar marks)))))
9785                       (push (cdar marks) to-marks)
9786                       ;; If the other group is the same as this group,
9787                       ;; then we have to add the mark to the list.
9788                       (when (equal to-group gnus-newsgroup-name)
9789                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9790                              (cons to-article
9791                                    (symbol-value
9792                                     (intern (format "gnus-newsgroup-%s"
9793                                                     (caar marks)))))))
9794                       ;; Copy the marks to other group.
9795                       (gnus-add-marked-articles
9796                        to-group (cdar marks) (list to-article) info)))
9797                   (setq marks (cdr marks)))
9798
9799                 (gnus-request-set-mark
9800                  to-group (list (list (list to-article) 'add to-marks))))
9801
9802               (gnus-dribble-enter
9803                (concat "(gnus-group-set-info '"
9804                        (gnus-prin1-to-string (gnus-get-info to-group))
9805                        ")"))))
9806
9807           ;; Update the Xref header in this article to point to
9808           ;; the new crossposted article we have just created.
9809           (when (eq action 'crosspost)
9810             (with-current-buffer copy-buf
9811               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9812               (nnheader-replace-header "Xref" new-xref)
9813               (gnus-request-replace-article
9814                article gnus-newsgroup-name (current-buffer) t)))
9815
9816           ;; run the move/copy/crosspost/respool hook
9817           (run-hook-with-args 'gnus-summary-article-move-hook
9818                               action
9819                               (gnus-data-header
9820                                (assoc article (gnus-data-list nil)))
9821                               gnus-newsgroup-name
9822                               to-newsgroup
9823                               select-method))
9824
9825         ;;;!!!Why is this necessary?
9826         (set-buffer gnus-summary-buffer)
9827
9828         (gnus-summary-goto-subject article)
9829         (when (eq action 'move)
9830           (gnus-summary-mark-article article gnus-canceled-mark))))
9831       (push article articles-to-update-marks))
9832
9833     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9834     ;; Re-activate all groups that have been moved to.
9835     (with-current-buffer gnus-group-buffer
9836       (let ((gnus-group-marked to-groups))
9837         (gnus-group-get-new-news-this-group nil t)))
9838
9839     (gnus-kill-buffer copy-buf)
9840     (gnus-summary-position-point)
9841     (gnus-set-mode-line 'summary)))
9842
9843 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9844   "Copy the current article to some other group.
9845 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9846 When called interactively, if TO-NEWSGROUP is nil, use the value of
9847 the variable `gnus-move-split-methods' for finding a default target
9848 newsgroup.
9849 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9850 re-spool using this method."
9851   (interactive "P")
9852   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9853
9854 (defun gnus-summary-crosspost-article (&optional n)
9855   "Crosspost the current article to some other group."
9856   (interactive "P")
9857   (gnus-summary-move-article n nil nil 'crosspost))
9858
9859 (defcustom gnus-summary-respool-default-method nil
9860   "Default method type for respooling an article.
9861 If nil, use to the current newsgroup method."
9862   :type 'symbol
9863   :group 'gnus-summary-mail)
9864
9865 (defun gnus-summary-respool-article (&optional n method)
9866   "Respool the current article.
9867 The article will be squeezed through the mail spooling process again,
9868 which means that it will be put in some mail newsgroup or other
9869 depending on `nnmail-split-methods'.
9870 If N is a positive number, respool the N next articles.
9871 If N is a negative number, respool the N previous articles.
9872 If N is nil and any articles have been marked with the process mark,
9873 respool those articles instead.
9874
9875 Respooling can be done both from mail groups and \"real\" newsgroups.
9876 In the former case, the articles in question will be moved from the
9877 current group into whatever groups they are destined to.  In the
9878 latter case, they will be copied into the relevant groups."
9879   (interactive
9880    (list current-prefix-arg
9881          (let* ((methods (gnus-methods-using 'respool))
9882                 (methname
9883                  (symbol-name (or gnus-summary-respool-default-method
9884                                   (car (gnus-find-method-for-group
9885                                         gnus-newsgroup-name)))))
9886                 (method
9887                  (gnus-completing-read-with-default
9888                   methname "Backend to use when respooling"
9889                   methods nil t nil 'gnus-mail-method-history))
9890                 ms)
9891            (cond
9892             ((zerop (length (setq ms (gnus-servers-using-backend
9893                                       (intern method)))))
9894              (list (intern method) ""))
9895             ((= 1 (length ms))
9896              (car ms))
9897             (t
9898              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9899                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9900                            ms-alist))))))))
9901   (unless method
9902     (error "No method given for respooling"))
9903   (if (assoc (symbol-name
9904               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9905              (gnus-methods-using 'respool))
9906       (gnus-summary-move-article n nil method)
9907     (gnus-summary-copy-article n nil method)))
9908
9909 (defun gnus-summary-import-article (file &optional edit)
9910   "Import an arbitrary file into a mail newsgroup."
9911   (interactive "fImport file: \nP")
9912   (let ((group gnus-newsgroup-name)
9913         (now (current-time))
9914         atts lines group-art)
9915     (unless (gnus-check-backend-function 'request-accept-article group)
9916       (error "%s does not support article importing" group))
9917     (or (file-readable-p file)
9918         (not (file-regular-p file))
9919         (error "Can't read %s" file))
9920     (with-current-buffer (gnus-get-buffer-create " *import file*")
9921       (erase-buffer)
9922       (nnheader-insert-file-contents file)
9923       (goto-char (point-min))
9924       (if (nnheader-article-p)
9925           (save-restriction
9926             (goto-char (point-min))
9927             (search-forward "\n\n" nil t)
9928             (narrow-to-region (point-min) (1- (point)))
9929             (goto-char (point-min))
9930             (unless (re-search-forward "^date:" nil t)
9931               (goto-char (point-max))
9932               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9933        ;; This doesn't look like an article, so we fudge some headers.
9934         (setq atts (file-attributes file)
9935               lines (count-lines (point-min) (point-max)))
9936         (insert "From: " (read-string "From: ") "\n"
9937                 "Subject: " (read-string "Subject: ") "\n"
9938                 "Date: " (message-make-date (nth 5 atts)) "\n"
9939                 "Message-ID: " (message-make-message-id) "\n"
9940                 "Lines: " (int-to-string lines) "\n"
9941                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9942       (setq group-art (gnus-request-accept-article group nil t))
9943       (kill-buffer (current-buffer)))
9944     (setq gnus-newsgroup-active (gnus-activate-group group))
9945     (forward-line 1)
9946     (gnus-summary-goto-article (cdr group-art) nil t)
9947     (when edit
9948       (gnus-summary-edit-article))))
9949
9950 (defun gnus-summary-create-article ()
9951   "Create an article in a mail newsgroup."
9952   (interactive)
9953   (let ((group gnus-newsgroup-name)
9954         (now (current-time))
9955         group-art)
9956     (unless (gnus-check-backend-function 'request-accept-article group)
9957       (error "%s does not support article importing" group))
9958     (with-current-buffer (gnus-get-buffer-create " *import file*")
9959       (erase-buffer)
9960       (goto-char (point-min))
9961       ;; This doesn't look like an article, so we fudge some headers.
9962       (insert "From: " (read-string "From: ") "\n"
9963               "Subject: " (read-string "Subject: ") "\n"
9964               "Date: " (message-make-date now) "\n"
9965               "Message-ID: " (message-make-message-id) "\n")
9966       (setq group-art (gnus-request-accept-article group nil t))
9967       (kill-buffer (current-buffer)))
9968     (setq gnus-newsgroup-active (gnus-activate-group group))
9969     (forward-line 1)
9970     (gnus-summary-goto-article (cdr group-art) nil t)
9971     (gnus-summary-edit-article)))
9972
9973 (defun gnus-summary-article-posted-p ()
9974   "Say whether the current (mail) article is available from news as well.
9975 This will be the case if the article has both been mailed and posted."
9976   (interactive)
9977   (let ((id (mail-header-references (gnus-summary-article-header)))
9978         (gnus-override-method (car (gnus-refer-article-methods))))
9979     (if (gnus-request-head id "")
9980         (gnus-message 2 "The current message was found on %s"
9981                       gnus-override-method)
9982       (gnus-message 2 "The current message couldn't be found on %s"
9983                     gnus-override-method)
9984       nil)))
9985
9986 (defun gnus-summary-expire-articles (&optional now)
9987   "Expire all articles that are marked as expirable in the current group."
9988   (interactive)
9989   (when (and (not gnus-group-is-exiting-without-update-p)
9990              (gnus-check-backend-function
9991               'request-expire-articles gnus-newsgroup-name))
9992     ;; This backend supports expiry.
9993     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9994            (expirable (if total
9995                           (progn
9996                             ;; We need to update the info for
9997                             ;; this group for `gnus-list-of-read-articles'
9998                             ;; to give us the right answer.
9999                             (gnus-run-hooks 'gnus-exit-group-hook)
10000                             (gnus-summary-update-info)
10001                             (gnus-list-of-read-articles gnus-newsgroup-name))
10002                         (setq gnus-newsgroup-expirable
10003                               (sort gnus-newsgroup-expirable '<))))
10004            (expiry-wait (if now 'immediate
10005                           (gnus-group-find-parameter
10006                            gnus-newsgroup-name 'expiry-wait)))
10007            (nnmail-expiry-target
10008             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10009                 nnmail-expiry-target))
10010            es)
10011       (when expirable
10012         ;; There are expirable articles in this group, so we run them
10013         ;; through the expiry process.
10014         (gnus-message 6 "Expiring articles...")
10015         (unless (gnus-check-group gnus-newsgroup-name)
10016           (error "Can't open server for %s" gnus-newsgroup-name))
10017         ;; The list of articles that weren't expired is returned.
10018         (save-excursion
10019           (if expiry-wait
10020               (let ((nnmail-expiry-wait-function nil)
10021                     (nnmail-expiry-wait expiry-wait))
10022                 (setq es (gnus-request-expire-articles
10023                           expirable gnus-newsgroup-name)))
10024             (setq es (gnus-request-expire-articles
10025                       expirable gnus-newsgroup-name)))
10026           (unless total
10027             (setq gnus-newsgroup-expirable es))
10028           ;; We go through the old list of expirable, and mark all
10029           ;; really expired articles as nonexistent.
10030           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10031             (let ((gnus-use-cache nil))
10032               (dolist (article expirable)
10033                 (when (and (not (memq article es))
10034                            (gnus-data-find article))
10035                   (gnus-summary-mark-article article gnus-canceled-mark)
10036                   (run-hook-with-args 'gnus-summary-article-expire-hook
10037                                       'delete
10038                                       (gnus-data-header
10039                                        (assoc article (gnus-data-list nil)))
10040                                       gnus-newsgroup-name
10041                                       nil
10042                                       nil))))))
10043         (gnus-message 6 "Expiring articles...done")))))
10044
10045 (defun gnus-summary-expire-articles-now ()
10046   "Expunge all expirable articles in the current group.
10047 This means that *all* articles that are marked as expirable will be
10048 deleted forever, right now."
10049   (interactive)
10050   (or gnus-expert-user
10051       (gnus-yes-or-no-p
10052        "Are you really, really sure you want to delete all expirable messages? ")
10053       (error "Phew!"))
10054   (gnus-summary-expire-articles t))
10055
10056 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10057 (defun gnus-summary-delete-article (&optional n)
10058   "Delete the N next (mail) articles.
10059 This command actually deletes articles.  This is not a marking
10060 command.  The article will disappear forever from your life, never to
10061 return.
10062
10063 If N is negative, delete backwards.
10064 If N is nil and articles have been marked with the process mark,
10065 delete these instead.
10066
10067 If `gnus-novice-user' is non-nil you will be asked for
10068 confirmation before the articles are deleted."
10069   (interactive "P")
10070   (unless (gnus-check-backend-function 'request-expire-articles
10071                                        gnus-newsgroup-name)
10072     (error "The current newsgroup does not support article deletion"))
10073   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10074     (error "Couldn't open server"))
10075   ;; Compute the list of articles to delete.
10076   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10077         (nnmail-expiry-target 'delete)
10078         not-deleted)
10079     (if (and gnus-novice-user
10080              (not (gnus-yes-or-no-p
10081                    (format "Do you really want to delete %s forever? "
10082                            (if (> (length articles) 1)
10083                                (format "these %s articles" (length articles))
10084                              "this article")))))
10085         ()
10086       ;; Delete the articles.
10087       (setq not-deleted (gnus-request-expire-articles
10088                          articles gnus-newsgroup-name 'force))
10089       (while articles
10090         (gnus-summary-remove-process-mark (car articles))
10091         ;; The backend might not have been able to delete the article
10092         ;; after all.
10093         (unless (memq (car articles) not-deleted)
10094           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10095         (let* ((article (car articles))
10096                (ghead  (gnus-data-header
10097                                     (assoc article (gnus-data-list nil)))))
10098           (run-hook-with-args 'gnus-summary-article-delete-hook
10099                               'delete ghead gnus-newsgroup-name nil
10100                               nil))
10101         (setq articles (cdr articles)))
10102       (when not-deleted
10103         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10104     (gnus-summary-position-point)
10105     (gnus-set-mode-line 'summary)
10106     not-deleted))
10107
10108 (defun gnus-summary-edit-article (&optional arg)
10109   "Edit the current article.
10110 This will have permanent effect only in mail groups.
10111 If ARG is nil, edit the decoded articles.
10112 If ARG is 1, edit the raw articles.
10113 If ARG is 2, edit the raw articles even in read-only groups.
10114 If ARG is 3, edit the articles with the current handles.
10115 Otherwise, allow editing of articles even in read-only
10116 groups."
10117   (interactive "P")
10118   (let (force raw current-handles)
10119     (cond
10120      ((null arg))
10121      ((eq arg 1)
10122       (setq raw t))
10123      ((eq arg 2)
10124       (setq raw t
10125             force t))
10126      ((eq arg 3)
10127       (setq current-handles
10128             (and (gnus-buffer-live-p gnus-article-buffer)
10129                  (with-current-buffer gnus-article-buffer
10130                    (prog1
10131                        gnus-article-mime-handles
10132                      (setq gnus-article-mime-handles nil))))))
10133      (t
10134       (setq force t)))
10135     (when (and raw (not force)
10136                (member gnus-newsgroup-name '("nndraft:delayed"
10137                                              "nndraft:drafts"
10138                                              "nndraft:queue")))
10139       (error "Can't edit the raw article in group %s"
10140              gnus-newsgroup-name))
10141     (with-current-buffer gnus-summary-buffer
10142       (let ((mail-parse-charset gnus-newsgroup-charset)
10143             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10144         (gnus-set-global-variables)
10145         (when (and (not force)
10146                    (gnus-group-read-only-p))
10147           (error "The current newsgroup does not support article editing"))
10148         (gnus-summary-show-article t)
10149         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10150           (with-current-buffer gnus-article-buffer
10151             (mm-enable-multibyte)))
10152         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10153             (setq raw t))
10154         (gnus-article-edit-article
10155          (if raw 'ignore
10156            `(lambda ()
10157               (let ((mbl mml-buffer-list))
10158                 (setq mml-buffer-list nil)
10159                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10160                   (mime-to-mml ,'current-handles))
10161                 (let ((mbl1 mml-buffer-list))
10162                   (setq mml-buffer-list mbl)
10163                   (set (make-local-variable 'mml-buffer-list) mbl1))
10164                 (gnus-make-local-hook 'kill-buffer-hook)
10165                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10166          `(lambda (no-highlight)
10167             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10168                   (message-options message-options)
10169                   (message-options-set-recipient)
10170                   (mail-parse-ignored-charsets
10171                    ',gnus-newsgroup-ignored-charsets)
10172                   (rfc2047-header-encoding-alist
10173                    ',(let ((charset (gnus-group-name-charset
10174                                      (gnus-find-method-for-group
10175                                       gnus-newsgroup-name)
10176                                      gnus-newsgroup-name)))
10177                        (append (list (cons "Newsgroups" charset)
10178                                      (cons "Followup-To" charset)
10179                                      (cons "Xref" charset))
10180                                rfc2047-header-encoding-alist))))
10181               ,(if (not raw) '(progn
10182                                 (mml-to-mime)
10183                                 (mml-destroy-buffers)
10184                                 (remove-hook 'kill-buffer-hook
10185                                              'mml-destroy-buffers t)
10186                                 (kill-local-variable 'mml-buffer-list)))
10187               (gnus-summary-edit-article-done
10188                ,(or (mail-header-references gnus-current-headers) "")
10189                ,(gnus-group-read-only-p)
10190                ,gnus-summary-buffer no-highlight))))))))
10191
10192 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10193
10194 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10195                                                  no-highlight)
10196   "Make edits to the current article permanent."
10197   (interactive)
10198   (save-excursion
10199    ;; The buffer restriction contains the entire article if it exists.
10200     (when (article-goto-body)
10201       (let ((lines (count-lines (point) (point-max)))
10202             (length (- (point-max) (point)))
10203             (case-fold-search t)
10204             (body (copy-marker (point))))
10205         (goto-char (point-min))
10206         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10207           (delete-region (match-beginning 1) (match-end 1))
10208           (insert (number-to-string length)))
10209         (goto-char (point-min))
10210         (when (re-search-forward
10211                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10212           (delete-region (match-beginning 1) (match-end 1))
10213           (insert (number-to-string length)))
10214         (goto-char (point-min))
10215         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10216           (delete-region (match-beginning 1) (match-end 1))
10217           (insert (number-to-string lines))))))
10218   ;; Replace the article.
10219   (let ((buf (current-buffer)))
10220     (with-temp-buffer
10221       (insert-buffer-substring buf)
10222
10223       (if (and (not read-only)
10224                (not (gnus-request-replace-article
10225                      (cdr gnus-article-current) (car gnus-article-current)
10226                      (current-buffer) t)))
10227           (error "Couldn't replace article")
10228         ;; Update the summary buffer.
10229         (if (and references
10230                  (equal (message-tokenize-header references " ")
10231                         (message-tokenize-header
10232                          (or (message-fetch-field "references") "") " ")))
10233             ;; We only have to update this line.
10234             (save-excursion
10235               (save-restriction
10236                 (message-narrow-to-head)
10237                 (let ((head (buffer-substring-no-properties
10238                              (point-min) (point-max)))
10239                       header)
10240                   (with-temp-buffer
10241                     (insert (format "211 %d Article retrieved.\n"
10242                                     (cdr gnus-article-current)))
10243                     (insert head)
10244                     (insert ".\n")
10245                     (let ((nntp-server-buffer (current-buffer)))
10246                       (setq header (car (gnus-get-newsgroup-headers
10247                                          nil t))))
10248                     (with-current-buffer gnus-summary-buffer
10249                       (gnus-data-set-header
10250                        (gnus-data-find (cdr gnus-article-current))
10251                        header)
10252                       (gnus-summary-update-article-line
10253                        (cdr gnus-article-current) header)
10254                       (if (gnus-summary-goto-subject
10255                            (cdr gnus-article-current) nil t)
10256                           (gnus-summary-update-secondary-mark
10257                            (cdr gnus-article-current))))))))
10258           ;; Update threads.
10259           (set-buffer (or buffer gnus-summary-buffer))
10260           (gnus-summary-update-article (cdr gnus-article-current))
10261           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10262               (gnus-summary-update-secondary-mark
10263                (cdr gnus-article-current))))
10264         ;; Prettify the article buffer again.
10265         (unless no-highlight
10266           (with-current-buffer gnus-article-buffer
10267             ;;;!!! Fix this -- article should be rehighlighted.
10268             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10269             (set-buffer gnus-original-article-buffer)
10270             (gnus-request-article
10271              (cdr gnus-article-current)
10272              (car gnus-article-current) (current-buffer))))
10273         ;; Prettify the summary buffer line.
10274         (when (gnus-visual-p 'summary-highlight 'highlight)
10275           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10276
10277 (defun gnus-summary-edit-wash (key)
10278   "Perform editing command KEY in the article buffer."
10279   (interactive
10280    (list
10281     (progn
10282       (message "%s" (concat (this-command-keys) "- "))
10283       (read-char))))
10284   (message "")
10285   (gnus-summary-edit-article)
10286   (execute-kbd-macro (concat (this-command-keys) key))
10287   (gnus-article-edit-done))
10288
10289 ;;; Respooling
10290
10291 (defun gnus-summary-respool-query (&optional silent trace)
10292   "Query where the respool algorithm would put this article."
10293   (interactive)
10294   (let (gnus-mark-article-hook)
10295     (gnus-summary-select-article)
10296     (with-current-buffer gnus-original-article-buffer
10297       (let ((groups (nnmail-article-group 'identity trace)))
10298         (unless silent
10299           (if groups
10300               (message "This message would go to %s"
10301                        (mapconcat 'car groups ", "))
10302             (message "This message would go to no groups"))
10303           groups)))))
10304
10305 (defun gnus-summary-respool-trace ()
10306   "Trace where the respool algorithm would put this article.
10307 Display a buffer showing all fancy splitting patterns which matched."
10308   (interactive)
10309   (gnus-summary-respool-query nil t))
10310
10311 ;; Summary marking commands.
10312
10313 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10314   "Mark articles which has the same subject as read, and then select the next.
10315 If UNMARK is positive, remove any kind of mark.
10316 If UNMARK is negative, tick articles."
10317   (interactive "P")
10318   (when unmark
10319     (setq unmark (prefix-numeric-value unmark)))
10320   (let ((count
10321          (gnus-summary-mark-same-subject
10322           (gnus-summary-article-subject) unmark)))
10323     ;; Select next unread article.  If auto-select-same mode, should
10324     ;; select the first unread article.
10325     (gnus-summary-next-article t (and gnus-auto-select-same
10326                                       (gnus-summary-article-subject)))
10327     (gnus-message 7 "%d article%s marked as %s"
10328                   count (if (= count 1) " is" "s are")
10329                   (if unmark "unread" "read"))))
10330
10331 (defun gnus-summary-kill-same-subject (&optional unmark)
10332   "Mark articles which has the same subject as read.
10333 If UNMARK is positive, remove any kind of mark.
10334 If UNMARK is negative, tick articles."
10335   (interactive "P")
10336   (when unmark
10337     (setq unmark (prefix-numeric-value unmark)))
10338   (let ((count
10339          (gnus-summary-mark-same-subject
10340           (gnus-summary-article-subject) unmark)))
10341     ;; If marked as read, go to next unread subject.
10342     (when (null unmark)
10343       ;; Go to next unread subject.
10344       (gnus-summary-next-subject 1 t))
10345     (gnus-message 7 "%d articles are marked as %s"
10346                   count (if unmark "unread" "read"))))
10347
10348 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10349   "Mark articles with same SUBJECT as read, and return marked number.
10350 If optional argument UNMARK is positive, remove any kinds of marks.
10351 If optional argument UNMARK is negative, mark articles as unread instead."
10352   (let ((count 1))
10353     (save-excursion
10354       (cond
10355        ((null unmark)                   ; Mark as read.
10356         (while (and
10357                 (progn
10358                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10359                   (gnus-summary-show-thread) t)
10360                 (gnus-summary-find-subject subject))
10361           (setq count (1+ count))))
10362        ((> unmark 0)                    ; Tick.
10363         (while (and
10364                 (progn
10365                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10366                   (gnus-summary-show-thread) t)
10367                 (gnus-summary-find-subject subject))
10368           (setq count (1+ count))))
10369        (t                               ; Mark as unread.
10370         (while (and
10371                 (progn
10372                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10373                   (gnus-summary-show-thread) t)
10374                 (gnus-summary-find-subject subject))
10375           (setq count (1+ count)))))
10376       (gnus-set-mode-line 'summary)
10377       ;; Return the number of marked articles.
10378       count)))
10379
10380 (defun gnus-summary-mark-as-processable (n &optional unmark)
10381   "Set the process mark on the next N articles.
10382 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10383 the process mark instead.  The difference between N and the actual
10384 number of articles marked is returned."
10385   (interactive "P")
10386   (if (and (null n) (gnus-region-active-p))
10387       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10388     (setq n (prefix-numeric-value n))
10389     (let ((backward (< n 0))
10390           (n (abs n)))
10391       (while (and
10392               (> n 0)
10393               (if unmark
10394                   (gnus-summary-remove-process-mark
10395                    (gnus-summary-article-number))
10396                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10397               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10398         (setq n (1- n)))
10399       (when (/= 0 n)
10400         (gnus-message 7 "No more articles"))
10401       (gnus-summary-recenter)
10402       (gnus-summary-position-point)
10403       n)))
10404
10405 (defun gnus-summary-unmark-as-processable (n)
10406   "Remove the process mark from the next N articles.
10407 If N is negative, unmark backward instead.  The difference between N and
10408 the actual number of articles unmarked is returned."
10409   (interactive "P")
10410   (gnus-summary-mark-as-processable n t))
10411
10412 (defun gnus-summary-unmark-all-processable ()
10413   "Remove the process mark from all articles."
10414   (interactive)
10415   (save-excursion
10416     (while gnus-newsgroup-processable
10417       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10418   (gnus-summary-position-point))
10419
10420 (defun gnus-summary-add-mark (article type)
10421   "Mark ARTICLE with a mark of TYPE."
10422   (let ((vtype (car (assq type gnus-article-mark-lists)))
10423         var)
10424     (if (not vtype)
10425         (error "No such mark type: %s" type)
10426       (setq var (intern (format "gnus-newsgroup-%s" type)))
10427       (set var (cons article (symbol-value var)))
10428       (if (memq type '(processable cached replied forwarded recent saved))
10429           (gnus-summary-update-secondary-mark article)
10430         ;;; !!! This is bogus.  We should find out what primary
10431         ;;; !!! mark we want to set.
10432         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10433
10434 (defun gnus-summary-mark-as-expirable (n)
10435   "Mark N articles forward as expirable.
10436 If N is negative, mark backward instead.  The difference between N and
10437 the actual number of articles marked is returned."
10438   (interactive "p")
10439   (gnus-summary-mark-forward n gnus-expirable-mark))
10440
10441 (defun gnus-summary-mark-as-spam (n)
10442   "Mark N articles forward as spam.
10443 If N is negative, mark backward instead.  The difference between N and
10444 the actual number of articles marked is returned."
10445   (interactive "p")
10446   (gnus-summary-mark-forward n gnus-spam-mark))
10447
10448 (defun gnus-summary-mark-article-as-replied (article)
10449   "Mark ARTICLE as replied to and update the summary line.
10450 ARTICLE can also be a list of articles."
10451   (interactive (list (gnus-summary-article-number)))
10452   (let ((articles (if (listp article) article (list article))))
10453     (dolist (article articles)
10454       (unless (numberp article)
10455         (error "%s is not a number" article))
10456       (push article gnus-newsgroup-replied)
10457       (let ((inhibit-read-only t))
10458         (when (gnus-summary-goto-subject article nil t)
10459           (gnus-summary-update-secondary-mark article))))))
10460
10461 (defun gnus-summary-mark-article-as-forwarded (article)
10462   "Mark ARTICLE as forwarded and update the summary line.
10463 ARTICLE can also be a list of articles."
10464   (let ((articles (if (listp article) article (list article))))
10465     (dolist (article articles)
10466       (push article gnus-newsgroup-forwarded)
10467       (let ((inhibit-read-only t))
10468         (when (gnus-summary-goto-subject article nil t)
10469           (gnus-summary-update-secondary-mark article))))))
10470
10471 (defun gnus-summary-set-bookmark (article)
10472   "Set a bookmark in current article."
10473   (interactive (list (gnus-summary-article-number)))
10474   (when (or (not (get-buffer gnus-article-buffer))
10475             (not gnus-current-article)
10476             (not gnus-article-current)
10477             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10478     (error "No current article selected"))
10479   ;; Remove old bookmark, if one exists.
10480   (gnus-pull article gnus-newsgroup-bookmarks)
10481   ;; Set the new bookmark, which is on the form
10482   ;; (article-number . line-number-in-body).
10483   (push
10484    (cons article
10485          (with-current-buffer gnus-article-buffer
10486            (count-lines
10487             (min (point)
10488                  (save-excursion
10489                    (article-goto-body)
10490                    (point)))
10491             (point))))
10492    gnus-newsgroup-bookmarks)
10493   (gnus-message 6 "A bookmark has been added to the current article."))
10494
10495 (defun gnus-summary-remove-bookmark (article)
10496   "Remove the bookmark from the current article."
10497   (interactive (list (gnus-summary-article-number)))
10498   ;; Remove old bookmark, if one exists.
10499   (if (not (assq article gnus-newsgroup-bookmarks))
10500       (gnus-message 6 "No bookmark in current article.")
10501     (gnus-pull article gnus-newsgroup-bookmarks)
10502     (gnus-message 6 "Removed bookmark.")))
10503
10504 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10505 (defun gnus-summary-mark-as-dormant (n)
10506   "Mark N articles forward as dormant.
10507 If N is negative, mark backward instead.  The difference between N and
10508 the actual number of articles marked is returned."
10509   (interactive "p")
10510   (gnus-summary-mark-forward n gnus-dormant-mark))
10511
10512 (defun gnus-summary-set-process-mark (article)
10513   "Set the process mark on ARTICLE and update the summary line."
10514   (setq gnus-newsgroup-processable
10515         (cons article
10516               (delq article gnus-newsgroup-processable)))
10517   (when (gnus-summary-goto-subject article)
10518     (gnus-summary-show-thread)
10519     (gnus-summary-goto-subject article)
10520     (gnus-summary-update-secondary-mark article)))
10521
10522 (defun gnus-summary-remove-process-mark (&rest articles)
10523   "Remove the process mark from ARTICLES and update the summary line."
10524   (dolist (article articles)
10525     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10526     (when (gnus-summary-goto-subject article)
10527       (gnus-summary-show-thread)
10528       (gnus-summary-goto-subject article)
10529       (gnus-summary-update-secondary-mark article)))
10530   t)
10531
10532 (defun gnus-summary-set-saved-mark (article)
10533   "Set the process mark on ARTICLE and update the summary line."
10534   (push article gnus-newsgroup-saved)
10535   (when (gnus-summary-goto-subject article)
10536     (gnus-summary-update-secondary-mark article)))
10537
10538 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10539   "Mark N articles as read forwards.
10540 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10541 The difference between N and the actual number of articles marked is
10542 returned.
10543 If NO-EXPIRE, auto-expiry will be inhibited."
10544   (interactive "p")
10545   (gnus-summary-show-thread)
10546   (let ((backward (< n 0))
10547         (gnus-summary-goto-unread
10548          (and gnus-summary-goto-unread
10549               (not (eq gnus-summary-goto-unread 'never))
10550               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10551                                     gnus-ticked-mark gnus-dormant-mark)))))
10552         (n (abs n))
10553         (mark (or mark gnus-del-mark)))
10554     (while (and (> n 0)
10555                 (gnus-summary-mark-article nil mark no-expire)
10556                 (zerop (gnus-summary-next-subject
10557                         (if backward -1 1)
10558                         (and gnus-summary-goto-unread
10559                              (not (eq gnus-summary-goto-unread 'never)))
10560                         t)))
10561       (setq n (1- n)))
10562     (when (/= 0 n)
10563       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10564     (gnus-summary-recenter)
10565     (gnus-summary-position-point)
10566     (gnus-set-mode-line 'summary)
10567     n))
10568
10569 (defun gnus-summary-mark-article-as-read (mark)
10570   "Mark the current article quickly as read with MARK."
10571   (let ((article (gnus-summary-article-number)))
10572     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10573     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10574     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10575     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10576     (push (cons article mark) gnus-newsgroup-reads)
10577     ;; Possibly remove from cache, if that is used.
10578     (when gnus-use-cache
10579       (gnus-cache-enter-remove-article article))
10580     ;; Allow the backend to change the mark.
10581     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10582     ;; Check for auto-expiry.
10583     (when (and gnus-newsgroup-auto-expire
10584                (memq mark gnus-auto-expirable-marks))
10585       (setq mark gnus-expirable-mark)
10586       ;; Let the backend know about the mark change.
10587       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10588       (push article gnus-newsgroup-expirable))
10589     ;; Set the mark in the buffer.
10590     (gnus-summary-update-mark mark 'unread)
10591     t))
10592
10593 (defun gnus-summary-mark-article-as-unread (mark)
10594   "Mark the current article quickly as unread with MARK."
10595   (let* ((article (gnus-summary-article-number))
10596          (old-mark (gnus-summary-article-mark article)))
10597     ;; Allow the backend to change the mark.
10598     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10599     (if (eq mark old-mark)
10600         t
10601       (if (<= article 0)
10602           (progn
10603             (gnus-error 1 "Can't mark negative article numbers")
10604             nil)
10605         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10606         (setq gnus-newsgroup-spam-marked
10607               (delq article gnus-newsgroup-spam-marked))
10608         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10609         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10610         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10611         (cond ((= mark gnus-ticked-mark)
10612                (setq gnus-newsgroup-marked
10613                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10614                                               article)))
10615               ((= mark gnus-spam-mark)
10616                (setq gnus-newsgroup-spam-marked
10617                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10618                                               article)))
10619               ((= mark gnus-dormant-mark)
10620                (setq gnus-newsgroup-dormant
10621                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10622                                               article)))
10623               (t
10624                (setq gnus-newsgroup-unreads
10625                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10626                                               article))))
10627         (gnus-pull article gnus-newsgroup-reads)
10628
10629         ;; See whether the article is to be put in the cache.
10630         (and gnus-use-cache
10631              (vectorp (gnus-summary-article-header article))
10632              (save-excursion
10633                (gnus-cache-possibly-enter-article
10634                 gnus-newsgroup-name article
10635                 (= mark gnus-ticked-mark)
10636                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10637
10638         ;; Fix the mark.
10639         (gnus-summary-update-mark mark 'unread)
10640         t))))
10641
10642 (defun gnus-summary-mark-article (&optional article mark no-expire)
10643   "Mark ARTICLE with MARK.  MARK can be any character.
10644 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10645 `??' (dormant) and `?E' (expirable).
10646 If MARK is nil, then the default character `?r' is used.
10647 If ARTICLE is nil, then the article on the current line will be
10648 marked.
10649 If NO-EXPIRE, auto-expiry will be inhibited."
10650   ;; The mark might be a string.
10651   (when (stringp mark)
10652     (setq mark (aref mark 0)))
10653   ;; If no mark is given, then we check auto-expiring.
10654   (when (null mark)
10655     (setq mark gnus-del-mark))
10656   (when (and (not no-expire)
10657              gnus-newsgroup-auto-expire
10658              (memq mark gnus-auto-expirable-marks))
10659     (setq mark gnus-expirable-mark))
10660   (let ((article (or article (gnus-summary-article-number)))
10661         (old-mark (gnus-summary-article-mark article)))
10662     ;; Allow the backend to change the mark.
10663     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10664     (if (eq mark old-mark)
10665         t
10666       (unless article
10667         (error "No article on current line"))
10668       (if (not (if (or (= mark gnus-unread-mark)
10669                        (= mark gnus-ticked-mark)
10670                        (= mark gnus-spam-mark)
10671                        (= mark gnus-dormant-mark))
10672                    (gnus-mark-article-as-unread article mark)
10673                  (gnus-mark-article-as-read article mark)))
10674           t
10675         ;; See whether the article is to be put in the cache.
10676         (and gnus-use-cache
10677              (not (= mark gnus-canceled-mark))
10678              (vectorp (gnus-summary-article-header article))
10679              (save-excursion
10680                (gnus-cache-possibly-enter-article
10681                 gnus-newsgroup-name article
10682                 (= mark gnus-ticked-mark)
10683                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10684
10685         (when (gnus-summary-goto-subject article nil t)
10686           (let ((inhibit-read-only t))
10687             (gnus-summary-show-thread)
10688             ;; Fix the mark.
10689             (gnus-summary-update-mark mark 'unread)
10690             t))))))
10691
10692 (defun gnus-summary-update-secondary-mark (article)
10693   "Update the secondary (read, process, cache) mark."
10694   (gnus-summary-update-mark
10695    (cond ((memq article gnus-newsgroup-processable)
10696           gnus-process-mark)
10697          ((memq article gnus-newsgroup-cached)
10698           gnus-cached-mark)
10699          ((memq article gnus-newsgroup-replied)
10700           gnus-replied-mark)
10701          ((memq article gnus-newsgroup-forwarded)
10702           gnus-forwarded-mark)
10703          ((memq article gnus-newsgroup-saved)
10704           gnus-saved-mark)
10705          ((memq article gnus-newsgroup-recent)
10706           gnus-recent-mark)
10707          ((memq article gnus-newsgroup-unseen)
10708           gnus-unseen-mark)
10709          (t gnus-no-mark))
10710    'replied)
10711   (when (gnus-visual-p 'summary-highlight 'highlight)
10712     (gnus-run-hooks 'gnus-summary-update-hook))
10713   t)
10714
10715 (defun gnus-summary-update-download-mark (article)
10716   "Update the download mark."
10717   (gnus-summary-update-mark
10718    (cond ((memq article gnus-newsgroup-undownloaded)
10719           gnus-undownloaded-mark)
10720          (gnus-newsgroup-agentized
10721           gnus-downloaded-mark)
10722          (t
10723           gnus-no-mark))
10724    'download)
10725   (gnus-summary-update-line t)
10726   t)
10727
10728 (defun gnus-summary-update-mark (mark type)
10729   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10730         (inhibit-read-only t))
10731     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10732     (when forward
10733       (when (looking-at "\r")
10734         (incf forward))
10735       (when (<= (+ forward (point)) (point-max))
10736         ;; Go to the right position on the line.
10737         (goto-char (+ forward (point)))
10738         ;; Replace the old mark with the new mark.
10739         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10740         ;; Optionally update the marks by some user rule.
10741         (when (eq type 'unread)
10742           (gnus-data-set-mark
10743            (gnus-data-find (gnus-summary-article-number)) mark)
10744           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10745
10746 (defun gnus-mark-article-as-read (article &optional mark)
10747   "Enter ARTICLE in the pertinent lists and remove it from others."
10748   ;; Make the article expirable.
10749   (let ((mark (or mark gnus-del-mark)))
10750     (setq gnus-newsgroup-expirable
10751           (if (= mark gnus-expirable-mark)
10752               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10753             (delq article gnus-newsgroup-expirable)))
10754     ;; Remove from unread and marked lists.
10755     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10756     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10757     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10758     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10759     (push (cons article mark) gnus-newsgroup-reads)
10760     ;; Possibly remove from cache, if that is used.
10761     (when gnus-use-cache
10762       (gnus-cache-enter-remove-article article))
10763     t))
10764
10765 (defun gnus-mark-article-as-unread (article &optional mark)
10766   "Enter ARTICLE in the pertinent lists and remove it from others."
10767   (let ((mark (or mark gnus-ticked-mark)))
10768     (if (<= article 0)
10769         (progn
10770           (gnus-error 1 "Can't mark negative article numbers")
10771           nil)
10772       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10773             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10774             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10775             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10776             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10777
10778       ;; Unsuppress duplicates?
10779       (when gnus-suppress-duplicates
10780         (gnus-dup-unsuppress-article article))
10781
10782       (cond ((= mark gnus-ticked-mark)
10783              (setq gnus-newsgroup-marked
10784                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10785             ((= mark gnus-spam-mark)
10786              (setq gnus-newsgroup-spam-marked
10787                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10788                                             article)))
10789             ((= mark gnus-dormant-mark)
10790              (setq gnus-newsgroup-dormant
10791                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10792             (t
10793              (setq gnus-newsgroup-unreads
10794                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10795       (gnus-pull article gnus-newsgroup-reads)
10796       t)))
10797
10798 (defalias 'gnus-summary-mark-as-unread-forward
10799   'gnus-summary-tick-article-forward)
10800 (make-obsolete 'gnus-summary-mark-as-unread-forward
10801                'gnus-summary-tick-article-forward)
10802 (defun gnus-summary-tick-article-forward (n)
10803   "Tick N articles forwards.
10804 If N is negative, tick backwards instead.
10805 The difference between N and the number of articles ticked is returned."
10806   (interactive "p")
10807   (gnus-summary-mark-forward n gnus-ticked-mark))
10808
10809 (defalias 'gnus-summary-mark-as-unread-backward
10810   'gnus-summary-tick-article-backward)
10811 (make-obsolete 'gnus-summary-mark-as-unread-backward
10812                'gnus-summary-tick-article-backward)
10813 (defun gnus-summary-tick-article-backward (n)
10814   "Tick N articles backwards.
10815 The difference between N and the number of articles ticked is returned."
10816   (interactive "p")
10817   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10818
10819 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10820 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10821 (defun gnus-summary-tick-article (&optional article clear-mark)
10822   "Mark current article as unread.
10823 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10824 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10825   (interactive)
10826   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10827                                        gnus-ticked-mark)))
10828
10829 (defun gnus-summary-mark-as-read-forward (n)
10830   "Mark N articles as read forwards.
10831 If N is negative, mark backwards instead.
10832 The difference between N and the actual number of articles marked is
10833 returned."
10834   (interactive "p")
10835   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10836
10837 (defun gnus-summary-mark-as-read-backward (n)
10838   "Mark the N articles as read backwards.
10839 The difference between N and the actual number of articles marked is
10840 returned."
10841   (interactive "p")
10842   (gnus-summary-mark-forward
10843    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10844
10845 (defun gnus-summary-mark-as-read (&optional article mark)
10846   "Mark current article as read.
10847 ARTICLE specifies the article to be marked as read.
10848 MARK specifies a string to be inserted at the beginning of the line."
10849   (gnus-summary-mark-article article mark))
10850
10851 (defun gnus-summary-clear-mark-forward (n)
10852   "Clear marks from N articles forward.
10853 If N is negative, clear backward instead.
10854 The difference between N and the number of marks cleared is returned."
10855   (interactive "p")
10856   (gnus-summary-mark-forward n gnus-unread-mark))
10857
10858 (defun gnus-summary-clear-mark-backward (n)
10859   "Clear marks from N articles backward.
10860 The difference between N and the number of marks cleared is returned."
10861   (interactive "p")
10862   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10863
10864 (defun gnus-summary-mark-unread-as-read ()
10865   "Intended to be used by `gnus-mark-article-hook'."
10866   (when (memq gnus-current-article gnus-newsgroup-unreads)
10867     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10868
10869 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10870   "Intended to be used by `gnus-mark-article-hook'."
10871   (let ((mark (gnus-summary-article-mark)))
10872     (when (or (gnus-unread-mark-p mark)
10873               (gnus-read-mark-p mark))
10874       (gnus-summary-mark-article gnus-current-article
10875                                  (or new-mark gnus-read-mark)))))
10876
10877 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10878   "Intended to be used by `gnus-mark-article-hook'."
10879   (let ((mark (gnus-summary-article-mark)))
10880     (when (or (gnus-unread-mark-p mark)
10881               (gnus-read-mark-p mark))
10882       (gnus-summary-mark-article (gnus-summary-article-number)
10883                                  (or new-mark gnus-read-mark)))))
10884
10885 (defun gnus-summary-mark-unread-as-ticked ()
10886   "Intended to be used by `gnus-mark-article-hook'."
10887   (when (memq gnus-current-article gnus-newsgroup-unreads)
10888     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10889
10890 (defun gnus-summary-mark-region-as-read (point mark all)
10891   "Mark all unread articles between point and mark as read.
10892 If given a prefix, mark all articles between point and mark as read,
10893 even ticked and dormant ones."
10894   (interactive "r\nP")
10895   (save-excursion
10896     (let (article)
10897       (goto-char point)
10898       (beginning-of-line)
10899       (while (and
10900               (< (point) mark)
10901               (progn
10902                 (when (or all
10903                           (memq (setq article (gnus-summary-article-number))
10904                                 gnus-newsgroup-unreads))
10905                   (gnus-summary-mark-article article gnus-del-mark))
10906                 t)
10907               (gnus-summary-find-next))))))
10908
10909 (defun gnus-summary-mark-below (score mark)
10910   "Mark articles with score less than SCORE with MARK."
10911   (interactive "P\ncMark: ")
10912   (setq score (if score
10913                   (prefix-numeric-value score)
10914                 (or gnus-summary-default-score 0)))
10915   (with-current-buffer gnus-summary-buffer
10916     (goto-char (point-min))
10917     (while
10918         (progn
10919           (and (< (gnus-summary-article-score) score)
10920                (gnus-summary-mark-article nil mark))
10921           (gnus-summary-find-next)))))
10922
10923 (defun gnus-summary-kill-below (&optional score)
10924   "Mark articles with score below SCORE as read."
10925   (interactive "P")
10926   (gnus-summary-mark-below score gnus-killed-mark))
10927
10928 (defun gnus-summary-clear-above (&optional score)
10929   "Clear all marks from articles with score above SCORE."
10930   (interactive "P")
10931   (gnus-summary-mark-above score gnus-unread-mark))
10932
10933 (defun gnus-summary-tick-above (&optional score)
10934   "Tick all articles with score above SCORE."
10935   (interactive "P")
10936   (gnus-summary-mark-above score gnus-ticked-mark))
10937
10938 (defun gnus-summary-mark-above (score mark)
10939   "Mark articles with score over SCORE with MARK."
10940   (interactive "P\ncMark: ")
10941   (setq score (if score
10942                   (prefix-numeric-value score)
10943                 (or gnus-summary-default-score 0)))
10944   (with-current-buffer gnus-summary-buffer
10945     (goto-char (point-min))
10946     (while (and (progn
10947                   (when (> (gnus-summary-article-score) score)
10948                     (gnus-summary-mark-article nil mark))
10949                   t)
10950                 (gnus-summary-find-next)))))
10951
10952 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10953 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10954 (defun gnus-summary-limit-include-expunged (&optional no-error)
10955   "Display all the hidden articles that were expunged for low scores."
10956   (interactive)
10957   (let ((inhibit-read-only t))
10958     (let ((scored gnus-newsgroup-scored)
10959           headers h)
10960       (while scored
10961         (unless (gnus-summary-article-header (caar scored))
10962           (and (setq h (gnus-number-to-header (caar scored)))
10963                (< (cdar scored) gnus-summary-expunge-below)
10964                (push h headers)))
10965         (setq scored (cdr scored)))
10966       (if (not headers)
10967           (when (not no-error)
10968             (error "No expunged articles hidden"))
10969         (goto-char (point-min))
10970         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10971         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10972         (dolist (x headers)
10973           (push (mail-header-number x) gnus-newsgroup-limit))
10974         (gnus-summary-prepare-unthreaded (nreverse headers))
10975         (goto-char (point-min))
10976         (gnus-summary-position-point)
10977         t))))
10978
10979 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10980   "Mark all unread articles in this newsgroup as read.
10981 If prefix argument ALL is non-nil, ticked and dormant articles will
10982 also be marked as read.
10983 If QUIETLY is non-nil, no questions will be asked.
10984
10985 If TO-HERE is non-nil, it should be a point in the buffer.  All
10986 articles before (after, if REVERSE is set) this point will be marked
10987 as read.
10988
10989 Note that this function will only catch up the unread article
10990 in the current summary buffer limitation.
10991
10992 The number of articles marked as read is returned."
10993   (interactive "P")
10994   (prog1
10995       (save-excursion
10996         (when (or quietly
10997                   (not gnus-interactive-catchup) ;Without confirmation?
10998                   gnus-expert-user
10999                   (gnus-y-or-n-p
11000                    (if all
11001                        "Mark absolutely all articles as read? "
11002                      "Mark all unread articles as read? ")))
11003           (if (and not-mark
11004                    (not gnus-newsgroup-adaptive)
11005                    (not gnus-newsgroup-auto-expire)
11006                    (not gnus-suppress-duplicates)
11007                    (or (not gnus-use-cache)
11008                        (eq gnus-use-cache 'passive)))
11009               (progn
11010                 (when all
11011                   (setq gnus-newsgroup-marked nil
11012                         gnus-newsgroup-spam-marked nil
11013                         gnus-newsgroup-dormant nil))
11014                 (setq gnus-newsgroup-unreads
11015                       (gnus-sorted-nunion
11016                        (gnus-sorted-intersection gnus-newsgroup-unreads
11017                                                  gnus-newsgroup-downloadable)
11018                        (gnus-sorted-difference gnus-newsgroup-unfetched
11019                                                gnus-newsgroup-cached))))
11020             ;; We actually mark all articles as canceled, which we
11021             ;; have to do when using auto-expiry or adaptive scoring.
11022             (gnus-summary-show-all-threads)
11023             (if (and to-here reverse)
11024                 (progn
11025                   (goto-char to-here)
11026                   (gnus-summary-mark-current-read-and-unread-as-read
11027                    gnus-catchup-mark)
11028                   (while (gnus-summary-find-next (not all))
11029                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11030               (when (gnus-summary-first-subject (not all))
11031                 (while (and
11032                         (if to-here (< (point) to-here) t)
11033                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11034                         (gnus-summary-find-next (not all))))))
11035             (gnus-set-mode-line 'summary))
11036           t))
11037     (gnus-summary-position-point)))
11038
11039 (defun gnus-summary-catchup-to-here (&optional all)
11040   "Mark all unticked articles before the current one as read.
11041 If ALL is non-nil, also mark ticked and dormant articles as read."
11042   (interactive "P")
11043   (save-excursion
11044     (gnus-save-hidden-threads
11045       (let ((beg (point)))
11046         ;; We check that there are unread articles.
11047         (when (or all (gnus-summary-find-prev))
11048           (gnus-summary-catchup all t beg)))))
11049   (gnus-summary-position-point))
11050
11051 (defun gnus-summary-catchup-from-here (&optional all)
11052   "Mark all unticked articles after (and including) the current one as read.
11053 If ALL is non-nil, also mark ticked and dormant articles as read."
11054   (interactive "P")
11055   (save-excursion
11056     (gnus-save-hidden-threads
11057       (let ((beg (point)))
11058         ;; We check that there are unread articles.
11059         (when (or all (gnus-summary-find-next))
11060           (gnus-summary-catchup all t beg nil t)))))
11061   (gnus-summary-position-point))
11062
11063 (defun gnus-summary-catchup-all (&optional quietly)
11064   "Mark all articles in this newsgroup as read.
11065 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11066 instead, which marks only unread articles as read."
11067   (interactive "P")
11068   (gnus-summary-catchup t quietly))
11069
11070 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11071   "Mark all unread articles in this group as read, then exit.
11072 If prefix argument ALL is non-nil, all articles are marked as read.
11073 If QUIETLY is non-nil, no questions will be asked."
11074   (interactive "P")
11075   (when (gnus-summary-catchup all quietly nil 'fast)
11076     ;; Select next newsgroup or exit.
11077     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11078              (eq gnus-auto-select-next 'quietly))
11079         (gnus-summary-next-group nil)
11080       (gnus-summary-exit))))
11081
11082 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11083   "Mark all articles in this newsgroup as read, and then exit.
11084 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11085 instead, which marks only unread articles as read."
11086   (interactive "P")
11087   (gnus-summary-catchup-and-exit t quietly))
11088
11089 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11090   "Mark all articles in this group as read and select the next group.
11091 If given a prefix, mark all articles, unread as well as ticked, as
11092 read."
11093   (interactive "P")
11094   (save-excursion
11095     (gnus-summary-catchup all))
11096   (gnus-summary-next-group))
11097
11098 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11099   "Mark all articles in this group as read and select the previous group.
11100 If given a prefix, mark all articles, unread as well as ticked, as
11101 read."
11102   (interactive "P")
11103   (save-excursion
11104     (gnus-summary-catchup all))
11105   (gnus-summary-next-group nil nil t))
11106
11107 ;;;
11108 ;;; with article
11109 ;;;
11110
11111 (defmacro gnus-with-article (article &rest forms)
11112   "Select ARTICLE and perform FORMS in the original article buffer.
11113 Then replace the article with the result."
11114   `(progn
11115      ;; We don't want the article to be marked as read.
11116      (let (gnus-mark-article-hook)
11117        (gnus-summary-select-article t t nil ,article))
11118      (set-buffer gnus-original-article-buffer)
11119      ,@forms
11120      (if (not (gnus-check-backend-function
11121                'request-replace-article (car gnus-article-current)))
11122          (gnus-message 5 "Read-only group; not replacing")
11123        (unless (gnus-request-replace-article
11124                 ,article (car gnus-article-current)
11125                 (current-buffer) t)
11126          (error "Couldn't replace article")))
11127      ;; The cache and backlog have to be flushed somewhat.
11128      (when gnus-keep-backlog
11129        (gnus-backlog-remove-article
11130         (car gnus-article-current) (cdr gnus-article-current)))
11131      (when gnus-use-cache
11132        (gnus-cache-update-article
11133         (car gnus-article-current) (cdr gnus-article-current)))))
11134
11135 (put 'gnus-with-article 'lisp-indent-function 1)
11136 (put 'gnus-with-article 'edebug-form-spec '(form body))
11137
11138 ;; Thread-based commands.
11139
11140 (defun gnus-summary-articles-in-thread (&optional article)
11141   "Return a list of all articles in the current thread.
11142 If ARTICLE is non-nil, return all articles in the thread that starts
11143 with that article."
11144   (let* ((article (or article (gnus-summary-article-number)))
11145          (data (gnus-data-find-list article))
11146          (top-level (gnus-data-level (car data)))
11147          (top-subject
11148           (cond ((null gnus-thread-operation-ignore-subject)
11149                  (gnus-simplify-subject-re
11150                   (mail-header-subject (gnus-data-header (car data)))))
11151                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11152                  (gnus-simplify-subject-fuzzy
11153                   (mail-header-subject (gnus-data-header (car data)))))
11154                 (t nil)))
11155          (end-point (save-excursion
11156                       (if (gnus-summary-go-to-next-thread)
11157                           (point) (point-max))))
11158          articles)
11159     (while (and data
11160                 (< (gnus-data-pos (car data)) end-point))
11161       (when (or (not top-subject)
11162                 (string= top-subject
11163                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11164                              (gnus-simplify-subject-fuzzy
11165                               (mail-header-subject
11166                                (gnus-data-header (car data))))
11167                            (gnus-simplify-subject-re
11168                             (mail-header-subject
11169                              (gnus-data-header (car data)))))))
11170         (push (gnus-data-number (car data)) articles))
11171       (unless (and (setq data (cdr data))
11172                    (> (gnus-data-level (car data)) top-level))
11173         (setq data nil)))
11174     ;; Return the list of articles.
11175     (nreverse articles)))
11176
11177 (defun gnus-summary-rethread-current ()
11178   "Rethread the thread the current article is part of."
11179   (interactive)
11180   (let* ((gnus-show-threads t)
11181          (article (gnus-summary-article-number))
11182          (id (mail-header-id (gnus-summary-article-header)))
11183          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11184     (unless id
11185       (error "No article on the current line"))
11186     (gnus-rebuild-thread id)
11187     (gnus-summary-goto-subject article)))
11188
11189 (defun gnus-summary-reparent-thread ()
11190   "Make the current article child of the marked (or previous) article.
11191
11192 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11193 is non-nil or the Subject: of both articles are the same."
11194   (interactive)
11195   (unless (not (gnus-group-read-only-p))
11196     (error "The current newsgroup does not support article editing"))
11197   (unless (<= (length gnus-newsgroup-processable) 1)
11198     (error "No more than one article may be marked"))
11199   (let ((child (gnus-summary-article-number))
11200         ;; First grab the marked article, otherwise one line up.
11201         (parent (if (not (null gnus-newsgroup-processable))
11202                     (car gnus-newsgroup-processable)
11203                   (save-excursion
11204                     (if (eq (forward-line -1) 0)
11205                         (gnus-summary-article-number)
11206                       (error "Beginning of summary buffer"))))))
11207     (gnus-summary-reparent-children parent (list child))))
11208
11209 (defun gnus-summary-reparent-children (parent children)
11210   "Make PARENT the parent of CHILDREN.
11211 When called interactively, PARENT is the current article and CHILDREN
11212 are the process-marked articles."
11213   (interactive
11214    (list (gnus-summary-article-number)
11215          (gnus-summary-work-articles nil)))
11216   (dolist (child children)
11217     (save-window-excursion
11218       (let ((gnus-article-buffer " *reparent*"))
11219         (unless (not (eq parent child))
11220           (error "An article may not be self-referential"))
11221         (let ((message-id (mail-header-id
11222                            (gnus-summary-article-header parent))))
11223           (unless (and message-id (not (equal message-id "")))
11224             (error "No message-id in desired parent"))
11225           (gnus-with-article child
11226             (save-restriction
11227               (goto-char (point-min))
11228               (message-narrow-to-head)
11229               (if (re-search-forward "^References: " nil t)
11230                   (progn
11231                     (re-search-forward "^[^ \t]" nil t)
11232                     (forward-line -1)
11233                     (end-of-line)
11234                     (insert " " message-id))
11235                 (insert "References: " message-id "\n"))))
11236           (set-buffer gnus-summary-buffer)
11237           (gnus-summary-unmark-all-processable)
11238           (gnus-summary-update-article child)
11239           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11240             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11241           (gnus-summary-rethread-current)
11242           (gnus-message 3 "Article %d is now the child of article %d"
11243                         child parent))))))
11244
11245 (defun gnus-summary-toggle-threads (&optional arg)
11246   "Toggle showing conversation threads.
11247 If ARG is positive number, turn showing conversation threads on."
11248   (interactive "P")
11249   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11250     (setq gnus-show-threads
11251           (if (null arg) (not gnus-show-threads)
11252             (> (prefix-numeric-value arg) 0)))
11253     (gnus-summary-prepare)
11254     (gnus-summary-goto-subject current)
11255     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11256     (gnus-summary-position-point)))
11257
11258 (defun gnus-summary-show-all-threads ()
11259   "Show all threads."
11260   (interactive)
11261   (save-excursion
11262     (let ((buffer-read-only nil))
11263       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11264   (gnus-summary-position-point))
11265
11266 (defun gnus-summary-show-thread ()
11267   "Show thread subtrees.
11268 Returns nil if no thread was there to be shown."
11269   (interactive)
11270   (let ((buffer-read-only nil)
11271         (orig (point))
11272         (end (point-at-eol))
11273         ;; Leave point at bol
11274         (beg (progn (beginning-of-line) (point))))
11275     (prog1
11276         ;; Any hidden lines here?
11277         (search-forward "\r" end t)
11278       (subst-char-in-region beg end ?\^M ?\n t)
11279       (goto-char orig)
11280       (gnus-summary-position-point))))
11281
11282 (defun gnus-summary-maybe-hide-threads ()
11283   "If requested, hide the threads that should be hidden."
11284   (when (and gnus-show-threads
11285              gnus-thread-hide-subtree)
11286     (gnus-summary-hide-all-threads
11287      (if (or (consp gnus-thread-hide-subtree)
11288              (functionp gnus-thread-hide-subtree))
11289          (gnus-make-predicate gnus-thread-hide-subtree)
11290        nil))))
11291
11292 ;;; Hiding predicates.
11293
11294 (defun gnus-article-unread-p (header)
11295   (memq (mail-header-number header) gnus-newsgroup-unreads))
11296
11297 (defun gnus-article-unseen-p (header)
11298   (memq (mail-header-number header) gnus-newsgroup-unseen))
11299
11300 (defun gnus-map-articles (predicate articles)
11301   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11302   (apply 'gnus-or (mapcar predicate
11303                           (mapcar (lambda (number)
11304                                     (gnus-summary-article-header number))
11305                                   articles))))
11306
11307 (defun gnus-summary-hide-all-threads (&optional predicate)
11308   "Hide all thread subtrees.
11309 If PREDICATE is supplied, threads that satisfy this predicate
11310 will not be hidden."
11311   (interactive)
11312   (save-excursion
11313     (goto-char (point-min))
11314     (let ((end nil))
11315       (while (not end)
11316         (when (or (not predicate)
11317                   (gnus-map-articles
11318                    predicate (gnus-summary-article-children)))
11319             (gnus-summary-hide-thread))
11320         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11321   (gnus-summary-position-point))
11322
11323 (defun gnus-summary-hide-thread ()
11324   "Hide thread subtrees.
11325 If PREDICATE is supplied, threads that satisfy this predicate
11326 will not be hidden.
11327 Returns nil if no threads were there to be hidden."
11328   (interactive)
11329   (let ((buffer-read-only nil)
11330         (start (point))
11331         (article (gnus-summary-article-number)))
11332     (goto-char start)
11333     ;; Go forward until either the buffer ends or the subthread ends.
11334     (when (and (not (eobp))
11335                (or (zerop (gnus-summary-next-thread 1 t))
11336                    (goto-char (point-max))))
11337       (prog1
11338           (if (and (> (point) start)
11339                    (search-backward "\n" start t))
11340               (progn
11341                 (subst-char-in-region start (point) ?\n ?\^M)
11342                 (gnus-summary-goto-subject article))
11343             (goto-char start)
11344             nil)))))
11345
11346 (defun gnus-summary-go-to-next-thread (&optional previous)
11347   "Go to the same level (or less) next thread.
11348 If PREVIOUS is non-nil, go to previous thread instead.
11349 Return the article number moved to, or nil if moving was impossible."
11350   (let ((level (gnus-summary-thread-level))
11351         (way (if previous -1 1))
11352         (beg (point)))
11353     (forward-line way)
11354     (while (and (not (eobp))
11355                 (< level (gnus-summary-thread-level)))
11356       (forward-line way))
11357     (if (eobp)
11358         (progn
11359           (goto-char beg)
11360           nil)
11361       (setq beg (point))
11362       (prog1
11363           (gnus-summary-article-number)
11364         (goto-char beg)))))
11365
11366 (defun gnus-summary-next-thread (n &optional silent)
11367   "Go to the same level next N'th thread.
11368 If N is negative, search backward instead.
11369 Returns the difference between N and the number of skips actually
11370 done.
11371
11372 If SILENT, don't output messages."
11373   (interactive "p")
11374   (let ((backward (< n 0))
11375         (n (abs n)))
11376     (while (and (> n 0)
11377                 (gnus-summary-go-to-next-thread backward))
11378       (decf n))
11379     (unless silent
11380       (gnus-summary-position-point))
11381     (when (and (not silent) (/= 0 n))
11382       (gnus-message 7 "No more threads"))
11383     n))
11384
11385 (defun gnus-summary-prev-thread (n)
11386   "Go to the same level previous N'th thread.
11387 Returns the difference between N and the number of skips actually
11388 done."
11389   (interactive "p")
11390   (gnus-summary-next-thread (- n)))
11391
11392 (defun gnus-summary-go-down-thread ()
11393   "Go down one level in the current thread."
11394   (let ((children (gnus-summary-article-children)))
11395     (when children
11396       (gnus-summary-goto-subject (car children)))))
11397
11398 (defun gnus-summary-go-up-thread ()
11399   "Go up one level in the current thread."
11400   (let ((parent (gnus-summary-article-parent)))
11401     (when parent
11402       (gnus-summary-goto-subject parent))))
11403
11404 (defun gnus-summary-down-thread (n)
11405   "Go down thread N steps.
11406 If N is negative, go up instead.
11407 Returns the difference between N and how many steps down that were
11408 taken."
11409   (interactive "p")
11410   (let ((up (< n 0))
11411         (n (abs n)))
11412     (while (and (> n 0)
11413                 (if up (gnus-summary-go-up-thread)
11414                   (gnus-summary-go-down-thread)))
11415       (setq n (1- n)))
11416     (gnus-summary-position-point)
11417     (when (/= 0 n)
11418       (gnus-message 7 "Can't go further"))
11419     n))
11420
11421 (defun gnus-summary-up-thread (n)
11422   "Go up thread N steps.
11423 If N is negative, go down instead.
11424 Returns the difference between N and how many steps down that were
11425 taken."
11426   (interactive "p")
11427   (gnus-summary-down-thread (- n)))
11428
11429 (defun gnus-summary-top-thread ()
11430   "Go to the top of the thread."
11431   (interactive)
11432   (while (gnus-summary-go-up-thread))
11433   (gnus-summary-article-number))
11434
11435 (defun gnus-summary-expire-thread ()
11436   "Mark articles under current thread as expired."
11437   (interactive)
11438   (gnus-summary-kill-thread 0))
11439
11440 (defun gnus-summary-kill-thread (&optional unmark)
11441   "Mark articles under current thread as read.
11442 If the prefix argument is positive, remove any kinds of marks.
11443 If the prefix argument is zero, mark thread as expired.
11444 If the prefix argument is negative, tick articles instead."
11445   (interactive "P")
11446   (when unmark
11447     (setq unmark (prefix-numeric-value unmark)))
11448   (let ((articles (gnus-summary-articles-in-thread))
11449         (hide (or (null unmark) (= unmark 0))))
11450     (save-excursion
11451       ;; Expand the thread.
11452       (gnus-summary-show-thread)
11453       ;; Mark all the articles.
11454       (while articles
11455         (gnus-summary-goto-subject (car articles))
11456         (cond ((null unmark)
11457                (gnus-summary-mark-article-as-read gnus-killed-mark))
11458               ((> unmark 0)
11459                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11460               ((= unmark 0)
11461                (gnus-summary-mark-article-as-unread gnus-expirable-mark))
11462               (t
11463                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11464         (setq articles (cdr articles))))
11465     ;; Hide killed subtrees when hide is true.
11466     (and hide
11467          gnus-thread-hide-killed
11468          (gnus-summary-hide-thread))
11469     ;; If hide is t, go to next unread subject.
11470     (when hide
11471       ;; Go to next unread subject.
11472       (gnus-summary-next-subject 1 t)))
11473   (gnus-set-mode-line 'summary))
11474
11475 ;; Summary sorting commands
11476
11477 (defun gnus-summary-sort-by-number (&optional reverse)
11478   "Sort the summary buffer by article number.
11479 Argument REVERSE means reverse order."
11480   (interactive "P")
11481   (gnus-summary-sort 'number reverse))
11482
11483 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11484   "Sort the summary buffer by most recent article number.
11485 Argument REVERSE means reverse order."
11486   (interactive "P")
11487   (gnus-summary-sort 'most-recent-number reverse))
11488
11489 (defun gnus-summary-sort-by-random (&optional reverse)
11490   "Randomize the order in the summary buffer.
11491 Argument REVERSE means to randomize in reverse order."
11492   (interactive "P")
11493   (gnus-summary-sort 'random reverse))
11494
11495 (defun gnus-summary-sort-by-author (&optional reverse)
11496   "Sort the summary buffer by author name alphabetically.
11497 If `case-fold-search' is non-nil, case of letters is ignored.
11498 Argument REVERSE means reverse order."
11499   (interactive "P")
11500   (gnus-summary-sort 'author reverse))
11501
11502 (defun gnus-summary-sort-by-recipient (&optional reverse)
11503   "Sort the summary buffer by recipient name alphabetically.
11504 If `case-fold-search' is non-nil, case of letters is ignored.
11505 Argument REVERSE means reverse order."
11506   (interactive "P")
11507   (gnus-summary-sort 'recipient reverse))
11508
11509 (defun gnus-summary-sort-by-subject (&optional reverse)
11510   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11511 If `case-fold-search' is non-nil, case of letters is ignored.
11512 Argument REVERSE means reverse order."
11513   (interactive "P")
11514   (gnus-summary-sort 'subject reverse))
11515
11516 (defun gnus-summary-sort-by-date (&optional reverse)
11517   "Sort the summary buffer by date.
11518 Argument REVERSE means reverse order."
11519   (interactive "P")
11520   (gnus-summary-sort 'date reverse))
11521
11522 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11523   "Sort the summary buffer by most recent date.
11524 Argument REVERSE means reverse order."
11525   (interactive "P")
11526   (gnus-summary-sort 'most-recent-date reverse))
11527
11528 (defun gnus-summary-sort-by-score (&optional reverse)
11529   "Sort the summary buffer by score.
11530 Argument REVERSE means reverse order."
11531   (interactive "P")
11532   (gnus-summary-sort 'score reverse))
11533
11534 (defun gnus-summary-sort-by-lines (&optional reverse)
11535   "Sort the summary buffer by the number of lines.
11536 Argument REVERSE means reverse order."
11537   (interactive "P")
11538   (gnus-summary-sort 'lines reverse))
11539
11540 (defun gnus-summary-sort-by-chars (&optional reverse)
11541   "Sort the summary buffer by article length.
11542 Argument REVERSE means reverse order."
11543   (interactive "P")
11544   (gnus-summary-sort 'chars reverse))
11545
11546 (defun gnus-summary-sort-by-original (&optional reverse)
11547   "Sort the summary buffer using the default sorting method.
11548 Argument REVERSE means reverse order."
11549   (interactive "P")
11550   (let* ((inhibit-read-only t)
11551          (gnus-summary-prepare-hook nil))
11552     ;; We do the sorting by regenerating the threads.
11553     (gnus-summary-prepare)
11554     ;; Hide subthreads if needed.
11555     (gnus-summary-maybe-hide-threads)))
11556
11557 (defun gnus-summary-sort (predicate reverse)
11558   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11559   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11560          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11561          (gnus-thread-sort-functions
11562           (if (not reverse)
11563               thread
11564             `(lambda (t1 t2)
11565                (,thread t2 t1))))
11566          (gnus-sort-gathered-threads-function
11567           gnus-thread-sort-functions)
11568          (gnus-article-sort-functions
11569           (if (not reverse)
11570               article
11571             `(lambda (t1 t2)
11572                (,article t2 t1))))
11573          (inhibit-read-only t)
11574          (gnus-summary-prepare-hook nil))
11575     ;; We do the sorting by regenerating the threads.
11576     (gnus-summary-prepare)
11577     ;; Hide subthreads if needed.
11578     (gnus-summary-maybe-hide-threads)))
11579
11580 ;; Summary saving commands.
11581
11582 (defun gnus-summary-save-article (&optional n not-saved)
11583   "Save the current article using the default saver function.
11584 If N is a positive number, save the N next articles.
11585 If N is a negative number, save the N previous articles.
11586 If N is nil and any articles have been marked with the process mark,
11587 save those articles instead.
11588 The variable `gnus-default-article-saver' specifies the saver function.
11589
11590 If the optional second argument NOT-SAVED is non-nil, articles saved
11591 will not be marked as saved."
11592   (interactive "P")
11593   (require 'gnus-art)
11594   (let* ((articles (gnus-summary-work-articles n))
11595          (save-buffer (save-excursion
11596                         (nnheader-set-temp-buffer " *Gnus Save*")))
11597          (num (length articles))
11598          ;; Whether to save decoded articles or raw articles.
11599          (decode (when gnus-article-save-coding-system
11600                    (get gnus-default-article-saver :decode)))
11601          ;; When saving many articles in a single file, use the other
11602          ;; function to save articles other than the first one.
11603          (saver2 (get gnus-default-article-saver :function))
11604          (gnus-prompt-before-saving (if saver2
11605                                         t
11606                                       gnus-prompt-before-saving))
11607          (gnus-default-article-saver gnus-default-article-saver)
11608          header file)
11609     (dolist (article articles)
11610       (setq header (gnus-summary-article-header article))
11611       (if (not (vectorp header))
11612           ;; This is a pseudo-article.
11613           (if (assq 'name header)
11614               (gnus-copy-file (cdr (assq 'name header)))
11615             (gnus-message 1 "Article %d is unsaveable" article))
11616         ;; This is a real article.
11617         (save-window-excursion
11618           (let ((gnus-display-mime-function (when decode
11619                                               gnus-display-mime-function))
11620                 (gnus-article-prepare-hook (when decode
11621                                              gnus-article-prepare-hook)))
11622             (gnus-summary-select-article t t nil article)
11623             (gnus-summary-goto-subject article)))
11624         (with-current-buffer save-buffer
11625           (erase-buffer)
11626           (insert-buffer-substring (if decode
11627                                        gnus-article-buffer
11628                                      gnus-original-article-buffer)))
11629         (setq file (gnus-article-save save-buffer file num))
11630         (gnus-summary-remove-process-mark article)
11631         (unless not-saved
11632           (gnus-summary-set-saved-mark article)))
11633       (when saver2
11634         (setq gnus-default-article-saver saver2
11635               saver2 nil)))
11636     (gnus-kill-buffer save-buffer)
11637     (gnus-summary-position-point)
11638     (gnus-set-mode-line 'summary)
11639     n))
11640
11641 (defun gnus-summary-pipe-output (&optional n sym)
11642   "Pipe the current article to a subprocess.
11643 If N is a positive number, pipe the N next articles.
11644 If N is a negative number, pipe the N previous articles.
11645 If N is nil and any articles have been marked with the process mark,
11646 pipe those articles instead.
11647 The default command to which articles are piped is specified by the
11648 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11649 will be prompted for the command.
11650
11651 The properties `:decode' and `:headers' that are put to the function
11652 symbol `gnus-summary-save-in-pipe' control whether this function
11653 decodes articles and what headers to keep (see the doc string for the
11654 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11655 is neither omitted nor the symbol `r', force including all headers
11656 regardless of the `:headers' property.  If it is the symbol `r',
11657 articles that are not decoded and include all headers will be piped
11658 no matter what the properties `:decode' and `:headers' are."
11659   (interactive (gnus-interactive "P\ny"))
11660   (require 'gnus-art)
11661   (let* ((articles (gnus-summary-work-articles n))
11662          (result-buffer "*Shell Command Output*")
11663          (all-headers (not (memq sym '(nil r))))
11664          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11665          (raw (eq sym 'r))
11666          (headers (get 'gnus-summary-save-in-pipe :headers))
11667          command result)
11668     (unless (numberp (car articles))
11669       (error "No article to pipe"))
11670     (setq command (gnus-read-shell-command
11671                    (concat "Shell command on "
11672                            (if (cdr articles)
11673                                (format "these %d articles" (length articles))
11674                              "this article")
11675                            ": ")
11676                    gnus-summary-pipe-output-default-command))
11677     (when (string-equal command "")
11678       (error "A command is required"))
11679     (when all-headers
11680       (put 'gnus-summary-save-in-pipe :headers nil))
11681     (unwind-protect
11682         (while articles
11683           (gnus-summary-goto-subject (pop articles))
11684           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11685           (when (and (get-buffer result-buffer)
11686                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11687             (setq result (concat result (with-current-buffer result-buffer
11688                                           (buffer-string))))))
11689       (put 'gnus-summary-save-in-pipe :headers headers))
11690     (unless (zerop (length result))
11691       (if (with-current-buffer (get-buffer-create result-buffer)
11692             (erase-buffer)
11693             (insert result)
11694             (prog1
11695                 (and (= (count-lines (point-min) (point)) 1)
11696                      (progn
11697                        (end-of-line 0)
11698                        (<= (current-column)
11699                            (window-width (minibuffer-window)))))
11700               (goto-char (point-min))))
11701           (message "%s" (substring result 0 -1))
11702         (message nil)
11703         (gnus-configure-windows 'pipe)))))
11704
11705 (defun gnus-summary-save-article-mail (&optional arg)
11706   "Append the current article to a Unix mail box file.
11707 If N is a positive number, save the N next articles.
11708 If N is a negative number, save the N previous articles.
11709 If N is nil and any articles have been marked with the process mark,
11710 save those articles instead."
11711   (interactive "P")
11712   (require 'gnus-art)
11713   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11714     (gnus-summary-save-article arg)))
11715
11716 (defun gnus-summary-save-article-rmail (&optional arg)
11717   "Append the current article to an rmail file.
11718 If N is a positive number, save the N next articles.
11719 If N is a negative number, save the N previous articles.
11720 If N is nil and any articles have been marked with the process mark,
11721 save those articles instead."
11722   (interactive "P")
11723   (require 'gnus-art)
11724   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11725     (gnus-summary-save-article arg)))
11726
11727 (defun gnus-summary-save-article-file (&optional arg)
11728   "Append the current article to a file.
11729 If N is a positive number, save the N next articles.
11730 If N is a negative number, save the N previous articles.
11731 If N is nil and any articles have been marked with the process mark,
11732 save those articles instead."
11733   (interactive "P")
11734   (require 'gnus-art)
11735   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11736     (gnus-summary-save-article arg)))
11737
11738 (defun gnus-summary-write-article-file (&optional arg)
11739   "Write the current article to a file, deleting the previous file.
11740 If N is a positive number, save the N next articles.
11741 If N is a negative number, save the N previous articles.
11742 If N is nil and any articles have been marked with the process mark,
11743 save those articles instead."
11744   (interactive "P")
11745   (require 'gnus-art)
11746   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11747     (gnus-summary-save-article arg)))
11748
11749 (defun gnus-summary-save-article-body-file (&optional arg)
11750   "Append the current article body to a file.
11751 If N is a positive number, save the N next articles.
11752 If N is a negative number, save the N previous articles.
11753 If N is nil and any articles have been marked with the process mark,
11754 save those articles instead."
11755   (interactive "P")
11756   (require 'gnus-art)
11757   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11758     (gnus-summary-save-article arg)))
11759
11760 (defun gnus-summary-write-article-body-file (&optional arg)
11761   "Write the current article body to a file, deleting the previous file.
11762 If N is a positive number, save the N next articles.
11763 If N is a negative number, save the N previous articles.
11764 If N is nil and any articles have been marked with the process mark,
11765 save those articles instead."
11766   (interactive "P")
11767   (require 'gnus-art)
11768   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11769     (gnus-summary-save-article arg)))
11770
11771 (defun gnus-summary-muttprint (&optional arg)
11772   "Print the current article using Muttprint.
11773 If N is a positive number, save the N next articles.
11774 If N is a negative number, save the N previous articles.
11775 If N is nil and any articles have been marked with the process mark,
11776 save those articles instead."
11777   (interactive "P")
11778   (require 'gnus-art)
11779   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11780     (gnus-summary-save-article arg t)))
11781
11782 (defun gnus-summary-pipe-message (program)
11783   "Pipe the current article through PROGRAM."
11784   (interactive "sProgram: ")
11785   (gnus-summary-select-article)
11786   (let ((mail-header-separator ""))
11787     (gnus-eval-in-buffer-window gnus-article-buffer
11788       (save-restriction
11789         (widen)
11790         (let ((start (window-start))
11791               (inhibit-read-only t))
11792           (message-pipe-buffer-body program)
11793           (set-window-start (get-buffer-window (current-buffer)) start))))))
11794
11795 (defun gnus-get-split-value (methods)
11796   "Return a value based on the split METHODS."
11797   (let (split-name method result match)
11798     (when methods
11799       (with-current-buffer gnus-original-article-buffer
11800         (save-restriction
11801           (nnheader-narrow-to-headers)
11802           (while (and methods (not split-name))
11803             (goto-char (point-min))
11804             (setq method (pop methods))
11805             (setq match (car method))
11806             (when (cond
11807                    ((stringp match)
11808                     ;; Regular expression.
11809                     (ignore-errors
11810                       (re-search-forward match nil t)))
11811                    ((functionp match)
11812                     ;; Function.
11813                     (save-restriction
11814                       (widen)
11815                       (setq result (funcall match gnus-newsgroup-name))))
11816                    ((consp match)
11817                     ;; Form.
11818                     (save-restriction
11819                       (widen)
11820                       (setq result (eval match)))))
11821               (setq split-name (cdr method))
11822               (cond ((stringp result)
11823                      (push (expand-file-name
11824                             result gnus-article-save-directory)
11825                            split-name))
11826                     ((consp result)
11827                      (setq split-name (append result split-name)))))))))
11828     (nreverse split-name)))
11829
11830 (defun gnus-valid-move-group-p (group)
11831   (and (boundp group)
11832        (symbol-name group)
11833        (symbol-value group)
11834        (gnus-get-function (gnus-find-method-for-group
11835                            (symbol-name group)) 'request-accept-article t)))
11836
11837 (defun gnus-read-move-group-name (prompt default articles prefix)
11838   "Read a group name."
11839   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11840          (minibuffer-confirm-incomplete nil) ; XEmacs
11841          (prom
11842           (format "%s %s to"
11843                   prompt
11844                   (if (> (length articles) 1)
11845                       (format "these %d articles" (length articles))
11846                     "this article")))
11847          (to-newsgroup
11848           (let (active group)
11849             (when (or (null split-name) (= 1 (length split-name)))
11850               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11851               (mapatoms (lambda (symbol)
11852                           (setq group (symbol-name symbol))
11853                           (when (string-match "[^\000-\177]" group)
11854                             (setq group (gnus-group-decoded-name group)))
11855                           (set (intern group active) group))
11856                         gnus-active-hashtb))
11857             (cond
11858              ((null split-name)
11859               (gnus-completing-read-with-default
11860                default prom active 'gnus-valid-move-group-p nil prefix
11861                'gnus-group-history))
11862              ((= 1 (length split-name))
11863               (gnus-completing-read-with-default
11864                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11865                'gnus-group-history))
11866              (t
11867               (gnus-completing-read-with-default
11868                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11869                'gnus-group-history)))))
11870          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11871          encoded)
11872     (when to-newsgroup
11873       (if (or (string= to-newsgroup "")
11874               (string= to-newsgroup prefix))
11875           (setq to-newsgroup default))
11876       (unless to-newsgroup
11877         (error "No group name entered"))
11878       (setq encoded (mm-encode-coding-string
11879                      to-newsgroup
11880                      (gnus-group-name-charset to-method to-newsgroup)))
11881       (or (gnus-active encoded)
11882           (gnus-activate-group encoded nil nil to-method)
11883           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11884                                      to-newsgroup))
11885               (or (and (gnus-request-create-group encoded to-method)
11886                        (gnus-activate-group encoded nil nil to-method)
11887                        (gnus-subscribe-group encoded))
11888                   (error "Couldn't create group %s" to-newsgroup)))
11889           (error "No such group: %s" to-newsgroup))
11890       encoded)))
11891
11892 (defvar gnus-summary-save-parts-counter)
11893 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11894
11895 (defun gnus-summary-save-parts (type dir n &optional reverse)
11896   "Save parts matching TYPE to DIR.
11897 If REVERSE, save parts that do not match TYPE."
11898   (interactive
11899    (list (read-string "Save parts of type: "
11900                       (or (car gnus-summary-save-parts-type-history)
11901                           gnus-summary-save-parts-default-mime)
11902                       'gnus-summary-save-parts-type-history)
11903          (setq gnus-summary-save-parts-last-directory
11904                (read-file-name "Save to directory: "
11905                                gnus-summary-save-parts-last-directory
11906                                nil t))
11907          current-prefix-arg))
11908   (gnus-summary-iterate n
11909     (let ((gnus-display-mime-function nil)
11910           gnus-article-prepare-hook
11911           gnus-article-decode-hook
11912           gnus-display-mime-function
11913           gnus-break-pages
11914           (gnus-inhibit-treatment t))
11915       (gnus-summary-select-article))
11916     (with-current-buffer gnus-article-buffer
11917       (let ((handles (or gnus-article-mime-handles
11918                          (mm-dissect-buffer nil gnus-article-loose-mime)
11919                          (and gnus-article-emulate-mime
11920                               (mm-uu-dissect))))
11921             (gnus-summary-save-parts-counter 1))
11922         (when handles
11923           (gnus-summary-save-parts-1 type dir handles reverse)
11924           (unless gnus-article-mime-handles ;; Don't destroy this case.
11925             (mm-destroy-parts handles)))))))
11926
11927 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11928   (if (stringp (car handle))
11929       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11930               (cdr handle))
11931     (when (if reverse
11932               (not (string-match type (mm-handle-media-type handle)))
11933             (string-match type (mm-handle-media-type handle)))
11934       (let ((file (expand-file-name
11935                    (gnus-map-function
11936                     mm-file-name-rewrite-functions
11937                     (file-name-nondirectory
11938                      (or
11939                       (mail-content-type-get
11940                        (mm-handle-disposition handle) 'filename)
11941                       (mail-content-type-get
11942                        (mm-handle-type handle) 'name)
11943                       (format "%s.%d.%d" gnus-newsgroup-name
11944                               (cdr gnus-article-current)
11945                               gnus-summary-save-parts-counter))))
11946                    dir)))
11947         (incf gnus-summary-save-parts-counter)
11948         (unless (file-exists-p file)
11949           (mm-save-part-to-file handle file))))))
11950
11951 ;; Summary extract commands
11952
11953 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11954   (let ((inhibit-read-only t)
11955         (article (gnus-summary-article-number))
11956         after-article b e)
11957     (unless (gnus-summary-goto-subject article)
11958       (error "No such article: %d" article))
11959     (gnus-summary-position-point)
11960     ;; If all commands are to be bunched up on one line, we collect
11961     ;; them here.
11962     (unless gnus-view-pseudos-separately
11963       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11964             files action)
11965         (while ps
11966           (setq action (cdr (assq 'action (car ps))))
11967           (setq files (list (cdr (assq 'name (car ps)))))
11968           (while (and ps (cdr ps)
11969                       (string= (or action "1")
11970                                (or (cdr (assq 'action (cadr ps))) "2")))
11971             (push (cdr (assq 'name (cadr ps))) files)
11972             (setcdr ps (cddr ps)))
11973           (when files
11974             (when (not (string-match "%s" action))
11975               (push " " files))
11976             (push " " files)
11977             (when (assq 'execute (car ps))
11978               (setcdr (assq 'execute (car ps))
11979                       (funcall (if (string-match "%s" action)
11980                                    'format 'concat)
11981                                action
11982                                (mapconcat
11983                                 (lambda (f)
11984                                   (if (equal f " ")
11985                                       f
11986                                     (shell-quote-argument f)))
11987                                 files " ")))))
11988           (setq ps (cdr ps)))))
11989     (if (and gnus-view-pseudos (not not-view))
11990         (while pslist
11991           (when (assq 'execute (car pslist))
11992             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11993                                   (eq gnus-view-pseudos 'not-confirm)))
11994           (setq pslist (cdr pslist)))
11995       (save-excursion
11996         (while pslist
11997           (setq after-article (or (cdr (assq 'article (car pslist)))
11998                                   (gnus-summary-article-number)))
11999           (gnus-summary-goto-subject after-article)
12000           (forward-line 1)
12001           (setq b (point))
12002           (insert "    " (file-name-nondirectory
12003                           (cdr (assq 'name (car pslist))))
12004                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12005           (setq e (point))
12006           (forward-line -1)             ; back to `b'
12007           (gnus-add-text-properties
12008            b (1- e) (list 'gnus-number gnus-reffed-article-number
12009                           gnus-mouse-face-prop gnus-mouse-face))
12010           (gnus-data-enter
12011            after-article gnus-reffed-article-number
12012            gnus-unread-mark b (car pslist) 0 (- e b))
12013           (setq gnus-newsgroup-unreads
12014                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12015                                          gnus-reffed-article-number))
12016           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12017           (setq pslist (cdr pslist)))))))
12018
12019 (defun gnus-pseudos< (p1 p2)
12020   (let ((c1 (cdr (assq 'action p1)))
12021         (c2 (cdr (assq 'action p2))))
12022     (and c1 c2 (string< c1 c2))))
12023
12024 (defun gnus-request-pseudo-article (props)
12025   (cond ((assq 'execute props)
12026          (gnus-execute-command (cdr (assq 'execute props)))))
12027   (let ((gnus-current-article (gnus-summary-article-number)))
12028     (gnus-run-hooks 'gnus-mark-article-hook)))
12029
12030 (defun gnus-execute-command (command &optional automatic)
12031   (save-excursion
12032     (gnus-article-setup-buffer)
12033     (set-buffer gnus-article-buffer)
12034     (setq buffer-read-only nil)
12035     (let ((command (if automatic command
12036                      (read-string "Command: " (cons command 0)))))
12037       (erase-buffer)
12038       (insert "$ " command "\n\n")
12039       (if gnus-view-pseudo-asynchronously
12040           (start-process "gnus-execute" (current-buffer) shell-file-name
12041                          shell-command-switch command)
12042         (call-process shell-file-name nil t nil
12043                       shell-command-switch command)))))
12044
12045 ;; Summary kill commands.
12046
12047 (defun gnus-summary-edit-global-kill (article)
12048   "Edit the \"global\" kill file."
12049   (interactive (list (gnus-summary-article-number)))
12050   (gnus-group-edit-global-kill article))
12051
12052 (defun gnus-summary-edit-local-kill ()
12053   "Edit a local kill file applied to the current newsgroup."
12054   (interactive)
12055   (setq gnus-current-headers (gnus-summary-article-header))
12056   (gnus-group-edit-local-kill
12057    (gnus-summary-article-number) gnus-newsgroup-name))
12058
12059 ;;; Header reading.
12060
12061 (defun gnus-read-header (id &optional header)
12062   "Read the headers of article ID and enter them into the Gnus system."
12063   (let ((group gnus-newsgroup-name)
12064         (gnus-override-method
12065          (or
12066           gnus-override-method
12067           (and (gnus-news-group-p gnus-newsgroup-name)
12068                (car (gnus-refer-article-methods)))))
12069         where)
12070     ;; First we check to see whether the header in question is already
12071     ;; fetched.
12072     (if (stringp id)
12073         ;; This is a Message-ID.
12074         (setq header (or header (gnus-id-to-header id)))
12075       ;; This is an article number.
12076       (setq header (or header (gnus-summary-article-header id))))
12077     (if (and header
12078              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12079         ;; We have found the header.
12080         header
12081       ;; We have to really fetch the header to this article.
12082       (with-current-buffer nntp-server-buffer
12083         (when (setq where (gnus-request-head id group))
12084           (nnheader-fold-continuation-lines)
12085           (goto-char (point-max))
12086           (insert ".\n")
12087           (goto-char (point-min))
12088           (insert "211 ")
12089           (princ (cond
12090                   ((numberp id) id)
12091                   ((cdr where) (cdr where))
12092                   (header (mail-header-number header))
12093                   (t gnus-reffed-article-number))
12094                  (current-buffer))
12095           (insert " Article retrieved.\n"))
12096         (if (or (not where)
12097                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12098             ()                          ; Malformed head.
12099           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12100             (when (and (stringp id)
12101                        (or
12102                         (not (string= (gnus-group-real-name group)
12103                                       (car where)))
12104                         (not (gnus-server-equal gnus-override-method
12105                                                 (gnus-group-method group)))))
12106               ;; If we fetched by Message-ID and the article came from
12107               ;; a different group (or server), we fudge some bogus
12108               ;; article numbers for this article.
12109               (mail-header-set-number header gnus-reffed-article-number))
12110             (with-current-buffer gnus-summary-buffer
12111               (decf gnus-reffed-article-number)
12112               (gnus-remove-header (mail-header-number header))
12113               (push header gnus-newsgroup-headers)
12114               (setq gnus-current-headers header)
12115               (push (mail-header-number header) gnus-newsgroup-limit)))
12116           header)))))
12117
12118 (defun gnus-remove-header (number)
12119   "Remove header NUMBER from `gnus-newsgroup-headers'."
12120   (if (and gnus-newsgroup-headers
12121            (= number (mail-header-number (car gnus-newsgroup-headers))))
12122       (pop gnus-newsgroup-headers)
12123     (let ((headers gnus-newsgroup-headers))
12124       (while (and (cdr headers)
12125                   (not (= number (mail-header-number (cadr headers)))))
12126         (pop headers))
12127       (when (cdr headers)
12128         (setcdr headers (cddr headers))))))
12129
12130 ;;;
12131 ;;; summary highlights
12132 ;;;
12133
12134 (defun gnus-highlight-selected-summary ()
12135   "Highlight selected article in summary buffer."
12136   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12137   (when gnus-summary-selected-face
12138     (save-excursion
12139       (let* ((beg (point-at-bol))
12140              (end (point-at-eol))
12141              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12142              (from (if (get-text-property beg gnus-mouse-face-prop)
12143                        beg
12144                      (or (next-single-property-change
12145                           beg gnus-mouse-face-prop nil end)
12146                          beg)))
12147              (to
12148               (if (= from end)
12149                   (- from 2)
12150                 (or (next-single-property-change
12151                      from gnus-mouse-face-prop nil end)
12152                     end))))
12153         ;; If no mouse-face prop on line we will have to = from = end,
12154         ;; so we highlight the entire line instead.
12155         (when (= (+ to 2) from)
12156           (setq from beg)
12157           (setq to end))
12158         (if gnus-newsgroup-selected-overlay
12159             ;; Move old overlay.
12160             (gnus-move-overlay
12161              gnus-newsgroup-selected-overlay from to (current-buffer))
12162           ;; Create new overlay.
12163           (gnus-overlay-put
12164            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12165            'face gnus-summary-selected-face))))))
12166
12167 (defvar gnus-summary-highlight-line-cached nil)
12168 (defvar gnus-summary-highlight-line-trigger nil)
12169
12170 (defun gnus-summary-highlight-line-0 ()
12171   (if (and (eq gnus-summary-highlight-line-trigger
12172                gnus-summary-highlight)
12173            gnus-summary-highlight-line-cached)
12174       gnus-summary-highlight-line-cached
12175     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12176           gnus-summary-highlight-line-cached
12177           (let* ((cond (list 'cond))
12178                  (c cond)
12179                  (list gnus-summary-highlight))
12180             (while list
12181               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12182                               nil))
12183               (setq c (cdr c)
12184                     list (cdr list)))
12185             (gnus-byte-compile (list 'lambda nil cond))))))
12186
12187 (defun gnus-summary-highlight-line ()
12188   "Highlight current line according to `gnus-summary-highlight'."
12189   (let* ((beg (point-at-bol))
12190          (article (or (gnus-summary-article-number) gnus-current-article))
12191          (score (or (cdr (assq article
12192                                gnus-newsgroup-scored))
12193                     gnus-summary-default-score 0))
12194          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12195          (inhibit-read-only t)
12196          (default gnus-summary-default-score)
12197          (default-high gnus-summary-default-high-score)
12198          (default-low gnus-summary-default-low-score)
12199          (uncached (and gnus-summary-use-undownloaded-faces
12200                         (memq article gnus-newsgroup-undownloaded)
12201                         (not (memq article gnus-newsgroup-cached)))))
12202     (let ((face (funcall (gnus-summary-highlight-line-0))))
12203       (unless (eq face (get-text-property beg 'face))
12204         (gnus-put-text-property-excluding-characters-with-faces
12205          beg (point-at-eol) 'face
12206          (setq face (if (boundp face) (symbol-value face) face)))
12207         (when gnus-summary-highlight-line-function
12208           (funcall gnus-summary-highlight-line-function article face))))))
12209
12210 (defun gnus-update-read-articles (group unread &optional compute)
12211   "Update the list of read articles in GROUP.
12212 UNREAD is a sorted list."
12213   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12214         (info (gnus-get-info group))
12215         (prev 1)
12216         read)
12217     (if (or (not info) (not active))
12218         ;; There is no info on this group if it was, in fact,
12219         ;; killed.  Gnus stores no information on killed groups, so
12220         ;; there's nothing to be done.
12221         ;; One could store the information somewhere temporarily,
12222         ;; perhaps...  Hmmm...
12223         ()
12224       ;; Remove any negative articles numbers.
12225       (while (and unread (< (car unread) 0))
12226         (setq unread (cdr unread)))
12227       ;; Remove any expired article numbers
12228       (while (and unread (< (car unread) (car active)))
12229         (setq unread (cdr unread)))
12230       ;; Compute the ranges of read articles by looking at the list of
12231       ;; unread articles.
12232       (while unread
12233         (when (/= (car unread) prev)
12234           (push (if (= prev (1- (car unread))) prev
12235                   (cons prev (1- (car unread))))
12236                 read))
12237         (setq prev (1+ (car unread)))
12238         (setq unread (cdr unread)))
12239       (when (<= prev (cdr active))
12240         (push (cons prev (cdr active)) read))
12241       (setq read (if (> (length read) 1) (nreverse read) read))
12242       (if compute
12243           read
12244         (save-excursion
12245           (let (setmarkundo)
12246             ;; Propagate the read marks to the backend.
12247             (when (and gnus-propagate-marks
12248                        (gnus-check-backend-function 'request-set-mark group))
12249               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12250                     (add (gnus-remove-from-range read (gnus-info-read info))))
12251                 (when (or add del)
12252                   (unless (gnus-check-group group)
12253                     (error "Can't open server for %s" group))
12254                   (gnus-request-set-mark
12255                    group (delq nil (list (if add (list add 'add '(read)))
12256                                          (if del (list del 'del '(read))))))
12257                   (setq setmarkundo
12258                         `(gnus-request-set-mark
12259                           ,group
12260                           ',(delq nil (list
12261                                        (if del (list del 'add '(read)))
12262                                        (if add (list add 'del '(read))))))))))
12263             (set-buffer gnus-group-buffer)
12264             (gnus-undo-register
12265               `(progn
12266                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12267                  (gnus-info-set-read ',info ',(gnus-info-read info))
12268                  (gnus-get-unread-articles-in-group ',info
12269                                                     (gnus-active ,group))
12270                  (gnus-group-update-group ,group t)
12271                  ,setmarkundo))))
12272         ;; Enter this list into the group info.
12273         (gnus-info-set-read info read)
12274         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12275         (gnus-get-unread-articles-in-group info (gnus-active group))
12276         t))))
12277
12278 (defun gnus-offer-save-summaries ()
12279   "Offer to save all active summary buffers."
12280   (let (buffers)
12281     ;; Go through all buffers and find all summaries.
12282     (dolist (buffer (buffer-list))
12283       (when (and (setq buffer (buffer-name buffer))
12284                  (string-match "Summary" buffer)
12285                  (with-current-buffer buffer
12286                    ;; We check that this is, indeed, a summary buffer.
12287                    (and (eq major-mode 'gnus-summary-mode)
12288                         ;; Also make sure this isn't bogus.
12289                         gnus-newsgroup-prepared
12290                         ;; Also make sure that this isn't a
12291                         ;; dead summary buffer.
12292                         (not gnus-dead-summary-mode))))
12293         (push buffer buffers)))
12294     ;; Go through all these summary buffers and offer to save them.
12295     (when buffers
12296       (save-excursion
12297         (map-y-or-n-p
12298          "Update summary buffer %s? "
12299          (lambda (buf)
12300            (switch-to-buffer buf)
12301            (gnus-summary-exit))
12302          buffers)))))
12303
12304 (defun gnus-summary-setup-default-charset ()
12305   "Setup newsgroup default charset."
12306   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12307       (setq gnus-newsgroup-charset nil)
12308     (let* ((ignored-charsets
12309             (or gnus-newsgroup-ephemeral-ignored-charsets
12310                 (append
12311                  (and gnus-newsgroup-name
12312                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12313                  gnus-newsgroup-ignored-charsets))))
12314       (setq gnus-newsgroup-charset
12315             (or gnus-newsgroup-ephemeral-charset
12316                 (and gnus-newsgroup-name
12317                      (gnus-parameter-charset gnus-newsgroup-name))
12318                 gnus-default-charset))
12319       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12320            ignored-charsets))))
12321
12322 ;;;
12323 ;;; Mime Commands
12324 ;;;
12325
12326 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12327   "Display the current article buffer fully MIME-buttonized.
12328 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12329 treated as multipart/mixed."
12330   (interactive "P")
12331   (require 'gnus-art)
12332   (let ((gnus-unbuttonized-mime-types nil)
12333         (gnus-mime-display-multipart-as-mixed show-all-parts))
12334     (gnus-summary-show-article)))
12335
12336 (defun gnus-summary-repair-multipart (article)
12337   "Add a Content-Type header to a multipart article without one."
12338   (interactive (list (gnus-summary-article-number)))
12339   (gnus-with-article article
12340     (message-narrow-to-head)
12341     (message-remove-header "Mime-Version")
12342     (goto-char (point-max))
12343     (insert "Mime-Version: 1.0\n")
12344     (widen)
12345     (when (search-forward "\n--" nil t)
12346       (let ((separator (buffer-substring (point) (point-at-eol))))
12347         (message-narrow-to-head)
12348         (message-remove-header "Content-Type")
12349         (goto-char (point-max))
12350         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12351                         separator))
12352         (widen))))
12353   (let (gnus-mark-article-hook)
12354     (gnus-summary-select-article t t nil article)))
12355
12356 (defun gnus-summary-toggle-display-buttonized ()
12357   "Toggle the buttonizing of the article buffer."
12358   (interactive)
12359   (require 'gnus-art)
12360   (if (setq gnus-inhibit-mime-unbuttonizing
12361             (not gnus-inhibit-mime-unbuttonizing))
12362       (let ((gnus-unbuttonized-mime-types nil))
12363         (gnus-summary-show-article))
12364     (gnus-summary-show-article)))
12365
12366 ;;;
12367 ;;; Generic summary marking commands
12368 ;;;
12369
12370 (defvar gnus-summary-marking-alist
12371   '((read gnus-del-mark "d")
12372     (unread gnus-unread-mark "u")
12373     (ticked gnus-ticked-mark "!")
12374     (dormant gnus-dormant-mark "?")
12375     (expirable gnus-expirable-mark "e"))
12376   "An alist of names/marks/keystrokes.")
12377
12378 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12379 (defvar gnus-summary-mark-map)
12380
12381 (defun gnus-summary-make-all-marking-commands ()
12382   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12383   (dolist (elem gnus-summary-marking-alist)
12384     (apply 'gnus-summary-make-marking-command elem)))
12385
12386 (defun gnus-summary-make-marking-command (name mark keystroke)
12387   (let ((map (make-sparse-keymap)))
12388     (define-key gnus-summary-generic-mark-map keystroke map)
12389     (dolist (lway `((next "next" next nil "n")
12390                     (next-unread "next unread" next t "N")
12391                     (prev "previous" prev nil "p")
12392                     (prev-unread "previous unread" prev t "P")
12393                     (nomove "" nil nil ,keystroke)))
12394       (let ((func (gnus-summary-make-marking-command-1
12395                    mark (car lway) lway name)))
12396         (setq func (eval func))
12397         (define-key map (nth 4 lway) func)))))
12398
12399 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12400   `(defun ,(intern
12401             (format "gnus-summary-put-mark-as-%s%s"
12402                     name (if (eq way 'nomove)
12403                              ""
12404                            (concat "-" (symbol-name way)))))
12405      (n)
12406      ,(format
12407        "Mark the current article as %s%s.
12408 If N, the prefix, then repeat N times.
12409 If N is negative, move in reverse order.
12410 The difference between N and the actual number of articles marked is
12411 returned."
12412        name (cadr lway))
12413      (interactive "p")
12414      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12415
12416 (defun gnus-summary-generic-mark (n mark move unread)
12417   "Mark N articles with MARK."
12418   (unless (eq major-mode 'gnus-summary-mode)
12419     (error "This command can only be used in the summary buffer"))
12420   (gnus-summary-show-thread)
12421   (let ((nummove
12422          (cond
12423           ((eq move 'next) 1)
12424           ((eq move 'prev) -1)
12425           (t 0))))
12426     (if (zerop nummove)
12427         (setq n 1)
12428       (when (< n 0)
12429         (setq n (abs n)
12430               nummove (* -1 nummove))))
12431     (while (and (> n 0)
12432                 (gnus-summary-mark-article nil mark)
12433                 (zerop (gnus-summary-next-subject nummove unread t)))
12434       (setq n (1- n)))
12435     (when (/= 0 n)
12436       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12437     (gnus-summary-recenter)
12438     (gnus-summary-position-point)
12439     (gnus-set-mode-line 'summary)
12440     n))
12441
12442 (defun gnus-summary-insert-articles (articles)
12443   (when (setq articles
12444               (gnus-sorted-difference articles
12445                                       (mapcar (lambda (h)
12446                                                 (mail-header-number h))
12447                                               gnus-newsgroup-headers)))
12448     (setq gnus-newsgroup-headers
12449           (gnus-merge 'list
12450                       gnus-newsgroup-headers
12451                       (gnus-fetch-headers articles)
12452                       'gnus-article-sort-by-number))
12453     ;; Suppress duplicates?
12454     (when gnus-suppress-duplicates
12455       (gnus-dup-suppress-articles))
12456
12457     (if (and gnus-fetch-old-headers
12458              (eq gnus-headers-retrieved-by 'nov))
12459         ;; We might want to build some more threads first.
12460         (if (eq gnus-fetch-old-headers 'invisible)
12461             (gnus-build-all-threads)
12462           (gnus-build-old-threads))
12463       ;; Mark the inserted articles that are unread as unread.
12464       (setq gnus-newsgroup-unreads
12465             (gnus-sorted-nunion
12466              gnus-newsgroup-unreads
12467              (gnus-sorted-nintersection
12468               (gnus-list-of-unread-articles gnus-newsgroup-name)
12469               articles)))
12470       ;; Mark the inserted articles as selected so that the information
12471       ;; of the marks having been changed by a user may be updated when
12472       ;; exiting this group.  See `gnus-summary-update-info'.
12473       (dolist (art articles)
12474         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12475     ;; Let the Gnus agent mark articles as read.
12476     (when gnus-agent
12477       (gnus-agent-get-undownloaded-list))
12478     ;; Remove list identifiers from subject
12479     (when gnus-list-identifiers
12480       (gnus-summary-remove-list-identifiers))
12481     ;; First and last article in this newsgroup.
12482     (when gnus-newsgroup-headers
12483       (setq gnus-newsgroup-begin
12484             (mail-header-number (car gnus-newsgroup-headers))
12485             gnus-newsgroup-end
12486             (mail-header-number
12487              (gnus-last-element gnus-newsgroup-headers))))
12488     (when gnus-use-scoring
12489       (gnus-possibly-score-headers))))
12490
12491 (defun gnus-summary-insert-old-articles (&optional all)
12492   "Insert all old articles in this group.
12493 If ALL is non-nil, already read articles become readable.
12494 If ALL is a number, fetch this number of articles."
12495   (interactive "P")
12496   (prog1
12497       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12498             older len)
12499         (setq older
12500               ;; Some nntp servers lie about their active range.  When
12501               ;; this happens, the active range can be in the millions.
12502               ;; Use a compressed range to avoid creating a huge list.
12503               (gnus-range-difference (list gnus-newsgroup-active) old))
12504         (setq len (gnus-range-length older))
12505         (cond
12506          ((null older) nil)
12507          ((numberp all)
12508           (if (< all len)
12509               (let ((older-range (nreverse older)))
12510                 (setq older nil)
12511
12512                 (while (> all 0)
12513                   (let* ((r (pop older-range))
12514                          (min (if (numberp r) r (car r)))
12515                          (max (if (numberp r) r (cdr r))))
12516                     (while (and (<= min max)
12517                                 (> all 0))
12518                       (push max older)
12519                       (setq all (1- all)
12520                             max (1- max))))))
12521             (setq older (gnus-uncompress-range older))))
12522          (all
12523           (setq older (gnus-uncompress-range older)))
12524          (t
12525           (when (and (numberp gnus-large-newsgroup)
12526                    (> len gnus-large-newsgroup))
12527               (let* ((cursor-in-echo-area nil)
12528                      (initial (gnus-parameter-large-newsgroup-initial
12529                                gnus-newsgroup-name))
12530                      (input
12531                       (read-string
12532                        (format
12533                         "How many articles from %s (%s %d): "
12534                         (gnus-group-decoded-name gnus-newsgroup-name)
12535                         (if initial "max" "default")
12536                         len)
12537                        (if initial
12538                            (cons (number-to-string initial)
12539                                  0)))))
12540                 (unless (string-match "^[ \t]*$" input)
12541                   (setq all (string-to-number input))
12542                   (if (< all len)
12543                       (let ((older-range (nreverse older)))
12544                         (setq older nil)
12545
12546                         (while (> all 0)
12547                           (let* ((r (pop older-range))
12548                                  (min (if (numberp r) r (car r)))
12549                                  (max (if (numberp r) r (cdr r))))
12550                             (while (and (<= min max)
12551                                         (> all 0))
12552                               (push max older)
12553                               (setq all (1- all)
12554                                     max (1- max))))))))))
12555           (setq older (gnus-uncompress-range older))))
12556         (if (not older)
12557             (message "No old news.")
12558           (gnus-summary-insert-articles older)
12559           (gnus-summary-limit (gnus-sorted-nunion old older))))
12560     (gnus-summary-position-point)))
12561
12562 (defun gnus-summary-insert-new-articles ()
12563   "Insert all new articles in this group."
12564   (interactive)
12565   (prog1
12566       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12567             (old-active gnus-newsgroup-active)
12568             (nnmail-fetched-sources (list t))
12569             i new)
12570         (setq gnus-newsgroup-active
12571               (gnus-activate-group gnus-newsgroup-name 'scan))
12572         (setq i (cdr gnus-newsgroup-active))
12573         (while (> i (cdr old-active))
12574           (push i new)
12575           (decf i))
12576         (if (not new)
12577             (message "No gnus is bad news")
12578           (gnus-summary-insert-articles new)
12579           (setq gnus-newsgroup-unreads
12580                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12581           (gnus-summary-limit (gnus-sorted-nunion old new))))
12582     (gnus-summary-position-point)))
12583
12584 (gnus-summary-make-all-marking-commands)
12585
12586 (gnus-ems-redefine)
12587
12588 (provide 'gnus-sum)
12589
12590 (run-hooks 'gnus-sum-load-hook)
12591
12592 ;; Local Variables:
12593 ;; coding: iso-8859-1
12594 ;; End:
12595
12596 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12597 ;;; gnus-sum.el ends here