Don't prompt for unexisting articles
[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-backward
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           (gnus-list-range-difference
5817           ;; Select all articles if `read-all' is non-nil, or if there
5818           ;; are no unread articles.
5819           (if (or read-all
5820                   (and (zerop (length gnus-newsgroup-marked))
5821                        (zerop (length gnus-newsgroup-unreads)))
5822                   ;; Fetch all if the predicate is non-nil.
5823                   gnus-newsgroup-display)
5824               ;; We want to select the headers for all the articles in
5825               ;; the group, so we select either all the active
5826               ;; articles in the group, or (if that's nil), the
5827               ;; articles in the cache.
5828               (or
5829                (if gnus-newsgroup-maximum-articles
5830                    (let ((active (gnus-active group)))
5831                      (gnus-uncompress-range
5832                       (cons (max (car active)
5833                                  (- (cdr active)
5834                                     gnus-newsgroup-maximum-articles
5835                                     -1))
5836                             (cdr active))))
5837                  (gnus-uncompress-range (gnus-active group)))
5838                (gnus-cache-articles-in-group group))
5839             ;; Select only the "normal" subset of articles.
5840             (setq only-read-p nil)
5841             (gnus-sorted-nunion
5842              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5843              gnus-newsgroup-unreads))
5844           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5845          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5846          (scored (length scored-list))
5847          (number (length articles))
5848          (marked (+ (length gnus-newsgroup-marked)
5849                     (length gnus-newsgroup-dormant)))
5850          (select
5851           (cond
5852            ((numberp read-all)
5853             read-all)
5854            ((numberp gnus-newsgroup-display)
5855             gnus-newsgroup-display)
5856            (t
5857             (condition-case ()
5858                 (cond
5859                  ((and (or (<= scored marked) (= scored number))
5860                        (numberp gnus-large-newsgroup)
5861                        (> number gnus-large-newsgroup))
5862                   (let* ((cursor-in-echo-area nil)
5863                          (initial (gnus-parameter-large-newsgroup-initial
5864                                    gnus-newsgroup-name))
5865                          (default (if only-read-p
5866                                       (or initial gnus-large-newsgroup)
5867                                     number))
5868                          (input
5869                           (read-string
5870                            (if only-read-p
5871                                (format
5872                                 "How many articles from %s (available %d, default %d): "
5873                                 (gnus-group-decoded-name
5874                                  (gnus-group-real-name gnus-newsgroup-name))
5875                                 number default)
5876                              (format
5877                               "How many articles from %s (%d default): "
5878                               (gnus-group-decoded-name
5879                                (gnus-group-real-name gnus-newsgroup-name))
5880                               default))
5881                            nil
5882                            nil
5883                            (number-to-string default))))
5884                     (if (string-match "^[ \t]*$" input) number input)))
5885                  ((and (> scored marked) (< scored number)
5886                        (> (- scored number) 20))
5887                   (let ((input
5888                          (read-string
5889                           (format "%s %s (%d scored, %d total): "
5890                                   "How many articles from"
5891                                   (gnus-group-decoded-name
5892                                    (gnus-group-real-name gnus-newsgroup-name))
5893                                   scored number))))
5894                     (if (string-match "^[ \t]*$" input)
5895                         number input)))
5896                  (t number))
5897               (quit
5898                (message "Quit getting the articles to read")
5899                nil))))))
5900     (setq select (if (stringp select) (string-to-number select) select))
5901     (if (or (null select) (zerop select))
5902         select
5903       (if (and (not (zerop scored)) (<= (abs select) scored))
5904           (progn
5905             (setq articles (sort scored-list '<))
5906             (setq number (length articles)))
5907         (setq articles (copy-sequence articles)))
5908
5909       (when (< (abs select) number)
5910         (if (< select 0)
5911             ;; Select the N oldest articles.
5912             (setcdr (nthcdr (1- (abs select)) articles) nil)
5913           ;; Select the N most recent articles.
5914           (setq articles (nthcdr (- number select) articles))))
5915       (setq gnus-newsgroup-unselected
5916             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5917       (when gnus-alter-articles-to-read-function
5918         (setq articles
5919               (sort
5920                (funcall gnus-alter-articles-to-read-function
5921                         gnus-newsgroup-name articles)
5922                '<)))
5923       articles)))
5924
5925 (defun gnus-killed-articles (killed articles)
5926   (let (out)
5927     (while articles
5928       (when (inline (gnus-member-of-range (car articles) killed))
5929         (push (car articles) out))
5930       (setq articles (cdr articles)))
5931     out))
5932
5933 (defun gnus-uncompress-marks (marks)
5934   "Uncompress the mark ranges in MARKS."
5935   (let ((uncompressed '(score bookmark))
5936         out)
5937     (while marks
5938       (if (memq (caar marks) uncompressed)
5939           (push (car marks) out)
5940         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5941       (setq marks (cdr marks)))
5942     out))
5943
5944 (defun gnus-article-mark-to-type (mark)
5945   "Return the type of MARK."
5946   (or (cadr (assq mark gnus-article-special-mark-lists))
5947       'list))
5948
5949 (defun gnus-article-unpropagatable-p (mark)
5950   "Return whether MARK should be propagated to back end."
5951   (memq mark gnus-article-unpropagated-mark-lists))
5952
5953 (defun gnus-adjust-marked-articles (info)
5954   "Set all article lists and remove all marks that are no longer valid."
5955   (let* ((marked-lists (gnus-info-marks info))
5956          (active (gnus-active (gnus-info-group info)))
5957          (min (car active))
5958          (max (cdr active))
5959          (types gnus-article-mark-lists)
5960          marks var articles article mark mark-type
5961          bgn end)
5962     ;; Hack to avoid adjusting marks for imap.
5963     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5964               'nnimap)
5965       (setq min 1))
5966
5967     (dolist (marks marked-lists)
5968       (setq mark (car marks)
5969             mark-type (gnus-article-mark-to-type mark)
5970             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5971
5972       ;; We set the variable according to the type of the marks list,
5973       ;; and then adjust the marks to a subset of the active articles.
5974       (cond
5975        ;; Adjust "simple" lists - compressed yet unsorted
5976        ((eq mark-type 'list)
5977         ;; Simultaneously uncompress and clip to active range
5978         ;; See gnus-uncompress-range for a description of possible marks
5979         (let (l lh)
5980           (if (not (cadr marks))
5981               (set var nil)
5982             (setq articles (if (numberp (cddr marks))
5983                                (list (cdr marks))
5984                              (cdr marks))
5985                   lh (cons nil nil)
5986                   l lh)
5987
5988             (while (setq article (pop articles))
5989               (cond ((consp article)
5990                      (setq bgn (max (car article) min)
5991                            end (min (cdr article) max))
5992                      (while (<= bgn end)
5993                        (setq l (setcdr l (cons bgn nil))
5994                              bgn (1+ bgn))))
5995                     ((and (<= min article)
5996                           (>= max article))
5997                      (setq l (setcdr l (cons article nil))))))
5998             (set var (cdr lh)))))
5999        ;; Adjust assocs.
6000        ((eq mark-type 'tuple)
6001         (set var (setq articles (cdr marks)))
6002         (when (not (listp (cdr (symbol-value var))))
6003           (set var (list (symbol-value var))))
6004         (when (not (listp (cdr articles)))
6005           (setq articles (list articles)))
6006         (while articles
6007           (when (or (not (consp (setq article (pop articles))))
6008                     (< (car article) min)
6009                     (> (car article) max))
6010             (set var (delq article (symbol-value var))))))
6011        ;; Adjust ranges (sloppily).
6012        ((eq mark-type 'range)
6013         (cond
6014          ((eq mark 'seen)
6015           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6016           ;; It should be (seen (NUM1 . NUM2)).
6017           (when (numberp (cddr marks))
6018             (setcdr marks (list (cdr marks))))
6019           (setq articles (cdr marks))
6020           (while (and articles
6021                       (or (and (consp (car articles))
6022                                (> min (cdar articles)))
6023                           (and (numberp (car articles))
6024                                (> min (car articles)))))
6025             (pop articles))
6026           (set var articles))))))))
6027
6028 (defun gnus-update-missing-marks (missing)
6029   "Go through the list of MISSING articles and remove them from the mark lists."
6030   (when missing
6031     (let (var m)
6032       ;; Go through all types.
6033       (dolist (elem gnus-article-mark-lists)
6034         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6035           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6036           (when (symbol-value var)
6037             ;; This list has articles.  So we delete all missing
6038             ;; articles from it.
6039             (setq m missing)
6040             (while m
6041               (set var (delq (pop m) (symbol-value var))))))))))
6042
6043 (defun gnus-update-marks ()
6044   "Enter the various lists of marked articles into the newsgroup info list."
6045   (let ((types gnus-article-mark-lists)
6046         (info (gnus-get-info gnus-newsgroup-name))
6047         type list newmarked symbol delta-marks)
6048     (when info
6049       ;; Add all marks lists to the list of marks lists.
6050       (while (setq type (pop types))
6051         (setq list (symbol-value
6052                     (setq symbol
6053                           (intern (format "gnus-newsgroup-%s" (car type))))))
6054
6055         (when list
6056           ;; Get rid of the entries of the articles that have the
6057           ;; default score.
6058           (when (and (eq (cdr type) 'score)
6059                      gnus-save-score
6060                      list)
6061             (let* ((arts list)
6062                    (prev (cons nil list))
6063                    (all prev))
6064               (while arts
6065                 (if (or (not (consp (car arts)))
6066                         (= (cdar arts) gnus-summary-default-score))
6067                     (setcdr prev (cdr arts))
6068                   (setq prev arts))
6069                 (setq arts (cdr arts)))
6070               (setq list (cdr all)))))
6071
6072         (when (eq (cdr type) 'seen)
6073           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6074
6075         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6076           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6077
6078         (when (and (gnus-check-backend-function
6079                     'request-set-mark gnus-newsgroup-name)
6080                    (not (gnus-article-unpropagatable-p (cdr type))))
6081           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6082                  ;; Don't do anything about marks for articles we
6083                  ;; didn't actually get any headers for.
6084                  (del
6085                   (gnus-list-range-intersection
6086                    gnus-newsgroup-articles
6087                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6088                  (add
6089                   (gnus-list-range-intersection
6090                    gnus-newsgroup-articles
6091                    (gnus-remove-from-range
6092                     (gnus-copy-sequence list) old))))
6093             (when add
6094               (push (list add 'add (list (cdr type))) delta-marks))
6095             (when del
6096               ;; Don't delete marks from outside the active range.
6097               ;; This shouldn't happen, but is a sanity check.
6098               (setq del (gnus-sorted-range-intersection
6099                          (gnus-active gnus-newsgroup-name) del))
6100               (push (list del 'del (list (cdr type))) delta-marks))))
6101
6102         (when list
6103           (push (cons (cdr type) list) newmarked)))
6104
6105       (when delta-marks
6106         (unless (gnus-check-group gnus-newsgroup-name)
6107           (error "Can't open server for %s" gnus-newsgroup-name))
6108         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6109
6110       ;; Enter these new marks into the info of the group.
6111       (if (nthcdr 3 info)
6112           (setcar (nthcdr 3 info) newmarked)
6113         ;; Add the marks lists to the end of the info.
6114         (when newmarked
6115           (setcdr (nthcdr 2 info) (list newmarked))))
6116
6117       ;; Cut off the end of the info if there's nothing else there.
6118       (let ((i 5))
6119         (while (and (> i 2)
6120                     (not (nth i info)))
6121           (when (nthcdr (decf i) info)
6122             (setcdr (nthcdr i info) nil)))))))
6123
6124 (defun gnus-set-mode-line (where)
6125   "Set the mode line of the article or summary buffers.
6126 If WHERE is `summary', the summary mode line format will be used."
6127   ;; Is this mode line one we keep updated?
6128   (when (and (memq where gnus-updated-mode-lines)
6129              (symbol-value
6130               (intern (format "gnus-%s-mode-line-format-spec" where))))
6131     (let (mode-string)
6132       ;; We evaluate this in the summary buffer since these
6133       ;; variables are buffer-local to that buffer.
6134       (with-current-buffer gnus-summary-buffer
6135         ;; We bind all these variables that are used in the `eval' form
6136         ;; below.
6137         (let* ((mformat (symbol-value
6138                          (intern
6139                           (format "gnus-%s-mode-line-format-spec" where))))
6140                (gnus-tmp-group-name (gnus-mode-string-quote
6141                                      (gnus-group-decoded-name
6142                                       gnus-newsgroup-name)))
6143                (gnus-tmp-article-number (or gnus-current-article 0))
6144                (gnus-tmp-unread gnus-newsgroup-unreads)
6145                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6146                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6147                (gnus-tmp-unread-and-unselected
6148                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6149                             (zerop gnus-tmp-unselected))
6150                        "")
6151                       ((zerop gnus-tmp-unselected)
6152                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6153                       (t (format "{%d(+%d) more}"
6154                                  gnus-tmp-unread-and-unticked
6155                                  gnus-tmp-unselected))))
6156                (gnus-tmp-subject
6157                 (if (and gnus-current-headers
6158                          (vectorp gnus-current-headers))
6159                     (gnus-mode-string-quote
6160                      (mail-header-subject gnus-current-headers))
6161                   ""))
6162                bufname-length max-len
6163                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6164           (setq mode-string (eval mformat))
6165           (setq bufname-length (if (string-match "%b" mode-string)
6166                                    (- (length
6167                                        (buffer-name
6168                                         (if (eq where 'summary)
6169                                             nil
6170                                           (get-buffer gnus-article-buffer))))
6171                                       2)
6172                                  0))
6173           (setq max-len (max 4 (if gnus-mode-non-string-length
6174                                    (- (window-width)
6175                                       gnus-mode-non-string-length
6176                                       bufname-length)
6177                                  (length mode-string))))
6178           ;; We might have to chop a bit of the string off...
6179           (when (> (length mode-string) max-len)
6180             (setq mode-string
6181                   (concat (truncate-string-to-width mode-string (- max-len 3))
6182                           "...")))))
6183       ;; Update the mode line.
6184       (setq mode-line-buffer-identification
6185             (gnus-mode-line-buffer-identification (list mode-string)))
6186       (set-buffer-modified-p t))))
6187
6188 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6189   "Go through the HEADERS list and add all Xrefs to a hash table.
6190 The resulting hash table is returned, or nil if no Xrefs were found."
6191   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6192          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6193          (xref-hashtb (gnus-make-hashtable))
6194          start group entry number xrefs header)
6195     (while headers
6196       (setq header (pop headers))
6197       (when (and (setq xrefs (mail-header-xref header))
6198                  (not (memq (setq number (mail-header-number header))
6199                             unreads)))
6200         (setq start 0)
6201         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6202           (setq start (match-end 0))
6203           (setq group (if prefix
6204                           (concat prefix (substring xrefs (match-beginning 1)
6205                                                     (match-end 1)))
6206                         (substring xrefs (match-beginning 1) (match-end 1))))
6207           (setq number
6208                 (string-to-number (substring xrefs (match-beginning 2)
6209                                           (match-end 2))))
6210           (if (setq entry (gnus-gethash group xref-hashtb))
6211               (setcdr entry (cons number (cdr entry)))
6212             (gnus-sethash group (cons number nil) xref-hashtb)))))
6213     (and start xref-hashtb)))
6214
6215 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6216   "Look through all the headers and mark the Xrefs as read."
6217   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6218         name info xref-hashtb idlist method nth4)
6219     (with-current-buffer gnus-group-buffer
6220       (when (setq xref-hashtb
6221                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6222         (mapatoms
6223          (lambda (group)
6224            (unless (string= from-newsgroup (setq name (symbol-name group)))
6225              (setq idlist (symbol-value group))
6226              ;; Dead groups are not updated.
6227              (and (prog1
6228                       (setq info (gnus-get-info name))
6229                     (when (stringp (setq nth4 (gnus-info-method info)))
6230                       (setq nth4 (gnus-server-to-method nth4))))
6231                   ;; Only do the xrefs if the group has the same
6232                   ;; select method as the group we have just read.
6233                   (or (gnus-methods-equal-p
6234                        nth4 (gnus-find-method-for-group from-newsgroup))
6235                       virtual
6236                       (equal nth4 (setq method (gnus-find-method-for-group
6237                                                 from-newsgroup)))
6238                       (and (equal (car nth4) (car method))
6239                            (equal (nth 1 nth4) (nth 1 method))))
6240                   gnus-use-cross-reference
6241                   (or (not (eq gnus-use-cross-reference t))
6242                       virtual
6243                       ;; Only do cross-references on subscribed
6244                       ;; groups, if that is what is wanted.
6245                       (<= (gnus-info-level info) gnus-level-subscribed))
6246                   (gnus-group-make-articles-read name idlist))))
6247          xref-hashtb)))))
6248
6249 (defun gnus-compute-read-articles (group articles)
6250   (let* ((entry (gnus-group-entry group))
6251          (info (nth 2 entry))
6252          (active (gnus-active group))
6253          ninfo)
6254     (when entry
6255       ;; First peel off all invalid article numbers.
6256       (when active
6257         (let ((ids articles)
6258               id first)
6259           (while (setq id (pop ids))
6260             (when (and first (> id (cdr active)))
6261               ;; We'll end up in this situation in one particular
6262               ;; obscure situation.  If you re-scan a group and get
6263               ;; a new article that is cross-posted to a different
6264               ;; group that has not been re-scanned, you might get
6265               ;; crossposted article that has a higher number than
6266               ;; Gnus believes possible.  So we re-activate this
6267               ;; group as well.  This might mean doing the
6268               ;; crossposting thingy will *increase* the number
6269               ;; of articles in some groups.  Tsk, tsk.
6270               (setq active (or (gnus-activate-group group) active)))
6271             (when (or (> id (cdr active))
6272                       (< id (car active)))
6273               (setq articles (delq id articles))))))
6274       ;; If the read list is nil, we init it.
6275       (if (and active
6276                (null (gnus-info-read info))
6277                (> (car active) 1))
6278           (setq ninfo (cons 1 (1- (car active))))
6279         (setq ninfo (gnus-info-read info)))
6280       ;; Then we add the read articles to the range.
6281       (gnus-add-to-range
6282        ninfo (setq articles (sort articles '<))))))
6283
6284 (defun gnus-group-make-articles-read (group articles)
6285   "Update the info of GROUP to say that ARTICLES are read."
6286   (let* ((num 0)
6287          (entry (gnus-group-entry group))
6288          (info (nth 2 entry))
6289          (active (gnus-active group))
6290          (set-marks
6291           (or gnus-propagate-marks
6292               (gnus-method-option-p
6293                (gnus-find-method-for-group group)
6294                'server-marks)))
6295          range)
6296     (if (not entry)
6297         ;; Group that Gnus doesn't know exists, but still allow the
6298         ;; backend to set marks.
6299         (when set-marks
6300           (gnus-request-set-mark
6301            group (list (list (gnus-compress-sequence (sort articles #'<))
6302                              'add '(read)))))
6303       ;; Normal, subscribed groups.
6304       (setq range (gnus-compute-read-articles group articles))
6305       (with-current-buffer gnus-group-buffer
6306         (gnus-undo-register
6307           `(progn
6308              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6309              (gnus-info-set-read ',info ',(gnus-info-read info))
6310              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6311              (when ,set-marks
6312                (gnus-request-set-mark
6313                 ,group (list (list ',range 'del '(read)))))
6314              (gnus-group-update-group ,group t))))
6315       ;; Add the read articles to the range.
6316       (gnus-info-set-read info range)
6317       (when set-marks
6318         (gnus-request-set-mark group (list (list range 'add '(read)))))
6319       ;; Then we have to re-compute how many unread
6320       ;; articles there are in this group.
6321       (when active
6322         (cond
6323          ((not range)
6324           (setq num (- (1+ (cdr active)) (car active))))
6325          ((not (listp (cdr range)))
6326           (setq num (- (cdr active) (- (1+ (cdr range))
6327                                        (car range)))))
6328          (t
6329           (while range
6330             (if (numberp (car range))
6331                 (setq num (1+ num))
6332               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6333             (setq range (cdr range)))
6334           (setq num (- (cdr active) num))))
6335         ;; Update the number of unread articles.
6336         (setcar entry num)
6337         ;; Update the group buffer.
6338         (unless (gnus-ephemeral-group-p group)
6339           (gnus-group-update-group group t))))))
6340
6341 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6342   (let ((cur nntp-server-buffer)
6343         (dependencies
6344          (or dependencies
6345              (with-current-buffer gnus-summary-buffer
6346                gnus-newsgroup-dependencies)))
6347         headers id end ref number
6348         (mail-parse-charset gnus-newsgroup-charset)
6349         (mail-parse-ignored-charsets
6350          (save-current-buffer (condition-case nil
6351                                   (set-buffer gnus-summary-buffer)
6352                                 (error))
6353                               gnus-newsgroup-ignored-charsets)))
6354     (with-current-buffer nntp-server-buffer
6355       ;; Translate all TAB characters into SPACE characters.
6356       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6357       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6358       (ietf-drums-unfold-fws)
6359       (gnus-run-hooks 'gnus-parse-headers-hook)
6360       (let ((case-fold-search t)
6361             in-reply-to header p lines chars)
6362         (goto-char (point-min))
6363         ;; Search to the beginning of the next header.  Error messages
6364         ;; do not begin with 2 or 3.
6365         (while (re-search-forward "^[23][0-9]+ " nil t)
6366           (setq id nil
6367                 ref nil)
6368           ;; This implementation of this function, with nine
6369           ;; search-forwards instead of the one re-search-forward and
6370           ;; a case (which basically was the old function) is actually
6371           ;; about twice as fast, even though it looks messier.  You
6372           ;; can't have everything, I guess.  Speed and elegance
6373           ;; doesn't always go hand in hand.
6374           (setq
6375            header
6376            (vector
6377             ;; Number.
6378             (prog1
6379                 (setq number (read cur))
6380               (end-of-line)
6381               (setq p (point))
6382               (narrow-to-region (point)
6383                                 (or (and (search-forward "\n.\n" nil t)
6384                                          (- (point) 2))
6385                                     (point))))
6386             ;; Subject.
6387             (progn
6388               (goto-char p)
6389               (if (search-forward "\nsubject:" nil t)
6390                   (funcall gnus-decode-encoded-word-function
6391                            (nnheader-header-value))
6392                 "(none)"))
6393             ;; From.
6394             (progn
6395               (goto-char p)
6396               (if (search-forward "\nfrom:" nil t)
6397                   (funcall gnus-decode-encoded-address-function
6398                            (nnheader-header-value))
6399                 "(nobody)"))
6400             ;; Date.
6401             (progn
6402               (goto-char p)
6403               (if (search-forward "\ndate:" nil t)
6404                   (nnheader-header-value) ""))
6405             ;; Message-ID.
6406             (progn
6407               (goto-char p)
6408               (setq id (if (re-search-forward
6409                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6410                            ;; We do it this way to make sure the Message-ID
6411                            ;; is (somewhat) syntactically valid.
6412                            (buffer-substring (match-beginning 1)
6413                                              (match-end 1))
6414                          ;; If there was no message-id, we just fake one
6415                          ;; to make subsequent routines simpler.
6416                          (nnheader-generate-fake-message-id number))))
6417             ;; References.
6418             (progn
6419               (goto-char p)
6420               (if (search-forward "\nreferences:" nil t)
6421                   (progn
6422                     (setq end (point))
6423                     (prog1
6424                         (nnheader-header-value)
6425                       (setq ref
6426                             (buffer-substring
6427                              (progn
6428                                (end-of-line)
6429                                (search-backward ">" end t)
6430                                (1+ (point)))
6431                              (progn
6432                                (search-backward "<" end t)
6433                                (point))))))
6434                 ;; Get the references from the in-reply-to header if there
6435                 ;; were no references and the in-reply-to header looks
6436                 ;; promising.
6437                 (if (and (search-forward "\nin-reply-to:" nil t)
6438                          (setq in-reply-to (nnheader-header-value))
6439                          (string-match "<[^>]+>" in-reply-to))
6440                     (let (ref2)
6441                       (setq ref (substring in-reply-to (match-beginning 0)
6442                                            (match-end 0)))
6443                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6444                         (setq ref2 (substring in-reply-to (match-beginning 0)
6445                                               (match-end 0)))
6446                         (when (> (length ref2) (length ref))
6447                           (setq ref ref2)))
6448                       ref)
6449                   (setq ref nil))))
6450             ;; Chars.
6451             (progn
6452               (goto-char p)
6453               (if (search-forward "\nchars: " nil t)
6454                   (if (numberp (setq chars (ignore-errors (read cur))))
6455                       chars -1)
6456                 -1))
6457             ;; Lines.
6458             (progn
6459               (goto-char p)
6460               (if (search-forward "\nlines: " nil t)
6461                   (if (numberp (setq lines (ignore-errors (read cur))))
6462                       lines -1)
6463                 -1))
6464             ;; Xref.
6465             (progn
6466               (goto-char p)
6467               (and (search-forward "\nxref:" nil t)
6468                    (nnheader-header-value)))
6469             ;; Extra.
6470             (when gnus-extra-headers
6471               (let ((extra gnus-extra-headers)
6472                     out)
6473                 (while extra
6474                   (goto-char p)
6475                   (when (search-forward
6476                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6477                     (push (cons (car extra) (nnheader-header-value))
6478                           out))
6479                   (pop extra))
6480                 out))))
6481           (when (equal id ref)
6482             (setq ref nil))
6483
6484           (when gnus-alter-header-function
6485             (funcall gnus-alter-header-function header)
6486             (setq id (mail-header-id header)
6487                   ref (gnus-parent-id (mail-header-references header))))
6488
6489           (when (setq header
6490                       (gnus-dependencies-add-header
6491                        header dependencies force-new))
6492             (push header headers))
6493           (goto-char (point-max))
6494           (widen))
6495         (nreverse headers)))))
6496
6497 ;; Goes through the xover lines and returns a list of vectors
6498 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6499                                                   force-new dependencies
6500                                                   group also-fetch-heads)
6501   "Parse the news overview data in the server buffer.
6502 Return a list of headers that match SEQUENCE (see
6503 `nntp-retrieve-headers')."
6504   ;; Get the Xref when the users reads the articles since most/some
6505   ;; NNTP servers do not include Xrefs when using XOVER.
6506   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6507   (let ((mail-parse-charset gnus-newsgroup-charset)
6508         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6509         (cur nntp-server-buffer)
6510         (dependencies (or dependencies gnus-newsgroup-dependencies))
6511         (allp (cond
6512                ((eq gnus-read-all-available-headers t)
6513                 t)
6514                ((and (stringp gnus-read-all-available-headers)
6515                      group)
6516                 (string-match gnus-read-all-available-headers group))
6517                (t
6518                 nil)))
6519         number headers header)
6520     (with-current-buffer nntp-server-buffer
6521       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6522       ;; Allow the user to mangle the headers before parsing them.
6523       (gnus-run-hooks 'gnus-parse-headers-hook)
6524       (goto-char (point-min))
6525       (gnus-parse-without-error
6526         (while (and (or sequence allp)
6527                     (not (eobp)))
6528           (setq number (read cur))
6529           (when (not allp)
6530             (while (and sequence
6531                         (< (car sequence) number))
6532               (setq sequence (cdr sequence))))
6533           (when (and (or allp
6534                          (and sequence
6535                               (eq number (car sequence))))
6536                      (progn
6537                        (setq sequence (cdr sequence))
6538                        (setq header (inline
6539                                       (gnus-nov-parse-line
6540                                        number dependencies force-new)))))
6541             (push header headers))
6542           (forward-line 1)))
6543       ;; A common bug in inn is that if you have posted an article and
6544       ;; then retrieves the active file, it will answer correctly --
6545       ;; the new article is included.  However, a NOV entry for the
6546       ;; article may not have been generated yet, so this may fail.
6547       ;; We work around this problem by retrieving the last few
6548       ;; headers using HEAD.
6549       (if (or (not also-fetch-heads)
6550               (not sequence))
6551           ;; We (probably) got all the headers.
6552           (nreverse headers)
6553         (let ((gnus-nov-is-evil t))
6554           (nconc
6555            (nreverse headers)
6556            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6557              (gnus-get-newsgroup-headers))))))))
6558
6559 (defun gnus-article-get-xrefs ()
6560   "Fill in the Xref value in `gnus-current-headers', if necessary.
6561 This is meant to be called in `gnus-article-internal-prepare-hook'."
6562   (let ((headers (with-current-buffer gnus-summary-buffer
6563                    gnus-current-headers)))
6564     (or (not gnus-use-cross-reference)
6565         (not headers)
6566         (and (mail-header-xref headers)
6567              (not (string= (mail-header-xref headers) "")))
6568         (let ((case-fold-search t)
6569               xref)
6570           (save-restriction
6571             (nnheader-narrow-to-headers)
6572             (goto-char (point-min))
6573             (when (or (and (not (eobp))
6574                            (eq (downcase (char-after)) ?x)
6575                            (looking-at "Xref:"))
6576                       (search-forward "\nXref:" nil t))
6577               (goto-char (1+ (match-end 0)))
6578               (setq xref (buffer-substring (point) (point-at-eol)))
6579               (mail-header-set-xref headers xref)))))))
6580
6581 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6582   "Find article ID and insert the summary line for that article.
6583 OLD-HEADER can either be a header or a line number to insert
6584 the subject line on.
6585 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6586 and OLD-HEADER will be used when the summary line is inserted,
6587 too, instead of trying to fetch new headers."
6588   (let* ((line (and (numberp old-header) old-header))
6589          (old-header (and (vectorp old-header) old-header))
6590          (header (cond ((and old-header use-old-header)
6591                         old-header)
6592                        ((and (numberp id)
6593                              (gnus-number-to-header id))
6594                         (gnus-number-to-header id))
6595                        (t
6596                         (gnus-read-header id))))
6597          (number (and (numberp id) id))
6598          d)
6599     (when header
6600       ;; Rebuild the thread that this article is part of and go to the
6601       ;; article we have fetched.
6602       (when (and (not gnus-show-threads)
6603                  old-header)
6604         (when (and number
6605                    (setq d (gnus-data-find (mail-header-number old-header))))
6606           (goto-char (gnus-data-pos d))
6607           (gnus-data-remove
6608            number
6609            (- (point-at-bol)
6610               (prog1
6611                   (1+ (point-at-eol))
6612                 (gnus-delete-line))))))
6613       ;; Remove list identifiers from subject.
6614       (let ((gnus-newsgroup-headers (list header)))
6615         (gnus-summary-remove-list-identifiers))
6616       (when old-header
6617         (mail-header-set-number header (mail-header-number old-header)))
6618       (setq gnus-newsgroup-sparse
6619             (delq (setq number (mail-header-number header))
6620                   gnus-newsgroup-sparse))
6621       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6622       (push number gnus-newsgroup-limit)
6623       (gnus-rebuild-thread (mail-header-id header) line)
6624       (gnus-summary-goto-subject number nil t))
6625     (when (and (numberp number)
6626                (> number 0))
6627       ;; We have to update the boundaries even if we can't fetch the
6628       ;; article if ID is a number -- so that the next `P' or `N'
6629       ;; command will fetch the previous (or next) article even
6630       ;; if the one we tried to fetch this time has been canceled.
6631       (when (> number gnus-newsgroup-end)
6632         (setq gnus-newsgroup-end number))
6633       (when (< number gnus-newsgroup-begin)
6634         (setq gnus-newsgroup-begin number))
6635       (setq gnus-newsgroup-unselected
6636             (delq number gnus-newsgroup-unselected)))
6637     ;; Report back a success?
6638     (and header (mail-header-number header))))
6639
6640 ;;; Process/prefix in the summary buffer
6641
6642 (defun gnus-summary-work-articles (n)
6643   "Return a list of articles to be worked upon.
6644 The prefix argument, the list of process marked articles, and the
6645 current article will be taken into consideration."
6646   (with-current-buffer gnus-summary-buffer
6647     (cond
6648      (n
6649       ;; A numerical prefix has been given.
6650       (setq n (prefix-numeric-value n))
6651       (let ((backward (< n 0))
6652             (n (abs (prefix-numeric-value n)))
6653             articles article)
6654         (save-excursion
6655           (while
6656               (and (> n 0)
6657                    (push (setq article (gnus-summary-article-number))
6658                          articles)
6659                    (if backward
6660                        (gnus-summary-find-prev nil article)
6661                      (gnus-summary-find-next nil article)))
6662             (decf n)))
6663         (nreverse articles)))
6664      ((and (gnus-region-active-p) (mark))
6665       (message "region active")
6666       ;; Work on the region between point and mark.
6667       (let ((max (max (point) (mark)))
6668             articles article)
6669         (save-excursion
6670           (goto-char (min (point) (mark)))
6671           (while
6672               (and
6673                (push (setq article (gnus-summary-article-number)) articles)
6674                (gnus-summary-find-next nil article)
6675                (< (point) max)))
6676           (nreverse articles))))
6677      (gnus-newsgroup-processable
6678       ;; There are process-marked articles present.
6679       ;; Save current state.
6680       (gnus-summary-save-process-mark)
6681       ;; Return the list.
6682       (reverse gnus-newsgroup-processable))
6683      (t
6684       ;; Just return the current article.
6685       (list (gnus-summary-article-number))))))
6686
6687 (defmacro gnus-summary-iterate (arg &rest forms)
6688   "Iterate over the process/prefixed articles and do FORMS.
6689 ARG is the interactive prefix given to the command.  FORMS will be
6690 executed with point over the summary line of the articles."
6691   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6692     `(let ((,articles (gnus-summary-work-articles ,arg)))
6693        (while ,articles
6694          (gnus-summary-goto-subject (car ,articles))
6695          ,@forms
6696          (pop ,articles)))))
6697
6698 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6699 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6700
6701 (defun gnus-summary-save-process-mark ()
6702   "Push the current set of process marked articles on the stack."
6703   (interactive)
6704   (push (copy-sequence gnus-newsgroup-processable)
6705         gnus-newsgroup-process-stack))
6706
6707 (defun gnus-summary-kill-process-mark ()
6708   "Push the current set of process marked articles on the stack and unmark."
6709   (interactive)
6710   (gnus-summary-save-process-mark)
6711   (gnus-summary-unmark-all-processable))
6712
6713 (defun gnus-summary-yank-process-mark ()
6714   "Pop the last process mark state off the stack and restore it."
6715   (interactive)
6716   (unless gnus-newsgroup-process-stack
6717     (error "Empty mark stack"))
6718   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6719
6720 (defun gnus-summary-process-mark-set (set)
6721   "Make SET into the current process marked articles."
6722   (gnus-summary-unmark-all-processable)
6723   (mapc 'gnus-summary-set-process-mark set))
6724
6725 ;;; Searching and stuff
6726
6727 (defun gnus-summary-search-group (&optional backward use-level)
6728   "Search for next unread newsgroup.
6729 If optional argument BACKWARD is non-nil, search backward instead."
6730   (with-current-buffer gnus-group-buffer
6731     (when (gnus-group-search-forward
6732            backward nil (if use-level (gnus-group-group-level) nil))
6733       (gnus-group-group-name))))
6734
6735 (defun gnus-summary-best-group (&optional exclude-group)
6736   "Find the name of the best unread group.
6737 If EXCLUDE-GROUP, do not go to this group."
6738   (with-current-buffer gnus-group-buffer
6739     (save-excursion
6740       (gnus-group-best-unread-group exclude-group))))
6741
6742 (defun gnus-summary-find-next (&optional unread article backward)
6743   (if backward
6744       (gnus-summary-find-prev unread article)
6745     (let* ((dummy (gnus-summary-article-intangible-p))
6746            (article (or article (gnus-summary-article-number)))
6747            (data (gnus-data-find-list article))
6748            result)
6749       (when (and (not dummy)
6750                  (or (not gnus-summary-check-current)
6751                      (not unread)
6752                      (not (gnus-data-unread-p (car data)))))
6753         (setq data (cdr data)))
6754       (when (setq result
6755                   (if unread
6756                       (progn
6757                         (while data
6758                           (unless (memq (gnus-data-number (car data))
6759                                         (cond
6760                                          ((eq gnus-auto-goto-ignores
6761                                               'always-undownloaded)
6762                                           gnus-newsgroup-undownloaded)
6763                                          (gnus-plugged
6764                                           nil)
6765                                          ((eq gnus-auto-goto-ignores
6766                                               'unfetched)
6767                                           gnus-newsgroup-unfetched)
6768                                          ((eq gnus-auto-goto-ignores
6769                                               'undownloaded)
6770                                           gnus-newsgroup-undownloaded)))
6771                             (when (gnus-data-unread-p (car data))
6772                               (setq result (car data)
6773                                     data nil)))
6774                           (setq data (cdr data)))
6775                         result)
6776                     (car data)))
6777         (goto-char (gnus-data-pos result))
6778         (gnus-data-number result)))))
6779
6780 (defun gnus-summary-find-prev (&optional unread article)
6781   (let* ((eobp (eobp))
6782          (article (or article (gnus-summary-article-number)))
6783          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6784          result)
6785     (when (and (not eobp)
6786                (or (not gnus-summary-check-current)
6787                    (not unread)
6788                    (not (gnus-data-unread-p (car data)))))
6789       (setq data (cdr data)))
6790     (when (setq result
6791                 (if unread
6792                     (progn
6793                       (while data
6794                         (unless (memq (gnus-data-number (car data))
6795                                       (cond
6796                                        ((eq gnus-auto-goto-ignores
6797                                             'always-undownloaded)
6798                                         gnus-newsgroup-undownloaded)
6799                                        (gnus-plugged
6800                                         nil)
6801                                        ((eq gnus-auto-goto-ignores
6802                                             'unfetched)
6803                                         gnus-newsgroup-unfetched)
6804                                        ((eq gnus-auto-goto-ignores
6805                                             'undownloaded)
6806                                         gnus-newsgroup-undownloaded)))
6807                           (when (gnus-data-unread-p (car data))
6808                             (setq result (car data)
6809                                   data nil)))
6810                         (setq data (cdr data)))
6811                       result)
6812                   (car data)))
6813       (goto-char (gnus-data-pos result))
6814       (gnus-data-number result))))
6815
6816 (defun gnus-summary-find-subject (subject &optional unread backward article)
6817   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6818          (article (or article (gnus-summary-article-number)))
6819          (articles (gnus-data-list backward))
6820          (arts (gnus-data-find-list article articles))
6821          result)
6822     (when (or (not gnus-summary-check-current)
6823               (not unread)
6824               (not (gnus-data-unread-p (car arts))))
6825       (setq arts (cdr arts)))
6826     (while arts
6827       (and (or (not unread)
6828                (gnus-data-unread-p (car arts)))
6829            (vectorp (gnus-data-header (car arts)))
6830            (gnus-subject-equal
6831             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6832            (setq result (car arts)
6833                  arts nil))
6834       (setq arts (cdr arts)))
6835     (and result
6836          (goto-char (gnus-data-pos result))
6837          (gnus-data-number result))))
6838
6839 (defun gnus-summary-search-forward (&optional unread subject backward)
6840   "Search forward for an article.
6841 If UNREAD, look for unread articles.  If SUBJECT, look for
6842 articles with that subject.  If BACKWARD, search backward instead."
6843   (cond (subject (gnus-summary-find-subject subject unread backward))
6844         (backward (gnus-summary-find-prev unread))
6845         (t (gnus-summary-find-next unread))))
6846
6847 (defun gnus-recenter (&optional n)
6848   "Center point in window and redisplay frame.
6849 Also do horizontal recentering."
6850   (interactive "P")
6851   (when (and gnus-auto-center-summary
6852              (not (eq gnus-auto-center-summary 'vertical)))
6853     (gnus-horizontal-recenter))
6854   (if (fboundp 'recenter-top-bottom)
6855       (recenter-top-bottom n)
6856     (recenter n)))
6857
6858 (put 'gnus-recenter 'isearch-scroll t)
6859
6860 (defun gnus-forward-line-ignore-invisible (n)
6861   "Move N lines forward (backward if N is negative).
6862 Like forward-line, but skip over (and don't count) invisible lines."
6863   (let (done)
6864     (while (and (> n 0) (not done))
6865       ;; If the following character is currently invisible,
6866       ;; skip all characters with that same `invisible' property value.
6867       (while (gnus-invisible-p (point))
6868         (goto-char (gnus-next-char-property-change (point))))
6869       (forward-line 1)
6870       (if (eobp)
6871           (setq done t)
6872         (setq n (1- n))))
6873     (while (and (< n 0) (not done))
6874       (forward-line -1)
6875       (if (bobp) (setq done t)
6876         (setq n (1+ n))
6877         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6878           (goto-char (gnus-previous-char-property-change (point))))))))
6879
6880 (defun gnus-summary-recenter ()
6881   "Center point in the summary window.
6882 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6883 displayed, no centering will be performed."
6884   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6885   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6886   (interactive)
6887   ;; The user has to want it.
6888   (when gnus-auto-center-summary
6889     (let* ((top (cond ((< (window-height) 4) 0)
6890                       ((< (window-height) 7) 1)
6891                       (t (if (numberp gnus-auto-center-summary)
6892                              gnus-auto-center-summary
6893                            (/ (1- (window-height)) 2)))))
6894            (height (1- (window-height)))
6895            (bottom (save-excursion
6896                      (goto-char (point-max))
6897                      (gnus-forward-line-ignore-invisible (- height))
6898                      (point)))
6899            (window (get-buffer-window (current-buffer))))
6900       (when (get-buffer-window gnus-article-buffer)
6901         ;; Only do recentering when the article buffer is displayed,
6902         ;; Set the window start to either `bottom', which is the biggest
6903         ;; possible valid number, or the second line from the top,
6904         ;; whichever is the least.
6905         (let ((top-pos (save-excursion
6906                          (gnus-forward-line-ignore-invisible (- top))
6907                          (point))))
6908           (if (> bottom top-pos)
6909               ;; Keep the second line from the top visible
6910               (set-window-start window top-pos)
6911             ;; Try to keep the bottom line visible; if it's partially
6912             ;; obscured, either scroll one more line to make it fully
6913             ;; visible, or revert to using TOP-POS.
6914             (save-excursion
6915               (goto-char (point-max))
6916               (gnus-forward-line-ignore-invisible -1)
6917               (let ((last-line-start (point)))
6918                 (goto-char bottom)
6919                 (set-window-start window (point) t)
6920                 (when (not (pos-visible-in-window-p last-line-start window))
6921                   (gnus-forward-line-ignore-invisible 1)
6922                   (set-window-start window (min (point) top-pos) t)))))))
6923       ;; Do horizontal recentering while we're at it.
6924       (when (and (get-buffer-window (current-buffer) t)
6925                  (not (eq gnus-auto-center-summary 'vertical)))
6926         (let ((selected (selected-window)))
6927           (select-window (get-buffer-window (current-buffer) t))
6928           (gnus-summary-position-point)
6929           (gnus-horizontal-recenter)
6930           (select-window selected))))))
6931
6932 (defun gnus-summary-jump-to-group (newsgroup)
6933   "Move point to NEWSGROUP in group mode buffer."
6934   ;; Keep update point of group mode buffer if visible.
6935   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6936       (save-window-excursion
6937         ;; Take care of tree window mode.
6938         (when (get-buffer-window gnus-group-buffer)
6939           (pop-to-buffer gnus-group-buffer))
6940         (gnus-group-jump-to-group newsgroup))
6941     (save-excursion
6942       ;; Take care of tree window mode.
6943       (if (get-buffer-window gnus-group-buffer 0)
6944           (pop-to-buffer gnus-group-buffer)
6945         (set-buffer gnus-group-buffer))
6946       (gnus-group-jump-to-group newsgroup))))
6947
6948 ;; This function returns a list of article numbers based on the
6949 ;; difference between the ranges of read articles in this group and
6950 ;; the range of active articles.
6951 (defun gnus-list-of-unread-articles (group)
6952   (let* ((read (gnus-info-read (gnus-get-info group)))
6953          (active (or (gnus-active group) (gnus-activate-group group)))
6954          (last (or (cdr active)
6955                    (error "Group %s couldn't be activated " group)))
6956          (bottom (if gnus-newsgroup-maximum-articles
6957                      (max (car active)
6958                           (- last gnus-newsgroup-maximum-articles -1))
6959                    (car active)))
6960          first nlast unread)
6961     ;; If none are read, then all are unread.
6962     (if (not read)
6963         (setq first bottom)
6964       ;; If the range of read articles is a single range, then the
6965       ;; first unread article is the article after the last read
6966       ;; article.  Sounds logical, doesn't it?
6967       (if (and (not (listp (cdr read)))
6968                (or (< (car read) bottom)
6969                    (progn (setq read (list read))
6970                           nil)))
6971           (setq first (max bottom (1+ (cdr read))))
6972         ;; `read' is a list of ranges.
6973         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6974                                   (caar read)))
6975                   1)
6976           (setq first bottom))
6977         (while read
6978           (when first
6979             (while (< first nlast)
6980               (setq unread (cons first unread)
6981                     first (1+ first))))
6982           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6983           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6984           (setq read (cdr read)))))
6985     ;; And add the last unread articles.
6986     (while (<= first last)
6987       (setq unread (cons first unread)
6988             first (1+ first)))
6989     ;; Return the list of unread articles.
6990     (delq 0 (nreverse unread))))
6991
6992 (defun gnus-list-of-read-articles (group)
6993   "Return a list of unread, unticked and non-dormant articles."
6994   (let* ((info (gnus-get-info group))
6995          (marked (gnus-info-marks info))
6996          (active (gnus-active group)))
6997     (and info active
6998          (gnus-list-range-difference
6999           (gnus-list-range-difference
7000            (gnus-sorted-complement
7001             (gnus-uncompress-range
7002              (if gnus-newsgroup-maximum-articles
7003                  (cons (max (car active)
7004                             (- (cdr active)
7005                                gnus-newsgroup-maximum-articles
7006                                -1))
7007                        (cdr active))
7008                active))
7009             (gnus-list-of-unread-articles group))
7010            (cdr (assq 'dormant marked)))
7011           (cdr (assq 'tick marked))))))
7012
7013 ;; This function returns a sequence of article numbers based on the
7014 ;; difference between the ranges of read articles in this group and
7015 ;; the range of active articles.
7016 (defun gnus-sequence-of-unread-articles (group)
7017   (let* ((read (gnus-info-read (gnus-get-info group)))
7018          (active (or (gnus-active group) (gnus-activate-group group)))
7019          (last (cdr active))
7020          (bottom (if gnus-newsgroup-maximum-articles
7021                      (max (car active)
7022                           (- last gnus-newsgroup-maximum-articles -1))
7023                    (car active)))
7024          first nlast unread)
7025     ;; If none are read, then all are unread.
7026     (if (not read)
7027         (setq first bottom)
7028       ;; If the range of read articles is a single range, then the
7029       ;; first unread article is the article after the last read
7030       ;; article.  Sounds logical, doesn't it?
7031       (if (and (not (listp (cdr read)))
7032                (or (< (car read) bottom)
7033                    (progn (setq read (list read))
7034                           nil)))
7035           (setq first (max bottom (1+ (cdr read))))
7036         ;; `read' is a list of ranges.
7037         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7038                                   (caar read)))
7039                   1)
7040           (setq first bottom))
7041         (while read
7042           (when first
7043             (push (cons first nlast) unread))
7044           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7045           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7046           (setq read (cdr read)))))
7047     ;; And add the last unread articles.
7048     (cond ((not (and first last))
7049            nil)
7050           ((< first last)
7051            (push (cons first last) unread))
7052           ((= first last)
7053            (push first unread)))
7054     ;; Return the sequence of unread articles.
7055     (delq 0 (nreverse unread))))
7056
7057 ;; Various summary commands
7058
7059 (defun gnus-summary-select-article-buffer ()
7060   "Reconfigure windows to show the article buffer.
7061 If `gnus-widen-article-window' is set, show only the article
7062 buffer."
7063   (interactive)
7064   (if (not (gnus-buffer-live-p gnus-article-buffer))
7065       (error "There is no article buffer for this summary buffer")
7066     (unless (get-buffer-window gnus-article-buffer)
7067       (gnus-summary-show-article))
7068     (gnus-configure-windows
7069      (if gnus-widen-article-window
7070          'only-article
7071        'article)
7072      t)
7073     (select-window (get-buffer-window gnus-article-buffer))))
7074
7075 (defun gnus-summary-universal-argument (arg)
7076   "Perform any operation on all articles that are process/prefixed."
7077   (interactive "P")
7078   (let ((articles (gnus-summary-work-articles arg))
7079         func article)
7080     (if (eq
7081          (setq
7082           func
7083           (key-binding
7084            (read-key-sequence
7085             (substitute-command-keys
7086              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7087          'undefined)
7088         (gnus-error 1 "Undefined key")
7089       (save-excursion
7090         (while articles
7091           (gnus-summary-goto-subject (setq article (pop articles)))
7092           (let (gnus-newsgroup-processable)
7093             (command-execute func))
7094           (gnus-summary-remove-process-mark article)))))
7095   (gnus-summary-position-point))
7096
7097 (defun gnus-summary-toggle-truncation (&optional arg)
7098   "Toggle truncation of summary lines.
7099 With ARG, turn line truncation on if ARG is positive."
7100   (interactive "P")
7101   (setq truncate-lines
7102         (if (null arg) (not truncate-lines)
7103           (> (prefix-numeric-value arg) 0)))
7104   (redraw-display))
7105
7106 (defun gnus-summary-find-for-reselect ()
7107   "Return the number of an article to stay on across a reselect.
7108 The current article is considered, then following articles, then previous
7109 articles.  An article is sought which is not canceled and isn't a temporary
7110 insertion from another group.  If there's no such then return a dummy 0."
7111   (let (found)
7112     (dolist (rev '(nil t))
7113       (unless found      ; don't demand the reverse list if we don't need it
7114         (let ((data (gnus-data-find-list
7115                      (gnus-summary-article-number) (gnus-data-list rev))))
7116           (while (and data (not found))
7117             (if (and (< 0 (gnus-data-number (car data)))
7118                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7119                 (setq found (gnus-data-number (car data))))
7120             (setq data (cdr data))))))
7121     (or found 0)))
7122
7123 (defun gnus-summary-reselect-current-group (&optional all rescan)
7124   "Exit and then reselect the current newsgroup.
7125 The prefix argument ALL means to select all articles."
7126   (interactive "P")
7127   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7128     (error "Ephemeral groups can't be reselected"))
7129   (let ((current-subject (gnus-summary-find-for-reselect))
7130         (group gnus-newsgroup-name))
7131     (setq gnus-newsgroup-begin nil)
7132     (gnus-summary-exit nil 'leave-hidden)
7133     ;; We have to adjust the point of group mode buffer because
7134     ;; point was moved to the next unread newsgroup by exiting.
7135     (gnus-summary-jump-to-group group)
7136     (when rescan
7137       (save-excursion
7138         (gnus-group-get-new-news-this-group 1)))
7139     (gnus-group-read-group all t)
7140     (gnus-summary-goto-subject current-subject nil t)))
7141
7142 (defun gnus-summary-rescan-group (&optional all)
7143   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7144   (interactive "P")
7145   (let ((config gnus-current-window-configuration))
7146     (gnus-summary-reselect-current-group all t)
7147     (gnus-configure-windows config)
7148     (when (eq config 'article)
7149       (gnus-summary-select-article))))
7150
7151 (defun gnus-summary-update-info (&optional non-destructive)
7152   (save-excursion
7153     (let ((group gnus-newsgroup-name))
7154       (when group
7155         (when gnus-newsgroup-kill-headers
7156           (setq gnus-newsgroup-killed
7157                 (gnus-compress-sequence
7158                  (gnus-sorted-union
7159                   (gnus-list-range-intersection
7160                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7161                   gnus-newsgroup-unreads)
7162                  t)))
7163         (unless (listp (cdr gnus-newsgroup-killed))
7164           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7165         (let ((headers gnus-newsgroup-headers)
7166               (ephemeral-p (gnus-ephemeral-group-p group))
7167               info)
7168           (unless ephemeral-p
7169             (setq info (copy-sequence (gnus-get-info group))
7170                   info (delq (gnus-info-params info) info)))
7171           ;; Set the new ranges of read articles.
7172           (with-current-buffer gnus-group-buffer
7173             (gnus-undo-force-boundary))
7174           (gnus-update-read-articles
7175            group (gnus-sorted-union
7176                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7177           ;; Set the current article marks.
7178           (let ((gnus-newsgroup-scored
7179                  (if (and (not gnus-save-score)
7180                           (not non-destructive))
7181                      nil
7182                    gnus-newsgroup-scored)))
7183             (save-excursion
7184               (gnus-update-marks)))
7185           ;; Do the cross-ref thing.
7186           (when gnus-use-cross-reference
7187             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7188           ;; Do not switch windows but change the buffer to work.
7189           (set-buffer gnus-group-buffer)
7190           (unless ephemeral-p
7191             (gnus-group-update-group
7192              group nil
7193              (equal info
7194                     (setq info (copy-sequence (gnus-get-info group))
7195                           info (delq (gnus-info-params info) info))))))))))
7196
7197 (defun gnus-summary-save-newsrc (&optional force)
7198   "Save the current number of read/marked articles in the dribble buffer.
7199 The dribble buffer will then be saved.
7200 If FORCE (the prefix), also save the .newsrc file(s)."
7201   (interactive "P")
7202   (gnus-summary-update-info t)
7203   (if force
7204       (gnus-save-newsrc-file)
7205     (gnus-dribble-save)))
7206
7207 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7208
7209 (defun gnus-summary-exit (&optional temporary leave-hidden)
7210   "Exit reading current newsgroup, and then return to group selection mode.
7211 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7212   (interactive)
7213   (gnus-set-global-variables)
7214   (when (gnus-buffer-live-p gnus-article-buffer)
7215     (with-current-buffer gnus-article-buffer
7216       (mm-destroy-parts gnus-article-mime-handles)
7217       ;; Set it to nil for safety reason.
7218       (setq gnus-article-mime-handle-alist nil)
7219       (setq gnus-article-mime-handles nil)))
7220   (gnus-kill-save-kill-buffer)
7221   (gnus-async-halt-prefetch)
7222   (let* ((group gnus-newsgroup-name)
7223          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7224          (gnus-group-is-exiting-p t)
7225          (article-buffer gnus-article-buffer)
7226          (original-article-buffer gnus-original-article-buffer)
7227          (mode major-mode)
7228          (group-point nil)
7229          (buf (current-buffer))
7230          ;; `gnus-single-article-buffer' is nil buffer-locally in
7231          ;; ephemeral group of which summary buffer will be killed,
7232          ;; but the global value may be non-nil.
7233          (single-article-buffer gnus-single-article-buffer))
7234     (unless quit-config
7235       ;; Do adaptive scoring, and possibly save score files.
7236       (when gnus-newsgroup-adaptive
7237         (gnus-score-adaptive))
7238       (when gnus-use-scoring
7239         (gnus-score-save)))
7240     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7241     (when gnus-use-cache
7242       (gnus-cache-possibly-remove-articles)
7243       (gnus-cache-save-buffers))
7244     (gnus-async-prefetch-remove-group group)
7245     (when gnus-suppress-duplicates
7246       (gnus-dup-enter-articles))
7247     (when gnus-use-trees
7248       (gnus-tree-close group))
7249     (when gnus-use-cache
7250       (gnus-cache-write-active))
7251     ;; Remove entries for this group.
7252     (nnmail-purge-split-history (gnus-group-real-name group))
7253     ;; Make all changes in this group permanent.
7254     (unless quit-config
7255       (gnus-run-hooks 'gnus-exit-group-hook)
7256       (gnus-summary-update-info))
7257     (gnus-close-group group)
7258     ;; Make sure where we were, and go to next newsgroup.
7259     (set-buffer gnus-group-buffer)
7260     (unless quit-config
7261       (gnus-group-jump-to-group group))
7262     (gnus-run-hooks 'gnus-summary-exit-hook)
7263     (unless (or quit-config
7264                 (not gnus-summary-next-group-on-exit)
7265                 ;; If this group has disappeared from the summary
7266                 ;; buffer, don't skip forwards.
7267                 (not (string= group (gnus-group-group-name))))
7268       (gnus-group-next-unread-group 1))
7269     (setq group-point (point))
7270     (if temporary
7271         nil                             ;Nothing to do.
7272       (set-buffer buf)
7273       (if (not gnus-kill-summary-on-exit)
7274           (progn
7275             (gnus-deaden-summary)
7276             (setq mode nil))
7277         (when (get-buffer gnus-article-buffer)
7278           (bury-buffer gnus-article-buffer))
7279         ;; Return to group mode buffer.
7280         (when (eq mode 'gnus-summary-mode)
7281           (gnus-kill-buffer buf)))
7282
7283       (setq gnus-current-select-method gnus-select-method)
7284       (set-buffer gnus-group-buffer)
7285       (if quit-config
7286           (gnus-handle-ephemeral-exit quit-config)
7287         (goto-char group-point)
7288         ;; If gnus-group-buffer is already displayed, make sure we also move
7289         ;; the cursor in the window that displays it.
7290         (let ((win (get-buffer-window (current-buffer) 0)))
7291           (if win (set-window-point win (point))))
7292         (unless leave-hidden
7293           (gnus-configure-windows 'group 'force)))
7294
7295       ;; If we have several article buffers, we kill them at exit.
7296       (unless single-article-buffer
7297         (when (gnus-buffer-live-p article-buffer)
7298           (with-current-buffer article-buffer
7299             ;; Don't kill sticky article buffers
7300             (unless (eq major-mode 'gnus-sticky-article-mode)
7301               (gnus-kill-buffer article-buffer)
7302               (setq gnus-article-current nil))))
7303         (gnus-kill-buffer original-article-buffer))
7304
7305       ;; Clear the current group name.
7306       (unless quit-config
7307         (setq gnus-newsgroup-name nil)))))
7308
7309 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7310 (defun gnus-summary-exit-no-update (&optional no-questions)
7311   "Quit reading current newsgroup without updating read article info."
7312   (interactive)
7313   (let* ((group gnus-newsgroup-name)
7314          (gnus-group-is-exiting-p t)
7315          (gnus-group-is-exiting-without-update-p t)
7316          (quit-config (gnus-group-quit-config group)))
7317     (when (or no-questions
7318               gnus-expert-user
7319               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7320       (gnus-async-halt-prefetch)
7321       (run-hooks 'gnus-summary-prepare-exit-hook)
7322       (when (gnus-buffer-live-p gnus-article-buffer)
7323         (with-current-buffer gnus-article-buffer
7324           (gnus-article-stop-animations)
7325           (mm-destroy-parts gnus-article-mime-handles)
7326           ;; Set it to nil for safety reason.
7327           (setq gnus-article-mime-handle-alist nil)
7328           (setq gnus-article-mime-handles nil)))
7329       ;; If we have several article buffers, we kill them at exit.
7330       (unless gnus-single-article-buffer
7331         (gnus-kill-buffer gnus-article-buffer)
7332         (gnus-kill-buffer gnus-original-article-buffer)
7333         (setq gnus-article-current nil))
7334       ;; Return to the group buffer.
7335       (if (not gnus-kill-summary-on-exit)
7336           (progn
7337             (gnus-deaden-summary)
7338             (gnus-configure-windows 'group 'force))
7339         (gnus-configure-windows 'group 'force)
7340         (gnus-close-group group)
7341         (gnus-kill-buffer gnus-summary-buffer))
7342       (unless gnus-single-article-buffer
7343         (setq gnus-article-current nil))
7344       (when gnus-use-trees
7345         (gnus-tree-close group))
7346       (gnus-async-prefetch-remove-group group)
7347       (when (get-buffer gnus-article-buffer)
7348         (bury-buffer gnus-article-buffer))
7349       ;; Clear the current group name.
7350       (setq gnus-newsgroup-name nil)
7351       (unless (gnus-ephemeral-group-p group)
7352         (gnus-group-update-group group nil t))
7353       (when (equal (gnus-group-group-name) group)
7354         (gnus-group-next-unread-group 1))
7355       (when quit-config
7356         (gnus-handle-ephemeral-exit quit-config)))))
7357
7358 (defun gnus-handle-ephemeral-exit (quit-config)
7359   "Handle movement when leaving an ephemeral group.
7360 The state which existed when entering the ephemeral is reset."
7361   (if (not (buffer-live-p (car quit-config)))
7362       (gnus-configure-windows 'group 'force)
7363     (set-buffer (car quit-config))
7364     (unless (eq (cdr quit-config) 'group)
7365       (setq gnus-current-select-method
7366             (gnus-find-method-for-group gnus-newsgroup-name)))
7367     (cond ((eq major-mode 'gnus-summary-mode)
7368            (gnus-set-global-variables))
7369           ((eq major-mode 'gnus-article-mode)
7370            (save-current-buffer
7371              ;; The `gnus-summary-buffer' variable may point
7372              ;; to the old summary buffer when using a single
7373              ;; article buffer.
7374              (unless (gnus-buffer-live-p gnus-summary-buffer)
7375                (set-buffer gnus-group-buffer))
7376              (set-buffer gnus-summary-buffer)
7377              (gnus-set-global-variables))))
7378     (if (or (eq (cdr quit-config) 'article)
7379             (eq (cdr quit-config) 'pick))
7380         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7381             (gnus-configure-windows 'pick 'force)
7382           (gnus-configure-windows (cdr quit-config) 'force))
7383       (gnus-configure-windows (cdr quit-config) 'force))
7384     (when (eq major-mode 'gnus-summary-mode)
7385       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7386                                                      next-unread-noselect))
7387           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7388                                   'next-noselect)
7389                               (gnus-summary-next-subject 1 nil t))
7390                              ((eq gnus-auto-select-on-ephemeral-exit
7391                                   'next-unread-noselect)
7392                               (gnus-summary-next-subject 1 t t))))
7393             ;; Hide the article buffer which displays the article different
7394             ;; from the one that the cursor points to in the summary buffer.
7395             (gnus-configure-windows 'summary 'force))
7396         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7397                (gnus-summary-next-subject 1))
7398               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7399                (gnus-summary-next-subject 1 t))))
7400       (gnus-summary-recenter)
7401       (gnus-summary-position-point))))
7402
7403 ;;; Dead summaries.
7404
7405 (defvar gnus-dead-summary-mode-map
7406   (let ((map (make-keymap)))
7407     (suppress-keymap map)
7408     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7409     (dolist (key '("\C-d" "\r" "\177" [delete]))
7410       (define-key map key 'gnus-summary-wake-up-the-dead))
7411     (dolist (key '("q" "Q"))
7412       (define-key map key 'bury-buffer))
7413     map))
7414
7415 (define-minor-mode gnus-dead-summary-mode
7416   "Minor mode for Gnus summary buffers."
7417   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7418   (unless (derived-mode-p 'gnus-summary-mode)
7419     (setq gnus-dead-summary-mode nil)))
7420
7421 (defun gnus-deaden-summary ()
7422   "Make the current summary buffer into a dead summary buffer."
7423   ;; Kill any previous dead summary buffer.
7424   (when (and gnus-dead-summary
7425              (buffer-name gnus-dead-summary))
7426     (with-current-buffer gnus-dead-summary
7427       (when gnus-dead-summary-mode
7428         (kill-buffer (current-buffer)))))
7429   ;; Make this the current dead summary.
7430   (setq gnus-dead-summary (current-buffer))
7431   (gnus-dead-summary-mode 1)
7432   (let ((name (buffer-name)))
7433     (when (string-match "Summary" name)
7434       (rename-buffer
7435        (concat (substring name 0 (match-beginning 0)) "Dead "
7436                (substring name (match-beginning 0)))
7437        t)
7438       (bury-buffer))))
7439
7440 (defun gnus-kill-or-deaden-summary (buffer)
7441   "Kill or deaden the summary BUFFER."
7442   (save-excursion
7443     (when (and (buffer-name buffer)
7444                (not gnus-single-article-buffer))
7445       (with-current-buffer buffer
7446         (gnus-kill-buffer gnus-article-buffer)
7447         (gnus-kill-buffer gnus-original-article-buffer)))
7448     (cond
7449      ;; Kill the buffer.
7450      (gnus-kill-summary-on-exit
7451       (when (and gnus-use-trees
7452                  (gnus-buffer-exists-p buffer))
7453         (with-current-buffer buffer
7454           (gnus-tree-close gnus-newsgroup-name)))
7455       (gnus-kill-buffer buffer))
7456      ;; Deaden the buffer.
7457      ((gnus-buffer-exists-p buffer)
7458       (with-current-buffer buffer
7459         (gnus-deaden-summary))))))
7460
7461 (defun gnus-summary-wake-up-the-dead (&rest args)
7462   "Wake up the dead summary buffer."
7463   (interactive)
7464   (gnus-dead-summary-mode -1)
7465   (let ((name (buffer-name)))
7466     (when (string-match "Dead " name)
7467       (rename-buffer
7468        (concat (substring name 0 (match-beginning 0))
7469                (substring name (match-end 0)))
7470        t)))
7471   (gnus-message 3 "This dead summary is now alive again"))
7472
7473 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7474 (defun gnus-summary-describe-group (&optional force)
7475   "Describe the current newsgroup."
7476   (interactive "P")
7477   (gnus-group-describe-group force gnus-newsgroup-name))
7478
7479 (defun gnus-summary-describe-briefly ()
7480   "Describe summary mode commands briefly."
7481   (interactive)
7482   (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")))
7483
7484 ;; Walking around group mode buffer from summary mode.
7485
7486 (defun gnus-summary-next-group (&optional no-article target-group backward)
7487   "Exit current newsgroup and then select next unread newsgroup.
7488 If prefix argument NO-ARTICLE is non-nil, no article is selected
7489 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7490 previous group instead."
7491   (interactive "P")
7492   ;; Stop pre-fetching.
7493   (gnus-async-halt-prefetch)
7494   (let ((current-group gnus-newsgroup-name)
7495         (current-buffer (current-buffer))
7496         entered)
7497     ;; First we semi-exit this group to update Xrefs and all variables.
7498     ;; We can't do a real exit, because the window conf must remain
7499     ;; the same in case the user is prompted for info, and we don't
7500     ;; want the window conf to change before that...
7501     (gnus-summary-exit t)
7502     (while (not entered)
7503       ;; Then we find what group we are supposed to enter.
7504       (set-buffer gnus-group-buffer)
7505       (gnus-group-jump-to-group current-group)
7506       (setq target-group
7507             (or target-group
7508                 (if (eq gnus-keep-same-level 'best)
7509                     (gnus-summary-best-group gnus-newsgroup-name)
7510                   (gnus-summary-search-group backward gnus-keep-same-level))))
7511       (if (not target-group)
7512           ;; There are no further groups, so we return to the group
7513           ;; buffer.
7514           (progn
7515             (gnus-message 5 "Returning to the group buffer")
7516             (setq entered t)
7517             (when (gnus-buffer-live-p current-buffer)
7518               (set-buffer current-buffer)
7519               (gnus-summary-exit))
7520             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7521         ;; We try to enter the target group.
7522         (gnus-group-jump-to-group target-group)
7523         (let ((unreads (gnus-group-group-unread)))
7524           (if (and (or (eq t unreads)
7525                        (and unreads (not (zerop unreads))))
7526                    (gnus-summary-read-group
7527                     target-group nil no-article
7528                     (and (buffer-name current-buffer) current-buffer)
7529                     nil backward))
7530               (setq entered t)
7531             (setq current-group target-group
7532                   target-group nil)))))))
7533
7534 (defun gnus-summary-prev-group (&optional no-article)
7535   "Exit current newsgroup and then select previous unread newsgroup.
7536 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7537   (interactive "P")
7538   (gnus-summary-next-group no-article nil t))
7539
7540 ;; Walking around summary lines.
7541
7542 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7543   "Go to the first subject satisfying any non-nil constraint.
7544 If UNREAD is non-nil, the article should be unread.
7545 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7546 If UNSEEN is non-nil, the article should be unseen as well as unread.
7547 Returns the article selected or nil if there are no matching articles."
7548   (interactive "P")
7549   (cond
7550    ;; Empty summary.
7551    ((null gnus-newsgroup-data)
7552     (gnus-message 3 "No articles in the group")
7553     nil)
7554    ;; Pick the first article.
7555    ((not (or unread undownloaded unseen))
7556     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7557     (gnus-data-number (car gnus-newsgroup-data)))
7558    ;; Find the first unread article.
7559    (t
7560     (let ((data gnus-newsgroup-data))
7561       (while (and data
7562                   (let ((num (gnus-data-number (car data))))
7563                     (or (memq num gnus-newsgroup-unfetched)
7564                         (not (or (and unread
7565                                       (memq num gnus-newsgroup-unreads))
7566                                  (and undownloaded
7567                                       (memq num gnus-newsgroup-undownloaded))
7568                                  (and unseen
7569                                       (memq num gnus-newsgroup-unseen)
7570                                       (memq num gnus-newsgroup-unreads)))))))
7571         (setq data (cdr data)))
7572       (prog1
7573           (if data
7574               (progn
7575                 (goto-char (gnus-data-pos (car data)))
7576                 (gnus-data-number (car data)))
7577             (gnus-message 3 "No more%s articles"
7578                           (let* ((r (when unread " unread"))
7579                                  (d (when undownloaded " undownloaded"))
7580                                  (s (when unseen " unseen"))
7581                                  (l (delq nil (list r d s))))
7582                             (cond ((= 3 (length l))
7583                                    (concat r "," d ", or" s))
7584                                   ((= 2 (length l))
7585                                    (concat (car l) ", or" (cadr l)))
7586                                   ((= 1 (length l))
7587                                    (car l))
7588                                   (t
7589                                    ""))))
7590             nil
7591             )
7592         (gnus-summary-position-point))))))
7593
7594 (defun gnus-summary-next-subject (n &optional unread dont-display)
7595   "Go to next N'th summary line.
7596 If N is negative, go to the previous N'th subject line.
7597 If UNREAD is non-nil, only unread articles are selected.
7598 The difference between N and the actual number of steps taken is
7599 returned."
7600   (interactive "p")
7601   (let ((backward (< n 0))
7602         (n (abs n)))
7603     (while (and (> n 0)
7604                 (if backward
7605                     (gnus-summary-find-prev unread)
7606                   (gnus-summary-find-next unread)))
7607       (unless (zerop (setq n (1- n)))
7608         (gnus-summary-show-thread)))
7609     (when (/= 0 n)
7610       (gnus-message 7 "No more%s articles"
7611                     (if unread " unread" "")))
7612     (unless dont-display
7613       (gnus-summary-recenter)
7614       (gnus-summary-position-point))
7615     n))
7616
7617 (defun gnus-summary-next-unread-subject (n)
7618   "Go to next N'th unread summary line."
7619   (interactive "p")
7620   (gnus-summary-next-subject n t))
7621
7622 (defun gnus-summary-prev-subject (n &optional unread)
7623   "Go to previous N'th summary line.
7624 If optional argument UNREAD is non-nil, only unread article is selected."
7625   (interactive "p")
7626   (gnus-summary-next-subject (- n) unread))
7627
7628 (defun gnus-summary-prev-unread-subject (n)
7629   "Go to previous N'th unread summary line."
7630   (interactive "p")
7631   (gnus-summary-next-subject (- n) t))
7632
7633 (defun gnus-summary-goto-subjects (articles)
7634   "Insert the subject header for ARTICLES in the current buffer."
7635   (save-excursion
7636     (dolist (article articles)
7637       (gnus-summary-goto-subject article t)))
7638   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7639   (gnus-summary-position-point))
7640
7641 (defun gnus-summary-goto-subject (article &optional force silent)
7642   "Go to the subject line of ARTICLE.
7643 If FORCE, also allow jumping to articles not currently shown."
7644   (interactive "nArticle number: ")
7645   (unless (numberp article)
7646     (error "Article %s is not a number" article))
7647   (let ((b (point))
7648         (data (gnus-data-find article)))
7649     ;; We read in the article if we have to.
7650     (and (not data)
7651          force
7652          (gnus-summary-insert-subject
7653           article
7654           (if (or (numberp force) (vectorp force)) force)
7655           t)
7656          (setq data (gnus-data-find article)))
7657     (goto-char b)
7658     (if (not data)
7659         (progn
7660           (unless silent
7661             (gnus-message 3 "Can't find article %d" article))
7662           nil)
7663       (let ((pt (gnus-data-pos data)))
7664         (goto-char pt)
7665         (gnus-summary-set-article-display-arrow pt))
7666       (gnus-summary-position-point)
7667       article)))
7668
7669 ;; Walking around summary lines with displaying articles.
7670
7671 (defun gnus-summary-expand-window (&optional arg)
7672   "Make the summary buffer take up the entire Emacs frame.
7673 Given a prefix, will force an `article' buffer configuration."
7674   (interactive "P")
7675   (if arg
7676       (gnus-configure-windows 'article 'force)
7677     (gnus-configure-windows 'summary 'force)))
7678
7679 (defun gnus-summary-display-article (article &optional all-header)
7680   "Display ARTICLE in article buffer."
7681   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7682                (with-current-buffer gnus-article-buffer
7683                  (eq major-mode 'gnus-article-mode)))
7684     (gnus-article-setup-buffer))
7685   (gnus-set-global-variables)
7686   (with-current-buffer gnus-article-buffer
7687     (setq gnus-article-charset gnus-newsgroup-charset)
7688     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7689     (mm-enable-multibyte))
7690   (if (null article)
7691       nil
7692     (prog1
7693         (if gnus-summary-display-article-function
7694             (funcall gnus-summary-display-article-function article all-header)
7695           (gnus-article-prepare article all-header))
7696       (gnus-run-hooks 'gnus-select-article-hook)
7697       (when (and gnus-current-article
7698                  (not (zerop gnus-current-article)))
7699         (gnus-summary-goto-subject gnus-current-article))
7700       (gnus-summary-recenter)
7701       (when (and gnus-use-trees gnus-show-threads)
7702         (gnus-possibly-generate-tree article)
7703         (gnus-highlight-selected-tree article))
7704       ;; Successfully display article.
7705       (gnus-article-set-window-start
7706        (cdr (assq article gnus-newsgroup-bookmarks))))))
7707
7708 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7709   "Select the current article.
7710 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7711 non-nil, the article will be re-fetched even if it already present in
7712 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7713 be displayed."
7714   ;; Make sure we are in the summary buffer to work around bbdb bug.
7715   (unless (eq major-mode 'gnus-summary-mode)
7716     (set-buffer gnus-summary-buffer))
7717   (let ((article (or article (gnus-summary-article-number)))
7718         (all-headers (not (not all-headers))) ;Must be t or nil.
7719         gnus-summary-display-article-function)
7720     (and (not pseudo)
7721          (gnus-summary-article-pseudo-p article)
7722          (error "This is a pseudo-article"))
7723     (with-current-buffer gnus-summary-buffer
7724       (if (or (and gnus-single-article-buffer
7725                    (or (null gnus-current-article)
7726                        (null gnus-article-current)
7727                        (null (get-buffer gnus-article-buffer))
7728                        (not (eq article (cdr gnus-article-current)))
7729                        (not (equal (car gnus-article-current)
7730                                    gnus-newsgroup-name))
7731                        (not (get-buffer gnus-original-article-buffer))))
7732               (and (not gnus-single-article-buffer)
7733                    (or (null gnus-current-article)
7734                        (not (get-buffer gnus-original-article-buffer))
7735                        (not (eq gnus-current-article article))))
7736               force)
7737           ;; The requested article is different from the current article.
7738           (progn
7739             (gnus-summary-display-article article all-headers)
7740             (when (gnus-buffer-live-p gnus-article-buffer)
7741               (with-current-buffer gnus-article-buffer
7742                 (if (not gnus-article-decoded-p) ;; a local variable
7743                     (mm-disable-multibyte))))
7744             (gnus-article-set-window-start
7745              (cdr (assq article gnus-newsgroup-bookmarks)))
7746             article)
7747         'old))))
7748
7749 (defun gnus-summary-force-verify-and-decrypt ()
7750   "Display buttons for signed/encrypted parts and verify/decrypt them."
7751   (interactive)
7752   (let ((mm-verify-option 'known)
7753         (mm-decrypt-option 'known)
7754         (gnus-article-emulate-mime t)
7755         (gnus-buttonized-mime-types (append (list "multipart/signed"
7756                                                   "multipart/encrypted")
7757                                             gnus-buttonized-mime-types)))
7758     (gnus-summary-select-article nil 'force)))
7759
7760 (defun gnus-summary-set-current-mark (&optional current-mark)
7761   "Obsolete function."
7762   nil)
7763
7764 (defun gnus-summary-next-article (&optional unread subject backward push)
7765   "Select the next article.
7766 If UNREAD, only unread articles are selected.
7767 If SUBJECT, only articles with SUBJECT are selected.
7768 If BACKWARD, the previous article is selected instead of the next."
7769   (interactive "P")
7770   ;; Make sure we are in the summary buffer.
7771   (unless (eq major-mode 'gnus-summary-mode)
7772     (set-buffer gnus-summary-buffer))
7773   (cond
7774    ;; Is there such an article?
7775    ((and (gnus-summary-search-forward unread subject backward)
7776          (or (gnus-summary-display-article (gnus-summary-article-number))
7777              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7778     (gnus-summary-position-point))
7779    ;; If not, we try the first unread, if that is wanted.
7780    ((and subject
7781          gnus-auto-select-same
7782          (gnus-summary-first-unread-article))
7783     (gnus-summary-position-point)
7784     (gnus-message 6 "Wrapped"))
7785    ;; Try to get next/previous article not displayed in this group.
7786    ((and gnus-auto-extend-newsgroup
7787          (not unread) (not subject))
7788     (gnus-summary-goto-article
7789      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7790      nil (count-lines (point-min) (point))))
7791    ;; Go to next/previous group.
7792    (t
7793     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7794       (gnus-summary-jump-to-group gnus-newsgroup-name))
7795     (let ((cmd (if (featurep 'xemacs)
7796                    last-command-char
7797                  last-command-event))
7798           (point
7799            (with-current-buffer gnus-group-buffer
7800              (point)))
7801           (current-summary (current-buffer))
7802           (group
7803            (if (eq gnus-keep-same-level 'best)
7804                (gnus-summary-best-group gnus-newsgroup-name)
7805              (gnus-summary-search-group backward gnus-keep-same-level))))
7806       ;; Select next unread newsgroup automagically.
7807       (cond
7808        ((or (not gnus-auto-select-next)
7809             (not cmd))
7810         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7811        ((or (eq gnus-auto-select-next 'quietly)
7812             (and (eq gnus-auto-select-next 'slightly-quietly)
7813                  push)
7814             (and (eq gnus-auto-select-next 'almost-quietly)
7815                  (gnus-summary-last-article-p)))
7816         ;; Select quietly.
7817         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7818             (gnus-summary-exit)
7819           (gnus-message 7 "No more%s articles (%s)..."
7820                         (if unread " unread" "")
7821                         (if group (concat "selecting " group)
7822                           "exiting"))
7823           (gnus-summary-next-group nil group backward)))
7824        (t
7825         (when (gnus-key-press-event-p last-input-event)
7826           ;; Somehow or other, we may now have selected a different
7827           ;; window.  Make point go back to the summary buffer.
7828           (when (eq current-summary (current-buffer))
7829             ;; FIXME: This burps when get-buffer-window returns nil.
7830             (select-window (get-buffer-window current-summary 0)))
7831           (gnus-summary-walk-group-buffer
7832            gnus-newsgroup-name cmd unread backward point))))))))
7833
7834 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7835   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7836                       (?\C-p (gnus-group-prev-unread-group 1))))
7837         (cursor-in-echo-area t)
7838         keve key group ended prompt)
7839     (with-current-buffer gnus-group-buffer
7840       (goto-char start)
7841       (setq group
7842             (if (eq gnus-keep-same-level 'best)
7843                 (gnus-summary-best-group gnus-newsgroup-name)
7844               (gnus-summary-search-group backward gnus-keep-same-level))))
7845     (while (not ended)
7846       (setq prompt
7847             (format
7848              "No more%s articles%s " (if unread " unread" "")
7849              (if (and group
7850                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7851                  (format " (Type %s for %s [%s])"
7852                          (single-key-description cmd)
7853                          (gnus-group-decoded-name group)
7854                          (gnus-group-unread group))
7855                (format " (Type %s to exit %s)"
7856                        (single-key-description cmd)
7857                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7858       ;; Confirm auto selection.
7859       (setq key (car (setq keve (gnus-read-event-char prompt)))
7860             ended t)
7861       (cond
7862        ((assq key keystrokes)
7863         (let ((obuf (current-buffer)))
7864           (switch-to-buffer gnus-group-buffer)
7865           (when group
7866             (gnus-group-jump-to-group group))
7867           (eval (cadr (assq key keystrokes)))
7868           (setq group (gnus-group-group-name))
7869           (switch-to-buffer obuf))
7870         (setq ended nil))
7871        ((equal key cmd)
7872         (if (or (not group)
7873                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7874             (gnus-summary-exit)
7875           (gnus-summary-next-group nil group backward)))
7876        (t
7877         (push (cdr keve) unread-command-events))))))
7878
7879 (defun gnus-summary-next-unread-article ()
7880   "Select unread article after current one."
7881   (interactive)
7882   (gnus-summary-next-article
7883    (or (not (eq gnus-summary-goto-unread 'never))
7884        (gnus-summary-last-article-p (gnus-summary-article-number)))
7885    (and gnus-auto-select-same
7886         (gnus-summary-article-subject))))
7887
7888 (defun gnus-summary-prev-article (&optional unread subject)
7889   "Select the article before the current one.
7890 If UNREAD is non-nil, only unread articles are selected."
7891   (interactive "P")
7892   (gnus-summary-next-article unread subject t))
7893
7894 (defun gnus-summary-prev-unread-article ()
7895   "Select unread article before current one."
7896   (interactive)
7897   (gnus-summary-prev-article
7898    (or (not (eq gnus-summary-goto-unread 'never))
7899        (gnus-summary-first-article-p (gnus-summary-article-number)))
7900    (and gnus-auto-select-same
7901         (gnus-summary-article-subject))))
7902
7903 (defun gnus-summary-next-page (&optional lines circular stop)
7904   "Show next page of the selected article.
7905 If at the end of the current article, select the next article.
7906 LINES says how many lines should be scrolled up.
7907
7908 If CIRCULAR is non-nil, go to the start of the article instead of
7909 selecting the next article when reaching the end of the current
7910 article.
7911
7912 If STOP is non-nil, just stop when reaching the end of the message.
7913
7914 Also see the variable `gnus-article-skip-boring'."
7915   (interactive "P")
7916   (setq gnus-summary-buffer (current-buffer))
7917   (gnus-set-global-variables)
7918   (let ((article (gnus-summary-article-number))
7919         (article-window (get-buffer-window gnus-article-buffer t))
7920         endp)
7921     ;; If the buffer is empty, we have no article.
7922     (unless article
7923       (error "No article to select"))
7924     (gnus-configure-windows 'article)
7925     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7926         (if (and (eq gnus-summary-goto-unread 'never)
7927                  (not (gnus-summary-last-article-p article)))
7928             (gnus-summary-next-article)
7929           (gnus-summary-next-unread-article))
7930       (if (or (null gnus-current-article)
7931               (null gnus-article-current)
7932               (/= article (cdr gnus-article-current))
7933               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7934           ;; Selected subject is different from current article's.
7935           (gnus-summary-display-article article)
7936         (when article-window
7937           (gnus-eval-in-buffer-window gnus-article-buffer
7938             (setq endp (or (gnus-article-next-page lines)
7939                            (gnus-article-only-boring-p))))
7940           (when endp
7941             (cond ((or stop gnus-summary-stop-at-end-of-message)
7942                    (gnus-message 3 "End of message"))
7943                   (circular
7944                    (gnus-summary-beginning-of-article))
7945                   (lines
7946                    (gnus-message 3 "End of message"))
7947                   ((null lines)
7948                    (if (and (eq gnus-summary-goto-unread 'never)
7949                             (not (gnus-summary-last-article-p article)))
7950                        (gnus-summary-next-article)
7951                      (gnus-summary-next-unread-article))))))))
7952     (gnus-summary-recenter)
7953     (gnus-summary-position-point)))
7954
7955 (defun gnus-summary-prev-page (&optional lines move)
7956   "Show previous page of selected article.
7957 Argument LINES specifies lines to be scrolled down.
7958 If MOVE, move to the previous unread article if point is at
7959 the beginning of the buffer."
7960   (interactive "P")
7961   (let ((article (gnus-summary-article-number))
7962         (article-window (get-buffer-window gnus-article-buffer t))
7963         endp)
7964     (gnus-configure-windows 'article)
7965     (if (or (null gnus-current-article)
7966             (null gnus-article-current)
7967             (/= article (cdr gnus-article-current))
7968             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7969         ;; Selected subject is different from current article's.
7970         (gnus-summary-display-article article)
7971       (gnus-summary-recenter)
7972       (when article-window
7973         (gnus-eval-in-buffer-window gnus-article-buffer
7974           (setq endp (gnus-article-prev-page lines)))
7975         (when (and move endp)
7976           (cond (lines
7977                  (gnus-message 3 "Beginning of message"))
7978                 ((null lines)
7979                  (if (and (eq gnus-summary-goto-unread 'never)
7980                           (not (gnus-summary-first-article-p article)))
7981                      (gnus-summary-prev-article)
7982                    (gnus-summary-prev-unread-article))))))))
7983   (gnus-summary-position-point))
7984
7985 (defun gnus-summary-prev-page-or-article (&optional lines)
7986   "Show previous page of selected article.
7987 Argument LINES specifies lines to be scrolled down.
7988 If at the beginning of the article, go to the next article."
7989   (interactive "P")
7990   (gnus-summary-prev-page lines t))
7991
7992 (defun gnus-summary-scroll-up (lines)
7993   "Scroll up (or down) one line current article.
7994 Argument LINES specifies lines to be scrolled up (or down if negative).
7995 If no article is selected, then the current article will be selected first."
7996   (interactive "p")
7997   (gnus-configure-windows 'article)
7998   (gnus-summary-show-thread)
7999   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
8000     (gnus-eval-in-buffer-window gnus-article-buffer
8001       (cond ((> lines 0)
8002              (when (gnus-article-next-page lines)
8003                (gnus-message 3 "End of message")))
8004             ((< lines 0)
8005              (gnus-article-prev-page (- lines))))))
8006   (gnus-summary-recenter)
8007   (gnus-summary-position-point))
8008
8009 (defun gnus-summary-scroll-down (lines)
8010   "Scroll down (or up) one line current article.
8011 Argument LINES specifies lines to be scrolled down (or up if negative).
8012 If no article is selected, then the current article will be selected first."
8013   (interactive "p")
8014   (gnus-summary-scroll-up (- lines)))
8015
8016 (defun gnus-summary-next-same-subject ()
8017   "Select next article which has the same subject as current one."
8018   (interactive)
8019   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8020
8021 (defun gnus-summary-prev-same-subject ()
8022   "Select previous article which has the same subject as current one."
8023   (interactive)
8024   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8025
8026 (defun gnus-summary-next-unread-same-subject ()
8027   "Select next unread article which has the same subject as current one."
8028   (interactive)
8029   (gnus-summary-next-article t (gnus-summary-article-subject)))
8030
8031 (defun gnus-summary-prev-unread-same-subject ()
8032   "Select previous unread article which has the same subject as current one."
8033   (interactive)
8034   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8035
8036 (defun gnus-summary-first-unread-article ()
8037   "Select the first unread article.
8038 Return nil if there are no unread articles."
8039   (interactive)
8040   (prog1
8041       (when (gnus-summary-first-subject t)
8042         (gnus-summary-show-thread)
8043         (gnus-summary-first-subject t)
8044         (gnus-summary-display-article (gnus-summary-article-number)))
8045     (gnus-summary-position-point)))
8046
8047 (defun gnus-summary-first-unread-subject ()
8048   "Place the point on the subject line of the first unread article.
8049 Return nil if there are no unread articles."
8050   (interactive)
8051   (prog1
8052       (when (gnus-summary-first-subject t)
8053         (gnus-summary-show-thread)
8054         (gnus-summary-first-subject t))
8055     (gnus-summary-position-point)))
8056
8057 (defun gnus-summary-first-unseen-subject ()
8058   "Place the point on the subject line of the first unseen article.
8059 Return nil if there are no unseen articles."
8060   (interactive)
8061   (prog1
8062       (when (gnus-summary-first-subject nil nil t)
8063         (gnus-summary-show-thread)
8064         (gnus-summary-first-subject nil nil t))
8065     (gnus-summary-position-point)))
8066
8067 (defun gnus-summary-first-unseen-or-unread-subject ()
8068   "Place the point on the subject line of the first unseen and unread article.
8069 If all article have been seen, on the subject line of the first unread
8070 article."
8071   (interactive)
8072   (prog1
8073       (unless (when (gnus-summary-first-subject nil nil t)
8074                 (gnus-summary-show-thread)
8075                 (gnus-summary-first-subject nil nil t))
8076         (when (gnus-summary-first-subject t)
8077           (gnus-summary-show-thread)
8078           (gnus-summary-first-subject t)))
8079     (gnus-summary-position-point)))
8080
8081 (defun gnus-summary-first-article ()
8082   "Select the first article.
8083 Return nil if there are no articles."
8084   (interactive)
8085   (prog1
8086       (when (gnus-summary-first-subject)
8087         (gnus-summary-show-thread)
8088         (gnus-summary-first-subject)
8089         (gnus-summary-display-article (gnus-summary-article-number)))
8090     (gnus-summary-position-point)))
8091
8092 (defun gnus-summary-best-unread-article (&optional arg)
8093   "Select the unread article with the highest score.
8094 If given a prefix argument, select the next unread article that has a
8095 score higher than the default score."
8096   (interactive "P")
8097   (let ((article (if arg
8098                      (gnus-summary-better-unread-subject)
8099                    (gnus-summary-best-unread-subject))))
8100     (if article
8101         (gnus-summary-goto-article article)
8102       (error "No unread articles"))))
8103
8104 (defun gnus-summary-best-unread-subject ()
8105   "Select the unread subject with the highest score."
8106   (interactive)
8107   (let ((best -1000000)
8108         (data gnus-newsgroup-data)
8109         article score)
8110     (while data
8111       (and (gnus-data-unread-p (car data))
8112            (> (setq score
8113                     (gnus-summary-article-score (gnus-data-number (car data))))
8114               best)
8115            (setq best score
8116                  article (gnus-data-number (car data))))
8117       (setq data (cdr data)))
8118     (when article
8119       (gnus-summary-goto-subject article))
8120     (gnus-summary-position-point)
8121     article))
8122
8123 (defun gnus-summary-better-unread-subject ()
8124   "Select the first unread subject that has a score over the default score."
8125   (interactive)
8126   (let ((data gnus-newsgroup-data)
8127         article score)
8128     (while (and (setq article (gnus-data-number (car data)))
8129                 (or (gnus-data-read-p (car data))
8130                     (not (> (gnus-summary-article-score article)
8131                             gnus-summary-default-score))))
8132       (setq data (cdr data)))
8133     (when article
8134       (gnus-summary-goto-subject article))
8135     (gnus-summary-position-point)
8136     article))
8137
8138 (defun gnus-summary-last-subject ()
8139   "Go to the last displayed subject line in the group."
8140   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8141     (when article
8142       (gnus-summary-goto-subject article))))
8143
8144 (defun gnus-summary-goto-article (article &optional all-headers force)
8145   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8146 If ALL-HEADERS is non-nil, no header lines are hidden.
8147 If FORCE, go to the article even if it isn't displayed.  If FORCE
8148 is a number, it is the line the article is to be displayed on."
8149   (interactive
8150    (list
8151     (gnus-completing-read
8152      "Article number or Message-ID"
8153      (mapcar 'int-to-string gnus-newsgroup-limit))
8154     current-prefix-arg
8155     t))
8156   (prog1
8157       (if (and (stringp article)
8158                (string-match "@\\|%40" article))
8159           (gnus-summary-refer-article article)
8160         (when (stringp article)
8161           (setq article (string-to-number article)))
8162         (if (gnus-summary-goto-subject article force)
8163             (gnus-summary-display-article article all-headers)
8164           (gnus-message 4 "Couldn't go to article %s" article) nil))
8165     (gnus-summary-position-point)))
8166
8167 (defun gnus-summary-goto-last-article ()
8168   "Go to the previously read article."
8169   (interactive)
8170   (prog1
8171       (when gnus-last-article
8172         (gnus-summary-goto-article gnus-last-article nil t))
8173     (gnus-summary-position-point)))
8174
8175 (defun gnus-summary-pop-article (number)
8176   "Pop one article off the history and go to the previous.
8177 NUMBER articles will be popped off."
8178   (interactive "p")
8179   (let (to)
8180     (setq gnus-newsgroup-history
8181           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8182     (if to
8183         (gnus-summary-goto-article (car to) nil t)
8184       (error "Article history empty")))
8185   (gnus-summary-position-point))
8186
8187 ;; Summary commands and functions for limiting the summary buffer.
8188
8189 (defun gnus-summary-limit-to-articles (n)
8190   "Limit the summary buffer to the next N articles.
8191 If not given a prefix, use the process marked articles instead."
8192   (interactive "P")
8193   (prog1
8194       (let ((articles (gnus-summary-work-articles n)))
8195         (setq gnus-newsgroup-processable nil)
8196         (gnus-summary-limit articles))
8197     (gnus-summary-position-point)))
8198
8199 (defun gnus-summary-pop-limit (&optional total)
8200   "Restore the previous limit.
8201 If given a prefix, remove all limits."
8202   (interactive "P")
8203   (when total
8204     (setq gnus-newsgroup-limits
8205           (list (mapcar (lambda (h) (mail-header-number h))
8206                         gnus-newsgroup-headers))))
8207   (unless gnus-newsgroup-limits
8208     (error "No limit to pop"))
8209   (prog1
8210       (gnus-summary-limit nil 'pop)
8211     (gnus-summary-position-point)))
8212
8213 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8214   "Limit the summary buffer to articles that have subjects that match a regexp.
8215 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8216   (interactive
8217    (list (read-string (if current-prefix-arg
8218                           "Exclude subject (regexp): "
8219                         "Limit to subject (regexp): "))
8220          nil current-prefix-arg))
8221   (unless header
8222     (setq header "subject"))
8223   (when (not (equal "" subject))
8224     (prog1
8225         (let ((articles (gnus-summary-find-matching
8226                          (or header "subject") subject 'all nil nil
8227                          not-matching)))
8228           (unless articles
8229             (error "Found no matches for \"%s\"" subject))
8230           (gnus-summary-limit articles))
8231       (gnus-summary-position-point))))
8232
8233 (defun gnus-summary-limit-to-author (from &optional not-matching)
8234   "Limit the summary buffer to articles that have authors that match a regexp.
8235 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8236   (interactive
8237    (list (read-string (if current-prefix-arg
8238                           "Exclude author (regexp): "
8239                         "Limit to author (regexp): "))
8240          current-prefix-arg))
8241   (gnus-summary-limit-to-subject from "from" not-matching))
8242
8243 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8244   "Limit the summary buffer to articles with the given RECIPIENT.
8245
8246 If NOT-MATCHING, exclude RECIPIENT.
8247
8248 To and Cc headers are checked.  You need to include them in
8249 `nnmail-extra-headers'."
8250   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8251   (interactive
8252    (list (read-string (format "%s recipient (regexp): "
8253                               (if current-prefix-arg "Exclude" "Limit to")))
8254          current-prefix-arg))
8255   (when (not (equal "" recipient))
8256     (prog1 (let* ((to
8257                    (if (memq 'To nnmail-extra-headers)
8258                        (gnus-summary-find-matching
8259                         (cons 'extra 'To) recipient 'all nil nil
8260                         not-matching)
8261                      (gnus-message
8262                       1 "`To' isn't present in `nnmail-extra-headers'")
8263                      (sit-for 1)
8264                      nil))
8265                   (cc
8266                    (if (memq 'Cc nnmail-extra-headers)
8267                        (gnus-summary-find-matching
8268                         (cons 'extra 'Cc) recipient 'all nil nil
8269                         not-matching)
8270                      (gnus-message
8271                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8272                      (sit-for 1)
8273                      nil))
8274                   (articles
8275                    (if not-matching
8276                        ;; We need the numbers that are in both lists:
8277                        (mapcar (lambda (a)
8278                                  (and (memq a to) a))
8279                                cc)
8280                      (nconc to cc))))
8281              (unless articles
8282                (error "Found no matches for \"%s\"" recipient))
8283              (gnus-summary-limit articles))
8284       (gnus-summary-position-point))))
8285
8286 (defun gnus-summary-limit-to-address (address &optional not-matching)
8287   "Limit the summary buffer to articles with the given ADDRESS.
8288
8289 If NOT-MATCHING, exclude ADDRESS.
8290
8291 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8292 in `nnmail-extra-headers'."
8293   (interactive
8294    (list (read-string (format "%s address (regexp): "
8295                               (if current-prefix-arg "Exclude" "Limit to")))
8296          current-prefix-arg))
8297   (when (not (equal "" address))
8298     (prog1 (let* ((to
8299                    (if (memq 'To nnmail-extra-headers)
8300                        (gnus-summary-find-matching
8301                         (cons 'extra 'To) address 'all nil nil
8302                         not-matching)
8303                      (gnus-message
8304                       1 "`To' isn't present in `nnmail-extra-headers'")
8305                      (sit-for 1)
8306                      t))
8307                   (cc
8308                    (if (memq 'Cc nnmail-extra-headers)
8309                        (gnus-summary-find-matching
8310                         (cons 'extra 'Cc) address 'all nil nil
8311                         not-matching)
8312                      (gnus-message
8313                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8314                      (sit-for 1)
8315                      t))
8316                   (from
8317                    (gnus-summary-find-matching "from" address
8318                                                'all nil nil not-matching))
8319                   (articles
8320                    (if not-matching
8321                        ;; We need the numbers that are in all lists:
8322                        (if (eq cc t)
8323                            (if (eq to t)
8324                                from
8325                              (mapcar (lambda (a) (car (memq a from))) to))
8326                          (if (eq to t)
8327                              (mapcar (lambda (a) (car (memq a from))) cc)
8328                            (mapcar (lambda (a) (car (memq a from)))
8329                                    (mapcar (lambda (a) (car (memq a to)))
8330                                            cc))))
8331                      (nconc (if (eq to t) nil to)
8332                             (if (eq cc t) nil cc)
8333                             from))))
8334              (unless articles
8335                (error "Found no matches for \"%s\"" address))
8336              (gnus-summary-limit articles))
8337       (gnus-summary-position-point))))
8338
8339 (defun gnus-summary-limit-strange-charsets-predicate (header)
8340   (when (fboundp 'char-charset)
8341     (let ((string (concat (mail-header-subject header)
8342                           (mail-header-from header)))
8343           charset found)
8344       (dotimes (i (1- (length string)))
8345         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8346         (when (string-match "unicode\\|big\\|japanese" charset)
8347           (setq found t)))
8348       found)))
8349
8350 (defun gnus-summary-limit-to-predicate (predicate)
8351   "Limit to articles where PREDICATE returns non-nil.
8352 PREDICATE will be called with the header structures of the
8353 articles."
8354   (let ((articles nil)
8355         (case-fold-search t))
8356     (dolist (header gnus-newsgroup-headers)
8357       (when (funcall predicate header)
8358         (push (mail-header-number header) articles)))
8359     (gnus-summary-limit (nreverse articles))))
8360
8361 (defun gnus-summary-limit-to-age (age &optional younger-p)
8362   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8363 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8364 articles that are younger than AGE days."
8365   (interactive
8366    (let ((younger current-prefix-arg)
8367          (days-got nil)
8368          days)
8369      (while (not days-got)
8370        (setq days (if younger
8371                       (read-string "Limit to articles younger than (in days, older when negative): ")
8372                     (read-string
8373                      "Limit to articles older than (in days, younger when negative): ")))
8374        (when (> (length days) 0)
8375          (setq days (read days)))
8376        (if (numberp days)
8377            (progn
8378              (setq days-got t)
8379              (when (< days 0)
8380                (setq younger (not younger))
8381                (setq days (* days -1))))
8382          (message "Please enter a number.")
8383          (sleep-for 1)))
8384      (list days younger)))
8385   (prog1
8386       (let ((data gnus-newsgroup-data)
8387             (cutoff (days-to-time age))
8388             articles d date is-younger)
8389         (while (setq d (pop data))
8390           (when (and (vectorp (gnus-data-header d))
8391                      (setq date (mail-header-date (gnus-data-header d))))
8392             (setq is-younger (time-less-p
8393                               (time-since (gnus-date-get-time date))
8394                               cutoff))
8395             (when (if younger-p
8396                       is-younger
8397                     (not is-younger))
8398               (push (gnus-data-number d) articles))))
8399         (gnus-summary-limit (nreverse articles)))
8400     (gnus-summary-position-point)))
8401
8402 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8403   "Limit the summary buffer to articles that match an 'extra' header."
8404   (interactive
8405    (let ((header
8406           (intern
8407            (gnus-completing-read
8408             (if current-prefix-arg
8409                 "Exclude extra header"
8410               "Limit extra header")
8411             (mapcar 'symbol-name gnus-extra-headers)
8412             t nil nil
8413             (symbol-name (car gnus-extra-headers))))))
8414      (list header
8415            (read-string (format "%s header %s (regexp): "
8416                                 (if current-prefix-arg "Exclude" "Limit to")
8417                                 header))
8418            current-prefix-arg)))
8419   (when (not (equal "" regexp))
8420     (prog1
8421         (let ((articles (gnus-summary-find-matching
8422                          (cons 'extra header) regexp 'all nil nil
8423                          not-matching)))
8424           (unless articles
8425             (error "Found no matches for \"%s\"" regexp))
8426           (gnus-summary-limit articles))
8427       (gnus-summary-position-point))))
8428
8429 (defun gnus-summary-limit-to-display-predicate ()
8430   "Limit the summary buffer to the predicated in the `display' group parameter."
8431   (interactive)
8432   (unless gnus-newsgroup-display
8433     (error "There is no `display' group parameter"))
8434   (let (articles)
8435     (dolist (gnus-number gnus-newsgroup-articles)
8436       (when (funcall gnus-newsgroup-display)
8437         (push gnus-number articles)))
8438     (gnus-summary-limit articles))
8439   (gnus-summary-position-point))
8440
8441 (defun gnus-summary-limit-to-unread (&optional all)
8442   "Limit the summary buffer to articles that are not marked as read.
8443 If ALL is non-nil, limit strictly to unread articles."
8444   (interactive "P")
8445   (if all
8446       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8447     (gnus-summary-limit-to-marks
8448      ;; Concat all the marks that say that an article is read and have
8449      ;; those removed.
8450      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8451            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8452            gnus-low-score-mark gnus-expirable-mark
8453            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8454            gnus-duplicate-mark)
8455      'reverse)))
8456
8457 (defun gnus-summary-limit-to-headers (match &optional reverse)
8458   "Limit the summary buffer to articles that have headers that match MATCH.
8459 If REVERSE (the prefix), limit to articles that don't match."
8460   (interactive "sMatch headers (regexp): \nP")
8461   (gnus-summary-limit-to-bodies match reverse t))
8462
8463 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8464   "Limit the summary buffer to articles that have bodies that match MATCH.
8465 If REVERSE (the prefix), limit to articles that don't match."
8466   (interactive "sMatch body (regexp): \nP")
8467   (let ((articles nil)
8468         (gnus-select-article-hook nil)  ;Disable hook.
8469         (gnus-article-prepare-hook nil)
8470         (gnus-use-article-prefetch nil)
8471         (gnus-keep-backlog nil)
8472         (gnus-break-pages nil)
8473         (gnus-summary-display-arrow nil)
8474         (gnus-updated-mode-lines nil)
8475         (gnus-auto-center-summary nil)
8476         (gnus-display-mime-function nil))
8477     (dolist (data gnus-newsgroup-data)
8478       (let (gnus-mark-article-hook)
8479         (gnus-summary-select-article t t nil (gnus-data-number data)))
8480       (with-current-buffer gnus-article-buffer
8481         (article-goto-body)
8482         (let* ((case-fold-search t)
8483                (found (if headersp
8484                           (re-search-backward match nil t)
8485                         (re-search-forward match nil t))))
8486           (when (or (and found
8487                          (not reverse))
8488                     (and (not found)
8489                          reverse))
8490             (push (gnus-data-number data) articles)))))
8491     (if (not articles)
8492         (message "No messages matched")
8493       (gnus-summary-limit articles)))
8494   (gnus-summary-position-point))
8495
8496 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8497   "Limit the summary buffer to articles that aren't part on any thread.
8498 If THREADSP (the prefix), limit to articles that are in threads."
8499   (interactive "P")
8500   (let ((articles nil)
8501         thread-articles
8502         threads)
8503     (dolist (thread gnus-newsgroup-threads)
8504       (if (stringp (car thread))
8505           (dolist (thread (cdr thread))
8506             (push thread threads))
8507         (push thread threads)))
8508     (dolist (thread threads)
8509       (setq thread-articles (gnus-articles-in-thread thread))
8510       (when (or (and threadsp
8511                      (> (length thread-articles) 1))
8512                 (and (not threadsp)
8513                      (= (length thread-articles) 1)))
8514         (setq articles (nconc thread-articles articles))))
8515     (if (not articles)
8516         (message "No messages matched")
8517       (gnus-summary-limit articles))
8518     (gnus-summary-position-point)))
8519
8520 (defun gnus-summary-limit-to-replied (&optional unreplied)
8521   "Limit the summary buffer to replied articles.
8522 If UNREPLIED (the prefix), limit to unreplied articles."
8523   (interactive "P")
8524   (if unreplied
8525       (gnus-summary-limit
8526        (gnus-set-difference gnus-newsgroup-articles
8527         gnus-newsgroup-replied))
8528     (gnus-summary-limit gnus-newsgroup-replied))
8529   (gnus-summary-position-point))
8530
8531 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8532   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8533 If REVERSE, limit the summary buffer to articles that are marked
8534 with MARKS.  MARKS can either be a string of marks or a list of marks.
8535 Returns how many articles were removed."
8536   (interactive "sMarks: ")
8537   (gnus-summary-limit-to-marks marks t))
8538
8539 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8540   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8541 If REVERSE (the prefix), limit the summary buffer to articles that are
8542 not marked with MARKS.  MARKS can either be a string of marks or a
8543 list of marks.
8544 Returns how many articles were removed."
8545   (interactive "sMarks: \nP")
8546   (prog1
8547       (let ((data gnus-newsgroup-data)
8548             (marks (if (listp marks) marks
8549                      (append marks nil))) ; Transform to list.
8550             articles)
8551         (while data
8552           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8553                   (memq (gnus-data-mark (car data)) marks))
8554             (push (gnus-data-number (car data)) articles))
8555           (setq data (cdr data)))
8556         (gnus-summary-limit articles))
8557     (gnus-summary-position-point)))
8558
8559 (defun gnus-summary-limit-to-score (score)
8560   "Limit to articles with score at or above SCORE."
8561   (interactive "NLimit to articles with score of at least: ")
8562   (let ((data gnus-newsgroup-data)
8563         articles)
8564     (while data
8565       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8566                 score)
8567         (push (gnus-data-number (car data)) articles))
8568       (setq data (cdr data)))
8569     (prog1
8570         (gnus-summary-limit articles)
8571       (gnus-summary-position-point))))
8572
8573 (defun gnus-summary-limit-to-unseen ()
8574   "Limit to unseen articles."
8575   (interactive)
8576   (prog1
8577       (gnus-summary-limit gnus-newsgroup-unseen)
8578     (gnus-summary-position-point)))
8579
8580 (defun gnus-summary-limit-include-thread (id)
8581   "Display all the hidden articles that is in the thread with ID in it.
8582 When called interactively, ID is the Message-ID of the current
8583 article."
8584   (interactive (list (mail-header-id (gnus-summary-article-header))))
8585   (let ((articles (gnus-articles-in-thread
8586                    (gnus-id-to-thread (gnus-root-id id))))
8587         ;;we REALLY want the whole thread---this prevents cut-threads
8588         ;;from removing the thread we want to include.
8589         (gnus-fetch-old-headers nil)
8590         (gnus-build-sparse-threads nil))
8591     (prog1
8592         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8593       (gnus-summary-limit-include-matching-articles
8594        "subject"
8595        (regexp-quote (gnus-simplify-subject-re
8596                       (mail-header-subject (gnus-id-to-header id)))))
8597       (gnus-summary-position-point))))
8598
8599 (defun gnus-summary-limit-include-matching-articles (header regexp)
8600   "Display all the hidden articles that have HEADERs that match REGEXP."
8601   (interactive (list (read-string "Match on header: ")
8602                      (read-string "Regexp: ")))
8603   (let ((articles (gnus-find-matching-articles header regexp)))
8604     (prog1
8605         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8606       (gnus-summary-position-point))))
8607
8608 (defun gnus-summary-insert-dormant-articles ()
8609   "Insert all the dormant articles for this group into the current buffer."
8610   (interactive)
8611   (let ((gnus-verbose (max 6 gnus-verbose)))
8612     (if (not gnus-newsgroup-dormant)
8613         (gnus-message 3 "No dormant articles for this group")
8614       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8615
8616 (defun gnus-summary-insert-ticked-articles ()
8617   "Insert ticked articles for this group into the current buffer."
8618   (interactive)
8619   (let ((gnus-verbose (max 6 gnus-verbose)))
8620     (if (not gnus-newsgroup-marked)
8621         (gnus-message 3 "No ticked articles for this group")
8622       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8623
8624 (defun gnus-summary-limit-include-dormant ()
8625   "Display all the hidden articles that are marked as dormant.
8626 Note that this command only works on a subset of the articles currently
8627 fetched for this group."
8628   (interactive)
8629   (unless gnus-newsgroup-dormant
8630     (error "There are no dormant articles in this group"))
8631   (prog1
8632       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8633     (gnus-summary-position-point)))
8634
8635 (defun gnus-summary-include-articles (articles)
8636   "Fetch the headers for ARTICLES and then display the summary lines."
8637   (let ((gnus-inhibit-demon t)
8638         (gnus-agent nil)
8639         (gnus-read-all-available-headers t))
8640     (setq gnus-newsgroup-headers
8641           (gnus-merge
8642            'list gnus-newsgroup-headers
8643            (gnus-fetch-headers articles nil t)
8644            'gnus-article-sort-by-number))
8645     (setq gnus-newsgroup-articles
8646           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8647     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8648
8649 (defun gnus-summary-limit-exclude-dormant ()
8650   "Hide all dormant articles."
8651   (interactive)
8652   (prog1
8653       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8654     (gnus-summary-position-point)))
8655
8656 (defun gnus-summary-limit-exclude-childless-dormant ()
8657   "Hide all dormant articles that have no children."
8658   (interactive)
8659   (let ((data (gnus-data-list t))
8660         articles d children)
8661     ;; Find all articles that are either not dormant or have
8662     ;; children.
8663     (while (setq d (pop data))
8664       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8665                 (and (setq children
8666                            (gnus-article-children (gnus-data-number d)))
8667                      (let (found)
8668                        (while children
8669                          (when (memq (car children) articles)
8670                            (setq children nil
8671                                  found t))
8672                          (pop children))
8673                        found)))
8674         (push (gnus-data-number d) articles)))
8675     ;; Do the limiting.
8676     (prog1
8677         (gnus-summary-limit articles)
8678       (gnus-summary-position-point))))
8679
8680 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8681   "Mark all unread excluded articles as read.
8682 If ALL, mark even excluded ticked and dormants as read."
8683   (interactive "P")
8684   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8685   (let ((articles (gnus-sorted-ndifference
8686                    (sort
8687                     (mapcar (lambda (h) (mail-header-number h))
8688                             gnus-newsgroup-headers)
8689                     '<)
8690                    gnus-newsgroup-limit))
8691         article)
8692     (setq gnus-newsgroup-unreads
8693           (gnus-sorted-intersection gnus-newsgroup-unreads
8694                                     gnus-newsgroup-limit))
8695     (if all
8696         (setq gnus-newsgroup-dormant nil
8697               gnus-newsgroup-marked nil
8698               gnus-newsgroup-reads
8699               (nconc
8700                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8701                gnus-newsgroup-reads))
8702       (while (setq article (pop articles))
8703         (unless (or (memq article gnus-newsgroup-dormant)
8704                     (memq article gnus-newsgroup-marked))
8705           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8706
8707 (defun gnus-summary-limit (articles &optional pop)
8708   (if pop
8709       ;; We pop the previous limit off the stack and use that.
8710       (setq articles (car gnus-newsgroup-limits)
8711             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8712     ;; We use the new limit, so we push the old limit on the stack.
8713     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8714   ;; Set the limit.
8715   (setq gnus-newsgroup-limit articles)
8716   (let ((total (length gnus-newsgroup-data))
8717         (data (gnus-data-find-list (gnus-summary-article-number)))
8718         (gnus-summary-mark-below nil)   ; Inhibit this.
8719         found)
8720     ;; This will do all the work of generating the new summary buffer
8721     ;; according to the new limit.
8722     (gnus-summary-prepare)
8723     ;; Hide any threads, possibly.
8724     (gnus-summary-maybe-hide-threads)
8725     ;; Try to return to the article you were at, or one in the
8726     ;; neighborhood.
8727     (when data
8728       ;; We try to find some article after the current one.
8729       (while data
8730         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8731           (setq data nil
8732                 found t))
8733         (setq data (cdr data))))
8734     (unless found
8735       ;; If there is no data, that means that we were after the last
8736       ;; article.  The same goes when we can't find any articles
8737       ;; after the current one.
8738       (goto-char (point-max))
8739       (gnus-summary-find-prev))
8740     (gnus-set-mode-line 'summary)
8741     ;; We return how many articles were removed from the summary
8742     ;; buffer as a result of the new limit.
8743     (- total (length gnus-newsgroup-data))))
8744
8745 (defsubst gnus-invisible-cut-children (threads)
8746   (let ((num 0))
8747     (while threads
8748       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8749         (incf num))
8750       (pop threads))
8751     (< num 2)))
8752
8753 (defsubst gnus-cut-thread (thread)
8754   "Go forwards in the thread until we find an article that we want to display."
8755   (when (or (eq gnus-fetch-old-headers 'some)
8756             (eq gnus-fetch-old-headers 'invisible)
8757             (numberp gnus-fetch-old-headers)
8758             (eq gnus-build-sparse-threads 'some)
8759             (eq gnus-build-sparse-threads 'more))
8760     ;; Deal with old-fetched headers and sparse threads.
8761     (while (and
8762             thread
8763             (or
8764              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8765              (gnus-summary-article-ancient-p
8766               (mail-header-number (car thread))))
8767             (if (or (<= (length (cdr thread)) 1)
8768                     (eq gnus-fetch-old-headers 'invisible))
8769                 (setq gnus-newsgroup-limit
8770                       (delq (mail-header-number (car thread))
8771                             gnus-newsgroup-limit)
8772                       thread (cadr thread))
8773               (when (gnus-invisible-cut-children (cdr thread))
8774                 (let ((th (cdr thread)))
8775                   (while th
8776                     (if (memq (mail-header-number (caar th))
8777                               gnus-newsgroup-limit)
8778                         (setq thread (car th)
8779                               th nil)
8780                       (setq th (cdr th))))))))))
8781   thread)
8782
8783 (defun gnus-cut-threads (threads)
8784   "Cut off all uninteresting articles from the beginning of THREADS."
8785   (when (or (eq gnus-fetch-old-headers 'some)
8786             (eq gnus-fetch-old-headers 'invisible)
8787             (numberp gnus-fetch-old-headers)
8788             (eq gnus-build-sparse-threads 'some)
8789             (eq gnus-build-sparse-threads 'more))
8790     (let ((th threads))
8791       (while th
8792         (setcar th (gnus-cut-thread (car th)))
8793         (setq th (cdr th)))))
8794   ;; Remove nixed out threads.
8795   (delq nil threads))
8796
8797 (defun gnus-summary-initial-limit (&optional show-if-empty)
8798   "Figure out what the initial limit is supposed to be on group entry.
8799 This entails weeding out unwanted dormants, low-scored articles,
8800 fetch-old-headers verbiage, and so on."
8801   ;; Most groups have nothing to remove.
8802   (unless (or gnus-inhibit-limiting
8803               (and (null gnus-newsgroup-dormant)
8804                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8805                    (not (eq gnus-fetch-old-headers 'some))
8806                    (not (numberp gnus-fetch-old-headers))
8807                    (not (eq gnus-fetch-old-headers 'invisible))
8808                    (null gnus-summary-expunge-below)
8809                    (not (eq gnus-build-sparse-threads 'some))
8810                    (not (eq gnus-build-sparse-threads 'more))
8811                    (null gnus-thread-expunge-below)))
8812     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8813     (setq gnus-newsgroup-limit nil)
8814     (mapatoms
8815      (lambda (node)
8816        (unless (car (symbol-value node))
8817          ;; These threads have no parents -- they are roots.
8818          (let ((nodes (cdr (symbol-value node)))
8819                thread)
8820            (while nodes
8821              (if (and gnus-thread-expunge-below
8822                       (< (gnus-thread-total-score (car nodes))
8823                          gnus-thread-expunge-below))
8824                  (gnus-expunge-thread (pop nodes))
8825                (setq thread (pop nodes))
8826                (gnus-summary-limit-children thread))))))
8827      gnus-newsgroup-dependencies)
8828     ;; If this limitation resulted in an empty group, we might
8829     ;; pop the previous limit and use it instead.
8830     (when (and (not gnus-newsgroup-limit)
8831                show-if-empty)
8832       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8833     gnus-newsgroup-limit))
8834
8835 (defun gnus-summary-limit-children (thread)
8836   "Return 1 if this subthread is visible and 0 if it is not."
8837   ;; First we get the number of visible children to this thread.  This
8838   ;; is done by recursing down the thread using this function, so this
8839   ;; will really go down to a leaf article first, before slowly
8840   ;; working its way up towards the root.
8841   (when thread
8842     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8843            (children
8844            (if (cdr thread)
8845                (apply '+ (mapcar 'gnus-summary-limit-children
8846                                  (cdr thread)))
8847              0))
8848            (number (mail-header-number (car thread)))
8849            score)
8850       (if (and
8851            (not (memq number gnus-newsgroup-marked))
8852            (or
8853             ;; If this article is dormant and has absolutely no visible
8854             ;; children, then this article isn't visible.
8855             (and (memq number gnus-newsgroup-dormant)
8856                  (zerop children))
8857             ;; If this is "fetch-old-headered" and there is no
8858             ;; visible children, then we don't want this article.
8859             (and (or (eq gnus-fetch-old-headers 'some)
8860                      (numberp gnus-fetch-old-headers))
8861                  (gnus-summary-article-ancient-p number)
8862                  (zerop children))
8863             ;; If this is "fetch-old-headered" and `invisible', then
8864             ;; we don't want this article.
8865             (and (eq gnus-fetch-old-headers 'invisible)
8866                  (gnus-summary-article-ancient-p number))
8867             ;; If this is a sparsely inserted article with no children,
8868             ;; we don't want it.
8869             (and (eq gnus-build-sparse-threads 'some)
8870                  (gnus-summary-article-sparse-p number)
8871                  (zerop children))
8872             ;; If we use expunging, and this article is really
8873             ;; low-scored, then we don't want this article.
8874             (when (and gnus-summary-expunge-below
8875                        (< (setq score
8876                                 (or (cdr (assq number gnus-newsgroup-scored))
8877                                     gnus-summary-default-score))
8878                           gnus-summary-expunge-below))
8879               ;; We increase the expunge-tally here, but that has
8880               ;; nothing to do with the limits, really.
8881               (incf gnus-newsgroup-expunged-tally)
8882               ;; We also mark as read here, if that's wanted.
8883               (when (and gnus-summary-mark-below
8884                          (< score gnus-summary-mark-below))
8885                 (setq gnus-newsgroup-unreads
8886                       (delq number gnus-newsgroup-unreads))
8887                 (if gnus-newsgroup-auto-expire
8888                     (push number gnus-newsgroup-expirable)
8889                   (push (cons number gnus-low-score-mark)
8890                         gnus-newsgroup-reads)))
8891               t)
8892             ;; Do the `display' group parameter.
8893             (and gnus-newsgroup-display
8894                  (let ((gnus-number number))
8895                    (not (funcall gnus-newsgroup-display))))))
8896           ;; Nope, invisible article.
8897           0
8898         ;; Ok, this article is to be visible, so we add it to the limit
8899         ;; and return 1.
8900         (push number gnus-newsgroup-limit)
8901         1))))
8902
8903 (defun gnus-expunge-thread (thread)
8904   "Mark all articles in THREAD as read."
8905   (let* ((number (mail-header-number (car thread))))
8906     (incf gnus-newsgroup-expunged-tally)
8907     ;; We also mark as read here, if that's wanted.
8908     (setq gnus-newsgroup-unreads
8909           (delq number gnus-newsgroup-unreads))
8910     (if gnus-newsgroup-auto-expire
8911         (push number gnus-newsgroup-expirable)
8912       (push (cons number gnus-low-score-mark)
8913             gnus-newsgroup-reads)))
8914   ;; Go recursively through all subthreads.
8915   (mapcar 'gnus-expunge-thread (cdr thread)))
8916
8917 ;; Summary article oriented commands
8918
8919 (defun gnus-summary-refer-parent-article (n)
8920   "Refer parent article N times.
8921 If N is negative, go to ancestor -N instead.
8922 The difference between N and the number of articles fetched is returned."
8923   (interactive "p")
8924   (let ((skip 1)
8925         error header ref)
8926     (when (not (natnump n))
8927       (setq skip (abs n)
8928             n 1))
8929     (while (and (> n 0)
8930                 (not error))
8931       (setq header (gnus-summary-article-header))
8932       (if (and (eq (mail-header-number header)
8933                    (cdr gnus-article-current))
8934                (equal gnus-newsgroup-name
8935                       (car gnus-article-current)))
8936           ;; If we try to find the parent of the currently
8937           ;; displayed article, then we take a look at the actual
8938           ;; References header, since this is slightly more
8939           ;; reliable than the References field we got from the
8940           ;; server.
8941           (with-current-buffer gnus-original-article-buffer
8942             (nnheader-narrow-to-headers)
8943             (unless (setq ref (message-fetch-field "references"))
8944               (when (setq ref (message-fetch-field "in-reply-to"))
8945                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8946             (widen))
8947         (setq ref
8948               ;; It's not the current article, so we take a bet on
8949               ;; the value we got from the server.
8950               (mail-header-references header)))
8951       (if (and ref
8952                (not (equal ref "")))
8953           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8954             (gnus-message 1 "Couldn't find parent"))
8955         (gnus-message 1 "No references in article %d"
8956                       (gnus-summary-article-number))
8957         (setq error t))
8958       (decf n))
8959     (gnus-summary-position-point)
8960     n))
8961
8962 (defun gnus-summary-refer-references ()
8963   "Fetch all articles mentioned in the References header.
8964 Return the number of articles fetched."
8965   (interactive)
8966   (let ((ref (mail-header-references (gnus-summary-article-header)))
8967         (current (gnus-summary-article-number))
8968         (n 0))
8969     (if (or (not ref)
8970             (equal ref ""))
8971         (error "No References in the current article")
8972       ;; For each Message-ID in the References header...
8973       (while (string-match "<[^>]*>" ref)
8974         (incf n)
8975         ;; ... fetch that article.
8976         (gnus-summary-refer-article
8977          (prog1 (match-string 0 ref)
8978            (setq ref (substring ref (match-end 0))))))
8979       (gnus-summary-goto-subject current)
8980       (gnus-summary-position-point)
8981       n)))
8982
8983 (defun gnus-delete-duplicate-headers (headers)
8984   ;; First remove leading duplicates.
8985   (while (and (> (length headers) 1)
8986               (= (mail-header-number (car headers))
8987                  (mail-header-number (cadr headers))))
8988     (pop headers))
8989   ;; Then the rest.
8990   (let ((result headers))
8991     (while (> (length headers) 1)
8992       (if (= (mail-header-number (car headers))
8993              (mail-header-number (cadr headers)))
8994           (setcdr headers (cddr headers))
8995         (pop headers)))
8996     result))
8997
8998 (defun gnus-summary-refer-thread (&optional limit)
8999   "Fetch all articles in the current thread. For backends that
9000 know how to search for threads (currently only 'nnimap) a
9001 non-numeric prefix arg will use nnir to search the entire
9002 server; without a prefix arg only the current group is
9003 searched. If the variable `gnus-refer-thread-use-nnir' is
9004 non-nil the prefix arg has the reverse meaning. If no
9005 backend-specific 'request-thread function is available fetch
9006 LIMIT (the numerical prefix) old headers. If LIMIT is
9007 non-numeric or nil fetch the number specified by the
9008 `gnus-refer-thread-limit' variable."
9009   (interactive "P")
9010   (gnus-warp-to-article)
9011   (let* ((header (gnus-summary-article-header))
9012          (id (mail-header-id header))
9013          (gnus-inhibit-demon t)
9014          (gnus-summary-ignore-duplicates t)
9015          (gnus-read-all-available-headers t)
9016          (gnus-refer-thread-use-nnir
9017           (if (and (not (null limit)) (listp limit))
9018               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9019          (new-headers
9020           (if (gnus-check-backend-function
9021                'request-thread gnus-newsgroup-name)
9022               (gnus-request-thread header gnus-newsgroup-name)
9023             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9024                             gnus-refer-thread-limit))
9025                    (last (if (numberp limit)
9026                              (min (+ (mail-header-number header)
9027                                      limit)
9028                                   gnus-newsgroup-highest)
9029                            gnus-newsgroup-highest))
9030                    (subject (gnus-simplify-subject
9031                              (mail-header-subject header)))
9032                    (refs (split-string (or (mail-header-references header)
9033                                            "")))
9034                    (gnus-parse-headers-hook
9035                     `(lambda () (goto-char (point-min))
9036                       (keep-lines
9037                        (regexp-opt ',(append refs (list id subject)))))))
9038               (gnus-fetch-headers (list last) (if (numberp limit)
9039                                                   (* 2 limit) limit) t))))
9040          article-ids)
9041     (when (listp new-headers)
9042       (dolist (header new-headers)
9043         (push (mail-header-number header) article-ids)
9044         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9045           (push (mail-header-number header) gnus-newsgroup-unreads)
9046           (setq gnus-newsgroup-unselected
9047                 (delete (mail-header-number header)
9048                         gnus-newsgroup-unselected))))
9049       (setq gnus-newsgroup-headers
9050             (gnus-delete-duplicate-headers
9051              (gnus-merge
9052               'list gnus-newsgroup-headers new-headers
9053               'gnus-article-sort-by-number)))
9054       (setq gnus-newsgroup-articles
9055             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9056       (gnus-summary-limit-include-thread id)))
9057   (gnus-summary-show-thread))
9058
9059 (defun gnus-summary-refer-article (message-id)
9060   "Fetch an article specified by MESSAGE-ID."
9061   (interactive "sMessage-ID: ")
9062   (gnus-warp-to-article)
9063   (when (and (stringp message-id)
9064              (not (zerop (length message-id))))
9065     (setq message-id (gnus-replace-in-string message-id " " ""))
9066     ;; Construct the correct Message-ID if necessary.
9067     ;; Suggested by tale@pawl.rpi.edu.
9068     (unless (string-match "^<" message-id)
9069       (setq message-id (concat "<" message-id)))
9070     (unless (string-match ">$" message-id)
9071       (setq message-id (concat message-id ">")))
9072     ;; People often post MIDs from URLs, so unhex it:
9073     (unless (string-match "@" message-id)
9074       (setq message-id (gnus-url-unhex-string message-id)))
9075     (let* ((header (gnus-id-to-header message-id))
9076            (sparse (and header
9077                         (gnus-summary-article-sparse-p
9078                          (mail-header-number header))
9079                         (memq (mail-header-number header)
9080                               gnus-newsgroup-limit)))
9081            number)
9082       (cond
9083        ;; If the article is present in the buffer we just go to it.
9084        ((and header
9085              (or (not (gnus-summary-article-sparse-p
9086                        (mail-header-number header)))
9087                  sparse))
9088         (prog1
9089             (gnus-summary-goto-article
9090              (mail-header-number header) nil t)
9091           (when sparse
9092             (gnus-summary-update-article (mail-header-number header)))))
9093        (t
9094         ;; We fetch the article.
9095         (catch 'found
9096           (dolist (gnus-override-method (gnus-refer-article-methods))
9097             (when (and (gnus-check-server gnus-override-method)
9098                        ;; Fetch the header,
9099                        (setq number (gnus-summary-insert-subject message-id)))
9100               ;; and display the article.
9101               (gnus-summary-select-article nil nil nil number)
9102               (throw 'found t)))
9103           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9104
9105 (defun gnus-refer-article-methods ()
9106   "Return a list of referable methods."
9107   (cond
9108    ;; No method, so we default to current and native.
9109    ((null gnus-refer-article-method)
9110     (list gnus-current-select-method gnus-select-method))
9111    ;; Current.
9112    ((eq 'current gnus-refer-article-method)
9113     (list gnus-current-select-method))
9114    ;; List of select methods.
9115    ((not (and (symbolp (car gnus-refer-article-method))
9116               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9117     (let (out)
9118       (dolist (method gnus-refer-article-method)
9119         (push (if (eq 'current method)
9120                   gnus-current-select-method
9121                 (if (eq 'nnir (car method))
9122                     (list
9123                      'nnir
9124                      (or (cadr method)
9125                          (gnus-method-to-server gnus-current-select-method)))
9126                   method))
9127               out))
9128       (nreverse out)))
9129    ;; One single select method.
9130    (t
9131     (list gnus-refer-article-method))))
9132
9133 (defun gnus-summary-edit-parameters ()
9134   "Edit the group parameters of the current group."
9135   (interactive)
9136   (gnus-group-edit-group gnus-newsgroup-name 'params))
9137
9138 (defun gnus-summary-customize-parameters ()
9139   "Customize the group parameters of the current group."
9140   (interactive)
9141   (gnus-group-customize gnus-newsgroup-name))
9142
9143 (defun gnus-summary-enter-digest-group (&optional force)
9144   "Enter an nndoc group based on the current article.
9145 If FORCE, force a digest interpretation.  If not, try to guess
9146 what the document format is.
9147
9148 To control what happens when you exit the group, see the
9149 `gnus-auto-select-on-ephemeral-exit' variable."
9150   (interactive "P")
9151   (let ((conf gnus-current-window-configuration))
9152     (save-window-excursion
9153       (save-excursion
9154         (let (gnus-article-prepare-hook
9155               gnus-display-mime-function
9156               gnus-break-pages)
9157           (gnus-summary-select-article))))
9158     (setq gnus-current-window-configuration conf)
9159     (let* ((name (format "%s-%d"
9160                          (gnus-group-prefixed-name
9161                           gnus-newsgroup-name (list 'nndoc ""))
9162                          (with-current-buffer gnus-summary-buffer
9163                            gnus-current-article)))
9164            (ogroup gnus-newsgroup-name)
9165            (params (append (gnus-info-params (gnus-get-info ogroup))
9166                            (list (cons 'to-group ogroup))
9167                            (list (cons 'parent-group ogroup))
9168                            (list (cons 'save-article-group ogroup))))
9169            (case-fold-search t)
9170            (buf (current-buffer))
9171            dig to-address)
9172       (with-current-buffer gnus-original-article-buffer
9173         ;; Have the digest group inherit the main mail address of
9174         ;; the parent article.
9175         (when (setq to-address (or (gnus-fetch-field "reply-to")
9176                                    (gnus-fetch-field "from")))
9177           (setq params
9178                 (append
9179                  (list (cons 'to-address
9180                              (funcall gnus-decode-encoded-address-function
9181                                       to-address))))))
9182         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9183         (insert-buffer-substring gnus-original-article-buffer)
9184         ;; Remove lines that may lead nndoc to misinterpret the
9185         ;; document type.
9186         (narrow-to-region
9187          (goto-char (point-min))
9188          (or (search-forward "\n\n" nil t) (point)))
9189         (goto-char (point-min))
9190         (delete-matching-lines "^Path:\\|^From ")
9191         (widen))
9192       (unwind-protect
9193           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9194                     (gnus-newsgroup-ephemeral-ignored-charsets
9195                      gnus-newsgroup-ignored-charsets))
9196                 (gnus-group-read-ephemeral-group
9197                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9198                               (nndoc-article-type
9199                                ,(if force 'mbox 'guess)))
9200                  t nil nil nil
9201                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9202                                                         "ADAPT")))))
9203               ;; Make all postings to this group go to the parent group.
9204               (nconc (gnus-info-params (gnus-get-info name))
9205                      params)
9206             ;; Couldn't select this doc group.
9207             (switch-to-buffer buf)
9208             (gnus-set-global-variables)
9209             (gnus-configure-windows 'summary)
9210             (gnus-message 3 "Article couldn't be entered?"))
9211         (kill-buffer dig)))))
9212
9213 (defun gnus-summary-read-document (n)
9214   "Open a new group based on the current article(s).
9215 This will allow you to read digests and other similar
9216 documents as newsgroups.
9217 Obeys the standard process/prefix convention."
9218   (interactive "P")
9219   (let* ((ogroup gnus-newsgroup-name)
9220          (params (append (gnus-info-params (gnus-get-info ogroup))
9221                          (list (cons 'to-group ogroup))))
9222          group egroup groups vgroup)
9223     (dolist (article (gnus-summary-work-articles n))
9224       (setq group (format "%s-%d" gnus-newsgroup-name article))
9225       (gnus-summary-remove-process-mark article)
9226       (when (gnus-summary-display-article article)
9227         (save-excursion ;;What for?
9228           (with-temp-buffer
9229             (insert-buffer-substring gnus-original-article-buffer)
9230             ;; Remove some headers that may lead nndoc to make
9231             ;; the wrong guess.
9232             (message-narrow-to-head)
9233             (goto-char (point-min))
9234             (delete-matching-lines "^Path:\\|^From ")
9235             (widen)
9236             (if (setq egroup
9237                       (gnus-group-read-ephemeral-group
9238                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9239                                      (nndoc-article-type guess))
9240                        t nil t))
9241                 (progn
9242                   ;; Make all postings to this group go to the parent group.
9243                   (nconc (gnus-info-params (gnus-get-info egroup))
9244                          params)
9245                   (push egroup groups))
9246               ;; Couldn't select this doc group.
9247               (gnus-error 3 "Article couldn't be entered"))))))
9248     ;; Now we have selected all the documents.
9249     (cond
9250      ((not groups)
9251       (error "None of the articles could be interpreted as documents"))
9252      ((gnus-group-read-ephemeral-group
9253        (setq vgroup (format
9254                      "nnvirtual:%s-%s" gnus-newsgroup-name
9255                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9256        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9257        t
9258        (cons (current-buffer) 'summary)))
9259      (t
9260       (error "Couldn't select virtual nndoc group")))))
9261
9262 (defun gnus-summary-widget-forward (arg)
9263   "Move point to the next field or button in the article.
9264 With optional ARG, move across that many fields."
9265   (interactive "p")
9266   (gnus-summary-select-article)
9267   (gnus-configure-windows 'article)
9268   (select-window (gnus-get-buffer-window gnus-article-buffer))
9269   (widget-forward arg))
9270
9271 (defun gnus-summary-widget-backward (arg)
9272   "Move point to the previous field or button in the article.
9273 With optional ARG, move across that many fields."
9274   (interactive "p")
9275   (gnus-summary-select-article)
9276   (gnus-configure-windows 'article)
9277   (select-window (gnus-get-buffer-window gnus-article-buffer))
9278   (unless (widget-at (point))
9279     (goto-char (point-max)))
9280   (widget-backward arg))
9281
9282 (defun gnus-summary-isearch-article (&optional regexp-p)
9283   "Do incremental search forward on the current article.
9284 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9285   (interactive "P")
9286   (gnus-summary-select-article)
9287   (gnus-configure-windows 'article)
9288   (gnus-eval-in-buffer-window gnus-article-buffer
9289     (save-restriction
9290       (widen)
9291       (isearch-forward regexp-p))))
9292
9293 (defun gnus-summary-repeat-search-article-forward ()
9294   "Repeat the previous search forwards."
9295   (interactive)
9296   (unless gnus-last-search-regexp
9297     (error "No previous search"))
9298   (gnus-summary-search-article-forward gnus-last-search-regexp))
9299
9300 (defun gnus-summary-repeat-search-article-backward ()
9301   "Repeat the previous search backwards."
9302   (interactive)
9303   (unless gnus-last-search-regexp
9304     (error "No previous search"))
9305   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9306
9307 (defun gnus-summary-search-article-forward (regexp &optional backward)
9308   "Search for an article containing REGEXP forward.
9309 If BACKWARD, search backward instead."
9310   (interactive
9311    (list (read-string
9312           (format "Search article %s (regexp%s): "
9313                   (if current-prefix-arg "backward" "forward")
9314                   (if gnus-last-search-regexp
9315                       (concat ", default " gnus-last-search-regexp)
9316                     "")))
9317          current-prefix-arg))
9318   (if (string-equal regexp "")
9319       (setq regexp (or gnus-last-search-regexp ""))
9320     (setq gnus-last-search-regexp regexp)
9321     (setq gnus-article-before-search gnus-current-article))
9322   ;; Intentionally set gnus-last-article.
9323   (setq gnus-last-article gnus-article-before-search)
9324   (let ((gnus-last-article gnus-last-article))
9325     (if (gnus-summary-search-article regexp backward)
9326         (gnus-summary-show-thread)
9327       (signal 'search-failed (list regexp)))))
9328
9329 (defun gnus-summary-search-article-backward (regexp)
9330   "Search for an article containing REGEXP backward."
9331   (interactive
9332    (list (read-string
9333           (format "Search article backward (regexp%s): "
9334                   (if gnus-last-search-regexp
9335                       (concat ", default " gnus-last-search-regexp)
9336                     "")))))
9337   (gnus-summary-search-article-forward regexp 'backward))
9338
9339 (defun gnus-summary-search-article (regexp &optional backward)
9340   "Search for an article containing REGEXP.
9341 Optional argument BACKWARD means do search for backward.
9342 `gnus-select-article-hook' is not called during the search."
9343   ;; We have to require this here to make sure that the following
9344   ;; dynamic binding isn't shadowed by autoloading.
9345   (require 'gnus-async)
9346   (require 'gnus-art)
9347   (let ((gnus-select-article-hook nil)  ;Disable hook.
9348         (gnus-article-prepare-hook nil)
9349         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9350         (gnus-use-article-prefetch nil)
9351         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9352         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9353         (gnus-visual nil)
9354         (gnus-keep-backlog nil)
9355         (gnus-break-pages nil)
9356         (gnus-summary-display-arrow nil)
9357         (gnus-updated-mode-lines nil)
9358         (gnus-auto-center-summary nil)
9359         (sum (current-buffer))
9360         (gnus-display-mime-function nil)
9361         (found nil)
9362         point)
9363     (gnus-save-hidden-threads
9364       (gnus-summary-select-article)
9365       (set-buffer gnus-article-buffer)
9366       (goto-char (window-point (get-buffer-window (current-buffer))))
9367       (when backward
9368         (forward-line -1))
9369       (while (not found)
9370         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9371         (if (if backward
9372                 (re-search-backward regexp nil t)
9373               (re-search-forward regexp nil t))
9374             ;; We found the regexp.
9375             (progn
9376               (setq found 'found)
9377               (beginning-of-line)
9378               (set-window-start
9379                (get-buffer-window (current-buffer))
9380                (point))
9381               (forward-line 1)
9382               (set-window-point
9383                (get-buffer-window (current-buffer))
9384                (point))
9385               (set-buffer sum)
9386               (setq point (point)))
9387           ;; We didn't find it, so we go to the next article.
9388           (set-buffer sum)
9389           (setq found 'not)
9390           (while (eq found 'not)
9391             (if (not (if backward (gnus-summary-find-prev)
9392                        (gnus-summary-find-next)))
9393                 ;; No more articles.
9394                 (setq found t)
9395               ;; Select the next article and adjust point.
9396               (unless (gnus-summary-article-sparse-p
9397                        (gnus-summary-article-number))
9398                 (setq found nil)
9399                 (gnus-summary-select-article)
9400                 (set-buffer gnus-article-buffer)
9401                 (widen)
9402                 (goto-char (if backward (point-max) (point-min))))))))
9403       (gnus-message 7 ""))
9404     ;; Return whether we found the regexp.
9405     (when (eq found 'found)
9406       (goto-char point)
9407       (gnus-summary-show-thread)
9408       (gnus-summary-goto-subject gnus-current-article)
9409       (gnus-summary-position-point)
9410       t)))
9411
9412 (defun gnus-find-matching-articles (header regexp)
9413   "Return a list of all articles that match REGEXP on HEADER.
9414 This search includes all articles in the current group that Gnus has
9415 fetched headers for, whether they are displayed or not."
9416   (let ((articles nil)
9417         ;; Can't eta-reduce because it's a macro.
9418         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9419         (case-fold-search t))
9420     (dolist (header gnus-newsgroup-headers)
9421       (when (string-match regexp (funcall func header))
9422         (push (mail-header-number header) articles)))
9423     (nreverse articles)))
9424
9425 (defun gnus-summary-find-matching (header regexp &optional backward unread
9426                                           not-case-fold not-matching)
9427   "Return a list of all articles that match REGEXP on HEADER.
9428 The search stars on the current article and goes forwards unless
9429 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9430 If UNREAD is non-nil, only unread articles will
9431 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9432 in the comparisons. If NOT-MATCHING, return a list of all articles that
9433 not match REGEXP on HEADER."
9434   (let ((case-fold-search (not not-case-fold))
9435         articles d func)
9436     (if (consp header)
9437         (if (eq (car header) 'extra)
9438             (setq func
9439                   `(lambda (h)
9440                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9441                          "")))
9442           (error "%s is an invalid header" header))
9443       (unless (fboundp (intern (concat "mail-header-" header)))
9444         (error "%s is not a valid header" header))
9445       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9446     (dolist (d (if (eq backward 'all)
9447                    gnus-newsgroup-data
9448                  (gnus-data-find-list
9449                   (gnus-summary-article-number)
9450                   (gnus-data-list backward))))
9451       (when (and (or (not unread)       ; We want all articles...
9452                      (gnus-data-unread-p d)) ; Or just unreads.
9453                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9454                  (if not-matching
9455                      (not (string-match
9456                            regexp
9457                            (funcall func (gnus-data-header d))))
9458                    (string-match regexp
9459                                  (funcall func (gnus-data-header d)))))
9460         (push (gnus-data-number d) articles))) ; Success!
9461     (nreverse articles)))
9462
9463 (defun gnus-summary-execute-command (header regexp command &optional backward)
9464   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9465 If HEADER is an empty string (or nil), the match is done on the entire
9466 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9467   (interactive
9468    (list (let ((completion-ignore-case t))
9469            (gnus-completing-read
9470             "Header name"
9471             (mapcar 'symbol-name
9472                     (append
9473                      '(Number Subject From Lines Date
9474                        Message-ID Xref References Body)
9475                      gnus-extra-headers))
9476             'require-match))
9477          (read-string "Regexp: ")
9478          (read-key-sequence "Command: ")
9479          current-prefix-arg))
9480   (when (equal header "Body")
9481     (setq header ""))
9482   ;; Hidden thread subtrees must be searched as well.
9483   (gnus-summary-show-all-threads)
9484   ;; We don't want to change current point nor window configuration.
9485   (save-excursion
9486     (save-window-excursion
9487       (let (gnus-visual
9488             gnus-treat-strip-trailing-blank-lines
9489             gnus-treat-strip-leading-blank-lines
9490             gnus-treat-strip-multiple-blank-lines
9491             gnus-treat-hide-boring-headers
9492             gnus-treat-fold-newsgroups
9493             gnus-article-prepare-hook)
9494         (gnus-message 6 "Executing %s..." (key-description command))
9495         ;; We'd like to execute COMMAND interactively so as to give arguments.
9496         (gnus-execute header regexp
9497                       `(call-interactively ',(key-binding command))
9498                       backward)
9499         (gnus-message 6 "Executing %s...done" (key-description command))))))
9500
9501 (defun gnus-summary-beginning-of-article ()
9502   "Scroll the article back to the beginning."
9503   (interactive)
9504   (gnus-summary-select-article)
9505   (gnus-configure-windows 'article)
9506   (gnus-eval-in-buffer-window gnus-article-buffer
9507     (widen)
9508     (goto-char (point-min))
9509     (when gnus-break-pages
9510       (gnus-narrow-to-page))))
9511
9512 (defun gnus-summary-end-of-article ()
9513   "Scroll to the end of the article."
9514   (interactive)
9515   (gnus-summary-select-article)
9516   (gnus-configure-windows 'article)
9517   (gnus-eval-in-buffer-window gnus-article-buffer
9518     (widen)
9519     (goto-char (point-max))
9520     (recenter -3)
9521     (when gnus-break-pages
9522       (gnus-narrow-to-page))))
9523
9524 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9525   "Truncate to LEN and quote all \"(\"'s in STRING."
9526   (gnus-replace-in-string (if (and len (> (length string) len))
9527                               (substring string 0 len)
9528                             string)
9529                           "[()]" "\\\\\\&"))
9530
9531 (defun gnus-summary-print-article (&optional filename n)
9532   "Generate and print a PostScript image of the process-marked (mail) articles.
9533
9534 If used interactively, print the current article if none are
9535 process-marked.  With prefix arg, prompt the user for the name of the
9536 file to save in.
9537
9538 When used from Lisp, accept two optional args FILENAME and N.  N means
9539 to print the next N articles.  If N is negative, print the N previous
9540 articles.  If N is nil and articles have been marked with the process
9541 mark, print these instead.
9542
9543 If the optional first argument FILENAME is nil, send the image to the
9544 printer.  If FILENAME is a string, save the PostScript image in a file with
9545 that name.  If FILENAME is a number, prompt the user for the name of the file
9546 to save in."
9547   (interactive (list (ps-print-preprint current-prefix-arg)))
9548   (dolist (article (gnus-summary-work-articles n))
9549     (gnus-summary-select-article nil nil 'pseudo article)
9550     (gnus-eval-in-buffer-window gnus-article-buffer
9551       (gnus-print-buffer))
9552     (gnus-summary-remove-process-mark article))
9553   (ps-despool filename))
9554
9555 (defun gnus-print-buffer ()
9556   (let ((ps-left-header
9557          (list
9558           (concat "("
9559                   (gnus-summary-print-truncate-and-quote
9560                    (mail-header-subject gnus-current-headers)
9561                    66) ")")
9562           (concat "("
9563                   (gnus-summary-print-truncate-and-quote
9564                    (mail-header-from gnus-current-headers)
9565                    45) ")")))
9566         (ps-right-header
9567          (list
9568           "/pagenumberstring load"
9569           (concat "("
9570                   (mail-header-date gnus-current-headers) ")"))))
9571     (gnus-run-hooks 'gnus-ps-print-hook)
9572     (save-excursion
9573       (if ps-print-color-p
9574           (ps-spool-buffer-with-faces)
9575         (ps-spool-buffer)))))
9576
9577 (defun gnus-summary-show-complete-article ()
9578   "Show a complete version of the current article.
9579 This is only useful if you're looking at a partial version of the
9580 article currently."
9581   (interactive)
9582   (let ((gnus-keep-backlog nil)
9583         (gnus-use-cache nil)
9584         (gnus-agent nil)
9585         (variable (intern
9586                    (format "%s-fetch-partial-articles"
9587                            (car (gnus-find-method-for-group
9588                                  gnus-newsgroup-name)))
9589                    obarray))
9590         old-val)
9591     (unwind-protect
9592         (progn
9593           (setq old-val (symbol-value variable))
9594           (set variable nil)
9595           (gnus-flush-original-article-buffer)
9596           (gnus-summary-show-article))
9597       (set variable old-val))))
9598
9599 (defun gnus-summary-show-article (&optional arg)
9600   "Force redisplaying of the current article.
9601 If ARG (the prefix) is a number, show the article with the charset
9602 defined in `gnus-summary-show-article-charset-alist', or the charset
9603 input.
9604 If ARG (the prefix) is non-nil and not a number, show the article,
9605 but without running any of the article treatment functions
9606 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9607 C-u g', show the raw article."
9608   (interactive "P")
9609   (cond
9610    ((numberp arg)
9611     (gnus-summary-show-article t)
9612     (let ((gnus-newsgroup-charset
9613            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9614                (mm-read-coding-system
9615                 "View as charset: " ;; actually it is coding system.
9616                 (with-current-buffer gnus-article-buffer
9617                   (mm-detect-coding-region (point) (point-max))))))
9618           (gnus-newsgroup-ignored-charsets 'gnus-all))
9619       (gnus-summary-select-article nil 'force)
9620       (let ((deps gnus-newsgroup-dependencies)
9621             head header lines)
9622         (with-current-buffer gnus-original-article-buffer
9623           (save-restriction
9624             (message-narrow-to-head)
9625             (setq head (buffer-string))
9626             (goto-char (point-min))
9627             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9628               (goto-char (point-max))
9629               (widen)
9630               (setq lines (1- (count-lines (point) (point-max))))))
9631           (with-temp-buffer
9632             (insert (format "211 %d Article retrieved.\n"
9633                             (cdr gnus-article-current)))
9634             (insert head)
9635             (if lines (insert (format "Lines: %d\n" lines)))
9636             (insert ".\n")
9637             (let ((nntp-server-buffer (current-buffer)))
9638               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9639         (gnus-data-set-header
9640          (gnus-data-find (cdr gnus-article-current))
9641          header)
9642         (gnus-summary-update-article-line
9643          (cdr gnus-article-current) header)
9644         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9645           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9646    ((not arg)
9647     ;; Select the article the normal way.
9648     (if (eq mm-text-html-renderer 'shr)
9649         (progn
9650           (require 'shr)
9651           (let ((shr-ignore-cache t))
9652             (gnus-summary-select-article nil 'force)))
9653       (gnus-summary-select-article nil 'force)))
9654    ((equal arg '(16))
9655     ;; C-u C-u g
9656     (let ((gnus-inhibit-article-treatments t))
9657       (gnus-summary-select-article nil 'force)))
9658    (t
9659     ;; We have to require this here to make sure that the following
9660     ;; dynamic binding isn't shadowed by autoloading.
9661     (require 'gnus-async)
9662     (require 'gnus-art)
9663     ;; Bind the article treatment functions to nil.
9664     (let ((gnus-have-all-headers t)
9665           gnus-article-prepare-hook
9666           gnus-article-decode-hook
9667           gnus-display-mime-function
9668           gnus-break-pages)
9669       ;; Destroy any MIME parts.
9670       (when (gnus-buffer-live-p gnus-article-buffer)
9671         (with-current-buffer gnus-article-buffer
9672           (gnus-article-stop-animations)
9673           (mm-destroy-parts gnus-article-mime-handles)
9674           ;; Set it to nil for safety reason.
9675           (setq gnus-article-mime-handle-alist nil)
9676           (setq gnus-article-mime-handles nil)))
9677       (gnus-summary-select-article nil 'force))))
9678   (gnus-summary-goto-subject gnus-current-article)
9679   (gnus-summary-position-point))
9680
9681 (defun gnus-summary-show-raw-article ()
9682   "Show the raw article without any article massaging functions being run."
9683   (interactive)
9684   (gnus-summary-show-article t))
9685
9686 (defun gnus-summary-verbose-headers (&optional arg)
9687   "Toggle permanent full header display.
9688 If ARG is a positive number, turn header display on.
9689 If ARG is a negative number, turn header display off."
9690   (interactive "P")
9691   (setq gnus-show-all-headers
9692         (cond ((or (not (numberp arg))
9693                    (zerop arg))
9694                (not gnus-show-all-headers))
9695               ((natnump arg)
9696                t)))
9697   (gnus-summary-show-article))
9698
9699 (defun gnus-summary-toggle-header (&optional arg)
9700   "Show the headers if they are hidden, or hide them if they are shown.
9701 If ARG is a positive number, show the entire header.
9702 If ARG is a negative number, hide the unwanted header lines."
9703   (interactive "P")
9704   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9705                      (get-buffer-window gnus-article-buffer t))))
9706     (with-current-buffer gnus-article-buffer
9707       (widen)
9708       (article-narrow-to-head)
9709       (let* ((inhibit-read-only t)
9710              (inhibit-point-motion-hooks t)
9711              (hidden (if (numberp arg)
9712                          (>= arg 0)
9713                        (or (not (looking-at "[^ \t\n]+:"))
9714                            (gnus-article-hidden-text-p 'headers))))
9715              s e)
9716         (delete-region (point-min) (point-max))
9717         (with-current-buffer gnus-original-article-buffer
9718           (goto-char (setq s (point-min)))
9719           (setq e (if (search-forward "\n\n" nil t)
9720                       (1- (point))
9721                     (point-max))))
9722         (insert-buffer-substring gnus-original-article-buffer s e)
9723         (run-hooks 'gnus-article-decode-hook)
9724         (if hidden
9725             (let ((gnus-treat-hide-headers nil)
9726                   (gnus-treat-hide-boring-headers nil))
9727               (gnus-delete-wash-type 'headers)
9728               (gnus-treat-article 'head))
9729           (gnus-treat-article 'head))
9730         (widen)
9731         (if window
9732             (set-window-start window (goto-char (point-min))))
9733         (if gnus-break-pages
9734             (gnus-narrow-to-page)
9735           (when (gnus-visual-p 'page-marker)
9736             (let ((inhibit-read-only t))
9737               (gnus-remove-text-with-property 'gnus-prev)
9738               (gnus-remove-text-with-property 'gnus-next))))
9739         (gnus-set-mode-line 'article)))))
9740
9741 (defun gnus-summary-show-all-headers ()
9742   "Make all header lines visible."
9743   (interactive)
9744   (gnus-summary-toggle-header 1))
9745
9746 (defun gnus-summary-caesar-message (&optional arg)
9747   "Caesar rotate the current article by 13.
9748 With a non-numerical prefix, also rotate headers.  A numerical
9749 prefix specifies how many places to rotate each letter forward."
9750   (interactive "P")
9751   (gnus-summary-select-article)
9752   (let ((mail-header-separator ""))
9753     (gnus-eval-in-buffer-window gnus-article-buffer
9754       (save-restriction
9755         (widen)
9756         (let ((start (window-start))
9757               (inhibit-read-only t))
9758           (if (equal arg '(4))
9759               (message-caesar-buffer-body nil t)
9760             (message-caesar-buffer-body arg))
9761           (set-window-start (get-buffer-window (current-buffer)) start)))))
9762   ;; Create buttons and stuff...
9763   (gnus-treat-article nil))
9764
9765 (declare-function idna-to-unicode "ext:idna" (str))
9766
9767 (defun gnus-summary-idna-message (&optional arg)
9768   "Decode IDNA encoded domain names in the current articles.
9769 IDNA encoded domain names looks like `xn--bar'.  If a string
9770 remain unencoded after running this function, it is likely an
9771 invalid IDNA string (`xn--bar' is invalid).
9772
9773 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9774 installed for this command to work."
9775   (interactive "P")
9776   (if (not (and (condition-case nil (require 'idna)
9777                   (file-error))
9778                 (mm-coding-system-p 'utf-8)
9779                 (executable-find (symbol-value 'idna-program))))
9780       (gnus-message
9781        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9782     (gnus-summary-select-article)
9783     (let ((mail-header-separator ""))
9784       (gnus-eval-in-buffer-window gnus-article-buffer
9785         (save-restriction
9786           (widen)
9787           (let ((start (window-start))
9788                 buffer-read-only)
9789             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9790               (replace-match (idna-to-unicode (match-string 1))))
9791             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9792
9793 (defun gnus-summary-morse-message (&optional arg)
9794   "Morse decode the current article."
9795   (interactive "P")
9796   (gnus-summary-select-article)
9797   (let ((mail-header-separator ""))
9798     (gnus-eval-in-buffer-window gnus-article-buffer
9799       (save-excursion
9800         (save-restriction
9801           (widen)
9802           (let ((pos (window-start))
9803                 (inhibit-read-only t))
9804             (goto-char (point-min))
9805             (when (message-goto-body)
9806               (gnus-narrow-to-body))
9807             (goto-char (point-min))
9808             (while (search-forward "·" (point-max) t)
9809               (replace-match "."))
9810             (unmorse-region (point-min) (point-max))
9811             (widen)
9812             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9813
9814 (defun gnus-summary-stop-page-breaking ()
9815   "Stop page breaking in the current article."
9816   (interactive)
9817   (gnus-summary-select-article)
9818   (gnus-eval-in-buffer-window gnus-article-buffer
9819     (widen)
9820     (when (gnus-visual-p 'page-marker)
9821       (let ((inhibit-read-only t))
9822         (gnus-remove-text-with-property 'gnus-prev)
9823         (gnus-remove-text-with-property 'gnus-next))
9824       (setq gnus-page-broken nil))))
9825
9826 (defun gnus-summary-move-article (&optional n to-newsgroup
9827                                             select-method action)
9828   "Move the current article to a different newsgroup.
9829 If N is a positive number, move the N next articles.
9830 If N is a negative number, move the N previous articles.
9831 If N is nil and any articles have been marked with the process mark,
9832 move those articles instead.
9833 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9834 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9835 re-spool using this method.
9836
9837 When called interactively with TO-NEWSGROUP being nil, the value of
9838 the variable `gnus-move-split-methods' is used for finding a default
9839 for the target newsgroup.
9840
9841 For this function to work, both the current newsgroup and the
9842 newsgroup that you want to move to have to support the `request-move'
9843 and `request-accept' functions.
9844
9845 ACTION can be either `move' (the default), `crosspost' or `copy'."
9846   (interactive "P")
9847   (unless action
9848     (setq action 'move))
9849   ;; Check whether the source group supports the required functions.
9850   (cond ((and (eq action 'move)
9851               (not (gnus-check-backend-function
9852                     'request-move-article gnus-newsgroup-name)))
9853          (error "The current group does not support article moving"))
9854         ((and (eq action 'crosspost)
9855               (not (gnus-check-backend-function
9856                     'request-replace-article gnus-newsgroup-name)))
9857          (error "The current group does not support article editing")))
9858   (let ((articles (gnus-summary-work-articles n))
9859         (prefix (if (gnus-check-backend-function
9860                      'request-move-article gnus-newsgroup-name)
9861                     (funcall gnus-move-group-prefix-function
9862                              gnus-newsgroup-name)
9863                   ""))
9864         (names '((move "Move" "Moving")
9865                  (copy "Copy" "Copying")
9866                  (crosspost "Crosspost" "Crossposting")))
9867         (copy-buf (save-excursion
9868                     (nnheader-set-temp-buffer " *copy article*")))
9869         art-group to-method new-xref article to-groups
9870         articles-to-update-marks encoded)
9871     (unless (assq action names)
9872       (error "Unknown action %s" action))
9873     ;; Read the newsgroup name.
9874     (when (and (not to-newsgroup)
9875                (not select-method))
9876       (if (and gnus-move-split-methods
9877                (not
9878                 (and (memq gnus-current-article articles)
9879                      (gnus-buffer-live-p gnus-original-article-buffer))))
9880           ;; When `gnus-move-split-methods' is non-nil, we have to
9881           ;; select an article to give `gnus-read-move-group-name' an
9882           ;; opportunity to suggest an appropriate default.  However,
9883           ;; we needn't render or mark the article.
9884           (let ((gnus-display-mime-function nil)
9885                 (gnus-article-prepare-hook nil)
9886                 (gnus-mark-article-hook nil))
9887             (gnus-summary-select-article nil nil nil (car articles))))
9888       (setq to-newsgroup (gnus-read-move-group-name
9889                           (cadr (assq action names))
9890                           (symbol-value
9891                            (intern (format "gnus-current-%s-group" action)))
9892                           articles prefix)
9893             encoded to-newsgroup
9894             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9895       (set (intern (format "gnus-current-%s-group" action))
9896            (mm-decode-coding-string
9897             to-newsgroup
9898             (gnus-group-name-charset to-method to-newsgroup))))
9899     (unless to-method
9900       (setq to-method (or select-method
9901                           (gnus-server-to-method
9902                            (gnus-group-method to-newsgroup)))))
9903     (setq to-newsgroup
9904           (or encoded
9905               (and to-newsgroup
9906                    (mm-encode-coding-string
9907                     to-newsgroup
9908                     (gnus-group-name-charset to-method to-newsgroup)))))
9909     ;; Check the method we are to move this article to...
9910     (unless (gnus-check-backend-function
9911              'request-accept-article (car to-method))
9912       (error "%s does not support article copying" (car to-method)))
9913     (unless (gnus-check-server to-method)
9914       (error "Can't open server %s" (car to-method)))
9915     (gnus-message 6 "%s to %s: %s..."
9916                   (caddr (assq action names))
9917                   (or (car select-method)
9918                       (gnus-group-decoded-name to-newsgroup))
9919                   articles)
9920     (while articles
9921       (setq article (pop articles))
9922       ;; Set any marks that may have changed in the summary buffer.
9923       (when gnus-preserve-marks
9924         (gnus-summary-push-marks-to-backend article))
9925       (setq
9926        art-group
9927        (cond
9928         ;; Move the article.
9929         ((eq action 'move)
9930          ;; Remove this article from future suppression.
9931          (gnus-dup-unsuppress-article article)
9932          (let* ((from-method (gnus-find-method-for-group
9933                               gnus-newsgroup-name))
9934                 (to-method (or select-method
9935                                (gnus-find-method-for-group to-newsgroup)))
9936                 (move-is-internal (gnus-server-equal from-method to-method)))
9937            (gnus-request-move-article
9938             article                     ; Article to move
9939             gnus-newsgroup-name         ; From newsgroup
9940             (nth 1 (gnus-find-method-for-group
9941                     gnus-newsgroup-name)) ; Server
9942             (list 'gnus-request-accept-article
9943                   to-newsgroup (list 'quote select-method)
9944                   (not articles) t)     ; Accept form
9945             (not articles)              ; Only save nov last time
9946             (and move-is-internal
9947                  to-newsgroup           ; Not respooling
9948                                         ; Is this move internal?
9949                  (gnus-group-real-name to-newsgroup)))))
9950         ;; Copy the article.
9951         ((eq action 'copy)
9952          (with-current-buffer copy-buf
9953            (when (gnus-request-article-this-buffer article
9954                                                    gnus-newsgroup-name)
9955              (save-restriction
9956                (nnheader-narrow-to-headers)
9957                (dolist (hdr gnus-copy-article-ignored-headers)
9958                  (message-remove-header hdr t)))
9959              (gnus-request-accept-article
9960               to-newsgroup select-method (not articles) t))))
9961         ;; Crosspost the article.
9962         ((eq action 'crosspost)
9963          (let ((xref (message-tokenize-header
9964                       (mail-header-xref (gnus-summary-article-header
9965                                          article))
9966                       " ")))
9967            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9968                                   ":" (number-to-string article)))
9969            (unless xref
9970              (setq xref (list (system-name))))
9971            (setq new-xref
9972                  (concat
9973                   (mapconcat 'identity
9974                              (delete "Xref:" (delete new-xref xref))
9975                              " ")
9976                   " " new-xref))
9977            (with-current-buffer copy-buf
9978              ;; First put the article in the destination group.
9979              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9980              (when (consp (setq art-group
9981                                 (gnus-request-accept-article
9982                                  to-newsgroup select-method (not articles)
9983                                  t)))
9984                (setq new-xref (concat new-xref " " (car art-group)
9985                                       ":"
9986                                       (number-to-string (cdr art-group))))
9987                ;; Now we have the new Xrefs header, so we insert
9988                ;; it and replace the new article.
9989                (nnheader-replace-header "Xref" new-xref)
9990                (gnus-request-replace-article
9991                 (cdr art-group) to-newsgroup (current-buffer) t)
9992                art-group))))))
9993       (cond
9994        ((not art-group)
9995         (gnus-message 1 "Couldn't %s article %s: %s"
9996                       (cadr (assq action names)) article
9997                       (nnheader-get-report (car to-method))))
9998        ((eq art-group 'junk)
9999         (when (eq action 'move)
10000           (gnus-summary-mark-article article gnus-canceled-mark)
10001           (gnus-message 4 "Deleted article %s" article)
10002           ;; run the delete hook
10003           (run-hook-with-args 'gnus-summary-article-delete-hook
10004                               action
10005                               (gnus-data-header
10006                                (assoc article (gnus-data-list nil)))
10007                               gnus-newsgroup-name nil
10008                               select-method)))
10009        (t
10010         (let* ((pto-group (gnus-group-prefixed-name
10011                            (car art-group) to-method))
10012                (info (gnus-get-info pto-group))
10013                (to-group (gnus-info-group info))
10014                to-marks)
10015           ;; Update the group that has been moved to.
10016           (when (and info
10017                      (memq action '(move copy)))
10018             (unless (member to-group to-groups)
10019               (push to-group to-groups))
10020
10021             (when (and (not (memq article gnus-newsgroup-unreads))
10022                        (cdr art-group))
10023               (push 'read to-marks)
10024               (gnus-info-set-read
10025                info (gnus-add-to-range (gnus-info-read info)
10026                                        (list (cdr art-group)))))
10027
10028             ;; See whether the article is to be put in the cache.
10029             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10030                    (marks (if expirable
10031                               gnus-article-mark-lists
10032                             (delete '(expirable . expire)
10033                                     (copy-sequence
10034                                      gnus-article-mark-lists))))
10035                    (to-article (cdr art-group)))
10036
10037               ;; Enter the article into the cache in the new group,
10038               ;; if that is required.
10039               (when (and to-article
10040                          gnus-use-cache)
10041                 (gnus-cache-possibly-enter-article
10042                  to-group to-article
10043                  (memq article gnus-newsgroup-marked)
10044                  (memq article gnus-newsgroup-dormant)
10045                  (memq article gnus-newsgroup-unreads)))
10046
10047               (when (and gnus-preserve-marks
10048                          to-article)
10049                 ;; Copy any marks over to the new group.
10050                 (when (and (equal to-group gnus-newsgroup-name)
10051                            (not (memq article gnus-newsgroup-unreads)))
10052                   ;; Mark this article as read in this group.
10053                   (push (cons to-article gnus-read-mark)
10054                         gnus-newsgroup-reads)
10055                   ;; Increase the active status of this group.
10056                   (setcdr (gnus-active to-group) to-article)
10057                   (setcdr gnus-newsgroup-active to-article))
10058
10059                 (while marks
10060                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10061                     (when (memq article (symbol-value
10062                                          (intern (format "gnus-newsgroup-%s"
10063                                                          (caar marks)))))
10064                       (push (cdar marks) to-marks)
10065                       ;; If the other group is the same as this group,
10066                       ;; then we have to add the mark to the list.
10067                       (when (equal to-group gnus-newsgroup-name)
10068                         (set (intern (format "gnus-newsgroup-%s"
10069                                              (caar marks)))
10070                              (cons to-article
10071                                    (symbol-value
10072                                     (intern (format "gnus-newsgroup-%s"
10073                                                     (caar marks)))))))
10074                       ;; Copy the marks to other group.
10075                       (gnus-add-marked-articles
10076                        to-group (cdar marks) (list to-article) info)))
10077                   (setq marks (cdr marks)))
10078
10079                 (when (and expirable
10080                            gnus-mark-copied-or-moved-articles-as-expirable
10081                            (not (memq 'expire to-marks)))
10082                   ;; Mark this article as expirable.
10083                   (push 'expire to-marks)
10084                   (when (equal to-group gnus-newsgroup-name)
10085                     (push to-article gnus-newsgroup-expirable))
10086                   ;; Copy the expirable mark to other group.
10087                   (gnus-add-marked-articles
10088                    to-group 'expire (list to-article) info))
10089
10090                 (when (and to-marks
10091                            (or gnus-propagate-marks
10092                                (gnus-method-option-p
10093                                 (gnus-find-method-for-group to-group)
10094                                 'server-marks)))
10095                   (gnus-request-set-mark
10096                    to-group (list (list (list to-article) 'add to-marks)))))
10097
10098               (gnus-dribble-enter
10099                (concat "(gnus-group-set-info '"
10100                        (gnus-prin1-to-string (gnus-get-info to-group))
10101                        ")")
10102                (concat "^(gnus-group-set-info '(\""
10103                        (regexp-quote to-group) "\""))))
10104
10105           ;; Update the Xref header in this article to point to
10106           ;; the new crossposted article we have just created.
10107           (when (eq action 'crosspost)
10108             (with-current-buffer copy-buf
10109               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10110               (nnheader-replace-header "Xref" new-xref)
10111               (gnus-request-replace-article
10112                article gnus-newsgroup-name (current-buffer) t)))
10113
10114           ;; run the move/copy/crosspost/respool hook
10115           (run-hook-with-args 'gnus-summary-article-move-hook
10116                               action
10117                               (gnus-data-header
10118                                (assoc article (gnus-data-list nil)))
10119                               gnus-newsgroup-name
10120                               to-newsgroup
10121                               select-method))
10122
10123         ;;;!!!Why is this necessary?
10124         (set-buffer gnus-summary-buffer)
10125
10126         (when (eq action 'move)
10127           (save-excursion
10128             (gnus-summary-goto-subject article)
10129             (gnus-summary-mark-article article gnus-canceled-mark)))))
10130       (push article articles-to-update-marks))
10131
10132     (save-excursion
10133       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10134     ;; Re-activate all groups that have been moved to.
10135     (with-current-buffer gnus-group-buffer
10136       (let ((gnus-group-marked to-groups))
10137         (gnus-group-get-new-news-this-group nil t)))
10138
10139     (gnus-kill-buffer copy-buf)
10140     (gnus-summary-position-point)
10141     (gnus-set-mode-line 'summary)))
10142
10143 (defun gnus-summary-push-marks-to-backend (article)
10144   (let ((set nil)
10145         (marks gnus-article-mark-lists))
10146     (unless (memq article gnus-newsgroup-unreads)
10147       (push 'read set))
10148     (while marks
10149       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10150                  (memq article (symbol-value
10151                                 (intern (format "gnus-newsgroup-%s"
10152                                                 (caar marks))))))
10153         (push (cdar marks) set))
10154       (pop marks))
10155     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10156
10157 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10158   "Copy the current article to some other group.
10159 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10160 When called interactively, if TO-NEWSGROUP is nil, use the value of
10161 the variable `gnus-move-split-methods' for finding a default target
10162 newsgroup.
10163 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10164 re-spool using this method."
10165   (interactive "P")
10166   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10167
10168 (defun gnus-summary-crosspost-article (&optional n)
10169   "Crosspost the current article to some other group."
10170   (interactive "P")
10171   (gnus-summary-move-article n nil nil 'crosspost))
10172
10173 (defcustom gnus-summary-respool-default-method nil
10174   "Default method type for respooling an article.
10175 If nil, use to the current newsgroup method."
10176   :type 'symbol
10177   :group 'gnus-summary-mail)
10178
10179 (defun gnus-summary-respool-article (&optional n method)
10180   "Respool the current article.
10181 The article will be squeezed through the mail spooling process again,
10182 which means that it will be put in some mail newsgroup or other
10183 depending on `nnmail-split-methods'.
10184 If N is a positive number, respool the N next articles.
10185 If N is a negative number, respool the N previous articles.
10186 If N is nil and any articles have been marked with the process mark,
10187 respool those articles instead.
10188
10189 Respooling can be done both from mail groups and \"real\" newsgroups.
10190 In the former case, the articles in question will be moved from the
10191 current group into whatever groups they are destined to.  In the
10192 latter case, they will be copied into the relevant groups."
10193   (interactive
10194    (list current-prefix-arg
10195          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10196                 (methname
10197                  (symbol-name (or gnus-summary-respool-default-method
10198                                   (car (gnus-find-method-for-group
10199                                         gnus-newsgroup-name)))))
10200                 (method
10201                  (gnus-completing-read
10202                   "Backend to use when respooling"
10203                   methods t nil 'gnus-mail-method-history methname))
10204                 ms)
10205            (cond
10206             ((zerop (length (setq ms (gnus-servers-using-backend
10207                                       (intern method)))))
10208              (list (intern method) ""))
10209             ((= 1 (length ms))
10210              (car ms))
10211             (t
10212              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10213                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10214                            ms-alist))))))))
10215   (unless method
10216     (error "No method given for respooling"))
10217   (if (assoc (symbol-name
10218               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10219              (gnus-methods-using 'respool))
10220       (gnus-summary-move-article n nil method)
10221     (gnus-summary-copy-article n nil method)))
10222
10223 (defun gnus-summary-import-article (file &optional edit)
10224   "Import an arbitrary file into a mail newsgroup."
10225   (interactive "fImport file: \nP")
10226   (let ((group gnus-newsgroup-name)
10227         (now (current-time))
10228         atts lines group-art)
10229     (unless (gnus-check-backend-function 'request-accept-article group)
10230       (error "%s does not support article importing" group))
10231     (or (file-readable-p file)
10232         (not (file-regular-p file))
10233         (error "Can't read %s" file))
10234     (with-current-buffer (gnus-get-buffer-create " *import file*")
10235       (erase-buffer)
10236       (nnheader-insert-file-contents file)
10237       (goto-char (point-min))
10238       (if (nnheader-article-p)
10239           (save-restriction
10240             (goto-char (point-min))
10241             (search-forward "\n\n" nil t)
10242             (narrow-to-region (point-min) (1- (point)))
10243             (goto-char (point-min))
10244             (unless (re-search-forward "^date:" nil t)
10245               (goto-char (point-max))
10246               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10247        ;; This doesn't look like an article, so we fudge some headers.
10248         (setq atts (file-attributes file)
10249               lines (count-lines (point-min) (point-max)))
10250         (insert "From: " (read-string "From: ") "\n"
10251                 "Subject: " (read-string "Subject: ") "\n"
10252                 "Date: " (message-make-date (nth 5 atts)) "\n"
10253                 "Message-ID: " (message-make-message-id) "\n"
10254                 "Lines: " (int-to-string lines) "\n"
10255                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10256       (setq group-art (gnus-request-accept-article group nil t))
10257       (kill-buffer (current-buffer)))
10258     (setq gnus-newsgroup-active (gnus-activate-group group))
10259     (forward-line 1)
10260     (gnus-summary-goto-article (cdr group-art) nil t)
10261     (when edit
10262       (gnus-summary-edit-article))))
10263
10264 (defun gnus-summary-create-article ()
10265   "Create an article in a mail newsgroup."
10266   (interactive)
10267   (let ((group gnus-newsgroup-name)
10268         (now (current-time))
10269         group-art)
10270     (unless (gnus-check-backend-function 'request-accept-article group)
10271       (error "%s does not support article importing" group))
10272     (with-current-buffer (gnus-get-buffer-create " *import file*")
10273       (erase-buffer)
10274       (goto-char (point-min))
10275       ;; This doesn't look like an article, so we fudge some headers.
10276       (insert "From: " (read-string "From: ") "\n"
10277               "Subject: " (read-string "Subject: ") "\n"
10278               "Date: " (message-make-date now) "\n"
10279               "Message-ID: " (message-make-message-id) "\n")
10280       (setq group-art (gnus-request-accept-article group nil t))
10281       (kill-buffer (current-buffer)))
10282     (setq gnus-newsgroup-active (gnus-activate-group group))
10283     (forward-line 1)
10284     (gnus-summary-goto-article (cdr group-art) nil t)
10285     (gnus-summary-edit-article)))
10286
10287 (defun gnus-summary-article-posted-p ()
10288   "Say whether the current (mail) article is available from news as well.
10289 This will be the case if the article has both been mailed and posted."
10290   (interactive)
10291   (let ((id (mail-header-references (gnus-summary-article-header)))
10292         (gnus-override-method (car (gnus-refer-article-methods))))
10293     (if (gnus-request-head id "")
10294         (gnus-message 2 "The current message was found on %s"
10295                       gnus-override-method)
10296       (gnus-message 2 "The current message couldn't be found on %s"
10297                     gnus-override-method)
10298       nil)))
10299
10300 (defun gnus-summary-expire-articles (&optional now)
10301   "Expire all articles that are marked as expirable in the current group."
10302   (interactive)
10303   (when (and (not gnus-group-is-exiting-without-update-p)
10304              (gnus-check-backend-function
10305               'request-expire-articles gnus-newsgroup-name))
10306     ;; This backend supports expiry.
10307     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10308            (expirable (if total
10309                           (progn
10310                             ;; We need to update the info for
10311                             ;; this group for `gnus-list-of-read-articles'
10312                             ;; to give us the right answer.
10313                             (gnus-run-hooks 'gnus-exit-group-hook)
10314                             (gnus-summary-update-info)
10315                             (gnus-list-of-read-articles gnus-newsgroup-name))
10316                         (setq gnus-newsgroup-expirable
10317                               (sort gnus-newsgroup-expirable '<))))
10318            (expiry-wait (if now 'immediate
10319                           (gnus-group-find-parameter
10320                            gnus-newsgroup-name 'expiry-wait)))
10321            (nnmail-expiry-target
10322             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10323                 nnmail-expiry-target))
10324            es)
10325       (when expirable
10326         ;; There are expirable articles in this group, so we run them
10327         ;; through the expiry process.
10328         (gnus-message 6 "Expiring articles...")
10329         (when (gnus-check-group gnus-newsgroup-name)
10330           ;; The list of articles that weren't expired is returned.
10331           (save-excursion
10332             (if expiry-wait
10333                 (let ((nnmail-expiry-wait-function nil)
10334                       (nnmail-expiry-wait expiry-wait))
10335                   (setq es (gnus-request-expire-articles
10336                             expirable gnus-newsgroup-name)))
10337               (setq es (gnus-request-expire-articles
10338                         expirable gnus-newsgroup-name)))
10339             (unless total
10340               (setq gnus-newsgroup-expirable es))
10341             ;; We go through the old list of expirable, and mark all
10342             ;; really expired articles as nonexistent.
10343             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10344               (let ((gnus-use-cache nil))
10345                 (dolist (article expirable)
10346                   (when (and (not (memq article es))
10347                              (gnus-data-find article))
10348                     (gnus-summary-mark-article article gnus-canceled-mark)
10349                     (run-hook-with-args 'gnus-summary-article-expire-hook
10350                                         'delete
10351                                         (gnus-data-header
10352                                          (assoc article (gnus-data-list nil)))
10353                                         gnus-newsgroup-name
10354                                         nil
10355                                         nil)))))))
10356         (gnus-message 6 "Expiring articles...done")))))
10357
10358 (defun gnus-summary-expire-articles-now ()
10359   "Expunge all expirable articles in the current group.
10360 This means that *all* articles that are marked as expirable will be
10361 deleted forever, right now."
10362   (interactive)
10363   (or gnus-expert-user
10364       (gnus-yes-or-no-p
10365        "Are you really, really sure you want to delete all expirable messages? ")
10366       (error "Phew!"))
10367   (gnus-summary-expire-articles t))
10368
10369 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10370 (defun gnus-summary-delete-article (&optional n)
10371   "Delete the N next (mail) articles.
10372 This command actually deletes articles.  This is not a marking
10373 command.  The article will disappear forever from your life, never to
10374 return.
10375
10376 If N is negative, delete backwards.
10377 If N is nil and articles have been marked with the process mark,
10378 delete these instead.
10379
10380 If `gnus-novice-user' is non-nil you will be asked for
10381 confirmation before the articles are deleted."
10382   (interactive "P")
10383   (unless (gnus-check-backend-function 'request-expire-articles
10384                                        gnus-newsgroup-name)
10385     (error "The current newsgroup does not support article deletion"))
10386   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10387     (error "Couldn't open server"))
10388   ;; Compute the list of articles to delete.
10389   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10390         (nnmail-expiry-target 'delete)
10391         not-deleted)
10392     (if (and gnus-novice-user
10393              (not (gnus-yes-or-no-p
10394                    (format "Do you really want to delete %s forever? "
10395                            (if (> (length articles) 1)
10396                                (format "these %s articles" (length articles))
10397                              "this article")))))
10398         ()
10399       ;; Delete the articles.
10400       (setq not-deleted (gnus-request-expire-articles
10401                          articles gnus-newsgroup-name 'force))
10402       (save-excursion
10403         (while articles
10404           (gnus-summary-remove-process-mark (car articles))
10405           ;; The backend might not have been able to delete the article
10406           ;; after all.
10407           (unless (memq (car articles) not-deleted)
10408             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10409             (let* ((article (car articles))
10410                    (ghead  (gnus-data-header
10411                             (assoc article (gnus-data-list nil)))))
10412               (run-hook-with-args 'gnus-summary-article-delete-hook
10413                                   'delete ghead gnus-newsgroup-name nil
10414                                   nil)))
10415           (setq articles (cdr articles))))
10416       (when not-deleted
10417         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10418     (gnus-summary-position-point)
10419     (gnus-set-mode-line 'summary)
10420     not-deleted))
10421
10422 (defun gnus-summary-edit-article (&optional arg)
10423   "Edit the current article.
10424 This will have permanent effect only in mail groups.
10425 If ARG is nil, edit the decoded articles.
10426 If ARG is 1, edit the raw articles.
10427 If ARG is 2, edit the raw articles even in read-only groups.
10428 If ARG is 3, edit the articles with the current handles.
10429 Otherwise, allow editing of articles even in read-only
10430 groups."
10431   (interactive "P")
10432   (let (force raw current-handles)
10433     (cond
10434      ((null arg))
10435      ((eq arg 1)
10436       (setq raw t))
10437      ((eq arg 2)
10438       (setq raw t
10439             force t))
10440      ((eq arg 3)
10441       (setq current-handles
10442             (and (gnus-buffer-live-p gnus-article-buffer)
10443                  (with-current-buffer gnus-article-buffer
10444                    (prog1
10445                        gnus-article-mime-handles
10446                      (setq gnus-article-mime-handles nil))))))
10447      (t
10448       (setq force t)))
10449     (when (and raw (not force)
10450                (member gnus-newsgroup-name '("nndraft:delayed"
10451                                              "nndraft:drafts"
10452                                              "nndraft:queue")))
10453       (error "Can't edit the raw article in group %s"
10454              gnus-newsgroup-name))
10455     (with-current-buffer gnus-summary-buffer
10456       (let ((mail-parse-charset gnus-newsgroup-charset)
10457             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10458         (gnus-set-global-variables)
10459         (when (and (not force)
10460                    (gnus-group-read-only-p))
10461           (error "The current newsgroup does not support article editing"))
10462         (gnus-summary-show-article t)
10463         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10464           (with-current-buffer gnus-article-buffer
10465             (mm-enable-multibyte)))
10466         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10467             (setq raw t))
10468         (gnus-article-edit-article
10469          (if raw 'ignore
10470            `(lambda ()
10471               (let ((mbl mml-buffer-list))
10472                 (setq mml-buffer-list nil)
10473                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10474                   (mime-to-mml ,'current-handles))
10475                 (let ((mbl1 mml-buffer-list))
10476                   (setq mml-buffer-list mbl)
10477                   (set (make-local-variable 'mml-buffer-list) mbl1))
10478                 (gnus-make-local-hook 'kill-buffer-hook)
10479                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10480          `(lambda (no-highlight)
10481             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10482                   (message-options message-options)
10483                   (message-options-set-recipient)
10484                   (mail-parse-ignored-charsets
10485                    ',gnus-newsgroup-ignored-charsets)
10486                   (rfc2047-header-encoding-alist
10487                    ',(let ((charset (gnus-group-name-charset
10488                                      (gnus-find-method-for-group
10489                                       gnus-newsgroup-name)
10490                                      gnus-newsgroup-name)))
10491                        (append (list (cons "Newsgroups" charset)
10492                                      (cons "Followup-To" charset)
10493                                      (cons "Xref" charset))
10494                                rfc2047-header-encoding-alist))))
10495               ,(if (not raw) '(progn
10496                                 (mml-to-mime)
10497                                 (mml-destroy-buffers)
10498                                 (remove-hook 'kill-buffer-hook
10499                                              'mml-destroy-buffers t)
10500                                 (kill-local-variable 'mml-buffer-list)))
10501               (gnus-summary-edit-article-done
10502                ,(or (mail-header-references gnus-current-headers) "")
10503                ,(gnus-group-read-only-p)
10504                ,gnus-summary-buffer no-highlight))))))))
10505
10506 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10507
10508 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10509                                                  no-highlight)
10510   "Make edits to the current article permanent."
10511   (interactive)
10512   (save-excursion
10513     ;; The buffer restriction contains the entire article if it exists.
10514     (when (article-goto-body)
10515       (let ((lines (count-lines (point) (point-max)))
10516             (length (- (point-max) (point)))
10517             (case-fold-search t)
10518             (body (copy-marker (point))))
10519         (goto-char (point-min))
10520         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10521           (delete-region (match-beginning 1) (match-end 1))
10522           (insert (number-to-string length)))
10523         (goto-char (point-min))
10524         (when (re-search-forward
10525                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10526           (delete-region (match-beginning 1) (match-end 1))
10527           (insert (number-to-string length)))
10528         (goto-char (point-min))
10529         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10530           (delete-region (match-beginning 1) (match-end 1))
10531           (insert (number-to-string lines))))))
10532   ;; Replace the article.
10533   (let ((buf (current-buffer))
10534         (article (cdr gnus-article-current))
10535         replace-result)
10536     (with-temp-buffer
10537       (insert-buffer-substring buf)
10538       (if (and (not read-only)
10539                (not (setq replace-result
10540                           (gnus-request-replace-article
10541                            article (car gnus-article-current)
10542                            (current-buffer) t))))
10543           (error "Couldn't replace article")
10544         ;; If we got a number back, then that's the new article number
10545         ;; for this article.  Otherwise, the article number didn't change.
10546         (when (numberp replace-result)
10547           (with-current-buffer gnus-summary-buffer
10548             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10549             (gnus-summary-limit gnus-newsgroup-limit)
10550             (setq article replace-result)
10551             (gnus-summary-goto-subject article t)))
10552         ;; Update the summary buffer.
10553         (if (and references
10554                  (equal (message-tokenize-header references " ")
10555                         (message-tokenize-header
10556                          (or (message-fetch-field "references") "") " ")))
10557             ;; We only have to update this line.
10558             (save-excursion
10559               (save-restriction
10560                 (message-narrow-to-head)
10561                 (let ((head (buffer-substring-no-properties
10562                              (point-min) (point-max)))
10563                       header)
10564                   (with-temp-buffer
10565                     (insert (format "211 %d Article retrieved.\n" article))
10566                     (insert head)
10567                     (insert ".\n")
10568                     (let ((nntp-server-buffer (current-buffer)))
10569                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10570                     (with-current-buffer gnus-summary-buffer
10571                       (gnus-data-set-header (gnus-data-find article) header)
10572                       (gnus-summary-update-article-line article header)
10573                       (if (gnus-summary-goto-subject article nil t)
10574                           (gnus-summary-update-secondary-mark article)))))))
10575           ;; Update threads.
10576           (set-buffer (or buffer gnus-summary-buffer))
10577           (gnus-summary-update-article article)
10578           (if (gnus-summary-goto-subject article nil t)
10579               (gnus-summary-update-secondary-mark article)))
10580         ;; Prettify the article buffer again.
10581         (unless no-highlight
10582           (with-current-buffer gnus-article-buffer
10583             ;;!!! Fix this -- article should be rehighlighted.
10584             ;;(gnus-run-hooks 'gnus-article-display-hook)
10585             (set-buffer gnus-original-article-buffer)
10586             (gnus-request-article
10587              article (car gnus-article-current) (current-buffer))))
10588         ;; Prettify the summary buffer line.
10589         (when (gnus-visual-p 'summary-highlight 'highlight)
10590           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10591
10592 (defun gnus-summary-edit-wash (key)
10593   "Perform editing command KEY in the article buffer."
10594   (interactive
10595    (list
10596     (progn
10597       (message "%s" (concat (this-command-keys) "- "))
10598       (read-char))))
10599   (message "")
10600   (gnus-summary-edit-article)
10601   (execute-kbd-macro (concat (this-command-keys) key))
10602   (gnus-article-edit-done))
10603
10604 ;;; Respooling
10605
10606 (defun gnus-summary-respool-query (&optional silent trace)
10607   "Query where the respool algorithm would put this article."
10608   (interactive)
10609   (let (gnus-mark-article-hook)
10610     (gnus-summary-select-article)
10611     (with-current-buffer gnus-original-article-buffer
10612       (let ((groups (nnmail-article-group 'identity trace)))
10613         (unless silent
10614           (if groups
10615               (message "This message would go to %s"
10616                        (mapconcat 'car groups ", "))
10617             (message "This message would go to no groups"))
10618           groups)))))
10619
10620 (defun gnus-summary-respool-trace ()
10621   "Trace where the respool algorithm would put this article.
10622 Display a buffer showing all fancy splitting patterns which matched."
10623   (interactive)
10624   (gnus-summary-respool-query nil t))
10625
10626 ;; Summary marking commands.
10627
10628 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10629   "Mark articles which has the same subject as read, and then select the next.
10630 If UNMARK is positive, remove any kind of mark.
10631 If UNMARK is negative, tick articles."
10632   (interactive "P")
10633   (when unmark
10634     (setq unmark (prefix-numeric-value unmark)))
10635   (let ((count
10636          (gnus-summary-mark-same-subject
10637           (gnus-summary-article-subject) unmark)))
10638     ;; Select next unread article.  If auto-select-same mode, should
10639     ;; select the first unread article.
10640     (gnus-summary-next-article t (and gnus-auto-select-same
10641                                       (gnus-summary-article-subject)))
10642     (gnus-message 7 "%d article%s marked as %s"
10643                   count (if (= count 1) " is" "s are")
10644                   (if unmark "unread" "read"))))
10645
10646 (defun gnus-summary-kill-same-subject (&optional unmark)
10647   "Mark articles which has the same subject as read.
10648 If UNMARK is positive, remove any kind of mark.
10649 If UNMARK is negative, tick articles."
10650   (interactive "P")
10651   (when unmark
10652     (setq unmark (prefix-numeric-value unmark)))
10653   (let ((count
10654          (gnus-summary-mark-same-subject
10655           (gnus-summary-article-subject) unmark)))
10656     ;; If marked as read, go to next unread subject.
10657     (when (null unmark)
10658       ;; Go to next unread subject.
10659       (gnus-summary-next-subject 1 t))
10660     (gnus-message 7 "%d articles are marked as %s"
10661                   count (if unmark "unread" "read"))))
10662
10663 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10664   "Mark articles with same SUBJECT as read, and return marked number.
10665 If optional argument UNMARK is positive, remove any kinds of marks.
10666 If optional argument UNMARK is negative, mark articles as unread instead."
10667   (let ((count 1))
10668     (save-excursion
10669       (cond
10670        ((null unmark)                   ; Mark as read.
10671         (while (and
10672                 (progn
10673                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10674                   (gnus-summary-show-thread) t)
10675                 (gnus-summary-find-subject subject))
10676           (setq count (1+ count))))
10677        ((> unmark 0)                    ; Tick.
10678         (while (and
10679                 (progn
10680                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10681                   (gnus-summary-show-thread) t)
10682                 (gnus-summary-find-subject subject))
10683           (setq count (1+ count))))
10684        (t                               ; Mark as unread.
10685         (while (and
10686                 (progn
10687                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10688                   (gnus-summary-show-thread) t)
10689                 (gnus-summary-find-subject subject))
10690           (setq count (1+ count)))))
10691       (gnus-set-mode-line 'summary)
10692       ;; Return the number of marked articles.
10693       count)))
10694
10695 (defun gnus-summary-mark-as-processable (n &optional unmark)
10696   "Set the process mark on the next N articles.
10697 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10698 the process mark instead.  The difference between N and the actual
10699 number of articles marked is returned."
10700   (interactive "P")
10701   (if (and (null n) (gnus-region-active-p))
10702       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10703     (setq n (prefix-numeric-value n))
10704     (let ((backward (< n 0))
10705           (n (abs n)))
10706       (while (and
10707               (> n 0)
10708               (if unmark
10709                   (gnus-summary-remove-process-mark
10710                    (gnus-summary-article-number))
10711                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10712               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10713         (setq n (1- n)))
10714       (when (/= 0 n)
10715         (gnus-message 7 "No more articles"))
10716       (gnus-summary-recenter)
10717       (gnus-summary-position-point)
10718       n)))
10719
10720 (defun gnus-summary-unmark-as-processable (n)
10721   "Remove the process mark from the next N articles.
10722 If N is negative, unmark backward instead.  The difference between N and
10723 the actual number of articles unmarked is returned."
10724   (interactive "P")
10725   (gnus-summary-mark-as-processable n t))
10726
10727 (defun gnus-summary-unmark-all-processable ()
10728   "Remove the process mark from all articles."
10729   (interactive)
10730   (save-excursion
10731     (while gnus-newsgroup-processable
10732       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10733   (gnus-summary-position-point))
10734
10735 (defun gnus-summary-add-mark (article type)
10736   "Mark ARTICLE with a mark of TYPE."
10737   (let ((vtype (car (assq type gnus-article-mark-lists)))
10738         var)
10739     (if (not vtype)
10740         (error "No such mark type: %s" type)
10741       (setq var (intern (format "gnus-newsgroup-%s" type)))
10742       (set var (cons article (symbol-value var)))
10743       (if (memq type '(processable cached replied forwarded recent saved))
10744           (gnus-summary-update-secondary-mark article)
10745         ;;; !!! This is bogus.  We should find out what primary
10746         ;;; !!! mark we want to set.
10747         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10748
10749 (defun gnus-summary-mark-as-expirable (n)
10750   "Mark N articles forward as expirable.
10751 If N is negative, mark backward instead.  The difference between N and
10752 the actual number of articles marked is returned."
10753   (interactive "p")
10754   (gnus-summary-mark-forward n gnus-expirable-mark))
10755
10756 (defun gnus-summary-mark-as-spam (n)
10757   "Mark N articles forward as spam.
10758 If N is negative, mark backward instead.  The difference between N and
10759 the actual number of articles marked is returned."
10760   (interactive "p")
10761   (gnus-summary-mark-forward n gnus-spam-mark))
10762
10763 (defun gnus-summary-mark-article-as-replied (article)
10764   "Mark ARTICLE as replied to and update the summary line.
10765 ARTICLE can also be a list of articles."
10766   (interactive (list (gnus-summary-article-number)))
10767   (let ((articles (if (listp article) article (list article))))
10768     (dolist (article articles)
10769       (unless (numberp article)
10770         (error "%s is not a number" article))
10771       (push article gnus-newsgroup-replied)
10772       (let ((inhibit-read-only t))
10773         (when (gnus-summary-goto-subject article nil t)
10774           (gnus-summary-update-secondary-mark article))))))
10775
10776 (defun gnus-summary-mark-article-as-forwarded (article)
10777   "Mark ARTICLE as forwarded and update the summary line.
10778 ARTICLE can also be a list of articles."
10779   (let ((articles (if (listp article) article (list article))))
10780     (dolist (article articles)
10781       (push article gnus-newsgroup-forwarded)
10782       (let ((inhibit-read-only t))
10783         (when (gnus-summary-goto-subject article nil t)
10784           (gnus-summary-update-secondary-mark article))))))
10785
10786 (defun gnus-summary-set-bookmark (article)
10787   "Set a bookmark in current article."
10788   (interactive (list (gnus-summary-article-number)))
10789   (when (or (not (get-buffer gnus-article-buffer))
10790             (not gnus-current-article)
10791             (not gnus-article-current)
10792             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10793     (error "No current article selected"))
10794   ;; Remove old bookmark, if one exists.
10795   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10796   ;; Set the new bookmark, which is on the form
10797   ;; (article-number . line-number-in-body).
10798   (push
10799    (cons article
10800          (with-current-buffer gnus-article-buffer
10801            (count-lines
10802             (min (point)
10803                  (save-excursion
10804                    (article-goto-body)
10805                    (point)))
10806             (point))))
10807    gnus-newsgroup-bookmarks)
10808   (gnus-message 6 "A bookmark has been added to the current article."))
10809
10810 (defun gnus-summary-remove-bookmark (article)
10811   "Remove the bookmark from the current article."
10812   (interactive (list (gnus-summary-article-number)))
10813   ;; Remove old bookmark, if one exists.
10814   (if (not (assq article gnus-newsgroup-bookmarks))
10815       (gnus-message 6 "No bookmark in current article.")
10816     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10817     (gnus-message 6 "Removed bookmark.")))
10818
10819 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10820 (defun gnus-summary-mark-as-dormant (n)
10821   "Mark N articles forward as dormant.
10822 If N is negative, mark backward instead.  The difference between N and
10823 the actual number of articles marked is returned."
10824   (interactive "p")
10825   (gnus-summary-mark-forward n gnus-dormant-mark))
10826
10827 (defun gnus-summary-set-process-mark (article)
10828   "Set the process mark on ARTICLE and update the summary line."
10829   (setq gnus-newsgroup-processable
10830         (cons article
10831               (delq article gnus-newsgroup-processable)))
10832   (when (gnus-summary-goto-subject article)
10833     (gnus-summary-show-thread)
10834     (gnus-summary-goto-subject article)
10835     (gnus-summary-update-secondary-mark article)))
10836
10837 (defun gnus-summary-remove-process-mark (&rest articles)
10838   "Remove the process mark from ARTICLES and update the summary line."
10839   (dolist (article articles)
10840     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10841     (when (gnus-summary-goto-subject article)
10842       (gnus-summary-show-thread)
10843       (gnus-summary-goto-subject article)
10844       (gnus-summary-update-secondary-mark article)))
10845   t)
10846
10847 (defun gnus-summary-set-saved-mark (article)
10848   "Set the process mark on ARTICLE and update the summary line."
10849   (push article gnus-newsgroup-saved)
10850   (when (gnus-summary-goto-subject article)
10851     (gnus-summary-update-secondary-mark article)))
10852
10853 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10854   "Mark N articles as read forwards.
10855 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10856 The difference between N and the actual number of articles marked is
10857 returned.
10858 If NO-EXPIRE, auto-expiry will be inhibited."
10859   (interactive "p")
10860   (gnus-summary-show-thread)
10861   (let ((backward (< n 0))
10862         (gnus-summary-goto-unread
10863          (and gnus-summary-goto-unread
10864               (not (eq gnus-summary-goto-unread 'never))
10865               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10866                                     gnus-ticked-mark gnus-dormant-mark)))))
10867         (n (abs n))
10868         (mark (or mark gnus-del-mark)))
10869     (while (and (> n 0)
10870                 (gnus-summary-mark-article nil mark no-expire)
10871                 (zerop (gnus-summary-next-subject
10872                         (if backward -1 1)
10873                         (and gnus-summary-goto-unread
10874                              (not (eq gnus-summary-goto-unread 'never)))
10875                         t)))
10876       (setq n (1- n)))
10877     (when (/= 0 n)
10878       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10879     (gnus-summary-recenter)
10880     (gnus-summary-position-point)
10881     (gnus-set-mode-line 'summary)
10882     n))
10883
10884 (defun gnus-summary-mark-article-as-read (mark)
10885   "Mark the current article quickly as read with MARK."
10886   (let ((article (gnus-summary-article-number)))
10887     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10888     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10889     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10890     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10891     (push (cons article mark) gnus-newsgroup-reads)
10892     ;; Possibly remove from cache, if that is used.
10893     (when gnus-use-cache
10894       (gnus-cache-enter-remove-article article))
10895     ;; Allow the backend to change the mark.
10896     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10897     ;; Check for auto-expiry.
10898     (when (and gnus-newsgroup-auto-expire
10899                (memq mark gnus-auto-expirable-marks))
10900       (setq mark gnus-expirable-mark)
10901       ;; Let the backend know about the mark change.
10902       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10903       (push article gnus-newsgroup-expirable))
10904     ;; Set the mark in the buffer.
10905     (gnus-summary-update-mark mark 'unread)
10906     t))
10907
10908 (defun gnus-summary-mark-article-as-unread (mark)
10909   "Mark the current article quickly as unread with MARK."
10910   (let* ((article (gnus-summary-article-number))
10911          (old-mark (gnus-summary-article-mark article)))
10912     ;; Allow the backend to change the mark.
10913     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10914     (if (eq mark old-mark)
10915         t
10916       (if (<= article 0)
10917           (progn
10918             (gnus-error 1 "Can't mark negative article numbers")
10919             nil)
10920         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10921         (setq gnus-newsgroup-spam-marked
10922               (delq article gnus-newsgroup-spam-marked))
10923         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10924         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10925         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10926         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10927         (cond ((= mark gnus-ticked-mark)
10928                (setq gnus-newsgroup-marked
10929                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10930                                               article)))
10931               ((= mark gnus-spam-mark)
10932                (setq gnus-newsgroup-spam-marked
10933                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10934                                               article)))
10935               ((= mark gnus-dormant-mark)
10936                (setq gnus-newsgroup-dormant
10937                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10938                                               article)))
10939               (t
10940                (setq gnus-newsgroup-unreads
10941                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10942                                               article))))
10943         (gnus-alist-pull article gnus-newsgroup-reads)
10944
10945         ;; See whether the article is to be put in the cache.
10946         (and gnus-use-cache
10947              (vectorp (gnus-summary-article-header article))
10948              (save-excursion
10949                (gnus-cache-possibly-enter-article
10950                 gnus-newsgroup-name article
10951                 (= mark gnus-ticked-mark)
10952                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10953
10954         ;; Fix the mark.
10955         (gnus-summary-update-mark mark 'unread)
10956         t))))
10957
10958 (defun gnus-summary-mark-article (&optional article mark no-expire)
10959   "Mark ARTICLE with MARK.  MARK can be any character.
10960 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10961 `??' (dormant) and `?E' (expirable).
10962 If MARK is nil, then the default character `?r' is used.
10963 If ARTICLE is nil, then the article on the current line will be
10964 marked.
10965 If NO-EXPIRE, auto-expiry will be inhibited."
10966   ;; The mark might be a string.
10967   (when (stringp mark)
10968     (setq mark (aref mark 0)))
10969   ;; If no mark is given, then we check auto-expiring.
10970   (when (null mark)
10971     (setq mark gnus-del-mark))
10972   (when (and (not no-expire)
10973              gnus-newsgroup-auto-expire
10974              (memq mark gnus-auto-expirable-marks))
10975     (setq mark gnus-expirable-mark))
10976   (let ((article (or article (gnus-summary-article-number)))
10977         (old-mark (gnus-summary-article-mark article)))
10978     ;; Allow the backend to change the mark.
10979     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10980     (if (eq mark old-mark)
10981         t
10982       (unless article
10983         (error "No article on current line"))
10984       (if (not (if (or (= mark gnus-unread-mark)
10985                        (= mark gnus-ticked-mark)
10986                        (= mark gnus-spam-mark)
10987                        (= mark gnus-dormant-mark))
10988                    (gnus-mark-article-as-unread article mark)
10989                  (gnus-mark-article-as-read article mark)))
10990           t
10991         ;; See whether the article is to be put in the cache.
10992         (and gnus-use-cache
10993              (not (= mark gnus-canceled-mark))
10994              (vectorp (gnus-summary-article-header article))
10995              (save-excursion
10996                (gnus-cache-possibly-enter-article
10997                 gnus-newsgroup-name article
10998                 (= mark gnus-ticked-mark)
10999                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11000
11001         (when (gnus-summary-goto-subject article nil t)
11002           (let ((inhibit-read-only t))
11003             (gnus-summary-show-thread)
11004             ;; Fix the mark.
11005             (gnus-summary-update-mark mark 'unread)
11006             t))))))
11007
11008 (defun gnus-summary-update-secondary-mark (article)
11009   "Update the secondary (read, process, cache) mark."
11010   (gnus-summary-update-mark
11011    (cond ((memq article gnus-newsgroup-processable)
11012           gnus-process-mark)
11013          ((memq article gnus-newsgroup-cached)
11014           gnus-cached-mark)
11015          ((memq article gnus-newsgroup-replied)
11016           gnus-replied-mark)
11017          ((memq article gnus-newsgroup-forwarded)
11018           gnus-forwarded-mark)
11019          ((memq article gnus-newsgroup-saved)
11020           gnus-saved-mark)
11021          ((memq article gnus-newsgroup-unseen)
11022           gnus-unseen-mark)
11023          (t gnus-no-mark))
11024    'replied)
11025   (when (gnus-visual-p 'summary-highlight 'highlight)
11026     (gnus-summary-highlight-line)
11027     (gnus-run-hooks 'gnus-summary-update-hook))
11028   t)
11029
11030 (defun gnus-summary-update-download-mark (article)
11031   "Update the download mark."
11032   (gnus-summary-update-mark
11033    (cond ((memq article gnus-newsgroup-undownloaded)
11034           gnus-undownloaded-mark)
11035          (gnus-newsgroup-agentized
11036           gnus-downloaded-mark)
11037          (t
11038           gnus-no-mark))
11039    'download)
11040   (gnus-summary-update-line t)
11041   t)
11042
11043 (defun gnus-summary-update-mark (mark type)
11044   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11045         (inhibit-read-only t))
11046     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11047     (when forward
11048       (when (looking-at "\r")
11049         (incf forward))
11050       (when (<= (+ forward (point)) (point-max))
11051         ;; Go to the right position on the line.
11052         (goto-char (+ forward (point)))
11053         ;; Replace the old mark with the new mark.
11054         (let ((to-insert
11055                (mm-subst-char-in-string
11056                 (char-after) mark
11057                 (buffer-substring (point) (1+ (point))))))
11058           (delete-region (point) (1+ (point)))
11059           (insert to-insert))
11060         ;; Optionally update the marks by some user rule.
11061         (when (eq type 'unread)
11062           (gnus-data-set-mark
11063            (gnus-data-find (gnus-summary-article-number)) mark)
11064           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11065
11066 (defun gnus-mark-article-as-read (article &optional mark)
11067   "Enter ARTICLE in the pertinent lists and remove it from others."
11068   ;; Make the article expirable.
11069   (let ((mark (or mark gnus-del-mark)))
11070     (setq gnus-newsgroup-expirable
11071           (if (= mark gnus-expirable-mark)
11072               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11073             (delq article gnus-newsgroup-expirable)))
11074     ;; Remove from unread and marked lists.
11075     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11076     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11077     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11078     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11079     (push (cons article mark) gnus-newsgroup-reads)
11080     ;; Possibly remove from cache, if that is used.
11081     (when gnus-use-cache
11082       (gnus-cache-enter-remove-article article))
11083     t))
11084
11085 (defun gnus-mark-article-as-unread (article &optional mark)
11086   "Enter ARTICLE in the pertinent lists and remove it from others."
11087   (let ((mark (or mark gnus-ticked-mark)))
11088     (if (<= article 0)
11089         (progn
11090           (gnus-error 1 "Can't mark negative article numbers")
11091           nil)
11092       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11093             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11094             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11095             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11096             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11097
11098       ;; Unsuppress duplicates?
11099       (when gnus-suppress-duplicates
11100         (gnus-dup-unsuppress-article article))
11101
11102       (cond ((= mark gnus-ticked-mark)
11103              (setq gnus-newsgroup-marked
11104                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11105             ((= mark gnus-spam-mark)
11106              (setq gnus-newsgroup-spam-marked
11107                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11108                                             article)))
11109             ((= mark gnus-dormant-mark)
11110              (setq gnus-newsgroup-dormant
11111                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11112             (t
11113              (setq gnus-newsgroup-unreads
11114                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11115       (gnus-alist-pull article gnus-newsgroup-reads)
11116       t)))
11117
11118 (defun gnus-summary-tick-article-forward (n)
11119   "Tick N articles forwards.
11120 If N is negative, tick backwards instead.
11121 The difference between N and the number of articles ticked is returned."
11122   (interactive "p")
11123   (gnus-summary-mark-forward n gnus-ticked-mark))
11124
11125 (defun gnus-summary-tick-article-backward (n)
11126   "Tick N articles backwards.
11127 The difference between N and the number of articles ticked is returned."
11128   (interactive "p")
11129   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11130
11131 (defun gnus-summary-tick-article (&optional article clear-mark)
11132   "Mark current article as unread.
11133 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11134 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11135   (interactive)
11136   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11137                                        gnus-ticked-mark)))
11138
11139 (defun gnus-summary-mark-as-read-forward (n)
11140   "Mark N articles as read forwards.
11141 If N is negative, mark backwards instead.
11142 The difference between N and the actual number of articles marked is
11143 returned."
11144   (interactive "p")
11145   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11146
11147 (defun gnus-summary-mark-as-read-backward (n)
11148   "Mark the N articles as read backwards.
11149 The difference between N and the actual number of articles marked is
11150 returned."
11151   (interactive "p")
11152   (gnus-summary-mark-forward
11153    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11154
11155 (defun gnus-summary-mark-as-read (&optional article mark)
11156   "Mark current article as read.
11157 ARTICLE specifies the article to be marked as read.
11158 MARK specifies a string to be inserted at the beginning of the line."
11159   (gnus-summary-mark-article article mark))
11160
11161 (defun gnus-summary-clear-mark-forward (n)
11162   "Clear marks from N articles forward.
11163 If N is negative, clear backward instead.
11164 The difference between N and the number of marks cleared is returned."
11165   (interactive "p")
11166   (gnus-summary-mark-forward n gnus-unread-mark))
11167
11168 (defun gnus-summary-clear-mark-backward (n)
11169   "Clear marks from N articles backward.
11170 The difference between N and the number of marks cleared is returned."
11171   (interactive "p")
11172   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11173
11174 (defun gnus-summary-mark-unread-as-read ()
11175   "Intended to be used by `gnus-mark-article-hook'."
11176   (when (memq gnus-current-article gnus-newsgroup-unreads)
11177     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11178
11179 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11180   "Intended to be used by `gnus-mark-article-hook'."
11181   (let ((mark (gnus-summary-article-mark)))
11182     (when (or (gnus-unread-mark-p mark)
11183               (gnus-read-mark-p mark))
11184       (gnus-summary-mark-article gnus-current-article
11185                                  (or new-mark gnus-read-mark)))))
11186
11187 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11188   "Intended to be used by `gnus-mark-article-hook'."
11189   (let ((mark (gnus-summary-article-mark)))
11190     (when (or (gnus-unread-mark-p mark)
11191               (gnus-read-mark-p mark))
11192       (gnus-summary-mark-article (gnus-summary-article-number)
11193                                  (or new-mark gnus-read-mark)))))
11194
11195 (defun gnus-summary-mark-unread-as-ticked ()
11196   "Intended to be used by `gnus-mark-article-hook'."
11197   (when (memq gnus-current-article gnus-newsgroup-unreads)
11198     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11199
11200 (defun gnus-summary-mark-region-as-read (point mark all)
11201   "Mark all unread articles between point and mark as read.
11202 If given a prefix, mark all articles between point and mark as read,
11203 even ticked and dormant ones."
11204   (interactive "r\nP")
11205   (save-excursion
11206     (let (article)
11207       (goto-char point)
11208       (beginning-of-line)
11209       (while (and
11210               (< (point) mark)
11211               (progn
11212                 (when (or all
11213                           (memq (setq article (gnus-summary-article-number))
11214                                 gnus-newsgroup-unreads))
11215                   (gnus-summary-mark-article article gnus-del-mark))
11216                 t)
11217               (gnus-summary-find-next))))))
11218
11219 (defun gnus-summary-mark-below (score mark)
11220   "Mark articles with score less than SCORE with MARK."
11221   (interactive "P\ncMark: ")
11222   (setq score (if score
11223                   (prefix-numeric-value score)
11224                 (or gnus-summary-default-score 0)))
11225   (with-current-buffer gnus-summary-buffer
11226     (goto-char (point-min))
11227     (while
11228         (progn
11229           (and (< (gnus-summary-article-score) score)
11230                (gnus-summary-mark-article nil mark))
11231           (gnus-summary-find-next)))))
11232
11233 (defun gnus-summary-kill-below (&optional score)
11234   "Mark articles with score below SCORE as read."
11235   (interactive "P")
11236   (gnus-summary-mark-below score gnus-killed-mark))
11237
11238 (defun gnus-summary-clear-above (&optional score)
11239   "Clear all marks from articles with score above SCORE."
11240   (interactive "P")
11241   (gnus-summary-mark-above score gnus-unread-mark))
11242
11243 (defun gnus-summary-tick-above (&optional score)
11244   "Tick all articles with score above SCORE."
11245   (interactive "P")
11246   (gnus-summary-mark-above score gnus-ticked-mark))
11247
11248 (defun gnus-summary-mark-above (score mark)
11249   "Mark articles with score over SCORE with MARK."
11250   (interactive "P\ncMark: ")
11251   (setq score (if score
11252                   (prefix-numeric-value score)
11253                 (or gnus-summary-default-score 0)))
11254   (with-current-buffer gnus-summary-buffer
11255     (goto-char (point-min))
11256     (while (and (progn
11257                   (when (> (gnus-summary-article-score) score)
11258                     (gnus-summary-mark-article nil mark))
11259                   t)
11260                 (gnus-summary-find-next)))))
11261
11262 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11263 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11264 (defun gnus-summary-limit-include-expunged (&optional no-error)
11265   "Display all the hidden articles that were expunged for low scores."
11266   (interactive)
11267   (let ((inhibit-read-only t))
11268     (let ((scored gnus-newsgroup-scored)
11269           headers h)
11270       (while scored
11271         (unless (gnus-summary-article-header (caar scored))
11272           (and (setq h (gnus-number-to-header (caar scored)))
11273                (< (cdar scored) gnus-summary-expunge-below)
11274                (push h headers)))
11275         (setq scored (cdr scored)))
11276       (if (not headers)
11277           (when (not no-error)
11278             (error "No expunged articles hidden"))
11279         (goto-char (point-min))
11280         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11281         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11282         (dolist (x headers)
11283           (push (mail-header-number x) gnus-newsgroup-limit))
11284         (gnus-summary-prepare-unthreaded (nreverse headers))
11285         (goto-char (point-min))
11286         (gnus-summary-position-point)
11287         t))))
11288
11289 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11290   "Mark all unread articles in this newsgroup as read.
11291 If prefix argument ALL is non-nil, ticked and dormant articles will
11292 also be marked as read.
11293 If QUIETLY is non-nil, no questions will be asked.
11294
11295 If TO-HERE is non-nil, it should be a point in the buffer.  All
11296 articles before (after, if REVERSE is set) this point will be marked
11297 as read.
11298
11299 Note that this function will only catch up the unread article
11300 in the current summary buffer limitation.
11301
11302 The number of articles marked as read is returned."
11303   (interactive "P")
11304   (prog1
11305       (save-excursion
11306         (when (or quietly
11307                   (not gnus-interactive-catchup) ;Without confirmation?
11308                   gnus-expert-user
11309                   (gnus-y-or-n-p
11310                    (if all
11311                        "Mark absolutely all articles as read? "
11312                      "Mark all unread articles as read? ")))
11313           (if (and not-mark
11314                    (not gnus-newsgroup-adaptive)
11315                    (not gnus-newsgroup-auto-expire)
11316                    (not gnus-suppress-duplicates)
11317                    (or (not gnus-use-cache)
11318                        (eq gnus-use-cache 'passive)))
11319               (progn
11320                 (when all
11321                   (setq gnus-newsgroup-marked nil
11322                         gnus-newsgroup-spam-marked nil
11323                         gnus-newsgroup-dormant nil))
11324                 (setq gnus-newsgroup-unreads
11325                       (gnus-sorted-nunion
11326                        (gnus-sorted-intersection gnus-newsgroup-unreads
11327                                                  gnus-newsgroup-downloadable)
11328                        (gnus-sorted-difference gnus-newsgroup-unfetched
11329                                                gnus-newsgroup-cached))))
11330             ;; We actually mark all articles as canceled, which we
11331             ;; have to do when using auto-expiry or adaptive scoring.
11332             (gnus-summary-show-all-threads)
11333             (if (and to-here reverse)
11334                 (progn
11335                   (goto-char to-here)
11336                   (gnus-summary-mark-current-read-and-unread-as-read
11337                    gnus-catchup-mark)
11338                   (while (gnus-summary-find-next (not all))
11339                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11340               (when (gnus-summary-first-subject (not all))
11341                 (while (and
11342                         (if to-here (< (point) to-here) t)
11343                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11344                         (gnus-summary-find-next (not all))))))
11345             (gnus-set-mode-line 'summary))
11346           t))
11347     (gnus-summary-position-point)))
11348
11349 (defun gnus-summary-catchup-to-here (&optional all)
11350   "Mark all unticked articles before the current one as read.
11351 If ALL is non-nil, also mark ticked and dormant articles as read."
11352   (interactive "P")
11353   (save-excursion
11354     (gnus-save-hidden-threads
11355       (let ((beg (point)))
11356         ;; We check that there are unread articles.
11357         (when (or all (gnus-summary-find-prev))
11358           (gnus-summary-catchup all t beg)))))
11359   (gnus-summary-position-point))
11360
11361 (defun gnus-summary-catchup-from-here (&optional all)
11362   "Mark all unticked articles after (and including) the current one as read.
11363 If ALL is non-nil, also mark ticked and dormant articles as read."
11364   (interactive "P")
11365   (save-excursion
11366     (gnus-save-hidden-threads
11367       (let ((beg (point)))
11368         ;; We check that there are unread articles.
11369         (when (or all (gnus-summary-find-next))
11370           (gnus-summary-catchup all t beg nil t)))))
11371   (gnus-summary-position-point))
11372
11373 (defun gnus-summary-catchup-all (&optional quietly)
11374   "Mark all articles in this newsgroup as read.
11375 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11376 instead, which marks only unread articles as read."
11377   (interactive "P")
11378   (gnus-summary-catchup t quietly))
11379
11380 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11381   "Mark all unread articles in this group as read, then exit.
11382 If prefix argument ALL is non-nil, all articles are marked as read.
11383 If QUIETLY is non-nil, no questions will be asked."
11384   (interactive "P")
11385   (when (gnus-summary-catchup all quietly nil 'fast)
11386     ;; Select next newsgroup or exit.
11387     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11388              (eq gnus-auto-select-next 'quietly))
11389         (gnus-summary-next-group nil)
11390       (gnus-summary-exit))))
11391
11392 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11393   "Mark all articles in this newsgroup as read, and then exit.
11394 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11395 instead, which marks only unread articles as read."
11396   (interactive "P")
11397   (gnus-summary-catchup-and-exit t quietly))
11398
11399 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11400   "Mark all articles in this group as read and select the next group.
11401 If given a prefix, mark all articles, unread as well as ticked, as
11402 read."
11403   (interactive "P")
11404   (save-excursion
11405     (gnus-summary-catchup all))
11406   (gnus-summary-next-group))
11407
11408 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11409   "Mark all articles in this group as read and select the previous group.
11410 If given a prefix, mark all articles, unread as well as ticked, as
11411 read."
11412   (interactive "P")
11413   (save-excursion
11414     (gnus-summary-catchup all))
11415   (gnus-summary-next-group nil nil t))
11416
11417 ;;;
11418 ;;; with article
11419 ;;;
11420
11421 (defmacro gnus-with-article (article &rest forms)
11422   "Select ARTICLE and perform FORMS in the original article buffer.
11423 Then replace the article with the result."
11424   `(progn
11425      ;; We don't want the article to be marked as read.
11426      (let (gnus-mark-article-hook)
11427        (gnus-summary-select-article t t nil ,article))
11428      (set-buffer gnus-original-article-buffer)
11429      ,@forms
11430      (if (not (gnus-check-backend-function
11431                'request-replace-article (car gnus-article-current)))
11432          (gnus-message 5 "Read-only group; not replacing")
11433        (unless (gnus-request-replace-article
11434                 ,article (car gnus-article-current)
11435                 (current-buffer) t)
11436          (error "Couldn't replace article")))
11437      ;; The cache and backlog have to be flushed somewhat.
11438      (when gnus-keep-backlog
11439        (gnus-backlog-remove-article
11440         (car gnus-article-current) (cdr gnus-article-current)))
11441      (when gnus-use-cache
11442        (gnus-cache-update-article
11443         (car gnus-article-current) (cdr gnus-article-current)))))
11444
11445 (put 'gnus-with-article 'lisp-indent-function 1)
11446 (put 'gnus-with-article 'edebug-form-spec '(form body))
11447
11448 ;; Thread-based commands.
11449
11450 (defun gnus-summary-articles-in-thread (&optional article)
11451   "Return a list of all articles in the current thread.
11452 If ARTICLE is non-nil, return all articles in the thread that starts
11453 with that article."
11454   (let* ((article (or article (gnus-summary-article-number)))
11455          (data (gnus-data-find-list article))
11456          (top-level (gnus-data-level (car data)))
11457          (top-subject
11458           (cond ((null gnus-thread-operation-ignore-subject)
11459                  (gnus-simplify-subject-re
11460                   (mail-header-subject (gnus-data-header (car data)))))
11461                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11462                  (gnus-simplify-subject-fuzzy
11463                   (mail-header-subject (gnus-data-header (car data)))))
11464                 (t nil)))
11465          (end-point (save-excursion
11466                       (goto-char (gnus-data-pos (car data)))
11467                       (if (gnus-summary-go-to-next-thread)
11468                           (point) (point-max))))
11469          articles)
11470     (while (and data
11471                 (< (gnus-data-pos (car data)) end-point))
11472       (when (or (not top-subject)
11473                 (string= top-subject
11474                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11475                              (gnus-simplify-subject-fuzzy
11476                               (mail-header-subject
11477                                (gnus-data-header (car data))))
11478                            (gnus-simplify-subject-re
11479                             (mail-header-subject
11480                              (gnus-data-header (car data)))))))
11481         (push (gnus-data-number (car data)) articles))
11482       (unless (and (setq data (cdr data))
11483                    (> (gnus-data-level (car data)) top-level))
11484         (setq data nil)))
11485     ;; Return the list of articles.
11486     (nreverse articles)))
11487
11488 (defun gnus-summary-rethread-current ()
11489   "Rethread the thread the current article is part of."
11490   (interactive)
11491   (let* ((gnus-show-threads t)
11492          (article (gnus-summary-article-number))
11493          (id (mail-header-id (gnus-summary-article-header)))
11494          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11495     (unless id
11496       (error "No article on the current line"))
11497     (gnus-rebuild-thread id)
11498     (gnus-summary-goto-subject article)))
11499
11500 (defun gnus-summary-reparent-thread ()
11501   "Make the current article child of the marked (or previous) article.
11502
11503 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11504 is non-nil or the Subject: of both articles are the same."
11505   (interactive)
11506   (unless (not (gnus-group-read-only-p))
11507     (error "The current newsgroup does not support article editing"))
11508   (unless (<= (length gnus-newsgroup-processable) 1)
11509     (error "No more than one article may be marked"))
11510   (let ((child (gnus-summary-article-number))
11511         ;; First grab the marked article, otherwise one line up.
11512         (parent (if (not (null gnus-newsgroup-processable))
11513                     (car gnus-newsgroup-processable)
11514                   (save-excursion
11515                     (if (eq (forward-line -1) 0)
11516                         (gnus-summary-article-number)
11517                       (error "Beginning of summary buffer"))))))
11518     (gnus-summary-reparent-children parent (list child))))
11519
11520 (defun gnus-summary-reparent-children (parent children)
11521   "Make PARENT the parent of CHILDREN.
11522 When called interactively, PARENT is the current article and CHILDREN
11523 are the process-marked articles."
11524   (interactive
11525    (list (gnus-summary-article-number)
11526          (gnus-summary-work-articles nil)))
11527   (dolist (child children)
11528     (save-window-excursion
11529       (let ((gnus-article-buffer " *reparent*"))
11530         (unless (not (eq parent child))
11531           (error "An article may not be self-referential"))
11532         (let ((message-id (mail-header-id
11533                            (gnus-summary-article-header parent))))
11534           (unless (and message-id (not (equal message-id "")))
11535             (error "No message-id in desired parent"))
11536           (gnus-with-article child
11537             (save-restriction
11538               (goto-char (point-min))
11539               (message-narrow-to-head)
11540               (if (re-search-forward "^References: " nil t)
11541                   (progn
11542                     (re-search-forward "^[^ \t]" nil t)
11543                     (forward-line -1)
11544                     (end-of-line)
11545                     (insert " " message-id))
11546                 (insert "References: " message-id "\n"))))
11547           (set-buffer gnus-summary-buffer)
11548           (gnus-summary-unmark-all-processable)
11549           (gnus-summary-update-article child)
11550           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11551             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11552           (gnus-summary-rethread-current)
11553           (gnus-message 3 "Article %d is now the child of article %d"
11554                         child parent))))))
11555
11556 (defun gnus-summary-toggle-threads (&optional arg)
11557   "Toggle showing conversation threads.
11558 If ARG is positive number, turn showing conversation threads on."
11559   (interactive "P")
11560   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11561     (setq gnus-show-threads
11562           (if (null arg) (not gnus-show-threads)
11563             (> (prefix-numeric-value arg) 0)))
11564     (gnus-summary-prepare)
11565     (gnus-summary-goto-subject current)
11566     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11567     (gnus-summary-position-point)))
11568
11569 (eval-and-compile
11570   (if (fboundp 'remove-overlays)
11571       (defalias 'gnus-remove-overlays 'remove-overlays)
11572     (defun gnus-remove-overlays (beg end name val)
11573       "Clear BEG and END of overlays whose property NAME has value VAL.
11574 For compatibility with XEmacs."
11575       (dolist (ov (gnus-overlays-in beg end))
11576         (when (eq (gnus-overlay-get ov name) val)
11577           (gnus-delete-overlay ov))))))
11578
11579 (defun gnus-summary-show-all-threads ()
11580   "Show all threads."
11581   (interactive)
11582   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11583   (gnus-summary-position-point))
11584
11585 (defsubst gnus-summary--inv (p)
11586   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11587
11588 (defun gnus-summary-show-thread ()
11589   "Show thread subtrees.
11590 Returns nil if no thread was there to be shown."
11591   (interactive)
11592   (let* ((orig (point))
11593          (end (point-at-eol))
11594          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11595          ;; Leave point at bol
11596          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11597          (eoi (when end
11598                 (if (fboundp 'next-single-char-property-change)
11599                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11600                     (or (next-single-char-property-change end 'invisible)
11601                         (point-max))
11602                   (while (progn
11603                            (end-of-line 2)
11604                            (and (not (eobp))
11605                                 (eq (get-char-property (point) 'invisible)
11606                                     'gnus-sum))))
11607                   (point)))))
11608     (when eoi
11609       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11610       (goto-char orig)
11611       (gnus-summary-position-point)
11612       eoi)))
11613
11614 (defun gnus-summary-maybe-hide-threads ()
11615   "If requested, hide the threads that should be hidden."
11616   (when (and gnus-show-threads
11617              gnus-thread-hide-subtree)
11618     (gnus-summary-hide-all-threads
11619      (if (or (consp gnus-thread-hide-subtree)
11620              (functionp gnus-thread-hide-subtree))
11621          (gnus-make-predicate gnus-thread-hide-subtree)
11622        nil))))
11623
11624 ;;; Hiding predicates.
11625
11626 (defun gnus-article-unread-p (header)
11627   (memq (mail-header-number header) gnus-newsgroup-unreads))
11628
11629 (defun gnus-article-unseen-p (header)
11630   (memq (mail-header-number header) gnus-newsgroup-unseen))
11631
11632 (defun gnus-map-articles (predicate articles)
11633   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11634   (apply 'gnus-or (mapcar predicate
11635                           (mapcar (lambda (number)
11636                                     (gnus-summary-article-header number))
11637                                   articles))))
11638
11639 (defun gnus-summary-hide-all-threads (&optional predicate)
11640   "Hide all thread subtrees.
11641 If PREDICATE is supplied, threads that satisfy this predicate
11642 will not be hidden."
11643   (interactive)
11644   (save-excursion
11645     (goto-char (point-min))
11646     (let ((end nil)
11647           (count 0))
11648       (while (not end)
11649         (incf count)
11650         (when (zerop (mod count 1000))
11651           (message "Hiding all threads... %d" count))
11652         (when (or (not predicate)
11653                   (gnus-map-articles
11654                    predicate (gnus-summary-article-children)))
11655             (gnus-summary-hide-thread))
11656         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11657   (gnus-summary-position-point))
11658
11659 (defun gnus-summary-hide-thread ()
11660   "Hide thread subtrees.
11661 If PREDICATE is supplied, threads that satisfy this predicate
11662 will not be hidden.
11663 Returns nil if no threads were there to be hidden."
11664   (interactive)
11665   (let ((start (point))
11666         (starteol (line-end-position))
11667         (article (gnus-summary-article-number)))
11668     (goto-char start)
11669     ;; Go forward until either the buffer ends or the subthread ends.
11670     (when (and (not (eobp))
11671                (or (zerop (gnus-summary-next-thread 1 t))
11672                    (goto-char (point-max))))
11673       (if (and (> (point) start)
11674                ;; FIXME: this should actually search for a non-invisible \n.
11675                (search-backward "\n" start t))
11676           (progn
11677             (when (> (point) starteol)
11678               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11679               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11680                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11681                 (gnus-overlay-put ol 'evaporate t)))
11682             (gnus-summary-goto-subject article)
11683             (when (> start (point))
11684               (message "Hiding the thread moved us backwards, aborting!")
11685               (goto-char (point-max))))
11686         (goto-char start)
11687         nil))))
11688
11689 (defun gnus-summary-go-to-next-thread (&optional previous)
11690   "Go to the same level (or less) next thread.
11691 If PREVIOUS is non-nil, go to previous thread instead.
11692 Return the article number moved to, or nil if moving was impossible."
11693   (let ((level (gnus-summary-thread-level))
11694         (way (if previous -1 1))
11695         (beg (point)))
11696     (forward-line way)
11697     (while (and (not (eobp))
11698                 (< level (gnus-summary-thread-level)))
11699       (forward-line way))
11700     (if (eobp)
11701         (progn
11702           (goto-char beg)
11703           nil)
11704       (setq beg (point))
11705       (prog1
11706           (gnus-summary-article-number)
11707         (goto-char beg)))))
11708
11709 (defun gnus-summary-next-thread (n &optional silent)
11710   "Go to the same level next N'th thread.
11711 If N is negative, search backward instead.
11712 Returns the difference between N and the number of skips actually
11713 done.
11714
11715 If SILENT, don't output messages."
11716   (interactive "p")
11717   (let ((backward (< n 0))
11718         (n (abs n)))
11719     (while (and (> n 0)
11720                 (gnus-summary-go-to-next-thread backward))
11721       (decf n))
11722     (unless silent
11723       (gnus-summary-position-point))
11724     (when (and (not silent) (/= 0 n))
11725       (gnus-message 7 "No more threads"))
11726     n))
11727
11728 (defun gnus-summary-prev-thread (n)
11729   "Go to the same level previous N'th thread.
11730 Returns the difference between N and the number of skips actually
11731 done."
11732   (interactive "p")
11733   (gnus-summary-next-thread (- n)))
11734
11735 (defun gnus-summary-go-down-thread ()
11736   "Go down one level in the current thread."
11737   (let ((children (gnus-summary-article-children)))
11738     (when children
11739       (gnus-summary-goto-subject (car children)))))
11740
11741 (defun gnus-summary-go-up-thread ()
11742   "Go up one level in the current thread."
11743   (let ((parent (gnus-summary-article-parent)))
11744     (when parent
11745       (gnus-summary-goto-subject parent))))
11746
11747 (defun gnus-summary-down-thread (n)
11748   "Go down thread N steps.
11749 If N is negative, go up instead.
11750 Returns the difference between N and how many steps down that were
11751 taken."
11752   (interactive "p")
11753   (let ((up (< n 0))
11754         (n (abs n)))
11755     (while (and (> n 0)
11756                 (if up (gnus-summary-go-up-thread)
11757                   (gnus-summary-go-down-thread)))
11758       (setq n (1- n)))
11759     (gnus-summary-position-point)
11760     (when (/= 0 n)
11761       (gnus-message 7 "Can't go further"))
11762     n))
11763
11764 (defun gnus-summary-up-thread (n)
11765   "Go up thread N steps.
11766 If N is negative, go down instead.
11767 Returns the difference between N and how many steps down that were
11768 taken."
11769   (interactive "p")
11770   (gnus-summary-down-thread (- n)))
11771
11772 (defun gnus-summary-top-thread ()
11773   "Go to the top of the thread."
11774   (interactive)
11775   (while (gnus-summary-go-up-thread))
11776   (gnus-summary-article-number))
11777
11778 (defun gnus-summary-expire-thread ()
11779   "Mark articles under current thread as expired."
11780   (interactive)
11781   (gnus-summary-kill-thread 0))
11782
11783 (defun gnus-summary-kill-thread (&optional unmark)
11784   "Mark articles under current thread as read.
11785 If the prefix argument is positive, remove any kinds of marks.
11786 If the prefix argument is zero, mark thread as expired.
11787 If the prefix argument is negative, tick articles instead."
11788   (interactive "P")
11789   (when unmark
11790     (setq unmark (prefix-numeric-value unmark)))
11791   (let ((articles (gnus-summary-articles-in-thread))
11792         (hide (or (null unmark) (= unmark 0))))
11793     (save-excursion
11794       ;; Expand the thread.
11795       (gnus-summary-show-thread)
11796       ;; Mark all the articles.
11797       (while articles
11798         (gnus-summary-goto-subject (car articles))
11799         (cond ((null unmark)
11800                (gnus-summary-mark-article-as-read gnus-killed-mark))
11801               ((> unmark 0)
11802                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11803               ((= unmark 0)
11804                (gnus-summary-mark-article nil gnus-expirable-mark))
11805               (t
11806                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11807         (setq articles (cdr articles))))
11808     ;; Hide killed subtrees when hide is true.
11809     (and hide
11810          gnus-thread-hide-killed
11811          (gnus-summary-hide-thread))
11812     ;; If hide is t, go to next unread subject.
11813     (when hide
11814       ;; Go to next unread subject.
11815       (gnus-summary-next-subject 1 t)))
11816   (gnus-set-mode-line 'summary))
11817
11818 ;; Summary sorting commands
11819
11820 (defun gnus-summary-sort-by-number (&optional reverse)
11821   "Sort the summary buffer by article number.
11822 Argument REVERSE means reverse order."
11823   (interactive "P")
11824   (gnus-summary-sort 'number reverse))
11825
11826 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11827   "Sort the summary buffer by most recent article number.
11828 Argument REVERSE means reverse order."
11829   (interactive "P")
11830   (gnus-summary-sort 'most-recent-number reverse))
11831
11832 (defun gnus-summary-sort-by-random (&optional reverse)
11833   "Randomize the order in the summary buffer.
11834 Argument REVERSE means to randomize in reverse order."
11835   (interactive "P")
11836   (gnus-summary-sort 'random reverse))
11837
11838 (defun gnus-summary-sort-by-author (&optional reverse)
11839   "Sort the summary buffer by author name alphabetically.
11840 If `case-fold-search' is non-nil, case of letters is ignored.
11841 Argument REVERSE means reverse order."
11842   (interactive "P")
11843   (gnus-summary-sort 'author reverse))
11844
11845 (defun gnus-summary-sort-by-recipient (&optional reverse)
11846   "Sort the summary buffer by recipient name alphabetically.
11847 If `case-fold-search' is non-nil, case of letters is ignored.
11848 Argument REVERSE means reverse order."
11849   (interactive "P")
11850   (gnus-summary-sort 'recipient reverse))
11851
11852 (defun gnus-summary-sort-by-subject (&optional reverse)
11853   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11854 If `case-fold-search' is non-nil, case of letters is ignored.
11855 Argument REVERSE means reverse order."
11856   (interactive "P")
11857   (gnus-summary-sort 'subject reverse))
11858
11859 (defun gnus-summary-sort-by-date (&optional reverse)
11860   "Sort the summary buffer by date.
11861 Argument REVERSE means reverse order."
11862   (interactive "P")
11863   (gnus-summary-sort 'date reverse))
11864
11865 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11866   "Sort the summary buffer by most recent date.
11867 Argument REVERSE means reverse order."
11868   (interactive "P")
11869   (gnus-summary-sort 'most-recent-date reverse))
11870
11871 (defun gnus-summary-sort-by-score (&optional reverse)
11872   "Sort the summary buffer by score.
11873 Argument REVERSE means reverse order."
11874   (interactive "P")
11875   (gnus-summary-sort 'score reverse))
11876
11877 (defun gnus-summary-sort-by-lines (&optional reverse)
11878   "Sort the summary buffer by the number of lines.
11879 Argument REVERSE means reverse order."
11880   (interactive "P")
11881   (gnus-summary-sort 'lines reverse))
11882
11883 (defun gnus-summary-sort-by-chars (&optional reverse)
11884   "Sort the summary buffer by article length.
11885 Argument REVERSE means reverse order."
11886   (interactive "P")
11887   (gnus-summary-sort 'chars reverse))
11888
11889 (defun gnus-summary-sort-by-original (&optional reverse)
11890   "Sort the summary buffer using the default sorting method.
11891 Argument REVERSE means reverse order."
11892   (interactive "P")
11893   (let* ((inhibit-read-only t)
11894          (gnus-summary-prepare-hook nil))
11895     ;; We do the sorting by regenerating the threads.
11896     (gnus-summary-prepare)
11897     ;; Hide subthreads if needed.
11898     (gnus-summary-maybe-hide-threads)))
11899
11900 (defun gnus-summary-sort (predicate reverse)
11901   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11902   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11903          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11904          (gnus-thread-sort-functions
11905           (if (not reverse)
11906               thread
11907             `(lambda (t1 t2)
11908                (,thread t2 t1))))
11909          (gnus-sort-gathered-threads-function
11910           gnus-thread-sort-functions)
11911          (gnus-article-sort-functions
11912           (if (not reverse)
11913               article
11914             `(lambda (t1 t2)
11915                (,article t2 t1))))
11916          (inhibit-read-only t)
11917          (gnus-summary-prepare-hook nil))
11918     ;; We do the sorting by regenerating the threads.
11919     (gnus-summary-prepare)
11920     ;; Hide subthreads if needed.
11921     (gnus-summary-maybe-hide-threads)))
11922
11923 ;; Summary saving commands.
11924
11925 (defun gnus-summary-save-article (&optional n not-saved)
11926   "Save the current article using the default saver function.
11927 If N is a positive number, save the N next articles.
11928 If N is a negative number, save the N previous articles.
11929 If N is nil and any articles have been marked with the process mark,
11930 save those articles instead.
11931 The variable `gnus-default-article-saver' specifies the saver function.
11932
11933 If the optional second argument NOT-SAVED is non-nil, articles saved
11934 will not be marked as saved."
11935   (interactive "P")
11936   (require 'gnus-art)
11937   (let* ((articles (gnus-summary-work-articles n))
11938          (save-buffer (save-excursion
11939                         (nnheader-set-temp-buffer " *Gnus Save*")))
11940          (num (length articles))
11941          ;; Whether to save decoded articles or raw articles.
11942          (decode (when gnus-article-save-coding-system
11943                    (get gnus-default-article-saver :decode)))
11944          ;; When saving many articles in a single file, use the other
11945          ;; function to save articles other than the first one.
11946          (saver2 (get gnus-default-article-saver :function))
11947          (gnus-prompt-before-saving (if saver2
11948                                         t
11949                                       gnus-prompt-before-saving))
11950          (gnus-default-article-saver gnus-default-article-saver)
11951          header file)
11952     (dolist (article articles)
11953       (setq header (gnus-summary-article-header article))
11954       (if (not (vectorp header))
11955           ;; This is a pseudo-article.
11956           (if (assq 'name header)
11957               (gnus-copy-file (cdr (assq 'name header)))
11958             (gnus-message 1 "Article %d is unsavable" article))
11959         ;; This is a real article.
11960         (save-window-excursion
11961           (gnus-summary-select-article decode decode nil article)
11962           (gnus-summary-goto-subject article))
11963         (with-current-buffer save-buffer
11964           (erase-buffer)
11965           (insert-buffer-substring (if decode
11966                                        gnus-article-buffer
11967                                      gnus-original-article-buffer)))
11968         (setq file (gnus-article-save save-buffer file num))
11969         (gnus-summary-remove-process-mark article)
11970         (unless not-saved
11971           (gnus-summary-set-saved-mark article)))
11972       (when saver2
11973         (setq gnus-default-article-saver saver2
11974               saver2 nil)))
11975     (gnus-kill-buffer save-buffer)
11976     (gnus-summary-position-point)
11977     (gnus-set-mode-line 'summary)
11978     n))
11979
11980 (defun gnus-summary-pipe-output (&optional n sym)
11981   "Pipe the current article to a subprocess.
11982 If N is a positive number, pipe the N next articles.
11983 If N is a negative number, pipe the N previous articles.
11984 If N is nil and any articles have been marked with the process mark,
11985 pipe those articles instead.
11986 The default command to which articles are piped is specified by the
11987 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11988 will be prompted for the command.
11989
11990 The properties `:decode' and `:headers' that are put to the function
11991 symbol `gnus-summary-save-in-pipe' control whether this function
11992 decodes articles and what headers to keep (see the doc string for the
11993 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11994 is neither omitted nor the symbol `r', force including all headers
11995 regardless of the `:headers' property.  If it is the symbol `r',
11996 articles that are not decoded and include all headers will be piped
11997 no matter what the properties `:decode' and `:headers' are."
11998   (interactive (gnus-interactive "P\ny"))
11999   (require 'gnus-art)
12000   (let* ((articles (gnus-summary-work-articles n))
12001          (result-buffer "*Shell Command Output*")
12002          (all-headers (not (memq sym '(nil r))))
12003          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12004          (raw (eq sym 'r))
12005          (headers (get 'gnus-summary-save-in-pipe :headers))
12006          command result)
12007     (unless (numberp (car articles))
12008       (error "No article to pipe"))
12009     (setq command (gnus-read-shell-command
12010                    (concat "Shell command on "
12011                            (if (cdr articles)
12012                                (format "these %d articles" (length articles))
12013                              "this article")
12014                            ": ")
12015                    gnus-summary-pipe-output-default-command))
12016     (when (string-equal command "")
12017       (error "A command is required"))
12018     (when all-headers
12019       (put 'gnus-summary-save-in-pipe :headers nil))
12020     (unwind-protect
12021         (while articles
12022           (gnus-summary-goto-subject (pop articles))
12023           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12024           (when (and (get-buffer result-buffer)
12025                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12026             (setq result (concat result (with-current-buffer result-buffer
12027                                           (buffer-string))))))
12028       (put 'gnus-summary-save-in-pipe :headers headers))
12029     (unless (zerop (length result))
12030       (if (with-current-buffer (get-buffer-create result-buffer)
12031             (erase-buffer)
12032             (insert result)
12033             (prog1
12034                 (and (= (count-lines (point-min) (point)) 1)
12035                      (progn
12036                        (end-of-line 0)
12037                        (<= (current-column)
12038                            (window-width (minibuffer-window)))))
12039               (goto-char (point-min))))
12040           (message "%s" (substring result 0 -1))
12041         (message nil)
12042         (gnus-configure-windows 'pipe)))))
12043
12044 (defun gnus-summary-save-article-mail (&optional arg)
12045   "Append the current article to a Unix mail box file.
12046 If N is a positive number, save the N next articles.
12047 If N is a negative number, save the N previous articles.
12048 If N is nil and any articles have been marked with the process mark,
12049 save those articles instead."
12050   (interactive "P")
12051   (require 'gnus-art)
12052   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12053     (gnus-summary-save-article arg)))
12054
12055 (defun gnus-summary-save-article-rmail (&optional arg)
12056   "Append the current article to an rmail file.
12057 If N is a positive number, save the N next articles.
12058 If N is a negative number, save the N previous articles.
12059 If N is nil and any articles have been marked with the process mark,
12060 save those articles instead."
12061   (interactive "P")
12062   (require 'gnus-art)
12063   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12064     (gnus-summary-save-article arg)))
12065
12066 (defun gnus-summary-save-article-file (&optional arg)
12067   "Append the current article to a file.
12068 If N is a positive number, save the N next articles.
12069 If N is a negative number, save the N previous articles.
12070 If N is nil and any articles have been marked with the process mark,
12071 save those articles instead."
12072   (interactive "P")
12073   (require 'gnus-art)
12074   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12075     (gnus-summary-save-article arg)))
12076
12077 (defun gnus-summary-write-article-file (&optional arg)
12078   "Write the current article to a file, deleting the previous file.
12079 If N is a positive number, save the N next articles.
12080 If N is a negative number, save the N previous articles.
12081 If N is nil and any articles have been marked with the process mark,
12082 save those articles instead."
12083   (interactive "P")
12084   (require 'gnus-art)
12085   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12086     (gnus-summary-save-article arg)))
12087
12088 (defun gnus-summary-save-article-body-file (&optional arg)
12089   "Append the current article body to a file.
12090 If N is a positive number, save the N next articles.
12091 If N is a negative number, save the N previous articles.
12092 If N is nil and any articles have been marked with the process mark,
12093 save those articles instead."
12094   (interactive "P")
12095   (require 'gnus-art)
12096   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12097     (gnus-summary-save-article arg)))
12098
12099 (defun gnus-summary-write-article-body-file (&optional arg)
12100   "Write the current article body to a file, deleting the previous file.
12101 If N is a positive number, save the N next articles.
12102 If N is a negative number, save the N previous articles.
12103 If N is nil and any articles have been marked with the process mark,
12104 save those articles instead."
12105   (interactive "P")
12106   (require 'gnus-art)
12107   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12108     (gnus-summary-save-article arg)))
12109
12110 (defun gnus-summary-muttprint (&optional arg)
12111   "Print the current article using Muttprint.
12112 If N is a positive number, save the N next articles.
12113 If N is a negative number, save the N previous articles.
12114 If N is nil and any articles have been marked with the process mark,
12115 save those articles instead."
12116   (interactive "P")
12117   (require 'gnus-art)
12118   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12119     (gnus-summary-save-article arg t)))
12120
12121 (defun gnus-summary-pipe-message (program)
12122   "Pipe the current article through PROGRAM."
12123   (interactive "sProgram: ")
12124   (gnus-summary-select-article)
12125   (let ((mail-header-separator ""))
12126     (gnus-eval-in-buffer-window gnus-article-buffer
12127       (save-restriction
12128         (widen)
12129         (let ((start (window-start))
12130               (inhibit-read-only t))
12131           (message-pipe-buffer-body program)
12132           (set-window-start (get-buffer-window (current-buffer)) start))))))
12133
12134 (defun gnus-get-split-value (methods)
12135   "Return a value based on the split METHODS."
12136   (let (split-name method result match)
12137     (when methods
12138       (with-current-buffer gnus-original-article-buffer
12139         (save-restriction
12140           (nnheader-narrow-to-headers)
12141           (while (and methods (not split-name))
12142             (goto-char (point-min))
12143             (setq method (pop methods))
12144             (setq match (car method))
12145             (when (cond
12146                    ((stringp match)
12147                     ;; Regular expression.
12148                     (ignore-errors
12149                       (re-search-forward match nil t)))
12150                    ((functionp match)
12151                     ;; Function.
12152                     (save-restriction
12153                       (widen)
12154                       (setq result (funcall match gnus-newsgroup-name))))
12155                    ((consp match)
12156                     ;; Form.
12157                     (save-restriction
12158                       (widen)
12159                       (setq result (eval match)))))
12160               (setq split-name (cdr method))
12161               (cond ((stringp result)
12162                      (push (expand-file-name
12163                             result gnus-article-save-directory)
12164                            split-name))
12165                     ((consp result)
12166                      (setq split-name (append result split-name)))))))))
12167     (nreverse split-name)))
12168
12169 (defun gnus-valid-move-group-p (group)
12170   (and (symbolp group)
12171        (boundp group)
12172        (symbol-name group)
12173        (symbol-value group)
12174        (gnus-get-function (gnus-find-method-for-group
12175                            (symbol-name group)) 'request-accept-article t)))
12176
12177 (defun gnus-read-move-group-name (prompt default articles prefix)
12178   "Read a group name."
12179   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12180          (minibuffer-confirm-incomplete nil) ; XEmacs
12181          (prom
12182           (format "%s %s to"
12183                   prompt
12184                   (if (> (length articles) 1)
12185                       (format "these %d articles" (length articles))
12186                     "this article")))
12187          (to-newsgroup
12188           (cond
12189            ((null split-name)
12190             (gnus-group-completing-read
12191              prom
12192              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12193              nil prefix nil default))
12194            ((= 1 (length split-name))
12195             (gnus-group-completing-read
12196              prom
12197              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12198              nil prefix 'gnus-group-history (car split-name)))
12199            (t
12200             (gnus-completing-read
12201              prom (nreverse split-name) nil nil 'gnus-group-history))))
12202          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12203          encoded)
12204     (when to-newsgroup
12205       (if (or (string= to-newsgroup "")
12206               (string= to-newsgroup prefix))
12207           (setq to-newsgroup default))
12208       (unless to-newsgroup
12209         (error "No group name entered"))
12210       (setq encoded (mm-encode-coding-string
12211                      to-newsgroup
12212                      (gnus-group-name-charset to-method to-newsgroup)))
12213       (or (gnus-active encoded)
12214           (gnus-activate-group encoded nil nil to-method)
12215           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12216                                      to-newsgroup))
12217               (or (and (gnus-request-create-group encoded to-method)
12218                        (gnus-activate-group encoded nil nil to-method)
12219                        (gnus-subscribe-group encoded))
12220                   (error "Couldn't create group %s" to-newsgroup)))
12221           (error "No such group: %s" to-newsgroup))
12222       encoded)))
12223
12224 (defvar gnus-summary-save-parts-counter)
12225 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12226
12227 (defun gnus-summary-save-parts (type dir n &optional reverse)
12228   "Save parts matching TYPE to DIR.
12229 If REVERSE, save parts that do not match TYPE."
12230   (interactive
12231    (list (read-string "Save parts of type: "
12232                       (or (car gnus-summary-save-parts-type-history)
12233                           gnus-summary-save-parts-default-mime)
12234                       'gnus-summary-save-parts-type-history)
12235          (setq gnus-summary-save-parts-last-directory
12236                (read-directory-name "Save to directory: "
12237                                     gnus-summary-save-parts-last-directory
12238                                     nil t))
12239          current-prefix-arg))
12240   (gnus-summary-iterate n
12241     (let ((gnus-display-mime-function nil)
12242           gnus-article-prepare-hook
12243           gnus-article-decode-hook
12244           gnus-display-mime-function
12245           gnus-break-pages
12246           (gnus-inhibit-treatment t))
12247       (gnus-summary-select-article))
12248     (with-current-buffer gnus-article-buffer
12249       (let ((handles (or gnus-article-mime-handles
12250                          (mm-dissect-buffer nil gnus-article-loose-mime)
12251                          (and gnus-article-emulate-mime
12252                               (mm-uu-dissect))))
12253             (gnus-summary-save-parts-counter 1))
12254         (when handles
12255           (gnus-summary-save-parts-1 type dir handles reverse)
12256           (unless gnus-article-mime-handles ;; Don't destroy this case.
12257             (mm-destroy-parts handles)))))))
12258
12259 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12260   (if (stringp (car handle))
12261       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12262               (cdr handle))
12263     (when (if reverse
12264               (not (string-match type (mm-handle-media-type handle)))
12265             (string-match type (mm-handle-media-type handle)))
12266       (let ((file (expand-file-name
12267                    (gnus-map-function
12268                     mm-file-name-rewrite-functions
12269                     (file-name-nondirectory
12270                      (or
12271                       (mm-handle-filename handle)
12272                       (format "%s.%d.%d" gnus-newsgroup-name
12273                               (cdr gnus-article-current)
12274                               gnus-summary-save-parts-counter))))
12275                    dir)))
12276         (incf gnus-summary-save-parts-counter)
12277         (unless (file-exists-p file)
12278           (mm-save-part-to-file handle file))))))
12279
12280 ;; Summary extract commands
12281
12282 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12283   (let ((inhibit-read-only t)
12284         (article (gnus-summary-article-number))
12285         after-article b e)
12286     (unless (gnus-summary-goto-subject article)
12287       (error "No such article: %d" article))
12288     (gnus-summary-position-point)
12289     ;; If all commands are to be bunched up on one line, we collect
12290     ;; them here.
12291     (unless gnus-view-pseudos-separately
12292       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12293             files action)
12294         (while ps
12295           (setq action (cdr (assq 'action (car ps))))
12296           (setq files (list (cdr (assq 'name (car ps)))))
12297           (while (and ps (cdr ps)
12298                       (string= (or action "1")
12299                                (or (cdr (assq 'action (cadr ps))) "2")))
12300             (push (cdr (assq 'name (cadr ps))) files)
12301             (setcdr ps (cddr ps)))
12302           (when files
12303             (when (not (string-match "%s" action))
12304               (push " " files))
12305             (push " " files)
12306             (when (assq 'execute (car ps))
12307               (setcdr (assq 'execute (car ps))
12308                       (funcall (if (string-match "%s" action)
12309                                    'format 'concat)
12310                                action
12311                                (mapconcat
12312                                 (lambda (f)
12313                                   (if (equal f " ")
12314                                       f
12315                                     (shell-quote-argument f)))
12316                                 files " ")))))
12317           (setq ps (cdr ps)))))
12318     (if (and gnus-view-pseudos (not not-view))
12319         (while pslist
12320           (when (assq 'execute (car pslist))
12321             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12322                                   (eq gnus-view-pseudos 'not-confirm)))
12323           (setq pslist (cdr pslist)))
12324       (save-excursion
12325         (while pslist
12326           (setq after-article (or (cdr (assq 'article (car pslist)))
12327                                   (gnus-summary-article-number)))
12328           (gnus-summary-goto-subject after-article)
12329           (forward-line 1)
12330           (setq b (point))
12331           (insert "    " (file-name-nondirectory
12332                           (cdr (assq 'name (car pslist))))
12333                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12334           (setq e (point))
12335           (forward-line -1)             ; back to `b'
12336           (gnus-add-text-properties
12337            b (1- e) (list 'gnus-number gnus-reffed-article-number
12338                           gnus-mouse-face-prop gnus-mouse-face))
12339           (gnus-data-enter
12340            after-article gnus-reffed-article-number
12341            gnus-unread-mark b (car pslist) 0 (- e b))
12342           (setq gnus-newsgroup-unreads
12343                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12344                                          gnus-reffed-article-number))
12345           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12346           (setq pslist (cdr pslist)))))))
12347
12348 (defun gnus-pseudos< (p1 p2)
12349   (let ((c1 (cdr (assq 'action p1)))
12350         (c2 (cdr (assq 'action p2))))
12351     (and c1 c2 (string< c1 c2))))
12352
12353 (defun gnus-request-pseudo-article (props)
12354   (cond ((assq 'execute props)
12355          (gnus-execute-command (cdr (assq 'execute props)))))
12356   (let ((gnus-current-article (gnus-summary-article-number)))
12357     (gnus-run-hooks 'gnus-mark-article-hook)))
12358
12359 (defun gnus-execute-command (command &optional automatic)
12360   (save-excursion
12361     (gnus-article-setup-buffer)
12362     (set-buffer gnus-article-buffer)
12363     (setq buffer-read-only nil)
12364     (let ((command (if automatic command
12365                      (read-string "Command: " (cons command 0)))))
12366       (erase-buffer)
12367       (insert "$ " command "\n\n")
12368       (if gnus-view-pseudo-asynchronously
12369           (start-process "gnus-execute" (current-buffer) shell-file-name
12370                          shell-command-switch command)
12371         (call-process shell-file-name nil t nil
12372                       shell-command-switch command)))))
12373
12374 ;; Summary kill commands.
12375
12376 (defun gnus-summary-edit-global-kill (article)
12377   "Edit the \"global\" kill file."
12378   (interactive (list (gnus-summary-article-number)))
12379   (gnus-group-edit-global-kill article))
12380
12381 (defun gnus-summary-edit-local-kill ()
12382   "Edit a local kill file applied to the current newsgroup."
12383   (interactive)
12384   (setq gnus-current-headers (gnus-summary-article-header))
12385   (gnus-group-edit-local-kill
12386    (gnus-summary-article-number) gnus-newsgroup-name))
12387
12388 ;;; Header reading.
12389
12390 (defun gnus-read-header (id &optional header)
12391   "Read the headers of article ID and enter them into the Gnus system."
12392   (let ((group gnus-newsgroup-name)
12393         (gnus-override-method
12394          (or
12395           gnus-override-method
12396           (and (gnus-news-group-p gnus-newsgroup-name)
12397                (car (gnus-refer-article-methods)))))
12398         where)
12399     ;; First we check to see whether the header in question is already
12400     ;; fetched.
12401     (if (stringp id)
12402         ;; This is a Message-ID.
12403         (setq header (or header (gnus-id-to-header id)))
12404       ;; This is an article number.
12405       (setq header (or header (gnus-summary-article-header id))))
12406     (if (and header
12407              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12408         ;; We have found the header.
12409         header
12410       ;; We have to really fetch the header to this article.
12411       (with-current-buffer nntp-server-buffer
12412         (when (setq where (gnus-request-head id group))
12413           (nnheader-fold-continuation-lines)
12414           (goto-char (point-max))
12415           (insert ".\n")
12416           (goto-char (point-min))
12417           (insert "211 ")
12418           (princ (cond
12419                   ((numberp id) id)
12420                   ((cdr where) (cdr where))
12421                   (header (mail-header-number header))
12422                   (t gnus-reffed-article-number))
12423                  (current-buffer))
12424           (insert " Article retrieved.\n"))
12425         (if (or (not where)
12426                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12427             ()                          ; Malformed head.
12428           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12429             (when (and (stringp id)
12430                        (or
12431                         (not (string= (gnus-group-real-name group)
12432                                       (car where)))
12433                         (not (gnus-server-equal gnus-override-method
12434                                                 (gnus-group-method group)))))
12435               ;; If we fetched by Message-ID and the article came from
12436               ;; a different group (or server), we fudge some bogus
12437               ;; article numbers for this article.
12438               (mail-header-set-number header gnus-reffed-article-number))
12439             (with-current-buffer gnus-summary-buffer
12440               (decf gnus-reffed-article-number)
12441               (gnus-remove-header (mail-header-number header))
12442               (push header gnus-newsgroup-headers)
12443               (setq gnus-current-headers header)
12444               (push (mail-header-number header) gnus-newsgroup-limit)))
12445           header)))))
12446
12447 (defun gnus-remove-header (number)
12448   "Remove header NUMBER from `gnus-newsgroup-headers'."
12449   (if (and gnus-newsgroup-headers
12450            (= number (mail-header-number (car gnus-newsgroup-headers))))
12451       (pop gnus-newsgroup-headers)
12452     (let ((headers gnus-newsgroup-headers))
12453       (while (and (cdr headers)
12454                   (not (= number (mail-header-number (cadr headers)))))
12455         (pop headers))
12456       (when (cdr headers)
12457         (setcdr headers (cddr headers))))))
12458
12459 ;;;
12460 ;;; summary highlights
12461 ;;;
12462
12463 (defun gnus-highlight-selected-summary ()
12464   "Highlight selected article in summary buffer."
12465   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12466   (when gnus-summary-selected-face
12467     (save-excursion
12468       (let* ((beg (point-at-bol))
12469              (end (point-at-eol))
12470              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12471              (from (if (get-text-property beg gnus-mouse-face-prop)
12472                        beg
12473                      (or (next-single-property-change
12474                           beg gnus-mouse-face-prop nil end)
12475                          beg)))
12476              (to
12477               (if (= from end)
12478                   (- from 2)
12479                 (or (next-single-property-change
12480                      from gnus-mouse-face-prop nil end)
12481                     end))))
12482         ;; If no mouse-face prop on line we will have to = from = end,
12483         ;; so we highlight the entire line instead.
12484         (when (= (+ to 2) from)
12485           (setq from beg)
12486           (setq to end))
12487         (if gnus-newsgroup-selected-overlay
12488             ;; Move old overlay.
12489             (gnus-move-overlay
12490              gnus-newsgroup-selected-overlay from to (current-buffer))
12491           ;; Create new overlay.
12492           (gnus-overlay-put
12493            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12494            'face gnus-summary-selected-face))))))
12495
12496 (defvar gnus-summary-highlight-line-cached nil)
12497 (defvar gnus-summary-highlight-line-trigger nil)
12498
12499 (defun gnus-summary-highlight-line-0 ()
12500   (if (and (eq gnus-summary-highlight-line-trigger
12501                gnus-summary-highlight)
12502            gnus-summary-highlight-line-cached)
12503       gnus-summary-highlight-line-cached
12504     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12505           gnus-summary-highlight-line-cached
12506           (let* ((cond (list 'cond))
12507                  (c cond)
12508                  (list gnus-summary-highlight))
12509             (while list
12510               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12511                               nil))
12512               (setq c (cdr c)
12513                     list (cdr list)))
12514             (gnus-byte-compile (list 'lambda nil cond))))))
12515
12516 (defun gnus-summary-highlight-line ()
12517   "Highlight current line according to `gnus-summary-highlight'."
12518   (let* ((beg (point-at-bol))
12519          (article (or (gnus-summary-article-number) gnus-current-article))
12520          (score (or (cdr (assq article
12521                                gnus-newsgroup-scored))
12522                     gnus-summary-default-score 0))
12523          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12524          (inhibit-read-only t)
12525          (default gnus-summary-default-score)
12526          (default-high gnus-summary-default-high-score)
12527          (default-low gnus-summary-default-low-score)
12528          (uncached (and gnus-summary-use-undownloaded-faces
12529                         (memq article gnus-newsgroup-undownloaded)
12530                         (not (memq article gnus-newsgroup-cached)))))
12531     (let ((face (funcall (gnus-summary-highlight-line-0))))
12532       (unless (eq face (get-text-property beg 'face))
12533         (gnus-put-text-property-excluding-characters-with-faces
12534          beg (point-at-eol) 'face
12535          (setq face (if (boundp face) (symbol-value face) face)))
12536         (when gnus-summary-highlight-line-function
12537           (funcall gnus-summary-highlight-line-function article face))))))
12538
12539 (defun gnus-update-read-articles (group unread &optional compute)
12540   "Update the list of read articles in GROUP.
12541 UNREAD is a sorted list."
12542   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12543         (info (gnus-get-info group))
12544         (prev 1)
12545         read)
12546     (if (or (not info) (not active))
12547         ;; There is no info on this group if it was, in fact,
12548         ;; killed.  Gnus stores no information on killed groups, so
12549         ;; there's nothing to be done.
12550         ;; One could store the information somewhere temporarily,
12551         ;; perhaps...  Hmmm...
12552         ()
12553       ;; Remove any negative articles numbers.
12554       (while (and unread (< (car unread) 0))
12555         (setq unread (cdr unread)))
12556       ;; Remove any expired article numbers
12557       (while (and unread (< (car unread) (car active)))
12558         (setq unread (cdr unread)))
12559       ;; Compute the ranges of read articles by looking at the list of
12560       ;; unread articles.
12561       (while unread
12562         (when (/= (car unread) prev)
12563           (push (if (= prev (1- (car unread))) prev
12564                   (cons prev (1- (car unread))))
12565                 read))
12566         (setq prev (1+ (car unread)))
12567         (setq unread (cdr unread)))
12568       (when (<= prev (cdr active))
12569         (push (cons prev (cdr active)) read))
12570       (setq read (if (> (length read) 1) (nreverse read) read))
12571       (if compute
12572           read
12573         (save-excursion
12574           (let (setmarkundo)
12575             ;; Propagate the read marks to the backend.
12576             (when (and (or gnus-propagate-marks
12577                            (gnus-method-option-p
12578                             (gnus-find-method-for-group group)
12579                             'server-marks))
12580                        (gnus-check-backend-function 'request-set-mark group))
12581               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12582                     (add (gnus-remove-from-range read (gnus-info-read info))))
12583                 (when (or add del)
12584                   (unless (gnus-check-group group)
12585                     (error "Can't open server for %s" group))
12586                   (gnus-request-set-mark
12587                    group (delq nil (list (if add (list add 'add '(read)))
12588                                          (if del (list del 'del '(read))))))
12589                   (setq setmarkundo
12590                         `(gnus-request-set-mark
12591                           ,group
12592                           ',(delq nil (list
12593                                        (if del (list del 'add '(read)))
12594                                        (if add (list add 'del '(read))))))))))
12595             (set-buffer gnus-group-buffer)
12596             (gnus-undo-register
12597               `(progn
12598                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12599                  (gnus-info-set-read ',info ',(gnus-info-read info))
12600                  (gnus-get-unread-articles-in-group ',info
12601                                                     (gnus-active ,group))
12602                  (gnus-group-update-group ,group t)
12603                  ,setmarkundo))))
12604         ;; Enter this list into the group info.
12605         (gnus-info-set-read info read)
12606         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12607         (gnus-get-unread-articles-in-group info (gnus-active group))
12608         t))))
12609
12610 (defun gnus-offer-save-summaries ()
12611   "Offer to save all active summary buffers."
12612   (let (buffers)
12613     ;; Go through all buffers and find all summaries.
12614     (dolist (buffer (buffer-list))
12615       (when (and (setq buffer (buffer-name buffer))
12616                  (string-match "Summary" buffer)
12617                  (with-current-buffer buffer
12618                    ;; We check that this is, indeed, a summary buffer.
12619                    (and (eq major-mode 'gnus-summary-mode)
12620                         ;; Also make sure this isn't bogus.
12621                         gnus-newsgroup-prepared
12622                         ;; Also make sure that this isn't a
12623                         ;; dead summary buffer.
12624                         (not gnus-dead-summary-mode))))
12625         (push buffer buffers)))
12626     ;; Go through all these summary buffers and offer to save them.
12627     (when buffers
12628       (save-excursion
12629         (if (eq gnus-interactive-exit 'quiet)
12630             (dolist (buffer buffers)
12631               (switch-to-buffer buffer)
12632               (gnus-summary-exit))
12633           (map-y-or-n-p
12634            "Update summary buffer %s? "
12635            (lambda (buf)
12636              (switch-to-buffer buf)
12637              (gnus-summary-exit))
12638            buffers))))))
12639
12640 (defun gnus-summary-setup-default-charset ()
12641   "Setup newsgroup default charset."
12642   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12643       (setq gnus-newsgroup-charset nil)
12644     (let* ((ignored-charsets
12645             (or gnus-newsgroup-ephemeral-ignored-charsets
12646                 (append
12647                  (and gnus-newsgroup-name
12648                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12649                  gnus-newsgroup-ignored-charsets))))
12650       (setq gnus-newsgroup-charset
12651             (or gnus-newsgroup-ephemeral-charset
12652                 (and gnus-newsgroup-name
12653                      (gnus-parameter-charset gnus-newsgroup-name))
12654                 gnus-default-charset))
12655       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12656            ignored-charsets))))
12657
12658 ;;;
12659 ;;; Mime Commands
12660 ;;;
12661
12662 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12663   "Display the current article buffer fully MIME-buttonized.
12664 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12665 treated as multipart/mixed."
12666   (interactive "P")
12667   (require 'gnus-art)
12668   (let ((gnus-unbuttonized-mime-types nil)
12669         (gnus-mime-display-multipart-as-mixed show-all-parts))
12670     (gnus-summary-show-article)))
12671
12672 (defun gnus-summary-repair-multipart (article)
12673   "Add a Content-Type header to a multipart article without one."
12674   (interactive (list (gnus-summary-article-number)))
12675   (gnus-with-article article
12676     (message-narrow-to-head)
12677     (message-remove-header "Mime-Version")
12678     (goto-char (point-max))
12679     (insert "Mime-Version: 1.0\n")
12680     (widen)
12681     (when (search-forward "\n--" nil t)
12682       (let ((separator (buffer-substring (point) (point-at-eol))))
12683         (message-narrow-to-head)
12684         (message-remove-header "Content-Type")
12685         (goto-char (point-max))
12686         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12687                         separator))
12688         (widen))))
12689   (let (gnus-mark-article-hook)
12690     (gnus-summary-select-article t t nil article)))
12691
12692 (defun gnus-summary-toggle-display-buttonized ()
12693   "Toggle the buttonizing of the article buffer."
12694   (interactive)
12695   (require 'gnus-art)
12696   (if (setq gnus-inhibit-mime-unbuttonizing
12697             (not gnus-inhibit-mime-unbuttonizing))
12698       (let ((gnus-unbuttonized-mime-types nil))
12699         (gnus-summary-show-article))
12700     (gnus-summary-show-article)))
12701
12702 ;;;
12703 ;;; Generic summary marking commands
12704 ;;;
12705
12706 (defvar gnus-summary-marking-alist
12707   '((read gnus-del-mark "d")
12708     (unread gnus-unread-mark "u")
12709     (ticked gnus-ticked-mark "!")
12710     (dormant gnus-dormant-mark "?")
12711     (expirable gnus-expirable-mark "e"))
12712   "An alist of names/marks/keystrokes.")
12713
12714 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12715 (defvar gnus-summary-mark-map)
12716
12717 (defun gnus-summary-make-all-marking-commands ()
12718   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12719   (dolist (elem gnus-summary-marking-alist)
12720     (apply 'gnus-summary-make-marking-command elem)))
12721
12722 (defun gnus-summary-make-marking-command (name mark keystroke)
12723   (let ((map (make-sparse-keymap)))
12724     (define-key gnus-summary-generic-mark-map keystroke map)
12725     (dolist (lway `((next "next" next nil "n")
12726                     (next-unread "next unread" next t "N")
12727                     (prev "previous" prev nil "p")
12728                     (prev-unread "previous unread" prev t "P")
12729                     (nomove "" nil nil ,keystroke)))
12730       (let ((func (gnus-summary-make-marking-command-1
12731                    mark (car lway) lway name)))
12732         (setq func (eval func))
12733         (define-key map (nth 4 lway) func)))))
12734
12735 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12736   `(defun ,(intern
12737             (format "gnus-summary-put-mark-as-%s%s"
12738                     name (if (eq way 'nomove)
12739                              ""
12740                            (concat "-" (symbol-name way)))))
12741      (n)
12742      ,(format
12743        "Mark the current article as %s%s.
12744 If N, the prefix, then repeat N times.
12745 If N is negative, move in reverse order.
12746 The difference between N and the actual number of articles marked is
12747 returned."
12748        name (cadr lway))
12749      (interactive "p")
12750      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12751
12752 (defun gnus-summary-generic-mark (n mark move unread)
12753   "Mark N articles with MARK."
12754   (unless (eq major-mode 'gnus-summary-mode)
12755     (error "This command can only be used in the summary buffer"))
12756   (gnus-summary-show-thread)
12757   (let ((nummove
12758          (cond
12759           ((eq move 'next) 1)
12760           ((eq move 'prev) -1)
12761           (t 0))))
12762     (if (zerop nummove)
12763         (setq n 1)
12764       (when (< n 0)
12765         (setq n (abs n)
12766               nummove (* -1 nummove))))
12767     (while (and (> n 0)
12768                 (gnus-summary-mark-article nil mark)
12769                 (zerop (gnus-summary-next-subject nummove unread t)))
12770       (setq n (1- n)))
12771     (when (/= 0 n)
12772       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12773     (gnus-summary-recenter)
12774     (gnus-summary-position-point)
12775     (gnus-set-mode-line 'summary)
12776     n))
12777
12778 (defun gnus-summary-insert-articles (articles)
12779   (when (setq articles
12780               (gnus-sorted-difference articles
12781                                       (mapcar (lambda (h)
12782                                                 (mail-header-number h))
12783                                               gnus-newsgroup-headers)))
12784     (setq gnus-newsgroup-headers
12785           (gnus-merge 'list
12786                       gnus-newsgroup-headers
12787                       (gnus-fetch-headers articles)
12788                       'gnus-article-sort-by-number))
12789     (setq gnus-newsgroup-articles
12790           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12791     ;; Suppress duplicates?
12792     (when gnus-suppress-duplicates
12793       (gnus-dup-suppress-articles))
12794
12795     (if (and gnus-fetch-old-headers
12796              (eq gnus-headers-retrieved-by 'nov))
12797         ;; We might want to build some more threads first.
12798         (if (eq gnus-fetch-old-headers 'invisible)
12799             (gnus-build-all-threads)
12800           (gnus-build-old-threads))
12801       ;; Mark the inserted articles that are unread as unread.
12802       (setq gnus-newsgroup-unreads
12803             (gnus-sorted-nunion
12804              gnus-newsgroup-unreads
12805              (gnus-sorted-nintersection
12806               (gnus-list-of-unread-articles gnus-newsgroup-name)
12807               articles)))
12808       ;; Mark the inserted articles as selected so that the information
12809       ;; of the marks having been changed by a user may be updated when
12810       ;; exiting this group.  See `gnus-summary-update-info'.
12811       (dolist (art articles)
12812         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12813     ;; Let the Gnus agent mark articles as read.
12814     (when gnus-agent
12815       (gnus-agent-get-undownloaded-list))
12816     ;; Remove list identifiers from subject
12817     (gnus-summary-remove-list-identifiers)
12818     ;; First and last article in this newsgroup.
12819     (when gnus-newsgroup-headers
12820       (setq gnus-newsgroup-begin
12821             (mail-header-number (car gnus-newsgroup-headers))
12822             gnus-newsgroup-end
12823             (mail-header-number
12824              (gnus-last-element gnus-newsgroup-headers))))
12825     (when gnus-use-scoring
12826       (gnus-possibly-score-headers))))
12827
12828 (defun gnus-summary-insert-old-articles (&optional all)
12829   "Insert all old articles in this group.
12830 If ALL is non-nil, already read articles become readable.
12831 If ALL is a number, fetch this number of articles."
12832   (interactive "P")
12833   (prog1
12834       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12835             older len)
12836         (setq older
12837               ;; Some nntp servers lie about their active range.  When
12838               ;; this happens, the active range can be in the millions.
12839               ;; Use a compressed range to avoid creating a huge list.
12840               (gnus-range-difference (list gnus-newsgroup-active) old))
12841         (setq len (gnus-range-length older))
12842         (cond
12843          ((null older) nil)
12844          ((numberp all)
12845           (if (< all len)
12846               (let ((older-range (nreverse older)))
12847                 (setq older nil)
12848
12849                 (while (> all 0)
12850                   (let* ((r (pop older-range))
12851                          (min (if (numberp r) r (car r)))
12852                          (max (if (numberp r) r (cdr r))))
12853                     (while (and (<= min max)
12854                                 (> all 0))
12855                       (push max older)
12856                       (setq all (1- all)
12857                             max (1- max))))))
12858             (setq older (gnus-uncompress-range older))))
12859          (all
12860           (setq older (gnus-uncompress-range older)))
12861          (t
12862           (when (and (numberp gnus-large-newsgroup)
12863                    (> len gnus-large-newsgroup))
12864               (let* ((cursor-in-echo-area nil)
12865                      (initial (gnus-parameter-large-newsgroup-initial
12866                                gnus-newsgroup-name))
12867                      (input
12868                       (read-string
12869                        (format
12870                         "How many articles from %s (%s %d): "
12871                         (gnus-group-decoded-name gnus-newsgroup-name)
12872                         (if initial "max" "default")
12873                         len)
12874                        nil nil
12875                        (if initial
12876                            (cons (number-to-string initial)
12877                                  0)))))
12878                 (unless (string-match "^[ \t]*$" input)
12879                   (setq all (string-to-number input))
12880                   (if (< all len)
12881                       (let ((older-range (nreverse older)))
12882                         (setq older nil)
12883
12884                         (while (> all 0)
12885                           (let* ((r (pop older-range))
12886                                  (min (if (numberp r) r (car r)))
12887                                  (max (if (numberp r) r (cdr r))))
12888                             (while (and (<= min max)
12889                                         (> all 0))
12890                               (push max older)
12891                               (setq all (1- all)
12892                                     max (1- max))))))))))
12893           (setq older (gnus-uncompress-range older))))
12894         (if (not older)
12895             (message "No old news.")
12896           (gnus-summary-insert-articles older)
12897           (gnus-summary-limit (gnus-sorted-nunion old older))))
12898     (gnus-summary-position-point)))
12899
12900 (defun gnus-summary-insert-new-articles ()
12901   "Insert all new articles in this group."
12902   (interactive)
12903   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12904         (old-high gnus-newsgroup-highest)
12905         (nnmail-fetched-sources (list t))
12906         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12907         i new)
12908     (unless new-active
12909       (error "Couldn't fetch new data"))
12910     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12911     (setq i (cdr gnus-newsgroup-active)
12912           gnus-newsgroup-highest i)
12913     (while (> i old-high)
12914       (push i new)
12915       (decf i))
12916     (if (not new)
12917         (message "No gnus is bad news")
12918       (gnus-summary-insert-articles new)
12919       (setq gnus-newsgroup-unreads
12920             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12921       (gnus-summary-limit (gnus-sorted-nunion old new))))
12922   (gnus-summary-position-point))
12923
12924 ;;; Bookmark support for Gnus.
12925 (declare-function bookmark-make-record-default
12926                   "bookmark" (&optional no-file no-context posn))
12927 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12928 (declare-function bookmark-default-handler "bookmark" (bmk))
12929 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12930 (defvar bookmark-yank-point)
12931 (defvar bookmark-current-buffer)
12932
12933 (defun gnus-summary-bookmark-make-record ()
12934   "Make a bookmark entry for a Gnus summary buffer."
12935   (let (pos buf)
12936     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12937       (save-restriction              ; FIXME is it necessary to widen?
12938         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12939       (setq buf "art") ; We are recording bookmark from article buffer.
12940       (setq bookmark-yank-point (point))
12941       (setq bookmark-current-buffer (current-buffer))
12942       (gnus-article-show-summary))      ; Go back in summary buffer.
12943     ;; We are now recording bookmark from summary buffer.
12944     (unless buf (setq buf "sum"))
12945     (let* ((subject (elt (gnus-summary-article-header) 1))
12946            (grp     (car gnus-article-current))
12947            (art     (cdr gnus-article-current))
12948            (head    (gnus-summary-article-header art))
12949            (id      (mail-header-id head)))
12950       `(,subject
12951         ,@(condition-case nil
12952               (bookmark-make-record-default 'no-file 'no-context pos)
12953             (wrong-number-of-arguments
12954              (bookmark-make-record-default 'point-only)))
12955         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12956         (group . ,grp) (article . ,art)
12957         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12958
12959 ;;;###autoload
12960 (defun gnus-summary-bookmark-jump (bookmark)
12961   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12962 BOOKMARK is a bookmark name or a bookmark record."
12963   (let ((group    (bookmark-prop-get bookmark 'group))
12964         (article  (bookmark-prop-get bookmark 'article))
12965         (id       (bookmark-prop-get bookmark 'message-id))
12966         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12967     (gnus-fetch-group group (list article))
12968     (gnus-summary-insert-cached-articles)
12969     (gnus-summary-goto-article id nil 'force)
12970     ;; FIXME we have to wait article buffer is ready (only large buffer)
12971     ;; Is there a better solution to know that?
12972     ;; If we don't wait `bookmark-default-handler' will have no chance
12973     ;; to set position. However there is no error, just wrong pos.
12974     (sit-for 1)
12975     (when (string= buf "Gnus-art")
12976       (other-window 1))
12977     (bookmark-default-handler
12978      `(""
12979        (buffer . ,(current-buffer))
12980        . ,(bookmark-get-bookmark-record bookmark)))))
12981
12982 (gnus-summary-make-all-marking-commands)
12983
12984 (gnus-ems-redefine)
12985
12986 (provide 'gnus-sum)
12987
12988 (run-hooks 'gnus-sum-load-hook)
12989
12990 ;; Local Variables:
12991 ;; coding: iso-8859-1
12992 ;; End:
12993
12994 ;;; gnus-sum.el ends here