Add missing :version tags to new defcustoms and defgroups
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-refer-thread-use-nnir nil
122   "*Use nnir to search an entire server when referring threads. A
123 nil value will only search for thread-related articles in the
124 current group."
125   :version "24.1"
126   :group 'gnus-thread
127   :type 'boolean)
128
129 (defcustom gnus-summary-make-false-root 'adopt
130   "*nil means that Gnus won't gather loose threads.
131 If the root of a thread has expired or been read in a previous
132 session, the information necessary to build a complete thread has been
133 lost.  Instead of having many small sub-threads from this original thread
134 scattered all over the summary buffer, Gnus can gather them.
135
136 If non-nil, Gnus will try to gather all loose sub-threads from an
137 original thread into one large thread.
138
139 If this variable is non-nil, it should be one of `none', `adopt',
140 `dummy' or `empty'.
141
142 If this variable is `none', Gnus will not make a false root, but just
143 present the sub-threads after another.
144 If this variable is `dummy', Gnus will create a dummy root that will
145 have all the sub-threads as children.
146 If this variable is `adopt', Gnus will make one of the \"children\"
147 the parent and mark all the step-children as such.
148 If this variable is `empty', the \"children\" are printed with empty
149 subject fields.  (Or rather, they will be printed with a string
150 given by the `gnus-summary-same-subject' variable.)"
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  (const none)
154                  (const dummy)
155                  (const adopt)
156                  (const empty)))
157
158 (defcustom gnus-summary-make-false-root-always nil
159   "Always make a false dummy root."
160   :version "22.1"
161   :group 'gnus-thread
162   :type 'boolean)
163
164 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
165   "*A regexp to match subjects to be excluded from loose thread gathering.
166 As loose thread gathering is done on subjects only, that means that
167 there can be many false gatherings performed.  By rooting out certain
168 common subjects, gathering might become saner."
169   :group 'gnus-thread
170   :type 'regexp)
171
172 (defcustom gnus-summary-gather-subject-limit nil
173   "*Maximum length of subject comparisons when gathering loose threads.
174 Use nil to compare full subjects.  Setting this variable to a low
175 number will help gather threads that have been corrupted by
176 newsreaders chopping off subject lines, but it might also mean that
177 unrelated articles that have subject that happen to begin with the
178 same few characters will be incorrectly gathered.
179
180 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
181 comparing subjects."
182   :group 'gnus-thread
183   :type '(choice (const :tag "off" nil)
184                  (const fuzzy)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-simplify-subject-functions nil
188   "List of functions taking a string argument that simplify subjects.
189 The functions are applied recursively.
190
191 Useful functions to put in this list include:
192 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
193 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
194   :group 'gnus-thread
195   :type '(repeat function))
196
197 (defcustom gnus-simplify-ignored-prefixes nil
198   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  regexp))
202
203 (defcustom gnus-build-sparse-threads nil
204   "*If non-nil, fill in the gaps in threads.
205 If `some', only fill in the gaps that are needed to tie loose threads
206 together.  If `more', fill in all leaf nodes that Gnus can find.  If
207 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
208   :group 'gnus-thread
209   :type '(choice (const :tag "off" nil)
210                  (const some)
211                  (const more)
212                  (sexp :menu-tag "all" t)))
213
214 (defcustom gnus-summary-thread-gathering-function
215   'gnus-gather-threads-by-subject
216   "*Function used for gathering loose threads.
217 There are two pre-defined functions: `gnus-gather-threads-by-subject',
218 which only takes Subjects into consideration; and
219 `gnus-gather-threads-by-references', which compared the References
220 headers of the articles to find matches."
221   :group 'gnus-thread
222   :type '(radio (function-item gnus-gather-threads-by-subject)
223                 (function-item gnus-gather-threads-by-references)
224                 (function :tag "other")))
225
226 (defcustom gnus-summary-same-subject ""
227   "*String indicating that the current article has the same subject as the previous.
228 This variable will only be used if the value of
229 `gnus-summary-make-false-root' is `empty'."
230   :group 'gnus-summary-format
231   :type 'string)
232
233 (defcustom gnus-summary-goto-unread nil
234   "*If t, many commands will go to the next unread article.
235 This applies to marking commands as well as other commands that
236 \"naturally\" select the next article, like, for instance, `SPC' at
237 the end of an article.
238
239 If nil, the marking commands do NOT go to the next unread article
240 \(they go to the next article instead).  If `never', commands that
241 usually go to the next unread article, will go to the next article,
242 whether it is read or not."
243   :version "24.1"
244   :group 'gnus-summary-marks
245   :link '(custom-manual "(gnus)Setting Marks")
246   :type '(choice (const :tag "off" nil)
247                  (const never)
248                  (sexp :menu-tag "on" t)))
249
250 (defcustom gnus-summary-default-score 0
251   "*Default article score level.
252 All scores generated by the score files will be added to this score.
253 If this variable is nil, scoring will be disabled."
254   :group 'gnus-score-default
255   :type '(choice (const :tag "disable")
256                  integer))
257
258 (defcustom gnus-summary-default-high-score 0
259   "*Default threshold for a high scored article.
260 An article will be highlighted as high scored if its score is greater
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-default-low-score 0
267   "*Default threshold for a low scored article.
268 An article will be highlighted as low scored if its score is smaller
269 than this score."
270   :version "22.1"
271   :group 'gnus-score-default
272   :type 'integer)
273
274 (defcustom gnus-summary-zcore-fuzz 0
275   "*Fuzziness factor for the zcore in the summary buffer.
276 Articles with scores closer than this to `gnus-summary-default-score'
277 will not be marked."
278   :group 'gnus-summary-format
279   :type 'integer)
280
281 (defcustom gnus-simplify-subject-fuzzy-regexp nil
282   "*Strings to be removed when doing fuzzy matches.
283 This can either be a regular expression or list of regular expressions
284 that will be removed from subject strings if fuzzy subject
285 simplification is selected."
286   :group 'gnus-thread
287   :type '(repeat regexp))
288
289 (defcustom gnus-show-threads t
290   "*If non-nil, display threads in summary mode."
291   :group 'gnus-thread
292   :type 'boolean)
293
294 (defcustom gnus-thread-hide-subtree nil
295   "*If non-nil, hide all threads initially.
296 This can be a predicate specifier which says which threads to hide.
297 If threads are hidden, you have to run the command
298 `gnus-summary-show-thread' by hand or select an article."
299   :group 'gnus-thread
300   :type '(radio (sexp :format "Non-nil\n"
301                       :match (lambda (widget value)
302                                (not (or (consp value) (functionp value))))
303                       :value t)
304                 (const nil)
305                 (sexp :tag "Predicate specifier")))
306
307 (defcustom gnus-thread-hide-killed t
308   "*If non-nil, hide killed threads automatically."
309   :group 'gnus-thread
310   :type 'boolean)
311
312 (defcustom gnus-thread-ignore-subject t
313   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
314 If nil, articles that have different subjects from their parents will
315 start separate threads."
316   :group 'gnus-thread
317   :type 'boolean)
318
319 (defcustom gnus-thread-operation-ignore-subject t
320   "*If non-nil, subjects will be ignored when doing thread commands.
321 This affects commands like `gnus-summary-kill-thread' and
322 `gnus-summary-lower-thread'.
323
324 If this variable is nil, articles in the same thread with different
325 subjects will not be included in the operation in question.  If this
326 variable is `fuzzy', only articles that have subjects that are fuzzily
327 equal will be included."
328   :group 'gnus-thread
329   :type '(choice (const :tag "off" nil)
330                  (const fuzzy)
331                  (sexp :tag "on" t)))
332
333 (defcustom gnus-thread-indent-level 4
334   "*Number that says how much each sub-thread should be indented."
335   :group 'gnus-thread
336   :type 'integer)
337
338 (defcustom gnus-auto-extend-newsgroup t
339   "*If non-nil, extend newsgroup forward and backward when requested."
340   :group 'gnus-summary-choose
341   :type 'boolean)
342
343 (defcustom gnus-auto-select-first t
344   "If non-nil, select an article on group entry.
345 An article is selected automatically when entering a group
346 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
347 `gnus-summary-catchup-and-goto-next-group'.
348
349 Which article is selected is controlled by the variable
350 `gnus-auto-select-subject'.
351
352 If you want to prevent automatic selection of articles in some
353 newsgroups, set the variable to nil in `gnus-select-group-hook'."
354   ;; Commands include...
355   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
357   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
358   :group 'gnus-group-select
359   :type '(choice (const :tag "none" nil)
360                  (sexp :menu-tag "first" t)))
361
362 (defcustom gnus-auto-select-subject 'unseen-or-unread
363   "*Says what subject to place under point when entering a group.
364
365 This variable can either be the symbols `first' (place point on the
366 first subject), `unread' (place point on the subject line of the first
367 unread article), `best' (place point on the subject line of the
368 highest-scored article), `unseen' (place point on the subject line of
369 the first unseen article), `unseen-or-unread' (place point on the subject
370 line of the first unseen article or, if all articles have been seen, on the
371 subject line of the first unread article), or a function to be called to
372 place point on some subject line."
373   :version "24.1"
374   :group 'gnus-group-select
375   :type '(choice (const best)
376                  (const unread)
377                  (const first)
378                  (const unseen)
379                  (const unseen-or-unread)
380                  (function :tag "Function to call")))
381
382 (defcustom gnus-auto-select-next t
383   "*If non-nil, offer to go to the next group from the end of the previous.
384 If the value is t and the next newsgroup is empty, Gnus will exit
385 summary mode and go back to group mode.  If the value is neither nil
386 nor t, Gnus will select the following unread newsgroup.  In
387 particular, if the value is the symbol `quietly', the next unread
388 newsgroup will be selected without any confirmation, and if it is
389 `almost-quietly', the next group will be selected without any
390 confirmation if you are located on the last article in the group.
391 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
392 will go to the next group without confirmation."
393   :group 'gnus-summary-maneuvering
394   :type '(choice (const :tag "off" nil)
395                  (const quietly)
396                  (const almost-quietly)
397                  (const slightly-quietly)
398                  (sexp :menu-tag "on" t)))
399
400 (defcustom gnus-auto-select-same nil
401   "*If non-nil, select the next article with the same subject.
402 If there are no more articles with the same subject, go to
403 the first unread article."
404   :group 'gnus-summary-maneuvering
405   :type 'boolean)
406
407 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
408   "What article should be selected after exiting an ephemeral group.
409 Valid values include:
410
411 `next'
412   Select the next article.
413 `next-unread'
414   Select the next unread article.
415 `next-noselect'
416   Move the cursor to the next article.  This is the default.
417 `next-unread-noselect'
418   Move the cursor to the next unread article.
419
420 If it has any other value or there is no next (unread) article, the
421 article selected before entering to the ephemeral group will appear."
422   :version "23.1" ;; No Gnus
423   :group 'gnus-summary-maneuvering
424   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
425                  (const next) (const next-unread)
426                  (const next-noselect) (const next-unread-noselect)
427                  (sexp :tag "other" :value nil)))
428
429 (defcustom gnus-auto-goto-ignores 'unfetched
430   "*Says how to handle unfetched articles when maneuvering.
431
432 This variable can either be the symbols nil (maneuver to any
433 article), `undownloaded' (maneuvering while unplugged ignores articles
434 that have not been fetched), `always-undownloaded' (maneuvering always
435 ignores articles that have not been fetched), `unfetched' (maneuvering
436 ignores articles whose headers have not been fetched).
437
438 NOTE: The list of unfetched articles will always be nil when plugged
439 and, when unplugged, a subset of the undownloaded article list."
440   :version "22.1"
441   :group 'gnus-summary-maneuvering
442   :type '(choice (const :tag "None" nil)
443                  (const :tag "Undownloaded when unplugged" undownloaded)
444                  (const :tag "Undownloaded" always-undownloaded)
445                  (const :tag "Unfetched" unfetched)))
446
447 (defcustom gnus-summary-check-current nil
448   "*If non-nil, consider the current article when moving.
449 The \"unread\" movement commands will stay on the same line if the
450 current article is unread."
451   :group 'gnus-summary-maneuvering
452   :type 'boolean)
453
454 (defcustom gnus-auto-center-summary 2
455   "*If non-nil, always center the current summary buffer.
456 In particular, if `vertical' do only vertical recentering.  If non-nil
457 and non-`vertical', do both horizontal and vertical recentering."
458   :group 'gnus-summary-maneuvering
459   :type '(choice (const :tag "none" nil)
460                  (const vertical)
461                  (integer :tag "height")
462                  (sexp :menu-tag "both" t)))
463
464 (defcustom gnus-auto-center-group t
465   "If non-nil, always center the group buffer."
466   :group 'gnus-summary-maneuvering
467   :type 'boolean)
468
469 (defcustom gnus-show-all-headers nil
470   "*If non-nil, don't hide any headers."
471   :group 'gnus-article-hiding
472   :group 'gnus-article-headers
473   :type 'boolean)
474
475 (defcustom gnus-summary-ignore-duplicates nil
476   "*If non-nil, ignore articles with identical Message-ID headers."
477   :group 'gnus-summary
478   :type 'boolean)
479
480 (defcustom gnus-single-article-buffer nil
481   "*If non-nil, display all articles in the same buffer.
482 If nil, each group will get its own article buffer."
483   :version "24.1"
484   :group 'gnus-article-various
485   :type 'boolean)
486
487 (defcustom gnus-widen-article-window nil
488   "If non-nil, selecting the article buffer will display only the article buffer."
489   :version "24.1"
490   :group 'gnus-article-various
491   :type 'boolean)
492
493 (defcustom gnus-break-pages t
494   "*If non-nil, do page breaking on articles.
495 The page delimiter is specified by the `gnus-page-delimiter'
496 variable."
497   :group 'gnus-article-various
498   :type 'boolean)
499
500 (defcustom gnus-move-split-methods nil
501   "*Variable used to suggest where articles are to be moved to.
502 It uses the same syntax as the `gnus-split-methods' variable.
503 However, whereas `gnus-split-methods' specifies file names as targets,
504 this variable specifies group names."
505   :group 'gnus-summary-mail
506   :type '(repeat (choice (list :value (fun) function)
507                          (cons :value ("" "") regexp (repeat string))
508                          (sexp :value nil))))
509
510 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
511   "Function used to compute default prefix for article move/copy/etc prompts.
512 The function should take one argument, a group name, and return a
513 string with the suggested prefix."
514   :group 'gnus-summary-mail
515   :type 'function)
516
517 ;; FIXME: Although the custom type is `character' for the following variables,
518 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
519
520 (defcustom gnus-unread-mark ?           ;Whitespace
521   "*Mark used for unread articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-ticked-mark ?!
526   "*Mark used for ticked articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-dormant-mark ??
531   "*Mark used for dormant articles."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-del-mark ?r
536   "*Mark used for del'd articles."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-read-mark ?R
541   "*Mark used for read articles."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-expirable-mark ?E
546   "*Mark used for expirable articles."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-killed-mark ?K
551   "*Mark used for killed articles."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-spam-mark ?$
556   "*Mark used for spam articles."
557   :version "22.1"
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-kill-file-mark ?X
562   "*Mark used for articles killed by kill files."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-low-score-mark ?Y
567   "*Mark used for articles with a low score."
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-catchup-mark ?C
572   "*Mark used for articles that are caught up."
573   :group 'gnus-summary-marks
574   :type 'character)
575
576 (defcustom gnus-replied-mark ?A
577   "*Mark used for articles that have been replied to."
578   :group 'gnus-summary-marks
579   :type 'character)
580
581 (defcustom gnus-forwarded-mark ?F
582   "*Mark used for articles that have been forwarded."
583   :version "22.1"
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-recent-mark ?N
588   "*Mark used for articles that are recent."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-cached-mark ?*
594   "*Mark used for articles that are in the cache."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-saved-mark ?S
599   "*Mark used for articles that have been saved."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-unseen-mark ?.
604   "*Mark used for articles that haven't been seen."
605   :version "22.1"
606   :group 'gnus-summary-marks
607   :type 'character)
608
609 (defcustom gnus-no-mark ?               ;Whitespace
610   "*Mark used for articles that have no other secondary mark."
611   :version "22.1"
612   :group 'gnus-summary-marks
613   :type 'character)
614
615 (defcustom gnus-ancient-mark ?O
616   "*Mark used for ancient articles."
617   :group 'gnus-summary-marks
618   :type 'character)
619
620 (defcustom gnus-sparse-mark ?Q
621   "*Mark used for sparsely reffed articles."
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-canceled-mark ?G
626   "*Mark used for canceled articles."
627   :group 'gnus-summary-marks
628   :type 'character)
629
630 (defcustom gnus-duplicate-mark ?M
631   "*Mark used for duplicate articles."
632   :group 'gnus-summary-marks
633   :type 'character)
634
635 (defcustom gnus-undownloaded-mark ?-
636   "*Mark used for articles that weren't downloaded."
637   :version "22.1"
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-downloaded-mark ?+
642   "*Mark used for articles that were downloaded."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-downloadable-mark ?%
647   "*Mark used for articles that are to be downloaded."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-unsendable-mark ?=
652   "*Mark used for articles that won't be sent."
653   :group 'gnus-summary-marks
654   :type 'character)
655
656 (defcustom gnus-score-over-mark ?+
657   "*Score mark used for articles with high scores."
658   :group 'gnus-summary-marks
659   :type 'character)
660
661 (defcustom gnus-score-below-mark ?-
662   "*Score mark used for articles with low scores."
663   :group 'gnus-summary-marks
664   :type 'character)
665
666 (defcustom gnus-empty-thread-mark ?     ;Whitespace
667   "*There is no thread under the article."
668   :group 'gnus-summary-marks
669   :type 'character)
670
671 (defcustom gnus-not-empty-thread-mark ?=
672   "*There is a thread under the article."
673   :group 'gnus-summary-marks
674   :type 'character)
675
676 (defcustom gnus-view-pseudo-asynchronously nil
677   "*If non-nil, Gnus will view pseudo-articles asynchronously."
678   :group 'gnus-extract-view
679   :type 'boolean)
680
681 (defcustom gnus-auto-expirable-marks
682   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
683         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
684         gnus-duplicate-mark)
685   "*The list of marks converted into expiration if a group is auto-expirable."
686   :version "24.1"
687   :group 'gnus-summary
688   :type '(repeat character))
689
690 (defcustom gnus-inhibit-user-auto-expire t
691   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
692   :version "21.1"
693   :group 'gnus-summary
694   :type 'boolean)
695
696 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
697   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
698 If nil, the expirable marks will be unchanged except that the marks
699 will be removed when copying or moving articles to a group that has
700 not turned auto-expire on.  If non-nil, articles that have been read
701 will be marked as expirable when being copied or moved to a group in
702 which auto-expire is turned on."
703   :version "23.2"
704   :type 'boolean
705   :group 'gnus-summary-marks)
706
707 (defcustom gnus-view-pseudos nil
708   "*If `automatic', pseudo-articles will be viewed automatically.
709 If `not-confirm', pseudos will be viewed automatically, and the user
710 will not be asked to confirm the command."
711   :group 'gnus-extract-view
712   :type '(choice (const :tag "off" nil)
713                  (const automatic)
714                  (const not-confirm)))
715
716 (defcustom gnus-view-pseudos-separately t
717   "*If non-nil, one pseudo-article will be created for each file to be viewed.
718 If nil, all files that use the same viewing command will be given as a
719 list of parameters to that command."
720   :group 'gnus-extract-view
721   :type 'boolean)
722
723 (defcustom gnus-insert-pseudo-articles t
724   "*If non-nil, insert pseudo-articles when decoding articles."
725   :group 'gnus-extract-view
726   :type 'boolean)
727
728 (defcustom gnus-summary-dummy-line-format
729   "   %(:                             :%) %S\n"
730   "*The format specification for the dummy roots in the summary buffer.
731 It works along the same lines as a normal formatting string,
732 with some simple extensions.
733
734 %S  The subject
735
736 General format specifiers can also be used.
737 See `(gnus)Formatting Variables'."
738   :link '(custom-manual "(gnus)Formatting Variables")
739   :group 'gnus-threading
740   :type 'string)
741
742 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
743   "*The format specification for the summary mode line.
744 It works along the same lines as a normal formatting string,
745 with some simple extensions:
746
747 %G  Group name
748 %p  Unprefixed group name
749 %A  Current article number
750 %z  Current article score
751 %V  Gnus version
752 %U  Number of unread articles in the group
753 %e  Number of unselected articles in the group
754 %Z  A string with unread/unselected article counts
755 %g  Shortish group name
756 %S  Subject of the current article
757 %u  User-defined spec
758 %s  Current score file name
759 %d  Number of dormant articles
760 %r  Number of articles that have been marked as read in this session
761 %E  Number of articles expunged by the score files"
762   :group 'gnus-summary-format
763   :type 'string)
764
765 (defcustom gnus-list-identifiers nil
766   "Regexp that matches list identifiers to be removed from subject.
767 This can also be a list of regexps."
768   :version "21.1"
769   :group 'gnus-summary-format
770   :group 'gnus-article-hiding
771   :type '(choice (const :tag "none" nil)
772                  (regexp :value ".*")
773                  (repeat :value (".*") regexp)))
774
775 (defcustom gnus-summary-mark-below 0
776   "*Mark all articles with a score below this variable as read.
777 This variable is local to each summary buffer and usually set by the
778 score file."
779   :group 'gnus-score-default
780   :type 'integer)
781
782 (defun gnus-widget-reversible-match (widget value)
783   "Ignoring WIDGET, convert VALUE to internal form.
784 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
785   ;; (debug value)
786   (or (symbolp value)
787       (and (listp value)
788            (eq (length value) 2)
789            (eq (nth 0 value) 'not)
790            (symbolp (nth 1 value)))))
791
792 (defun gnus-widget-reversible-to-internal (widget value)
793   "Ignoring WIDGET, convert VALUE to internal form.
794 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
795 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
796   ;; (debug value)
797   (if (atom value)
798       (list value nil)
799     (list (nth 1 value) t)))
800
801 (defun gnus-widget-reversible-to-external (widget value)
802   "Ignoring WIDGET, convert VALUE to external form.
803 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
804 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
805   ;; (debug value)
806   (if (nth 1 value)
807       (list 'not (nth 0 value))
808     (nth 0 value)))
809
810 (define-widget 'gnus-widget-reversible 'group
811   "A `group' that convert values."
812   :match 'gnus-widget-reversible-match
813   :value-to-internal 'gnus-widget-reversible-to-internal
814   :value-to-external 'gnus-widget-reversible-to-external)
815
816 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
817   "*List of functions used for sorting articles in the summary buffer.
818
819 Each function takes two articles and returns non-nil if the first
820 article should be sorted before the other.  If you use more than one
821 function, the primary sort function should be the last.  You should
822 probably always include `gnus-article-sort-by-number' in the list of
823 sorting functions -- preferably first.  Also note that sorting by date
824 is often much slower than sorting by number, and the sorting order is
825 very similar.  (Sorting by date means sorting by the time the message
826 was sent, sorting by number means sorting by arrival time.)
827
828 Each item can also be a list `(not F)' where F is a function;
829 this reverses the sort order.
830
831 Ready-made functions include `gnus-article-sort-by-number',
832 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
833 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
834 and `gnus-article-sort-by-score'.
835
836 When threading is turned on, the variable `gnus-thread-sort-functions'
837 controls how articles are sorted."
838   :group 'gnus-summary-sort
839   :type '(repeat (gnus-widget-reversible
840                   (choice (function-item gnus-article-sort-by-number)
841                           (function-item gnus-article-sort-by-author)
842                           (function-item gnus-article-sort-by-subject)
843                           (function-item gnus-article-sort-by-date)
844                           (function-item gnus-article-sort-by-score)
845                           (function-item gnus-article-sort-by-random)
846                           (function :tag "other"))
847                   (boolean :tag "Reverse order"))))
848
849
850 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
851   "*List of functions used for sorting threads in the summary buffer.
852 By default, threads are sorted by article number.
853
854 Each function takes two threads and returns non-nil if the first
855 thread should be sorted before the other.  If you use more than one
856 function, the primary sort function should be the last.  You should
857 probably always include `gnus-thread-sort-by-number' in the list of
858 sorting functions -- preferably first.  Also note that sorting by date
859 is often much slower than sorting by number, and the sorting order is
860 very similar.  (Sorting by date means sorting by the time the message
861 was sent, sorting by number means sorting by arrival time.)
862
863 Each list item can also be a list `(not F)' where F is a
864 function; this specifies reversed sort order.
865
866 Ready-made functions include `gnus-thread-sort-by-number',
867 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
868 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
869 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
870 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
871 and `gnus-thread-sort-by-total-score' (see
872 `gnus-thread-score-function').
873
874 When threading is turned off, the variable
875 `gnus-article-sort-functions' controls how articles are sorted."
876   :group 'gnus-summary-sort
877   :type '(repeat
878           (gnus-widget-reversible
879            (choice (function-item gnus-thread-sort-by-number)
880                    (function-item gnus-thread-sort-by-author)
881                    (function-item gnus-thread-sort-by-recipient)
882                    (function-item gnus-thread-sort-by-subject)
883                    (function-item gnus-thread-sort-by-date)
884                    (function-item gnus-thread-sort-by-score)
885                    (function-item gnus-thread-sort-by-most-recent-number)
886                    (function-item gnus-thread-sort-by-most-recent-date)
887                    (function-item gnus-thread-sort-by-random)
888                    (function-item gnus-thread-sort-by-total-score)
889                    (function :tag "other"))
890            (boolean :tag "Reverse order"))))
891
892 (defcustom gnus-thread-score-function '+
893   "*Function used for calculating the total score of a thread.
894
895 The function is called with the scores of the article and each
896 subthread and should then return the score of the thread.
897
898 Some functions you can use are `+', `max', or `min'."
899   :group 'gnus-summary-sort
900   :type 'function)
901
902 (defcustom gnus-summary-expunge-below nil
903   "All articles that have a score less than this variable will be expunged.
904 This variable is local to the summary buffers."
905   :group 'gnus-score-default
906   :type '(choice (const :tag "off" nil)
907                  integer))
908
909 (defcustom gnus-thread-expunge-below nil
910   "All threads that have a total score less than this variable will be expunged.
911 See `gnus-thread-score-function' for en explanation of what a
912 \"thread score\" is.
913
914 This variable is local to the summary buffers."
915   :group 'gnus-threading
916   :group 'gnus-score-default
917   :type '(choice (const :tag "off" nil)
918                  integer))
919
920 (defcustom gnus-summary-mode-hook nil
921   "*A hook for Gnus summary mode.
922 This hook is run before any variables are set in the summary buffer."
923   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
924   :group 'gnus-summary-various
925   :type 'hook)
926
927 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
928 (when (featurep 'xemacs)
929   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
930   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
931   (add-hook 'gnus-summary-mode-hook
932             'gnus-xmas-switch-horizontal-scrollbar-off))
933
934 (defcustom gnus-summary-menu-hook nil
935   "*Hook run after the creation of the summary mode menu."
936   :group 'gnus-summary-visual
937   :type 'hook)
938
939 (defcustom gnus-summary-exit-hook nil
940   "*A hook called on exit from the summary buffer.
941 It will be called with point in the group buffer."
942   :group 'gnus-summary-exit
943   :type 'hook)
944
945 (defcustom gnus-summary-prepare-hook nil
946   "*A hook called after the summary buffer has been generated.
947 If you want to modify the summary buffer, you can use this hook."
948   :group 'gnus-summary-various
949   :type 'hook)
950
951 (defcustom gnus-summary-prepared-hook nil
952   "*A hook called as the last thing after the summary buffer has been generated."
953   :group 'gnus-summary-various
954   :type 'hook)
955
956 (defcustom gnus-summary-generate-hook nil
957   "*A hook run just before generating the summary buffer.
958 This hook is commonly used to customize threading variables and the
959 like."
960   :group 'gnus-summary-various
961   :type 'hook)
962
963 (defcustom gnus-select-group-hook nil
964   "*A hook called when a newsgroup is selected.
965
966 If you'd like to simplify subjects like the
967 `gnus-summary-next-same-subject' command does, you can use the
968 following hook:
969
970  (add-hook gnus-select-group-hook
971            (lambda ()
972              (mapcar (lambda (header)
973                        (mail-header-set-subject
974                         header
975                         (gnus-simplify-subject
976                          (mail-header-subject header) 're-only)))
977                      gnus-newsgroup-headers)))"
978   :group 'gnus-group-select
979   :type 'hook)
980
981 (defcustom gnus-select-article-hook nil
982   "*A hook called when an article is selected."
983   :group 'gnus-summary-choose
984   :options '(gnus-agent-fetch-selected-article)
985   :type 'hook)
986
987 (defcustom gnus-visual-mark-article-hook
988   (list 'gnus-highlight-selected-summary)
989   "*Hook run after selecting an article in the summary buffer.
990 It is meant to be used for highlighting the article in some way.  It
991 is not run if `gnus-visual' is nil."
992   :group 'gnus-summary-visual
993   :type 'hook)
994
995 (defcustom gnus-parse-headers-hook nil
996   "*A hook called before parsing the headers."
997   :group 'gnus-various
998   :type 'hook)
999
1000 (defcustom gnus-exit-group-hook nil
1001   "*A hook called when exiting summary mode.
1002 This hook is not called from the non-updating exit commands like `Q'."
1003   :group 'gnus-various
1004   :type 'hook)
1005
1006 (defcustom gnus-summary-update-hook nil
1007   "*A hook called when a summary line is changed.
1008 The hook will not be called if `gnus-visual' is nil.
1009
1010 The default function `gnus-summary-highlight-line' will
1011 highlight the line according to the `gnus-summary-highlight'
1012 variable."
1013   :group 'gnus-summary-visual
1014   :type 'hook)
1015
1016 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1017   "*A hook called when an article is selected for the first time.
1018 The hook is intended to mark an article as read (or unread)
1019 automatically when it is selected."
1020   :group 'gnus-summary-choose
1021   :type 'hook)
1022
1023 (defcustom gnus-group-no-more-groups-hook nil
1024   "*A hook run when returning to group mode having no more (unread) groups."
1025   :group 'gnus-group-select
1026   :type 'hook)
1027
1028 (defcustom gnus-ps-print-hook nil
1029   "*A hook run before ps-printing something from Gnus."
1030   :group 'gnus-summary
1031   :type 'hook)
1032
1033 (defcustom gnus-summary-article-move-hook nil
1034   "*A hook called after an article is moved, copied, respooled, or crossposted."
1035   :version "22.1"
1036   :group 'gnus-summary
1037   :type 'hook)
1038
1039 (defcustom gnus-summary-article-delete-hook nil
1040   "*A hook called after an article is deleted."
1041   :version "22.1"
1042   :group 'gnus-summary
1043   :type 'hook)
1044
1045 (defcustom gnus-summary-article-expire-hook nil
1046   "*A hook called after an article is expired."
1047   :version "22.1"
1048   :group 'gnus-summary
1049   :type 'hook)
1050
1051 (defcustom gnus-summary-display-arrow
1052   (and (fboundp 'display-graphic-p)
1053        (display-graphic-p))
1054   "*If non-nil, display an arrow highlighting the current article."
1055   :version "22.1"
1056   :group 'gnus-summary
1057   :type 'boolean)
1058
1059 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1060   "Face used for highlighting the current article in the summary buffer."
1061   :group 'gnus-summary-visual
1062   :type 'face)
1063
1064 (defvar gnus-tmp-downloaded nil)
1065
1066 (defcustom gnus-summary-highlight
1067   '(((eq mark gnus-canceled-mark)
1068      . gnus-summary-cancelled)
1069     ((and uncached (> score default-high))
1070      . gnus-summary-high-undownloaded)
1071     ((and uncached (< score default-low))
1072      . gnus-summary-low-undownloaded)
1073     (uncached
1074      . gnus-summary-normal-undownloaded)
1075     ((and (> score default-high)
1076           (or (eq mark gnus-dormant-mark)
1077               (eq mark gnus-ticked-mark)))
1078      . gnus-summary-high-ticked)
1079     ((and (< score default-low)
1080           (or (eq mark gnus-dormant-mark)
1081               (eq mark gnus-ticked-mark)))
1082      . gnus-summary-low-ticked)
1083     ((or (eq mark gnus-dormant-mark)
1084          (eq mark gnus-ticked-mark))
1085      . gnus-summary-normal-ticked)
1086     ((and (> score default-high) (eq mark gnus-ancient-mark))
1087      . gnus-summary-high-ancient)
1088     ((and (< score default-low) (eq mark gnus-ancient-mark))
1089      . gnus-summary-low-ancient)
1090     ((eq mark gnus-ancient-mark)
1091      . gnus-summary-normal-ancient)
1092     ((and (> score default-high) (eq mark gnus-unread-mark))
1093      . gnus-summary-high-unread)
1094     ((and (< score default-low) (eq mark gnus-unread-mark))
1095      . gnus-summary-low-unread)
1096     ((eq mark gnus-unread-mark)
1097      . gnus-summary-normal-unread)
1098     ((> score default-high)
1099      . gnus-summary-high-read)
1100     ((< score default-low)
1101      . gnus-summary-low-read)
1102     (t
1103      . gnus-summary-normal-read))
1104   "*Controls the highlighting of summary buffer lines.
1105
1106 A list of (FORM . FACE) pairs.  When deciding how a particular
1107 summary line should be displayed, each form is evaluated.  The content
1108 of the face field after the first true form is used.  You can change
1109 how those summary lines are displayed, by editing the face field.
1110
1111 You can use the following variables in the FORM field.
1112
1113 score:        The article's score.
1114 default:      The default article score.
1115 default-high: The default score for high scored articles.
1116 default-low:  The default score for low scored articles.
1117 below:        The score below which articles are automatically marked as read.
1118 mark:         The article's mark.
1119 uncached:     Non-nil if the article is uncached."
1120   :group 'gnus-summary-visual
1121   :type '(repeat (cons (sexp :tag "Form" nil)
1122                        face)))
1123 (put 'gnus-summary-highlight 'risky-local-variable t)
1124
1125 (defcustom gnus-alter-header-function nil
1126   "Function called to allow alteration of article header structures.
1127 The function is called with one parameter, the article header vector,
1128 which it may alter in any way."
1129   :type '(choice (const :tag "None" nil)
1130                  function)
1131   :group 'gnus-summary)
1132
1133 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1134   "Function used to decode a string with encoded words.")
1135
1136 (defvar gnus-decode-encoded-address-function
1137   'mail-decode-encoded-address-string
1138   "Function used to decode addresses with encoded words.")
1139
1140 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1141   "*Extra headers to parse."
1142   :version "24.1"                       ; added Cc Keywords Gcc
1143   :group 'gnus-summary
1144   :type '(repeat symbol))
1145
1146 (defcustom gnus-ignored-from-addresses
1147   (and user-mail-address
1148        (not (string= user-mail-address ""))
1149        (regexp-quote user-mail-address))
1150   "*From headers that may be suppressed in favor of To headers.
1151 This can be a regexp or a list of regexps."
1152   :version "21.1"
1153   :group 'gnus-summary
1154   :type '(choice regexp
1155                  (repeat :tag "Regexp List" regexp)))
1156
1157 (defsubst gnus-ignored-from-addresses ()
1158   (gmm-regexp-concat gnus-ignored-from-addresses))
1159
1160 (defcustom gnus-summary-to-prefix "-> "
1161   "*String prefixed to the To field in the summary line when
1162 using `gnus-ignored-from-addresses'."
1163   :version "22.1"
1164   :group 'gnus-summary
1165   :type 'string)
1166
1167 (defcustom gnus-summary-newsgroup-prefix "=> "
1168   "*String prefixed to the Newsgroup field in the summary
1169 line when using `gnus-ignored-from-addresses'."
1170   :version "22.1"
1171   :group 'gnus-summary
1172   :type 'string)
1173
1174 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1175   "List of charsets that should be ignored.
1176 When these charsets are used in the \"charset\" parameter, the
1177 default charset will be used instead."
1178   :version "21.1"
1179   :type '(repeat symbol)
1180   :group 'gnus-charset)
1181
1182 (defcustom gnus-newsgroup-maximum-articles nil
1183   "The maximum number of articles a newsgroup.
1184 If this is a number, old articles in a newsgroup exceeding this number
1185 are silently ignored.  If it is nil, no article is ignored.  Note that
1186 setting this variable to a number might prevent you from reading very
1187 old articles."
1188   :group 'gnus-group-select
1189   :version "22.2"
1190   :type '(choice (const :tag "No limit" nil)
1191                  integer))
1192
1193 (gnus-define-group-parameter
1194  ignored-charsets
1195  :type list
1196  :function-document
1197  "Return the ignored charsets of GROUP."
1198  :variable gnus-group-ignored-charsets-alist
1199  :variable-default
1200  '(("alt\\.chinese\\.text" iso-8859-1))
1201  :variable-document
1202  "Alist of regexps (to match group names) and charsets that should be ignored.
1203 When these charsets are used in the \"charset\" parameter, the
1204 default charset will be used instead."
1205  :variable-group gnus-charset
1206  :variable-type '(repeat (cons (regexp :tag "Group")
1207                                (repeat symbol)))
1208  :parameter-type '(choice :tag "Ignored charsets"
1209                           :value nil
1210                           (repeat (symbol)))
1211  :parameter-document       "\
1212 List of charsets that should be ignored.
1213
1214 When these charsets are used in the \"charset\" parameter, the
1215 default charset will be used instead.")
1216
1217 (defcustom gnus-group-highlight-words-alist nil
1218   "Alist of group regexps and highlight regexps.
1219 This variable uses the same syntax as `gnus-emphasis-alist'."
1220   :version "21.1"
1221   :type '(repeat (cons (regexp :tag "Group")
1222                        (repeat (list (regexp :tag "Highlight regexp")
1223                                      (number :tag "Group for entire word" 0)
1224                                      (number :tag "Group for displayed part" 0)
1225                                      (symbol :tag "Face"
1226                                              gnus-emphasis-highlight-words)))))
1227   :group 'gnus-summary-visual)
1228
1229 (defcustom gnus-summary-show-article-charset-alist
1230   nil
1231   "Alist of number and charset.
1232 The article will be shown with the charset corresponding to the
1233 numbered argument.
1234 For example: ((1 . cn-gb-2312) (2 . big5))."
1235   :version "21.1"
1236   :type '(repeat (cons (number :tag "Argument" 1)
1237                        (symbol :tag "Charset")))
1238   :group 'gnus-charset)
1239
1240 (defcustom gnus-preserve-marks t
1241   "Whether marks are preserved when moving, copying and respooling messages."
1242   :version "21.1"
1243   :type 'boolean
1244   :group 'gnus-summary-marks)
1245
1246 (defcustom gnus-propagate-marks nil
1247   "If non-nil, Gnus will store and retrieve marks from the backends.
1248 This means that marks will be stored both in .newsrc.eld and in
1249 the backend, and will slow operation down somewhat."
1250   :type 'boolean
1251   :group 'gnus-summary-marks)
1252
1253 (defcustom gnus-alter-articles-to-read-function nil
1254   "Function to be called to alter the list of articles to be selected."
1255   :type '(choice (const nil) function)
1256   :group 'gnus-summary)
1257
1258 (defcustom gnus-orphan-score nil
1259   "*All orphans get this score added.  Set in the score file."
1260   :group 'gnus-score-default
1261   :type '(choice (const nil)
1262                  integer))
1263
1264 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1265   "*A regexp to match MIME parts when saving multiple parts of a
1266 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1267 This regexp will be used by default when prompting the user for which
1268 type of files to save."
1269   :group 'gnus-summary
1270   :type 'regexp)
1271
1272 (defcustom gnus-read-all-available-headers nil
1273   "Whether Gnus should parse all headers made available to it.
1274 This is mostly relevant for slow back ends where the user may
1275 wish to widen the summary buffer to include all headers
1276 that were fetched."
1277   :version "22.1"
1278   :group 'gnus-summary
1279   :type '(choice boolean regexp))
1280
1281 (defcustom gnus-summary-pipe-output-default-command nil
1282   "Command (and optional arguments) used to pipe article to subprocess.
1283 This will be used as the default command if it is non-nil.  The value
1284 will be updated if you modify it when executing the command
1285 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1286   :version "23.1" ;; No Gnus
1287   :group 'gnus-summary
1288   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1289
1290 (defcustom gnus-summary-muttprint-program "muttprint"
1291   "Command (and optional arguments) used to run Muttprint.
1292 The value will be updated if you modify it when executing the command
1293 `gnus-summary-muttprint'."
1294   :version "22.1"
1295   :group 'gnus-summary
1296   :type 'string)
1297
1298 (defcustom gnus-article-loose-mime t
1299   "If non-nil, don't require MIME-Version header.
1300 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1301 supply the MIME-Version header or deliberately strip it from the mail.
1302 If non-nil (the default), Gnus will treat some articles as MIME
1303 even if the MIME-Version header is missing."
1304   :version "22.1"
1305   :type 'boolean
1306   :group 'gnus-article-mime)
1307
1308 (defcustom gnus-article-emulate-mime t
1309   "If non-nil, use MIME emulation for uuencode and the like.
1310 This means that Gnus will search message bodies for text that look
1311 like uuencoded bits, yEncoded bits, and so on, and present that using
1312 the normal Gnus MIME machinery."
1313   :version "22.1"
1314   :type 'boolean
1315   :group 'gnus-article-mime)
1316
1317 ;;; Internal variables
1318
1319 (defvar gnus-summary-display-cache nil)
1320 (defvar gnus-article-mime-handles nil)
1321 (defvar gnus-article-decoded-p nil)
1322 (defvar gnus-article-charset nil)
1323 (defvar gnus-article-ignored-charsets nil)
1324 (defvar gnus-scores-exclude-files nil)
1325 (defvar gnus-page-broken nil)
1326
1327 (defvar gnus-original-article nil)
1328 (defvar gnus-article-internal-prepare-hook nil)
1329 (defvar gnus-newsgroup-process-stack nil)
1330
1331 (defvar gnus-thread-indent-array nil)
1332 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1333 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1334   "Function called to sort the articles within a thread after it has been gathered together.")
1335
1336 (defvar gnus-summary-save-parts-type-history nil)
1337 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1338
1339 ;; Avoid highlighting in kill files.
1340 (defvar gnus-summary-inhibit-highlight nil)
1341 (defvar gnus-newsgroup-selected-overlay nil)
1342 (defvar gnus-inhibit-limiting nil)
1343 (defvar gnus-newsgroup-adaptive-score-file nil)
1344 (defvar gnus-current-score-file nil)
1345 (defvar gnus-current-move-group nil)
1346 (defvar gnus-current-copy-group nil)
1347 (defvar gnus-current-crosspost-group nil)
1348 (defvar gnus-newsgroup-display nil)
1349
1350 (defvar gnus-newsgroup-dependencies nil)
1351 (defvar gnus-newsgroup-adaptive nil)
1352 (defvar gnus-summary-display-article-function nil)
1353 (defvar gnus-summary-highlight-line-function nil
1354   "Function called after highlighting a summary line.")
1355
1356 (defvar gnus-summary-line-format-alist
1357   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1358     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1359     (?s gnus-tmp-subject-or-nil ?s)
1360     (?n gnus-tmp-name ?s)
1361     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1362         ?s)
1363     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1364             gnus-tmp-from) ?s)
1365     (?F gnus-tmp-from ?s)
1366     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1367     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1368     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1369     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1370     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1371     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1372     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1373     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1374     (?L gnus-tmp-lines ?s)
1375     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1376             0) ?d)
1377     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1378             "") ?s)
1379     (?g (or (gnus-group-short-name
1380              (nnir-article-group (mail-header-number gnus-tmp-header)))
1381             "") ?s)
1382     (?O gnus-tmp-downloaded ?c)
1383     (?I gnus-tmp-indentation ?s)
1384     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1385     (?R gnus-tmp-replied ?c)
1386     (?\[ gnus-tmp-opening-bracket ?c)
1387     (?\] gnus-tmp-closing-bracket ?c)
1388     (?\> (make-string gnus-tmp-level ? ) ?s)
1389     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1390     (?i gnus-tmp-score ?d)
1391     (?z gnus-tmp-score-char ?c)
1392     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1393     (?U gnus-tmp-unread ?c)
1394     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1395         ?s)
1396     (?t (gnus-summary-number-of-articles-in-thread
1397          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1398         ?d)
1399     (?e (gnus-summary-number-of-articles-in-thread
1400          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1401         ?c)
1402     (?u gnus-tmp-user-defined ?s)
1403     (?P (gnus-pick-line-number) ?d)
1404     (?B gnus-tmp-thread-tree-header-string ?s)
1405     (user-date (gnus-user-date
1406                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1407   "An alist of format specifications that can appear in summary lines.
1408 These are paired with what variables they correspond with, along with
1409 the type of the variable (string, integer, character, etc).")
1410
1411 (defvar gnus-summary-dummy-line-format-alist
1412   `((?S gnus-tmp-subject ?s)
1413     (?N gnus-tmp-number ?d)
1414     (?u gnus-tmp-user-defined ?s)))
1415
1416 (defvar gnus-summary-mode-line-format-alist
1417   `((?G gnus-tmp-group-name ?s)
1418     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1419     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1420     (?A gnus-tmp-article-number ?d)
1421     (?Z gnus-tmp-unread-and-unselected ?s)
1422     (?V gnus-version ?s)
1423     (?U gnus-tmp-unread-and-unticked ?d)
1424     (?S gnus-tmp-subject ?s)
1425     (?e gnus-tmp-unselected ?d)
1426     (?u gnus-tmp-user-defined ?s)
1427     (?d (length gnus-newsgroup-dormant) ?d)
1428     (?t (length gnus-newsgroup-marked) ?d)
1429     (?h (length gnus-newsgroup-spam-marked) ?d)
1430     (?r (length gnus-newsgroup-reads) ?d)
1431     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1432     (?E gnus-newsgroup-expunged-tally ?d)
1433     (?s (gnus-current-score-file-nondirectory) ?s)))
1434
1435 ;; This is here rather than in gnus-art for compilation reasons.
1436 (defvar gnus-article-mode-line-format-alist
1437   (nconc '((?w (gnus-article-wash-status) ?s)
1438            (?m (gnus-article-mime-part-status) ?s))
1439          gnus-summary-mode-line-format-alist))
1440
1441 (defvar gnus-last-search-regexp nil
1442   "Default regexp for article search command.")
1443
1444 (defvar gnus-last-shell-command nil
1445   "Default shell command on article.")
1446
1447 (defvar gnus-newsgroup-agentized nil
1448   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1449 (defvar gnus-newsgroup-begin nil)
1450 (defvar gnus-newsgroup-end nil)
1451 (defvar gnus-newsgroup-last-rmail nil)
1452 (defvar gnus-newsgroup-last-mail nil)
1453 (defvar gnus-newsgroup-last-folder nil)
1454 (defvar gnus-newsgroup-last-file nil)
1455 (defvar gnus-newsgroup-last-directory nil)
1456 (defvar gnus-newsgroup-auto-expire nil)
1457 (defvar gnus-newsgroup-active nil)
1458 (defvar gnus-newsgroup-highest nil)
1459
1460 (defvar gnus-newsgroup-data nil)
1461 (defvar gnus-newsgroup-data-reverse nil)
1462 (defvar gnus-newsgroup-limit nil)
1463 (defvar gnus-newsgroup-limits nil)
1464 (defvar gnus-summary-use-undownloaded-faces nil)
1465
1466 (defvar gnus-newsgroup-unreads nil
1467   "Sorted list of unread articles in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-unselected nil
1470   "Sorted list of unselected unread articles in the current newsgroup.")
1471
1472 (defvar gnus-newsgroup-reads nil
1473   "Alist of read articles and article marks in the current newsgroup.")
1474
1475 (defvar gnus-newsgroup-expunged-tally nil)
1476
1477 (defvar gnus-newsgroup-marked nil
1478   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1479
1480 (defvar gnus-newsgroup-spam-marked nil
1481   "List of ranges of articles that have been marked as spam.")
1482
1483 (defvar gnus-newsgroup-killed nil
1484   "List of ranges of articles that have been through the scoring process.")
1485
1486 (defvar gnus-newsgroup-cached nil
1487   "Sorted list of articles that come from the article cache.")
1488
1489 (defvar gnus-newsgroup-saved nil
1490   "List of articles that have been saved.")
1491
1492 (defvar gnus-newsgroup-kill-headers nil)
1493
1494 (defvar gnus-newsgroup-replied nil
1495   "List of articles that have been replied to in the current newsgroup.")
1496
1497 (defvar gnus-newsgroup-forwarded nil
1498   "List of articles that have been forwarded in the current newsgroup.")
1499
1500 (defvar gnus-newsgroup-expirable nil
1501   "Sorted list of articles in the current newsgroup that can be expired.")
1502
1503 (defvar gnus-newsgroup-processable nil
1504   "List of articles in the current newsgroup that can be processed.")
1505
1506 (defvar gnus-newsgroup-downloadable nil
1507   "Sorted list of articles in the current newsgroup that can be processed.")
1508
1509 (defvar gnus-newsgroup-unfetched nil
1510   "Sorted list of articles in the current newsgroup whose headers have
1511 not been fetched into the agent.
1512
1513 This list will always be a subset of gnus-newsgroup-undownloaded.")
1514
1515 (defvar gnus-newsgroup-undownloaded nil
1516   "List of articles in the current newsgroup that haven't been downloaded.")
1517
1518 (defvar gnus-newsgroup-unsendable nil
1519   "List of articles in the current newsgroup that won't be sent.")
1520
1521 (defvar gnus-newsgroup-bookmarks nil
1522   "List of articles in the current newsgroup that have bookmarks.")
1523
1524 (defvar gnus-newsgroup-dormant nil
1525   "Sorted list of dormant articles in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-unseen nil
1528   "List of unseen articles in the current newsgroup.")
1529
1530 (defvar gnus-newsgroup-seen nil
1531   "Range of seen articles in the current newsgroup.")
1532
1533 (defvar gnus-newsgroup-articles nil
1534   "List of articles in the current newsgroup.")
1535
1536 (defvar gnus-newsgroup-scored nil
1537   "List of scored articles in the current newsgroup.")
1538
1539 (defvar gnus-newsgroup-headers nil
1540   "List of article headers in the current newsgroup.")
1541
1542 (defvar gnus-newsgroup-threads nil)
1543
1544 (defvar gnus-newsgroup-prepared nil
1545   "Whether the current group has been prepared properly.")
1546
1547 (defvar gnus-newsgroup-ancient nil
1548   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1549
1550 (defvar gnus-newsgroup-sparse nil)
1551
1552 (defvar gnus-current-article nil)
1553 (defvar gnus-article-current nil)
1554 (defvar gnus-current-headers nil)
1555 (defvar gnus-have-all-headers nil)
1556 (defvar gnus-last-article nil)
1557 (defvar gnus-newsgroup-history nil)
1558 (defvar gnus-newsgroup-charset nil)
1559 (defvar gnus-newsgroup-ephemeral-charset nil)
1560 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1561
1562 (defvar gnus-article-before-search nil)
1563
1564 (defvar gnus-summary-local-variables
1565   '(gnus-newsgroup-name
1566
1567     ;; Marks lists
1568     gnus-newsgroup-unreads
1569     gnus-newsgroup-unselected
1570     gnus-newsgroup-marked
1571     gnus-newsgroup-spam-marked
1572     gnus-newsgroup-reads
1573     gnus-newsgroup-saved
1574     gnus-newsgroup-replied
1575     gnus-newsgroup-forwarded
1576     gnus-newsgroup-expirable
1577     gnus-newsgroup-killed
1578     gnus-newsgroup-unseen
1579     gnus-newsgroup-seen
1580     gnus-newsgroup-cached
1581     gnus-newsgroup-downloadable
1582     gnus-newsgroup-undownloaded
1583     gnus-newsgroup-unsendable
1584
1585     gnus-newsgroup-begin gnus-newsgroup-end
1586     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1587     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1588     gnus-newsgroup-last-directory
1589     gnus-newsgroup-auto-expire
1590     gnus-newsgroup-processable
1591     gnus-newsgroup-unfetched
1592     gnus-newsgroup-articles
1593     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1594     gnus-newsgroup-headers gnus-newsgroup-threads
1595     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1596     gnus-current-article gnus-current-headers gnus-have-all-headers
1597     gnus-last-article gnus-article-internal-prepare-hook
1598     (gnus-summary-article-delete-hook . global)
1599     (gnus-summary-article-move-hook . global)
1600     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1601     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1602     gnus-thread-expunge-below
1603     gnus-score-alist gnus-current-score-file
1604     (gnus-summary-expunge-below . global)
1605     (gnus-summary-mark-below . global)
1606     (gnus-orphan-score . global)
1607     gnus-newsgroup-active gnus-scores-exclude-files
1608     gnus-newsgroup-highest
1609     gnus-newsgroup-history gnus-newsgroup-ancient
1610     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1611     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1612     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1613     (gnus-newsgroup-expunged-tally . 0)
1614     gnus-cache-removable-articles
1615     gnus-newsgroup-data gnus-newsgroup-data-reverse
1616     gnus-newsgroup-limit gnus-newsgroup-limits
1617     gnus-newsgroup-charset gnus-newsgroup-display
1618     gnus-summary-use-undownloaded-faces)
1619   "Variables that are buffer-local to the summary buffers.")
1620
1621 (defvar gnus-newsgroup-variables nil
1622   "A list of variables that have separate values in different newsgroups.
1623 A list of newsgroup (summary buffer) local variables, or cons of
1624 variables and their default expressions to be evalled (when the default
1625 values are not nil), that should be made global while the summary buffer
1626 is active.
1627
1628 Note: The default expressions will be evaluated (using function `eval')
1629 before assignment to the local variable rather than just assigned to it.
1630 If the default expression is the symbol `global', that symbol will not
1631 be evaluated but the global value of the local variable will be used
1632 instead.
1633
1634 These variables can be used to set variables in the group parameters
1635 while still allowing them to affect operations done in other buffers.
1636 For example:
1637
1638 \(setq gnus-newsgroup-variables
1639      '(message-use-followup-to
1640        (gnus-visible-headers .
1641          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1642 ")
1643
1644 (eval-when-compile
1645   ;; Bind features so that require will believe that gnus-sum has
1646   ;; already been loaded (avoids infinite recursion)
1647   (let ((features (cons 'gnus-sum features)))
1648     (require 'gnus-art)))
1649
1650 ;; MIME stuff.
1651
1652 (defvar gnus-decode-encoded-word-methods
1653   '(mail-decode-encoded-word-string)
1654   "List of methods used to decode encoded words.
1655
1656 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1657 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1658 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1659 whose names match REGEXP.
1660
1661 For example:
1662 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1663  mail-decode-encoded-word-string
1664  (\"chinese\" . rfc1843-decode-string))")
1665
1666 (defvar gnus-decode-encoded-word-methods-cache nil)
1667
1668 (defun gnus-multi-decode-encoded-word-string (string)
1669   "Apply the functions from `gnus-encoded-word-methods' that match."
1670   (unless (and gnus-decode-encoded-word-methods-cache
1671                (eq gnus-newsgroup-name
1672                    (car gnus-decode-encoded-word-methods-cache)))
1673     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1674     (dolist (method gnus-decode-encoded-word-methods)
1675       (if (symbolp method)
1676           (nconc gnus-decode-encoded-word-methods-cache (list method))
1677         (if (and gnus-newsgroup-name
1678                  (string-match (car method) gnus-newsgroup-name))
1679             (nconc gnus-decode-encoded-word-methods-cache
1680                    (list (cdr method)))))))
1681   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1682     (setq string (funcall method string))))
1683
1684 ;; Subject simplification.
1685
1686 (defun gnus-simplify-whitespace (str)
1687   "Remove excessive whitespace from STR."
1688   ;; Multiple spaces.
1689   (while (string-match "[ \t][ \t]+" str)
1690     (setq str (concat (substring str 0 (match-beginning 0))
1691                         " "
1692                         (substring str (match-end 0)))))
1693   ;; Leading spaces.
1694   (when (string-match "^[ \t]+" str)
1695     (setq str (substring str (match-end 0))))
1696   ;; Trailing spaces.
1697   (when (string-match "[ \t]+$" str)
1698     (setq str (substring str 0 (match-beginning 0))))
1699   str)
1700
1701 (defun gnus-simplify-all-whitespace (str)
1702   "Remove all whitespace from STR."
1703   (while (string-match "[ \t\n]+" str)
1704     (setq str (replace-match "" nil nil str)))
1705   str)
1706
1707 (defsubst gnus-simplify-subject-re (subject)
1708   "Remove \"Re:\" from subject lines."
1709   (if (string-match message-subject-re-regexp subject)
1710       (substring subject (match-end 0))
1711     subject))
1712
1713 (defun gnus-simplify-subject (subject &optional re-only)
1714   "Remove `Re:' and words in parentheses.
1715 If RE-ONLY is non-nil, strip leading `Re:'s only."
1716   (let ((case-fold-search t))           ;Ignore case.
1717     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1718     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1719       (setq subject (substring subject (match-end 0))))
1720     ;; Remove uninteresting prefixes.
1721     (when (and (not re-only)
1722                gnus-simplify-ignored-prefixes
1723                (string-match gnus-simplify-ignored-prefixes subject))
1724       (setq subject (substring subject (match-end 0))))
1725     ;; Remove words in parentheses from end.
1726     (unless re-only
1727       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1728         (setq subject (substring subject 0 (match-beginning 0)))))
1729     ;; Return subject string.
1730     subject))
1731
1732 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1733 ;; all whitespace.
1734 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1735   (goto-char (point-min))
1736   (while (re-search-forward regexp nil t)
1737     (replace-match (or newtext ""))))
1738
1739 (defun gnus-simplify-buffer-fuzzy (regexp)
1740   "Simplify string in the buffer fuzzily.
1741 The string in the accessible portion of the current buffer is simplified.
1742 It is assumed to be a single-line subject.
1743 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1744 matter is removed.  Additional things can be deleted by setting
1745 `gnus-simplify-subject-fuzzy-regexp'."
1746   (let ((case-fold-search t)
1747         (modified-tick))
1748     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1749
1750     (while (not (eq modified-tick (buffer-modified-tick)))
1751       (setq modified-tick (buffer-modified-tick))
1752       (cond
1753        ((listp regexp)
1754         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1755        (regexp
1756         (gnus-simplify-buffer-fuzzy-step regexp)))
1757       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1758       (gnus-simplify-buffer-fuzzy-step
1759        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1760       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1761
1762     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1763     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1764     (gnus-simplify-buffer-fuzzy-step " $")
1765     (gnus-simplify-buffer-fuzzy-step "^ +")))
1766
1767 (defun gnus-simplify-subject-fuzzy (subject)
1768   "Simplify a subject string fuzzily.
1769 See `gnus-simplify-buffer-fuzzy' for details."
1770   (save-excursion
1771     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1772       (gnus-set-work-buffer)
1773       (let ((case-fold-search t))
1774         ;; Remove uninteresting prefixes.
1775         (when (and gnus-simplify-ignored-prefixes
1776                    (string-match gnus-simplify-ignored-prefixes subject))
1777           (setq subject (substring subject (match-end 0))))
1778         (insert subject)
1779         (inline (gnus-simplify-buffer-fuzzy regexp))
1780         (buffer-string)))))
1781
1782 (defsubst gnus-simplify-subject-fully (subject)
1783   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1784   (cond
1785    (gnus-simplify-subject-functions
1786     (gnus-map-function gnus-simplify-subject-functions subject))
1787    ((null gnus-summary-gather-subject-limit)
1788     (gnus-simplify-subject-re subject))
1789    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1790     (gnus-simplify-subject-fuzzy subject))
1791    ((numberp gnus-summary-gather-subject-limit)
1792     (truncate-string-to-width (gnus-simplify-subject-re subject)
1793                               gnus-summary-gather-subject-limit))
1794    (t
1795     subject)))
1796
1797 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1798   "Check whether two subjects are equal.
1799 If optional argument SIMPLE-FIRST is t, first argument is already
1800 simplified."
1801   (cond
1802    ((null simple-first)
1803     (equal (gnus-simplify-subject-fully s1)
1804            (gnus-simplify-subject-fully s2)))
1805    (t
1806     (equal s1
1807            (gnus-simplify-subject-fully s2)))))
1808
1809 (defun gnus-summary-bubble-group ()
1810   "Increase the score of the current group.
1811 This is a handy function to add to `gnus-summary-exit-hook' to
1812 increase the score of each group you read."
1813   (gnus-group-add-score gnus-newsgroup-name))
1814
1815 \f
1816 ;;;
1817 ;;; Gnus summary mode
1818 ;;;
1819
1820 (put 'gnus-summary-mode 'mode-class 'special)
1821
1822 (defvar gnus-article-commands-menu)
1823
1824 ;; Non-orthogonal keys
1825
1826 (gnus-define-keys gnus-summary-mode-map
1827   " " gnus-summary-next-page
1828   "\177" gnus-summary-prev-page
1829   [delete] gnus-summary-prev-page
1830   [backspace] gnus-summary-prev-page
1831   "\r" gnus-summary-scroll-up
1832   "\M-\r" gnus-summary-scroll-down
1833   "n" gnus-summary-next-unread-article
1834   "p" gnus-summary-prev-unread-article
1835   "N" gnus-summary-next-article
1836   "P" gnus-summary-prev-article
1837   "\M-\C-n" gnus-summary-next-same-subject
1838   "\M-\C-p" gnus-summary-prev-same-subject
1839   "\M-n" gnus-summary-next-unread-subject
1840   "\M-p" gnus-summary-prev-unread-subject
1841   "." gnus-summary-first-unread-article
1842   "," gnus-summary-best-unread-article
1843   "\M-s" gnus-summary-search-article-forward
1844   "\M-r" gnus-summary-search-article-backward
1845   "\M-S" gnus-summary-repeat-search-article-forward
1846   "\M-R" gnus-summary-repeat-search-article-backward
1847   "<" gnus-summary-beginning-of-article
1848   ">" gnus-summary-end-of-article
1849   "j" gnus-summary-goto-article
1850   "^" gnus-summary-refer-parent-article
1851   "\M-^" gnus-summary-refer-article
1852   "u" gnus-summary-tick-article-forward
1853   "!" gnus-summary-tick-article-forward
1854   "U" gnus-summary-tick-article-backward
1855   "d" gnus-summary-mark-as-read-forward
1856   "D" gnus-summary-mark-as-read-backward
1857   "E" gnus-summary-mark-as-expirable
1858   "\M-u" gnus-summary-clear-mark-forward
1859   "\M-U" gnus-summary-clear-mark-backward
1860   "k" gnus-summary-kill-same-subject-and-select
1861   "\C-k" gnus-summary-kill-same-subject
1862   "\M-\C-k" gnus-summary-kill-thread
1863   "\M-\C-l" gnus-summary-lower-thread
1864   "e" gnus-summary-edit-article
1865   "#" gnus-summary-mark-as-processable
1866   "\M-#" gnus-summary-unmark-as-processable
1867   "\M-\C-t" gnus-summary-toggle-threads
1868   "\M-\C-s" gnus-summary-show-thread
1869   "\M-\C-h" gnus-summary-hide-thread
1870   "\M-\C-f" gnus-summary-next-thread
1871   "\M-\C-b" gnus-summary-prev-thread
1872   [(meta down)] gnus-summary-next-thread
1873   [(meta up)] gnus-summary-prev-thread
1874   "\M-\C-u" gnus-summary-up-thread
1875   "\M-\C-d" gnus-summary-down-thread
1876   "&" gnus-summary-execute-command
1877   "c" gnus-summary-catchup-and-exit
1878   "\C-w" gnus-summary-mark-region-as-read
1879   "\C-t" gnus-summary-toggle-truncation
1880   "?" gnus-summary-mark-as-dormant
1881   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1882   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1883   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1884   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1885   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1886   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1887   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1888   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1889   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1890   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1891   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1892   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1893   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1894   "=" gnus-summary-expand-window
1895   "\C-x\C-s" gnus-summary-reselect-current-group
1896   "\M-g" gnus-summary-rescan-group
1897   "\C-c\C-r" gnus-summary-caesar-message
1898   "f" gnus-summary-followup
1899   "F" gnus-summary-followup-with-original
1900   "C" gnus-summary-cancel-article
1901   "r" gnus-summary-reply
1902   "R" gnus-summary-reply-with-original
1903   "\C-c\C-f" gnus-summary-mail-forward
1904   "o" gnus-summary-save-article
1905   "\C-o" gnus-summary-save-article-mail
1906   "|" gnus-summary-pipe-output
1907   "\M-k" gnus-summary-edit-local-kill
1908   "\M-K" gnus-summary-edit-global-kill
1909   ;; "V" gnus-version
1910   "\C-c\C-d" gnus-summary-describe-group
1911   "q" gnus-summary-exit
1912   "Q" gnus-summary-exit-no-update
1913   "\C-c\C-i" gnus-info-find-node
1914   gnus-mouse-2 gnus-mouse-pick-article
1915   [follow-link] mouse-face
1916   "m" gnus-summary-mail-other-window
1917   "a" gnus-summary-post-news
1918   "x" gnus-summary-limit-to-unread
1919   "s" gnus-summary-isearch-article
1920   [tab] gnus-summary-widget-forward
1921   "t" gnus-summary-toggle-header
1922   "g" gnus-summary-show-article
1923   "l" gnus-summary-goto-last-article
1924   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1925   "\C-d" gnus-summary-enter-digest-group
1926   "\M-\C-d" gnus-summary-read-document
1927   "\M-\C-e" gnus-summary-edit-parameters
1928   "\M-\C-a" gnus-summary-customize-parameters
1929   "\C-c\C-b" gnus-bug
1930   "*" gnus-cache-enter-article
1931   "\M-*" gnus-cache-remove-article
1932   "\M-&" gnus-summary-universal-argument
1933   "\C-l" gnus-recenter
1934   "I" gnus-summary-increase-score
1935   "L" gnus-summary-lower-score
1936   "\M-i" gnus-symbolic-argument
1937   "h" gnus-summary-select-article-buffer
1938
1939   "b" gnus-article-view-part
1940   "\M-t" gnus-summary-toggle-display-buttonized
1941
1942   "V" gnus-summary-score-map
1943   "X" gnus-uu-extract-map
1944   "S" gnus-summary-send-map)
1945
1946 ;; Sort of orthogonal keymap
1947 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1948   "t" gnus-summary-tick-article-forward
1949   "!" gnus-summary-tick-article-forward
1950   "d" gnus-summary-mark-as-read-forward
1951   "r" gnus-summary-mark-as-read-forward
1952   "c" gnus-summary-clear-mark-forward
1953   " " gnus-summary-clear-mark-forward
1954   "e" gnus-summary-mark-as-expirable
1955   "x" gnus-summary-mark-as-expirable
1956   "?" gnus-summary-mark-as-dormant
1957   "b" gnus-summary-set-bookmark
1958   "B" gnus-summary-remove-bookmark
1959   "#" gnus-summary-mark-as-processable
1960   "\M-#" gnus-summary-unmark-as-processable
1961   "S" gnus-summary-limit-include-expunged
1962   "C" gnus-summary-catchup
1963   "H" gnus-summary-catchup-to-here
1964   "h" gnus-summary-catchup-from-here
1965   "\C-c" gnus-summary-catchup-all
1966   "k" gnus-summary-kill-same-subject-and-select
1967   "K" gnus-summary-kill-same-subject
1968   "P" gnus-uu-mark-map)
1969
1970 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1971   "c" gnus-summary-clear-above
1972   "u" gnus-summary-tick-above
1973   "m" gnus-summary-mark-above
1974   "k" gnus-summary-kill-below)
1975
1976 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1977   "/" gnus-summary-limit-to-subject
1978   "n" gnus-summary-limit-to-articles
1979   "b" gnus-summary-limit-to-bodies
1980   "h" gnus-summary-limit-to-headers
1981   "w" gnus-summary-pop-limit
1982   "s" gnus-summary-limit-to-subject
1983   "a" gnus-summary-limit-to-author
1984   "u" gnus-summary-limit-to-unread
1985   "m" gnus-summary-limit-to-marks
1986   "M" gnus-summary-limit-exclude-marks
1987   "v" gnus-summary-limit-to-score
1988   "*" gnus-summary-limit-include-cached
1989   "D" gnus-summary-limit-include-dormant
1990   "T" gnus-summary-limit-include-thread
1991   "d" gnus-summary-limit-exclude-dormant
1992   "t" gnus-summary-limit-to-age
1993   "." gnus-summary-limit-to-unseen
1994   "x" gnus-summary-limit-to-extra
1995   "p" gnus-summary-limit-to-display-predicate
1996   "E" gnus-summary-limit-include-expunged
1997   "c" gnus-summary-limit-exclude-childless-dormant
1998   "C" gnus-summary-limit-mark-excluded-as-read
1999   "o" gnus-summary-insert-old-articles
2000   "N" gnus-summary-insert-new-articles
2001   "S" gnus-summary-limit-to-singletons
2002   "r" gnus-summary-limit-to-replied
2003   "R" gnus-summary-limit-to-recipient
2004   "A" gnus-summary-limit-to-address)
2005
2006 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2007   "n" gnus-summary-next-unread-article
2008   "p" gnus-summary-prev-unread-article
2009   "N" gnus-summary-next-article
2010   "P" gnus-summary-prev-article
2011   "\C-n" gnus-summary-next-same-subject
2012   "\C-p" gnus-summary-prev-same-subject
2013   "\M-n" gnus-summary-next-unread-subject
2014   "\M-p" gnus-summary-prev-unread-subject
2015   "f" gnus-summary-first-unread-article
2016   "b" gnus-summary-best-unread-article
2017   "j" gnus-summary-goto-article
2018   "g" gnus-summary-goto-subject
2019   "l" gnus-summary-goto-last-article
2020   "o" gnus-summary-pop-article)
2021
2022 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2023   "k" gnus-summary-kill-thread
2024   "E" gnus-summary-expire-thread
2025   "l" gnus-summary-lower-thread
2026   "i" gnus-summary-raise-thread
2027   "T" gnus-summary-toggle-threads
2028   "t" gnus-summary-rethread-current
2029   "^" gnus-summary-reparent-thread
2030   "\M-^" gnus-summary-reparent-children
2031   "s" gnus-summary-show-thread
2032   "S" gnus-summary-show-all-threads
2033   "h" gnus-summary-hide-thread
2034   "H" gnus-summary-hide-all-threads
2035   "n" gnus-summary-next-thread
2036   "p" gnus-summary-prev-thread
2037   "u" gnus-summary-up-thread
2038   "o" gnus-summary-top-thread
2039   "d" gnus-summary-down-thread
2040   "#" gnus-uu-mark-thread
2041   "\M-#" gnus-uu-unmark-thread)
2042
2043 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2044   "g" gnus-summary-prepare
2045   "c" gnus-summary-insert-cached-articles
2046   "d" gnus-summary-insert-dormant-articles
2047   "t" gnus-summary-insert-ticked-articles)
2048
2049 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2050   "c" gnus-summary-catchup-and-exit
2051   "C" gnus-summary-catchup-all-and-exit
2052   "E" gnus-summary-exit-no-update
2053   "Q" gnus-summary-exit
2054   "Z" gnus-summary-exit
2055   "n" gnus-summary-catchup-and-goto-next-group
2056   "p" gnus-summary-catchup-and-goto-prev-group
2057   "R" gnus-summary-reselect-current-group
2058   "G" gnus-summary-rescan-group
2059   "N" gnus-summary-next-group
2060   "s" gnus-summary-save-newsrc
2061   "P" gnus-summary-prev-group)
2062
2063 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2064   " " gnus-summary-next-page
2065   "n" gnus-summary-next-page
2066   "\177" gnus-summary-prev-page
2067   [delete] gnus-summary-prev-page
2068   "p" gnus-summary-prev-page
2069   "\r" gnus-summary-scroll-up
2070   "\M-\r" gnus-summary-scroll-down
2071   "<" gnus-summary-beginning-of-article
2072   ">" gnus-summary-end-of-article
2073   "b" gnus-summary-beginning-of-article
2074   "e" gnus-summary-end-of-article
2075   "^" gnus-summary-refer-parent-article
2076   "r" gnus-summary-refer-parent-article
2077   "C" gnus-summary-show-complete-article
2078   "D" gnus-summary-enter-digest-group
2079   "R" gnus-summary-refer-references
2080   "T" gnus-summary-refer-thread
2081   "W" gnus-warp-to-article
2082   "g" gnus-summary-show-article
2083   "s" gnus-summary-isearch-article
2084   [tab] gnus-summary-widget-forward
2085   "P" gnus-summary-print-article
2086   "S" gnus-sticky-article
2087   "M" gnus-mailing-list-insinuate
2088   "t" gnus-article-babel)
2089
2090 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2091   "b" gnus-article-add-buttons
2092   "B" gnus-article-add-buttons-to-head
2093   "o" gnus-article-treat-overstrike
2094   "e" gnus-article-emphasize
2095   "w" gnus-article-fill-cited-article
2096   "Q" gnus-article-fill-long-lines
2097   "L" gnus-article-toggle-truncate-lines
2098   "C" gnus-article-capitalize-sentences
2099   "c" gnus-article-remove-cr
2100   "q" gnus-article-de-quoted-unreadable
2101   "6" gnus-article-de-base64-unreadable
2102   "Z" gnus-article-decode-HZ
2103   "A" gnus-article-treat-ansi-sequences
2104   "h" gnus-article-wash-html
2105   "u" gnus-article-unsplit-urls
2106   "s" gnus-summary-force-verify-and-decrypt
2107   "f" gnus-article-display-x-face
2108   "l" gnus-summary-stop-page-breaking
2109   "r" gnus-summary-caesar-message
2110   "m" gnus-summary-morse-message
2111   "t" gnus-summary-toggle-header
2112   "g" gnus-treat-smiley
2113   "v" gnus-summary-verbose-headers
2114   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2115   "p" gnus-article-verify-x-pgp-sig
2116   "d" gnus-article-treat-dumbquotes
2117   "U" gnus-article-treat-non-ascii
2118   "i" gnus-summary-idna-message)
2119
2120 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2121   ;; mnemonic: deuglif*Y*
2122   "u" gnus-article-outlook-unwrap-lines
2123   "a" gnus-article-outlook-repair-attribution
2124   "c" gnus-article-outlook-rearrange-citation
2125   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2126
2127 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2128   "a" gnus-article-hide
2129   "h" gnus-article-hide-headers
2130   "b" gnus-article-hide-boring-headers
2131   "s" gnus-article-hide-signature
2132   "c" gnus-article-hide-citation
2133   "C" gnus-article-hide-citation-in-followups
2134   "l" gnus-article-hide-list-identifiers
2135   "B" gnus-article-strip-banner
2136   "P" gnus-article-hide-pem
2137   "\C-c" gnus-article-hide-citation-maybe)
2138
2139 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2140   "a" gnus-article-highlight
2141   "h" gnus-article-highlight-headers
2142   "c" gnus-article-highlight-citation
2143   "s" gnus-article-highlight-signature)
2144
2145 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2146   "f" gnus-article-treat-fold-headers
2147   "u" gnus-article-treat-unfold-headers
2148   "n" gnus-article-treat-fold-newsgroups)
2149
2150 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2151   "x" gnus-article-display-x-face
2152   "d" gnus-article-display-face
2153   "s" gnus-treat-smiley
2154   "D" gnus-article-remove-images
2155   "W" gnus-article-show-images
2156   "f" gnus-treat-from-picon
2157   "m" gnus-treat-mail-picon
2158   "n" gnus-treat-newsgroups-picon
2159   "g" gnus-treat-from-gravatar
2160   "h" gnus-treat-mail-gravatar)
2161
2162 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2163   "w" gnus-article-decode-mime-words
2164   "c" gnus-article-decode-charset
2165   "v" gnus-mime-view-all-parts
2166   "b" gnus-article-view-part)
2167
2168 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2169   "z" gnus-article-date-ut
2170   "u" gnus-article-date-ut
2171   "l" gnus-article-date-local
2172   "p" gnus-article-date-english
2173   "e" gnus-article-date-lapsed
2174   "o" gnus-article-date-original
2175   "i" gnus-article-date-iso8601
2176   "s" gnus-article-date-user)
2177
2178 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2179   "t" gnus-article-remove-trailing-blank-lines
2180   "l" gnus-article-strip-leading-blank-lines
2181   "m" gnus-article-strip-multiple-blank-lines
2182   "a" gnus-article-strip-blank-lines
2183   "A" gnus-article-strip-all-blank-lines
2184   "s" gnus-article-strip-leading-space
2185   "e" gnus-article-strip-trailing-space
2186   "w" gnus-article-remove-leading-whitespace)
2187
2188 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2189   "v" gnus-version
2190   "d" gnus-summary-describe-group
2191   "h" gnus-summary-describe-briefly
2192   "i" gnus-info-find-node)
2193
2194 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2195   "e" gnus-summary-expire-articles
2196   "\M-\C-e" gnus-summary-expire-articles-now
2197   "\177" gnus-summary-delete-article
2198   [delete] gnus-summary-delete-article
2199   [backspace] gnus-summary-delete-article
2200   "m" gnus-summary-move-article
2201   "r" gnus-summary-respool-article
2202   "w" gnus-summary-edit-article
2203   "c" gnus-summary-copy-article
2204   "B" gnus-summary-crosspost-article
2205   "q" gnus-summary-respool-query
2206   "t" gnus-summary-respool-trace
2207   "i" gnus-summary-import-article
2208   "I" gnus-summary-create-article
2209   "p" gnus-summary-article-posted-p)
2210
2211 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2212   "o" gnus-summary-save-article
2213   "m" gnus-summary-save-article-mail
2214   "F" gnus-summary-write-article-file
2215   "r" gnus-summary-save-article-rmail
2216   "f" gnus-summary-save-article-file
2217   "b" gnus-summary-save-article-body-file
2218   "B" gnus-summary-write-article-body-file
2219   "h" gnus-summary-save-article-folder
2220   "v" gnus-summary-save-article-vm
2221   "p" gnus-summary-pipe-output
2222   "P" gnus-summary-muttprint)
2223
2224 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2225   "b" gnus-summary-display-buttonized
2226   "m" gnus-summary-repair-multipart
2227   "v" gnus-article-view-part
2228   "o" gnus-article-save-part
2229   "O" gnus-article-save-part-and-strip
2230   "r" gnus-article-replace-part
2231   "d" gnus-article-delete-part
2232   "t" gnus-article-view-part-as-type
2233   "j" gnus-article-jump-to-part
2234   "c" gnus-article-copy-part
2235   "C" gnus-article-view-part-as-charset
2236   "e" gnus-article-view-part-externally
2237   "H" gnus-article-browse-html-article
2238   "E" gnus-article-encrypt-body
2239   "i" gnus-article-inline-part
2240   "|" gnus-article-pipe-part)
2241
2242 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2243   "p" gnus-summary-mark-as-processable
2244   "u" gnus-summary-unmark-as-processable
2245   "U" gnus-summary-unmark-all-processable
2246   "v" gnus-uu-mark-over
2247   "s" gnus-uu-mark-series
2248   "r" gnus-uu-mark-region
2249   "g" gnus-uu-unmark-region
2250   "R" gnus-uu-mark-by-regexp
2251   "G" gnus-uu-unmark-by-regexp
2252   "t" gnus-uu-mark-thread
2253   "T" gnus-uu-unmark-thread
2254   "a" gnus-uu-mark-all
2255   "b" gnus-uu-mark-buffer
2256   "S" gnus-uu-mark-sparse
2257   "k" gnus-summary-kill-process-mark
2258   "y" gnus-summary-yank-process-mark
2259   "w" gnus-summary-save-process-mark
2260   "i" gnus-uu-invert-processable)
2261
2262 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2263   ;;"x" gnus-uu-extract-any
2264   "m" gnus-summary-save-parts
2265   "u" gnus-uu-decode-uu
2266   "U" gnus-uu-decode-uu-and-save
2267   "s" gnus-uu-decode-unshar
2268   "S" gnus-uu-decode-unshar-and-save
2269   "o" gnus-uu-decode-save
2270   "O" gnus-uu-decode-save
2271   "b" gnus-uu-decode-binhex
2272   "B" gnus-uu-decode-binhex
2273   "Y" gnus-uu-decode-yenc
2274   "p" gnus-uu-decode-postscript
2275   "P" gnus-uu-decode-postscript-and-save)
2276
2277 (gnus-define-keys
2278     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2279   "u" gnus-uu-decode-uu-view
2280   "U" gnus-uu-decode-uu-and-save-view
2281   "s" gnus-uu-decode-unshar-view
2282   "S" gnus-uu-decode-unshar-and-save-view
2283   "o" gnus-uu-decode-save-view
2284   "O" gnus-uu-decode-save-view
2285   "b" gnus-uu-decode-binhex-view
2286   "B" gnus-uu-decode-binhex-view
2287   "p" gnus-uu-decode-postscript-view
2288   "P" gnus-uu-decode-postscript-and-save-view)
2289
2290 (defvar gnus-article-post-menu nil)
2291
2292 (defconst gnus-summary-menu-maxlen 20)
2293
2294 (defun gnus-summary-menu-split (menu)
2295   ;; If we have lots of elements, divide them into groups of 20
2296   ;; and make a pane (or submenu) for each one.
2297   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2298       (let ((menu menu) sublists next
2299             (i 1))
2300         (while menu
2301           ;; Pull off the next gnus-summary-menu-maxlen elements
2302           ;; and make them the next element of sublist.
2303           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2304           (if next
2305               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2306                       nil))
2307           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2308                                              (aref (car (last menu)) 0)) menu)
2309                                sublists))
2310           (setq i (1+ i))
2311           (setq menu next))
2312         (nreverse sublists))
2313     ;; Few elements--put them all in one pane.
2314     menu))
2315
2316 (defun gnus-summary-make-menu-bar ()
2317   (gnus-turn-off-edit-menu 'summary)
2318
2319   (unless (boundp 'gnus-summary-misc-menu)
2320
2321     (easy-menu-define
2322       gnus-summary-kill-menu gnus-summary-mode-map ""
2323       (cons
2324        "Score"
2325        (nconc
2326         (list
2327          ["Customize" gnus-score-customize t])
2328         (gnus-make-score-map 'increase)
2329         (gnus-make-score-map 'lower)
2330         '(("Mark"
2331            ["Kill below" gnus-summary-kill-below t]
2332            ["Mark above" gnus-summary-mark-above t]
2333            ["Tick above" gnus-summary-tick-above t]
2334            ["Clear above" gnus-summary-clear-above t])
2335           ["Current score" gnus-summary-current-score t]
2336           ["Set score" gnus-summary-set-score t]
2337           ["Switch current score file..." gnus-score-change-score-file t]
2338           ["Set mark below..." gnus-score-set-mark-below t]
2339           ["Set expunge below..." gnus-score-set-expunge-below t]
2340           ["Edit current score file" gnus-score-edit-current-scores t]
2341           ["Edit score file..." gnus-score-edit-file t]
2342           ["Trace score" gnus-score-find-trace t]
2343           ["Find words" gnus-score-find-favourite-words t]
2344           ["Rescore buffer" gnus-summary-rescore t]
2345           ["Increase score..." gnus-summary-increase-score t]
2346           ["Lower score..." gnus-summary-lower-score t]))))
2347
2348     ;; Define both the Article menu in the summary buffer and the
2349     ;; equivalent Commands menu in the article buffer here for
2350     ;; consistency.
2351     (let ((innards
2352            `(("Hide"
2353               ["All" gnus-article-hide t]
2354               ["Headers" gnus-article-hide-headers t]
2355               ["Signature" gnus-article-hide-signature t]
2356               ["Citation" gnus-article-hide-citation t]
2357               ["List identifiers" gnus-article-hide-list-identifiers t]
2358               ["Banner" gnus-article-strip-banner t]
2359               ["Boring headers" gnus-article-hide-boring-headers t])
2360              ("Highlight"
2361               ["All" gnus-article-highlight t]
2362               ["Headers" gnus-article-highlight-headers t]
2363               ["Signature" gnus-article-highlight-signature t]
2364               ["Citation" gnus-article-highlight-citation t])
2365              ("MIME"
2366               ["Words" gnus-article-decode-mime-words t]
2367               ["Charset" gnus-article-decode-charset t]
2368               ["QP" gnus-article-de-quoted-unreadable t]
2369               ["Base64" gnus-article-de-base64-unreadable t]
2370               ["View MIME buttons" gnus-summary-display-buttonized t]
2371               ["View all" gnus-mime-view-all-parts t]
2372               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2373               ["Encrypt body" gnus-article-encrypt-body
2374                :active (not (gnus-group-read-only-p))
2375                ,@(if (featurep 'xemacs) nil
2376                    '(:help "Encrypt the message body on disk"))]
2377               ["Extract all parts..." gnus-summary-save-parts t]
2378               ("Multipart"
2379                ["Repair multipart" gnus-summary-repair-multipart t]
2380                ["Pipe part..." gnus-article-pipe-part t]
2381                ["Inline part" gnus-article-inline-part t]
2382                ["View part as type..." gnus-article-view-part-as-type t]
2383                ["Encrypt body" gnus-article-encrypt-body
2384                 :active (not (gnus-group-read-only-p))
2385                ,@(if (featurep 'xemacs) nil
2386                    '(:help "Encrypt the message body on disk"))]
2387                ["View part externally" gnus-article-view-part-externally t]
2388                ["View HTML parts in browser" gnus-article-browse-html-article t]
2389                ["View part with charset..." gnus-article-view-part-as-charset t]
2390                ["Copy part" gnus-article-copy-part t]
2391                ["Save part..." gnus-article-save-part t]
2392                ["View part" gnus-article-view-part t]))
2393              ("Date"
2394               ["Local" gnus-article-date-local t]
2395               ["ISO8601" gnus-article-date-iso8601 t]
2396               ["UT" gnus-article-date-ut t]
2397               ["Original" gnus-article-date-original t]
2398               ["Lapsed" gnus-article-date-lapsed t]
2399               ["User-defined" gnus-article-date-user t])
2400              ("Display"
2401               ["Remove images" gnus-article-remove-images t]
2402               ["Toggle smiley" gnus-treat-smiley t]
2403               ["Show X-Face" gnus-article-display-x-face t]
2404               ["Show picons in From" gnus-treat-from-picon t]
2405               ["Show picons in mail headers" gnus-treat-mail-picon t]
2406               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2407               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2408               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2409               ("View as different encoding"
2410                ,@(gnus-summary-menu-split
2411                   (mapcar
2412                    (lambda (cs)
2413                      ;; Since easymenu under Emacs doesn't allow
2414                      ;; lambda forms for menu commands, we should
2415                      ;; provide intern'ed function symbols.
2416                      (let ((command (intern (format "\
2417 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2418                        (fset command
2419                              `(lambda ()
2420                                 (interactive)
2421                                 (let ((gnus-summary-show-article-charset-alist
2422                                        '((1 . ,cs))))
2423                                   (gnus-summary-show-article 1))))
2424                        `[,(symbol-name cs) ,command t]))
2425                    (sort (if (fboundp 'coding-system-list)
2426                              (coding-system-list)
2427                            (mapcar 'car mm-mime-mule-charset-alist))
2428                          'string<)))))
2429              ("Washing"
2430               ("Remove Blanks"
2431                ["Leading" gnus-article-strip-leading-blank-lines t]
2432                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2433                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2434                ["All of the above" gnus-article-strip-blank-lines t]
2435                ["All" gnus-article-strip-all-blank-lines t]
2436                ["Leading space" gnus-article-strip-leading-space t]
2437                ["Trailing space" gnus-article-strip-trailing-space t]
2438                ["Leading space in headers"
2439                 gnus-article-remove-leading-whitespace t])
2440               ["Overstrike" gnus-article-treat-overstrike t]
2441               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2442               ["Non-ASCII" gnus-article-treat-non-ascii t]
2443               ["Emphasis" gnus-article-emphasize t]
2444               ["Word wrap" gnus-article-fill-cited-article t]
2445               ["Fill long lines" gnus-article-fill-long-lines t]
2446               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2447               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2448               ["Remove CR" gnus-article-remove-cr t]
2449               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2450               ["Base64" gnus-article-de-base64-unreadable t]
2451               ["Rot 13" gnus-summary-caesar-message
2452                ,@(if (featurep 'xemacs) '(t)
2453                    '(:help "\"Caesar rotate\" article by 13"))]
2454               ["De-IDNA" gnus-summary-idna-message t]
2455               ["Morse decode" gnus-summary-morse-message t]
2456               ["Unix pipe..." gnus-summary-pipe-message t]
2457               ["Add buttons" gnus-article-add-buttons t]
2458               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2459               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2460               ["Verbose header" gnus-summary-verbose-headers t]
2461               ["Toggle header" gnus-summary-toggle-header t]
2462               ["Unfold headers" gnus-article-treat-unfold-headers t]
2463               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2464               ["Html" gnus-article-wash-html t]
2465               ["Unsplit URLs" gnus-article-unsplit-urls t]
2466               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2467               ["Decode HZ" gnus-article-decode-HZ t]
2468               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2469               ("(Outlook) Deuglify"
2470                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2471                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2472                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2473                ["Full (Outlook) deuglify"
2474                 gnus-article-outlook-deuglify-article t])
2475               )
2476              ("Output"
2477               ["Save in default format..." gnus-summary-save-article
2478                ,@(if (featurep 'xemacs) '(t)
2479                    '(:help "Save article using default method"))]
2480               ["Save in file..." gnus-summary-save-article-file
2481                ,@(if (featurep 'xemacs) '(t)
2482                    '(:help "Save article in file"))]
2483               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2484               ["Save in MH folder..." gnus-summary-save-article-folder t]
2485               ["Save in VM folder..." gnus-summary-save-article-vm t]
2486               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2487               ["Save body in file..." gnus-summary-save-article-body-file t]
2488               ["Pipe through a filter..." gnus-summary-pipe-output t]
2489               ["Print with Muttprint..." gnus-summary-muttprint t]
2490               ["Print" gnus-summary-print-article
2491                ,@(if (featurep 'xemacs) '(t)
2492                    '(:help "Generate and print a PostScript image"))])
2493              ("Copy, move,... (Backend)"
2494               ,@(if (featurep 'xemacs) nil
2495                   '(:help "Copying, moving, expiring articles..."))
2496               ["Respool article..." gnus-summary-respool-article t]
2497               ["Move article..." gnus-summary-move-article
2498                (gnus-check-backend-function
2499                 'request-move-article gnus-newsgroup-name)]
2500               ["Copy article..." gnus-summary-copy-article t]
2501               ["Crosspost article..." gnus-summary-crosspost-article
2502                (gnus-check-backend-function
2503                 'request-replace-article gnus-newsgroup-name)]
2504               ["Import file..." gnus-summary-import-article
2505                (gnus-check-backend-function
2506                 'request-accept-article gnus-newsgroup-name)]
2507               ["Create article..." gnus-summary-create-article
2508                (gnus-check-backend-function
2509                 'request-accept-article gnus-newsgroup-name)]
2510               ["Check if posted" gnus-summary-article-posted-p t]
2511               ["Edit article" gnus-summary-edit-article
2512                (not (gnus-group-read-only-p))]
2513               ["Delete article" gnus-summary-delete-article
2514                (gnus-check-backend-function
2515                 'request-expire-articles gnus-newsgroup-name)]
2516               ["Query respool" gnus-summary-respool-query t]
2517               ["Trace respool" gnus-summary-respool-trace t]
2518               ["Delete expirable articles" gnus-summary-expire-articles-now
2519                (gnus-check-backend-function
2520                 'request-expire-articles gnus-newsgroup-name)])
2521              ("Extract"
2522               ["Uudecode" gnus-uu-decode-uu
2523                ,@(if (featurep 'xemacs) '(t)
2524                    '(:help "Decode uuencoded article(s)"))]
2525               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2526               ["Unshar" gnus-uu-decode-unshar t]
2527               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2528               ["Save" gnus-uu-decode-save t]
2529               ["Binhex" gnus-uu-decode-binhex t]
2530               ["PostScript" gnus-uu-decode-postscript t]
2531               ["All MIME parts" gnus-summary-save-parts t])
2532              ("Cache"
2533               ["Enter article" gnus-cache-enter-article t]
2534               ["Remove article" gnus-cache-remove-article t])
2535              ["Translate" gnus-article-babel t]
2536              ["Select article buffer" gnus-summary-select-article-buffer t]
2537              ["Make article buffer sticky" gnus-sticky-article t]
2538              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2539              ["Isearch article..." gnus-summary-isearch-article t]
2540              ["Beginning of the article" gnus-summary-beginning-of-article t]
2541              ["End of the article" gnus-summary-end-of-article t]
2542              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2543              ["Fetch referenced articles" gnus-summary-refer-references t]
2544              ["Fetch current thread" gnus-summary-refer-thread t]
2545              ["Fetch article with id..." gnus-summary-refer-article t]
2546              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2547              ["Redisplay" gnus-summary-show-article t]
2548              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2549       (easy-menu-define
2550         gnus-summary-article-menu gnus-summary-mode-map ""
2551         (cons "Article" innards))
2552
2553       (if (not (keymapp gnus-summary-article-menu))
2554           (easy-menu-define
2555             gnus-article-commands-menu gnus-article-mode-map ""
2556             (cons "Commands" innards))
2557         ;; in Emacs, don't share menu.
2558         (setq gnus-article-commands-menu
2559               (copy-keymap gnus-summary-article-menu))
2560         (define-key gnus-article-mode-map [menu-bar commands]
2561           (cons "Commands" gnus-article-commands-menu))))
2562
2563     (easy-menu-define
2564       gnus-summary-thread-menu gnus-summary-mode-map ""
2565       '("Threads"
2566         ["Find all messages in thread" gnus-summary-refer-thread t]
2567         ["Toggle threading" gnus-summary-toggle-threads t]
2568         ["Hide threads" gnus-summary-hide-all-threads t]
2569         ["Show threads" gnus-summary-show-all-threads t]
2570         ["Hide thread" gnus-summary-hide-thread t]
2571         ["Show thread" gnus-summary-show-thread t]
2572         ["Go to next thread" gnus-summary-next-thread t]
2573         ["Go to previous thread" gnus-summary-prev-thread t]
2574         ["Go down thread" gnus-summary-down-thread t]
2575         ["Go up thread" gnus-summary-up-thread t]
2576         ["Top of thread" gnus-summary-top-thread t]
2577         ["Mark thread as read" gnus-summary-kill-thread t]
2578         ["Mark thread as expired" gnus-summary-expire-thread t]
2579         ["Lower thread score" gnus-summary-lower-thread t]
2580         ["Raise thread score" gnus-summary-raise-thread t]
2581         ["Rethread current" gnus-summary-rethread-current t]))
2582
2583     (easy-menu-define
2584       gnus-summary-post-menu gnus-summary-mode-map ""
2585       `("Post"
2586         ["Send a message (mail or news)" gnus-summary-post-news
2587          ,@(if (featurep 'xemacs) '(t)
2588              '(:help "Compose a new message (mail or news)"))]
2589         ["Followup" gnus-summary-followup
2590          ,@(if (featurep 'xemacs) '(t)
2591              '(:help "Post followup to this article"))]
2592         ["Followup and yank" gnus-summary-followup-with-original
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Post followup to this article, quoting its contents"))]
2595         ["Supersede article" gnus-summary-supersede-article t]
2596         ["Cancel article" gnus-summary-cancel-article
2597          ,@(if (featurep 'xemacs) '(t)
2598              '(:help "Cancel an article you posted"))]
2599         ["Reply" gnus-summary-reply t]
2600         ["Reply and yank" gnus-summary-reply-with-original t]
2601         ["Wide reply" gnus-summary-wide-reply t]
2602         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2603          ,@(if (featurep 'xemacs) '(t)
2604              '(:help "Mail a reply, quoting this article"))]
2605         ["Very wide reply" gnus-summary-very-wide-reply t]
2606         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2607          ,@(if (featurep 'xemacs) '(t)
2608              '(:help "Mail a very wide reply, quoting this article"))]
2609         ["Mail forward" gnus-summary-mail-forward t]
2610         ["Post forward" gnus-summary-post-forward t]
2611         ["Digest and mail" gnus-uu-digest-mail-forward t]
2612         ["Digest and post" gnus-uu-digest-post-forward t]
2613         ["Resend message" gnus-summary-resend-message t]
2614         ["Resend message edit" gnus-summary-resend-message-edit t]
2615         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2616         ["Send a mail" gnus-summary-mail-other-window t]
2617         ["Create a local message" gnus-summary-news-other-window t]
2618         ["Uuencode and post" gnus-uu-post-news
2619          ,@(if (featurep 'xemacs) '(t)
2620              '(:help "Post a uuencoded article"))]
2621         ["Followup via news" gnus-summary-followup-to-mail t]
2622         ["Followup via news and yank"
2623          gnus-summary-followup-to-mail-with-original t]
2624         ["Strip signature on reply"
2625          (lambda ()
2626            (interactive)
2627            (if (not (memq message-cite-function
2628                           '(message-cite-original-without-signature
2629                             message-cite-original)))
2630                ;; Stupid workaround for XEmacs not honoring :visible.
2631                (message "Can't toggle this value of `message-cite-function'")
2632              (setq message-cite-function
2633                    (if (eq message-cite-function
2634                            'message-cite-original-without-signature)
2635                        'message-cite-original
2636                      'message-cite-original-without-signature))))
2637          ;; XEmacs barfs on :visible.
2638          ,@(if (featurep 'xemacs) nil
2639              '(:visible (memq message-cite-function
2640                               '(message-cite-original-without-signature
2641                                 message-cite-original))))
2642          :style toggle
2643          :selected (eq message-cite-function
2644                        'message-cite-original-without-signature)
2645          ,@(if (featurep 'xemacs) nil
2646              '(:help "Strip signature from cited article when replying."))]
2647         ;;("Draft"
2648         ;;["Send" gnus-summary-send-draft t]
2649         ;;["Send bounced" gnus-resend-bounced-mail t])
2650         ))
2651
2652     (cond
2653      ((not (keymapp gnus-summary-post-menu))
2654       (setq gnus-article-post-menu gnus-summary-post-menu))
2655      ((not gnus-article-post-menu)
2656       ;; Don't share post menu.
2657       (setq gnus-article-post-menu
2658             (copy-keymap gnus-summary-post-menu))))
2659     (define-key gnus-article-mode-map [menu-bar post]
2660       (cons "Post" gnus-article-post-menu))
2661
2662     (easy-menu-define
2663       gnus-summary-misc-menu gnus-summary-mode-map ""
2664       `("Gnus"
2665         ("Mark Read"
2666          ["Mark as read" gnus-summary-mark-as-read-forward t]
2667          ["Mark same subject and select"
2668           gnus-summary-kill-same-subject-and-select t]
2669          ["Mark same subject" gnus-summary-kill-same-subject t]
2670          ["Catchup" gnus-summary-catchup
2671           ,@(if (featurep 'xemacs) '(t)
2672               '(:help "Mark unread articles in this group as read"))]
2673          ["Catchup all" gnus-summary-catchup-all t]
2674          ["Catchup to here" gnus-summary-catchup-to-here t]
2675          ["Catchup from here" gnus-summary-catchup-from-here t]
2676          ["Catchup region" gnus-summary-mark-region-as-read
2677           (gnus-mark-active-p)]
2678          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2679         ("Mark Various"
2680          ["Tick" gnus-summary-tick-article-forward t]
2681          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2682          ["Remove marks" gnus-summary-clear-mark-forward t]
2683          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2684          ["Set bookmark" gnus-summary-set-bookmark t]
2685          ["Remove bookmark" gnus-summary-remove-bookmark t])
2686         ("Limit to"
2687          ["Marks..." gnus-summary-limit-to-marks t]
2688          ["Subject..." gnus-summary-limit-to-subject t]
2689          ["Author..." gnus-summary-limit-to-author t]
2690          ["Recipient..." gnus-summary-limit-to-recipient t]
2691          ["Address..." gnus-summary-limit-to-address t]
2692          ["Age..." gnus-summary-limit-to-age t]
2693          ["Extra..." gnus-summary-limit-to-extra t]
2694          ["Score..." gnus-summary-limit-to-score t]
2695          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2696          ["Unread" gnus-summary-limit-to-unread t]
2697          ["Unseen" gnus-summary-limit-to-unseen t]
2698          ["Singletons" gnus-summary-limit-to-singletons t]
2699          ["Replied" gnus-summary-limit-to-replied t]
2700          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2701          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2702          ["Pop limit" gnus-summary-pop-limit t]
2703          ["Show dormant" gnus-summary-limit-include-dormant t]
2704          ["Hide childless dormant"
2705           gnus-summary-limit-exclude-childless-dormant t]
2706          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2707          ["Hide marked" gnus-summary-limit-exclude-marks t]
2708          ["Show expunged" gnus-summary-limit-include-expunged t])
2709         ("Process Mark"
2710          ["Set mark" gnus-summary-mark-as-processable t]
2711          ["Remove mark" gnus-summary-unmark-as-processable t]
2712          ["Remove all marks" gnus-summary-unmark-all-processable t]
2713          ["Invert marks" gnus-uu-invert-processable t]
2714          ["Mark above" gnus-uu-mark-over t]
2715          ["Mark series" gnus-uu-mark-series t]
2716          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2717          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2718          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2719          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2720          ["Mark all" gnus-uu-mark-all t]
2721          ["Mark buffer" gnus-uu-mark-buffer t]
2722          ["Mark sparse" gnus-uu-mark-sparse t]
2723          ["Mark thread" gnus-uu-mark-thread t]
2724          ["Unmark thread" gnus-uu-unmark-thread t]
2725          ("Process Mark Sets"
2726           ["Kill" gnus-summary-kill-process-mark t]
2727           ["Yank" gnus-summary-yank-process-mark
2728            gnus-newsgroup-process-stack]
2729           ["Save" gnus-summary-save-process-mark t]
2730           ["Run command on marked..." gnus-summary-universal-argument t]))
2731         ("Registry Marks")
2732         ("Scroll article"
2733          ["Page forward" gnus-summary-next-page
2734           ,@(if (featurep 'xemacs) '(t)
2735               '(:help "Show next page of article"))]
2736          ["Page backward" gnus-summary-prev-page
2737           ,@(if (featurep 'xemacs) '(t)
2738               '(:help "Show previous page of article"))]
2739          ["Line forward" gnus-summary-scroll-up t])
2740         ("Move"
2741          ["Next unread article" gnus-summary-next-unread-article t]
2742          ["Previous unread article" gnus-summary-prev-unread-article t]
2743          ["Next article" gnus-summary-next-article t]
2744          ["Previous article" gnus-summary-prev-article t]
2745          ["Next unread subject" gnus-summary-next-unread-subject t]
2746          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2747          ["Next article same subject" gnus-summary-next-same-subject t]
2748          ["Previous article same subject" gnus-summary-prev-same-subject t]
2749          ["First unread article" gnus-summary-first-unread-article t]
2750          ["Best unread article" gnus-summary-best-unread-article t]
2751          ["Go to subject number..." gnus-summary-goto-subject t]
2752          ["Go to article number..." gnus-summary-goto-article t]
2753          ["Go to the last article" gnus-summary-goto-last-article t]
2754          ["Pop article off history" gnus-summary-pop-article t])
2755         ("Sort"
2756          ["Sort by number" gnus-summary-sort-by-number t]
2757          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2758          ["Sort by author" gnus-summary-sort-by-author t]
2759          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2760          ["Sort by subject" gnus-summary-sort-by-subject t]
2761          ["Sort by date" gnus-summary-sort-by-date t]
2762          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2763          ["Sort by score" gnus-summary-sort-by-score t]
2764          ["Sort by lines" gnus-summary-sort-by-lines t]
2765          ["Sort by characters" gnus-summary-sort-by-chars t]
2766          ["Randomize" gnus-summary-sort-by-random t]
2767          ["Original sort" gnus-summary-sort-by-original t])
2768         ("Help"
2769          ["Describe group" gnus-summary-describe-group t]
2770          ["Read manual" gnus-info-find-node t])
2771         ("Modes"
2772          ["Pick and read" gnus-pick-mode t]
2773          ["Binary" gnus-binary-mode t])
2774         ("Regeneration"
2775          ["Regenerate" gnus-summary-prepare t]
2776          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2777          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2778          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2779          ["Toggle threading" gnus-summary-toggle-threads t])
2780         ["See old articles" gnus-summary-insert-old-articles t]
2781         ["See new articles" gnus-summary-insert-new-articles t]
2782         ["Filter articles..." gnus-summary-execute-command t]
2783         ["Run command on articles..." gnus-summary-universal-argument t]
2784         ["Search articles forward..." gnus-summary-search-article-forward t]
2785         ["Search articles backward..." gnus-summary-search-article-backward t]
2786         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2787         ["Expand window" gnus-summary-expand-window t]
2788         ["Expire expirable articles" gnus-summary-expire-articles
2789          (gnus-check-backend-function
2790           'request-expire-articles gnus-newsgroup-name)]
2791         ["Edit local kill file" gnus-summary-edit-local-kill t]
2792         ["Edit main kill file" gnus-summary-edit-global-kill t]
2793         ["Edit group parameters" gnus-summary-edit-parameters t]
2794         ["Customize group parameters" gnus-summary-customize-parameters t]
2795         ["Send a bug report" gnus-bug t]
2796         ("Exit"
2797          ["Catchup and exit" gnus-summary-catchup-and-exit
2798           ,@(if (featurep 'xemacs) '(t)
2799               '(:help "Mark unread articles in this group as read, then exit"))]
2800          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2801          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2802          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2803          ["Exit group" gnus-summary-exit
2804           ,@(if (featurep 'xemacs) '(t)
2805               '(:help "Exit current group, return to group selection mode"))]
2806          ["Exit group without updating" gnus-summary-exit-no-update t]
2807          ["Exit and goto next group" gnus-summary-next-group t]
2808          ["Exit and goto prev group" gnus-summary-prev-group t]
2809          ["Reselect group" gnus-summary-reselect-current-group t]
2810          ["Rescan group" gnus-summary-rescan-group t]
2811          ["Update dribble" gnus-summary-save-newsrc t])))
2812
2813     (gnus-run-hooks 'gnus-summary-menu-hook)))
2814
2815 (defvar gnus-summary-tool-bar-map nil)
2816
2817 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2818 ;; affect _new_ message buffers.  We might add a function that walks thru all
2819 ;; summary-mode buffers and force the update.
2820 (defun gnus-summary-tool-bar-update (&optional symbol value)
2821   "Update summary mode toolbar.
2822 Setter function for custom variables."
2823   (setq-default gnus-summary-tool-bar-map nil)
2824   (when symbol
2825     ;; When used as ":set" function:
2826     (set-default symbol value))
2827   (when (gnus-buffer-live-p gnus-summary-buffer)
2828     (with-current-buffer gnus-summary-buffer
2829       (gnus-summary-make-tool-bar))))
2830
2831 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2832                                      'gnus-summary-tool-bar-gnome
2833                                    'gnus-summary-tool-bar-retro)
2834   "Specifies the Gnus summary tool bar.
2835
2836 It can be either a list or a symbol referring to a list.  See
2837 `gmm-tool-bar-from-list' for the format of the list.  The
2838 default key map is `gnus-summary-mode-map'.
2839
2840 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2841 `gnus-summary-tool-bar-retro'."
2842   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2843                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2844                  (repeat :tag "User defined list" gmm-tool-bar-item)
2845                  (symbol))
2846   :version "23.1" ;; No Gnus
2847   :initialize 'custom-initialize-default
2848   :set 'gnus-summary-tool-bar-update
2849   :group 'gnus-summary)
2850
2851 (defcustom gnus-summary-tool-bar-gnome
2852   '((gnus-summary-post-news "mail/compose" nil)
2853     (gnus-summary-insert-new-articles "mail/inbox" nil
2854                                       :visible (or (not gnus-agent)
2855                                                    gnus-plugged))
2856     (gnus-summary-reply-with-original "mail/reply")
2857     (gnus-summary-reply "mail/reply" nil :visible nil)
2858     (gnus-summary-followup-with-original "mail/reply-all")
2859     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2860     (gnus-summary-mail-forward "mail/forward")
2861     (gnus-summary-save-article "mail/save")
2862     (gnus-summary-search-article-forward "search" nil :visible nil)
2863     (gnus-summary-print-article "print")
2864     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2865     ;; Some new commands that may need more suitable icons:
2866     (gnus-summary-save-newsrc "save" nil :visible nil)
2867     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2868     (gnus-summary-prev-article "left-arrow")
2869     (gnus-summary-next-article "right-arrow")
2870     (gnus-summary-next-page "next-page")
2871     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2872     ;;
2873     ;; Maybe some sort-by-... could be added:
2874     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2875     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2876     (gnus-summary-mark-as-expirable
2877      "delete" nil
2878      :visible (gnus-check-backend-function 'request-expire-articles
2879                                            gnus-newsgroup-name))
2880     (gnus-summary-mark-as-spam
2881      "mail/spam" t
2882      :visible (and (fboundp 'spam-group-ham-contents-p)
2883                    (spam-group-ham-contents-p gnus-newsgroup-name))
2884      :help "Mark as spam")
2885     (gnus-summary-mark-as-read-forward
2886      "mail/not-spam" nil
2887      :visible (and (fboundp 'spam-group-spam-contents-p)
2888                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2889     ;;
2890     (gnus-summary-exit "exit")
2891     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2892     (gnus-info-find-node "help"))
2893   "List of functions for the summary tool bar (GNOME style).
2894
2895 See `gmm-tool-bar-from-list' for the format of the list."
2896   :type '(repeat gmm-tool-bar-item)
2897   :version "23.1" ;; No Gnus
2898   :initialize 'custom-initialize-default
2899   :set 'gnus-summary-tool-bar-update
2900   :group 'gnus-summary)
2901
2902 (defcustom gnus-summary-tool-bar-retro
2903   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2904     (gnus-summary-next-unread-article "gnus/next-ur")
2905     (gnus-summary-post-news "gnus/post")
2906     (gnus-summary-followup-with-original "gnus/fuwo")
2907     (gnus-summary-followup "gnus/followup")
2908     (gnus-summary-reply-with-original "gnus/reply-wo")
2909     (gnus-summary-reply "gnus/reply")
2910     (gnus-summary-caesar-message "gnus/rot13")
2911     (gnus-uu-decode-uu "gnus/uu-decode")
2912     (gnus-summary-save-article-file "gnus/save-aif")
2913     (gnus-summary-save-article "gnus/save-art")
2914     (gnus-uu-post-news "gnus/uu-post")
2915     (gnus-summary-catchup "gnus/catchup")
2916     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2917     (gnus-summary-exit "gnus/exit-summ")
2918     ;; Some new command that may need more suitable icons:
2919     (gnus-summary-print-article "gnus/print" nil :visible nil)
2920     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2921     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2922     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2923     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2924     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2925     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2926     ;;
2927     (gnus-info-find-node "gnus/help" nil :visible nil))
2928   "List of functions for the summary tool bar (retro look).
2929
2930 See `gmm-tool-bar-from-list' for the format of the list."
2931   :type '(repeat gmm-tool-bar-item)
2932   :version "23.1" ;; No Gnus
2933   :initialize 'custom-initialize-default
2934   :set 'gnus-summary-tool-bar-update
2935   :group 'gnus-summary)
2936
2937 (defcustom gnus-summary-tool-bar-zap-list t
2938   "List of icon items from the global tool bar.
2939 These items are not displayed in the Gnus summary mode tool bar.
2940
2941 See `gmm-tool-bar-from-list' for the format of the list."
2942   :type 'gmm-tool-bar-zap-list
2943   :version "23.1" ;; No Gnus
2944   :initialize 'custom-initialize-default
2945   :set 'gnus-summary-tool-bar-update
2946   :group 'gnus-summary)
2947
2948 (defvar image-load-path)
2949 (defvar tool-bar-map)
2950
2951 (defun gnus-summary-make-tool-bar (&optional force)
2952   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2953 When FORCE, rebuild the tool bar."
2954   (when (and (not (featurep 'xemacs))
2955              (boundp 'tool-bar-mode)
2956              tool-bar-mode
2957              (or (not gnus-summary-tool-bar-map) force))
2958     (let* ((load-path
2959             (gmm-image-load-path-for-library "gnus"
2960                                              "mail/save.xpm"
2961                                              nil t))
2962            (image-load-path (cons (car load-path)
2963                                   (when (boundp 'image-load-path)
2964                                     image-load-path)))
2965            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2966                                         gnus-summary-tool-bar-zap-list
2967                                         'gnus-summary-mode-map)))
2968       (when map
2969         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2970         ;; uses its value.
2971         (setq gnus-summary-tool-bar-map map))))
2972   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2973
2974 (defun gnus-score-set-default (var value)
2975   "A version of set that updates the GNU Emacs menu-bar."
2976   (set var value)
2977   ;; It is the message that forces the active status to be updated.
2978   (message ""))
2979
2980 (defun gnus-make-score-map (type)
2981   "Make a summary score map of type TYPE."
2982   (if t
2983       nil
2984     (let ((headers '(("author" "from" string)
2985                      ("subject" "subject" string)
2986                      ("article body" "body" string)
2987                      ("article head" "head" string)
2988                      ("xref" "xref" string)
2989                      ("extra header" "extra" string)
2990                      ("lines" "lines" number)
2991                      ("followups to author" "followup" string)))
2992           (types '((number ("less than" <)
2993                            ("greater than" >)
2994                            ("equal" =))
2995                    (string ("substring" s)
2996                            ("exact string" e)
2997                            ("fuzzy string" f)
2998                            ("regexp" r))))
2999           (perms '(("temporary" (current-time-string))
3000                    ("permanent" nil)
3001                    ("immediate" now)))
3002           header)
3003       (list
3004        (apply
3005         'nconc
3006         (list
3007          (if (eq type 'lower)
3008              "Lower score"
3009            "Increase score"))
3010         (let (outh)
3011           (while headers
3012             (setq header (car headers))
3013             (setq outh
3014                   (cons
3015                    (apply
3016                     'nconc
3017                     (list (car header))
3018                     (let ((ts (cdr (assoc (nth 2 header) types)))
3019                           outt)
3020                       (while ts
3021                         (setq outt
3022                               (cons
3023                                (apply
3024                                 'nconc
3025                                 (list (caar ts))
3026                                 (let ((ps perms)
3027                                       outp)
3028                                   (while ps
3029                                     (setq outp
3030                                           (cons
3031                                            (vector
3032                                             (caar ps)
3033                                             (list
3034                                              'gnus-summary-score-entry
3035                                              (nth 1 header)
3036                                              (if (or (string= (nth 1 header)
3037                                                               "head")
3038                                                      (string= (nth 1 header)
3039                                                               "body"))
3040                                                  ""
3041                                                (list 'gnus-summary-header
3042                                                      (nth 1 header)))
3043                                              (list 'quote (nth 1 (car ts)))
3044                                              (list 'gnus-score-delta-default
3045                                                    nil)
3046                                              (nth 1 (car ps))
3047                                              t)
3048                                             t)
3049                                            outp))
3050                                     (setq ps (cdr ps)))
3051                                   (list (nreverse outp))))
3052                                outt))
3053                         (setq ts (cdr ts)))
3054                       (list (nreverse outt))))
3055                    outh))
3056             (setq headers (cdr headers)))
3057           (list (nreverse outh))))))))
3058
3059
3060 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3061 (defvar bookmark-make-record-function)
3062 \f
3063 (defvar bidi-paragraph-direction)
3064
3065 (defun gnus-summary-mode (&optional group)
3066   "Major mode for reading articles.
3067
3068 All normal editing commands are switched off.
3069 \\<gnus-summary-mode-map>
3070 Each line in this buffer represents one article.  To read an
3071 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3072 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3073 respectively.
3074
3075 You can also post articles and send mail from this buffer.  To
3076 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3077 of an article, type `\\[gnus-summary-reply]'.
3078
3079 There are approx. one gazillion commands you can execute in this
3080 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3081
3082 The following commands are available:
3083
3084 \\{gnus-summary-mode-map}"
3085   (interactive)
3086   (kill-all-local-variables)
3087   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3088     (gnus-summary-make-local-variables))
3089   (gnus-summary-make-local-variables)
3090   (setq gnus-newsgroup-name group)
3091   (when (gnus-visual-p 'summary-menu 'menu)
3092     (gnus-summary-make-menu-bar)
3093     (gnus-summary-make-tool-bar))
3094   (gnus-make-thread-indent-array)
3095   (gnus-simplify-mode-line)
3096   (setq major-mode 'gnus-summary-mode)
3097   (setq mode-name "Summary")
3098   (use-local-map gnus-summary-mode-map)
3099   (buffer-disable-undo)
3100   (setq buffer-read-only t              ;Disable modification
3101         show-trailing-whitespace nil)
3102   (setq truncate-lines t)
3103   ;; Force paragraph direction to be left-to-right.  Don't make it
3104   ;; bound globally in old Emacsen and XEmacsen.
3105   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3106   (add-to-invisibility-spec '(gnus-sum . t))
3107   (gnus-summary-set-display-table)
3108   (gnus-set-default-directory)
3109   (make-local-variable 'gnus-summary-line-format)
3110   (make-local-variable 'gnus-summary-line-format-spec)
3111   (make-local-variable 'gnus-summary-dummy-line-format)
3112   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3113   (make-local-variable 'gnus-summary-mark-positions)
3114   (gnus-make-local-hook 'pre-command-hook)
3115   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3116   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3117   (turn-on-gnus-mailing-list-mode)
3118   (mm-enable-multibyte)
3119   (set (make-local-variable 'bookmark-make-record-function)
3120        'gnus-summary-bookmark-make-record)
3121   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3122   (gnus-update-summary-mark-positions))
3123
3124 (defun gnus-summary-make-local-variables ()
3125   "Make all the local summary buffer variables."
3126   (let (global)
3127     (dolist (local gnus-summary-local-variables)
3128       (if (consp local)
3129           (progn
3130             (if (eq (cdr local) 'global)
3131                 ;; Copy the global value of the variable.
3132                 (setq global (symbol-value (car local)))
3133               ;; Use the value from the list.
3134               (setq global (eval (cdr local))))
3135             (set (make-local-variable (car local)) global))
3136         ;; Simple nil-valued local variable.
3137         (set (make-local-variable local) nil)))))
3138
3139 ;; Summary data functions.
3140
3141 (defmacro gnus-data-number (data)
3142   `(car ,data))
3143
3144 (defmacro gnus-data-set-number (data number)
3145   `(setcar ,data ,number))
3146
3147 (defmacro gnus-data-mark (data)
3148   `(nth 1 ,data))
3149
3150 (defmacro gnus-data-set-mark (data mark)
3151   `(setcar (nthcdr 1 ,data) ,mark))
3152
3153 (defmacro gnus-data-pos (data)
3154   `(nth 2 ,data))
3155
3156 (defmacro gnus-data-set-pos (data pos)
3157   `(setcar (nthcdr 2 ,data) ,pos))
3158
3159 (defmacro gnus-data-header (data)
3160   `(nth 3 ,data))
3161
3162 (defmacro gnus-data-set-header (data header)
3163   `(setf (nth 3 ,data) ,header))
3164
3165 (defmacro gnus-data-level (data)
3166   `(nth 4 ,data))
3167
3168 (defmacro gnus-data-unread-p (data)
3169   `(= (nth 1 ,data) gnus-unread-mark))
3170
3171 (defmacro gnus-data-read-p (data)
3172   `(/= (nth 1 ,data) gnus-unread-mark))
3173
3174 (defmacro gnus-data-pseudo-p (data)
3175   `(consp (nth 3 ,data)))
3176
3177 (defmacro gnus-data-find (number)
3178   `(assq ,number gnus-newsgroup-data))
3179
3180 (defmacro gnus-data-find-list (number &optional data)
3181   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3182      (memq (assq ,number bdata)
3183            bdata)))
3184
3185 (defmacro gnus-data-make (number mark pos header level)
3186   `(list ,number ,mark ,pos ,header ,level))
3187
3188 (defun gnus-data-enter (after-article number mark pos header level offset)
3189   (let ((data (gnus-data-find-list after-article)))
3190     (unless data
3191       (error "No such article: %d" after-article))
3192     (setcdr data (cons (gnus-data-make number mark pos header level)
3193                        (cdr data)))
3194     (setq gnus-newsgroup-data-reverse nil)
3195     (gnus-data-update-list (cddr data) offset)))
3196
3197 (defun gnus-data-enter-list (after-article list &optional offset)
3198   (when list
3199     (let ((data (and after-article (gnus-data-find-list after-article)))
3200           (ilist list))
3201       (if (not (or data
3202                    after-article))
3203           (let ((odata gnus-newsgroup-data))
3204             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3205             (when offset
3206               (gnus-data-update-list odata offset)))
3207         ;; Find the last element in the list to be spliced into the main
3208         ;; list.
3209         (setq list (last list))
3210         (if (not data)
3211             (progn
3212               (setcdr list gnus-newsgroup-data)
3213               (setq gnus-newsgroup-data ilist)
3214               (when offset
3215                 (gnus-data-update-list (cdr list) offset)))
3216           (setcdr list (cdr data))
3217           (setcdr data ilist)
3218           (when offset
3219             (gnus-data-update-list (cdr list) offset))))
3220       (setq gnus-newsgroup-data-reverse nil))))
3221
3222 (defun gnus-data-remove (article &optional offset)
3223   (let ((data gnus-newsgroup-data))
3224     (if (= (gnus-data-number (car data)) article)
3225         (progn
3226           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3227                 gnus-newsgroup-data-reverse nil)
3228           (when offset
3229             (gnus-data-update-list gnus-newsgroup-data offset)))
3230       (while (cdr data)
3231         (when (= (gnus-data-number (cadr data)) article)
3232           (setcdr data (cddr data))
3233           (when offset
3234             (gnus-data-update-list (cdr data) offset))
3235           (setq data nil
3236                 gnus-newsgroup-data-reverse nil))
3237         (setq data (cdr data))))))
3238
3239 (defmacro gnus-data-list (backward)
3240   `(if ,backward
3241        (or gnus-newsgroup-data-reverse
3242            (setq gnus-newsgroup-data-reverse
3243                  (reverse gnus-newsgroup-data)))
3244      gnus-newsgroup-data))
3245
3246 (defun gnus-data-update-list (data offset)
3247   "Add OFFSET to the POS of all data entries in DATA."
3248   (setq gnus-newsgroup-data-reverse nil)
3249   (while data
3250     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3251     (setq data (cdr data))))
3252
3253 (defun gnus-summary-article-pseudo-p (article)
3254   "Say whether this article is a pseudo article or not."
3255   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3256
3257 (defmacro gnus-summary-article-sparse-p (article)
3258   "Say whether this article is a sparse article or not."
3259   `(memq ,article gnus-newsgroup-sparse))
3260
3261 (defmacro gnus-summary-article-ancient-p (article)
3262   "Say whether this article is a sparse article or not."
3263   `(memq ,article gnus-newsgroup-ancient))
3264
3265 (defun gnus-article-parent-p (number)
3266   "Say whether this article is a parent or not."
3267   (let ((data (gnus-data-find-list number)))
3268     (and (cdr data)              ; There has to be an article after...
3269          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3270             (gnus-data-level (nth 1 data))))))
3271
3272 (defun gnus-article-children (number)
3273   "Return a list of all children to NUMBER."
3274   (let* ((data (gnus-data-find-list number))
3275          (level (gnus-data-level (car data)))
3276          children)
3277     (setq data (cdr data))
3278     (while (and data
3279                 (= (gnus-data-level (car data)) (1+ level)))
3280       (push (gnus-data-number (car data)) children)
3281       (setq data (cdr data)))
3282     children))
3283
3284 (defmacro gnus-summary-skip-intangible ()
3285   "If the current article is intangible, then jump to a different article."
3286   '(let ((to (get-text-property (point) 'gnus-intangible)))
3287      (and to (gnus-summary-goto-subject to))))
3288
3289 (defmacro gnus-summary-article-intangible-p ()
3290   "Say whether this article is intangible or not."
3291   '(get-text-property (point) 'gnus-intangible))
3292
3293 (defun gnus-article-read-p (article)
3294   "Say whether ARTICLE is read or not."
3295   (not (or (memq article gnus-newsgroup-marked)
3296            (memq article gnus-newsgroup-spam-marked)
3297            (memq article gnus-newsgroup-unreads)
3298            (memq article gnus-newsgroup-unselected)
3299            (memq article gnus-newsgroup-dormant))))
3300
3301 ;; Some summary mode macros.
3302
3303 (defmacro gnus-summary-article-number ()
3304   "The article number of the article on the current line.
3305 If there isn't an article number here, then we return the current
3306 article number."
3307   '(progn
3308      (gnus-summary-skip-intangible)
3309      (or (get-text-property (point) 'gnus-number)
3310          (gnus-summary-last-subject))))
3311
3312 (defmacro gnus-summary-article-header (&optional number)
3313   "Return the header of article NUMBER."
3314   `(gnus-data-header (gnus-data-find
3315                       ,(or number '(gnus-summary-article-number)))))
3316
3317 (defmacro gnus-summary-thread-level (&optional number)
3318   "Return the level of thread that starts with article NUMBER."
3319   `(if (and (eq gnus-summary-make-false-root 'dummy)
3320             (get-text-property (point) 'gnus-intangible))
3321        0
3322      (gnus-data-level (gnus-data-find
3323                        ,(or number '(gnus-summary-article-number))))))
3324
3325 (defmacro gnus-summary-article-mark (&optional number)
3326   "Return the mark of article NUMBER."
3327   `(gnus-data-mark (gnus-data-find
3328                     ,(or number '(gnus-summary-article-number)))))
3329
3330 (defmacro gnus-summary-article-pos (&optional number)
3331   "Return the position of the line of article NUMBER."
3332   `(gnus-data-pos (gnus-data-find
3333                    ,(or number '(gnus-summary-article-number)))))
3334
3335 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3336 (defmacro gnus-summary-article-subject (&optional number)
3337   "Return current subject string or nil if nothing."
3338   `(let ((headers
3339           ,(if number
3340                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3341              '(gnus-data-header (assq (gnus-summary-article-number)
3342                                       gnus-newsgroup-data)))))
3343      (and headers
3344           (vectorp headers)
3345           (mail-header-subject headers))))
3346
3347 (defmacro gnus-summary-article-score (&optional number)
3348   "Return current article score."
3349   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3350                   gnus-newsgroup-scored))
3351        gnus-summary-default-score 0))
3352
3353 (defun gnus-summary-article-children (&optional number)
3354   "Return a list of article numbers that are children of article NUMBER."
3355   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3356          (level (gnus-data-level (car data)))
3357          l children)
3358     (while (and (setq data (cdr data))
3359                 (> (setq l (gnus-data-level (car data))) level))
3360       (and (= (1+ level) l)
3361            (push (gnus-data-number (car data))
3362                  children)))
3363     (nreverse children)))
3364
3365 (defun gnus-summary-article-parent (&optional number)
3366   "Return the article number of the parent of article NUMBER."
3367   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3368                                     (gnus-data-list t)))
3369          (level (gnus-data-level (car data))))
3370     (if (zerop level)
3371         ()                              ; This is a root.
3372       ;; We search until we find an article with a level less than
3373       ;; this one.  That function has to be the parent.
3374       (while (and (setq data (cdr data))
3375                   (not (< (gnus-data-level (car data)) level))))
3376       (and data (gnus-data-number (car data))))))
3377
3378 (defun gnus-unread-mark-p (mark)
3379   "Say whether MARK is the unread mark."
3380   (= mark gnus-unread-mark))
3381
3382 (defun gnus-read-mark-p (mark)
3383   "Say whether MARK is one of the marks that mark as read.
3384 This is all marks except unread, ticked, dormant, and expirable."
3385   (not (or (= mark gnus-unread-mark)
3386            (= mark gnus-ticked-mark)
3387            (= mark gnus-spam-mark)
3388            (= mark gnus-dormant-mark)
3389            (= mark gnus-expirable-mark))))
3390
3391 (defmacro gnus-article-mark (number)
3392   "Return the MARK of article NUMBER.
3393 This macro should only be used when computing the mark the \"first\"
3394 time; i.e., when generating the summary lines.  After that,
3395 `gnus-summary-article-mark' should be used to examine the
3396 marks of articles."
3397   `(cond
3398     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3399     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3400     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3401     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3402     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3403     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3404     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3405     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3406            gnus-ancient-mark))))
3407
3408 ;; Saving hidden threads.
3409
3410 (defmacro gnus-save-hidden-threads (&rest forms)
3411   "Save hidden threads, eval FORMS, and restore the hidden threads."
3412   (let ((config (make-symbol "config")))
3413     `(let ((,config (gnus-hidden-threads-configuration)))
3414        (unwind-protect
3415            (save-excursion
3416              ,@forms)
3417          (gnus-restore-hidden-threads-configuration ,config)))))
3418 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3419 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3420
3421 (defun gnus-data-compute-positions ()
3422   "Compute the positions of all articles."
3423   (setq gnus-newsgroup-data-reverse nil)
3424   (let ((data gnus-newsgroup-data))
3425     (save-excursion
3426       (gnus-save-hidden-threads
3427         (gnus-summary-show-all-threads)
3428         (goto-char (point-min))
3429         (while data
3430           (while (get-text-property (point) 'gnus-intangible)
3431             (forward-line 1))
3432           (gnus-data-set-pos (car data) (+ (point) 3))
3433           (setq data (cdr data))
3434           (forward-line 1))))))
3435
3436 (defun gnus-hidden-threads-configuration ()
3437   "Return the current hidden threads configuration."
3438   (save-excursion
3439     (let (config)
3440       (goto-char (point-min))
3441       (while (not (eobp))
3442         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3443           (push (save-excursion (forward-line 0) (point)) config))
3444         (forward-line 1))
3445       config)))
3446
3447 (defun gnus-restore-hidden-threads-configuration (config)
3448   "Restore hidden threads configuration from CONFIG."
3449   (save-excursion
3450     (let (point (inhibit-read-only t))
3451       (while (setq point (pop config))
3452         (goto-char point)
3453         (gnus-summary-hide-thread)))))
3454
3455 ;; Various summary mode internalish functions.
3456
3457 (defun gnus-mouse-pick-article (e)
3458   (interactive "e")
3459   (mouse-set-point e)
3460   (gnus-summary-next-page nil t))
3461
3462 (defun gnus-summary-set-display-table ()
3463   "Change the display table.
3464 Odd characters have a tendency to mess
3465 up nicely formatted displays - we make all possible glyphs
3466 display only a single character."
3467
3468   ;; We start from the standard display table, if any.
3469   (let ((table (or (copy-sequence standard-display-table)
3470                    (make-display-table)))
3471         (i 32))
3472     ;; Nix out all the control chars...
3473     (while (>= (setq i (1- i)) 0)
3474       (gnus-put-display-table i [??] table))
3475    ;; ... but not newline and cr, of course.  (cr is necessary for the
3476     ;; selective display).
3477     (gnus-put-display-table ?\n nil table)
3478     (gnus-put-display-table ?\r nil table)
3479     ;; We keep TAB as well.
3480     (gnus-put-display-table ?\t nil table)
3481     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3482     ;; Emacs 23 (unicode), that are not set already.
3483     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3484                  160
3485                256)))
3486       (while (>= (setq i (1- i)) 127)
3487         ;; Only modify if the entry is nil.
3488         (unless (gnus-get-display-table i table)
3489           (gnus-put-display-table i [??] table))))
3490     (setq buffer-display-table table)))
3491
3492 (defun gnus-summary-set-article-display-arrow (pos)
3493   "Update the overlay arrow to point to line at position POS."
3494   (when gnus-summary-display-arrow
3495     (make-local-variable 'overlay-arrow-position)
3496     (make-local-variable 'overlay-arrow-string)
3497     (save-excursion
3498       (goto-char pos)
3499       (beginning-of-line)
3500       (unless overlay-arrow-position
3501         (setq overlay-arrow-position (make-marker)))
3502       (setq overlay-arrow-string "=>"
3503             overlay-arrow-position (set-marker overlay-arrow-position
3504                                                (point)
3505                                                (current-buffer))))))
3506
3507 (defun gnus-summary-setup-buffer (group)
3508   "Initialize summary buffer.
3509 If the setup was successful, non-nil is returned."
3510   (let ((buffer (gnus-summary-buffer-name group))
3511         (dead-name (concat "*Dead Summary "
3512                            (gnus-group-decoded-name group) "*")))
3513     ;; If a dead summary buffer exists, we kill it.
3514     (when (gnus-buffer-live-p dead-name)
3515       (gnus-kill-buffer dead-name))
3516     (if (get-buffer buffer)
3517         (progn
3518           (set-buffer buffer)
3519           (setq gnus-summary-buffer (current-buffer))
3520           (not gnus-newsgroup-prepared))
3521       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3522       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3523       (gnus-summary-mode group)
3524       (when (gnus-group-quit-config group)
3525         (set (make-local-variable 'gnus-single-article-buffer) nil))
3526       (make-local-variable 'gnus-article-buffer)
3527       (make-local-variable 'gnus-article-current)
3528       (make-local-variable 'gnus-original-article-buffer)
3529       (setq gnus-newsgroup-name group)
3530       ;; Set any local variables in the group parameters.
3531       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3532       t)))
3533
3534 (defun gnus-set-global-variables ()
3535   "Set the global equivalents of the buffer-local variables.
3536 They are set to the latest values they had.  These reflect the summary
3537 buffer that was in action when the last article was fetched."
3538   (when (eq major-mode 'gnus-summary-mode)
3539     (setq gnus-summary-buffer (current-buffer))
3540     (let ((name gnus-newsgroup-name)
3541           (marked gnus-newsgroup-marked)
3542           (spam gnus-newsgroup-spam-marked)
3543           (unread gnus-newsgroup-unreads)
3544           (headers gnus-current-headers)
3545           (data gnus-newsgroup-data)
3546           (summary gnus-summary-buffer)
3547           (article-buffer gnus-article-buffer)
3548           (original gnus-original-article-buffer)
3549           (gac gnus-article-current)
3550           (reffed gnus-reffed-article-number)
3551           (score-file gnus-current-score-file)
3552           (default-charset gnus-newsgroup-charset)
3553           vlist)
3554       (let ((locals gnus-newsgroup-variables))
3555         (while locals
3556           (if (consp (car locals))
3557               (push (eval (caar locals)) vlist)
3558             (push (eval (car locals)) vlist))
3559           (setq locals (cdr locals)))
3560         (setq vlist (nreverse vlist)))
3561       (with-current-buffer gnus-group-buffer
3562         (setq gnus-newsgroup-name name
3563               gnus-newsgroup-marked marked
3564               gnus-newsgroup-spam-marked spam
3565               gnus-newsgroup-unreads unread
3566               gnus-current-headers headers
3567               gnus-newsgroup-data data
3568               gnus-article-current gac
3569               gnus-summary-buffer summary
3570               gnus-article-buffer article-buffer
3571               gnus-original-article-buffer original
3572               gnus-reffed-article-number reffed
3573               gnus-current-score-file score-file
3574               gnus-newsgroup-charset default-charset)
3575         (let ((locals gnus-newsgroup-variables))
3576           (while locals
3577             (if (consp (car locals))
3578                 (set (caar locals) (pop vlist))
3579               (set (car locals) (pop vlist)))
3580             (setq locals (cdr locals))))
3581         ;; The article buffer also has local variables.
3582         (when (gnus-buffer-live-p gnus-article-buffer)
3583           (set-buffer gnus-article-buffer)
3584           (setq gnus-summary-buffer summary))))))
3585
3586 (defun gnus-summary-article-unread-p (article)
3587   "Say whether ARTICLE is unread or not."
3588   (memq article gnus-newsgroup-unreads))
3589
3590 (defun gnus-summary-first-article-p (&optional article)
3591   "Return whether ARTICLE is the first article in the buffer."
3592   (if (not (setq article (or article (gnus-summary-article-number))))
3593       nil
3594     (eq article (caar gnus-newsgroup-data))))
3595
3596 (defun gnus-summary-last-article-p (&optional article)
3597   "Return whether ARTICLE is the last article in the buffer."
3598   (if (not (setq article (or article (gnus-summary-article-number))))
3599       ;; All non-existent numbers are the last article.  :-)
3600       t
3601     (not (cdr (gnus-data-find-list article)))))
3602
3603 (defun gnus-make-thread-indent-array (&optional n)
3604   (when (or n
3605             (progn (setq n 200) nil)
3606             (null gnus-thread-indent-array)
3607             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3608     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3609           gnus-thread-indent-array-level gnus-thread-indent-level)
3610     (while (>= n 0)
3611       (aset gnus-thread-indent-array n
3612             (make-string (* n gnus-thread-indent-level) ? ))
3613       (setq n (1- n)))))
3614
3615 (defun gnus-update-summary-mark-positions ()
3616   "Compute where the summary marks are to go."
3617   (save-excursion
3618     (when (gnus-buffer-exists-p gnus-summary-buffer)
3619       (set-buffer gnus-summary-buffer))
3620     (let ((spec gnus-summary-line-format-spec)
3621           pos)
3622       (save-excursion
3623         (gnus-set-work-buffer)
3624         (let ((gnus-tmp-unread ?Z)
3625               (gnus-replied-mark ?Z)
3626               (gnus-score-below-mark ?Z)
3627               (gnus-score-over-mark ?Z)
3628               (gnus-undownloaded-mark ?Z)
3629               (gnus-summary-line-format-spec spec)
3630               (gnus-newsgroup-downloadable '(0))
3631               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3632               case-fold-search ignores)
3633           ;; Here, all marks are bound to Z.
3634           (gnus-summary-insert-line header
3635                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3636           (goto-char (point-min))
3637           ;; Memorize the positions of the same characters as dummy marks.
3638           (while (re-search-forward "[A-D]" nil t)
3639             (push (point) ignores))
3640           (erase-buffer)
3641           ;; We use A-D as dummy marks in order to know column positions
3642           ;; where marks should be inserted.
3643           (setq gnus-tmp-unread ?A
3644                 gnus-replied-mark ?B
3645                 gnus-score-below-mark ?C
3646                 gnus-score-over-mark ?C
3647                 gnus-undownloaded-mark ?D)
3648           (gnus-summary-insert-line header
3649                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3650           ;; Ignore characters which aren't dummy marks.
3651           (dolist (p ignores)
3652             (delete-region (goto-char (1- p)) p)
3653             (insert ?Z))
3654           (goto-char (point-min))
3655           (setq pos (list (cons 'unread
3656                                 (and (search-forward "A" nil t)
3657                                      (- (point) (point-min) 1)))))
3658           (goto-char (point-min))
3659           (push (cons 'replied (and (search-forward "B" nil t)
3660                                     (- (point) (point-min) 1)))
3661                 pos)
3662           (goto-char (point-min))
3663           (push (cons 'score (and (search-forward "C" nil t)
3664                                   (- (point) (point-min) 1)))
3665                 pos)
3666           (goto-char (point-min))
3667           (push (cons 'download (and (search-forward "D" nil t)
3668                                      (- (point) (point-min) 1)))
3669                 pos)))
3670       (setq gnus-summary-mark-positions pos))))
3671
3672 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3673   "Insert a dummy root in the summary buffer."
3674   (beginning-of-line)
3675   (gnus-add-text-properties
3676    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3677    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3678
3679 (defun gnus-summary-extract-address-component (from)
3680   (or (car (funcall gnus-extract-address-components from))
3681       from))
3682
3683 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3684   (let ((mail-parse-charset gnus-newsgroup-charset)
3685         (ignored-from-addresses (gnus-ignored-from-addresses))
3686         ; Is it really necessary to do this next part for each summary line?
3687         ; Luckily, doesn't seem to slow things down much.
3688         (mail-parse-ignored-charsets
3689          (with-current-buffer gnus-summary-buffer
3690            gnus-newsgroup-ignored-charsets)))
3691     (or
3692      (and ignored-from-addresses
3693           (string-match ignored-from-addresses gnus-tmp-from)
3694           (let ((extra-headers (mail-header-extra header))
3695                 to
3696                 newsgroups)
3697             (cond
3698              ((setq to (cdr (assq 'To extra-headers)))
3699               (concat gnus-summary-to-prefix
3700                       (inline
3701                         (gnus-summary-extract-address-component
3702                          (funcall gnus-decode-encoded-address-function to)))))
3703              ((setq newsgroups
3704                     (or
3705                      (cdr (assq 'Newsgroups extra-headers))
3706                      (and
3707                       (memq 'Newsgroups gnus-extra-headers)
3708                       (eq (car (gnus-find-method-for-group
3709                                 gnus-newsgroup-name)) 'nntp)
3710                       (gnus-group-real-name gnus-newsgroup-name))))
3711               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3712      (gnus-string-mark-left-to-right
3713       (inline
3714        (gnus-summary-extract-address-component gnus-tmp-from))))))
3715
3716 (defun gnus-summary-insert-line (gnus-tmp-header
3717                                  gnus-tmp-level gnus-tmp-current
3718                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3719                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3720                                  &optional gnus-tmp-dummy gnus-tmp-score
3721                                  gnus-tmp-process)
3722   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3723       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3724                                           gnus-tmp-level)))
3725   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3726          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3727          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3728          (gnus-tmp-score-char
3729           (if (or (null gnus-summary-default-score)
3730                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3731                       gnus-summary-zcore-fuzz))
3732               ?                         ;Whitespace
3733             (if (< gnus-tmp-score gnus-summary-default-score)
3734                 gnus-score-below-mark gnus-score-over-mark)))
3735          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3736          (gnus-tmp-replied
3737           (cond (gnus-tmp-process gnus-process-mark)
3738                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3739                  gnus-cached-mark)
3740                 (gnus-tmp-replied gnus-replied-mark)
3741                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3742                  gnus-forwarded-mark)
3743                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3744                  gnus-saved-mark)
3745                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3746                  gnus-unseen-mark)
3747                 (t gnus-no-mark)))
3748          (gnus-tmp-downloaded
3749           (cond (undownloaded
3750                  gnus-undownloaded-mark)
3751                 (gnus-newsgroup-agentized
3752                  gnus-downloaded-mark)
3753                 (t
3754                  gnus-no-mark)))
3755          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3756          (gnus-tmp-name
3757           (cond
3758            ((string-match "<[^>]+> *$" gnus-tmp-from)
3759             (let ((beg (match-beginning 0)))
3760               (or (and (string-match "^\".+\"" gnus-tmp-from)
3761                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3762                   (substring gnus-tmp-from 0 beg))))
3763            ((string-match "(.+)" gnus-tmp-from)
3764             (substring gnus-tmp-from
3765                        (1+ (match-beginning 0)) (1- (match-end 0))))
3766            (t gnus-tmp-from)))
3767          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3768          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3769          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3770          (inhibit-read-only t))
3771     (when (string= gnus-tmp-name "")
3772       (setq gnus-tmp-name gnus-tmp-from))
3773     (unless (numberp gnus-tmp-lines)
3774       (setq gnus-tmp-lines -1))
3775     (if (= gnus-tmp-lines -1)
3776         (setq gnus-tmp-lines "?")
3777       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3778     (condition-case ()
3779         (gnus-put-text-property
3780          (point)
3781          (progn (eval gnus-summary-line-format-spec) (point))
3782          'gnus-number gnus-tmp-number)
3783       (error (gnus-message 5 "Error updating the summary line")))
3784     (when (gnus-visual-p 'summary-highlight 'highlight)
3785       (forward-line -1)
3786       (gnus-summary-highlight-line)
3787       (gnus-run-hooks 'gnus-summary-update-hook)
3788       (forward-line 1))))
3789
3790 (defun gnus-summary-update-line (&optional dont-update)
3791   "Update summary line after change."
3792   (when (and gnus-summary-default-score
3793              (not gnus-summary-inhibit-highlight))
3794     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3795            (article (gnus-summary-article-number))
3796            (score (gnus-summary-article-score article)))
3797       (unless dont-update
3798         (if (and gnus-summary-mark-below
3799                  (< (gnus-summary-article-score)
3800                     gnus-summary-mark-below))
3801             ;; This article has a low score, so we mark it as read.
3802             (when (memq article gnus-newsgroup-unreads)
3803               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3804           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3805             ;; This article was previously marked as read on account
3806             ;; of a low score, but now it has risen, so we mark it as
3807             ;; unread.
3808             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3809         (gnus-summary-update-mark
3810          (if (or (null gnus-summary-default-score)
3811                  (<= (abs (- score gnus-summary-default-score))
3812                      gnus-summary-zcore-fuzz))
3813              ?                          ;Whitespace
3814            (if (< score gnus-summary-default-score)
3815                gnus-score-below-mark gnus-score-over-mark))
3816          'score))
3817       ;; Do visual highlighting.
3818       (when (gnus-visual-p 'summary-highlight 'highlight)
3819         (gnus-summary-highlight-line)
3820         (gnus-run-hooks 'gnus-summary-update-hook)))))
3821
3822 (defvar gnus-tmp-new-adopts nil)
3823
3824 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3825   "Return the number of articles in THREAD.
3826 This may be 0 in some cases -- if none of the articles in
3827 the thread are to be displayed."
3828   (let* ((number
3829          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3830           (cond
3831            ((not (listp thread))
3832             1)
3833            ((and (consp thread) (cdr thread))
3834             (apply
3835              '+ 1 (mapcar
3836                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3837            ((null thread)
3838             1)
3839            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3840             1)
3841            (t 0))))
3842     (when (and level (zerop level) gnus-tmp-new-adopts)
3843       (incf number
3844             (apply '+ (mapcar
3845                        'gnus-summary-number-of-articles-in-thread
3846                        gnus-tmp-new-adopts))))
3847     (if char
3848         (if (> number 1) gnus-not-empty-thread-mark
3849           gnus-empty-thread-mark)
3850       number)))
3851
3852 (defsubst gnus-summary-line-message-size (head)
3853   "Return pretty-printed version of message size.
3854 This function is intended to be used in
3855 `gnus-summary-line-format-alist'."
3856   (let ((c (or (mail-header-chars head) -1)))
3857     (cond ((< c 0) "n/a")               ; chars not available
3858           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3859           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3860           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3861           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3862
3863 (defcustom gnus-user-date-format-alist
3864   '(((gnus-seconds-today) . "Today, %H:%M")
3865     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3866     (604800 . "%A %H:%M")               ; That's one week
3867     ((gnus-seconds-month) . "%A %d")
3868     ((gnus-seconds-year) . "%B %d")
3869     (t . "%b %d %Y"))                   ; This one is used when no other
3870                                         ; does match
3871   "Specifies date format depending on age of article.
3872 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3873 seconds) or a Lisp expression evaluating to a number.  When the age of
3874 the article is less than this number, then use `format-time-string'
3875 with the corresponding FORMAT for displaying the date of the article.
3876 If AGE is not a number or a Lisp expression evaluating to a
3877 non-number, then the corresponding FORMAT is used as a default value.
3878
3879 Note that the list is processed from the beginning, so it should be
3880 sorted by ascending AGE.  Also note that items following the first
3881 non-number AGE will be ignored.
3882
3883 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3884 and `gnus-seconds-year' in the AGE spec.  They return the number of
3885 seconds passed since the start of today, of this month, of this year,
3886 respectively."
3887   :version "24.1"
3888   :group 'gnus-summary-format
3889   :type '(alist :key-type sexp :value-type string))
3890
3891 (defun gnus-user-date (messy-date)
3892   "Format the messy-date according to `gnus-user-date-format-alist'.
3893 Returns \"  ?  \" if there's bad input or if another error occurs.
3894 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3895   (condition-case ()
3896       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3897              (now (gnus-float-time))
3898              ;;If we don't find something suitable we'll use this one
3899              (my-format "%b %d '%y"))
3900         (let* ((difference (- now messy-date))
3901                (templist gnus-user-date-format-alist)
3902                (top (eval (caar templist))))
3903           (while (if (numberp top) (< top difference) (not top))
3904             (progn
3905               (setq templist (cdr templist))
3906               (setq top (eval (caar templist)))))
3907           (if (stringp (cdr (car templist)))
3908               (setq my-format (cdr (car templist)))))
3909         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3910     (error "  ?   ")))
3911
3912 (defun gnus-summary-set-local-parameters (group)
3913   "Go through the local params of GROUP and set all variable specs in that list."
3914   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3915                                         ; really variables.
3916     (dolist (elem (gnus-group-find-parameter group))
3917       (and (consp elem)                 ; Has to be a cons.
3918            (consp (cdr elem))           ; The cdr has to be a list.
3919            (symbolp (car elem))         ; Has to be a symbol in there.
3920            (not (memq (car elem) vars))
3921            (ignore-errors
3922              (push (car elem) vars)
3923              ;; Variables like `gnus-show-threads' that are globally
3924              ;; bound, if used as group parameters, need to get to be
3925              ;; buffer-local, whereas just parameters like `gcc-self',
3926              ;; `timestamp', etc. should not be bound as variables.
3927              (if (boundp (car elem))
3928                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3929                (eval (nth 1 elem))))))))
3930
3931 (defun gnus-summary-read-group (group &optional show-all no-article
3932                                       kill-buffer no-display backward
3933                                       select-articles)
3934   "Start reading news in newsgroup GROUP.
3935 If SHOW-ALL is non-nil, already read articles are also listed.
3936 If NO-ARTICLE is non-nil, no article is selected initially.
3937 If NO-DISPLAY, don't generate the summary buffer contents.
3938 If KILL-BUFFER, it should be a buffer that's killed once the new
3939 summary buffer has been generated.
3940 If BACKWARD, move point to the previous group in the group buffer
3941 If SELECT-ARTICLES, only select those articles from GROUP."
3942   (let (result)
3943     (while (and group
3944                 (null (setq result
3945                             (let ((gnus-auto-select-next nil))
3946                               (or (gnus-summary-read-group-1
3947                                    group show-all no-article
3948                                    kill-buffer no-display
3949                                    select-articles)
3950                                   (setq show-all nil
3951                                         select-articles nil)))))
3952                 (eq gnus-auto-select-next 'quietly))
3953       (set-buffer gnus-group-buffer)
3954       ;; The entry function called above goes to the next
3955       ;; group automatically, so we go two groups back
3956       ;; if we are searching for the previous group.
3957       (when backward
3958         (gnus-group-prev-unread-group 2))
3959       (if (not (equal group (gnus-group-group-name)))
3960           (setq group (gnus-group-group-name))
3961         (setq group nil)))
3962     result))
3963
3964 (defun gnus-summary-read-group-1 (group show-all no-article
3965                                         kill-buffer no-display
3966                                         &optional select-articles)
3967   ;; Killed foreign groups can't be entered.
3968   ;;  (when (and (not (gnus-group-native-p group))
3969   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3970   ;;    (error "Dead non-native groups can't be entered"))
3971   (gnus-message 7 "Retrieving newsgroup: %s..."
3972                 (gnus-group-decoded-name group))
3973   (let* ((new-group (gnus-summary-setup-buffer group))
3974          (quit-config (gnus-group-quit-config group))
3975          (did-select (and new-group (gnus-select-newsgroup
3976                                      group show-all select-articles))))
3977     (cond
3978      ;; This summary buffer exists already, so we just select it.
3979      ((not new-group)
3980       (gnus-set-global-variables)
3981       (when kill-buffer
3982         (gnus-kill-or-deaden-summary kill-buffer))
3983       (gnus-configure-windows 'summary 'force)
3984       (gnus-set-mode-line 'summary)
3985       (gnus-summary-position-point)
3986       (message "")
3987       t)
3988      ;; We couldn't select this group.
3989      ((null did-select)
3990       (when (and (eq major-mode 'gnus-summary-mode)
3991                  (not (equal (current-buffer) kill-buffer)))
3992         (kill-buffer (current-buffer))
3993         (if (not quit-config)
3994             (progn
3995               ;; Update the info -- marks might need to be removed,
3996               ;; for instance.
3997               (gnus-summary-update-info)
3998               (set-buffer gnus-group-buffer)
3999               (gnus-group-jump-to-group group)
4000               (gnus-group-next-unread-group 1))
4001           (gnus-handle-ephemeral-exit quit-config)))
4002       (if (null (gnus-list-of-unread-articles group))
4003           (gnus-message 3 "Group %s contains no messages" group)
4004         (gnus-message 3 "Can't select group"))
4005       nil)
4006      ;; The user did a `C-g' while prompting for number of articles,
4007      ;; so we exit this group.
4008      ((eq did-select 'quit)
4009       (and (eq major-mode 'gnus-summary-mode)
4010            (not (equal (current-buffer) kill-buffer))
4011            (kill-buffer (current-buffer)))
4012       (when kill-buffer
4013         (gnus-kill-or-deaden-summary kill-buffer))
4014       (if (not quit-config)
4015           (progn
4016             (set-buffer gnus-group-buffer)
4017             (gnus-group-jump-to-group group)
4018             (gnus-configure-windows 'group 'force))
4019         (gnus-handle-ephemeral-exit quit-config))
4020       ;; Finally signal the quit.
4021       (signal 'quit nil))
4022      ;; The group was successfully selected.
4023      (t
4024       (gnus-set-global-variables)
4025       ;; Save the active value in effect when the group was entered.
4026       (setq gnus-newsgroup-active
4027             (gnus-copy-sequence
4028              (gnus-active gnus-newsgroup-name)))
4029       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4030       ;; You can change the summary buffer in some way with this hook.
4031       (gnus-run-hooks 'gnus-select-group-hook)
4032       (when (memq 'summary (gnus-update-format-specifications
4033                             nil 'summary 'summary-mode 'summary-dummy))
4034         ;; The format specification for the summary line was updated,
4035         ;; so we need to update the mark positions as well.
4036         (gnus-update-summary-mark-positions))
4037       ;; Do score processing.
4038       (when gnus-use-scoring
4039         (gnus-possibly-score-headers))
4040       ;; Check whether to fill in the gaps in the threads.
4041       (when gnus-build-sparse-threads
4042         (gnus-build-sparse-threads))
4043       ;; Find the initial limit.
4044       (if show-all
4045           (let ((gnus-newsgroup-dormant nil))
4046             (gnus-summary-initial-limit show-all))
4047         (gnus-summary-initial-limit show-all))
4048       ;; Generate the summary buffer.
4049       (unless no-display
4050         (gnus-summary-prepare))
4051       (when gnus-use-trees
4052         (gnus-tree-open group)
4053         (setq gnus-summary-highlight-line-function
4054               'gnus-tree-highlight-article))
4055       ;; If the summary buffer is empty, but there are some low-scored
4056       ;; articles or some excluded dormants, we include these in the
4057       ;; buffer.
4058       (when (and (zerop (buffer-size))
4059                  (not no-display))
4060         (cond (gnus-newsgroup-dormant
4061                (gnus-summary-limit-include-dormant))
4062               ((and gnus-newsgroup-scored show-all)
4063                (gnus-summary-limit-include-expunged t))))
4064       ;; Function `gnus-apply-kill-file' must be called in this hook.
4065       (gnus-run-hooks 'gnus-apply-kill-hook)
4066       (if (and (zerop (buffer-size))
4067                (not no-display))
4068           (progn
4069             ;; This newsgroup is empty.
4070             (gnus-summary-catchup-and-exit nil t)
4071             (gnus-message 6 "No unread news")
4072             (when kill-buffer
4073               (gnus-kill-or-deaden-summary kill-buffer))
4074             ;; Return nil from this function.
4075             nil)
4076         ;; Hide conversation thread subtrees.  We cannot do this in
4077         ;; gnus-summary-prepare-hook since kill processing may not
4078         ;; work with hidden articles.
4079         (gnus-summary-maybe-hide-threads)
4080         (gnus-configure-windows 'summary)
4081         (when kill-buffer
4082           (gnus-kill-or-deaden-summary kill-buffer))
4083         (gnus-summary-auto-select-subject)
4084         ;; Show first unread article if requested.
4085         (if (and (not no-article)
4086                  (not no-display)
4087                  gnus-newsgroup-unreads
4088                  gnus-auto-select-first)
4089             (progn
4090               (let ((art (gnus-summary-article-number)))
4091                 (unless (and (not gnus-plugged)
4092                              (or (memq art gnus-newsgroup-undownloaded)
4093                                  (memq art gnus-newsgroup-downloadable)))
4094                   (gnus-summary-goto-article art))))
4095           ;; Don't select any articles.
4096           (gnus-summary-position-point)
4097           (gnus-configure-windows 'summary 'force)
4098           (gnus-set-mode-line 'summary))
4099         (when (and gnus-auto-center-group
4100                    (get-buffer-window gnus-group-buffer t))
4101           ;; Gotta use windows, because recenter does weird stuff if
4102           ;; the current buffer ain't the displayed window.
4103           (let ((owin (selected-window)))
4104             (select-window (get-buffer-window gnus-group-buffer t))
4105             (when (gnus-group-goto-group group)
4106               (recenter))
4107             (select-window owin)))
4108         ;; Mark this buffer as "prepared".
4109         (setq gnus-newsgroup-prepared t)
4110         (gnus-run-hooks 'gnus-summary-prepared-hook)
4111         (unless (gnus-ephemeral-group-p group)
4112           (gnus-group-update-group group nil t))
4113         t)))))
4114
4115 (defun gnus-summary-auto-select-subject ()
4116   "Select the subject line on initial group entry."
4117   (goto-char (point-min))
4118   (cond
4119    ((eq gnus-auto-select-subject 'best)
4120     (gnus-summary-best-unread-subject))
4121    ((eq gnus-auto-select-subject 'unread)
4122     (gnus-summary-first-unread-subject))
4123    ((eq gnus-auto-select-subject 'unseen)
4124     (gnus-summary-first-unseen-subject))
4125    ((eq gnus-auto-select-subject 'unseen-or-unread)
4126     (gnus-summary-first-unseen-or-unread-subject))
4127    ((eq gnus-auto-select-subject 'first)
4128     ;; Do nothing.
4129     )
4130    ((functionp gnus-auto-select-subject)
4131     (funcall gnus-auto-select-subject))))
4132
4133 (defun gnus-summary-prepare ()
4134   "Generate the summary buffer."
4135   (interactive)
4136   (let ((inhibit-read-only t))
4137     (erase-buffer)
4138     (setq gnus-newsgroup-data nil
4139           gnus-newsgroup-data-reverse nil)
4140     (gnus-run-hooks 'gnus-summary-generate-hook)
4141     ;; Generate the buffer, either with threads or without.
4142     (when gnus-newsgroup-headers
4143       (gnus-summary-prepare-threads
4144        (if gnus-show-threads
4145            (gnus-sort-gathered-threads
4146             (funcall gnus-summary-thread-gathering-function
4147                      (gnus-sort-threads
4148                       (gnus-cut-threads (gnus-make-threads)))))
4149          ;; Unthreaded display.
4150          (gnus-sort-articles gnus-newsgroup-headers))))
4151     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4152     ;; Call hooks for modifying summary buffer.
4153     (goto-char (point-min))
4154     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4155
4156 (defsubst gnus-general-simplify-subject (subject)
4157   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4158   (setq subject
4159         (cond
4160          ;; Truncate the subject.
4161          (gnus-simplify-subject-functions
4162           (gnus-map-function gnus-simplify-subject-functions subject))
4163          ((numberp gnus-summary-gather-subject-limit)
4164           (setq subject (gnus-simplify-subject-re subject))
4165           (if (> (length subject) gnus-summary-gather-subject-limit)
4166               (substring subject 0 gnus-summary-gather-subject-limit)
4167             subject))
4168          ;; Fuzzily simplify it.
4169          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4170           (gnus-simplify-subject-fuzzy subject))
4171          ;; Just remove the leading "Re:".
4172          (t
4173           (gnus-simplify-subject-re subject))))
4174
4175   (if (and gnus-summary-gather-exclude-subject
4176            (string-match gnus-summary-gather-exclude-subject subject))
4177       nil                         ; This article shouldn't be gathered
4178     subject))
4179
4180 (defun gnus-summary-simplify-subject-query ()
4181   "Query where the respool algorithm would put this article."
4182   (interactive)
4183   (gnus-summary-select-article)
4184   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4185
4186 (defun gnus-gather-threads-by-subject (threads)
4187   "Gather threads by looking at Subject headers."
4188   (if (not gnus-summary-make-false-root)
4189       threads
4190     (let ((hashtb (gnus-make-hashtable 1024))
4191           (prev threads)
4192           (result threads)
4193           subject hthread whole-subject)
4194       (while threads
4195         (setq subject (gnus-general-simplify-subject
4196                        (setq whole-subject (mail-header-subject
4197                                             (caar threads)))))
4198         (when subject
4199           (if (setq hthread (gnus-gethash subject hashtb))
4200               (progn
4201                 ;; We enter a dummy root into the thread, if we
4202                 ;; haven't done that already.
4203                 (unless (stringp (caar hthread))
4204                   (setcar hthread (list whole-subject (car hthread))))
4205                 ;; We add this new gathered thread to this gathered
4206                 ;; thread.
4207                 (setcdr (car hthread)
4208                         (nconc (cdar hthread) (list (car threads))))
4209                 ;; Remove it from the list of threads.
4210                 (setcdr prev (cdr threads))
4211                 (setq threads prev))
4212             ;; Enter this thread into the hash table.
4213             (gnus-sethash subject
4214                           (if gnus-summary-make-false-root-always
4215                               (progn
4216                                 ;; If you want a dummy root above all
4217                                 ;; threads...
4218                                 (setcar threads (list whole-subject
4219                                                       (car threads)))
4220                                 threads)
4221                             threads)
4222                           hashtb)))
4223         (setq prev threads)
4224         (setq threads (cdr threads)))
4225       result)))
4226
4227 (defun gnus-gather-threads-by-references (threads)
4228   "Gather threads by looking at References headers."
4229   (let ((idhashtb (gnus-make-hashtable 1024))
4230         (thhashtb (gnus-make-hashtable 1024))
4231         (prev threads)
4232         (result threads)
4233         ids references id gthread gid entered ref)
4234     (while threads
4235       (when (setq references (mail-header-references (caar threads)))
4236         (setq id (mail-header-id (caar threads))
4237               ids (inline (gnus-split-references references))
4238               entered nil)
4239         (while (setq ref (pop ids))
4240           (setq ids (delete ref ids))
4241           (if (not (setq gid (gnus-gethash ref idhashtb)))
4242               (progn
4243                 (gnus-sethash ref id idhashtb)
4244                 (gnus-sethash id threads thhashtb))
4245             (setq gthread (gnus-gethash gid thhashtb))
4246             (unless entered
4247               ;; We enter a dummy root into the thread, if we
4248               ;; haven't done that already.
4249               (unless (stringp (caar gthread))
4250                 (setcar gthread (list (mail-header-subject (caar gthread))
4251                                       (car gthread))))
4252               ;; We add this new gathered thread to this gathered
4253               ;; thread.
4254               (setcdr (car gthread)
4255                       (nconc (cdar gthread) (list (car threads)))))
4256             ;; Add it into the thread hash table.
4257             (gnus-sethash id gthread thhashtb)
4258             (setq entered t)
4259             ;; Remove it from the list of threads.
4260             (setcdr prev (cdr threads))
4261             (setq threads prev))))
4262       (setq prev threads)
4263       (setq threads (cdr threads)))
4264     result))
4265
4266 (defun gnus-sort-gathered-threads (threads)
4267   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4268   (let ((result threads))
4269     (while threads
4270       (when (stringp (caar threads))
4271         (setcdr (car threads)
4272                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4273       (setq threads (cdr threads)))
4274     result))
4275
4276 (defun gnus-thread-loop-p (root thread)
4277   "Say whether ROOT is in THREAD."
4278   (let ((stack (list thread))
4279         (infloop 0)
4280         th)
4281     (while (setq thread (pop stack))
4282       (setq th (cdr thread))
4283       (while (and th
4284                   (not (eq (caar th) root)))
4285         (pop th))
4286       (if th
4287           ;; We have found a loop.
4288           (let (ref-dep)
4289             (setcdr thread (delq (car th) (cdr thread)))
4290             (if (boundp (setq ref-dep (intern "none"
4291                                               gnus-newsgroup-dependencies)))
4292                 (setcdr (symbol-value ref-dep)
4293                         (nconc (cdr (symbol-value ref-dep))
4294                                (list (car th))))
4295               (set ref-dep (list nil (car th))))
4296             (setq infloop 1
4297                   stack nil))
4298         ;; Push all the subthreads onto the stack.
4299         (push (cdr thread) stack)))
4300     infloop))
4301
4302 (defun gnus-make-threads ()
4303   "Go through the dependency hashtb and find the roots.  Return all threads."
4304   (let (threads)
4305     (while (catch 'infloop
4306              (mapatoms
4307               (lambda (refs)
4308                 ;; Deal with self-referencing References loops.
4309                 (when (and (car (symbol-value refs))
4310                            (not (zerop
4311                                  (apply
4312                                   '+
4313                                   (mapcar
4314                                    (lambda (thread)
4315                                      (gnus-thread-loop-p
4316                                       (car (symbol-value refs)) thread))
4317                                    (cdr (symbol-value refs)))))))
4318                   (setq threads nil)
4319                   (throw 'infloop t))
4320                 (unless (car (symbol-value refs))
4321                   ;; These threads do not refer back to any other
4322                   ;; articles, so they're roots.
4323                   (setq threads (append (cdr (symbol-value refs)) threads))))
4324               gnus-newsgroup-dependencies)))
4325     threads))
4326
4327 ;; Build the thread tree.
4328 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4329   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4330
4331 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4332 if it was already present.
4333
4334 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4335 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4336 Message-IDs will be renamed to a unique Message-ID before being
4337 entered.
4338
4339 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4340   (let* ((id (mail-header-id header))
4341          (id-dep (and id (intern id dependencies)))
4342          parent-id ref ref-dep ref-header replaced)
4343     ;; Enter this `header' in the `dependencies' table.
4344     (cond
4345      ((not id-dep)
4346       (setq header nil))
4347      ;; The first two cases do the normal part: enter a new `header'
4348      ;; in the `dependencies' table.
4349      ((not (boundp id-dep))
4350       (set id-dep (list header)))
4351      ((null (car (symbol-value id-dep)))
4352       (setcar (symbol-value id-dep) header))
4353
4354      ;; From here the `header' was already present in the
4355      ;; `dependencies' table.
4356      (force-new
4357       ;; Overrides an existing entry;
4358       ;; just set the header part of the entry.
4359       (setcar (symbol-value id-dep) header)
4360       (setq replaced t))
4361
4362      ;; Renames the existing `header' to a unique Message-ID.
4363      ((not gnus-summary-ignore-duplicates)
4364       ;; An article with this Message-ID has already been seen.
4365       ;; We rename the Message-ID.
4366       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4367            (list header))
4368       (mail-header-set-id header id))
4369
4370      ;; The last case ignores an existing entry, except it adds any
4371      ;; additional Xrefs (in case the two articles came from different
4372      ;; servers.
4373      ;; Also sets `header' to `nil' meaning that the `dependencies'
4374      ;; table was *not* modified.
4375      (t
4376       (mail-header-set-xref
4377        (car (symbol-value id-dep))
4378        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4379                    "")
4380                (or (mail-header-xref header) "")))
4381       (setq header nil)))
4382
4383     (when (and header (not replaced))
4384       ;; First check that we are not creating a References loop.
4385       (setq parent-id (gnus-parent-id (mail-header-references header)))
4386       (setq ref parent-id)
4387       (while (and ref
4388                   (setq ref-dep (intern-soft ref dependencies))
4389                   (boundp ref-dep)
4390                   (setq ref-header (car (symbol-value ref-dep))))
4391         (if (string= id ref)
4392             ;; Yuk!  This is a reference loop.  Make the article be a
4393             ;; root article.
4394             (progn
4395               (mail-header-set-references (car (symbol-value id-dep)) "none")
4396               (setq ref nil)
4397               (setq parent-id nil))
4398           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4399       (setq ref-dep (intern (or parent-id "none") dependencies))
4400       (if (boundp ref-dep)
4401           (setcdr (symbol-value ref-dep)
4402                   (nconc (cdr (symbol-value ref-dep))
4403                          (list (symbol-value id-dep))))
4404         (set ref-dep (list nil (symbol-value id-dep)))))
4405     header))
4406
4407 (defun gnus-extract-message-id-from-in-reply-to (string)
4408   (if (string-match "<[^>]+>" string)
4409       (substring string (match-beginning 0) (match-end 0))
4410     nil))
4411
4412 (defun gnus-build-sparse-threads ()
4413   (let ((headers gnus-newsgroup-headers)
4414         (mail-parse-charset gnus-newsgroup-charset)
4415         (gnus-summary-ignore-duplicates t)
4416         header references generation relations
4417         subject child end new-child date)
4418     ;; First we create an alist of generations/relations, where
4419     ;; generations is how much we trust the relation, and the relation
4420     ;; is parent/child.
4421     (gnus-message 7 "Making sparse threads...")
4422     (save-excursion
4423       (nnheader-set-temp-buffer " *gnus sparse threads*")
4424       (while (setq header (pop headers))
4425         (when (and (setq references (mail-header-references header))
4426                    (not (string= references "")))
4427           (insert references)
4428           (setq child (mail-header-id header)
4429                 subject (mail-header-subject header)
4430                 date (mail-header-date header)
4431                 generation 0)
4432           (while (search-backward ">" nil t)
4433             (setq end (1+ (point)))
4434             (when (search-backward "<" nil t)
4435               (setq new-child (buffer-substring (point) end))
4436               (push (list (incf generation)
4437                           child (setq child new-child)
4438                           subject date)
4439                     relations)))
4440           (when child
4441             (push (list (1+ generation) child nil subject) relations))
4442           (erase-buffer)))
4443       (kill-buffer (current-buffer)))
4444     ;; Sort over trustworthiness.
4445     (dolist (relation (sort relations 'car-less-than-car))
4446       (when (gnus-dependencies-add-header
4447              (make-full-mail-header
4448               gnus-reffed-article-number
4449               (nth 3 relation) "" (or (nth 4 relation) "")
4450               (nth 1 relation)
4451               (or (nth 2 relation) "") 0 0 "")
4452              gnus-newsgroup-dependencies nil)
4453         (push gnus-reffed-article-number gnus-newsgroup-limit)
4454         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4455         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4456               gnus-newsgroup-reads)
4457         (decf gnus-reffed-article-number)))
4458     (gnus-message 7 "Making sparse threads...done")))
4459
4460 (defun gnus-build-old-threads ()
4461   ;; Look at all the articles that refer back to old articles, and
4462   ;; fetch the headers for the articles that aren't there.  This will
4463   ;; build complete threads - if the roots haven't been expired by the
4464   ;; server, that is.
4465   (let ((mail-parse-charset gnus-newsgroup-charset)
4466         id heads)
4467     (mapatoms
4468      (lambda (refs)
4469        (when (not (car (symbol-value refs)))
4470          (setq heads (cdr (symbol-value refs)))
4471          (while heads
4472            (if (memq (mail-header-number (caar heads))
4473                      gnus-newsgroup-dormant)
4474                (setq heads (cdr heads))
4475              (setq id (symbol-name refs))
4476              (while (and (setq id (gnus-build-get-header id))
4477                          (not (car (gnus-id-to-thread id)))))
4478              (setq heads nil)))))
4479      gnus-newsgroup-dependencies)))
4480
4481 (defsubst gnus-remove-odd-characters (string)
4482   "Translate STRING into something that doesn't contain weird characters."
4483   (mm-subst-char-in-string
4484    ?\r ?\-
4485    (mm-subst-char-in-string ?\n ?\- string t) t))
4486
4487 ;; This function has to be called with point after the article number
4488 ;; on the beginning of the line.
4489 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4490   (let ((eol (point-at-eol))
4491         (buffer (current-buffer))
4492         header references in-reply-to)
4493
4494     ;; overview: [num subject from date id refs chars lines misc]
4495     (unwind-protect
4496         (let (x)
4497           (narrow-to-region (point) eol)
4498           (unless (eobp)
4499             (forward-char))
4500
4501           (setq header
4502                 (make-full-mail-header
4503                  number                 ; number
4504                  (condition-case ()     ; subject
4505                      (gnus-remove-odd-characters
4506                       (funcall gnus-decode-encoded-word-function
4507                                (setq x (nnheader-nov-field))))
4508                    (error x))
4509                  (condition-case ()     ; from
4510                      (gnus-remove-odd-characters
4511                       (funcall gnus-decode-encoded-address-function
4512                                (setq x (nnheader-nov-field))))
4513                    (error x))
4514                  (nnheader-nov-field)   ; date
4515                  (nnheader-nov-read-message-id number)  ; id
4516                  (setq references (nnheader-nov-field)) ; refs
4517                  (nnheader-nov-read-integer) ; chars
4518                  (nnheader-nov-read-integer) ; lines
4519                  (unless (eobp)
4520                    (if (looking-at "Xref: ")
4521                        (goto-char (match-end 0)))
4522                    (nnheader-nov-field)) ; Xref
4523                  (nnheader-nov-parse-extra)))) ; extra
4524
4525       (widen))
4526
4527     (when (and (string= references "")
4528                (setq in-reply-to (mail-header-extra header))
4529                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4530       (mail-header-set-references
4531        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4532
4533     (when gnus-alter-header-function
4534       (funcall gnus-alter-header-function header))
4535     (gnus-dependencies-add-header header dependencies force-new)))
4536
4537 (defun gnus-build-get-header (id)
4538   "Look through the buffer of NOV lines and find the header to ID.
4539 Enter this line into the dependencies hash table, and return
4540 the id of the parent article (if any)."
4541   (let ((deps gnus-newsgroup-dependencies)
4542         found header)
4543     (prog1
4544         (with-current-buffer nntp-server-buffer
4545           (let ((case-fold-search nil))
4546             (goto-char (point-min))
4547             (while (and (not found)
4548                         (search-forward id nil t))
4549               (beginning-of-line)
4550               (setq found (looking-at
4551                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4552                                    (regexp-quote id))))
4553               (or found (beginning-of-line 2)))
4554             (when found
4555               (beginning-of-line)
4556               (and
4557                (setq header (gnus-nov-parse-line
4558                              (read (current-buffer)) deps))
4559                (gnus-parent-id (mail-header-references header))))))
4560       (when header
4561         (let ((number (mail-header-number header)))
4562           (push number gnus-newsgroup-limit)
4563           (push header gnus-newsgroup-headers)
4564           (if (memq number gnus-newsgroup-unselected)
4565               (progn
4566                 (setq gnus-newsgroup-unreads
4567                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4568                                                number))
4569                 (setq gnus-newsgroup-unselected
4570                       (delq number gnus-newsgroup-unselected)))
4571             (push number gnus-newsgroup-ancient)))))))
4572
4573 (defun gnus-build-all-threads ()
4574   "Read all the headers."
4575   (let ((gnus-summary-ignore-duplicates t)
4576         (mail-parse-charset gnus-newsgroup-charset)
4577         (dependencies gnus-newsgroup-dependencies)
4578         header article)
4579     (with-current-buffer nntp-server-buffer
4580       (let ((case-fold-search nil))
4581         (goto-char (point-min))
4582         (while (not (eobp))
4583           (ignore-errors
4584             (setq article (read (current-buffer))
4585                   header (gnus-nov-parse-line article dependencies t)))
4586           (when header
4587             (with-current-buffer gnus-summary-buffer
4588               (push header gnus-newsgroup-headers)
4589               (if (memq (setq article (mail-header-number header))
4590                         gnus-newsgroup-unselected)
4591                   (progn
4592                     (setq gnus-newsgroup-unreads
4593                           (gnus-add-to-sorted-list
4594                            gnus-newsgroup-unreads article))
4595                     (setq gnus-newsgroup-unselected
4596                           (delq article gnus-newsgroup-unselected)))
4597                 (push article gnus-newsgroup-ancient)))
4598             (forward-line 1)))))))
4599
4600 (defun gnus-summary-update-article-line (article header)
4601   "Update the line for ARTICLE using HEADER."
4602   (let* ((id (mail-header-id header))
4603          (thread (gnus-id-to-thread id)))
4604     (unless thread
4605       (error "Article in no thread"))
4606     ;; Update the thread.
4607     (setcar thread header)
4608     (gnus-summary-goto-subject article)
4609     (let* ((datal (gnus-data-find-list article))
4610            (data (car datal))
4611            (inhibit-read-only t)
4612            (level (gnus-summary-thread-level)))
4613       (gnus-delete-line)
4614       (let ((inserted (- (point)
4615                          (progn
4616                            (gnus-summary-insert-line
4617                             header level nil
4618                             (memq article gnus-newsgroup-undownloaded)
4619                             (gnus-article-mark article)
4620                             (memq article gnus-newsgroup-replied)
4621                             (memq article gnus-newsgroup-expirable)
4622                             ;; Only insert the Subject string when it's different
4623                             ;; from the previous Subject string.
4624                             (if (and
4625                                  gnus-show-threads
4626                                  (gnus-subject-equal
4627                                   (condition-case ()
4628                                       (mail-header-subject
4629                                        (gnus-data-header
4630                                         (cadr
4631                                          (gnus-data-find-list
4632                                           article
4633                                           (gnus-data-list t)))))
4634                                     ;; Error on the side of excessive subjects.
4635                                     (error ""))
4636                                   (mail-header-subject header)))
4637                                 ""
4638                               (mail-header-subject header))
4639                             nil (cdr (assq article gnus-newsgroup-scored))
4640                             (memq article gnus-newsgroup-processable))
4641                            (point)))))
4642         (when (cdr datal)
4643           (gnus-data-update-list
4644            (cdr datal)
4645            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4646
4647 (defun gnus-summary-update-article (article &optional iheader)
4648   "Update ARTICLE in the summary buffer."
4649   (set-buffer gnus-summary-buffer)
4650   (let* ((header (gnus-summary-article-header article))
4651          (id (mail-header-id header))
4652          (data (gnus-data-find article))
4653          (thread (gnus-id-to-thread id))
4654          (references (mail-header-references header))
4655          (parent
4656           (gnus-id-to-thread
4657            (or (gnus-parent-id
4658                 (when (and references
4659                            (not (equal "" references)))
4660                   references))
4661                "none")))
4662          (inhibit-read-only t)
4663          (old (car thread)))
4664     (when thread
4665       (unless iheader
4666         (setcar thread nil)
4667         (when parent
4668           (delq thread parent)))
4669       (if (gnus-summary-insert-subject id header)
4670           ;; Set the (possibly) new article number in the data structure.
4671           (gnus-data-set-number data (gnus-id-to-article id))
4672         (setcar thread old)
4673         nil))))
4674
4675 (defun gnus-rebuild-thread (id &optional line)
4676   "Rebuild the thread containing ID.
4677 If LINE, insert the rebuilt thread starting on line LINE."
4678   (let ((inhibit-read-only t)
4679         old-pos current thread data)
4680     (if (not gnus-show-threads)
4681         (setq thread (list (car (gnus-id-to-thread id))))
4682       ;; Get the thread this article is part of.
4683       (setq thread (gnus-remove-thread id)))
4684     (setq old-pos (point-at-bol))
4685     (setq current (save-excursion
4686                     (and (re-search-backward "[\r\n]" nil t)
4687                          (gnus-summary-article-number))))
4688     ;; If this is a gathered thread, we have to go some re-gathering.
4689     (when (stringp (car thread))
4690       (let ((subject (car thread))
4691             roots thr)
4692         (setq thread (cdr thread))
4693         (while thread
4694           (unless (memq (setq thr (gnus-id-to-thread
4695                                    (gnus-root-id
4696                                     (mail-header-id (caar thread)))))
4697                         roots)
4698             (push thr roots))
4699           (setq thread (cdr thread)))
4700         ;; We now have all (unique) roots.
4701         (if (= (length roots) 1)
4702             ;; All the loose roots are now one solid root.
4703             (setq thread (car roots))
4704           (setq thread (cons subject (gnus-sort-threads roots))))))
4705     (let (threads)
4706       ;; We then insert this thread into the summary buffer.
4707       (when line
4708         (goto-char (point-min))
4709         (forward-line (1- line)))
4710       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4711         (if gnus-show-threads
4712             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4713           (gnus-summary-prepare-unthreaded thread))
4714         (setq data (nreverse gnus-newsgroup-data))
4715         (setq threads gnus-newsgroup-threads))
4716       ;; We splice the new data into the data structure.
4717       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4718       ;;!!! then we want to insert at the beginning of the buffer.
4719       ;;!!! That happens to be true with Gnus now, but that may
4720       ;;!!! change in the future.  Perhaps.
4721       (gnus-data-enter-list
4722        (if line nil current) data (- (point) old-pos))
4723       (setq gnus-newsgroup-threads
4724             (nconc threads gnus-newsgroup-threads))
4725       (gnus-data-compute-positions))))
4726
4727 (defun gnus-number-to-header (number)
4728   "Return the header for article NUMBER."
4729   (let ((headers gnus-newsgroup-headers))
4730     (while (and headers
4731                 (not (= number (mail-header-number (car headers)))))
4732       (pop headers))
4733     (when headers
4734       (car headers))))
4735
4736 (defun gnus-parent-headers (in-headers &optional generation)
4737   "Return the headers of the GENERATIONth parent of HEADERS."
4738   (unless generation
4739     (setq generation 1))
4740   (let ((parent t)
4741         (headers in-headers)
4742         references)
4743     (while (and parent
4744                 (not (zerop generation))
4745                 (setq references (mail-header-references headers)))
4746       (setq headers (if (and references
4747                              (setq parent (gnus-parent-id references)))
4748                         (car (gnus-id-to-thread parent))
4749                       nil))
4750       (decf generation))
4751     (and (not (eq headers in-headers))
4752          headers)))
4753
4754 (defun gnus-id-to-thread (id)
4755   "Return the (sub-)thread where ID appears."
4756   (gnus-gethash id gnus-newsgroup-dependencies))
4757
4758 (defun gnus-id-to-article (id)
4759   "Return the article number of ID."
4760   (let ((thread (gnus-id-to-thread id)))
4761     (when (and thread
4762                (car thread))
4763       (mail-header-number (car thread)))))
4764
4765 (defun gnus-id-to-header (id)
4766   "Return the article headers of ID."
4767   (car (gnus-id-to-thread id)))
4768
4769 (defun gnus-article-displayed-root-p (article)
4770   "Say whether ARTICLE is a root(ish) article."
4771   (let ((level (gnus-summary-thread-level article))
4772         (refs (mail-header-references  (gnus-summary-article-header article)))
4773         particle)
4774     (cond
4775      ((null level) nil)
4776      ((zerop level) t)
4777      ((null refs) t)
4778      ((null (gnus-parent-id refs)) t)
4779      ((and (= 1 level)
4780            (null (setq particle (gnus-id-to-article
4781                                  (gnus-parent-id refs))))
4782            (null (gnus-summary-thread-level particle)))))))
4783
4784 (defun gnus-root-id (id)
4785   "Return the id of the root of the thread where ID appears."
4786   (let (last-id prev)
4787     (while (and id (setq prev (car (gnus-id-to-thread id))))
4788       (setq last-id id
4789             id (gnus-parent-id (mail-header-references prev))))
4790     last-id))
4791
4792 (defun gnus-articles-in-thread (thread)
4793   "Return the list of articles in THREAD."
4794   (cons (mail-header-number (car thread))
4795         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4796
4797 (defun gnus-remove-thread (id &optional dont-remove)
4798   "Remove the thread that has ID in it."
4799   (let (headers thread last-id)
4800     ;; First go up in this thread until we find the root.
4801     (setq last-id (gnus-root-id id)
4802           headers (message-flatten-list (gnus-id-to-thread last-id)))
4803     ;; We have now found the real root of this thread.  It might have
4804     ;; been gathered into some loose thread, so we have to search
4805     ;; through the threads to find the thread we wanted.
4806     (let ((threads gnus-newsgroup-threads)
4807           sub)
4808       (while threads
4809         (setq sub (car threads))
4810         (if (stringp (car sub))
4811             ;; This is a gathered thread, so we look at the roots
4812             ;; below it to find whether this article is in this
4813             ;; gathered root.
4814             (progn
4815               (setq sub (cdr sub))
4816               (while sub
4817                 (when (member (caar sub) headers)
4818                   (setq thread (car threads)
4819                         threads nil
4820                         sub nil))
4821                 (setq sub (cdr sub))))
4822           ;; It's an ordinary thread, so we check it.
4823           (when (eq (car sub) (car headers))
4824             (setq thread sub
4825                   threads nil)))
4826         (setq threads (cdr threads)))
4827       ;; If this article is in no thread, then it's a root.
4828       (if thread
4829           (unless dont-remove
4830             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4831         (setq thread (gnus-id-to-thread last-id)))
4832       (when thread
4833         (prog1
4834             thread                      ; We return this thread.
4835           (unless dont-remove
4836             (if (stringp (car thread))
4837                 (progn
4838                   ;; If we use dummy roots, then we have to remove the
4839                   ;; dummy root as well.
4840                   (when (eq gnus-summary-make-false-root 'dummy)
4841                     ;; We go to the dummy root by going to
4842                     ;; the first sub-"thread", and then one line up.
4843                     (gnus-summary-goto-article
4844                      (mail-header-number (caadr thread)))
4845                     (forward-line -1)
4846                     (gnus-delete-line)
4847                     (gnus-data-compute-positions))
4848                   (setq thread (cdr thread))
4849                   (while thread
4850                     (gnus-remove-thread-1 (car thread))
4851                     (setq thread (cdr thread))))
4852               (gnus-remove-thread-1 thread))))))))
4853
4854 (defun gnus-remove-thread-1 (thread)
4855   "Remove the thread THREAD recursively."
4856   (let ((number (mail-header-number (pop thread)))
4857         d)
4858     (setq thread (reverse thread))
4859     (while thread
4860       (gnus-remove-thread-1 (pop thread)))
4861     (when (setq d (gnus-data-find number))
4862       (goto-char (gnus-data-pos d))
4863       (gnus-summary-show-thread)
4864       (gnus-data-remove
4865        number
4866        (- (point-at-bol)
4867           (prog1
4868               (1+ (point-at-eol))
4869             (gnus-delete-line)))))))
4870
4871 (defun gnus-sort-threads-recursive (threads func)
4872   (sort (mapcar (lambda (thread)
4873                   (cons (car thread)
4874                         (and (cdr thread)
4875                              (gnus-sort-threads-recursive (cdr thread) func))))
4876                 threads) func))
4877
4878 (defun gnus-sort-threads-loop (threads func)
4879   (let* ((superthread (cons nil threads))
4880          (stack (list (cons superthread threads)))
4881          remaining-threads thread)
4882     (while stack
4883       (setq remaining-threads (cdr (car stack)))
4884       (if remaining-threads
4885           (progn (setq thread (car remaining-threads))
4886                  (setcdr (car stack) (cdr remaining-threads))
4887                  (if (cdr thread)
4888                      (push (cons thread (cdr thread)) stack)))
4889         (setq thread (caar stack))
4890         (setcdr thread (sort (cdr thread) func))
4891         (pop stack)))
4892     (cdr superthread)))
4893
4894 (defun gnus-sort-threads (threads)
4895   "Sort THREADS."
4896   (if (not gnus-thread-sort-functions)
4897       threads
4898     (gnus-message 8 "Sorting threads...")
4899     (prog1
4900         (condition-case nil
4901             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4902               (gnus-sort-threads-recursive
4903                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4904           ;; Even after binding max-lisp-eval-depth, the recursive
4905           ;; sorter might fail for very long threads.  In that case,
4906           ;; try using a (less well-tested) non-recursive sorter.
4907           (error (gnus-message 9 "Sorting threads with loop...")
4908                  (gnus-sort-threads-loop
4909                   threads (gnus-make-sort-function
4910                            gnus-thread-sort-functions))))
4911       (gnus-message 8 "Sorting threads...done"))))
4912
4913 (defun gnus-sort-articles (articles)
4914   "Sort ARTICLES."
4915   (when gnus-article-sort-functions
4916     (gnus-message 7 "Sorting articles...")
4917     (prog1
4918         (setq gnus-newsgroup-headers
4919               (sort articles (gnus-make-sort-function
4920                               gnus-article-sort-functions)))
4921       (gnus-message 7 "Sorting articles...done"))))
4922
4923 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4924 (defmacro gnus-thread-header (thread)
4925   "Return header of first article in THREAD.
4926 Note that THREAD must never, ever be anything else than a variable -
4927 using some other form will lead to serious barfage."
4928   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4929   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4930   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4931         (vector thread) 2))
4932
4933 (defsubst gnus-article-sort-by-number (h1 h2)
4934   "Sort articles by article number."
4935   (< (mail-header-number h1)
4936      (mail-header-number h2)))
4937
4938 (defun gnus-thread-sort-by-number (h1 h2)
4939   "Sort threads by root article number."
4940   (gnus-article-sort-by-number
4941    (gnus-thread-header h1) (gnus-thread-header h2)))
4942
4943 (defsubst gnus-article-sort-by-random (h1 h2)
4944   "Sort articles randomly."
4945   (zerop (random 2)))
4946
4947 (defun gnus-thread-sort-by-random (h1 h2)
4948   "Sort threads randomly."
4949   (gnus-article-sort-by-random
4950    (gnus-thread-header h1) (gnus-thread-header h2)))
4951
4952 (defsubst gnus-article-sort-by-lines (h1 h2)
4953   "Sort articles by article Lines header."
4954   (< (mail-header-lines h1)
4955      (mail-header-lines h2)))
4956
4957 (defun gnus-thread-sort-by-lines (h1 h2)
4958   "Sort threads by root article Lines header."
4959   (gnus-article-sort-by-lines
4960    (gnus-thread-header h1) (gnus-thread-header h2)))
4961
4962 (defsubst gnus-article-sort-by-chars (h1 h2)
4963   "Sort articles by octet length."
4964   (< (mail-header-chars h1)
4965      (mail-header-chars h2)))
4966
4967 (defun gnus-thread-sort-by-chars (h1 h2)
4968   "Sort threads by root article octet length."
4969   (gnus-article-sort-by-chars
4970    (gnus-thread-header h1) (gnus-thread-header h2)))
4971
4972 (defsubst gnus-article-sort-by-author (h1 h2)
4973   "Sort articles by root author."
4974   (gnus-string<
4975    (let ((extract (funcall
4976                    gnus-extract-address-components
4977                    (mail-header-from h1))))
4978      (or (car extract) (cadr extract) ""))
4979    (let ((extract (funcall
4980                    gnus-extract-address-components
4981                    (mail-header-from h2))))
4982      (or (car extract) (cadr extract) ""))))
4983
4984 (defun gnus-thread-sort-by-author (h1 h2)
4985   "Sort threads by root author."
4986   (gnus-article-sort-by-author
4987    (gnus-thread-header h1)  (gnus-thread-header h2)))
4988
4989 (defsubst gnus-article-sort-by-recipient (h1 h2)
4990   "Sort articles by recipient."
4991   (gnus-string<
4992    (let ((extract (funcall
4993                    gnus-extract-address-components
4994                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4995      (or (car extract) (cadr extract)))
4996    (let ((extract (funcall
4997                    gnus-extract-address-components
4998                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4999      (or (car extract) (cadr extract)))))
5000
5001 (defun gnus-thread-sort-by-recipient (h1 h2)
5002   "Sort threads by root recipient."
5003   (gnus-article-sort-by-recipient
5004    (gnus-thread-header h1) (gnus-thread-header h2)))
5005
5006 (defsubst gnus-article-sort-by-subject (h1 h2)
5007   "Sort articles by root subject."
5008   (gnus-string<
5009    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5010    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5011
5012 (defun gnus-thread-sort-by-subject (h1 h2)
5013   "Sort threads by root subject."
5014   (gnus-article-sort-by-subject
5015    (gnus-thread-header h1) (gnus-thread-header h2)))
5016
5017 (defsubst gnus-article-sort-by-date (h1 h2)
5018   "Sort articles by root article date."
5019   (time-less-p
5020    (gnus-date-get-time (mail-header-date h1))
5021    (gnus-date-get-time (mail-header-date h2))))
5022
5023 (defun gnus-thread-sort-by-date (h1 h2)
5024   "Sort threads by root article date."
5025   (gnus-article-sort-by-date
5026    (gnus-thread-header h1) (gnus-thread-header h2)))
5027
5028 (defsubst gnus-article-sort-by-score (h1 h2)
5029   "Sort articles by root article score.
5030 Unscored articles will be counted as having a score of zero."
5031   (> (or (cdr (assq (mail-header-number h1)
5032                     gnus-newsgroup-scored))
5033          gnus-summary-default-score 0)
5034      (or (cdr (assq (mail-header-number h2)
5035                     gnus-newsgroup-scored))
5036          gnus-summary-default-score 0)))
5037
5038 (defun gnus-thread-sort-by-score (h1 h2)
5039   "Sort threads by root article score."
5040   (gnus-article-sort-by-score
5041    (gnus-thread-header h1) (gnus-thread-header h2)))
5042
5043 (defun gnus-thread-sort-by-total-score (h1 h2)
5044   "Sort threads by the sum of all scores in the thread.
5045 Unscored articles will be counted as having a score of zero."
5046   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5047
5048 (defun gnus-thread-total-score (thread)
5049   ;; This function find the total score of THREAD.
5050   (cond
5051    ((null thread)
5052     0)
5053    ((consp thread)
5054     (if (stringp (car thread))
5055         (apply gnus-thread-score-function 0
5056                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5057       (gnus-thread-total-score-1 thread)))
5058    (t
5059     (gnus-thread-total-score-1 (list thread)))))
5060
5061 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5062   "Sort articles by number."
5063   (gnus-article-sort-by-number h1 h2))
5064
5065 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5066   "Sort threads such that the thread with the most recently arrived article comes first."
5067   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5068
5069 (defun gnus-thread-highest-number (thread)
5070   "Return the highest article number in THREAD."
5071   (apply 'max (mapcar (lambda (header)
5072                         (mail-header-number header))
5073                       (message-flatten-list thread))))
5074
5075 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5076   "Sort articles by number."
5077   (gnus-article-sort-by-date h1 h2))
5078
5079 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5080   "Sort threads such that the thread with the most recently dated article comes first."
5081   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5082
5083 ; Since this is called not only to sort the top-level threads, but
5084 ; also in recursive sorts to order the articles within a thread, each
5085 ; article will be processed many times.  Thus it speeds things up
5086 ; quite a bit to use gnus-date-get-time, which caches the time value.
5087 (defun gnus-thread-latest-date (thread)
5088   "Return the highest article date in THREAD."
5089   (apply 'max
5090          (mapcar (lambda (header) (gnus-float-time
5091                                    (gnus-date-get-time
5092                                     (mail-header-date header))))
5093                  (message-flatten-list thread))))
5094
5095 (defun gnus-thread-total-score-1 (root)
5096   ;; This function find the total score of the thread below ROOT.
5097   (setq root (car root))
5098   (apply gnus-thread-score-function
5099          (or (append
5100               (mapcar 'gnus-thread-total-score
5101                       (cdr (gnus-id-to-thread (mail-header-id root))))
5102               (when (> (mail-header-number root) 0)
5103                 (list (or (cdr (assq (mail-header-number root)
5104                                      gnus-newsgroup-scored))
5105                           gnus-summary-default-score 0))))
5106              (list gnus-summary-default-score)
5107              '(0))))
5108
5109 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5110 (defvar gnus-tmp-prev-subject nil)
5111 (defvar gnus-tmp-false-parent nil)
5112 (defvar gnus-tmp-root-expunged nil)
5113 (defvar gnus-tmp-dummy-line nil)
5114
5115 (defun gnus-extra-header (type &optional header)
5116   "Return the extra header of TYPE."
5117   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5118       ""))
5119
5120 (defvar gnus-tmp-thread-tree-header-string "")
5121
5122 (defcustom gnus-sum-thread-tree-root "> "
5123   "With %B spec, used for the root of a thread.
5124 If nil, use subject instead."
5125   :version "22.1"
5126   :type '(radio (const :format "%v  " nil) string)
5127   :group 'gnus-thread)
5128
5129 (defcustom gnus-sum-thread-tree-false-root "> "
5130   "With %B spec, used for a false root of a thread.
5131 If nil, use subject instead."
5132   :version "22.1"
5133   :type '(radio (const :format "%v  " nil) string)
5134   :group 'gnus-thread)
5135
5136 (defcustom gnus-sum-thread-tree-single-indent ""
5137   "With %B spec, used for a thread with just one message.
5138 If nil, use subject instead."
5139   :version "22.1"
5140   :type '(radio (const :format "%v  " nil) string)
5141   :group 'gnus-thread)
5142
5143 (defcustom gnus-sum-thread-tree-vertical "| "
5144   "With %B spec, used for drawing a vertical line."
5145   :version "22.1"
5146   :type 'string
5147   :group 'gnus-thread)
5148
5149 (defcustom gnus-sum-thread-tree-indent "  "
5150   "With %B spec, used for indenting."
5151   :version "22.1"
5152   :type 'string
5153   :group 'gnus-thread)
5154
5155 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5156   "With %B spec, used for a leaf with brothers."
5157   :version "22.1"
5158   :type 'string
5159   :group 'gnus-thread)
5160
5161 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5162   "With %B spec, used for a leaf without brothers."
5163   :version "22.1"
5164   :type 'string
5165   :group 'gnus-thread)
5166
5167 (defcustom gnus-summary-display-while-building nil
5168   "If non-nil, show and update the summary buffer as it's being built.
5169 If the value is t, update the buffer after every line is inserted.  If
5170 the value is an integer (N), update the display every N lines."
5171   :version "22.1"
5172   :group 'gnus-thread
5173   :type '(choice (const :tag "off" nil)
5174                  number
5175                  (const :tag "frequently" t)))
5176
5177 (defun gnus-summary-prepare-threads (threads)
5178   "Prepare summary buffer from THREADS and indentation LEVEL.
5179 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5180 or a straight list of headers."
5181   (gnus-message 7 "Generating summary...")
5182
5183   (setq gnus-newsgroup-threads threads)
5184   (beginning-of-line)
5185
5186   (let ((gnus-tmp-level 0)
5187         (default-score (or gnus-summary-default-score 0))
5188         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5189         (building-line-count gnus-summary-display-while-building)
5190         (building-count (integerp gnus-summary-display-while-building))
5191         thread number subject stack state gnus-tmp-gathered beg-match
5192         new-roots gnus-tmp-new-adopts thread-end simp-subject
5193         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5194         gnus-tmp-replied gnus-tmp-subject-or-nil
5195         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5196         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5197         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5198         tree-stack)
5199
5200     (setq gnus-tmp-prev-subject nil
5201           gnus-tmp-thread-tree-header-string "")
5202
5203     (if (vectorp (car threads))
5204         ;; If this is a straight (sic) list of headers, then a
5205         ;; threaded summary display isn't required, so we just create
5206         ;; an unthreaded one.
5207         (gnus-summary-prepare-unthreaded threads)
5208
5209       ;; Do the threaded display.
5210
5211       (if gnus-summary-display-while-building
5212           (switch-to-buffer (buffer-name)))
5213       (while (or threads stack gnus-tmp-new-adopts new-roots)
5214
5215         (if (and (= gnus-tmp-level 0)
5216                  (or (not stack)
5217                      (= (caar stack) 0))
5218                  (not gnus-tmp-false-parent)
5219                  (or gnus-tmp-new-adopts new-roots))
5220             (if gnus-tmp-new-adopts
5221                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5222                       thread (list (car gnus-tmp-new-adopts))
5223                       gnus-tmp-header (caar thread)
5224                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5225               (when new-roots
5226                 (setq thread (list (car new-roots))
5227                       gnus-tmp-header (caar thread)
5228                       new-roots (cdr new-roots))))
5229
5230           (if threads
5231               ;; If there are some threads, we do them before the
5232               ;; threads on the stack.
5233               (setq thread threads
5234                     gnus-tmp-header (caar thread))
5235             ;; There were no current threads, so we pop something off
5236             ;; the stack.
5237             (setq state (car stack)
5238                   gnus-tmp-level (car state)
5239                   tree-stack (cadr state)
5240                   thread (caddr state)
5241                   stack (cdr stack)
5242                   gnus-tmp-header (caar thread))))
5243
5244         (setq gnus-tmp-false-parent nil)
5245         (setq gnus-tmp-root-expunged nil)
5246         (setq thread-end nil)
5247
5248         (if (stringp gnus-tmp-header)
5249             ;; The header is a dummy root.
5250             (cond
5251              ((eq gnus-summary-make-false-root 'adopt)
5252               ;; We let the first article adopt the rest.
5253               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5254                                                (cddar thread)))
5255               (setq gnus-tmp-gathered
5256                     (nconc (mapcar
5257                             (lambda (h) (mail-header-number (car h)))
5258                             (cddar thread))
5259                            gnus-tmp-gathered))
5260               (setq thread (cons (list (caar thread)
5261                                        (cadar thread))
5262                                  (cdr thread)))
5263               (setq gnus-tmp-level -1
5264                     gnus-tmp-false-parent t))
5265              ((eq gnus-summary-make-false-root 'empty)
5266               ;; We print adopted articles with empty subject fields.
5267               (setq gnus-tmp-gathered
5268                     (nconc (mapcar
5269                             (lambda (h) (mail-header-number (car h)))
5270                             (cddar thread))
5271                            gnus-tmp-gathered))
5272               (setq gnus-tmp-level -1))
5273              ((eq gnus-summary-make-false-root 'dummy)
5274               ;; We remember that we probably want to output a dummy
5275               ;; root.
5276               (setq gnus-tmp-dummy-line gnus-tmp-header)
5277               (setq gnus-tmp-prev-subject gnus-tmp-header))
5278              (t
5279               ;; We do not make a root for the gathered
5280               ;; sub-threads at all.
5281               (setq gnus-tmp-level -1)))
5282
5283           (setq number (mail-header-number gnus-tmp-header)
5284                 subject (mail-header-subject gnus-tmp-header)
5285                 simp-subject (gnus-simplify-subject-fully subject))
5286
5287           (cond
5288            ;; If the thread has changed subject, we might want to make
5289            ;; this subthread into a root.
5290            ((and (null gnus-thread-ignore-subject)
5291                  (not (zerop gnus-tmp-level))
5292                  gnus-tmp-prev-subject
5293                  (not (string= gnus-tmp-prev-subject simp-subject)))
5294             (setq new-roots (nconc new-roots (list (car thread)))
5295                   thread-end t
5296                   gnus-tmp-header nil))
5297            ;; If the article lies outside the current limit,
5298            ;; then we do not display it.
5299            ((not (memq number gnus-newsgroup-limit))
5300             (setq gnus-tmp-gathered
5301                   (nconc (mapcar
5302                           (lambda (h) (mail-header-number (car h)))
5303                           (cdar thread))
5304                          gnus-tmp-gathered))
5305             (setq gnus-tmp-new-adopts (if (cdar thread)
5306                                           (append gnus-tmp-new-adopts
5307                                                   (cdar thread))
5308                                         gnus-tmp-new-adopts)
5309                   thread-end t
5310                   gnus-tmp-header nil)
5311             (when (zerop gnus-tmp-level)
5312               (setq gnus-tmp-root-expunged t)))
5313            ;; Perhaps this article is to be marked as read?
5314            ((and gnus-summary-mark-below
5315                  (< (or (cdr (assq number gnus-newsgroup-scored))
5316                         default-score)
5317                     gnus-summary-mark-below)
5318                  ;; Don't touch sparse articles.
5319                  (not (gnus-summary-article-sparse-p number))
5320                  (not (gnus-summary-article-ancient-p number)))
5321             (setq gnus-newsgroup-unreads
5322                   (delq number gnus-newsgroup-unreads))
5323             (if gnus-newsgroup-auto-expire
5324                 (setq gnus-newsgroup-expirable
5325                       (gnus-add-to-sorted-list
5326                        gnus-newsgroup-expirable number))
5327               (push (cons number gnus-low-score-mark)
5328                     gnus-newsgroup-reads))))
5329
5330           (when gnus-tmp-header
5331             ;; We may have an old dummy line to output before this
5332             ;; article.
5333             (when (and gnus-tmp-dummy-line
5334                        (gnus-subject-equal
5335                         gnus-tmp-dummy-line
5336                         (mail-header-subject gnus-tmp-header)))
5337               (gnus-summary-insert-dummy-line
5338                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5339               (setq gnus-tmp-dummy-line nil))
5340
5341             ;; Compute the mark.
5342             (setq gnus-tmp-unread (gnus-article-mark number))
5343
5344             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5345                                   gnus-tmp-header gnus-tmp-level)
5346                   gnus-newsgroup-data)
5347
5348             ;; Actually insert the line.
5349             (setq
5350              gnus-tmp-subject-or-nil
5351              (cond
5352               ((and gnus-thread-ignore-subject
5353                     gnus-tmp-prev-subject
5354                     (not (string= gnus-tmp-prev-subject simp-subject)))
5355                subject)
5356               ((zerop gnus-tmp-level)
5357                (if (and (eq gnus-summary-make-false-root 'empty)
5358                         (memq number gnus-tmp-gathered)
5359                         gnus-tmp-prev-subject
5360                         (string= gnus-tmp-prev-subject simp-subject))
5361                    gnus-summary-same-subject
5362                  subject))
5363               (t gnus-summary-same-subject)))
5364             (if (and (eq gnus-summary-make-false-root 'adopt)
5365                      (= gnus-tmp-level 1)
5366                      (memq number gnus-tmp-gathered))
5367                 (setq gnus-tmp-opening-bracket ?\<
5368                       gnus-tmp-closing-bracket ?\>)
5369               (setq gnus-tmp-opening-bracket ?\[
5370                     gnus-tmp-closing-bracket ?\]))
5371             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5372                 (gnus-make-thread-indent-array
5373                  (max (* 2 (length gnus-thread-indent-array))
5374                       gnus-tmp-level)))
5375             (setq
5376              gnus-tmp-indentation
5377              (aref gnus-thread-indent-array gnus-tmp-level)
5378              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5379              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5380                                 gnus-summary-default-score 0)
5381              gnus-tmp-score-char
5382              (if (or (null gnus-summary-default-score)
5383                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5384                          gnus-summary-zcore-fuzz))
5385                  ?                      ;Whitespace
5386                (if (< gnus-tmp-score gnus-summary-default-score)
5387                    gnus-score-below-mark gnus-score-over-mark))
5388              gnus-tmp-replied
5389              (cond ((memq number gnus-newsgroup-processable)
5390                     gnus-process-mark)
5391                    ((memq number gnus-newsgroup-cached)
5392                     gnus-cached-mark)
5393                    ((memq number gnus-newsgroup-replied)
5394                     gnus-replied-mark)
5395                    ((memq number gnus-newsgroup-forwarded)
5396                     gnus-forwarded-mark)
5397                    ((memq number gnus-newsgroup-saved)
5398                     gnus-saved-mark)
5399                    ((memq number gnus-newsgroup-unseen)
5400                     gnus-unseen-mark)
5401                    (t gnus-no-mark))
5402              gnus-tmp-downloaded
5403              (cond ((memq number gnus-newsgroup-undownloaded)
5404                     gnus-undownloaded-mark)
5405                    (gnus-newsgroup-agentized
5406                     gnus-downloaded-mark)
5407                    (t
5408                     gnus-no-mark))
5409              gnus-tmp-from (mail-header-from gnus-tmp-header)
5410              gnus-tmp-name
5411              (cond
5412               ((string-match "<[^>]+> *$" gnus-tmp-from)
5413                (setq beg-match (match-beginning 0))
5414                (or (and (string-match "^\".+\"" gnus-tmp-from)
5415                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5416                    (substring gnus-tmp-from 0 beg-match)))
5417               ((string-match "(.+)" gnus-tmp-from)
5418                (substring gnus-tmp-from
5419                           (1+ (match-beginning 0)) (1- (match-end 0))))
5420               (t gnus-tmp-from))
5421
5422              ;; Do the %B string
5423              gnus-tmp-thread-tree-header-string
5424              (cond
5425               ((not gnus-show-threads) "")
5426               ((zerop gnus-tmp-level)
5427                (cond ((cdar thread)
5428                       (or gnus-sum-thread-tree-root subject))
5429                      (gnus-tmp-new-adopts
5430                       (or gnus-sum-thread-tree-false-root subject))
5431                      (t
5432                       (or gnus-sum-thread-tree-single-indent subject))))
5433               (t
5434                (concat (apply 'concat
5435                               (mapcar (lambda (item)
5436                                         (if (= item 1)
5437                                             gnus-sum-thread-tree-vertical
5438                                           gnus-sum-thread-tree-indent))
5439                                       (cdr (reverse tree-stack))))
5440                        (if (nth 1 thread)
5441                            gnus-sum-thread-tree-leaf-with-other
5442                          gnus-sum-thread-tree-single-leaf)))))
5443             (when (string= gnus-tmp-name "")
5444               (setq gnus-tmp-name gnus-tmp-from))
5445             (unless (numberp gnus-tmp-lines)
5446               (setq gnus-tmp-lines -1))
5447             (if (= gnus-tmp-lines -1)
5448                 (setq gnus-tmp-lines "?")
5449               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5450             (gnus-put-text-property
5451              (point)
5452              (progn (eval gnus-summary-line-format-spec) (point))
5453              'gnus-number number)
5454             (when gnus-visual-p
5455               (forward-line -1)
5456               (gnus-summary-highlight-line)
5457               (when gnus-summary-update-hook
5458                 (gnus-run-hooks 'gnus-summary-update-hook))
5459               (forward-line 1))
5460
5461             (setq gnus-tmp-prev-subject simp-subject)))
5462
5463         (when (nth 1 thread)
5464           (push (list (max 0 gnus-tmp-level)
5465                       (copy-sequence tree-stack)
5466                       (nthcdr 1 thread))
5467                 stack))
5468         (push (if (nth 1 thread) 1 0) tree-stack)
5469         (incf gnus-tmp-level)
5470         (setq threads (if thread-end nil (cdar thread)))
5471         (if gnus-summary-display-while-building
5472             (if building-count
5473                 (progn
5474                   ;; use a set frequency
5475                   (setq building-line-count (1- building-line-count))
5476                   (when (= building-line-count 0)
5477                     (sit-for 0)
5478                     (setq building-line-count
5479                           gnus-summary-display-while-building)))
5480               ;; always
5481               (sit-for 0)))
5482         (unless threads
5483           (setq gnus-tmp-level 0)))))
5484   (gnus-message 7 "Generating summary...done"))
5485
5486 (defun gnus-summary-prepare-unthreaded (headers)
5487   "Generate an unthreaded summary buffer based on HEADERS."
5488   (let (header number mark)
5489
5490     (beginning-of-line)
5491
5492     (while headers
5493       ;; We may have to root out some bad articles...
5494       (when (memq (setq number (mail-header-number
5495                                 (setq header (pop headers))))
5496                   gnus-newsgroup-limit)
5497         ;; Mark article as read when it has a low score.
5498         (when (and gnus-summary-mark-below
5499                    (< (or (cdr (assq number gnus-newsgroup-scored))
5500                           gnus-summary-default-score 0)
5501                       gnus-summary-mark-below)
5502                    (not (gnus-summary-article-ancient-p number)))
5503           (setq gnus-newsgroup-unreads
5504                 (delq number gnus-newsgroup-unreads))
5505           (if gnus-newsgroup-auto-expire
5506               (push number gnus-newsgroup-expirable)
5507             (push (cons number gnus-low-score-mark)
5508                   gnus-newsgroup-reads)))
5509
5510         (setq mark (gnus-article-mark number))
5511         (push (gnus-data-make number mark (1+ (point)) header 0)
5512               gnus-newsgroup-data)
5513         (gnus-summary-insert-line
5514          header 0 number
5515          (memq number gnus-newsgroup-undownloaded)
5516          mark (memq number gnus-newsgroup-replied)
5517          (memq number gnus-newsgroup-expirable)
5518          (mail-header-subject header) nil
5519          (cdr (assq number gnus-newsgroup-scored))
5520          (memq number gnus-newsgroup-processable))))))
5521
5522 (defun gnus-group-get-list-identifiers (group)
5523   "Get list identifier regexp for GROUP."
5524   (or (gnus-parameter-list-identifier group)
5525       (if (consp gnus-list-identifiers)
5526           (mapconcat 'identity gnus-list-identifiers " *\\|")
5527         gnus-list-identifiers)))
5528
5529 (defun gnus-summary-remove-list-identifiers ()
5530   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5531   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5532         changed subject)
5533     (when regexp
5534       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5535       (dolist (header gnus-newsgroup-headers)
5536         (setq subject (mail-header-subject header)
5537               changed nil)
5538         (while (string-match regexp subject)
5539           (setq subject
5540                 (concat (substring subject 0 (match-beginning 1))
5541                         (substring subject (match-end 0)))
5542                 changed t))
5543         (when changed
5544           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5545             (setq subject
5546                   (concat (substring subject 0 (match-beginning 1))
5547                           (substring subject (match-end 1)))))
5548           (mail-header-set-subject header subject))))))
5549
5550 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5551   "Fetch headers of ARTICLES."
5552   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5553     (gnus-message 7 "Fetching headers for %s..." name)
5554     (prog1
5555         (if (eq 'nov
5556                 (setq gnus-headers-retrieved-by
5557                       (gnus-retrieve-headers
5558                        articles gnus-newsgroup-name
5559                        (or limit
5560                            ;; We might want to fetch old headers, but
5561                            ;; not if there is only 1 article.
5562                            (and (or (and
5563                                      (not (eq gnus-fetch-old-headers 'some))
5564                                      (not (numberp gnus-fetch-old-headers)))
5565                                     (> (length articles) 1))
5566                                 gnus-fetch-old-headers)))))
5567             (gnus-get-newsgroup-headers-xover
5568              articles force-new dependencies gnus-newsgroup-name t)
5569           (gnus-get-newsgroup-headers dependencies force-new))
5570       (gnus-message 7 "Fetching headers for %s...done" name))))
5571
5572 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5573   "Select newsgroup GROUP.
5574 If READ-ALL is non-nil, all articles in the group are selected.
5575 If SELECT-ARTICLES, only select those articles from GROUP."
5576   (let* ((entry (gnus-group-entry group))
5577          ;;!!! Dirty hack; should be removed.
5578          (gnus-summary-ignore-duplicates
5579           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5580               t
5581             gnus-summary-ignore-duplicates))
5582          (info (nth 2 entry))
5583          charset articles fetched-articles cached)
5584
5585     (unless (gnus-check-server
5586              (set (make-local-variable 'gnus-current-select-method)
5587                   (gnus-find-method-for-group group)))
5588       (error "Couldn't open server"))
5589     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5590
5591     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5592         (gnus-activate-group group)     ; Or we can activate it...
5593         (progn                          ; Or we bug out.
5594           (when (equal major-mode 'gnus-summary-mode)
5595             (gnus-kill-buffer (current-buffer)))
5596           (error
5597            "Couldn't activate group %s: %s"
5598            (mm-decode-coding-string group charset)
5599            (mm-decode-coding-string (gnus-status-message group) charset))))
5600
5601     (unless (gnus-request-group group t)
5602       (when (equal major-mode 'gnus-summary-mode)
5603         (gnus-kill-buffer (current-buffer)))
5604       (error "Couldn't request group %s: %s"
5605              (mm-decode-coding-string group charset)
5606              (mm-decode-coding-string (gnus-status-message group) charset)))
5607
5608     (when (and gnus-agent
5609                (gnus-active group))
5610       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5611
5612       (setq gnus-summary-use-undownloaded-faces
5613             (gnus-agent-find-parameter
5614              group
5615              'agent-enable-undownloaded-faces)))
5616
5617     (setq gnus-newsgroup-name group
5618           gnus-newsgroup-unselected nil
5619           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5620
5621     (let ((display (gnus-group-find-parameter group 'display)))
5622       (setq gnus-newsgroup-display
5623             (cond
5624              ((not (zerop (or (car-safe read-all) 0)))
5625               ;; The user entered the group with C-u SPC/RET, let's show
5626               ;; all articles.
5627               'gnus-not-ignore)
5628              ((eq display 'all)
5629               'gnus-not-ignore)
5630              ((arrayp display)
5631               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5632              ((numberp display)
5633               ;; The following is probably the "correct" solution, but
5634               ;; it makes Gnus fetch all headers and then limit the
5635               ;; articles (which is slow), so instead we hack the
5636               ;; select-articles parameter instead. -- Simon Josefsson
5637               ;; <jas@kth.se>
5638               ;;
5639               ;; (gnus-byte-compile
5640               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5641               ;;                         display)))))
5642               (setq select-articles
5643                     (gnus-uncompress-range
5644                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5645                              (if (> tmp 0)
5646                                  tmp
5647                                1))
5648                            (cdr (gnus-active group)))))
5649               nil)
5650              (t
5651               nil))))
5652
5653     (gnus-summary-setup-default-charset)
5654
5655     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5656     (when (gnus-virtual-group-p group)
5657       (setq cached gnus-newsgroup-cached))
5658
5659     (setq gnus-newsgroup-unreads
5660           (gnus-sorted-ndifference
5661            (gnus-sorted-ndifference gnus-newsgroup-unreads
5662                                     gnus-newsgroup-marked)
5663            gnus-newsgroup-dormant))
5664
5665     (setq gnus-newsgroup-processable nil)
5666
5667     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5668
5669     ;; Adjust and set lists of article marks.
5670     (when info
5671       (gnus-adjust-marked-articles info))
5672     (if (setq articles select-articles)
5673         (setq gnus-newsgroup-unselected
5674               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5675       (setq articles (gnus-articles-to-read group read-all)))
5676
5677     (cond
5678      ((null articles)
5679       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5680       'quit)
5681      ((eq articles 0) nil)
5682      (t
5683       ;; Init the dependencies hash table.
5684       (setq gnus-newsgroup-dependencies
5685             (gnus-make-hashtable (length articles)))
5686       (gnus-set-global-variables)
5687       ;; Retrieve the headers and read them in.
5688
5689       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5690
5691       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5692       (when cached
5693         (setq gnus-newsgroup-cached cached))
5694
5695       ;; Suppress duplicates?
5696       (when gnus-suppress-duplicates
5697         (gnus-dup-suppress-articles))
5698
5699       ;; Set the initial limit.
5700       (setq gnus-newsgroup-limit (copy-sequence articles))
5701       ;; Remove canceled articles from the list of unread articles.
5702       (setq fetched-articles
5703             (mapcar (lambda (headers) (mail-header-number headers))
5704                     gnus-newsgroup-headers))
5705       (setq gnus-newsgroup-articles fetched-articles)
5706       (setq gnus-newsgroup-unreads
5707             (gnus-sorted-nintersection
5708              gnus-newsgroup-unreads fetched-articles))
5709       (gnus-compute-unseen-list)
5710
5711       ;; Removed marked articles that do not exist.
5712       (gnus-update-missing-marks
5713        (gnus-sorted-difference articles fetched-articles))
5714       ;; We might want to build some more threads first.
5715       (when (and gnus-fetch-old-headers
5716                  (eq gnus-headers-retrieved-by 'nov))
5717         (if (eq gnus-fetch-old-headers 'invisible)
5718             (gnus-build-all-threads)
5719           (gnus-build-old-threads)))
5720       ;; Let the Gnus agent mark articles as read.
5721       (when gnus-agent
5722         (gnus-agent-get-undownloaded-list))
5723       ;; Remove list identifiers from subject
5724       (gnus-summary-remove-list-identifiers)
5725       ;; Check whether auto-expire is to be done in this group.
5726       (setq gnus-newsgroup-auto-expire
5727             (and (gnus-group-auto-expirable-p group)
5728                  (not (gnus-group-read-only-p group))))
5729       ;; Set up the article buffer now, if necessary.
5730       (unless (and gnus-single-article-buffer
5731                    (equal gnus-article-buffer "*Article*"))
5732         (gnus-article-setup-buffer))
5733       ;; First and last article in this newsgroup.
5734       (when gnus-newsgroup-headers
5735         (setq gnus-newsgroup-begin
5736               (mail-header-number (car gnus-newsgroup-headers))
5737               gnus-newsgroup-end
5738               (mail-header-number
5739                (gnus-last-element gnus-newsgroup-headers))))
5740       ;; GROUP is successfully selected.
5741       (or gnus-newsgroup-headers t)))))
5742
5743 (defun gnus-compute-unseen-list ()
5744   ;; The `seen' marks are treated specially.
5745   (if (not gnus-newsgroup-seen)
5746       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5747     (setq gnus-newsgroup-unseen
5748           (gnus-inverse-list-range-intersection
5749            gnus-newsgroup-articles gnus-newsgroup-seen))))
5750
5751 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5752
5753 (defun gnus-summary-display-make-predicate (display)
5754   (require 'gnus-agent)
5755   (when (= (length display) 1)
5756     (setq display (car display)))
5757   (unless gnus-summary-display-cache
5758     (dolist (elem (append '((unread . unread)
5759                             (read . read)
5760                             (unseen . unseen))
5761                           gnus-article-mark-lists))
5762       (push (cons (cdr elem)
5763                   (gnus-byte-compile    ;Why bother?
5764                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5765             gnus-summary-display-cache)))
5766   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5767         (gnus-category-predicate-cache gnus-summary-display-cache))
5768     (gnus-get-predicate display)))
5769
5770 ;; Uses the dynamically bound `gnus-number' variable.
5771 (defvar gnus-number)
5772 (defun gnus-article-marked-p (type &optional article)
5773   (let ((article (or article gnus-number)))
5774     (cond
5775      ((eq type 'tick)
5776       (memq article gnus-newsgroup-marked))
5777      ((eq type 'spam)
5778       (memq article gnus-newsgroup-spam-marked))
5779      ((eq type 'unsend)
5780       (memq article gnus-newsgroup-unsendable))
5781      ((eq type 'undownload)
5782       (memq article gnus-newsgroup-undownloaded))
5783      ((eq type 'download)
5784       (memq article gnus-newsgroup-downloadable))
5785      ((eq type 'unread)
5786       (memq article gnus-newsgroup-unreads))
5787      ((eq type 'read)
5788       (memq article gnus-newsgroup-reads))
5789      ((eq type 'dormant)
5790       (memq article gnus-newsgroup-dormant) )
5791      ((eq type 'expire)
5792       (memq article gnus-newsgroup-expirable))
5793      ((eq type 'reply)
5794       (memq article gnus-newsgroup-replied))
5795      ((eq type 'killed)
5796       (memq article gnus-newsgroup-killed))
5797      ((eq type 'bookmark)
5798       (assq article gnus-newsgroup-bookmarks))
5799      ((eq type 'score)
5800       (assq article gnus-newsgroup-scored))
5801      ((eq type 'save)
5802       (memq article gnus-newsgroup-saved))
5803      ((eq type 'cache)
5804       (memq article gnus-newsgroup-cached))
5805      ((eq type 'forward)
5806       (memq article gnus-newsgroup-forwarded))
5807      ((eq type 'seen)
5808       (not (memq article gnus-newsgroup-unseen)))
5809      (t t))))
5810
5811 (defun gnus-articles-to-read (group &optional read-all)
5812   "Find out what articles the user wants to read."
5813   (let* ((only-read-p t)
5814          (articles
5815           ;; Select all articles if `read-all' is non-nil, or if there
5816           ;; are no unread articles.
5817           (if (or read-all
5818                   (and (zerop (length gnus-newsgroup-marked))
5819                        (zerop (length gnus-newsgroup-unreads)))
5820                   ;; Fetch all if the predicate is non-nil.
5821                   gnus-newsgroup-display)
5822               ;; We want to select the headers for all the articles in
5823               ;; the group, so we select either all the active
5824               ;; articles in the group, or (if that's nil), the
5825               ;; articles in the cache.
5826               (or
5827                (if gnus-newsgroup-maximum-articles
5828                    (let ((active (gnus-active group)))
5829                      (gnus-uncompress-range
5830                       (cons (max (car active)
5831                                  (- (cdr active)
5832                                     gnus-newsgroup-maximum-articles
5833                                     -1))
5834                             (cdr active))))
5835                  (gnus-uncompress-range (gnus-active group)))
5836                (gnus-cache-articles-in-group group))
5837             ;; Select only the "normal" subset of articles.
5838             (setq only-read-p nil)
5839             (gnus-sorted-nunion
5840              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5841              gnus-newsgroup-unreads)))
5842          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5843          (scored (length scored-list))
5844          (number (length articles))
5845          (marked (+ (length gnus-newsgroup-marked)
5846                     (length gnus-newsgroup-dormant)))
5847          (select
5848           (cond
5849            ((numberp read-all)
5850             read-all)
5851            ((numberp gnus-newsgroup-display)
5852             gnus-newsgroup-display)
5853            (t
5854             (condition-case ()
5855                 (cond
5856                  ((and (or (<= scored marked) (= scored number))
5857                        (numberp gnus-large-newsgroup)
5858                        (> number gnus-large-newsgroup))
5859                   (let* ((cursor-in-echo-area nil)
5860                          (initial (gnus-parameter-large-newsgroup-initial
5861                                    gnus-newsgroup-name))
5862                          (default (if only-read-p
5863                                       (or initial gnus-large-newsgroup)
5864                                     number))
5865                          (input
5866                           (read-string
5867                            (if only-read-p
5868                                (format
5869                                 "How many articles from %s (available %d, default %d): "
5870                                 (gnus-group-decoded-name
5871                                  (gnus-group-real-name gnus-newsgroup-name))
5872                                 number default)
5873                              (format
5874                               "How many articles from %s (%d default): "
5875                               (gnus-group-decoded-name
5876                                (gnus-group-real-name gnus-newsgroup-name))
5877                               default))
5878                            nil
5879                            nil
5880                            (number-to-string default))))
5881                     (if (string-match "^[ \t]*$" input) number input)))
5882                  ((and (> scored marked) (< scored number)
5883                        (> (- scored number) 20))
5884                   (let ((input
5885                          (read-string
5886                           (format "%s %s (%d scored, %d total): "
5887                                   "How many articles from"
5888                                   (gnus-group-decoded-name
5889                                    (gnus-group-real-name gnus-newsgroup-name))
5890                                   scored number))))
5891                     (if (string-match "^[ \t]*$" input)
5892                         number input)))
5893                  (t number))
5894               (quit
5895                (message "Quit getting the articles to read")
5896                nil))))))
5897     (setq select (if (stringp select) (string-to-number select) select))
5898     (if (or (null select) (zerop select))
5899         select
5900       (if (and (not (zerop scored)) (<= (abs select) scored))
5901           (progn
5902             (setq articles (sort scored-list '<))
5903             (setq number (length articles)))
5904         (setq articles (copy-sequence articles)))
5905
5906       (when (< (abs select) number)
5907         (if (< select 0)
5908             ;; Select the N oldest articles.
5909             (setcdr (nthcdr (1- (abs select)) articles) nil)
5910           ;; Select the N most recent articles.
5911           (setq articles (nthcdr (- number select) articles))))
5912       (setq gnus-newsgroup-unselected
5913             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5914       (when gnus-alter-articles-to-read-function
5915         (setq articles
5916               (sort
5917                (funcall gnus-alter-articles-to-read-function
5918                         gnus-newsgroup-name articles)
5919                '<)))
5920       articles)))
5921
5922 (defun gnus-killed-articles (killed articles)
5923   (let (out)
5924     (while articles
5925       (when (inline (gnus-member-of-range (car articles) killed))
5926         (push (car articles) out))
5927       (setq articles (cdr articles)))
5928     out))
5929
5930 (defun gnus-uncompress-marks (marks)
5931   "Uncompress the mark ranges in MARKS."
5932   (let ((uncompressed '(score bookmark))
5933         out)
5934     (while marks
5935       (if (memq (caar marks) uncompressed)
5936           (push (car marks) out)
5937         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5938       (setq marks (cdr marks)))
5939     out))
5940
5941 (defun gnus-article-mark-to-type (mark)
5942   "Return the type of MARK."
5943   (or (cadr (assq mark gnus-article-special-mark-lists))
5944       'list))
5945
5946 (defun gnus-article-unpropagatable-p (mark)
5947   "Return whether MARK should be propagated to back end."
5948   (memq mark gnus-article-unpropagated-mark-lists))
5949
5950 (defun gnus-adjust-marked-articles (info)
5951   "Set all article lists and remove all marks that are no longer valid."
5952   (let* ((marked-lists (gnus-info-marks info))
5953          (active (gnus-active (gnus-info-group info)))
5954          (min (car active))
5955          (max (cdr active))
5956          (types gnus-article-mark-lists)
5957          marks var articles article mark mark-type
5958          bgn end)
5959     ;; Hack to avoid adjusting marks for imap.
5960     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5961               'nnimap)
5962       (setq min 1))
5963
5964     (dolist (marks marked-lists)
5965       (setq mark (car marks)
5966             mark-type (gnus-article-mark-to-type mark)
5967             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5968
5969       ;; We set the variable according to the type of the marks list,
5970       ;; and then adjust the marks to a subset of the active articles.
5971       (cond
5972        ;; Adjust "simple" lists - compressed yet unsorted
5973        ((eq mark-type 'list)
5974         ;; Simultaneously uncompress and clip to active range
5975         ;; See gnus-uncompress-range for a description of possible marks
5976         (let (l lh)
5977           (if (not (cadr marks))
5978               (set var nil)
5979             (setq articles (if (numberp (cddr marks))
5980                                (list (cdr marks))
5981                              (cdr marks))
5982                   lh (cons nil nil)
5983                   l lh)
5984
5985             (while (setq article (pop articles))
5986               (cond ((consp article)
5987                      (setq bgn (max (car article) min)
5988                            end (min (cdr article) max))
5989                      (while (<= bgn end)
5990                        (setq l (setcdr l (cons bgn nil))
5991                              bgn (1+ bgn))))
5992                     ((and (<= min article)
5993                           (>= max article))
5994                      (setq l (setcdr l (cons article nil))))))
5995             (set var (cdr lh)))))
5996        ;; Adjust assocs.
5997        ((eq mark-type 'tuple)
5998         (set var (setq articles (cdr marks)))
5999         (when (not (listp (cdr (symbol-value var))))
6000           (set var (list (symbol-value var))))
6001         (when (not (listp (cdr articles)))
6002           (setq articles (list articles)))
6003         (while articles
6004           (when (or (not (consp (setq article (pop articles))))
6005                     (< (car article) min)
6006                     (> (car article) max))
6007             (set var (delq article (symbol-value var))))))
6008        ;; Adjust ranges (sloppily).
6009        ((eq mark-type 'range)
6010         (cond
6011          ((eq mark 'seen)
6012           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6013           ;; It should be (seen (NUM1 . NUM2)).
6014           (when (numberp (cddr marks))
6015             (setcdr marks (list (cdr marks))))
6016           (setq articles (cdr marks))
6017           (while (and articles
6018                       (or (and (consp (car articles))
6019                                (> min (cdar articles)))
6020                           (and (numberp (car articles))
6021                                (> min (car articles)))))
6022             (pop articles))
6023           (set var articles))))))))
6024
6025 (defun gnus-update-missing-marks (missing)
6026   "Go through the list of MISSING articles and remove them from the mark lists."
6027   (when missing
6028     (let (var m)
6029       ;; Go through all types.
6030       (dolist (elem gnus-article-mark-lists)
6031         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6032           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6033           (when (symbol-value var)
6034             ;; This list has articles.  So we delete all missing
6035             ;; articles from it.
6036             (setq m missing)
6037             (while m
6038               (set var (delq (pop m) (symbol-value var))))))))))
6039
6040 (defun gnus-update-marks ()
6041   "Enter the various lists of marked articles into the newsgroup info list."
6042   (let ((types gnus-article-mark-lists)
6043         (info (gnus-get-info gnus-newsgroup-name))
6044         type list newmarked symbol delta-marks)
6045     (when info
6046       ;; Add all marks lists to the list of marks lists.
6047       (while (setq type (pop types))
6048         (setq list (symbol-value
6049                     (setq symbol
6050                           (intern (format "gnus-newsgroup-%s" (car type))))))
6051
6052         (when list
6053           ;; Get rid of the entries of the articles that have the
6054           ;; default score.
6055           (when (and (eq (cdr type) 'score)
6056                      gnus-save-score
6057                      list)
6058             (let* ((arts list)
6059                    (prev (cons nil list))
6060                    (all prev))
6061               (while arts
6062                 (if (or (not (consp (car arts)))
6063                         (= (cdar arts) gnus-summary-default-score))
6064                     (setcdr prev (cdr arts))
6065                   (setq prev arts))
6066                 (setq arts (cdr arts)))
6067               (setq list (cdr all)))))
6068
6069         (when (eq (cdr type) 'seen)
6070           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6071
6072         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6073           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6074
6075         (when (and (gnus-check-backend-function
6076                     'request-set-mark gnus-newsgroup-name)
6077                    (not (gnus-article-unpropagatable-p (cdr type))))
6078           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6079                  ;; Don't do anything about marks for articles we
6080                  ;; didn't actually get any headers for.
6081                  (del
6082                   (gnus-list-range-intersection
6083                    gnus-newsgroup-articles
6084                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6085                  (add
6086                   (gnus-list-range-intersection
6087                    gnus-newsgroup-articles
6088                    (gnus-remove-from-range
6089                     (gnus-copy-sequence list) old))))
6090             (when add
6091               (push (list add 'add (list (cdr type))) delta-marks))
6092             (when del
6093               ;; Don't delete marks from outside the active range.
6094               ;; This shouldn't happen, but is a sanity check.
6095               (setq del (gnus-sorted-range-intersection
6096                          (gnus-active gnus-newsgroup-name) del))
6097               (push (list del 'del (list (cdr type))) delta-marks))))
6098
6099         (when list
6100           (push (cons (cdr type) list) newmarked)))
6101
6102       (when delta-marks
6103         (unless (gnus-check-group gnus-newsgroup-name)
6104           (error "Can't open server for %s" gnus-newsgroup-name))
6105         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6106
6107       ;; Enter these new marks into the info of the group.
6108       (if (nthcdr 3 info)
6109           (setcar (nthcdr 3 info) newmarked)
6110         ;; Add the marks lists to the end of the info.
6111         (when newmarked
6112           (setcdr (nthcdr 2 info) (list newmarked))))
6113
6114       ;; Cut off the end of the info if there's nothing else there.
6115       (let ((i 5))
6116         (while (and (> i 2)
6117                     (not (nth i info)))
6118           (when (nthcdr (decf i) info)
6119             (setcdr (nthcdr i info) nil)))))))
6120
6121 (defun gnus-set-mode-line (where)
6122   "Set the mode line of the article or summary buffers.
6123 If WHERE is `summary', the summary mode line format will be used."
6124   ;; Is this mode line one we keep updated?
6125   (when (and (memq where gnus-updated-mode-lines)
6126              (symbol-value
6127               (intern (format "gnus-%s-mode-line-format-spec" where))))
6128     (let (mode-string)
6129       ;; We evaluate this in the summary buffer since these
6130       ;; variables are buffer-local to that buffer.
6131       (with-current-buffer gnus-summary-buffer
6132         ;; We bind all these variables that are used in the `eval' form
6133         ;; below.
6134         (let* ((mformat (symbol-value
6135                          (intern
6136                           (format "gnus-%s-mode-line-format-spec" where))))
6137                (gnus-tmp-group-name (gnus-mode-string-quote
6138                                      (gnus-group-decoded-name
6139                                       gnus-newsgroup-name)))
6140                (gnus-tmp-article-number (or gnus-current-article 0))
6141                (gnus-tmp-unread gnus-newsgroup-unreads)
6142                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6143                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6144                (gnus-tmp-unread-and-unselected
6145                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6146                             (zerop gnus-tmp-unselected))
6147                        "")
6148                       ((zerop gnus-tmp-unselected)
6149                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6150                       (t (format "{%d(+%d) more}"
6151                                  gnus-tmp-unread-and-unticked
6152                                  gnus-tmp-unselected))))
6153                (gnus-tmp-subject
6154                 (if (and gnus-current-headers
6155                          (vectorp gnus-current-headers))
6156                     (gnus-mode-string-quote
6157                      (mail-header-subject gnus-current-headers))
6158                   ""))
6159                bufname-length max-len
6160                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6161           (setq mode-string (eval mformat))
6162           (setq bufname-length (if (string-match "%b" mode-string)
6163                                    (- (length
6164                                        (buffer-name
6165                                         (if (eq where 'summary)
6166                                             nil
6167                                           (get-buffer gnus-article-buffer))))
6168                                       2)
6169                                  0))
6170           (setq max-len (max 4 (if gnus-mode-non-string-length
6171                                    (- (window-width)
6172                                       gnus-mode-non-string-length
6173                                       bufname-length)
6174                                  (length mode-string))))
6175           ;; We might have to chop a bit of the string off...
6176           (when (> (length mode-string) max-len)
6177             (setq mode-string
6178                   (concat (truncate-string-to-width mode-string (- max-len 3))
6179                           "...")))))
6180       ;; Update the mode line.
6181       (setq mode-line-buffer-identification
6182             (gnus-mode-line-buffer-identification (list mode-string)))
6183       (set-buffer-modified-p t))))
6184
6185 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6186   "Go through the HEADERS list and add all Xrefs to a hash table.
6187 The resulting hash table is returned, or nil if no Xrefs were found."
6188   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6189          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6190          (xref-hashtb (gnus-make-hashtable))
6191          start group entry number xrefs header)
6192     (while headers
6193       (setq header (pop headers))
6194       (when (and (setq xrefs (mail-header-xref header))
6195                  (not (memq (setq number (mail-header-number header))
6196                             unreads)))
6197         (setq start 0)
6198         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6199           (setq start (match-end 0))
6200           (setq group (if prefix
6201                           (concat prefix (substring xrefs (match-beginning 1)
6202                                                     (match-end 1)))
6203                         (substring xrefs (match-beginning 1) (match-end 1))))
6204           (setq number
6205                 (string-to-number (substring xrefs (match-beginning 2)
6206                                           (match-end 2))))
6207           (if (setq entry (gnus-gethash group xref-hashtb))
6208               (setcdr entry (cons number (cdr entry)))
6209             (gnus-sethash group (cons number nil) xref-hashtb)))))
6210     (and start xref-hashtb)))
6211
6212 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6213   "Look through all the headers and mark the Xrefs as read."
6214   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6215         name info xref-hashtb idlist method nth4)
6216     (with-current-buffer gnus-group-buffer
6217       (when (setq xref-hashtb
6218                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6219         (mapatoms
6220          (lambda (group)
6221            (unless (string= from-newsgroup (setq name (symbol-name group)))
6222              (setq idlist (symbol-value group))
6223              ;; Dead groups are not updated.
6224              (and (prog1
6225                       (setq info (gnus-get-info name))
6226                     (when (stringp (setq nth4 (gnus-info-method info)))
6227                       (setq nth4 (gnus-server-to-method nth4))))
6228                   ;; Only do the xrefs if the group has the same
6229                   ;; select method as the group we have just read.
6230                   (or (gnus-methods-equal-p
6231                        nth4 (gnus-find-method-for-group from-newsgroup))
6232                       virtual
6233                       (equal nth4 (setq method (gnus-find-method-for-group
6234                                                 from-newsgroup)))
6235                       (and (equal (car nth4) (car method))
6236                            (equal (nth 1 nth4) (nth 1 method))))
6237                   gnus-use-cross-reference
6238                   (or (not (eq gnus-use-cross-reference t))
6239                       virtual
6240                       ;; Only do cross-references on subscribed
6241                       ;; groups, if that is what is wanted.
6242                       (<= (gnus-info-level info) gnus-level-subscribed))
6243                   (gnus-group-make-articles-read name idlist))))
6244          xref-hashtb)))))
6245
6246 (defun gnus-compute-read-articles (group articles)
6247   (let* ((entry (gnus-group-entry group))
6248          (info (nth 2 entry))
6249          (active (gnus-active group))
6250          ninfo)
6251     (when entry
6252       ;; First peel off all invalid article numbers.
6253       (when active
6254         (let ((ids articles)
6255               id first)
6256           (while (setq id (pop ids))
6257             (when (and first (> id (cdr active)))
6258               ;; We'll end up in this situation in one particular
6259               ;; obscure situation.  If you re-scan a group and get
6260               ;; a new article that is cross-posted to a different
6261               ;; group that has not been re-scanned, you might get
6262               ;; crossposted article that has a higher number than
6263               ;; Gnus believes possible.  So we re-activate this
6264               ;; group as well.  This might mean doing the
6265               ;; crossposting thingy will *increase* the number
6266               ;; of articles in some groups.  Tsk, tsk.
6267               (setq active (or (gnus-activate-group group) active)))
6268             (when (or (> id (cdr active))
6269                       (< id (car active)))
6270               (setq articles (delq id articles))))))
6271       ;; If the read list is nil, we init it.
6272       (if (and active
6273                (null (gnus-info-read info))
6274                (> (car active) 1))
6275           (setq ninfo (cons 1 (1- (car active))))
6276         (setq ninfo (gnus-info-read info)))
6277       ;; Then we add the read articles to the range.
6278       (gnus-add-to-range
6279        ninfo (setq articles (sort articles '<))))))
6280
6281 (defun gnus-group-make-articles-read (group articles)
6282   "Update the info of GROUP to say that ARTICLES are read."
6283   (let* ((num 0)
6284          (entry (gnus-group-entry group))
6285          (info (nth 2 entry))
6286          (active (gnus-active group))
6287          (set-marks
6288           (or gnus-propagate-marks
6289               (gnus-method-option-p
6290                (gnus-find-method-for-group group)
6291                'server-marks)))
6292          range)
6293     (if (not entry)
6294         ;; Group that Gnus doesn't know exists, but still allow the
6295         ;; backend to set marks.
6296         (when set-marks
6297           (gnus-request-set-mark
6298            group (list (list (gnus-compress-sequence (sort articles #'<))
6299                              'add '(read)))))
6300       ;; Normal, subscribed groups.
6301       (setq range (gnus-compute-read-articles group articles))
6302       (with-current-buffer gnus-group-buffer
6303         (gnus-undo-register
6304           `(progn
6305              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6306              (gnus-info-set-read ',info ',(gnus-info-read info))
6307              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6308              (when ,set-marks
6309                (gnus-request-set-mark
6310                 ,group (list (list ',range 'del '(read)))))
6311              (gnus-group-update-group ,group t))))
6312       ;; Add the read articles to the range.
6313       (gnus-info-set-read info range)
6314       (when set-marks
6315         (gnus-request-set-mark group (list (list range 'add '(read)))))
6316       ;; Then we have to re-compute how many unread
6317       ;; articles there are in this group.
6318       (when active
6319         (cond
6320          ((not range)
6321           (setq num (- (1+ (cdr active)) (car active))))
6322          ((not (listp (cdr range)))
6323           (setq num (- (cdr active) (- (1+ (cdr range))
6324                                        (car range)))))
6325          (t
6326           (while range
6327             (if (numberp (car range))
6328                 (setq num (1+ num))
6329               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6330             (setq range (cdr range)))
6331           (setq num (- (cdr active) num))))
6332         ;; Update the number of unread articles.
6333         (setcar entry num)
6334         ;; Update the group buffer.
6335         (unless (gnus-ephemeral-group-p group)
6336           (gnus-group-update-group group t))))))
6337
6338 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6339   (let ((cur nntp-server-buffer)
6340         (dependencies
6341          (or dependencies
6342              (with-current-buffer gnus-summary-buffer
6343                gnus-newsgroup-dependencies)))
6344         headers id end ref number
6345         (mail-parse-charset gnus-newsgroup-charset)
6346         (mail-parse-ignored-charsets
6347          (save-current-buffer (condition-case nil
6348                                   (set-buffer gnus-summary-buffer)
6349                                 (error))
6350                               gnus-newsgroup-ignored-charsets)))
6351     (with-current-buffer nntp-server-buffer
6352       ;; Translate all TAB characters into SPACE characters.
6353       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6354       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6355       (ietf-drums-unfold-fws)
6356       (gnus-run-hooks 'gnus-parse-headers-hook)
6357       (let ((case-fold-search t)
6358             in-reply-to header p lines chars)
6359         (goto-char (point-min))
6360         ;; Search to the beginning of the next header.  Error messages
6361         ;; do not begin with 2 or 3.
6362         (while (re-search-forward "^[23][0-9]+ " nil t)
6363           (setq id nil
6364                 ref nil)
6365           ;; This implementation of this function, with nine
6366           ;; search-forwards instead of the one re-search-forward and
6367           ;; a case (which basically was the old function) is actually
6368           ;; about twice as fast, even though it looks messier.  You
6369           ;; can't have everything, I guess.  Speed and elegance
6370           ;; doesn't always go hand in hand.
6371           (setq
6372            header
6373            (vector
6374             ;; Number.
6375             (prog1
6376                 (setq number (read cur))
6377               (end-of-line)
6378               (setq p (point))
6379               (narrow-to-region (point)
6380                                 (or (and (search-forward "\n.\n" nil t)
6381                                          (- (point) 2))
6382                                     (point))))
6383             ;; Subject.
6384             (progn
6385               (goto-char p)
6386               (if (search-forward "\nsubject:" nil t)
6387                   (funcall gnus-decode-encoded-word-function
6388                            (nnheader-header-value))
6389                 "(none)"))
6390             ;; From.
6391             (progn
6392               (goto-char p)
6393               (if (search-forward "\nfrom:" nil t)
6394                   (funcall gnus-decode-encoded-address-function
6395                            (nnheader-header-value))
6396                 "(nobody)"))
6397             ;; Date.
6398             (progn
6399               (goto-char p)
6400               (if (search-forward "\ndate:" nil t)
6401                   (nnheader-header-value) ""))
6402             ;; Message-ID.
6403             (progn
6404               (goto-char p)
6405               (setq id (if (re-search-forward
6406                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6407                            ;; We do it this way to make sure the Message-ID
6408                            ;; is (somewhat) syntactically valid.
6409                            (buffer-substring (match-beginning 1)
6410                                              (match-end 1))
6411                          ;; If there was no message-id, we just fake one
6412                          ;; to make subsequent routines simpler.
6413                          (nnheader-generate-fake-message-id number))))
6414             ;; References.
6415             (progn
6416               (goto-char p)
6417               (if (search-forward "\nreferences:" nil t)
6418                   (progn
6419                     (setq end (point))
6420                     (prog1
6421                         (nnheader-header-value)
6422                       (setq ref
6423                             (buffer-substring
6424                              (progn
6425                                (end-of-line)
6426                                (search-backward ">" end t)
6427                                (1+ (point)))
6428                              (progn
6429                                (search-backward "<" end t)
6430                                (point))))))
6431                 ;; Get the references from the in-reply-to header if there
6432                 ;; were no references and the in-reply-to header looks
6433                 ;; promising.
6434                 (if (and (search-forward "\nin-reply-to:" nil t)
6435                          (setq in-reply-to (nnheader-header-value))
6436                          (string-match "<[^>]+>" in-reply-to))
6437                     (let (ref2)
6438                       (setq ref (substring in-reply-to (match-beginning 0)
6439                                            (match-end 0)))
6440                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6441                         (setq ref2 (substring in-reply-to (match-beginning 0)
6442                                               (match-end 0)))
6443                         (when (> (length ref2) (length ref))
6444                           (setq ref ref2)))
6445                       ref)
6446                   (setq ref nil))))
6447             ;; Chars.
6448             (progn
6449               (goto-char p)
6450               (if (search-forward "\nchars: " nil t)
6451                   (if (numberp (setq chars (ignore-errors (read cur))))
6452                       chars -1)
6453                 -1))
6454             ;; Lines.
6455             (progn
6456               (goto-char p)
6457               (if (search-forward "\nlines: " nil t)
6458                   (if (numberp (setq lines (ignore-errors (read cur))))
6459                       lines -1)
6460                 -1))
6461             ;; Xref.
6462             (progn
6463               (goto-char p)
6464               (and (search-forward "\nxref:" nil t)
6465                    (nnheader-header-value)))
6466             ;; Extra.
6467             (when gnus-extra-headers
6468               (let ((extra gnus-extra-headers)
6469                     out)
6470                 (while extra
6471                   (goto-char p)
6472                   (when (search-forward
6473                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6474                     (push (cons (car extra) (nnheader-header-value))
6475                           out))
6476                   (pop extra))
6477                 out))))
6478           (when (equal id ref)
6479             (setq ref nil))
6480
6481           (when gnus-alter-header-function
6482             (funcall gnus-alter-header-function header)
6483             (setq id (mail-header-id header)
6484                   ref (gnus-parent-id (mail-header-references header))))
6485
6486           (when (setq header
6487                       (gnus-dependencies-add-header
6488                        header dependencies force-new))
6489             (push header headers))
6490           (goto-char (point-max))
6491           (widen))
6492         (nreverse headers)))))
6493
6494 ;; Goes through the xover lines and returns a list of vectors
6495 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6496                                                   force-new dependencies
6497                                                   group also-fetch-heads)
6498   "Parse the news overview data in the server buffer.
6499 Return a list of headers that match SEQUENCE (see
6500 `nntp-retrieve-headers')."
6501   ;; Get the Xref when the users reads the articles since most/some
6502   ;; NNTP servers do not include Xrefs when using XOVER.
6503   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6504   (let ((mail-parse-charset gnus-newsgroup-charset)
6505         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6506         (cur nntp-server-buffer)
6507         (dependencies (or dependencies gnus-newsgroup-dependencies))
6508         (allp (cond
6509                ((eq gnus-read-all-available-headers t)
6510                 t)
6511                ((and (stringp gnus-read-all-available-headers)
6512                      group)
6513                 (string-match gnus-read-all-available-headers group))
6514                (t
6515                 nil)))
6516         number headers header)
6517     (with-current-buffer nntp-server-buffer
6518       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6519       ;; Allow the user to mangle the headers before parsing them.
6520       (gnus-run-hooks 'gnus-parse-headers-hook)
6521       (goto-char (point-min))
6522       (gnus-parse-without-error
6523         (while (and (or sequence allp)
6524                     (not (eobp)))
6525           (setq number (read cur))
6526           (when (not allp)
6527             (while (and sequence
6528                         (< (car sequence) number))
6529               (setq sequence (cdr sequence))))
6530           (when (and (or allp
6531                          (and sequence
6532                               (eq number (car sequence))))
6533                      (progn
6534                        (setq sequence (cdr sequence))
6535                        (setq header (inline
6536                                       (gnus-nov-parse-line
6537                                        number dependencies force-new)))))
6538             (push header headers))
6539           (forward-line 1)))
6540       ;; A common bug in inn is that if you have posted an article and
6541       ;; then retrieves the active file, it will answer correctly --
6542       ;; the new article is included.  However, a NOV entry for the
6543       ;; article may not have been generated yet, so this may fail.
6544       ;; We work around this problem by retrieving the last few
6545       ;; headers using HEAD.
6546       (if (or (not also-fetch-heads)
6547               (not sequence))
6548           ;; We (probably) got all the headers.
6549           (nreverse headers)
6550         (let ((gnus-nov-is-evil t))
6551           (nconc
6552            (nreverse headers)
6553            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6554              (gnus-get-newsgroup-headers))))))))
6555
6556 (defun gnus-article-get-xrefs ()
6557   "Fill in the Xref value in `gnus-current-headers', if necessary.
6558 This is meant to be called in `gnus-article-internal-prepare-hook'."
6559   (let ((headers (with-current-buffer gnus-summary-buffer
6560                    gnus-current-headers)))
6561     (or (not gnus-use-cross-reference)
6562         (not headers)
6563         (and (mail-header-xref headers)
6564              (not (string= (mail-header-xref headers) "")))
6565         (let ((case-fold-search t)
6566               xref)
6567           (save-restriction
6568             (nnheader-narrow-to-headers)
6569             (goto-char (point-min))
6570             (when (or (and (not (eobp))
6571                            (eq (downcase (char-after)) ?x)
6572                            (looking-at "Xref:"))
6573                       (search-forward "\nXref:" nil t))
6574               (goto-char (1+ (match-end 0)))
6575               (setq xref (buffer-substring (point) (point-at-eol)))
6576               (mail-header-set-xref headers xref)))))))
6577
6578 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6579   "Find article ID and insert the summary line for that article.
6580 OLD-HEADER can either be a header or a line number to insert
6581 the subject line on.
6582 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6583 and OLD-HEADER will be used when the summary line is inserted,
6584 too, instead of trying to fetch new headers."
6585   (let* ((line (and (numberp old-header) old-header))
6586          (old-header (and (vectorp old-header) old-header))
6587          (header (cond ((and old-header use-old-header)
6588                         old-header)
6589                        ((and (numberp id)
6590                              (gnus-number-to-header id))
6591                         (gnus-number-to-header id))
6592                        (t
6593                         (gnus-read-header id))))
6594          (number (and (numberp id) id))
6595          d)
6596     (when header
6597       ;; Rebuild the thread that this article is part of and go to the
6598       ;; article we have fetched.
6599       (when (and (not gnus-show-threads)
6600                  old-header)
6601         (when (and number
6602                    (setq d (gnus-data-find (mail-header-number old-header))))
6603           (goto-char (gnus-data-pos d))
6604           (gnus-data-remove
6605            number
6606            (- (point-at-bol)
6607               (prog1
6608                   (1+ (point-at-eol))
6609                 (gnus-delete-line))))))
6610       ;; Remove list identifiers from subject.
6611       (let ((gnus-newsgroup-headers (list header)))
6612         (gnus-summary-remove-list-identifiers))
6613       (when old-header
6614         (mail-header-set-number header (mail-header-number old-header)))
6615       (setq gnus-newsgroup-sparse
6616             (delq (setq number (mail-header-number header))
6617                   gnus-newsgroup-sparse))
6618       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6619       (push number gnus-newsgroup-limit)
6620       (gnus-rebuild-thread (mail-header-id header) line)
6621       (gnus-summary-goto-subject number nil t))
6622     (when (and (numberp number)
6623                (> number 0))
6624       ;; We have to update the boundaries even if we can't fetch the
6625       ;; article if ID is a number -- so that the next `P' or `N'
6626       ;; command will fetch the previous (or next) article even
6627       ;; if the one we tried to fetch this time has been canceled.
6628       (when (> number gnus-newsgroup-end)
6629         (setq gnus-newsgroup-end number))
6630       (when (< number gnus-newsgroup-begin)
6631         (setq gnus-newsgroup-begin number))
6632       (setq gnus-newsgroup-unselected
6633             (delq number gnus-newsgroup-unselected)))
6634     ;; Report back a success?
6635     (and header (mail-header-number header))))
6636
6637 ;;; Process/prefix in the summary buffer
6638
6639 (defun gnus-summary-work-articles (n)
6640   "Return a list of articles to be worked upon.
6641 The prefix argument, the list of process marked articles, and the
6642 current article will be taken into consideration."
6643   (with-current-buffer gnus-summary-buffer
6644     (cond
6645      (n
6646       ;; A numerical prefix has been given.
6647       (setq n (prefix-numeric-value n))
6648       (let ((backward (< n 0))
6649             (n (abs (prefix-numeric-value n)))
6650             articles article)
6651         (save-excursion
6652           (while
6653               (and (> n 0)
6654                    (push (setq article (gnus-summary-article-number))
6655                          articles)
6656                    (if backward
6657                        (gnus-summary-find-prev nil article)
6658                      (gnus-summary-find-next nil article)))
6659             (decf n)))
6660         (nreverse articles)))
6661      ((and (gnus-region-active-p) (mark))
6662       (message "region active")
6663       ;; Work on the region between point and mark.
6664       (let ((max (max (point) (mark)))
6665             articles article)
6666         (save-excursion
6667           (goto-char (min (point) (mark)))
6668           (while
6669               (and
6670                (push (setq article (gnus-summary-article-number)) articles)
6671                (gnus-summary-find-next nil article)
6672                (< (point) max)))
6673           (nreverse articles))))
6674      (gnus-newsgroup-processable
6675       ;; There are process-marked articles present.
6676       ;; Save current state.
6677       (gnus-summary-save-process-mark)
6678       ;; Return the list.
6679       (reverse gnus-newsgroup-processable))
6680      (t
6681       ;; Just return the current article.
6682       (list (gnus-summary-article-number))))))
6683
6684 (defmacro gnus-summary-iterate (arg &rest forms)
6685   "Iterate over the process/prefixed articles and do FORMS.
6686 ARG is the interactive prefix given to the command.  FORMS will be
6687 executed with point over the summary line of the articles."
6688   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6689     `(let ((,articles (gnus-summary-work-articles ,arg)))
6690        (while ,articles
6691          (gnus-summary-goto-subject (car ,articles))
6692          ,@forms
6693          (pop ,articles)))))
6694
6695 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6696 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6697
6698 (defun gnus-summary-save-process-mark ()
6699   "Push the current set of process marked articles on the stack."
6700   (interactive)
6701   (push (copy-sequence gnus-newsgroup-processable)
6702         gnus-newsgroup-process-stack))
6703
6704 (defun gnus-summary-kill-process-mark ()
6705   "Push the current set of process marked articles on the stack and unmark."
6706   (interactive)
6707   (gnus-summary-save-process-mark)
6708   (gnus-summary-unmark-all-processable))
6709
6710 (defun gnus-summary-yank-process-mark ()
6711   "Pop the last process mark state off the stack and restore it."
6712   (interactive)
6713   (unless gnus-newsgroup-process-stack
6714     (error "Empty mark stack"))
6715   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6716
6717 (defun gnus-summary-process-mark-set (set)
6718   "Make SET into the current process marked articles."
6719   (gnus-summary-unmark-all-processable)
6720   (mapc 'gnus-summary-set-process-mark set))
6721
6722 ;;; Searching and stuff
6723
6724 (defun gnus-summary-search-group (&optional backward use-level)
6725   "Search for next unread newsgroup.
6726 If optional argument BACKWARD is non-nil, search backward instead."
6727   (with-current-buffer gnus-group-buffer
6728     (when (gnus-group-search-forward
6729            backward nil (if use-level (gnus-group-group-level) nil))
6730       (gnus-group-group-name))))
6731
6732 (defun gnus-summary-best-group (&optional exclude-group)
6733   "Find the name of the best unread group.
6734 If EXCLUDE-GROUP, do not go to this group."
6735   (with-current-buffer gnus-group-buffer
6736     (save-excursion
6737       (gnus-group-best-unread-group exclude-group))))
6738
6739 (defun gnus-summary-find-next (&optional unread article backward)
6740   (if backward
6741       (gnus-summary-find-prev unread article)
6742     (let* ((dummy (gnus-summary-article-intangible-p))
6743            (article (or article (gnus-summary-article-number)))
6744            (data (gnus-data-find-list article))
6745            result)
6746       (when (and (not dummy)
6747                  (or (not gnus-summary-check-current)
6748                      (not unread)
6749                      (not (gnus-data-unread-p (car data)))))
6750         (setq data (cdr data)))
6751       (when (setq result
6752                   (if unread
6753                       (progn
6754                         (while data
6755                           (unless (memq (gnus-data-number (car data))
6756                                         (cond
6757                                          ((eq gnus-auto-goto-ignores
6758                                               'always-undownloaded)
6759                                           gnus-newsgroup-undownloaded)
6760                                          (gnus-plugged
6761                                           nil)
6762                                          ((eq gnus-auto-goto-ignores
6763                                               'unfetched)
6764                                           gnus-newsgroup-unfetched)
6765                                          ((eq gnus-auto-goto-ignores
6766                                               'undownloaded)
6767                                           gnus-newsgroup-undownloaded)))
6768                             (when (gnus-data-unread-p (car data))
6769                               (setq result (car data)
6770                                     data nil)))
6771                           (setq data (cdr data)))
6772                         result)
6773                     (car data)))
6774         (goto-char (gnus-data-pos result))
6775         (gnus-data-number result)))))
6776
6777 (defun gnus-summary-find-prev (&optional unread article)
6778   (let* ((eobp (eobp))
6779          (article (or article (gnus-summary-article-number)))
6780          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6781          result)
6782     (when (and (not eobp)
6783                (or (not gnus-summary-check-current)
6784                    (not unread)
6785                    (not (gnus-data-unread-p (car data)))))
6786       (setq data (cdr data)))
6787     (when (setq result
6788                 (if unread
6789                     (progn
6790                       (while data
6791                         (unless (memq (gnus-data-number (car data))
6792                                       (cond
6793                                        ((eq gnus-auto-goto-ignores
6794                                             'always-undownloaded)
6795                                         gnus-newsgroup-undownloaded)
6796                                        (gnus-plugged
6797                                         nil)
6798                                        ((eq gnus-auto-goto-ignores
6799                                             'unfetched)
6800                                         gnus-newsgroup-unfetched)
6801                                        ((eq gnus-auto-goto-ignores
6802                                             'undownloaded)
6803                                         gnus-newsgroup-undownloaded)))
6804                           (when (gnus-data-unread-p (car data))
6805                             (setq result (car data)
6806                                   data nil)))
6807                         (setq data (cdr data)))
6808                       result)
6809                   (car data)))
6810       (goto-char (gnus-data-pos result))
6811       (gnus-data-number result))))
6812
6813 (defun gnus-summary-find-subject (subject &optional unread backward article)
6814   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6815          (article (or article (gnus-summary-article-number)))
6816          (articles (gnus-data-list backward))
6817          (arts (gnus-data-find-list article articles))
6818          result)
6819     (when (or (not gnus-summary-check-current)
6820               (not unread)
6821               (not (gnus-data-unread-p (car arts))))
6822       (setq arts (cdr arts)))
6823     (while arts
6824       (and (or (not unread)
6825                (gnus-data-unread-p (car arts)))
6826            (vectorp (gnus-data-header (car arts)))
6827            (gnus-subject-equal
6828             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6829            (setq result (car arts)
6830                  arts nil))
6831       (setq arts (cdr arts)))
6832     (and result
6833          (goto-char (gnus-data-pos result))
6834          (gnus-data-number result))))
6835
6836 (defun gnus-summary-search-forward (&optional unread subject backward)
6837   "Search forward for an article.
6838 If UNREAD, look for unread articles.  If SUBJECT, look for
6839 articles with that subject.  If BACKWARD, search backward instead."
6840   (cond (subject (gnus-summary-find-subject subject unread backward))
6841         (backward (gnus-summary-find-prev unread))
6842         (t (gnus-summary-find-next unread))))
6843
6844 (defun gnus-recenter (&optional n)
6845   "Center point in window and redisplay frame.
6846 Also do horizontal recentering."
6847   (interactive "P")
6848   (when (and gnus-auto-center-summary
6849              (not (eq gnus-auto-center-summary 'vertical)))
6850     (gnus-horizontal-recenter))
6851   (if (fboundp 'recenter-top-bottom)
6852       (recenter-top-bottom n)
6853     (recenter n)))
6854
6855 (put 'gnus-recenter 'isearch-scroll t)
6856
6857 (defun gnus-forward-line-ignore-invisible (n)
6858   "Move N lines forward (backward if N is negative).
6859 Like forward-line, but skip over (and don't count) invisible lines."
6860   (let (done)
6861     (while (and (> n 0) (not done))
6862       ;; If the following character is currently invisible,
6863       ;; skip all characters with that same `invisible' property value.
6864       (while (gnus-invisible-p (point))
6865         (goto-char (gnus-next-char-property-change (point))))
6866       (forward-line 1)
6867       (if (eobp)
6868           (setq done t)
6869         (setq n (1- n))))
6870     (while (and (< n 0) (not done))
6871       (forward-line -1)
6872       (if (bobp) (setq done t)
6873         (setq n (1+ n))
6874         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6875           (goto-char (gnus-previous-char-property-change (point))))))))
6876
6877 (defun gnus-summary-recenter ()
6878   "Center point in the summary window.
6879 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6880 displayed, no centering will be performed."
6881   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6882   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6883   (interactive)
6884   ;; The user has to want it.
6885   (when gnus-auto-center-summary
6886     (let* ((top (cond ((< (window-height) 4) 0)
6887                       ((< (window-height) 7) 1)
6888                       (t (if (numberp gnus-auto-center-summary)
6889                              gnus-auto-center-summary
6890                            (/ (1- (window-height)) 2)))))
6891            (height (1- (window-height)))
6892            (bottom (save-excursion
6893                      (goto-char (point-max))
6894                      (gnus-forward-line-ignore-invisible (- height))
6895                      (point)))
6896            (window (get-buffer-window (current-buffer))))
6897       (when (get-buffer-window gnus-article-buffer)
6898         ;; Only do recentering when the article buffer is displayed,
6899         ;; Set the window start to either `bottom', which is the biggest
6900         ;; possible valid number, or the second line from the top,
6901         ;; whichever is the least.
6902         (let ((top-pos (save-excursion
6903                          (gnus-forward-line-ignore-invisible (- top))
6904                          (point))))
6905           (if (> bottom top-pos)
6906               ;; Keep the second line from the top visible
6907               (set-window-start window top-pos)
6908             ;; Try to keep the bottom line visible; if it's partially
6909             ;; obscured, either scroll one more line to make it fully
6910             ;; visible, or revert to using TOP-POS.
6911             (save-excursion
6912               (goto-char (point-max))
6913               (gnus-forward-line-ignore-invisible -1)
6914               (let ((last-line-start (point)))
6915                 (goto-char bottom)
6916                 (set-window-start window (point) t)
6917                 (when (not (pos-visible-in-window-p last-line-start window))
6918                   (gnus-forward-line-ignore-invisible 1)
6919                   (set-window-start window (min (point) top-pos) t)))))))
6920       ;; Do horizontal recentering while we're at it.
6921       (when (and (get-buffer-window (current-buffer) t)
6922                  (not (eq gnus-auto-center-summary 'vertical)))
6923         (let ((selected (selected-window)))
6924           (select-window (get-buffer-window (current-buffer) t))
6925           (gnus-summary-position-point)
6926           (gnus-horizontal-recenter)
6927           (select-window selected))))))
6928
6929 (defun gnus-summary-jump-to-group (newsgroup)
6930   "Move point to NEWSGROUP in group mode buffer."
6931   ;; Keep update point of group mode buffer if visible.
6932   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6933       (save-window-excursion
6934         ;; Take care of tree window mode.
6935         (when (get-buffer-window gnus-group-buffer)
6936           (pop-to-buffer gnus-group-buffer))
6937         (gnus-group-jump-to-group newsgroup))
6938     (save-excursion
6939       ;; Take care of tree window mode.
6940       (if (get-buffer-window gnus-group-buffer 0)
6941           (pop-to-buffer gnus-group-buffer)
6942         (set-buffer gnus-group-buffer))
6943       (gnus-group-jump-to-group newsgroup))))
6944
6945 ;; This function returns a list of article numbers based on the
6946 ;; difference between the ranges of read articles in this group and
6947 ;; the range of active articles.
6948 (defun gnus-list-of-unread-articles (group)
6949   (let* ((read (gnus-info-read (gnus-get-info group)))
6950          (active (or (gnus-active group) (gnus-activate-group group)))
6951          (last (or (cdr active)
6952                    (error "Group %s couldn't be activated " group)))
6953          (bottom (if gnus-newsgroup-maximum-articles
6954                      (max (car active)
6955                           (- last gnus-newsgroup-maximum-articles -1))
6956                    (car active)))
6957          first nlast unread)
6958     ;; If none are read, then all are unread.
6959     (if (not read)
6960         (setq first bottom)
6961       ;; If the range of read articles is a single range, then the
6962       ;; first unread article is the article after the last read
6963       ;; article.  Sounds logical, doesn't it?
6964       (if (and (not (listp (cdr read)))
6965                (or (< (car read) bottom)
6966                    (progn (setq read (list read))
6967                           nil)))
6968           (setq first (max bottom (1+ (cdr read))))
6969         ;; `read' is a list of ranges.
6970         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6971                                   (caar read)))
6972                   1)
6973           (setq first bottom))
6974         (while read
6975           (when first
6976             (while (< first nlast)
6977               (setq unread (cons first unread)
6978                     first (1+ first))))
6979           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6980           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6981           (setq read (cdr read)))))
6982     ;; And add the last unread articles.
6983     (while (<= first last)
6984       (setq unread (cons first unread)
6985             first (1+ first)))
6986     ;; Return the list of unread articles.
6987     (delq 0 (nreverse unread))))
6988
6989 (defun gnus-list-of-read-articles (group)
6990   "Return a list of unread, unticked and non-dormant articles."
6991   (let* ((info (gnus-get-info group))
6992          (marked (gnus-info-marks info))
6993          (active (gnus-active group)))
6994     (and info active
6995          (gnus-list-range-difference
6996           (gnus-list-range-difference
6997            (gnus-sorted-complement
6998             (gnus-uncompress-range
6999              (if gnus-newsgroup-maximum-articles
7000                  (cons (max (car active)
7001                             (- (cdr active)
7002                                gnus-newsgroup-maximum-articles
7003                                -1))
7004                        (cdr active))
7005                active))
7006             (gnus-list-of-unread-articles group))
7007            (cdr (assq 'dormant marked)))
7008           (cdr (assq 'tick marked))))))
7009
7010 ;; This function returns a sequence of article numbers based on the
7011 ;; difference between the ranges of read articles in this group and
7012 ;; the range of active articles.
7013 (defun gnus-sequence-of-unread-articles (group)
7014   (let* ((read (gnus-info-read (gnus-get-info group)))
7015          (active (or (gnus-active group) (gnus-activate-group group)))
7016          (last (cdr active))
7017          (bottom (if gnus-newsgroup-maximum-articles
7018                      (max (car active)
7019                           (- last gnus-newsgroup-maximum-articles -1))
7020                    (car active)))
7021          first nlast unread)
7022     ;; If none are read, then all are unread.
7023     (if (not read)
7024         (setq first bottom)
7025       ;; If the range of read articles is a single range, then the
7026       ;; first unread article is the article after the last read
7027       ;; article.  Sounds logical, doesn't it?
7028       (if (and (not (listp (cdr read)))
7029                (or (< (car read) bottom)
7030                    (progn (setq read (list read))
7031                           nil)))
7032           (setq first (max bottom (1+ (cdr read))))
7033         ;; `read' is a list of ranges.
7034         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7035                                   (caar read)))
7036                   1)
7037           (setq first bottom))
7038         (while read
7039           (when first
7040             (push (cons first nlast) unread))
7041           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7042           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7043           (setq read (cdr read)))))
7044     ;; And add the last unread articles.
7045     (cond ((not (and first last))
7046            nil)
7047           ((< first last)
7048            (push (cons first last) unread))
7049           ((= first last)
7050            (push first unread)))
7051     ;; Return the sequence of unread articles.
7052     (delq 0 (nreverse unread))))
7053
7054 ;; Various summary commands
7055
7056 (defun gnus-summary-select-article-buffer ()
7057   "Reconfigure windows to show the article buffer.
7058 If `gnus-widen-article-window' is set, show only the article
7059 buffer."
7060   (interactive)
7061   (if (not (gnus-buffer-live-p gnus-article-buffer))
7062       (error "There is no article buffer for this summary buffer")
7063     (unless (get-buffer-window gnus-article-buffer)
7064       (gnus-summary-show-article))
7065     (gnus-configure-windows
7066      (if gnus-widen-article-window
7067          'only-article
7068        'article)
7069      t)
7070     (select-window (get-buffer-window gnus-article-buffer))))
7071
7072 (defun gnus-summary-universal-argument (arg)
7073   "Perform any operation on all articles that are process/prefixed."
7074   (interactive "P")
7075   (let ((articles (gnus-summary-work-articles arg))
7076         func article)
7077     (if (eq
7078          (setq
7079           func
7080           (key-binding
7081            (read-key-sequence
7082             (substitute-command-keys
7083              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7084          'undefined)
7085         (gnus-error 1 "Undefined key")
7086       (save-excursion
7087         (while articles
7088           (gnus-summary-goto-subject (setq article (pop articles)))
7089           (let (gnus-newsgroup-processable)
7090             (command-execute func))
7091           (gnus-summary-remove-process-mark article)))))
7092   (gnus-summary-position-point))
7093
7094 (defun gnus-summary-toggle-truncation (&optional arg)
7095   "Toggle truncation of summary lines.
7096 With ARG, turn line truncation on if ARG is positive."
7097   (interactive "P")
7098   (setq truncate-lines
7099         (if (null arg) (not truncate-lines)
7100           (> (prefix-numeric-value arg) 0)))
7101   (redraw-display))
7102
7103 (defun gnus-summary-find-for-reselect ()
7104   "Return the number of an article to stay on across a reselect.
7105 The current article is considered, then following articles, then previous
7106 articles.  An article is sought which is not canceled and isn't a temporary
7107 insertion from another group.  If there's no such then return a dummy 0."
7108   (let (found)
7109     (dolist (rev '(nil t))
7110       (unless found      ; don't demand the reverse list if we don't need it
7111         (let ((data (gnus-data-find-list
7112                      (gnus-summary-article-number) (gnus-data-list rev))))
7113           (while (and data (not found))
7114             (if (and (< 0 (gnus-data-number (car data)))
7115                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7116                 (setq found (gnus-data-number (car data))))
7117             (setq data (cdr data))))))
7118     (or found 0)))
7119
7120 (defun gnus-summary-reselect-current-group (&optional all rescan)
7121   "Exit and then reselect the current newsgroup.
7122 The prefix argument ALL means to select all articles."
7123   (interactive "P")
7124   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7125     (error "Ephemeral groups can't be reselected"))
7126   (let ((current-subject (gnus-summary-find-for-reselect))
7127         (group gnus-newsgroup-name))
7128     (setq gnus-newsgroup-begin nil)
7129     (gnus-summary-exit nil 'leave-hidden)
7130     ;; We have to adjust the point of group mode buffer because
7131     ;; point was moved to the next unread newsgroup by exiting.
7132     (gnus-summary-jump-to-group group)
7133     (when rescan
7134       (save-excursion
7135         (gnus-group-get-new-news-this-group 1)))
7136     (gnus-group-read-group all t)
7137     (gnus-summary-goto-subject current-subject nil t)))
7138
7139 (defun gnus-summary-rescan-group (&optional all)
7140   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7141   (interactive "P")
7142   (let ((config gnus-current-window-configuration))
7143     (gnus-summary-reselect-current-group all t)
7144     (gnus-configure-windows config)
7145     (when (eq config 'article)
7146       (gnus-summary-select-article))))
7147
7148 (defun gnus-summary-update-info (&optional non-destructive)
7149   (save-excursion
7150     (let ((group gnus-newsgroup-name))
7151       (when group
7152         (when gnus-newsgroup-kill-headers
7153           (setq gnus-newsgroup-killed
7154                 (gnus-compress-sequence
7155                  (gnus-sorted-union
7156                   (gnus-list-range-intersection
7157                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7158                   gnus-newsgroup-unreads)
7159                  t)))
7160         (unless (listp (cdr gnus-newsgroup-killed))
7161           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7162         (let ((headers gnus-newsgroup-headers)
7163               (ephemeral-p (gnus-ephemeral-group-p group))
7164               info)
7165           (unless ephemeral-p
7166             (setq info (copy-sequence (gnus-get-info group))
7167                   info (delq (gnus-info-params info) info)))
7168           ;; Set the new ranges of read articles.
7169           (with-current-buffer gnus-group-buffer
7170             (gnus-undo-force-boundary))
7171           (gnus-update-read-articles
7172            group (gnus-sorted-union
7173                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7174           ;; Set the current article marks.
7175           (let ((gnus-newsgroup-scored
7176                  (if (and (not gnus-save-score)
7177                           (not non-destructive))
7178                      nil
7179                    gnus-newsgroup-scored)))
7180             (save-excursion
7181               (gnus-update-marks)))
7182           ;; Do the cross-ref thing.
7183           (when gnus-use-cross-reference
7184             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7185           ;; Do not switch windows but change the buffer to work.
7186           (set-buffer gnus-group-buffer)
7187           (unless ephemeral-p
7188             (gnus-group-update-group
7189              group nil
7190              (equal info
7191                     (setq info (copy-sequence (gnus-get-info group))
7192                           info (delq (gnus-info-params info) info))))))))))
7193
7194 (defun gnus-summary-save-newsrc (&optional force)
7195   "Save the current number of read/marked articles in the dribble buffer.
7196 The dribble buffer will then be saved.
7197 If FORCE (the prefix), also save the .newsrc file(s)."
7198   (interactive "P")
7199   (gnus-summary-update-info t)
7200   (if force
7201       (gnus-save-newsrc-file)
7202     (gnus-dribble-save)))
7203
7204 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7205
7206 (defun gnus-summary-exit (&optional temporary leave-hidden)
7207   "Exit reading current newsgroup, and then return to group selection mode.
7208 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7209   (interactive)
7210   (gnus-set-global-variables)
7211   (when (gnus-buffer-live-p gnus-article-buffer)
7212     (with-current-buffer gnus-article-buffer
7213       (mm-destroy-parts gnus-article-mime-handles)
7214       ;; Set it to nil for safety reason.
7215       (setq gnus-article-mime-handle-alist nil)
7216       (setq gnus-article-mime-handles nil)))
7217   (gnus-kill-save-kill-buffer)
7218   (gnus-async-halt-prefetch)
7219   (let* ((group gnus-newsgroup-name)
7220          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7221          (gnus-group-is-exiting-p t)
7222          (article-buffer gnus-article-buffer)
7223          (original-article-buffer gnus-original-article-buffer)
7224          (mode major-mode)
7225          (group-point nil)
7226          (buf (current-buffer))
7227          ;; `gnus-single-article-buffer' is nil buffer-locally in
7228          ;; ephemeral group of which summary buffer will be killed,
7229          ;; but the global value may be non-nil.
7230          (single-article-buffer gnus-single-article-buffer))
7231     (unless quit-config
7232       ;; Do adaptive scoring, and possibly save score files.
7233       (when gnus-newsgroup-adaptive
7234         (gnus-score-adaptive))
7235       (when gnus-use-scoring
7236         (gnus-score-save)))
7237     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7238     (when gnus-use-cache
7239       (gnus-cache-possibly-remove-articles)
7240       (gnus-cache-save-buffers))
7241     (gnus-async-prefetch-remove-group group)
7242     (when gnus-suppress-duplicates
7243       (gnus-dup-enter-articles))
7244     (when gnus-use-trees
7245       (gnus-tree-close group))
7246     (when gnus-use-cache
7247       (gnus-cache-write-active))
7248     ;; Remove entries for this group.
7249     (nnmail-purge-split-history (gnus-group-real-name group))
7250     ;; Make all changes in this group permanent.
7251     (unless quit-config
7252       (gnus-run-hooks 'gnus-exit-group-hook)
7253       (gnus-summary-update-info))
7254     (gnus-close-group group)
7255     ;; Make sure where we were, and go to next newsgroup.
7256     (set-buffer gnus-group-buffer)
7257     (unless quit-config
7258       (gnus-group-jump-to-group group))
7259     (gnus-run-hooks 'gnus-summary-exit-hook)
7260     (unless (or quit-config
7261                 (not gnus-summary-next-group-on-exit)
7262                 ;; If this group has disappeared from the summary
7263                 ;; buffer, don't skip forwards.
7264                 (not (string= group (gnus-group-group-name))))
7265       (gnus-group-next-unread-group 1))
7266     (setq group-point (point))
7267     (if temporary
7268         nil                             ;Nothing to do.
7269       (set-buffer buf)
7270       (if (not gnus-kill-summary-on-exit)
7271           (progn
7272             (gnus-deaden-summary)
7273             (setq mode nil))
7274         (when (get-buffer gnus-article-buffer)
7275           (bury-buffer gnus-article-buffer))
7276         ;; Return to group mode buffer.
7277         (when (eq mode 'gnus-summary-mode)
7278           (gnus-kill-buffer buf)))
7279
7280       (setq gnus-current-select-method gnus-select-method)
7281       (set-buffer gnus-group-buffer)
7282       (if quit-config
7283           (gnus-handle-ephemeral-exit quit-config)
7284         (goto-char group-point)
7285         ;; If gnus-group-buffer is already displayed, make sure we also move
7286         ;; the cursor in the window that displays it.
7287         (let ((win (get-buffer-window (current-buffer) 0)))
7288           (if win (set-window-point win (point))))
7289         (unless leave-hidden
7290           (gnus-configure-windows 'group 'force)))
7291
7292       ;; If we have several article buffers, we kill them at exit.
7293       (unless single-article-buffer
7294         (when (gnus-buffer-live-p article-buffer)
7295           (with-current-buffer article-buffer
7296             ;; Don't kill sticky article buffers
7297             (unless (eq major-mode 'gnus-sticky-article-mode)
7298               (gnus-kill-buffer article-buffer)
7299               (setq gnus-article-current nil))))
7300         (gnus-kill-buffer original-article-buffer))
7301
7302       ;; Clear the current group name.
7303       (unless quit-config
7304         (setq gnus-newsgroup-name nil)))))
7305
7306 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7307 (defun gnus-summary-exit-no-update (&optional no-questions)
7308   "Quit reading current newsgroup without updating read article info."
7309   (interactive)
7310   (let* ((group gnus-newsgroup-name)
7311          (gnus-group-is-exiting-p t)
7312          (gnus-group-is-exiting-without-update-p t)
7313          (quit-config (gnus-group-quit-config group)))
7314     (when (or no-questions
7315               gnus-expert-user
7316               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7317       (gnus-async-halt-prefetch)
7318       (run-hooks 'gnus-summary-prepare-exit-hook)
7319       (when (gnus-buffer-live-p gnus-article-buffer)
7320         (with-current-buffer gnus-article-buffer
7321           (gnus-article-stop-animations)
7322           (mm-destroy-parts gnus-article-mime-handles)
7323           ;; Set it to nil for safety reason.
7324           (setq gnus-article-mime-handle-alist nil)
7325           (setq gnus-article-mime-handles nil)))
7326       ;; If we have several article buffers, we kill them at exit.
7327       (unless gnus-single-article-buffer
7328         (gnus-kill-buffer gnus-article-buffer)
7329         (gnus-kill-buffer gnus-original-article-buffer)
7330         (setq gnus-article-current nil))
7331       ;; Return to the group buffer.
7332       (if (not gnus-kill-summary-on-exit)
7333           (progn
7334             (gnus-deaden-summary)
7335             (gnus-configure-windows 'group 'force))
7336         (gnus-configure-windows 'group 'force)
7337         (gnus-close-group group)
7338         (gnus-kill-buffer gnus-summary-buffer))
7339       (unless gnus-single-article-buffer
7340         (setq gnus-article-current nil))
7341       (when gnus-use-trees
7342         (gnus-tree-close group))
7343       (gnus-async-prefetch-remove-group group)
7344       (when (get-buffer gnus-article-buffer)
7345         (bury-buffer gnus-article-buffer))
7346       ;; Clear the current group name.
7347       (setq gnus-newsgroup-name nil)
7348       (unless (gnus-ephemeral-group-p group)
7349         (gnus-group-update-group group nil t))
7350       (when (equal (gnus-group-group-name) group)
7351         (gnus-group-next-unread-group 1))
7352       (when quit-config
7353         (gnus-handle-ephemeral-exit quit-config)))))
7354
7355 (defun gnus-handle-ephemeral-exit (quit-config)
7356   "Handle movement when leaving an ephemeral group.
7357 The state which existed when entering the ephemeral is reset."
7358   (if (not (buffer-live-p (car quit-config)))
7359       (gnus-configure-windows 'group 'force)
7360     (set-buffer (car quit-config))
7361     (unless (eq (cdr quit-config) 'group)
7362       (setq gnus-current-select-method
7363             (gnus-find-method-for-group gnus-newsgroup-name)))
7364     (cond ((eq major-mode 'gnus-summary-mode)
7365            (gnus-set-global-variables))
7366           ((eq major-mode 'gnus-article-mode)
7367            (save-current-buffer
7368              ;; The `gnus-summary-buffer' variable may point
7369              ;; to the old summary buffer when using a single
7370              ;; article buffer.
7371              (unless (gnus-buffer-live-p gnus-summary-buffer)
7372                (set-buffer gnus-group-buffer))
7373              (set-buffer gnus-summary-buffer)
7374              (gnus-set-global-variables))))
7375     (if (or (eq (cdr quit-config) 'article)
7376             (eq (cdr quit-config) 'pick))
7377         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7378             (gnus-configure-windows 'pick 'force)
7379           (gnus-configure-windows (cdr quit-config) 'force))
7380       (gnus-configure-windows (cdr quit-config) 'force))
7381     (when (eq major-mode 'gnus-summary-mode)
7382       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7383                                                      next-unread-noselect))
7384           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7385                                   'next-noselect)
7386                               (gnus-summary-next-subject 1 nil t))
7387                              ((eq gnus-auto-select-on-ephemeral-exit
7388                                   'next-unread-noselect)
7389                               (gnus-summary-next-subject 1 t t))))
7390             ;; Hide the article buffer which displays the article different
7391             ;; from the one that the cursor points to in the summary buffer.
7392             (gnus-configure-windows 'summary 'force))
7393         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7394                (gnus-summary-next-subject 1))
7395               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7396                (gnus-summary-next-subject 1 t))))
7397       (gnus-summary-recenter)
7398       (gnus-summary-position-point))))
7399
7400 ;;; Dead summaries.
7401
7402 (defvar gnus-dead-summary-mode-map
7403   (let ((map (make-keymap)))
7404     (suppress-keymap map)
7405     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7406     (dolist (key '("\C-d" "\r" "\177" [delete]))
7407       (define-key map key 'gnus-summary-wake-up-the-dead))
7408     (dolist (key '("q" "Q"))
7409       (define-key map key 'bury-buffer))
7410     map))
7411
7412 (define-minor-mode gnus-dead-summary-mode
7413   "Minor mode for Gnus summary buffers."
7414   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7415   (unless (derived-mode-p 'gnus-summary-mode)
7416     (setq gnus-dead-summary-mode nil)))
7417
7418 (defun gnus-deaden-summary ()
7419   "Make the current summary buffer into a dead summary buffer."
7420   ;; Kill any previous dead summary buffer.
7421   (when (and gnus-dead-summary
7422              (buffer-name gnus-dead-summary))
7423     (with-current-buffer gnus-dead-summary
7424       (when gnus-dead-summary-mode
7425         (kill-buffer (current-buffer)))))
7426   ;; Make this the current dead summary.
7427   (setq gnus-dead-summary (current-buffer))
7428   (gnus-dead-summary-mode 1)
7429   (let ((name (buffer-name)))
7430     (when (string-match "Summary" name)
7431       (rename-buffer
7432        (concat (substring name 0 (match-beginning 0)) "Dead "
7433                (substring name (match-beginning 0)))
7434        t)
7435       (bury-buffer))))
7436
7437 (defun gnus-kill-or-deaden-summary (buffer)
7438   "Kill or deaden the summary BUFFER."
7439   (save-excursion
7440     (when (and (buffer-name buffer)
7441                (not gnus-single-article-buffer))
7442       (with-current-buffer buffer
7443         (gnus-kill-buffer gnus-article-buffer)
7444         (gnus-kill-buffer gnus-original-article-buffer)))
7445     (cond
7446      ;; Kill the buffer.
7447      (gnus-kill-summary-on-exit
7448       (when (and gnus-use-trees
7449                  (gnus-buffer-exists-p buffer))
7450         (with-current-buffer buffer
7451           (gnus-tree-close gnus-newsgroup-name)))
7452       (gnus-kill-buffer buffer))
7453      ;; Deaden the buffer.
7454      ((gnus-buffer-exists-p buffer)
7455       (with-current-buffer buffer
7456         (gnus-deaden-summary))))))
7457
7458 (defun gnus-summary-wake-up-the-dead (&rest args)
7459   "Wake up the dead summary buffer."
7460   (interactive)
7461   (gnus-dead-summary-mode -1)
7462   (let ((name (buffer-name)))
7463     (when (string-match "Dead " name)
7464       (rename-buffer
7465        (concat (substring name 0 (match-beginning 0))
7466                (substring name (match-end 0)))
7467        t)))
7468   (gnus-message 3 "This dead summary is now alive again"))
7469
7470 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7471 (defun gnus-summary-describe-group (&optional force)
7472   "Describe the current newsgroup."
7473   (interactive "P")
7474   (gnus-group-describe-group force gnus-newsgroup-name))
7475
7476 (defun gnus-summary-describe-briefly ()
7477   "Describe summary mode commands briefly."
7478   (interactive)
7479   (gnus-message 6 "%s" (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")))
7480
7481 ;; Walking around group mode buffer from summary mode.
7482
7483 (defun gnus-summary-next-group (&optional no-article target-group backward)
7484   "Exit current newsgroup and then select next unread newsgroup.
7485 If prefix argument NO-ARTICLE is non-nil, no article is selected
7486 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7487 previous group instead."
7488   (interactive "P")
7489   ;; Stop pre-fetching.
7490   (gnus-async-halt-prefetch)
7491   (let ((current-group gnus-newsgroup-name)
7492         (current-buffer (current-buffer))
7493         entered)
7494     ;; First we semi-exit this group to update Xrefs and all variables.
7495     ;; We can't do a real exit, because the window conf must remain
7496     ;; the same in case the user is prompted for info, and we don't
7497     ;; want the window conf to change before that...
7498     (gnus-summary-exit t)
7499     (while (not entered)
7500       ;; Then we find what group we are supposed to enter.
7501       (set-buffer gnus-group-buffer)
7502       (gnus-group-jump-to-group current-group)
7503       (setq target-group
7504             (or target-group
7505                 (if (eq gnus-keep-same-level 'best)
7506                     (gnus-summary-best-group gnus-newsgroup-name)
7507                   (gnus-summary-search-group backward gnus-keep-same-level))))
7508       (if (not target-group)
7509           ;; There are no further groups, so we return to the group
7510           ;; buffer.
7511           (progn
7512             (gnus-message 5 "Returning to the group buffer")
7513             (setq entered t)
7514             (when (gnus-buffer-live-p current-buffer)
7515               (set-buffer current-buffer)
7516               (gnus-summary-exit))
7517             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7518         ;; We try to enter the target group.
7519         (gnus-group-jump-to-group target-group)
7520         (let ((unreads (gnus-group-group-unread)))
7521           (if (and (or (eq t unreads)
7522                        (and unreads (not (zerop unreads))))
7523                    (gnus-summary-read-group
7524                     target-group nil no-article
7525                     (and (buffer-name current-buffer) current-buffer)
7526                     nil backward))
7527               (setq entered t)
7528             (setq current-group target-group
7529                   target-group nil)))))))
7530
7531 (defun gnus-summary-prev-group (&optional no-article)
7532   "Exit current newsgroup and then select previous unread newsgroup.
7533 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7534   (interactive "P")
7535   (gnus-summary-next-group no-article nil t))
7536
7537 ;; Walking around summary lines.
7538
7539 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7540   "Go to the first subject satisfying any non-nil constraint.
7541 If UNREAD is non-nil, the article should be unread.
7542 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7543 If UNSEEN is non-nil, the article should be unseen as well as unread.
7544 Returns the article selected or nil if there are no matching articles."
7545   (interactive "P")
7546   (cond
7547    ;; Empty summary.
7548    ((null gnus-newsgroup-data)
7549     (gnus-message 3 "No articles in the group")
7550     nil)
7551    ;; Pick the first article.
7552    ((not (or unread undownloaded unseen))
7553     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7554     (gnus-data-number (car gnus-newsgroup-data)))
7555    ;; Find the first unread article.
7556    (t
7557     (let ((data gnus-newsgroup-data))
7558       (while (and data
7559                   (let ((num (gnus-data-number (car data))))
7560                     (or (memq num gnus-newsgroup-unfetched)
7561                         (not (or (and unread
7562                                       (memq num gnus-newsgroup-unreads))
7563                                  (and undownloaded
7564                                       (memq num gnus-newsgroup-undownloaded))
7565                                  (and unseen
7566                                       (memq num gnus-newsgroup-unseen)
7567                                       (memq num gnus-newsgroup-unreads)))))))
7568         (setq data (cdr data)))
7569       (prog1
7570           (if data
7571               (progn
7572                 (goto-char (gnus-data-pos (car data)))
7573                 (gnus-data-number (car data)))
7574             (gnus-message 3 "No more%s articles"
7575                           (let* ((r (when unread " unread"))
7576                                  (d (when undownloaded " undownloaded"))
7577                                  (s (when unseen " unseen"))
7578                                  (l (delq nil (list r d s))))
7579                             (cond ((= 3 (length l))
7580                                    (concat r "," d ", or" s))
7581                                   ((= 2 (length l))
7582                                    (concat (car l) ", or" (cadr l)))
7583                                   ((= 1 (length l))
7584                                    (car l))
7585                                   (t
7586                                    ""))))
7587             nil
7588             )
7589         (gnus-summary-position-point))))))
7590
7591 (defun gnus-summary-next-subject (n &optional unread dont-display)
7592   "Go to next N'th summary line.
7593 If N is negative, go to the previous N'th subject line.
7594 If UNREAD is non-nil, only unread articles are selected.
7595 The difference between N and the actual number of steps taken is
7596 returned."
7597   (interactive "p")
7598   (let ((backward (< n 0))
7599         (n (abs n)))
7600     (while (and (> n 0)
7601                 (if backward
7602                     (gnus-summary-find-prev unread)
7603                   (gnus-summary-find-next unread)))
7604       (unless (zerop (setq n (1- n)))
7605         (gnus-summary-show-thread)))
7606     (when (/= 0 n)
7607       (gnus-message 7 "No more%s articles"
7608                     (if unread " unread" "")))
7609     (unless dont-display
7610       (gnus-summary-recenter)
7611       (gnus-summary-position-point))
7612     n))
7613
7614 (defun gnus-summary-next-unread-subject (n)
7615   "Go to next N'th unread summary line."
7616   (interactive "p")
7617   (gnus-summary-next-subject n t))
7618
7619 (defun gnus-summary-prev-subject (n &optional unread)
7620   "Go to previous N'th summary line.
7621 If optional argument UNREAD is non-nil, only unread article is selected."
7622   (interactive "p")
7623   (gnus-summary-next-subject (- n) unread))
7624
7625 (defun gnus-summary-prev-unread-subject (n)
7626   "Go to previous N'th unread summary line."
7627   (interactive "p")
7628   (gnus-summary-next-subject (- n) t))
7629
7630 (defun gnus-summary-goto-subjects (articles)
7631   "Insert the subject header for ARTICLES in the current buffer."
7632   (save-excursion
7633     (dolist (article articles)
7634       (gnus-summary-goto-subject article t)))
7635   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7636   (gnus-summary-position-point))
7637
7638 (defun gnus-summary-goto-subject (article &optional force silent)
7639   "Go to the subject line of ARTICLE.
7640 If FORCE, also allow jumping to articles not currently shown."
7641   (interactive "nArticle number: ")
7642   (unless (numberp article)
7643     (error "Article %s is not a number" article))
7644   (let ((b (point))
7645         (data (gnus-data-find article)))
7646     ;; We read in the article if we have to.
7647     (and (not data)
7648          force
7649          (gnus-summary-insert-subject
7650           article
7651           (if (or (numberp force) (vectorp force)) force)
7652           t)
7653          (setq data (gnus-data-find article)))
7654     (goto-char b)
7655     (if (not data)
7656         (progn
7657           (unless silent
7658             (gnus-message 3 "Can't find article %d" article))
7659           nil)
7660       (let ((pt (gnus-data-pos data)))
7661         (goto-char pt)
7662         (gnus-summary-set-article-display-arrow pt))
7663       (gnus-summary-position-point)
7664       article)))
7665
7666 ;; Walking around summary lines with displaying articles.
7667
7668 (defun gnus-summary-expand-window (&optional arg)
7669   "Make the summary buffer take up the entire Emacs frame.
7670 Given a prefix, will force an `article' buffer configuration."
7671   (interactive "P")
7672   (if arg
7673       (gnus-configure-windows 'article 'force)
7674     (gnus-configure-windows 'summary 'force)))
7675
7676 (defun gnus-summary-display-article (article &optional all-header)
7677   "Display ARTICLE in article buffer."
7678   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7679                (with-current-buffer gnus-article-buffer
7680                  (eq major-mode 'gnus-article-mode)))
7681     (gnus-article-setup-buffer))
7682   (gnus-set-global-variables)
7683   (with-current-buffer gnus-article-buffer
7684     (setq gnus-article-charset gnus-newsgroup-charset)
7685     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7686     (mm-enable-multibyte))
7687   (if (null article)
7688       nil
7689     (prog1
7690         (if gnus-summary-display-article-function
7691             (funcall gnus-summary-display-article-function article all-header)
7692           (gnus-article-prepare article all-header))
7693       (gnus-run-hooks 'gnus-select-article-hook)
7694       (when (and gnus-current-article
7695                  (not (zerop gnus-current-article)))
7696         (gnus-summary-goto-subject gnus-current-article))
7697       (gnus-summary-recenter)
7698       (when (and gnus-use-trees gnus-show-threads)
7699         (gnus-possibly-generate-tree article)
7700         (gnus-highlight-selected-tree article))
7701       ;; Successfully display article.
7702       (gnus-article-set-window-start
7703        (cdr (assq article gnus-newsgroup-bookmarks))))))
7704
7705 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7706   "Select the current article.
7707 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7708 non-nil, the article will be re-fetched even if it already present in
7709 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7710 be displayed."
7711   ;; Make sure we are in the summary buffer to work around bbdb bug.
7712   (unless (eq major-mode 'gnus-summary-mode)
7713     (set-buffer gnus-summary-buffer))
7714   (let ((article (or article (gnus-summary-article-number)))
7715         (all-headers (not (not all-headers))) ;Must be t or nil.
7716         gnus-summary-display-article-function)
7717     (and (not pseudo)
7718          (gnus-summary-article-pseudo-p article)
7719          (error "This is a pseudo-article"))
7720     (with-current-buffer gnus-summary-buffer
7721       (if (or (and gnus-single-article-buffer
7722                    (or (null gnus-current-article)
7723                        (null gnus-article-current)
7724                        (null (get-buffer gnus-article-buffer))
7725                        (not (eq article (cdr gnus-article-current)))
7726                        (not (equal (car gnus-article-current)
7727                                    gnus-newsgroup-name))
7728                        (not (get-buffer gnus-original-article-buffer))))
7729               (and (not gnus-single-article-buffer)
7730                    (or (null gnus-current-article)
7731                        (not (get-buffer gnus-original-article-buffer))
7732                        (not (eq gnus-current-article article))))
7733               force)
7734           ;; The requested article is different from the current article.
7735           (progn
7736             (gnus-summary-display-article article all-headers)
7737             (when (gnus-buffer-live-p gnus-article-buffer)
7738               (with-current-buffer gnus-article-buffer
7739                 (if (not gnus-article-decoded-p) ;; a local variable
7740                     (mm-disable-multibyte))))
7741             (gnus-article-set-window-start
7742              (cdr (assq article gnus-newsgroup-bookmarks)))
7743             article)
7744         'old))))
7745
7746 (defun gnus-summary-force-verify-and-decrypt ()
7747   "Display buttons for signed/encrypted parts and verify/decrypt them."
7748   (interactive)
7749   (let ((mm-verify-option 'known)
7750         (mm-decrypt-option 'known)
7751         (gnus-article-emulate-mime t)
7752         (gnus-buttonized-mime-types (append (list "multipart/signed"
7753                                                   "multipart/encrypted")
7754                                             gnus-buttonized-mime-types)))
7755     (gnus-summary-select-article nil 'force)))
7756
7757 (defun gnus-summary-set-current-mark (&optional current-mark)
7758   "Obsolete function."
7759   nil)
7760
7761 (defun gnus-summary-next-article (&optional unread subject backward push)
7762   "Select the next article.
7763 If UNREAD, only unread articles are selected.
7764 If SUBJECT, only articles with SUBJECT are selected.
7765 If BACKWARD, the previous article is selected instead of the next."
7766   (interactive "P")
7767   ;; Make sure we are in the summary buffer.
7768   (unless (eq major-mode 'gnus-summary-mode)
7769     (set-buffer gnus-summary-buffer))
7770   (cond
7771    ;; Is there such an article?
7772    ((and (gnus-summary-search-forward unread subject backward)
7773          (or (gnus-summary-display-article (gnus-summary-article-number))
7774              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7775     (gnus-summary-position-point))
7776    ;; If not, we try the first unread, if that is wanted.
7777    ((and subject
7778          gnus-auto-select-same
7779          (gnus-summary-first-unread-article))
7780     (gnus-summary-position-point)
7781     (gnus-message 6 "Wrapped"))
7782    ;; Try to get next/previous article not displayed in this group.
7783    ((and gnus-auto-extend-newsgroup
7784          (not unread) (not subject))
7785     (gnus-summary-goto-article
7786      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7787      nil (count-lines (point-min) (point))))
7788    ;; Go to next/previous group.
7789    (t
7790     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7791       (gnus-summary-jump-to-group gnus-newsgroup-name))
7792     (let ((cmd (if (featurep 'xemacs)
7793                    last-command-char
7794                  last-command-event))
7795           (point
7796            (with-current-buffer gnus-group-buffer
7797              (point)))
7798           (current-summary (current-buffer))
7799           (group
7800            (if (eq gnus-keep-same-level 'best)
7801                (gnus-summary-best-group gnus-newsgroup-name)
7802              (gnus-summary-search-group backward gnus-keep-same-level))))
7803       ;; Select next unread newsgroup automagically.
7804       (cond
7805        ((or (not gnus-auto-select-next)
7806             (not cmd))
7807         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7808        ((or (eq gnus-auto-select-next 'quietly)
7809             (and (eq gnus-auto-select-next 'slightly-quietly)
7810                  push)
7811             (and (eq gnus-auto-select-next 'almost-quietly)
7812                  (gnus-summary-last-article-p)))
7813         ;; Select quietly.
7814         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7815             (gnus-summary-exit)
7816           (gnus-message 7 "No more%s articles (%s)..."
7817                         (if unread " unread" "")
7818                         (if group (concat "selecting " group)
7819                           "exiting"))
7820           (gnus-summary-next-group nil group backward)))
7821        (t
7822         (when (gnus-key-press-event-p last-input-event)
7823           ;; Somehow or other, we may now have selected a different
7824           ;; window.  Make point go back to the summary buffer.
7825           (when (eq current-summary (current-buffer))
7826             ;; FIXME: This burps when get-buffer-window returns nil.
7827             (select-window (get-buffer-window current-summary 0)))
7828           (gnus-summary-walk-group-buffer
7829            gnus-newsgroup-name cmd unread backward point))))))))
7830
7831 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7832   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7833                       (?\C-p (gnus-group-prev-unread-group 1))))
7834         (cursor-in-echo-area t)
7835         keve key group ended prompt)
7836     (with-current-buffer gnus-group-buffer
7837       (goto-char start)
7838       (setq group
7839             (if (eq gnus-keep-same-level 'best)
7840                 (gnus-summary-best-group gnus-newsgroup-name)
7841               (gnus-summary-search-group backward gnus-keep-same-level))))
7842     (while (not ended)
7843       (setq prompt
7844             (format
7845              "No more%s articles%s " (if unread " unread" "")
7846              (if (and group
7847                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7848                  (format " (Type %s for %s [%s])"
7849                          (single-key-description cmd)
7850                          (gnus-group-decoded-name group)
7851                          (gnus-group-unread group))
7852                (format " (Type %s to exit %s)"
7853                        (single-key-description cmd)
7854                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7855       ;; Confirm auto selection.
7856       (setq key (car (setq keve (gnus-read-event-char prompt)))
7857             ended t)
7858       (cond
7859        ((assq key keystrokes)
7860         (let ((obuf (current-buffer)))
7861           (switch-to-buffer gnus-group-buffer)
7862           (when group
7863             (gnus-group-jump-to-group group))
7864           (eval (cadr (assq key keystrokes)))
7865           (setq group (gnus-group-group-name))
7866           (switch-to-buffer obuf))
7867         (setq ended nil))
7868        ((equal key cmd)
7869         (if (or (not group)
7870                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7871             (gnus-summary-exit)
7872           (gnus-summary-next-group nil group backward)))
7873        (t
7874         (push (cdr keve) unread-command-events))))))
7875
7876 (defun gnus-summary-next-unread-article ()
7877   "Select unread article after current one."
7878   (interactive)
7879   (gnus-summary-next-article
7880    (or (not (eq gnus-summary-goto-unread 'never))
7881        (gnus-summary-last-article-p (gnus-summary-article-number)))
7882    (and gnus-auto-select-same
7883         (gnus-summary-article-subject))))
7884
7885 (defun gnus-summary-prev-article (&optional unread subject)
7886   "Select the article before the current one.
7887 If UNREAD is non-nil, only unread articles are selected."
7888   (interactive "P")
7889   (gnus-summary-next-article unread subject t))
7890
7891 (defun gnus-summary-prev-unread-article ()
7892   "Select unread article before current one."
7893   (interactive)
7894   (gnus-summary-prev-article
7895    (or (not (eq gnus-summary-goto-unread 'never))
7896        (gnus-summary-first-article-p (gnus-summary-article-number)))
7897    (and gnus-auto-select-same
7898         (gnus-summary-article-subject))))
7899
7900 (defun gnus-summary-next-page (&optional lines circular stop)
7901   "Show next page of the selected article.
7902 If at the end of the current article, select the next article.
7903 LINES says how many lines should be scrolled up.
7904
7905 If CIRCULAR is non-nil, go to the start of the article instead of
7906 selecting the next article when reaching the end of the current
7907 article.
7908
7909 If STOP is non-nil, just stop when reaching the end of the message.
7910
7911 Also see the variable `gnus-article-skip-boring'."
7912   (interactive "P")
7913   (setq gnus-summary-buffer (current-buffer))
7914   (gnus-set-global-variables)
7915   (let ((article (gnus-summary-article-number))
7916         (article-window (get-buffer-window gnus-article-buffer t))
7917         endp)
7918     ;; If the buffer is empty, we have no article.
7919     (unless article
7920       (error "No article to select"))
7921     (gnus-configure-windows 'article)
7922     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7923         (if (and (eq gnus-summary-goto-unread 'never)
7924                  (not (gnus-summary-last-article-p article)))
7925             (gnus-summary-next-article)
7926           (gnus-summary-next-unread-article))
7927       (if (or (null gnus-current-article)
7928               (null gnus-article-current)
7929               (/= article (cdr gnus-article-current))
7930               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7931           ;; Selected subject is different from current article's.
7932           (gnus-summary-display-article article)
7933         (when article-window
7934           (gnus-eval-in-buffer-window gnus-article-buffer
7935             (setq endp (or (gnus-article-next-page lines)
7936                            (gnus-article-only-boring-p))))
7937           (when endp
7938             (cond ((or stop gnus-summary-stop-at-end-of-message)
7939                    (gnus-message 3 "End of message"))
7940                   (circular
7941                    (gnus-summary-beginning-of-article))
7942                   (lines
7943                    (gnus-message 3 "End of message"))
7944                   ((null lines)
7945                    (if (and (eq gnus-summary-goto-unread 'never)
7946                             (not (gnus-summary-last-article-p article)))
7947                        (gnus-summary-next-article)
7948                      (gnus-summary-next-unread-article))))))))
7949     (gnus-summary-recenter)
7950     (gnus-summary-position-point)))
7951
7952 (defun gnus-summary-prev-page (&optional lines move)
7953   "Show previous page of selected article.
7954 Argument LINES specifies lines to be scrolled down.
7955 If MOVE, move to the previous unread article if point is at
7956 the beginning of the buffer."
7957   (interactive "P")
7958   (let ((article (gnus-summary-article-number))
7959         (article-window (get-buffer-window gnus-article-buffer t))
7960         endp)
7961     (gnus-configure-windows 'article)
7962     (if (or (null gnus-current-article)
7963             (null gnus-article-current)
7964             (/= article (cdr gnus-article-current))
7965             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7966         ;; Selected subject is different from current article's.
7967         (gnus-summary-display-article article)
7968       (gnus-summary-recenter)
7969       (when article-window
7970         (gnus-eval-in-buffer-window gnus-article-buffer
7971           (setq endp (gnus-article-prev-page lines)))
7972         (when (and move endp)
7973           (cond (lines
7974                  (gnus-message 3 "Beginning of message"))
7975                 ((null lines)
7976                  (if (and (eq gnus-summary-goto-unread 'never)
7977                           (not (gnus-summary-first-article-p article)))
7978                      (gnus-summary-prev-article)
7979                    (gnus-summary-prev-unread-article))))))))
7980   (gnus-summary-position-point))
7981
7982 (defun gnus-summary-prev-page-or-article (&optional lines)
7983   "Show previous page of selected article.
7984 Argument LINES specifies lines to be scrolled down.
7985 If at the beginning of the article, go to the next article."
7986   (interactive "P")
7987   (gnus-summary-prev-page lines t))
7988
7989 (defun gnus-summary-scroll-up (lines)
7990   "Scroll up (or down) one line current article.
7991 Argument LINES specifies lines to be scrolled up (or down if negative).
7992 If no article is selected, then the current article will be selected first."
7993   (interactive "p")
7994   (gnus-configure-windows 'article)
7995   (gnus-summary-show-thread)
7996   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7997     (gnus-eval-in-buffer-window gnus-article-buffer
7998       (cond ((> lines 0)
7999              (when (gnus-article-next-page lines)
8000                (gnus-message 3 "End of message")))
8001             ((< lines 0)
8002              (gnus-article-prev-page (- lines))))))
8003   (gnus-summary-recenter)
8004   (gnus-summary-position-point))
8005
8006 (defun gnus-summary-scroll-down (lines)
8007   "Scroll down (or up) one line current article.
8008 Argument LINES specifies lines to be scrolled down (or up if negative).
8009 If no article is selected, then the current article will be selected first."
8010   (interactive "p")
8011   (gnus-summary-scroll-up (- lines)))
8012
8013 (defun gnus-summary-next-same-subject ()
8014   "Select next article which has the same subject as current one."
8015   (interactive)
8016   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8017
8018 (defun gnus-summary-prev-same-subject ()
8019   "Select previous article which has the same subject as current one."
8020   (interactive)
8021   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8022
8023 (defun gnus-summary-next-unread-same-subject ()
8024   "Select next unread article which has the same subject as current one."
8025   (interactive)
8026   (gnus-summary-next-article t (gnus-summary-article-subject)))
8027
8028 (defun gnus-summary-prev-unread-same-subject ()
8029   "Select previous unread article which has the same subject as current one."
8030   (interactive)
8031   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8032
8033 (defun gnus-summary-first-unread-article ()
8034   "Select the first unread article.
8035 Return nil if there are no unread articles."
8036   (interactive)
8037   (prog1
8038       (when (gnus-summary-first-subject t)
8039         (gnus-summary-show-thread)
8040         (gnus-summary-first-subject t)
8041         (gnus-summary-display-article (gnus-summary-article-number)))
8042     (gnus-summary-position-point)))
8043
8044 (defun gnus-summary-first-unread-subject ()
8045   "Place the point on the subject line of the first unread article.
8046 Return nil if there are no unread articles."
8047   (interactive)
8048   (prog1
8049       (when (gnus-summary-first-subject t)
8050         (gnus-summary-show-thread)
8051         (gnus-summary-first-subject t))
8052     (gnus-summary-position-point)))
8053
8054 (defun gnus-summary-first-unseen-subject ()
8055   "Place the point on the subject line of the first unseen article.
8056 Return nil if there are no unseen articles."
8057   (interactive)
8058   (prog1
8059       (when (gnus-summary-first-subject nil nil t)
8060         (gnus-summary-show-thread)
8061         (gnus-summary-first-subject nil nil t))
8062     (gnus-summary-position-point)))
8063
8064 (defun gnus-summary-first-unseen-or-unread-subject ()
8065   "Place the point on the subject line of the first unseen and unread article.
8066 If all article have been seen, on the subject line of the first unread
8067 article."
8068   (interactive)
8069   (prog1
8070       (unless (when (gnus-summary-first-subject nil nil t)
8071                 (gnus-summary-show-thread)
8072                 (gnus-summary-first-subject nil nil t))
8073         (when (gnus-summary-first-subject t)
8074           (gnus-summary-show-thread)
8075           (gnus-summary-first-subject t)))
8076     (gnus-summary-position-point)))
8077
8078 (defun gnus-summary-first-article ()
8079   "Select the first article.
8080 Return nil if there are no articles."
8081   (interactive)
8082   (prog1
8083       (when (gnus-summary-first-subject)
8084         (gnus-summary-show-thread)
8085         (gnus-summary-first-subject)
8086         (gnus-summary-display-article (gnus-summary-article-number)))
8087     (gnus-summary-position-point)))
8088
8089 (defun gnus-summary-best-unread-article (&optional arg)
8090   "Select the unread article with the highest score.
8091 If given a prefix argument, select the next unread article that has a
8092 score higher than the default score."
8093   (interactive "P")
8094   (let ((article (if arg
8095                      (gnus-summary-better-unread-subject)
8096                    (gnus-summary-best-unread-subject))))
8097     (if article
8098         (gnus-summary-goto-article article)
8099       (error "No unread articles"))))
8100
8101 (defun gnus-summary-best-unread-subject ()
8102   "Select the unread subject with the highest score."
8103   (interactive)
8104   (let ((best -1000000)
8105         (data gnus-newsgroup-data)
8106         article score)
8107     (while data
8108       (and (gnus-data-unread-p (car data))
8109            (> (setq score
8110                     (gnus-summary-article-score (gnus-data-number (car data))))
8111               best)
8112            (setq best score
8113                  article (gnus-data-number (car data))))
8114       (setq data (cdr data)))
8115     (when article
8116       (gnus-summary-goto-subject article))
8117     (gnus-summary-position-point)
8118     article))
8119
8120 (defun gnus-summary-better-unread-subject ()
8121   "Select the first unread subject that has a score over the default score."
8122   (interactive)
8123   (let ((data gnus-newsgroup-data)
8124         article score)
8125     (while (and (setq article (gnus-data-number (car data)))
8126                 (or (gnus-data-read-p (car data))
8127                     (not (> (gnus-summary-article-score article)
8128                             gnus-summary-default-score))))
8129       (setq data (cdr data)))
8130     (when article
8131       (gnus-summary-goto-subject article))
8132     (gnus-summary-position-point)
8133     article))
8134
8135 (defun gnus-summary-last-subject ()
8136   "Go to the last displayed subject line in the group."
8137   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8138     (when article
8139       (gnus-summary-goto-subject article))))
8140
8141 (defun gnus-summary-goto-article (article &optional all-headers force)
8142   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8143 If ALL-HEADERS is non-nil, no header lines are hidden.
8144 If FORCE, go to the article even if it isn't displayed.  If FORCE
8145 is a number, it is the line the article is to be displayed on."
8146   (interactive
8147    (list
8148     (gnus-completing-read
8149      "Article number or Message-ID"
8150      (mapcar 'int-to-string gnus-newsgroup-limit))
8151     current-prefix-arg
8152     t))
8153   (prog1
8154       (if (and (stringp article)
8155                (string-match "@\\|%40" article))
8156           (gnus-summary-refer-article article)
8157         (when (stringp article)
8158           (setq article (string-to-number article)))
8159         (if (gnus-summary-goto-subject article force)
8160             (gnus-summary-display-article article all-headers)
8161           (gnus-message 4 "Couldn't go to article %s" article) nil))
8162     (gnus-summary-position-point)))
8163
8164 (defun gnus-summary-goto-last-article ()
8165   "Go to the previously read article."
8166   (interactive)
8167   (prog1
8168       (when gnus-last-article
8169         (gnus-summary-goto-article gnus-last-article nil t))
8170     (gnus-summary-position-point)))
8171
8172 (defun gnus-summary-pop-article (number)
8173   "Pop one article off the history and go to the previous.
8174 NUMBER articles will be popped off."
8175   (interactive "p")
8176   (let (to)
8177     (setq gnus-newsgroup-history
8178           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8179     (if to
8180         (gnus-summary-goto-article (car to) nil t)
8181       (error "Article history empty")))
8182   (gnus-summary-position-point))
8183
8184 ;; Summary commands and functions for limiting the summary buffer.
8185
8186 (defun gnus-summary-limit-to-articles (n)
8187   "Limit the summary buffer to the next N articles.
8188 If not given a prefix, use the process marked articles instead."
8189   (interactive "P")
8190   (prog1
8191       (let ((articles (gnus-summary-work-articles n)))
8192         (setq gnus-newsgroup-processable nil)
8193         (gnus-summary-limit articles))
8194     (gnus-summary-position-point)))
8195
8196 (defun gnus-summary-pop-limit (&optional total)
8197   "Restore the previous limit.
8198 If given a prefix, remove all limits."
8199   (interactive "P")
8200   (when total
8201     (setq gnus-newsgroup-limits
8202           (list (mapcar (lambda (h) (mail-header-number h))
8203                         gnus-newsgroup-headers))))
8204   (unless gnus-newsgroup-limits
8205     (error "No limit to pop"))
8206   (prog1
8207       (gnus-summary-limit nil 'pop)
8208     (gnus-summary-position-point)))
8209
8210 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8211   "Limit the summary buffer to articles that have subjects that match a regexp.
8212 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8213   (interactive
8214    (list (read-string (if current-prefix-arg
8215                           "Exclude subject (regexp): "
8216                         "Limit to subject (regexp): "))
8217          nil current-prefix-arg))
8218   (unless header
8219     (setq header "subject"))
8220   (when (not (equal "" subject))
8221     (prog1
8222         (let ((articles (gnus-summary-find-matching
8223                          (or header "subject") subject 'all nil nil
8224                          not-matching)))
8225           (unless articles
8226             (error "Found no matches for \"%s\"" subject))
8227           (gnus-summary-limit articles))
8228       (gnus-summary-position-point))))
8229
8230 (defun gnus-summary-limit-to-author (from &optional not-matching)
8231   "Limit the summary buffer to articles that have authors that match a regexp.
8232 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8233   (interactive
8234    (list (read-string (if current-prefix-arg
8235                           "Exclude author (regexp): "
8236                         "Limit to author (regexp): "))
8237          current-prefix-arg))
8238   (gnus-summary-limit-to-subject from "from" not-matching))
8239
8240 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8241   "Limit the summary buffer to articles with the given RECIPIENT.
8242
8243 If NOT-MATCHING, exclude RECIPIENT.
8244
8245 To and Cc headers are checked.  You need to include them in
8246 `nnmail-extra-headers'."
8247   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8248   (interactive
8249    (list (read-string (format "%s recipient (regexp): "
8250                               (if current-prefix-arg "Exclude" "Limit to")))
8251          current-prefix-arg))
8252   (when (not (equal "" recipient))
8253     (prog1 (let* ((to
8254                    (if (memq 'To nnmail-extra-headers)
8255                        (gnus-summary-find-matching
8256                         (cons 'extra 'To) recipient 'all nil nil
8257                         not-matching)
8258                      (gnus-message
8259                       1 "`To' isn't present in `nnmail-extra-headers'")
8260                      (sit-for 1)
8261                      nil))
8262                   (cc
8263                    (if (memq 'Cc nnmail-extra-headers)
8264                        (gnus-summary-find-matching
8265                         (cons 'extra 'Cc) recipient 'all nil nil
8266                         not-matching)
8267                      (gnus-message
8268                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8269                      (sit-for 1)
8270                      nil))
8271                   (articles
8272                    (if not-matching
8273                        ;; We need the numbers that are in both lists:
8274                        (mapcar (lambda (a)
8275                                  (and (memq a to) a))
8276                                cc)
8277                      (nconc to cc))))
8278              (unless articles
8279                (error "Found no matches for \"%s\"" recipient))
8280              (gnus-summary-limit articles))
8281       (gnus-summary-position-point))))
8282
8283 (defun gnus-summary-limit-to-address (address &optional not-matching)
8284   "Limit the summary buffer to articles with the given ADDRESS.
8285
8286 If NOT-MATCHING, exclude ADDRESS.
8287
8288 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8289 in `nnmail-extra-headers'."
8290   (interactive
8291    (list (read-string (format "%s address (regexp): "
8292                               (if current-prefix-arg "Exclude" "Limit to")))
8293          current-prefix-arg))
8294   (when (not (equal "" address))
8295     (prog1 (let* ((to
8296                    (if (memq 'To nnmail-extra-headers)
8297                        (gnus-summary-find-matching
8298                         (cons 'extra 'To) address 'all nil nil
8299                         not-matching)
8300                      (gnus-message
8301                       1 "`To' isn't present in `nnmail-extra-headers'")
8302                      (sit-for 1)
8303                      t))
8304                   (cc
8305                    (if (memq 'Cc nnmail-extra-headers)
8306                        (gnus-summary-find-matching
8307                         (cons 'extra 'Cc) address 'all nil nil
8308                         not-matching)
8309                      (gnus-message
8310                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8311                      (sit-for 1)
8312                      t))
8313                   (from
8314                    (gnus-summary-find-matching "from" address
8315                                                'all nil nil not-matching))
8316                   (articles
8317                    (if not-matching
8318                        ;; We need the numbers that are in all lists:
8319                        (if (eq cc t)
8320                            (if (eq to t)
8321                                from
8322                              (mapcar (lambda (a) (car (memq a from))) to))
8323                          (if (eq to t)
8324                              (mapcar (lambda (a) (car (memq a from))) cc)
8325                            (mapcar (lambda (a) (car (memq a from)))
8326                                    (mapcar (lambda (a) (car (memq a to)))
8327                                            cc))))
8328                      (nconc (if (eq to t) nil to)
8329                             (if (eq cc t) nil cc)
8330                             from))))
8331              (unless articles
8332                (error "Found no matches for \"%s\"" address))
8333              (gnus-summary-limit articles))
8334       (gnus-summary-position-point))))
8335
8336 (defun gnus-summary-limit-strange-charsets-predicate (header)
8337   (when (fboundp 'char-charset)
8338     (let ((string (concat (mail-header-subject header)
8339                           (mail-header-from header)))
8340           charset found)
8341       (dotimes (i (1- (length string)))
8342         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8343         (when (string-match "unicode\\|big\\|japanese" charset)
8344           (setq found t)))
8345       found)))
8346
8347 (defun gnus-summary-limit-to-predicate (predicate)
8348   "Limit to articles where PREDICATE returns non-nil.
8349 PREDICATE will be called with the header structures of the
8350 articles."
8351   (let ((articles nil)
8352         (case-fold-search t))
8353     (dolist (header gnus-newsgroup-headers)
8354       (when (funcall predicate header)
8355         (push (mail-header-number header) articles)))
8356     (gnus-summary-limit (nreverse articles))))
8357
8358 (defun gnus-summary-limit-to-age (age &optional younger-p)
8359   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8360 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8361 articles that are younger than AGE days."
8362   (interactive
8363    (let ((younger current-prefix-arg)
8364          (days-got nil)
8365          days)
8366      (while (not days-got)
8367        (setq days (if younger
8368                       (read-string "Limit to articles younger than (in days, older when negative): ")
8369                     (read-string
8370                      "Limit to articles older than (in days, younger when negative): ")))
8371        (when (> (length days) 0)
8372          (setq days (read days)))
8373        (if (numberp days)
8374            (progn
8375              (setq days-got t)
8376              (when (< days 0)
8377                (setq younger (not younger))
8378                (setq days (* days -1))))
8379          (message "Please enter a number.")
8380          (sleep-for 1)))
8381      (list days younger)))
8382   (prog1
8383       (let ((data gnus-newsgroup-data)
8384             (cutoff (days-to-time age))
8385             articles d date is-younger)
8386         (while (setq d (pop data))
8387           (when (and (vectorp (gnus-data-header d))
8388                      (setq date (mail-header-date (gnus-data-header d))))
8389             (setq is-younger (time-less-p
8390                               (time-since (gnus-date-get-time date))
8391                               cutoff))
8392             (when (if younger-p
8393                       is-younger
8394                     (not is-younger))
8395               (push (gnus-data-number d) articles))))
8396         (gnus-summary-limit (nreverse articles)))
8397     (gnus-summary-position-point)))
8398
8399 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8400   "Limit the summary buffer to articles that match an 'extra' header."
8401   (interactive
8402    (let ((header
8403           (intern
8404            (gnus-completing-read
8405             (if current-prefix-arg
8406                 "Exclude extra header"
8407               "Limit extra header")
8408             (mapcar 'symbol-name gnus-extra-headers)
8409             t nil nil
8410             (symbol-name (car gnus-extra-headers))))))
8411      (list header
8412            (read-string (format "%s header %s (regexp): "
8413                                 (if current-prefix-arg "Exclude" "Limit to")
8414                                 header))
8415            current-prefix-arg)))
8416   (when (not (equal "" regexp))
8417     (prog1
8418         (let ((articles (gnus-summary-find-matching
8419                          (cons 'extra header) regexp 'all nil nil
8420                          not-matching)))
8421           (unless articles
8422             (error "Found no matches for \"%s\"" regexp))
8423           (gnus-summary-limit articles))
8424       (gnus-summary-position-point))))
8425
8426 (defun gnus-summary-limit-to-display-predicate ()
8427   "Limit the summary buffer to the predicated in the `display' group parameter."
8428   (interactive)
8429   (unless gnus-newsgroup-display
8430     (error "There is no `display' group parameter"))
8431   (let (articles)
8432     (dolist (gnus-number gnus-newsgroup-articles)
8433       (when (funcall gnus-newsgroup-display)
8434         (push gnus-number articles)))
8435     (gnus-summary-limit articles))
8436   (gnus-summary-position-point))
8437
8438 (defun gnus-summary-limit-to-unread (&optional all)
8439   "Limit the summary buffer to articles that are not marked as read.
8440 If ALL is non-nil, limit strictly to unread articles."
8441   (interactive "P")
8442   (if all
8443       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8444     (gnus-summary-limit-to-marks
8445      ;; Concat all the marks that say that an article is read and have
8446      ;; those removed.
8447      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8448            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8449            gnus-low-score-mark gnus-expirable-mark
8450            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8451            gnus-duplicate-mark)
8452      'reverse)))
8453
8454 (defun gnus-summary-limit-to-headers (match &optional reverse)
8455   "Limit the summary buffer to articles that have headers that match MATCH.
8456 If REVERSE (the prefix), limit to articles that don't match."
8457   (interactive "sMatch headers (regexp): \nP")
8458   (gnus-summary-limit-to-bodies match reverse t))
8459
8460 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8461   "Limit the summary buffer to articles that have bodies that match MATCH.
8462 If REVERSE (the prefix), limit to articles that don't match."
8463   (interactive "sMatch body (regexp): \nP")
8464   (let ((articles nil)
8465         (gnus-select-article-hook nil)  ;Disable hook.
8466         (gnus-article-prepare-hook nil)
8467         (gnus-use-article-prefetch nil)
8468         (gnus-keep-backlog nil)
8469         (gnus-break-pages nil)
8470         (gnus-summary-display-arrow nil)
8471         (gnus-updated-mode-lines nil)
8472         (gnus-auto-center-summary nil)
8473         (gnus-display-mime-function nil))
8474     (dolist (data gnus-newsgroup-data)
8475       (let (gnus-mark-article-hook)
8476         (gnus-summary-select-article t t nil (gnus-data-number data)))
8477       (with-current-buffer gnus-article-buffer
8478         (article-goto-body)
8479         (let* ((case-fold-search t)
8480                (found (if headersp
8481                           (re-search-backward match nil t)
8482                         (re-search-forward match nil t))))
8483           (when (or (and found
8484                          (not reverse))
8485                     (and (not found)
8486                          reverse))
8487             (push (gnus-data-number data) articles)))))
8488     (if (not articles)
8489         (message "No messages matched")
8490       (gnus-summary-limit articles)))
8491   (gnus-summary-position-point))
8492
8493 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8494   "Limit the summary buffer to articles that aren't part on any thread.
8495 If THREADSP (the prefix), limit to articles that are in threads."
8496   (interactive "P")
8497   (let ((articles nil)
8498         thread-articles
8499         threads)
8500     (dolist (thread gnus-newsgroup-threads)
8501       (if (stringp (car thread))
8502           (dolist (thread (cdr thread))
8503             (push thread threads))
8504         (push thread threads)))
8505     (dolist (thread threads)
8506       (setq thread-articles (gnus-articles-in-thread thread))
8507       (when (or (and threadsp
8508                      (> (length thread-articles) 1))
8509                 (and (not threadsp)
8510                      (= (length thread-articles) 1)))
8511         (setq articles (nconc thread-articles articles))))
8512     (if (not articles)
8513         (message "No messages matched")
8514       (gnus-summary-limit articles))
8515     (gnus-summary-position-point)))
8516
8517 (defun gnus-summary-limit-to-replied (&optional unreplied)
8518   "Limit the summary buffer to replied articles.
8519 If UNREPLIED (the prefix), limit to unreplied articles."
8520   (interactive "P")
8521   (if unreplied
8522       (gnus-summary-limit
8523        (gnus-set-difference gnus-newsgroup-articles
8524         gnus-newsgroup-replied))
8525     (gnus-summary-limit gnus-newsgroup-replied))
8526   (gnus-summary-position-point))
8527
8528 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8529   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8530 If REVERSE, limit the summary buffer to articles that are marked
8531 with MARKS.  MARKS can either be a string of marks or a list of marks.
8532 Returns how many articles were removed."
8533   (interactive "sMarks: ")
8534   (gnus-summary-limit-to-marks marks t))
8535
8536 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8537   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8538 If REVERSE (the prefix), limit the summary buffer to articles that are
8539 not marked with MARKS.  MARKS can either be a string of marks or a
8540 list of marks.
8541 Returns how many articles were removed."
8542   (interactive "sMarks: \nP")
8543   (prog1
8544       (let ((data gnus-newsgroup-data)
8545             (marks (if (listp marks) marks
8546                      (append marks nil))) ; Transform to list.
8547             articles)
8548         (while data
8549           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8550                   (memq (gnus-data-mark (car data)) marks))
8551             (push (gnus-data-number (car data)) articles))
8552           (setq data (cdr data)))
8553         (gnus-summary-limit articles))
8554     (gnus-summary-position-point)))
8555
8556 (defun gnus-summary-limit-to-score (score)
8557   "Limit to articles with score at or above SCORE."
8558   (interactive "NLimit to articles with score of at least: ")
8559   (let ((data gnus-newsgroup-data)
8560         articles)
8561     (while data
8562       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8563                 score)
8564         (push (gnus-data-number (car data)) articles))
8565       (setq data (cdr data)))
8566     (prog1
8567         (gnus-summary-limit articles)
8568       (gnus-summary-position-point))))
8569
8570 (defun gnus-summary-limit-to-unseen ()
8571   "Limit to unseen articles."
8572   (interactive)
8573   (prog1
8574       (gnus-summary-limit gnus-newsgroup-unseen)
8575     (gnus-summary-position-point)))
8576
8577 (defun gnus-summary-limit-include-thread (id)
8578   "Display all the hidden articles that is in the thread with ID in it.
8579 When called interactively, ID is the Message-ID of the current
8580 article."
8581   (interactive (list (mail-header-id (gnus-summary-article-header))))
8582   (let ((articles (gnus-articles-in-thread
8583                    (gnus-id-to-thread (gnus-root-id id))))
8584         ;;we REALLY want the whole thread---this prevents cut-threads
8585         ;;from removing the thread we want to include.
8586         (gnus-fetch-old-headers nil)
8587         (gnus-build-sparse-threads nil))
8588     (prog1
8589         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8590       (gnus-summary-limit-include-matching-articles
8591        "subject"
8592        (regexp-quote (gnus-simplify-subject-re
8593                       (mail-header-subject (gnus-id-to-header id)))))
8594       (gnus-summary-position-point))))
8595
8596 (defun gnus-summary-limit-include-matching-articles (header regexp)
8597   "Display all the hidden articles that have HEADERs that match REGEXP."
8598   (interactive (list (read-string "Match on header: ")
8599                      (read-string "Regexp: ")))
8600   (let ((articles (gnus-find-matching-articles header regexp)))
8601     (prog1
8602         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8603       (gnus-summary-position-point))))
8604
8605 (defun gnus-summary-insert-dormant-articles ()
8606   "Insert all the dormant articles for this group into the current buffer."
8607   (interactive)
8608   (let ((gnus-verbose (max 6 gnus-verbose)))
8609     (if (not gnus-newsgroup-dormant)
8610         (gnus-message 3 "No dormant articles for this group")
8611       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8612
8613 (defun gnus-summary-insert-ticked-articles ()
8614   "Insert ticked articles for this group into the current buffer."
8615   (interactive)
8616   (let ((gnus-verbose (max 6 gnus-verbose)))
8617     (if (not gnus-newsgroup-marked)
8618         (gnus-message 3 "No ticked articles for this group")
8619       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8620
8621 (defun gnus-summary-limit-include-dormant ()
8622   "Display all the hidden articles that are marked as dormant.
8623 Note that this command only works on a subset of the articles currently
8624 fetched for this group."
8625   (interactive)
8626   (unless gnus-newsgroup-dormant
8627     (error "There are no dormant articles in this group"))
8628   (prog1
8629       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8630     (gnus-summary-position-point)))
8631
8632 (defun gnus-summary-include-articles (articles)
8633   "Fetch the headers for ARTICLES and then display the summary lines."
8634   (let ((gnus-inhibit-demon t)
8635         (gnus-agent nil)
8636         (gnus-read-all-available-headers t))
8637     (setq gnus-newsgroup-headers
8638           (gnus-merge
8639            'list gnus-newsgroup-headers
8640            (gnus-fetch-headers articles nil t)
8641            'gnus-article-sort-by-number))
8642     (setq gnus-newsgroup-articles
8643           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8644     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8645
8646 (defun gnus-summary-limit-exclude-dormant ()
8647   "Hide all dormant articles."
8648   (interactive)
8649   (prog1
8650       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8651     (gnus-summary-position-point)))
8652
8653 (defun gnus-summary-limit-exclude-childless-dormant ()
8654   "Hide all dormant articles that have no children."
8655   (interactive)
8656   (let ((data (gnus-data-list t))
8657         articles d children)
8658     ;; Find all articles that are either not dormant or have
8659     ;; children.
8660     (while (setq d (pop data))
8661       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8662                 (and (setq children
8663                            (gnus-article-children (gnus-data-number d)))
8664                      (let (found)
8665                        (while children
8666                          (when (memq (car children) articles)
8667                            (setq children nil
8668                                  found t))
8669                          (pop children))
8670                        found)))
8671         (push (gnus-data-number d) articles)))
8672     ;; Do the limiting.
8673     (prog1
8674         (gnus-summary-limit articles)
8675       (gnus-summary-position-point))))
8676
8677 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8678   "Mark all unread excluded articles as read.
8679 If ALL, mark even excluded ticked and dormants as read."
8680   (interactive "P")
8681   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8682   (let ((articles (gnus-sorted-ndifference
8683                    (sort
8684                     (mapcar (lambda (h) (mail-header-number h))
8685                             gnus-newsgroup-headers)
8686                     '<)
8687                    gnus-newsgroup-limit))
8688         article)
8689     (setq gnus-newsgroup-unreads
8690           (gnus-sorted-intersection gnus-newsgroup-unreads
8691                                     gnus-newsgroup-limit))
8692     (if all
8693         (setq gnus-newsgroup-dormant nil
8694               gnus-newsgroup-marked nil
8695               gnus-newsgroup-reads
8696               (nconc
8697                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8698                gnus-newsgroup-reads))
8699       (while (setq article (pop articles))
8700         (unless (or (memq article gnus-newsgroup-dormant)
8701                     (memq article gnus-newsgroup-marked))
8702           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8703
8704 (defun gnus-summary-limit (articles &optional pop)
8705   (if pop
8706       ;; We pop the previous limit off the stack and use that.
8707       (setq articles (car gnus-newsgroup-limits)
8708             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8709     ;; We use the new limit, so we push the old limit on the stack.
8710     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8711   ;; Set the limit.
8712   (setq gnus-newsgroup-limit articles)
8713   (let ((total (length gnus-newsgroup-data))
8714         (data (gnus-data-find-list (gnus-summary-article-number)))
8715         (gnus-summary-mark-below nil)   ; Inhibit this.
8716         found)
8717     ;; This will do all the work of generating the new summary buffer
8718     ;; according to the new limit.
8719     (gnus-summary-prepare)
8720     ;; Hide any threads, possibly.
8721     (gnus-summary-maybe-hide-threads)
8722     ;; Try to return to the article you were at, or one in the
8723     ;; neighborhood.
8724     (when data
8725       ;; We try to find some article after the current one.
8726       (while data
8727         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8728           (setq data nil
8729                 found t))
8730         (setq data (cdr data))))
8731     (unless found
8732       ;; If there is no data, that means that we were after the last
8733       ;; article.  The same goes when we can't find any articles
8734       ;; after the current one.
8735       (goto-char (point-max))
8736       (gnus-summary-find-prev))
8737     (gnus-set-mode-line 'summary)
8738     ;; We return how many articles were removed from the summary
8739     ;; buffer as a result of the new limit.
8740     (- total (length gnus-newsgroup-data))))
8741
8742 (defsubst gnus-invisible-cut-children (threads)
8743   (let ((num 0))
8744     (while threads
8745       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8746         (incf num))
8747       (pop threads))
8748     (< num 2)))
8749
8750 (defsubst gnus-cut-thread (thread)
8751   "Go forwards in the thread until we find an article that we want to display."
8752   (when (or (eq gnus-fetch-old-headers 'some)
8753             (eq gnus-fetch-old-headers 'invisible)
8754             (numberp gnus-fetch-old-headers)
8755             (eq gnus-build-sparse-threads 'some)
8756             (eq gnus-build-sparse-threads 'more))
8757     ;; Deal with old-fetched headers and sparse threads.
8758     (while (and
8759             thread
8760             (or
8761              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8762              (gnus-summary-article-ancient-p
8763               (mail-header-number (car thread))))
8764             (if (or (<= (length (cdr thread)) 1)
8765                     (eq gnus-fetch-old-headers 'invisible))
8766                 (setq gnus-newsgroup-limit
8767                       (delq (mail-header-number (car thread))
8768                             gnus-newsgroup-limit)
8769                       thread (cadr thread))
8770               (when (gnus-invisible-cut-children (cdr thread))
8771                 (let ((th (cdr thread)))
8772                   (while th
8773                     (if (memq (mail-header-number (caar th))
8774                               gnus-newsgroup-limit)
8775                         (setq thread (car th)
8776                               th nil)
8777                       (setq th (cdr th))))))))))
8778   thread)
8779
8780 (defun gnus-cut-threads (threads)
8781   "Cut off all uninteresting articles from the beginning of THREADS."
8782   (when (or (eq gnus-fetch-old-headers 'some)
8783             (eq gnus-fetch-old-headers 'invisible)
8784             (numberp gnus-fetch-old-headers)
8785             (eq gnus-build-sparse-threads 'some)
8786             (eq gnus-build-sparse-threads 'more))
8787     (let ((th threads))
8788       (while th
8789         (setcar th (gnus-cut-thread (car th)))
8790         (setq th (cdr th)))))
8791   ;; Remove nixed out threads.
8792   (delq nil threads))
8793
8794 (defun gnus-summary-initial-limit (&optional show-if-empty)
8795   "Figure out what the initial limit is supposed to be on group entry.
8796 This entails weeding out unwanted dormants, low-scored articles,
8797 fetch-old-headers verbiage, and so on."
8798   ;; Most groups have nothing to remove.
8799   (unless (or gnus-inhibit-limiting
8800               (and (null gnus-newsgroup-dormant)
8801                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8802                    (not (eq gnus-fetch-old-headers 'some))
8803                    (not (numberp gnus-fetch-old-headers))
8804                    (not (eq gnus-fetch-old-headers 'invisible))
8805                    (null gnus-summary-expunge-below)
8806                    (not (eq gnus-build-sparse-threads 'some))
8807                    (not (eq gnus-build-sparse-threads 'more))
8808                    (null gnus-thread-expunge-below)))
8809     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8810     (setq gnus-newsgroup-limit nil)
8811     (mapatoms
8812      (lambda (node)
8813        (unless (car (symbol-value node))
8814          ;; These threads have no parents -- they are roots.
8815          (let ((nodes (cdr (symbol-value node)))
8816                thread)
8817            (while nodes
8818              (if (and gnus-thread-expunge-below
8819                       (< (gnus-thread-total-score (car nodes))
8820                          gnus-thread-expunge-below))
8821                  (gnus-expunge-thread (pop nodes))
8822                (setq thread (pop nodes))
8823                (gnus-summary-limit-children thread))))))
8824      gnus-newsgroup-dependencies)
8825     ;; If this limitation resulted in an empty group, we might
8826     ;; pop the previous limit and use it instead.
8827     (when (and (not gnus-newsgroup-limit)
8828                show-if-empty)
8829       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8830     gnus-newsgroup-limit))
8831
8832 (defun gnus-summary-limit-children (thread)
8833   "Return 1 if this subthread is visible and 0 if it is not."
8834   ;; First we get the number of visible children to this thread.  This
8835   ;; is done by recursing down the thread using this function, so this
8836   ;; will really go down to a leaf article first, before slowly
8837   ;; working its way up towards the root.
8838   (when thread
8839     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8840            (children
8841            (if (cdr thread)
8842                (apply '+ (mapcar 'gnus-summary-limit-children
8843                                  (cdr thread)))
8844              0))
8845            (number (mail-header-number (car thread)))
8846            score)
8847       (if (and
8848            (not (memq number gnus-newsgroup-marked))
8849            (or
8850             ;; If this article is dormant and has absolutely no visible
8851             ;; children, then this article isn't visible.
8852             (and (memq number gnus-newsgroup-dormant)
8853                  (zerop children))
8854             ;; If this is "fetch-old-headered" and there is no
8855             ;; visible children, then we don't want this article.
8856             (and (or (eq gnus-fetch-old-headers 'some)
8857                      (numberp gnus-fetch-old-headers))
8858                  (gnus-summary-article-ancient-p number)
8859                  (zerop children))
8860             ;; If this is "fetch-old-headered" and `invisible', then
8861             ;; we don't want this article.
8862             (and (eq gnus-fetch-old-headers 'invisible)
8863                  (gnus-summary-article-ancient-p number))
8864             ;; If this is a sparsely inserted article with no children,
8865             ;; we don't want it.
8866             (and (eq gnus-build-sparse-threads 'some)
8867                  (gnus-summary-article-sparse-p number)
8868                  (zerop children))
8869             ;; If we use expunging, and this article is really
8870             ;; low-scored, then we don't want this article.
8871             (when (and gnus-summary-expunge-below
8872                        (< (setq score
8873                                 (or (cdr (assq number gnus-newsgroup-scored))
8874                                     gnus-summary-default-score))
8875                           gnus-summary-expunge-below))
8876               ;; We increase the expunge-tally here, but that has
8877               ;; nothing to do with the limits, really.
8878               (incf gnus-newsgroup-expunged-tally)
8879               ;; We also mark as read here, if that's wanted.
8880               (when (and gnus-summary-mark-below
8881                          (< score gnus-summary-mark-below))
8882                 (setq gnus-newsgroup-unreads
8883                       (delq number gnus-newsgroup-unreads))
8884                 (if gnus-newsgroup-auto-expire
8885                     (push number gnus-newsgroup-expirable)
8886                   (push (cons number gnus-low-score-mark)
8887                         gnus-newsgroup-reads)))
8888               t)
8889             ;; Do the `display' group parameter.
8890             (and gnus-newsgroup-display
8891                  (let ((gnus-number number))
8892                    (not (funcall gnus-newsgroup-display))))))
8893           ;; Nope, invisible article.
8894           0
8895         ;; Ok, this article is to be visible, so we add it to the limit
8896         ;; and return 1.
8897         (push number gnus-newsgroup-limit)
8898         1))))
8899
8900 (defun gnus-expunge-thread (thread)
8901   "Mark all articles in THREAD as read."
8902   (let* ((number (mail-header-number (car thread))))
8903     (incf gnus-newsgroup-expunged-tally)
8904     ;; We also mark as read here, if that's wanted.
8905     (setq gnus-newsgroup-unreads
8906           (delq number gnus-newsgroup-unreads))
8907     (if gnus-newsgroup-auto-expire
8908         (push number gnus-newsgroup-expirable)
8909       (push (cons number gnus-low-score-mark)
8910             gnus-newsgroup-reads)))
8911   ;; Go recursively through all subthreads.
8912   (mapcar 'gnus-expunge-thread (cdr thread)))
8913
8914 ;; Summary article oriented commands
8915
8916 (defun gnus-summary-refer-parent-article (n)
8917   "Refer parent article N times.
8918 If N is negative, go to ancestor -N instead.
8919 The difference between N and the number of articles fetched is returned."
8920   (interactive "p")
8921   (let ((skip 1)
8922         error header ref)
8923     (when (not (natnump n))
8924       (setq skip (abs n)
8925             n 1))
8926     (while (and (> n 0)
8927                 (not error))
8928       (setq header (gnus-summary-article-header))
8929       (if (and (eq (mail-header-number header)
8930                    (cdr gnus-article-current))
8931                (equal gnus-newsgroup-name
8932                       (car gnus-article-current)))
8933           ;; If we try to find the parent of the currently
8934           ;; displayed article, then we take a look at the actual
8935           ;; References header, since this is slightly more
8936           ;; reliable than the References field we got from the
8937           ;; server.
8938           (with-current-buffer gnus-original-article-buffer
8939             (nnheader-narrow-to-headers)
8940             (unless (setq ref (message-fetch-field "references"))
8941               (when (setq ref (message-fetch-field "in-reply-to"))
8942                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8943             (widen))
8944         (setq ref
8945               ;; It's not the current article, so we take a bet on
8946               ;; the value we got from the server.
8947               (mail-header-references header)))
8948       (if (and ref
8949                (not (equal ref "")))
8950           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8951             (gnus-message 1 "Couldn't find parent"))
8952         (gnus-message 1 "No references in article %d"
8953                       (gnus-summary-article-number))
8954         (setq error t))
8955       (decf n))
8956     (gnus-summary-position-point)
8957     n))
8958
8959 (defun gnus-summary-refer-references ()
8960   "Fetch all articles mentioned in the References header.
8961 Return the number of articles fetched."
8962   (interactive)
8963   (let ((ref (mail-header-references (gnus-summary-article-header)))
8964         (current (gnus-summary-article-number))
8965         (n 0))
8966     (if (or (not ref)
8967             (equal ref ""))
8968         (error "No References in the current article")
8969       ;; For each Message-ID in the References header...
8970       (while (string-match "<[^>]*>" ref)
8971         (incf n)
8972         ;; ... fetch that article.
8973         (gnus-summary-refer-article
8974          (prog1 (match-string 0 ref)
8975            (setq ref (substring ref (match-end 0))))))
8976       (gnus-summary-goto-subject current)
8977       (gnus-summary-position-point)
8978       n)))
8979
8980 (defun gnus-delete-duplicate-headers (headers)
8981   ;; First remove leading duplicates.
8982   (while (and (> (length headers) 1)
8983               (= (mail-header-number (car headers))
8984                  (mail-header-number (cadr headers))))
8985     (pop headers))
8986   ;; Then the rest.
8987   (let ((result headers))
8988     (while (> (length headers) 1)
8989       (if (= (mail-header-number (car headers))
8990              (mail-header-number (cadr headers)))
8991           (setcdr headers (cddr headers))
8992         (pop headers)))
8993     result))
8994
8995 (defun gnus-summary-refer-thread (&optional limit)
8996   "Fetch all articles in the current thread. For backends that
8997 know how to search for threads (currently only 'nnimap) a
8998 non-numeric prefix arg will use nnir to search the entire
8999 server; without a prefix arg only the current group is
9000 searched. If the variable `gnus-refer-thread-use-nnir' is
9001 non-nil the prefix arg has the reverse meaning. If no
9002 backend-specific 'request-thread function is available fetch
9003 LIMIT (the numerical prefix) old headers. If LIMIT is
9004 non-numeric or nil fetch the number specified by the
9005 `gnus-refer-thread-limit' variable."
9006   (interactive "P")
9007   (gnus-warp-to-article)
9008   (let* ((header (gnus-summary-article-header))
9009          (id (mail-header-id header))
9010          (gnus-inhibit-demon t)
9011          (gnus-summary-ignore-duplicates t)
9012          (gnus-read-all-available-headers t)
9013          (gnus-refer-thread-use-nnir
9014           (if (and (not (null limit)) (listp limit))
9015               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9016          (new-headers
9017           (if (gnus-check-backend-function
9018                'request-thread gnus-newsgroup-name)
9019               (gnus-request-thread header gnus-newsgroup-name)
9020             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9021                             gnus-refer-thread-limit))
9022                    (last (if (numberp limit)
9023                              (min (+ (mail-header-number header)
9024                                      limit)
9025                                   gnus-newsgroup-highest)
9026                            gnus-newsgroup-highest))
9027                    (subject (gnus-simplify-subject
9028                              (mail-header-subject header)))
9029                    (refs (split-string (or (mail-header-references header)
9030                                            "")))
9031                    (gnus-parse-headers-hook
9032                     `(lambda () (goto-char (point-min))
9033                       (keep-lines
9034                        (regexp-opt ',(append refs (list id subject)))))))
9035               (gnus-fetch-headers (list last) (if (numberp limit)
9036                                                   (* 2 limit) limit) t))))
9037          article-ids)
9038     (when (listp new-headers)
9039       (dolist (header new-headers)
9040         (push (mail-header-number header) article-ids)
9041         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9042           (push (mail-header-number header) gnus-newsgroup-unreads)
9043           (setq gnus-newsgroup-unselected
9044                 (delete (mail-header-number header)
9045                         gnus-newsgroup-unselected))))
9046       (setq gnus-newsgroup-headers
9047             (gnus-delete-duplicate-headers
9048              (gnus-merge
9049               'list gnus-newsgroup-headers new-headers
9050               'gnus-article-sort-by-number)))
9051       (setq gnus-newsgroup-articles
9052             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9053       (gnus-summary-limit-include-thread id)))
9054   (gnus-summary-show-thread))
9055
9056 (defun gnus-summary-refer-article (message-id)
9057   "Fetch an article specified by MESSAGE-ID."
9058   (interactive "sMessage-ID: ")
9059   (gnus-warp-to-article)
9060   (when (and (stringp message-id)
9061              (not (zerop (length message-id))))
9062     (setq message-id (gnus-replace-in-string message-id " " ""))
9063     ;; Construct the correct Message-ID if necessary.
9064     ;; Suggested by tale@pawl.rpi.edu.
9065     (unless (string-match "^<" message-id)
9066       (setq message-id (concat "<" message-id)))
9067     (unless (string-match ">$" message-id)
9068       (setq message-id (concat message-id ">")))
9069     ;; People often post MIDs from URLs, so unhex it:
9070     (unless (string-match "@" message-id)
9071       (setq message-id (gnus-url-unhex-string message-id)))
9072     (let* ((header (gnus-id-to-header message-id))
9073            (sparse (and header
9074                         (gnus-summary-article-sparse-p
9075                          (mail-header-number header))
9076                         (memq (mail-header-number header)
9077                               gnus-newsgroup-limit)))
9078            number)
9079       (cond
9080        ;; If the article is present in the buffer we just go to it.
9081        ((and header
9082              (or (not (gnus-summary-article-sparse-p
9083                        (mail-header-number header)))
9084                  sparse))
9085         (prog1
9086             (gnus-summary-goto-article
9087              (mail-header-number header) nil t)
9088           (when sparse
9089             (gnus-summary-update-article (mail-header-number header)))))
9090        (t
9091         ;; We fetch the article.
9092         (catch 'found
9093           (dolist (gnus-override-method (gnus-refer-article-methods))
9094             (when (and (gnus-check-server gnus-override-method)
9095                        ;; Fetch the header,
9096                        (setq number (gnus-summary-insert-subject message-id)))
9097               ;; and display the article.
9098               (gnus-summary-select-article nil nil nil number)
9099               (throw 'found t)))
9100           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9101
9102 (defun gnus-refer-article-methods ()
9103   "Return a list of referable methods."
9104   (cond
9105    ;; No method, so we default to current and native.
9106    ((null gnus-refer-article-method)
9107     (list gnus-current-select-method gnus-select-method))
9108    ;; Current.
9109    ((eq 'current gnus-refer-article-method)
9110     (list gnus-current-select-method))
9111    ;; List of select methods.
9112    ((not (and (symbolp (car gnus-refer-article-method))
9113               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9114     (let (out)
9115       (dolist (method gnus-refer-article-method)
9116         (push (if (eq 'current method)
9117                   gnus-current-select-method
9118                 (if (eq 'nnir (car method))
9119                     (list
9120                      'nnir
9121                      (or (cadr method)
9122                          (gnus-method-to-server gnus-current-select-method)))
9123                   method))
9124               out))
9125       (nreverse out)))
9126    ;; One single select method.
9127    (t
9128     (list gnus-refer-article-method))))
9129
9130 (defun gnus-summary-edit-parameters ()
9131   "Edit the group parameters of the current group."
9132   (interactive)
9133   (gnus-group-edit-group gnus-newsgroup-name 'params))
9134
9135 (defun gnus-summary-customize-parameters ()
9136   "Customize the group parameters of the current group."
9137   (interactive)
9138   (gnus-group-customize gnus-newsgroup-name))
9139
9140 (defun gnus-summary-enter-digest-group (&optional force)
9141   "Enter an nndoc group based on the current article.
9142 If FORCE, force a digest interpretation.  If not, try to guess
9143 what the document format is.
9144
9145 To control what happens when you exit the group, see the
9146 `gnus-auto-select-on-ephemeral-exit' variable."
9147   (interactive "P")
9148   (let ((conf gnus-current-window-configuration))
9149     (save-window-excursion
9150       (save-excursion
9151         (let (gnus-article-prepare-hook
9152               gnus-display-mime-function
9153               gnus-break-pages)
9154           (gnus-summary-select-article))))
9155     (setq gnus-current-window-configuration conf)
9156     (let* ((name (format "%s-%d"
9157                          (gnus-group-prefixed-name
9158                           gnus-newsgroup-name (list 'nndoc ""))
9159                          (with-current-buffer gnus-summary-buffer
9160                            gnus-current-article)))
9161            (ogroup gnus-newsgroup-name)
9162            (params (append (gnus-info-params (gnus-get-info ogroup))
9163                            (list (cons 'to-group ogroup))
9164                            (list (cons 'parent-group ogroup))
9165                            (list (cons 'save-article-group ogroup))))
9166            (case-fold-search t)
9167            (buf (current-buffer))
9168            dig to-address)
9169       (with-current-buffer gnus-original-article-buffer
9170         ;; Have the digest group inherit the main mail address of
9171         ;; the parent article.
9172         (when (setq to-address (or (gnus-fetch-field "reply-to")
9173                                    (gnus-fetch-field "from")))
9174           (setq params
9175                 (append
9176                  (list (cons 'to-address
9177                              (funcall gnus-decode-encoded-address-function
9178                                       to-address))))))
9179         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9180         (insert-buffer-substring gnus-original-article-buffer)
9181         ;; Remove lines that may lead nndoc to misinterpret the
9182         ;; document type.
9183         (narrow-to-region
9184          (goto-char (point-min))
9185          (or (search-forward "\n\n" nil t) (point)))
9186         (goto-char (point-min))
9187         (delete-matching-lines "^Path:\\|^From ")
9188         (widen))
9189       (unwind-protect
9190           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9191                     (gnus-newsgroup-ephemeral-ignored-charsets
9192                      gnus-newsgroup-ignored-charsets))
9193                 (gnus-group-read-ephemeral-group
9194                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9195                               (nndoc-article-type
9196                                ,(if force 'mbox 'guess)))
9197                  t nil nil nil
9198                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9199                                                         "ADAPT")))))
9200               ;; Make all postings to this group go to the parent group.
9201               (nconc (gnus-info-params (gnus-get-info name))
9202                      params)
9203             ;; Couldn't select this doc group.
9204             (switch-to-buffer buf)
9205             (gnus-set-global-variables)
9206             (gnus-configure-windows 'summary)
9207             (gnus-message 3 "Article couldn't be entered?"))
9208         (kill-buffer dig)))))
9209
9210 (defun gnus-summary-read-document (n)
9211   "Open a new group based on the current article(s).
9212 This will allow you to read digests and other similar
9213 documents as newsgroups.
9214 Obeys the standard process/prefix convention."
9215   (interactive "P")
9216   (let* ((ogroup gnus-newsgroup-name)
9217          (params (append (gnus-info-params (gnus-get-info ogroup))
9218                          (list (cons 'to-group ogroup))))
9219          group egroup groups vgroup)
9220     (dolist (article (gnus-summary-work-articles n))
9221       (setq group (format "%s-%d" gnus-newsgroup-name article))
9222       (gnus-summary-remove-process-mark article)
9223       (when (gnus-summary-display-article article)
9224         (save-excursion ;;What for?
9225           (with-temp-buffer
9226             (insert-buffer-substring gnus-original-article-buffer)
9227             ;; Remove some headers that may lead nndoc to make
9228             ;; the wrong guess.
9229             (message-narrow-to-head)
9230             (goto-char (point-min))
9231             (delete-matching-lines "^Path:\\|^From ")
9232             (widen)
9233             (if (setq egroup
9234                       (gnus-group-read-ephemeral-group
9235                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9236                                      (nndoc-article-type guess))
9237                        t nil t))
9238                 (progn
9239                   ;; Make all postings to this group go to the parent group.
9240                   (nconc (gnus-info-params (gnus-get-info egroup))
9241                          params)
9242                   (push egroup groups))
9243               ;; Couldn't select this doc group.
9244               (gnus-error 3 "Article couldn't be entered"))))))
9245     ;; Now we have selected all the documents.
9246     (cond
9247      ((not groups)
9248       (error "None of the articles could be interpreted as documents"))
9249      ((gnus-group-read-ephemeral-group
9250        (setq vgroup (format
9251                      "nnvirtual:%s-%s" gnus-newsgroup-name
9252                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9253        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9254        t
9255        (cons (current-buffer) 'summary)))
9256      (t
9257       (error "Couldn't select virtual nndoc group")))))
9258
9259 (defun gnus-summary-widget-forward (arg)
9260   "Move point to the next field or button in the article.
9261 With optional ARG, move across that many fields."
9262   (interactive "p")
9263   (gnus-summary-select-article)
9264   (gnus-configure-windows 'article)
9265   (select-window (gnus-get-buffer-window gnus-article-buffer))
9266   (widget-forward arg))
9267
9268 (defun gnus-summary-isearch-article (&optional regexp-p)
9269   "Do incremental search forward on the current article.
9270 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9271   (interactive "P")
9272   (gnus-summary-select-article)
9273   (gnus-configure-windows 'article)
9274   (gnus-eval-in-buffer-window gnus-article-buffer
9275     (save-restriction
9276       (widen)
9277       (isearch-forward regexp-p))))
9278
9279 (defun gnus-summary-repeat-search-article-forward ()
9280   "Repeat the previous search forwards."
9281   (interactive)
9282   (unless gnus-last-search-regexp
9283     (error "No previous search"))
9284   (gnus-summary-search-article-forward gnus-last-search-regexp))
9285
9286 (defun gnus-summary-repeat-search-article-backward ()
9287   "Repeat the previous search backwards."
9288   (interactive)
9289   (unless gnus-last-search-regexp
9290     (error "No previous search"))
9291   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9292
9293 (defun gnus-summary-search-article-forward (regexp &optional backward)
9294   "Search for an article containing REGEXP forward.
9295 If BACKWARD, search backward instead."
9296   (interactive
9297    (list (read-string
9298           (format "Search article %s (regexp%s): "
9299                   (if current-prefix-arg "backward" "forward")
9300                   (if gnus-last-search-regexp
9301                       (concat ", default " gnus-last-search-regexp)
9302                     "")))
9303          current-prefix-arg))
9304   (if (string-equal regexp "")
9305       (setq regexp (or gnus-last-search-regexp ""))
9306     (setq gnus-last-search-regexp regexp)
9307     (setq gnus-article-before-search gnus-current-article))
9308   ;; Intentionally set gnus-last-article.
9309   (setq gnus-last-article gnus-article-before-search)
9310   (let ((gnus-last-article gnus-last-article))
9311     (if (gnus-summary-search-article regexp backward)
9312         (gnus-summary-show-thread)
9313       (signal 'search-failed (list regexp)))))
9314
9315 (defun gnus-summary-search-article-backward (regexp)
9316   "Search for an article containing REGEXP backward."
9317   (interactive
9318    (list (read-string
9319           (format "Search article backward (regexp%s): "
9320                   (if gnus-last-search-regexp
9321                       (concat ", default " gnus-last-search-regexp)
9322                     "")))))
9323   (gnus-summary-search-article-forward regexp 'backward))
9324
9325 (defun gnus-summary-search-article (regexp &optional backward)
9326   "Search for an article containing REGEXP.
9327 Optional argument BACKWARD means do search for backward.
9328 `gnus-select-article-hook' is not called during the search."
9329   ;; We have to require this here to make sure that the following
9330   ;; dynamic binding isn't shadowed by autoloading.
9331   (require 'gnus-async)
9332   (require 'gnus-art)
9333   (let ((gnus-select-article-hook nil)  ;Disable hook.
9334         (gnus-article-prepare-hook nil)
9335         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9336         (gnus-use-article-prefetch nil)
9337         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9338         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9339         (gnus-visual nil)
9340         (gnus-keep-backlog nil)
9341         (gnus-break-pages nil)
9342         (gnus-summary-display-arrow nil)
9343         (gnus-updated-mode-lines nil)
9344         (gnus-auto-center-summary nil)
9345         (sum (current-buffer))
9346         (gnus-display-mime-function nil)
9347         (found nil)
9348         point)
9349     (gnus-save-hidden-threads
9350       (gnus-summary-select-article)
9351       (set-buffer gnus-article-buffer)
9352       (goto-char (window-point (get-buffer-window (current-buffer))))
9353       (when backward
9354         (forward-line -1))
9355       (while (not found)
9356         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9357         (if (if backward
9358                 (re-search-backward regexp nil t)
9359               (re-search-forward regexp nil t))
9360             ;; We found the regexp.
9361             (progn
9362               (setq found 'found)
9363               (beginning-of-line)
9364               (set-window-start
9365                (get-buffer-window (current-buffer))
9366                (point))
9367               (forward-line 1)
9368               (set-window-point
9369                (get-buffer-window (current-buffer))
9370                (point))
9371               (set-buffer sum)
9372               (setq point (point)))
9373           ;; We didn't find it, so we go to the next article.
9374           (set-buffer sum)
9375           (setq found 'not)
9376           (while (eq found 'not)
9377             (if (not (if backward (gnus-summary-find-prev)
9378                        (gnus-summary-find-next)))
9379                 ;; No more articles.
9380                 (setq found t)
9381               ;; Select the next article and adjust point.
9382               (unless (gnus-summary-article-sparse-p
9383                        (gnus-summary-article-number))
9384                 (setq found nil)
9385                 (gnus-summary-select-article)
9386                 (set-buffer gnus-article-buffer)
9387                 (widen)
9388                 (goto-char (if backward (point-max) (point-min))))))))
9389       (gnus-message 7 ""))
9390     ;; Return whether we found the regexp.
9391     (when (eq found 'found)
9392       (goto-char point)
9393       (gnus-summary-show-thread)
9394       (gnus-summary-goto-subject gnus-current-article)
9395       (gnus-summary-position-point)
9396       t)))
9397
9398 (defun gnus-find-matching-articles (header regexp)
9399   "Return a list of all articles that match REGEXP on HEADER.
9400 This search includes all articles in the current group that Gnus has
9401 fetched headers for, whether they are displayed or not."
9402   (let ((articles nil)
9403         ;; Can't eta-reduce because it's a macro.
9404         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9405         (case-fold-search t))
9406     (dolist (header gnus-newsgroup-headers)
9407       (when (string-match regexp (funcall func header))
9408         (push (mail-header-number header) articles)))
9409     (nreverse articles)))
9410
9411 (defun gnus-summary-find-matching (header regexp &optional backward unread
9412                                           not-case-fold not-matching)
9413   "Return a list of all articles that match REGEXP on HEADER.
9414 The search stars on the current article and goes forwards unless
9415 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9416 If UNREAD is non-nil, only unread articles will
9417 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9418 in the comparisons. If NOT-MATCHING, return a list of all articles that
9419 not match REGEXP on HEADER."
9420   (let ((case-fold-search (not not-case-fold))
9421         articles d func)
9422     (if (consp header)
9423         (if (eq (car header) 'extra)
9424             (setq func
9425                   `(lambda (h)
9426                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9427                          "")))
9428           (error "%s is an invalid header" header))
9429       (unless (fboundp (intern (concat "mail-header-" header)))
9430         (error "%s is not a valid header" header))
9431       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9432     (dolist (d (if (eq backward 'all)
9433                    gnus-newsgroup-data
9434                  (gnus-data-find-list
9435                   (gnus-summary-article-number)
9436                   (gnus-data-list backward))))
9437       (when (and (or (not unread)       ; We want all articles...
9438                      (gnus-data-unread-p d)) ; Or just unreads.
9439                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9440                  (if not-matching
9441                      (not (string-match
9442                            regexp
9443                            (funcall func (gnus-data-header d))))
9444                    (string-match regexp
9445                                  (funcall func (gnus-data-header d)))))
9446         (push (gnus-data-number d) articles))) ; Success!
9447     (nreverse articles)))
9448
9449 (defun gnus-summary-execute-command (header regexp command &optional backward)
9450   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9451 If HEADER is an empty string (or nil), the match is done on the entire
9452 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9453   (interactive
9454    (list (let ((completion-ignore-case t))
9455            (gnus-completing-read
9456             "Header name"
9457             (mapcar 'symbol-name
9458                     (append
9459                      '(Number Subject From Lines Date
9460                        Message-ID Xref References Body)
9461                      gnus-extra-headers))
9462             'require-match))
9463          (read-string "Regexp: ")
9464          (read-key-sequence "Command: ")
9465          current-prefix-arg))
9466   (when (equal header "Body")
9467     (setq header ""))
9468   ;; Hidden thread subtrees must be searched as well.
9469   (gnus-summary-show-all-threads)
9470   ;; We don't want to change current point nor window configuration.
9471   (save-excursion
9472     (save-window-excursion
9473       (let (gnus-visual
9474             gnus-treat-strip-trailing-blank-lines
9475             gnus-treat-strip-leading-blank-lines
9476             gnus-treat-strip-multiple-blank-lines
9477             gnus-treat-hide-boring-headers
9478             gnus-treat-fold-newsgroups
9479             gnus-article-prepare-hook)
9480         (gnus-message 6 "Executing %s..." (key-description command))
9481         ;; We'd like to execute COMMAND interactively so as to give arguments.
9482         (gnus-execute header regexp
9483                       `(call-interactively ',(key-binding command))
9484                       backward)
9485         (gnus-message 6 "Executing %s...done" (key-description command))))))
9486
9487 (defun gnus-summary-beginning-of-article ()
9488   "Scroll the article back to the beginning."
9489   (interactive)
9490   (gnus-summary-select-article)
9491   (gnus-configure-windows 'article)
9492   (gnus-eval-in-buffer-window gnus-article-buffer
9493     (widen)
9494     (goto-char (point-min))
9495     (when gnus-break-pages
9496       (gnus-narrow-to-page))))
9497
9498 (defun gnus-summary-end-of-article ()
9499   "Scroll to the end of the article."
9500   (interactive)
9501   (gnus-summary-select-article)
9502   (gnus-configure-windows 'article)
9503   (gnus-eval-in-buffer-window gnus-article-buffer
9504     (widen)
9505     (goto-char (point-max))
9506     (recenter -3)
9507     (when gnus-break-pages
9508       (gnus-narrow-to-page))))
9509
9510 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9511   "Truncate to LEN and quote all \"(\"'s in STRING."
9512   (gnus-replace-in-string (if (and len (> (length string) len))
9513                               (substring string 0 len)
9514                             string)
9515                           "[()]" "\\\\\\&"))
9516
9517 (defun gnus-summary-print-article (&optional filename n)
9518   "Generate and print a PostScript image of the process-marked (mail) articles.
9519
9520 If used interactively, print the current article if none are
9521 process-marked.  With prefix arg, prompt the user for the name of the
9522 file to save in.
9523
9524 When used from Lisp, accept two optional args FILENAME and N.  N means
9525 to print the next N articles.  If N is negative, print the N previous
9526 articles.  If N is nil and articles have been marked with the process
9527 mark, print these instead.
9528
9529 If the optional first argument FILENAME is nil, send the image to the
9530 printer.  If FILENAME is a string, save the PostScript image in a file with
9531 that name.  If FILENAME is a number, prompt the user for the name of the file
9532 to save in."
9533   (interactive (list (ps-print-preprint current-prefix-arg)))
9534   (dolist (article (gnus-summary-work-articles n))
9535     (gnus-summary-select-article nil nil 'pseudo article)
9536     (gnus-eval-in-buffer-window gnus-article-buffer
9537       (gnus-print-buffer))
9538     (gnus-summary-remove-process-mark article))
9539   (ps-despool filename))
9540
9541 (defun gnus-print-buffer ()
9542   (let ((ps-left-header
9543          (list
9544           (concat "("
9545                   (gnus-summary-print-truncate-and-quote
9546                    (mail-header-subject gnus-current-headers)
9547                    66) ")")
9548           (concat "("
9549                   (gnus-summary-print-truncate-and-quote
9550                    (mail-header-from gnus-current-headers)
9551                    45) ")")))
9552         (ps-right-header
9553          (list
9554           "/pagenumberstring load"
9555           (concat "("
9556                   (mail-header-date gnus-current-headers) ")"))))
9557     (gnus-run-hooks 'gnus-ps-print-hook)
9558     (save-excursion
9559       (if ps-print-color-p
9560           (ps-spool-buffer-with-faces)
9561         (ps-spool-buffer)))))
9562
9563 (defun gnus-summary-show-complete-article ()
9564   "Show a complete version of the current article.
9565 This is only useful if you're looking at a partial version of the
9566 article currently."
9567   (interactive)
9568   (let ((gnus-keep-backlog nil)
9569         (gnus-use-cache nil)
9570         (gnus-agent nil)
9571         (variable (intern
9572                    (format "%s-fetch-partial-articles"
9573                            (car (gnus-find-method-for-group
9574                                  gnus-newsgroup-name)))
9575                    obarray))
9576         old-val)
9577     (unwind-protect
9578         (progn
9579           (setq old-val (symbol-value variable))
9580           (set variable nil)
9581           (gnus-flush-original-article-buffer)
9582           (gnus-summary-show-article))
9583       (set variable old-val))))
9584
9585 (defun gnus-summary-show-article (&optional arg)
9586   "Force redisplaying of the current article.
9587 If ARG (the prefix) is a number, show the article with the charset
9588 defined in `gnus-summary-show-article-charset-alist', or the charset
9589 input.
9590 If ARG (the prefix) is non-nil and not a number, show the article,
9591 but without running any of the article treatment functions
9592 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9593 C-u g', show the raw article."
9594   (interactive "P")
9595   (cond
9596    ((numberp arg)
9597     (gnus-summary-show-article t)
9598     (let ((gnus-newsgroup-charset
9599            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9600                (mm-read-coding-system
9601                 "View as charset: " ;; actually it is coding system.
9602                 (with-current-buffer gnus-article-buffer
9603                   (mm-detect-coding-region (point) (point-max))))))
9604           (gnus-newsgroup-ignored-charsets 'gnus-all))
9605       (gnus-summary-select-article nil 'force)
9606       (let ((deps gnus-newsgroup-dependencies)
9607             head header lines)
9608         (with-current-buffer gnus-original-article-buffer
9609           (save-restriction
9610             (message-narrow-to-head)
9611             (setq head (buffer-string))
9612             (goto-char (point-min))
9613             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9614               (goto-char (point-max))
9615               (widen)
9616               (setq lines (1- (count-lines (point) (point-max))))))
9617           (with-temp-buffer
9618             (insert (format "211 %d Article retrieved.\n"
9619                             (cdr gnus-article-current)))
9620             (insert head)
9621             (if lines (insert (format "Lines: %d\n" lines)))
9622             (insert ".\n")
9623             (let ((nntp-server-buffer (current-buffer)))
9624               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9625         (gnus-data-set-header
9626          (gnus-data-find (cdr gnus-article-current))
9627          header)
9628         (gnus-summary-update-article-line
9629          (cdr gnus-article-current) header)
9630         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9631           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9632    ((not arg)
9633     ;; Select the article the normal way.
9634     (if (eq mm-text-html-renderer 'shr)
9635         (progn
9636           (require 'shr)
9637           (let ((shr-ignore-cache t))
9638             (gnus-summary-select-article nil 'force)))
9639       (gnus-summary-select-article nil 'force)))
9640    ((equal arg '(16))
9641     ;; C-u C-u g
9642     (let ((gnus-inhibit-article-treatments t))
9643       (gnus-summary-select-article nil 'force)))
9644    (t
9645     ;; We have to require this here to make sure that the following
9646     ;; dynamic binding isn't shadowed by autoloading.
9647     (require 'gnus-async)
9648     (require 'gnus-art)
9649     ;; Bind the article treatment functions to nil.
9650     (let ((gnus-have-all-headers t)
9651           gnus-article-prepare-hook
9652           gnus-article-decode-hook
9653           gnus-display-mime-function
9654           gnus-break-pages)
9655       ;; Destroy any MIME parts.
9656       (when (gnus-buffer-live-p gnus-article-buffer)
9657         (with-current-buffer gnus-article-buffer
9658           (gnus-article-stop-animations)
9659           (mm-destroy-parts gnus-article-mime-handles)
9660           ;; Set it to nil for safety reason.
9661           (setq gnus-article-mime-handle-alist nil)
9662           (setq gnus-article-mime-handles nil)))
9663       (gnus-summary-select-article nil 'force))))
9664   (gnus-summary-goto-subject gnus-current-article)
9665   (gnus-summary-position-point))
9666
9667 (defun gnus-summary-show-raw-article ()
9668   "Show the raw article without any article massaging functions being run."
9669   (interactive)
9670   (gnus-summary-show-article t))
9671
9672 (defun gnus-summary-verbose-headers (&optional arg)
9673   "Toggle permanent full header display.
9674 If ARG is a positive number, turn header display on.
9675 If ARG is a negative number, turn header display off."
9676   (interactive "P")
9677   (setq gnus-show-all-headers
9678         (cond ((or (not (numberp arg))
9679                    (zerop arg))
9680                (not gnus-show-all-headers))
9681               ((natnump arg)
9682                t)))
9683   (gnus-summary-show-article))
9684
9685 (defun gnus-summary-toggle-header (&optional arg)
9686   "Show the headers if they are hidden, or hide them if they are shown.
9687 If ARG is a positive number, show the entire header.
9688 If ARG is a negative number, hide the unwanted header lines."
9689   (interactive "P")
9690   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9691                      (get-buffer-window gnus-article-buffer t))))
9692     (with-current-buffer gnus-article-buffer
9693       (widen)
9694       (article-narrow-to-head)
9695       (let* ((inhibit-read-only t)
9696              (inhibit-point-motion-hooks t)
9697              (hidden (if (numberp arg)
9698                          (>= arg 0)
9699                        (or (not (looking-at "[^ \t\n]+:"))
9700                            (gnus-article-hidden-text-p 'headers))))
9701              s e)
9702         (delete-region (point-min) (point-max))
9703         (with-current-buffer gnus-original-article-buffer
9704           (goto-char (setq s (point-min)))
9705           (setq e (if (search-forward "\n\n" nil t)
9706                       (1- (point))
9707                     (point-max))))
9708         (insert-buffer-substring gnus-original-article-buffer s e)
9709         (run-hooks 'gnus-article-decode-hook)
9710         (if hidden
9711             (let ((gnus-treat-hide-headers nil)
9712                   (gnus-treat-hide-boring-headers nil))
9713               (gnus-delete-wash-type 'headers)
9714               (gnus-treat-article 'head))
9715           (gnus-treat-article 'head))
9716         (widen)
9717         (if window
9718             (set-window-start window (goto-char (point-min))))
9719         (if gnus-break-pages
9720             (gnus-narrow-to-page)
9721           (when (gnus-visual-p 'page-marker)
9722             (let ((inhibit-read-only t))
9723               (gnus-remove-text-with-property 'gnus-prev)
9724               (gnus-remove-text-with-property 'gnus-next))))
9725         (gnus-set-mode-line 'article)))))
9726
9727 (defun gnus-summary-show-all-headers ()
9728   "Make all header lines visible."
9729   (interactive)
9730   (gnus-summary-toggle-header 1))
9731
9732 (defun gnus-summary-caesar-message (&optional arg)
9733   "Caesar rotate the current article by 13.
9734 With a non-numerical prefix, also rotate headers.  A numerical
9735 prefix specifies how many places to rotate each letter forward."
9736   (interactive "P")
9737   (gnus-summary-select-article)
9738   (let ((mail-header-separator ""))
9739     (gnus-eval-in-buffer-window gnus-article-buffer
9740       (save-restriction
9741         (widen)
9742         (let ((start (window-start))
9743               (inhibit-read-only t))
9744           (if (equal arg '(4))
9745               (message-caesar-buffer-body nil t)
9746             (message-caesar-buffer-body arg))
9747           (set-window-start (get-buffer-window (current-buffer)) start)))))
9748   ;; Create buttons and stuff...
9749   (gnus-treat-article nil))
9750
9751 (declare-function idna-to-unicode "ext:idna" (str))
9752
9753 (defun gnus-summary-idna-message (&optional arg)
9754   "Decode IDNA encoded domain names in the current articles.
9755 IDNA encoded domain names looks like `xn--bar'.  If a string
9756 remain unencoded after running this function, it is likely an
9757 invalid IDNA string (`xn--bar' is invalid).
9758
9759 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9760 installed for this command to work."
9761   (interactive "P")
9762   (if (not (and (condition-case nil (require 'idna)
9763                   (file-error))
9764                 (mm-coding-system-p 'utf-8)
9765                 (executable-find (symbol-value 'idna-program))))
9766       (gnus-message
9767        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9768     (gnus-summary-select-article)
9769     (let ((mail-header-separator ""))
9770       (gnus-eval-in-buffer-window gnus-article-buffer
9771         (save-restriction
9772           (widen)
9773           (let ((start (window-start))
9774                 buffer-read-only)
9775             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9776               (replace-match (idna-to-unicode (match-string 1))))
9777             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9778
9779 (defun gnus-summary-morse-message (&optional arg)
9780   "Morse decode the current article."
9781   (interactive "P")
9782   (gnus-summary-select-article)
9783   (let ((mail-header-separator ""))
9784     (gnus-eval-in-buffer-window gnus-article-buffer
9785       (save-excursion
9786         (save-restriction
9787           (widen)
9788           (let ((pos (window-start))
9789                 (inhibit-read-only t))
9790             (goto-char (point-min))
9791             (when (message-goto-body)
9792               (gnus-narrow-to-body))
9793             (goto-char (point-min))
9794             (while (search-forward "·" (point-max) t)
9795               (replace-match "."))
9796             (unmorse-region (point-min) (point-max))
9797             (widen)
9798             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9799
9800 (defun gnus-summary-stop-page-breaking ()
9801   "Stop page breaking in the current article."
9802   (interactive)
9803   (gnus-summary-select-article)
9804   (gnus-eval-in-buffer-window gnus-article-buffer
9805     (widen)
9806     (when (gnus-visual-p 'page-marker)
9807       (let ((inhibit-read-only t))
9808         (gnus-remove-text-with-property 'gnus-prev)
9809         (gnus-remove-text-with-property 'gnus-next))
9810       (setq gnus-page-broken nil))))
9811
9812 (defun gnus-summary-move-article (&optional n to-newsgroup
9813                                             select-method action)
9814   "Move the current article to a different newsgroup.
9815 If N is a positive number, move the N next articles.
9816 If N is a negative number, move the N previous articles.
9817 If N is nil and any articles have been marked with the process mark,
9818 move those articles instead.
9819 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9820 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9821 re-spool using this method.
9822
9823 When called interactively with TO-NEWSGROUP being nil, the value of
9824 the variable `gnus-move-split-methods' is used for finding a default
9825 for the target newsgroup.
9826
9827 For this function to work, both the current newsgroup and the
9828 newsgroup that you want to move to have to support the `request-move'
9829 and `request-accept' functions.
9830
9831 ACTION can be either `move' (the default), `crosspost' or `copy'."
9832   (interactive "P")
9833   (unless action
9834     (setq action 'move))
9835   ;; Check whether the source group supports the required functions.
9836   (cond ((and (eq action 'move)
9837               (not (gnus-check-backend-function
9838                     'request-move-article gnus-newsgroup-name)))
9839          (error "The current group does not support article moving"))
9840         ((and (eq action 'crosspost)
9841               (not (gnus-check-backend-function
9842                     'request-replace-article gnus-newsgroup-name)))
9843          (error "The current group does not support article editing")))
9844   (let ((articles (gnus-summary-work-articles n))
9845         (prefix (if (gnus-check-backend-function
9846                      'request-move-article gnus-newsgroup-name)
9847                     (funcall gnus-move-group-prefix-function
9848                              gnus-newsgroup-name)
9849                   ""))
9850         (names '((move "Move" "Moving")
9851                  (copy "Copy" "Copying")
9852                  (crosspost "Crosspost" "Crossposting")))
9853         (copy-buf (save-excursion
9854                     (nnheader-set-temp-buffer " *copy article*")))
9855         art-group to-method new-xref article to-groups
9856         articles-to-update-marks encoded)
9857     (unless (assq action names)
9858       (error "Unknown action %s" action))
9859     ;; Read the newsgroup name.
9860     (when (and (not to-newsgroup)
9861                (not select-method))
9862       (if (and gnus-move-split-methods
9863                (not
9864                 (and (memq gnus-current-article articles)
9865                      (gnus-buffer-live-p gnus-original-article-buffer))))
9866           ;; When `gnus-move-split-methods' is non-nil, we have to
9867           ;; select an article to give `gnus-read-move-group-name' an
9868           ;; opportunity to suggest an appropriate default.  However,
9869           ;; we needn't render or mark the article.
9870           (let ((gnus-display-mime-function nil)
9871                 (gnus-article-prepare-hook nil)
9872                 (gnus-mark-article-hook nil))
9873             (gnus-summary-select-article nil nil nil (car articles))))
9874       (setq to-newsgroup (gnus-read-move-group-name
9875                           (cadr (assq action names))
9876                           (symbol-value
9877                            (intern (format "gnus-current-%s-group" action)))
9878                           articles prefix)
9879             encoded to-newsgroup
9880             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9881       (set (intern (format "gnus-current-%s-group" action))
9882            (mm-decode-coding-string
9883             to-newsgroup
9884             (gnus-group-name-charset to-method to-newsgroup))))
9885     (unless to-method
9886       (setq to-method (or select-method
9887                           (gnus-server-to-method
9888                            (gnus-group-method to-newsgroup)))))
9889     (setq to-newsgroup
9890           (or encoded
9891               (and to-newsgroup
9892                    (mm-encode-coding-string
9893                     to-newsgroup
9894                     (gnus-group-name-charset to-method to-newsgroup)))))
9895     ;; Check the method we are to move this article to...
9896     (unless (gnus-check-backend-function
9897              'request-accept-article (car to-method))
9898       (error "%s does not support article copying" (car to-method)))
9899     (unless (gnus-check-server to-method)
9900       (error "Can't open server %s" (car to-method)))
9901     (gnus-message 6 "%s to %s: %s..."
9902                   (caddr (assq action names))
9903                   (or (car select-method)
9904                       (gnus-group-decoded-name to-newsgroup))
9905                   articles)
9906     (while articles
9907       (setq article (pop articles))
9908       ;; Set any marks that may have changed in the summary buffer.
9909       (when gnus-preserve-marks
9910         (gnus-summary-push-marks-to-backend article))
9911       (setq
9912        art-group
9913        (cond
9914         ;; Move the article.
9915         ((eq action 'move)
9916          ;; Remove this article from future suppression.
9917          (gnus-dup-unsuppress-article article)
9918          (let* ((from-method (gnus-find-method-for-group
9919                               gnus-newsgroup-name))
9920                 (to-method (or select-method
9921                                (gnus-find-method-for-group to-newsgroup)))
9922                 (move-is-internal (gnus-server-equal from-method to-method)))
9923            (gnus-request-move-article
9924             article                     ; Article to move
9925             gnus-newsgroup-name         ; From newsgroup
9926             (nth 1 (gnus-find-method-for-group
9927                     gnus-newsgroup-name)) ; Server
9928             (list 'gnus-request-accept-article
9929                   to-newsgroup (list 'quote select-method)
9930                   (not articles) t)     ; Accept form
9931             (not articles)              ; Only save nov last time
9932             (and move-is-internal
9933                  to-newsgroup           ; Not respooling
9934                                         ; Is this move internal?
9935                  (gnus-group-real-name to-newsgroup)))))
9936         ;; Copy the article.
9937         ((eq action 'copy)
9938          (with-current-buffer copy-buf
9939            (when (gnus-request-article-this-buffer article
9940                                                    gnus-newsgroup-name)
9941              (save-restriction
9942                (nnheader-narrow-to-headers)
9943                (dolist (hdr gnus-copy-article-ignored-headers)
9944                  (message-remove-header hdr t)))
9945              (gnus-request-accept-article
9946               to-newsgroup select-method (not articles) t))))
9947         ;; Crosspost the article.
9948         ((eq action 'crosspost)
9949          (let ((xref (message-tokenize-header
9950                       (mail-header-xref (gnus-summary-article-header
9951                                          article))
9952                       " ")))
9953            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9954                                   ":" (number-to-string article)))
9955            (unless xref
9956              (setq xref (list (system-name))))
9957            (setq new-xref
9958                  (concat
9959                   (mapconcat 'identity
9960                              (delete "Xref:" (delete new-xref xref))
9961                              " ")
9962                   " " new-xref))
9963            (with-current-buffer copy-buf
9964              ;; First put the article in the destination group.
9965              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9966              (when (consp (setq art-group
9967                                 (gnus-request-accept-article
9968                                  to-newsgroup select-method (not articles)
9969                                  t)))
9970                (setq new-xref (concat new-xref " " (car art-group)
9971                                       ":"
9972                                       (number-to-string (cdr art-group))))
9973                ;; Now we have the new Xrefs header, so we insert
9974                ;; it and replace the new article.
9975                (nnheader-replace-header "Xref" new-xref)
9976                (gnus-request-replace-article
9977                 (cdr art-group) to-newsgroup (current-buffer) t)
9978                art-group))))))
9979       (cond
9980        ((not art-group)
9981         (gnus-message 1 "Couldn't %s article %s: %s"
9982                       (cadr (assq action names)) article
9983                       (nnheader-get-report (car to-method))))
9984        ((eq art-group 'junk)
9985         (when (eq action 'move)
9986           (gnus-summary-mark-article article gnus-canceled-mark)
9987           (gnus-message 4 "Deleted article %s" article)
9988           ;; run the delete hook
9989           (run-hook-with-args 'gnus-summary-article-delete-hook
9990                               action
9991                               (gnus-data-header
9992                                (assoc article (gnus-data-list nil)))
9993                               gnus-newsgroup-name nil
9994                               select-method)))
9995        (t
9996         (let* ((pto-group (gnus-group-prefixed-name
9997                            (car art-group) to-method))
9998                (info (gnus-get-info pto-group))
9999                (to-group (gnus-info-group info))
10000                to-marks)
10001           ;; Update the group that has been moved to.
10002           (when (and info
10003                      (memq action '(move copy)))
10004             (unless (member to-group to-groups)
10005               (push to-group to-groups))
10006
10007             (when (and (not (memq article gnus-newsgroup-unreads))
10008                        (cdr art-group))
10009               (push 'read to-marks)
10010               (gnus-info-set-read
10011                info (gnus-add-to-range (gnus-info-read info)
10012                                        (list (cdr art-group)))))
10013
10014             ;; See whether the article is to be put in the cache.
10015             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10016                    (marks (if expirable
10017                               gnus-article-mark-lists
10018                             (delete '(expirable . expire)
10019                                     (copy-sequence
10020                                      gnus-article-mark-lists))))
10021                    (to-article (cdr art-group)))
10022
10023               ;; Enter the article into the cache in the new group,
10024               ;; if that is required.
10025               (when (and to-article
10026                          gnus-use-cache)
10027                 (gnus-cache-possibly-enter-article
10028                  to-group to-article
10029                  (memq article gnus-newsgroup-marked)
10030                  (memq article gnus-newsgroup-dormant)
10031                  (memq article gnus-newsgroup-unreads)))
10032
10033               (when (and gnus-preserve-marks
10034                          to-article)
10035                 ;; Copy any marks over to the new group.
10036                 (when (and (equal to-group gnus-newsgroup-name)
10037                            (not (memq article gnus-newsgroup-unreads)))
10038                   ;; Mark this article as read in this group.
10039                   (push (cons to-article gnus-read-mark)
10040                         gnus-newsgroup-reads)
10041                   ;; Increase the active status of this group.
10042                   (setcdr (gnus-active to-group) to-article)
10043                   (setcdr gnus-newsgroup-active to-article))
10044
10045                 (while marks
10046                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10047                     (when (memq article (symbol-value
10048                                          (intern (format "gnus-newsgroup-%s"
10049                                                          (caar marks)))))
10050                       (push (cdar marks) to-marks)
10051                       ;; If the other group is the same as this group,
10052                       ;; then we have to add the mark to the list.
10053                       (when (equal to-group gnus-newsgroup-name)
10054                         (set (intern (format "gnus-newsgroup-%s"
10055                                              (caar marks)))
10056                              (cons to-article
10057                                    (symbol-value
10058                                     (intern (format "gnus-newsgroup-%s"
10059                                                     (caar marks)))))))
10060                       ;; Copy the marks to other group.
10061                       (gnus-add-marked-articles
10062                        to-group (cdar marks) (list to-article) info)))
10063                   (setq marks (cdr marks)))
10064
10065                 (when (and expirable
10066                            gnus-mark-copied-or-moved-articles-as-expirable
10067                            (not (memq 'expire to-marks)))
10068                   ;; Mark this article as expirable.
10069                   (push 'expire to-marks)
10070                   (when (equal to-group gnus-newsgroup-name)
10071                     (push to-article gnus-newsgroup-expirable))
10072                   ;; Copy the expirable mark to other group.
10073                   (gnus-add-marked-articles
10074                    to-group 'expire (list to-article) info))
10075
10076                 (when (and to-marks
10077                            (or gnus-propagate-marks
10078                                (gnus-method-option-p
10079                                 (gnus-find-method-for-group to-group)
10080                                 'server-marks)))
10081                   (gnus-request-set-mark
10082                    to-group (list (list (list to-article) 'add to-marks)))))
10083
10084               (gnus-dribble-enter
10085                (concat "(gnus-group-set-info '"
10086                        (gnus-prin1-to-string (gnus-get-info to-group))
10087                        ")")
10088                (concat "^(gnus-group-set-info '(\""
10089                        (regexp-quote to-group) "\""))))
10090
10091           ;; Update the Xref header in this article to point to
10092           ;; the new crossposted article we have just created.
10093           (when (eq action 'crosspost)
10094             (with-current-buffer copy-buf
10095               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10096               (nnheader-replace-header "Xref" new-xref)
10097               (gnus-request-replace-article
10098                article gnus-newsgroup-name (current-buffer) t)))
10099
10100           ;; run the move/copy/crosspost/respool hook
10101           (run-hook-with-args 'gnus-summary-article-move-hook
10102                               action
10103                               (gnus-data-header
10104                                (assoc article (gnus-data-list nil)))
10105                               gnus-newsgroup-name
10106                               to-newsgroup
10107                               select-method))
10108
10109         ;;;!!!Why is this necessary?
10110         (set-buffer gnus-summary-buffer)
10111
10112         (when (eq action 'move)
10113           (save-excursion
10114             (gnus-summary-goto-subject article)
10115             (gnus-summary-mark-article article gnus-canceled-mark)))))
10116       (push article articles-to-update-marks))
10117
10118     (save-excursion
10119       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10120     ;; Re-activate all groups that have been moved to.
10121     (with-current-buffer gnus-group-buffer
10122       (let ((gnus-group-marked to-groups))
10123         (gnus-group-get-new-news-this-group nil t)))
10124
10125     (gnus-kill-buffer copy-buf)
10126     (gnus-summary-position-point)
10127     (gnus-set-mode-line 'summary)))
10128
10129 (defun gnus-summary-push-marks-to-backend (article)
10130   (let ((set nil)
10131         (marks gnus-article-mark-lists))
10132     (unless (memq article gnus-newsgroup-unreads)
10133       (push 'read set))
10134     (while marks
10135       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10136                  (memq article (symbol-value
10137                                 (intern (format "gnus-newsgroup-%s"
10138                                                 (caar marks))))))
10139         (push (cdar marks) set))
10140       (pop marks))
10141     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10142
10143 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10144   "Copy the current article to some other group.
10145 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10146 When called interactively, if TO-NEWSGROUP is nil, use the value of
10147 the variable `gnus-move-split-methods' for finding a default target
10148 newsgroup.
10149 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10150 re-spool using this method."
10151   (interactive "P")
10152   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10153
10154 (defun gnus-summary-crosspost-article (&optional n)
10155   "Crosspost the current article to some other group."
10156   (interactive "P")
10157   (gnus-summary-move-article n nil nil 'crosspost))
10158
10159 (defcustom gnus-summary-respool-default-method nil
10160   "Default method type for respooling an article.
10161 If nil, use to the current newsgroup method."
10162   :type 'symbol
10163   :group 'gnus-summary-mail)
10164
10165 (defun gnus-summary-respool-article (&optional n method)
10166   "Respool the current article.
10167 The article will be squeezed through the mail spooling process again,
10168 which means that it will be put in some mail newsgroup or other
10169 depending on `nnmail-split-methods'.
10170 If N is a positive number, respool the N next articles.
10171 If N is a negative number, respool the N previous articles.
10172 If N is nil and any articles have been marked with the process mark,
10173 respool those articles instead.
10174
10175 Respooling can be done both from mail groups and \"real\" newsgroups.
10176 In the former case, the articles in question will be moved from the
10177 current group into whatever groups they are destined to.  In the
10178 latter case, they will be copied into the relevant groups."
10179   (interactive
10180    (list current-prefix-arg
10181          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10182                 (methname
10183                  (symbol-name (or gnus-summary-respool-default-method
10184                                   (car (gnus-find-method-for-group
10185                                         gnus-newsgroup-name)))))
10186                 (method
10187                  (gnus-completing-read
10188                   "Backend to use when respooling"
10189                   methods t nil 'gnus-mail-method-history methname))
10190                 ms)
10191            (cond
10192             ((zerop (length (setq ms (gnus-servers-using-backend
10193                                       (intern method)))))
10194              (list (intern method) ""))
10195             ((= 1 (length ms))
10196              (car ms))
10197             (t
10198              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10199                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10200                            ms-alist))))))))
10201   (unless method
10202     (error "No method given for respooling"))
10203   (if (assoc (symbol-name
10204               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10205              (gnus-methods-using 'respool))
10206       (gnus-summary-move-article n nil method)
10207     (gnus-summary-copy-article n nil method)))
10208
10209 (defun gnus-summary-import-article (file &optional edit)
10210   "Import an arbitrary file into a mail newsgroup."
10211   (interactive "fImport file: \nP")
10212   (let ((group gnus-newsgroup-name)
10213         (now (current-time))
10214         atts lines group-art)
10215     (unless (gnus-check-backend-function 'request-accept-article group)
10216       (error "%s does not support article importing" group))
10217     (or (file-readable-p file)
10218         (not (file-regular-p file))
10219         (error "Can't read %s" file))
10220     (with-current-buffer (gnus-get-buffer-create " *import file*")
10221       (erase-buffer)
10222       (nnheader-insert-file-contents file)
10223       (goto-char (point-min))
10224       (if (nnheader-article-p)
10225           (save-restriction
10226             (goto-char (point-min))
10227             (search-forward "\n\n" nil t)
10228             (narrow-to-region (point-min) (1- (point)))
10229             (goto-char (point-min))
10230             (unless (re-search-forward "^date:" nil t)
10231               (goto-char (point-max))
10232               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10233        ;; This doesn't look like an article, so we fudge some headers.
10234         (setq atts (file-attributes file)
10235               lines (count-lines (point-min) (point-max)))
10236         (insert "From: " (read-string "From: ") "\n"
10237                 "Subject: " (read-string "Subject: ") "\n"
10238                 "Date: " (message-make-date (nth 5 atts)) "\n"
10239                 "Message-ID: " (message-make-message-id) "\n"
10240                 "Lines: " (int-to-string lines) "\n"
10241                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10242       (setq group-art (gnus-request-accept-article group nil t))
10243       (kill-buffer (current-buffer)))
10244     (setq gnus-newsgroup-active (gnus-activate-group group))
10245     (forward-line 1)
10246     (gnus-summary-goto-article (cdr group-art) nil t)
10247     (when edit
10248       (gnus-summary-edit-article))))
10249
10250 (defun gnus-summary-create-article ()
10251   "Create an article in a mail newsgroup."
10252   (interactive)
10253   (let ((group gnus-newsgroup-name)
10254         (now (current-time))
10255         group-art)
10256     (unless (gnus-check-backend-function 'request-accept-article group)
10257       (error "%s does not support article importing" group))
10258     (with-current-buffer (gnus-get-buffer-create " *import file*")
10259       (erase-buffer)
10260       (goto-char (point-min))
10261       ;; This doesn't look like an article, so we fudge some headers.
10262       (insert "From: " (read-string "From: ") "\n"
10263               "Subject: " (read-string "Subject: ") "\n"
10264               "Date: " (message-make-date now) "\n"
10265               "Message-ID: " (message-make-message-id) "\n")
10266       (setq group-art (gnus-request-accept-article group nil t))
10267       (kill-buffer (current-buffer)))
10268     (setq gnus-newsgroup-active (gnus-activate-group group))
10269     (forward-line 1)
10270     (gnus-summary-goto-article (cdr group-art) nil t)
10271     (gnus-summary-edit-article)))
10272
10273 (defun gnus-summary-article-posted-p ()
10274   "Say whether the current (mail) article is available from news as well.
10275 This will be the case if the article has both been mailed and posted."
10276   (interactive)
10277   (let ((id (mail-header-references (gnus-summary-article-header)))
10278         (gnus-override-method (car (gnus-refer-article-methods))))
10279     (if (gnus-request-head id "")
10280         (gnus-message 2 "The current message was found on %s"
10281                       gnus-override-method)
10282       (gnus-message 2 "The current message couldn't be found on %s"
10283                     gnus-override-method)
10284       nil)))
10285
10286 (defun gnus-summary-expire-articles (&optional now)
10287   "Expire all articles that are marked as expirable in the current group."
10288   (interactive)
10289   (when (and (not gnus-group-is-exiting-without-update-p)
10290              (gnus-check-backend-function
10291               'request-expire-articles gnus-newsgroup-name))
10292     ;; This backend supports expiry.
10293     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10294            (expirable (if total
10295                           (progn
10296                             ;; We need to update the info for
10297                             ;; this group for `gnus-list-of-read-articles'
10298                             ;; to give us the right answer.
10299                             (gnus-run-hooks 'gnus-exit-group-hook)
10300                             (gnus-summary-update-info)
10301                             (gnus-list-of-read-articles gnus-newsgroup-name))
10302                         (setq gnus-newsgroup-expirable
10303                               (sort gnus-newsgroup-expirable '<))))
10304            (expiry-wait (if now 'immediate
10305                           (gnus-group-find-parameter
10306                            gnus-newsgroup-name 'expiry-wait)))
10307            (nnmail-expiry-target
10308             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10309                 nnmail-expiry-target))
10310            es)
10311       (when expirable
10312         ;; There are expirable articles in this group, so we run them
10313         ;; through the expiry process.
10314         (gnus-message 6 "Expiring articles...")
10315         (when (gnus-check-group gnus-newsgroup-name)
10316           ;; The list of articles that weren't expired is returned.
10317           (save-excursion
10318             (if expiry-wait
10319                 (let ((nnmail-expiry-wait-function nil)
10320                       (nnmail-expiry-wait expiry-wait))
10321                   (setq es (gnus-request-expire-articles
10322                             expirable gnus-newsgroup-name)))
10323               (setq es (gnus-request-expire-articles
10324                         expirable gnus-newsgroup-name)))
10325             (unless total
10326               (setq gnus-newsgroup-expirable es))
10327             ;; We go through the old list of expirable, and mark all
10328             ;; really expired articles as nonexistent.
10329             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10330               (let ((gnus-use-cache nil))
10331                 (dolist (article expirable)
10332                   (when (and (not (memq article es))
10333                              (gnus-data-find article))
10334                     (gnus-summary-mark-article article gnus-canceled-mark)
10335                     (run-hook-with-args 'gnus-summary-article-expire-hook
10336                                         'delete
10337                                         (gnus-data-header
10338                                          (assoc article (gnus-data-list nil)))
10339                                         gnus-newsgroup-name
10340                                         nil
10341                                         nil)))))))
10342         (gnus-message 6 "Expiring articles...done")))))
10343
10344 (defun gnus-summary-expire-articles-now ()
10345   "Expunge all expirable articles in the current group.
10346 This means that *all* articles that are marked as expirable will be
10347 deleted forever, right now."
10348   (interactive)
10349   (or gnus-expert-user
10350       (gnus-yes-or-no-p
10351        "Are you really, really sure you want to delete all expirable messages? ")
10352       (error "Phew!"))
10353   (gnus-summary-expire-articles t))
10354
10355 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10356 (defun gnus-summary-delete-article (&optional n)
10357   "Delete the N next (mail) articles.
10358 This command actually deletes articles.  This is not a marking
10359 command.  The article will disappear forever from your life, never to
10360 return.
10361
10362 If N is negative, delete backwards.
10363 If N is nil and articles have been marked with the process mark,
10364 delete these instead.
10365
10366 If `gnus-novice-user' is non-nil you will be asked for
10367 confirmation before the articles are deleted."
10368   (interactive "P")
10369   (unless (gnus-check-backend-function 'request-expire-articles
10370                                        gnus-newsgroup-name)
10371     (error "The current newsgroup does not support article deletion"))
10372   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10373     (error "Couldn't open server"))
10374   ;; Compute the list of articles to delete.
10375   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10376         (nnmail-expiry-target 'delete)
10377         not-deleted)
10378     (if (and gnus-novice-user
10379              (not (gnus-yes-or-no-p
10380                    (format "Do you really want to delete %s forever? "
10381                            (if (> (length articles) 1)
10382                                (format "these %s articles" (length articles))
10383                              "this article")))))
10384         ()
10385       ;; Delete the articles.
10386       (setq not-deleted (gnus-request-expire-articles
10387                          articles gnus-newsgroup-name 'force))
10388       (save-excursion
10389         (while articles
10390           (gnus-summary-remove-process-mark (car articles))
10391           ;; The backend might not have been able to delete the article
10392           ;; after all.
10393           (unless (memq (car articles) not-deleted)
10394             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10395             (let* ((article (car articles))
10396                    (ghead  (gnus-data-header
10397                             (assoc article (gnus-data-list nil)))))
10398               (run-hook-with-args 'gnus-summary-article-delete-hook
10399                                   'delete ghead gnus-newsgroup-name nil
10400                                   nil)))
10401           (setq articles (cdr articles))))
10402       (when not-deleted
10403         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10404     (gnus-summary-position-point)
10405     (gnus-set-mode-line 'summary)
10406     not-deleted))
10407
10408 (defun gnus-summary-edit-article (&optional arg)
10409   "Edit the current article.
10410 This will have permanent effect only in mail groups.
10411 If ARG is nil, edit the decoded articles.
10412 If ARG is 1, edit the raw articles.
10413 If ARG is 2, edit the raw articles even in read-only groups.
10414 If ARG is 3, edit the articles with the current handles.
10415 Otherwise, allow editing of articles even in read-only
10416 groups."
10417   (interactive "P")
10418   (let (force raw current-handles)
10419     (cond
10420      ((null arg))
10421      ((eq arg 1)
10422       (setq raw t))
10423      ((eq arg 2)
10424       (setq raw t
10425             force t))
10426      ((eq arg 3)
10427       (setq current-handles
10428             (and (gnus-buffer-live-p gnus-article-buffer)
10429                  (with-current-buffer gnus-article-buffer
10430                    (prog1
10431                        gnus-article-mime-handles
10432                      (setq gnus-article-mime-handles nil))))))
10433      (t
10434       (setq force t)))
10435     (when (and raw (not force)
10436                (member gnus-newsgroup-name '("nndraft:delayed"
10437                                              "nndraft:drafts"
10438                                              "nndraft:queue")))
10439       (error "Can't edit the raw article in group %s"
10440              gnus-newsgroup-name))
10441     (with-current-buffer gnus-summary-buffer
10442       (let ((mail-parse-charset gnus-newsgroup-charset)
10443             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10444         (gnus-set-global-variables)
10445         (when (and (not force)
10446                    (gnus-group-read-only-p))
10447           (error "The current newsgroup does not support article editing"))
10448         (gnus-summary-show-article t)
10449         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10450           (with-current-buffer gnus-article-buffer
10451             (mm-enable-multibyte)))
10452         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10453             (setq raw t))
10454         (gnus-article-edit-article
10455          (if raw 'ignore
10456            `(lambda ()
10457               (let ((mbl mml-buffer-list))
10458                 (setq mml-buffer-list nil)
10459                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10460                   (mime-to-mml ,'current-handles))
10461                 (let ((mbl1 mml-buffer-list))
10462                   (setq mml-buffer-list mbl)
10463                   (set (make-local-variable 'mml-buffer-list) mbl1))
10464                 (gnus-make-local-hook 'kill-buffer-hook)
10465                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10466          `(lambda (no-highlight)
10467             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10468                   (message-options message-options)
10469                   (message-options-set-recipient)
10470                   (mail-parse-ignored-charsets
10471                    ',gnus-newsgroup-ignored-charsets)
10472                   (rfc2047-header-encoding-alist
10473                    ',(let ((charset (gnus-group-name-charset
10474                                      (gnus-find-method-for-group
10475                                       gnus-newsgroup-name)
10476                                      gnus-newsgroup-name)))
10477                        (append (list (cons "Newsgroups" charset)
10478                                      (cons "Followup-To" charset)
10479                                      (cons "Xref" charset))
10480                                rfc2047-header-encoding-alist))))
10481               ,(if (not raw) '(progn
10482                                 (mml-to-mime)
10483                                 (mml-destroy-buffers)
10484                                 (remove-hook 'kill-buffer-hook
10485                                              'mml-destroy-buffers t)
10486                                 (kill-local-variable 'mml-buffer-list)))
10487               (gnus-summary-edit-article-done
10488                ,(or (mail-header-references gnus-current-headers) "")
10489                ,(gnus-group-read-only-p)
10490                ,gnus-summary-buffer no-highlight))))))))
10491
10492 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10493
10494 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10495                                                  no-highlight)
10496   "Make edits to the current article permanent."
10497   (interactive)
10498   (save-excursion
10499     ;; The buffer restriction contains the entire article if it exists.
10500     (when (article-goto-body)
10501       (let ((lines (count-lines (point) (point-max)))
10502             (length (- (point-max) (point)))
10503             (case-fold-search t)
10504             (body (copy-marker (point))))
10505         (goto-char (point-min))
10506         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10507           (delete-region (match-beginning 1) (match-end 1))
10508           (insert (number-to-string length)))
10509         (goto-char (point-min))
10510         (when (re-search-forward
10511                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10512           (delete-region (match-beginning 1) (match-end 1))
10513           (insert (number-to-string length)))
10514         (goto-char (point-min))
10515         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10516           (delete-region (match-beginning 1) (match-end 1))
10517           (insert (number-to-string lines))))))
10518   ;; Replace the article.
10519   (let ((buf (current-buffer))
10520         (article (cdr gnus-article-current))
10521         replace-result)
10522     (with-temp-buffer
10523       (insert-buffer-substring buf)
10524       (if (and (not read-only)
10525                (not (setq replace-result
10526                           (gnus-request-replace-article
10527                            article (car gnus-article-current)
10528                            (current-buffer) t))))
10529           (error "Couldn't replace article")
10530         ;; If we got a number back, then that's the new article number
10531         ;; for this article.  Otherwise, the article number didn't change.
10532         (when (numberp replace-result)
10533           (with-current-buffer gnus-summary-buffer
10534             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10535             (gnus-summary-limit gnus-newsgroup-limit)
10536             (setq article replace-result)
10537             (gnus-summary-goto-subject article t)))
10538         ;; Update the summary buffer.
10539         (if (and references
10540                  (equal (message-tokenize-header references " ")
10541                         (message-tokenize-header
10542                          (or (message-fetch-field "references") "") " ")))
10543             ;; We only have to update this line.
10544             (save-excursion
10545               (save-restriction
10546                 (message-narrow-to-head)
10547                 (let ((head (buffer-substring-no-properties
10548                              (point-min) (point-max)))
10549                       header)
10550                   (with-temp-buffer
10551                     (insert (format "211 %d Article retrieved.\n" article))
10552                     (insert head)
10553                     (insert ".\n")
10554                     (let ((nntp-server-buffer (current-buffer)))
10555                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10556                     (with-current-buffer gnus-summary-buffer
10557                       (gnus-data-set-header (gnus-data-find article) header)
10558                       (gnus-summary-update-article-line article header)
10559                       (if (gnus-summary-goto-subject article nil t)
10560                           (gnus-summary-update-secondary-mark article)))))))
10561           ;; Update threads.
10562           (set-buffer (or buffer gnus-summary-buffer))
10563           (gnus-summary-update-article article)
10564           (if (gnus-summary-goto-subject article nil t)
10565               (gnus-summary-update-secondary-mark article)))
10566         ;; Prettify the article buffer again.
10567         (unless no-highlight
10568           (with-current-buffer gnus-article-buffer
10569             ;;!!! Fix this -- article should be rehighlighted.
10570             ;;(gnus-run-hooks 'gnus-article-display-hook)
10571             (set-buffer gnus-original-article-buffer)
10572             (gnus-request-article
10573              article (car gnus-article-current) (current-buffer))))
10574         ;; Prettify the summary buffer line.
10575         (when (gnus-visual-p 'summary-highlight 'highlight)
10576           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10577
10578 (defun gnus-summary-edit-wash (key)
10579   "Perform editing command KEY in the article buffer."
10580   (interactive
10581    (list
10582     (progn
10583       (message "%s" (concat (this-command-keys) "- "))
10584       (read-char))))
10585   (message "")
10586   (gnus-summary-edit-article)
10587   (execute-kbd-macro (concat (this-command-keys) key))
10588   (gnus-article-edit-done))
10589
10590 ;;; Respooling
10591
10592 (defun gnus-summary-respool-query (&optional silent trace)
10593   "Query where the respool algorithm would put this article."
10594   (interactive)
10595   (let (gnus-mark-article-hook)
10596     (gnus-summary-select-article)
10597     (with-current-buffer gnus-original-article-buffer
10598       (let ((groups (nnmail-article-group 'identity trace)))
10599         (unless silent
10600           (if groups
10601               (message "This message would go to %s"
10602                        (mapconcat 'car groups ", "))
10603             (message "This message would go to no groups"))
10604           groups)))))
10605
10606 (defun gnus-summary-respool-trace ()
10607   "Trace where the respool algorithm would put this article.
10608 Display a buffer showing all fancy splitting patterns which matched."
10609   (interactive)
10610   (gnus-summary-respool-query nil t))
10611
10612 ;; Summary marking commands.
10613
10614 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10615   "Mark articles which has the same subject as read, and then select the next.
10616 If UNMARK is positive, remove any kind of mark.
10617 If UNMARK is negative, tick articles."
10618   (interactive "P")
10619   (when unmark
10620     (setq unmark (prefix-numeric-value unmark)))
10621   (let ((count
10622          (gnus-summary-mark-same-subject
10623           (gnus-summary-article-subject) unmark)))
10624     ;; Select next unread article.  If auto-select-same mode, should
10625     ;; select the first unread article.
10626     (gnus-summary-next-article t (and gnus-auto-select-same
10627                                       (gnus-summary-article-subject)))
10628     (gnus-message 7 "%d article%s marked as %s"
10629                   count (if (= count 1) " is" "s are")
10630                   (if unmark "unread" "read"))))
10631
10632 (defun gnus-summary-kill-same-subject (&optional unmark)
10633   "Mark articles which has the same subject as read.
10634 If UNMARK is positive, remove any kind of mark.
10635 If UNMARK is negative, tick articles."
10636   (interactive "P")
10637   (when unmark
10638     (setq unmark (prefix-numeric-value unmark)))
10639   (let ((count
10640          (gnus-summary-mark-same-subject
10641           (gnus-summary-article-subject) unmark)))
10642     ;; If marked as read, go to next unread subject.
10643     (when (null unmark)
10644       ;; Go to next unread subject.
10645       (gnus-summary-next-subject 1 t))
10646     (gnus-message 7 "%d articles are marked as %s"
10647                   count (if unmark "unread" "read"))))
10648
10649 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10650   "Mark articles with same SUBJECT as read, and return marked number.
10651 If optional argument UNMARK is positive, remove any kinds of marks.
10652 If optional argument UNMARK is negative, mark articles as unread instead."
10653   (let ((count 1))
10654     (save-excursion
10655       (cond
10656        ((null unmark)                   ; Mark as read.
10657         (while (and
10658                 (progn
10659                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10660                   (gnus-summary-show-thread) t)
10661                 (gnus-summary-find-subject subject))
10662           (setq count (1+ count))))
10663        ((> unmark 0)                    ; Tick.
10664         (while (and
10665                 (progn
10666                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10667                   (gnus-summary-show-thread) t)
10668                 (gnus-summary-find-subject subject))
10669           (setq count (1+ count))))
10670        (t                               ; Mark as unread.
10671         (while (and
10672                 (progn
10673                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10674                   (gnus-summary-show-thread) t)
10675                 (gnus-summary-find-subject subject))
10676           (setq count (1+ count)))))
10677       (gnus-set-mode-line 'summary)
10678       ;; Return the number of marked articles.
10679       count)))
10680
10681 (defun gnus-summary-mark-as-processable (n &optional unmark)
10682   "Set the process mark on the next N articles.
10683 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10684 the process mark instead.  The difference between N and the actual
10685 number of articles marked is returned."
10686   (interactive "P")
10687   (if (and (null n) (gnus-region-active-p))
10688       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10689     (setq n (prefix-numeric-value n))
10690     (let ((backward (< n 0))
10691           (n (abs n)))
10692       (while (and
10693               (> n 0)
10694               (if unmark
10695                   (gnus-summary-remove-process-mark
10696                    (gnus-summary-article-number))
10697                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10698               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10699         (setq n (1- n)))
10700       (when (/= 0 n)
10701         (gnus-message 7 "No more articles"))
10702       (gnus-summary-recenter)
10703       (gnus-summary-position-point)
10704       n)))
10705
10706 (defun gnus-summary-unmark-as-processable (n)
10707   "Remove the process mark from the next N articles.
10708 If N is negative, unmark backward instead.  The difference between N and
10709 the actual number of articles unmarked is returned."
10710   (interactive "P")
10711   (gnus-summary-mark-as-processable n t))
10712
10713 (defun gnus-summary-unmark-all-processable ()
10714   "Remove the process mark from all articles."
10715   (interactive)
10716   (save-excursion
10717     (while gnus-newsgroup-processable
10718       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10719   (gnus-summary-position-point))
10720
10721 (defun gnus-summary-add-mark (article type)
10722   "Mark ARTICLE with a mark of TYPE."
10723   (let ((vtype (car (assq type gnus-article-mark-lists)))
10724         var)
10725     (if (not vtype)
10726         (error "No such mark type: %s" type)
10727       (setq var (intern (format "gnus-newsgroup-%s" type)))
10728       (set var (cons article (symbol-value var)))
10729       (if (memq type '(processable cached replied forwarded recent saved))
10730           (gnus-summary-update-secondary-mark article)
10731         ;;; !!! This is bogus.  We should find out what primary
10732         ;;; !!! mark we want to set.
10733         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10734
10735 (defun gnus-summary-mark-as-expirable (n)
10736   "Mark N articles forward as expirable.
10737 If N is negative, mark backward instead.  The difference between N and
10738 the actual number of articles marked is returned."
10739   (interactive "p")
10740   (gnus-summary-mark-forward n gnus-expirable-mark))
10741
10742 (defun gnus-summary-mark-as-spam (n)
10743   "Mark N articles forward as spam.
10744 If N is negative, mark backward instead.  The difference between N and
10745 the actual number of articles marked is returned."
10746   (interactive "p")
10747   (gnus-summary-mark-forward n gnus-spam-mark))
10748
10749 (defun gnus-summary-mark-article-as-replied (article)
10750   "Mark ARTICLE as replied to and update the summary line.
10751 ARTICLE can also be a list of articles."
10752   (interactive (list (gnus-summary-article-number)))
10753   (let ((articles (if (listp article) article (list article))))
10754     (dolist (article articles)
10755       (unless (numberp article)
10756         (error "%s is not a number" article))
10757       (push article gnus-newsgroup-replied)
10758       (let ((inhibit-read-only t))
10759         (when (gnus-summary-goto-subject article nil t)
10760           (gnus-summary-update-secondary-mark article))))))
10761
10762 (defun gnus-summary-mark-article-as-forwarded (article)
10763   "Mark ARTICLE as forwarded and update the summary line.
10764 ARTICLE can also be a list of articles."
10765   (let ((articles (if (listp article) article (list article))))
10766     (dolist (article articles)
10767       (push article gnus-newsgroup-forwarded)
10768       (let ((inhibit-read-only t))
10769         (when (gnus-summary-goto-subject article nil t)
10770           (gnus-summary-update-secondary-mark article))))))
10771
10772 (defun gnus-summary-set-bookmark (article)
10773   "Set a bookmark in current article."
10774   (interactive (list (gnus-summary-article-number)))
10775   (when (or (not (get-buffer gnus-article-buffer))
10776             (not gnus-current-article)
10777             (not gnus-article-current)
10778             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10779     (error "No current article selected"))
10780   ;; Remove old bookmark, if one exists.
10781   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10782   ;; Set the new bookmark, which is on the form
10783   ;; (article-number . line-number-in-body).
10784   (push
10785    (cons article
10786          (with-current-buffer gnus-article-buffer
10787            (count-lines
10788             (min (point)
10789                  (save-excursion
10790                    (article-goto-body)
10791                    (point)))
10792             (point))))
10793    gnus-newsgroup-bookmarks)
10794   (gnus-message 6 "A bookmark has been added to the current article."))
10795
10796 (defun gnus-summary-remove-bookmark (article)
10797   "Remove the bookmark from the current article."
10798   (interactive (list (gnus-summary-article-number)))
10799   ;; Remove old bookmark, if one exists.
10800   (if (not (assq article gnus-newsgroup-bookmarks))
10801       (gnus-message 6 "No bookmark in current article.")
10802     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10803     (gnus-message 6 "Removed bookmark.")))
10804
10805 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10806 (defun gnus-summary-mark-as-dormant (n)
10807   "Mark N articles forward as dormant.
10808 If N is negative, mark backward instead.  The difference between N and
10809 the actual number of articles marked is returned."
10810   (interactive "p")
10811   (gnus-summary-mark-forward n gnus-dormant-mark))
10812
10813 (defun gnus-summary-set-process-mark (article)
10814   "Set the process mark on ARTICLE and update the summary line."
10815   (setq gnus-newsgroup-processable
10816         (cons article
10817               (delq article gnus-newsgroup-processable)))
10818   (when (gnus-summary-goto-subject article)
10819     (gnus-summary-show-thread)
10820     (gnus-summary-goto-subject article)
10821     (gnus-summary-update-secondary-mark article)))
10822
10823 (defun gnus-summary-remove-process-mark (&rest articles)
10824   "Remove the process mark from ARTICLES and update the summary line."
10825   (dolist (article articles)
10826     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10827     (when (gnus-summary-goto-subject article)
10828       (gnus-summary-show-thread)
10829       (gnus-summary-goto-subject article)
10830       (gnus-summary-update-secondary-mark article)))
10831   t)
10832
10833 (defun gnus-summary-set-saved-mark (article)
10834   "Set the process mark on ARTICLE and update the summary line."
10835   (push article gnus-newsgroup-saved)
10836   (when (gnus-summary-goto-subject article)
10837     (gnus-summary-update-secondary-mark article)))
10838
10839 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10840   "Mark N articles as read forwards.
10841 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10842 The difference between N and the actual number of articles marked is
10843 returned.
10844 If NO-EXPIRE, auto-expiry will be inhibited."
10845   (interactive "p")
10846   (gnus-summary-show-thread)
10847   (let ((backward (< n 0))
10848         (gnus-summary-goto-unread
10849          (and gnus-summary-goto-unread
10850               (not (eq gnus-summary-goto-unread 'never))
10851               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10852                                     gnus-ticked-mark gnus-dormant-mark)))))
10853         (n (abs n))
10854         (mark (or mark gnus-del-mark)))
10855     (while (and (> n 0)
10856                 (gnus-summary-mark-article nil mark no-expire)
10857                 (zerop (gnus-summary-next-subject
10858                         (if backward -1 1)
10859                         (and gnus-summary-goto-unread
10860                              (not (eq gnus-summary-goto-unread 'never)))
10861                         t)))
10862       (setq n (1- n)))
10863     (when (/= 0 n)
10864       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10865     (gnus-summary-recenter)
10866     (gnus-summary-position-point)
10867     (gnus-set-mode-line 'summary)
10868     n))
10869
10870 (defun gnus-summary-mark-article-as-read (mark)
10871   "Mark the current article quickly as read with MARK."
10872   (let ((article (gnus-summary-article-number)))
10873     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10874     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10875     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10876     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10877     (push (cons article mark) gnus-newsgroup-reads)
10878     ;; Possibly remove from cache, if that is used.
10879     (when gnus-use-cache
10880       (gnus-cache-enter-remove-article article))
10881     ;; Allow the backend to change the mark.
10882     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10883     ;; Check for auto-expiry.
10884     (when (and gnus-newsgroup-auto-expire
10885                (memq mark gnus-auto-expirable-marks))
10886       (setq mark gnus-expirable-mark)
10887       ;; Let the backend know about the mark change.
10888       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10889       (push article gnus-newsgroup-expirable))
10890     ;; Set the mark in the buffer.
10891     (gnus-summary-update-mark mark 'unread)
10892     t))
10893
10894 (defun gnus-summary-mark-article-as-unread (mark)
10895   "Mark the current article quickly as unread with MARK."
10896   (let* ((article (gnus-summary-article-number))
10897          (old-mark (gnus-summary-article-mark article)))
10898     ;; Allow the backend to change the mark.
10899     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10900     (if (eq mark old-mark)
10901         t
10902       (if (<= article 0)
10903           (progn
10904             (gnus-error 1 "Can't mark negative article numbers")
10905             nil)
10906         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10907         (setq gnus-newsgroup-spam-marked
10908               (delq article gnus-newsgroup-spam-marked))
10909         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10910         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10911         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10912         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10913         (cond ((= mark gnus-ticked-mark)
10914                (setq gnus-newsgroup-marked
10915                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10916                                               article)))
10917               ((= mark gnus-spam-mark)
10918                (setq gnus-newsgroup-spam-marked
10919                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10920                                               article)))
10921               ((= mark gnus-dormant-mark)
10922                (setq gnus-newsgroup-dormant
10923                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10924                                               article)))
10925               (t
10926                (setq gnus-newsgroup-unreads
10927                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10928                                               article))))
10929         (gnus-alist-pull article gnus-newsgroup-reads)
10930
10931         ;; See whether the article is to be put in the cache.
10932         (and gnus-use-cache
10933              (vectorp (gnus-summary-article-header article))
10934              (save-excursion
10935                (gnus-cache-possibly-enter-article
10936                 gnus-newsgroup-name article
10937                 (= mark gnus-ticked-mark)
10938                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10939
10940         ;; Fix the mark.
10941         (gnus-summary-update-mark mark 'unread)
10942         t))))
10943
10944 (defun gnus-summary-mark-article (&optional article mark no-expire)
10945   "Mark ARTICLE with MARK.  MARK can be any character.
10946 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10947 `??' (dormant) and `?E' (expirable).
10948 If MARK is nil, then the default character `?r' is used.
10949 If ARTICLE is nil, then the article on the current line will be
10950 marked.
10951 If NO-EXPIRE, auto-expiry will be inhibited."
10952   ;; The mark might be a string.
10953   (when (stringp mark)
10954     (setq mark (aref mark 0)))
10955   ;; If no mark is given, then we check auto-expiring.
10956   (when (null mark)
10957     (setq mark gnus-del-mark))
10958   (when (and (not no-expire)
10959              gnus-newsgroup-auto-expire
10960              (memq mark gnus-auto-expirable-marks))
10961     (setq mark gnus-expirable-mark))
10962   (let ((article (or article (gnus-summary-article-number)))
10963         (old-mark (gnus-summary-article-mark article)))
10964     ;; Allow the backend to change the mark.
10965     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10966     (if (eq mark old-mark)
10967         t
10968       (unless article
10969         (error "No article on current line"))
10970       (if (not (if (or (= mark gnus-unread-mark)
10971                        (= mark gnus-ticked-mark)
10972                        (= mark gnus-spam-mark)
10973                        (= mark gnus-dormant-mark))
10974                    (gnus-mark-article-as-unread article mark)
10975                  (gnus-mark-article-as-read article mark)))
10976           t
10977         ;; See whether the article is to be put in the cache.
10978         (and gnus-use-cache
10979              (not (= mark gnus-canceled-mark))
10980              (vectorp (gnus-summary-article-header article))
10981              (save-excursion
10982                (gnus-cache-possibly-enter-article
10983                 gnus-newsgroup-name article
10984                 (= mark gnus-ticked-mark)
10985                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10986
10987         (when (gnus-summary-goto-subject article nil t)
10988           (let ((inhibit-read-only t))
10989             (gnus-summary-show-thread)
10990             ;; Fix the mark.
10991             (gnus-summary-update-mark mark 'unread)
10992             t))))))
10993
10994 (defun gnus-summary-update-secondary-mark (article)
10995   "Update the secondary (read, process, cache) mark."
10996   (gnus-summary-update-mark
10997    (cond ((memq article gnus-newsgroup-processable)
10998           gnus-process-mark)
10999          ((memq article gnus-newsgroup-cached)
11000           gnus-cached-mark)
11001          ((memq article gnus-newsgroup-replied)
11002           gnus-replied-mark)
11003          ((memq article gnus-newsgroup-forwarded)
11004           gnus-forwarded-mark)
11005          ((memq article gnus-newsgroup-saved)
11006           gnus-saved-mark)
11007          ((memq article gnus-newsgroup-unseen)
11008           gnus-unseen-mark)
11009          (t gnus-no-mark))
11010    'replied)
11011   (when (gnus-visual-p 'summary-highlight 'highlight)
11012     (gnus-summary-highlight-line)
11013     (gnus-run-hooks 'gnus-summary-update-hook))
11014   t)
11015
11016 (defun gnus-summary-update-download-mark (article)
11017   "Update the download mark."
11018   (gnus-summary-update-mark
11019    (cond ((memq article gnus-newsgroup-undownloaded)
11020           gnus-undownloaded-mark)
11021          (gnus-newsgroup-agentized
11022           gnus-downloaded-mark)
11023          (t
11024           gnus-no-mark))
11025    'download)
11026   (gnus-summary-update-line t)
11027   t)
11028
11029 (defun gnus-summary-update-mark (mark type)
11030   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11031         (inhibit-read-only t))
11032     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11033     (when forward
11034       (when (looking-at "\r")
11035         (incf forward))
11036       (when (<= (+ forward (point)) (point-max))
11037         ;; Go to the right position on the line.
11038         (goto-char (+ forward (point)))
11039         ;; Replace the old mark with the new mark.
11040         (let ((to-insert
11041                (mm-subst-char-in-string
11042                 (char-after) mark
11043                 (buffer-substring (point) (1+ (point))))))
11044           (delete-region (point) (1+ (point)))
11045           (insert to-insert))
11046         ;; Optionally update the marks by some user rule.
11047         (when (eq type 'unread)
11048           (gnus-data-set-mark
11049            (gnus-data-find (gnus-summary-article-number)) mark)
11050           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11051
11052 (defun gnus-mark-article-as-read (article &optional mark)
11053   "Enter ARTICLE in the pertinent lists and remove it from others."
11054   ;; Make the article expirable.
11055   (let ((mark (or mark gnus-del-mark)))
11056     (setq gnus-newsgroup-expirable
11057           (if (= mark gnus-expirable-mark)
11058               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11059             (delq article gnus-newsgroup-expirable)))
11060     ;; Remove from unread and marked lists.
11061     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11062     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11063     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11064     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11065     (push (cons article mark) gnus-newsgroup-reads)
11066     ;; Possibly remove from cache, if that is used.
11067     (when gnus-use-cache
11068       (gnus-cache-enter-remove-article article))
11069     t))
11070
11071 (defun gnus-mark-article-as-unread (article &optional mark)
11072   "Enter ARTICLE in the pertinent lists and remove it from others."
11073   (let ((mark (or mark gnus-ticked-mark)))
11074     (if (<= article 0)
11075         (progn
11076           (gnus-error 1 "Can't mark negative article numbers")
11077           nil)
11078       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11079             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11080             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11081             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11082             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11083
11084       ;; Unsuppress duplicates?
11085       (when gnus-suppress-duplicates
11086         (gnus-dup-unsuppress-article article))
11087
11088       (cond ((= mark gnus-ticked-mark)
11089              (setq gnus-newsgroup-marked
11090                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11091             ((= mark gnus-spam-mark)
11092              (setq gnus-newsgroup-spam-marked
11093                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11094                                             article)))
11095             ((= mark gnus-dormant-mark)
11096              (setq gnus-newsgroup-dormant
11097                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11098             (t
11099              (setq gnus-newsgroup-unreads
11100                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11101       (gnus-alist-pull article gnus-newsgroup-reads)
11102       t)))
11103
11104 (defun gnus-summary-tick-article-forward (n)
11105   "Tick N articles forwards.
11106 If N is negative, tick backwards instead.
11107 The difference between N and the number of articles ticked is returned."
11108   (interactive "p")
11109   (gnus-summary-mark-forward n gnus-ticked-mark))
11110
11111 (defun gnus-summary-tick-article-backward (n)
11112   "Tick N articles backwards.
11113 The difference between N and the number of articles ticked is returned."
11114   (interactive "p")
11115   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11116
11117 (defun gnus-summary-tick-article (&optional article clear-mark)
11118   "Mark current article as unread.
11119 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11120 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11121   (interactive)
11122   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11123                                        gnus-ticked-mark)))
11124
11125 (defun gnus-summary-mark-as-read-forward (n)
11126   "Mark N articles as read forwards.
11127 If N is negative, mark backwards instead.
11128 The difference between N and the actual number of articles marked is
11129 returned."
11130   (interactive "p")
11131   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11132
11133 (defun gnus-summary-mark-as-read-backward (n)
11134   "Mark the N articles as read backwards.
11135 The difference between N and the actual number of articles marked is
11136 returned."
11137   (interactive "p")
11138   (gnus-summary-mark-forward
11139    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11140
11141 (defun gnus-summary-mark-as-read (&optional article mark)
11142   "Mark current article as read.
11143 ARTICLE specifies the article to be marked as read.
11144 MARK specifies a string to be inserted at the beginning of the line."
11145   (gnus-summary-mark-article article mark))
11146
11147 (defun gnus-summary-clear-mark-forward (n)
11148   "Clear marks from N articles forward.
11149 If N is negative, clear backward instead.
11150 The difference between N and the number of marks cleared is returned."
11151   (interactive "p")
11152   (gnus-summary-mark-forward n gnus-unread-mark))
11153
11154 (defun gnus-summary-clear-mark-backward (n)
11155   "Clear marks from N articles backward.
11156 The difference between N and the number of marks cleared is returned."
11157   (interactive "p")
11158   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11159
11160 (defun gnus-summary-mark-unread-as-read ()
11161   "Intended to be used by `gnus-mark-article-hook'."
11162   (when (memq gnus-current-article gnus-newsgroup-unreads)
11163     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11164
11165 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11166   "Intended to be used by `gnus-mark-article-hook'."
11167   (let ((mark (gnus-summary-article-mark)))
11168     (when (or (gnus-unread-mark-p mark)
11169               (gnus-read-mark-p mark))
11170       (gnus-summary-mark-article gnus-current-article
11171                                  (or new-mark gnus-read-mark)))))
11172
11173 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11174   "Intended to be used by `gnus-mark-article-hook'."
11175   (let ((mark (gnus-summary-article-mark)))
11176     (when (or (gnus-unread-mark-p mark)
11177               (gnus-read-mark-p mark))
11178       (gnus-summary-mark-article (gnus-summary-article-number)
11179                                  (or new-mark gnus-read-mark)))))
11180
11181 (defun gnus-summary-mark-unread-as-ticked ()
11182   "Intended to be used by `gnus-mark-article-hook'."
11183   (when (memq gnus-current-article gnus-newsgroup-unreads)
11184     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11185
11186 (defun gnus-summary-mark-region-as-read (point mark all)
11187   "Mark all unread articles between point and mark as read.
11188 If given a prefix, mark all articles between point and mark as read,
11189 even ticked and dormant ones."
11190   (interactive "r\nP")
11191   (save-excursion
11192     (let (article)
11193       (goto-char point)
11194       (beginning-of-line)
11195       (while (and
11196               (< (point) mark)
11197               (progn
11198                 (when (or all
11199                           (memq (setq article (gnus-summary-article-number))
11200                                 gnus-newsgroup-unreads))
11201                   (gnus-summary-mark-article article gnus-del-mark))
11202                 t)
11203               (gnus-summary-find-next))))))
11204
11205 (defun gnus-summary-mark-below (score mark)
11206   "Mark articles with score less than SCORE with MARK."
11207   (interactive "P\ncMark: ")
11208   (setq score (if score
11209                   (prefix-numeric-value score)
11210                 (or gnus-summary-default-score 0)))
11211   (with-current-buffer gnus-summary-buffer
11212     (goto-char (point-min))
11213     (while
11214         (progn
11215           (and (< (gnus-summary-article-score) score)
11216                (gnus-summary-mark-article nil mark))
11217           (gnus-summary-find-next)))))
11218
11219 (defun gnus-summary-kill-below (&optional score)
11220   "Mark articles with score below SCORE as read."
11221   (interactive "P")
11222   (gnus-summary-mark-below score gnus-killed-mark))
11223
11224 (defun gnus-summary-clear-above (&optional score)
11225   "Clear all marks from articles with score above SCORE."
11226   (interactive "P")
11227   (gnus-summary-mark-above score gnus-unread-mark))
11228
11229 (defun gnus-summary-tick-above (&optional score)
11230   "Tick all articles with score above SCORE."
11231   (interactive "P")
11232   (gnus-summary-mark-above score gnus-ticked-mark))
11233
11234 (defun gnus-summary-mark-above (score mark)
11235   "Mark articles with score over SCORE with MARK."
11236   (interactive "P\ncMark: ")
11237   (setq score (if score
11238                   (prefix-numeric-value score)
11239                 (or gnus-summary-default-score 0)))
11240   (with-current-buffer gnus-summary-buffer
11241     (goto-char (point-min))
11242     (while (and (progn
11243                   (when (> (gnus-summary-article-score) score)
11244                     (gnus-summary-mark-article nil mark))
11245                   t)
11246                 (gnus-summary-find-next)))))
11247
11248 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11249 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11250 (defun gnus-summary-limit-include-expunged (&optional no-error)
11251   "Display all the hidden articles that were expunged for low scores."
11252   (interactive)
11253   (let ((inhibit-read-only t))
11254     (let ((scored gnus-newsgroup-scored)
11255           headers h)
11256       (while scored
11257         (unless (gnus-summary-article-header (caar scored))
11258           (and (setq h (gnus-number-to-header (caar scored)))
11259                (< (cdar scored) gnus-summary-expunge-below)
11260                (push h headers)))
11261         (setq scored (cdr scored)))
11262       (if (not headers)
11263           (when (not no-error)
11264             (error "No expunged articles hidden"))
11265         (goto-char (point-min))
11266         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11267         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11268         (dolist (x headers)
11269           (push (mail-header-number x) gnus-newsgroup-limit))
11270         (gnus-summary-prepare-unthreaded (nreverse headers))
11271         (goto-char (point-min))
11272         (gnus-summary-position-point)
11273         t))))
11274
11275 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11276   "Mark all unread articles in this newsgroup as read.
11277 If prefix argument ALL is non-nil, ticked and dormant articles will
11278 also be marked as read.
11279 If QUIETLY is non-nil, no questions will be asked.
11280
11281 If TO-HERE is non-nil, it should be a point in the buffer.  All
11282 articles before (after, if REVERSE is set) this point will be marked
11283 as read.
11284
11285 Note that this function will only catch up the unread article
11286 in the current summary buffer limitation.
11287
11288 The number of articles marked as read is returned."
11289   (interactive "P")
11290   (prog1
11291       (save-excursion
11292         (when (or quietly
11293                   (not gnus-interactive-catchup) ;Without confirmation?
11294                   gnus-expert-user
11295                   (gnus-y-or-n-p
11296                    (if all
11297                        "Mark absolutely all articles as read? "
11298                      "Mark all unread articles as read? ")))
11299           (if (and not-mark
11300                    (not gnus-newsgroup-adaptive)
11301                    (not gnus-newsgroup-auto-expire)
11302                    (not gnus-suppress-duplicates)
11303                    (or (not gnus-use-cache)
11304                        (eq gnus-use-cache 'passive)))
11305               (progn
11306                 (when all
11307                   (setq gnus-newsgroup-marked nil
11308                         gnus-newsgroup-spam-marked nil
11309                         gnus-newsgroup-dormant nil))
11310                 (setq gnus-newsgroup-unreads
11311                       (gnus-sorted-nunion
11312                        (gnus-sorted-intersection gnus-newsgroup-unreads
11313                                                  gnus-newsgroup-downloadable)
11314                        (gnus-sorted-difference gnus-newsgroup-unfetched
11315                                                gnus-newsgroup-cached))))
11316             ;; We actually mark all articles as canceled, which we
11317             ;; have to do when using auto-expiry or adaptive scoring.
11318             (gnus-summary-show-all-threads)
11319             (if (and to-here reverse)
11320                 (progn
11321                   (goto-char to-here)
11322                   (gnus-summary-mark-current-read-and-unread-as-read
11323                    gnus-catchup-mark)
11324                   (while (gnus-summary-find-next (not all))
11325                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11326               (when (gnus-summary-first-subject (not all))
11327                 (while (and
11328                         (if to-here (< (point) to-here) t)
11329                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11330                         (gnus-summary-find-next (not all))))))
11331             (gnus-set-mode-line 'summary))
11332           t))
11333     (gnus-summary-position-point)))
11334
11335 (defun gnus-summary-catchup-to-here (&optional all)
11336   "Mark all unticked articles before the current one as read.
11337 If ALL is non-nil, also mark ticked and dormant articles as read."
11338   (interactive "P")
11339   (save-excursion
11340     (gnus-save-hidden-threads
11341       (let ((beg (point)))
11342         ;; We check that there are unread articles.
11343         (when (or all (gnus-summary-find-prev))
11344           (gnus-summary-catchup all t beg)))))
11345   (gnus-summary-position-point))
11346
11347 (defun gnus-summary-catchup-from-here (&optional all)
11348   "Mark all unticked articles after (and including) the current one as read.
11349 If ALL is non-nil, also mark ticked and dormant articles as read."
11350   (interactive "P")
11351   (save-excursion
11352     (gnus-save-hidden-threads
11353       (let ((beg (point)))
11354         ;; We check that there are unread articles.
11355         (when (or all (gnus-summary-find-next))
11356           (gnus-summary-catchup all t beg nil t)))))
11357   (gnus-summary-position-point))
11358
11359 (defun gnus-summary-catchup-all (&optional quietly)
11360   "Mark all articles in this newsgroup as read.
11361 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11362 instead, which marks only unread articles as read."
11363   (interactive "P")
11364   (gnus-summary-catchup t quietly))
11365
11366 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11367   "Mark all unread articles in this group as read, then exit.
11368 If prefix argument ALL is non-nil, all articles are marked as read.
11369 If QUIETLY is non-nil, no questions will be asked."
11370   (interactive "P")
11371   (when (gnus-summary-catchup all quietly nil 'fast)
11372     ;; Select next newsgroup or exit.
11373     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11374              (eq gnus-auto-select-next 'quietly))
11375         (gnus-summary-next-group nil)
11376       (gnus-summary-exit))))
11377
11378 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11379   "Mark all articles in this newsgroup as read, and then exit.
11380 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11381 instead, which marks only unread articles as read."
11382   (interactive "P")
11383   (gnus-summary-catchup-and-exit t quietly))
11384
11385 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11386   "Mark all articles in this group as read and select the next group.
11387 If given a prefix, mark all articles, unread as well as ticked, as
11388 read."
11389   (interactive "P")
11390   (save-excursion
11391     (gnus-summary-catchup all))
11392   (gnus-summary-next-group))
11393
11394 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11395   "Mark all articles in this group as read and select the previous group.
11396 If given a prefix, mark all articles, unread as well as ticked, as
11397 read."
11398   (interactive "P")
11399   (save-excursion
11400     (gnus-summary-catchup all))
11401   (gnus-summary-next-group nil nil t))
11402
11403 ;;;
11404 ;;; with article
11405 ;;;
11406
11407 (defmacro gnus-with-article (article &rest forms)
11408   "Select ARTICLE and perform FORMS in the original article buffer.
11409 Then replace the article with the result."
11410   `(progn
11411      ;; We don't want the article to be marked as read.
11412      (let (gnus-mark-article-hook)
11413        (gnus-summary-select-article t t nil ,article))
11414      (set-buffer gnus-original-article-buffer)
11415      ,@forms
11416      (if (not (gnus-check-backend-function
11417                'request-replace-article (car gnus-article-current)))
11418          (gnus-message 5 "Read-only group; not replacing")
11419        (unless (gnus-request-replace-article
11420                 ,article (car gnus-article-current)
11421                 (current-buffer) t)
11422          (error "Couldn't replace article")))
11423      ;; The cache and backlog have to be flushed somewhat.
11424      (when gnus-keep-backlog
11425        (gnus-backlog-remove-article
11426         (car gnus-article-current) (cdr gnus-article-current)))
11427      (when gnus-use-cache
11428        (gnus-cache-update-article
11429         (car gnus-article-current) (cdr gnus-article-current)))))
11430
11431 (put 'gnus-with-article 'lisp-indent-function 1)
11432 (put 'gnus-with-article 'edebug-form-spec '(form body))
11433
11434 ;; Thread-based commands.
11435
11436 (defun gnus-summary-articles-in-thread (&optional article)
11437   "Return a list of all articles in the current thread.
11438 If ARTICLE is non-nil, return all articles in the thread that starts
11439 with that article."
11440   (let* ((article (or article (gnus-summary-article-number)))
11441          (data (gnus-data-find-list article))
11442          (top-level (gnus-data-level (car data)))
11443          (top-subject
11444           (cond ((null gnus-thread-operation-ignore-subject)
11445                  (gnus-simplify-subject-re
11446                   (mail-header-subject (gnus-data-header (car data)))))
11447                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11448                  (gnus-simplify-subject-fuzzy
11449                   (mail-header-subject (gnus-data-header (car data)))))
11450                 (t nil)))
11451          (end-point (save-excursion
11452                       (goto-char (gnus-data-pos (car data)))
11453                       (if (gnus-summary-go-to-next-thread)
11454                           (point) (point-max))))
11455          articles)
11456     (while (and data
11457                 (< (gnus-data-pos (car data)) end-point))
11458       (when (or (not top-subject)
11459                 (string= top-subject
11460                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11461                              (gnus-simplify-subject-fuzzy
11462                               (mail-header-subject
11463                                (gnus-data-header (car data))))
11464                            (gnus-simplify-subject-re
11465                             (mail-header-subject
11466                              (gnus-data-header (car data)))))))
11467         (push (gnus-data-number (car data)) articles))
11468       (unless (and (setq data (cdr data))
11469                    (> (gnus-data-level (car data)) top-level))
11470         (setq data nil)))
11471     ;; Return the list of articles.
11472     (nreverse articles)))
11473
11474 (defun gnus-summary-rethread-current ()
11475   "Rethread the thread the current article is part of."
11476   (interactive)
11477   (let* ((gnus-show-threads t)
11478          (article (gnus-summary-article-number))
11479          (id (mail-header-id (gnus-summary-article-header)))
11480          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11481     (unless id
11482       (error "No article on the current line"))
11483     (gnus-rebuild-thread id)
11484     (gnus-summary-goto-subject article)))
11485
11486 (defun gnus-summary-reparent-thread ()
11487   "Make the current article child of the marked (or previous) article.
11488
11489 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11490 is non-nil or the Subject: of both articles are the same."
11491   (interactive)
11492   (unless (not (gnus-group-read-only-p))
11493     (error "The current newsgroup does not support article editing"))
11494   (unless (<= (length gnus-newsgroup-processable) 1)
11495     (error "No more than one article may be marked"))
11496   (let ((child (gnus-summary-article-number))
11497         ;; First grab the marked article, otherwise one line up.
11498         (parent (if (not (null gnus-newsgroup-processable))
11499                     (car gnus-newsgroup-processable)
11500                   (save-excursion
11501                     (if (eq (forward-line -1) 0)
11502                         (gnus-summary-article-number)
11503                       (error "Beginning of summary buffer"))))))
11504     (gnus-summary-reparent-children parent (list child))))
11505
11506 (defun gnus-summary-reparent-children (parent children)
11507   "Make PARENT the parent of CHILDREN.
11508 When called interactively, PARENT is the current article and CHILDREN
11509 are the process-marked articles."
11510   (interactive
11511    (list (gnus-summary-article-number)
11512          (gnus-summary-work-articles nil)))
11513   (dolist (child children)
11514     (save-window-excursion
11515       (let ((gnus-article-buffer " *reparent*"))
11516         (unless (not (eq parent child))
11517           (error "An article may not be self-referential"))
11518         (let ((message-id (mail-header-id
11519                            (gnus-summary-article-header parent))))
11520           (unless (and message-id (not (equal message-id "")))
11521             (error "No message-id in desired parent"))
11522           (gnus-with-article child
11523             (save-restriction
11524               (goto-char (point-min))
11525               (message-narrow-to-head)
11526               (if (re-search-forward "^References: " nil t)
11527                   (progn
11528                     (re-search-forward "^[^ \t]" nil t)
11529                     (forward-line -1)
11530                     (end-of-line)
11531                     (insert " " message-id))
11532                 (insert "References: " message-id "\n"))))
11533           (set-buffer gnus-summary-buffer)
11534           (gnus-summary-unmark-all-processable)
11535           (gnus-summary-update-article child)
11536           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11537             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11538           (gnus-summary-rethread-current)
11539           (gnus-message 3 "Article %d is now the child of article %d"
11540                         child parent))))))
11541
11542 (defun gnus-summary-toggle-threads (&optional arg)
11543   "Toggle showing conversation threads.
11544 If ARG is positive number, turn showing conversation threads on."
11545   (interactive "P")
11546   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11547     (setq gnus-show-threads
11548           (if (null arg) (not gnus-show-threads)
11549             (> (prefix-numeric-value arg) 0)))
11550     (gnus-summary-prepare)
11551     (gnus-summary-goto-subject current)
11552     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11553     (gnus-summary-position-point)))
11554
11555 (eval-and-compile
11556   (if (fboundp 'remove-overlays)
11557       (defalias 'gnus-remove-overlays 'remove-overlays)
11558     (defun gnus-remove-overlays (beg end name val)
11559       "Clear BEG and END of overlays whose property NAME has value VAL.
11560 For compatibility with XEmacs."
11561       (dolist (ov (gnus-overlays-in beg end))
11562         (when (eq (gnus-overlay-get ov name) val)
11563           (gnus-delete-overlay ov))))))
11564
11565 (defun gnus-summary-show-all-threads ()
11566   "Show all threads."
11567   (interactive)
11568   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11569   (gnus-summary-position-point))
11570
11571 (defsubst gnus-summary--inv (p)
11572   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11573
11574 (defun gnus-summary-show-thread ()
11575   "Show thread subtrees.
11576 Returns nil if no thread was there to be shown."
11577   (interactive)
11578   (let* ((orig (point))
11579          (end (point-at-eol))
11580          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11581          ;; Leave point at bol
11582          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11583          (eoi (when end
11584                 (if (fboundp 'next-single-char-property-change)
11585                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11586                     (or (next-single-char-property-change end 'invisible)
11587                         (point-max))
11588                   (while (progn
11589                            (end-of-line 2)
11590                            (and (not (eobp))
11591                                 (eq (get-char-property (point) 'invisible)
11592                                     'gnus-sum))))
11593                   (point)))))
11594     (when eoi
11595       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11596       (goto-char orig)
11597       (gnus-summary-position-point)
11598       eoi)))
11599
11600 (defun gnus-summary-maybe-hide-threads ()
11601   "If requested, hide the threads that should be hidden."
11602   (when (and gnus-show-threads
11603              gnus-thread-hide-subtree)
11604     (gnus-summary-hide-all-threads
11605      (if (or (consp gnus-thread-hide-subtree)
11606              (functionp gnus-thread-hide-subtree))
11607          (gnus-make-predicate gnus-thread-hide-subtree)
11608        nil))))
11609
11610 ;;; Hiding predicates.
11611
11612 (defun gnus-article-unread-p (header)
11613   (memq (mail-header-number header) gnus-newsgroup-unreads))
11614
11615 (defun gnus-article-unseen-p (header)
11616   (memq (mail-header-number header) gnus-newsgroup-unseen))
11617
11618 (defun gnus-map-articles (predicate articles)
11619   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11620   (apply 'gnus-or (mapcar predicate
11621                           (mapcar (lambda (number)
11622                                     (gnus-summary-article-header number))
11623                                   articles))))
11624
11625 (defun gnus-summary-hide-all-threads (&optional predicate)
11626   "Hide all thread subtrees.
11627 If PREDICATE is supplied, threads that satisfy this predicate
11628 will not be hidden."
11629   (interactive)
11630   (save-excursion
11631     (goto-char (point-min))
11632     (let ((end nil)
11633           (count 0))
11634       (while (not end)
11635         (incf count)
11636         (when (zerop (mod count 1000))
11637           (message "Hiding all threads... %d" count))
11638         (when (or (not predicate)
11639                   (gnus-map-articles
11640                    predicate (gnus-summary-article-children)))
11641             (gnus-summary-hide-thread))
11642         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11643   (gnus-summary-position-point))
11644
11645 (defun gnus-summary-hide-thread ()
11646   "Hide thread subtrees.
11647 If PREDICATE is supplied, threads that satisfy this predicate
11648 will not be hidden.
11649 Returns nil if no threads were there to be hidden."
11650   (interactive)
11651   (let ((start (point))
11652         (starteol (line-end-position))
11653         (article (gnus-summary-article-number)))
11654     (goto-char start)
11655     ;; Go forward until either the buffer ends or the subthread ends.
11656     (when (and (not (eobp))
11657                (or (zerop (gnus-summary-next-thread 1 t))
11658                    (goto-char (point-max))))
11659       (if (and (> (point) start)
11660                ;; FIXME: this should actually search for a non-invisible \n.
11661                (search-backward "\n" start t))
11662           (progn
11663             (when (> (point) starteol)
11664               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11665               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11666                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11667                 (gnus-overlay-put ol 'evaporate t)))
11668             (gnus-summary-goto-subject article)
11669             (when (> start (point))
11670               (message "Hiding the thread moved us backwards, aborting!")
11671               (goto-char (point-max))))
11672         (goto-char start)
11673         nil))))
11674
11675 (defun gnus-summary-go-to-next-thread (&optional previous)
11676   "Go to the same level (or less) next thread.
11677 If PREVIOUS is non-nil, go to previous thread instead.
11678 Return the article number moved to, or nil if moving was impossible."
11679   (let ((level (gnus-summary-thread-level))
11680         (way (if previous -1 1))
11681         (beg (point)))
11682     (forward-line way)
11683     (while (and (not (eobp))
11684                 (< level (gnus-summary-thread-level)))
11685       (forward-line way))
11686     (if (eobp)
11687         (progn
11688           (goto-char beg)
11689           nil)
11690       (setq beg (point))
11691       (prog1
11692           (gnus-summary-article-number)
11693         (goto-char beg)))))
11694
11695 (defun gnus-summary-next-thread (n &optional silent)
11696   "Go to the same level next N'th thread.
11697 If N is negative, search backward instead.
11698 Returns the difference between N and the number of skips actually
11699 done.
11700
11701 If SILENT, don't output messages."
11702   (interactive "p")
11703   (let ((backward (< n 0))
11704         (n (abs n)))
11705     (while (and (> n 0)
11706                 (gnus-summary-go-to-next-thread backward))
11707       (decf n))
11708     (unless silent
11709       (gnus-summary-position-point))
11710     (when (and (not silent) (/= 0 n))
11711       (gnus-message 7 "No more threads"))
11712     n))
11713
11714 (defun gnus-summary-prev-thread (n)
11715   "Go to the same level previous N'th thread.
11716 Returns the difference between N and the number of skips actually
11717 done."
11718   (interactive "p")
11719   (gnus-summary-next-thread (- n)))
11720
11721 (defun gnus-summary-go-down-thread ()
11722   "Go down one level in the current thread."
11723   (let ((children (gnus-summary-article-children)))
11724     (when children
11725       (gnus-summary-goto-subject (car children)))))
11726
11727 (defun gnus-summary-go-up-thread ()
11728   "Go up one level in the current thread."
11729   (let ((parent (gnus-summary-article-parent)))
11730     (when parent
11731       (gnus-summary-goto-subject parent))))
11732
11733 (defun gnus-summary-down-thread (n)
11734   "Go down thread N steps.
11735 If N is negative, go up instead.
11736 Returns the difference between N and how many steps down that were
11737 taken."
11738   (interactive "p")
11739   (let ((up (< n 0))
11740         (n (abs n)))
11741     (while (and (> n 0)
11742                 (if up (gnus-summary-go-up-thread)
11743                   (gnus-summary-go-down-thread)))
11744       (setq n (1- n)))
11745     (gnus-summary-position-point)
11746     (when (/= 0 n)
11747       (gnus-message 7 "Can't go further"))
11748     n))
11749
11750 (defun gnus-summary-up-thread (n)
11751   "Go up thread N steps.
11752 If N is negative, go down instead.
11753 Returns the difference between N and how many steps down that were
11754 taken."
11755   (interactive "p")
11756   (gnus-summary-down-thread (- n)))
11757
11758 (defun gnus-summary-top-thread ()
11759   "Go to the top of the thread."
11760   (interactive)
11761   (while (gnus-summary-go-up-thread))
11762   (gnus-summary-article-number))
11763
11764 (defun gnus-summary-expire-thread ()
11765   "Mark articles under current thread as expired."
11766   (interactive)
11767   (gnus-summary-kill-thread 0))
11768
11769 (defun gnus-summary-kill-thread (&optional unmark)
11770   "Mark articles under current thread as read.
11771 If the prefix argument is positive, remove any kinds of marks.
11772 If the prefix argument is zero, mark thread as expired.
11773 If the prefix argument is negative, tick articles instead."
11774   (interactive "P")
11775   (when unmark
11776     (setq unmark (prefix-numeric-value unmark)))
11777   (let ((articles (gnus-summary-articles-in-thread))
11778         (hide (or (null unmark) (= unmark 0))))
11779     (save-excursion
11780       ;; Expand the thread.
11781       (gnus-summary-show-thread)
11782       ;; Mark all the articles.
11783       (while articles
11784         (gnus-summary-goto-subject (car articles))
11785         (cond ((null unmark)
11786                (gnus-summary-mark-article-as-read gnus-killed-mark))
11787               ((> unmark 0)
11788                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11789               ((= unmark 0)
11790                (gnus-summary-mark-article nil gnus-expirable-mark))
11791               (t
11792                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11793         (setq articles (cdr articles))))
11794     ;; Hide killed subtrees when hide is true.
11795     (and hide
11796          gnus-thread-hide-killed
11797          (gnus-summary-hide-thread))
11798     ;; If hide is t, go to next unread subject.
11799     (when hide
11800       ;; Go to next unread subject.
11801       (gnus-summary-next-subject 1 t)))
11802   (gnus-set-mode-line 'summary))
11803
11804 ;; Summary sorting commands
11805
11806 (defun gnus-summary-sort-by-number (&optional reverse)
11807   "Sort the summary buffer by article number.
11808 Argument REVERSE means reverse order."
11809   (interactive "P")
11810   (gnus-summary-sort 'number reverse))
11811
11812 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11813   "Sort the summary buffer by most recent article number.
11814 Argument REVERSE means reverse order."
11815   (interactive "P")
11816   (gnus-summary-sort 'most-recent-number reverse))
11817
11818 (defun gnus-summary-sort-by-random (&optional reverse)
11819   "Randomize the order in the summary buffer.
11820 Argument REVERSE means to randomize in reverse order."
11821   (interactive "P")
11822   (gnus-summary-sort 'random reverse))
11823
11824 (defun gnus-summary-sort-by-author (&optional reverse)
11825   "Sort the summary buffer by author name alphabetically.
11826 If `case-fold-search' is non-nil, case of letters is ignored.
11827 Argument REVERSE means reverse order."
11828   (interactive "P")
11829   (gnus-summary-sort 'author reverse))
11830
11831 (defun gnus-summary-sort-by-recipient (&optional reverse)
11832   "Sort the summary buffer by recipient name alphabetically.
11833 If `case-fold-search' is non-nil, case of letters is ignored.
11834 Argument REVERSE means reverse order."
11835   (interactive "P")
11836   (gnus-summary-sort 'recipient reverse))
11837
11838 (defun gnus-summary-sort-by-subject (&optional reverse)
11839   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11840 If `case-fold-search' is non-nil, case of letters is ignored.
11841 Argument REVERSE means reverse order."
11842   (interactive "P")
11843   (gnus-summary-sort 'subject reverse))
11844
11845 (defun gnus-summary-sort-by-date (&optional reverse)
11846   "Sort the summary buffer by date.
11847 Argument REVERSE means reverse order."
11848   (interactive "P")
11849   (gnus-summary-sort 'date reverse))
11850
11851 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11852   "Sort the summary buffer by most recent date.
11853 Argument REVERSE means reverse order."
11854   (interactive "P")
11855   (gnus-summary-sort 'most-recent-date reverse))
11856
11857 (defun gnus-summary-sort-by-score (&optional reverse)
11858   "Sort the summary buffer by score.
11859 Argument REVERSE means reverse order."
11860   (interactive "P")
11861   (gnus-summary-sort 'score reverse))
11862
11863 (defun gnus-summary-sort-by-lines (&optional reverse)
11864   "Sort the summary buffer by the number of lines.
11865 Argument REVERSE means reverse order."
11866   (interactive "P")
11867   (gnus-summary-sort 'lines reverse))
11868
11869 (defun gnus-summary-sort-by-chars (&optional reverse)
11870   "Sort the summary buffer by article length.
11871 Argument REVERSE means reverse order."
11872   (interactive "P")
11873   (gnus-summary-sort 'chars reverse))
11874
11875 (defun gnus-summary-sort-by-original (&optional reverse)
11876   "Sort the summary buffer using the default sorting method.
11877 Argument REVERSE means reverse order."
11878   (interactive "P")
11879   (let* ((inhibit-read-only t)
11880          (gnus-summary-prepare-hook nil))
11881     ;; We do the sorting by regenerating the threads.
11882     (gnus-summary-prepare)
11883     ;; Hide subthreads if needed.
11884     (gnus-summary-maybe-hide-threads)))
11885
11886 (defun gnus-summary-sort (predicate reverse)
11887   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11888   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11889          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11890          (gnus-thread-sort-functions
11891           (if (not reverse)
11892               thread
11893             `(lambda (t1 t2)
11894                (,thread t2 t1))))
11895          (gnus-sort-gathered-threads-function
11896           gnus-thread-sort-functions)
11897          (gnus-article-sort-functions
11898           (if (not reverse)
11899               article
11900             `(lambda (t1 t2)
11901                (,article t2 t1))))
11902          (inhibit-read-only t)
11903          (gnus-summary-prepare-hook nil))
11904     ;; We do the sorting by regenerating the threads.
11905     (gnus-summary-prepare)
11906     ;; Hide subthreads if needed.
11907     (gnus-summary-maybe-hide-threads)))
11908
11909 ;; Summary saving commands.
11910
11911 (defun gnus-summary-save-article (&optional n not-saved)
11912   "Save the current article using the default saver function.
11913 If N is a positive number, save the N next articles.
11914 If N is a negative number, save the N previous articles.
11915 If N is nil and any articles have been marked with the process mark,
11916 save those articles instead.
11917 The variable `gnus-default-article-saver' specifies the saver function.
11918
11919 If the optional second argument NOT-SAVED is non-nil, articles saved
11920 will not be marked as saved."
11921   (interactive "P")
11922   (require 'gnus-art)
11923   (let* ((articles (gnus-summary-work-articles n))
11924          (save-buffer (save-excursion
11925                         (nnheader-set-temp-buffer " *Gnus Save*")))
11926          (num (length articles))
11927          ;; Whether to save decoded articles or raw articles.
11928          (decode (when gnus-article-save-coding-system
11929                    (get gnus-default-article-saver :decode)))
11930          ;; When saving many articles in a single file, use the other
11931          ;; function to save articles other than the first one.
11932          (saver2 (get gnus-default-article-saver :function))
11933          (gnus-prompt-before-saving (if saver2
11934                                         t
11935                                       gnus-prompt-before-saving))
11936          (gnus-default-article-saver gnus-default-article-saver)
11937          header file)
11938     (dolist (article articles)
11939       (setq header (gnus-summary-article-header article))
11940       (if (not (vectorp header))
11941           ;; This is a pseudo-article.
11942           (if (assq 'name header)
11943               (gnus-copy-file (cdr (assq 'name header)))
11944             (gnus-message 1 "Article %d is unsavable" article))
11945         ;; This is a real article.
11946         (save-window-excursion
11947           (gnus-summary-select-article decode decode nil article)
11948           (gnus-summary-goto-subject article))
11949         (with-current-buffer save-buffer
11950           (erase-buffer)
11951           (insert-buffer-substring (if decode
11952                                        gnus-article-buffer
11953                                      gnus-original-article-buffer)))
11954         (setq file (gnus-article-save save-buffer file num))
11955         (gnus-summary-remove-process-mark article)
11956         (unless not-saved
11957           (gnus-summary-set-saved-mark article)))
11958       (when saver2
11959         (setq gnus-default-article-saver saver2
11960               saver2 nil)))
11961     (gnus-kill-buffer save-buffer)
11962     (gnus-summary-position-point)
11963     (gnus-set-mode-line 'summary)
11964     n))
11965
11966 (defun gnus-summary-pipe-output (&optional n sym)
11967   "Pipe the current article to a subprocess.
11968 If N is a positive number, pipe the N next articles.
11969 If N is a negative number, pipe the N previous articles.
11970 If N is nil and any articles have been marked with the process mark,
11971 pipe those articles instead.
11972 The default command to which articles are piped is specified by the
11973 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11974 will be prompted for the command.
11975
11976 The properties `:decode' and `:headers' that are put to the function
11977 symbol `gnus-summary-save-in-pipe' control whether this function
11978 decodes articles and what headers to keep (see the doc string for the
11979 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11980 is neither omitted nor the symbol `r', force including all headers
11981 regardless of the `:headers' property.  If it is the symbol `r',
11982 articles that are not decoded and include all headers will be piped
11983 no matter what the properties `:decode' and `:headers' are."
11984   (interactive (gnus-interactive "P\ny"))
11985   (require 'gnus-art)
11986   (let* ((articles (gnus-summary-work-articles n))
11987          (result-buffer "*Shell Command Output*")
11988          (all-headers (not (memq sym '(nil r))))
11989          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11990          (raw (eq sym 'r))
11991          (headers (get 'gnus-summary-save-in-pipe :headers))
11992          command result)
11993     (unless (numberp (car articles))
11994       (error "No article to pipe"))
11995     (setq command (gnus-read-shell-command
11996                    (concat "Shell command on "
11997                            (if (cdr articles)
11998                                (format "these %d articles" (length articles))
11999                              "this article")
12000                            ": ")
12001                    gnus-summary-pipe-output-default-command))
12002     (when (string-equal command "")
12003       (error "A command is required"))
12004     (when all-headers
12005       (put 'gnus-summary-save-in-pipe :headers nil))
12006     (unwind-protect
12007         (while articles
12008           (gnus-summary-goto-subject (pop articles))
12009           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12010           (when (and (get-buffer result-buffer)
12011                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12012             (setq result (concat result (with-current-buffer result-buffer
12013                                           (buffer-string))))))
12014       (put 'gnus-summary-save-in-pipe :headers headers))
12015     (unless (zerop (length result))
12016       (if (with-current-buffer (get-buffer-create result-buffer)
12017             (erase-buffer)
12018             (insert result)
12019             (prog1
12020                 (and (= (count-lines (point-min) (point)) 1)
12021                      (progn
12022                        (end-of-line 0)
12023                        (<= (current-column)
12024                            (window-width (minibuffer-window)))))
12025               (goto-char (point-min))))
12026           (message "%s" (substring result 0 -1))
12027         (message nil)
12028         (gnus-configure-windows 'pipe)))))
12029
12030 (defun gnus-summary-save-article-mail (&optional arg)
12031   "Append the current article to a Unix mail box file.
12032 If N is a positive number, save the N next articles.
12033 If N is a negative number, save the N previous articles.
12034 If N is nil and any articles have been marked with the process mark,
12035 save those articles instead."
12036   (interactive "P")
12037   (require 'gnus-art)
12038   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12039     (gnus-summary-save-article arg)))
12040
12041 (defun gnus-summary-save-article-rmail (&optional arg)
12042   "Append the current article to an rmail file.
12043 If N is a positive number, save the N next articles.
12044 If N is a negative number, save the N previous articles.
12045 If N is nil and any articles have been marked with the process mark,
12046 save those articles instead."
12047   (interactive "P")
12048   (require 'gnus-art)
12049   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12050     (gnus-summary-save-article arg)))
12051
12052 (defun gnus-summary-save-article-file (&optional arg)
12053   "Append the current article to a file.
12054 If N is a positive number, save the N next articles.
12055 If N is a negative number, save the N previous articles.
12056 If N is nil and any articles have been marked with the process mark,
12057 save those articles instead."
12058   (interactive "P")
12059   (require 'gnus-art)
12060   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12061     (gnus-summary-save-article arg)))
12062
12063 (defun gnus-summary-write-article-file (&optional arg)
12064   "Write the current article to a file, deleting the previous file.
12065 If N is a positive number, save the N next articles.
12066 If N is a negative number, save the N previous articles.
12067 If N is nil and any articles have been marked with the process mark,
12068 save those articles instead."
12069   (interactive "P")
12070   (require 'gnus-art)
12071   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12072     (gnus-summary-save-article arg)))
12073
12074 (defun gnus-summary-save-article-body-file (&optional arg)
12075   "Append the current article body to a file.
12076 If N is a positive number, save the N next articles.
12077 If N is a negative number, save the N previous articles.
12078 If N is nil and any articles have been marked with the process mark,
12079 save those articles instead."
12080   (interactive "P")
12081   (require 'gnus-art)
12082   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12083     (gnus-summary-save-article arg)))
12084
12085 (defun gnus-summary-write-article-body-file (&optional arg)
12086   "Write the current article body to a file, deleting the previous file.
12087 If N is a positive number, save the N next articles.
12088 If N is a negative number, save the N previous articles.
12089 If N is nil and any articles have been marked with the process mark,
12090 save those articles instead."
12091   (interactive "P")
12092   (require 'gnus-art)
12093   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12094     (gnus-summary-save-article arg)))
12095
12096 (defun gnus-summary-muttprint (&optional arg)
12097   "Print the current article using Muttprint.
12098 If N is a positive number, save the N next articles.
12099 If N is a negative number, save the N previous articles.
12100 If N is nil and any articles have been marked with the process mark,
12101 save those articles instead."
12102   (interactive "P")
12103   (require 'gnus-art)
12104   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12105     (gnus-summary-save-article arg t)))
12106
12107 (defun gnus-summary-pipe-message (program)
12108   "Pipe the current article through PROGRAM."
12109   (interactive "sProgram: ")
12110   (gnus-summary-select-article)
12111   (let ((mail-header-separator ""))
12112     (gnus-eval-in-buffer-window gnus-article-buffer
12113       (save-restriction
12114         (widen)
12115         (let ((start (window-start))
12116               (inhibit-read-only t))
12117           (message-pipe-buffer-body program)
12118           (set-window-start (get-buffer-window (current-buffer)) start))))))
12119
12120 (defun gnus-get-split-value (methods)
12121   "Return a value based on the split METHODS."
12122   (let (split-name method result match)
12123     (when methods
12124       (with-current-buffer gnus-original-article-buffer
12125         (save-restriction
12126           (nnheader-narrow-to-headers)
12127           (while (and methods (not split-name))
12128             (goto-char (point-min))
12129             (setq method (pop methods))
12130             (setq match (car method))
12131             (when (cond
12132                    ((stringp match)
12133                     ;; Regular expression.
12134                     (ignore-errors
12135                       (re-search-forward match nil t)))
12136                    ((functionp match)
12137                     ;; Function.
12138                     (save-restriction
12139                       (widen)
12140                       (setq result (funcall match gnus-newsgroup-name))))
12141                    ((consp match)
12142                     ;; Form.
12143                     (save-restriction
12144                       (widen)
12145                       (setq result (eval match)))))
12146               (setq split-name (cdr method))
12147               (cond ((stringp result)
12148                      (push (expand-file-name
12149                             result gnus-article-save-directory)
12150                            split-name))
12151                     ((consp result)
12152                      (setq split-name (append result split-name)))))))))
12153     (nreverse split-name)))
12154
12155 (defun gnus-valid-move-group-p (group)
12156   (and (symbolp group)
12157        (boundp group)
12158        (symbol-name group)
12159        (symbol-value group)
12160        (gnus-get-function (gnus-find-method-for-group
12161                            (symbol-name group)) 'request-accept-article t)))
12162
12163 (defun gnus-read-move-group-name (prompt default articles prefix)
12164   "Read a group name."
12165   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12166          (minibuffer-confirm-incomplete nil) ; XEmacs
12167          (prom
12168           (format "%s %s to"
12169                   prompt
12170                   (if (> (length articles) 1)
12171                       (format "these %d articles" (length articles))
12172                     "this article")))
12173          (to-newsgroup
12174           (cond
12175            ((null split-name)
12176             (gnus-group-completing-read
12177              prom
12178              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12179              nil prefix nil default))
12180            ((= 1 (length split-name))
12181             (gnus-group-completing-read
12182              prom
12183              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12184              nil prefix 'gnus-group-history (car split-name)))
12185            (t
12186             (gnus-completing-read
12187              prom (nreverse split-name) nil nil 'gnus-group-history))))
12188          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12189          encoded)
12190     (when to-newsgroup
12191       (if (or (string= to-newsgroup "")
12192               (string= to-newsgroup prefix))
12193           (setq to-newsgroup default))
12194       (unless to-newsgroup
12195         (error "No group name entered"))
12196       (setq encoded (mm-encode-coding-string
12197                      to-newsgroup
12198                      (gnus-group-name-charset to-method to-newsgroup)))
12199       (or (gnus-active encoded)
12200           (gnus-activate-group encoded nil nil to-method)
12201           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12202                                      to-newsgroup))
12203               (or (and (gnus-request-create-group encoded to-method)
12204                        (gnus-activate-group encoded nil nil to-method)
12205                        (gnus-subscribe-group encoded))
12206                   (error "Couldn't create group %s" to-newsgroup)))
12207           (error "No such group: %s" to-newsgroup))
12208       encoded)))
12209
12210 (defvar gnus-summary-save-parts-counter)
12211 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12212
12213 (defun gnus-summary-save-parts (type dir n &optional reverse)
12214   "Save parts matching TYPE to DIR.
12215 If REVERSE, save parts that do not match TYPE."
12216   (interactive
12217    (list (read-string "Save parts of type: "
12218                       (or (car gnus-summary-save-parts-type-history)
12219                           gnus-summary-save-parts-default-mime)
12220                       'gnus-summary-save-parts-type-history)
12221          (setq gnus-summary-save-parts-last-directory
12222                (read-directory-name "Save to directory: "
12223                                     gnus-summary-save-parts-last-directory
12224                                     nil t))
12225          current-prefix-arg))
12226   (gnus-summary-iterate n
12227     (let ((gnus-display-mime-function nil)
12228           gnus-article-prepare-hook
12229           gnus-article-decode-hook
12230           gnus-display-mime-function
12231           gnus-break-pages
12232           (gnus-inhibit-treatment t))
12233       (gnus-summary-select-article))
12234     (with-current-buffer gnus-article-buffer
12235       (let ((handles (or gnus-article-mime-handles
12236                          (mm-dissect-buffer nil gnus-article-loose-mime)
12237                          (and gnus-article-emulate-mime
12238                               (mm-uu-dissect))))
12239             (gnus-summary-save-parts-counter 1))
12240         (when handles
12241           (gnus-summary-save-parts-1 type dir handles reverse)
12242           (unless gnus-article-mime-handles ;; Don't destroy this case.
12243             (mm-destroy-parts handles)))))))
12244
12245 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12246   (if (stringp (car handle))
12247       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12248               (cdr handle))
12249     (when (if reverse
12250               (not (string-match type (mm-handle-media-type handle)))
12251             (string-match type (mm-handle-media-type handle)))
12252       (let ((file (expand-file-name
12253                    (gnus-map-function
12254                     mm-file-name-rewrite-functions
12255                     (file-name-nondirectory
12256                      (or
12257                       (mm-handle-filename handle)
12258                       (format "%s.%d.%d" gnus-newsgroup-name
12259                               (cdr gnus-article-current)
12260                               gnus-summary-save-parts-counter))))
12261                    dir)))
12262         (incf gnus-summary-save-parts-counter)
12263         (unless (file-exists-p file)
12264           (mm-save-part-to-file handle file))))))
12265
12266 ;; Summary extract commands
12267
12268 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12269   (let ((inhibit-read-only t)
12270         (article (gnus-summary-article-number))
12271         after-article b e)
12272     (unless (gnus-summary-goto-subject article)
12273       (error "No such article: %d" article))
12274     (gnus-summary-position-point)
12275     ;; If all commands are to be bunched up on one line, we collect
12276     ;; them here.
12277     (unless gnus-view-pseudos-separately
12278       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12279             files action)
12280         (while ps
12281           (setq action (cdr (assq 'action (car ps))))
12282           (setq files (list (cdr (assq 'name (car ps)))))
12283           (while (and ps (cdr ps)
12284                       (string= (or action "1")
12285                                (or (cdr (assq 'action (cadr ps))) "2")))
12286             (push (cdr (assq 'name (cadr ps))) files)
12287             (setcdr ps (cddr ps)))
12288           (when files
12289             (when (not (string-match "%s" action))
12290               (push " " files))
12291             (push " " files)
12292             (when (assq 'execute (car ps))
12293               (setcdr (assq 'execute (car ps))
12294                       (funcall (if (string-match "%s" action)
12295                                    'format 'concat)
12296                                action
12297                                (mapconcat
12298                                 (lambda (f)
12299                                   (if (equal f " ")
12300                                       f
12301                                     (shell-quote-argument f)))
12302                                 files " ")))))
12303           (setq ps (cdr ps)))))
12304     (if (and gnus-view-pseudos (not not-view))
12305         (while pslist
12306           (when (assq 'execute (car pslist))
12307             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12308                                   (eq gnus-view-pseudos 'not-confirm)))
12309           (setq pslist (cdr pslist)))
12310       (save-excursion
12311         (while pslist
12312           (setq after-article (or (cdr (assq 'article (car pslist)))
12313                                   (gnus-summary-article-number)))
12314           (gnus-summary-goto-subject after-article)
12315           (forward-line 1)
12316           (setq b (point))
12317           (insert "    " (file-name-nondirectory
12318                           (cdr (assq 'name (car pslist))))
12319                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12320           (setq e (point))
12321           (forward-line -1)             ; back to `b'
12322           (gnus-add-text-properties
12323            b (1- e) (list 'gnus-number gnus-reffed-article-number
12324                           gnus-mouse-face-prop gnus-mouse-face))
12325           (gnus-data-enter
12326            after-article gnus-reffed-article-number
12327            gnus-unread-mark b (car pslist) 0 (- e b))
12328           (setq gnus-newsgroup-unreads
12329                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12330                                          gnus-reffed-article-number))
12331           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12332           (setq pslist (cdr pslist)))))))
12333
12334 (defun gnus-pseudos< (p1 p2)
12335   (let ((c1 (cdr (assq 'action p1)))
12336         (c2 (cdr (assq 'action p2))))
12337     (and c1 c2 (string< c1 c2))))
12338
12339 (defun gnus-request-pseudo-article (props)
12340   (cond ((assq 'execute props)
12341          (gnus-execute-command (cdr (assq 'execute props)))))
12342   (let ((gnus-current-article (gnus-summary-article-number)))
12343     (gnus-run-hooks 'gnus-mark-article-hook)))
12344
12345 (defun gnus-execute-command (command &optional automatic)
12346   (save-excursion
12347     (gnus-article-setup-buffer)
12348     (set-buffer gnus-article-buffer)
12349     (setq buffer-read-only nil)
12350     (let ((command (if automatic command
12351                      (read-string "Command: " (cons command 0)))))
12352       (erase-buffer)
12353       (insert "$ " command "\n\n")
12354       (if gnus-view-pseudo-asynchronously
12355           (start-process "gnus-execute" (current-buffer) shell-file-name
12356                          shell-command-switch command)
12357         (call-process shell-file-name nil t nil
12358                       shell-command-switch command)))))
12359
12360 ;; Summary kill commands.
12361
12362 (defun gnus-summary-edit-global-kill (article)
12363   "Edit the \"global\" kill file."
12364   (interactive (list (gnus-summary-article-number)))
12365   (gnus-group-edit-global-kill article))
12366
12367 (defun gnus-summary-edit-local-kill ()
12368   "Edit a local kill file applied to the current newsgroup."
12369   (interactive)
12370   (setq gnus-current-headers (gnus-summary-article-header))
12371   (gnus-group-edit-local-kill
12372    (gnus-summary-article-number) gnus-newsgroup-name))
12373
12374 ;;; Header reading.
12375
12376 (defun gnus-read-header (id &optional header)
12377   "Read the headers of article ID and enter them into the Gnus system."
12378   (let ((group gnus-newsgroup-name)
12379         (gnus-override-method
12380          (or
12381           gnus-override-method
12382           (and (gnus-news-group-p gnus-newsgroup-name)
12383                (car (gnus-refer-article-methods)))))
12384         where)
12385     ;; First we check to see whether the header in question is already
12386     ;; fetched.
12387     (if (stringp id)
12388         ;; This is a Message-ID.
12389         (setq header (or header (gnus-id-to-header id)))
12390       ;; This is an article number.
12391       (setq header (or header (gnus-summary-article-header id))))
12392     (if (and header
12393              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12394         ;; We have found the header.
12395         header
12396       ;; We have to really fetch the header to this article.
12397       (with-current-buffer nntp-server-buffer
12398         (when (setq where (gnus-request-head id group))
12399           (nnheader-fold-continuation-lines)
12400           (goto-char (point-max))
12401           (insert ".\n")
12402           (goto-char (point-min))
12403           (insert "211 ")
12404           (princ (cond
12405                   ((numberp id) id)
12406                   ((cdr where) (cdr where))
12407                   (header (mail-header-number header))
12408                   (t gnus-reffed-article-number))
12409                  (current-buffer))
12410           (insert " Article retrieved.\n"))
12411         (if (or (not where)
12412                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12413             ()                          ; Malformed head.
12414           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12415             (when (and (stringp id)
12416                        (or
12417                         (not (string= (gnus-group-real-name group)
12418                                       (car where)))
12419                         (not (gnus-server-equal gnus-override-method
12420                                                 (gnus-group-method group)))))
12421               ;; If we fetched by Message-ID and the article came from
12422               ;; a different group (or server), we fudge some bogus
12423               ;; article numbers for this article.
12424               (mail-header-set-number header gnus-reffed-article-number))
12425             (with-current-buffer gnus-summary-buffer
12426               (decf gnus-reffed-article-number)
12427               (gnus-remove-header (mail-header-number header))
12428               (push header gnus-newsgroup-headers)
12429               (setq gnus-current-headers header)
12430               (push (mail-header-number header) gnus-newsgroup-limit)))
12431           header)))))
12432
12433 (defun gnus-remove-header (number)
12434   "Remove header NUMBER from `gnus-newsgroup-headers'."
12435   (if (and gnus-newsgroup-headers
12436            (= number (mail-header-number (car gnus-newsgroup-headers))))
12437       (pop gnus-newsgroup-headers)
12438     (let ((headers gnus-newsgroup-headers))
12439       (while (and (cdr headers)
12440                   (not (= number (mail-header-number (cadr headers)))))
12441         (pop headers))
12442       (when (cdr headers)
12443         (setcdr headers (cddr headers))))))
12444
12445 ;;;
12446 ;;; summary highlights
12447 ;;;
12448
12449 (defun gnus-highlight-selected-summary ()
12450   "Highlight selected article in summary buffer."
12451   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12452   (when gnus-summary-selected-face
12453     (save-excursion
12454       (let* ((beg (point-at-bol))
12455              (end (point-at-eol))
12456              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12457              (from (if (get-text-property beg gnus-mouse-face-prop)
12458                        beg
12459                      (or (next-single-property-change
12460                           beg gnus-mouse-face-prop nil end)
12461                          beg)))
12462              (to
12463               (if (= from end)
12464                   (- from 2)
12465                 (or (next-single-property-change
12466                      from gnus-mouse-face-prop nil end)
12467                     end))))
12468         ;; If no mouse-face prop on line we will have to = from = end,
12469         ;; so we highlight the entire line instead.
12470         (when (= (+ to 2) from)
12471           (setq from beg)
12472           (setq to end))
12473         (if gnus-newsgroup-selected-overlay
12474             ;; Move old overlay.
12475             (gnus-move-overlay
12476              gnus-newsgroup-selected-overlay from to (current-buffer))
12477           ;; Create new overlay.
12478           (gnus-overlay-put
12479            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12480            'face gnus-summary-selected-face))))))
12481
12482 (defvar gnus-summary-highlight-line-cached nil)
12483 (defvar gnus-summary-highlight-line-trigger nil)
12484
12485 (defun gnus-summary-highlight-line-0 ()
12486   (if (and (eq gnus-summary-highlight-line-trigger
12487                gnus-summary-highlight)
12488            gnus-summary-highlight-line-cached)
12489       gnus-summary-highlight-line-cached
12490     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12491           gnus-summary-highlight-line-cached
12492           (let* ((cond (list 'cond))
12493                  (c cond)
12494                  (list gnus-summary-highlight))
12495             (while list
12496               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12497                               nil))
12498               (setq c (cdr c)
12499                     list (cdr list)))
12500             (gnus-byte-compile (list 'lambda nil cond))))))
12501
12502 (defun gnus-summary-highlight-line ()
12503   "Highlight current line according to `gnus-summary-highlight'."
12504   (let* ((beg (point-at-bol))
12505          (article (or (gnus-summary-article-number) gnus-current-article))
12506          (score (or (cdr (assq article
12507                                gnus-newsgroup-scored))
12508                     gnus-summary-default-score 0))
12509          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12510          (inhibit-read-only t)
12511          (default gnus-summary-default-score)
12512          (default-high gnus-summary-default-high-score)
12513          (default-low gnus-summary-default-low-score)
12514          (uncached (and gnus-summary-use-undownloaded-faces
12515                         (memq article gnus-newsgroup-undownloaded)
12516                         (not (memq article gnus-newsgroup-cached)))))
12517     (let ((face (funcall (gnus-summary-highlight-line-0))))
12518       (unless (eq face (get-text-property beg 'face))
12519         (gnus-put-text-property-excluding-characters-with-faces
12520          beg (point-at-eol) 'face
12521          (setq face (if (boundp face) (symbol-value face) face)))
12522         (when gnus-summary-highlight-line-function
12523           (funcall gnus-summary-highlight-line-function article face))))))
12524
12525 (defun gnus-update-read-articles (group unread &optional compute)
12526   "Update the list of read articles in GROUP.
12527 UNREAD is a sorted list."
12528   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12529         (info (gnus-get-info group))
12530         (prev 1)
12531         read)
12532     (if (or (not info) (not active))
12533         ;; There is no info on this group if it was, in fact,
12534         ;; killed.  Gnus stores no information on killed groups, so
12535         ;; there's nothing to be done.
12536         ;; One could store the information somewhere temporarily,
12537         ;; perhaps...  Hmmm...
12538         ()
12539       ;; Remove any negative articles numbers.
12540       (while (and unread (< (car unread) 0))
12541         (setq unread (cdr unread)))
12542       ;; Remove any expired article numbers
12543       (while (and unread (< (car unread) (car active)))
12544         (setq unread (cdr unread)))
12545       ;; Compute the ranges of read articles by looking at the list of
12546       ;; unread articles.
12547       (while unread
12548         (when (/= (car unread) prev)
12549           (push (if (= prev (1- (car unread))) prev
12550                   (cons prev (1- (car unread))))
12551                 read))
12552         (setq prev (1+ (car unread)))
12553         (setq unread (cdr unread)))
12554       (when (<= prev (cdr active))
12555         (push (cons prev (cdr active)) read))
12556       (setq read (if (> (length read) 1) (nreverse read) read))
12557       (if compute
12558           read
12559         (save-excursion
12560           (let (setmarkundo)
12561             ;; Propagate the read marks to the backend.
12562             (when (and (or gnus-propagate-marks
12563                            (gnus-method-option-p
12564                             (gnus-find-method-for-group group)
12565                             'server-marks))
12566                        (gnus-check-backend-function 'request-set-mark group))
12567               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12568                     (add (gnus-remove-from-range read (gnus-info-read info))))
12569                 (when (or add del)
12570                   (unless (gnus-check-group group)
12571                     (error "Can't open server for %s" group))
12572                   (gnus-request-set-mark
12573                    group (delq nil (list (if add (list add 'add '(read)))
12574                                          (if del (list del 'del '(read))))))
12575                   (setq setmarkundo
12576                         `(gnus-request-set-mark
12577                           ,group
12578                           ',(delq nil (list
12579                                        (if del (list del 'add '(read)))
12580                                        (if add (list add 'del '(read))))))))))
12581             (set-buffer gnus-group-buffer)
12582             (gnus-undo-register
12583               `(progn
12584                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12585                  (gnus-info-set-read ',info ',(gnus-info-read info))
12586                  (gnus-get-unread-articles-in-group ',info
12587                                                     (gnus-active ,group))
12588                  (gnus-group-update-group ,group t)
12589                  ,setmarkundo))))
12590         ;; Enter this list into the group info.
12591         (gnus-info-set-read info read)
12592         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12593         (gnus-get-unread-articles-in-group info (gnus-active group))
12594         t))))
12595
12596 (defun gnus-offer-save-summaries ()
12597   "Offer to save all active summary buffers."
12598   (let (buffers)
12599     ;; Go through all buffers and find all summaries.
12600     (dolist (buffer (buffer-list))
12601       (when (and (setq buffer (buffer-name buffer))
12602                  (string-match "Summary" buffer)
12603                  (with-current-buffer buffer
12604                    ;; We check that this is, indeed, a summary buffer.
12605                    (and (eq major-mode 'gnus-summary-mode)
12606                         ;; Also make sure this isn't bogus.
12607                         gnus-newsgroup-prepared
12608                         ;; Also make sure that this isn't a
12609                         ;; dead summary buffer.
12610                         (not gnus-dead-summary-mode))))
12611         (push buffer buffers)))
12612     ;; Go through all these summary buffers and offer to save them.
12613     (when buffers
12614       (save-excursion
12615         (if (eq gnus-interactive-exit 'quiet)
12616             (dolist (buffer buffers)
12617               (switch-to-buffer buffer)
12618               (gnus-summary-exit))
12619           (map-y-or-n-p
12620            "Update summary buffer %s? "
12621            (lambda (buf)
12622              (switch-to-buffer buf)
12623              (gnus-summary-exit))
12624            buffers))))))
12625
12626 (defun gnus-summary-setup-default-charset ()
12627   "Setup newsgroup default charset."
12628   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12629       (setq gnus-newsgroup-charset nil)
12630     (let* ((ignored-charsets
12631             (or gnus-newsgroup-ephemeral-ignored-charsets
12632                 (append
12633                  (and gnus-newsgroup-name
12634                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12635                  gnus-newsgroup-ignored-charsets))))
12636       (setq gnus-newsgroup-charset
12637             (or gnus-newsgroup-ephemeral-charset
12638                 (and gnus-newsgroup-name
12639                      (gnus-parameter-charset gnus-newsgroup-name))
12640                 gnus-default-charset))
12641       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12642            ignored-charsets))))
12643
12644 ;;;
12645 ;;; Mime Commands
12646 ;;;
12647
12648 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12649   "Display the current article buffer fully MIME-buttonized.
12650 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12651 treated as multipart/mixed."
12652   (interactive "P")
12653   (require 'gnus-art)
12654   (let ((gnus-unbuttonized-mime-types nil)
12655         (gnus-mime-display-multipart-as-mixed show-all-parts))
12656     (gnus-summary-show-article)))
12657
12658 (defun gnus-summary-repair-multipart (article)
12659   "Add a Content-Type header to a multipart article without one."
12660   (interactive (list (gnus-summary-article-number)))
12661   (gnus-with-article article
12662     (message-narrow-to-head)
12663     (message-remove-header "Mime-Version")
12664     (goto-char (point-max))
12665     (insert "Mime-Version: 1.0\n")
12666     (widen)
12667     (when (search-forward "\n--" nil t)
12668       (let ((separator (buffer-substring (point) (point-at-eol))))
12669         (message-narrow-to-head)
12670         (message-remove-header "Content-Type")
12671         (goto-char (point-max))
12672         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12673                         separator))
12674         (widen))))
12675   (let (gnus-mark-article-hook)
12676     (gnus-summary-select-article t t nil article)))
12677
12678 (defun gnus-summary-toggle-display-buttonized ()
12679   "Toggle the buttonizing of the article buffer."
12680   (interactive)
12681   (require 'gnus-art)
12682   (if (setq gnus-inhibit-mime-unbuttonizing
12683             (not gnus-inhibit-mime-unbuttonizing))
12684       (let ((gnus-unbuttonized-mime-types nil))
12685         (gnus-summary-show-article))
12686     (gnus-summary-show-article)))
12687
12688 ;;;
12689 ;;; Generic summary marking commands
12690 ;;;
12691
12692 (defvar gnus-summary-marking-alist
12693   '((read gnus-del-mark "d")
12694     (unread gnus-unread-mark "u")
12695     (ticked gnus-ticked-mark "!")
12696     (dormant gnus-dormant-mark "?")
12697     (expirable gnus-expirable-mark "e"))
12698   "An alist of names/marks/keystrokes.")
12699
12700 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12701 (defvar gnus-summary-mark-map)
12702
12703 (defun gnus-summary-make-all-marking-commands ()
12704   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12705   (dolist (elem gnus-summary-marking-alist)
12706     (apply 'gnus-summary-make-marking-command elem)))
12707
12708 (defun gnus-summary-make-marking-command (name mark keystroke)
12709   (let ((map (make-sparse-keymap)))
12710     (define-key gnus-summary-generic-mark-map keystroke map)
12711     (dolist (lway `((next "next" next nil "n")
12712                     (next-unread "next unread" next t "N")
12713                     (prev "previous" prev nil "p")
12714                     (prev-unread "previous unread" prev t "P")
12715                     (nomove "" nil nil ,keystroke)))
12716       (let ((func (gnus-summary-make-marking-command-1
12717                    mark (car lway) lway name)))
12718         (setq func (eval func))
12719         (define-key map (nth 4 lway) func)))))
12720
12721 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12722   `(defun ,(intern
12723             (format "gnus-summary-put-mark-as-%s%s"
12724                     name (if (eq way 'nomove)
12725                              ""
12726                            (concat "-" (symbol-name way)))))
12727      (n)
12728      ,(format
12729        "Mark the current article as %s%s.
12730 If N, the prefix, then repeat N times.
12731 If N is negative, move in reverse order.
12732 The difference between N and the actual number of articles marked is
12733 returned."
12734        name (cadr lway))
12735      (interactive "p")
12736      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12737
12738 (defun gnus-summary-generic-mark (n mark move unread)
12739   "Mark N articles with MARK."
12740   (unless (eq major-mode 'gnus-summary-mode)
12741     (error "This command can only be used in the summary buffer"))
12742   (gnus-summary-show-thread)
12743   (let ((nummove
12744          (cond
12745           ((eq move 'next) 1)
12746           ((eq move 'prev) -1)
12747           (t 0))))
12748     (if (zerop nummove)
12749         (setq n 1)
12750       (when (< n 0)
12751         (setq n (abs n)
12752               nummove (* -1 nummove))))
12753     (while (and (> n 0)
12754                 (gnus-summary-mark-article nil mark)
12755                 (zerop (gnus-summary-next-subject nummove unread t)))
12756       (setq n (1- n)))
12757     (when (/= 0 n)
12758       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12759     (gnus-summary-recenter)
12760     (gnus-summary-position-point)
12761     (gnus-set-mode-line 'summary)
12762     n))
12763
12764 (defun gnus-summary-insert-articles (articles)
12765   (when (setq articles
12766               (gnus-sorted-difference articles
12767                                       (mapcar (lambda (h)
12768                                                 (mail-header-number h))
12769                                               gnus-newsgroup-headers)))
12770     (setq gnus-newsgroup-headers
12771           (gnus-merge 'list
12772                       gnus-newsgroup-headers
12773                       (gnus-fetch-headers articles)
12774                       'gnus-article-sort-by-number))
12775     (setq gnus-newsgroup-articles
12776           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12777     ;; Suppress duplicates?
12778     (when gnus-suppress-duplicates
12779       (gnus-dup-suppress-articles))
12780
12781     (if (and gnus-fetch-old-headers
12782              (eq gnus-headers-retrieved-by 'nov))
12783         ;; We might want to build some more threads first.
12784         (if (eq gnus-fetch-old-headers 'invisible)
12785             (gnus-build-all-threads)
12786           (gnus-build-old-threads))
12787       ;; Mark the inserted articles that are unread as unread.
12788       (setq gnus-newsgroup-unreads
12789             (gnus-sorted-nunion
12790              gnus-newsgroup-unreads
12791              (gnus-sorted-nintersection
12792               (gnus-list-of-unread-articles gnus-newsgroup-name)
12793               articles)))
12794       ;; Mark the inserted articles as selected so that the information
12795       ;; of the marks having been changed by a user may be updated when
12796       ;; exiting this group.  See `gnus-summary-update-info'.
12797       (dolist (art articles)
12798         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12799     ;; Let the Gnus agent mark articles as read.
12800     (when gnus-agent
12801       (gnus-agent-get-undownloaded-list))
12802     ;; Remove list identifiers from subject
12803     (gnus-summary-remove-list-identifiers)
12804     ;; First and last article in this newsgroup.
12805     (when gnus-newsgroup-headers
12806       (setq gnus-newsgroup-begin
12807             (mail-header-number (car gnus-newsgroup-headers))
12808             gnus-newsgroup-end
12809             (mail-header-number
12810              (gnus-last-element gnus-newsgroup-headers))))
12811     (when gnus-use-scoring
12812       (gnus-possibly-score-headers))))
12813
12814 (defun gnus-summary-insert-old-articles (&optional all)
12815   "Insert all old articles in this group.
12816 If ALL is non-nil, already read articles become readable.
12817 If ALL is a number, fetch this number of articles."
12818   (interactive "P")
12819   (prog1
12820       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12821             older len)
12822         (setq older
12823               ;; Some nntp servers lie about their active range.  When
12824               ;; this happens, the active range can be in the millions.
12825               ;; Use a compressed range to avoid creating a huge list.
12826               (gnus-range-difference (list gnus-newsgroup-active) old))
12827         (setq len (gnus-range-length older))
12828         (cond
12829          ((null older) nil)
12830          ((numberp all)
12831           (if (< all len)
12832               (let ((older-range (nreverse older)))
12833                 (setq older nil)
12834
12835                 (while (> all 0)
12836                   (let* ((r (pop older-range))
12837                          (min (if (numberp r) r (car r)))
12838                          (max (if (numberp r) r (cdr r))))
12839                     (while (and (<= min max)
12840                                 (> all 0))
12841                       (push max older)
12842                       (setq all (1- all)
12843                             max (1- max))))))
12844             (setq older (gnus-uncompress-range older))))
12845          (all
12846           (setq older (gnus-uncompress-range older)))
12847          (t
12848           (when (and (numberp gnus-large-newsgroup)
12849                    (> len gnus-large-newsgroup))
12850               (let* ((cursor-in-echo-area nil)
12851                      (initial (gnus-parameter-large-newsgroup-initial
12852                                gnus-newsgroup-name))
12853                      (input
12854                       (read-string
12855                        (format
12856                         "How many articles from %s (%s %d): "
12857                         (gnus-group-decoded-name gnus-newsgroup-name)
12858                         (if initial "max" "default")
12859                         len)
12860                        nil nil
12861                        (if initial
12862                            (cons (number-to-string initial)
12863                                  0)))))
12864                 (unless (string-match "^[ \t]*$" input)
12865                   (setq all (string-to-number input))
12866                   (if (< all len)
12867                       (let ((older-range (nreverse older)))
12868                         (setq older nil)
12869
12870                         (while (> all 0)
12871                           (let* ((r (pop older-range))
12872                                  (min (if (numberp r) r (car r)))
12873                                  (max (if (numberp r) r (cdr r))))
12874                             (while (and (<= min max)
12875                                         (> all 0))
12876                               (push max older)
12877                               (setq all (1- all)
12878                                     max (1- max))))))))))
12879           (setq older (gnus-uncompress-range older))))
12880         (if (not older)
12881             (message "No old news.")
12882           (gnus-summary-insert-articles older)
12883           (gnus-summary-limit (gnus-sorted-nunion old older))))
12884     (gnus-summary-position-point)))
12885
12886 (defun gnus-summary-insert-new-articles ()
12887   "Insert all new articles in this group."
12888   (interactive)
12889   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12890         (old-high gnus-newsgroup-highest)
12891         (nnmail-fetched-sources (list t))
12892         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12893         i new)
12894     (unless new-active
12895       (error "Couldn't fetch new data"))
12896     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12897     (setq i (cdr gnus-newsgroup-active)
12898           gnus-newsgroup-highest i)
12899     (while (> i old-high)
12900       (push i new)
12901       (decf i))
12902     (if (not new)
12903         (message "No gnus is bad news")
12904       (gnus-summary-insert-articles new)
12905       (setq gnus-newsgroup-unreads
12906             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12907       (gnus-summary-limit (gnus-sorted-nunion old new))))
12908   (gnus-summary-position-point))
12909
12910 ;;; Bookmark support for Gnus.
12911 (declare-function bookmark-make-record-default
12912                   "bookmark" (&optional no-file no-context posn))
12913 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12914 (declare-function bookmark-default-handler "bookmark" (bmk))
12915 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12916 (defvar bookmark-yank-point)
12917 (defvar bookmark-current-buffer)
12918
12919 (defun gnus-summary-bookmark-make-record ()
12920   "Make a bookmark entry for a Gnus summary buffer."
12921   (let (pos buf)
12922     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12923       (save-restriction              ; FIXME is it necessary to widen?
12924         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12925       (setq buf "art") ; We are recording bookmark from article buffer.
12926       (setq bookmark-yank-point (point))
12927       (setq bookmark-current-buffer (current-buffer))
12928       (gnus-article-show-summary))      ; Go back in summary buffer.
12929     ;; We are now recording bookmark from summary buffer.
12930     (unless buf (setq buf "sum"))
12931     (let* ((subject (elt (gnus-summary-article-header) 1))
12932            (grp     (car gnus-article-current))
12933            (art     (cdr gnus-article-current))
12934            (head    (gnus-summary-article-header art))
12935            (id      (mail-header-id head)))
12936       `(,subject
12937         ,@(condition-case nil
12938               (bookmark-make-record-default 'no-file 'no-context pos)
12939             (wrong-number-of-arguments
12940              (bookmark-make-record-default 'point-only)))
12941         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12942         (group . ,grp) (article . ,art)
12943         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12944
12945 ;;;###autoload
12946 (defun gnus-summary-bookmark-jump (bookmark)
12947   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12948 BOOKMARK is a bookmark name or a bookmark record."
12949   (let ((group    (bookmark-prop-get bookmark 'group))
12950         (article  (bookmark-prop-get bookmark 'article))
12951         (id       (bookmark-prop-get bookmark 'message-id))
12952         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12953     (gnus-fetch-group group (list article))
12954     (gnus-summary-insert-cached-articles)
12955     (gnus-summary-goto-article id nil 'force)
12956     ;; FIXME we have to wait article buffer is ready (only large buffer)
12957     ;; Is there a better solution to know that?
12958     ;; If we don't wait `bookmark-default-handler' will have no chance
12959     ;; to set position. However there is no error, just wrong pos.
12960     (sit-for 1)
12961     (when (string= buf "Gnus-art")
12962       (other-window 1))
12963     (bookmark-default-handler
12964      `(""
12965        (buffer . ,(current-buffer))
12966        . ,(bookmark-get-bookmark-record bookmark)))))
12967
12968 (gnus-summary-make-all-marking-commands)
12969
12970 (gnus-ems-redefine)
12971
12972 (provide 'gnus-sum)
12973
12974 (run-hooks 'gnus-sum-load-hook)
12975
12976 ;; Local Variables:
12977 ;; coding: iso-8859-1
12978 ;; End:
12979
12980 ;;; gnus-sum.el ends here