Merge remote-tracking branch 'origin/no-gnus'
[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   :group 'gnus-thread
126   :type 'boolean)
127
128 (defcustom gnus-summary-make-false-root 'adopt
129   "*nil means that Gnus won't gather loose threads.
130 If the root of a thread has expired or been read in a previous
131 session, the information necessary to build a complete thread has been
132 lost.  Instead of having many small sub-threads from this original thread
133 scattered all over the summary buffer, Gnus can gather them.
134
135 If non-nil, Gnus will try to gather all loose sub-threads from an
136 original thread into one large thread.
137
138 If this variable is non-nil, it should be one of `none', `adopt',
139 `dummy' or `empty'.
140
141 If this variable is `none', Gnus will not make a false root, but just
142 present the sub-threads after another.
143 If this variable is `dummy', Gnus will create a dummy root that will
144 have all the sub-threads as children.
145 If this variable is `adopt', Gnus will make one of the \"children\"
146 the parent and mark all the step-children as such.
147 If this variable is `empty', the \"children\" are printed with empty
148 subject fields.  (Or rather, they will be printed with a string
149 given by the `gnus-summary-same-subject' variable.)"
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const none)
153                  (const dummy)
154                  (const adopt)
155                  (const empty)))
156
157 (defcustom gnus-summary-make-false-root-always nil
158   "Always make a false dummy root."
159   :version "22.1"
160   :group 'gnus-thread
161   :type 'boolean)
162
163 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
164   "*A regexp to match subjects to be excluded from loose thread gathering.
165 As loose thread gathering is done on subjects only, that means that
166 there can be many false gatherings performed.  By rooting out certain
167 common subjects, gathering might become saner."
168   :group 'gnus-thread
169   :type 'regexp)
170
171 (defcustom gnus-summary-gather-subject-limit nil
172   "*Maximum length of subject comparisons when gathering loose threads.
173 Use nil to compare full subjects.  Setting this variable to a low
174 number will help gather threads that have been corrupted by
175 newsreaders chopping off subject lines, but it might also mean that
176 unrelated articles that have subject that happen to begin with the
177 same few characters will be incorrectly gathered.
178
179 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
180 comparing subjects."
181   :group 'gnus-thread
182   :type '(choice (const :tag "off" nil)
183                  (const fuzzy)
184                  (sexp :menu-tag "on" t)))
185
186 (defcustom gnus-simplify-subject-functions nil
187   "List of functions taking a string argument that simplify subjects.
188 The functions are applied recursively.
189
190 Useful functions to put in this list include:
191 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
192 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
193   :group 'gnus-thread
194   :type '(repeat function))
195
196 (defcustom gnus-simplify-ignored-prefixes nil
197   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
198   :group 'gnus-thread
199   :type '(choice (const :tag "off" nil)
200                  regexp))
201
202 (defcustom gnus-build-sparse-threads nil
203   "*If non-nil, fill in the gaps in threads.
204 If `some', only fill in the gaps that are needed to tie loose threads
205 together.  If `more', fill in all leaf nodes that Gnus can find.  If
206 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
207   :group 'gnus-thread
208   :type '(choice (const :tag "off" nil)
209                  (const some)
210                  (const more)
211                  (sexp :menu-tag "all" t)))
212
213 (defcustom gnus-summary-thread-gathering-function
214   'gnus-gather-threads-by-subject
215   "*Function used for gathering loose threads.
216 There are two pre-defined functions: `gnus-gather-threads-by-subject',
217 which only takes Subjects into consideration; and
218 `gnus-gather-threads-by-references', which compared the References
219 headers of the articles to find matches."
220   :group 'gnus-thread
221   :type '(radio (function-item gnus-gather-threads-by-subject)
222                 (function-item gnus-gather-threads-by-references)
223                 (function :tag "other")))
224
225 (defcustom gnus-summary-same-subject ""
226   "*String indicating that the current article has the same subject as the previous.
227 This variable will only be used if the value of
228 `gnus-summary-make-false-root' is `empty'."
229   :group 'gnus-summary-format
230   :type 'string)
231
232 (defcustom gnus-summary-goto-unread nil
233   "*If t, many commands will go to the next unread article.
234 This applies to marking commands as well as other commands that
235 \"naturally\" select the next article, like, for instance, `SPC' at
236 the end of an article.
237
238 If nil, the marking commands do NOT go to the next unread article
239 \(they go to the next article instead).  If `never', commands that
240 usually go to the next unread article, will go to the next article,
241 whether it is read or not."
242   :version "24.1"
243   :group 'gnus-summary-marks
244   :link '(custom-manual "(gnus)Setting Marks")
245   :type '(choice (const :tag "off" nil)
246                  (const never)
247                  (sexp :menu-tag "on" t)))
248
249 (defcustom gnus-summary-default-score 0
250   "*Default article score level.
251 All scores generated by the score files will be added to this score.
252 If this variable is nil, scoring will be disabled."
253   :group 'gnus-score-default
254   :type '(choice (const :tag "disable")
255                  integer))
256
257 (defcustom gnus-summary-default-high-score 0
258   "*Default threshold for a high scored article.
259 An article will be highlighted as high scored if its score is greater
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-default-low-score 0
266   "*Default threshold for a low scored article.
267 An article will be highlighted as low scored if its score is smaller
268 than this score."
269   :version "22.1"
270   :group 'gnus-score-default
271   :type 'integer)
272
273 (defcustom gnus-summary-zcore-fuzz 0
274   "*Fuzziness factor for the zcore in the summary buffer.
275 Articles with scores closer than this to `gnus-summary-default-score'
276 will not be marked."
277   :group 'gnus-summary-format
278   :type 'integer)
279
280 (defcustom gnus-simplify-subject-fuzzy-regexp nil
281   "*Strings to be removed when doing fuzzy matches.
282 This can either be a regular expression or list of regular expressions
283 that will be removed from subject strings if fuzzy subject
284 simplification is selected."
285   :group 'gnus-thread
286   :type '(repeat regexp))
287
288 (defcustom gnus-show-threads t
289   "*If non-nil, display threads in summary mode."
290   :group 'gnus-thread
291   :type 'boolean)
292
293 (defcustom gnus-thread-hide-subtree nil
294   "*If non-nil, hide all threads initially.
295 This can be a predicate specifier which says which threads to hide.
296 If threads are hidden, you have to run the command
297 `gnus-summary-show-thread' by hand or select an article."
298   :group 'gnus-thread
299   :type '(radio (sexp :format "Non-nil\n"
300                       :match (lambda (widget value)
301                                (not (or (consp value) (functionp value))))
302                       :value t)
303                 (const nil)
304                 (sexp :tag "Predicate specifier")))
305
306 (defcustom gnus-thread-hide-killed t
307   "*If non-nil, hide killed threads automatically."
308   :group 'gnus-thread
309   :type 'boolean)
310
311 (defcustom gnus-thread-ignore-subject t
312   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
313 If nil, articles that have different subjects from their parents will
314 start separate threads."
315   :group 'gnus-thread
316   :type 'boolean)
317
318 (defcustom gnus-thread-operation-ignore-subject t
319   "*If non-nil, subjects will be ignored when doing thread commands.
320 This affects commands like `gnus-summary-kill-thread' and
321 `gnus-summary-lower-thread'.
322
323 If this variable is nil, articles in the same thread with different
324 subjects will not be included in the operation in question.  If this
325 variable is `fuzzy', only articles that have subjects that are fuzzily
326 equal will be included."
327   :group 'gnus-thread
328   :type '(choice (const :tag "off" nil)
329                  (const fuzzy)
330                  (sexp :tag "on" t)))
331
332 (defcustom gnus-thread-indent-level 4
333   "*Number that says how much each sub-thread should be indented."
334   :group 'gnus-thread
335   :type 'integer)
336
337 (defcustom gnus-auto-extend-newsgroup t
338   "*If non-nil, extend newsgroup forward and backward when requested."
339   :group 'gnus-summary-choose
340   :type 'boolean)
341
342 (defcustom gnus-auto-select-first t
343   "If non-nil, select an article on group entry.
344 An article is selected automatically when entering a group
345 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
346 `gnus-summary-catchup-and-goto-next-group'.
347
348 Which article is selected is controlled by the variable
349 `gnus-auto-select-subject'.
350
351 If you want to prevent automatic selection of articles in some
352 newsgroups, set the variable to nil in `gnus-select-group-hook'."
353   ;; Commands include...
354   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
355   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
357   :group 'gnus-group-select
358   :type '(choice (const :tag "none" nil)
359                  (sexp :menu-tag "first" t)))
360
361 (defcustom gnus-auto-select-subject 'unseen-or-unread
362   "*Says what subject to place under point when entering a group.
363
364 This variable can either be the symbols `first' (place point on the
365 first subject), `unread' (place point on the subject line of the first
366 unread article), `best' (place point on the subject line of the
367 highest-scored article), `unseen' (place point on the subject line of
368 the first unseen article), `unseen-or-unread' (place point on the subject
369 line of the first unseen article or, if all articles have been seen, on the
370 subject line of the first unread article), or a function to be called to
371 place point on some subject line."
372   :version "24.1"
373   :group 'gnus-group-select
374   :type '(choice (const best)
375                  (const unread)
376                  (const first)
377                  (const unseen)
378                  (const unseen-or-unread)
379                  (function :tag "Function to call")))
380
381 (defcustom gnus-auto-select-next t
382   "*If non-nil, offer to go to the next group from the end of the previous.
383 If the value is t and the next newsgroup is empty, Gnus will exit
384 summary mode and go back to group mode.  If the value is neither nil
385 nor t, Gnus will select the following unread newsgroup.  In
386 particular, if the value is the symbol `quietly', the next unread
387 newsgroup will be selected without any confirmation, and if it is
388 `almost-quietly', the next group will be selected without any
389 confirmation if you are located on the last article in the group.
390 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
391 will go to the next group without confirmation."
392   :group 'gnus-summary-maneuvering
393   :type '(choice (const :tag "off" nil)
394                  (const quietly)
395                  (const almost-quietly)
396                  (const slightly-quietly)
397                  (sexp :menu-tag "on" t)))
398
399 (defcustom gnus-auto-select-same nil
400   "*If non-nil, select the next article with the same subject.
401 If there are no more articles with the same subject, go to
402 the first unread article."
403   :group 'gnus-summary-maneuvering
404   :type 'boolean)
405
406 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
407   "What article should be selected after exiting an ephemeral group.
408 Valid values include:
409
410 `next'
411   Select the next article.
412 `next-unread'
413   Select the next unread article.
414 `next-noselect'
415   Move the cursor to the next article.  This is the default.
416 `next-unread-noselect'
417   Move the cursor to the next unread article.
418
419 If it has any other value or there is no next (unread) article, the
420 article selected before entering to the ephemeral group will appear."
421   :version "23.1" ;; No Gnus
422   :group 'gnus-summary-maneuvering
423   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
424                  (const next) (const next-unread)
425                  (const next-noselect) (const next-unread-noselect)
426                  (sexp :tag "other" :value nil)))
427
428 (defcustom gnus-auto-goto-ignores 'unfetched
429   "*Says how to handle unfetched articles when maneuvering.
430
431 This variable can either be the symbols nil (maneuver to any
432 article), `undownloaded' (maneuvering while unplugged ignores articles
433 that have not been fetched), `always-undownloaded' (maneuvering always
434 ignores articles that have not been fetched), `unfetched' (maneuvering
435 ignores articles whose headers have not been fetched).
436
437 NOTE: The list of unfetched articles will always be nil when plugged
438 and, when unplugged, a subset of the undownloaded article list."
439   :version "22.1"
440   :group 'gnus-summary-maneuvering
441   :type '(choice (const :tag "None" nil)
442                  (const :tag "Undownloaded when unplugged" undownloaded)
443                  (const :tag "Undownloaded" always-undownloaded)
444                  (const :tag "Unfetched" unfetched)))
445
446 (defcustom gnus-summary-check-current nil
447   "*If non-nil, consider the current article when moving.
448 The \"unread\" movement commands will stay on the same line if the
449 current article is unread."
450   :group 'gnus-summary-maneuvering
451   :type 'boolean)
452
453 (defcustom gnus-auto-center-summary 2
454   "*If non-nil, always center the current summary buffer.
455 In particular, if `vertical' do only vertical recentering.  If non-nil
456 and non-`vertical', do both horizontal and vertical recentering."
457   :group 'gnus-summary-maneuvering
458   :type '(choice (const :tag "none" nil)
459                  (const vertical)
460                  (integer :tag "height")
461                  (sexp :menu-tag "both" t)))
462
463 (defcustom gnus-auto-center-group t
464   "If non-nil, always center the group buffer."
465   :group 'gnus-summary-maneuvering
466   :type 'boolean)
467
468 (defcustom gnus-show-all-headers nil
469   "*If non-nil, don't hide any headers."
470   :group 'gnus-article-hiding
471   :group 'gnus-article-headers
472   :type 'boolean)
473
474 (defcustom gnus-summary-ignore-duplicates nil
475   "*If non-nil, ignore articles with identical Message-ID headers."
476   :group 'gnus-summary
477   :type 'boolean)
478
479 (defcustom gnus-single-article-buffer nil
480   "*If non-nil, display all articles in the same buffer.
481 If nil, each group will get its own article buffer."
482   :version "24.1"
483   :group 'gnus-article-various
484   :type 'boolean)
485
486 (defcustom gnus-widen-article-window nil
487   "If non-nil, selecting the article buffer will display only the article buffer."
488   :version "24.1"
489   :group 'gnus-article-various
490   :type 'boolean)
491
492 (defcustom gnus-break-pages t
493   "*If non-nil, do page breaking on articles.
494 The page delimiter is specified by the `gnus-page-delimiter'
495 variable."
496   :group 'gnus-article-various
497   :type 'boolean)
498
499 (defcustom gnus-move-split-methods nil
500   "*Variable used to suggest where articles are to be moved to.
501 It uses the same syntax as the `gnus-split-methods' variable.
502 However, whereas `gnus-split-methods' specifies file names as targets,
503 this variable specifies group names."
504   :group 'gnus-summary-mail
505   :type '(repeat (choice (list :value (fun) function)
506                          (cons :value ("" "") regexp (repeat string))
507                          (sexp :value nil))))
508
509 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
510   "Function used to compute default prefix for article move/copy/etc prompts.
511 The function should take one argument, a group name, and return a
512 string with the suggested prefix."
513   :group 'gnus-summary-mail
514   :type 'function)
515
516 ;; FIXME: Although the custom type is `character' for the following variables,
517 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
518
519 (defcustom gnus-unread-mark ?           ;Whitespace
520   "*Mark used for unread articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-ticked-mark ?!
525   "*Mark used for ticked articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-dormant-mark ??
530   "*Mark used for dormant articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-del-mark ?r
535   "*Mark used for del'd articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-read-mark ?R
540   "*Mark used for read articles."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-expirable-mark ?E
545   "*Mark used for expirable articles."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-killed-mark ?K
550   "*Mark used for killed articles."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-spam-mark ?$
555   "*Mark used for spam articles."
556   :version "22.1"
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-kill-file-mark ?X
561   "*Mark used for articles killed by kill files."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-low-score-mark ?Y
566   "*Mark used for articles with a low score."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-catchup-mark ?C
571   "*Mark used for articles that are caught up."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-replied-mark ?A
576   "*Mark used for articles that have been replied to."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-forwarded-mark ?F
581   "*Mark used for articles that have been forwarded."
582   :version "22.1"
583   :group 'gnus-summary-marks
584   :type 'character)
585
586 (defcustom gnus-recent-mark ?N
587   "*Mark used for articles that are recent."
588   :version "22.1"
589   :group 'gnus-summary-marks
590   :type 'character)
591
592 (defcustom gnus-cached-mark ?*
593   "*Mark used for articles that are in the cache."
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-saved-mark ?S
598   "*Mark used for articles that have been saved."
599   :group 'gnus-summary-marks
600   :type 'character)
601
602 (defcustom gnus-unseen-mark ?.
603   "*Mark used for articles that haven't been seen."
604   :version "22.1"
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-no-mark ?               ;Whitespace
609   "*Mark used for articles that have no other secondary mark."
610   :version "22.1"
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-ancient-mark ?O
615   "*Mark used for ancient articles."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-sparse-mark ?Q
620   "*Mark used for sparsely reffed articles."
621   :group 'gnus-summary-marks
622   :type 'character)
623
624 (defcustom gnus-canceled-mark ?G
625   "*Mark used for canceled articles."
626   :group 'gnus-summary-marks
627   :type 'character)
628
629 (defcustom gnus-duplicate-mark ?M
630   "*Mark used for duplicate articles."
631   :group 'gnus-summary-marks
632   :type 'character)
633
634 (defcustom gnus-undownloaded-mark ?-
635   "*Mark used for articles that weren't downloaded."
636   :version "22.1"
637   :group 'gnus-summary-marks
638   :type 'character)
639
640 (defcustom gnus-downloaded-mark ?+
641   "*Mark used for articles that were downloaded."
642   :group 'gnus-summary-marks
643   :type 'character)
644
645 (defcustom gnus-downloadable-mark ?%
646   "*Mark used for articles that are to be downloaded."
647   :group 'gnus-summary-marks
648   :type 'character)
649
650 (defcustom gnus-unsendable-mark ?=
651   "*Mark used for articles that won't be sent."
652   :group 'gnus-summary-marks
653   :type 'character)
654
655 (defcustom gnus-score-over-mark ?+
656   "*Score mark used for articles with high scores."
657   :group 'gnus-summary-marks
658   :type 'character)
659
660 (defcustom gnus-score-below-mark ?-
661   "*Score mark used for articles with low scores."
662   :group 'gnus-summary-marks
663   :type 'character)
664
665 (defcustom gnus-empty-thread-mark ?     ;Whitespace
666   "*There is no thread under the article."
667   :group 'gnus-summary-marks
668   :type 'character)
669
670 (defcustom gnus-not-empty-thread-mark ?=
671   "*There is a thread under the article."
672   :group 'gnus-summary-marks
673   :type 'character)
674
675 (defcustom gnus-view-pseudo-asynchronously nil
676   "*If non-nil, Gnus will view pseudo-articles asynchronously."
677   :group 'gnus-extract-view
678   :type 'boolean)
679
680 (defcustom gnus-auto-expirable-marks
681   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
682         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
683         gnus-duplicate-mark)
684   "*The list of marks converted into expiration if a group is auto-expirable."
685   :version "24.1"
686   :group 'gnus-summary
687   :type '(repeat character))
688
689 (defcustom gnus-inhibit-user-auto-expire t
690   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
691   :version "21.1"
692   :group 'gnus-summary
693   :type 'boolean)
694
695 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
696   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
697 If nil, the expirable marks will be unchanged except that the marks
698 will be removed when copying or moving articles to a group that has
699 not turned auto-expire on.  If non-nil, articles that have been read
700 will be marked as expirable when being copied or moved to a group in
701 which auto-expire is turned on."
702   :version "23.2"
703   :type 'boolean
704   :group 'gnus-summary-marks)
705
706 (defcustom gnus-view-pseudos nil
707   "*If `automatic', pseudo-articles will be viewed automatically.
708 If `not-confirm', pseudos will be viewed automatically, and the user
709 will not be asked to confirm the command."
710   :group 'gnus-extract-view
711   :type '(choice (const :tag "off" nil)
712                  (const automatic)
713                  (const not-confirm)))
714
715 (defcustom gnus-view-pseudos-separately t
716   "*If non-nil, one pseudo-article will be created for each file to be viewed.
717 If nil, all files that use the same viewing command will be given as a
718 list of parameters to that command."
719   :group 'gnus-extract-view
720   :type 'boolean)
721
722 (defcustom gnus-insert-pseudo-articles t
723   "*If non-nil, insert pseudo-articles when decoding articles."
724   :group 'gnus-extract-view
725   :type 'boolean)
726
727 (defcustom gnus-summary-dummy-line-format
728   "   %(:                             :%) %S\n"
729   "*The format specification for the dummy roots in the summary buffer.
730 It works along the same lines as a normal formatting string,
731 with some simple extensions.
732
733 %S  The subject
734
735 General format specifiers can also be used.
736 See `(gnus)Formatting Variables'."
737   :link '(custom-manual "(gnus)Formatting Variables")
738   :group 'gnus-threading
739   :type 'string)
740
741 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
742   "*The format specification for the summary mode line.
743 It works along the same lines as a normal formatting string,
744 with some simple extensions:
745
746 %G  Group name
747 %p  Unprefixed group name
748 %A  Current article number
749 %z  Current article score
750 %V  Gnus version
751 %U  Number of unread articles in the group
752 %e  Number of unselected articles in the group
753 %Z  A string with unread/unselected article counts
754 %g  Shortish group name
755 %S  Subject of the current article
756 %u  User-defined spec
757 %s  Current score file name
758 %d  Number of dormant articles
759 %r  Number of articles that have been marked as read in this session
760 %E  Number of articles expunged by the score files"
761   :group 'gnus-summary-format
762   :type 'string)
763
764 (defcustom gnus-list-identifiers nil
765   "Regexp that matches list identifiers to be removed from subject.
766 This can also be a list of regexps."
767   :version "21.1"
768   :group 'gnus-summary-format
769   :group 'gnus-article-hiding
770   :type '(choice (const :tag "none" nil)
771                  (regexp :value ".*")
772                  (repeat :value (".*") regexp)))
773
774 (defcustom gnus-summary-mark-below 0
775   "*Mark all articles with a score below this variable as read.
776 This variable is local to each summary buffer and usually set by the
777 score file."
778   :group 'gnus-score-default
779   :type 'integer)
780
781 (defun gnus-widget-reversible-match (widget value)
782   "Ignoring WIDGET, convert VALUE to internal form.
783 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
784   ;; (debug value)
785   (or (symbolp value)
786       (and (listp value)
787            (eq (length value) 2)
788            (eq (nth 0 value) 'not)
789            (symbolp (nth 1 value)))))
790
791 (defun gnus-widget-reversible-to-internal (widget value)
792   "Ignoring WIDGET, convert VALUE to internal form.
793 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
794 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
795   ;; (debug value)
796   (if (atom value)
797       (list value nil)
798     (list (nth 1 value) t)))
799
800 (defun gnus-widget-reversible-to-external (widget value)
801   "Ignoring WIDGET, convert VALUE to external form.
802 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
803 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
804   ;; (debug value)
805   (if (nth 1 value)
806       (list 'not (nth 0 value))
807     (nth 0 value)))
808
809 (define-widget 'gnus-widget-reversible 'group
810   "A `group' that convert values."
811   :match 'gnus-widget-reversible-match
812   :value-to-internal 'gnus-widget-reversible-to-internal
813   :value-to-external 'gnus-widget-reversible-to-external)
814
815 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
816   "*List of functions used for sorting articles in the summary buffer.
817
818 Each function takes two articles and returns non-nil if the first
819 article should be sorted before the other.  If you use more than one
820 function, the primary sort function should be the last.  You should
821 probably always include `gnus-article-sort-by-number' in the list of
822 sorting functions -- preferably first.  Also note that sorting by date
823 is often much slower than sorting by number, and the sorting order is
824 very similar.  (Sorting by date means sorting by the time the message
825 was sent, sorting by number means sorting by arrival time.)
826
827 Each item can also be a list `(not F)' where F is a function;
828 this reverses the sort order.
829
830 Ready-made functions include `gnus-article-sort-by-number',
831 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
832 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
833 and `gnus-article-sort-by-score'.
834
835 When threading is turned on, the variable `gnus-thread-sort-functions'
836 controls how articles are sorted."
837   :group 'gnus-summary-sort
838   :type '(repeat (gnus-widget-reversible
839                   (choice (function-item gnus-article-sort-by-number)
840                           (function-item gnus-article-sort-by-author)
841                           (function-item gnus-article-sort-by-subject)
842                           (function-item gnus-article-sort-by-date)
843                           (function-item gnus-article-sort-by-score)
844                           (function-item gnus-article-sort-by-random)
845                           (function :tag "other"))
846                   (boolean :tag "Reverse order"))))
847
848
849 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
850   "*List of functions used for sorting threads in the summary buffer.
851 By default, threads are sorted by article number.
852
853 Each function takes two threads and returns non-nil if the first
854 thread should be sorted before the other.  If you use more than one
855 function, the primary sort function should be the last.  You should
856 probably always include `gnus-thread-sort-by-number' in the list of
857 sorting functions -- preferably first.  Also note that sorting by date
858 is often much slower than sorting by number, and the sorting order is
859 very similar.  (Sorting by date means sorting by the time the message
860 was sent, sorting by number means sorting by arrival time.)
861
862 Each list item can also be a list `(not F)' where F is a
863 function; this specifies reversed sort order.
864
865 Ready-made functions include `gnus-thread-sort-by-number',
866 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
867 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
868 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
869 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
870 and `gnus-thread-sort-by-total-score' (see
871 `gnus-thread-score-function').
872
873 When threading is turned off, the variable
874 `gnus-article-sort-functions' controls how articles are sorted."
875   :group 'gnus-summary-sort
876   :type '(repeat
877           (gnus-widget-reversible
878            (choice (function-item gnus-thread-sort-by-number)
879                    (function-item gnus-thread-sort-by-author)
880                    (function-item gnus-thread-sort-by-recipient)
881                    (function-item gnus-thread-sort-by-subject)
882                    (function-item gnus-thread-sort-by-date)
883                    (function-item gnus-thread-sort-by-score)
884                    (function-item gnus-thread-sort-by-most-recent-number)
885                    (function-item gnus-thread-sort-by-most-recent-date)
886                    (function-item gnus-thread-sort-by-random)
887                    (function-item gnus-thread-sort-by-total-score)
888                    (function :tag "other"))
889            (boolean :tag "Reverse order"))))
890
891 (defcustom gnus-thread-score-function '+
892   "*Function used for calculating the total score of a thread.
893
894 The function is called with the scores of the article and each
895 subthread and should then return the score of the thread.
896
897 Some functions you can use are `+', `max', or `min'."
898   :group 'gnus-summary-sort
899   :type 'function)
900
901 (defcustom gnus-summary-expunge-below nil
902   "All articles that have a score less than this variable will be expunged.
903 This variable is local to the summary buffers."
904   :group 'gnus-score-default
905   :type '(choice (const :tag "off" nil)
906                  integer))
907
908 (defcustom gnus-thread-expunge-below nil
909   "All threads that have a total score less than this variable will be expunged.
910 See `gnus-thread-score-function' for en explanation of what a
911 \"thread score\" is.
912
913 This variable is local to the summary buffers."
914   :group 'gnus-threading
915   :group 'gnus-score-default
916   :type '(choice (const :tag "off" nil)
917                  integer))
918
919 (defcustom gnus-summary-mode-hook nil
920   "*A hook for Gnus summary mode.
921 This hook is run before any variables are set in the summary buffer."
922   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
923   :group 'gnus-summary-various
924   :type 'hook)
925
926 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
927 (when (featurep 'xemacs)
928   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
929   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
930   (add-hook 'gnus-summary-mode-hook
931             'gnus-xmas-switch-horizontal-scrollbar-off))
932
933 (defcustom gnus-summary-menu-hook nil
934   "*Hook run after the creation of the summary mode menu."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-summary-exit-hook nil
939   "*A hook called on exit from the summary buffer.
940 It will be called with point in the group buffer."
941   :group 'gnus-summary-exit
942   :type 'hook)
943
944 (defcustom gnus-summary-prepare-hook nil
945   "*A hook called after the summary buffer has been generated.
946 If you want to modify the summary buffer, you can use this hook."
947   :group 'gnus-summary-various
948   :type 'hook)
949
950 (defcustom gnus-summary-prepared-hook nil
951   "*A hook called as the last thing after the summary buffer has been generated."
952   :group 'gnus-summary-various
953   :type 'hook)
954
955 (defcustom gnus-summary-generate-hook nil
956   "*A hook run just before generating the summary buffer.
957 This hook is commonly used to customize threading variables and the
958 like."
959   :group 'gnus-summary-various
960   :type 'hook)
961
962 (defcustom gnus-select-group-hook nil
963   "*A hook called when a newsgroup is selected.
964
965 If you'd like to simplify subjects like the
966 `gnus-summary-next-same-subject' command does, you can use the
967 following hook:
968
969  (add-hook gnus-select-group-hook
970            (lambda ()
971              (mapcar (lambda (header)
972                        (mail-header-set-subject
973                         header
974                         (gnus-simplify-subject
975                          (mail-header-subject header) 're-only)))
976                      gnus-newsgroup-headers)))"
977   :group 'gnus-group-select
978   :type 'hook)
979
980 (defcustom gnus-select-article-hook nil
981   "*A hook called when an article is selected."
982   :group 'gnus-summary-choose
983   :options '(gnus-agent-fetch-selected-article)
984   :type 'hook)
985
986 (defcustom gnus-visual-mark-article-hook
987   (list 'gnus-highlight-selected-summary)
988   "*Hook run after selecting an article in the summary buffer.
989 It is meant to be used for highlighting the article in some way.  It
990 is not run if `gnus-visual' is nil."
991   :group 'gnus-summary-visual
992   :type 'hook)
993
994 (defcustom gnus-parse-headers-hook nil
995   "*A hook called before parsing the headers."
996   :group 'gnus-various
997   :type 'hook)
998
999 (defcustom gnus-exit-group-hook nil
1000   "*A hook called when exiting summary mode.
1001 This hook is not called from the non-updating exit commands like `Q'."
1002   :group 'gnus-various
1003   :type 'hook)
1004
1005 (defcustom gnus-summary-update-hook nil
1006   "*A hook called when a summary line is changed.
1007 The hook will not be called if `gnus-visual' is nil.
1008
1009 The default function `gnus-summary-highlight-line' will
1010 highlight the line according to the `gnus-summary-highlight'
1011 variable."
1012   :group 'gnus-summary-visual
1013   :type 'hook)
1014
1015 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1016   "*A hook called when an article is selected for the first time.
1017 The hook is intended to mark an article as read (or unread)
1018 automatically when it is selected."
1019   :group 'gnus-summary-choose
1020   :type 'hook)
1021
1022 (defcustom gnus-group-no-more-groups-hook nil
1023   "*A hook run when returning to group mode having no more (unread) groups."
1024   :group 'gnus-group-select
1025   :type 'hook)
1026
1027 (defcustom gnus-ps-print-hook nil
1028   "*A hook run before ps-printing something from Gnus."
1029   :group 'gnus-summary
1030   :type 'hook)
1031
1032 (defcustom gnus-summary-article-move-hook nil
1033   "*A hook called after an article is moved, copied, respooled, or crossposted."
1034   :version "22.1"
1035   :group 'gnus-summary
1036   :type 'hook)
1037
1038 (defcustom gnus-summary-article-delete-hook nil
1039   "*A hook called after an article is deleted."
1040   :version "22.1"
1041   :group 'gnus-summary
1042   :type 'hook)
1043
1044 (defcustom gnus-summary-article-expire-hook nil
1045   "*A hook called after an article is expired."
1046   :version "22.1"
1047   :group 'gnus-summary
1048   :type 'hook)
1049
1050 (defcustom gnus-summary-display-arrow
1051   (and (fboundp 'display-graphic-p)
1052        (display-graphic-p))
1053   "*If non-nil, display an arrow highlighting the current article."
1054   :version "22.1"
1055   :group 'gnus-summary
1056   :type 'boolean)
1057
1058 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1059   "Face used for highlighting the current article in the summary buffer."
1060   :group 'gnus-summary-visual
1061   :type 'face)
1062
1063 (defvar gnus-tmp-downloaded nil)
1064
1065 (defcustom gnus-summary-highlight
1066   '(((eq mark gnus-canceled-mark)
1067      . gnus-summary-cancelled)
1068     ((and uncached (> score default-high))
1069      . gnus-summary-high-undownloaded)
1070     ((and uncached (< score default-low))
1071      . gnus-summary-low-undownloaded)
1072     (uncached
1073      . gnus-summary-normal-undownloaded)
1074     ((and (> score default-high)
1075           (or (eq mark gnus-dormant-mark)
1076               (eq mark gnus-ticked-mark)))
1077      . gnus-summary-high-ticked)
1078     ((and (< score default-low)
1079           (or (eq mark gnus-dormant-mark)
1080               (eq mark gnus-ticked-mark)))
1081      . gnus-summary-low-ticked)
1082     ((or (eq mark gnus-dormant-mark)
1083          (eq mark gnus-ticked-mark))
1084      . gnus-summary-normal-ticked)
1085     ((and (> score default-high) (eq mark gnus-ancient-mark))
1086      . gnus-summary-high-ancient)
1087     ((and (< score default-low) (eq mark gnus-ancient-mark))
1088      . gnus-summary-low-ancient)
1089     ((eq mark gnus-ancient-mark)
1090      . gnus-summary-normal-ancient)
1091     ((and (> score default-high) (eq mark gnus-unread-mark))
1092      . gnus-summary-high-unread)
1093     ((and (< score default-low) (eq mark gnus-unread-mark))
1094      . gnus-summary-low-unread)
1095     ((eq mark gnus-unread-mark)
1096      . gnus-summary-normal-unread)
1097     ((> score default-high)
1098      . gnus-summary-high-read)
1099     ((< score default-low)
1100      . gnus-summary-low-read)
1101     (t
1102      . gnus-summary-normal-read))
1103   "*Controls the highlighting of summary buffer lines.
1104
1105 A list of (FORM . FACE) pairs.  When deciding how a particular
1106 summary line should be displayed, each form is evaluated.  The content
1107 of the face field after the first true form is used.  You can change
1108 how those summary lines are displayed, by editing the face field.
1109
1110 You can use the following variables in the FORM field.
1111
1112 score:        The article's score.
1113 default:      The default article score.
1114 default-high: The default score for high scored articles.
1115 default-low:  The default score for low scored articles.
1116 below:        The score below which articles are automatically marked as read.
1117 mark:         The article's mark.
1118 uncached:     Non-nil if the article is uncached."
1119   :group 'gnus-summary-visual
1120   :type '(repeat (cons (sexp :tag "Form" nil)
1121                        face)))
1122 (put 'gnus-summary-highlight 'risky-local-variable t)
1123
1124 (defcustom gnus-alter-header-function nil
1125   "Function called to allow alteration of article header structures.
1126 The function is called with one parameter, the article header vector,
1127 which it may alter in any way."
1128   :type '(choice (const :tag "None" nil)
1129                  function)
1130   :group 'gnus-summary)
1131
1132 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1133   "Function used to decode a string with encoded words.")
1134
1135 (defvar gnus-decode-encoded-address-function
1136   'mail-decode-encoded-address-string
1137   "Function used to decode addresses with encoded words.")
1138
1139 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1140   "*Extra headers to parse."
1141   :version "24.1"                       ; added Cc Keywords Gcc
1142   :group 'gnus-summary
1143   :type '(repeat symbol))
1144
1145 (defcustom gnus-ignored-from-addresses
1146   (and user-mail-address
1147        (not (string= user-mail-address ""))
1148        (regexp-quote user-mail-address))
1149   "*From headers that may be suppressed in favor of To headers.
1150 This can be a regexp or a list of regexps."
1151   :version "21.1"
1152   :group 'gnus-summary
1153   :type '(choice regexp
1154                  (repeat :tag "Regexp List" regexp)))
1155
1156 (defsubst gnus-ignored-from-addresses ()
1157   (gmm-regexp-concat gnus-ignored-from-addresses))
1158
1159 (defcustom gnus-summary-to-prefix "-> "
1160   "*String prefixed to the To field in the summary line when
1161 using `gnus-ignored-from-addresses'."
1162   :version "22.1"
1163   :group 'gnus-summary
1164   :type 'string)
1165
1166 (defcustom gnus-summary-newsgroup-prefix "=> "
1167   "*String prefixed to the Newsgroup field in the summary
1168 line when using `gnus-ignored-from-addresses'."
1169   :version "22.1"
1170   :group 'gnus-summary
1171   :type 'string)
1172
1173 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1174   "List of charsets that should be ignored.
1175 When these charsets are used in the \"charset\" parameter, the
1176 default charset will be used instead."
1177   :version "21.1"
1178   :type '(repeat symbol)
1179   :group 'gnus-charset)
1180
1181 (defcustom gnus-newsgroup-maximum-articles nil
1182   "The maximum number of articles a newsgroup.
1183 If this is a number, old articles in a newsgroup exceeding this number
1184 are silently ignored.  If it is nil, no article is ignored.  Note that
1185 setting this variable to a number might prevent you from reading very
1186 old articles."
1187   :group 'gnus-group-select
1188   :version "22.2"
1189   :type '(choice (const :tag "No limit" nil)
1190                  integer))
1191
1192 (gnus-define-group-parameter
1193  ignored-charsets
1194  :type list
1195  :function-document
1196  "Return the ignored charsets of GROUP."
1197  :variable gnus-group-ignored-charsets-alist
1198  :variable-default
1199  '(("alt\\.chinese\\.text" iso-8859-1))
1200  :variable-document
1201  "Alist of regexps (to match group names) and charsets that should be ignored.
1202 When these charsets are used in the \"charset\" parameter, the
1203 default charset will be used instead."
1204  :variable-group gnus-charset
1205  :variable-type '(repeat (cons (regexp :tag "Group")
1206                                (repeat symbol)))
1207  :parameter-type '(choice :tag "Ignored charsets"
1208                           :value nil
1209                           (repeat (symbol)))
1210  :parameter-document       "\
1211 List of charsets that should be ignored.
1212
1213 When these charsets are used in the \"charset\" parameter, the
1214 default charset will be used instead.")
1215
1216 (defcustom gnus-group-highlight-words-alist nil
1217   "Alist of group regexps and highlight regexps.
1218 This variable uses the same syntax as `gnus-emphasis-alist'."
1219   :version "21.1"
1220   :type '(repeat (cons (regexp :tag "Group")
1221                        (repeat (list (regexp :tag "Highlight regexp")
1222                                      (number :tag "Group for entire word" 0)
1223                                      (number :tag "Group for displayed part" 0)
1224                                      (symbol :tag "Face"
1225                                              gnus-emphasis-highlight-words)))))
1226   :group 'gnus-summary-visual)
1227
1228 (defcustom gnus-summary-show-article-charset-alist
1229   nil
1230   "Alist of number and charset.
1231 The article will be shown with the charset corresponding to the
1232 numbered argument.
1233 For example: ((1 . cn-gb-2312) (2 . big5))."
1234   :version "21.1"
1235   :type '(repeat (cons (number :tag "Argument" 1)
1236                        (symbol :tag "Charset")))
1237   :group 'gnus-charset)
1238
1239 (defcustom gnus-preserve-marks t
1240   "Whether marks are preserved when moving, copying and respooling messages."
1241   :version "21.1"
1242   :type 'boolean
1243   :group 'gnus-summary-marks)
1244
1245 (defcustom gnus-propagate-marks nil
1246   "If non-nil, Gnus will store and retrieve marks from the backends.
1247 This means that marks will be stored both in .newsrc.eld and in
1248 the backend, and will slow operation down somewhat."
1249   :type 'boolean
1250   :group 'gnus-summary-marks)
1251
1252 (defcustom gnus-alter-articles-to-read-function nil
1253   "Function to be called to alter the list of articles to be selected."
1254   :type '(choice (const nil) function)
1255   :group 'gnus-summary)
1256
1257 (defcustom gnus-orphan-score nil
1258   "*All orphans get this score added.  Set in the score file."
1259   :group 'gnus-score-default
1260   :type '(choice (const nil)
1261                  integer))
1262
1263 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1264   "*A regexp to match MIME parts when saving multiple parts of a
1265 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1266 This regexp will be used by default when prompting the user for which
1267 type of files to save."
1268   :group 'gnus-summary
1269   :type 'regexp)
1270
1271 (defcustom gnus-read-all-available-headers nil
1272   "Whether Gnus should parse all headers made available to it.
1273 This is mostly relevant for slow back ends where the user may
1274 wish to widen the summary buffer to include all headers
1275 that were fetched."
1276   :version "22.1"
1277   :group 'gnus-summary
1278   :type '(choice boolean regexp))
1279
1280 (defcustom gnus-summary-pipe-output-default-command nil
1281   "Command (and optional arguments) used to pipe article to subprocess.
1282 This will be used as the default command if it is non-nil.  The value
1283 will be updated if you modify it when executing the command
1284 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1285   :version "23.1" ;; No Gnus
1286   :group 'gnus-summary
1287   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1288
1289 (defcustom gnus-summary-muttprint-program "muttprint"
1290   "Command (and optional arguments) used to run Muttprint.
1291 The value will be updated if you modify it when executing the command
1292 `gnus-summary-muttprint'."
1293   :version "22.1"
1294   :group 'gnus-summary
1295   :type 'string)
1296
1297 (defcustom gnus-article-loose-mime t
1298   "If non-nil, don't require MIME-Version header.
1299 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1300 supply the MIME-Version header or deliberately strip it from the mail.
1301 If non-nil (the default), Gnus will treat some articles as MIME
1302 even if the MIME-Version header is missing."
1303   :version "22.1"
1304   :type 'boolean
1305   :group 'gnus-article-mime)
1306
1307 (defcustom gnus-article-emulate-mime t
1308   "If non-nil, use MIME emulation for uuencode and the like.
1309 This means that Gnus will search message bodies for text that look
1310 like uuencoded bits, yEncoded bits, and so on, and present that using
1311 the normal Gnus MIME machinery."
1312   :version "22.1"
1313   :type 'boolean
1314   :group 'gnus-article-mime)
1315
1316 ;;; Internal variables
1317
1318 (defvar gnus-summary-display-cache nil)
1319 (defvar gnus-article-mime-handles nil)
1320 (defvar gnus-article-decoded-p nil)
1321 (defvar gnus-article-charset nil)
1322 (defvar gnus-article-ignored-charsets nil)
1323 (defvar gnus-scores-exclude-files nil)
1324 (defvar gnus-page-broken nil)
1325
1326 (defvar gnus-original-article nil)
1327 (defvar gnus-article-internal-prepare-hook nil)
1328 (defvar gnus-newsgroup-process-stack nil)
1329
1330 (defvar gnus-thread-indent-array nil)
1331 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1332 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1333   "Function called to sort the articles within a thread after it has been gathered together.")
1334
1335 (defvar gnus-summary-save-parts-type-history nil)
1336 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1337
1338 ;; Avoid highlighting in kill files.
1339 (defvar gnus-summary-inhibit-highlight nil)
1340 (defvar gnus-newsgroup-selected-overlay nil)
1341 (defvar gnus-inhibit-limiting nil)
1342 (defvar gnus-newsgroup-adaptive-score-file nil)
1343 (defvar gnus-current-score-file nil)
1344 (defvar gnus-current-move-group nil)
1345 (defvar gnus-current-copy-group nil)
1346 (defvar gnus-current-crosspost-group nil)
1347 (defvar gnus-newsgroup-display nil)
1348
1349 (defvar gnus-newsgroup-dependencies nil)
1350 (defvar gnus-newsgroup-adaptive nil)
1351 (defvar gnus-summary-display-article-function nil)
1352 (defvar gnus-summary-highlight-line-function nil
1353   "Function called after highlighting a summary line.")
1354
1355 (defvar gnus-summary-line-format-alist
1356   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1357     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1358     (?s gnus-tmp-subject-or-nil ?s)
1359     (?n gnus-tmp-name ?s)
1360     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1361         ?s)
1362     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1363             gnus-tmp-from) ?s)
1364     (?F gnus-tmp-from ?s)
1365     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1366     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1367     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1368     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1369     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1370     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1371     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1372     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1373     (?L gnus-tmp-lines ?s)
1374     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1375             0) ?d)
1376     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1377             "") ?s)
1378     (?g (or (gnus-group-short-name
1379              (nnir-article-group (mail-header-number gnus-tmp-header)))
1380             "") ?s)
1381     (?O gnus-tmp-downloaded ?c)
1382     (?I gnus-tmp-indentation ?s)
1383     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1384     (?R gnus-tmp-replied ?c)
1385     (?\[ gnus-tmp-opening-bracket ?c)
1386     (?\] gnus-tmp-closing-bracket ?c)
1387     (?\> (make-string gnus-tmp-level ? ) ?s)
1388     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1389     (?i gnus-tmp-score ?d)
1390     (?z gnus-tmp-score-char ?c)
1391     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1392     (?U gnus-tmp-unread ?c)
1393     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1394         ?s)
1395     (?t (gnus-summary-number-of-articles-in-thread
1396          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1397         ?d)
1398     (?e (gnus-summary-number-of-articles-in-thread
1399          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1400         ?c)
1401     (?u gnus-tmp-user-defined ?s)
1402     (?P (gnus-pick-line-number) ?d)
1403     (?B gnus-tmp-thread-tree-header-string ?s)
1404     (user-date (gnus-user-date
1405                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1406   "An alist of format specifications that can appear in summary lines.
1407 These are paired with what variables they correspond with, along with
1408 the type of the variable (string, integer, character, etc).")
1409
1410 (defvar gnus-summary-dummy-line-format-alist
1411   `((?S gnus-tmp-subject ?s)
1412     (?N gnus-tmp-number ?d)
1413     (?u gnus-tmp-user-defined ?s)))
1414
1415 (defvar gnus-summary-mode-line-format-alist
1416   `((?G gnus-tmp-group-name ?s)
1417     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1418     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1419     (?A gnus-tmp-article-number ?d)
1420     (?Z gnus-tmp-unread-and-unselected ?s)
1421     (?V gnus-version ?s)
1422     (?U gnus-tmp-unread-and-unticked ?d)
1423     (?S gnus-tmp-subject ?s)
1424     (?e gnus-tmp-unselected ?d)
1425     (?u gnus-tmp-user-defined ?s)
1426     (?d (length gnus-newsgroup-dormant) ?d)
1427     (?t (length gnus-newsgroup-marked) ?d)
1428     (?h (length gnus-newsgroup-spam-marked) ?d)
1429     (?r (length gnus-newsgroup-reads) ?d)
1430     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1431     (?E gnus-newsgroup-expunged-tally ?d)
1432     (?s (gnus-current-score-file-nondirectory) ?s)))
1433
1434 ;; This is here rather than in gnus-art for compilation reasons.
1435 (defvar gnus-article-mode-line-format-alist
1436   (nconc '((?w (gnus-article-wash-status) ?s)
1437            (?m (gnus-article-mime-part-status) ?s))
1438          gnus-summary-mode-line-format-alist))
1439
1440 (defvar gnus-last-search-regexp nil
1441   "Default regexp for article search command.")
1442
1443 (defvar gnus-last-shell-command nil
1444   "Default shell command on article.")
1445
1446 (defvar gnus-newsgroup-agentized nil
1447   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1448 (defvar gnus-newsgroup-begin nil)
1449 (defvar gnus-newsgroup-end nil)
1450 (defvar gnus-newsgroup-last-rmail nil)
1451 (defvar gnus-newsgroup-last-mail nil)
1452 (defvar gnus-newsgroup-last-folder nil)
1453 (defvar gnus-newsgroup-last-file nil)
1454 (defvar gnus-newsgroup-last-directory nil)
1455 (defvar gnus-newsgroup-auto-expire nil)
1456 (defvar gnus-newsgroup-active nil)
1457 (defvar gnus-newsgroup-highest nil)
1458
1459 (defvar gnus-newsgroup-data nil)
1460 (defvar gnus-newsgroup-data-reverse nil)
1461 (defvar gnus-newsgroup-limit nil)
1462 (defvar gnus-newsgroup-limits nil)
1463 (defvar gnus-summary-use-undownloaded-faces nil)
1464
1465 (defvar gnus-newsgroup-unreads nil
1466   "Sorted list of unread articles in the current newsgroup.")
1467
1468 (defvar gnus-newsgroup-unselected nil
1469   "Sorted list of unselected unread articles in the current newsgroup.")
1470
1471 (defvar gnus-newsgroup-reads nil
1472   "Alist of read articles and article marks in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-expunged-tally nil)
1475
1476 (defvar gnus-newsgroup-marked nil
1477   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1478
1479 (defvar gnus-newsgroup-spam-marked nil
1480   "List of ranges of articles that have been marked as spam.")
1481
1482 (defvar gnus-newsgroup-killed nil
1483   "List of ranges of articles that have been through the scoring process.")
1484
1485 (defvar gnus-newsgroup-cached nil
1486   "Sorted list of articles that come from the article cache.")
1487
1488 (defvar gnus-newsgroup-saved nil
1489   "List of articles that have been saved.")
1490
1491 (defvar gnus-newsgroup-kill-headers nil)
1492
1493 (defvar gnus-newsgroup-replied nil
1494   "List of articles that have been replied to in the current newsgroup.")
1495
1496 (defvar gnus-newsgroup-forwarded nil
1497   "List of articles that have been forwarded in the current newsgroup.")
1498
1499 (defvar gnus-newsgroup-expirable nil
1500   "Sorted list of articles in the current newsgroup that can be expired.")
1501
1502 (defvar gnus-newsgroup-processable nil
1503   "List of articles in the current newsgroup that can be processed.")
1504
1505 (defvar gnus-newsgroup-downloadable nil
1506   "Sorted list of articles in the current newsgroup that can be processed.")
1507
1508 (defvar gnus-newsgroup-unfetched nil
1509   "Sorted list of articles in the current newsgroup whose headers have
1510 not been fetched into the agent.
1511
1512 This list will always be a subset of gnus-newsgroup-undownloaded.")
1513
1514 (defvar gnus-newsgroup-undownloaded nil
1515   "List of articles in the current newsgroup that haven't been downloaded.")
1516
1517 (defvar gnus-newsgroup-unsendable nil
1518   "List of articles in the current newsgroup that won't be sent.")
1519
1520 (defvar gnus-newsgroup-bookmarks nil
1521   "List of articles in the current newsgroup that have bookmarks.")
1522
1523 (defvar gnus-newsgroup-dormant nil
1524   "Sorted list of dormant articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-unseen nil
1527   "List of unseen articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-seen nil
1530   "Range of seen articles in the current newsgroup.")
1531
1532 (defvar gnus-newsgroup-articles nil
1533   "List of articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-scored nil
1536   "List of scored articles in the current newsgroup.")
1537
1538 (defvar gnus-newsgroup-headers nil
1539   "List of article headers in the current newsgroup.")
1540
1541 (defvar gnus-newsgroup-threads nil)
1542
1543 (defvar gnus-newsgroup-prepared nil
1544   "Whether the current group has been prepared properly.")
1545
1546 (defvar gnus-newsgroup-ancient nil
1547   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1548
1549 (defvar gnus-newsgroup-sparse nil)
1550
1551 (defvar gnus-current-article nil)
1552 (defvar gnus-article-current nil)
1553 (defvar gnus-current-headers nil)
1554 (defvar gnus-have-all-headers nil)
1555 (defvar gnus-last-article nil)
1556 (defvar gnus-newsgroup-history nil)
1557 (defvar gnus-newsgroup-charset nil)
1558 (defvar gnus-newsgroup-ephemeral-charset nil)
1559 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1560
1561 (defvar gnus-article-before-search nil)
1562
1563 (defvar gnus-summary-local-variables
1564   '(gnus-newsgroup-name
1565
1566     ;; Marks lists
1567     gnus-newsgroup-unreads
1568     gnus-newsgroup-unselected
1569     gnus-newsgroup-marked
1570     gnus-newsgroup-spam-marked
1571     gnus-newsgroup-reads
1572     gnus-newsgroup-saved
1573     gnus-newsgroup-replied
1574     gnus-newsgroup-forwarded
1575     gnus-newsgroup-expirable
1576     gnus-newsgroup-killed
1577     gnus-newsgroup-unseen
1578     gnus-newsgroup-seen
1579     gnus-newsgroup-cached
1580     gnus-newsgroup-downloadable
1581     gnus-newsgroup-undownloaded
1582     gnus-newsgroup-unsendable
1583
1584     gnus-newsgroup-begin gnus-newsgroup-end
1585     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1586     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1587     gnus-newsgroup-last-directory
1588     gnus-newsgroup-auto-expire
1589     gnus-newsgroup-processable
1590     gnus-newsgroup-unfetched
1591     gnus-newsgroup-articles
1592     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1593     gnus-newsgroup-headers gnus-newsgroup-threads
1594     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1595     gnus-current-article gnus-current-headers gnus-have-all-headers
1596     gnus-last-article gnus-article-internal-prepare-hook
1597     (gnus-summary-article-delete-hook . global)
1598     (gnus-summary-article-move-hook . global)
1599     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1600     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1601     gnus-thread-expunge-below
1602     gnus-score-alist gnus-current-score-file
1603     (gnus-summary-expunge-below . global)
1604     (gnus-summary-mark-below . global)
1605     (gnus-orphan-score . global)
1606     gnus-newsgroup-active gnus-scores-exclude-files
1607     gnus-newsgroup-highest
1608     gnus-newsgroup-history gnus-newsgroup-ancient
1609     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1610     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1611     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1612     (gnus-newsgroup-expunged-tally . 0)
1613     gnus-cache-removable-articles
1614     gnus-newsgroup-data gnus-newsgroup-data-reverse
1615     gnus-newsgroup-limit gnus-newsgroup-limits
1616     gnus-newsgroup-charset gnus-newsgroup-display
1617     gnus-summary-use-undownloaded-faces)
1618   "Variables that are buffer-local to the summary buffers.")
1619
1620 (defvar gnus-newsgroup-variables nil
1621   "A list of variables that have separate values in different newsgroups.
1622 A list of newsgroup (summary buffer) local variables, or cons of
1623 variables and their default expressions to be evalled (when the default
1624 values are not nil), that should be made global while the summary buffer
1625 is active.
1626
1627 Note: The default expressions will be evaluated (using function `eval')
1628 before assignment to the local variable rather than just assigned to it.
1629 If the default expression is the symbol `global', that symbol will not
1630 be evaluated but the global value of the local variable will be used
1631 instead.
1632
1633 These variables can be used to set variables in the group parameters
1634 while still allowing them to affect operations done in other buffers.
1635 For example:
1636
1637 \(setq gnus-newsgroup-variables
1638      '(message-use-followup-to
1639        (gnus-visible-headers .
1640          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1641 ")
1642
1643 (eval-when-compile
1644   ;; Bind features so that require will believe that gnus-sum has
1645   ;; already been loaded (avoids infinite recursion)
1646   (let ((features (cons 'gnus-sum features)))
1647     (require 'gnus-art)))
1648
1649 ;; MIME stuff.
1650
1651 (defvar gnus-decode-encoded-word-methods
1652   '(mail-decode-encoded-word-string)
1653   "List of methods used to decode encoded words.
1654
1655 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1656 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1657 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1658 whose names match REGEXP.
1659
1660 For example:
1661 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1662  mail-decode-encoded-word-string
1663  (\"chinese\" . rfc1843-decode-string))")
1664
1665 (defvar gnus-decode-encoded-word-methods-cache nil)
1666
1667 (defun gnus-multi-decode-encoded-word-string (string)
1668   "Apply the functions from `gnus-encoded-word-methods' that match."
1669   (unless (and gnus-decode-encoded-word-methods-cache
1670                (eq gnus-newsgroup-name
1671                    (car gnus-decode-encoded-word-methods-cache)))
1672     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1673     (dolist (method gnus-decode-encoded-word-methods)
1674       (if (symbolp method)
1675           (nconc gnus-decode-encoded-word-methods-cache (list method))
1676         (if (and gnus-newsgroup-name
1677                  (string-match (car method) gnus-newsgroup-name))
1678             (nconc gnus-decode-encoded-word-methods-cache
1679                    (list (cdr method)))))))
1680   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1681     (setq string (funcall method string))))
1682
1683 ;; Subject simplification.
1684
1685 (defun gnus-simplify-whitespace (str)
1686   "Remove excessive whitespace from STR."
1687   ;; Multiple spaces.
1688   (while (string-match "[ \t][ \t]+" str)
1689     (setq str (concat (substring str 0 (match-beginning 0))
1690                         " "
1691                         (substring str (match-end 0)))))
1692   ;; Leading spaces.
1693   (when (string-match "^[ \t]+" str)
1694     (setq str (substring str (match-end 0))))
1695   ;; Trailing spaces.
1696   (when (string-match "[ \t]+$" str)
1697     (setq str (substring str 0 (match-beginning 0))))
1698   str)
1699
1700 (defun gnus-simplify-all-whitespace (str)
1701   "Remove all whitespace from STR."
1702   (while (string-match "[ \t\n]+" str)
1703     (setq str (replace-match "" nil nil str)))
1704   str)
1705
1706 (defsubst gnus-simplify-subject-re (subject)
1707   "Remove \"Re:\" from subject lines."
1708   (if (string-match message-subject-re-regexp subject)
1709       (substring subject (match-end 0))
1710     subject))
1711
1712 (defun gnus-simplify-subject (subject &optional re-only)
1713   "Remove `Re:' and words in parentheses.
1714 If RE-ONLY is non-nil, strip leading `Re:'s only."
1715   (let ((case-fold-search t))           ;Ignore case.
1716     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1717     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1718       (setq subject (substring subject (match-end 0))))
1719     ;; Remove uninteresting prefixes.
1720     (when (and (not re-only)
1721                gnus-simplify-ignored-prefixes
1722                (string-match gnus-simplify-ignored-prefixes subject))
1723       (setq subject (substring subject (match-end 0))))
1724     ;; Remove words in parentheses from end.
1725     (unless re-only
1726       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1727         (setq subject (substring subject 0 (match-beginning 0)))))
1728     ;; Return subject string.
1729     subject))
1730
1731 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1732 ;; all whitespace.
1733 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1734   (goto-char (point-min))
1735   (while (re-search-forward regexp nil t)
1736     (replace-match (or newtext ""))))
1737
1738 (defun gnus-simplify-buffer-fuzzy (regexp)
1739   "Simplify string in the buffer fuzzily.
1740 The string in the accessible portion of the current buffer is simplified.
1741 It is assumed to be a single-line subject.
1742 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1743 matter is removed.  Additional things can be deleted by setting
1744 `gnus-simplify-subject-fuzzy-regexp'."
1745   (let ((case-fold-search t)
1746         (modified-tick))
1747     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1748
1749     (while (not (eq modified-tick (buffer-modified-tick)))
1750       (setq modified-tick (buffer-modified-tick))
1751       (cond
1752        ((listp regexp)
1753         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1754        (regexp
1755         (gnus-simplify-buffer-fuzzy-step regexp)))
1756       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1757       (gnus-simplify-buffer-fuzzy-step
1758        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1759       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1760
1761     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1762     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1763     (gnus-simplify-buffer-fuzzy-step " $")
1764     (gnus-simplify-buffer-fuzzy-step "^ +")))
1765
1766 (defun gnus-simplify-subject-fuzzy (subject)
1767   "Simplify a subject string fuzzily.
1768 See `gnus-simplify-buffer-fuzzy' for details."
1769   (save-excursion
1770     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1771       (gnus-set-work-buffer)
1772       (let ((case-fold-search t))
1773         ;; Remove uninteresting prefixes.
1774         (when (and gnus-simplify-ignored-prefixes
1775                    (string-match gnus-simplify-ignored-prefixes subject))
1776           (setq subject (substring subject (match-end 0))))
1777         (insert subject)
1778         (inline (gnus-simplify-buffer-fuzzy regexp))
1779         (buffer-string)))))
1780
1781 (defsubst gnus-simplify-subject-fully (subject)
1782   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1783   (cond
1784    (gnus-simplify-subject-functions
1785     (gnus-map-function gnus-simplify-subject-functions subject))
1786    ((null gnus-summary-gather-subject-limit)
1787     (gnus-simplify-subject-re subject))
1788    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1789     (gnus-simplify-subject-fuzzy subject))
1790    ((numberp gnus-summary-gather-subject-limit)
1791     (truncate-string-to-width (gnus-simplify-subject-re subject)
1792                               gnus-summary-gather-subject-limit))
1793    (t
1794     subject)))
1795
1796 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1797   "Check whether two subjects are equal.
1798 If optional argument SIMPLE-FIRST is t, first argument is already
1799 simplified."
1800   (cond
1801    ((null simple-first)
1802     (equal (gnus-simplify-subject-fully s1)
1803            (gnus-simplify-subject-fully s2)))
1804    (t
1805     (equal s1
1806            (gnus-simplify-subject-fully s2)))))
1807
1808 (defun gnus-summary-bubble-group ()
1809   "Increase the score of the current group.
1810 This is a handy function to add to `gnus-summary-exit-hook' to
1811 increase the score of each group you read."
1812   (gnus-group-add-score gnus-newsgroup-name))
1813
1814 \f
1815 ;;;
1816 ;;; Gnus summary mode
1817 ;;;
1818
1819 (put 'gnus-summary-mode 'mode-class 'special)
1820
1821 (defvar gnus-article-commands-menu)
1822
1823 ;; Non-orthogonal keys
1824
1825 (gnus-define-keys gnus-summary-mode-map
1826   " " gnus-summary-next-page
1827   "\177" gnus-summary-prev-page
1828   [delete] gnus-summary-prev-page
1829   [backspace] gnus-summary-prev-page
1830   "\r" gnus-summary-scroll-up
1831   "\M-\r" gnus-summary-scroll-down
1832   "n" gnus-summary-next-unread-article
1833   "p" gnus-summary-prev-unread-article
1834   "N" gnus-summary-next-article
1835   "P" gnus-summary-prev-article
1836   "\M-\C-n" gnus-summary-next-same-subject
1837   "\M-\C-p" gnus-summary-prev-same-subject
1838   "\M-n" gnus-summary-next-unread-subject
1839   "\M-p" gnus-summary-prev-unread-subject
1840   "." gnus-summary-first-unread-article
1841   "," gnus-summary-best-unread-article
1842   "\M-s" gnus-summary-search-article-forward
1843   "\M-r" gnus-summary-search-article-backward
1844   "\M-S" gnus-summary-repeat-search-article-forward
1845   "\M-R" gnus-summary-repeat-search-article-backward
1846   "<" gnus-summary-beginning-of-article
1847   ">" gnus-summary-end-of-article
1848   "j" gnus-summary-goto-article
1849   "^" gnus-summary-refer-parent-article
1850   "\M-^" gnus-summary-refer-article
1851   "u" gnus-summary-tick-article-forward
1852   "!" gnus-summary-tick-article-forward
1853   "U" gnus-summary-tick-article-backward
1854   "d" gnus-summary-mark-as-read-forward
1855   "D" gnus-summary-mark-as-read-backward
1856   "E" gnus-summary-mark-as-expirable
1857   "\M-u" gnus-summary-clear-mark-forward
1858   "\M-U" gnus-summary-clear-mark-backward
1859   "k" gnus-summary-kill-same-subject-and-select
1860   "\C-k" gnus-summary-kill-same-subject
1861   "\M-\C-k" gnus-summary-kill-thread
1862   "\M-\C-l" gnus-summary-lower-thread
1863   "e" gnus-summary-edit-article
1864   "#" gnus-summary-mark-as-processable
1865   "\M-#" gnus-summary-unmark-as-processable
1866   "\M-\C-t" gnus-summary-toggle-threads
1867   "\M-\C-s" gnus-summary-show-thread
1868   "\M-\C-h" gnus-summary-hide-thread
1869   "\M-\C-f" gnus-summary-next-thread
1870   "\M-\C-b" gnus-summary-prev-thread
1871   [(meta down)] gnus-summary-next-thread
1872   [(meta up)] gnus-summary-prev-thread
1873   "\M-\C-u" gnus-summary-up-thread
1874   "\M-\C-d" gnus-summary-down-thread
1875   "&" gnus-summary-execute-command
1876   "c" gnus-summary-catchup-and-exit
1877   "\C-w" gnus-summary-mark-region-as-read
1878   "\C-t" gnus-summary-toggle-truncation
1879   "?" gnus-summary-mark-as-dormant
1880   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1881   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1882   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1883   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1884   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1885   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1886   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1887   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1888   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1889   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1890   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1891   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1892   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1893   "=" gnus-summary-expand-window
1894   "\C-x\C-s" gnus-summary-reselect-current-group
1895   "\M-g" gnus-summary-rescan-group
1896   "\C-c\C-r" gnus-summary-caesar-message
1897   "f" gnus-summary-followup
1898   "F" gnus-summary-followup-with-original
1899   "C" gnus-summary-cancel-article
1900   "r" gnus-summary-reply
1901   "R" gnus-summary-reply-with-original
1902   "\C-c\C-f" gnus-summary-mail-forward
1903   "o" gnus-summary-save-article
1904   "\C-o" gnus-summary-save-article-mail
1905   "|" gnus-summary-pipe-output
1906   "\M-k" gnus-summary-edit-local-kill
1907   "\M-K" gnus-summary-edit-global-kill
1908   ;; "V" gnus-version
1909   "\C-c\C-d" gnus-summary-describe-group
1910   "q" gnus-summary-exit
1911   "Q" gnus-summary-exit-no-update
1912   "\C-c\C-i" gnus-info-find-node
1913   gnus-mouse-2 gnus-mouse-pick-article
1914   [follow-link] mouse-face
1915   "m" gnus-summary-mail-other-window
1916   "a" gnus-summary-post-news
1917   "x" gnus-summary-limit-to-unread
1918   "s" gnus-summary-isearch-article
1919   [tab] gnus-summary-widget-forward
1920   [backtab] gnus-summary-widget-backward
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   [backtab] gnus-summary-widget-forward
2086   "P" gnus-summary-print-article
2087   "S" gnus-sticky-article
2088   "M" gnus-mailing-list-insinuate
2089   "t" gnus-article-babel)
2090
2091 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2092   "b" gnus-article-add-buttons
2093   "B" gnus-article-add-buttons-to-head
2094   "o" gnus-article-treat-overstrike
2095   "e" gnus-article-emphasize
2096   "w" gnus-article-fill-cited-article
2097   "Q" gnus-article-fill-long-lines
2098   "L" gnus-article-toggle-truncate-lines
2099   "C" gnus-article-capitalize-sentences
2100   "c" gnus-article-remove-cr
2101   "q" gnus-article-de-quoted-unreadable
2102   "6" gnus-article-de-base64-unreadable
2103   "Z" gnus-article-decode-HZ
2104   "A" gnus-article-treat-ansi-sequences
2105   "h" gnus-article-wash-html
2106   "u" gnus-article-unsplit-urls
2107   "s" gnus-summary-force-verify-and-decrypt
2108   "f" gnus-article-display-x-face
2109   "l" gnus-summary-stop-page-breaking
2110   "r" gnus-summary-caesar-message
2111   "m" gnus-summary-morse-message
2112   "t" gnus-summary-toggle-header
2113   "g" gnus-treat-smiley
2114   "v" gnus-summary-verbose-headers
2115   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2116   "p" gnus-article-verify-x-pgp-sig
2117   "d" gnus-article-treat-dumbquotes
2118   "U" gnus-article-treat-non-ascii
2119   "i" gnus-summary-idna-message)
2120
2121 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2122   ;; mnemonic: deuglif*Y*
2123   "u" gnus-article-outlook-unwrap-lines
2124   "a" gnus-article-outlook-repair-attribution
2125   "c" gnus-article-outlook-rearrange-citation
2126   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2127
2128 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2129   "a" gnus-article-hide
2130   "h" gnus-article-hide-headers
2131   "b" gnus-article-hide-boring-headers
2132   "s" gnus-article-hide-signature
2133   "c" gnus-article-hide-citation
2134   "C" gnus-article-hide-citation-in-followups
2135   "l" gnus-article-hide-list-identifiers
2136   "B" gnus-article-strip-banner
2137   "P" gnus-article-hide-pem
2138   "\C-c" gnus-article-hide-citation-maybe)
2139
2140 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2141   "a" gnus-article-highlight
2142   "h" gnus-article-highlight-headers
2143   "c" gnus-article-highlight-citation
2144   "s" gnus-article-highlight-signature)
2145
2146 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2147   "f" gnus-article-treat-fold-headers
2148   "u" gnus-article-treat-unfold-headers
2149   "n" gnus-article-treat-fold-newsgroups)
2150
2151 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2152   "x" gnus-article-display-x-face
2153   "d" gnus-article-display-face
2154   "s" gnus-treat-smiley
2155   "D" gnus-article-remove-images
2156   "W" gnus-article-show-images
2157   "f" gnus-treat-from-picon
2158   "m" gnus-treat-mail-picon
2159   "n" gnus-treat-newsgroups-picon
2160   "g" gnus-treat-from-gravatar
2161   "h" gnus-treat-mail-gravatar)
2162
2163 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2164   "w" gnus-article-decode-mime-words
2165   "c" gnus-article-decode-charset
2166   "v" gnus-mime-view-all-parts
2167   "b" gnus-article-view-part)
2168
2169 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2170   "z" gnus-article-date-ut
2171   "u" gnus-article-date-ut
2172   "l" gnus-article-date-local
2173   "p" gnus-article-date-english
2174   "e" gnus-article-date-lapsed
2175   "o" gnus-article-date-original
2176   "i" gnus-article-date-iso8601
2177   "s" gnus-article-date-user)
2178
2179 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2180   "t" gnus-article-remove-trailing-blank-lines
2181   "l" gnus-article-strip-leading-blank-lines
2182   "m" gnus-article-strip-multiple-blank-lines
2183   "a" gnus-article-strip-blank-lines
2184   "A" gnus-article-strip-all-blank-lines
2185   "s" gnus-article-strip-leading-space
2186   "e" gnus-article-strip-trailing-space
2187   "w" gnus-article-remove-leading-whitespace)
2188
2189 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2190   "v" gnus-version
2191   "d" gnus-summary-describe-group
2192   "h" gnus-summary-describe-briefly
2193   "i" gnus-info-find-node)
2194
2195 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2196   "e" gnus-summary-expire-articles
2197   "\M-\C-e" gnus-summary-expire-articles-now
2198   "\177" gnus-summary-delete-article
2199   [delete] gnus-summary-delete-article
2200   [backspace] gnus-summary-delete-article
2201   "m" gnus-summary-move-article
2202   "r" gnus-summary-respool-article
2203   "w" gnus-summary-edit-article
2204   "c" gnus-summary-copy-article
2205   "B" gnus-summary-crosspost-article
2206   "q" gnus-summary-respool-query
2207   "t" gnus-summary-respool-trace
2208   "i" gnus-summary-import-article
2209   "I" gnus-summary-create-article
2210   "p" gnus-summary-article-posted-p)
2211
2212 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2213   "o" gnus-summary-save-article
2214   "m" gnus-summary-save-article-mail
2215   "F" gnus-summary-write-article-file
2216   "r" gnus-summary-save-article-rmail
2217   "f" gnus-summary-save-article-file
2218   "b" gnus-summary-save-article-body-file
2219   "B" gnus-summary-write-article-body-file
2220   "h" gnus-summary-save-article-folder
2221   "v" gnus-summary-save-article-vm
2222   "p" gnus-summary-pipe-output
2223   "P" gnus-summary-muttprint)
2224
2225 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2226   "b" gnus-summary-display-buttonized
2227   "m" gnus-summary-repair-multipart
2228   "v" gnus-article-view-part
2229   "o" gnus-article-save-part
2230   "O" gnus-article-save-part-and-strip
2231   "r" gnus-article-replace-part
2232   "d" gnus-article-delete-part
2233   "t" gnus-article-view-part-as-type
2234   "j" gnus-article-jump-to-part
2235   "c" gnus-article-copy-part
2236   "C" gnus-article-view-part-as-charset
2237   "e" gnus-article-view-part-externally
2238   "H" gnus-article-browse-html-article
2239   "E" gnus-article-encrypt-body
2240   "i" gnus-article-inline-part
2241   "|" gnus-article-pipe-part)
2242
2243 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2244   "p" gnus-summary-mark-as-processable
2245   "u" gnus-summary-unmark-as-processable
2246   "U" gnus-summary-unmark-all-processable
2247   "v" gnus-uu-mark-over
2248   "s" gnus-uu-mark-series
2249   "r" gnus-uu-mark-region
2250   "g" gnus-uu-unmark-region
2251   "R" gnus-uu-mark-by-regexp
2252   "G" gnus-uu-unmark-by-regexp
2253   "t" gnus-uu-mark-thread
2254   "T" gnus-uu-unmark-thread
2255   "a" gnus-uu-mark-all
2256   "b" gnus-uu-mark-buffer
2257   "S" gnus-uu-mark-sparse
2258   "k" gnus-summary-kill-process-mark
2259   "y" gnus-summary-yank-process-mark
2260   "w" gnus-summary-save-process-mark
2261   "i" gnus-uu-invert-processable)
2262
2263 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2264   ;;"x" gnus-uu-extract-any
2265   "m" gnus-summary-save-parts
2266   "u" gnus-uu-decode-uu
2267   "U" gnus-uu-decode-uu-and-save
2268   "s" gnus-uu-decode-unshar
2269   "S" gnus-uu-decode-unshar-and-save
2270   "o" gnus-uu-decode-save
2271   "O" gnus-uu-decode-save
2272   "b" gnus-uu-decode-binhex
2273   "B" gnus-uu-decode-binhex
2274   "Y" gnus-uu-decode-yenc
2275   "p" gnus-uu-decode-postscript
2276   "P" gnus-uu-decode-postscript-and-save)
2277
2278 (gnus-define-keys
2279     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2280   "u" gnus-uu-decode-uu-view
2281   "U" gnus-uu-decode-uu-and-save-view
2282   "s" gnus-uu-decode-unshar-view
2283   "S" gnus-uu-decode-unshar-and-save-view
2284   "o" gnus-uu-decode-save-view
2285   "O" gnus-uu-decode-save-view
2286   "b" gnus-uu-decode-binhex-view
2287   "B" gnus-uu-decode-binhex-view
2288   "p" gnus-uu-decode-postscript-view
2289   "P" gnus-uu-decode-postscript-and-save-view)
2290
2291 (defvar gnus-article-post-menu nil)
2292
2293 (defconst gnus-summary-menu-maxlen 20)
2294
2295 (defun gnus-summary-menu-split (menu)
2296   ;; If we have lots of elements, divide them into groups of 20
2297   ;; and make a pane (or submenu) for each one.
2298   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2299       (let ((menu menu) sublists next
2300             (i 1))
2301         (while menu
2302           ;; Pull off the next gnus-summary-menu-maxlen elements
2303           ;; and make them the next element of sublist.
2304           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2305           (if next
2306               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2307                       nil))
2308           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2309                                              (aref (car (last menu)) 0)) menu)
2310                                sublists))
2311           (setq i (1+ i))
2312           (setq menu next))
2313         (nreverse sublists))
2314     ;; Few elements--put them all in one pane.
2315     menu))
2316
2317 (defun gnus-summary-make-menu-bar ()
2318   (gnus-turn-off-edit-menu 'summary)
2319
2320   (unless (boundp 'gnus-summary-misc-menu)
2321
2322     (easy-menu-define
2323       gnus-summary-kill-menu gnus-summary-mode-map ""
2324       (cons
2325        "Score"
2326        (nconc
2327         (list
2328          ["Customize" gnus-score-customize t])
2329         (gnus-make-score-map 'increase)
2330         (gnus-make-score-map 'lower)
2331         '(("Mark"
2332            ["Kill below" gnus-summary-kill-below t]
2333            ["Mark above" gnus-summary-mark-above t]
2334            ["Tick above" gnus-summary-tick-above t]
2335            ["Clear above" gnus-summary-clear-above t])
2336           ["Current score" gnus-summary-current-score t]
2337           ["Set score" gnus-summary-set-score t]
2338           ["Switch current score file..." gnus-score-change-score-file t]
2339           ["Set mark below..." gnus-score-set-mark-below t]
2340           ["Set expunge below..." gnus-score-set-expunge-below t]
2341           ["Edit current score file" gnus-score-edit-current-scores t]
2342           ["Edit score file..." gnus-score-edit-file t]
2343           ["Trace score" gnus-score-find-trace t]
2344           ["Find words" gnus-score-find-favourite-words t]
2345           ["Rescore buffer" gnus-summary-rescore t]
2346           ["Increase score..." gnus-summary-increase-score t]
2347           ["Lower score..." gnus-summary-lower-score t]))))
2348
2349     ;; Define both the Article menu in the summary buffer and the
2350     ;; equivalent Commands menu in the article buffer here for
2351     ;; consistency.
2352     (let ((innards
2353            `(("Hide"
2354               ["All" gnus-article-hide t]
2355               ["Headers" gnus-article-hide-headers t]
2356               ["Signature" gnus-article-hide-signature t]
2357               ["Citation" gnus-article-hide-citation t]
2358               ["List identifiers" gnus-article-hide-list-identifiers t]
2359               ["Banner" gnus-article-strip-banner t]
2360               ["Boring headers" gnus-article-hide-boring-headers t])
2361              ("Highlight"
2362               ["All" gnus-article-highlight t]
2363               ["Headers" gnus-article-highlight-headers t]
2364               ["Signature" gnus-article-highlight-signature t]
2365               ["Citation" gnus-article-highlight-citation t])
2366              ("MIME"
2367               ["Words" gnus-article-decode-mime-words t]
2368               ["Charset" gnus-article-decode-charset t]
2369               ["QP" gnus-article-de-quoted-unreadable t]
2370               ["Base64" gnus-article-de-base64-unreadable t]
2371               ["View MIME buttons" gnus-summary-display-buttonized t]
2372               ["View all" gnus-mime-view-all-parts t]
2373               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2374               ["Encrypt body" gnus-article-encrypt-body
2375                :active (not (gnus-group-read-only-p))
2376                ,@(if (featurep 'xemacs) nil
2377                    '(:help "Encrypt the message body on disk"))]
2378               ["Extract all parts..." gnus-summary-save-parts t]
2379               ("Multipart"
2380                ["Repair multipart" gnus-summary-repair-multipart t]
2381                ["Pipe part..." gnus-article-pipe-part t]
2382                ["Inline part" gnus-article-inline-part t]
2383                ["View part as type..." gnus-article-view-part-as-type t]
2384                ["Encrypt body" gnus-article-encrypt-body
2385                 :active (not (gnus-group-read-only-p))
2386                ,@(if (featurep 'xemacs) nil
2387                    '(:help "Encrypt the message body on disk"))]
2388                ["View part externally" gnus-article-view-part-externally t]
2389                ["View HTML parts in browser" gnus-article-browse-html-article t]
2390                ["View part with charset..." gnus-article-view-part-as-charset t]
2391                ["Copy part" gnus-article-copy-part t]
2392                ["Save part..." gnus-article-save-part t]
2393                ["View part" gnus-article-view-part t]))
2394              ("Date"
2395               ["Local" gnus-article-date-local t]
2396               ["ISO8601" gnus-article-date-iso8601 t]
2397               ["UT" gnus-article-date-ut t]
2398               ["Original" gnus-article-date-original t]
2399               ["Lapsed" gnus-article-date-lapsed t]
2400               ["User-defined" gnus-article-date-user t])
2401              ("Display"
2402               ["Remove images" gnus-article-remove-images t]
2403               ["Toggle smiley" gnus-treat-smiley t]
2404               ["Show X-Face" gnus-article-display-x-face t]
2405               ["Show picons in From" gnus-treat-from-picon t]
2406               ["Show picons in mail headers" gnus-treat-mail-picon t]
2407               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2408               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2409               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2410               ("View as different encoding"
2411                ,@(gnus-summary-menu-split
2412                   (mapcar
2413                    (lambda (cs)
2414                      ;; Since easymenu under Emacs doesn't allow
2415                      ;; lambda forms for menu commands, we should
2416                      ;; provide intern'ed function symbols.
2417                      (let ((command (intern (format "\
2418 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2419                        (fset command
2420                              `(lambda ()
2421                                 (interactive)
2422                                 (let ((gnus-summary-show-article-charset-alist
2423                                        '((1 . ,cs))))
2424                                   (gnus-summary-show-article 1))))
2425                        `[,(symbol-name cs) ,command t]))
2426                    (sort (if (fboundp 'coding-system-list)
2427                              (coding-system-list)
2428                            (mapcar 'car mm-mime-mule-charset-alist))
2429                          'string<)))))
2430              ("Washing"
2431               ("Remove Blanks"
2432                ["Leading" gnus-article-strip-leading-blank-lines t]
2433                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2434                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2435                ["All of the above" gnus-article-strip-blank-lines t]
2436                ["All" gnus-article-strip-all-blank-lines t]
2437                ["Leading space" gnus-article-strip-leading-space t]
2438                ["Trailing space" gnus-article-strip-trailing-space t]
2439                ["Leading space in headers"
2440                 gnus-article-remove-leading-whitespace t])
2441               ["Overstrike" gnus-article-treat-overstrike t]
2442               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2443               ["Non-ASCII" gnus-article-treat-non-ascii t]
2444               ["Emphasis" gnus-article-emphasize t]
2445               ["Word wrap" gnus-article-fill-cited-article t]
2446               ["Fill long lines" gnus-article-fill-long-lines t]
2447               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2448               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2449               ["Remove CR" gnus-article-remove-cr t]
2450               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2451               ["Base64" gnus-article-de-base64-unreadable t]
2452               ["Rot 13" gnus-summary-caesar-message
2453                ,@(if (featurep 'xemacs) '(t)
2454                    '(:help "\"Caesar rotate\" article by 13"))]
2455               ["De-IDNA" gnus-summary-idna-message t]
2456               ["Morse decode" gnus-summary-morse-message t]
2457               ["Unix pipe..." gnus-summary-pipe-message t]
2458               ["Add buttons" gnus-article-add-buttons t]
2459               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2460               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2461               ["Verbose header" gnus-summary-verbose-headers t]
2462               ["Toggle header" gnus-summary-toggle-header t]
2463               ["Unfold headers" gnus-article-treat-unfold-headers t]
2464               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2465               ["Html" gnus-article-wash-html t]
2466               ["Unsplit URLs" gnus-article-unsplit-urls t]
2467               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2468               ["Decode HZ" gnus-article-decode-HZ t]
2469               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2470               ("(Outlook) Deuglify"
2471                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2472                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2473                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2474                ["Full (Outlook) deuglify"
2475                 gnus-article-outlook-deuglify-article t])
2476               )
2477              ("Output"
2478               ["Save in default format..." gnus-summary-save-article
2479                ,@(if (featurep 'xemacs) '(t)
2480                    '(:help "Save article using default method"))]
2481               ["Save in file..." gnus-summary-save-article-file
2482                ,@(if (featurep 'xemacs) '(t)
2483                    '(:help "Save article in file"))]
2484               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2485               ["Save in MH folder..." gnus-summary-save-article-folder t]
2486               ["Save in VM folder..." gnus-summary-save-article-vm t]
2487               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2488               ["Save body in file..." gnus-summary-save-article-body-file t]
2489               ["Pipe through a filter..." gnus-summary-pipe-output t]
2490               ["Print with Muttprint..." gnus-summary-muttprint t]
2491               ["Print" gnus-summary-print-article
2492                ,@(if (featurep 'xemacs) '(t)
2493                    '(:help "Generate and print a PostScript image"))])
2494              ("Copy, move,... (Backend)"
2495               ,@(if (featurep 'xemacs) nil
2496                   '(:help "Copying, moving, expiring articles..."))
2497               ["Respool article..." gnus-summary-respool-article t]
2498               ["Move article..." gnus-summary-move-article
2499                (gnus-check-backend-function
2500                 'request-move-article gnus-newsgroup-name)]
2501               ["Copy article..." gnus-summary-copy-article t]
2502               ["Crosspost article..." gnus-summary-crosspost-article
2503                (gnus-check-backend-function
2504                 'request-replace-article gnus-newsgroup-name)]
2505               ["Import file..." gnus-summary-import-article
2506                (gnus-check-backend-function
2507                 'request-accept-article gnus-newsgroup-name)]
2508               ["Create article..." gnus-summary-create-article
2509                (gnus-check-backend-function
2510                 'request-accept-article gnus-newsgroup-name)]
2511               ["Check if posted" gnus-summary-article-posted-p t]
2512               ["Edit article" gnus-summary-edit-article
2513                (not (gnus-group-read-only-p))]
2514               ["Delete article" gnus-summary-delete-article
2515                (gnus-check-backend-function
2516                 'request-expire-articles gnus-newsgroup-name)]
2517               ["Query respool" gnus-summary-respool-query t]
2518               ["Trace respool" gnus-summary-respool-trace t]
2519               ["Delete expirable articles" gnus-summary-expire-articles-now
2520                (gnus-check-backend-function
2521                 'request-expire-articles gnus-newsgroup-name)])
2522              ("Extract"
2523               ["Uudecode" gnus-uu-decode-uu
2524                ,@(if (featurep 'xemacs) '(t)
2525                    '(:help "Decode uuencoded article(s)"))]
2526               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2527               ["Unshar" gnus-uu-decode-unshar t]
2528               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2529               ["Save" gnus-uu-decode-save t]
2530               ["Binhex" gnus-uu-decode-binhex t]
2531               ["PostScript" gnus-uu-decode-postscript t]
2532               ["All MIME parts" gnus-summary-save-parts t])
2533              ("Cache"
2534               ["Enter article" gnus-cache-enter-article t]
2535               ["Remove article" gnus-cache-remove-article t])
2536              ["Translate" gnus-article-babel t]
2537              ["Select article buffer" gnus-summary-select-article-buffer t]
2538              ["Make article buffer sticky" gnus-sticky-article t]
2539              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2540              ["Isearch article..." gnus-summary-isearch-article t]
2541              ["Beginning of the article" gnus-summary-beginning-of-article t]
2542              ["End of the article" gnus-summary-end-of-article t]
2543              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2544              ["Fetch referenced articles" gnus-summary-refer-references t]
2545              ["Fetch current thread" gnus-summary-refer-thread t]
2546              ["Fetch article with id..." gnus-summary-refer-article t]
2547              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2548              ["Redisplay" gnus-summary-show-article t]
2549              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2550       (easy-menu-define
2551         gnus-summary-article-menu gnus-summary-mode-map ""
2552         (cons "Article" innards))
2553
2554       (if (not (keymapp gnus-summary-article-menu))
2555           (easy-menu-define
2556             gnus-article-commands-menu gnus-article-mode-map ""
2557             (cons "Commands" innards))
2558         ;; in Emacs, don't share menu.
2559         (setq gnus-article-commands-menu
2560               (copy-keymap gnus-summary-article-menu))
2561         (define-key gnus-article-mode-map [menu-bar commands]
2562           (cons "Commands" gnus-article-commands-menu))))
2563
2564     (easy-menu-define
2565       gnus-summary-thread-menu gnus-summary-mode-map ""
2566       '("Threads"
2567         ["Find all messages in thread" gnus-summary-refer-thread t]
2568         ["Toggle threading" gnus-summary-toggle-threads t]
2569         ["Hide threads" gnus-summary-hide-all-threads t]
2570         ["Show threads" gnus-summary-show-all-threads t]
2571         ["Hide thread" gnus-summary-hide-thread t]
2572         ["Show thread" gnus-summary-show-thread t]
2573         ["Go to next thread" gnus-summary-next-thread t]
2574         ["Go to previous thread" gnus-summary-prev-thread t]
2575         ["Go down thread" gnus-summary-down-thread t]
2576         ["Go up thread" gnus-summary-up-thread t]
2577         ["Top of thread" gnus-summary-top-thread t]
2578         ["Mark thread as read" gnus-summary-kill-thread t]
2579         ["Mark thread as expired" gnus-summary-expire-thread t]
2580         ["Lower thread score" gnus-summary-lower-thread t]
2581         ["Raise thread score" gnus-summary-raise-thread t]
2582         ["Rethread current" gnus-summary-rethread-current t]))
2583
2584     (easy-menu-define
2585       gnus-summary-post-menu gnus-summary-mode-map ""
2586       `("Post"
2587         ["Send a message (mail or news)" gnus-summary-post-news
2588          ,@(if (featurep 'xemacs) '(t)
2589              '(:help "Compose a new message (mail or news)"))]
2590         ["Followup" gnus-summary-followup
2591          ,@(if (featurep 'xemacs) '(t)
2592              '(:help "Post followup to this article"))]
2593         ["Followup and yank" gnus-summary-followup-with-original
2594          ,@(if (featurep 'xemacs) '(t)
2595              '(:help "Post followup to this article, quoting its contents"))]
2596         ["Supersede article" gnus-summary-supersede-article t]
2597         ["Cancel article" gnus-summary-cancel-article
2598          ,@(if (featurep 'xemacs) '(t)
2599              '(:help "Cancel an article you posted"))]
2600         ["Reply" gnus-summary-reply t]
2601         ["Reply and yank" gnus-summary-reply-with-original t]
2602         ["Wide reply" gnus-summary-wide-reply t]
2603         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2604          ,@(if (featurep 'xemacs) '(t)
2605              '(:help "Mail a reply, quoting this article"))]
2606         ["Very wide reply" gnus-summary-very-wide-reply t]
2607         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2608          ,@(if (featurep 'xemacs) '(t)
2609              '(:help "Mail a very wide reply, quoting this article"))]
2610         ["Mail forward" gnus-summary-mail-forward t]
2611         ["Post forward" gnus-summary-post-forward t]
2612         ["Digest and mail" gnus-uu-digest-mail-forward t]
2613         ["Digest and post" gnus-uu-digest-post-forward t]
2614         ["Resend message" gnus-summary-resend-message t]
2615         ["Resend message edit" gnus-summary-resend-message-edit t]
2616         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2617         ["Send a mail" gnus-summary-mail-other-window t]
2618         ["Create a local message" gnus-summary-news-other-window t]
2619         ["Uuencode and post" gnus-uu-post-news
2620          ,@(if (featurep 'xemacs) '(t)
2621              '(:help "Post a uuencoded article"))]
2622         ["Followup via news" gnus-summary-followup-to-mail t]
2623         ["Followup via news and yank"
2624          gnus-summary-followup-to-mail-with-original t]
2625         ["Strip signature on reply"
2626          (lambda ()
2627            (interactive)
2628            (if (not (memq message-cite-function
2629                           '(message-cite-original-without-signature
2630                             message-cite-original)))
2631                ;; Stupid workaround for XEmacs not honoring :visible.
2632                (message "Can't toggle this value of `message-cite-function'")
2633              (setq message-cite-function
2634                    (if (eq message-cite-function
2635                            'message-cite-original-without-signature)
2636                        'message-cite-original
2637                      'message-cite-original-without-signature))))
2638          ;; XEmacs barfs on :visible.
2639          ,@(if (featurep 'xemacs) nil
2640              '(:visible (memq message-cite-function
2641                               '(message-cite-original-without-signature
2642                                 message-cite-original))))
2643          :style toggle
2644          :selected (eq message-cite-function
2645                        'message-cite-original-without-signature)
2646          ,@(if (featurep 'xemacs) nil
2647              '(:help "Strip signature from cited article when replying."))]
2648         ;;("Draft"
2649         ;;["Send" gnus-summary-send-draft t]
2650         ;;["Send bounced" gnus-resend-bounced-mail t])
2651         ))
2652
2653     (cond
2654      ((not (keymapp gnus-summary-post-menu))
2655       (setq gnus-article-post-menu gnus-summary-post-menu))
2656      ((not gnus-article-post-menu)
2657       ;; Don't share post menu.
2658       (setq gnus-article-post-menu
2659             (copy-keymap gnus-summary-post-menu))))
2660     (define-key gnus-article-mode-map [menu-bar post]
2661       (cons "Post" gnus-article-post-menu))
2662
2663     (easy-menu-define
2664       gnus-summary-misc-menu gnus-summary-mode-map ""
2665       `("Gnus"
2666         ("Mark Read"
2667          ["Mark as read" gnus-summary-mark-as-read-forward t]
2668          ["Mark same subject and select"
2669           gnus-summary-kill-same-subject-and-select t]
2670          ["Mark same subject" gnus-summary-kill-same-subject t]
2671          ["Catchup" gnus-summary-catchup
2672           ,@(if (featurep 'xemacs) '(t)
2673               '(:help "Mark unread articles in this group as read"))]
2674          ["Catchup all" gnus-summary-catchup-all t]
2675          ["Catchup to here" gnus-summary-catchup-to-here t]
2676          ["Catchup from here" gnus-summary-catchup-from-here t]
2677          ["Catchup region" gnus-summary-mark-region-as-read
2678           (gnus-mark-active-p)]
2679          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2680         ("Mark Various"
2681          ["Tick" gnus-summary-tick-article-forward t]
2682          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2683          ["Remove marks" gnus-summary-clear-mark-forward t]
2684          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2685          ["Set bookmark" gnus-summary-set-bookmark t]
2686          ["Remove bookmark" gnus-summary-remove-bookmark t])
2687         ("Limit to"
2688          ["Marks..." gnus-summary-limit-to-marks t]
2689          ["Subject..." gnus-summary-limit-to-subject t]
2690          ["Author..." gnus-summary-limit-to-author t]
2691          ["Recipient..." gnus-summary-limit-to-recipient t]
2692          ["Address..." gnus-summary-limit-to-address t]
2693          ["Age..." gnus-summary-limit-to-age t]
2694          ["Extra..." gnus-summary-limit-to-extra t]
2695          ["Score..." gnus-summary-limit-to-score t]
2696          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2697          ["Unread" gnus-summary-limit-to-unread t]
2698          ["Unseen" gnus-summary-limit-to-unseen t]
2699          ["Singletons" gnus-summary-limit-to-singletons t]
2700          ["Replied" gnus-summary-limit-to-replied t]
2701          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2702          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2703          ["Pop limit" gnus-summary-pop-limit t]
2704          ["Show dormant" gnus-summary-limit-include-dormant t]
2705          ["Hide childless dormant"
2706           gnus-summary-limit-exclude-childless-dormant t]
2707          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2708          ["Hide marked" gnus-summary-limit-exclude-marks t]
2709          ["Show expunged" gnus-summary-limit-include-expunged t])
2710         ("Process Mark"
2711          ["Set mark" gnus-summary-mark-as-processable t]
2712          ["Remove mark" gnus-summary-unmark-as-processable t]
2713          ["Remove all marks" gnus-summary-unmark-all-processable t]
2714          ["Invert marks" gnus-uu-invert-processable t]
2715          ["Mark above" gnus-uu-mark-over t]
2716          ["Mark series" gnus-uu-mark-series t]
2717          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2718          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2719          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2720          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2721          ["Mark all" gnus-uu-mark-all t]
2722          ["Mark buffer" gnus-uu-mark-buffer t]
2723          ["Mark sparse" gnus-uu-mark-sparse t]
2724          ["Mark thread" gnus-uu-mark-thread t]
2725          ["Unmark thread" gnus-uu-unmark-thread t]
2726          ("Process Mark Sets"
2727           ["Kill" gnus-summary-kill-process-mark t]
2728           ["Yank" gnus-summary-yank-process-mark
2729            gnus-newsgroup-process-stack]
2730           ["Save" gnus-summary-save-process-mark t]
2731           ["Run command on marked..." gnus-summary-universal-argument t]))
2732         ("Registry Marks")
2733         ("Scroll article"
2734          ["Page forward" gnus-summary-next-page
2735           ,@(if (featurep 'xemacs) '(t)
2736               '(:help "Show next page of article"))]
2737          ["Page backward" gnus-summary-prev-page
2738           ,@(if (featurep 'xemacs) '(t)
2739               '(:help "Show previous page of article"))]
2740          ["Line forward" gnus-summary-scroll-up t])
2741         ("Move"
2742          ["Next unread article" gnus-summary-next-unread-article t]
2743          ["Previous unread article" gnus-summary-prev-unread-article t]
2744          ["Next article" gnus-summary-next-article t]
2745          ["Previous article" gnus-summary-prev-article t]
2746          ["Next unread subject" gnus-summary-next-unread-subject t]
2747          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2748          ["Next article same subject" gnus-summary-next-same-subject t]
2749          ["Previous article same subject" gnus-summary-prev-same-subject t]
2750          ["First unread article" gnus-summary-first-unread-article t]
2751          ["Best unread article" gnus-summary-best-unread-article t]
2752          ["Go to subject number..." gnus-summary-goto-subject t]
2753          ["Go to article number..." gnus-summary-goto-article t]
2754          ["Go to the last article" gnus-summary-goto-last-article t]
2755          ["Pop article off history" gnus-summary-pop-article t])
2756         ("Sort"
2757          ["Sort by number" gnus-summary-sort-by-number t]
2758          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2759          ["Sort by author" gnus-summary-sort-by-author t]
2760          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2761          ["Sort by subject" gnus-summary-sort-by-subject t]
2762          ["Sort by date" gnus-summary-sort-by-date t]
2763          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2764          ["Sort by score" gnus-summary-sort-by-score t]
2765          ["Sort by lines" gnus-summary-sort-by-lines t]
2766          ["Sort by characters" gnus-summary-sort-by-chars t]
2767          ["Randomize" gnus-summary-sort-by-random t]
2768          ["Original sort" gnus-summary-sort-by-original t])
2769         ("Help"
2770          ["Describe group" gnus-summary-describe-group t]
2771          ["Read manual" gnus-info-find-node t])
2772         ("Modes"
2773          ["Pick and read" gnus-pick-mode t]
2774          ["Binary" gnus-binary-mode t])
2775         ("Regeneration"
2776          ["Regenerate" gnus-summary-prepare t]
2777          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2778          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2779          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2780          ["Toggle threading" gnus-summary-toggle-threads t])
2781         ["See old articles" gnus-summary-insert-old-articles t]
2782         ["See new articles" gnus-summary-insert-new-articles t]
2783         ["Filter articles..." gnus-summary-execute-command t]
2784         ["Run command on articles..." gnus-summary-universal-argument t]
2785         ["Search articles forward..." gnus-summary-search-article-forward t]
2786         ["Search articles backward..." gnus-summary-search-article-backward t]
2787         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2788         ["Expand window" gnus-summary-expand-window t]
2789         ["Expire expirable articles" gnus-summary-expire-articles
2790          (gnus-check-backend-function
2791           'request-expire-articles gnus-newsgroup-name)]
2792         ["Edit local kill file" gnus-summary-edit-local-kill t]
2793         ["Edit main kill file" gnus-summary-edit-global-kill t]
2794         ["Edit group parameters" gnus-summary-edit-parameters t]
2795         ["Customize group parameters" gnus-summary-customize-parameters t]
2796         ["Send a bug report" gnus-bug t]
2797         ("Exit"
2798          ["Catchup and exit" gnus-summary-catchup-and-exit
2799           ,@(if (featurep 'xemacs) '(t)
2800               '(:help "Mark unread articles in this group as read, then exit"))]
2801          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2802          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2803          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2804          ["Exit group" gnus-summary-exit
2805           ,@(if (featurep 'xemacs) '(t)
2806               '(:help "Exit current group, return to group selection mode"))]
2807          ["Exit group without updating" gnus-summary-exit-no-update t]
2808          ["Exit and goto next group" gnus-summary-next-group t]
2809          ["Exit and goto prev group" gnus-summary-prev-group t]
2810          ["Reselect group" gnus-summary-reselect-current-group t]
2811          ["Rescan group" gnus-summary-rescan-group t]
2812          ["Update dribble" gnus-summary-save-newsrc t])))
2813
2814     (gnus-run-hooks 'gnus-summary-menu-hook)))
2815
2816 (defvar gnus-summary-tool-bar-map nil)
2817
2818 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2819 ;; affect _new_ message buffers.  We might add a function that walks thru all
2820 ;; summary-mode buffers and force the update.
2821 (defun gnus-summary-tool-bar-update (&optional symbol value)
2822   "Update summary mode toolbar.
2823 Setter function for custom variables."
2824   (setq-default gnus-summary-tool-bar-map nil)
2825   (when symbol
2826     ;; When used as ":set" function:
2827     (set-default symbol value))
2828   (when (gnus-buffer-live-p gnus-summary-buffer)
2829     (with-current-buffer gnus-summary-buffer
2830       (gnus-summary-make-tool-bar))))
2831
2832 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2833                                      'gnus-summary-tool-bar-gnome
2834                                    'gnus-summary-tool-bar-retro)
2835   "Specifies the Gnus summary tool bar.
2836
2837 It can be either a list or a symbol referring to a list.  See
2838 `gmm-tool-bar-from-list' for the format of the list.  The
2839 default key map is `gnus-summary-mode-map'.
2840
2841 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2842 `gnus-summary-tool-bar-retro'."
2843   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2844                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2845                  (repeat :tag "User defined list" gmm-tool-bar-item)
2846                  (symbol))
2847   :version "23.1" ;; No Gnus
2848   :initialize 'custom-initialize-default
2849   :set 'gnus-summary-tool-bar-update
2850   :group 'gnus-summary)
2851
2852 (defcustom gnus-summary-tool-bar-gnome
2853   '((gnus-summary-post-news "mail/compose" nil)
2854     (gnus-summary-insert-new-articles "mail/inbox" nil
2855                                       :visible (or (not gnus-agent)
2856                                                    gnus-plugged))
2857     (gnus-summary-reply-with-original "mail/reply")
2858     (gnus-summary-reply "mail/reply" nil :visible nil)
2859     (gnus-summary-followup-with-original "mail/reply-all")
2860     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2861     (gnus-summary-mail-forward "mail/forward")
2862     (gnus-summary-save-article "mail/save")
2863     (gnus-summary-search-article-forward "search" nil :visible nil)
2864     (gnus-summary-print-article "print")
2865     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2866     ;; Some new commands that may need more suitable icons:
2867     (gnus-summary-save-newsrc "save" nil :visible nil)
2868     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2869     (gnus-summary-prev-article "left-arrow")
2870     (gnus-summary-next-article "right-arrow")
2871     (gnus-summary-next-page "next-page")
2872     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2873     ;;
2874     ;; Maybe some sort-by-... could be added:
2875     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2876     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2877     (gnus-summary-mark-as-expirable
2878      "delete" nil
2879      :visible (gnus-check-backend-function 'request-expire-articles
2880                                            gnus-newsgroup-name))
2881     (gnus-summary-mark-as-spam
2882      "mail/spam" t
2883      :visible (and (fboundp 'spam-group-ham-contents-p)
2884                    (spam-group-ham-contents-p gnus-newsgroup-name))
2885      :help "Mark as spam")
2886     (gnus-summary-mark-as-read-forward
2887      "mail/not-spam" nil
2888      :visible (and (fboundp 'spam-group-spam-contents-p)
2889                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2890     ;;
2891     (gnus-summary-exit "exit")
2892     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2893     (gnus-info-find-node "help"))
2894   "List of functions for the summary tool bar (GNOME style).
2895
2896 See `gmm-tool-bar-from-list' for the format of the list."
2897   :type '(repeat gmm-tool-bar-item)
2898   :version "23.1" ;; No Gnus
2899   :initialize 'custom-initialize-default
2900   :set 'gnus-summary-tool-bar-update
2901   :group 'gnus-summary)
2902
2903 (defcustom gnus-summary-tool-bar-retro
2904   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2905     (gnus-summary-next-unread-article "gnus/next-ur")
2906     (gnus-summary-post-news "gnus/post")
2907     (gnus-summary-followup-with-original "gnus/fuwo")
2908     (gnus-summary-followup "gnus/followup")
2909     (gnus-summary-reply-with-original "gnus/reply-wo")
2910     (gnus-summary-reply "gnus/reply")
2911     (gnus-summary-caesar-message "gnus/rot13")
2912     (gnus-uu-decode-uu "gnus/uu-decode")
2913     (gnus-summary-save-article-file "gnus/save-aif")
2914     (gnus-summary-save-article "gnus/save-art")
2915     (gnus-uu-post-news "gnus/uu-post")
2916     (gnus-summary-catchup "gnus/catchup")
2917     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2918     (gnus-summary-exit "gnus/exit-summ")
2919     ;; Some new command that may need more suitable icons:
2920     (gnus-summary-print-article "gnus/print" nil :visible nil)
2921     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2922     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2923     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2924     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2925     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2926     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2927     ;;
2928     (gnus-info-find-node "gnus/help" nil :visible nil))
2929   "List of functions for the summary tool bar (retro look).
2930
2931 See `gmm-tool-bar-from-list' for the format of the list."
2932   :type '(repeat gmm-tool-bar-item)
2933   :version "23.1" ;; No Gnus
2934   :initialize 'custom-initialize-default
2935   :set 'gnus-summary-tool-bar-update
2936   :group 'gnus-summary)
2937
2938 (defcustom gnus-summary-tool-bar-zap-list t
2939   "List of icon items from the global tool bar.
2940 These items are not displayed in the Gnus summary mode tool bar.
2941
2942 See `gmm-tool-bar-from-list' for the format of the list."
2943   :type 'gmm-tool-bar-zap-list
2944   :version "23.1" ;; No Gnus
2945   :initialize 'custom-initialize-default
2946   :set 'gnus-summary-tool-bar-update
2947   :group 'gnus-summary)
2948
2949 (defvar image-load-path)
2950 (defvar tool-bar-map)
2951
2952 (defun gnus-summary-make-tool-bar (&optional force)
2953   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2954 When FORCE, rebuild the tool bar."
2955   (when (and (not (featurep 'xemacs))
2956              (boundp 'tool-bar-mode)
2957              tool-bar-mode
2958              (or (not gnus-summary-tool-bar-map) force))
2959     (let* ((load-path
2960             (gmm-image-load-path-for-library "gnus"
2961                                              "mail/save.xpm"
2962                                              nil t))
2963            (image-load-path (cons (car load-path)
2964                                   (when (boundp 'image-load-path)
2965                                     image-load-path)))
2966            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2967                                         gnus-summary-tool-bar-zap-list
2968                                         'gnus-summary-mode-map)))
2969       (when map
2970         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2971         ;; uses its value.
2972         (setq gnus-summary-tool-bar-map map))))
2973   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2974
2975 (defun gnus-score-set-default (var value)
2976   "A version of set that updates the GNU Emacs menu-bar."
2977   (set var value)
2978   ;; It is the message that forces the active status to be updated.
2979   (message ""))
2980
2981 (defun gnus-make-score-map (type)
2982   "Make a summary score map of type TYPE."
2983   (if t
2984       nil
2985     (let ((headers '(("author" "from" string)
2986                      ("subject" "subject" string)
2987                      ("article body" "body" string)
2988                      ("article head" "head" string)
2989                      ("xref" "xref" string)
2990                      ("extra header" "extra" string)
2991                      ("lines" "lines" number)
2992                      ("followups to author" "followup" string)))
2993           (types '((number ("less than" <)
2994                            ("greater than" >)
2995                            ("equal" =))
2996                    (string ("substring" s)
2997                            ("exact string" e)
2998                            ("fuzzy string" f)
2999                            ("regexp" r))))
3000           (perms '(("temporary" (current-time-string))
3001                    ("permanent" nil)
3002                    ("immediate" now)))
3003           header)
3004       (list
3005        (apply
3006         'nconc
3007         (list
3008          (if (eq type 'lower)
3009              "Lower score"
3010            "Increase score"))
3011         (let (outh)
3012           (while headers
3013             (setq header (car headers))
3014             (setq outh
3015                   (cons
3016                    (apply
3017                     'nconc
3018                     (list (car header))
3019                     (let ((ts (cdr (assoc (nth 2 header) types)))
3020                           outt)
3021                       (while ts
3022                         (setq outt
3023                               (cons
3024                                (apply
3025                                 'nconc
3026                                 (list (caar ts))
3027                                 (let ((ps perms)
3028                                       outp)
3029                                   (while ps
3030                                     (setq outp
3031                                           (cons
3032                                            (vector
3033                                             (caar ps)
3034                                             (list
3035                                              'gnus-summary-score-entry
3036                                              (nth 1 header)
3037                                              (if (or (string= (nth 1 header)
3038                                                               "head")
3039                                                      (string= (nth 1 header)
3040                                                               "body"))
3041                                                  ""
3042                                                (list 'gnus-summary-header
3043                                                      (nth 1 header)))
3044                                              (list 'quote (nth 1 (car ts)))
3045                                              (list 'gnus-score-delta-default
3046                                                    nil)
3047                                              (nth 1 (car ps))
3048                                              t)
3049                                             t)
3050                                            outp))
3051                                     (setq ps (cdr ps)))
3052                                   (list (nreverse outp))))
3053                                outt))
3054                         (setq ts (cdr ts)))
3055                       (list (nreverse outt))))
3056                    outh))
3057             (setq headers (cdr headers)))
3058           (list (nreverse outh))))))))
3059
3060
3061 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3062 (defvar bookmark-make-record-function)
3063 \f
3064 (defvar bidi-paragraph-direction)
3065
3066 (defun gnus-summary-mode (&optional group)
3067   "Major mode for reading articles.
3068
3069 All normal editing commands are switched off.
3070 \\<gnus-summary-mode-map>
3071 Each line in this buffer represents one article.  To read an
3072 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3073 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3074 respectively.
3075
3076 You can also post articles and send mail from this buffer.  To
3077 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3078 of an article, type `\\[gnus-summary-reply]'.
3079
3080 There are approx. one gazillion commands you can execute in this
3081 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3082
3083 The following commands are available:
3084
3085 \\{gnus-summary-mode-map}"
3086   (interactive)
3087   (kill-all-local-variables)
3088   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3089     (gnus-summary-make-local-variables))
3090   (gnus-summary-make-local-variables)
3091   (setq gnus-newsgroup-name group)
3092   (when (gnus-visual-p 'summary-menu 'menu)
3093     (gnus-summary-make-menu-bar)
3094     (gnus-summary-make-tool-bar))
3095   (gnus-make-thread-indent-array)
3096   (gnus-simplify-mode-line)
3097   (setq major-mode 'gnus-summary-mode)
3098   (setq mode-name "Summary")
3099   (use-local-map gnus-summary-mode-map)
3100   (buffer-disable-undo)
3101   (setq buffer-read-only t              ;Disable modification
3102         show-trailing-whitespace nil)
3103   (setq truncate-lines t)
3104   ;; Force paragraph direction to be left-to-right.  Don't make it
3105   ;; bound globally in old Emacsen and XEmacsen.
3106   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3107   (add-to-invisibility-spec '(gnus-sum . t))
3108   (gnus-summary-set-display-table)
3109   (gnus-set-default-directory)
3110   (make-local-variable 'gnus-summary-line-format)
3111   (make-local-variable 'gnus-summary-line-format-spec)
3112   (make-local-variable 'gnus-summary-dummy-line-format)
3113   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3114   (make-local-variable 'gnus-summary-mark-positions)
3115   (gnus-make-local-hook 'pre-command-hook)
3116   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3117   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3118   (turn-on-gnus-mailing-list-mode)
3119   (mm-enable-multibyte)
3120   (set (make-local-variable 'bookmark-make-record-function)
3121        'gnus-summary-bookmark-make-record)
3122   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3123   (gnus-update-summary-mark-positions))
3124
3125 (defun gnus-summary-make-local-variables ()
3126   "Make all the local summary buffer variables."
3127   (let (global)
3128     (dolist (local gnus-summary-local-variables)
3129       (if (consp local)
3130           (progn
3131             (if (eq (cdr local) 'global)
3132                 ;; Copy the global value of the variable.
3133                 (setq global (symbol-value (car local)))
3134               ;; Use the value from the list.
3135               (setq global (eval (cdr local))))
3136             (set (make-local-variable (car local)) global))
3137         ;; Simple nil-valued local variable.
3138         (set (make-local-variable local) nil)))))
3139
3140 ;; Summary data functions.
3141
3142 (defmacro gnus-data-number (data)
3143   `(car ,data))
3144
3145 (defmacro gnus-data-set-number (data number)
3146   `(setcar ,data ,number))
3147
3148 (defmacro gnus-data-mark (data)
3149   `(nth 1 ,data))
3150
3151 (defmacro gnus-data-set-mark (data mark)
3152   `(setcar (nthcdr 1 ,data) ,mark))
3153
3154 (defmacro gnus-data-pos (data)
3155   `(nth 2 ,data))
3156
3157 (defmacro gnus-data-set-pos (data pos)
3158   `(setcar (nthcdr 2 ,data) ,pos))
3159
3160 (defmacro gnus-data-header (data)
3161   `(nth 3 ,data))
3162
3163 (defmacro gnus-data-set-header (data header)
3164   `(setf (nth 3 ,data) ,header))
3165
3166 (defmacro gnus-data-level (data)
3167   `(nth 4 ,data))
3168
3169 (defmacro gnus-data-unread-p (data)
3170   `(= (nth 1 ,data) gnus-unread-mark))
3171
3172 (defmacro gnus-data-read-p (data)
3173   `(/= (nth 1 ,data) gnus-unread-mark))
3174
3175 (defmacro gnus-data-pseudo-p (data)
3176   `(consp (nth 3 ,data)))
3177
3178 (defmacro gnus-data-find (number)
3179   `(assq ,number gnus-newsgroup-data))
3180
3181 (defmacro gnus-data-find-list (number &optional data)
3182   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3183      (memq (assq ,number bdata)
3184            bdata)))
3185
3186 (defmacro gnus-data-make (number mark pos header level)
3187   `(list ,number ,mark ,pos ,header ,level))
3188
3189 (defun gnus-data-enter (after-article number mark pos header level offset)
3190   (let ((data (gnus-data-find-list after-article)))
3191     (unless data
3192       (error "No such article: %d" after-article))
3193     (setcdr data (cons (gnus-data-make number mark pos header level)
3194                        (cdr data)))
3195     (setq gnus-newsgroup-data-reverse nil)
3196     (gnus-data-update-list (cddr data) offset)))
3197
3198 (defun gnus-data-enter-list (after-article list &optional offset)
3199   (when list
3200     (let ((data (and after-article (gnus-data-find-list after-article)))
3201           (ilist list))
3202       (if (not (or data
3203                    after-article))
3204           (let ((odata gnus-newsgroup-data))
3205             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3206             (when offset
3207               (gnus-data-update-list odata offset)))
3208         ;; Find the last element in the list to be spliced into the main
3209         ;; list.
3210         (setq list (last list))
3211         (if (not data)
3212             (progn
3213               (setcdr list gnus-newsgroup-data)
3214               (setq gnus-newsgroup-data ilist)
3215               (when offset
3216                 (gnus-data-update-list (cdr list) offset)))
3217           (setcdr list (cdr data))
3218           (setcdr data ilist)
3219           (when offset
3220             (gnus-data-update-list (cdr list) offset))))
3221       (setq gnus-newsgroup-data-reverse nil))))
3222
3223 (defun gnus-data-remove (article &optional offset)
3224   (let ((data gnus-newsgroup-data))
3225     (if (= (gnus-data-number (car data)) article)
3226         (progn
3227           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3228                 gnus-newsgroup-data-reverse nil)
3229           (when offset
3230             (gnus-data-update-list gnus-newsgroup-data offset)))
3231       (while (cdr data)
3232         (when (= (gnus-data-number (cadr data)) article)
3233           (setcdr data (cddr data))
3234           (when offset
3235             (gnus-data-update-list (cdr data) offset))
3236           (setq data nil
3237                 gnus-newsgroup-data-reverse nil))
3238         (setq data (cdr data))))))
3239
3240 (defmacro gnus-data-list (backward)
3241   `(if ,backward
3242        (or gnus-newsgroup-data-reverse
3243            (setq gnus-newsgroup-data-reverse
3244                  (reverse gnus-newsgroup-data)))
3245      gnus-newsgroup-data))
3246
3247 (defun gnus-data-update-list (data offset)
3248   "Add OFFSET to the POS of all data entries in DATA."
3249   (setq gnus-newsgroup-data-reverse nil)
3250   (while data
3251     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3252     (setq data (cdr data))))
3253
3254 (defun gnus-summary-article-pseudo-p (article)
3255   "Say whether this article is a pseudo article or not."
3256   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3257
3258 (defmacro gnus-summary-article-sparse-p (article)
3259   "Say whether this article is a sparse article or not."
3260   `(memq ,article gnus-newsgroup-sparse))
3261
3262 (defmacro gnus-summary-article-ancient-p (article)
3263   "Say whether this article is a sparse article or not."
3264   `(memq ,article gnus-newsgroup-ancient))
3265
3266 (defun gnus-article-parent-p (number)
3267   "Say whether this article is a parent or not."
3268   (let ((data (gnus-data-find-list number)))
3269     (and (cdr data)              ; There has to be an article after...
3270          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3271             (gnus-data-level (nth 1 data))))))
3272
3273 (defun gnus-article-children (number)
3274   "Return a list of all children to NUMBER."
3275   (let* ((data (gnus-data-find-list number))
3276          (level (gnus-data-level (car data)))
3277          children)
3278     (setq data (cdr data))
3279     (while (and data
3280                 (= (gnus-data-level (car data)) (1+ level)))
3281       (push (gnus-data-number (car data)) children)
3282       (setq data (cdr data)))
3283     children))
3284
3285 (defmacro gnus-summary-skip-intangible ()
3286   "If the current article is intangible, then jump to a different article."
3287   '(let ((to (get-text-property (point) 'gnus-intangible)))
3288      (and to (gnus-summary-goto-subject to))))
3289
3290 (defmacro gnus-summary-article-intangible-p ()
3291   "Say whether this article is intangible or not."
3292   '(get-text-property (point) 'gnus-intangible))
3293
3294 (defun gnus-article-read-p (article)
3295   "Say whether ARTICLE is read or not."
3296   (not (or (memq article gnus-newsgroup-marked)
3297            (memq article gnus-newsgroup-spam-marked)
3298            (memq article gnus-newsgroup-unreads)
3299            (memq article gnus-newsgroup-unselected)
3300            (memq article gnus-newsgroup-dormant))))
3301
3302 ;; Some summary mode macros.
3303
3304 (defmacro gnus-summary-article-number ()
3305   "The article number of the article on the current line.
3306 If there isn't an article number here, then we return the current
3307 article number."
3308   '(progn
3309      (gnus-summary-skip-intangible)
3310      (or (get-text-property (point) 'gnus-number)
3311          (gnus-summary-last-subject))))
3312
3313 (defmacro gnus-summary-article-header (&optional number)
3314   "Return the header of article NUMBER."
3315   `(gnus-data-header (gnus-data-find
3316                       ,(or number '(gnus-summary-article-number)))))
3317
3318 (defmacro gnus-summary-thread-level (&optional number)
3319   "Return the level of thread that starts with article NUMBER."
3320   `(if (and (eq gnus-summary-make-false-root 'dummy)
3321             (get-text-property (point) 'gnus-intangible))
3322        0
3323      (gnus-data-level (gnus-data-find
3324                        ,(or number '(gnus-summary-article-number))))))
3325
3326 (defmacro gnus-summary-article-mark (&optional number)
3327   "Return the mark of article NUMBER."
3328   `(gnus-data-mark (gnus-data-find
3329                     ,(or number '(gnus-summary-article-number)))))
3330
3331 (defmacro gnus-summary-article-pos (&optional number)
3332   "Return the position of the line of article NUMBER."
3333   `(gnus-data-pos (gnus-data-find
3334                    ,(or number '(gnus-summary-article-number)))))
3335
3336 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3337 (defmacro gnus-summary-article-subject (&optional number)
3338   "Return current subject string or nil if nothing."
3339   `(let ((headers
3340           ,(if number
3341                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3342              '(gnus-data-header (assq (gnus-summary-article-number)
3343                                       gnus-newsgroup-data)))))
3344      (and headers
3345           (vectorp headers)
3346           (mail-header-subject headers))))
3347
3348 (defmacro gnus-summary-article-score (&optional number)
3349   "Return current article score."
3350   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3351                   gnus-newsgroup-scored))
3352        gnus-summary-default-score 0))
3353
3354 (defun gnus-summary-article-children (&optional number)
3355   "Return a list of article numbers that are children of article NUMBER."
3356   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3357          (level (gnus-data-level (car data)))
3358          l children)
3359     (while (and (setq data (cdr data))
3360                 (> (setq l (gnus-data-level (car data))) level))
3361       (and (= (1+ level) l)
3362            (push (gnus-data-number (car data))
3363                  children)))
3364     (nreverse children)))
3365
3366 (defun gnus-summary-article-parent (&optional number)
3367   "Return the article number of the parent of article NUMBER."
3368   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3369                                     (gnus-data-list t)))
3370          (level (gnus-data-level (car data))))
3371     (if (zerop level)
3372         ()                              ; This is a root.
3373       ;; We search until we find an article with a level less than
3374       ;; this one.  That function has to be the parent.
3375       (while (and (setq data (cdr data))
3376                   (not (< (gnus-data-level (car data)) level))))
3377       (and data (gnus-data-number (car data))))))
3378
3379 (defun gnus-unread-mark-p (mark)
3380   "Say whether MARK is the unread mark."
3381   (= mark gnus-unread-mark))
3382
3383 (defun gnus-read-mark-p (mark)
3384   "Say whether MARK is one of the marks that mark as read.
3385 This is all marks except unread, ticked, dormant, and expirable."
3386   (not (or (= mark gnus-unread-mark)
3387            (= mark gnus-ticked-mark)
3388            (= mark gnus-spam-mark)
3389            (= mark gnus-dormant-mark)
3390            (= mark gnus-expirable-mark))))
3391
3392 (defmacro gnus-article-mark (number)
3393   "Return the MARK of article NUMBER.
3394 This macro should only be used when computing the mark the \"first\"
3395 time; i.e., when generating the summary lines.  After that,
3396 `gnus-summary-article-mark' should be used to examine the
3397 marks of articles."
3398   `(cond
3399     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3400     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3401     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3402     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3403     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3404     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3405     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3406     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3407            gnus-ancient-mark))))
3408
3409 ;; Saving hidden threads.
3410
3411 (defmacro gnus-save-hidden-threads (&rest forms)
3412   "Save hidden threads, eval FORMS, and restore the hidden threads."
3413   (let ((config (make-symbol "config")))
3414     `(let ((,config (gnus-hidden-threads-configuration)))
3415        (unwind-protect
3416            (save-excursion
3417              ,@forms)
3418          (gnus-restore-hidden-threads-configuration ,config)))))
3419 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3420 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3421
3422 (defun gnus-data-compute-positions ()
3423   "Compute the positions of all articles."
3424   (setq gnus-newsgroup-data-reverse nil)
3425   (let ((data gnus-newsgroup-data))
3426     (save-excursion
3427       (gnus-save-hidden-threads
3428         (gnus-summary-show-all-threads)
3429         (goto-char (point-min))
3430         (while data
3431           (while (get-text-property (point) 'gnus-intangible)
3432             (forward-line 1))
3433           (gnus-data-set-pos (car data) (+ (point) 3))
3434           (setq data (cdr data))
3435           (forward-line 1))))))
3436
3437 (defun gnus-hidden-threads-configuration ()
3438   "Return the current hidden threads configuration."
3439   (save-excursion
3440     (let (config)
3441       (goto-char (point-min))
3442       (while (not (eobp))
3443         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3444           (push (save-excursion (forward-line 0) (point)) config))
3445         (forward-line 1))
3446       config)))
3447
3448 (defun gnus-restore-hidden-threads-configuration (config)
3449   "Restore hidden threads configuration from CONFIG."
3450   (save-excursion
3451     (let (point (inhibit-read-only t))
3452       (while (setq point (pop config))
3453         (goto-char point)
3454         (gnus-summary-hide-thread)))))
3455
3456 ;; Various summary mode internalish functions.
3457
3458 (defun gnus-mouse-pick-article (e)
3459   (interactive "e")
3460   (mouse-set-point e)
3461   (gnus-summary-next-page nil t))
3462
3463 (defun gnus-summary-set-display-table ()
3464   "Change the display table.
3465 Odd characters have a tendency to mess
3466 up nicely formatted displays - we make all possible glyphs
3467 display only a single character."
3468
3469   ;; We start from the standard display table, if any.
3470   (let ((table (or (copy-sequence standard-display-table)
3471                    (make-display-table)))
3472         (i 32))
3473     ;; Nix out all the control chars...
3474     (while (>= (setq i (1- i)) 0)
3475       (gnus-put-display-table i [??] table))
3476    ;; ... but not newline and cr, of course.  (cr is necessary for the
3477     ;; selective display).
3478     (gnus-put-display-table ?\n nil table)
3479     (gnus-put-display-table ?\r nil table)
3480     ;; We keep TAB as well.
3481     (gnus-put-display-table ?\t nil table)
3482     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3483     ;; Emacs 23 (unicode), that are not set already.
3484     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3485                  160
3486                256)))
3487       (while (>= (setq i (1- i)) 127)
3488         ;; Only modify if the entry is nil.
3489         (unless (gnus-get-display-table i table)
3490           (gnus-put-display-table i [??] table))))
3491     (setq buffer-display-table table)))
3492
3493 (defun gnus-summary-set-article-display-arrow (pos)
3494   "Update the overlay arrow to point to line at position POS."
3495   (when gnus-summary-display-arrow
3496     (make-local-variable 'overlay-arrow-position)
3497     (make-local-variable 'overlay-arrow-string)
3498     (save-excursion
3499       (goto-char pos)
3500       (beginning-of-line)
3501       (unless overlay-arrow-position
3502         (setq overlay-arrow-position (make-marker)))
3503       (setq overlay-arrow-string "=>"
3504             overlay-arrow-position (set-marker overlay-arrow-position
3505                                                (point)
3506                                                (current-buffer))))))
3507
3508 (defun gnus-summary-setup-buffer (group)
3509   "Initialize summary buffer.
3510 If the setup was successful, non-nil is returned."
3511   (let ((buffer (gnus-summary-buffer-name group))
3512         (dead-name (concat "*Dead Summary "
3513                            (gnus-group-decoded-name group) "*")))
3514     ;; If a dead summary buffer exists, we kill it.
3515     (when (gnus-buffer-live-p dead-name)
3516       (gnus-kill-buffer dead-name))
3517     (if (get-buffer buffer)
3518         (progn
3519           (set-buffer buffer)
3520           (setq gnus-summary-buffer (current-buffer))
3521           (not gnus-newsgroup-prepared))
3522       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3523       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3524       (gnus-summary-mode group)
3525       (when (gnus-group-quit-config group)
3526         (set (make-local-variable 'gnus-single-article-buffer) nil))
3527       (make-local-variable 'gnus-article-buffer)
3528       (make-local-variable 'gnus-article-current)
3529       (make-local-variable 'gnus-original-article-buffer)
3530       (setq gnus-newsgroup-name group)
3531       ;; Set any local variables in the group parameters.
3532       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3533       t)))
3534
3535 (defun gnus-set-global-variables ()
3536   "Set the global equivalents of the buffer-local variables.
3537 They are set to the latest values they had.  These reflect the summary
3538 buffer that was in action when the last article was fetched."
3539   (when (eq major-mode 'gnus-summary-mode)
3540     (setq gnus-summary-buffer (current-buffer))
3541     (let ((name gnus-newsgroup-name)
3542           (marked gnus-newsgroup-marked)
3543           (spam gnus-newsgroup-spam-marked)
3544           (unread gnus-newsgroup-unreads)
3545           (headers gnus-current-headers)
3546           (data gnus-newsgroup-data)
3547           (summary gnus-summary-buffer)
3548           (article-buffer gnus-article-buffer)
3549           (original gnus-original-article-buffer)
3550           (gac gnus-article-current)
3551           (reffed gnus-reffed-article-number)
3552           (score-file gnus-current-score-file)
3553           (default-charset gnus-newsgroup-charset)
3554           vlist)
3555       (let ((locals gnus-newsgroup-variables))
3556         (while locals
3557           (if (consp (car locals))
3558               (push (eval (caar locals)) vlist)
3559             (push (eval (car locals)) vlist))
3560           (setq locals (cdr locals)))
3561         (setq vlist (nreverse vlist)))
3562       (with-current-buffer gnus-group-buffer
3563         (setq gnus-newsgroup-name name
3564               gnus-newsgroup-marked marked
3565               gnus-newsgroup-spam-marked spam
3566               gnus-newsgroup-unreads unread
3567               gnus-current-headers headers
3568               gnus-newsgroup-data data
3569               gnus-article-current gac
3570               gnus-summary-buffer summary
3571               gnus-article-buffer article-buffer
3572               gnus-original-article-buffer original
3573               gnus-reffed-article-number reffed
3574               gnus-current-score-file score-file
3575               gnus-newsgroup-charset default-charset)
3576         (let ((locals gnus-newsgroup-variables))
3577           (while locals
3578             (if (consp (car locals))
3579                 (set (caar locals) (pop vlist))
3580               (set (car locals) (pop vlist)))
3581             (setq locals (cdr locals))))
3582         ;; The article buffer also has local variables.
3583         (when (gnus-buffer-live-p gnus-article-buffer)
3584           (set-buffer gnus-article-buffer)
3585           (setq gnus-summary-buffer summary))))))
3586
3587 (defun gnus-summary-article-unread-p (article)
3588   "Say whether ARTICLE is unread or not."
3589   (memq article gnus-newsgroup-unreads))
3590
3591 (defun gnus-summary-first-article-p (&optional article)
3592   "Return whether ARTICLE is the first article in the buffer."
3593   (if (not (setq article (or article (gnus-summary-article-number))))
3594       nil
3595     (eq article (caar gnus-newsgroup-data))))
3596
3597 (defun gnus-summary-last-article-p (&optional article)
3598   "Return whether ARTICLE is the last article in the buffer."
3599   (if (not (setq article (or article (gnus-summary-article-number))))
3600       ;; All non-existent numbers are the last article.  :-)
3601       t
3602     (not (cdr (gnus-data-find-list article)))))
3603
3604 (defun gnus-make-thread-indent-array (&optional n)
3605   (when (or n
3606             (progn (setq n 200) nil)
3607             (null gnus-thread-indent-array)
3608             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3609     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3610           gnus-thread-indent-array-level gnus-thread-indent-level)
3611     (while (>= n 0)
3612       (aset gnus-thread-indent-array n
3613             (make-string (* n gnus-thread-indent-level) ? ))
3614       (setq n (1- n)))))
3615
3616 (defun gnus-update-summary-mark-positions ()
3617   "Compute where the summary marks are to go."
3618   (save-excursion
3619     (when (gnus-buffer-exists-p gnus-summary-buffer)
3620       (set-buffer gnus-summary-buffer))
3621     (let ((spec gnus-summary-line-format-spec)
3622           pos)
3623       (save-excursion
3624         (gnus-set-work-buffer)
3625         (let ((gnus-tmp-unread ?Z)
3626               (gnus-replied-mark ?Z)
3627               (gnus-score-below-mark ?Z)
3628               (gnus-score-over-mark ?Z)
3629               (gnus-undownloaded-mark ?Z)
3630               (gnus-summary-line-format-spec spec)
3631               (gnus-newsgroup-downloadable '(0))
3632               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3633               case-fold-search ignores)
3634           ;; Here, all marks are bound to Z.
3635           (gnus-summary-insert-line header
3636                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3637           (goto-char (point-min))
3638           ;; Memorize the positions of the same characters as dummy marks.
3639           (while (re-search-forward "[A-D]" nil t)
3640             (push (point) ignores))
3641           (erase-buffer)
3642           ;; We use A-D as dummy marks in order to know column positions
3643           ;; where marks should be inserted.
3644           (setq gnus-tmp-unread ?A
3645                 gnus-replied-mark ?B
3646                 gnus-score-below-mark ?C
3647                 gnus-score-over-mark ?C
3648                 gnus-undownloaded-mark ?D)
3649           (gnus-summary-insert-line header
3650                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3651           ;; Ignore characters which aren't dummy marks.
3652           (dolist (p ignores)
3653             (delete-region (goto-char (1- p)) p)
3654             (insert ?Z))
3655           (goto-char (point-min))
3656           (setq pos (list (cons 'unread
3657                                 (and (search-forward "A" nil t)
3658                                      (- (point) (point-min) 1)))))
3659           (goto-char (point-min))
3660           (push (cons 'replied (and (search-forward "B" nil t)
3661                                     (- (point) (point-min) 1)))
3662                 pos)
3663           (goto-char (point-min))
3664           (push (cons 'score (and (search-forward "C" nil t)
3665                                   (- (point) (point-min) 1)))
3666                 pos)
3667           (goto-char (point-min))
3668           (push (cons 'download (and (search-forward "D" nil t)
3669                                      (- (point) (point-min) 1)))
3670                 pos)))
3671       (setq gnus-summary-mark-positions pos))))
3672
3673 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3674   "Insert a dummy root in the summary buffer."
3675   (beginning-of-line)
3676   (gnus-add-text-properties
3677    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3678    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3679
3680 (defun gnus-summary-extract-address-component (from)
3681   (or (car (funcall gnus-extract-address-components from))
3682       from))
3683
3684 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3685   (let ((mail-parse-charset gnus-newsgroup-charset)
3686         (ignored-from-addresses (gnus-ignored-from-addresses))
3687         ; Is it really necessary to do this next part for each summary line?
3688         ; Luckily, doesn't seem to slow things down much.
3689         (mail-parse-ignored-charsets
3690          (with-current-buffer gnus-summary-buffer
3691            gnus-newsgroup-ignored-charsets)))
3692     (or
3693      (and ignored-from-addresses
3694           (string-match ignored-from-addresses gnus-tmp-from)
3695           (let ((extra-headers (mail-header-extra header))
3696                 to
3697                 newsgroups)
3698             (cond
3699              ((setq to (cdr (assq 'To extra-headers)))
3700               (concat gnus-summary-to-prefix
3701                       (inline
3702                         (gnus-summary-extract-address-component
3703                          (funcall gnus-decode-encoded-address-function to)))))
3704              ((setq newsgroups
3705                     (or
3706                      (cdr (assq 'Newsgroups extra-headers))
3707                      (and
3708                       (memq 'Newsgroups gnus-extra-headers)
3709                       (eq (car (gnus-find-method-for-group
3710                                 gnus-newsgroup-name)) 'nntp)
3711                       (gnus-group-real-name gnus-newsgroup-name))))
3712               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3713      (gnus-string-mark-left-to-right
3714       (inline
3715        (gnus-summary-extract-address-component gnus-tmp-from))))))
3716
3717 (defun gnus-summary-insert-line (gnus-tmp-header
3718                                  gnus-tmp-level gnus-tmp-current
3719                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3720                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3721                                  &optional gnus-tmp-dummy gnus-tmp-score
3722                                  gnus-tmp-process)
3723   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3724       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3725                                           gnus-tmp-level)))
3726   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3727          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3728          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3729          (gnus-tmp-score-char
3730           (if (or (null gnus-summary-default-score)
3731                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3732                       gnus-summary-zcore-fuzz))
3733               ?                         ;Whitespace
3734             (if (< gnus-tmp-score gnus-summary-default-score)
3735                 gnus-score-below-mark gnus-score-over-mark)))
3736          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3737          (gnus-tmp-replied
3738           (cond (gnus-tmp-process gnus-process-mark)
3739                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3740                  gnus-cached-mark)
3741                 (gnus-tmp-replied gnus-replied-mark)
3742                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3743                  gnus-forwarded-mark)
3744                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3745                  gnus-saved-mark)
3746                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3747                  gnus-unseen-mark)
3748                 (t gnus-no-mark)))
3749          (gnus-tmp-downloaded
3750           (cond (undownloaded
3751                  gnus-undownloaded-mark)
3752                 (gnus-newsgroup-agentized
3753                  gnus-downloaded-mark)
3754                 (t
3755                  gnus-no-mark)))
3756          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3757          (gnus-tmp-name
3758           (cond
3759            ((string-match "<[^>]+> *$" gnus-tmp-from)
3760             (let ((beg (match-beginning 0)))
3761               (or (and (string-match "^\".+\"" gnus-tmp-from)
3762                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3763                   (substring gnus-tmp-from 0 beg))))
3764            ((string-match "(.+)" gnus-tmp-from)
3765             (substring gnus-tmp-from
3766                        (1+ (match-beginning 0)) (1- (match-end 0))))
3767            (t gnus-tmp-from)))
3768          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3769          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3770          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3771          (inhibit-read-only t))
3772     (when (string= gnus-tmp-name "")
3773       (setq gnus-tmp-name gnus-tmp-from))
3774     (unless (numberp gnus-tmp-lines)
3775       (setq gnus-tmp-lines -1))
3776     (if (= gnus-tmp-lines -1)
3777         (setq gnus-tmp-lines "?")
3778       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3779     (condition-case ()
3780         (gnus-put-text-property
3781          (point)
3782          (progn (eval gnus-summary-line-format-spec) (point))
3783          'gnus-number gnus-tmp-number)
3784       (error (gnus-message 5 "Error updating the summary line")))
3785     (when (gnus-visual-p 'summary-highlight 'highlight)
3786       (forward-line -1)
3787       (gnus-summary-highlight-line)
3788       (gnus-run-hooks 'gnus-summary-update-hook)
3789       (forward-line 1))))
3790
3791 (defun gnus-summary-update-line (&optional dont-update)
3792   "Update summary line after change."
3793   (when (and gnus-summary-default-score
3794              (not gnus-summary-inhibit-highlight))
3795     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3796            (article (gnus-summary-article-number))
3797            (score (gnus-summary-article-score article)))
3798       (unless dont-update
3799         (if (and gnus-summary-mark-below
3800                  (< (gnus-summary-article-score)
3801                     gnus-summary-mark-below))
3802             ;; This article has a low score, so we mark it as read.
3803             (when (memq article gnus-newsgroup-unreads)
3804               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3805           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3806             ;; This article was previously marked as read on account
3807             ;; of a low score, but now it has risen, so we mark it as
3808             ;; unread.
3809             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3810         (gnus-summary-update-mark
3811          (if (or (null gnus-summary-default-score)
3812                  (<= (abs (- score gnus-summary-default-score))
3813                      gnus-summary-zcore-fuzz))
3814              ?                          ;Whitespace
3815            (if (< score gnus-summary-default-score)
3816                gnus-score-below-mark gnus-score-over-mark))
3817          'score))
3818       ;; Do visual highlighting.
3819       (when (gnus-visual-p 'summary-highlight 'highlight)
3820         (gnus-summary-highlight-line)
3821         (gnus-run-hooks 'gnus-summary-update-hook)))))
3822
3823 (defvar gnus-tmp-new-adopts nil)
3824
3825 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3826   "Return the number of articles in THREAD.
3827 This may be 0 in some cases -- if none of the articles in
3828 the thread are to be displayed."
3829   (let* ((number
3830          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3831           (cond
3832            ((not (listp thread))
3833             1)
3834            ((and (consp thread) (cdr thread))
3835             (apply
3836              '+ 1 (mapcar
3837                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3838            ((null thread)
3839             1)
3840            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3841             1)
3842            (t 0))))
3843     (when (and level (zerop level) gnus-tmp-new-adopts)
3844       (incf number
3845             (apply '+ (mapcar
3846                        'gnus-summary-number-of-articles-in-thread
3847                        gnus-tmp-new-adopts))))
3848     (if char
3849         (if (> number 1) gnus-not-empty-thread-mark
3850           gnus-empty-thread-mark)
3851       number)))
3852
3853 (defsubst gnus-summary-line-message-size (head)
3854   "Return pretty-printed version of message size.
3855 This function is intended to be used in
3856 `gnus-summary-line-format-alist'."
3857   (let ((c (or (mail-header-chars head) -1)))
3858     (cond ((< c 0) "n/a")               ; chars not available
3859           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3860           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3861           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3862           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3863
3864 (defcustom gnus-user-date-format-alist
3865   '(((gnus-seconds-today) . "Today, %H:%M")
3866     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3867     (604800 . "%A %H:%M")               ; That's one week
3868     ((gnus-seconds-month) . "%A %d")
3869     ((gnus-seconds-year) . "%B %d")
3870     (t . "%b %d %Y"))                   ; This one is used when no other
3871                                         ; does match
3872   "Specifies date format depending on age of article.
3873 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3874 seconds) or a Lisp expression evaluating to a number.  When the age of
3875 the article is less than this number, then use `format-time-string'
3876 with the corresponding FORMAT for displaying the date of the article.
3877 If AGE is not a number or a Lisp expression evaluating to a
3878 non-number, then the corresponding FORMAT is used as a default value.
3879
3880 Note that the list is processed from the beginning, so it should be
3881 sorted by ascending AGE.  Also note that items following the first
3882 non-number AGE will be ignored.
3883
3884 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3885 and `gnus-seconds-year' in the AGE spec.  They return the number of
3886 seconds passed since the start of today, of this month, of this year,
3887 respectively."
3888   :version "24.1"
3889   :group 'gnus-summary-format
3890   :type '(alist :key-type sexp :value-type string))
3891
3892 (defun gnus-user-date (messy-date)
3893   "Format the messy-date according to `gnus-user-date-format-alist'.
3894 Returns \"  ?  \" if there's bad input or if another error occurs.
3895 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3896   (condition-case ()
3897       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3898              (now (gnus-float-time))
3899              ;;If we don't find something suitable we'll use this one
3900              (my-format "%b %d '%y"))
3901         (let* ((difference (- now messy-date))
3902                (templist gnus-user-date-format-alist)
3903                (top (eval (caar templist))))
3904           (while (if (numberp top) (< top difference) (not top))
3905             (progn
3906               (setq templist (cdr templist))
3907               (setq top (eval (caar templist)))))
3908           (if (stringp (cdr (car templist)))
3909               (setq my-format (cdr (car templist)))))
3910         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3911     (error "  ?   ")))
3912
3913 (defun gnus-summary-set-local-parameters (group)
3914   "Go through the local params of GROUP and set all variable specs in that list."
3915   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3916                                         ; really variables.
3917     (dolist (elem (gnus-group-find-parameter group))
3918       (and (consp elem)                 ; Has to be a cons.
3919            (consp (cdr elem))           ; The cdr has to be a list.
3920            (symbolp (car elem))         ; Has to be a symbol in there.
3921            (not (memq (car elem) vars))
3922            (ignore-errors
3923              (push (car elem) vars)
3924              ;; Variables like `gnus-show-threads' that are globally
3925              ;; bound, if used as group parameters, need to get to be
3926              ;; buffer-local, whereas just parameters like `gcc-self',
3927              ;; `timestamp', etc. should not be bound as variables.
3928              (if (boundp (car elem))
3929                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3930                (eval (nth 1 elem))))))))
3931
3932 (defun gnus-summary-read-group (group &optional show-all no-article
3933                                       kill-buffer no-display backward
3934                                       select-articles)
3935   "Start reading news in newsgroup GROUP.
3936 If SHOW-ALL is non-nil, already read articles are also listed.
3937 If NO-ARTICLE is non-nil, no article is selected initially.
3938 If NO-DISPLAY, don't generate the summary buffer contents.
3939 If KILL-BUFFER, it should be a buffer that's killed once the new
3940 summary buffer has been generated.
3941 If BACKWARD, move point to the previous group in the group buffer
3942 If SELECT-ARTICLES, only select those articles from GROUP."
3943   (let (result)
3944     (while (and group
3945                 (null (setq result
3946                             (let ((gnus-auto-select-next nil))
3947                               (or (gnus-summary-read-group-1
3948                                    group show-all no-article
3949                                    kill-buffer no-display
3950                                    select-articles)
3951                                   (setq show-all nil
3952                                         select-articles nil)))))
3953                 (eq gnus-auto-select-next 'quietly))
3954       (set-buffer gnus-group-buffer)
3955       ;; The entry function called above goes to the next
3956       ;; group automatically, so we go two groups back
3957       ;; if we are searching for the previous group.
3958       (when backward
3959         (gnus-group-prev-unread-group 2))
3960       (if (not (equal group (gnus-group-group-name)))
3961           (setq group (gnus-group-group-name))
3962         (setq group nil)))
3963     result))
3964
3965 (defun gnus-summary-read-group-1 (group show-all no-article
3966                                         kill-buffer no-display
3967                                         &optional select-articles)
3968   ;; Killed foreign groups can't be entered.
3969   ;;  (when (and (not (gnus-group-native-p group))
3970   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3971   ;;    (error "Dead non-native groups can't be entered"))
3972   (gnus-message 7 "Retrieving newsgroup: %s..."
3973                 (gnus-group-decoded-name group))
3974   (let* ((new-group (gnus-summary-setup-buffer group))
3975          (quit-config (gnus-group-quit-config group))
3976          (did-select (and new-group (gnus-select-newsgroup
3977                                      group show-all select-articles))))
3978     (cond
3979      ;; This summary buffer exists already, so we just select it.
3980      ((not new-group)
3981       (gnus-set-global-variables)
3982       (when kill-buffer
3983         (gnus-kill-or-deaden-summary kill-buffer))
3984       (gnus-configure-windows 'summary 'force)
3985       (gnus-set-mode-line 'summary)
3986       (gnus-summary-position-point)
3987       (message "")
3988       t)
3989      ;; We couldn't select this group.
3990      ((null did-select)
3991       (when (and (eq major-mode 'gnus-summary-mode)
3992                  (not (equal (current-buffer) kill-buffer)))
3993         (kill-buffer (current-buffer))
3994         (if (not quit-config)
3995             (progn
3996               ;; Update the info -- marks might need to be removed,
3997               ;; for instance.
3998               (gnus-summary-update-info)
3999               (set-buffer gnus-group-buffer)
4000               (gnus-group-jump-to-group group)
4001               (gnus-group-next-unread-group 1))
4002           (gnus-handle-ephemeral-exit quit-config)))
4003       (if (null (gnus-list-of-unread-articles group))
4004           (gnus-message 3 "Group %s contains no messages" group)
4005         (gnus-message 3 "Can't select group"))
4006       nil)
4007      ;; The user did a `C-g' while prompting for number of articles,
4008      ;; so we exit this group.
4009      ((eq did-select 'quit)
4010       (and (eq major-mode 'gnus-summary-mode)
4011            (not (equal (current-buffer) kill-buffer))
4012            (kill-buffer (current-buffer)))
4013       (when kill-buffer
4014         (gnus-kill-or-deaden-summary kill-buffer))
4015       (if (not quit-config)
4016           (progn
4017             (set-buffer gnus-group-buffer)
4018             (gnus-group-jump-to-group group)
4019             (gnus-configure-windows 'group 'force))
4020         (gnus-handle-ephemeral-exit quit-config))
4021       ;; Finally signal the quit.
4022       (signal 'quit nil))
4023      ;; The group was successfully selected.
4024      (t
4025       (gnus-set-global-variables)
4026       ;; Save the active value in effect when the group was entered.
4027       (setq gnus-newsgroup-active
4028             (gnus-copy-sequence
4029              (gnus-active gnus-newsgroup-name)))
4030       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4031       ;; You can change the summary buffer in some way with this hook.
4032       (gnus-run-hooks 'gnus-select-group-hook)
4033       (when (memq 'summary (gnus-update-format-specifications
4034                             nil 'summary 'summary-mode 'summary-dummy))
4035         ;; The format specification for the summary line was updated,
4036         ;; so we need to update the mark positions as well.
4037         (gnus-update-summary-mark-positions))
4038       ;; Do score processing.
4039       (when gnus-use-scoring
4040         (gnus-possibly-score-headers))
4041       ;; Check whether to fill in the gaps in the threads.
4042       (when gnus-build-sparse-threads
4043         (gnus-build-sparse-threads))
4044       ;; Find the initial limit.
4045       (if show-all
4046           (let ((gnus-newsgroup-dormant nil))
4047             (gnus-summary-initial-limit show-all))
4048         (gnus-summary-initial-limit show-all))
4049       ;; Generate the summary buffer.
4050       (unless no-display
4051         (gnus-summary-prepare))
4052       (when gnus-use-trees
4053         (gnus-tree-open group)
4054         (setq gnus-summary-highlight-line-function
4055               'gnus-tree-highlight-article))
4056       ;; If the summary buffer is empty, but there are some low-scored
4057       ;; articles or some excluded dormants, we include these in the
4058       ;; buffer.
4059       (when (and (zerop (buffer-size))
4060                  (not no-display))
4061         (cond (gnus-newsgroup-dormant
4062                (gnus-summary-limit-include-dormant))
4063               ((and gnus-newsgroup-scored show-all)
4064                (gnus-summary-limit-include-expunged t))))
4065       ;; Function `gnus-apply-kill-file' must be called in this hook.
4066       (gnus-run-hooks 'gnus-apply-kill-hook)
4067       (if (and (zerop (buffer-size))
4068                (not no-display))
4069           (progn
4070             ;; This newsgroup is empty.
4071             (gnus-summary-catchup-and-exit nil t)
4072             (gnus-message 6 "No unread news")
4073             (when kill-buffer
4074               (gnus-kill-or-deaden-summary kill-buffer))
4075             ;; Return nil from this function.
4076             nil)
4077         ;; Hide conversation thread subtrees.  We cannot do this in
4078         ;; gnus-summary-prepare-hook since kill processing may not
4079         ;; work with hidden articles.
4080         (gnus-summary-maybe-hide-threads)
4081         (gnus-configure-windows 'summary)
4082         (when kill-buffer
4083           (gnus-kill-or-deaden-summary kill-buffer))
4084         (gnus-summary-auto-select-subject)
4085         ;; Show first unread article if requested.
4086         (if (and (not no-article)
4087                  (not no-display)
4088                  gnus-newsgroup-unreads
4089                  gnus-auto-select-first)
4090             (progn
4091               (let ((art (gnus-summary-article-number)))
4092                 (unless (and (not gnus-plugged)
4093                              (or (memq art gnus-newsgroup-undownloaded)
4094                                  (memq art gnus-newsgroup-downloadable)))
4095                   (gnus-summary-goto-article art))))
4096           ;; Don't select any articles.
4097           (gnus-summary-position-point)
4098           (gnus-configure-windows 'summary 'force)
4099           (gnus-set-mode-line 'summary))
4100         (when (and gnus-auto-center-group
4101                    (get-buffer-window gnus-group-buffer t))
4102           ;; Gotta use windows, because recenter does weird stuff if
4103           ;; the current buffer ain't the displayed window.
4104           (let ((owin (selected-window)))
4105             (select-window (get-buffer-window gnus-group-buffer t))
4106             (when (gnus-group-goto-group group)
4107               (recenter))
4108             (select-window owin)))
4109         ;; Mark this buffer as "prepared".
4110         (setq gnus-newsgroup-prepared t)
4111         (gnus-run-hooks 'gnus-summary-prepared-hook)
4112         (unless (gnus-ephemeral-group-p group)
4113           (gnus-group-update-group group nil t))
4114         t)))))
4115
4116 (defun gnus-summary-auto-select-subject ()
4117   "Select the subject line on initial group entry."
4118   (goto-char (point-min))
4119   (cond
4120    ((eq gnus-auto-select-subject 'best)
4121     (gnus-summary-best-unread-subject))
4122    ((eq gnus-auto-select-subject 'unread)
4123     (gnus-summary-first-unread-subject))
4124    ((eq gnus-auto-select-subject 'unseen)
4125     (gnus-summary-first-unseen-subject))
4126    ((eq gnus-auto-select-subject 'unseen-or-unread)
4127     (gnus-summary-first-unseen-or-unread-subject))
4128    ((eq gnus-auto-select-subject 'first)
4129     ;; Do nothing.
4130     )
4131    ((functionp gnus-auto-select-subject)
4132     (funcall gnus-auto-select-subject))))
4133
4134 (defun gnus-summary-prepare ()
4135   "Generate the summary buffer."
4136   (interactive)
4137   (let ((inhibit-read-only t))
4138     (erase-buffer)
4139     (setq gnus-newsgroup-data nil
4140           gnus-newsgroup-data-reverse nil)
4141     (gnus-run-hooks 'gnus-summary-generate-hook)
4142     ;; Generate the buffer, either with threads or without.
4143     (when gnus-newsgroup-headers
4144       (gnus-summary-prepare-threads
4145        (if gnus-show-threads
4146            (gnus-sort-gathered-threads
4147             (funcall gnus-summary-thread-gathering-function
4148                      (gnus-sort-threads
4149                       (gnus-cut-threads (gnus-make-threads)))))
4150          ;; Unthreaded display.
4151          (gnus-sort-articles gnus-newsgroup-headers))))
4152     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4153     ;; Call hooks for modifying summary buffer.
4154     (goto-char (point-min))
4155     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4156
4157 (defsubst gnus-general-simplify-subject (subject)
4158   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4159   (setq subject
4160         (cond
4161          ;; Truncate the subject.
4162          (gnus-simplify-subject-functions
4163           (gnus-map-function gnus-simplify-subject-functions subject))
4164          ((numberp gnus-summary-gather-subject-limit)
4165           (setq subject (gnus-simplify-subject-re subject))
4166           (if (> (length subject) gnus-summary-gather-subject-limit)
4167               (substring subject 0 gnus-summary-gather-subject-limit)
4168             subject))
4169          ;; Fuzzily simplify it.
4170          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4171           (gnus-simplify-subject-fuzzy subject))
4172          ;; Just remove the leading "Re:".
4173          (t
4174           (gnus-simplify-subject-re subject))))
4175
4176   (if (and gnus-summary-gather-exclude-subject
4177            (string-match gnus-summary-gather-exclude-subject subject))
4178       nil                         ; This article shouldn't be gathered
4179     subject))
4180
4181 (defun gnus-summary-simplify-subject-query ()
4182   "Query where the respool algorithm would put this article."
4183   (interactive)
4184   (gnus-summary-select-article)
4185   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4186
4187 (defun gnus-gather-threads-by-subject (threads)
4188   "Gather threads by looking at Subject headers."
4189   (if (not gnus-summary-make-false-root)
4190       threads
4191     (let ((hashtb (gnus-make-hashtable 1024))
4192           (prev threads)
4193           (result threads)
4194           subject hthread whole-subject)
4195       (while threads
4196         (setq subject (gnus-general-simplify-subject
4197                        (setq whole-subject (mail-header-subject
4198                                             (caar threads)))))
4199         (when subject
4200           (if (setq hthread (gnus-gethash subject hashtb))
4201               (progn
4202                 ;; We enter a dummy root into the thread, if we
4203                 ;; haven't done that already.
4204                 (unless (stringp (caar hthread))
4205                   (setcar hthread (list whole-subject (car hthread))))
4206                 ;; We add this new gathered thread to this gathered
4207                 ;; thread.
4208                 (setcdr (car hthread)
4209                         (nconc (cdar hthread) (list (car threads))))
4210                 ;; Remove it from the list of threads.
4211                 (setcdr prev (cdr threads))
4212                 (setq threads prev))
4213             ;; Enter this thread into the hash table.
4214             (gnus-sethash subject
4215                           (if gnus-summary-make-false-root-always
4216                               (progn
4217                                 ;; If you want a dummy root above all
4218                                 ;; threads...
4219                                 (setcar threads (list whole-subject
4220                                                       (car threads)))
4221                                 threads)
4222                             threads)
4223                           hashtb)))
4224         (setq prev threads)
4225         (setq threads (cdr threads)))
4226       result)))
4227
4228 (defun gnus-gather-threads-by-references (threads)
4229   "Gather threads by looking at References headers."
4230   (let ((idhashtb (gnus-make-hashtable 1024))
4231         (thhashtb (gnus-make-hashtable 1024))
4232         (prev threads)
4233         (result threads)
4234         ids references id gthread gid entered ref)
4235     (while threads
4236       (when (setq references (mail-header-references (caar threads)))
4237         (setq id (mail-header-id (caar threads))
4238               ids (inline (gnus-split-references references))
4239               entered nil)
4240         (while (setq ref (pop ids))
4241           (setq ids (delete ref ids))
4242           (if (not (setq gid (gnus-gethash ref idhashtb)))
4243               (progn
4244                 (gnus-sethash ref id idhashtb)
4245                 (gnus-sethash id threads thhashtb))
4246             (setq gthread (gnus-gethash gid thhashtb))
4247             (unless entered
4248               ;; We enter a dummy root into the thread, if we
4249               ;; haven't done that already.
4250               (unless (stringp (caar gthread))
4251                 (setcar gthread (list (mail-header-subject (caar gthread))
4252                                       (car gthread))))
4253               ;; We add this new gathered thread to this gathered
4254               ;; thread.
4255               (setcdr (car gthread)
4256                       (nconc (cdar gthread) (list (car threads)))))
4257             ;; Add it into the thread hash table.
4258             (gnus-sethash id gthread thhashtb)
4259             (setq entered t)
4260             ;; Remove it from the list of threads.
4261             (setcdr prev (cdr threads))
4262             (setq threads prev))))
4263       (setq prev threads)
4264       (setq threads (cdr threads)))
4265     result))
4266
4267 (defun gnus-sort-gathered-threads (threads)
4268   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4269   (let ((result threads))
4270     (while threads
4271       (when (stringp (caar threads))
4272         (setcdr (car threads)
4273                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4274       (setq threads (cdr threads)))
4275     result))
4276
4277 (defun gnus-thread-loop-p (root thread)
4278   "Say whether ROOT is in THREAD."
4279   (let ((stack (list thread))
4280         (infloop 0)
4281         th)
4282     (while (setq thread (pop stack))
4283       (setq th (cdr thread))
4284       (while (and th
4285                   (not (eq (caar th) root)))
4286         (pop th))
4287       (if th
4288           ;; We have found a loop.
4289           (let (ref-dep)
4290             (setcdr thread (delq (car th) (cdr thread)))
4291             (if (boundp (setq ref-dep (intern "none"
4292                                               gnus-newsgroup-dependencies)))
4293                 (setcdr (symbol-value ref-dep)
4294                         (nconc (cdr (symbol-value ref-dep))
4295                                (list (car th))))
4296               (set ref-dep (list nil (car th))))
4297             (setq infloop 1
4298                   stack nil))
4299         ;; Push all the subthreads onto the stack.
4300         (push (cdr thread) stack)))
4301     infloop))
4302
4303 (defun gnus-make-threads ()
4304   "Go through the dependency hashtb and find the roots.  Return all threads."
4305   (let (threads)
4306     (while (catch 'infloop
4307              (mapatoms
4308               (lambda (refs)
4309                 ;; Deal with self-referencing References loops.
4310                 (when (and (car (symbol-value refs))
4311                            (not (zerop
4312                                  (apply
4313                                   '+
4314                                   (mapcar
4315                                    (lambda (thread)
4316                                      (gnus-thread-loop-p
4317                                       (car (symbol-value refs)) thread))
4318                                    (cdr (symbol-value refs)))))))
4319                   (setq threads nil)
4320                   (throw 'infloop t))
4321                 (unless (car (symbol-value refs))
4322                   ;; These threads do not refer back to any other
4323                   ;; articles, so they're roots.
4324                   (setq threads (append (cdr (symbol-value refs)) threads))))
4325               gnus-newsgroup-dependencies)))
4326     threads))
4327
4328 ;; Build the thread tree.
4329 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4330   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4331
4332 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4333 if it was already present.
4334
4335 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4336 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4337 Message-IDs will be renamed to a unique Message-ID before being
4338 entered.
4339
4340 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4341   (let* ((id (mail-header-id header))
4342          (id-dep (and id (intern id dependencies)))
4343          parent-id ref ref-dep ref-header replaced)
4344     ;; Enter this `header' in the `dependencies' table.
4345     (cond
4346      ((not id-dep)
4347       (setq header nil))
4348      ;; The first two cases do the normal part: enter a new `header'
4349      ;; in the `dependencies' table.
4350      ((not (boundp id-dep))
4351       (set id-dep (list header)))
4352      ((null (car (symbol-value id-dep)))
4353       (setcar (symbol-value id-dep) header))
4354
4355      ;; From here the `header' was already present in the
4356      ;; `dependencies' table.
4357      (force-new
4358       ;; Overrides an existing entry;
4359       ;; just set the header part of the entry.
4360       (setcar (symbol-value id-dep) header)
4361       (setq replaced t))
4362
4363      ;; Renames the existing `header' to a unique Message-ID.
4364      ((not gnus-summary-ignore-duplicates)
4365       ;; An article with this Message-ID has already been seen.
4366       ;; We rename the Message-ID.
4367       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4368            (list header))
4369       (mail-header-set-id header id))
4370
4371      ;; The last case ignores an existing entry, except it adds any
4372      ;; additional Xrefs (in case the two articles came from different
4373      ;; servers.
4374      ;; Also sets `header' to `nil' meaning that the `dependencies'
4375      ;; table was *not* modified.
4376      (t
4377       (mail-header-set-xref
4378        (car (symbol-value id-dep))
4379        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4380                    "")
4381                (or (mail-header-xref header) "")))
4382       (setq header nil)))
4383
4384     (when (and header (not replaced))
4385       ;; First check that we are not creating a References loop.
4386       (setq parent-id (gnus-parent-id (mail-header-references header)))
4387       (setq ref parent-id)
4388       (while (and ref
4389                   (setq ref-dep (intern-soft ref dependencies))
4390                   (boundp ref-dep)
4391                   (setq ref-header (car (symbol-value ref-dep))))
4392         (if (string= id ref)
4393             ;; Yuk!  This is a reference loop.  Make the article be a
4394             ;; root article.
4395             (progn
4396               (mail-header-set-references (car (symbol-value id-dep)) "none")
4397               (setq ref nil)
4398               (setq parent-id nil))
4399           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4400       (setq ref-dep (intern (or parent-id "none") dependencies))
4401       (if (boundp ref-dep)
4402           (setcdr (symbol-value ref-dep)
4403                   (nconc (cdr (symbol-value ref-dep))
4404                          (list (symbol-value id-dep))))
4405         (set ref-dep (list nil (symbol-value id-dep)))))
4406     header))
4407
4408 (defun gnus-extract-message-id-from-in-reply-to (string)
4409   (if (string-match "<[^>]+>" string)
4410       (substring string (match-beginning 0) (match-end 0))
4411     nil))
4412
4413 (defun gnus-build-sparse-threads ()
4414   (let ((headers gnus-newsgroup-headers)
4415         (mail-parse-charset gnus-newsgroup-charset)
4416         (gnus-summary-ignore-duplicates t)
4417         header references generation relations
4418         subject child end new-child date)
4419     ;; First we create an alist of generations/relations, where
4420     ;; generations is how much we trust the relation, and the relation
4421     ;; is parent/child.
4422     (gnus-message 7 "Making sparse threads...")
4423     (save-excursion
4424       (nnheader-set-temp-buffer " *gnus sparse threads*")
4425       (while (setq header (pop headers))
4426         (when (and (setq references (mail-header-references header))
4427                    (not (string= references "")))
4428           (insert references)
4429           (setq child (mail-header-id header)
4430                 subject (mail-header-subject header)
4431                 date (mail-header-date header)
4432                 generation 0)
4433           (while (search-backward ">" nil t)
4434             (setq end (1+ (point)))
4435             (when (search-backward "<" nil t)
4436               (setq new-child (buffer-substring (point) end))
4437               (push (list (incf generation)
4438                           child (setq child new-child)
4439                           subject date)
4440                     relations)))
4441           (when child
4442             (push (list (1+ generation) child nil subject) relations))
4443           (erase-buffer)))
4444       (kill-buffer (current-buffer)))
4445     ;; Sort over trustworthiness.
4446     (dolist (relation (sort relations 'car-less-than-car))
4447       (when (gnus-dependencies-add-header
4448              (make-full-mail-header
4449               gnus-reffed-article-number
4450               (nth 3 relation) "" (or (nth 4 relation) "")
4451               (nth 1 relation)
4452               (or (nth 2 relation) "") 0 0 "")
4453              gnus-newsgroup-dependencies nil)
4454         (push gnus-reffed-article-number gnus-newsgroup-limit)
4455         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4456         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4457               gnus-newsgroup-reads)
4458         (decf gnus-reffed-article-number)))
4459     (gnus-message 7 "Making sparse threads...done")))
4460
4461 (defun gnus-build-old-threads ()
4462   ;; Look at all the articles that refer back to old articles, and
4463   ;; fetch the headers for the articles that aren't there.  This will
4464   ;; build complete threads - if the roots haven't been expired by the
4465   ;; server, that is.
4466   (let ((mail-parse-charset gnus-newsgroup-charset)
4467         id heads)
4468     (mapatoms
4469      (lambda (refs)
4470        (when (not (car (symbol-value refs)))
4471          (setq heads (cdr (symbol-value refs)))
4472          (while heads
4473            (if (memq (mail-header-number (caar heads))
4474                      gnus-newsgroup-dormant)
4475                (setq heads (cdr heads))
4476              (setq id (symbol-name refs))
4477              (while (and (setq id (gnus-build-get-header id))
4478                          (not (car (gnus-id-to-thread id)))))
4479              (setq heads nil)))))
4480      gnus-newsgroup-dependencies)))
4481
4482 (defsubst gnus-remove-odd-characters (string)
4483   "Translate STRING into something that doesn't contain weird characters."
4484   (mm-subst-char-in-string
4485    ?\r ?\-
4486    (mm-subst-char-in-string ?\n ?\- string t) t))
4487
4488 ;; This function has to be called with point after the article number
4489 ;; on the beginning of the line.
4490 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4491   (let ((eol (point-at-eol))
4492         (buffer (current-buffer))
4493         header references in-reply-to)
4494
4495     ;; overview: [num subject from date id refs chars lines misc]
4496     (unwind-protect
4497         (let (x)
4498           (narrow-to-region (point) eol)
4499           (unless (eobp)
4500             (forward-char))
4501
4502           (setq header
4503                 (make-full-mail-header
4504                  number                 ; number
4505                  (condition-case ()     ; subject
4506                      (gnus-remove-odd-characters
4507                       (funcall gnus-decode-encoded-word-function
4508                                (setq x (nnheader-nov-field))))
4509                    (error x))
4510                  (condition-case ()     ; from
4511                      (gnus-remove-odd-characters
4512                       (funcall gnus-decode-encoded-address-function
4513                                (setq x (nnheader-nov-field))))
4514                    (error x))
4515                  (nnheader-nov-field)   ; date
4516                  (nnheader-nov-read-message-id number)  ; id
4517                  (setq references (nnheader-nov-field)) ; refs
4518                  (nnheader-nov-read-integer) ; chars
4519                  (nnheader-nov-read-integer) ; lines
4520                  (unless (eobp)
4521                    (if (looking-at "Xref: ")
4522                        (goto-char (match-end 0)))
4523                    (nnheader-nov-field)) ; Xref
4524                  (nnheader-nov-parse-extra)))) ; extra
4525
4526       (widen))
4527
4528     (when (and (string= references "")
4529                (setq in-reply-to (mail-header-extra header))
4530                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4531       (mail-header-set-references
4532        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4533
4534     (when gnus-alter-header-function
4535       (funcall gnus-alter-header-function header))
4536     (gnus-dependencies-add-header header dependencies force-new)))
4537
4538 (defun gnus-build-get-header (id)
4539   "Look through the buffer of NOV lines and find the header to ID.
4540 Enter this line into the dependencies hash table, and return
4541 the id of the parent article (if any)."
4542   (let ((deps gnus-newsgroup-dependencies)
4543         found header)
4544     (prog1
4545         (with-current-buffer nntp-server-buffer
4546           (let ((case-fold-search nil))
4547             (goto-char (point-min))
4548             (while (and (not found)
4549                         (search-forward id nil t))
4550               (beginning-of-line)
4551               (setq found (looking-at
4552                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4553                                    (regexp-quote id))))
4554               (or found (beginning-of-line 2)))
4555             (when found
4556               (beginning-of-line)
4557               (and
4558                (setq header (gnus-nov-parse-line
4559                              (read (current-buffer)) deps))
4560                (gnus-parent-id (mail-header-references header))))))
4561       (when header
4562         (let ((number (mail-header-number header)))
4563           (push number gnus-newsgroup-limit)
4564           (push header gnus-newsgroup-headers)
4565           (if (memq number gnus-newsgroup-unselected)
4566               (progn
4567                 (setq gnus-newsgroup-unreads
4568                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4569                                                number))
4570                 (setq gnus-newsgroup-unselected
4571                       (delq number gnus-newsgroup-unselected)))
4572             (push number gnus-newsgroup-ancient)))))))
4573
4574 (defun gnus-build-all-threads ()
4575   "Read all the headers."
4576   (let ((gnus-summary-ignore-duplicates t)
4577         (mail-parse-charset gnus-newsgroup-charset)
4578         (dependencies gnus-newsgroup-dependencies)
4579         header article)
4580     (with-current-buffer nntp-server-buffer
4581       (let ((case-fold-search nil))
4582         (goto-char (point-min))
4583         (while (not (eobp))
4584           (ignore-errors
4585             (setq article (read (current-buffer))
4586                   header (gnus-nov-parse-line article dependencies t)))
4587           (when header
4588             (with-current-buffer gnus-summary-buffer
4589               (push header gnus-newsgroup-headers)
4590               (if (memq (setq article (mail-header-number header))
4591                         gnus-newsgroup-unselected)
4592                   (progn
4593                     (setq gnus-newsgroup-unreads
4594                           (gnus-add-to-sorted-list
4595                            gnus-newsgroup-unreads article))
4596                     (setq gnus-newsgroup-unselected
4597                           (delq article gnus-newsgroup-unselected)))
4598                 (push article gnus-newsgroup-ancient)))
4599             (forward-line 1)))))))
4600
4601 (defun gnus-summary-update-article-line (article header)
4602   "Update the line for ARTICLE using HEADER."
4603   (let* ((id (mail-header-id header))
4604          (thread (gnus-id-to-thread id)))
4605     (unless thread
4606       (error "Article in no thread"))
4607     ;; Update the thread.
4608     (setcar thread header)
4609     (gnus-summary-goto-subject article)
4610     (let* ((datal (gnus-data-find-list article))
4611            (data (car datal))
4612            (inhibit-read-only t)
4613            (level (gnus-summary-thread-level)))
4614       (gnus-delete-line)
4615       (let ((inserted (- (point)
4616                          (progn
4617                            (gnus-summary-insert-line
4618                             header level nil
4619                             (memq article gnus-newsgroup-undownloaded)
4620                             (gnus-article-mark article)
4621                             (memq article gnus-newsgroup-replied)
4622                             (memq article gnus-newsgroup-expirable)
4623                             ;; Only insert the Subject string when it's different
4624                             ;; from the previous Subject string.
4625                             (if (and
4626                                  gnus-show-threads
4627                                  (gnus-subject-equal
4628                                   (condition-case ()
4629                                       (mail-header-subject
4630                                        (gnus-data-header
4631                                         (cadr
4632                                          (gnus-data-find-list
4633                                           article
4634                                           (gnus-data-list t)))))
4635                                     ;; Error on the side of excessive subjects.
4636                                     (error ""))
4637                                   (mail-header-subject header)))
4638                                 ""
4639                               (mail-header-subject header))
4640                             nil (cdr (assq article gnus-newsgroup-scored))
4641                             (memq article gnus-newsgroup-processable))
4642                            (point)))))
4643         (when (cdr datal)
4644           (gnus-data-update-list
4645            (cdr datal)
4646            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4647
4648 (defun gnus-summary-update-article (article &optional iheader)
4649   "Update ARTICLE in the summary buffer."
4650   (set-buffer gnus-summary-buffer)
4651   (let* ((header (gnus-summary-article-header article))
4652          (id (mail-header-id header))
4653          (data (gnus-data-find article))
4654          (thread (gnus-id-to-thread id))
4655          (references (mail-header-references header))
4656          (parent
4657           (gnus-id-to-thread
4658            (or (gnus-parent-id
4659                 (when (and references
4660                            (not (equal "" references)))
4661                   references))
4662                "none")))
4663          (inhibit-read-only t)
4664          (old (car thread)))
4665     (when thread
4666       (unless iheader
4667         (setcar thread nil)
4668         (when parent
4669           (delq thread parent)))
4670       (if (gnus-summary-insert-subject id header)
4671           ;; Set the (possibly) new article number in the data structure.
4672           (gnus-data-set-number data (gnus-id-to-article id))
4673         (setcar thread old)
4674         nil))))
4675
4676 (defun gnus-rebuild-thread (id &optional line)
4677   "Rebuild the thread containing ID.
4678 If LINE, insert the rebuilt thread starting on line LINE."
4679   (let ((inhibit-read-only t)
4680         old-pos current thread data)
4681     (if (not gnus-show-threads)
4682         (setq thread (list (car (gnus-id-to-thread id))))
4683       ;; Get the thread this article is part of.
4684       (setq thread (gnus-remove-thread id)))
4685     (setq old-pos (point-at-bol))
4686     (setq current (save-excursion
4687                     (and (re-search-backward "[\r\n]" nil t)
4688                          (gnus-summary-article-number))))
4689     ;; If this is a gathered thread, we have to go some re-gathering.
4690     (when (stringp (car thread))
4691       (let ((subject (car thread))
4692             roots thr)
4693         (setq thread (cdr thread))
4694         (while thread
4695           (unless (memq (setq thr (gnus-id-to-thread
4696                                    (gnus-root-id
4697                                     (mail-header-id (caar thread)))))
4698                         roots)
4699             (push thr roots))
4700           (setq thread (cdr thread)))
4701         ;; We now have all (unique) roots.
4702         (if (= (length roots) 1)
4703             ;; All the loose roots are now one solid root.
4704             (setq thread (car roots))
4705           (setq thread (cons subject (gnus-sort-threads roots))))))
4706     (let (threads)
4707       ;; We then insert this thread into the summary buffer.
4708       (when line
4709         (goto-char (point-min))
4710         (forward-line (1- line)))
4711       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4712         (if gnus-show-threads
4713             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4714           (gnus-summary-prepare-unthreaded thread))
4715         (setq data (nreverse gnus-newsgroup-data))
4716         (setq threads gnus-newsgroup-threads))
4717       ;; We splice the new data into the data structure.
4718       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4719       ;;!!! then we want to insert at the beginning of the buffer.
4720       ;;!!! That happens to be true with Gnus now, but that may
4721       ;;!!! change in the future.  Perhaps.
4722       (gnus-data-enter-list
4723        (if line nil current) data (- (point) old-pos))
4724       (setq gnus-newsgroup-threads
4725             (nconc threads gnus-newsgroup-threads))
4726       (gnus-data-compute-positions))))
4727
4728 (defun gnus-number-to-header (number)
4729   "Return the header for article NUMBER."
4730   (let ((headers gnus-newsgroup-headers))
4731     (while (and headers
4732                 (not (= number (mail-header-number (car headers)))))
4733       (pop headers))
4734     (when headers
4735       (car headers))))
4736
4737 (defun gnus-parent-headers (in-headers &optional generation)
4738   "Return the headers of the GENERATIONth parent of HEADERS."
4739   (unless generation
4740     (setq generation 1))
4741   (let ((parent t)
4742         (headers in-headers)
4743         references)
4744     (while (and parent
4745                 (not (zerop generation))
4746                 (setq references (mail-header-references headers)))
4747       (setq headers (if (and references
4748                              (setq parent (gnus-parent-id references)))
4749                         (car (gnus-id-to-thread parent))
4750                       nil))
4751       (decf generation))
4752     (and (not (eq headers in-headers))
4753          headers)))
4754
4755 (defun gnus-id-to-thread (id)
4756   "Return the (sub-)thread where ID appears."
4757   (gnus-gethash id gnus-newsgroup-dependencies))
4758
4759 (defun gnus-id-to-article (id)
4760   "Return the article number of ID."
4761   (let ((thread (gnus-id-to-thread id)))
4762     (when (and thread
4763                (car thread))
4764       (mail-header-number (car thread)))))
4765
4766 (defun gnus-id-to-header (id)
4767   "Return the article headers of ID."
4768   (car (gnus-id-to-thread id)))
4769
4770 (defun gnus-article-displayed-root-p (article)
4771   "Say whether ARTICLE is a root(ish) article."
4772   (let ((level (gnus-summary-thread-level article))
4773         (refs (mail-header-references  (gnus-summary-article-header article)))
4774         particle)
4775     (cond
4776      ((null level) nil)
4777      ((zerop level) t)
4778      ((null refs) t)
4779      ((null (gnus-parent-id refs)) t)
4780      ((and (= 1 level)
4781            (null (setq particle (gnus-id-to-article
4782                                  (gnus-parent-id refs))))
4783            (null (gnus-summary-thread-level particle)))))))
4784
4785 (defun gnus-root-id (id)
4786   "Return the id of the root of the thread where ID appears."
4787   (let (last-id prev)
4788     (while (and id (setq prev (car (gnus-id-to-thread id))))
4789       (setq last-id id
4790             id (gnus-parent-id (mail-header-references prev))))
4791     last-id))
4792
4793 (defun gnus-articles-in-thread (thread)
4794   "Return the list of articles in THREAD."
4795   (cons (mail-header-number (car thread))
4796         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4797
4798 (defun gnus-remove-thread (id &optional dont-remove)
4799   "Remove the thread that has ID in it."
4800   (let (headers thread last-id)
4801     ;; First go up in this thread until we find the root.
4802     (setq last-id (gnus-root-id id)
4803           headers (message-flatten-list (gnus-id-to-thread last-id)))
4804     ;; We have now found the real root of this thread.  It might have
4805     ;; been gathered into some loose thread, so we have to search
4806     ;; through the threads to find the thread we wanted.
4807     (let ((threads gnus-newsgroup-threads)
4808           sub)
4809       (while threads
4810         (setq sub (car threads))
4811         (if (stringp (car sub))
4812             ;; This is a gathered thread, so we look at the roots
4813             ;; below it to find whether this article is in this
4814             ;; gathered root.
4815             (progn
4816               (setq sub (cdr sub))
4817               (while sub
4818                 (when (member (caar sub) headers)
4819                   (setq thread (car threads)
4820                         threads nil
4821                         sub nil))
4822                 (setq sub (cdr sub))))
4823           ;; It's an ordinary thread, so we check it.
4824           (when (eq (car sub) (car headers))
4825             (setq thread sub
4826                   threads nil)))
4827         (setq threads (cdr threads)))
4828       ;; If this article is in no thread, then it's a root.
4829       (if thread
4830           (unless dont-remove
4831             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4832         (setq thread (gnus-id-to-thread last-id)))
4833       (when thread
4834         (prog1
4835             thread                      ; We return this thread.
4836           (unless dont-remove
4837             (if (stringp (car thread))
4838                 (progn
4839                   ;; If we use dummy roots, then we have to remove the
4840                   ;; dummy root as well.
4841                   (when (eq gnus-summary-make-false-root 'dummy)
4842                     ;; We go to the dummy root by going to
4843                     ;; the first sub-"thread", and then one line up.
4844                     (gnus-summary-goto-article
4845                      (mail-header-number (caadr thread)))
4846                     (forward-line -1)
4847                     (gnus-delete-line)
4848                     (gnus-data-compute-positions))
4849                   (setq thread (cdr thread))
4850                   (while thread
4851                     (gnus-remove-thread-1 (car thread))
4852                     (setq thread (cdr thread))))
4853               (gnus-remove-thread-1 thread))))))))
4854
4855 (defun gnus-remove-thread-1 (thread)
4856   "Remove the thread THREAD recursively."
4857   (let ((number (mail-header-number (pop thread)))
4858         d)
4859     (setq thread (reverse thread))
4860     (while thread
4861       (gnus-remove-thread-1 (pop thread)))
4862     (when (setq d (gnus-data-find number))
4863       (goto-char (gnus-data-pos d))
4864       (gnus-summary-show-thread)
4865       (gnus-data-remove
4866        number
4867        (- (point-at-bol)
4868           (prog1
4869               (1+ (point-at-eol))
4870             (gnus-delete-line)))))))
4871
4872 (defun gnus-sort-threads-recursive (threads func)
4873   (sort (mapcar (lambda (thread)
4874                   (cons (car thread)
4875                         (and (cdr thread)
4876                              (gnus-sort-threads-recursive (cdr thread) func))))
4877                 threads) func))
4878
4879 (defun gnus-sort-threads-loop (threads func)
4880   (let* ((superthread (cons nil threads))
4881          (stack (list (cons superthread threads)))
4882          remaining-threads thread)
4883     (while stack
4884       (setq remaining-threads (cdr (car stack)))
4885       (if remaining-threads
4886           (progn (setq thread (car remaining-threads))
4887                  (setcdr (car stack) (cdr remaining-threads))
4888                  (if (cdr thread)
4889                      (push (cons thread (cdr thread)) stack)))
4890         (setq thread (caar stack))
4891         (setcdr thread (sort (cdr thread) func))
4892         (pop stack)))
4893     (cdr superthread)))
4894
4895 (defun gnus-sort-threads (threads)
4896   "Sort THREADS."
4897   (if (not gnus-thread-sort-functions)
4898       threads
4899     (gnus-message 8 "Sorting threads...")
4900     (prog1
4901         (condition-case nil
4902             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4903               (gnus-sort-threads-recursive
4904                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4905           ;; Even after binding max-lisp-eval-depth, the recursive
4906           ;; sorter might fail for very long threads.  In that case,
4907           ;; try using a (less well-tested) non-recursive sorter.
4908           (error (gnus-message 9 "Sorting threads with loop...")
4909                  (gnus-sort-threads-loop
4910                   threads (gnus-make-sort-function
4911                            gnus-thread-sort-functions))))
4912       (gnus-message 8 "Sorting threads...done"))))
4913
4914 (defun gnus-sort-articles (articles)
4915   "Sort ARTICLES."
4916   (when gnus-article-sort-functions
4917     (gnus-message 7 "Sorting articles...")
4918     (prog1
4919         (setq gnus-newsgroup-headers
4920               (sort articles (gnus-make-sort-function
4921                               gnus-article-sort-functions)))
4922       (gnus-message 7 "Sorting articles...done"))))
4923
4924 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4925 (defmacro gnus-thread-header (thread)
4926   "Return header of first article in THREAD.
4927 Note that THREAD must never, ever be anything else than a variable -
4928 using some other form will lead to serious barfage."
4929   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4930   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4931   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4932         (vector thread) 2))
4933
4934 (defsubst gnus-article-sort-by-number (h1 h2)
4935   "Sort articles by article number."
4936   (< (mail-header-number h1)
4937      (mail-header-number h2)))
4938
4939 (defun gnus-thread-sort-by-number (h1 h2)
4940   "Sort threads by root article number."
4941   (gnus-article-sort-by-number
4942    (gnus-thread-header h1) (gnus-thread-header h2)))
4943
4944 (defsubst gnus-article-sort-by-random (h1 h2)
4945   "Sort articles randomly."
4946   (zerop (random 2)))
4947
4948 (defun gnus-thread-sort-by-random (h1 h2)
4949   "Sort threads randomly."
4950   (gnus-article-sort-by-random
4951    (gnus-thread-header h1) (gnus-thread-header h2)))
4952
4953 (defsubst gnus-article-sort-by-lines (h1 h2)
4954   "Sort articles by article Lines header."
4955   (< (mail-header-lines h1)
4956      (mail-header-lines h2)))
4957
4958 (defun gnus-thread-sort-by-lines (h1 h2)
4959   "Sort threads by root article Lines header."
4960   (gnus-article-sort-by-lines
4961    (gnus-thread-header h1) (gnus-thread-header h2)))
4962
4963 (defsubst gnus-article-sort-by-chars (h1 h2)
4964   "Sort articles by octet length."
4965   (< (mail-header-chars h1)
4966      (mail-header-chars h2)))
4967
4968 (defun gnus-thread-sort-by-chars (h1 h2)
4969   "Sort threads by root article octet length."
4970   (gnus-article-sort-by-chars
4971    (gnus-thread-header h1) (gnus-thread-header h2)))
4972
4973 (defsubst gnus-article-sort-by-author (h1 h2)
4974   "Sort articles by root author."
4975   (gnus-string<
4976    (let ((extract (funcall
4977                    gnus-extract-address-components
4978                    (mail-header-from h1))))
4979      (or (car extract) (cadr extract) ""))
4980    (let ((extract (funcall
4981                    gnus-extract-address-components
4982                    (mail-header-from h2))))
4983      (or (car extract) (cadr extract) ""))))
4984
4985 (defun gnus-thread-sort-by-author (h1 h2)
4986   "Sort threads by root author."
4987   (gnus-article-sort-by-author
4988    (gnus-thread-header h1)  (gnus-thread-header h2)))
4989
4990 (defsubst gnus-article-sort-by-recipient (h1 h2)
4991   "Sort articles by recipient."
4992   (gnus-string<
4993    (let ((extract (funcall
4994                    gnus-extract-address-components
4995                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4996      (or (car extract) (cadr extract)))
4997    (let ((extract (funcall
4998                    gnus-extract-address-components
4999                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
5000      (or (car extract) (cadr extract)))))
5001
5002 (defun gnus-thread-sort-by-recipient (h1 h2)
5003   "Sort threads by root recipient."
5004   (gnus-article-sort-by-recipient
5005    (gnus-thread-header h1) (gnus-thread-header h2)))
5006
5007 (defsubst gnus-article-sort-by-subject (h1 h2)
5008   "Sort articles by root subject."
5009   (gnus-string<
5010    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5011    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5012
5013 (defun gnus-thread-sort-by-subject (h1 h2)
5014   "Sort threads by root subject."
5015   (gnus-article-sort-by-subject
5016    (gnus-thread-header h1) (gnus-thread-header h2)))
5017
5018 (defsubst gnus-article-sort-by-date (h1 h2)
5019   "Sort articles by root article date."
5020   (time-less-p
5021    (gnus-date-get-time (mail-header-date h1))
5022    (gnus-date-get-time (mail-header-date h2))))
5023
5024 (defun gnus-thread-sort-by-date (h1 h2)
5025   "Sort threads by root article date."
5026   (gnus-article-sort-by-date
5027    (gnus-thread-header h1) (gnus-thread-header h2)))
5028
5029 (defsubst gnus-article-sort-by-score (h1 h2)
5030   "Sort articles by root article score.
5031 Unscored articles will be counted as having a score of zero."
5032   (> (or (cdr (assq (mail-header-number h1)
5033                     gnus-newsgroup-scored))
5034          gnus-summary-default-score 0)
5035      (or (cdr (assq (mail-header-number h2)
5036                     gnus-newsgroup-scored))
5037          gnus-summary-default-score 0)))
5038
5039 (defun gnus-thread-sort-by-score (h1 h2)
5040   "Sort threads by root article score."
5041   (gnus-article-sort-by-score
5042    (gnus-thread-header h1) (gnus-thread-header h2)))
5043
5044 (defun gnus-thread-sort-by-total-score (h1 h2)
5045   "Sort threads by the sum of all scores in the thread.
5046 Unscored articles will be counted as having a score of zero."
5047   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5048
5049 (defun gnus-thread-total-score (thread)
5050   ;; This function find the total score of THREAD.
5051   (cond
5052    ((null thread)
5053     0)
5054    ((consp thread)
5055     (if (stringp (car thread))
5056         (apply gnus-thread-score-function 0
5057                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5058       (gnus-thread-total-score-1 thread)))
5059    (t
5060     (gnus-thread-total-score-1 (list thread)))))
5061
5062 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5063   "Sort articles by number."
5064   (gnus-article-sort-by-number h1 h2))
5065
5066 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5067   "Sort threads such that the thread with the most recently arrived article comes first."
5068   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5069
5070 (defun gnus-thread-highest-number (thread)
5071   "Return the highest article number in THREAD."
5072   (apply 'max (mapcar (lambda (header)
5073                         (mail-header-number header))
5074                       (message-flatten-list thread))))
5075
5076 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5077   "Sort articles by number."
5078   (gnus-article-sort-by-date h1 h2))
5079
5080 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5081   "Sort threads such that the thread with the most recently dated article comes first."
5082   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5083
5084 ; Since this is called not only to sort the top-level threads, but
5085 ; also in recursive sorts to order the articles within a thread, each
5086 ; article will be processed many times.  Thus it speeds things up
5087 ; quite a bit to use gnus-date-get-time, which caches the time value.
5088 (defun gnus-thread-latest-date (thread)
5089   "Return the highest article date in THREAD."
5090   (apply 'max
5091          (mapcar (lambda (header) (gnus-float-time
5092                                    (gnus-date-get-time
5093                                     (mail-header-date header))))
5094                  (message-flatten-list thread))))
5095
5096 (defun gnus-thread-total-score-1 (root)
5097   ;; This function find the total score of the thread below ROOT.
5098   (setq root (car root))
5099   (apply gnus-thread-score-function
5100          (or (append
5101               (mapcar 'gnus-thread-total-score
5102                       (cdr (gnus-id-to-thread (mail-header-id root))))
5103               (when (> (mail-header-number root) 0)
5104                 (list (or (cdr (assq (mail-header-number root)
5105                                      gnus-newsgroup-scored))
5106                           gnus-summary-default-score 0))))
5107              (list gnus-summary-default-score)
5108              '(0))))
5109
5110 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5111 (defvar gnus-tmp-prev-subject nil)
5112 (defvar gnus-tmp-false-parent nil)
5113 (defvar gnus-tmp-root-expunged nil)
5114 (defvar gnus-tmp-dummy-line nil)
5115
5116 (defun gnus-extra-header (type &optional header)
5117   "Return the extra header of TYPE."
5118   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5119       ""))
5120
5121 (defvar gnus-tmp-thread-tree-header-string "")
5122
5123 (defcustom gnus-sum-thread-tree-root "> "
5124   "With %B spec, used for the root of a thread.
5125 If nil, use subject instead."
5126   :version "22.1"
5127   :type '(radio (const :format "%v  " nil) string)
5128   :group 'gnus-thread)
5129
5130 (defcustom gnus-sum-thread-tree-false-root "> "
5131   "With %B spec, used for a false root of a thread.
5132 If nil, use subject instead."
5133   :version "22.1"
5134   :type '(radio (const :format "%v  " nil) string)
5135   :group 'gnus-thread)
5136
5137 (defcustom gnus-sum-thread-tree-single-indent ""
5138   "With %B spec, used for a thread with just one message.
5139 If nil, use subject instead."
5140   :version "22.1"
5141   :type '(radio (const :format "%v  " nil) string)
5142   :group 'gnus-thread)
5143
5144 (defcustom gnus-sum-thread-tree-vertical "| "
5145   "With %B spec, used for drawing a vertical line."
5146   :version "22.1"
5147   :type 'string
5148   :group 'gnus-thread)
5149
5150 (defcustom gnus-sum-thread-tree-indent "  "
5151   "With %B spec, used for indenting."
5152   :version "22.1"
5153   :type 'string
5154   :group 'gnus-thread)
5155
5156 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5157   "With %B spec, used for a leaf with brothers."
5158   :version "22.1"
5159   :type 'string
5160   :group 'gnus-thread)
5161
5162 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5163   "With %B spec, used for a leaf without brothers."
5164   :version "22.1"
5165   :type 'string
5166   :group 'gnus-thread)
5167
5168 (defcustom gnus-summary-display-while-building nil
5169   "If non-nil, show and update the summary buffer as it's being built.
5170 If the value is t, update the buffer after every line is inserted.  If
5171 the value is an integer (N), update the display every N lines."
5172   :version "22.1"
5173   :group 'gnus-thread
5174   :type '(choice (const :tag "off" nil)
5175                  number
5176                  (const :tag "frequently" t)))
5177
5178 (defun gnus-summary-prepare-threads (threads)
5179   "Prepare summary buffer from THREADS and indentation LEVEL.
5180 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5181 or a straight list of headers."
5182   (gnus-message 7 "Generating summary...")
5183
5184   (setq gnus-newsgroup-threads threads)
5185   (beginning-of-line)
5186
5187   (let ((gnus-tmp-level 0)
5188         (default-score (or gnus-summary-default-score 0))
5189         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5190         (building-line-count gnus-summary-display-while-building)
5191         (building-count (integerp gnus-summary-display-while-building))
5192         thread number subject stack state gnus-tmp-gathered beg-match
5193         new-roots gnus-tmp-new-adopts thread-end simp-subject
5194         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5195         gnus-tmp-replied gnus-tmp-subject-or-nil
5196         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5197         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5198         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5199         tree-stack)
5200
5201     (setq gnus-tmp-prev-subject nil
5202           gnus-tmp-thread-tree-header-string "")
5203
5204     (if (vectorp (car threads))
5205         ;; If this is a straight (sic) list of headers, then a
5206         ;; threaded summary display isn't required, so we just create
5207         ;; an unthreaded one.
5208         (gnus-summary-prepare-unthreaded threads)
5209
5210       ;; Do the threaded display.
5211
5212       (if gnus-summary-display-while-building
5213           (switch-to-buffer (buffer-name)))
5214       (while (or threads stack gnus-tmp-new-adopts new-roots)
5215
5216         (if (and (= gnus-tmp-level 0)
5217                  (or (not stack)
5218                      (= (caar stack) 0))
5219                  (not gnus-tmp-false-parent)
5220                  (or gnus-tmp-new-adopts new-roots))
5221             (if gnus-tmp-new-adopts
5222                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5223                       thread (list (car gnus-tmp-new-adopts))
5224                       gnus-tmp-header (caar thread)
5225                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5226               (when new-roots
5227                 (setq thread (list (car new-roots))
5228                       gnus-tmp-header (caar thread)
5229                       new-roots (cdr new-roots))))
5230
5231           (if threads
5232               ;; If there are some threads, we do them before the
5233               ;; threads on the stack.
5234               (setq thread threads
5235                     gnus-tmp-header (caar thread))
5236             ;; There were no current threads, so we pop something off
5237             ;; the stack.
5238             (setq state (car stack)
5239                   gnus-tmp-level (car state)
5240                   tree-stack (cadr state)
5241                   thread (caddr state)
5242                   stack (cdr stack)
5243                   gnus-tmp-header (caar thread))))
5244
5245         (setq gnus-tmp-false-parent nil)
5246         (setq gnus-tmp-root-expunged nil)
5247         (setq thread-end nil)
5248
5249         (if (stringp gnus-tmp-header)
5250             ;; The header is a dummy root.
5251             (cond
5252              ((eq gnus-summary-make-false-root 'adopt)
5253               ;; We let the first article adopt the rest.
5254               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5255                                                (cddar thread)))
5256               (setq gnus-tmp-gathered
5257                     (nconc (mapcar
5258                             (lambda (h) (mail-header-number (car h)))
5259                             (cddar thread))
5260                            gnus-tmp-gathered))
5261               (setq thread (cons (list (caar thread)
5262                                        (cadar thread))
5263                                  (cdr thread)))
5264               (setq gnus-tmp-level -1
5265                     gnus-tmp-false-parent t))
5266              ((eq gnus-summary-make-false-root 'empty)
5267               ;; We print adopted articles with empty subject fields.
5268               (setq gnus-tmp-gathered
5269                     (nconc (mapcar
5270                             (lambda (h) (mail-header-number (car h)))
5271                             (cddar thread))
5272                            gnus-tmp-gathered))
5273               (setq gnus-tmp-level -1))
5274              ((eq gnus-summary-make-false-root 'dummy)
5275               ;; We remember that we probably want to output a dummy
5276               ;; root.
5277               (setq gnus-tmp-dummy-line gnus-tmp-header)
5278               (setq gnus-tmp-prev-subject gnus-tmp-header))
5279              (t
5280               ;; We do not make a root for the gathered
5281               ;; sub-threads at all.
5282               (setq gnus-tmp-level -1)))
5283
5284           (setq number (mail-header-number gnus-tmp-header)
5285                 subject (mail-header-subject gnus-tmp-header)
5286                 simp-subject (gnus-simplify-subject-fully subject))
5287
5288           (cond
5289            ;; If the thread has changed subject, we might want to make
5290            ;; this subthread into a root.
5291            ((and (null gnus-thread-ignore-subject)
5292                  (not (zerop gnus-tmp-level))
5293                  gnus-tmp-prev-subject
5294                  (not (string= gnus-tmp-prev-subject simp-subject)))
5295             (setq new-roots (nconc new-roots (list (car thread)))
5296                   thread-end t
5297                   gnus-tmp-header nil))
5298            ;; If the article lies outside the current limit,
5299            ;; then we do not display it.
5300            ((not (memq number gnus-newsgroup-limit))
5301             (setq gnus-tmp-gathered
5302                   (nconc (mapcar
5303                           (lambda (h) (mail-header-number (car h)))
5304                           (cdar thread))
5305                          gnus-tmp-gathered))
5306             (setq gnus-tmp-new-adopts (if (cdar thread)
5307                                           (append gnus-tmp-new-adopts
5308                                                   (cdar thread))
5309                                         gnus-tmp-new-adopts)
5310                   thread-end t
5311                   gnus-tmp-header nil)
5312             (when (zerop gnus-tmp-level)
5313               (setq gnus-tmp-root-expunged t)))
5314            ;; Perhaps this article is to be marked as read?
5315            ((and gnus-summary-mark-below
5316                  (< (or (cdr (assq number gnus-newsgroup-scored))
5317                         default-score)
5318                     gnus-summary-mark-below)
5319                  ;; Don't touch sparse articles.
5320                  (not (gnus-summary-article-sparse-p number))
5321                  (not (gnus-summary-article-ancient-p number)))
5322             (setq gnus-newsgroup-unreads
5323                   (delq number gnus-newsgroup-unreads))
5324             (if gnus-newsgroup-auto-expire
5325                 (setq gnus-newsgroup-expirable
5326                       (gnus-add-to-sorted-list
5327                        gnus-newsgroup-expirable number))
5328               (push (cons number gnus-low-score-mark)
5329                     gnus-newsgroup-reads))))
5330
5331           (when gnus-tmp-header
5332             ;; We may have an old dummy line to output before this
5333             ;; article.
5334             (when (and gnus-tmp-dummy-line
5335                        (gnus-subject-equal
5336                         gnus-tmp-dummy-line
5337                         (mail-header-subject gnus-tmp-header)))
5338               (gnus-summary-insert-dummy-line
5339                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5340               (setq gnus-tmp-dummy-line nil))
5341
5342             ;; Compute the mark.
5343             (setq gnus-tmp-unread (gnus-article-mark number))
5344
5345             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5346                                   gnus-tmp-header gnus-tmp-level)
5347                   gnus-newsgroup-data)
5348
5349             ;; Actually insert the line.
5350             (setq
5351              gnus-tmp-subject-or-nil
5352              (cond
5353               ((and gnus-thread-ignore-subject
5354                     gnus-tmp-prev-subject
5355                     (not (string= gnus-tmp-prev-subject simp-subject)))
5356                subject)
5357               ((zerop gnus-tmp-level)
5358                (if (and (eq gnus-summary-make-false-root 'empty)
5359                         (memq number gnus-tmp-gathered)
5360                         gnus-tmp-prev-subject
5361                         (string= gnus-tmp-prev-subject simp-subject))
5362                    gnus-summary-same-subject
5363                  subject))
5364               (t gnus-summary-same-subject)))
5365             (if (and (eq gnus-summary-make-false-root 'adopt)
5366                      (= gnus-tmp-level 1)
5367                      (memq number gnus-tmp-gathered))
5368                 (setq gnus-tmp-opening-bracket ?\<
5369                       gnus-tmp-closing-bracket ?\>)
5370               (setq gnus-tmp-opening-bracket ?\[
5371                     gnus-tmp-closing-bracket ?\]))
5372             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5373                 (gnus-make-thread-indent-array
5374                  (max (* 2 (length gnus-thread-indent-array))
5375                       gnus-tmp-level)))
5376             (setq
5377              gnus-tmp-indentation
5378              (aref gnus-thread-indent-array gnus-tmp-level)
5379              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5380              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5381                                 gnus-summary-default-score 0)
5382              gnus-tmp-score-char
5383              (if (or (null gnus-summary-default-score)
5384                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5385                          gnus-summary-zcore-fuzz))
5386                  ?                      ;Whitespace
5387                (if (< gnus-tmp-score gnus-summary-default-score)
5388                    gnus-score-below-mark gnus-score-over-mark))
5389              gnus-tmp-replied
5390              (cond ((memq number gnus-newsgroup-processable)
5391                     gnus-process-mark)
5392                    ((memq number gnus-newsgroup-cached)
5393                     gnus-cached-mark)
5394                    ((memq number gnus-newsgroup-replied)
5395                     gnus-replied-mark)
5396                    ((memq number gnus-newsgroup-forwarded)
5397                     gnus-forwarded-mark)
5398                    ((memq number gnus-newsgroup-saved)
5399                     gnus-saved-mark)
5400                    ((memq number gnus-newsgroup-unseen)
5401                     gnus-unseen-mark)
5402                    (t gnus-no-mark))
5403              gnus-tmp-downloaded
5404              (cond ((memq number gnus-newsgroup-undownloaded)
5405                     gnus-undownloaded-mark)
5406                    (gnus-newsgroup-agentized
5407                     gnus-downloaded-mark)
5408                    (t
5409                     gnus-no-mark))
5410              gnus-tmp-from (mail-header-from gnus-tmp-header)
5411              gnus-tmp-name
5412              (cond
5413               ((string-match "<[^>]+> *$" gnus-tmp-from)
5414                (setq beg-match (match-beginning 0))
5415                (or (and (string-match "^\".+\"" gnus-tmp-from)
5416                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5417                    (substring gnus-tmp-from 0 beg-match)))
5418               ((string-match "(.+)" gnus-tmp-from)
5419                (substring gnus-tmp-from
5420                           (1+ (match-beginning 0)) (1- (match-end 0))))
5421               (t gnus-tmp-from))
5422
5423              ;; Do the %B string
5424              gnus-tmp-thread-tree-header-string
5425              (cond
5426               ((not gnus-show-threads) "")
5427               ((zerop gnus-tmp-level)
5428                (cond ((cdar thread)
5429                       (or gnus-sum-thread-tree-root subject))
5430                      (gnus-tmp-new-adopts
5431                       (or gnus-sum-thread-tree-false-root subject))
5432                      (t
5433                       (or gnus-sum-thread-tree-single-indent subject))))
5434               (t
5435                (concat (apply 'concat
5436                               (mapcar (lambda (item)
5437                                         (if (= item 1)
5438                                             gnus-sum-thread-tree-vertical
5439                                           gnus-sum-thread-tree-indent))
5440                                       (cdr (reverse tree-stack))))
5441                        (if (nth 1 thread)
5442                            gnus-sum-thread-tree-leaf-with-other
5443                          gnus-sum-thread-tree-single-leaf)))))
5444             (when (string= gnus-tmp-name "")
5445               (setq gnus-tmp-name gnus-tmp-from))
5446             (unless (numberp gnus-tmp-lines)
5447               (setq gnus-tmp-lines -1))
5448             (if (= gnus-tmp-lines -1)
5449                 (setq gnus-tmp-lines "?")
5450               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5451             (gnus-put-text-property
5452              (point)
5453              (progn (eval gnus-summary-line-format-spec) (point))
5454              'gnus-number number)
5455             (when gnus-visual-p
5456               (forward-line -1)
5457               (gnus-summary-highlight-line)
5458               (when gnus-summary-update-hook
5459                 (gnus-run-hooks 'gnus-summary-update-hook))
5460               (forward-line 1))
5461
5462             (setq gnus-tmp-prev-subject simp-subject)))
5463
5464         (when (nth 1 thread)
5465           (push (list (max 0 gnus-tmp-level)
5466                       (copy-sequence tree-stack)
5467                       (nthcdr 1 thread))
5468                 stack))
5469         (push (if (nth 1 thread) 1 0) tree-stack)
5470         (incf gnus-tmp-level)
5471         (setq threads (if thread-end nil (cdar thread)))
5472         (if gnus-summary-display-while-building
5473             (if building-count
5474                 (progn
5475                   ;; use a set frequency
5476                   (setq building-line-count (1- building-line-count))
5477                   (when (= building-line-count 0)
5478                     (sit-for 0)
5479                     (setq building-line-count
5480                           gnus-summary-display-while-building)))
5481               ;; always
5482               (sit-for 0)))
5483         (unless threads
5484           (setq gnus-tmp-level 0)))))
5485   (gnus-message 7 "Generating summary...done"))
5486
5487 (defun gnus-summary-prepare-unthreaded (headers)
5488   "Generate an unthreaded summary buffer based on HEADERS."
5489   (let (header number mark)
5490
5491     (beginning-of-line)
5492
5493     (while headers
5494       ;; We may have to root out some bad articles...
5495       (when (memq (setq number (mail-header-number
5496                                 (setq header (pop headers))))
5497                   gnus-newsgroup-limit)
5498         ;; Mark article as read when it has a low score.
5499         (when (and gnus-summary-mark-below
5500                    (< (or (cdr (assq number gnus-newsgroup-scored))
5501                           gnus-summary-default-score 0)
5502                       gnus-summary-mark-below)
5503                    (not (gnus-summary-article-ancient-p number)))
5504           (setq gnus-newsgroup-unreads
5505                 (delq number gnus-newsgroup-unreads))
5506           (if gnus-newsgroup-auto-expire
5507               (push number gnus-newsgroup-expirable)
5508             (push (cons number gnus-low-score-mark)
5509                   gnus-newsgroup-reads)))
5510
5511         (setq mark (gnus-article-mark number))
5512         (push (gnus-data-make number mark (1+ (point)) header 0)
5513               gnus-newsgroup-data)
5514         (gnus-summary-insert-line
5515          header 0 number
5516          (memq number gnus-newsgroup-undownloaded)
5517          mark (memq number gnus-newsgroup-replied)
5518          (memq number gnus-newsgroup-expirable)
5519          (mail-header-subject header) nil
5520          (cdr (assq number gnus-newsgroup-scored))
5521          (memq number gnus-newsgroup-processable))))))
5522
5523 (defun gnus-group-get-list-identifiers (group)
5524   "Get list identifier regexp for GROUP."
5525   (or (gnus-parameter-list-identifier group)
5526       (if (consp gnus-list-identifiers)
5527           (mapconcat 'identity gnus-list-identifiers " *\\|")
5528         gnus-list-identifiers)))
5529
5530 (defun gnus-summary-remove-list-identifiers ()
5531   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5532   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5533         changed subject)
5534     (when regexp
5535       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5536       (dolist (header gnus-newsgroup-headers)
5537         (setq subject (mail-header-subject header)
5538               changed nil)
5539         (while (string-match regexp subject)
5540           (setq subject
5541                 (concat (substring subject 0 (match-beginning 1))
5542                         (substring subject (match-end 0)))
5543                 changed t))
5544         (when changed
5545           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5546             (setq subject
5547                   (concat (substring subject 0 (match-beginning 1))
5548                           (substring subject (match-end 1)))))
5549           (mail-header-set-subject header subject))))))
5550
5551 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5552   "Fetch headers of ARTICLES."
5553   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5554     (gnus-message 7 "Fetching headers for %s..." name)
5555     (prog1
5556         (if (eq 'nov
5557                 (setq gnus-headers-retrieved-by
5558                       (gnus-retrieve-headers
5559                        articles gnus-newsgroup-name
5560                        (or limit
5561                            ;; We might want to fetch old headers, but
5562                            ;; not if there is only 1 article.
5563                            (and (or (and
5564                                      (not (eq gnus-fetch-old-headers 'some))
5565                                      (not (numberp gnus-fetch-old-headers)))
5566                                     (> (length articles) 1))
5567                                 gnus-fetch-old-headers)))))
5568             (gnus-get-newsgroup-headers-xover
5569              articles force-new dependencies gnus-newsgroup-name t)
5570           (gnus-get-newsgroup-headers dependencies force-new))
5571       (gnus-message 7 "Fetching headers for %s...done" name))))
5572
5573 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5574   "Select newsgroup GROUP.
5575 If READ-ALL is non-nil, all articles in the group are selected.
5576 If SELECT-ARTICLES, only select those articles from GROUP."
5577   (let* ((entry (gnus-group-entry group))
5578          ;;!!! Dirty hack; should be removed.
5579          (gnus-summary-ignore-duplicates
5580           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5581               t
5582             gnus-summary-ignore-duplicates))
5583          (info (nth 2 entry))
5584          charset articles fetched-articles cached)
5585
5586     (unless (gnus-check-server
5587              (set (make-local-variable 'gnus-current-select-method)
5588                   (gnus-find-method-for-group group)))
5589       (error "Couldn't open server"))
5590     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5591
5592     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5593         (gnus-activate-group group)     ; Or we can activate it...
5594         (progn                          ; Or we bug out.
5595           (when (equal major-mode 'gnus-summary-mode)
5596             (gnus-kill-buffer (current-buffer)))
5597           (error
5598            "Couldn't activate group %s: %s"
5599            (mm-decode-coding-string group charset)
5600            (mm-decode-coding-string (gnus-status-message group) charset))))
5601
5602     (unless (gnus-request-group group t)
5603       (when (equal major-mode 'gnus-summary-mode)
5604         (gnus-kill-buffer (current-buffer)))
5605       (error "Couldn't request group %s: %s"
5606              (mm-decode-coding-string group charset)
5607              (mm-decode-coding-string (gnus-status-message group) charset)))
5608
5609     (when (and gnus-agent
5610                (gnus-active group))
5611       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5612
5613       (setq gnus-summary-use-undownloaded-faces
5614             (gnus-agent-find-parameter
5615              group
5616              'agent-enable-undownloaded-faces)))
5617
5618     (setq gnus-newsgroup-name group
5619           gnus-newsgroup-unselected nil
5620           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5621
5622     (let ((display (gnus-group-find-parameter group 'display)))
5623       (setq gnus-newsgroup-display
5624             (cond
5625              ((not (zerop (or (car-safe read-all) 0)))
5626               ;; The user entered the group with C-u SPC/RET, let's show
5627               ;; all articles.
5628               'gnus-not-ignore)
5629              ((eq display 'all)
5630               'gnus-not-ignore)
5631              ((arrayp display)
5632               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5633              ((numberp display)
5634               ;; The following is probably the "correct" solution, but
5635               ;; it makes Gnus fetch all headers and then limit the
5636               ;; articles (which is slow), so instead we hack the
5637               ;; select-articles parameter instead. -- Simon Josefsson
5638               ;; <jas@kth.se>
5639               ;;
5640               ;; (gnus-byte-compile
5641               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5642               ;;                         display)))))
5643               (setq select-articles
5644                     (gnus-uncompress-range
5645                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5646                              (if (> tmp 0)
5647                                  tmp
5648                                1))
5649                            (cdr (gnus-active group)))))
5650               nil)
5651              (t
5652               nil))))
5653
5654     (gnus-summary-setup-default-charset)
5655
5656     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5657     (when (gnus-virtual-group-p group)
5658       (setq cached gnus-newsgroup-cached))
5659
5660     (setq gnus-newsgroup-unreads
5661           (gnus-sorted-ndifference
5662            (gnus-sorted-ndifference gnus-newsgroup-unreads
5663                                     gnus-newsgroup-marked)
5664            gnus-newsgroup-dormant))
5665
5666     (setq gnus-newsgroup-processable nil)
5667
5668     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5669
5670     ;; Adjust and set lists of article marks.
5671     (when info
5672       (gnus-adjust-marked-articles info))
5673     (if (setq articles select-articles)
5674         (setq gnus-newsgroup-unselected
5675               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5676       (setq articles (gnus-articles-to-read group read-all)))
5677
5678     (cond
5679      ((null articles)
5680       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5681       'quit)
5682      ((eq articles 0) nil)
5683      (t
5684       ;; Init the dependencies hash table.
5685       (setq gnus-newsgroup-dependencies
5686             (gnus-make-hashtable (length articles)))
5687       (gnus-set-global-variables)
5688       ;; Retrieve the headers and read them in.
5689
5690       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5691
5692       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5693       (when cached
5694         (setq gnus-newsgroup-cached cached))
5695
5696       ;; Suppress duplicates?
5697       (when gnus-suppress-duplicates
5698         (gnus-dup-suppress-articles))
5699
5700       ;; Set the initial limit.
5701       (setq gnus-newsgroup-limit (copy-sequence articles))
5702       ;; Remove canceled articles from the list of unread articles.
5703       (setq fetched-articles
5704             (mapcar (lambda (headers) (mail-header-number headers))
5705                     gnus-newsgroup-headers))
5706       (setq gnus-newsgroup-articles fetched-articles)
5707       (setq gnus-newsgroup-unreads
5708             (gnus-sorted-nintersection
5709              gnus-newsgroup-unreads fetched-articles))
5710       (gnus-compute-unseen-list)
5711
5712       ;; Removed marked articles that do not exist.
5713       (gnus-update-missing-marks
5714        (gnus-sorted-difference articles fetched-articles))
5715       ;; We might want to build some more threads first.
5716       (when (and gnus-fetch-old-headers
5717                  (eq gnus-headers-retrieved-by 'nov))
5718         (if (eq gnus-fetch-old-headers 'invisible)
5719             (gnus-build-all-threads)
5720           (gnus-build-old-threads)))
5721       ;; Let the Gnus agent mark articles as read.
5722       (when gnus-agent
5723         (gnus-agent-get-undownloaded-list))
5724       ;; Remove list identifiers from subject
5725       (gnus-summary-remove-list-identifiers)
5726       ;; Check whether auto-expire is to be done in this group.
5727       (setq gnus-newsgroup-auto-expire
5728             (and (gnus-group-auto-expirable-p group)
5729                  (not (gnus-group-read-only-p group))))
5730       ;; Set up the article buffer now, if necessary.
5731       (unless (and gnus-single-article-buffer
5732                    (equal gnus-article-buffer "*Article*"))
5733         (gnus-article-setup-buffer))
5734       ;; First and last article in this newsgroup.
5735       (when gnus-newsgroup-headers
5736         (setq gnus-newsgroup-begin
5737               (mail-header-number (car gnus-newsgroup-headers))
5738               gnus-newsgroup-end
5739               (mail-header-number
5740                (gnus-last-element gnus-newsgroup-headers))))
5741       ;; GROUP is successfully selected.
5742       (or gnus-newsgroup-headers t)))))
5743
5744 (defun gnus-compute-unseen-list ()
5745   ;; The `seen' marks are treated specially.
5746   (if (not gnus-newsgroup-seen)
5747       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5748     (setq gnus-newsgroup-unseen
5749           (gnus-inverse-list-range-intersection
5750            gnus-newsgroup-articles gnus-newsgroup-seen))))
5751
5752 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5753
5754 (defun gnus-summary-display-make-predicate (display)
5755   (require 'gnus-agent)
5756   (when (= (length display) 1)
5757     (setq display (car display)))
5758   (unless gnus-summary-display-cache
5759     (dolist (elem (append '((unread . unread)
5760                             (read . read)
5761                             (unseen . unseen))
5762                           gnus-article-mark-lists))
5763       (push (cons (cdr elem)
5764                   (gnus-byte-compile    ;Why bother?
5765                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5766             gnus-summary-display-cache)))
5767   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5768         (gnus-category-predicate-cache gnus-summary-display-cache))
5769     (gnus-get-predicate display)))
5770
5771 ;; Uses the dynamically bound `gnus-number' variable.
5772 (defvar gnus-number)
5773 (defun gnus-article-marked-p (type &optional article)
5774   (let ((article (or article gnus-number)))
5775     (cond
5776      ((eq type 'tick)
5777       (memq article gnus-newsgroup-marked))
5778      ((eq type 'spam)
5779       (memq article gnus-newsgroup-spam-marked))
5780      ((eq type 'unsend)
5781       (memq article gnus-newsgroup-unsendable))
5782      ((eq type 'undownload)
5783       (memq article gnus-newsgroup-undownloaded))
5784      ((eq type 'download)
5785       (memq article gnus-newsgroup-downloadable))
5786      ((eq type 'unread)
5787       (memq article gnus-newsgroup-unreads))
5788      ((eq type 'read)
5789       (memq article gnus-newsgroup-reads))
5790      ((eq type 'dormant)
5791       (memq article gnus-newsgroup-dormant) )
5792      ((eq type 'expire)
5793       (memq article gnus-newsgroup-expirable))
5794      ((eq type 'reply)
5795       (memq article gnus-newsgroup-replied))
5796      ((eq type 'killed)
5797       (memq article gnus-newsgroup-killed))
5798      ((eq type 'bookmark)
5799       (assq article gnus-newsgroup-bookmarks))
5800      ((eq type 'score)
5801       (assq article gnus-newsgroup-scored))
5802      ((eq type 'save)
5803       (memq article gnus-newsgroup-saved))
5804      ((eq type 'cache)
5805       (memq article gnus-newsgroup-cached))
5806      ((eq type 'forward)
5807       (memq article gnus-newsgroup-forwarded))
5808      ((eq type 'seen)
5809       (not (memq article gnus-newsgroup-unseen)))
5810      (t t))))
5811
5812 (defun gnus-articles-to-read (group &optional read-all)
5813   "Find out what articles the user wants to read."
5814   (let* ((only-read-p t)
5815          (articles
5816           ;; Select all articles if `read-all' is non-nil, or if there
5817           ;; are no unread articles.
5818           (if (or read-all
5819                   (and (zerop (length gnus-newsgroup-marked))
5820                        (zerop (length gnus-newsgroup-unreads)))
5821                   ;; Fetch all if the predicate is non-nil.
5822                   gnus-newsgroup-display)
5823               ;; We want to select the headers for all the articles in
5824               ;; the group, so we select either all the active
5825               ;; articles in the group, or (if that's nil), the
5826               ;; articles in the cache.
5827               (or
5828                (if gnus-newsgroup-maximum-articles
5829                    (let ((active (gnus-active group)))
5830                      (gnus-uncompress-range
5831                       (cons (max (car active)
5832                                  (- (cdr active)
5833                                     gnus-newsgroup-maximum-articles
5834                                     -1))
5835                             (cdr active))))
5836                  (gnus-uncompress-range (gnus-active group)))
5837                (gnus-cache-articles-in-group group))
5838             ;; Select only the "normal" subset of articles.
5839             (setq only-read-p nil)
5840             (gnus-sorted-nunion
5841              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5842              gnus-newsgroup-unreads)))
5843          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5844          (scored (length scored-list))
5845          (number (length articles))
5846          (marked (+ (length gnus-newsgroup-marked)
5847                     (length gnus-newsgroup-dormant)))
5848          (select
5849           (cond
5850            ((numberp read-all)
5851             read-all)
5852            ((numberp gnus-newsgroup-display)
5853             gnus-newsgroup-display)
5854            (t
5855             (condition-case ()
5856                 (cond
5857                  ((and (or (<= scored marked) (= scored number))
5858                        (numberp gnus-large-newsgroup)
5859                        (> number gnus-large-newsgroup))
5860                   (let* ((cursor-in-echo-area nil)
5861                          (initial (gnus-parameter-large-newsgroup-initial
5862                                    gnus-newsgroup-name))
5863                          (default (if only-read-p
5864                                       (or initial gnus-large-newsgroup)
5865                                     number))
5866                          (input
5867                           (read-string
5868                            (if only-read-p
5869                                (format
5870                                 "How many articles from %s (available %d, default %d): "
5871                                 (gnus-group-decoded-name
5872                                  (gnus-group-real-name gnus-newsgroup-name))
5873                                 number default)
5874                              (format
5875                               "How many articles from %s (%d default): "
5876                               (gnus-group-decoded-name
5877                                (gnus-group-real-name gnus-newsgroup-name))
5878                               default))
5879                            nil
5880                            nil
5881                            (number-to-string default))))
5882                     (if (string-match "^[ \t]*$" input) number input)))
5883                  ((and (> scored marked) (< scored number)
5884                        (> (- scored number) 20))
5885                   (let ((input
5886                          (read-string
5887                           (format "%s %s (%d scored, %d total): "
5888                                   "How many articles from"
5889                                   (gnus-group-decoded-name
5890                                    (gnus-group-real-name gnus-newsgroup-name))
5891                                   scored number))))
5892                     (if (string-match "^[ \t]*$" input)
5893                         number input)))
5894                  (t number))
5895               (quit
5896                (message "Quit getting the articles to read")
5897                nil))))))
5898     (setq select (if (stringp select) (string-to-number select) select))
5899     (if (or (null select) (zerop select))
5900         select
5901       (if (and (not (zerop scored)) (<= (abs select) scored))
5902           (progn
5903             (setq articles (sort scored-list '<))
5904             (setq number (length articles)))
5905         (setq articles (copy-sequence articles)))
5906
5907       (when (< (abs select) number)
5908         (if (< select 0)
5909             ;; Select the N oldest articles.
5910             (setcdr (nthcdr (1- (abs select)) articles) nil)
5911           ;; Select the N most recent articles.
5912           (setq articles (nthcdr (- number select) articles))))
5913       (setq gnus-newsgroup-unselected
5914             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5915       (when gnus-alter-articles-to-read-function
5916         (setq articles
5917               (sort
5918                (funcall gnus-alter-articles-to-read-function
5919                         gnus-newsgroup-name articles)
5920                '<)))
5921       articles)))
5922
5923 (defun gnus-killed-articles (killed articles)
5924   (let (out)
5925     (while articles
5926       (when (inline (gnus-member-of-range (car articles) killed))
5927         (push (car articles) out))
5928       (setq articles (cdr articles)))
5929     out))
5930
5931 (defun gnus-uncompress-marks (marks)
5932   "Uncompress the mark ranges in MARKS."
5933   (let ((uncompressed '(score bookmark))
5934         out)
5935     (while marks
5936       (if (memq (caar marks) uncompressed)
5937           (push (car marks) out)
5938         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5939       (setq marks (cdr marks)))
5940     out))
5941
5942 (defun gnus-article-mark-to-type (mark)
5943   "Return the type of MARK."
5944   (or (cadr (assq mark gnus-article-special-mark-lists))
5945       'list))
5946
5947 (defun gnus-article-unpropagatable-p (mark)
5948   "Return whether MARK should be propagated to back end."
5949   (memq mark gnus-article-unpropagated-mark-lists))
5950
5951 (defun gnus-adjust-marked-articles (info)
5952   "Set all article lists and remove all marks that are no longer valid."
5953   (let* ((marked-lists (gnus-info-marks info))
5954          (active (gnus-active (gnus-info-group info)))
5955          (min (car active))
5956          (max (cdr active))
5957          (types gnus-article-mark-lists)
5958          marks var articles article mark mark-type
5959          bgn end)
5960     ;; Hack to avoid adjusting marks for imap.
5961     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5962               'nnimap)
5963       (setq min 1))
5964
5965     (dolist (marks marked-lists)
5966       (setq mark (car marks)
5967             mark-type (gnus-article-mark-to-type mark)
5968             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5969
5970       ;; We set the variable according to the type of the marks list,
5971       ;; and then adjust the marks to a subset of the active articles.
5972       (cond
5973        ;; Adjust "simple" lists - compressed yet unsorted
5974        ((eq mark-type 'list)
5975         ;; Simultaneously uncompress and clip to active range
5976         ;; See gnus-uncompress-range for a description of possible marks
5977         (let (l lh)
5978           (if (not (cadr marks))
5979               (set var nil)
5980             (setq articles (if (numberp (cddr marks))
5981                                (list (cdr marks))
5982                              (cdr marks))
5983                   lh (cons nil nil)
5984                   l lh)
5985
5986             (while (setq article (pop articles))
5987               (cond ((consp article)
5988                      (setq bgn (max (car article) min)
5989                            end (min (cdr article) max))
5990                      (while (<= bgn end)
5991                        (setq l (setcdr l (cons bgn nil))
5992                              bgn (1+ bgn))))
5993                     ((and (<= min article)
5994                           (>= max article))
5995                      (setq l (setcdr l (cons article nil))))))
5996             (set var (cdr lh)))))
5997        ;; Adjust assocs.
5998        ((eq mark-type 'tuple)
5999         (set var (setq articles (cdr marks)))
6000         (when (not (listp (cdr (symbol-value var))))
6001           (set var (list (symbol-value var))))
6002         (when (not (listp (cdr articles)))
6003           (setq articles (list articles)))
6004         (while articles
6005           (when (or (not (consp (setq article (pop articles))))
6006                     (< (car article) min)
6007                     (> (car article) max))
6008             (set var (delq article (symbol-value var))))))
6009        ;; Adjust ranges (sloppily).
6010        ((eq mark-type 'range)
6011         (cond
6012          ((eq mark 'seen)
6013           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6014           ;; It should be (seen (NUM1 . NUM2)).
6015           (when (numberp (cddr marks))
6016             (setcdr marks (list (cdr marks))))
6017           (setq articles (cdr marks))
6018           (while (and articles
6019                       (or (and (consp (car articles))
6020                                (> min (cdar articles)))
6021                           (and (numberp (car articles))
6022                                (> min (car articles)))))
6023             (pop articles))
6024           (set var articles))))))))
6025
6026 (defun gnus-update-missing-marks (missing)
6027   "Go through the list of MISSING articles and remove them from the mark lists."
6028   (when missing
6029     (let (var m)
6030       ;; Go through all types.
6031       (dolist (elem gnus-article-mark-lists)
6032         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6033           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6034           (when (symbol-value var)
6035             ;; This list has articles.  So we delete all missing
6036             ;; articles from it.
6037             (setq m missing)
6038             (while m
6039               (set var (delq (pop m) (symbol-value var))))))))))
6040
6041 (defun gnus-update-marks ()
6042   "Enter the various lists of marked articles into the newsgroup info list."
6043   (let ((types gnus-article-mark-lists)
6044         (info (gnus-get-info gnus-newsgroup-name))
6045         type list newmarked symbol delta-marks)
6046     (when info
6047       ;; Add all marks lists to the list of marks lists.
6048       (while (setq type (pop types))
6049         (setq list (symbol-value
6050                     (setq symbol
6051                           (intern (format "gnus-newsgroup-%s" (car type))))))
6052
6053         (when list
6054           ;; Get rid of the entries of the articles that have the
6055           ;; default score.
6056           (when (and (eq (cdr type) 'score)
6057                      gnus-save-score
6058                      list)
6059             (let* ((arts list)
6060                    (prev (cons nil list))
6061                    (all prev))
6062               (while arts
6063                 (if (or (not (consp (car arts)))
6064                         (= (cdar arts) gnus-summary-default-score))
6065                     (setcdr prev (cdr arts))
6066                   (setq prev arts))
6067                 (setq arts (cdr arts)))
6068               (setq list (cdr all)))))
6069
6070         (when (eq (cdr type) 'seen)
6071           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6072
6073         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6074           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6075
6076         (when (and (gnus-check-backend-function
6077                     'request-set-mark gnus-newsgroup-name)
6078                    (not (gnus-article-unpropagatable-p (cdr type))))
6079           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6080                  ;; Don't do anything about marks for articles we
6081                  ;; didn't actually get any headers for.
6082                  (del
6083                   (gnus-list-range-intersection
6084                    gnus-newsgroup-articles
6085                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6086                  (add
6087                   (gnus-list-range-intersection
6088                    gnus-newsgroup-articles
6089                    (gnus-remove-from-range
6090                     (gnus-copy-sequence list) old))))
6091             (when add
6092               (push (list add 'add (list (cdr type))) delta-marks))
6093             (when del
6094               ;; Don't delete marks from outside the active range.
6095               ;; This shouldn't happen, but is a sanity check.
6096               (setq del (gnus-sorted-range-intersection
6097                          (gnus-active gnus-newsgroup-name) del))
6098               (push (list del 'del (list (cdr type))) delta-marks))))
6099
6100         (when list
6101           (push (cons (cdr type) list) newmarked)))
6102
6103       (when delta-marks
6104         (unless (gnus-check-group gnus-newsgroup-name)
6105           (error "Can't open server for %s" gnus-newsgroup-name))
6106         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6107
6108       ;; Enter these new marks into the info of the group.
6109       (if (nthcdr 3 info)
6110           (setcar (nthcdr 3 info) newmarked)
6111         ;; Add the marks lists to the end of the info.
6112         (when newmarked
6113           (setcdr (nthcdr 2 info) (list newmarked))))
6114
6115       ;; Cut off the end of the info if there's nothing else there.
6116       (let ((i 5))
6117         (while (and (> i 2)
6118                     (not (nth i info)))
6119           (when (nthcdr (decf i) info)
6120             (setcdr (nthcdr i info) nil)))))))
6121
6122 (defun gnus-set-mode-line (where)
6123   "Set the mode line of the article or summary buffers.
6124 If WHERE is `summary', the summary mode line format will be used."
6125   ;; Is this mode line one we keep updated?
6126   (when (and (memq where gnus-updated-mode-lines)
6127              (symbol-value
6128               (intern (format "gnus-%s-mode-line-format-spec" where))))
6129     (let (mode-string)
6130       ;; We evaluate this in the summary buffer since these
6131       ;; variables are buffer-local to that buffer.
6132       (with-current-buffer gnus-summary-buffer
6133         ;; We bind all these variables that are used in the `eval' form
6134         ;; below.
6135         (let* ((mformat (symbol-value
6136                          (intern
6137                           (format "gnus-%s-mode-line-format-spec" where))))
6138                (gnus-tmp-group-name (gnus-mode-string-quote
6139                                      (gnus-group-decoded-name
6140                                       gnus-newsgroup-name)))
6141                (gnus-tmp-article-number (or gnus-current-article 0))
6142                (gnus-tmp-unread gnus-newsgroup-unreads)
6143                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6144                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6145                (gnus-tmp-unread-and-unselected
6146                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6147                             (zerop gnus-tmp-unselected))
6148                        "")
6149                       ((zerop gnus-tmp-unselected)
6150                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6151                       (t (format "{%d(+%d) more}"
6152                                  gnus-tmp-unread-and-unticked
6153                                  gnus-tmp-unselected))))
6154                (gnus-tmp-subject
6155                 (if (and gnus-current-headers
6156                          (vectorp gnus-current-headers))
6157                     (gnus-mode-string-quote
6158                      (mail-header-subject gnus-current-headers))
6159                   ""))
6160                bufname-length max-len
6161                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6162           (setq mode-string (eval mformat))
6163           (setq bufname-length (if (string-match "%b" mode-string)
6164                                    (- (length
6165                                        (buffer-name
6166                                         (if (eq where 'summary)
6167                                             nil
6168                                           (get-buffer gnus-article-buffer))))
6169                                       2)
6170                                  0))
6171           (setq max-len (max 4 (if gnus-mode-non-string-length
6172                                    (- (window-width)
6173                                       gnus-mode-non-string-length
6174                                       bufname-length)
6175                                  (length mode-string))))
6176           ;; We might have to chop a bit of the string off...
6177           (when (> (length mode-string) max-len)
6178             (setq mode-string
6179                   (concat (truncate-string-to-width mode-string (- max-len 3))
6180                           "...")))))
6181       ;; Update the mode line.
6182       (setq mode-line-buffer-identification
6183             (gnus-mode-line-buffer-identification (list mode-string)))
6184       (set-buffer-modified-p t))))
6185
6186 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6187   "Go through the HEADERS list and add all Xrefs to a hash table.
6188 The resulting hash table is returned, or nil if no Xrefs were found."
6189   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6190          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6191          (xref-hashtb (gnus-make-hashtable))
6192          start group entry number xrefs header)
6193     (while headers
6194       (setq header (pop headers))
6195       (when (and (setq xrefs (mail-header-xref header))
6196                  (not (memq (setq number (mail-header-number header))
6197                             unreads)))
6198         (setq start 0)
6199         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6200           (setq start (match-end 0))
6201           (setq group (if prefix
6202                           (concat prefix (substring xrefs (match-beginning 1)
6203                                                     (match-end 1)))
6204                         (substring xrefs (match-beginning 1) (match-end 1))))
6205           (setq number
6206                 (string-to-number (substring xrefs (match-beginning 2)
6207                                           (match-end 2))))
6208           (if (setq entry (gnus-gethash group xref-hashtb))
6209               (setcdr entry (cons number (cdr entry)))
6210             (gnus-sethash group (cons number nil) xref-hashtb)))))
6211     (and start xref-hashtb)))
6212
6213 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6214   "Look through all the headers and mark the Xrefs as read."
6215   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6216         name info xref-hashtb idlist method nth4)
6217     (with-current-buffer gnus-group-buffer
6218       (when (setq xref-hashtb
6219                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6220         (mapatoms
6221          (lambda (group)
6222            (unless (string= from-newsgroup (setq name (symbol-name group)))
6223              (setq idlist (symbol-value group))
6224              ;; Dead groups are not updated.
6225              (and (prog1
6226                       (setq info (gnus-get-info name))
6227                     (when (stringp (setq nth4 (gnus-info-method info)))
6228                       (setq nth4 (gnus-server-to-method nth4))))
6229                   ;; Only do the xrefs if the group has the same
6230                   ;; select method as the group we have just read.
6231                   (or (gnus-methods-equal-p
6232                        nth4 (gnus-find-method-for-group from-newsgroup))
6233                       virtual
6234                       (equal nth4 (setq method (gnus-find-method-for-group
6235                                                 from-newsgroup)))
6236                       (and (equal (car nth4) (car method))
6237                            (equal (nth 1 nth4) (nth 1 method))))
6238                   gnus-use-cross-reference
6239                   (or (not (eq gnus-use-cross-reference t))
6240                       virtual
6241                       ;; Only do cross-references on subscribed
6242                       ;; groups, if that is what is wanted.
6243                       (<= (gnus-info-level info) gnus-level-subscribed))
6244                   (gnus-group-make-articles-read name idlist))))
6245          xref-hashtb)))))
6246
6247 (defun gnus-compute-read-articles (group articles)
6248   (let* ((entry (gnus-group-entry group))
6249          (info (nth 2 entry))
6250          (active (gnus-active group))
6251          ninfo)
6252     (when entry
6253       ;; First peel off all invalid article numbers.
6254       (when active
6255         (let ((ids articles)
6256               id first)
6257           (while (setq id (pop ids))
6258             (when (and first (> id (cdr active)))
6259               ;; We'll end up in this situation in one particular
6260               ;; obscure situation.  If you re-scan a group and get
6261               ;; a new article that is cross-posted to a different
6262               ;; group that has not been re-scanned, you might get
6263               ;; crossposted article that has a higher number than
6264               ;; Gnus believes possible.  So we re-activate this
6265               ;; group as well.  This might mean doing the
6266               ;; crossposting thingy will *increase* the number
6267               ;; of articles in some groups.  Tsk, tsk.
6268               (setq active (or (gnus-activate-group group) active)))
6269             (when (or (> id (cdr active))
6270                       (< id (car active)))
6271               (setq articles (delq id articles))))))
6272       ;; If the read list is nil, we init it.
6273       (if (and active
6274                (null (gnus-info-read info))
6275                (> (car active) 1))
6276           (setq ninfo (cons 1 (1- (car active))))
6277         (setq ninfo (gnus-info-read info)))
6278       ;; Then we add the read articles to the range.
6279       (gnus-add-to-range
6280        ninfo (setq articles (sort articles '<))))))
6281
6282 (defun gnus-group-make-articles-read (group articles)
6283   "Update the info of GROUP to say that ARTICLES are read."
6284   (let* ((num 0)
6285          (entry (gnus-group-entry group))
6286          (info (nth 2 entry))
6287          (active (gnus-active group))
6288          (set-marks
6289           (or gnus-propagate-marks
6290               (gnus-method-option-p
6291                (gnus-find-method-for-group group)
6292                'server-marks)))
6293          range)
6294     (if (not entry)
6295         ;; Group that Gnus doesn't know exists, but still allow the
6296         ;; backend to set marks.
6297         (when set-marks
6298           (gnus-request-set-mark
6299            group (list (list (gnus-compress-sequence (sort articles #'<))
6300                              'add '(read)))))
6301       ;; Normal, subscribed groups.
6302       (setq range (gnus-compute-read-articles group articles))
6303       (with-current-buffer gnus-group-buffer
6304         (gnus-undo-register
6305           `(progn
6306              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6307              (gnus-info-set-read ',info ',(gnus-info-read info))
6308              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6309              (when ,set-marks
6310                (gnus-request-set-mark
6311                 ,group (list (list ',range 'del '(read)))))
6312              (gnus-group-update-group ,group t))))
6313       ;; Add the read articles to the range.
6314       (gnus-info-set-read info range)
6315       (when set-marks
6316         (gnus-request-set-mark group (list (list range 'add '(read)))))
6317       ;; Then we have to re-compute how many unread
6318       ;; articles there are in this group.
6319       (when active
6320         (cond
6321          ((not range)
6322           (setq num (- (1+ (cdr active)) (car active))))
6323          ((not (listp (cdr range)))
6324           (setq num (- (cdr active) (- (1+ (cdr range))
6325                                        (car range)))))
6326          (t
6327           (while range
6328             (if (numberp (car range))
6329                 (setq num (1+ num))
6330               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6331             (setq range (cdr range)))
6332           (setq num (- (cdr active) num))))
6333         ;; Update the number of unread articles.
6334         (setcar entry num)
6335         ;; Update the group buffer.
6336         (unless (gnus-ephemeral-group-p group)
6337           (gnus-group-update-group group t))))))
6338
6339 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6340   (let ((cur nntp-server-buffer)
6341         (dependencies
6342          (or dependencies
6343              (with-current-buffer gnus-summary-buffer
6344                gnus-newsgroup-dependencies)))
6345         headers id end ref number
6346         (mail-parse-charset gnus-newsgroup-charset)
6347         (mail-parse-ignored-charsets
6348          (save-current-buffer (condition-case nil
6349                                   (set-buffer gnus-summary-buffer)
6350                                 (error))
6351                               gnus-newsgroup-ignored-charsets)))
6352     (with-current-buffer nntp-server-buffer
6353       ;; Translate all TAB characters into SPACE characters.
6354       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6355       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6356       (ietf-drums-unfold-fws)
6357       (gnus-run-hooks 'gnus-parse-headers-hook)
6358       (let ((case-fold-search t)
6359             in-reply-to header p lines chars)
6360         (goto-char (point-min))
6361         ;; Search to the beginning of the next header.  Error messages
6362         ;; do not begin with 2 or 3.
6363         (while (re-search-forward "^[23][0-9]+ " nil t)
6364           (setq id nil
6365                 ref nil)
6366           ;; This implementation of this function, with nine
6367           ;; search-forwards instead of the one re-search-forward and
6368           ;; a case (which basically was the old function) is actually
6369           ;; about twice as fast, even though it looks messier.  You
6370           ;; can't have everything, I guess.  Speed and elegance
6371           ;; doesn't always go hand in hand.
6372           (setq
6373            header
6374            (vector
6375             ;; Number.
6376             (prog1
6377                 (setq number (read cur))
6378               (end-of-line)
6379               (setq p (point))
6380               (narrow-to-region (point)
6381                                 (or (and (search-forward "\n.\n" nil t)
6382                                          (- (point) 2))
6383                                     (point))))
6384             ;; Subject.
6385             (progn
6386               (goto-char p)
6387               (if (search-forward "\nsubject:" nil t)
6388                   (funcall gnus-decode-encoded-word-function
6389                            (nnheader-header-value))
6390                 "(none)"))
6391             ;; From.
6392             (progn
6393               (goto-char p)
6394               (if (search-forward "\nfrom:" nil t)
6395                   (funcall gnus-decode-encoded-address-function
6396                            (nnheader-header-value))
6397                 "(nobody)"))
6398             ;; Date.
6399             (progn
6400               (goto-char p)
6401               (if (search-forward "\ndate:" nil t)
6402                   (nnheader-header-value) ""))
6403             ;; Message-ID.
6404             (progn
6405               (goto-char p)
6406               (setq id (if (re-search-forward
6407                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6408                            ;; We do it this way to make sure the Message-ID
6409                            ;; is (somewhat) syntactically valid.
6410                            (buffer-substring (match-beginning 1)
6411                                              (match-end 1))
6412                          ;; If there was no message-id, we just fake one
6413                          ;; to make subsequent routines simpler.
6414                          (nnheader-generate-fake-message-id number))))
6415             ;; References.
6416             (progn
6417               (goto-char p)
6418               (if (search-forward "\nreferences:" nil t)
6419                   (progn
6420                     (setq end (point))
6421                     (prog1
6422                         (nnheader-header-value)
6423                       (setq ref
6424                             (buffer-substring
6425                              (progn
6426                                (end-of-line)
6427                                (search-backward ">" end t)
6428                                (1+ (point)))
6429                              (progn
6430                                (search-backward "<" end t)
6431                                (point))))))
6432                 ;; Get the references from the in-reply-to header if there
6433                 ;; were no references and the in-reply-to header looks
6434                 ;; promising.
6435                 (if (and (search-forward "\nin-reply-to:" nil t)
6436                          (setq in-reply-to (nnheader-header-value))
6437                          (string-match "<[^>]+>" in-reply-to))
6438                     (let (ref2)
6439                       (setq ref (substring in-reply-to (match-beginning 0)
6440                                            (match-end 0)))
6441                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6442                         (setq ref2 (substring in-reply-to (match-beginning 0)
6443                                               (match-end 0)))
6444                         (when (> (length ref2) (length ref))
6445                           (setq ref ref2)))
6446                       ref)
6447                   (setq ref nil))))
6448             ;; Chars.
6449             (progn
6450               (goto-char p)
6451               (if (search-forward "\nchars: " nil t)
6452                   (if (numberp (setq chars (ignore-errors (read cur))))
6453                       chars -1)
6454                 -1))
6455             ;; Lines.
6456             (progn
6457               (goto-char p)
6458               (if (search-forward "\nlines: " nil t)
6459                   (if (numberp (setq lines (ignore-errors (read cur))))
6460                       lines -1)
6461                 -1))
6462             ;; Xref.
6463             (progn
6464               (goto-char p)
6465               (and (search-forward "\nxref:" nil t)
6466                    (nnheader-header-value)))
6467             ;; Extra.
6468             (when gnus-extra-headers
6469               (let ((extra gnus-extra-headers)
6470                     out)
6471                 (while extra
6472                   (goto-char p)
6473                   (when (search-forward
6474                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6475                     (push (cons (car extra) (nnheader-header-value))
6476                           out))
6477                   (pop extra))
6478                 out))))
6479           (when (equal id ref)
6480             (setq ref nil))
6481
6482           (when gnus-alter-header-function
6483             (funcall gnus-alter-header-function header)
6484             (setq id (mail-header-id header)
6485                   ref (gnus-parent-id (mail-header-references header))))
6486
6487           (when (setq header
6488                       (gnus-dependencies-add-header
6489                        header dependencies force-new))
6490             (push header headers))
6491           (goto-char (point-max))
6492           (widen))
6493         (nreverse headers)))))
6494
6495 ;; Goes through the xover lines and returns a list of vectors
6496 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6497                                                   force-new dependencies
6498                                                   group also-fetch-heads)
6499   "Parse the news overview data in the server buffer.
6500 Return a list of headers that match SEQUENCE (see
6501 `nntp-retrieve-headers')."
6502   ;; Get the Xref when the users reads the articles since most/some
6503   ;; NNTP servers do not include Xrefs when using XOVER.
6504   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6505   (let ((mail-parse-charset gnus-newsgroup-charset)
6506         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6507         (cur nntp-server-buffer)
6508         (dependencies (or dependencies gnus-newsgroup-dependencies))
6509         (allp (cond
6510                ((eq gnus-read-all-available-headers t)
6511                 t)
6512                ((and (stringp gnus-read-all-available-headers)
6513                      group)
6514                 (string-match gnus-read-all-available-headers group))
6515                (t
6516                 nil)))
6517         number headers header)
6518     (with-current-buffer nntp-server-buffer
6519       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6520       ;; Allow the user to mangle the headers before parsing them.
6521       (gnus-run-hooks 'gnus-parse-headers-hook)
6522       (goto-char (point-min))
6523       (gnus-parse-without-error
6524         (while (and (or sequence allp)
6525                     (not (eobp)))
6526           (setq number (read cur))
6527           (when (not allp)
6528             (while (and sequence
6529                         (< (car sequence) number))
6530               (setq sequence (cdr sequence))))
6531           (when (and (or allp
6532                          (and sequence
6533                               (eq number (car sequence))))
6534                      (progn
6535                        (setq sequence (cdr sequence))
6536                        (setq header (inline
6537                                       (gnus-nov-parse-line
6538                                        number dependencies force-new)))))
6539             (push header headers))
6540           (forward-line 1)))
6541       ;; A common bug in inn is that if you have posted an article and
6542       ;; then retrieves the active file, it will answer correctly --
6543       ;; the new article is included.  However, a NOV entry for the
6544       ;; article may not have been generated yet, so this may fail.
6545       ;; We work around this problem by retrieving the last few
6546       ;; headers using HEAD.
6547       (if (or (not also-fetch-heads)
6548               (not sequence))
6549           ;; We (probably) got all the headers.
6550           (nreverse headers)
6551         (let ((gnus-nov-is-evil t))
6552           (nconc
6553            (nreverse headers)
6554            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6555              (gnus-get-newsgroup-headers))))))))
6556
6557 (defun gnus-article-get-xrefs ()
6558   "Fill in the Xref value in `gnus-current-headers', if necessary.
6559 This is meant to be called in `gnus-article-internal-prepare-hook'."
6560   (let ((headers (with-current-buffer gnus-summary-buffer
6561                    gnus-current-headers)))
6562     (or (not gnus-use-cross-reference)
6563         (not headers)
6564         (and (mail-header-xref headers)
6565              (not (string= (mail-header-xref headers) "")))
6566         (let ((case-fold-search t)
6567               xref)
6568           (save-restriction
6569             (nnheader-narrow-to-headers)
6570             (goto-char (point-min))
6571             (when (or (and (not (eobp))
6572                            (eq (downcase (char-after)) ?x)
6573                            (looking-at "Xref:"))
6574                       (search-forward "\nXref:" nil t))
6575               (goto-char (1+ (match-end 0)))
6576               (setq xref (buffer-substring (point) (point-at-eol)))
6577               (mail-header-set-xref headers xref)))))))
6578
6579 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6580   "Find article ID and insert the summary line for that article.
6581 OLD-HEADER can either be a header or a line number to insert
6582 the subject line on.
6583 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6584 and OLD-HEADER will be used when the summary line is inserted,
6585 too, instead of trying to fetch new headers."
6586   (let* ((line (and (numberp old-header) old-header))
6587          (old-header (and (vectorp old-header) old-header))
6588          (header (cond ((and old-header use-old-header)
6589                         old-header)
6590                        ((and (numberp id)
6591                              (gnus-number-to-header id))
6592                         (gnus-number-to-header id))
6593                        (t
6594                         (gnus-read-header id))))
6595          (number (and (numberp id) id))
6596          d)
6597     (when header
6598       ;; Rebuild the thread that this article is part of and go to the
6599       ;; article we have fetched.
6600       (when (and (not gnus-show-threads)
6601                  old-header)
6602         (when (and number
6603                    (setq d (gnus-data-find (mail-header-number old-header))))
6604           (goto-char (gnus-data-pos d))
6605           (gnus-data-remove
6606            number
6607            (- (point-at-bol)
6608               (prog1
6609                   (1+ (point-at-eol))
6610                 (gnus-delete-line))))))
6611       ;; Remove list identifiers from subject.
6612       (let ((gnus-newsgroup-headers (list header)))
6613         (gnus-summary-remove-list-identifiers))
6614       (when old-header
6615         (mail-header-set-number header (mail-header-number old-header)))
6616       (setq gnus-newsgroup-sparse
6617             (delq (setq number (mail-header-number header))
6618                   gnus-newsgroup-sparse))
6619       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6620       (push number gnus-newsgroup-limit)
6621       (gnus-rebuild-thread (mail-header-id header) line)
6622       (gnus-summary-goto-subject number nil t))
6623     (when (and (numberp number)
6624                (> number 0))
6625       ;; We have to update the boundaries even if we can't fetch the
6626       ;; article if ID is a number -- so that the next `P' or `N'
6627       ;; command will fetch the previous (or next) article even
6628       ;; if the one we tried to fetch this time has been canceled.
6629       (when (> number gnus-newsgroup-end)
6630         (setq gnus-newsgroup-end number))
6631       (when (< number gnus-newsgroup-begin)
6632         (setq gnus-newsgroup-begin number))
6633       (setq gnus-newsgroup-unselected
6634             (delq number gnus-newsgroup-unselected)))
6635     ;; Report back a success?
6636     (and header (mail-header-number header))))
6637
6638 ;;; Process/prefix in the summary buffer
6639
6640 (defun gnus-summary-work-articles (n)
6641   "Return a list of articles to be worked upon.
6642 The prefix argument, the list of process marked articles, and the
6643 current article will be taken into consideration."
6644   (with-current-buffer gnus-summary-buffer
6645     (cond
6646      (n
6647       ;; A numerical prefix has been given.
6648       (setq n (prefix-numeric-value n))
6649       (let ((backward (< n 0))
6650             (n (abs (prefix-numeric-value n)))
6651             articles article)
6652         (save-excursion
6653           (while
6654               (and (> n 0)
6655                    (push (setq article (gnus-summary-article-number))
6656                          articles)
6657                    (if backward
6658                        (gnus-summary-find-prev nil article)
6659                      (gnus-summary-find-next nil article)))
6660             (decf n)))
6661         (nreverse articles)))
6662      ((and (gnus-region-active-p) (mark))
6663       (message "region active")
6664       ;; Work on the region between point and mark.
6665       (let ((max (max (point) (mark)))
6666             articles article)
6667         (save-excursion
6668           (goto-char (min (point) (mark)))
6669           (while
6670               (and
6671                (push (setq article (gnus-summary-article-number)) articles)
6672                (gnus-summary-find-next nil article)
6673                (< (point) max)))
6674           (nreverse articles))))
6675      (gnus-newsgroup-processable
6676       ;; There are process-marked articles present.
6677       ;; Save current state.
6678       (gnus-summary-save-process-mark)
6679       ;; Return the list.
6680       (reverse gnus-newsgroup-processable))
6681      (t
6682       ;; Just return the current article.
6683       (list (gnus-summary-article-number))))))
6684
6685 (defmacro gnus-summary-iterate (arg &rest forms)
6686   "Iterate over the process/prefixed articles and do FORMS.
6687 ARG is the interactive prefix given to the command.  FORMS will be
6688 executed with point over the summary line of the articles."
6689   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6690     `(let ((,articles (gnus-summary-work-articles ,arg)))
6691        (while ,articles
6692          (gnus-summary-goto-subject (car ,articles))
6693          ,@forms
6694          (pop ,articles)))))
6695
6696 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6697 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6698
6699 (defun gnus-summary-save-process-mark ()
6700   "Push the current set of process marked articles on the stack."
6701   (interactive)
6702   (push (copy-sequence gnus-newsgroup-processable)
6703         gnus-newsgroup-process-stack))
6704
6705 (defun gnus-summary-kill-process-mark ()
6706   "Push the current set of process marked articles on the stack and unmark."
6707   (interactive)
6708   (gnus-summary-save-process-mark)
6709   (gnus-summary-unmark-all-processable))
6710
6711 (defun gnus-summary-yank-process-mark ()
6712   "Pop the last process mark state off the stack and restore it."
6713   (interactive)
6714   (unless gnus-newsgroup-process-stack
6715     (error "Empty mark stack"))
6716   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6717
6718 (defun gnus-summary-process-mark-set (set)
6719   "Make SET into the current process marked articles."
6720   (gnus-summary-unmark-all-processable)
6721   (mapc 'gnus-summary-set-process-mark set))
6722
6723 ;;; Searching and stuff
6724
6725 (defun gnus-summary-search-group (&optional backward use-level)
6726   "Search for next unread newsgroup.
6727 If optional argument BACKWARD is non-nil, search backward instead."
6728   (with-current-buffer gnus-group-buffer
6729     (when (gnus-group-search-forward
6730            backward nil (if use-level (gnus-group-group-level) nil))
6731       (gnus-group-group-name))))
6732
6733 (defun gnus-summary-best-group (&optional exclude-group)
6734   "Find the name of the best unread group.
6735 If EXCLUDE-GROUP, do not go to this group."
6736   (with-current-buffer gnus-group-buffer
6737     (save-excursion
6738       (gnus-group-best-unread-group exclude-group))))
6739
6740 (defun gnus-summary-find-next (&optional unread article backward)
6741   (if backward
6742       (gnus-summary-find-prev unread article)
6743     (let* ((dummy (gnus-summary-article-intangible-p))
6744            (article (or article (gnus-summary-article-number)))
6745            (data (gnus-data-find-list article))
6746            result)
6747       (when (and (not dummy)
6748                  (or (not gnus-summary-check-current)
6749                      (not unread)
6750                      (not (gnus-data-unread-p (car data)))))
6751         (setq data (cdr data)))
6752       (when (setq result
6753                   (if unread
6754                       (progn
6755                         (while data
6756                           (unless (memq (gnus-data-number (car data))
6757                                         (cond
6758                                          ((eq gnus-auto-goto-ignores
6759                                               'always-undownloaded)
6760                                           gnus-newsgroup-undownloaded)
6761                                          (gnus-plugged
6762                                           nil)
6763                                          ((eq gnus-auto-goto-ignores
6764                                               'unfetched)
6765                                           gnus-newsgroup-unfetched)
6766                                          ((eq gnus-auto-goto-ignores
6767                                               'undownloaded)
6768                                           gnus-newsgroup-undownloaded)))
6769                             (when (gnus-data-unread-p (car data))
6770                               (setq result (car data)
6771                                     data nil)))
6772                           (setq data (cdr data)))
6773                         result)
6774                     (car data)))
6775         (goto-char (gnus-data-pos result))
6776         (gnus-data-number result)))))
6777
6778 (defun gnus-summary-find-prev (&optional unread article)
6779   (let* ((eobp (eobp))
6780          (article (or article (gnus-summary-article-number)))
6781          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6782          result)
6783     (when (and (not eobp)
6784                (or (not gnus-summary-check-current)
6785                    (not unread)
6786                    (not (gnus-data-unread-p (car data)))))
6787       (setq data (cdr data)))
6788     (when (setq result
6789                 (if unread
6790                     (progn
6791                       (while data
6792                         (unless (memq (gnus-data-number (car data))
6793                                       (cond
6794                                        ((eq gnus-auto-goto-ignores
6795                                             'always-undownloaded)
6796                                         gnus-newsgroup-undownloaded)
6797                                        (gnus-plugged
6798                                         nil)
6799                                        ((eq gnus-auto-goto-ignores
6800                                             'unfetched)
6801                                         gnus-newsgroup-unfetched)
6802                                        ((eq gnus-auto-goto-ignores
6803                                             'undownloaded)
6804                                         gnus-newsgroup-undownloaded)))
6805                           (when (gnus-data-unread-p (car data))
6806                             (setq result (car data)
6807                                   data nil)))
6808                         (setq data (cdr data)))
6809                       result)
6810                   (car data)))
6811       (goto-char (gnus-data-pos result))
6812       (gnus-data-number result))))
6813
6814 (defun gnus-summary-find-subject (subject &optional unread backward article)
6815   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6816          (article (or article (gnus-summary-article-number)))
6817          (articles (gnus-data-list backward))
6818          (arts (gnus-data-find-list article articles))
6819          result)
6820     (when (or (not gnus-summary-check-current)
6821               (not unread)
6822               (not (gnus-data-unread-p (car arts))))
6823       (setq arts (cdr arts)))
6824     (while arts
6825       (and (or (not unread)
6826                (gnus-data-unread-p (car arts)))
6827            (vectorp (gnus-data-header (car arts)))
6828            (gnus-subject-equal
6829             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6830            (setq result (car arts)
6831                  arts nil))
6832       (setq arts (cdr arts)))
6833     (and result
6834          (goto-char (gnus-data-pos result))
6835          (gnus-data-number result))))
6836
6837 (defun gnus-summary-search-forward (&optional unread subject backward)
6838   "Search forward for an article.
6839 If UNREAD, look for unread articles.  If SUBJECT, look for
6840 articles with that subject.  If BACKWARD, search backward instead."
6841   (cond (subject (gnus-summary-find-subject subject unread backward))
6842         (backward (gnus-summary-find-prev unread))
6843         (t (gnus-summary-find-next unread))))
6844
6845 (defun gnus-recenter (&optional n)
6846   "Center point in window and redisplay frame.
6847 Also do horizontal recentering."
6848   (interactive "P")
6849   (when (and gnus-auto-center-summary
6850              (not (eq gnus-auto-center-summary 'vertical)))
6851     (gnus-horizontal-recenter))
6852   (if (fboundp 'recenter-top-bottom)
6853       (recenter-top-bottom n)
6854     (recenter n)))
6855
6856 (put 'gnus-recenter 'isearch-scroll t)
6857
6858 (defun gnus-forward-line-ignore-invisible (n)
6859   "Move N lines forward (backward if N is negative).
6860 Like forward-line, but skip over (and don't count) invisible lines."
6861   (let (done)
6862     (while (and (> n 0) (not done))
6863       ;; If the following character is currently invisible,
6864       ;; skip all characters with that same `invisible' property value.
6865       (while (gnus-invisible-p (point))
6866         (goto-char (gnus-next-char-property-change (point))))
6867       (forward-line 1)
6868       (if (eobp)
6869           (setq done t)
6870         (setq n (1- n))))
6871     (while (and (< n 0) (not done))
6872       (forward-line -1)
6873       (if (bobp) (setq done t)
6874         (setq n (1+ n))
6875         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6876           (goto-char (gnus-previous-char-property-change (point))))))))
6877
6878 (defun gnus-summary-recenter ()
6879   "Center point in the summary window.
6880 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6881 displayed, no centering will be performed."
6882   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6883   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6884   (interactive)
6885   ;; The user has to want it.
6886   (when gnus-auto-center-summary
6887     (let* ((top (cond ((< (window-height) 4) 0)
6888                       ((< (window-height) 7) 1)
6889                       (t (if (numberp gnus-auto-center-summary)
6890                              gnus-auto-center-summary
6891                            (/ (1- (window-height)) 2)))))
6892            (height (1- (window-height)))
6893            (bottom (save-excursion
6894                      (goto-char (point-max))
6895                      (gnus-forward-line-ignore-invisible (- height))
6896                      (point)))
6897            (window (get-buffer-window (current-buffer))))
6898       (when (get-buffer-window gnus-article-buffer)
6899         ;; Only do recentering when the article buffer is displayed,
6900         ;; Set the window start to either `bottom', which is the biggest
6901         ;; possible valid number, or the second line from the top,
6902         ;; whichever is the least.
6903         (let ((top-pos (save-excursion
6904                          (gnus-forward-line-ignore-invisible (- top))
6905                          (point))))
6906           (if (> bottom top-pos)
6907               ;; Keep the second line from the top visible
6908               (set-window-start window top-pos)
6909             ;; Try to keep the bottom line visible; if it's partially
6910             ;; obscured, either scroll one more line to make it fully
6911             ;; visible, or revert to using TOP-POS.
6912             (save-excursion
6913               (goto-char (point-max))
6914               (gnus-forward-line-ignore-invisible -1)
6915               (let ((last-line-start (point)))
6916                 (goto-char bottom)
6917                 (set-window-start window (point) t)
6918                 (when (not (pos-visible-in-window-p last-line-start window))
6919                   (gnus-forward-line-ignore-invisible 1)
6920                   (set-window-start window (min (point) top-pos) t)))))))
6921       ;; Do horizontal recentering while we're at it.
6922       (when (and (get-buffer-window (current-buffer) t)
6923                  (not (eq gnus-auto-center-summary 'vertical)))
6924         (let ((selected (selected-window)))
6925           (select-window (get-buffer-window (current-buffer) t))
6926           (gnus-summary-position-point)
6927           (gnus-horizontal-recenter)
6928           (select-window selected))))))
6929
6930 (defun gnus-summary-jump-to-group (newsgroup)
6931   "Move point to NEWSGROUP in group mode buffer."
6932   ;; Keep update point of group mode buffer if visible.
6933   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6934       (save-window-excursion
6935         ;; Take care of tree window mode.
6936         (when (get-buffer-window gnus-group-buffer)
6937           (pop-to-buffer gnus-group-buffer))
6938         (gnus-group-jump-to-group newsgroup))
6939     (save-excursion
6940       ;; Take care of tree window mode.
6941       (if (get-buffer-window gnus-group-buffer 0)
6942           (pop-to-buffer gnus-group-buffer)
6943         (set-buffer gnus-group-buffer))
6944       (gnus-group-jump-to-group newsgroup))))
6945
6946 ;; This function returns a list of article numbers based on the
6947 ;; difference between the ranges of read articles in this group and
6948 ;; the range of active articles.
6949 (defun gnus-list-of-unread-articles (group)
6950   (let* ((read (gnus-info-read (gnus-get-info group)))
6951          (active (or (gnus-active group) (gnus-activate-group group)))
6952          (last (or (cdr active)
6953                    (error "Group %s couldn't be activated " group)))
6954          (bottom (if gnus-newsgroup-maximum-articles
6955                      (max (car active)
6956                           (- last gnus-newsgroup-maximum-articles -1))
6957                    (car active)))
6958          first nlast unread)
6959     ;; If none are read, then all are unread.
6960     (if (not read)
6961         (setq first bottom)
6962       ;; If the range of read articles is a single range, then the
6963       ;; first unread article is the article after the last read
6964       ;; article.  Sounds logical, doesn't it?
6965       (if (and (not (listp (cdr read)))
6966                (or (< (car read) bottom)
6967                    (progn (setq read (list read))
6968                           nil)))
6969           (setq first (max bottom (1+ (cdr read))))
6970         ;; `read' is a list of ranges.
6971         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6972                                   (caar read)))
6973                   1)
6974           (setq first bottom))
6975         (while read
6976           (when first
6977             (while (< first nlast)
6978               (setq unread (cons first unread)
6979                     first (1+ first))))
6980           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6981           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6982           (setq read (cdr read)))))
6983     ;; And add the last unread articles.
6984     (while (<= first last)
6985       (setq unread (cons first unread)
6986             first (1+ first)))
6987     ;; Return the list of unread articles.
6988     (delq 0 (nreverse unread))))
6989
6990 (defun gnus-list-of-read-articles (group)
6991   "Return a list of unread, unticked and non-dormant articles."
6992   (let* ((info (gnus-get-info group))
6993          (marked (gnus-info-marks info))
6994          (active (gnus-active group)))
6995     (and info active
6996          (gnus-list-range-difference
6997           (gnus-list-range-difference
6998            (gnus-sorted-complement
6999             (gnus-uncompress-range
7000              (if gnus-newsgroup-maximum-articles
7001                  (cons (max (car active)
7002                             (- (cdr active)
7003                                gnus-newsgroup-maximum-articles
7004                                -1))
7005                        (cdr active))
7006                active))
7007             (gnus-list-of-unread-articles group))
7008            (cdr (assq 'dormant marked)))
7009           (cdr (assq 'tick marked))))))
7010
7011 ;; This function returns a sequence of article numbers based on the
7012 ;; difference between the ranges of read articles in this group and
7013 ;; the range of active articles.
7014 (defun gnus-sequence-of-unread-articles (group)
7015   (let* ((read (gnus-info-read (gnus-get-info group)))
7016          (active (or (gnus-active group) (gnus-activate-group group)))
7017          (last (cdr active))
7018          (bottom (if gnus-newsgroup-maximum-articles
7019                      (max (car active)
7020                           (- last gnus-newsgroup-maximum-articles -1))
7021                    (car active)))
7022          first nlast unread)
7023     ;; If none are read, then all are unread.
7024     (if (not read)
7025         (setq first bottom)
7026       ;; If the range of read articles is a single range, then the
7027       ;; first unread article is the article after the last read
7028       ;; article.  Sounds logical, doesn't it?
7029       (if (and (not (listp (cdr read)))
7030                (or (< (car read) bottom)
7031                    (progn (setq read (list read))
7032                           nil)))
7033           (setq first (max bottom (1+ (cdr read))))
7034         ;; `read' is a list of ranges.
7035         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7036                                   (caar read)))
7037                   1)
7038           (setq first bottom))
7039         (while read
7040           (when first
7041             (push (cons first nlast) unread))
7042           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7043           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7044           (setq read (cdr read)))))
7045     ;; And add the last unread articles.
7046     (cond ((not (and first last))
7047            nil)
7048           ((< first last)
7049            (push (cons first last) unread))
7050           ((= first last)
7051            (push first unread)))
7052     ;; Return the sequence of unread articles.
7053     (delq 0 (nreverse unread))))
7054
7055 ;; Various summary commands
7056
7057 (defun gnus-summary-select-article-buffer ()
7058   "Reconfigure windows to show the article buffer.
7059 If `gnus-widen-article-window' is set, show only the article
7060 buffer."
7061   (interactive)
7062   (if (not (gnus-buffer-live-p gnus-article-buffer))
7063       (error "There is no article buffer for this summary buffer")
7064     (unless (get-buffer-window gnus-article-buffer)
7065       (gnus-summary-show-article))
7066     (gnus-configure-windows
7067      (if gnus-widen-article-window
7068          'only-article
7069        'article)
7070      t)
7071     (select-window (get-buffer-window gnus-article-buffer))))
7072
7073 (defun gnus-summary-universal-argument (arg)
7074   "Perform any operation on all articles that are process/prefixed."
7075   (interactive "P")
7076   (let ((articles (gnus-summary-work-articles arg))
7077         func article)
7078     (if (eq
7079          (setq
7080           func
7081           (key-binding
7082            (read-key-sequence
7083             (substitute-command-keys
7084              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7085          'undefined)
7086         (gnus-error 1 "Undefined key")
7087       (save-excursion
7088         (while articles
7089           (gnus-summary-goto-subject (setq article (pop articles)))
7090           (let (gnus-newsgroup-processable)
7091             (command-execute func))
7092           (gnus-summary-remove-process-mark article)))))
7093   (gnus-summary-position-point))
7094
7095 (defun gnus-summary-toggle-truncation (&optional arg)
7096   "Toggle truncation of summary lines.
7097 With ARG, turn line truncation on if ARG is positive."
7098   (interactive "P")
7099   (setq truncate-lines
7100         (if (null arg) (not truncate-lines)
7101           (> (prefix-numeric-value arg) 0)))
7102   (redraw-display))
7103
7104 (defun gnus-summary-find-for-reselect ()
7105   "Return the number of an article to stay on across a reselect.
7106 The current article is considered, then following articles, then previous
7107 articles.  An article is sought which is not canceled and isn't a temporary
7108 insertion from another group.  If there's no such then return a dummy 0."
7109   (let (found)
7110     (dolist (rev '(nil t))
7111       (unless found      ; don't demand the reverse list if we don't need it
7112         (let ((data (gnus-data-find-list
7113                      (gnus-summary-article-number) (gnus-data-list rev))))
7114           (while (and data (not found))
7115             (if (and (< 0 (gnus-data-number (car data)))
7116                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7117                 (setq found (gnus-data-number (car data))))
7118             (setq data (cdr data))))))
7119     (or found 0)))
7120
7121 (defun gnus-summary-reselect-current-group (&optional all rescan)
7122   "Exit and then reselect the current newsgroup.
7123 The prefix argument ALL means to select all articles."
7124   (interactive "P")
7125   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7126     (error "Ephemeral groups can't be reselected"))
7127   (let ((current-subject (gnus-summary-find-for-reselect))
7128         (group gnus-newsgroup-name))
7129     (setq gnus-newsgroup-begin nil)
7130     (gnus-summary-exit nil 'leave-hidden)
7131     ;; We have to adjust the point of group mode buffer because
7132     ;; point was moved to the next unread newsgroup by exiting.
7133     (gnus-summary-jump-to-group group)
7134     (when rescan
7135       (save-excursion
7136         (gnus-group-get-new-news-this-group 1)))
7137     (gnus-group-read-group all t)
7138     (gnus-summary-goto-subject current-subject nil t)))
7139
7140 (defun gnus-summary-rescan-group (&optional all)
7141   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7142   (interactive "P")
7143   (let ((config gnus-current-window-configuration))
7144     (gnus-summary-reselect-current-group all t)
7145     (gnus-configure-windows config)
7146     (when (eq config 'article)
7147       (gnus-summary-select-article))))
7148
7149 (defun gnus-summary-update-info (&optional non-destructive)
7150   (save-excursion
7151     (let ((group gnus-newsgroup-name))
7152       (when group
7153         (when gnus-newsgroup-kill-headers
7154           (setq gnus-newsgroup-killed
7155                 (gnus-compress-sequence
7156                  (gnus-sorted-union
7157                   (gnus-list-range-intersection
7158                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7159                   gnus-newsgroup-unreads)
7160                  t)))
7161         (unless (listp (cdr gnus-newsgroup-killed))
7162           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7163         (let ((headers gnus-newsgroup-headers)
7164               (ephemeral-p (gnus-ephemeral-group-p group))
7165               info)
7166           (unless ephemeral-p
7167             (setq info (copy-sequence (gnus-get-info group))
7168                   info (delq (gnus-info-params info) info)))
7169           ;; Set the new ranges of read articles.
7170           (with-current-buffer gnus-group-buffer
7171             (gnus-undo-force-boundary))
7172           (gnus-update-read-articles
7173            group (gnus-sorted-union
7174                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7175           ;; Set the current article marks.
7176           (let ((gnus-newsgroup-scored
7177                  (if (and (not gnus-save-score)
7178                           (not non-destructive))
7179                      nil
7180                    gnus-newsgroup-scored)))
7181             (save-excursion
7182               (gnus-update-marks)))
7183           ;; Do the cross-ref thing.
7184           (when gnus-use-cross-reference
7185             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7186           ;; Do not switch windows but change the buffer to work.
7187           (set-buffer gnus-group-buffer)
7188           (unless ephemeral-p
7189             (gnus-group-update-group
7190              group nil
7191              (equal info
7192                     (setq info (copy-sequence (gnus-get-info group))
7193                           info (delq (gnus-info-params info) info))))))))))
7194
7195 (defun gnus-summary-save-newsrc (&optional force)
7196   "Save the current number of read/marked articles in the dribble buffer.
7197 The dribble buffer will then be saved.
7198 If FORCE (the prefix), also save the .newsrc file(s)."
7199   (interactive "P")
7200   (gnus-summary-update-info t)
7201   (if force
7202       (gnus-save-newsrc-file)
7203     (gnus-dribble-save)))
7204
7205 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7206
7207 (defun gnus-summary-exit (&optional temporary leave-hidden)
7208   "Exit reading current newsgroup, and then return to group selection mode.
7209 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7210   (interactive)
7211   (gnus-set-global-variables)
7212   (when (gnus-buffer-live-p gnus-article-buffer)
7213     (with-current-buffer gnus-article-buffer
7214       (mm-destroy-parts gnus-article-mime-handles)
7215       ;; Set it to nil for safety reason.
7216       (setq gnus-article-mime-handle-alist nil)
7217       (setq gnus-article-mime-handles nil)))
7218   (gnus-kill-save-kill-buffer)
7219   (gnus-async-halt-prefetch)
7220   (let* ((group gnus-newsgroup-name)
7221          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7222          (gnus-group-is-exiting-p t)
7223          (article-buffer gnus-article-buffer)
7224          (original-article-buffer gnus-original-article-buffer)
7225          (mode major-mode)
7226          (group-point nil)
7227          (buf (current-buffer))
7228          ;; `gnus-single-article-buffer' is nil buffer-locally in
7229          ;; ephemeral group of which summary buffer will be killed,
7230          ;; but the global value may be non-nil.
7231          (single-article-buffer gnus-single-article-buffer))
7232     (unless quit-config
7233       ;; Do adaptive scoring, and possibly save score files.
7234       (when gnus-newsgroup-adaptive
7235         (gnus-score-adaptive))
7236       (when gnus-use-scoring
7237         (gnus-score-save)))
7238     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7239     (when gnus-use-cache
7240       (gnus-cache-possibly-remove-articles)
7241       (gnus-cache-save-buffers))
7242     (gnus-async-prefetch-remove-group group)
7243     (when gnus-suppress-duplicates
7244       (gnus-dup-enter-articles))
7245     (when gnus-use-trees
7246       (gnus-tree-close group))
7247     (when gnus-use-cache
7248       (gnus-cache-write-active))
7249     ;; Remove entries for this group.
7250     (nnmail-purge-split-history (gnus-group-real-name group))
7251     ;; Make all changes in this group permanent.
7252     (unless quit-config
7253       (gnus-run-hooks 'gnus-exit-group-hook)
7254       (gnus-summary-update-info))
7255     (gnus-close-group group)
7256     ;; Make sure where we were, and go to next newsgroup.
7257     (set-buffer gnus-group-buffer)
7258     (unless quit-config
7259       (gnus-group-jump-to-group group))
7260     (gnus-run-hooks 'gnus-summary-exit-hook)
7261     (unless (or quit-config
7262                 (not gnus-summary-next-group-on-exit)
7263                 ;; If this group has disappeared from the summary
7264                 ;; buffer, don't skip forwards.
7265                 (not (string= group (gnus-group-group-name))))
7266       (gnus-group-next-unread-group 1))
7267     (setq group-point (point))
7268     (if temporary
7269         nil                             ;Nothing to do.
7270       (set-buffer buf)
7271       (if (not gnus-kill-summary-on-exit)
7272           (progn
7273             (gnus-deaden-summary)
7274             (setq mode nil))
7275         (when (get-buffer gnus-article-buffer)
7276           (bury-buffer gnus-article-buffer))
7277         ;; Return to group mode buffer.
7278         (when (eq mode 'gnus-summary-mode)
7279           (gnus-kill-buffer buf)))
7280
7281       (setq gnus-current-select-method gnus-select-method)
7282       (set-buffer gnus-group-buffer)
7283       (if quit-config
7284           (gnus-handle-ephemeral-exit quit-config)
7285         (goto-char group-point)
7286         ;; If gnus-group-buffer is already displayed, make sure we also move
7287         ;; the cursor in the window that displays it.
7288         (let ((win (get-buffer-window (current-buffer) 0)))
7289           (if win (set-window-point win (point))))
7290         (unless leave-hidden
7291           (gnus-configure-windows 'group 'force)))
7292
7293       ;; If we have several article buffers, we kill them at exit.
7294       (unless single-article-buffer
7295         (when (gnus-buffer-live-p article-buffer)
7296           (with-current-buffer article-buffer
7297             ;; Don't kill sticky article buffers
7298             (unless (eq major-mode 'gnus-sticky-article-mode)
7299               (gnus-kill-buffer article-buffer)
7300               (setq gnus-article-current nil))))
7301         (gnus-kill-buffer original-article-buffer))
7302
7303       ;; Clear the current group name.
7304       (unless quit-config
7305         (setq gnus-newsgroup-name nil)))))
7306
7307 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7308 (defun gnus-summary-exit-no-update (&optional no-questions)
7309   "Quit reading current newsgroup without updating read article info."
7310   (interactive)
7311   (let* ((group gnus-newsgroup-name)
7312          (gnus-group-is-exiting-p t)
7313          (gnus-group-is-exiting-without-update-p t)
7314          (quit-config (gnus-group-quit-config group)))
7315     (when (or no-questions
7316               gnus-expert-user
7317               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7318       (gnus-async-halt-prefetch)
7319       (run-hooks 'gnus-summary-prepare-exit-hook)
7320       (when (gnus-buffer-live-p gnus-article-buffer)
7321         (with-current-buffer gnus-article-buffer
7322           (gnus-article-stop-animations)
7323           (mm-destroy-parts gnus-article-mime-handles)
7324           ;; Set it to nil for safety reason.
7325           (setq gnus-article-mime-handle-alist nil)
7326           (setq gnus-article-mime-handles nil)))
7327       ;; If we have several article buffers, we kill them at exit.
7328       (unless gnus-single-article-buffer
7329         (gnus-kill-buffer gnus-article-buffer)
7330         (gnus-kill-buffer gnus-original-article-buffer)
7331         (setq gnus-article-current nil))
7332       ;; Return to the group buffer.
7333       (gnus-configure-windows 'group 'force)
7334       (if (not gnus-kill-summary-on-exit)
7335           (gnus-deaden-summary)
7336         (gnus-close-group group)
7337         (gnus-kill-buffer gnus-summary-buffer))
7338       (unless gnus-single-article-buffer
7339         (setq gnus-article-current nil))
7340       (when gnus-use-trees
7341         (gnus-tree-close group))
7342       (gnus-async-prefetch-remove-group group)
7343       (when (get-buffer gnus-article-buffer)
7344         (bury-buffer gnus-article-buffer))
7345       ;; Clear the current group name.
7346       (setq gnus-newsgroup-name nil)
7347       (unless (gnus-ephemeral-group-p group)
7348         (gnus-group-update-group group nil t))
7349       (when (equal (gnus-group-group-name) group)
7350         (gnus-group-next-unread-group 1))
7351       (when quit-config
7352         (gnus-handle-ephemeral-exit quit-config)))))
7353
7354 (defun gnus-handle-ephemeral-exit (quit-config)
7355   "Handle movement when leaving an ephemeral group.
7356 The state which existed when entering the ephemeral is reset."
7357   (if (not (buffer-live-p (car quit-config)))
7358       (gnus-configure-windows 'group 'force)
7359     (set-buffer (car quit-config))
7360     (unless (eq (cdr quit-config) 'group)
7361       (setq gnus-current-select-method
7362             (gnus-find-method-for-group gnus-newsgroup-name)))
7363     (cond ((eq major-mode 'gnus-summary-mode)
7364            (gnus-set-global-variables))
7365           ((eq major-mode 'gnus-article-mode)
7366            (save-current-buffer
7367              ;; The `gnus-summary-buffer' variable may point
7368              ;; to the old summary buffer when using a single
7369              ;; article buffer.
7370              (unless (gnus-buffer-live-p gnus-summary-buffer)
7371                (set-buffer gnus-group-buffer))
7372              (set-buffer gnus-summary-buffer)
7373              (gnus-set-global-variables))))
7374     (if (or (eq (cdr quit-config) 'article)
7375             (eq (cdr quit-config) 'pick))
7376         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7377             (gnus-configure-windows 'pick 'force)
7378           (gnus-configure-windows (cdr quit-config) 'force))
7379       (gnus-configure-windows (cdr quit-config) 'force))
7380     (when (eq major-mode 'gnus-summary-mode)
7381       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7382                                                      next-unread-noselect))
7383           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7384                                   'next-noselect)
7385                               (gnus-summary-next-subject 1 nil t))
7386                              ((eq gnus-auto-select-on-ephemeral-exit
7387                                   'next-unread-noselect)
7388                               (gnus-summary-next-subject 1 t t))))
7389             ;; Hide the article buffer which displays the article different
7390             ;; from the one that the cursor points to in the summary buffer.
7391             (gnus-configure-windows 'summary 'force))
7392         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7393                (gnus-summary-next-subject 1))
7394               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7395                (gnus-summary-next-subject 1 t))))
7396       (gnus-summary-recenter)
7397       (gnus-summary-position-point))))
7398
7399 ;;; Dead summaries.
7400
7401 (defvar gnus-dead-summary-mode-map
7402   (let ((map (make-keymap)))
7403     (suppress-keymap map)
7404     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7405     (dolist (key '("\C-d" "\r" "\177" [delete]))
7406       (define-key map key 'gnus-summary-wake-up-the-dead))
7407     (dolist (key '("q" "Q"))
7408       (define-key map key 'bury-buffer))
7409     map))
7410
7411 (define-minor-mode gnus-dead-summary-mode
7412   "Minor mode for Gnus summary buffers."
7413   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7414   (unless (derived-mode-p 'gnus-summary-mode)
7415     (setq gnus-dead-summary-mode nil)))
7416
7417 (defun gnus-deaden-summary ()
7418   "Make the current summary buffer into a dead summary buffer."
7419   ;; Kill any previous dead summary buffer.
7420   (when (and gnus-dead-summary
7421              (buffer-name gnus-dead-summary))
7422     (with-current-buffer gnus-dead-summary
7423       (when gnus-dead-summary-mode
7424         (kill-buffer (current-buffer)))))
7425   ;; Make this the current dead summary.
7426   (setq gnus-dead-summary (current-buffer))
7427   (gnus-dead-summary-mode 1)
7428   (let ((name (buffer-name)))
7429     (when (string-match "Summary" name)
7430       (rename-buffer
7431        (concat (substring name 0 (match-beginning 0)) "Dead "
7432                (substring name (match-beginning 0)))
7433        t)
7434       (bury-buffer))))
7435
7436 (defun gnus-kill-or-deaden-summary (buffer)
7437   "Kill or deaden the summary BUFFER."
7438   (save-excursion
7439     (when (and (buffer-name buffer)
7440                (not gnus-single-article-buffer))
7441       (with-current-buffer buffer
7442         (gnus-kill-buffer gnus-article-buffer)
7443         (gnus-kill-buffer gnus-original-article-buffer)))
7444     (cond
7445      ;; Kill the buffer.
7446      (gnus-kill-summary-on-exit
7447       (when (and gnus-use-trees
7448                  (gnus-buffer-exists-p buffer))
7449         (with-current-buffer buffer
7450           (gnus-tree-close gnus-newsgroup-name)))
7451       (gnus-kill-buffer buffer))
7452      ;; Deaden the buffer.
7453      ((gnus-buffer-exists-p buffer)
7454       (with-current-buffer buffer
7455         (gnus-deaden-summary))))))
7456
7457 (defun gnus-summary-wake-up-the-dead (&rest args)
7458   "Wake up the dead summary buffer."
7459   (interactive)
7460   (gnus-dead-summary-mode -1)
7461   (let ((name (buffer-name)))
7462     (when (string-match "Dead " name)
7463       (rename-buffer
7464        (concat (substring name 0 (match-beginning 0))
7465                (substring name (match-end 0)))
7466        t)))
7467   (gnus-message 3 "This dead summary is now alive again"))
7468
7469 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7470 (defun gnus-summary-describe-group (&optional force)
7471   "Describe the current newsgroup."
7472   (interactive "P")
7473   (gnus-group-describe-group force gnus-newsgroup-name))
7474
7475 (defun gnus-summary-describe-briefly ()
7476   "Describe summary mode commands briefly."
7477   (interactive)
7478   (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")))
7479
7480 ;; Walking around group mode buffer from summary mode.
7481
7482 (defun gnus-summary-next-group (&optional no-article target-group backward)
7483   "Exit current newsgroup and then select next unread newsgroup.
7484 If prefix argument NO-ARTICLE is non-nil, no article is selected
7485 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7486 previous group instead."
7487   (interactive "P")
7488   ;; Stop pre-fetching.
7489   (gnus-async-halt-prefetch)
7490   (let ((current-group gnus-newsgroup-name)
7491         (current-buffer (current-buffer))
7492         entered)
7493     ;; First we semi-exit this group to update Xrefs and all variables.
7494     ;; We can't do a real exit, because the window conf must remain
7495     ;; the same in case the user is prompted for info, and we don't
7496     ;; want the window conf to change before that...
7497     (gnus-summary-exit t)
7498     (while (not entered)
7499       ;; Then we find what group we are supposed to enter.
7500       (set-buffer gnus-group-buffer)
7501       (gnus-group-jump-to-group current-group)
7502       (setq target-group
7503             (or target-group
7504                 (if (eq gnus-keep-same-level 'best)
7505                     (gnus-summary-best-group gnus-newsgroup-name)
7506                   (gnus-summary-search-group backward gnus-keep-same-level))))
7507       (if (not target-group)
7508           ;; There are no further groups, so we return to the group
7509           ;; buffer.
7510           (progn
7511             (gnus-message 5 "Returning to the group buffer")
7512             (setq entered t)
7513             (when (gnus-buffer-live-p current-buffer)
7514               (set-buffer current-buffer)
7515               (gnus-summary-exit))
7516             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7517         ;; We try to enter the target group.
7518         (gnus-group-jump-to-group target-group)
7519         (let ((unreads (gnus-group-group-unread)))
7520           (if (and (or (eq t unreads)
7521                        (and unreads (not (zerop unreads))))
7522                    (gnus-summary-read-group
7523                     target-group nil no-article
7524                     (and (buffer-name current-buffer) current-buffer)
7525                     nil backward))
7526               (setq entered t)
7527             (setq current-group target-group
7528                   target-group nil)))))))
7529
7530 (defun gnus-summary-prev-group (&optional no-article)
7531   "Exit current newsgroup and then select previous unread newsgroup.
7532 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7533   (interactive "P")
7534   (gnus-summary-next-group no-article nil t))
7535
7536 ;; Walking around summary lines.
7537
7538 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7539   "Go to the first subject satisfying any non-nil constraint.
7540 If UNREAD is non-nil, the article should be unread.
7541 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7542 If UNSEEN is non-nil, the article should be unseen as well as unread.
7543 Returns the article selected or nil if there are no matching articles."
7544   (interactive "P")
7545   (cond
7546    ;; Empty summary.
7547    ((null gnus-newsgroup-data)
7548     (gnus-message 3 "No articles in the group")
7549     nil)
7550    ;; Pick the first article.
7551    ((not (or unread undownloaded unseen))
7552     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7553     (gnus-data-number (car gnus-newsgroup-data)))
7554    ;; Find the first unread article.
7555    (t
7556     (let ((data gnus-newsgroup-data))
7557       (while (and data
7558                   (let ((num (gnus-data-number (car data))))
7559                     (or (memq num gnus-newsgroup-unfetched)
7560                         (not (or (and unread
7561                                       (memq num gnus-newsgroup-unreads))
7562                                  (and undownloaded
7563                                       (memq num gnus-newsgroup-undownloaded))
7564                                  (and unseen
7565                                       (memq num gnus-newsgroup-unseen)
7566                                       (memq num gnus-newsgroup-unreads)))))))
7567         (setq data (cdr data)))
7568       (prog1
7569           (if data
7570               (progn
7571                 (goto-char (gnus-data-pos (car data)))
7572                 (gnus-data-number (car data)))
7573             (gnus-message 3 "No more%s articles"
7574                           (let* ((r (when unread " unread"))
7575                                  (d (when undownloaded " undownloaded"))
7576                                  (s (when unseen " unseen"))
7577                                  (l (delq nil (list r d s))))
7578                             (cond ((= 3 (length l))
7579                                    (concat r "," d ", or" s))
7580                                   ((= 2 (length l))
7581                                    (concat (car l) ", or" (cadr l)))
7582                                   ((= 1 (length l))
7583                                    (car l))
7584                                   (t
7585                                    ""))))
7586             nil
7587             )
7588         (gnus-summary-position-point))))))
7589
7590 (defun gnus-summary-next-subject (n &optional unread dont-display)
7591   "Go to next N'th summary line.
7592 If N is negative, go to the previous N'th subject line.
7593 If UNREAD is non-nil, only unread articles are selected.
7594 The difference between N and the actual number of steps taken is
7595 returned."
7596   (interactive "p")
7597   (let ((backward (< n 0))
7598         (n (abs n)))
7599     (while (and (> n 0)
7600                 (if backward
7601                     (gnus-summary-find-prev unread)
7602                   (gnus-summary-find-next unread)))
7603       (unless (zerop (setq n (1- n)))
7604         (gnus-summary-show-thread)))
7605     (when (/= 0 n)
7606       (gnus-message 7 "No more%s articles"
7607                     (if unread " unread" "")))
7608     (unless dont-display
7609       (gnus-summary-recenter)
7610       (gnus-summary-position-point))
7611     n))
7612
7613 (defun gnus-summary-next-unread-subject (n)
7614   "Go to next N'th unread summary line."
7615   (interactive "p")
7616   (gnus-summary-next-subject n t))
7617
7618 (defun gnus-summary-prev-subject (n &optional unread)
7619   "Go to previous N'th summary line.
7620 If optional argument UNREAD is non-nil, only unread article is selected."
7621   (interactive "p")
7622   (gnus-summary-next-subject (- n) unread))
7623
7624 (defun gnus-summary-prev-unread-subject (n)
7625   "Go to previous N'th unread summary line."
7626   (interactive "p")
7627   (gnus-summary-next-subject (- n) t))
7628
7629 (defun gnus-summary-goto-subjects (articles)
7630   "Insert the subject header for ARTICLES in the current buffer."
7631   (save-excursion
7632     (dolist (article articles)
7633       (gnus-summary-goto-subject article t)))
7634   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7635   (gnus-summary-position-point))
7636
7637 (defun gnus-summary-goto-subject (article &optional force silent)
7638   "Go to the subject line of ARTICLE.
7639 If FORCE, also allow jumping to articles not currently shown."
7640   (interactive "nArticle number: ")
7641   (unless (numberp article)
7642     (error "Article %s is not a number" article))
7643   (let ((b (point))
7644         (data (gnus-data-find article)))
7645     ;; We read in the article if we have to.
7646     (and (not data)
7647          force
7648          (gnus-summary-insert-subject
7649           article
7650           (if (or (numberp force) (vectorp force)) force)
7651           t)
7652          (setq data (gnus-data-find article)))
7653     (goto-char b)
7654     (if (not data)
7655         (progn
7656           (unless silent
7657             (gnus-message 3 "Can't find article %d" article))
7658           nil)
7659       (let ((pt (gnus-data-pos data)))
7660         (goto-char pt)
7661         (gnus-summary-set-article-display-arrow pt))
7662       (gnus-summary-position-point)
7663       article)))
7664
7665 ;; Walking around summary lines with displaying articles.
7666
7667 (defun gnus-summary-expand-window (&optional arg)
7668   "Make the summary buffer take up the entire Emacs frame.
7669 Given a prefix, will force an `article' buffer configuration."
7670   (interactive "P")
7671   (if arg
7672       (gnus-configure-windows 'article 'force)
7673     (gnus-configure-windows 'summary 'force)))
7674
7675 (defun gnus-summary-display-article (article &optional all-header)
7676   "Display ARTICLE in article buffer."
7677   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7678                (with-current-buffer gnus-article-buffer
7679                  (eq major-mode 'gnus-article-mode)))
7680     (gnus-article-setup-buffer))
7681   (gnus-set-global-variables)
7682   (with-current-buffer gnus-article-buffer
7683     (setq gnus-article-charset gnus-newsgroup-charset)
7684     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7685     (mm-enable-multibyte))
7686   (if (null article)
7687       nil
7688     (prog1
7689         (if gnus-summary-display-article-function
7690             (funcall gnus-summary-display-article-function article all-header)
7691           (gnus-article-prepare article all-header))
7692       (gnus-run-hooks 'gnus-select-article-hook)
7693       (when (and gnus-current-article
7694                  (not (zerop gnus-current-article)))
7695         (gnus-summary-goto-subject gnus-current-article))
7696       (gnus-summary-recenter)
7697       (when (and gnus-use-trees gnus-show-threads)
7698         (gnus-possibly-generate-tree article)
7699         (gnus-highlight-selected-tree article))
7700       ;; Successfully display article.
7701       (gnus-article-set-window-start
7702        (cdr (assq article gnus-newsgroup-bookmarks))))))
7703
7704 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7705   "Select the current article.
7706 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7707 non-nil, the article will be re-fetched even if it already present in
7708 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7709 be displayed."
7710   ;; Make sure we are in the summary buffer to work around bbdb bug.
7711   (unless (eq major-mode 'gnus-summary-mode)
7712     (set-buffer gnus-summary-buffer))
7713   (let ((article (or article (gnus-summary-article-number)))
7714         (all-headers (not (not all-headers))) ;Must be t or nil.
7715         gnus-summary-display-article-function)
7716     (and (not pseudo)
7717          (gnus-summary-article-pseudo-p article)
7718          (error "This is a pseudo-article"))
7719     (with-current-buffer gnus-summary-buffer
7720       (if (or (and gnus-single-article-buffer
7721                    (or (null gnus-current-article)
7722                        (null gnus-article-current)
7723                        (null (get-buffer gnus-article-buffer))
7724                        (not (eq article (cdr gnus-article-current)))
7725                        (not (equal (car gnus-article-current)
7726                                    gnus-newsgroup-name))
7727                        (not (get-buffer gnus-original-article-buffer))))
7728               (and (not gnus-single-article-buffer)
7729                    (or (null gnus-current-article)
7730                        (not (get-buffer gnus-original-article-buffer))
7731                        (not (eq gnus-current-article article))))
7732               force)
7733           ;; The requested article is different from the current article.
7734           (progn
7735             (gnus-summary-display-article article all-headers)
7736             (when (gnus-buffer-live-p gnus-article-buffer)
7737               (with-current-buffer gnus-article-buffer
7738                 (if (not gnus-article-decoded-p) ;; a local variable
7739                     (mm-disable-multibyte))))
7740             (gnus-article-set-window-start
7741              (cdr (assq article gnus-newsgroup-bookmarks)))
7742             article)
7743         'old))))
7744
7745 (defun gnus-summary-force-verify-and-decrypt ()
7746   "Display buttons for signed/encrypted parts and verify/decrypt them."
7747   (interactive)
7748   (let ((mm-verify-option 'known)
7749         (mm-decrypt-option 'known)
7750         (gnus-article-emulate-mime t)
7751         (gnus-buttonized-mime-types (append (list "multipart/signed"
7752                                                   "multipart/encrypted")
7753                                             gnus-buttonized-mime-types)))
7754     (gnus-summary-select-article nil 'force)))
7755
7756 (defun gnus-summary-set-current-mark (&optional current-mark)
7757   "Obsolete function."
7758   nil)
7759
7760 (defun gnus-summary-next-article (&optional unread subject backward push)
7761   "Select the next article.
7762 If UNREAD, only unread articles are selected.
7763 If SUBJECT, only articles with SUBJECT are selected.
7764 If BACKWARD, the previous article is selected instead of the next."
7765   (interactive "P")
7766   ;; Make sure we are in the summary buffer.
7767   (unless (eq major-mode 'gnus-summary-mode)
7768     (set-buffer gnus-summary-buffer))
7769   (cond
7770    ;; Is there such an article?
7771    ((and (gnus-summary-search-forward unread subject backward)
7772          (or (gnus-summary-display-article (gnus-summary-article-number))
7773              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7774     (gnus-summary-position-point))
7775    ;; If not, we try the first unread, if that is wanted.
7776    ((and subject
7777          gnus-auto-select-same
7778          (gnus-summary-first-unread-article))
7779     (gnus-summary-position-point)
7780     (gnus-message 6 "Wrapped"))
7781    ;; Try to get next/previous article not displayed in this group.
7782    ((and gnus-auto-extend-newsgroup
7783          (not unread) (not subject))
7784     (gnus-summary-goto-article
7785      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7786      nil (count-lines (point-min) (point))))
7787    ;; Go to next/previous group.
7788    (t
7789     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7790       (gnus-summary-jump-to-group gnus-newsgroup-name))
7791     (let ((cmd (if (featurep 'xemacs)
7792                    last-command-char
7793                  last-command-event))
7794           (point
7795            (with-current-buffer gnus-group-buffer
7796              (point)))
7797           (current-summary (current-buffer))
7798           (group
7799            (if (eq gnus-keep-same-level 'best)
7800                (gnus-summary-best-group gnus-newsgroup-name)
7801              (gnus-summary-search-group backward gnus-keep-same-level))))
7802       ;; Select next unread newsgroup automagically.
7803       (cond
7804        ((or (not gnus-auto-select-next)
7805             (not cmd))
7806         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7807        ((or (eq gnus-auto-select-next 'quietly)
7808             (and (eq gnus-auto-select-next 'slightly-quietly)
7809                  push)
7810             (and (eq gnus-auto-select-next 'almost-quietly)
7811                  (gnus-summary-last-article-p)))
7812         ;; Select quietly.
7813         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7814             (gnus-summary-exit)
7815           (gnus-message 7 "No more%s articles (%s)..."
7816                         (if unread " unread" "")
7817                         (if group (concat "selecting " group)
7818                           "exiting"))
7819           (gnus-summary-next-group nil group backward)))
7820        (t
7821         (when (gnus-key-press-event-p last-input-event)
7822           ;; Somehow or other, we may now have selected a different
7823           ;; window.  Make point go back to the summary buffer.
7824           (when (eq current-summary (current-buffer))
7825             ;; FIXME: This burps when get-buffer-window returns nil.
7826             (select-window (get-buffer-window current-summary 0)))
7827           (gnus-summary-walk-group-buffer
7828            gnus-newsgroup-name cmd unread backward point))))))))
7829
7830 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7831   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7832                       (?\C-p (gnus-group-prev-unread-group 1))))
7833         (cursor-in-echo-area t)
7834         keve key group ended prompt)
7835     (with-current-buffer gnus-group-buffer
7836       (goto-char start)
7837       (setq group
7838             (if (eq gnus-keep-same-level 'best)
7839                 (gnus-summary-best-group gnus-newsgroup-name)
7840               (gnus-summary-search-group backward gnus-keep-same-level))))
7841     (while (not ended)
7842       (setq prompt
7843             (format
7844              "No more%s articles%s " (if unread " unread" "")
7845              (if (and group
7846                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7847                  (format " (Type %s for %s [%s])"
7848                          (single-key-description cmd)
7849                          (gnus-group-decoded-name group)
7850                          (gnus-group-unread group))
7851                (format " (Type %s to exit %s)"
7852                        (single-key-description cmd)
7853                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7854       ;; Confirm auto selection.
7855       (setq key (car (setq keve (gnus-read-event-char prompt)))
7856             ended t)
7857       (cond
7858        ((assq key keystrokes)
7859         (let ((obuf (current-buffer)))
7860           (switch-to-buffer gnus-group-buffer)
7861           (when group
7862             (gnus-group-jump-to-group group))
7863           (eval (cadr (assq key keystrokes)))
7864           (setq group (gnus-group-group-name))
7865           (switch-to-buffer obuf))
7866         (setq ended nil))
7867        ((equal key cmd)
7868         (if (or (not group)
7869                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7870             (gnus-summary-exit)
7871           (gnus-summary-next-group nil group backward)))
7872        (t
7873         (push (cdr keve) unread-command-events))))))
7874
7875 (defun gnus-summary-next-unread-article ()
7876   "Select unread article after current one."
7877   (interactive)
7878   (gnus-summary-next-article
7879    (or (not (eq gnus-summary-goto-unread 'never))
7880        (gnus-summary-last-article-p (gnus-summary-article-number)))
7881    (and gnus-auto-select-same
7882         (gnus-summary-article-subject))))
7883
7884 (defun gnus-summary-prev-article (&optional unread subject)
7885   "Select the article before the current one.
7886 If UNREAD is non-nil, only unread articles are selected."
7887   (interactive "P")
7888   (gnus-summary-next-article unread subject t))
7889
7890 (defun gnus-summary-prev-unread-article ()
7891   "Select unread article before current one."
7892   (interactive)
7893   (gnus-summary-prev-article
7894    (or (not (eq gnus-summary-goto-unread 'never))
7895        (gnus-summary-first-article-p (gnus-summary-article-number)))
7896    (and gnus-auto-select-same
7897         (gnus-summary-article-subject))))
7898
7899 (defun gnus-summary-next-page (&optional lines circular stop)
7900   "Show next page of the selected article.
7901 If at the end of the current article, select the next article.
7902 LINES says how many lines should be scrolled up.
7903
7904 If CIRCULAR is non-nil, go to the start of the article instead of
7905 selecting the next article when reaching the end of the current
7906 article.
7907
7908 If STOP is non-nil, just stop when reaching the end of the message.
7909
7910 Also see the variable `gnus-article-skip-boring'."
7911   (interactive "P")
7912   (setq gnus-summary-buffer (current-buffer))
7913   (gnus-set-global-variables)
7914   (let ((article (gnus-summary-article-number))
7915         (article-window (get-buffer-window gnus-article-buffer t))
7916         endp)
7917     ;; If the buffer is empty, we have no article.
7918     (unless article
7919       (error "No article to select"))
7920     (gnus-configure-windows 'article)
7921     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7922         (if (and (eq gnus-summary-goto-unread 'never)
7923                  (not (gnus-summary-last-article-p article)))
7924             (gnus-summary-next-article)
7925           (gnus-summary-next-unread-article))
7926       (if (or (null gnus-current-article)
7927               (null gnus-article-current)
7928               (/= article (cdr gnus-article-current))
7929               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7930           ;; Selected subject is different from current article's.
7931           (gnus-summary-display-article article)
7932         (when article-window
7933           (gnus-eval-in-buffer-window gnus-article-buffer
7934             (setq endp (or (gnus-article-next-page lines)
7935                            (gnus-article-only-boring-p))))
7936           (when endp
7937             (cond ((or stop gnus-summary-stop-at-end-of-message)
7938                    (gnus-message 3 "End of message"))
7939                   (circular
7940                    (gnus-summary-beginning-of-article))
7941                   (lines
7942                    (gnus-message 3 "End of message"))
7943                   ((null lines)
7944                    (if (and (eq gnus-summary-goto-unread 'never)
7945                             (not (gnus-summary-last-article-p article)))
7946                        (gnus-summary-next-article)
7947                      (gnus-summary-next-unread-article))))))))
7948     (gnus-summary-recenter)
7949     (gnus-summary-position-point)))
7950
7951 (defun gnus-summary-prev-page (&optional lines move)
7952   "Show previous page of selected article.
7953 Argument LINES specifies lines to be scrolled down.
7954 If MOVE, move to the previous unread article if point is at
7955 the beginning of the buffer."
7956   (interactive "P")
7957   (let ((article (gnus-summary-article-number))
7958         (article-window (get-buffer-window gnus-article-buffer t))
7959         endp)
7960     (gnus-configure-windows 'article)
7961     (if (or (null gnus-current-article)
7962             (null gnus-article-current)
7963             (/= article (cdr gnus-article-current))
7964             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7965         ;; Selected subject is different from current article's.
7966         (gnus-summary-display-article article)
7967       (gnus-summary-recenter)
7968       (when article-window
7969         (gnus-eval-in-buffer-window gnus-article-buffer
7970           (setq endp (gnus-article-prev-page lines)))
7971         (when (and move endp)
7972           (cond (lines
7973                  (gnus-message 3 "Beginning of message"))
7974                 ((null lines)
7975                  (if (and (eq gnus-summary-goto-unread 'never)
7976                           (not (gnus-summary-first-article-p article)))
7977                      (gnus-summary-prev-article)
7978                    (gnus-summary-prev-unread-article))))))))
7979   (gnus-summary-position-point))
7980
7981 (defun gnus-summary-prev-page-or-article (&optional lines)
7982   "Show previous page of selected article.
7983 Argument LINES specifies lines to be scrolled down.
7984 If at the beginning of the article, go to the next article."
7985   (interactive "P")
7986   (gnus-summary-prev-page lines t))
7987
7988 (defun gnus-summary-scroll-up (lines)
7989   "Scroll up (or down) one line current article.
7990 Argument LINES specifies lines to be scrolled up (or down if negative).
7991 If no article is selected, then the current article will be selected first."
7992   (interactive "p")
7993   (gnus-configure-windows 'article)
7994   (gnus-summary-show-thread)
7995   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7996     (gnus-eval-in-buffer-window gnus-article-buffer
7997       (cond ((> lines 0)
7998              (when (gnus-article-next-page lines)
7999                (gnus-message 3 "End of message")))
8000             ((< lines 0)
8001              (gnus-article-prev-page (- lines))))))
8002   (gnus-summary-recenter)
8003   (gnus-summary-position-point))
8004
8005 (defun gnus-summary-scroll-down (lines)
8006   "Scroll down (or up) one line current article.
8007 Argument LINES specifies lines to be scrolled down (or up if negative).
8008 If no article is selected, then the current article will be selected first."
8009   (interactive "p")
8010   (gnus-summary-scroll-up (- lines)))
8011
8012 (defun gnus-summary-next-same-subject ()
8013   "Select next article which has the same subject as current one."
8014   (interactive)
8015   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8016
8017 (defun gnus-summary-prev-same-subject ()
8018   "Select previous article which has the same subject as current one."
8019   (interactive)
8020   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8021
8022 (defun gnus-summary-next-unread-same-subject ()
8023   "Select next unread article which has the same subject as current one."
8024   (interactive)
8025   (gnus-summary-next-article t (gnus-summary-article-subject)))
8026
8027 (defun gnus-summary-prev-unread-same-subject ()
8028   "Select previous unread article which has the same subject as current one."
8029   (interactive)
8030   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8031
8032 (defun gnus-summary-first-unread-article ()
8033   "Select the first unread article.
8034 Return nil if there are no unread articles."
8035   (interactive)
8036   (prog1
8037       (when (gnus-summary-first-subject t)
8038         (gnus-summary-show-thread)
8039         (gnus-summary-first-subject t)
8040         (gnus-summary-display-article (gnus-summary-article-number)))
8041     (gnus-summary-position-point)))
8042
8043 (defun gnus-summary-first-unread-subject ()
8044   "Place the point on the subject line of the first unread article.
8045 Return nil if there are no unread articles."
8046   (interactive)
8047   (prog1
8048       (when (gnus-summary-first-subject t)
8049         (gnus-summary-show-thread)
8050         (gnus-summary-first-subject t))
8051     (gnus-summary-position-point)))
8052
8053 (defun gnus-summary-first-unseen-subject ()
8054   "Place the point on the subject line of the first unseen article.
8055 Return nil if there are no unseen articles."
8056   (interactive)
8057   (prog1
8058       (when (gnus-summary-first-subject nil nil t)
8059         (gnus-summary-show-thread)
8060         (gnus-summary-first-subject nil nil t))
8061     (gnus-summary-position-point)))
8062
8063 (defun gnus-summary-first-unseen-or-unread-subject ()
8064   "Place the point on the subject line of the first unseen and unread article.
8065 If all article have been seen, on the subject line of the first unread
8066 article."
8067   (interactive)
8068   (prog1
8069       (unless (when (gnus-summary-first-subject nil nil t)
8070                 (gnus-summary-show-thread)
8071                 (gnus-summary-first-subject nil nil t))
8072         (when (gnus-summary-first-subject t)
8073           (gnus-summary-show-thread)
8074           (gnus-summary-first-subject t)))
8075     (gnus-summary-position-point)))
8076
8077 (defun gnus-summary-first-article ()
8078   "Select the first article.
8079 Return nil if there are no articles."
8080   (interactive)
8081   (prog1
8082       (when (gnus-summary-first-subject)
8083         (gnus-summary-show-thread)
8084         (gnus-summary-first-subject)
8085         (gnus-summary-display-article (gnus-summary-article-number)))
8086     (gnus-summary-position-point)))
8087
8088 (defun gnus-summary-best-unread-article (&optional arg)
8089   "Select the unread article with the highest score.
8090 If given a prefix argument, select the next unread article that has a
8091 score higher than the default score."
8092   (interactive "P")
8093   (let ((article (if arg
8094                      (gnus-summary-better-unread-subject)
8095                    (gnus-summary-best-unread-subject))))
8096     (if article
8097         (gnus-summary-goto-article article)
8098       (error "No unread articles"))))
8099
8100 (defun gnus-summary-best-unread-subject ()
8101   "Select the unread subject with the highest score."
8102   (interactive)
8103   (let ((best -1000000)
8104         (data gnus-newsgroup-data)
8105         article score)
8106     (while data
8107       (and (gnus-data-unread-p (car data))
8108            (> (setq score
8109                     (gnus-summary-article-score (gnus-data-number (car data))))
8110               best)
8111            (setq best score
8112                  article (gnus-data-number (car data))))
8113       (setq data (cdr data)))
8114     (when article
8115       (gnus-summary-goto-subject article))
8116     (gnus-summary-position-point)
8117     article))
8118
8119 (defun gnus-summary-better-unread-subject ()
8120   "Select the first unread subject that has a score over the default score."
8121   (interactive)
8122   (let ((data gnus-newsgroup-data)
8123         article score)
8124     (while (and (setq article (gnus-data-number (car data)))
8125                 (or (gnus-data-read-p (car data))
8126                     (not (> (gnus-summary-article-score article)
8127                             gnus-summary-default-score))))
8128       (setq data (cdr data)))
8129     (when article
8130       (gnus-summary-goto-subject article))
8131     (gnus-summary-position-point)
8132     article))
8133
8134 (defun gnus-summary-last-subject ()
8135   "Go to the last displayed subject line in the group."
8136   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8137     (when article
8138       (gnus-summary-goto-subject article))))
8139
8140 (defun gnus-summary-goto-article (article &optional all-headers force)
8141   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8142 If ALL-HEADERS is non-nil, no header lines are hidden.
8143 If FORCE, go to the article even if it isn't displayed.  If FORCE
8144 is a number, it is the line the article is to be displayed on."
8145   (interactive
8146    (list
8147     (gnus-completing-read
8148      "Article number or Message-ID"
8149      (mapcar 'int-to-string gnus-newsgroup-limit))
8150     current-prefix-arg
8151     t))
8152   (prog1
8153       (if (and (stringp article)
8154                (string-match "@\\|%40" article))
8155           (gnus-summary-refer-article article)
8156         (when (stringp article)
8157           (setq article (string-to-number article)))
8158         (if (gnus-summary-goto-subject article force)
8159             (gnus-summary-display-article article all-headers)
8160           (gnus-message 4 "Couldn't go to article %s" article) nil))
8161     (gnus-summary-position-point)))
8162
8163 (defun gnus-summary-goto-last-article ()
8164   "Go to the previously read article."
8165   (interactive)
8166   (prog1
8167       (when gnus-last-article
8168         (gnus-summary-goto-article gnus-last-article nil t))
8169     (gnus-summary-position-point)))
8170
8171 (defun gnus-summary-pop-article (number)
8172   "Pop one article off the history and go to the previous.
8173 NUMBER articles will be popped off."
8174   (interactive "p")
8175   (let (to)
8176     (setq gnus-newsgroup-history
8177           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8178     (if to
8179         (gnus-summary-goto-article (car to) nil t)
8180       (error "Article history empty")))
8181   (gnus-summary-position-point))
8182
8183 ;; Summary commands and functions for limiting the summary buffer.
8184
8185 (defun gnus-summary-limit-to-articles (n)
8186   "Limit the summary buffer to the next N articles.
8187 If not given a prefix, use the process marked articles instead."
8188   (interactive "P")
8189   (prog1
8190       (let ((articles (gnus-summary-work-articles n)))
8191         (setq gnus-newsgroup-processable nil)
8192         (gnus-summary-limit articles))
8193     (gnus-summary-position-point)))
8194
8195 (defun gnus-summary-pop-limit (&optional total)
8196   "Restore the previous limit.
8197 If given a prefix, remove all limits."
8198   (interactive "P")
8199   (when total
8200     (setq gnus-newsgroup-limits
8201           (list (mapcar (lambda (h) (mail-header-number h))
8202                         gnus-newsgroup-headers))))
8203   (unless gnus-newsgroup-limits
8204     (error "No limit to pop"))
8205   (prog1
8206       (gnus-summary-limit nil 'pop)
8207     (gnus-summary-position-point)))
8208
8209 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8210   "Limit the summary buffer to articles that have subjects that match a regexp.
8211 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8212   (interactive
8213    (list (read-string (if current-prefix-arg
8214                           "Exclude subject (regexp): "
8215                         "Limit to subject (regexp): "))
8216          nil current-prefix-arg))
8217   (unless header
8218     (setq header "subject"))
8219   (when (not (equal "" subject))
8220     (prog1
8221         (let ((articles (gnus-summary-find-matching
8222                          (or header "subject") subject 'all nil nil
8223                          not-matching)))
8224           (unless articles
8225             (error "Found no matches for \"%s\"" subject))
8226           (gnus-summary-limit articles))
8227       (gnus-summary-position-point))))
8228
8229 (defun gnus-summary-limit-to-author (from &optional not-matching)
8230   "Limit the summary buffer to articles that have authors that match a regexp.
8231 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8232   (interactive
8233    (list (read-string (if current-prefix-arg
8234                           "Exclude author (regexp): "
8235                         "Limit to author (regexp): "))
8236          current-prefix-arg))
8237   (gnus-summary-limit-to-subject from "from" not-matching))
8238
8239 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8240   "Limit the summary buffer to articles with the given RECIPIENT.
8241
8242 If NOT-MATCHING, exclude RECIPIENT.
8243
8244 To and Cc headers are checked.  You need to include them in
8245 `nnmail-extra-headers'."
8246   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8247   (interactive
8248    (list (read-string (format "%s recipient (regexp): "
8249                               (if current-prefix-arg "Exclude" "Limit to")))
8250          current-prefix-arg))
8251   (when (not (equal "" recipient))
8252     (prog1 (let* ((to
8253                    (if (memq 'To nnmail-extra-headers)
8254                        (gnus-summary-find-matching
8255                         (cons 'extra 'To) recipient 'all nil nil
8256                         not-matching)
8257                      (gnus-message
8258                       1 "`To' isn't present in `nnmail-extra-headers'")
8259                      (sit-for 1)
8260                      nil))
8261                   (cc
8262                    (if (memq 'Cc nnmail-extra-headers)
8263                        (gnus-summary-find-matching
8264                         (cons 'extra 'Cc) recipient 'all nil nil
8265                         not-matching)
8266                      (gnus-message
8267                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8268                      (sit-for 1)
8269                      nil))
8270                   (articles
8271                    (if not-matching
8272                        ;; We need the numbers that are in both lists:
8273                        (mapcar (lambda (a)
8274                                  (and (memq a to) a))
8275                                cc)
8276                      (nconc to cc))))
8277              (unless articles
8278                (error "Found no matches for \"%s\"" recipient))
8279              (gnus-summary-limit articles))
8280       (gnus-summary-position-point))))
8281
8282 (defun gnus-summary-limit-to-address (address &optional not-matching)
8283   "Limit the summary buffer to articles with the given ADDRESS.
8284
8285 If NOT-MATCHING, exclude ADDRESS.
8286
8287 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8288 in `nnmail-extra-headers'."
8289   (interactive
8290    (list (read-string (format "%s address (regexp): "
8291                               (if current-prefix-arg "Exclude" "Limit to")))
8292          current-prefix-arg))
8293   (when (not (equal "" address))
8294     (prog1 (let* ((to
8295                    (if (memq 'To nnmail-extra-headers)
8296                        (gnus-summary-find-matching
8297                         (cons 'extra 'To) address 'all nil nil
8298                         not-matching)
8299                      (gnus-message
8300                       1 "`To' isn't present in `nnmail-extra-headers'")
8301                      (sit-for 1)
8302                      t))
8303                   (cc
8304                    (if (memq 'Cc nnmail-extra-headers)
8305                        (gnus-summary-find-matching
8306                         (cons 'extra 'Cc) address 'all nil nil
8307                         not-matching)
8308                      (gnus-message
8309                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8310                      (sit-for 1)
8311                      t))
8312                   (from
8313                    (gnus-summary-find-matching "from" address
8314                                                'all nil nil not-matching))
8315                   (articles
8316                    (if not-matching
8317                        ;; We need the numbers that are in all lists:
8318                        (if (eq cc t)
8319                            (if (eq to t)
8320                                from
8321                              (mapcar (lambda (a) (car (memq a from))) to))
8322                          (if (eq to t)
8323                              (mapcar (lambda (a) (car (memq a from))) cc)
8324                            (mapcar (lambda (a) (car (memq a from)))
8325                                    (mapcar (lambda (a) (car (memq a to)))
8326                                            cc))))
8327                      (nconc (if (eq to t) nil to)
8328                             (if (eq cc t) nil cc)
8329                             from))))
8330              (unless articles
8331                (error "Found no matches for \"%s\"" address))
8332              (gnus-summary-limit articles))
8333       (gnus-summary-position-point))))
8334
8335 (defun gnus-summary-limit-strange-charsets-predicate (header)
8336   (when (fboundp 'char-charset)
8337     (let ((string (concat (mail-header-subject header)
8338                           (mail-header-from header)))
8339           charset found)
8340       (dotimes (i (1- (length string)))
8341         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8342         (when (string-match "unicode\\|big\\|japanese" charset)
8343           (setq found t)))
8344       found)))
8345
8346 (defun gnus-summary-limit-to-predicate (predicate)
8347   "Limit to articles where PREDICATE returns non-nil.
8348 PREDICATE will be called with the header structures of the
8349 articles."
8350   (let ((articles nil)
8351         (case-fold-search t))
8352     (dolist (header gnus-newsgroup-headers)
8353       (when (funcall predicate header)
8354         (push (mail-header-number header) articles)))
8355     (gnus-summary-limit (nreverse articles))))
8356
8357 (defun gnus-summary-limit-to-age (age &optional younger-p)
8358   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8359 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8360 articles that are younger than AGE days."
8361   (interactive
8362    (let ((younger current-prefix-arg)
8363          (days-got nil)
8364          days)
8365      (while (not days-got)
8366        (setq days (if younger
8367                       (read-string "Limit to articles younger than (in days, older when negative): ")
8368                     (read-string
8369                      "Limit to articles older than (in days, younger when negative): ")))
8370        (when (> (length days) 0)
8371          (setq days (read days)))
8372        (if (numberp days)
8373            (progn
8374              (setq days-got t)
8375              (when (< days 0)
8376                (setq younger (not younger))
8377                (setq days (* days -1))))
8378          (message "Please enter a number.")
8379          (sleep-for 1)))
8380      (list days younger)))
8381   (prog1
8382       (let ((data gnus-newsgroup-data)
8383             (cutoff (days-to-time age))
8384             articles d date is-younger)
8385         (while (setq d (pop data))
8386           (when (and (vectorp (gnus-data-header d))
8387                      (setq date (mail-header-date (gnus-data-header d))))
8388             (setq is-younger (time-less-p
8389                               (time-since (gnus-date-get-time date))
8390                               cutoff))
8391             (when (if younger-p
8392                       is-younger
8393                     (not is-younger))
8394               (push (gnus-data-number d) articles))))
8395         (gnus-summary-limit (nreverse articles)))
8396     (gnus-summary-position-point)))
8397
8398 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8399   "Limit the summary buffer to articles that match an 'extra' header."
8400   (interactive
8401    (let ((header
8402           (intern
8403            (gnus-completing-read
8404             (if current-prefix-arg
8405                 "Exclude extra header"
8406               "Limit extra header")
8407             (mapcar 'symbol-name gnus-extra-headers)
8408             t nil nil
8409             (symbol-name (car gnus-extra-headers))))))
8410      (list header
8411            (read-string (format "%s header %s (regexp): "
8412                                 (if current-prefix-arg "Exclude" "Limit to")
8413                                 header))
8414            current-prefix-arg)))
8415   (when (not (equal "" regexp))
8416     (prog1
8417         (let ((articles (gnus-summary-find-matching
8418                          (cons 'extra header) regexp 'all nil nil
8419                          not-matching)))
8420           (unless articles
8421             (error "Found no matches for \"%s\"" regexp))
8422           (gnus-summary-limit articles))
8423       (gnus-summary-position-point))))
8424
8425 (defun gnus-summary-limit-to-display-predicate ()
8426   "Limit the summary buffer to the predicated in the `display' group parameter."
8427   (interactive)
8428   (unless gnus-newsgroup-display
8429     (error "There is no `display' group parameter"))
8430   (let (articles)
8431     (dolist (gnus-number gnus-newsgroup-articles)
8432       (when (funcall gnus-newsgroup-display)
8433         (push gnus-number articles)))
8434     (gnus-summary-limit articles))
8435   (gnus-summary-position-point))
8436
8437 (defun gnus-summary-limit-to-unread (&optional all)
8438   "Limit the summary buffer to articles that are not marked as read.
8439 If ALL is non-nil, limit strictly to unread articles."
8440   (interactive "P")
8441   (if all
8442       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8443     (gnus-summary-limit-to-marks
8444      ;; Concat all the marks that say that an article is read and have
8445      ;; those removed.
8446      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8447            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8448            gnus-low-score-mark gnus-expirable-mark
8449            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8450            gnus-duplicate-mark)
8451      'reverse)))
8452
8453 (defun gnus-summary-limit-to-headers (match &optional reverse)
8454   "Limit the summary buffer to articles that have headers that match MATCH.
8455 If REVERSE (the prefix), limit to articles that don't match."
8456   (interactive "sMatch headers (regexp): \nP")
8457   (gnus-summary-limit-to-bodies match reverse t))
8458
8459 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8460   "Limit the summary buffer to articles that have bodies that match MATCH.
8461 If REVERSE (the prefix), limit to articles that don't match."
8462   (interactive "sMatch body (regexp): \nP")
8463   (let ((articles nil)
8464         (gnus-select-article-hook nil)  ;Disable hook.
8465         (gnus-article-prepare-hook nil)
8466         (gnus-use-article-prefetch nil)
8467         (gnus-keep-backlog nil)
8468         (gnus-break-pages nil)
8469         (gnus-summary-display-arrow nil)
8470         (gnus-updated-mode-lines nil)
8471         (gnus-auto-center-summary nil)
8472         (gnus-display-mime-function nil))
8473     (dolist (data gnus-newsgroup-data)
8474       (let (gnus-mark-article-hook)
8475         (gnus-summary-select-article t t nil (gnus-data-number data)))
8476       (with-current-buffer gnus-article-buffer
8477         (article-goto-body)
8478         (let* ((case-fold-search t)
8479                (found (if headersp
8480                           (re-search-backward match nil t)
8481                         (re-search-forward match nil t))))
8482           (when (or (and found
8483                          (not reverse))
8484                     (and (not found)
8485                          reverse))
8486             (push (gnus-data-number data) articles)))))
8487     (if (not articles)
8488         (message "No messages matched")
8489       (gnus-summary-limit articles)))
8490   (gnus-summary-position-point))
8491
8492 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8493   "Limit the summary buffer to articles that aren't part on any thread.
8494 If THREADSP (the prefix), limit to articles that are in threads."
8495   (interactive "P")
8496   (let ((articles nil)
8497         thread-articles
8498         threads)
8499     (dolist (thread gnus-newsgroup-threads)
8500       (if (stringp (car thread))
8501           (dolist (thread (cdr thread))
8502             (push thread threads))
8503         (push thread threads)))
8504     (dolist (thread threads)
8505       (setq thread-articles (gnus-articles-in-thread thread))
8506       (when (or (and threadsp
8507                      (> (length thread-articles) 1))
8508                 (and (not threadsp)
8509                      (= (length thread-articles) 1)))
8510         (setq articles (nconc thread-articles articles))))
8511     (if (not articles)
8512         (message "No messages matched")
8513       (gnus-summary-limit articles))
8514     (gnus-summary-position-point)))
8515
8516 (defun gnus-summary-limit-to-replied (&optional unreplied)
8517   "Limit the summary buffer to replied articles.
8518 If UNREPLIED (the prefix), limit to unreplied articles."
8519   (interactive "P")
8520   (if unreplied
8521       (gnus-summary-limit
8522        (gnus-set-difference gnus-newsgroup-articles
8523         gnus-newsgroup-replied))
8524     (gnus-summary-limit gnus-newsgroup-replied))
8525   (gnus-summary-position-point))
8526
8527 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8528   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8529 If REVERSE, limit the summary buffer to articles that are marked
8530 with MARKS.  MARKS can either be a string of marks or a list of marks.
8531 Returns how many articles were removed."
8532   (interactive "sMarks: ")
8533   (gnus-summary-limit-to-marks marks t))
8534
8535 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8536   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8537 If REVERSE (the prefix), limit the summary buffer to articles that are
8538 not marked with MARKS.  MARKS can either be a string of marks or a
8539 list of marks.
8540 Returns how many articles were removed."
8541   (interactive "sMarks: \nP")
8542   (prog1
8543       (let ((data gnus-newsgroup-data)
8544             (marks (if (listp marks) marks
8545                      (append marks nil))) ; Transform to list.
8546             articles)
8547         (while data
8548           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8549                   (memq (gnus-data-mark (car data)) marks))
8550             (push (gnus-data-number (car data)) articles))
8551           (setq data (cdr data)))
8552         (gnus-summary-limit articles))
8553     (gnus-summary-position-point)))
8554
8555 (defun gnus-summary-limit-to-score (score)
8556   "Limit to articles with score at or above SCORE."
8557   (interactive "NLimit to articles with score of at least: ")
8558   (let ((data gnus-newsgroup-data)
8559         articles)
8560     (while data
8561       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8562                 score)
8563         (push (gnus-data-number (car data)) articles))
8564       (setq data (cdr data)))
8565     (prog1
8566         (gnus-summary-limit articles)
8567       (gnus-summary-position-point))))
8568
8569 (defun gnus-summary-limit-to-unseen ()
8570   "Limit to unseen articles."
8571   (interactive)
8572   (prog1
8573       (gnus-summary-limit gnus-newsgroup-unseen)
8574     (gnus-summary-position-point)))
8575
8576 (defun gnus-summary-limit-include-thread (id)
8577   "Display all the hidden articles that is in the thread with ID in it.
8578 When called interactively, ID is the Message-ID of the current
8579 article."
8580   (interactive (list (mail-header-id (gnus-summary-article-header))))
8581   (let ((articles (gnus-articles-in-thread
8582                    (gnus-id-to-thread (gnus-root-id id))))
8583         ;;we REALLY want the whole thread---this prevents cut-threads
8584         ;;from removing the thread we want to include.
8585         (gnus-fetch-old-headers nil)
8586         (gnus-build-sparse-threads nil))
8587     (prog1
8588         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8589       (gnus-summary-limit-include-matching-articles
8590        "subject"
8591        (regexp-quote (gnus-simplify-subject-re
8592                       (mail-header-subject (gnus-id-to-header id)))))
8593       (gnus-summary-position-point))))
8594
8595 (defun gnus-summary-limit-include-matching-articles (header regexp)
8596   "Display all the hidden articles that have HEADERs that match REGEXP."
8597   (interactive (list (read-string "Match on header: ")
8598                      (read-string "Regexp: ")))
8599   (let ((articles (gnus-find-matching-articles header regexp)))
8600     (prog1
8601         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8602       (gnus-summary-position-point))))
8603
8604 (defun gnus-summary-insert-dormant-articles ()
8605   "Insert all the dormant articles for this group into the current buffer."
8606   (interactive)
8607   (let ((gnus-verbose (max 6 gnus-verbose)))
8608     (if (not gnus-newsgroup-dormant)
8609         (gnus-message 3 "No dormant articles for this group")
8610       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8611
8612 (defun gnus-summary-insert-ticked-articles ()
8613   "Insert ticked articles for this group into the current buffer."
8614   (interactive)
8615   (let ((gnus-verbose (max 6 gnus-verbose)))
8616     (if (not gnus-newsgroup-marked)
8617         (gnus-message 3 "No ticked articles for this group")
8618       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8619
8620 (defun gnus-summary-limit-include-dormant ()
8621   "Display all the hidden articles that are marked as dormant.
8622 Note that this command only works on a subset of the articles currently
8623 fetched for this group."
8624   (interactive)
8625   (unless gnus-newsgroup-dormant
8626     (error "There are no dormant articles in this group"))
8627   (prog1
8628       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8629     (gnus-summary-position-point)))
8630
8631 (defun gnus-summary-include-articles (articles)
8632   "Fetch the headers for ARTICLES and then display the summary lines."
8633   (let ((gnus-inhibit-demon t)
8634         (gnus-agent nil)
8635         (gnus-read-all-available-headers t))
8636     (setq gnus-newsgroup-headers
8637           (gnus-merge
8638            'list gnus-newsgroup-headers
8639            (gnus-fetch-headers articles nil t)
8640            'gnus-article-sort-by-number))
8641     (setq gnus-newsgroup-articles
8642           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8643     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8644
8645 (defun gnus-summary-limit-exclude-dormant ()
8646   "Hide all dormant articles."
8647   (interactive)
8648   (prog1
8649       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8650     (gnus-summary-position-point)))
8651
8652 (defun gnus-summary-limit-exclude-childless-dormant ()
8653   "Hide all dormant articles that have no children."
8654   (interactive)
8655   (let ((data (gnus-data-list t))
8656         articles d children)
8657     ;; Find all articles that are either not dormant or have
8658     ;; children.
8659     (while (setq d (pop data))
8660       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8661                 (and (setq children
8662                            (gnus-article-children (gnus-data-number d)))
8663                      (let (found)
8664                        (while children
8665                          (when (memq (car children) articles)
8666                            (setq children nil
8667                                  found t))
8668                          (pop children))
8669                        found)))
8670         (push (gnus-data-number d) articles)))
8671     ;; Do the limiting.
8672     (prog1
8673         (gnus-summary-limit articles)
8674       (gnus-summary-position-point))))
8675
8676 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8677   "Mark all unread excluded articles as read.
8678 If ALL, mark even excluded ticked and dormants as read."
8679   (interactive "P")
8680   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8681   (let ((articles (gnus-sorted-ndifference
8682                    (sort
8683                     (mapcar (lambda (h) (mail-header-number h))
8684                             gnus-newsgroup-headers)
8685                     '<)
8686                    gnus-newsgroup-limit))
8687         article)
8688     (setq gnus-newsgroup-unreads
8689           (gnus-sorted-intersection gnus-newsgroup-unreads
8690                                     gnus-newsgroup-limit))
8691     (if all
8692         (setq gnus-newsgroup-dormant nil
8693               gnus-newsgroup-marked nil
8694               gnus-newsgroup-reads
8695               (nconc
8696                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8697                gnus-newsgroup-reads))
8698       (while (setq article (pop articles))
8699         (unless (or (memq article gnus-newsgroup-dormant)
8700                     (memq article gnus-newsgroup-marked))
8701           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8702
8703 (defun gnus-summary-limit (articles &optional pop)
8704   (if pop
8705       ;; We pop the previous limit off the stack and use that.
8706       (setq articles (car gnus-newsgroup-limits)
8707             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8708     ;; We use the new limit, so we push the old limit on the stack.
8709     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8710   ;; Set the limit.
8711   (setq gnus-newsgroup-limit articles)
8712   (let ((total (length gnus-newsgroup-data))
8713         (data (gnus-data-find-list (gnus-summary-article-number)))
8714         (gnus-summary-mark-below nil)   ; Inhibit this.
8715         found)
8716     ;; This will do all the work of generating the new summary buffer
8717     ;; according to the new limit.
8718     (gnus-summary-prepare)
8719     ;; Hide any threads, possibly.
8720     (gnus-summary-maybe-hide-threads)
8721     ;; Try to return to the article you were at, or one in the
8722     ;; neighborhood.
8723     (when data
8724       ;; We try to find some article after the current one.
8725       (while data
8726         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8727           (setq data nil
8728                 found t))
8729         (setq data (cdr data))))
8730     (unless found
8731       ;; If there is no data, that means that we were after the last
8732       ;; article.  The same goes when we can't find any articles
8733       ;; after the current one.
8734       (goto-char (point-max))
8735       (gnus-summary-find-prev))
8736     (gnus-set-mode-line 'summary)
8737     ;; We return how many articles were removed from the summary
8738     ;; buffer as a result of the new limit.
8739     (- total (length gnus-newsgroup-data))))
8740
8741 (defsubst gnus-invisible-cut-children (threads)
8742   (let ((num 0))
8743     (while threads
8744       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8745         (incf num))
8746       (pop threads))
8747     (< num 2)))
8748
8749 (defsubst gnus-cut-thread (thread)
8750   "Go forwards in the thread until we find an article that we want to display."
8751   (when (or (eq gnus-fetch-old-headers 'some)
8752             (eq gnus-fetch-old-headers 'invisible)
8753             (numberp gnus-fetch-old-headers)
8754             (eq gnus-build-sparse-threads 'some)
8755             (eq gnus-build-sparse-threads 'more))
8756     ;; Deal with old-fetched headers and sparse threads.
8757     (while (and
8758             thread
8759             (or
8760              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8761              (gnus-summary-article-ancient-p
8762               (mail-header-number (car thread))))
8763             (if (or (<= (length (cdr thread)) 1)
8764                     (eq gnus-fetch-old-headers 'invisible))
8765                 (setq gnus-newsgroup-limit
8766                       (delq (mail-header-number (car thread))
8767                             gnus-newsgroup-limit)
8768                       thread (cadr thread))
8769               (when (gnus-invisible-cut-children (cdr thread))
8770                 (let ((th (cdr thread)))
8771                   (while th
8772                     (if (memq (mail-header-number (caar th))
8773                               gnus-newsgroup-limit)
8774                         (setq thread (car th)
8775                               th nil)
8776                       (setq th (cdr th))))))))))
8777   thread)
8778
8779 (defun gnus-cut-threads (threads)
8780   "Cut off all uninteresting articles from the beginning of THREADS."
8781   (when (or (eq gnus-fetch-old-headers 'some)
8782             (eq gnus-fetch-old-headers 'invisible)
8783             (numberp gnus-fetch-old-headers)
8784             (eq gnus-build-sparse-threads 'some)
8785             (eq gnus-build-sparse-threads 'more))
8786     (let ((th threads))
8787       (while th
8788         (setcar th (gnus-cut-thread (car th)))
8789         (setq th (cdr th)))))
8790   ;; Remove nixed out threads.
8791   (delq nil threads))
8792
8793 (defun gnus-summary-initial-limit (&optional show-if-empty)
8794   "Figure out what the initial limit is supposed to be on group entry.
8795 This entails weeding out unwanted dormants, low-scored articles,
8796 fetch-old-headers verbiage, and so on."
8797   ;; Most groups have nothing to remove.
8798   (unless (or gnus-inhibit-limiting
8799               (and (null gnus-newsgroup-dormant)
8800                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8801                    (not (eq gnus-fetch-old-headers 'some))
8802                    (not (numberp gnus-fetch-old-headers))
8803                    (not (eq gnus-fetch-old-headers 'invisible))
8804                    (null gnus-summary-expunge-below)
8805                    (not (eq gnus-build-sparse-threads 'some))
8806                    (not (eq gnus-build-sparse-threads 'more))
8807                    (null gnus-thread-expunge-below)))
8808     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8809     (setq gnus-newsgroup-limit nil)
8810     (mapatoms
8811      (lambda (node)
8812        (unless (car (symbol-value node))
8813          ;; These threads have no parents -- they are roots.
8814          (let ((nodes (cdr (symbol-value node)))
8815                thread)
8816            (while nodes
8817              (if (and gnus-thread-expunge-below
8818                       (< (gnus-thread-total-score (car nodes))
8819                          gnus-thread-expunge-below))
8820                  (gnus-expunge-thread (pop nodes))
8821                (setq thread (pop nodes))
8822                (gnus-summary-limit-children thread))))))
8823      gnus-newsgroup-dependencies)
8824     ;; If this limitation resulted in an empty group, we might
8825     ;; pop the previous limit and use it instead.
8826     (when (and (not gnus-newsgroup-limit)
8827                show-if-empty)
8828       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8829     gnus-newsgroup-limit))
8830
8831 (defun gnus-summary-limit-children (thread)
8832   "Return 1 if this subthread is visible and 0 if it is not."
8833   ;; First we get the number of visible children to this thread.  This
8834   ;; is done by recursing down the thread using this function, so this
8835   ;; will really go down to a leaf article first, before slowly
8836   ;; working its way up towards the root.
8837   (when thread
8838     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8839            (children
8840            (if (cdr thread)
8841                (apply '+ (mapcar 'gnus-summary-limit-children
8842                                  (cdr thread)))
8843              0))
8844            (number (mail-header-number (car thread)))
8845            score)
8846       (if (and
8847            (not (memq number gnus-newsgroup-marked))
8848            (or
8849             ;; If this article is dormant and has absolutely no visible
8850             ;; children, then this article isn't visible.
8851             (and (memq number gnus-newsgroup-dormant)
8852                  (zerop children))
8853             ;; If this is "fetch-old-headered" and there is no
8854             ;; visible children, then we don't want this article.
8855             (and (or (eq gnus-fetch-old-headers 'some)
8856                      (numberp gnus-fetch-old-headers))
8857                  (gnus-summary-article-ancient-p number)
8858                  (zerop children))
8859             ;; If this is "fetch-old-headered" and `invisible', then
8860             ;; we don't want this article.
8861             (and (eq gnus-fetch-old-headers 'invisible)
8862                  (gnus-summary-article-ancient-p number))
8863             ;; If this is a sparsely inserted article with no children,
8864             ;; we don't want it.
8865             (and (eq gnus-build-sparse-threads 'some)
8866                  (gnus-summary-article-sparse-p number)
8867                  (zerop children))
8868             ;; If we use expunging, and this article is really
8869             ;; low-scored, then we don't want this article.
8870             (when (and gnus-summary-expunge-below
8871                        (< (setq score
8872                                 (or (cdr (assq number gnus-newsgroup-scored))
8873                                     gnus-summary-default-score))
8874                           gnus-summary-expunge-below))
8875               ;; We increase the expunge-tally here, but that has
8876               ;; nothing to do with the limits, really.
8877               (incf gnus-newsgroup-expunged-tally)
8878               ;; We also mark as read here, if that's wanted.
8879               (when (and gnus-summary-mark-below
8880                          (< score gnus-summary-mark-below))
8881                 (setq gnus-newsgroup-unreads
8882                       (delq number gnus-newsgroup-unreads))
8883                 (if gnus-newsgroup-auto-expire
8884                     (push number gnus-newsgroup-expirable)
8885                   (push (cons number gnus-low-score-mark)
8886                         gnus-newsgroup-reads)))
8887               t)
8888             ;; Do the `display' group parameter.
8889             (and gnus-newsgroup-display
8890                  (let ((gnus-number number))
8891                    (not (funcall gnus-newsgroup-display))))))
8892           ;; Nope, invisible article.
8893           0
8894         ;; Ok, this article is to be visible, so we add it to the limit
8895         ;; and return 1.
8896         (push number gnus-newsgroup-limit)
8897         1))))
8898
8899 (defun gnus-expunge-thread (thread)
8900   "Mark all articles in THREAD as read."
8901   (let* ((number (mail-header-number (car thread))))
8902     (incf gnus-newsgroup-expunged-tally)
8903     ;; We also mark as read here, if that's wanted.
8904     (setq gnus-newsgroup-unreads
8905           (delq number gnus-newsgroup-unreads))
8906     (if gnus-newsgroup-auto-expire
8907         (push number gnus-newsgroup-expirable)
8908       (push (cons number gnus-low-score-mark)
8909             gnus-newsgroup-reads)))
8910   ;; Go recursively through all subthreads.
8911   (mapcar 'gnus-expunge-thread (cdr thread)))
8912
8913 ;; Summary article oriented commands
8914
8915 (defun gnus-summary-refer-parent-article (n)
8916   "Refer parent article N times.
8917 If N is negative, go to ancestor -N instead.
8918 The difference between N and the number of articles fetched is returned."
8919   (interactive "p")
8920   (let ((skip 1)
8921         error header ref)
8922     (when (not (natnump n))
8923       (setq skip (abs n)
8924             n 1))
8925     (while (and (> n 0)
8926                 (not error))
8927       (setq header (gnus-summary-article-header))
8928       (if (and (eq (mail-header-number header)
8929                    (cdr gnus-article-current))
8930                (equal gnus-newsgroup-name
8931                       (car gnus-article-current)))
8932           ;; If we try to find the parent of the currently
8933           ;; displayed article, then we take a look at the actual
8934           ;; References header, since this is slightly more
8935           ;; reliable than the References field we got from the
8936           ;; server.
8937           (with-current-buffer gnus-original-article-buffer
8938             (nnheader-narrow-to-headers)
8939             (unless (setq ref (message-fetch-field "references"))
8940               (when (setq ref (message-fetch-field "in-reply-to"))
8941                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8942             (widen))
8943         (setq ref
8944               ;; It's not the current article, so we take a bet on
8945               ;; the value we got from the server.
8946               (mail-header-references header)))
8947       (if (and ref
8948                (not (equal ref "")))
8949           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8950             (gnus-message 1 "Couldn't find parent"))
8951         (gnus-message 1 "No references in article %d"
8952                       (gnus-summary-article-number))
8953         (setq error t))
8954       (decf n))
8955     (gnus-summary-position-point)
8956     n))
8957
8958 (defun gnus-summary-refer-references ()
8959   "Fetch all articles mentioned in the References header.
8960 Return the number of articles fetched."
8961   (interactive)
8962   (let ((ref (mail-header-references (gnus-summary-article-header)))
8963         (current (gnus-summary-article-number))
8964         (n 0))
8965     (if (or (not ref)
8966             (equal ref ""))
8967         (error "No References in the current article")
8968       ;; For each Message-ID in the References header...
8969       (while (string-match "<[^>]*>" ref)
8970         (incf n)
8971         ;; ... fetch that article.
8972         (gnus-summary-refer-article
8973          (prog1 (match-string 0 ref)
8974            (setq ref (substring ref (match-end 0))))))
8975       (gnus-summary-goto-subject current)
8976       (gnus-summary-position-point)
8977       n)))
8978
8979 (defun gnus-delete-duplicate-headers (headers)
8980   ;; First remove leading duplicates.
8981   (while (and (> (length headers) 1)
8982               (= (mail-header-number (car headers))
8983                  (mail-header-number (cadr headers))))
8984     (pop headers))
8985   ;; Then the rest.
8986   (let ((result headers))
8987     (while (> (length headers) 1)
8988       (if (= (mail-header-number (car headers))
8989              (mail-header-number (cadr headers)))
8990           (setcdr headers (cddr headers))
8991         (pop headers)))
8992     result))
8993
8994 (defun gnus-summary-refer-thread (&optional limit)
8995   "Fetch all articles in the current thread. For backends that
8996 know how to search for threads (currently only 'nnimap) a
8997 non-numeric prefix arg will use nnir to search the entire
8998 server; without a prefix arg only the current group is
8999 searched. If the variable `gnus-refer-thread-use-nnir' is
9000 non-nil the prefix arg has the reverse meaning. If no
9001 backend-specific 'request-thread function is available fetch
9002 LIMIT (the numerical prefix) old headers. If LIMIT is
9003 non-numeric or nil fetch the number specified by the
9004 `gnus-refer-thread-limit' variable."
9005   (interactive "P")
9006   (gnus-warp-to-article)
9007   (let* ((header (gnus-summary-article-header))
9008          (id (mail-header-id header))
9009          (gnus-inhibit-demon t)
9010          (gnus-summary-ignore-duplicates t)
9011          (gnus-read-all-available-headers t)
9012          (gnus-refer-thread-use-nnir
9013           (if (and (not (null limit)) (listp limit))
9014               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9015          (new-headers
9016           (if (gnus-check-backend-function
9017                'request-thread gnus-newsgroup-name)
9018               (gnus-request-thread header gnus-newsgroup-name)
9019             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9020                             gnus-refer-thread-limit))
9021                    (last (if (numberp limit)
9022                              (min (+ (mail-header-number header)
9023                                      limit)
9024                                   gnus-newsgroup-highest)
9025                            gnus-newsgroup-highest))
9026                    (subject (gnus-simplify-subject
9027                              (mail-header-subject header)))
9028                    (refs (split-string (or (mail-header-references header)
9029                                            "")))
9030                    (gnus-parse-headers-hook
9031                     `(lambda () (goto-char (point-min))
9032                       (keep-lines
9033                        (regexp-opt ',(append refs (list id subject)))))))
9034               (gnus-fetch-headers (list last) (if (numberp limit)
9035                                                   (* 2 limit) limit) t))))
9036          article-ids)
9037     (when (listp new-headers)
9038       (dolist (header new-headers)
9039         (push (mail-header-number header) article-ids)
9040         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9041           (push (mail-header-number header) gnus-newsgroup-unreads)
9042           (setq gnus-newsgroup-unselected
9043                 (delete (mail-header-number header)
9044                         gnus-newsgroup-unselected))))
9045       (setq gnus-newsgroup-headers
9046             (gnus-delete-duplicate-headers
9047              (gnus-merge
9048               'list gnus-newsgroup-headers new-headers
9049               'gnus-article-sort-by-number)))
9050       (setq gnus-newsgroup-articles
9051             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9052       (gnus-summary-limit-include-thread id)))
9053   (gnus-summary-show-thread))
9054
9055 (defun gnus-summary-refer-article (message-id)
9056   "Fetch an article specified by MESSAGE-ID."
9057   (interactive "sMessage-ID: ")
9058   (gnus-warp-to-article)
9059   (when (and (stringp message-id)
9060              (not (zerop (length message-id))))
9061     (setq message-id (gnus-replace-in-string message-id " " ""))
9062     ;; Construct the correct Message-ID if necessary.
9063     ;; Suggested by tale@pawl.rpi.edu.
9064     (unless (string-match "^<" message-id)
9065       (setq message-id (concat "<" message-id)))
9066     (unless (string-match ">$" message-id)
9067       (setq message-id (concat message-id ">")))
9068     ;; People often post MIDs from URLs, so unhex it:
9069     (unless (string-match "@" message-id)
9070       (setq message-id (gnus-url-unhex-string message-id)))
9071     (let* ((header (gnus-id-to-header message-id))
9072            (sparse (and header
9073                         (gnus-summary-article-sparse-p
9074                          (mail-header-number header))
9075                         (memq (mail-header-number header)
9076                               gnus-newsgroup-limit)))
9077            number)
9078       (cond
9079        ;; If the article is present in the buffer we just go to it.
9080        ((and header
9081              (or (not (gnus-summary-article-sparse-p
9082                        (mail-header-number header)))
9083                  sparse))
9084         (prog1
9085             (gnus-summary-goto-article
9086              (mail-header-number header) nil t)
9087           (when sparse
9088             (gnus-summary-update-article (mail-header-number header)))))
9089        (t
9090         ;; We fetch the article.
9091         (catch 'found
9092           (dolist (gnus-override-method (gnus-refer-article-methods))
9093             (when (and (gnus-check-server gnus-override-method)
9094                        ;; Fetch the header,
9095                        (setq number (gnus-summary-insert-subject message-id)))
9096               ;; and display the article.
9097               (gnus-summary-select-article nil nil nil number)
9098               (throw 'found t)))
9099           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9100
9101 (defun gnus-refer-article-methods ()
9102   "Return a list of referable methods."
9103   (cond
9104    ;; No method, so we default to current and native.
9105    ((null gnus-refer-article-method)
9106     (list gnus-current-select-method gnus-select-method))
9107    ;; Current.
9108    ((eq 'current gnus-refer-article-method)
9109     (list gnus-current-select-method))
9110    ;; List of select methods.
9111    ((not (and (symbolp (car gnus-refer-article-method))
9112               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9113     (let (out)
9114       (dolist (method gnus-refer-article-method)
9115         (push (if (eq 'current method)
9116                   gnus-current-select-method
9117                 (if (eq 'nnir (car method))
9118                     (list
9119                      'nnir
9120                      (or (cadr method)
9121                          (gnus-method-to-server gnus-current-select-method)))
9122                   method))
9123               out))
9124       (nreverse out)))
9125    ;; One single select method.
9126    (t
9127     (list gnus-refer-article-method))))
9128
9129 (defun gnus-summary-edit-parameters ()
9130   "Edit the group parameters of the current group."
9131   (interactive)
9132   (gnus-group-edit-group gnus-newsgroup-name 'params))
9133
9134 (defun gnus-summary-customize-parameters ()
9135   "Customize the group parameters of the current group."
9136   (interactive)
9137   (gnus-group-customize gnus-newsgroup-name))
9138
9139 (defun gnus-summary-enter-digest-group (&optional force)
9140   "Enter an nndoc group based on the current article.
9141 If FORCE, force a digest interpretation.  If not, try to guess
9142 what the document format is.
9143
9144 To control what happens when you exit the group, see the
9145 `gnus-auto-select-on-ephemeral-exit' variable."
9146   (interactive "P")
9147   (let ((conf gnus-current-window-configuration))
9148     (save-window-excursion
9149       (save-excursion
9150         (let (gnus-article-prepare-hook
9151               gnus-display-mime-function
9152               gnus-break-pages)
9153           (gnus-summary-select-article))))
9154     (setq gnus-current-window-configuration conf)
9155     (let* ((name (format "%s-%d"
9156                          (gnus-group-prefixed-name
9157                           gnus-newsgroup-name (list 'nndoc ""))
9158                          (with-current-buffer gnus-summary-buffer
9159                            gnus-current-article)))
9160            (ogroup gnus-newsgroup-name)
9161            (params (append (gnus-info-params (gnus-get-info ogroup))
9162                            (list (cons 'to-group ogroup))
9163                            (list (cons 'parent-group ogroup))
9164                            (list (cons 'save-article-group ogroup))))
9165            (case-fold-search t)
9166            (buf (current-buffer))
9167            dig to-address)
9168       (with-current-buffer gnus-original-article-buffer
9169         ;; Have the digest group inherit the main mail address of
9170         ;; the parent article.
9171         (when (setq to-address (or (gnus-fetch-field "reply-to")
9172                                    (gnus-fetch-field "from")))
9173           (setq params
9174                 (append
9175                  (list (cons 'to-address
9176                              (funcall gnus-decode-encoded-address-function
9177                                       to-address))))))
9178         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9179         (insert-buffer-substring gnus-original-article-buffer)
9180         ;; Remove lines that may lead nndoc to misinterpret the
9181         ;; document type.
9182         (narrow-to-region
9183          (goto-char (point-min))
9184          (or (search-forward "\n\n" nil t) (point)))
9185         (goto-char (point-min))
9186         (delete-matching-lines "^Path:\\|^From ")
9187         (widen))
9188       (unwind-protect
9189           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9190                     (gnus-newsgroup-ephemeral-ignored-charsets
9191                      gnus-newsgroup-ignored-charsets))
9192                 (gnus-group-read-ephemeral-group
9193                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9194                               (nndoc-article-type
9195                                ,(if force 'mbox 'guess)))
9196                  t nil nil nil
9197                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9198                                                         "ADAPT")))))
9199               ;; Make all postings to this group go to the parent group.
9200               (nconc (gnus-info-params (gnus-get-info name))
9201                      params)
9202             ;; Couldn't select this doc group.
9203             (switch-to-buffer buf)
9204             (gnus-set-global-variables)
9205             (gnus-configure-windows 'summary)
9206             (gnus-message 3 "Article couldn't be entered?"))
9207         (kill-buffer dig)))))
9208
9209 (defun gnus-summary-read-document (n)
9210   "Open a new group based on the current article(s).
9211 This will allow you to read digests and other similar
9212 documents as newsgroups.
9213 Obeys the standard process/prefix convention."
9214   (interactive "P")
9215   (let* ((ogroup gnus-newsgroup-name)
9216          (params (append (gnus-info-params (gnus-get-info ogroup))
9217                          (list (cons 'to-group ogroup))))
9218          group egroup groups vgroup)
9219     (dolist (article (gnus-summary-work-articles n))
9220       (setq group (format "%s-%d" gnus-newsgroup-name article))
9221       (gnus-summary-remove-process-mark article)
9222       (when (gnus-summary-display-article article)
9223         (save-excursion ;;What for?
9224           (with-temp-buffer
9225             (insert-buffer-substring gnus-original-article-buffer)
9226             ;; Remove some headers that may lead nndoc to make
9227             ;; the wrong guess.
9228             (message-narrow-to-head)
9229             (goto-char (point-min))
9230             (delete-matching-lines "^Path:\\|^From ")
9231             (widen)
9232             (if (setq egroup
9233                       (gnus-group-read-ephemeral-group
9234                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9235                                      (nndoc-article-type guess))
9236                        t nil t))
9237                 (progn
9238                   ;; Make all postings to this group go to the parent group.
9239                   (nconc (gnus-info-params (gnus-get-info egroup))
9240                          params)
9241                   (push egroup groups))
9242               ;; Couldn't select this doc group.
9243               (gnus-error 3 "Article couldn't be entered"))))))
9244     ;; Now we have selected all the documents.
9245     (cond
9246      ((not groups)
9247       (error "None of the articles could be interpreted as documents"))
9248      ((gnus-group-read-ephemeral-group
9249        (setq vgroup (format
9250                      "nnvirtual:%s-%s" gnus-newsgroup-name
9251                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9252        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9253        t
9254        (cons (current-buffer) 'summary)))
9255      (t
9256       (error "Couldn't select virtual nndoc group")))))
9257
9258 (defun gnus-summary-widget-forward (arg)
9259   "Move point to the next field or button in the article.
9260 With optional ARG, move across that many fields."
9261   (interactive "p")
9262   (gnus-summary-select-article)
9263   (gnus-configure-windows 'article)
9264   (select-window (gnus-get-buffer-window gnus-article-buffer))
9265   (widget-forward arg))
9266
9267 (defun gnus-summary-widget-backward (arg)
9268   "Move point to the previous field or button in the article.
9269 With optional ARG, move across that many fields."
9270   (interactive "p")
9271   (gnus-summary-select-article)
9272   (gnus-configure-windows 'article)
9273   (select-window (gnus-get-buffer-window gnus-article-buffer))
9274   (unless (widget-at (point))
9275     (goto-char (point-max)))
9276   (widget-backward arg))
9277
9278 (defun gnus-summary-isearch-article (&optional regexp-p)
9279   "Do incremental search forward on the current article.
9280 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9281   (interactive "P")
9282   (gnus-summary-select-article)
9283   (gnus-configure-windows 'article)
9284   (gnus-eval-in-buffer-window gnus-article-buffer
9285     (save-restriction
9286       (widen)
9287       (isearch-forward regexp-p))))
9288
9289 (defun gnus-summary-repeat-search-article-forward ()
9290   "Repeat the previous search forwards."
9291   (interactive)
9292   (unless gnus-last-search-regexp
9293     (error "No previous search"))
9294   (gnus-summary-search-article-forward gnus-last-search-regexp))
9295
9296 (defun gnus-summary-repeat-search-article-backward ()
9297   "Repeat the previous search backwards."
9298   (interactive)
9299   (unless gnus-last-search-regexp
9300     (error "No previous search"))
9301   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9302
9303 (defun gnus-summary-search-article-forward (regexp &optional backward)
9304   "Search for an article containing REGEXP forward.
9305 If BACKWARD, search backward instead."
9306   (interactive
9307    (list (read-string
9308           (format "Search article %s (regexp%s): "
9309                   (if current-prefix-arg "backward" "forward")
9310                   (if gnus-last-search-regexp
9311                       (concat ", default " gnus-last-search-regexp)
9312                     "")))
9313          current-prefix-arg))
9314   (if (string-equal regexp "")
9315       (setq regexp (or gnus-last-search-regexp ""))
9316     (setq gnus-last-search-regexp regexp)
9317     (setq gnus-article-before-search gnus-current-article))
9318   ;; Intentionally set gnus-last-article.
9319   (setq gnus-last-article gnus-article-before-search)
9320   (let ((gnus-last-article gnus-last-article))
9321     (if (gnus-summary-search-article regexp backward)
9322         (gnus-summary-show-thread)
9323       (signal 'search-failed (list regexp)))))
9324
9325 (defun gnus-summary-search-article-backward (regexp)
9326   "Search for an article containing REGEXP backward."
9327   (interactive
9328    (list (read-string
9329           (format "Search article backward (regexp%s): "
9330                   (if gnus-last-search-regexp
9331                       (concat ", default " gnus-last-search-regexp)
9332                     "")))))
9333   (gnus-summary-search-article-forward regexp 'backward))
9334
9335 (defun gnus-summary-search-article (regexp &optional backward)
9336   "Search for an article containing REGEXP.
9337 Optional argument BACKWARD means do search for backward.
9338 `gnus-select-article-hook' is not called during the search."
9339   ;; We have to require this here to make sure that the following
9340   ;; dynamic binding isn't shadowed by autoloading.
9341   (require 'gnus-async)
9342   (require 'gnus-art)
9343   (let ((gnus-select-article-hook nil)  ;Disable hook.
9344         (gnus-article-prepare-hook nil)
9345         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9346         (gnus-use-article-prefetch nil)
9347         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9348         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9349         (gnus-visual nil)
9350         (gnus-keep-backlog nil)
9351         (gnus-break-pages nil)
9352         (gnus-summary-display-arrow nil)
9353         (gnus-updated-mode-lines nil)
9354         (gnus-auto-center-summary nil)
9355         (sum (current-buffer))
9356         (gnus-display-mime-function nil)
9357         (found nil)
9358         point)
9359     (gnus-save-hidden-threads
9360       (gnus-summary-select-article)
9361       (set-buffer gnus-article-buffer)
9362       (goto-char (window-point (get-buffer-window (current-buffer))))
9363       (when backward
9364         (forward-line -1))
9365       (while (not found)
9366         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9367         (if (if backward
9368                 (re-search-backward regexp nil t)
9369               (re-search-forward regexp nil t))
9370             ;; We found the regexp.
9371             (progn
9372               (setq found 'found)
9373               (beginning-of-line)
9374               (set-window-start
9375                (get-buffer-window (current-buffer))
9376                (point))
9377               (forward-line 1)
9378               (set-window-point
9379                (get-buffer-window (current-buffer))
9380                (point))
9381               (set-buffer sum)
9382               (setq point (point)))
9383           ;; We didn't find it, so we go to the next article.
9384           (set-buffer sum)
9385           (setq found 'not)
9386           (while (eq found 'not)
9387             (if (not (if backward (gnus-summary-find-prev)
9388                        (gnus-summary-find-next)))
9389                 ;; No more articles.
9390                 (setq found t)
9391               ;; Select the next article and adjust point.
9392               (unless (gnus-summary-article-sparse-p
9393                        (gnus-summary-article-number))
9394                 (setq found nil)
9395                 (gnus-summary-select-article)
9396                 (set-buffer gnus-article-buffer)
9397                 (widen)
9398                 (goto-char (if backward (point-max) (point-min))))))))
9399       (gnus-message 7 ""))
9400     ;; Return whether we found the regexp.
9401     (when (eq found 'found)
9402       (goto-char point)
9403       (gnus-summary-show-thread)
9404       (gnus-summary-goto-subject gnus-current-article)
9405       (gnus-summary-position-point)
9406       t)))
9407
9408 (defun gnus-find-matching-articles (header regexp)
9409   "Return a list of all articles that match REGEXP on HEADER.
9410 This search includes all articles in the current group that Gnus has
9411 fetched headers for, whether they are displayed or not."
9412   (let ((articles nil)
9413         ;; Can't eta-reduce because it's a macro.
9414         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9415         (case-fold-search t))
9416     (dolist (header gnus-newsgroup-headers)
9417       (when (string-match regexp (funcall func header))
9418         (push (mail-header-number header) articles)))
9419     (nreverse articles)))
9420
9421 (defun gnus-summary-find-matching (header regexp &optional backward unread
9422                                           not-case-fold not-matching)
9423   "Return a list of all articles that match REGEXP on HEADER.
9424 The search stars on the current article and goes forwards unless
9425 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9426 If UNREAD is non-nil, only unread articles will
9427 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9428 in the comparisons. If NOT-MATCHING, return a list of all articles that
9429 not match REGEXP on HEADER."
9430   (let ((case-fold-search (not not-case-fold))
9431         articles d func)
9432     (if (consp header)
9433         (if (eq (car header) 'extra)
9434             (setq func
9435                   `(lambda (h)
9436                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9437                          "")))
9438           (error "%s is an invalid header" header))
9439       (unless (fboundp (intern (concat "mail-header-" header)))
9440         (error "%s is not a valid header" header))
9441       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9442     (dolist (d (if (eq backward 'all)
9443                    gnus-newsgroup-data
9444                  (gnus-data-find-list
9445                   (gnus-summary-article-number)
9446                   (gnus-data-list backward))))
9447       (when (and (or (not unread)       ; We want all articles...
9448                      (gnus-data-unread-p d)) ; Or just unreads.
9449                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9450                  (if not-matching
9451                      (not (string-match
9452                            regexp
9453                            (funcall func (gnus-data-header d))))
9454                    (string-match regexp
9455                                  (funcall func (gnus-data-header d)))))
9456         (push (gnus-data-number d) articles))) ; Success!
9457     (nreverse articles)))
9458
9459 (defun gnus-summary-execute-command (header regexp command &optional backward)
9460   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9461 If HEADER is an empty string (or nil), the match is done on the entire
9462 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9463   (interactive
9464    (list (let ((completion-ignore-case t))
9465            (gnus-completing-read
9466             "Header name"
9467             (mapcar 'symbol-name
9468                     (append
9469                      '(Number Subject From Lines Date
9470                        Message-ID Xref References Body)
9471                      gnus-extra-headers))
9472             'require-match))
9473          (read-string "Regexp: ")
9474          (read-key-sequence "Command: ")
9475          current-prefix-arg))
9476   (when (equal header "Body")
9477     (setq header ""))
9478   ;; Hidden thread subtrees must be searched as well.
9479   (gnus-summary-show-all-threads)
9480   ;; We don't want to change current point nor window configuration.
9481   (save-excursion
9482     (save-window-excursion
9483       (let (gnus-visual
9484             gnus-treat-strip-trailing-blank-lines
9485             gnus-treat-strip-leading-blank-lines
9486             gnus-treat-strip-multiple-blank-lines
9487             gnus-treat-hide-boring-headers
9488             gnus-treat-fold-newsgroups
9489             gnus-article-prepare-hook)
9490         (gnus-message 6 "Executing %s..." (key-description command))
9491         ;; We'd like to execute COMMAND interactively so as to give arguments.
9492         (gnus-execute header regexp
9493                       `(call-interactively ',(key-binding command))
9494                       backward)
9495         (gnus-message 6 "Executing %s...done" (key-description command))))))
9496
9497 (defun gnus-summary-beginning-of-article ()
9498   "Scroll the article back to the beginning."
9499   (interactive)
9500   (gnus-summary-select-article)
9501   (gnus-configure-windows 'article)
9502   (gnus-eval-in-buffer-window gnus-article-buffer
9503     (widen)
9504     (goto-char (point-min))
9505     (when gnus-break-pages
9506       (gnus-narrow-to-page))))
9507
9508 (defun gnus-summary-end-of-article ()
9509   "Scroll to the end of the article."
9510   (interactive)
9511   (gnus-summary-select-article)
9512   (gnus-configure-windows 'article)
9513   (gnus-eval-in-buffer-window gnus-article-buffer
9514     (widen)
9515     (goto-char (point-max))
9516     (recenter -3)
9517     (when gnus-break-pages
9518       (gnus-narrow-to-page))))
9519
9520 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9521   "Truncate to LEN and quote all \"(\"'s in STRING."
9522   (gnus-replace-in-string (if (and len (> (length string) len))
9523                               (substring string 0 len)
9524                             string)
9525                           "[()]" "\\\\\\&"))
9526
9527 (defun gnus-summary-print-article (&optional filename n)
9528   "Generate and print a PostScript image of the process-marked (mail) articles.
9529
9530 If used interactively, print the current article if none are
9531 process-marked.  With prefix arg, prompt the user for the name of the
9532 file to save in.
9533
9534 When used from Lisp, accept two optional args FILENAME and N.  N means
9535 to print the next N articles.  If N is negative, print the N previous
9536 articles.  If N is nil and articles have been marked with the process
9537 mark, print these instead.
9538
9539 If the optional first argument FILENAME is nil, send the image to the
9540 printer.  If FILENAME is a string, save the PostScript image in a file with
9541 that name.  If FILENAME is a number, prompt the user for the name of the file
9542 to save in."
9543   (interactive (list (ps-print-preprint current-prefix-arg)))
9544   (dolist (article (gnus-summary-work-articles n))
9545     (gnus-summary-select-article nil nil 'pseudo article)
9546     (gnus-eval-in-buffer-window gnus-article-buffer
9547       (gnus-print-buffer))
9548     (gnus-summary-remove-process-mark article))
9549   (ps-despool filename))
9550
9551 (defun gnus-print-buffer ()
9552   (let ((ps-left-header
9553          (list
9554           (concat "("
9555                   (gnus-summary-print-truncate-and-quote
9556                    (mail-header-subject gnus-current-headers)
9557                    66) ")")
9558           (concat "("
9559                   (gnus-summary-print-truncate-and-quote
9560                    (mail-header-from gnus-current-headers)
9561                    45) ")")))
9562         (ps-right-header
9563          (list
9564           "/pagenumberstring load"
9565           (concat "("
9566                   (mail-header-date gnus-current-headers) ")"))))
9567     (gnus-run-hooks 'gnus-ps-print-hook)
9568     (save-excursion
9569       (if ps-print-color-p
9570           (ps-spool-buffer-with-faces)
9571         (ps-spool-buffer)))))
9572
9573 (defun gnus-summary-show-complete-article ()
9574   "Show a complete version of the current article.
9575 This is only useful if you're looking at a partial version of the
9576 article currently."
9577   (interactive)
9578   (let ((gnus-keep-backlog nil)
9579         (gnus-use-cache nil)
9580         (gnus-agent nil)
9581         (variable (intern
9582                    (format "%s-fetch-partial-articles"
9583                            (car (gnus-find-method-for-group
9584                                  gnus-newsgroup-name)))
9585                    obarray))
9586         old-val)
9587     (unwind-protect
9588         (progn
9589           (setq old-val (symbol-value variable))
9590           (set variable nil)
9591           (gnus-flush-original-article-buffer)
9592           (gnus-summary-show-article))
9593       (set variable old-val))))
9594
9595 (defun gnus-summary-show-article (&optional arg)
9596   "Force redisplaying of the current article.
9597 If ARG (the prefix) is a number, show the article with the charset
9598 defined in `gnus-summary-show-article-charset-alist', or the charset
9599 input.
9600 If ARG (the prefix) is non-nil and not a number, show the article,
9601 but without running any of the article treatment functions
9602 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9603 C-u g', show the raw article."
9604   (interactive "P")
9605   (cond
9606    ((numberp arg)
9607     (gnus-summary-show-article t)
9608     (let ((gnus-newsgroup-charset
9609            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9610                (mm-read-coding-system
9611                 "View as charset: " ;; actually it is coding system.
9612                 (with-current-buffer gnus-article-buffer
9613                   (mm-detect-coding-region (point) (point-max))))))
9614           (gnus-newsgroup-ignored-charsets 'gnus-all))
9615       (gnus-summary-select-article nil 'force)
9616       (let ((deps gnus-newsgroup-dependencies)
9617             head header lines)
9618         (with-current-buffer gnus-original-article-buffer
9619           (save-restriction
9620             (message-narrow-to-head)
9621             (setq head (buffer-string))
9622             (goto-char (point-min))
9623             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9624               (goto-char (point-max))
9625               (widen)
9626               (setq lines (1- (count-lines (point) (point-max))))))
9627           (with-temp-buffer
9628             (insert (format "211 %d Article retrieved.\n"
9629                             (cdr gnus-article-current)))
9630             (insert head)
9631             (if lines (insert (format "Lines: %d\n" lines)))
9632             (insert ".\n")
9633             (let ((nntp-server-buffer (current-buffer)))
9634               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9635         (gnus-data-set-header
9636          (gnus-data-find (cdr gnus-article-current))
9637          header)
9638         (gnus-summary-update-article-line
9639          (cdr gnus-article-current) header)
9640         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9641           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9642    ((not arg)
9643     ;; Select the article the normal way.
9644     (if (eq mm-text-html-renderer 'shr)
9645         (progn
9646           (require 'shr)
9647           (let ((shr-ignore-cache t))
9648             (gnus-summary-select-article nil 'force)))
9649       (gnus-summary-select-article nil 'force)))
9650    ((equal arg '(16))
9651     ;; C-u C-u g
9652     (let ((gnus-inhibit-article-treatments t))
9653       (gnus-summary-select-article nil 'force)))
9654    (t
9655     ;; We have to require this here to make sure that the following
9656     ;; dynamic binding isn't shadowed by autoloading.
9657     (require 'gnus-async)
9658     (require 'gnus-art)
9659     ;; Bind the article treatment functions to nil.
9660     (let ((gnus-have-all-headers t)
9661           gnus-article-prepare-hook
9662           gnus-article-decode-hook
9663           gnus-display-mime-function
9664           gnus-break-pages)
9665       ;; Destroy any MIME parts.
9666       (when (gnus-buffer-live-p gnus-article-buffer)
9667         (with-current-buffer gnus-article-buffer
9668           (gnus-article-stop-animations)
9669           (mm-destroy-parts gnus-article-mime-handles)
9670           ;; Set it to nil for safety reason.
9671           (setq gnus-article-mime-handle-alist nil)
9672           (setq gnus-article-mime-handles nil)))
9673       (gnus-summary-select-article nil 'force))))
9674   (gnus-summary-goto-subject gnus-current-article)
9675   (gnus-summary-position-point))
9676
9677 (defun gnus-summary-show-raw-article ()
9678   "Show the raw article without any article massaging functions being run."
9679   (interactive)
9680   (gnus-summary-show-article t))
9681
9682 (defun gnus-summary-verbose-headers (&optional arg)
9683   "Toggle permanent full header display.
9684 If ARG is a positive number, turn header display on.
9685 If ARG is a negative number, turn header display off."
9686   (interactive "P")
9687   (setq gnus-show-all-headers
9688         (cond ((or (not (numberp arg))
9689                    (zerop arg))
9690                (not gnus-show-all-headers))
9691               ((natnump arg)
9692                t)))
9693   (gnus-summary-show-article))
9694
9695 (defun gnus-summary-toggle-header (&optional arg)
9696   "Show the headers if they are hidden, or hide them if they are shown.
9697 If ARG is a positive number, show the entire header.
9698 If ARG is a negative number, hide the unwanted header lines."
9699   (interactive "P")
9700   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9701                      (get-buffer-window gnus-article-buffer t))))
9702     (with-current-buffer gnus-article-buffer
9703       (widen)
9704       (article-narrow-to-head)
9705       (let* ((inhibit-read-only t)
9706              (inhibit-point-motion-hooks t)
9707              (hidden (if (numberp arg)
9708                          (>= arg 0)
9709                        (or (not (looking-at "[^ \t\n]+:"))
9710                            (gnus-article-hidden-text-p 'headers))))
9711              s e)
9712         (delete-region (point-min) (point-max))
9713         (with-current-buffer gnus-original-article-buffer
9714           (goto-char (setq s (point-min)))
9715           (setq e (if (search-forward "\n\n" nil t)
9716                       (1- (point))
9717                     (point-max))))
9718         (insert-buffer-substring gnus-original-article-buffer s e)
9719         (run-hooks 'gnus-article-decode-hook)
9720         (if hidden
9721             (let ((gnus-treat-hide-headers nil)
9722                   (gnus-treat-hide-boring-headers nil))
9723               (gnus-delete-wash-type 'headers)
9724               (gnus-treat-article 'head))
9725           (gnus-treat-article 'head))
9726         (widen)
9727         (if window
9728             (set-window-start window (goto-char (point-min))))
9729         (if gnus-break-pages
9730             (gnus-narrow-to-page)
9731           (when (gnus-visual-p 'page-marker)
9732             (let ((inhibit-read-only t))
9733               (gnus-remove-text-with-property 'gnus-prev)
9734               (gnus-remove-text-with-property 'gnus-next))))
9735         (gnus-set-mode-line 'article)))))
9736
9737 (defun gnus-summary-show-all-headers ()
9738   "Make all header lines visible."
9739   (interactive)
9740   (gnus-summary-toggle-header 1))
9741
9742 (defun gnus-summary-caesar-message (&optional arg)
9743   "Caesar rotate the current article by 13.
9744 With a non-numerical prefix, also rotate headers.  A numerical
9745 prefix specifies how many places to rotate each letter forward."
9746   (interactive "P")
9747   (gnus-summary-select-article)
9748   (let ((mail-header-separator ""))
9749     (gnus-eval-in-buffer-window gnus-article-buffer
9750       (save-restriction
9751         (widen)
9752         (let ((start (window-start))
9753               (inhibit-read-only t))
9754           (if (equal arg '(4))
9755               (message-caesar-buffer-body nil t)
9756             (message-caesar-buffer-body arg))
9757           (set-window-start (get-buffer-window (current-buffer)) start)))))
9758   ;; Create buttons and stuff...
9759   (gnus-treat-article nil))
9760
9761 (declare-function idna-to-unicode "ext:idna" (str))
9762
9763 (defun gnus-summary-idna-message (&optional arg)
9764   "Decode IDNA encoded domain names in the current articles.
9765 IDNA encoded domain names looks like `xn--bar'.  If a string
9766 remain unencoded after running this function, it is likely an
9767 invalid IDNA string (`xn--bar' is invalid).
9768
9769 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9770 installed for this command to work."
9771   (interactive "P")
9772   (if (not (and (condition-case nil (require 'idna)
9773                   (file-error))
9774                 (mm-coding-system-p 'utf-8)
9775                 (executable-find (symbol-value 'idna-program))))
9776       (gnus-message
9777        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9778     (gnus-summary-select-article)
9779     (let ((mail-header-separator ""))
9780       (gnus-eval-in-buffer-window gnus-article-buffer
9781         (save-restriction
9782           (widen)
9783           (let ((start (window-start))
9784                 buffer-read-only)
9785             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9786               (replace-match (idna-to-unicode (match-string 1))))
9787             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9788
9789 (defun gnus-summary-morse-message (&optional arg)
9790   "Morse decode the current article."
9791   (interactive "P")
9792   (gnus-summary-select-article)
9793   (let ((mail-header-separator ""))
9794     (gnus-eval-in-buffer-window gnus-article-buffer
9795       (save-excursion
9796         (save-restriction
9797           (widen)
9798           (let ((pos (window-start))
9799                 (inhibit-read-only t))
9800             (goto-char (point-min))
9801             (when (message-goto-body)
9802               (gnus-narrow-to-body))
9803             (goto-char (point-min))
9804             (while (search-forward "·" (point-max) t)
9805               (replace-match "."))
9806             (unmorse-region (point-min) (point-max))
9807             (widen)
9808             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9809
9810 (defun gnus-summary-stop-page-breaking ()
9811   "Stop page breaking in the current article."
9812   (interactive)
9813   (gnus-summary-select-article)
9814   (gnus-eval-in-buffer-window gnus-article-buffer
9815     (widen)
9816     (when (gnus-visual-p 'page-marker)
9817       (let ((inhibit-read-only t))
9818         (gnus-remove-text-with-property 'gnus-prev)
9819         (gnus-remove-text-with-property 'gnus-next))
9820       (setq gnus-page-broken nil))))
9821
9822 (defun gnus-summary-move-article (&optional n to-newsgroup
9823                                             select-method action)
9824   "Move the current article to a different newsgroup.
9825 If N is a positive number, move the N next articles.
9826 If N is a negative number, move the N previous articles.
9827 If N is nil and any articles have been marked with the process mark,
9828 move those articles instead.
9829 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9830 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9831 re-spool using this method.
9832
9833 When called interactively with TO-NEWSGROUP being nil, the value of
9834 the variable `gnus-move-split-methods' is used for finding a default
9835 for the target newsgroup.
9836
9837 For this function to work, both the current newsgroup and the
9838 newsgroup that you want to move to have to support the `request-move'
9839 and `request-accept' functions.
9840
9841 ACTION can be either `move' (the default), `crosspost' or `copy'."
9842   (interactive "P")
9843   (unless action
9844     (setq action 'move))
9845   ;; Check whether the source group supports the required functions.
9846   (cond ((and (eq action 'move)
9847               (not (gnus-check-backend-function
9848                     'request-move-article gnus-newsgroup-name)))
9849          (error "The current group does not support article moving"))
9850         ((and (eq action 'crosspost)
9851               (not (gnus-check-backend-function
9852                     'request-replace-article gnus-newsgroup-name)))
9853          (error "The current group does not support article editing")))
9854   (let ((articles (gnus-summary-work-articles n))
9855         (prefix (if (gnus-check-backend-function
9856                      'request-move-article gnus-newsgroup-name)
9857                     (funcall gnus-move-group-prefix-function
9858                              gnus-newsgroup-name)
9859                   ""))
9860         (names '((move "Move" "Moving")
9861                  (copy "Copy" "Copying")
9862                  (crosspost "Crosspost" "Crossposting")))
9863         (copy-buf (save-excursion
9864                     (nnheader-set-temp-buffer " *copy article*")))
9865         art-group to-method new-xref article to-groups
9866         articles-to-update-marks encoded)
9867     (unless (assq action names)
9868       (error "Unknown action %s" action))
9869     ;; Read the newsgroup name.
9870     (when (and (not to-newsgroup)
9871                (not select-method))
9872       (if (and gnus-move-split-methods
9873                (not
9874                 (and (memq gnus-current-article articles)
9875                      (gnus-buffer-live-p gnus-original-article-buffer))))
9876           ;; When `gnus-move-split-methods' is non-nil, we have to
9877           ;; select an article to give `gnus-read-move-group-name' an
9878           ;; opportunity to suggest an appropriate default.  However,
9879           ;; we needn't render or mark the article.
9880           (let ((gnus-display-mime-function nil)
9881                 (gnus-article-prepare-hook nil)
9882                 (gnus-mark-article-hook nil))
9883             (gnus-summary-select-article nil nil nil (car articles))))
9884       (setq to-newsgroup (gnus-read-move-group-name
9885                           (cadr (assq action names))
9886                           (symbol-value
9887                            (intern (format "gnus-current-%s-group" action)))
9888                           articles prefix)
9889             encoded to-newsgroup
9890             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9891       (set (intern (format "gnus-current-%s-group" action))
9892            (mm-decode-coding-string
9893             to-newsgroup
9894             (gnus-group-name-charset to-method to-newsgroup))))
9895     (unless to-method
9896       (setq to-method (or select-method
9897                           (gnus-server-to-method
9898                            (gnus-group-method to-newsgroup)))))
9899     (setq to-newsgroup
9900           (or encoded
9901               (and to-newsgroup
9902                    (mm-encode-coding-string
9903                     to-newsgroup
9904                     (gnus-group-name-charset to-method to-newsgroup)))))
9905     ;; Check the method we are to move this article to...
9906     (unless (gnus-check-backend-function
9907              'request-accept-article (car to-method))
9908       (error "%s does not support article copying" (car to-method)))
9909     (unless (gnus-check-server to-method)
9910       (error "Can't open server %s" (car to-method)))
9911     (gnus-message 6 "%s to %s: %s..."
9912                   (caddr (assq action names))
9913                   (or (car select-method)
9914                       (gnus-group-decoded-name to-newsgroup))
9915                   articles)
9916     (while articles
9917       (setq article (pop articles))
9918       ;; Set any marks that may have changed in the summary buffer.
9919       (when gnus-preserve-marks
9920         (gnus-summary-push-marks-to-backend article))
9921       (setq
9922        art-group
9923        (cond
9924         ;; Move the article.
9925         ((eq action 'move)
9926          ;; Remove this article from future suppression.
9927          (gnus-dup-unsuppress-article article)
9928          (let* ((from-method (gnus-find-method-for-group
9929                               gnus-newsgroup-name))
9930                 (to-method (or select-method
9931                                (gnus-find-method-for-group to-newsgroup)))
9932                 (move-is-internal (gnus-server-equal from-method to-method)))
9933            (gnus-request-move-article
9934             article                     ; Article to move
9935             gnus-newsgroup-name         ; From newsgroup
9936             (nth 1 (gnus-find-method-for-group
9937                     gnus-newsgroup-name)) ; Server
9938             (list 'gnus-request-accept-article
9939                   to-newsgroup (list 'quote select-method)
9940                   (not articles) t)     ; Accept form
9941             (not articles)              ; Only save nov last time
9942             (and move-is-internal
9943                  to-newsgroup           ; Not respooling
9944                                         ; Is this move internal?
9945                  (gnus-group-real-name to-newsgroup)))))
9946         ;; Copy the article.
9947         ((eq action 'copy)
9948          (with-current-buffer copy-buf
9949            (when (gnus-request-article-this-buffer article
9950                                                    gnus-newsgroup-name)
9951              (save-restriction
9952                (nnheader-narrow-to-headers)
9953                (dolist (hdr gnus-copy-article-ignored-headers)
9954                  (message-remove-header hdr t)))
9955              (gnus-request-accept-article
9956               to-newsgroup select-method (not articles) t))))
9957         ;; Crosspost the article.
9958         ((eq action 'crosspost)
9959          (let ((xref (message-tokenize-header
9960                       (mail-header-xref (gnus-summary-article-header
9961                                          article))
9962                       " ")))
9963            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9964                                   ":" (number-to-string article)))
9965            (unless xref
9966              (setq xref (list (system-name))))
9967            (setq new-xref
9968                  (concat
9969                   (mapconcat 'identity
9970                              (delete "Xref:" (delete new-xref xref))
9971                              " ")
9972                   " " new-xref))
9973            (with-current-buffer copy-buf
9974              ;; First put the article in the destination group.
9975              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9976              (when (consp (setq art-group
9977                                 (gnus-request-accept-article
9978                                  to-newsgroup select-method (not articles)
9979                                  t)))
9980                (setq new-xref (concat new-xref " " (car art-group)
9981                                       ":"
9982                                       (number-to-string (cdr art-group))))
9983                ;; Now we have the new Xrefs header, so we insert
9984                ;; it and replace the new article.
9985                (nnheader-replace-header "Xref" new-xref)
9986                (gnus-request-replace-article
9987                 (cdr art-group) to-newsgroup (current-buffer) t)
9988                art-group))))))
9989       (cond
9990        ((not art-group)
9991         (gnus-message 1 "Couldn't %s article %s: %s"
9992                       (cadr (assq action names)) article
9993                       (nnheader-get-report (car to-method))))
9994        ((eq art-group 'junk)
9995         (when (eq action 'move)
9996           (gnus-summary-mark-article article gnus-canceled-mark)
9997           (gnus-message 4 "Deleted article %s" article)
9998           ;; run the delete hook
9999           (run-hook-with-args 'gnus-summary-article-delete-hook
10000                               action
10001                               (gnus-data-header
10002                                (assoc article (gnus-data-list nil)))
10003                               gnus-newsgroup-name nil
10004                               select-method)))
10005        (t
10006         (let* ((pto-group (gnus-group-prefixed-name
10007                            (car art-group) to-method))
10008                (info (gnus-get-info pto-group))
10009                (to-group (gnus-info-group info))
10010                to-marks)
10011           ;; Update the group that has been moved to.
10012           (when (and info
10013                      (memq action '(move copy)))
10014             (unless (member to-group to-groups)
10015               (push to-group to-groups))
10016
10017             (when (and (not (memq article gnus-newsgroup-unreads))
10018                        (cdr art-group))
10019               (push 'read to-marks)
10020               (gnus-info-set-read
10021                info (gnus-add-to-range (gnus-info-read info)
10022                                        (list (cdr art-group)))))
10023
10024             ;; See whether the article is to be put in the cache.
10025             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10026                    (marks (if expirable
10027                               gnus-article-mark-lists
10028                             (delete '(expirable . expire)
10029                                     (copy-sequence
10030                                      gnus-article-mark-lists))))
10031                    (to-article (cdr art-group)))
10032
10033               ;; Enter the article into the cache in the new group,
10034               ;; if that is required.
10035               (when (and to-article
10036                          gnus-use-cache)
10037                 (gnus-cache-possibly-enter-article
10038                  to-group to-article
10039                  (memq article gnus-newsgroup-marked)
10040                  (memq article gnus-newsgroup-dormant)
10041                  (memq article gnus-newsgroup-unreads)))
10042
10043               (when (and gnus-preserve-marks
10044                          to-article)
10045                 ;; Copy any marks over to the new group.
10046                 (when (and (equal to-group gnus-newsgroup-name)
10047                            (not (memq article gnus-newsgroup-unreads)))
10048                   ;; Mark this article as read in this group.
10049                   (push (cons to-article gnus-read-mark)
10050                         gnus-newsgroup-reads)
10051                   ;; Increase the active status of this group.
10052                   (setcdr (gnus-active to-group) to-article)
10053                   (setcdr gnus-newsgroup-active to-article))
10054
10055                 (while marks
10056                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10057                     (when (memq article (symbol-value
10058                                          (intern (format "gnus-newsgroup-%s"
10059                                                          (caar marks)))))
10060                       (push (cdar marks) to-marks)
10061                       ;; If the other group is the same as this group,
10062                       ;; then we have to add the mark to the list.
10063                       (when (equal to-group gnus-newsgroup-name)
10064                         (set (intern (format "gnus-newsgroup-%s"
10065                                              (caar marks)))
10066                              (cons to-article
10067                                    (symbol-value
10068                                     (intern (format "gnus-newsgroup-%s"
10069                                                     (caar marks)))))))
10070                       ;; Copy the marks to other group.
10071                       (gnus-add-marked-articles
10072                        to-group (cdar marks) (list to-article) info)))
10073                   (setq marks (cdr marks)))
10074
10075                 (when (and expirable
10076                            gnus-mark-copied-or-moved-articles-as-expirable
10077                            (not (memq 'expire to-marks)))
10078                   ;; Mark this article as expirable.
10079                   (push 'expire to-marks)
10080                   (when (equal to-group gnus-newsgroup-name)
10081                     (push to-article gnus-newsgroup-expirable))
10082                   ;; Copy the expirable mark to other group.
10083                   (gnus-add-marked-articles
10084                    to-group 'expire (list to-article) info))
10085
10086                 (when (and to-marks
10087                            (or gnus-propagate-marks
10088                                (gnus-method-option-p
10089                                 (gnus-find-method-for-group to-group)
10090                                 'server-marks)))
10091                   (gnus-request-set-mark
10092                    to-group (list (list (list to-article) 'add to-marks)))))
10093
10094               (gnus-dribble-enter
10095                (concat "(gnus-group-set-info '"
10096                        (gnus-prin1-to-string (gnus-get-info to-group))
10097                        ")")
10098                (concat "^(gnus-group-set-info '(\""
10099                        (regexp-quote to-group) "\""))))
10100
10101           ;; Update the Xref header in this article to point to
10102           ;; the new crossposted article we have just created.
10103           (when (eq action 'crosspost)
10104             (with-current-buffer copy-buf
10105               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10106               (nnheader-replace-header "Xref" new-xref)
10107               (gnus-request-replace-article
10108                article gnus-newsgroup-name (current-buffer) t)))
10109
10110           ;; run the move/copy/crosspost/respool hook
10111           (run-hook-with-args 'gnus-summary-article-move-hook
10112                               action
10113                               (gnus-data-header
10114                                (assoc article (gnus-data-list nil)))
10115                               gnus-newsgroup-name
10116                               to-newsgroup
10117                               select-method))
10118
10119         ;;;!!!Why is this necessary?
10120         (set-buffer gnus-summary-buffer)
10121
10122         (when (eq action 'move)
10123           (save-excursion
10124             (gnus-summary-goto-subject article)
10125             (gnus-summary-mark-article article gnus-canceled-mark)))))
10126       (push article articles-to-update-marks))
10127
10128     (save-excursion
10129       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10130     ;; Re-activate all groups that have been moved to.
10131     (with-current-buffer gnus-group-buffer
10132       (let ((gnus-group-marked to-groups))
10133         (gnus-group-get-new-news-this-group nil t)))
10134
10135     (gnus-kill-buffer copy-buf)
10136     (gnus-summary-position-point)
10137     (gnus-set-mode-line 'summary)))
10138
10139 (defun gnus-summary-push-marks-to-backend (article)
10140   (let ((set nil)
10141         (marks gnus-article-mark-lists))
10142     (unless (memq article gnus-newsgroup-unreads)
10143       (push 'read set))
10144     (while marks
10145       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10146                  (memq article (symbol-value
10147                                 (intern (format "gnus-newsgroup-%s"
10148                                                 (caar marks))))))
10149         (push (cdar marks) set))
10150       (pop marks))
10151     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10152
10153 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10154   "Copy the current article to some other group.
10155 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10156 When called interactively, if TO-NEWSGROUP is nil, use the value of
10157 the variable `gnus-move-split-methods' for finding a default target
10158 newsgroup.
10159 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10160 re-spool using this method."
10161   (interactive "P")
10162   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10163
10164 (defun gnus-summary-crosspost-article (&optional n)
10165   "Crosspost the current article to some other group."
10166   (interactive "P")
10167   (gnus-summary-move-article n nil nil 'crosspost))
10168
10169 (defcustom gnus-summary-respool-default-method nil
10170   "Default method type for respooling an article.
10171 If nil, use to the current newsgroup method."
10172   :type 'symbol
10173   :group 'gnus-summary-mail)
10174
10175 (defun gnus-summary-respool-article (&optional n method)
10176   "Respool the current article.
10177 The article will be squeezed through the mail spooling process again,
10178 which means that it will be put in some mail newsgroup or other
10179 depending on `nnmail-split-methods'.
10180 If N is a positive number, respool the N next articles.
10181 If N is a negative number, respool the N previous articles.
10182 If N is nil and any articles have been marked with the process mark,
10183 respool those articles instead.
10184
10185 Respooling can be done both from mail groups and \"real\" newsgroups.
10186 In the former case, the articles in question will be moved from the
10187 current group into whatever groups they are destined to.  In the
10188 latter case, they will be copied into the relevant groups."
10189   (interactive
10190    (list current-prefix-arg
10191          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10192                 (methname
10193                  (symbol-name (or gnus-summary-respool-default-method
10194                                   (car (gnus-find-method-for-group
10195                                         gnus-newsgroup-name)))))
10196                 (method
10197                  (gnus-completing-read
10198                   "Backend to use when respooling"
10199                   methods t nil 'gnus-mail-method-history methname))
10200                 ms)
10201            (cond
10202             ((zerop (length (setq ms (gnus-servers-using-backend
10203                                       (intern method)))))
10204              (list (intern method) ""))
10205             ((= 1 (length ms))
10206              (car ms))
10207             (t
10208              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10209                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10210                            ms-alist))))))))
10211   (unless method
10212     (error "No method given for respooling"))
10213   (if (assoc (symbol-name
10214               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10215              (gnus-methods-using 'respool))
10216       (gnus-summary-move-article n nil method)
10217     (gnus-summary-copy-article n nil method)))
10218
10219 (defun gnus-summary-import-article (file &optional edit)
10220   "Import an arbitrary file into a mail newsgroup."
10221   (interactive "fImport file: \nP")
10222   (let ((group gnus-newsgroup-name)
10223         (now (current-time))
10224         atts lines group-art)
10225     (unless (gnus-check-backend-function 'request-accept-article group)
10226       (error "%s does not support article importing" group))
10227     (or (file-readable-p file)
10228         (not (file-regular-p file))
10229         (error "Can't read %s" file))
10230     (with-current-buffer (gnus-get-buffer-create " *import file*")
10231       (erase-buffer)
10232       (nnheader-insert-file-contents file)
10233       (goto-char (point-min))
10234       (if (nnheader-article-p)
10235           (save-restriction
10236             (goto-char (point-min))
10237             (search-forward "\n\n" nil t)
10238             (narrow-to-region (point-min) (1- (point)))
10239             (goto-char (point-min))
10240             (unless (re-search-forward "^date:" nil t)
10241               (goto-char (point-max))
10242               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10243        ;; This doesn't look like an article, so we fudge some headers.
10244         (setq atts (file-attributes file)
10245               lines (count-lines (point-min) (point-max)))
10246         (insert "From: " (read-string "From: ") "\n"
10247                 "Subject: " (read-string "Subject: ") "\n"
10248                 "Date: " (message-make-date (nth 5 atts)) "\n"
10249                 "Message-ID: " (message-make-message-id) "\n"
10250                 "Lines: " (int-to-string lines) "\n"
10251                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10252       (setq group-art (gnus-request-accept-article group nil t))
10253       (kill-buffer (current-buffer)))
10254     (setq gnus-newsgroup-active (gnus-activate-group group))
10255     (forward-line 1)
10256     (gnus-summary-goto-article (cdr group-art) nil t)
10257     (when edit
10258       (gnus-summary-edit-article))))
10259
10260 (defun gnus-summary-create-article ()
10261   "Create an article in a mail newsgroup."
10262   (interactive)
10263   (let ((group gnus-newsgroup-name)
10264         (now (current-time))
10265         group-art)
10266     (unless (gnus-check-backend-function 'request-accept-article group)
10267       (error "%s does not support article importing" group))
10268     (with-current-buffer (gnus-get-buffer-create " *import file*")
10269       (erase-buffer)
10270       (goto-char (point-min))
10271       ;; This doesn't look like an article, so we fudge some headers.
10272       (insert "From: " (read-string "From: ") "\n"
10273               "Subject: " (read-string "Subject: ") "\n"
10274               "Date: " (message-make-date now) "\n"
10275               "Message-ID: " (message-make-message-id) "\n")
10276       (setq group-art (gnus-request-accept-article group nil t))
10277       (kill-buffer (current-buffer)))
10278     (setq gnus-newsgroup-active (gnus-activate-group group))
10279     (forward-line 1)
10280     (gnus-summary-goto-article (cdr group-art) nil t)
10281     (gnus-summary-edit-article)))
10282
10283 (defun gnus-summary-article-posted-p ()
10284   "Say whether the current (mail) article is available from news as well.
10285 This will be the case if the article has both been mailed and posted."
10286   (interactive)
10287   (let ((id (mail-header-references (gnus-summary-article-header)))
10288         (gnus-override-method (car (gnus-refer-article-methods))))
10289     (if (gnus-request-head id "")
10290         (gnus-message 2 "The current message was found on %s"
10291                       gnus-override-method)
10292       (gnus-message 2 "The current message couldn't be found on %s"
10293                     gnus-override-method)
10294       nil)))
10295
10296 (defun gnus-summary-expire-articles (&optional now)
10297   "Expire all articles that are marked as expirable in the current group."
10298   (interactive)
10299   (when (and (not gnus-group-is-exiting-without-update-p)
10300              (gnus-check-backend-function
10301               'request-expire-articles gnus-newsgroup-name))
10302     ;; This backend supports expiry.
10303     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10304            (expirable (if total
10305                           (progn
10306                             ;; We need to update the info for
10307                             ;; this group for `gnus-list-of-read-articles'
10308                             ;; to give us the right answer.
10309                             (gnus-run-hooks 'gnus-exit-group-hook)
10310                             (gnus-summary-update-info)
10311                             (gnus-list-of-read-articles gnus-newsgroup-name))
10312                         (setq gnus-newsgroup-expirable
10313                               (sort gnus-newsgroup-expirable '<))))
10314            (expiry-wait (if now 'immediate
10315                           (gnus-group-find-parameter
10316                            gnus-newsgroup-name 'expiry-wait)))
10317            (nnmail-expiry-target
10318             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10319                 nnmail-expiry-target))
10320            es)
10321       (when expirable
10322         ;; There are expirable articles in this group, so we run them
10323         ;; through the expiry process.
10324         (gnus-message 6 "Expiring articles...")
10325         (when (gnus-check-group gnus-newsgroup-name)
10326           ;; The list of articles that weren't expired is returned.
10327           (save-excursion
10328             (if expiry-wait
10329                 (let ((nnmail-expiry-wait-function nil)
10330                       (nnmail-expiry-wait expiry-wait))
10331                   (setq es (gnus-request-expire-articles
10332                             expirable gnus-newsgroup-name)))
10333               (setq es (gnus-request-expire-articles
10334                         expirable gnus-newsgroup-name)))
10335             (unless total
10336               (setq gnus-newsgroup-expirable es))
10337             ;; We go through the old list of expirable, and mark all
10338             ;; really expired articles as nonexistent.
10339             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10340               (let ((gnus-use-cache nil))
10341                 (dolist (article expirable)
10342                   (when (and (not (memq article es))
10343                              (gnus-data-find article))
10344                     (gnus-summary-mark-article article gnus-canceled-mark)
10345                     (run-hook-with-args 'gnus-summary-article-expire-hook
10346                                         'delete
10347                                         (gnus-data-header
10348                                          (assoc article (gnus-data-list nil)))
10349                                         gnus-newsgroup-name
10350                                         nil
10351                                         nil)))))))
10352         (gnus-message 6 "Expiring articles...done")))))
10353
10354 (defun gnus-summary-expire-articles-now ()
10355   "Expunge all expirable articles in the current group.
10356 This means that *all* articles that are marked as expirable will be
10357 deleted forever, right now."
10358   (interactive)
10359   (or gnus-expert-user
10360       (gnus-yes-or-no-p
10361        "Are you really, really sure you want to delete all expirable messages? ")
10362       (error "Phew!"))
10363   (gnus-summary-expire-articles t))
10364
10365 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10366 (defun gnus-summary-delete-article (&optional n)
10367   "Delete the N next (mail) articles.
10368 This command actually deletes articles.  This is not a marking
10369 command.  The article will disappear forever from your life, never to
10370 return.
10371
10372 If N is negative, delete backwards.
10373 If N is nil and articles have been marked with the process mark,
10374 delete these instead.
10375
10376 If `gnus-novice-user' is non-nil you will be asked for
10377 confirmation before the articles are deleted."
10378   (interactive "P")
10379   (unless (gnus-check-backend-function 'request-expire-articles
10380                                        gnus-newsgroup-name)
10381     (error "The current newsgroup does not support article deletion"))
10382   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10383     (error "Couldn't open server"))
10384   ;; Compute the list of articles to delete.
10385   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10386         (nnmail-expiry-target 'delete)
10387         not-deleted)
10388     (if (and gnus-novice-user
10389              (not (gnus-yes-or-no-p
10390                    (format "Do you really want to delete %s forever? "
10391                            (if (> (length articles) 1)
10392                                (format "these %s articles" (length articles))
10393                              "this article")))))
10394         ()
10395       ;; Delete the articles.
10396       (setq not-deleted (gnus-request-expire-articles
10397                          articles gnus-newsgroup-name 'force))
10398       (save-excursion
10399         (while articles
10400           (gnus-summary-remove-process-mark (car articles))
10401           ;; The backend might not have been able to delete the article
10402           ;; after all.
10403           (unless (memq (car articles) not-deleted)
10404             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10405             (let* ((article (car articles))
10406                    (ghead  (gnus-data-header
10407                             (assoc article (gnus-data-list nil)))))
10408               (run-hook-with-args 'gnus-summary-article-delete-hook
10409                                   'delete ghead gnus-newsgroup-name nil
10410                                   nil)))
10411           (setq articles (cdr articles))))
10412       (when not-deleted
10413         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10414     (gnus-summary-position-point)
10415     (gnus-set-mode-line 'summary)
10416     not-deleted))
10417
10418 (defun gnus-summary-edit-article (&optional arg)
10419   "Edit the current article.
10420 This will have permanent effect only in mail groups.
10421 If ARG is nil, edit the decoded articles.
10422 If ARG is 1, edit the raw articles.
10423 If ARG is 2, edit the raw articles even in read-only groups.
10424 If ARG is 3, edit the articles with the current handles.
10425 Otherwise, allow editing of articles even in read-only
10426 groups."
10427   (interactive "P")
10428   (let (force raw current-handles)
10429     (cond
10430      ((null arg))
10431      ((eq arg 1)
10432       (setq raw t))
10433      ((eq arg 2)
10434       (setq raw t
10435             force t))
10436      ((eq arg 3)
10437       (setq current-handles
10438             (and (gnus-buffer-live-p gnus-article-buffer)
10439                  (with-current-buffer gnus-article-buffer
10440                    (prog1
10441                        gnus-article-mime-handles
10442                      (setq gnus-article-mime-handles nil))))))
10443      (t
10444       (setq force t)))
10445     (when (and raw (not force)
10446                (member gnus-newsgroup-name '("nndraft:delayed"
10447                                              "nndraft:drafts"
10448                                              "nndraft:queue")))
10449       (error "Can't edit the raw article in group %s"
10450              gnus-newsgroup-name))
10451     (with-current-buffer gnus-summary-buffer
10452       (let ((mail-parse-charset gnus-newsgroup-charset)
10453             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10454         (gnus-set-global-variables)
10455         (when (and (not force)
10456                    (gnus-group-read-only-p))
10457           (error "The current newsgroup does not support article editing"))
10458         (gnus-summary-show-article t)
10459         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10460           (with-current-buffer gnus-article-buffer
10461             (mm-enable-multibyte)))
10462         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10463             (setq raw t))
10464         (gnus-article-edit-article
10465          (if raw 'ignore
10466            `(lambda ()
10467               (let ((mbl mml-buffer-list))
10468                 (setq mml-buffer-list nil)
10469                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10470                   (mime-to-mml ,'current-handles))
10471                 (let ((mbl1 mml-buffer-list))
10472                   (setq mml-buffer-list mbl)
10473                   (set (make-local-variable 'mml-buffer-list) mbl1))
10474                 (gnus-make-local-hook 'kill-buffer-hook)
10475                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10476          `(lambda (no-highlight)
10477             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10478                   (message-options message-options)
10479                   (message-options-set-recipient)
10480                   (mail-parse-ignored-charsets
10481                    ',gnus-newsgroup-ignored-charsets)
10482                   (rfc2047-header-encoding-alist
10483                    ',(let ((charset (gnus-group-name-charset
10484                                      (gnus-find-method-for-group
10485                                       gnus-newsgroup-name)
10486                                      gnus-newsgroup-name)))
10487                        (append (list (cons "Newsgroups" charset)
10488                                      (cons "Followup-To" charset)
10489                                      (cons "Xref" charset))
10490                                rfc2047-header-encoding-alist))))
10491               ,(if (not raw) '(progn
10492                                 (mml-to-mime)
10493                                 (mml-destroy-buffers)
10494                                 (remove-hook 'kill-buffer-hook
10495                                              'mml-destroy-buffers t)
10496                                 (kill-local-variable 'mml-buffer-list)))
10497               (gnus-summary-edit-article-done
10498                ,(or (mail-header-references gnus-current-headers) "")
10499                ,(gnus-group-read-only-p)
10500                ,gnus-summary-buffer no-highlight))))))))
10501
10502 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10503
10504 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10505                                                  no-highlight)
10506   "Make edits to the current article permanent."
10507   (interactive)
10508   (save-excursion
10509     ;; The buffer restriction contains the entire article if it exists.
10510     (when (article-goto-body)
10511       (let ((lines (count-lines (point) (point-max)))
10512             (length (- (point-max) (point)))
10513             (case-fold-search t)
10514             (body (copy-marker (point))))
10515         (goto-char (point-min))
10516         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10517           (delete-region (match-beginning 1) (match-end 1))
10518           (insert (number-to-string length)))
10519         (goto-char (point-min))
10520         (when (re-search-forward
10521                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10522           (delete-region (match-beginning 1) (match-end 1))
10523           (insert (number-to-string length)))
10524         (goto-char (point-min))
10525         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10526           (delete-region (match-beginning 1) (match-end 1))
10527           (insert (number-to-string lines))))))
10528   ;; Replace the article.
10529   (let ((buf (current-buffer))
10530         (article (cdr gnus-article-current))
10531         replace-result)
10532     (with-temp-buffer
10533       (insert-buffer-substring buf)
10534       (if (and (not read-only)
10535                (not (setq replace-result
10536                           (gnus-request-replace-article
10537                            article (car gnus-article-current)
10538                            (current-buffer) t))))
10539           (error "Couldn't replace article")
10540         ;; If we got a number back, then that's the new article number
10541         ;; for this article.  Otherwise, the article number didn't change.
10542         (when (numberp replace-result)
10543           (with-current-buffer gnus-summary-buffer
10544             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10545             (gnus-summary-limit gnus-newsgroup-limit)
10546             (setq article replace-result)
10547             (gnus-summary-goto-subject article t)))
10548         ;; Update the summary buffer.
10549         (if (and references
10550                  (equal (message-tokenize-header references " ")
10551                         (message-tokenize-header
10552                          (or (message-fetch-field "references") "") " ")))
10553             ;; We only have to update this line.
10554             (save-excursion
10555               (save-restriction
10556                 (message-narrow-to-head)
10557                 (let ((head (buffer-substring-no-properties
10558                              (point-min) (point-max)))
10559                       header)
10560                   (with-temp-buffer
10561                     (insert (format "211 %d Article retrieved.\n" article))
10562                     (insert head)
10563                     (insert ".\n")
10564                     (let ((nntp-server-buffer (current-buffer)))
10565                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10566                     (with-current-buffer gnus-summary-buffer
10567                       (gnus-data-set-header (gnus-data-find article) header)
10568                       (gnus-summary-update-article-line article header)
10569                       (if (gnus-summary-goto-subject article nil t)
10570                           (gnus-summary-update-secondary-mark article)))))))
10571           ;; Update threads.
10572           (set-buffer (or buffer gnus-summary-buffer))
10573           (gnus-summary-update-article article)
10574           (if (gnus-summary-goto-subject article nil t)
10575               (gnus-summary-update-secondary-mark article)))
10576         ;; Prettify the article buffer again.
10577         (unless no-highlight
10578           (with-current-buffer gnus-article-buffer
10579             ;;!!! Fix this -- article should be rehighlighted.
10580             ;;(gnus-run-hooks 'gnus-article-display-hook)
10581             (set-buffer gnus-original-article-buffer)
10582             (gnus-request-article
10583              article (car gnus-article-current) (current-buffer))))
10584         ;; Prettify the summary buffer line.
10585         (when (gnus-visual-p 'summary-highlight 'highlight)
10586           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10587
10588 (defun gnus-summary-edit-wash (key)
10589   "Perform editing command KEY in the article buffer."
10590   (interactive
10591    (list
10592     (progn
10593       (message "%s" (concat (this-command-keys) "- "))
10594       (read-char))))
10595   (message "")
10596   (gnus-summary-edit-article)
10597   (execute-kbd-macro (concat (this-command-keys) key))
10598   (gnus-article-edit-done))
10599
10600 ;;; Respooling
10601
10602 (defun gnus-summary-respool-query (&optional silent trace)
10603   "Query where the respool algorithm would put this article."
10604   (interactive)
10605   (let (gnus-mark-article-hook)
10606     (gnus-summary-select-article)
10607     (with-current-buffer gnus-original-article-buffer
10608       (let ((groups (nnmail-article-group 'identity trace)))
10609         (unless silent
10610           (if groups
10611               (message "This message would go to %s"
10612                        (mapconcat 'car groups ", "))
10613             (message "This message would go to no groups"))
10614           groups)))))
10615
10616 (defun gnus-summary-respool-trace ()
10617   "Trace where the respool algorithm would put this article.
10618 Display a buffer showing all fancy splitting patterns which matched."
10619   (interactive)
10620   (gnus-summary-respool-query nil t))
10621
10622 ;; Summary marking commands.
10623
10624 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10625   "Mark articles which has the same subject as read, and then select the next.
10626 If UNMARK is positive, remove any kind of mark.
10627 If UNMARK is negative, tick articles."
10628   (interactive "P")
10629   (when unmark
10630     (setq unmark (prefix-numeric-value unmark)))
10631   (let ((count
10632          (gnus-summary-mark-same-subject
10633           (gnus-summary-article-subject) unmark)))
10634     ;; Select next unread article.  If auto-select-same mode, should
10635     ;; select the first unread article.
10636     (gnus-summary-next-article t (and gnus-auto-select-same
10637                                       (gnus-summary-article-subject)))
10638     (gnus-message 7 "%d article%s marked as %s"
10639                   count (if (= count 1) " is" "s are")
10640                   (if unmark "unread" "read"))))
10641
10642 (defun gnus-summary-kill-same-subject (&optional unmark)
10643   "Mark articles which has the same subject as read.
10644 If UNMARK is positive, remove any kind of mark.
10645 If UNMARK is negative, tick articles."
10646   (interactive "P")
10647   (when unmark
10648     (setq unmark (prefix-numeric-value unmark)))
10649   (let ((count
10650          (gnus-summary-mark-same-subject
10651           (gnus-summary-article-subject) unmark)))
10652     ;; If marked as read, go to next unread subject.
10653     (when (null unmark)
10654       ;; Go to next unread subject.
10655       (gnus-summary-next-subject 1 t))
10656     (gnus-message 7 "%d articles are marked as %s"
10657                   count (if unmark "unread" "read"))))
10658
10659 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10660   "Mark articles with same SUBJECT as read, and return marked number.
10661 If optional argument UNMARK is positive, remove any kinds of marks.
10662 If optional argument UNMARK is negative, mark articles as unread instead."
10663   (let ((count 1))
10664     (save-excursion
10665       (cond
10666        ((null unmark)                   ; Mark as read.
10667         (while (and
10668                 (progn
10669                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10670                   (gnus-summary-show-thread) t)
10671                 (gnus-summary-find-subject subject))
10672           (setq count (1+ count))))
10673        ((> unmark 0)                    ; Tick.
10674         (while (and
10675                 (progn
10676                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10677                   (gnus-summary-show-thread) t)
10678                 (gnus-summary-find-subject subject))
10679           (setq count (1+ count))))
10680        (t                               ; Mark as unread.
10681         (while (and
10682                 (progn
10683                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10684                   (gnus-summary-show-thread) t)
10685                 (gnus-summary-find-subject subject))
10686           (setq count (1+ count)))))
10687       (gnus-set-mode-line 'summary)
10688       ;; Return the number of marked articles.
10689       count)))
10690
10691 (defun gnus-summary-mark-as-processable (n &optional unmark)
10692   "Set the process mark on the next N articles.
10693 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10694 the process mark instead.  The difference between N and the actual
10695 number of articles marked is returned."
10696   (interactive "P")
10697   (if (and (null n) (gnus-region-active-p))
10698       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10699     (setq n (prefix-numeric-value n))
10700     (let ((backward (< n 0))
10701           (n (abs n)))
10702       (while (and
10703               (> n 0)
10704               (if unmark
10705                   (gnus-summary-remove-process-mark
10706                    (gnus-summary-article-number))
10707                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10708               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10709         (setq n (1- n)))
10710       (when (/= 0 n)
10711         (gnus-message 7 "No more articles"))
10712       (gnus-summary-recenter)
10713       (gnus-summary-position-point)
10714       n)))
10715
10716 (defun gnus-summary-unmark-as-processable (n)
10717   "Remove the process mark from the next N articles.
10718 If N is negative, unmark backward instead.  The difference between N and
10719 the actual number of articles unmarked is returned."
10720   (interactive "P")
10721   (gnus-summary-mark-as-processable n t))
10722
10723 (defun gnus-summary-unmark-all-processable ()
10724   "Remove the process mark from all articles."
10725   (interactive)
10726   (save-excursion
10727     (while gnus-newsgroup-processable
10728       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10729   (gnus-summary-position-point))
10730
10731 (defun gnus-summary-add-mark (article type)
10732   "Mark ARTICLE with a mark of TYPE."
10733   (let ((vtype (car (assq type gnus-article-mark-lists)))
10734         var)
10735     (if (not vtype)
10736         (error "No such mark type: %s" type)
10737       (setq var (intern (format "gnus-newsgroup-%s" type)))
10738       (set var (cons article (symbol-value var)))
10739       (if (memq type '(processable cached replied forwarded recent saved))
10740           (gnus-summary-update-secondary-mark article)
10741         ;;; !!! This is bogus.  We should find out what primary
10742         ;;; !!! mark we want to set.
10743         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10744
10745 (defun gnus-summary-mark-as-expirable (n)
10746   "Mark N articles forward as expirable.
10747 If N is negative, mark backward instead.  The difference between N and
10748 the actual number of articles marked is returned."
10749   (interactive "p")
10750   (gnus-summary-mark-forward n gnus-expirable-mark))
10751
10752 (defun gnus-summary-mark-as-spam (n)
10753   "Mark N articles forward as spam.
10754 If N is negative, mark backward instead.  The difference between N and
10755 the actual number of articles marked is returned."
10756   (interactive "p")
10757   (gnus-summary-mark-forward n gnus-spam-mark))
10758
10759 (defun gnus-summary-mark-article-as-replied (article)
10760   "Mark ARTICLE as replied to and update the summary line.
10761 ARTICLE can also be a list of articles."
10762   (interactive (list (gnus-summary-article-number)))
10763   (let ((articles (if (listp article) article (list article))))
10764     (dolist (article articles)
10765       (unless (numberp article)
10766         (error "%s is not a number" article))
10767       (push article gnus-newsgroup-replied)
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-mark-article-as-forwarded (article)
10773   "Mark ARTICLE as forwarded and update the summary line.
10774 ARTICLE can also be a list of articles."
10775   (let ((articles (if (listp article) article (list article))))
10776     (dolist (article articles)
10777       (push article gnus-newsgroup-forwarded)
10778       (let ((inhibit-read-only t))
10779         (when (gnus-summary-goto-subject article nil t)
10780           (gnus-summary-update-secondary-mark article))))))
10781
10782 (defun gnus-summary-set-bookmark (article)
10783   "Set a bookmark in current article."
10784   (interactive (list (gnus-summary-article-number)))
10785   (when (or (not (get-buffer gnus-article-buffer))
10786             (not gnus-current-article)
10787             (not gnus-article-current)
10788             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10789     (error "No current article selected"))
10790   ;; Remove old bookmark, if one exists.
10791   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10792   ;; Set the new bookmark, which is on the form
10793   ;; (article-number . line-number-in-body).
10794   (push
10795    (cons article
10796          (with-current-buffer gnus-article-buffer
10797            (count-lines
10798             (min (point)
10799                  (save-excursion
10800                    (article-goto-body)
10801                    (point)))
10802             (point))))
10803    gnus-newsgroup-bookmarks)
10804   (gnus-message 6 "A bookmark has been added to the current article."))
10805
10806 (defun gnus-summary-remove-bookmark (article)
10807   "Remove the bookmark from the current article."
10808   (interactive (list (gnus-summary-article-number)))
10809   ;; Remove old bookmark, if one exists.
10810   (if (not (assq article gnus-newsgroup-bookmarks))
10811       (gnus-message 6 "No bookmark in current article.")
10812     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10813     (gnus-message 6 "Removed bookmark.")))
10814
10815 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10816 (defun gnus-summary-mark-as-dormant (n)
10817   "Mark N articles forward as dormant.
10818 If N is negative, mark backward instead.  The difference between N and
10819 the actual number of articles marked is returned."
10820   (interactive "p")
10821   (gnus-summary-mark-forward n gnus-dormant-mark))
10822
10823 (defun gnus-summary-set-process-mark (article)
10824   "Set the process mark on ARTICLE and update the summary line."
10825   (setq gnus-newsgroup-processable
10826         (cons article
10827               (delq article gnus-newsgroup-processable)))
10828   (when (gnus-summary-goto-subject article)
10829     (gnus-summary-show-thread)
10830     (gnus-summary-goto-subject article)
10831     (gnus-summary-update-secondary-mark article)))
10832
10833 (defun gnus-summary-remove-process-mark (&rest articles)
10834   "Remove the process mark from ARTICLES and update the summary line."
10835   (dolist (article articles)
10836     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10837     (when (gnus-summary-goto-subject article)
10838       (gnus-summary-show-thread)
10839       (gnus-summary-goto-subject article)
10840       (gnus-summary-update-secondary-mark article)))
10841   t)
10842
10843 (defun gnus-summary-set-saved-mark (article)
10844   "Set the process mark on ARTICLE and update the summary line."
10845   (push article gnus-newsgroup-saved)
10846   (when (gnus-summary-goto-subject article)
10847     (gnus-summary-update-secondary-mark article)))
10848
10849 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10850   "Mark N articles as read forwards.
10851 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10852 The difference between N and the actual number of articles marked is
10853 returned.
10854 If NO-EXPIRE, auto-expiry will be inhibited."
10855   (interactive "p")
10856   (gnus-summary-show-thread)
10857   (let ((backward (< n 0))
10858         (gnus-summary-goto-unread
10859          (and gnus-summary-goto-unread
10860               (not (eq gnus-summary-goto-unread 'never))
10861               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10862                                     gnus-ticked-mark gnus-dormant-mark)))))
10863         (n (abs n))
10864         (mark (or mark gnus-del-mark)))
10865     (while (and (> n 0)
10866                 (gnus-summary-mark-article nil mark no-expire)
10867                 (zerop (gnus-summary-next-subject
10868                         (if backward -1 1)
10869                         (and gnus-summary-goto-unread
10870                              (not (eq gnus-summary-goto-unread 'never)))
10871                         t)))
10872       (setq n (1- n)))
10873     (when (/= 0 n)
10874       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10875     (gnus-summary-recenter)
10876     (gnus-summary-position-point)
10877     (gnus-set-mode-line 'summary)
10878     n))
10879
10880 (defun gnus-summary-mark-article-as-read (mark)
10881   "Mark the current article quickly as read with MARK."
10882   (let ((article (gnus-summary-article-number)))
10883     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10884     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10885     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10886     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10887     (push (cons article mark) gnus-newsgroup-reads)
10888     ;; Possibly remove from cache, if that is used.
10889     (when gnus-use-cache
10890       (gnus-cache-enter-remove-article article))
10891     ;; Allow the backend to change the mark.
10892     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10893     ;; Check for auto-expiry.
10894     (when (and gnus-newsgroup-auto-expire
10895                (memq mark gnus-auto-expirable-marks))
10896       (setq mark gnus-expirable-mark)
10897       ;; Let the backend know about the mark change.
10898       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10899       (push article gnus-newsgroup-expirable))
10900     ;; Set the mark in the buffer.
10901     (gnus-summary-update-mark mark 'unread)
10902     t))
10903
10904 (defun gnus-summary-mark-article-as-unread (mark)
10905   "Mark the current article quickly as unread with MARK."
10906   (let* ((article (gnus-summary-article-number))
10907          (old-mark (gnus-summary-article-mark article)))
10908     ;; Allow the backend to change the mark.
10909     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10910     (if (eq mark old-mark)
10911         t
10912       (if (<= article 0)
10913           (progn
10914             (gnus-error 1 "Can't mark negative article numbers")
10915             nil)
10916         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10917         (setq gnus-newsgroup-spam-marked
10918               (delq article gnus-newsgroup-spam-marked))
10919         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10920         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10921         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10922         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10923         (cond ((= mark gnus-ticked-mark)
10924                (setq gnus-newsgroup-marked
10925                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10926                                               article)))
10927               ((= mark gnus-spam-mark)
10928                (setq gnus-newsgroup-spam-marked
10929                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10930                                               article)))
10931               ((= mark gnus-dormant-mark)
10932                (setq gnus-newsgroup-dormant
10933                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10934                                               article)))
10935               (t
10936                (setq gnus-newsgroup-unreads
10937                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10938                                               article))))
10939         (gnus-alist-pull article gnus-newsgroup-reads)
10940
10941         ;; See whether the article is to be put in the cache.
10942         (and gnus-use-cache
10943              (vectorp (gnus-summary-article-header article))
10944              (save-excursion
10945                (gnus-cache-possibly-enter-article
10946                 gnus-newsgroup-name article
10947                 (= mark gnus-ticked-mark)
10948                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10949
10950         ;; Fix the mark.
10951         (gnus-summary-update-mark mark 'unread)
10952         t))))
10953
10954 (defun gnus-summary-mark-article (&optional article mark no-expire)
10955   "Mark ARTICLE with MARK.  MARK can be any character.
10956 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10957 `??' (dormant) and `?E' (expirable).
10958 If MARK is nil, then the default character `?r' is used.
10959 If ARTICLE is nil, then the article on the current line will be
10960 marked.
10961 If NO-EXPIRE, auto-expiry will be inhibited."
10962   ;; The mark might be a string.
10963   (when (stringp mark)
10964     (setq mark (aref mark 0)))
10965   ;; If no mark is given, then we check auto-expiring.
10966   (when (null mark)
10967     (setq mark gnus-del-mark))
10968   (when (and (not no-expire)
10969              gnus-newsgroup-auto-expire
10970              (memq mark gnus-auto-expirable-marks))
10971     (setq mark gnus-expirable-mark))
10972   (let ((article (or article (gnus-summary-article-number)))
10973         (old-mark (gnus-summary-article-mark article)))
10974     ;; Allow the backend to change the mark.
10975     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10976     (if (eq mark old-mark)
10977         t
10978       (unless article
10979         (error "No article on current line"))
10980       (if (not (if (or (= mark gnus-unread-mark)
10981                        (= mark gnus-ticked-mark)
10982                        (= mark gnus-spam-mark)
10983                        (= mark gnus-dormant-mark))
10984                    (gnus-mark-article-as-unread article mark)
10985                  (gnus-mark-article-as-read article mark)))
10986           t
10987         ;; See whether the article is to be put in the cache.
10988         (and gnus-use-cache
10989              (not (= mark gnus-canceled-mark))
10990              (vectorp (gnus-summary-article-header article))
10991              (save-excursion
10992                (gnus-cache-possibly-enter-article
10993                 gnus-newsgroup-name article
10994                 (= mark gnus-ticked-mark)
10995                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10996
10997         (when (gnus-summary-goto-subject article nil t)
10998           (let ((inhibit-read-only t))
10999             (gnus-summary-show-thread)
11000             ;; Fix the mark.
11001             (gnus-summary-update-mark mark 'unread)
11002             t))))))
11003
11004 (defun gnus-summary-update-secondary-mark (article)
11005   "Update the secondary (read, process, cache) mark."
11006   (gnus-summary-update-mark
11007    (cond ((memq article gnus-newsgroup-processable)
11008           gnus-process-mark)
11009          ((memq article gnus-newsgroup-cached)
11010           gnus-cached-mark)
11011          ((memq article gnus-newsgroup-replied)
11012           gnus-replied-mark)
11013          ((memq article gnus-newsgroup-forwarded)
11014           gnus-forwarded-mark)
11015          ((memq article gnus-newsgroup-saved)
11016           gnus-saved-mark)
11017          ((memq article gnus-newsgroup-unseen)
11018           gnus-unseen-mark)
11019          (t gnus-no-mark))
11020    'replied)
11021   (when (gnus-visual-p 'summary-highlight 'highlight)
11022     (gnus-summary-highlight-line)
11023     (gnus-run-hooks 'gnus-summary-update-hook))
11024   t)
11025
11026 (defun gnus-summary-update-download-mark (article)
11027   "Update the download mark."
11028   (gnus-summary-update-mark
11029    (cond ((memq article gnus-newsgroup-undownloaded)
11030           gnus-undownloaded-mark)
11031          (gnus-newsgroup-agentized
11032           gnus-downloaded-mark)
11033          (t
11034           gnus-no-mark))
11035    'download)
11036   (gnus-summary-update-line t)
11037   t)
11038
11039 (defun gnus-summary-update-mark (mark type)
11040   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11041         (inhibit-read-only t))
11042     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11043     (when forward
11044       (when (looking-at "\r")
11045         (incf forward))
11046       (when (<= (+ forward (point)) (point-max))
11047         ;; Go to the right position on the line.
11048         (goto-char (+ forward (point)))
11049         ;; Replace the old mark with the new mark.
11050         (let ((to-insert
11051                (mm-subst-char-in-string
11052                 (char-after) mark
11053                 (buffer-substring (point) (1+ (point))))))
11054           (delete-region (point) (1+ (point)))
11055           (insert to-insert))
11056         ;; Optionally update the marks by some user rule.
11057         (when (eq type 'unread)
11058           (gnus-data-set-mark
11059            (gnus-data-find (gnus-summary-article-number)) mark)
11060           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11061
11062 (defun gnus-mark-article-as-read (article &optional mark)
11063   "Enter ARTICLE in the pertinent lists and remove it from others."
11064   ;; Make the article expirable.
11065   (let ((mark (or mark gnus-del-mark)))
11066     (setq gnus-newsgroup-expirable
11067           (if (= mark gnus-expirable-mark)
11068               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11069             (delq article gnus-newsgroup-expirable)))
11070     ;; Remove from unread and marked lists.
11071     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11072     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11073     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11074     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11075     (push (cons article mark) gnus-newsgroup-reads)
11076     ;; Possibly remove from cache, if that is used.
11077     (when gnus-use-cache
11078       (gnus-cache-enter-remove-article article))
11079     t))
11080
11081 (defun gnus-mark-article-as-unread (article &optional mark)
11082   "Enter ARTICLE in the pertinent lists and remove it from others."
11083   (let ((mark (or mark gnus-ticked-mark)))
11084     (if (<= article 0)
11085         (progn
11086           (gnus-error 1 "Can't mark negative article numbers")
11087           nil)
11088       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11089             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11090             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11091             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11092             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11093
11094       ;; Unsuppress duplicates?
11095       (when gnus-suppress-duplicates
11096         (gnus-dup-unsuppress-article article))
11097
11098       (cond ((= mark gnus-ticked-mark)
11099              (setq gnus-newsgroup-marked
11100                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11101             ((= mark gnus-spam-mark)
11102              (setq gnus-newsgroup-spam-marked
11103                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11104                                             article)))
11105             ((= mark gnus-dormant-mark)
11106              (setq gnus-newsgroup-dormant
11107                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11108             (t
11109              (setq gnus-newsgroup-unreads
11110                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11111       (gnus-alist-pull article gnus-newsgroup-reads)
11112       t)))
11113
11114 (defun gnus-summary-tick-article-forward (n)
11115   "Tick N articles forwards.
11116 If N is negative, tick backwards instead.
11117 The difference between N and the number of articles ticked is returned."
11118   (interactive "p")
11119   (gnus-summary-mark-forward n gnus-ticked-mark))
11120
11121 (defun gnus-summary-tick-article-backward (n)
11122   "Tick N articles backwards.
11123 The difference between N and the number of articles ticked is returned."
11124   (interactive "p")
11125   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11126
11127 (defun gnus-summary-tick-article (&optional article clear-mark)
11128   "Mark current article as unread.
11129 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11130 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11131   (interactive)
11132   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11133                                        gnus-ticked-mark)))
11134
11135 (defun gnus-summary-mark-as-read-forward (n)
11136   "Mark N articles as read forwards.
11137 If N is negative, mark backwards instead.
11138 The difference between N and the actual number of articles marked is
11139 returned."
11140   (interactive "p")
11141   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11142
11143 (defun gnus-summary-mark-as-read-backward (n)
11144   "Mark the N articles as read backwards.
11145 The difference between N and the actual number of articles marked is
11146 returned."
11147   (interactive "p")
11148   (gnus-summary-mark-forward
11149    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11150
11151 (defun gnus-summary-mark-as-read (&optional article mark)
11152   "Mark current article as read.
11153 ARTICLE specifies the article to be marked as read.
11154 MARK specifies a string to be inserted at the beginning of the line."
11155   (gnus-summary-mark-article article mark))
11156
11157 (defun gnus-summary-clear-mark-forward (n)
11158   "Clear marks from N articles forward.
11159 If N is negative, clear backward instead.
11160 The difference between N and the number of marks cleared is returned."
11161   (interactive "p")
11162   (gnus-summary-mark-forward n gnus-unread-mark))
11163
11164 (defun gnus-summary-clear-mark-backward (n)
11165   "Clear marks from N articles backward.
11166 The difference between N and the number of marks cleared is returned."
11167   (interactive "p")
11168   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11169
11170 (defun gnus-summary-mark-unread-as-read ()
11171   "Intended to be used by `gnus-mark-article-hook'."
11172   (when (memq gnus-current-article gnus-newsgroup-unreads)
11173     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11174
11175 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11176   "Intended to be used by `gnus-mark-article-hook'."
11177   (let ((mark (gnus-summary-article-mark)))
11178     (when (or (gnus-unread-mark-p mark)
11179               (gnus-read-mark-p mark))
11180       (gnus-summary-mark-article gnus-current-article
11181                                  (or new-mark gnus-read-mark)))))
11182
11183 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11184   "Intended to be used by `gnus-mark-article-hook'."
11185   (let ((mark (gnus-summary-article-mark)))
11186     (when (or (gnus-unread-mark-p mark)
11187               (gnus-read-mark-p mark))
11188       (gnus-summary-mark-article (gnus-summary-article-number)
11189                                  (or new-mark gnus-read-mark)))))
11190
11191 (defun gnus-summary-mark-unread-as-ticked ()
11192   "Intended to be used by `gnus-mark-article-hook'."
11193   (when (memq gnus-current-article gnus-newsgroup-unreads)
11194     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11195
11196 (defun gnus-summary-mark-region-as-read (point mark all)
11197   "Mark all unread articles between point and mark as read.
11198 If given a prefix, mark all articles between point and mark as read,
11199 even ticked and dormant ones."
11200   (interactive "r\nP")
11201   (save-excursion
11202     (let (article)
11203       (goto-char point)
11204       (beginning-of-line)
11205       (while (and
11206               (< (point) mark)
11207               (progn
11208                 (when (or all
11209                           (memq (setq article (gnus-summary-article-number))
11210                                 gnus-newsgroup-unreads))
11211                   (gnus-summary-mark-article article gnus-del-mark))
11212                 t)
11213               (gnus-summary-find-next))))))
11214
11215 (defun gnus-summary-mark-below (score mark)
11216   "Mark articles with score less than SCORE with MARK."
11217   (interactive "P\ncMark: ")
11218   (setq score (if score
11219                   (prefix-numeric-value score)
11220                 (or gnus-summary-default-score 0)))
11221   (with-current-buffer gnus-summary-buffer
11222     (goto-char (point-min))
11223     (while
11224         (progn
11225           (and (< (gnus-summary-article-score) score)
11226                (gnus-summary-mark-article nil mark))
11227           (gnus-summary-find-next)))))
11228
11229 (defun gnus-summary-kill-below (&optional score)
11230   "Mark articles with score below SCORE as read."
11231   (interactive "P")
11232   (gnus-summary-mark-below score gnus-killed-mark))
11233
11234 (defun gnus-summary-clear-above (&optional score)
11235   "Clear all marks from articles with score above SCORE."
11236   (interactive "P")
11237   (gnus-summary-mark-above score gnus-unread-mark))
11238
11239 (defun gnus-summary-tick-above (&optional score)
11240   "Tick all articles with score above SCORE."
11241   (interactive "P")
11242   (gnus-summary-mark-above score gnus-ticked-mark))
11243
11244 (defun gnus-summary-mark-above (score mark)
11245   "Mark articles with score over SCORE with MARK."
11246   (interactive "P\ncMark: ")
11247   (setq score (if score
11248                   (prefix-numeric-value score)
11249                 (or gnus-summary-default-score 0)))
11250   (with-current-buffer gnus-summary-buffer
11251     (goto-char (point-min))
11252     (while (and (progn
11253                   (when (> (gnus-summary-article-score) score)
11254                     (gnus-summary-mark-article nil mark))
11255                   t)
11256                 (gnus-summary-find-next)))))
11257
11258 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11259 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11260 (defun gnus-summary-limit-include-expunged (&optional no-error)
11261   "Display all the hidden articles that were expunged for low scores."
11262   (interactive)
11263   (let ((inhibit-read-only t))
11264     (let ((scored gnus-newsgroup-scored)
11265           headers h)
11266       (while scored
11267         (unless (gnus-summary-article-header (caar scored))
11268           (and (setq h (gnus-number-to-header (caar scored)))
11269                (< (cdar scored) gnus-summary-expunge-below)
11270                (push h headers)))
11271         (setq scored (cdr scored)))
11272       (if (not headers)
11273           (when (not no-error)
11274             (error "No expunged articles hidden"))
11275         (goto-char (point-min))
11276         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11277         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11278         (dolist (x headers)
11279           (push (mail-header-number x) gnus-newsgroup-limit))
11280         (gnus-summary-prepare-unthreaded (nreverse headers))
11281         (goto-char (point-min))
11282         (gnus-summary-position-point)
11283         t))))
11284
11285 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11286   "Mark all unread articles in this newsgroup as read.
11287 If prefix argument ALL is non-nil, ticked and dormant articles will
11288 also be marked as read.
11289 If QUIETLY is non-nil, no questions will be asked.
11290
11291 If TO-HERE is non-nil, it should be a point in the buffer.  All
11292 articles before (after, if REVERSE is set) this point will be marked
11293 as read.
11294
11295 Note that this function will only catch up the unread article
11296 in the current summary buffer limitation.
11297
11298 The number of articles marked as read is returned."
11299   (interactive "P")
11300   (prog1
11301       (save-excursion
11302         (when (or quietly
11303                   (not gnus-interactive-catchup) ;Without confirmation?
11304                   gnus-expert-user
11305                   (gnus-y-or-n-p
11306                    (if all
11307                        "Mark absolutely all articles as read? "
11308                      "Mark all unread articles as read? ")))
11309           (if (and not-mark
11310                    (not gnus-newsgroup-adaptive)
11311                    (not gnus-newsgroup-auto-expire)
11312                    (not gnus-suppress-duplicates)
11313                    (or (not gnus-use-cache)
11314                        (eq gnus-use-cache 'passive)))
11315               (progn
11316                 (when all
11317                   (setq gnus-newsgroup-marked nil
11318                         gnus-newsgroup-spam-marked nil
11319                         gnus-newsgroup-dormant nil))
11320                 (setq gnus-newsgroup-unreads
11321                       (gnus-sorted-nunion
11322                        (gnus-sorted-intersection gnus-newsgroup-unreads
11323                                                  gnus-newsgroup-downloadable)
11324                        (gnus-sorted-difference gnus-newsgroup-unfetched
11325                                                gnus-newsgroup-cached))))
11326             ;; We actually mark all articles as canceled, which we
11327             ;; have to do when using auto-expiry or adaptive scoring.
11328             (gnus-summary-show-all-threads)
11329             (if (and to-here reverse)
11330                 (progn
11331                   (goto-char to-here)
11332                   (gnus-summary-mark-current-read-and-unread-as-read
11333                    gnus-catchup-mark)
11334                   (while (gnus-summary-find-next (not all))
11335                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11336               (when (gnus-summary-first-subject (not all))
11337                 (while (and
11338                         (if to-here (< (point) to-here) t)
11339                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11340                         (gnus-summary-find-next (not all))))))
11341             (gnus-set-mode-line 'summary))
11342           t))
11343     (gnus-summary-position-point)))
11344
11345 (defun gnus-summary-catchup-to-here (&optional all)
11346   "Mark all unticked articles before the current one as read.
11347 If ALL is non-nil, also mark ticked and dormant articles as read."
11348   (interactive "P")
11349   (save-excursion
11350     (gnus-save-hidden-threads
11351       (let ((beg (point)))
11352         ;; We check that there are unread articles.
11353         (when (or all (gnus-summary-find-prev))
11354           (gnus-summary-catchup all t beg)))))
11355   (gnus-summary-position-point))
11356
11357 (defun gnus-summary-catchup-from-here (&optional all)
11358   "Mark all unticked articles after (and including) the current one as read.
11359 If ALL is non-nil, also mark ticked and dormant articles as read."
11360   (interactive "P")
11361   (save-excursion
11362     (gnus-save-hidden-threads
11363       (let ((beg (point)))
11364         ;; We check that there are unread articles.
11365         (when (or all (gnus-summary-find-next))
11366           (gnus-summary-catchup all t beg nil t)))))
11367   (gnus-summary-position-point))
11368
11369 (defun gnus-summary-catchup-all (&optional quietly)
11370   "Mark all articles in this newsgroup as read.
11371 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11372 instead, which marks only unread articles as read."
11373   (interactive "P")
11374   (gnus-summary-catchup t quietly))
11375
11376 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11377   "Mark all unread articles in this group as read, then exit.
11378 If prefix argument ALL is non-nil, all articles are marked as read.
11379 If QUIETLY is non-nil, no questions will be asked."
11380   (interactive "P")
11381   (when (gnus-summary-catchup all quietly nil 'fast)
11382     ;; Select next newsgroup or exit.
11383     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11384              (eq gnus-auto-select-next 'quietly))
11385         (gnus-summary-next-group nil)
11386       (gnus-summary-exit))))
11387
11388 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11389   "Mark all articles in this newsgroup as read, and then exit.
11390 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11391 instead, which marks only unread articles as read."
11392   (interactive "P")
11393   (gnus-summary-catchup-and-exit t quietly))
11394
11395 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11396   "Mark all articles in this group as read and select the next group.
11397 If given a prefix, mark all articles, unread as well as ticked, as
11398 read."
11399   (interactive "P")
11400   (save-excursion
11401     (gnus-summary-catchup all))
11402   (gnus-summary-next-group))
11403
11404 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11405   "Mark all articles in this group as read and select the previous group.
11406 If given a prefix, mark all articles, unread as well as ticked, as
11407 read."
11408   (interactive "P")
11409   (save-excursion
11410     (gnus-summary-catchup all))
11411   (gnus-summary-next-group nil nil t))
11412
11413 ;;;
11414 ;;; with article
11415 ;;;
11416
11417 (defmacro gnus-with-article (article &rest forms)
11418   "Select ARTICLE and perform FORMS in the original article buffer.
11419 Then replace the article with the result."
11420   `(progn
11421      ;; We don't want the article to be marked as read.
11422      (let (gnus-mark-article-hook)
11423        (gnus-summary-select-article t t nil ,article))
11424      (set-buffer gnus-original-article-buffer)
11425      ,@forms
11426      (if (not (gnus-check-backend-function
11427                'request-replace-article (car gnus-article-current)))
11428          (gnus-message 5 "Read-only group; not replacing")
11429        (unless (gnus-request-replace-article
11430                 ,article (car gnus-article-current)
11431                 (current-buffer) t)
11432          (error "Couldn't replace article")))
11433      ;; The cache and backlog have to be flushed somewhat.
11434      (when gnus-keep-backlog
11435        (gnus-backlog-remove-article
11436         (car gnus-article-current) (cdr gnus-article-current)))
11437      (when gnus-use-cache
11438        (gnus-cache-update-article
11439         (car gnus-article-current) (cdr gnus-article-current)))))
11440
11441 (put 'gnus-with-article 'lisp-indent-function 1)
11442 (put 'gnus-with-article 'edebug-form-spec '(form body))
11443
11444 ;; Thread-based commands.
11445
11446 (defun gnus-summary-articles-in-thread (&optional article)
11447   "Return a list of all articles in the current thread.
11448 If ARTICLE is non-nil, return all articles in the thread that starts
11449 with that article."
11450   (let* ((article (or article (gnus-summary-article-number)))
11451          (data (gnus-data-find-list article))
11452          (top-level (gnus-data-level (car data)))
11453          (top-subject
11454           (cond ((null gnus-thread-operation-ignore-subject)
11455                  (gnus-simplify-subject-re
11456                   (mail-header-subject (gnus-data-header (car data)))))
11457                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11458                  (gnus-simplify-subject-fuzzy
11459                   (mail-header-subject (gnus-data-header (car data)))))
11460                 (t nil)))
11461          (end-point (save-excursion
11462                       (goto-char (gnus-data-pos (car data)))
11463                       (if (gnus-summary-go-to-next-thread)
11464                           (point) (point-max))))
11465          articles)
11466     (while (and data
11467                 (< (gnus-data-pos (car data)) end-point))
11468       (when (or (not top-subject)
11469                 (string= top-subject
11470                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11471                              (gnus-simplify-subject-fuzzy
11472                               (mail-header-subject
11473                                (gnus-data-header (car data))))
11474                            (gnus-simplify-subject-re
11475                             (mail-header-subject
11476                              (gnus-data-header (car data)))))))
11477         (push (gnus-data-number (car data)) articles))
11478       (unless (and (setq data (cdr data))
11479                    (> (gnus-data-level (car data)) top-level))
11480         (setq data nil)))
11481     ;; Return the list of articles.
11482     (nreverse articles)))
11483
11484 (defun gnus-summary-rethread-current ()
11485   "Rethread the thread the current article is part of."
11486   (interactive)
11487   (let* ((gnus-show-threads t)
11488          (article (gnus-summary-article-number))
11489          (id (mail-header-id (gnus-summary-article-header)))
11490          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11491     (unless id
11492       (error "No article on the current line"))
11493     (gnus-rebuild-thread id)
11494     (gnus-summary-goto-subject article)))
11495
11496 (defun gnus-summary-reparent-thread ()
11497   "Make the current article child of the marked (or previous) article.
11498
11499 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11500 is non-nil or the Subject: of both articles are the same."
11501   (interactive)
11502   (unless (not (gnus-group-read-only-p))
11503     (error "The current newsgroup does not support article editing"))
11504   (unless (<= (length gnus-newsgroup-processable) 1)
11505     (error "No more than one article may be marked"))
11506   (let ((child (gnus-summary-article-number))
11507         ;; First grab the marked article, otherwise one line up.
11508         (parent (if (not (null gnus-newsgroup-processable))
11509                     (car gnus-newsgroup-processable)
11510                   (save-excursion
11511                     (if (eq (forward-line -1) 0)
11512                         (gnus-summary-article-number)
11513                       (error "Beginning of summary buffer"))))))
11514     (gnus-summary-reparent-children parent (list child))))
11515
11516 (defun gnus-summary-reparent-children (parent children)
11517   "Make PARENT the parent of CHILDREN.
11518 When called interactively, PARENT is the current article and CHILDREN
11519 are the process-marked articles."
11520   (interactive
11521    (list (gnus-summary-article-number)
11522          (gnus-summary-work-articles nil)))
11523   (dolist (child children)
11524     (save-window-excursion
11525       (let ((gnus-article-buffer " *reparent*"))
11526         (unless (not (eq parent child))
11527           (error "An article may not be self-referential"))
11528         (let ((message-id (mail-header-id
11529                            (gnus-summary-article-header parent))))
11530           (unless (and message-id (not (equal message-id "")))
11531             (error "No message-id in desired parent"))
11532           (gnus-with-article child
11533             (save-restriction
11534               (goto-char (point-min))
11535               (message-narrow-to-head)
11536               (if (re-search-forward "^References: " nil t)
11537                   (progn
11538                     (re-search-forward "^[^ \t]" nil t)
11539                     (forward-line -1)
11540                     (end-of-line)
11541                     (insert " " message-id))
11542                 (insert "References: " message-id "\n"))))
11543           (set-buffer gnus-summary-buffer)
11544           (gnus-summary-unmark-all-processable)
11545           (gnus-summary-update-article child)
11546           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11547             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11548           (gnus-summary-rethread-current)
11549           (gnus-message 3 "Article %d is now the child of article %d"
11550                         child parent))))))
11551
11552 (defun gnus-summary-toggle-threads (&optional arg)
11553   "Toggle showing conversation threads.
11554 If ARG is positive number, turn showing conversation threads on."
11555   (interactive "P")
11556   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11557     (setq gnus-show-threads
11558           (if (null arg) (not gnus-show-threads)
11559             (> (prefix-numeric-value arg) 0)))
11560     (gnus-summary-prepare)
11561     (gnus-summary-goto-subject current)
11562     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11563     (gnus-summary-position-point)))
11564
11565 (eval-and-compile
11566   (if (fboundp 'remove-overlays)
11567       (defalias 'gnus-remove-overlays 'remove-overlays)
11568     (defun gnus-remove-overlays (beg end name val)
11569       "Clear BEG and END of overlays whose property NAME has value VAL.
11570 For compatibility with XEmacs."
11571       (dolist (ov (gnus-overlays-in beg end))
11572         (when (eq (gnus-overlay-get ov name) val)
11573           (gnus-delete-overlay ov))))))
11574
11575 (defun gnus-summary-show-all-threads ()
11576   "Show all threads."
11577   (interactive)
11578   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11579   (gnus-summary-position-point))
11580
11581 (defsubst gnus-summary--inv (p)
11582   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11583
11584 (defun gnus-summary-show-thread ()
11585   "Show thread subtrees.
11586 Returns nil if no thread was there to be shown."
11587   (interactive)
11588   (let* ((orig (point))
11589          (end (point-at-eol))
11590          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11591          ;; Leave point at bol
11592          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11593          (eoi (when end
11594                 (if (fboundp 'next-single-char-property-change)
11595                     (next-single-char-property-change end 'invisible)
11596                   (while (progn
11597                            (end-of-line 2)
11598                            (and (not (eobp))
11599                                 (eq (get-char-property (point) 'invisible)
11600                                     'gnus-sum))))
11601                   (point)))))
11602     (when eoi
11603       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11604       (goto-char orig)
11605       (gnus-summary-position-point)
11606       eoi)))
11607
11608 (defun gnus-summary-maybe-hide-threads ()
11609   "If requested, hide the threads that should be hidden."
11610   (when (and gnus-show-threads
11611              gnus-thread-hide-subtree)
11612     (gnus-summary-hide-all-threads
11613      (if (or (consp gnus-thread-hide-subtree)
11614              (functionp gnus-thread-hide-subtree))
11615          (gnus-make-predicate gnus-thread-hide-subtree)
11616        nil))))
11617
11618 ;;; Hiding predicates.
11619
11620 (defun gnus-article-unread-p (header)
11621   (memq (mail-header-number header) gnus-newsgroup-unreads))
11622
11623 (defun gnus-article-unseen-p (header)
11624   (memq (mail-header-number header) gnus-newsgroup-unseen))
11625
11626 (defun gnus-map-articles (predicate articles)
11627   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11628   (apply 'gnus-or (mapcar predicate
11629                           (mapcar (lambda (number)
11630                                     (gnus-summary-article-header number))
11631                                   articles))))
11632
11633 (defun gnus-summary-hide-all-threads (&optional predicate)
11634   "Hide all thread subtrees.
11635 If PREDICATE is supplied, threads that satisfy this predicate
11636 will not be hidden."
11637   (interactive)
11638   (save-excursion
11639     (goto-char (point-min))
11640     (let ((end nil)
11641           (count 0))
11642       (while (not end)
11643         (incf count)
11644         (when (zerop (mod count 1000))
11645           (message "Hiding all threads... %d" count))
11646         (when (or (not predicate)
11647                   (gnus-map-articles
11648                    predicate (gnus-summary-article-children)))
11649             (gnus-summary-hide-thread))
11650         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11651   (gnus-summary-position-point))
11652
11653 (defun gnus-summary-hide-thread ()
11654   "Hide thread subtrees.
11655 If PREDICATE is supplied, threads that satisfy this predicate
11656 will not be hidden.
11657 Returns nil if no threads were there to be hidden."
11658   (interactive)
11659   (let ((start (point))
11660         (starteol (line-end-position))
11661         (article (gnus-summary-article-number)))
11662     (goto-char start)
11663     ;; Go forward until either the buffer ends or the subthread ends.
11664     (when (and (not (eobp))
11665                (or (zerop (gnus-summary-next-thread 1 t))
11666                    (goto-char (point-max))))
11667       (if (and (> (point) start)
11668                ;; FIXME: this should actually search for a non-invisible \n.
11669                (search-backward "\n" start t))
11670           (progn
11671             (when (> (point) starteol)
11672               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11673               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11674                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11675                 (gnus-overlay-put ol 'evaporate t)))
11676             (gnus-summary-goto-subject article)
11677             (when (> start (point))
11678               (message "Hiding the thread moved us backwards, aborting!")
11679               (goto-char (point-max))))
11680         (goto-char start)
11681         nil))))
11682
11683 (defun gnus-summary-go-to-next-thread (&optional previous)
11684   "Go to the same level (or less) next thread.
11685 If PREVIOUS is non-nil, go to previous thread instead.
11686 Return the article number moved to, or nil if moving was impossible."
11687   (let ((level (gnus-summary-thread-level))
11688         (way (if previous -1 1))
11689         (beg (point)))
11690     (forward-line way)
11691     (while (and (not (eobp))
11692                 (< level (gnus-summary-thread-level)))
11693       (forward-line way))
11694     (if (eobp)
11695         (progn
11696           (goto-char beg)
11697           nil)
11698       (setq beg (point))
11699       (prog1
11700           (gnus-summary-article-number)
11701         (goto-char beg)))))
11702
11703 (defun gnus-summary-next-thread (n &optional silent)
11704   "Go to the same level next N'th thread.
11705 If N is negative, search backward instead.
11706 Returns the difference between N and the number of skips actually
11707 done.
11708
11709 If SILENT, don't output messages."
11710   (interactive "p")
11711   (let ((backward (< n 0))
11712         (n (abs n)))
11713     (while (and (> n 0)
11714                 (gnus-summary-go-to-next-thread backward))
11715       (decf n))
11716     (unless silent
11717       (gnus-summary-position-point))
11718     (when (and (not silent) (/= 0 n))
11719       (gnus-message 7 "No more threads"))
11720     n))
11721
11722 (defun gnus-summary-prev-thread (n)
11723   "Go to the same level previous N'th thread.
11724 Returns the difference between N and the number of skips actually
11725 done."
11726   (interactive "p")
11727   (gnus-summary-next-thread (- n)))
11728
11729 (defun gnus-summary-go-down-thread ()
11730   "Go down one level in the current thread."
11731   (let ((children (gnus-summary-article-children)))
11732     (when children
11733       (gnus-summary-goto-subject (car children)))))
11734
11735 (defun gnus-summary-go-up-thread ()
11736   "Go up one level in the current thread."
11737   (let ((parent (gnus-summary-article-parent)))
11738     (when parent
11739       (gnus-summary-goto-subject parent))))
11740
11741 (defun gnus-summary-down-thread (n)
11742   "Go down thread N steps.
11743 If N is negative, go up instead.
11744 Returns the difference between N and how many steps down that were
11745 taken."
11746   (interactive "p")
11747   (let ((up (< n 0))
11748         (n (abs n)))
11749     (while (and (> n 0)
11750                 (if up (gnus-summary-go-up-thread)
11751                   (gnus-summary-go-down-thread)))
11752       (setq n (1- n)))
11753     (gnus-summary-position-point)
11754     (when (/= 0 n)
11755       (gnus-message 7 "Can't go further"))
11756     n))
11757
11758 (defun gnus-summary-up-thread (n)
11759   "Go up thread N steps.
11760 If N is negative, go down instead.
11761 Returns the difference between N and how many steps down that were
11762 taken."
11763   (interactive "p")
11764   (gnus-summary-down-thread (- n)))
11765
11766 (defun gnus-summary-top-thread ()
11767   "Go to the top of the thread."
11768   (interactive)
11769   (while (gnus-summary-go-up-thread))
11770   (gnus-summary-article-number))
11771
11772 (defun gnus-summary-expire-thread ()
11773   "Mark articles under current thread as expired."
11774   (interactive)
11775   (gnus-summary-kill-thread 0))
11776
11777 (defun gnus-summary-kill-thread (&optional unmark)
11778   "Mark articles under current thread as read.
11779 If the prefix argument is positive, remove any kinds of marks.
11780 If the prefix argument is zero, mark thread as expired.
11781 If the prefix argument is negative, tick articles instead."
11782   (interactive "P")
11783   (when unmark
11784     (setq unmark (prefix-numeric-value unmark)))
11785   (let ((articles (gnus-summary-articles-in-thread))
11786         (hide (or (null unmark) (= unmark 0))))
11787     (save-excursion
11788       ;; Expand the thread.
11789       (gnus-summary-show-thread)
11790       ;; Mark all the articles.
11791       (while articles
11792         (gnus-summary-goto-subject (car articles))
11793         (cond ((null unmark)
11794                (gnus-summary-mark-article-as-read gnus-killed-mark))
11795               ((> unmark 0)
11796                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11797               ((= unmark 0)
11798                (gnus-summary-mark-article nil gnus-expirable-mark))
11799               (t
11800                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11801         (setq articles (cdr articles))))
11802     ;; Hide killed subtrees when hide is true.
11803     (and hide
11804          gnus-thread-hide-killed
11805          (gnus-summary-hide-thread))
11806     ;; If hide is t, go to next unread subject.
11807     (when hide
11808       ;; Go to next unread subject.
11809       (gnus-summary-next-subject 1 t)))
11810   (gnus-set-mode-line 'summary))
11811
11812 ;; Summary sorting commands
11813
11814 (defun gnus-summary-sort-by-number (&optional reverse)
11815   "Sort the summary buffer by article number.
11816 Argument REVERSE means reverse order."
11817   (interactive "P")
11818   (gnus-summary-sort 'number reverse))
11819
11820 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11821   "Sort the summary buffer by most recent article number.
11822 Argument REVERSE means reverse order."
11823   (interactive "P")
11824   (gnus-summary-sort 'most-recent-number reverse))
11825
11826 (defun gnus-summary-sort-by-random (&optional reverse)
11827   "Randomize the order in the summary buffer.
11828 Argument REVERSE means to randomize in reverse order."
11829   (interactive "P")
11830   (gnus-summary-sort 'random reverse))
11831
11832 (defun gnus-summary-sort-by-author (&optional reverse)
11833   "Sort the summary buffer by author name alphabetically.
11834 If `case-fold-search' is non-nil, case of letters is ignored.
11835 Argument REVERSE means reverse order."
11836   (interactive "P")
11837   (gnus-summary-sort 'author reverse))
11838
11839 (defun gnus-summary-sort-by-recipient (&optional reverse)
11840   "Sort the summary buffer by recipient name alphabetically.
11841 If `case-fold-search' is non-nil, case of letters is ignored.
11842 Argument REVERSE means reverse order."
11843   (interactive "P")
11844   (gnus-summary-sort 'recipient reverse))
11845
11846 (defun gnus-summary-sort-by-subject (&optional reverse)
11847   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11848 If `case-fold-search' is non-nil, case of letters is ignored.
11849 Argument REVERSE means reverse order."
11850   (interactive "P")
11851   (gnus-summary-sort 'subject reverse))
11852
11853 (defun gnus-summary-sort-by-date (&optional reverse)
11854   "Sort the summary buffer by date.
11855 Argument REVERSE means reverse order."
11856   (interactive "P")
11857   (gnus-summary-sort 'date reverse))
11858
11859 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11860   "Sort the summary buffer by most recent date.
11861 Argument REVERSE means reverse order."
11862   (interactive "P")
11863   (gnus-summary-sort 'most-recent-date reverse))
11864
11865 (defun gnus-summary-sort-by-score (&optional reverse)
11866   "Sort the summary buffer by score.
11867 Argument REVERSE means reverse order."
11868   (interactive "P")
11869   (gnus-summary-sort 'score reverse))
11870
11871 (defun gnus-summary-sort-by-lines (&optional reverse)
11872   "Sort the summary buffer by the number of lines.
11873 Argument REVERSE means reverse order."
11874   (interactive "P")
11875   (gnus-summary-sort 'lines reverse))
11876
11877 (defun gnus-summary-sort-by-chars (&optional reverse)
11878   "Sort the summary buffer by article length.
11879 Argument REVERSE means reverse order."
11880   (interactive "P")
11881   (gnus-summary-sort 'chars reverse))
11882
11883 (defun gnus-summary-sort-by-original (&optional reverse)
11884   "Sort the summary buffer using the default sorting method.
11885 Argument REVERSE means reverse order."
11886   (interactive "P")
11887   (let* ((inhibit-read-only t)
11888          (gnus-summary-prepare-hook nil))
11889     ;; We do the sorting by regenerating the threads.
11890     (gnus-summary-prepare)
11891     ;; Hide subthreads if needed.
11892     (gnus-summary-maybe-hide-threads)))
11893
11894 (defun gnus-summary-sort (predicate reverse)
11895   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11896   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11897          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11898          (gnus-thread-sort-functions
11899           (if (not reverse)
11900               thread
11901             `(lambda (t1 t2)
11902                (,thread t2 t1))))
11903          (gnus-sort-gathered-threads-function
11904           gnus-thread-sort-functions)
11905          (gnus-article-sort-functions
11906           (if (not reverse)
11907               article
11908             `(lambda (t1 t2)
11909                (,article t2 t1))))
11910          (inhibit-read-only t)
11911          (gnus-summary-prepare-hook nil))
11912     ;; We do the sorting by regenerating the threads.
11913     (gnus-summary-prepare)
11914     ;; Hide subthreads if needed.
11915     (gnus-summary-maybe-hide-threads)))
11916
11917 ;; Summary saving commands.
11918
11919 (defun gnus-summary-save-article (&optional n not-saved)
11920   "Save the current article using the default saver function.
11921 If N is a positive number, save the N next articles.
11922 If N is a negative number, save the N previous articles.
11923 If N is nil and any articles have been marked with the process mark,
11924 save those articles instead.
11925 The variable `gnus-default-article-saver' specifies the saver function.
11926
11927 If the optional second argument NOT-SAVED is non-nil, articles saved
11928 will not be marked as saved."
11929   (interactive "P")
11930   (require 'gnus-art)
11931   (let* ((articles (gnus-summary-work-articles n))
11932          (save-buffer (save-excursion
11933                         (nnheader-set-temp-buffer " *Gnus Save*")))
11934          (num (length articles))
11935          ;; Whether to save decoded articles or raw articles.
11936          (decode (when gnus-article-save-coding-system
11937                    (get gnus-default-article-saver :decode)))
11938          ;; When saving many articles in a single file, use the other
11939          ;; function to save articles other than the first one.
11940          (saver2 (get gnus-default-article-saver :function))
11941          (gnus-prompt-before-saving (if saver2
11942                                         t
11943                                       gnus-prompt-before-saving))
11944          (gnus-default-article-saver gnus-default-article-saver)
11945          header file)
11946     (dolist (article articles)
11947       (setq header (gnus-summary-article-header article))
11948       (if (not (vectorp header))
11949           ;; This is a pseudo-article.
11950           (if (assq 'name header)
11951               (gnus-copy-file (cdr (assq 'name header)))
11952             (gnus-message 1 "Article %d is unsavable" article))
11953         ;; This is a real article.
11954         (save-window-excursion
11955           (gnus-summary-select-article decode decode nil article)
11956           (gnus-summary-goto-subject article))
11957         (with-current-buffer save-buffer
11958           (erase-buffer)
11959           (insert-buffer-substring (if decode
11960                                        gnus-article-buffer
11961                                      gnus-original-article-buffer)))
11962         (setq file (gnus-article-save save-buffer file num))
11963         (gnus-summary-remove-process-mark article)
11964         (unless not-saved
11965           (gnus-summary-set-saved-mark article)))
11966       (when saver2
11967         (setq gnus-default-article-saver saver2
11968               saver2 nil)))
11969     (gnus-kill-buffer save-buffer)
11970     (gnus-summary-position-point)
11971     (gnus-set-mode-line 'summary)
11972     n))
11973
11974 (defun gnus-summary-pipe-output (&optional n sym)
11975   "Pipe the current article to a subprocess.
11976 If N is a positive number, pipe the N next articles.
11977 If N is a negative number, pipe the N previous articles.
11978 If N is nil and any articles have been marked with the process mark,
11979 pipe those articles instead.
11980 The default command to which articles are piped is specified by the
11981 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11982 will be prompted for the command.
11983
11984 The properties `:decode' and `:headers' that are put to the function
11985 symbol `gnus-summary-save-in-pipe' control whether this function
11986 decodes articles and what headers to keep (see the doc string for the
11987 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11988 is neither omitted nor the symbol `r', force including all headers
11989 regardless of the `:headers' property.  If it is the symbol `r',
11990 articles that are not decoded and include all headers will be piped
11991 no matter what the properties `:decode' and `:headers' are."
11992   (interactive (gnus-interactive "P\ny"))
11993   (require 'gnus-art)
11994   (let* ((articles (gnus-summary-work-articles n))
11995          (result-buffer "*Shell Command Output*")
11996          (all-headers (not (memq sym '(nil r))))
11997          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11998          (raw (eq sym 'r))
11999          (headers (get 'gnus-summary-save-in-pipe :headers))
12000          command result)
12001     (unless (numberp (car articles))
12002       (error "No article to pipe"))
12003     (setq command (gnus-read-shell-command
12004                    (concat "Shell command on "
12005                            (if (cdr articles)
12006                                (format "these %d articles" (length articles))
12007                              "this article")
12008                            ": ")
12009                    gnus-summary-pipe-output-default-command))
12010     (when (string-equal command "")
12011       (error "A command is required"))
12012     (when all-headers
12013       (put 'gnus-summary-save-in-pipe :headers nil))
12014     (unwind-protect
12015         (while articles
12016           (gnus-summary-goto-subject (pop articles))
12017           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12018           (when (and (get-buffer result-buffer)
12019                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12020             (setq result (concat result (with-current-buffer result-buffer
12021                                           (buffer-string))))))
12022       (put 'gnus-summary-save-in-pipe :headers headers))
12023     (unless (zerop (length result))
12024       (if (with-current-buffer (get-buffer-create result-buffer)
12025             (erase-buffer)
12026             (insert result)
12027             (prog1
12028                 (and (= (count-lines (point-min) (point)) 1)
12029                      (progn
12030                        (end-of-line 0)
12031                        (<= (current-column)
12032                            (window-width (minibuffer-window)))))
12033               (goto-char (point-min))))
12034           (message "%s" (substring result 0 -1))
12035         (message nil)
12036         (gnus-configure-windows 'pipe)))))
12037
12038 (defun gnus-summary-save-article-mail (&optional arg)
12039   "Append the current article to a Unix mail box file.
12040 If N is a positive number, save the N next articles.
12041 If N is a negative number, save the N previous articles.
12042 If N is nil and any articles have been marked with the process mark,
12043 save those articles instead."
12044   (interactive "P")
12045   (require 'gnus-art)
12046   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12047     (gnus-summary-save-article arg)))
12048
12049 (defun gnus-summary-save-article-rmail (&optional arg)
12050   "Append the current article to an rmail file.
12051 If N is a positive number, save the N next articles.
12052 If N is a negative number, save the N previous articles.
12053 If N is nil and any articles have been marked with the process mark,
12054 save those articles instead."
12055   (interactive "P")
12056   (require 'gnus-art)
12057   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12058     (gnus-summary-save-article arg)))
12059
12060 (defun gnus-summary-save-article-file (&optional arg)
12061   "Append the current article to a file.
12062 If N is a positive number, save the N next articles.
12063 If N is a negative number, save the N previous articles.
12064 If N is nil and any articles have been marked with the process mark,
12065 save those articles instead."
12066   (interactive "P")
12067   (require 'gnus-art)
12068   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12069     (gnus-summary-save-article arg)))
12070
12071 (defun gnus-summary-write-article-file (&optional arg)
12072   "Write the current article to a file, deleting the previous file.
12073 If N is a positive number, save the N next articles.
12074 If N is a negative number, save the N previous articles.
12075 If N is nil and any articles have been marked with the process mark,
12076 save those articles instead."
12077   (interactive "P")
12078   (require 'gnus-art)
12079   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12080     (gnus-summary-save-article arg)))
12081
12082 (defun gnus-summary-save-article-body-file (&optional arg)
12083   "Append the current article body to a file.
12084 If N is a positive number, save the N next articles.
12085 If N is a negative number, save the N previous articles.
12086 If N is nil and any articles have been marked with the process mark,
12087 save those articles instead."
12088   (interactive "P")
12089   (require 'gnus-art)
12090   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12091     (gnus-summary-save-article arg)))
12092
12093 (defun gnus-summary-write-article-body-file (&optional arg)
12094   "Write the current article body to a file, deleting the previous file.
12095 If N is a positive number, save the N next articles.
12096 If N is a negative number, save the N previous articles.
12097 If N is nil and any articles have been marked with the process mark,
12098 save those articles instead."
12099   (interactive "P")
12100   (require 'gnus-art)
12101   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12102     (gnus-summary-save-article arg)))
12103
12104 (defun gnus-summary-muttprint (&optional arg)
12105   "Print the current article using Muttprint.
12106 If N is a positive number, save the N next articles.
12107 If N is a negative number, save the N previous articles.
12108 If N is nil and any articles have been marked with the process mark,
12109 save those articles instead."
12110   (interactive "P")
12111   (require 'gnus-art)
12112   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12113     (gnus-summary-save-article arg t)))
12114
12115 (defun gnus-summary-pipe-message (program)
12116   "Pipe the current article through PROGRAM."
12117   (interactive "sProgram: ")
12118   (gnus-summary-select-article)
12119   (let ((mail-header-separator ""))
12120     (gnus-eval-in-buffer-window gnus-article-buffer
12121       (save-restriction
12122         (widen)
12123         (let ((start (window-start))
12124               (inhibit-read-only t))
12125           (message-pipe-buffer-body program)
12126           (set-window-start (get-buffer-window (current-buffer)) start))))))
12127
12128 (defun gnus-get-split-value (methods)
12129   "Return a value based on the split METHODS."
12130   (let (split-name method result match)
12131     (when methods
12132       (with-current-buffer gnus-original-article-buffer
12133         (save-restriction
12134           (nnheader-narrow-to-headers)
12135           (while (and methods (not split-name))
12136             (goto-char (point-min))
12137             (setq method (pop methods))
12138             (setq match (car method))
12139             (when (cond
12140                    ((stringp match)
12141                     ;; Regular expression.
12142                     (ignore-errors
12143                       (re-search-forward match nil t)))
12144                    ((functionp match)
12145                     ;; Function.
12146                     (save-restriction
12147                       (widen)
12148                       (setq result (funcall match gnus-newsgroup-name))))
12149                    ((consp match)
12150                     ;; Form.
12151                     (save-restriction
12152                       (widen)
12153                       (setq result (eval match)))))
12154               (setq split-name (cdr method))
12155               (cond ((stringp result)
12156                      (push (expand-file-name
12157                             result gnus-article-save-directory)
12158                            split-name))
12159                     ((consp result)
12160                      (setq split-name (append result split-name)))))))))
12161     (nreverse split-name)))
12162
12163 (defun gnus-valid-move-group-p (group)
12164   (and (symbolp group)
12165        (boundp group)
12166        (symbol-name group)
12167        (symbol-value group)
12168        (gnus-get-function (gnus-find-method-for-group
12169                            (symbol-name group)) 'request-accept-article t)))
12170
12171 (defun gnus-read-move-group-name (prompt default articles prefix)
12172   "Read a group name."
12173   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12174          (minibuffer-confirm-incomplete nil) ; XEmacs
12175          (prom
12176           (format "%s %s to"
12177                   prompt
12178                   (if (> (length articles) 1)
12179                       (format "these %d articles" (length articles))
12180                     "this article")))
12181          (to-newsgroup
12182           (cond
12183            ((null split-name)
12184             (gnus-group-completing-read
12185              prom
12186              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12187              nil prefix nil default))
12188            ((= 1 (length split-name))
12189             (gnus-group-completing-read
12190              prom
12191              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12192              nil prefix 'gnus-group-history (car split-name)))
12193            (t
12194             (gnus-completing-read
12195              prom (nreverse split-name) nil nil 'gnus-group-history))))
12196          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12197          encoded)
12198     (when to-newsgroup
12199       (if (or (string= to-newsgroup "")
12200               (string= to-newsgroup prefix))
12201           (setq to-newsgroup default))
12202       (unless to-newsgroup
12203         (error "No group name entered"))
12204       (setq encoded (mm-encode-coding-string
12205                      to-newsgroup
12206                      (gnus-group-name-charset to-method to-newsgroup)))
12207       (or (gnus-active encoded)
12208           (gnus-activate-group encoded nil nil to-method)
12209           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12210                                      to-newsgroup))
12211               (or (and (gnus-request-create-group encoded to-method)
12212                        (gnus-activate-group encoded nil nil to-method)
12213                        (gnus-subscribe-group encoded))
12214                   (error "Couldn't create group %s" to-newsgroup)))
12215           (error "No such group: %s" to-newsgroup))
12216       encoded)))
12217
12218 (defvar gnus-summary-save-parts-counter)
12219 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12220
12221 (defun gnus-summary-save-parts (type dir n &optional reverse)
12222   "Save parts matching TYPE to DIR.
12223 If REVERSE, save parts that do not match TYPE."
12224   (interactive
12225    (list (read-string "Save parts of type: "
12226                       (or (car gnus-summary-save-parts-type-history)
12227                           gnus-summary-save-parts-default-mime)
12228                       'gnus-summary-save-parts-type-history)
12229          (setq gnus-summary-save-parts-last-directory
12230                (read-directory-name "Save to directory: "
12231                                     gnus-summary-save-parts-last-directory
12232                                     nil t))
12233          current-prefix-arg))
12234   (gnus-summary-iterate n
12235     (let ((gnus-display-mime-function nil)
12236           gnus-article-prepare-hook
12237           gnus-article-decode-hook
12238           gnus-display-mime-function
12239           gnus-break-pages
12240           (gnus-inhibit-treatment t))
12241       (gnus-summary-select-article))
12242     (with-current-buffer gnus-article-buffer
12243       (let ((handles (or gnus-article-mime-handles
12244                          (mm-dissect-buffer nil gnus-article-loose-mime)
12245                          (and gnus-article-emulate-mime
12246                               (mm-uu-dissect))))
12247             (gnus-summary-save-parts-counter 1))
12248         (when handles
12249           (gnus-summary-save-parts-1 type dir handles reverse)
12250           (unless gnus-article-mime-handles ;; Don't destroy this case.
12251             (mm-destroy-parts handles)))))))
12252
12253 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12254   (if (stringp (car handle))
12255       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12256               (cdr handle))
12257     (when (if reverse
12258               (not (string-match type (mm-handle-media-type handle)))
12259             (string-match type (mm-handle-media-type handle)))
12260       (let ((file (expand-file-name
12261                    (gnus-map-function
12262                     mm-file-name-rewrite-functions
12263                     (file-name-nondirectory
12264                      (or
12265                       (mm-handle-filename handle)
12266                       (format "%s.%d.%d" gnus-newsgroup-name
12267                               (cdr gnus-article-current)
12268                               gnus-summary-save-parts-counter))))
12269                    dir)))
12270         (incf gnus-summary-save-parts-counter)
12271         (unless (file-exists-p file)
12272           (mm-save-part-to-file handle file))))))
12273
12274 ;; Summary extract commands
12275
12276 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12277   (let ((inhibit-read-only t)
12278         (article (gnus-summary-article-number))
12279         after-article b e)
12280     (unless (gnus-summary-goto-subject article)
12281       (error "No such article: %d" article))
12282     (gnus-summary-position-point)
12283     ;; If all commands are to be bunched up on one line, we collect
12284     ;; them here.
12285     (unless gnus-view-pseudos-separately
12286       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12287             files action)
12288         (while ps
12289           (setq action (cdr (assq 'action (car ps))))
12290           (setq files (list (cdr (assq 'name (car ps)))))
12291           (while (and ps (cdr ps)
12292                       (string= (or action "1")
12293                                (or (cdr (assq 'action (cadr ps))) "2")))
12294             (push (cdr (assq 'name (cadr ps))) files)
12295             (setcdr ps (cddr ps)))
12296           (when files
12297             (when (not (string-match "%s" action))
12298               (push " " files))
12299             (push " " files)
12300             (when (assq 'execute (car ps))
12301               (setcdr (assq 'execute (car ps))
12302                       (funcall (if (string-match "%s" action)
12303                                    'format 'concat)
12304                                action
12305                                (mapconcat
12306                                 (lambda (f)
12307                                   (if (equal f " ")
12308                                       f
12309                                     (shell-quote-argument f)))
12310                                 files " ")))))
12311           (setq ps (cdr ps)))))
12312     (if (and gnus-view-pseudos (not not-view))
12313         (while pslist
12314           (when (assq 'execute (car pslist))
12315             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12316                                   (eq gnus-view-pseudos 'not-confirm)))
12317           (setq pslist (cdr pslist)))
12318       (save-excursion
12319         (while pslist
12320           (setq after-article (or (cdr (assq 'article (car pslist)))
12321                                   (gnus-summary-article-number)))
12322           (gnus-summary-goto-subject after-article)
12323           (forward-line 1)
12324           (setq b (point))
12325           (insert "    " (file-name-nondirectory
12326                           (cdr (assq 'name (car pslist))))
12327                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12328           (setq e (point))
12329           (forward-line -1)             ; back to `b'
12330           (gnus-add-text-properties
12331            b (1- e) (list 'gnus-number gnus-reffed-article-number
12332                           gnus-mouse-face-prop gnus-mouse-face))
12333           (gnus-data-enter
12334            after-article gnus-reffed-article-number
12335            gnus-unread-mark b (car pslist) 0 (- e b))
12336           (setq gnus-newsgroup-unreads
12337                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12338                                          gnus-reffed-article-number))
12339           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12340           (setq pslist (cdr pslist)))))))
12341
12342 (defun gnus-pseudos< (p1 p2)
12343   (let ((c1 (cdr (assq 'action p1)))
12344         (c2 (cdr (assq 'action p2))))
12345     (and c1 c2 (string< c1 c2))))
12346
12347 (defun gnus-request-pseudo-article (props)
12348   (cond ((assq 'execute props)
12349          (gnus-execute-command (cdr (assq 'execute props)))))
12350   (let ((gnus-current-article (gnus-summary-article-number)))
12351     (gnus-run-hooks 'gnus-mark-article-hook)))
12352
12353 (defun gnus-execute-command (command &optional automatic)
12354   (save-excursion
12355     (gnus-article-setup-buffer)
12356     (set-buffer gnus-article-buffer)
12357     (setq buffer-read-only nil)
12358     (let ((command (if automatic command
12359                      (read-string "Command: " (cons command 0)))))
12360       (erase-buffer)
12361       (insert "$ " command "\n\n")
12362       (if gnus-view-pseudo-asynchronously
12363           (start-process "gnus-execute" (current-buffer) shell-file-name
12364                          shell-command-switch command)
12365         (call-process shell-file-name nil t nil
12366                       shell-command-switch command)))))
12367
12368 ;; Summary kill commands.
12369
12370 (defun gnus-summary-edit-global-kill (article)
12371   "Edit the \"global\" kill file."
12372   (interactive (list (gnus-summary-article-number)))
12373   (gnus-group-edit-global-kill article))
12374
12375 (defun gnus-summary-edit-local-kill ()
12376   "Edit a local kill file applied to the current newsgroup."
12377   (interactive)
12378   (setq gnus-current-headers (gnus-summary-article-header))
12379   (gnus-group-edit-local-kill
12380    (gnus-summary-article-number) gnus-newsgroup-name))
12381
12382 ;;; Header reading.
12383
12384 (defun gnus-read-header (id &optional header)
12385   "Read the headers of article ID and enter them into the Gnus system."
12386   (let ((group gnus-newsgroup-name)
12387         (gnus-override-method
12388          (or
12389           gnus-override-method
12390           (and (gnus-news-group-p gnus-newsgroup-name)
12391                (car (gnus-refer-article-methods)))))
12392         where)
12393     ;; First we check to see whether the header in question is already
12394     ;; fetched.
12395     (if (stringp id)
12396         ;; This is a Message-ID.
12397         (setq header (or header (gnus-id-to-header id)))
12398       ;; This is an article number.
12399       (setq header (or header (gnus-summary-article-header id))))
12400     (if (and header
12401              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12402         ;; We have found the header.
12403         header
12404       ;; We have to really fetch the header to this article.
12405       (with-current-buffer nntp-server-buffer
12406         (when (setq where (gnus-request-head id group))
12407           (nnheader-fold-continuation-lines)
12408           (goto-char (point-max))
12409           (insert ".\n")
12410           (goto-char (point-min))
12411           (insert "211 ")
12412           (princ (cond
12413                   ((numberp id) id)
12414                   ((cdr where) (cdr where))
12415                   (header (mail-header-number header))
12416                   (t gnus-reffed-article-number))
12417                  (current-buffer))
12418           (insert " Article retrieved.\n"))
12419         (if (or (not where)
12420                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12421             ()                          ; Malformed head.
12422           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12423             (when (and (stringp id)
12424                        (or
12425                         (not (string= (gnus-group-real-name group)
12426                                       (car where)))
12427                         (not (gnus-server-equal gnus-override-method
12428                                                 (gnus-group-method group)))))
12429               ;; If we fetched by Message-ID and the article came from
12430               ;; a different group (or server), we fudge some bogus
12431               ;; article numbers for this article.
12432               (mail-header-set-number header gnus-reffed-article-number))
12433             (with-current-buffer gnus-summary-buffer
12434               (decf gnus-reffed-article-number)
12435               (gnus-remove-header (mail-header-number header))
12436               (push header gnus-newsgroup-headers)
12437               (setq gnus-current-headers header)
12438               (push (mail-header-number header) gnus-newsgroup-limit)))
12439           header)))))
12440
12441 (defun gnus-remove-header (number)
12442   "Remove header NUMBER from `gnus-newsgroup-headers'."
12443   (if (and gnus-newsgroup-headers
12444            (= number (mail-header-number (car gnus-newsgroup-headers))))
12445       (pop gnus-newsgroup-headers)
12446     (let ((headers gnus-newsgroup-headers))
12447       (while (and (cdr headers)
12448                   (not (= number (mail-header-number (cadr headers)))))
12449         (pop headers))
12450       (when (cdr headers)
12451         (setcdr headers (cddr headers))))))
12452
12453 ;;;
12454 ;;; summary highlights
12455 ;;;
12456
12457 (defun gnus-highlight-selected-summary ()
12458   "Highlight selected article in summary buffer."
12459   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12460   (when gnus-summary-selected-face
12461     (save-excursion
12462       (let* ((beg (point-at-bol))
12463              (end (point-at-eol))
12464              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12465              (from (if (get-text-property beg gnus-mouse-face-prop)
12466                        beg
12467                      (or (next-single-property-change
12468                           beg gnus-mouse-face-prop nil end)
12469                          beg)))
12470              (to
12471               (if (= from end)
12472                   (- from 2)
12473                 (or (next-single-property-change
12474                      from gnus-mouse-face-prop nil end)
12475                     end))))
12476         ;; If no mouse-face prop on line we will have to = from = end,
12477         ;; so we highlight the entire line instead.
12478         (when (= (+ to 2) from)
12479           (setq from beg)
12480           (setq to end))
12481         (if gnus-newsgroup-selected-overlay
12482             ;; Move old overlay.
12483             (gnus-move-overlay
12484              gnus-newsgroup-selected-overlay from to (current-buffer))
12485           ;; Create new overlay.
12486           (gnus-overlay-put
12487            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12488            'face gnus-summary-selected-face))))))
12489
12490 (defvar gnus-summary-highlight-line-cached nil)
12491 (defvar gnus-summary-highlight-line-trigger nil)
12492
12493 (defun gnus-summary-highlight-line-0 ()
12494   (if (and (eq gnus-summary-highlight-line-trigger
12495                gnus-summary-highlight)
12496            gnus-summary-highlight-line-cached)
12497       gnus-summary-highlight-line-cached
12498     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12499           gnus-summary-highlight-line-cached
12500           (let* ((cond (list 'cond))
12501                  (c cond)
12502                  (list gnus-summary-highlight))
12503             (while list
12504               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12505                               nil))
12506               (setq c (cdr c)
12507                     list (cdr list)))
12508             (gnus-byte-compile (list 'lambda nil cond))))))
12509
12510 (defun gnus-summary-highlight-line ()
12511   "Highlight current line according to `gnus-summary-highlight'."
12512   (let* ((beg (point-at-bol))
12513          (article (or (gnus-summary-article-number) gnus-current-article))
12514          (score (or (cdr (assq article
12515                                gnus-newsgroup-scored))
12516                     gnus-summary-default-score 0))
12517          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12518          (inhibit-read-only t)
12519          (default gnus-summary-default-score)
12520          (default-high gnus-summary-default-high-score)
12521          (default-low gnus-summary-default-low-score)
12522          (uncached (and gnus-summary-use-undownloaded-faces
12523                         (memq article gnus-newsgroup-undownloaded)
12524                         (not (memq article gnus-newsgroup-cached)))))
12525     (let ((face (funcall (gnus-summary-highlight-line-0))))
12526       (unless (eq face (get-text-property beg 'face))
12527         (gnus-put-text-property-excluding-characters-with-faces
12528          beg (point-at-eol) 'face
12529          (setq face (if (boundp face) (symbol-value face) face)))
12530         (when gnus-summary-highlight-line-function
12531           (funcall gnus-summary-highlight-line-function article face))))))
12532
12533 (defun gnus-update-read-articles (group unread &optional compute)
12534   "Update the list of read articles in GROUP.
12535 UNREAD is a sorted list."
12536   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12537         (info (gnus-get-info group))
12538         (prev 1)
12539         read)
12540     (if (or (not info) (not active))
12541         ;; There is no info on this group if it was, in fact,
12542         ;; killed.  Gnus stores no information on killed groups, so
12543         ;; there's nothing to be done.
12544         ;; One could store the information somewhere temporarily,
12545         ;; perhaps...  Hmmm...
12546         ()
12547       ;; Remove any negative articles numbers.
12548       (while (and unread (< (car unread) 0))
12549         (setq unread (cdr unread)))
12550       ;; Remove any expired article numbers
12551       (while (and unread (< (car unread) (car active)))
12552         (setq unread (cdr unread)))
12553       ;; Compute the ranges of read articles by looking at the list of
12554       ;; unread articles.
12555       (while unread
12556         (when (/= (car unread) prev)
12557           (push (if (= prev (1- (car unread))) prev
12558                   (cons prev (1- (car unread))))
12559                 read))
12560         (setq prev (1+ (car unread)))
12561         (setq unread (cdr unread)))
12562       (when (<= prev (cdr active))
12563         (push (cons prev (cdr active)) read))
12564       (setq read (if (> (length read) 1) (nreverse read) read))
12565       (if compute
12566           read
12567         (save-excursion
12568           (let (setmarkundo)
12569             ;; Propagate the read marks to the backend.
12570             (when (and (or gnus-propagate-marks
12571                            (gnus-method-option-p
12572                             (gnus-find-method-for-group group)
12573                             'server-marks))
12574                        (gnus-check-backend-function 'request-set-mark group))
12575               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12576                     (add (gnus-remove-from-range read (gnus-info-read info))))
12577                 (when (or add del)
12578                   (unless (gnus-check-group group)
12579                     (error "Can't open server for %s" group))
12580                   (gnus-request-set-mark
12581                    group (delq nil (list (if add (list add 'add '(read)))
12582                                          (if del (list del 'del '(read))))))
12583                   (setq setmarkundo
12584                         `(gnus-request-set-mark
12585                           ,group
12586                           ',(delq nil (list
12587                                        (if del (list del 'add '(read)))
12588                                        (if add (list add 'del '(read))))))))))
12589             (set-buffer gnus-group-buffer)
12590             (gnus-undo-register
12591               `(progn
12592                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12593                  (gnus-info-set-read ',info ',(gnus-info-read info))
12594                  (gnus-get-unread-articles-in-group ',info
12595                                                     (gnus-active ,group))
12596                  (gnus-group-update-group ,group t)
12597                  ,setmarkundo))))
12598         ;; Enter this list into the group info.
12599         (gnus-info-set-read info read)
12600         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12601         (gnus-get-unread-articles-in-group info (gnus-active group))
12602         t))))
12603
12604 (defun gnus-offer-save-summaries ()
12605   "Offer to save all active summary buffers."
12606   (let (buffers)
12607     ;; Go through all buffers and find all summaries.
12608     (dolist (buffer (buffer-list))
12609       (when (and (setq buffer (buffer-name buffer))
12610                  (string-match "Summary" buffer)
12611                  (with-current-buffer buffer
12612                    ;; We check that this is, indeed, a summary buffer.
12613                    (and (eq major-mode 'gnus-summary-mode)
12614                         ;; Also make sure this isn't bogus.
12615                         gnus-newsgroup-prepared
12616                         ;; Also make sure that this isn't a
12617                         ;; dead summary buffer.
12618                         (not gnus-dead-summary-mode))))
12619         (push buffer buffers)))
12620     ;; Go through all these summary buffers and offer to save them.
12621     (when buffers
12622       (save-excursion
12623         (if (eq gnus-interactive-exit 'quiet)
12624             (dolist (buffer buffers)
12625               (switch-to-buffer buffer)
12626               (gnus-summary-exit))
12627           (map-y-or-n-p
12628            "Update summary buffer %s? "
12629            (lambda (buf)
12630              (switch-to-buffer buf)
12631              (gnus-summary-exit))
12632            buffers))))))
12633
12634 (defun gnus-summary-setup-default-charset ()
12635   "Setup newsgroup default charset."
12636   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12637       (setq gnus-newsgroup-charset nil)
12638     (let* ((ignored-charsets
12639             (or gnus-newsgroup-ephemeral-ignored-charsets
12640                 (append
12641                  (and gnus-newsgroup-name
12642                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12643                  gnus-newsgroup-ignored-charsets))))
12644       (setq gnus-newsgroup-charset
12645             (or gnus-newsgroup-ephemeral-charset
12646                 (and gnus-newsgroup-name
12647                      (gnus-parameter-charset gnus-newsgroup-name))
12648                 gnus-default-charset))
12649       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12650            ignored-charsets))))
12651
12652 ;;;
12653 ;;; Mime Commands
12654 ;;;
12655
12656 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12657   "Display the current article buffer fully MIME-buttonized.
12658 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12659 treated as multipart/mixed."
12660   (interactive "P")
12661   (require 'gnus-art)
12662   (let ((gnus-unbuttonized-mime-types nil)
12663         (gnus-mime-display-multipart-as-mixed show-all-parts))
12664     (gnus-summary-show-article)))
12665
12666 (defun gnus-summary-repair-multipart (article)
12667   "Add a Content-Type header to a multipart article without one."
12668   (interactive (list (gnus-summary-article-number)))
12669   (gnus-with-article article
12670     (message-narrow-to-head)
12671     (message-remove-header "Mime-Version")
12672     (goto-char (point-max))
12673     (insert "Mime-Version: 1.0\n")
12674     (widen)
12675     (when (search-forward "\n--" nil t)
12676       (let ((separator (buffer-substring (point) (point-at-eol))))
12677         (message-narrow-to-head)
12678         (message-remove-header "Content-Type")
12679         (goto-char (point-max))
12680         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12681                         separator))
12682         (widen))))
12683   (let (gnus-mark-article-hook)
12684     (gnus-summary-select-article t t nil article)))
12685
12686 (defun gnus-summary-toggle-display-buttonized ()
12687   "Toggle the buttonizing of the article buffer."
12688   (interactive)
12689   (require 'gnus-art)
12690   (if (setq gnus-inhibit-mime-unbuttonizing
12691             (not gnus-inhibit-mime-unbuttonizing))
12692       (let ((gnus-unbuttonized-mime-types nil))
12693         (gnus-summary-show-article))
12694     (gnus-summary-show-article)))
12695
12696 ;;;
12697 ;;; Generic summary marking commands
12698 ;;;
12699
12700 (defvar gnus-summary-marking-alist
12701   '((read gnus-del-mark "d")
12702     (unread gnus-unread-mark "u")
12703     (ticked gnus-ticked-mark "!")
12704     (dormant gnus-dormant-mark "?")
12705     (expirable gnus-expirable-mark "e"))
12706   "An alist of names/marks/keystrokes.")
12707
12708 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12709 (defvar gnus-summary-mark-map)
12710
12711 (defun gnus-summary-make-all-marking-commands ()
12712   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12713   (dolist (elem gnus-summary-marking-alist)
12714     (apply 'gnus-summary-make-marking-command elem)))
12715
12716 (defun gnus-summary-make-marking-command (name mark keystroke)
12717   (let ((map (make-sparse-keymap)))
12718     (define-key gnus-summary-generic-mark-map keystroke map)
12719     (dolist (lway `((next "next" next nil "n")
12720                     (next-unread "next unread" next t "N")
12721                     (prev "previous" prev nil "p")
12722                     (prev-unread "previous unread" prev t "P")
12723                     (nomove "" nil nil ,keystroke)))
12724       (let ((func (gnus-summary-make-marking-command-1
12725                    mark (car lway) lway name)))
12726         (setq func (eval func))
12727         (define-key map (nth 4 lway) func)))))
12728
12729 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12730   `(defun ,(intern
12731             (format "gnus-summary-put-mark-as-%s%s"
12732                     name (if (eq way 'nomove)
12733                              ""
12734                            (concat "-" (symbol-name way)))))
12735      (n)
12736      ,(format
12737        "Mark the current article as %s%s.
12738 If N, the prefix, then repeat N times.
12739 If N is negative, move in reverse order.
12740 The difference between N and the actual number of articles marked is
12741 returned."
12742        name (cadr lway))
12743      (interactive "p")
12744      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12745
12746 (defun gnus-summary-generic-mark (n mark move unread)
12747   "Mark N articles with MARK."
12748   (unless (eq major-mode 'gnus-summary-mode)
12749     (error "This command can only be used in the summary buffer"))
12750   (gnus-summary-show-thread)
12751   (let ((nummove
12752          (cond
12753           ((eq move 'next) 1)
12754           ((eq move 'prev) -1)
12755           (t 0))))
12756     (if (zerop nummove)
12757         (setq n 1)
12758       (when (< n 0)
12759         (setq n (abs n)
12760               nummove (* -1 nummove))))
12761     (while (and (> n 0)
12762                 (gnus-summary-mark-article nil mark)
12763                 (zerop (gnus-summary-next-subject nummove unread t)))
12764       (setq n (1- n)))
12765     (when (/= 0 n)
12766       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12767     (gnus-summary-recenter)
12768     (gnus-summary-position-point)
12769     (gnus-set-mode-line 'summary)
12770     n))
12771
12772 (defun gnus-summary-insert-articles (articles)
12773   (when (setq articles
12774               (gnus-sorted-difference articles
12775                                       (mapcar (lambda (h)
12776                                                 (mail-header-number h))
12777                                               gnus-newsgroup-headers)))
12778     (setq gnus-newsgroup-headers
12779           (gnus-merge 'list
12780                       gnus-newsgroup-headers
12781                       (gnus-fetch-headers articles)
12782                       'gnus-article-sort-by-number))
12783     (setq gnus-newsgroup-articles
12784           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12785     ;; Suppress duplicates?
12786     (when gnus-suppress-duplicates
12787       (gnus-dup-suppress-articles))
12788
12789     (if (and gnus-fetch-old-headers
12790              (eq gnus-headers-retrieved-by 'nov))
12791         ;; We might want to build some more threads first.
12792         (if (eq gnus-fetch-old-headers 'invisible)
12793             (gnus-build-all-threads)
12794           (gnus-build-old-threads))
12795       ;; Mark the inserted articles that are unread as unread.
12796       (setq gnus-newsgroup-unreads
12797             (gnus-sorted-nunion
12798              gnus-newsgroup-unreads
12799              (gnus-sorted-nintersection
12800               (gnus-list-of-unread-articles gnus-newsgroup-name)
12801               articles)))
12802       ;; Mark the inserted articles as selected so that the information
12803       ;; of the marks having been changed by a user may be updated when
12804       ;; exiting this group.  See `gnus-summary-update-info'.
12805       (dolist (art articles)
12806         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12807     ;; Let the Gnus agent mark articles as read.
12808     (when gnus-agent
12809       (gnus-agent-get-undownloaded-list))
12810     ;; Remove list identifiers from subject
12811     (gnus-summary-remove-list-identifiers)
12812     ;; First and last article in this newsgroup.
12813     (when gnus-newsgroup-headers
12814       (setq gnus-newsgroup-begin
12815             (mail-header-number (car gnus-newsgroup-headers))
12816             gnus-newsgroup-end
12817             (mail-header-number
12818              (gnus-last-element gnus-newsgroup-headers))))
12819     (when gnus-use-scoring
12820       (gnus-possibly-score-headers))))
12821
12822 (defun gnus-summary-insert-old-articles (&optional all)
12823   "Insert all old articles in this group.
12824 If ALL is non-nil, already read articles become readable.
12825 If ALL is a number, fetch this number of articles."
12826   (interactive "P")
12827   (prog1
12828       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12829             older len)
12830         (setq older
12831               ;; Some nntp servers lie about their active range.  When
12832               ;; this happens, the active range can be in the millions.
12833               ;; Use a compressed range to avoid creating a huge list.
12834               (gnus-range-difference (list gnus-newsgroup-active) old))
12835         (setq len (gnus-range-length older))
12836         (cond
12837          ((null older) nil)
12838          ((numberp all)
12839           (if (< all len)
12840               (let ((older-range (nreverse older)))
12841                 (setq older nil)
12842
12843                 (while (> all 0)
12844                   (let* ((r (pop older-range))
12845                          (min (if (numberp r) r (car r)))
12846                          (max (if (numberp r) r (cdr r))))
12847                     (while (and (<= min max)
12848                                 (> all 0))
12849                       (push max older)
12850                       (setq all (1- all)
12851                             max (1- max))))))
12852             (setq older (gnus-uncompress-range older))))
12853          (all
12854           (setq older (gnus-uncompress-range older)))
12855          (t
12856           (when (and (numberp gnus-large-newsgroup)
12857                    (> len gnus-large-newsgroup))
12858               (let* ((cursor-in-echo-area nil)
12859                      (initial (gnus-parameter-large-newsgroup-initial
12860                                gnus-newsgroup-name))
12861                      (input
12862                       (read-string
12863                        (format
12864                         "How many articles from %s (%s %d): "
12865                         (gnus-group-decoded-name gnus-newsgroup-name)
12866                         (if initial "max" "default")
12867                         len)
12868                        (if initial
12869                            (cons (number-to-string initial)
12870                                  0)))))
12871                 (unless (string-match "^[ \t]*$" input)
12872                   (setq all (string-to-number input))
12873                   (if (< all len)
12874                       (let ((older-range (nreverse older)))
12875                         (setq older nil)
12876
12877                         (while (> all 0)
12878                           (let* ((r (pop older-range))
12879                                  (min (if (numberp r) r (car r)))
12880                                  (max (if (numberp r) r (cdr r))))
12881                             (while (and (<= min max)
12882                                         (> all 0))
12883                               (push max older)
12884                               (setq all (1- all)
12885                                     max (1- max))))))))))
12886           (setq older (gnus-uncompress-range older))))
12887         (if (not older)
12888             (message "No old news.")
12889           (gnus-summary-insert-articles older)
12890           (gnus-summary-limit (gnus-sorted-nunion old older))))
12891     (gnus-summary-position-point)))
12892
12893 (defun gnus-summary-insert-new-articles ()
12894   "Insert all new articles in this group."
12895   (interactive)
12896   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12897         (old-high gnus-newsgroup-highest)
12898         (nnmail-fetched-sources (list t))
12899         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12900         i new)
12901     (unless new-active
12902       (error "Couldn't fetch new data"))
12903     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12904     (setq i (cdr gnus-newsgroup-active)
12905           gnus-newsgroup-highest i)
12906     (while (> i old-high)
12907       (push i new)
12908       (decf i))
12909     (if (not new)
12910         (message "No gnus is bad news")
12911       (gnus-summary-insert-articles new)
12912       (setq gnus-newsgroup-unreads
12913             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12914       (gnus-summary-limit (gnus-sorted-nunion old new))))
12915   (gnus-summary-position-point))
12916
12917 ;;; Bookmark support for Gnus.
12918 (declare-function bookmark-make-record-default
12919                   "bookmark" (&optional no-file no-context posn))
12920 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12921 (declare-function bookmark-default-handler "bookmark" (bmk))
12922 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12923 (defvar bookmark-yank-point)
12924 (defvar bookmark-current-buffer)
12925
12926 (defun gnus-summary-bookmark-make-record ()
12927   "Make a bookmark entry for a Gnus summary buffer."
12928   (let (pos buf)
12929     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12930       (save-restriction              ; FIXME is it necessary to widen?
12931         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12932       (setq buf "art") ; We are recording bookmark from article buffer.
12933       (setq bookmark-yank-point (point))
12934       (setq bookmark-current-buffer (current-buffer))
12935       (gnus-article-show-summary))      ; Go back in summary buffer.
12936     ;; We are now recording bookmark from summary buffer.
12937     (unless buf (setq buf "sum"))
12938     (let* ((subject (elt (gnus-summary-article-header) 1))
12939            (grp     (car gnus-article-current))
12940            (art     (cdr gnus-article-current))
12941            (head    (gnus-summary-article-header art))
12942            (id      (mail-header-id head)))
12943       `(,subject
12944         ,@(condition-case nil
12945               (bookmark-make-record-default 'no-file 'no-context pos)
12946             (wrong-number-of-arguments
12947              (bookmark-make-record-default 'point-only)))
12948         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12949         (group . ,grp) (article . ,art)
12950         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12951
12952 ;;;###autoload
12953 (defun gnus-summary-bookmark-jump (bookmark)
12954   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12955 BOOKMARK is a bookmark name or a bookmark record."
12956   (let ((group    (bookmark-prop-get bookmark 'group))
12957         (article  (bookmark-prop-get bookmark 'article))
12958         (id       (bookmark-prop-get bookmark 'message-id))
12959         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12960     (gnus-fetch-group group (list article))
12961     (gnus-summary-insert-cached-articles)
12962     (gnus-summary-goto-article id nil 'force)
12963     ;; FIXME we have to wait article buffer is ready (only large buffer)
12964     ;; Is there a better solution to know that?
12965     ;; If we don't wait `bookmark-default-handler' will have no chance
12966     ;; to set position. However there is no error, just wrong pos.
12967     (sit-for 1)
12968     (when (string= buf "Gnus-art")
12969       (other-window 1))
12970     (bookmark-default-handler
12971      `(""
12972        (buffer . ,(current-buffer))
12973        . ,(bookmark-get-bookmark-record bookmark)))))
12974
12975 (gnus-summary-make-all-marking-commands)
12976
12977 (gnus-ems-redefine)
12978
12979 (provide 'gnus-sum)
12980
12981 (run-hooks 'gnus-sum-load-hook)
12982
12983 ;; Local Variables:
12984 ;; coding: iso-8859-1
12985 ;; End:
12986
12987 ;;; gnus-sum.el ends here