0184459c6cb8b19ceed354308c6993edde8b59cf
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-refer-thread-use-nnir nil
122   "*Use nnir to search an entire server when referring threads. A
123 nil value will only search for thread-related articles in the
124 current group."
125   :version "24.1"
126   :group 'gnus-thread
127   :type 'boolean)
128
129 (defcustom gnus-summary-make-false-root 'adopt
130   "*nil means that Gnus won't gather loose threads.
131 If the root of a thread has expired or been read in a previous
132 session, the information necessary to build a complete thread has been
133 lost.  Instead of having many small sub-threads from this original thread
134 scattered all over the summary buffer, Gnus can gather them.
135
136 If non-nil, Gnus will try to gather all loose sub-threads from an
137 original thread into one large thread.
138
139 If this variable is non-nil, it should be one of `none', `adopt',
140 `dummy' or `empty'.
141
142 If this variable is `none', Gnus will not make a false root, but just
143 present the sub-threads after another.
144 If this variable is `dummy', Gnus will create a dummy root that will
145 have all the sub-threads as children.
146 If this variable is `adopt', Gnus will make one of the \"children\"
147 the parent and mark all the step-children as such.
148 If this variable is `empty', the \"children\" are printed with empty
149 subject fields.  (Or rather, they will be printed with a string
150 given by the `gnus-summary-same-subject' variable.)"
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  (const none)
154                  (const dummy)
155                  (const adopt)
156                  (const empty)))
157
158 (defcustom gnus-summary-make-false-root-always nil
159   "Always make a false dummy root."
160   :version "22.1"
161   :group 'gnus-thread
162   :type 'boolean)
163
164 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
165   "*A regexp to match subjects to be excluded from loose thread gathering.
166 As loose thread gathering is done on subjects only, that means that
167 there can be many false gatherings performed.  By rooting out certain
168 common subjects, gathering might become saner."
169   :group 'gnus-thread
170   :type 'regexp)
171
172 (defcustom gnus-summary-gather-subject-limit nil
173   "*Maximum length of subject comparisons when gathering loose threads.
174 Use nil to compare full subjects.  Setting this variable to a low
175 number will help gather threads that have been corrupted by
176 newsreaders chopping off subject lines, but it might also mean that
177 unrelated articles that have subject that happen to begin with the
178 same few characters will be incorrectly gathered.
179
180 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
181 comparing subjects."
182   :group 'gnus-thread
183   :type '(choice (const :tag "off" nil)
184                  (const fuzzy)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-simplify-subject-functions nil
188   "List of functions taking a string argument that simplify subjects.
189 The functions are applied recursively.
190
191 Useful functions to put in this list include:
192 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
193 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
194   :group 'gnus-thread
195   :type '(repeat function))
196
197 (defcustom gnus-simplify-ignored-prefixes nil
198   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  regexp))
202
203 (defcustom gnus-build-sparse-threads nil
204   "*If non-nil, fill in the gaps in threads.
205 If `some', only fill in the gaps that are needed to tie loose threads
206 together.  If `more', fill in all leaf nodes that Gnus can find.  If
207 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
208   :group 'gnus-thread
209   :type '(choice (const :tag "off" nil)
210                  (const some)
211                  (const more)
212                  (sexp :menu-tag "all" t)))
213
214 (defcustom gnus-summary-thread-gathering-function
215   'gnus-gather-threads-by-subject
216   "*Function used for gathering loose threads.
217 There are two pre-defined functions: `gnus-gather-threads-by-subject',
218 which only takes Subjects into consideration; and
219 `gnus-gather-threads-by-references', which compared the References
220 headers of the articles to find matches."
221   :group 'gnus-thread
222   :type '(radio (function-item gnus-gather-threads-by-subject)
223                 (function-item gnus-gather-threads-by-references)
224                 (function :tag "other")))
225
226 (defcustom gnus-summary-same-subject ""
227   "*String indicating that the current article has the same subject as the previous.
228 This variable will only be used if the value of
229 `gnus-summary-make-false-root' is `empty'."
230   :group 'gnus-summary-format
231   :type 'string)
232
233 (defcustom gnus-summary-goto-unread nil
234   "*If t, many commands will go to the next unread article.
235 This applies to marking commands as well as other commands that
236 \"naturally\" select the next article, like, for instance, `SPC' at
237 the end of an article.
238
239 If nil, the marking commands do NOT go to the next unread article
240 \(they go to the next article instead).  If `never', commands that
241 usually go to the next unread article, will go to the next article,
242 whether it is read or not."
243   :version "24.1"
244   :group 'gnus-summary-marks
245   :link '(custom-manual "(gnus)Setting Marks")
246   :type '(choice (const :tag "off" nil)
247                  (const never)
248                  (sexp :menu-tag "on" t)))
249
250 (defcustom gnus-summary-default-score 0
251   "*Default article score level.
252 All scores generated by the score files will be added to this score.
253 If this variable is nil, scoring will be disabled."
254   :group 'gnus-score-default
255   :type '(choice (const :tag "disable")
256                  integer))
257
258 (defcustom gnus-summary-default-high-score 0
259   "*Default threshold for a high scored article.
260 An article will be highlighted as high scored if its score is greater
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-default-low-score 0
267   "*Default threshold for a low scored article.
268 An article will be highlighted as low scored if its score is smaller
269 than this score."
270   :version "22.1"
271   :group 'gnus-score-default
272   :type 'integer)
273
274 (defcustom gnus-summary-zcore-fuzz 0
275   "*Fuzziness factor for the zcore in the summary buffer.
276 Articles with scores closer than this to `gnus-summary-default-score'
277 will not be marked."
278   :group 'gnus-summary-format
279   :type 'integer)
280
281 (defcustom gnus-simplify-subject-fuzzy-regexp nil
282   "*Strings to be removed when doing fuzzy matches.
283 This can either be a regular expression or list of regular expressions
284 that will be removed from subject strings if fuzzy subject
285 simplification is selected."
286   :group 'gnus-thread
287   :type '(repeat regexp))
288
289 (defcustom gnus-show-threads t
290   "*If non-nil, display threads in summary mode."
291   :group 'gnus-thread
292   :type 'boolean)
293
294 (defcustom gnus-thread-hide-subtree nil
295   "*If non-nil, hide all threads initially.
296 This can be a predicate specifier which says which threads to hide.
297 If threads are hidden, you have to run the command
298 `gnus-summary-show-thread' by hand or select an article."
299   :group 'gnus-thread
300   :type '(radio (sexp :format "Non-nil\n"
301                       :match (lambda (widget value)
302                                (not (or (consp value) (functionp value))))
303                       :value t)
304                 (const nil)
305                 (sexp :tag "Predicate specifier")))
306
307 (defcustom gnus-thread-hide-killed t
308   "*If non-nil, hide killed threads automatically."
309   :group 'gnus-thread
310   :type 'boolean)
311
312 (defcustom gnus-thread-ignore-subject t
313   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
314 If nil, articles that have different subjects from their parents will
315 start separate threads."
316   :group 'gnus-thread
317   :type 'boolean)
318
319 (defcustom gnus-thread-operation-ignore-subject t
320   "*If non-nil, subjects will be ignored when doing thread commands.
321 This affects commands like `gnus-summary-kill-thread' and
322 `gnus-summary-lower-thread'.
323
324 If this variable is nil, articles in the same thread with different
325 subjects will not be included in the operation in question.  If this
326 variable is `fuzzy', only articles that have subjects that are fuzzily
327 equal will be included."
328   :group 'gnus-thread
329   :type '(choice (const :tag "off" nil)
330                  (const fuzzy)
331                  (sexp :tag "on" t)))
332
333 (defcustom gnus-thread-indent-level 4
334   "*Number that says how much each sub-thread should be indented."
335   :group 'gnus-thread
336   :type 'integer)
337
338 (defcustom gnus-auto-extend-newsgroup t
339   "*If non-nil, extend newsgroup forward and backward when requested."
340   :group 'gnus-summary-choose
341   :type 'boolean)
342
343 (defcustom gnus-auto-select-first t
344   "If non-nil, select an article on group entry.
345 An article is selected automatically when entering a group
346 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
347 `gnus-summary-catchup-and-goto-next-group'.
348
349 Which article is selected is controlled by the variable
350 `gnus-auto-select-subject'.
351
352 If you want to prevent automatic selection of articles in some
353 newsgroups, set the variable to nil in `gnus-select-group-hook'."
354   ;; Commands include...
355   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
357   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
358   :group 'gnus-group-select
359   :type '(choice (const :tag "none" nil)
360                  (sexp :menu-tag "first" t)))
361
362 (defcustom gnus-auto-select-subject 'unseen-or-unread
363   "*Says what subject to place under point when entering a group.
364
365 This variable can either be the symbols `first' (place point on the
366 first subject), `unread' (place point on the subject line of the first
367 unread article), `best' (place point on the subject line of the
368 highest-scored article), `unseen' (place point on the subject line of
369 the first unseen article), `unseen-or-unread' (place point on the subject
370 line of the first unseen article or, if all articles have been seen, on the
371 subject line of the first unread article), or a function to be called to
372 place point on some subject line."
373   :version "24.1"
374   :group 'gnus-group-select
375   :type '(choice (const best)
376                  (const unread)
377                  (const first)
378                  (const unseen)
379                  (const unseen-or-unread)
380                  (function :tag "Function to call")))
381
382 (defcustom gnus-auto-select-next t
383   "*If non-nil, offer to go to the next group from the end of the previous.
384 If the value is t and the next newsgroup is empty, Gnus will exit
385 summary mode and go back to group mode.  If the value is neither nil
386 nor t, Gnus will select the following unread newsgroup.  In
387 particular, if the value is the symbol `quietly', the next unread
388 newsgroup will be selected without any confirmation, and if it is
389 `almost-quietly', the next group will be selected without any
390 confirmation if you are located on the last article in the group.
391 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
392 will go to the next group without confirmation."
393   :group 'gnus-summary-maneuvering
394   :type '(choice (const :tag "off" nil)
395                  (const quietly)
396                  (const almost-quietly)
397                  (const slightly-quietly)
398                  (sexp :menu-tag "on" t)))
399
400 (defcustom gnus-auto-select-same nil
401   "*If non-nil, select the next article with the same subject.
402 If there are no more articles with the same subject, go to
403 the first unread article."
404   :group 'gnus-summary-maneuvering
405   :type 'boolean)
406
407 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
408   "What article should be selected after exiting an ephemeral group.
409 Valid values include:
410
411 `next'
412   Select the next article.
413 `next-unread'
414   Select the next unread article.
415 `next-noselect'
416   Move the cursor to the next article.  This is the default.
417 `next-unread-noselect'
418   Move the cursor to the next unread article.
419
420 If it has any other value or there is no next (unread) article, the
421 article selected before entering to the ephemeral group will appear."
422   :version "23.1" ;; No Gnus
423   :group 'gnus-summary-maneuvering
424   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
425                  (const next) (const next-unread)
426                  (const next-noselect) (const next-unread-noselect)
427                  (sexp :tag "other" :value nil)))
428
429 (defcustom gnus-auto-goto-ignores 'unfetched
430   "*Says how to handle unfetched articles when maneuvering.
431
432 This variable can either be the symbols nil (maneuver to any
433 article), `undownloaded' (maneuvering while unplugged ignores articles
434 that have not been fetched), `always-undownloaded' (maneuvering always
435 ignores articles that have not been fetched), `unfetched' (maneuvering
436 ignores articles whose headers have not been fetched).
437
438 NOTE: The list of unfetched articles will always be nil when plugged
439 and, when unplugged, a subset of the undownloaded article list."
440   :version "22.1"
441   :group 'gnus-summary-maneuvering
442   :type '(choice (const :tag "None" nil)
443                  (const :tag "Undownloaded when unplugged" undownloaded)
444                  (const :tag "Undownloaded" always-undownloaded)
445                  (const :tag "Unfetched" unfetched)))
446
447 (defcustom gnus-summary-check-current nil
448   "*If non-nil, consider the current article when moving.
449 The \"unread\" movement commands will stay on the same line if the
450 current article is unread."
451   :group 'gnus-summary-maneuvering
452   :type 'boolean)
453
454 (defcustom gnus-auto-center-summary 
455   (max (or (bound-and-true-p scroll-margin) 0) 2)
456   "*If non-nil, always center the current summary buffer.
457 In particular, if `vertical' do only vertical recentering.  If non-nil
458 and non-`vertical', do both horizontal and vertical recentering."
459   :group 'gnus-summary-maneuvering
460   :type '(choice (const :tag "none" nil)
461                  (const vertical)
462                  (integer :tag "height")
463                  (sexp :menu-tag "both" t)))
464
465 (defcustom gnus-auto-center-group t
466   "If non-nil, always center the group buffer."
467   :group 'gnus-summary-maneuvering
468   :type 'boolean)
469
470 (defcustom gnus-show-all-headers nil
471   "*If non-nil, don't hide any headers."
472   :group 'gnus-article-hiding
473   :group 'gnus-article-headers
474   :type 'boolean)
475
476 (defcustom gnus-summary-ignore-duplicates nil
477   "*If non-nil, ignore articles with identical Message-ID headers."
478   :group 'gnus-summary
479   :type 'boolean)
480
481 (defcustom gnus-single-article-buffer nil
482   "*If non-nil, display all articles in the same buffer.
483 If nil, each group will get its own article buffer."
484   :version "24.1"
485   :group 'gnus-article-various
486   :type 'boolean)
487
488 (defcustom gnus-widen-article-window nil
489   "If non-nil, selecting the article buffer will display only the article buffer."
490   :version "24.1"
491   :group 'gnus-article-various
492   :type 'boolean)
493
494 (defcustom gnus-break-pages t
495   "*If non-nil, do page breaking on articles.
496 The page delimiter is specified by the `gnus-page-delimiter'
497 variable."
498   :group 'gnus-article-various
499   :type 'boolean)
500
501 (defcustom gnus-move-split-methods nil
502   "*Variable used to suggest where articles are to be moved to.
503 It uses the same syntax as the `gnus-split-methods' variable.
504 However, whereas `gnus-split-methods' specifies file names as targets,
505 this variable specifies group names."
506   :group 'gnus-summary-mail
507   :type '(repeat (choice (list :value (fun) function)
508                          (cons :value ("" "") regexp (repeat string))
509                          (sexp :value nil))))
510
511 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
512   "Function used to compute default prefix for article move/copy/etc prompts.
513 The function should take one argument, a group name, and return a
514 string with the suggested prefix."
515   :group 'gnus-summary-mail
516   :type 'function)
517
518 ;; FIXME: Although the custom type is `character' for the following variables,
519 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
520
521 (defcustom gnus-unread-mark ?           ;Whitespace
522   "*Mark used for unread articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-ticked-mark ?!
527   "*Mark used for ticked articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-dormant-mark ??
532   "*Mark used for dormant articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-del-mark ?r
537   "*Mark used for del'd articles."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-read-mark ?R
542   "*Mark used for read articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-expirable-mark ?E
547   "*Mark used for expirable articles."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-killed-mark ?K
552   "*Mark used for killed articles."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-spam-mark ?$
557   "*Mark used for spam articles."
558   :version "22.1"
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-kill-file-mark ?X
563   "*Mark used for articles killed by kill files."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-low-score-mark ?Y
568   "*Mark used for articles with a low score."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-catchup-mark ?C
573   "*Mark used for articles that are caught up."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-replied-mark ?A
578   "*Mark used for articles that have been replied to."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-forwarded-mark ?F
583   "*Mark used for articles that have been forwarded."
584   :version "22.1"
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-recent-mark ?N
589   "*Mark used for articles that are recent."
590   :version "22.1"
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-cached-mark ?*
595   "*Mark used for articles that are in the cache."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-saved-mark ?S
600   "*Mark used for articles that have been saved."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-unseen-mark ?.
605   "*Mark used for articles that haven't been seen."
606   :version "22.1"
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-no-mark ?               ;Whitespace
611   "*Mark used for articles that have no other secondary mark."
612   :version "22.1"
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-ancient-mark ?O
617   "*Mark used for ancient articles."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-sparse-mark ?Q
622   "*Mark used for sparsely reffed articles."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-canceled-mark ?G
627   "*Mark used for canceled articles."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-duplicate-mark ?M
632   "*Mark used for duplicate articles."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-undownloaded-mark ?-
637   "*Mark used for articles that weren't downloaded."
638   :version "22.1"
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-downloaded-mark ?+
643   "*Mark used for articles that were downloaded."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-downloadable-mark ?%
648   "*Mark used for articles that are to be downloaded."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-unsendable-mark ?=
653   "*Mark used for articles that won't be sent."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-score-over-mark ?+
658   "*Score mark used for articles with high scores."
659   :group 'gnus-summary-marks
660   :type 'character)
661
662 (defcustom gnus-score-below-mark ?-
663   "*Score mark used for articles with low scores."
664   :group 'gnus-summary-marks
665   :type 'character)
666
667 (defcustom gnus-empty-thread-mark ?     ;Whitespace
668   "*There is no thread under the article."
669   :group 'gnus-summary-marks
670   :type 'character)
671
672 (defcustom gnus-not-empty-thread-mark ?=
673   "*There is a thread under the article."
674   :group 'gnus-summary-marks
675   :type 'character)
676
677 (defcustom gnus-view-pseudo-asynchronously nil
678   "*If non-nil, Gnus will view pseudo-articles asynchronously."
679   :group 'gnus-extract-view
680   :type 'boolean)
681
682 (defcustom gnus-auto-expirable-marks
683   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
684         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
685         gnus-duplicate-mark)
686   "*The list of marks converted into expiration if a group is auto-expirable."
687   :version "24.1"
688   :group 'gnus-summary
689   :type '(repeat character))
690
691 (defcustom gnus-inhibit-user-auto-expire t
692   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
693   :version "21.1"
694   :group 'gnus-summary
695   :type 'boolean)
696
697 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
698   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
699 If nil, the expirable marks will be unchanged except that the marks
700 will be removed when copying or moving articles to a group that has
701 not turned auto-expire on.  If non-nil, articles that have been read
702 will be marked as expirable when being copied or moved to a group in
703 which auto-expire is turned on."
704   :version "23.2"
705   :type 'boolean
706   :group 'gnus-summary-marks)
707
708 (defcustom gnus-view-pseudos nil
709   "*If `automatic', pseudo-articles will be viewed automatically.
710 If `not-confirm', pseudos will be viewed automatically, and the user
711 will not be asked to confirm the command."
712   :group 'gnus-extract-view
713   :type '(choice (const :tag "off" nil)
714                  (const automatic)
715                  (const not-confirm)))
716
717 (defcustom gnus-view-pseudos-separately t
718   "*If non-nil, one pseudo-article will be created for each file to be viewed.
719 If nil, all files that use the same viewing command will be given as a
720 list of parameters to that command."
721   :group 'gnus-extract-view
722   :type 'boolean)
723
724 (defcustom gnus-insert-pseudo-articles t
725   "*If non-nil, insert pseudo-articles when decoding articles."
726   :group 'gnus-extract-view
727   :type 'boolean)
728
729 (defcustom gnus-summary-dummy-line-format
730   "   %(:                             :%) %S\n"
731   "*The format specification for the dummy roots in the summary buffer.
732 It works along the same lines as a normal formatting string,
733 with some simple extensions.
734
735 %S  The subject
736
737 General format specifiers can also be used.
738 See `(gnus)Formatting Variables'."
739   :link '(custom-manual "(gnus)Formatting Variables")
740   :group 'gnus-threading
741   :type 'string)
742
743 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
744   "*The format specification for the summary mode line.
745 It works along the same lines as a normal formatting string,
746 with some simple extensions:
747
748 %G  Group name
749 %p  Unprefixed group name
750 %A  Current article number
751 %z  Current article score
752 %V  Gnus version
753 %U  Number of unread articles in the group
754 %e  Number of unselected articles in the group
755 %Z  A string with unread/unselected article counts
756 %g  Shortish group name
757 %S  Subject of the current article
758 %u  User-defined spec
759 %s  Current score file name
760 %d  Number of dormant articles
761 %r  Number of articles that have been marked as read in this session
762 %E  Number of articles expunged by the score files"
763   :group 'gnus-summary-format
764   :type 'string)
765
766 (defcustom gnus-list-identifiers nil
767   "Regexp that matches list identifiers to be removed from subject.
768 This can also be a list of regexps."
769   :version "21.1"
770   :group 'gnus-summary-format
771   :group 'gnus-article-hiding
772   :type '(choice (const :tag "none" nil)
773                  (regexp :value ".*")
774                  (repeat :value (".*") regexp)))
775
776 (defcustom gnus-summary-mark-below 0
777   "*Mark all articles with a score below this variable as read.
778 This variable is local to each summary buffer and usually set by the
779 score file."
780   :group 'gnus-score-default
781   :type 'integer)
782
783 (defun gnus-widget-reversible-match (widget value)
784   "Ignoring WIDGET, convert VALUE to internal form.
785 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
786   ;; (debug value)
787   (or (symbolp value)
788       (and (listp value)
789            (eq (length value) 2)
790            (eq (nth 0 value) 'not)
791            (symbolp (nth 1 value)))))
792
793 (defun gnus-widget-reversible-to-internal (widget value)
794   "Ignoring WIDGET, convert VALUE to internal form.
795 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
796 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
797   ;; (debug value)
798   (if (atom value)
799       (list value nil)
800     (list (nth 1 value) t)))
801
802 (defun gnus-widget-reversible-to-external (widget value)
803   "Ignoring WIDGET, convert VALUE to external form.
804 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
805 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
806   ;; (debug value)
807   (if (nth 1 value)
808       (list 'not (nth 0 value))
809     (nth 0 value)))
810
811 (define-widget 'gnus-widget-reversible 'group
812   "A `group' that convert values."
813   :match 'gnus-widget-reversible-match
814   :value-to-internal 'gnus-widget-reversible-to-internal
815   :value-to-external 'gnus-widget-reversible-to-external)
816
817 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
818   "*List of functions used for sorting articles in the summary buffer.
819
820 Each function takes two articles and returns non-nil if the first
821 article should be sorted before the other.  If you use more than one
822 function, the primary sort function should be the last.  You should
823 probably always include `gnus-article-sort-by-number' in the list of
824 sorting functions -- preferably first.  Also note that sorting by date
825 is often much slower than sorting by number, and the sorting order is
826 very similar.  (Sorting by date means sorting by the time the message
827 was sent, sorting by number means sorting by arrival time.)
828
829 Each item can also be a list `(not F)' where F is a function;
830 this reverses the sort order.
831
832 Ready-made functions include `gnus-article-sort-by-number',
833 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
834 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
835 and `gnus-article-sort-by-score'.
836
837 When threading is turned on, the variable `gnus-thread-sort-functions'
838 controls how articles are sorted."
839   :group 'gnus-summary-sort
840   :type '(repeat (gnus-widget-reversible
841                   (choice (function-item gnus-article-sort-by-number)
842                           (function-item gnus-article-sort-by-author)
843                           (function-item gnus-article-sort-by-subject)
844                           (function-item gnus-article-sort-by-date)
845                           (function-item gnus-article-sort-by-score)
846                           (function-item gnus-article-sort-by-random)
847                           (function :tag "other"))
848                   (boolean :tag "Reverse order"))))
849
850
851 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
852   "*List of functions used for sorting threads in the summary buffer.
853 By default, threads are sorted by article number.
854
855 Each function takes two threads and returns non-nil if the first
856 thread should be sorted before the other.  If you use more than one
857 function, the primary sort function should be the last.  You should
858 probably always include `gnus-thread-sort-by-number' in the list of
859 sorting functions -- preferably first.  Also note that sorting by date
860 is often much slower than sorting by number, and the sorting order is
861 very similar.  (Sorting by date means sorting by the time the message
862 was sent, sorting by number means sorting by arrival time.)
863
864 Each list item can also be a list `(not F)' where F is a
865 function; this specifies reversed sort order.
866
867 Ready-made functions include `gnus-thread-sort-by-number',
868 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
869 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
870 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
871 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
872 and `gnus-thread-sort-by-total-score' (see
873 `gnus-thread-score-function').
874
875 When threading is turned off, the variable
876 `gnus-article-sort-functions' controls how articles are sorted."
877   :group 'gnus-summary-sort
878   :type '(repeat
879           (gnus-widget-reversible
880            (choice (function-item gnus-thread-sort-by-number)
881                    (function-item gnus-thread-sort-by-author)
882                    (function-item gnus-thread-sort-by-recipient)
883                    (function-item gnus-thread-sort-by-subject)
884                    (function-item gnus-thread-sort-by-date)
885                    (function-item gnus-thread-sort-by-score)
886                    (function-item gnus-thread-sort-by-most-recent-number)
887                    (function-item gnus-thread-sort-by-most-recent-date)
888                    (function-item gnus-thread-sort-by-random)
889                    (function-item gnus-thread-sort-by-total-score)
890                    (function :tag "other"))
891            (boolean :tag "Reverse order"))))
892
893 (defcustom gnus-thread-score-function '+
894   "*Function used for calculating the total score of a thread.
895
896 The function is called with the scores of the article and each
897 subthread and should then return the score of the thread.
898
899 Some functions you can use are `+', `max', or `min'."
900   :group 'gnus-summary-sort
901   :type 'function)
902
903 (defcustom gnus-summary-expunge-below nil
904   "All articles that have a score less than this variable will be expunged.
905 This variable is local to the summary buffers."
906   :group 'gnus-score-default
907   :type '(choice (const :tag "off" nil)
908                  integer))
909
910 (defcustom gnus-thread-expunge-below nil
911   "All threads that have a total score less than this variable will be expunged.
912 See `gnus-thread-score-function' for en explanation of what a
913 \"thread score\" is.
914
915 This variable is local to the summary buffers."
916   :group 'gnus-threading
917   :group 'gnus-score-default
918   :type '(choice (const :tag "off" nil)
919                  integer))
920
921 (defcustom gnus-summary-mode-hook nil
922   "*A hook for Gnus summary mode.
923 This hook is run before any variables are set in the summary buffer."
924   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
925   :group 'gnus-summary-various
926   :type 'hook)
927
928 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
929 (when (featurep 'xemacs)
930   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
931   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
932   (add-hook 'gnus-summary-mode-hook
933             'gnus-xmas-switch-horizontal-scrollbar-off))
934
935 (defcustom gnus-summary-menu-hook nil
936   "*Hook run after the creation of the summary mode menu."
937   :group 'gnus-summary-visual
938   :type 'hook)
939
940 (defcustom gnus-summary-exit-hook nil
941   "*A hook called on exit from the summary buffer.
942 It will be called with point in the group buffer."
943   :group 'gnus-summary-exit
944   :type 'hook)
945
946 (defcustom gnus-summary-prepare-hook nil
947   "*A hook called after the summary buffer has been generated.
948 If you want to modify the summary buffer, you can use this hook."
949   :group 'gnus-summary-various
950   :type 'hook)
951
952 (defcustom gnus-summary-prepared-hook nil
953   "*A hook called as the last thing after the summary buffer has been generated."
954   :group 'gnus-summary-various
955   :type 'hook)
956
957 (defcustom gnus-summary-generate-hook nil
958   "*A hook run just before generating the summary buffer.
959 This hook is commonly used to customize threading variables and the
960 like."
961   :group 'gnus-summary-various
962   :type 'hook)
963
964 (defcustom gnus-select-group-hook nil
965   "*A hook called when a newsgroup is selected.
966
967 If you'd like to simplify subjects like the
968 `gnus-summary-next-same-subject' command does, you can use the
969 following hook:
970
971  (add-hook gnus-select-group-hook
972            (lambda ()
973              (mapcar (lambda (header)
974                        (mail-header-set-subject
975                         header
976                         (gnus-simplify-subject
977                          (mail-header-subject header) 're-only)))
978                      gnus-newsgroup-headers)))"
979   :group 'gnus-group-select
980   :type 'hook)
981
982 (defcustom gnus-select-article-hook nil
983   "*A hook called when an article is selected."
984   :group 'gnus-summary-choose
985   :options '(gnus-agent-fetch-selected-article)
986   :type 'hook)
987
988 (defcustom gnus-visual-mark-article-hook
989   (list 'gnus-highlight-selected-summary)
990   "*Hook run after selecting an article in the summary buffer.
991 It is meant to be used for highlighting the article in some way.  It
992 is not run if `gnus-visual' is nil."
993   :group 'gnus-summary-visual
994   :type 'hook)
995
996 (defcustom gnus-parse-headers-hook nil
997   "*A hook called before parsing the headers."
998   :group 'gnus-various
999   :type 'hook)
1000
1001 (defcustom gnus-exit-group-hook nil
1002   "*A hook called when exiting summary mode.
1003 This hook is not called from the non-updating exit commands like `Q'."
1004   :group 'gnus-various
1005   :type 'hook)
1006
1007 (defcustom gnus-summary-update-hook nil
1008   "*A hook called when a summary line is changed.
1009 The hook will not be called if `gnus-visual' is nil.
1010
1011 The default function `gnus-summary-highlight-line' will
1012 highlight the line according to the `gnus-summary-highlight'
1013 variable."
1014   :group 'gnus-summary-visual
1015   :type 'hook)
1016
1017 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1018   "*A hook called when an article is selected for the first time.
1019 The hook is intended to mark an article as read (or unread)
1020 automatically when it is selected."
1021   :group 'gnus-summary-choose
1022   :type 'hook)
1023
1024 (defcustom gnus-group-no-more-groups-hook nil
1025   "*A hook run when returning to group mode having no more (unread) groups."
1026   :group 'gnus-group-select
1027   :type 'hook)
1028
1029 (defcustom gnus-ps-print-hook nil
1030   "*A hook run before ps-printing something from Gnus."
1031   :group 'gnus-summary
1032   :type 'hook)
1033
1034 (defcustom gnus-summary-article-move-hook nil
1035   "*A hook called after an article is moved, copied, respooled, or crossposted."
1036   :version "22.1"
1037   :group 'gnus-summary
1038   :type 'hook)
1039
1040 (defcustom gnus-summary-article-delete-hook nil
1041   "*A hook called after an article is deleted."
1042   :version "22.1"
1043   :group 'gnus-summary
1044   :type 'hook)
1045
1046 (defcustom gnus-summary-article-expire-hook nil
1047   "*A hook called after an article is expired."
1048   :version "22.1"
1049   :group 'gnus-summary
1050   :type 'hook)
1051
1052 (defcustom gnus-summary-display-arrow
1053   (and (fboundp 'display-graphic-p)
1054        (display-graphic-p))
1055   "*If non-nil, display an arrow highlighting the current article."
1056   :version "22.1"
1057   :group 'gnus-summary
1058   :type 'boolean)
1059
1060 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1061   "Face used for highlighting the current article in the summary buffer."
1062   :group 'gnus-summary-visual
1063   :type 'face)
1064
1065 (defvar gnus-tmp-downloaded nil)
1066
1067 (defcustom gnus-summary-highlight
1068   '(((eq mark gnus-canceled-mark)
1069      . gnus-summary-cancelled)
1070     ((and uncached (> score default-high))
1071      . gnus-summary-high-undownloaded)
1072     ((and uncached (< score default-low))
1073      . gnus-summary-low-undownloaded)
1074     (uncached
1075      . gnus-summary-normal-undownloaded)
1076     ((and (> score default-high)
1077           (or (eq mark gnus-dormant-mark)
1078               (eq mark gnus-ticked-mark)))
1079      . gnus-summary-high-ticked)
1080     ((and (< score default-low)
1081           (or (eq mark gnus-dormant-mark)
1082               (eq mark gnus-ticked-mark)))
1083      . gnus-summary-low-ticked)
1084     ((or (eq mark gnus-dormant-mark)
1085          (eq mark gnus-ticked-mark))
1086      . gnus-summary-normal-ticked)
1087     ((and (> score default-high) (eq mark gnus-ancient-mark))
1088      . gnus-summary-high-ancient)
1089     ((and (< score default-low) (eq mark gnus-ancient-mark))
1090      . gnus-summary-low-ancient)
1091     ((eq mark gnus-ancient-mark)
1092      . gnus-summary-normal-ancient)
1093     ((and (> score default-high) (eq mark gnus-unread-mark))
1094      . gnus-summary-high-unread)
1095     ((and (< score default-low) (eq mark gnus-unread-mark))
1096      . gnus-summary-low-unread)
1097     ((eq mark gnus-unread-mark)
1098      . gnus-summary-normal-unread)
1099     ((> score default-high)
1100      . gnus-summary-high-read)
1101     ((< score default-low)
1102      . gnus-summary-low-read)
1103     (t
1104      . gnus-summary-normal-read))
1105   "*Controls the highlighting of summary buffer lines.
1106
1107 A list of (FORM . FACE) pairs.  When deciding how a particular
1108 summary line should be displayed, each form is evaluated.  The content
1109 of the face field after the first true form is used.  You can change
1110 how those summary lines are displayed, by editing the face field.
1111
1112 You can use the following variables in the FORM field.
1113
1114 score:        The article's score.
1115 default:      The default article score.
1116 default-high: The default score for high scored articles.
1117 default-low:  The default score for low scored articles.
1118 below:        The score below which articles are automatically marked as read.
1119 mark:         The article's mark.
1120 uncached:     Non-nil if the article is uncached."
1121   :group 'gnus-summary-visual
1122   :type '(repeat (cons (sexp :tag "Form" nil)
1123                        face)))
1124 (put 'gnus-summary-highlight 'risky-local-variable t)
1125
1126 (defcustom gnus-alter-header-function nil
1127   "Function called to allow alteration of article header structures.
1128 The function is called with one parameter, the article header vector,
1129 which it may alter in any way."
1130   :type '(choice (const :tag "None" nil)
1131                  function)
1132   :group 'gnus-summary)
1133
1134 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1135   "Function used to decode a string with encoded words.")
1136
1137 (defvar gnus-decode-encoded-address-function
1138   'mail-decode-encoded-address-string
1139   "Function used to decode addresses with encoded words.")
1140
1141 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1142   "*Extra headers to parse."
1143   :version "24.1"                       ; added Cc Keywords Gcc
1144   :group 'gnus-summary
1145   :type '(repeat symbol))
1146
1147 (defcustom gnus-ignored-from-addresses
1148   (and user-mail-address
1149        (not (string= user-mail-address ""))
1150        (regexp-quote user-mail-address))
1151   "*From headers that may be suppressed in favor of To headers.
1152 This can be a regexp or a list of regexps."
1153   :version "21.1"
1154   :group 'gnus-summary
1155   :type '(choice regexp
1156                  (repeat :tag "Regexp List" regexp)))
1157
1158 (defsubst gnus-ignored-from-addresses ()
1159   (gmm-regexp-concat gnus-ignored-from-addresses))
1160
1161 (defcustom gnus-summary-to-prefix "-> "
1162   "*String prefixed to the To field in the summary line when
1163 using `gnus-ignored-from-addresses'."
1164   :version "22.1"
1165   :group 'gnus-summary
1166   :type 'string)
1167
1168 (defcustom gnus-summary-newsgroup-prefix "=> "
1169   "*String prefixed to the Newsgroup field in the summary
1170 line when using the option `gnus-ignored-from-addresses'."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type 'string)
1174
1175 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1176   "List of charsets that should be ignored.
1177 When these charsets are used in the \"charset\" parameter, the
1178 default charset will be used instead."
1179   :version "21.1"
1180   :type '(repeat symbol)
1181   :group 'gnus-charset)
1182
1183 (defcustom gnus-newsgroup-maximum-articles nil
1184   "The maximum number of articles a newsgroup.
1185 If this is a number, old articles in a newsgroup exceeding this number
1186 are silently ignored.  If it is nil, no article is ignored.  Note that
1187 setting this variable to a number might prevent you from reading very
1188 old articles."
1189   :group 'gnus-group-select
1190   :version "22.2"
1191   :type '(choice (const :tag "No limit" nil)
1192                  integer))
1193
1194 (gnus-define-group-parameter
1195  ignored-charsets
1196  :type list
1197  :function-document
1198  "Return the ignored charsets of GROUP."
1199  :variable gnus-group-ignored-charsets-alist
1200  :variable-default
1201  '(("alt\\.chinese\\.text" iso-8859-1))
1202  :variable-document
1203  "Alist of regexps (to match group names) and charsets that should be ignored.
1204 When these charsets are used in the \"charset\" parameter, the
1205 default charset will be used instead."
1206  :variable-group gnus-charset
1207  :variable-type '(repeat (cons (regexp :tag "Group")
1208                                (repeat symbol)))
1209  :parameter-type '(choice :tag "Ignored charsets"
1210                           :value nil
1211                           (repeat (symbol)))
1212  :parameter-document       "\
1213 List of charsets that should be ignored.
1214
1215 When these charsets are used in the \"charset\" parameter, the
1216 default charset will be used instead.")
1217
1218 (defcustom gnus-group-highlight-words-alist nil
1219   "Alist of group regexps and highlight regexps.
1220 This variable uses the same syntax as `gnus-emphasis-alist'."
1221   :version "21.1"
1222   :type '(repeat (cons (regexp :tag "Group")
1223                        (repeat (list (regexp :tag "Highlight regexp")
1224                                      (number :tag "Group for entire word" 0)
1225                                      (number :tag "Group for displayed part" 0)
1226                                      (symbol :tag "Face"
1227                                              gnus-emphasis-highlight-words)))))
1228   :group 'gnus-summary-visual)
1229
1230 (defcustom gnus-summary-show-article-charset-alist
1231   nil
1232   "Alist of number and charset.
1233 The article will be shown with the charset corresponding to the
1234 numbered argument.
1235 For example: ((1 . cn-gb-2312) (2 . big5))."
1236   :version "21.1"
1237   :type '(repeat (cons (number :tag "Argument" 1)
1238                        (symbol :tag "Charset")))
1239   :group 'gnus-charset)
1240
1241 (defcustom gnus-preserve-marks t
1242   "Whether marks are preserved when moving, copying and respooling messages."
1243   :version "21.1"
1244   :type 'boolean
1245   :group 'gnus-summary-marks)
1246
1247 (defcustom gnus-alter-articles-to-read-function nil
1248   "Function to be called to alter the list of articles to be selected."
1249   :type '(choice (const nil) function)
1250   :group 'gnus-summary)
1251
1252 (defcustom gnus-orphan-score nil
1253   "*All orphans get this score added.  Set in the score file."
1254   :group 'gnus-score-default
1255   :type '(choice (const nil)
1256                  integer))
1257
1258 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1259   "*A regexp to match MIME parts when saving multiple parts of a
1260 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1261 This regexp will be used by default when prompting the user for which
1262 type of files to save."
1263   :group 'gnus-summary
1264   :type 'regexp)
1265
1266 (defcustom gnus-read-all-available-headers nil
1267   "Whether Gnus should parse all headers made available to it.
1268 This is mostly relevant for slow back ends where the user may
1269 wish to widen the summary buffer to include all headers
1270 that were fetched."
1271   :version "22.1"
1272   :group 'gnus-summary
1273   :type '(choice boolean regexp))
1274
1275 (defcustom gnus-summary-pipe-output-default-command nil
1276   "Command (and optional arguments) used to pipe article to subprocess.
1277 This will be used as the default command if it is non-nil.  The value
1278 will be updated if you modify it when executing the command
1279 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1280   :version "23.1" ;; No Gnus
1281   :group 'gnus-summary
1282   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1283
1284 (defcustom gnus-summary-muttprint-program "muttprint"
1285   "Command (and optional arguments) used to run Muttprint.
1286 The value will be updated if you modify it when executing the command
1287 `gnus-summary-muttprint'."
1288   :version "22.1"
1289   :group 'gnus-summary
1290   :type 'string)
1291
1292 (defcustom gnus-article-loose-mime t
1293   "If non-nil, don't require MIME-Version header.
1294 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1295 supply the MIME-Version header or deliberately strip it from the mail.
1296 If non-nil (the default), Gnus will treat some articles as MIME
1297 even if the MIME-Version header is missing."
1298   :version "22.1"
1299   :type 'boolean
1300   :group 'gnus-article-mime)
1301
1302 (defcustom gnus-article-emulate-mime t
1303   "If non-nil, use MIME emulation for uuencode and the like.
1304 This means that Gnus will search message bodies for text that look
1305 like uuencoded bits, yEncoded bits, and so on, and present that using
1306 the normal Gnus MIME machinery."
1307   :version "22.1"
1308   :type 'boolean
1309   :group 'gnus-article-mime)
1310
1311 ;;; Internal variables
1312
1313 (defvar gnus-summary-display-cache nil)
1314 (defvar gnus-article-mime-handles nil)
1315 (defvar gnus-article-decoded-p nil)
1316 (defvar gnus-article-charset nil)
1317 (defvar gnus-article-ignored-charsets nil)
1318 (defvar gnus-scores-exclude-files nil)
1319 (defvar gnus-page-broken nil)
1320
1321 (defvar gnus-original-article nil)
1322 (defvar gnus-article-internal-prepare-hook nil)
1323 (defvar gnus-newsgroup-process-stack nil)
1324
1325 (defvar gnus-thread-indent-array nil)
1326 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1327 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1328   "Function called to sort the articles within a thread after it has been gathered together.")
1329
1330 (defvar gnus-summary-save-parts-type-history nil)
1331 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1332
1333 ;; Avoid highlighting in kill files.
1334 (defvar gnus-summary-inhibit-highlight nil)
1335 (defvar gnus-newsgroup-selected-overlay nil)
1336 (defvar gnus-inhibit-limiting nil)
1337 (defvar gnus-newsgroup-adaptive-score-file nil)
1338 (defvar gnus-current-score-file nil)
1339 (defvar gnus-current-move-group nil)
1340 (defvar gnus-current-copy-group nil)
1341 (defvar gnus-current-crosspost-group nil)
1342 (defvar gnus-newsgroup-display nil)
1343
1344 (defvar gnus-newsgroup-dependencies nil)
1345 (defvar gnus-newsgroup-adaptive nil)
1346 (defvar gnus-summary-display-article-function nil)
1347 (defvar gnus-summary-highlight-line-function nil
1348   "Function called after highlighting a summary line.")
1349
1350 (defvar gnus-summary-line-format-alist
1351   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1352     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1353     (?s gnus-tmp-subject-or-nil ?s)
1354     (?n gnus-tmp-name ?s)
1355     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1356         ?s)
1357     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1358             gnus-tmp-from) ?s)
1359     (?F gnus-tmp-from ?s)
1360     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1361     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1362     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1363     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1364     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1365     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1366     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1367     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1368     (?L gnus-tmp-lines ?s)
1369     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1370             0) ?d)
1371     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1372             "") ?s)
1373     (?g (or (gnus-group-short-name
1374              (nnir-article-group (mail-header-number gnus-tmp-header)))
1375             "") ?s)
1376     (?O gnus-tmp-downloaded ?c)
1377     (?I gnus-tmp-indentation ?s)
1378     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1379     (?R gnus-tmp-replied ?c)
1380     (?\[ gnus-tmp-opening-bracket ?c)
1381     (?\] gnus-tmp-closing-bracket ?c)
1382     (?\> (make-string gnus-tmp-level ? ) ?s)
1383     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1384     (?i gnus-tmp-score ?d)
1385     (?z gnus-tmp-score-char ?c)
1386     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1387     (?U gnus-tmp-unread ?c)
1388     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1389         ?s)
1390     (?t (gnus-summary-number-of-articles-in-thread
1391          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1392         ?d)
1393     (?e (gnus-summary-number-of-articles-in-thread
1394          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1395         ?c)
1396     (?u gnus-tmp-user-defined ?s)
1397     (?P (gnus-pick-line-number) ?d)
1398     (?B gnus-tmp-thread-tree-header-string ?s)
1399     (user-date (gnus-user-date
1400                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1401   "An alist of format specifications that can appear in summary lines.
1402 These are paired with what variables they correspond with, along with
1403 the type of the variable (string, integer, character, etc).")
1404
1405 (defvar gnus-summary-dummy-line-format-alist
1406   `((?S gnus-tmp-subject ?s)
1407     (?N gnus-tmp-number ?d)
1408     (?u gnus-tmp-user-defined ?s)))
1409
1410 (defvar gnus-summary-mode-line-format-alist
1411   `((?G gnus-tmp-group-name ?s)
1412     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1413     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1414     (?A gnus-tmp-article-number ?d)
1415     (?Z gnus-tmp-unread-and-unselected ?s)
1416     (?V gnus-version ?s)
1417     (?U gnus-tmp-unread-and-unticked ?d)
1418     (?S gnus-tmp-subject ?s)
1419     (?e gnus-tmp-unselected ?d)
1420     (?u gnus-tmp-user-defined ?s)
1421     (?d (length gnus-newsgroup-dormant) ?d)
1422     (?t (length gnus-newsgroup-marked) ?d)
1423     (?h (length gnus-newsgroup-spam-marked) ?d)
1424     (?r (length gnus-newsgroup-reads) ?d)
1425     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1426     (?E gnus-newsgroup-expunged-tally ?d)
1427     (?s (gnus-current-score-file-nondirectory) ?s)))
1428
1429 ;; This is here rather than in gnus-art for compilation reasons.
1430 (defvar gnus-article-mode-line-format-alist
1431   (nconc '((?w (gnus-article-wash-status) ?s)
1432            (?m (gnus-article-mime-part-status) ?s))
1433          gnus-summary-mode-line-format-alist))
1434
1435 (defvar gnus-last-search-regexp nil
1436   "Default regexp for article search command.")
1437
1438 (defvar gnus-last-shell-command nil
1439   "Default shell command on article.")
1440
1441 (defvar gnus-newsgroup-agentized nil
1442   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1443 (defvar gnus-newsgroup-begin nil)
1444 (defvar gnus-newsgroup-end nil)
1445 (defvar gnus-newsgroup-last-rmail nil)
1446 (defvar gnus-newsgroup-last-mail nil)
1447 (defvar gnus-newsgroup-last-folder nil)
1448 (defvar gnus-newsgroup-last-file nil)
1449 (defvar gnus-newsgroup-last-directory nil)
1450 (defvar gnus-newsgroup-auto-expire nil)
1451 (defvar gnus-newsgroup-active nil)
1452 (defvar gnus-newsgroup-highest nil)
1453
1454 (defvar gnus-newsgroup-data nil)
1455 (defvar gnus-newsgroup-data-reverse nil)
1456 (defvar gnus-newsgroup-limit nil)
1457 (defvar gnus-newsgroup-limits nil)
1458 (defvar gnus-summary-use-undownloaded-faces nil)
1459
1460 (defvar gnus-newsgroup-unreads nil
1461   "Sorted list of unread articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-unselected nil
1464   "Sorted list of unselected unread articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-reads nil
1467   "Alist of read articles and article marks in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-expunged-tally nil)
1470
1471 (defvar gnus-newsgroup-marked nil
1472   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1473
1474 (defvar gnus-newsgroup-spam-marked nil
1475   "List of ranges of articles that have been marked as spam.")
1476
1477 (defvar gnus-newsgroup-killed nil
1478   "List of ranges of articles that have been through the scoring process.")
1479
1480 (defvar gnus-newsgroup-cached nil
1481   "Sorted list of articles that come from the article cache.")
1482
1483 (defvar gnus-newsgroup-saved nil
1484   "List of articles that have been saved.")
1485
1486 (defvar gnus-newsgroup-kill-headers nil)
1487
1488 (defvar gnus-newsgroup-replied nil
1489   "List of articles that have been replied to in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-forwarded nil
1492   "List of articles that have been forwarded in the current newsgroup.")
1493
1494 (defvar gnus-newsgroup-expirable nil
1495   "Sorted list of articles in the current newsgroup that can be expired.")
1496
1497 (defvar gnus-newsgroup-processable nil
1498   "List of articles in the current newsgroup that can be processed.")
1499
1500 (defvar gnus-newsgroup-downloadable nil
1501   "Sorted list of articles in the current newsgroup that can be processed.")
1502
1503 (defvar gnus-newsgroup-unfetched nil
1504   "Sorted list of articles in the current newsgroup whose headers have
1505 not been fetched into the agent.
1506
1507 This list will always be a subset of gnus-newsgroup-undownloaded.")
1508
1509 (defvar gnus-newsgroup-undownloaded nil
1510   "List of articles in the current newsgroup that haven't been downloaded.")
1511
1512 (defvar gnus-newsgroup-unsendable nil
1513   "List of articles in the current newsgroup that won't be sent.")
1514
1515 (defvar gnus-newsgroup-bookmarks nil
1516   "List of articles in the current newsgroup that have bookmarks.")
1517
1518 (defvar gnus-newsgroup-dormant nil
1519   "Sorted list of dormant articles in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-unseen nil
1522   "List of unseen articles in the current newsgroup.")
1523
1524 (defvar gnus-newsgroup-seen nil
1525   "Range of seen articles in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-unexist nil
1528   "Range of unexistent articles in the current newsgroup.")
1529
1530 (defvar gnus-newsgroup-articles nil
1531   "List of articles in the current newsgroup.")
1532
1533 (defvar gnus-newsgroup-scored nil
1534   "List of scored articles in the current newsgroup.")
1535
1536 (defvar gnus-newsgroup-headers nil
1537   "List of article headers in the current newsgroup.")
1538
1539 (defvar gnus-newsgroup-threads nil)
1540
1541 (defvar gnus-newsgroup-prepared nil
1542   "Whether the current group has been prepared properly.")
1543
1544 (defvar gnus-newsgroup-ancient nil
1545   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1546
1547 (defvar gnus-newsgroup-sparse nil)
1548
1549 (defvar gnus-current-article nil)
1550 (defvar gnus-article-current nil)
1551 (defvar gnus-current-headers nil)
1552 (defvar gnus-have-all-headers nil)
1553 (defvar gnus-last-article nil)
1554 (defvar gnus-newsgroup-history nil)
1555 (defvar gnus-newsgroup-charset nil)
1556 (defvar gnus-newsgroup-ephemeral-charset nil)
1557 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1558
1559 (defvar gnus-article-before-search nil)
1560
1561 (defvar gnus-summary-local-variables
1562   '(gnus-newsgroup-name
1563
1564     ;; Marks lists
1565     gnus-newsgroup-unreads
1566     gnus-newsgroup-unselected
1567     gnus-newsgroup-marked
1568     gnus-newsgroup-spam-marked
1569     gnus-newsgroup-reads
1570     gnus-newsgroup-saved
1571     gnus-newsgroup-replied
1572     gnus-newsgroup-forwarded
1573     gnus-newsgroup-expirable
1574     gnus-newsgroup-killed
1575     gnus-newsgroup-unseen
1576     gnus-newsgroup-seen
1577     gnus-newsgroup-unexist
1578     gnus-newsgroup-cached
1579     gnus-newsgroup-downloadable
1580     gnus-newsgroup-undownloaded
1581     gnus-newsgroup-unsendable
1582
1583     gnus-newsgroup-begin gnus-newsgroup-end
1584     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1585     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1586     gnus-newsgroup-last-directory
1587     gnus-newsgroup-auto-expire
1588     gnus-newsgroup-processable
1589     gnus-newsgroup-unfetched
1590     gnus-newsgroup-articles
1591     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1592     gnus-newsgroup-headers gnus-newsgroup-threads
1593     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1594     gnus-current-article gnus-current-headers gnus-have-all-headers
1595     gnus-last-article gnus-article-internal-prepare-hook
1596     (gnus-summary-article-delete-hook . global)
1597     (gnus-summary-article-move-hook . global)
1598     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1599     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1600     gnus-thread-expunge-below
1601     gnus-score-alist gnus-current-score-file
1602     (gnus-summary-expunge-below . global)
1603     (gnus-summary-mark-below . global)
1604     (gnus-orphan-score . global)
1605     gnus-newsgroup-active gnus-scores-exclude-files
1606     gnus-newsgroup-highest
1607     gnus-newsgroup-history gnus-newsgroup-ancient
1608     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1609     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1610     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1611     (gnus-newsgroup-expunged-tally . 0)
1612     gnus-cache-removable-articles
1613     gnus-newsgroup-data gnus-newsgroup-data-reverse
1614     gnus-newsgroup-limit gnus-newsgroup-limits
1615     gnus-newsgroup-charset gnus-newsgroup-display
1616     gnus-summary-use-undownloaded-faces)
1617   "Variables that are buffer-local to the summary buffers.")
1618
1619 (defvar gnus-newsgroup-variables nil
1620   "A list of variables that have separate values in different newsgroups.
1621 A list of newsgroup (summary buffer) local variables, or cons of
1622 variables and their default expressions to be evalled (when the default
1623 values are not nil), that should be made global while the summary buffer
1624 is active.
1625
1626 Note: The default expressions will be evaluated (using function `eval')
1627 before assignment to the local variable rather than just assigned to it.
1628 If the default expression is the symbol `global', that symbol will not
1629 be evaluated but the global value of the local variable will be used
1630 instead.
1631
1632 These variables can be used to set variables in the group parameters
1633 while still allowing them to affect operations done in other buffers.
1634 For example:
1635
1636 \(setq gnus-newsgroup-variables
1637      '(message-use-followup-to
1638        (gnus-visible-headers .
1639          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1640 ")
1641
1642 (eval-when-compile
1643   ;; Bind features so that require will believe that gnus-sum has
1644   ;; already been loaded (avoids infinite recursion)
1645   (let ((features (cons 'gnus-sum features)))
1646     (require 'gnus-art)))
1647
1648 ;; MIME stuff.
1649
1650 (defvar gnus-decode-encoded-word-methods
1651   '(mail-decode-encoded-word-string)
1652   "List of methods used to decode encoded words.
1653
1654 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1655 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1656 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1657 whose names match REGEXP.
1658
1659 For example:
1660 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1661  mail-decode-encoded-word-string
1662  (\"chinese\" . rfc1843-decode-string))")
1663
1664 (defvar gnus-decode-encoded-word-methods-cache nil)
1665
1666 (defun gnus-multi-decode-encoded-word-string (string)
1667   "Apply the functions from `gnus-encoded-word-methods' that match."
1668   (unless (and gnus-decode-encoded-word-methods-cache
1669                (eq gnus-newsgroup-name
1670                    (car gnus-decode-encoded-word-methods-cache)))
1671     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1672     (dolist (method gnus-decode-encoded-word-methods)
1673       (if (symbolp method)
1674           (nconc gnus-decode-encoded-word-methods-cache (list method))
1675         (if (and gnus-newsgroup-name
1676                  (string-match (car method) gnus-newsgroup-name))
1677             (nconc gnus-decode-encoded-word-methods-cache
1678                    (list (cdr method)))))))
1679   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1680     (setq string (funcall method string))))
1681
1682 ;; Subject simplification.
1683
1684 (defun gnus-simplify-whitespace (str)
1685   "Remove excessive whitespace from STR."
1686   ;; Multiple spaces.
1687   (while (string-match "[ \t][ \t]+" str)
1688     (setq str (concat (substring str 0 (match-beginning 0))
1689                         " "
1690                         (substring str (match-end 0)))))
1691   ;; Leading spaces.
1692   (when (string-match "^[ \t]+" str)
1693     (setq str (substring str (match-end 0))))
1694   ;; Trailing spaces.
1695   (when (string-match "[ \t]+$" str)
1696     (setq str (substring str 0 (match-beginning 0))))
1697   str)
1698
1699 (defun gnus-simplify-all-whitespace (str)
1700   "Remove all whitespace from STR."
1701   (while (string-match "[ \t\n]+" str)
1702     (setq str (replace-match "" nil nil str)))
1703   str)
1704
1705 (defsubst gnus-simplify-subject-re (subject)
1706   "Remove \"Re:\" from subject lines."
1707   (if (string-match message-subject-re-regexp subject)
1708       (substring subject (match-end 0))
1709     subject))
1710
1711 (defun gnus-simplify-subject (subject &optional re-only)
1712   "Remove `Re:' and words in parentheses.
1713 If RE-ONLY is non-nil, strip leading `Re:'s only."
1714   (let ((case-fold-search t))           ;Ignore case.
1715     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1716     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1717       (setq subject (substring subject (match-end 0))))
1718     ;; Remove uninteresting prefixes.
1719     (when (and (not re-only)
1720                gnus-simplify-ignored-prefixes
1721                (string-match gnus-simplify-ignored-prefixes subject))
1722       (setq subject (substring subject (match-end 0))))
1723     ;; Remove words in parentheses from end.
1724     (unless re-only
1725       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1726         (setq subject (substring subject 0 (match-beginning 0)))))
1727     ;; Return subject string.
1728     subject))
1729
1730 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1731 ;; all whitespace.
1732 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1733   (goto-char (point-min))
1734   (while (re-search-forward regexp nil t)
1735     (replace-match (or newtext ""))))
1736
1737 (defun gnus-simplify-buffer-fuzzy (regexp)
1738   "Simplify string in the buffer fuzzily.
1739 The string in the accessible portion of the current buffer is simplified.
1740 It is assumed to be a single-line subject.
1741 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1742 matter is removed.  Additional things can be deleted by setting
1743 `gnus-simplify-subject-fuzzy-regexp'."
1744   (let ((case-fold-search t)
1745         (modified-tick))
1746     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1747
1748     (while (not (eq modified-tick (buffer-modified-tick)))
1749       (setq modified-tick (buffer-modified-tick))
1750       (cond
1751        ((listp regexp)
1752         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1753        (regexp
1754         (gnus-simplify-buffer-fuzzy-step regexp)))
1755       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1756       (gnus-simplify-buffer-fuzzy-step
1757        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1758       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1759
1760     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1761     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1762     (gnus-simplify-buffer-fuzzy-step " $")
1763     (gnus-simplify-buffer-fuzzy-step "^ +")))
1764
1765 (defun gnus-simplify-subject-fuzzy (subject)
1766   "Simplify a subject string fuzzily.
1767 See `gnus-simplify-buffer-fuzzy' for details."
1768   (save-excursion
1769     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1770       (gnus-set-work-buffer)
1771       (let ((case-fold-search t))
1772         ;; Remove uninteresting prefixes.
1773         (when (and gnus-simplify-ignored-prefixes
1774                    (string-match gnus-simplify-ignored-prefixes subject))
1775           (setq subject (substring subject (match-end 0))))
1776         (insert subject)
1777         (inline (gnus-simplify-buffer-fuzzy regexp))
1778         (buffer-string)))))
1779
1780 (defsubst gnus-simplify-subject-fully (subject)
1781   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1782   (cond
1783    (gnus-simplify-subject-functions
1784     (gnus-map-function gnus-simplify-subject-functions subject))
1785    ((null gnus-summary-gather-subject-limit)
1786     (gnus-simplify-subject-re subject))
1787    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1788     (gnus-simplify-subject-fuzzy subject))
1789    ((numberp gnus-summary-gather-subject-limit)
1790     (truncate-string-to-width (gnus-simplify-subject-re subject)
1791                               gnus-summary-gather-subject-limit))
1792    (t
1793     subject)))
1794
1795 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1796   "Check whether two subjects are equal.
1797 If optional argument SIMPLE-FIRST is t, first argument is already
1798 simplified."
1799   (cond
1800    ((null simple-first)
1801     (equal (gnus-simplify-subject-fully s1)
1802            (gnus-simplify-subject-fully s2)))
1803    (t
1804     (equal s1
1805            (gnus-simplify-subject-fully s2)))))
1806
1807 (defun gnus-summary-bubble-group ()
1808   "Increase the score of the current group.
1809 This is a handy function to add to `gnus-summary-exit-hook' to
1810 increase the score of each group you read."
1811   (gnus-group-add-score gnus-newsgroup-name))
1812
1813 \f
1814 ;;;
1815 ;;; Gnus summary mode
1816 ;;;
1817
1818 (put 'gnus-summary-mode 'mode-class 'special)
1819
1820 (defvar gnus-article-commands-menu)
1821
1822 ;; Non-orthogonal keys
1823
1824 (gnus-define-keys gnus-summary-mode-map
1825   " " gnus-summary-next-page
1826   "\177" gnus-summary-prev-page
1827   [delete] gnus-summary-prev-page
1828   [backspace] gnus-summary-prev-page
1829   "\r" gnus-summary-scroll-up
1830   "\M-\r" gnus-summary-scroll-down
1831   "n" gnus-summary-next-unread-article
1832   "p" gnus-summary-prev-unread-article
1833   "N" gnus-summary-next-article
1834   "P" gnus-summary-prev-article
1835   "\M-\C-n" gnus-summary-next-same-subject
1836   "\M-\C-p" gnus-summary-prev-same-subject
1837   "\M-n" gnus-summary-next-unread-subject
1838   "\M-p" gnus-summary-prev-unread-subject
1839   "." gnus-summary-first-unread-article
1840   "," gnus-summary-best-unread-article
1841   "\M-s" gnus-summary-search-article-forward
1842   "\M-r" gnus-summary-search-article-backward
1843   "\M-S" gnus-summary-repeat-search-article-forward
1844   "\M-R" gnus-summary-repeat-search-article-backward
1845   "<" gnus-summary-beginning-of-article
1846   ">" gnus-summary-end-of-article
1847   "j" gnus-summary-goto-article
1848   "^" gnus-summary-refer-parent-article
1849   "\M-^" gnus-summary-refer-article
1850   "u" gnus-summary-tick-article-forward
1851   "!" gnus-summary-tick-article-forward
1852   "U" gnus-summary-tick-article-backward
1853   "d" gnus-summary-mark-as-read-forward
1854   "D" gnus-summary-mark-as-read-backward
1855   "E" gnus-summary-mark-as-expirable
1856   "\M-u" gnus-summary-clear-mark-forward
1857   "\M-U" gnus-summary-clear-mark-backward
1858   "k" gnus-summary-kill-same-subject-and-select
1859   "\C-k" gnus-summary-kill-same-subject
1860   "\M-\C-k" gnus-summary-kill-thread
1861   "\M-\C-l" gnus-summary-lower-thread
1862   "e" gnus-summary-edit-article
1863   "#" gnus-summary-mark-as-processable
1864   "\M-#" gnus-summary-unmark-as-processable
1865   "\M-\C-t" gnus-summary-toggle-threads
1866   "\M-\C-s" gnus-summary-show-thread
1867   "\M-\C-h" gnus-summary-hide-thread
1868   "\M-\C-f" gnus-summary-next-thread
1869   "\M-\C-b" gnus-summary-prev-thread
1870   [(meta down)] gnus-summary-next-thread
1871   [(meta up)] gnus-summary-prev-thread
1872   "\M-\C-u" gnus-summary-up-thread
1873   "\M-\C-d" gnus-summary-down-thread
1874   "&" gnus-summary-execute-command
1875   "c" gnus-summary-catchup-and-exit
1876   "\C-w" gnus-summary-mark-region-as-read
1877   "\C-t" gnus-summary-toggle-truncation
1878   "?" gnus-summary-mark-as-dormant
1879   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1880   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1881   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1882   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1883   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1884   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1885   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1886   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1887   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1888   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1889   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1890   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1891   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1892   "=" gnus-summary-expand-window
1893   "\C-x\C-s" gnus-summary-reselect-current-group
1894   "\M-g" gnus-summary-rescan-group
1895   "\C-c\C-r" gnus-summary-caesar-message
1896   "f" gnus-summary-followup
1897   "F" gnus-summary-followup-with-original
1898   "C" gnus-summary-cancel-article
1899   "r" gnus-summary-reply
1900   "R" gnus-summary-reply-with-original
1901   "\C-c\C-f" gnus-summary-mail-forward
1902   "o" gnus-summary-save-article
1903   "\C-o" gnus-summary-save-article-mail
1904   "|" gnus-summary-pipe-output
1905   "\M-k" gnus-summary-edit-local-kill
1906   "\M-K" gnus-summary-edit-global-kill
1907   ;; "V" gnus-version
1908   "\C-c\C-d" gnus-summary-describe-group
1909   "q" gnus-summary-exit
1910   "Q" gnus-summary-exit-no-update
1911   "\C-c\C-i" gnus-info-find-node
1912   gnus-mouse-2 gnus-mouse-pick-article
1913   [follow-link] mouse-face
1914   "m" gnus-summary-mail-other-window
1915   "a" gnus-summary-post-news
1916   "x" gnus-summary-limit-to-unread
1917   "s" gnus-summary-isearch-article
1918   "\t" gnus-summary-widget-forward
1919   [backtab] gnus-summary-widget-backward
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-summary-show-article
1922   "l" gnus-summary-goto-last-article
1923   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1924   "\C-d" gnus-summary-enter-digest-group
1925   "\M-\C-d" gnus-summary-read-document
1926   "\M-\C-e" gnus-summary-edit-parameters
1927   "\M-\C-a" gnus-summary-customize-parameters
1928   "\C-c\C-b" gnus-bug
1929   "*" gnus-cache-enter-article
1930   "\M-*" gnus-cache-remove-article
1931   "\M-&" gnus-summary-universal-argument
1932   "\C-l" gnus-recenter
1933   "I" gnus-summary-increase-score
1934   "L" gnus-summary-lower-score
1935   "\M-i" gnus-symbolic-argument
1936   "h" gnus-summary-select-article-buffer
1937
1938   "b" gnus-article-view-part
1939   "\M-t" gnus-summary-toggle-display-buttonized
1940
1941   "V" gnus-summary-score-map
1942   "X" gnus-uu-extract-map
1943   "S" gnus-summary-send-map)
1944
1945 ;; Sort of orthogonal keymap
1946 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1947   "t" gnus-summary-tick-article-forward
1948   "!" gnus-summary-tick-article-forward
1949   "d" gnus-summary-mark-as-read-forward
1950   "r" gnus-summary-mark-as-read-forward
1951   "c" gnus-summary-clear-mark-forward
1952   " " gnus-summary-clear-mark-forward
1953   "e" gnus-summary-mark-as-expirable
1954   "x" gnus-summary-mark-as-expirable
1955   "?" gnus-summary-mark-as-dormant
1956   "b" gnus-summary-set-bookmark
1957   "B" gnus-summary-remove-bookmark
1958   "#" gnus-summary-mark-as-processable
1959   "\M-#" gnus-summary-unmark-as-processable
1960   "S" gnus-summary-limit-include-expunged
1961   "C" gnus-summary-catchup
1962   "H" gnus-summary-catchup-to-here
1963   "h" gnus-summary-catchup-from-here
1964   "\C-c" gnus-summary-catchup-all
1965   "k" gnus-summary-kill-same-subject-and-select
1966   "K" gnus-summary-kill-same-subject
1967   "P" gnus-uu-mark-map)
1968
1969 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1970   "c" gnus-summary-clear-above
1971   "u" gnus-summary-tick-above
1972   "m" gnus-summary-mark-above
1973   "k" gnus-summary-kill-below)
1974
1975 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1976   "/" gnus-summary-limit-to-subject
1977   "n" gnus-summary-limit-to-articles
1978   "b" gnus-summary-limit-to-bodies
1979   "h" gnus-summary-limit-to-headers
1980   "w" gnus-summary-pop-limit
1981   "s" gnus-summary-limit-to-subject
1982   "a" gnus-summary-limit-to-author
1983   "u" gnus-summary-limit-to-unread
1984   "m" gnus-summary-limit-to-marks
1985   "M" gnus-summary-limit-exclude-marks
1986   "v" gnus-summary-limit-to-score
1987   "*" gnus-summary-limit-include-cached
1988   "D" gnus-summary-limit-include-dormant
1989   "T" gnus-summary-limit-include-thread
1990   "d" gnus-summary-limit-exclude-dormant
1991   "t" gnus-summary-limit-to-age
1992   "." gnus-summary-limit-to-unseen
1993   "x" gnus-summary-limit-to-extra
1994   "p" gnus-summary-limit-to-display-predicate
1995   "E" gnus-summary-limit-include-expunged
1996   "c" gnus-summary-limit-exclude-childless-dormant
1997   "C" gnus-summary-limit-mark-excluded-as-read
1998   "o" gnus-summary-insert-old-articles
1999   "N" gnus-summary-insert-new-articles
2000   "S" gnus-summary-limit-to-singletons
2001   "r" gnus-summary-limit-to-replied
2002   "R" gnus-summary-limit-to-recipient
2003   "A" gnus-summary-limit-to-address)
2004
2005 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2006   "n" gnus-summary-next-unread-article
2007   "p" gnus-summary-prev-unread-article
2008   "N" gnus-summary-next-article
2009   "P" gnus-summary-prev-article
2010   "\C-n" gnus-summary-next-same-subject
2011   "\C-p" gnus-summary-prev-same-subject
2012   "\M-n" gnus-summary-next-unread-subject
2013   "\M-p" gnus-summary-prev-unread-subject
2014   "f" gnus-summary-first-unread-article
2015   "b" gnus-summary-best-unread-article
2016   "j" gnus-summary-goto-article
2017   "g" gnus-summary-goto-subject
2018   "l" gnus-summary-goto-last-article
2019   "o" gnus-summary-pop-article)
2020
2021 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2022   "k" gnus-summary-kill-thread
2023   "E" gnus-summary-expire-thread
2024   "l" gnus-summary-lower-thread
2025   "i" gnus-summary-raise-thread
2026   "T" gnus-summary-toggle-threads
2027   "t" gnus-summary-rethread-current
2028   "^" gnus-summary-reparent-thread
2029   "\M-^" gnus-summary-reparent-children
2030   "s" gnus-summary-show-thread
2031   "S" gnus-summary-show-all-threads
2032   "h" gnus-summary-hide-thread
2033   "H" gnus-summary-hide-all-threads
2034   "n" gnus-summary-next-thread
2035   "p" gnus-summary-prev-thread
2036   "u" gnus-summary-up-thread
2037   "o" gnus-summary-top-thread
2038   "d" gnus-summary-down-thread
2039   "#" gnus-uu-mark-thread
2040   "\M-#" gnus-uu-unmark-thread)
2041
2042 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2043   "g" gnus-summary-prepare
2044   "c" gnus-summary-insert-cached-articles
2045   "d" gnus-summary-insert-dormant-articles
2046   "t" gnus-summary-insert-ticked-articles)
2047
2048 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2049   "c" gnus-summary-catchup-and-exit
2050   "C" gnus-summary-catchup-all-and-exit
2051   "E" gnus-summary-exit-no-update
2052   "Q" gnus-summary-exit
2053   "Z" gnus-summary-exit
2054   "n" gnus-summary-catchup-and-goto-next-group
2055   "p" gnus-summary-catchup-and-goto-prev-group
2056   "R" gnus-summary-reselect-current-group
2057   "G" gnus-summary-rescan-group
2058   "N" gnus-summary-next-group
2059   "s" gnus-summary-save-newsrc
2060   "P" gnus-summary-prev-group)
2061
2062 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2063   " " gnus-summary-next-page
2064   "n" gnus-summary-next-page
2065   "\177" gnus-summary-prev-page
2066   [delete] gnus-summary-prev-page
2067   "p" gnus-summary-prev-page
2068   "\r" gnus-summary-scroll-up
2069   "\M-\r" gnus-summary-scroll-down
2070   "<" gnus-summary-beginning-of-article
2071   ">" gnus-summary-end-of-article
2072   "b" gnus-summary-beginning-of-article
2073   "e" gnus-summary-end-of-article
2074   "^" gnus-summary-refer-parent-article
2075   "r" gnus-summary-refer-parent-article
2076   "C" gnus-summary-show-complete-article
2077   "D" gnus-summary-enter-digest-group
2078   "R" gnus-summary-refer-references
2079   "T" gnus-summary-refer-thread
2080   "W" gnus-warp-to-article
2081   "g" gnus-summary-show-article
2082   "s" gnus-summary-isearch-article
2083   "\t" gnus-summary-widget-forward
2084   [backtab] gnus-summary-widget-backward
2085   "P" gnus-summary-print-article
2086   "S" gnus-sticky-article
2087   "M" gnus-mailing-list-insinuate
2088   "t" gnus-article-babel)
2089
2090 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2091   "b" gnus-article-add-buttons
2092   "B" gnus-article-add-buttons-to-head
2093   "o" gnus-article-treat-overstrike
2094   "e" gnus-article-emphasize
2095   "w" gnus-article-fill-cited-article
2096   "Q" gnus-article-fill-long-lines
2097   "L" gnus-article-toggle-truncate-lines
2098   "C" gnus-article-capitalize-sentences
2099   "c" gnus-article-remove-cr
2100   "q" gnus-article-de-quoted-unreadable
2101   "6" gnus-article-de-base64-unreadable
2102   "Z" gnus-article-decode-HZ
2103   "A" gnus-article-treat-ansi-sequences
2104   "h" gnus-article-wash-html
2105   "u" gnus-article-unsplit-urls
2106   "s" gnus-summary-force-verify-and-decrypt
2107   "f" gnus-article-display-x-face
2108   "l" gnus-summary-stop-page-breaking
2109   "r" gnus-summary-caesar-message
2110   "m" gnus-summary-morse-message
2111   "t" gnus-summary-toggle-header
2112   "g" gnus-treat-smiley
2113   "v" gnus-summary-verbose-headers
2114   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2115   "p" gnus-article-verify-x-pgp-sig
2116   "d" gnus-article-treat-dumbquotes
2117   "U" gnus-article-treat-non-ascii
2118   "i" gnus-summary-idna-message)
2119
2120 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2121   ;; mnemonic: deuglif*Y*
2122   "u" gnus-article-outlook-unwrap-lines
2123   "a" gnus-article-outlook-repair-attribution
2124   "c" gnus-article-outlook-rearrange-citation
2125   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2126
2127 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2128   "a" gnus-article-hide
2129   "h" gnus-article-hide-headers
2130   "b" gnus-article-hide-boring-headers
2131   "s" gnus-article-hide-signature
2132   "c" gnus-article-hide-citation
2133   "C" gnus-article-hide-citation-in-followups
2134   "l" gnus-article-hide-list-identifiers
2135   "B" gnus-article-strip-banner
2136   "P" gnus-article-hide-pem
2137   "\C-c" gnus-article-hide-citation-maybe)
2138
2139 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2140   "a" gnus-article-highlight
2141   "h" gnus-article-highlight-headers
2142   "c" gnus-article-highlight-citation
2143   "s" gnus-article-highlight-signature)
2144
2145 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2146   "f" gnus-article-treat-fold-headers
2147   "u" gnus-article-treat-unfold-headers
2148   "n" gnus-article-treat-fold-newsgroups)
2149
2150 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2151   "x" gnus-article-display-x-face
2152   "d" gnus-article-display-face
2153   "s" gnus-treat-smiley
2154   "D" gnus-article-remove-images
2155   "W" gnus-article-show-images
2156   "f" gnus-treat-from-picon
2157   "m" gnus-treat-mail-picon
2158   "n" gnus-treat-newsgroups-picon
2159   "g" gnus-treat-from-gravatar
2160   "h" gnus-treat-mail-gravatar)
2161
2162 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2163   "w" gnus-article-decode-mime-words
2164   "c" gnus-article-decode-charset
2165   "v" gnus-mime-view-all-parts
2166   "b" gnus-article-view-part)
2167
2168 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2169   "z" gnus-article-date-ut
2170   "u" gnus-article-date-ut
2171   "l" gnus-article-date-local
2172   "p" gnus-article-date-english
2173   "e" gnus-article-date-lapsed
2174   "o" gnus-article-date-original
2175   "i" gnus-article-date-iso8601
2176   "s" gnus-article-date-user)
2177
2178 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2179   "t" gnus-article-remove-trailing-blank-lines
2180   "l" gnus-article-strip-leading-blank-lines
2181   "m" gnus-article-strip-multiple-blank-lines
2182   "a" gnus-article-strip-blank-lines
2183   "A" gnus-article-strip-all-blank-lines
2184   "s" gnus-article-strip-leading-space
2185   "e" gnus-article-strip-trailing-space
2186   "w" gnus-article-remove-leading-whitespace)
2187
2188 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2189   "v" gnus-version
2190   "d" gnus-summary-describe-group
2191   "h" gnus-summary-describe-briefly
2192   "i" gnus-info-find-node)
2193
2194 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2195   "e" gnus-summary-expire-articles
2196   "\M-\C-e" gnus-summary-expire-articles-now
2197   "\177" gnus-summary-delete-article
2198   [delete] gnus-summary-delete-article
2199   [backspace] gnus-summary-delete-article
2200   "m" gnus-summary-move-article
2201   "r" gnus-summary-respool-article
2202   "w" gnus-summary-edit-article
2203   "c" gnus-summary-copy-article
2204   "B" gnus-summary-crosspost-article
2205   "q" gnus-summary-respool-query
2206   "t" gnus-summary-respool-trace
2207   "i" gnus-summary-import-article
2208   "I" gnus-summary-create-article
2209   "p" gnus-summary-article-posted-p)
2210
2211 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2212   "o" gnus-summary-save-article
2213   "m" gnus-summary-save-article-mail
2214   "F" gnus-summary-write-article-file
2215   "r" gnus-summary-save-article-rmail
2216   "f" gnus-summary-save-article-file
2217   "b" gnus-summary-save-article-body-file
2218   "B" gnus-summary-write-article-body-file
2219   "h" gnus-summary-save-article-folder
2220   "v" gnus-summary-save-article-vm
2221   "p" gnus-summary-pipe-output
2222   "P" gnus-summary-muttprint)
2223
2224 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2225   "b" gnus-summary-display-buttonized
2226   "m" gnus-summary-repair-multipart
2227   "v" gnus-article-view-part
2228   "o" gnus-article-save-part
2229   "O" gnus-article-save-part-and-strip
2230   "r" gnus-article-replace-part
2231   "d" gnus-article-delete-part
2232   "t" gnus-article-view-part-as-type
2233   "j" gnus-article-jump-to-part
2234   "c" gnus-article-copy-part
2235   "C" gnus-article-view-part-as-charset
2236   "e" gnus-article-view-part-externally
2237   "H" gnus-article-browse-html-article
2238   "E" gnus-article-encrypt-body
2239   "i" gnus-article-inline-part
2240   "|" gnus-article-pipe-part)
2241
2242 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2243   "p" gnus-summary-mark-as-processable
2244   "u" gnus-summary-unmark-as-processable
2245   "U" gnus-summary-unmark-all-processable
2246   "v" gnus-uu-mark-over
2247   "s" gnus-uu-mark-series
2248   "r" gnus-uu-mark-region
2249   "g" gnus-uu-unmark-region
2250   "R" gnus-uu-mark-by-regexp
2251   "G" gnus-uu-unmark-by-regexp
2252   "t" gnus-uu-mark-thread
2253   "T" gnus-uu-unmark-thread
2254   "a" gnus-uu-mark-all
2255   "b" gnus-uu-mark-buffer
2256   "S" gnus-uu-mark-sparse
2257   "k" gnus-summary-kill-process-mark
2258   "y" gnus-summary-yank-process-mark
2259   "w" gnus-summary-save-process-mark
2260   "i" gnus-uu-invert-processable)
2261
2262 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2263   ;;"x" gnus-uu-extract-any
2264   "m" gnus-summary-save-parts
2265   "u" gnus-uu-decode-uu
2266   "U" gnus-uu-decode-uu-and-save
2267   "s" gnus-uu-decode-unshar
2268   "S" gnus-uu-decode-unshar-and-save
2269   "o" gnus-uu-decode-save
2270   "O" gnus-uu-decode-save
2271   "b" gnus-uu-decode-binhex
2272   "B" gnus-uu-decode-binhex
2273   "Y" gnus-uu-decode-yenc
2274   "p" gnus-uu-decode-postscript
2275   "P" gnus-uu-decode-postscript-and-save)
2276
2277 (gnus-define-keys
2278     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2279   "u" gnus-uu-decode-uu-view
2280   "U" gnus-uu-decode-uu-and-save-view
2281   "s" gnus-uu-decode-unshar-view
2282   "S" gnus-uu-decode-unshar-and-save-view
2283   "o" gnus-uu-decode-save-view
2284   "O" gnus-uu-decode-save-view
2285   "b" gnus-uu-decode-binhex-view
2286   "B" gnus-uu-decode-binhex-view
2287   "p" gnus-uu-decode-postscript-view
2288   "P" gnus-uu-decode-postscript-and-save-view)
2289
2290 (defvar gnus-article-post-menu nil)
2291
2292 (defconst gnus-summary-menu-maxlen 20)
2293
2294 (defun gnus-summary-menu-split (menu)
2295   ;; If we have lots of elements, divide them into groups of 20
2296   ;; and make a pane (or submenu) for each one.
2297   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2298       (let ((menu menu) sublists next
2299             (i 1))
2300         (while menu
2301           ;; Pull off the next gnus-summary-menu-maxlen elements
2302           ;; and make them the next element of sublist.
2303           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2304           (if next
2305               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2306                       nil))
2307           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2308                                              (aref (car (last menu)) 0)) menu)
2309                                sublists))
2310           (setq i (1+ i))
2311           (setq menu next))
2312         (nreverse sublists))
2313     ;; Few elements--put them all in one pane.
2314     menu))
2315
2316 (defun gnus-summary-make-menu-bar ()
2317   (gnus-turn-off-edit-menu 'summary)
2318
2319   (unless (boundp 'gnus-summary-misc-menu)
2320
2321     (easy-menu-define
2322       gnus-summary-kill-menu gnus-summary-mode-map ""
2323       (cons
2324        "Score"
2325        (nconc
2326         (list
2327          ["Customize" gnus-score-customize t])
2328         (gnus-make-score-map 'increase)
2329         (gnus-make-score-map 'lower)
2330         '(("Mark"
2331            ["Kill below" gnus-summary-kill-below t]
2332            ["Mark above" gnus-summary-mark-above t]
2333            ["Tick above" gnus-summary-tick-above t]
2334            ["Clear above" gnus-summary-clear-above t])
2335           ["Current score" gnus-summary-current-score t]
2336           ["Set score" gnus-summary-set-score t]
2337           ["Switch current score file..." gnus-score-change-score-file t]
2338           ["Set mark below..." gnus-score-set-mark-below t]
2339           ["Set expunge below..." gnus-score-set-expunge-below t]
2340           ["Edit current score file" gnus-score-edit-current-scores t]
2341           ["Edit score file..." gnus-score-edit-file t]
2342           ["Trace score" gnus-score-find-trace t]
2343           ["Find words" gnus-score-find-favourite-words t]
2344           ["Rescore buffer" gnus-summary-rescore t]
2345           ["Increase score..." gnus-summary-increase-score t]
2346           ["Lower score..." gnus-summary-lower-score t]))))
2347
2348     ;; Define both the Article menu in the summary buffer and the
2349     ;; equivalent Commands menu in the article buffer here for
2350     ;; consistency.
2351     (let ((innards
2352            `(("Hide"
2353               ["All" gnus-article-hide t]
2354               ["Headers" gnus-article-hide-headers t]
2355               ["Signature" gnus-article-hide-signature t]
2356               ["Citation" gnus-article-hide-citation t]
2357               ["List identifiers" gnus-article-hide-list-identifiers t]
2358               ["Banner" gnus-article-strip-banner t]
2359               ["Boring headers" gnus-article-hide-boring-headers t])
2360              ("Highlight"
2361               ["All" gnus-article-highlight t]
2362               ["Headers" gnus-article-highlight-headers t]
2363               ["Signature" gnus-article-highlight-signature t]
2364               ["Citation" gnus-article-highlight-citation t])
2365              ("MIME"
2366               ["Words" gnus-article-decode-mime-words t]
2367               ["Charset" gnus-article-decode-charset t]
2368               ["QP" gnus-article-de-quoted-unreadable t]
2369               ["Base64" gnus-article-de-base64-unreadable t]
2370               ["View MIME buttons" gnus-summary-display-buttonized t]
2371               ["View all" gnus-mime-view-all-parts t]
2372               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2373               ["Encrypt body" gnus-article-encrypt-body
2374                :active (not (gnus-group-read-only-p))
2375                ,@(if (featurep 'xemacs) nil
2376                    '(:help "Encrypt the message body on disk"))]
2377               ["Extract all parts..." gnus-summary-save-parts t]
2378               ("Multipart"
2379                ["Repair multipart" gnus-summary-repair-multipart t]
2380                ["Pipe part..." gnus-article-pipe-part t]
2381                ["Inline part" gnus-article-inline-part t]
2382                ["View part as type..." gnus-article-view-part-as-type t]
2383                ["Encrypt body" gnus-article-encrypt-body
2384                 :active (not (gnus-group-read-only-p))
2385                ,@(if (featurep 'xemacs) nil
2386                    '(:help "Encrypt the message body on disk"))]
2387                ["View part externally" gnus-article-view-part-externally t]
2388                ["View HTML parts in browser" gnus-article-browse-html-article t]
2389                ["View part with charset..." gnus-article-view-part-as-charset t]
2390                ["Copy part" gnus-article-copy-part t]
2391                ["Save part..." gnus-article-save-part t]
2392                ["View part" gnus-article-view-part t]))
2393              ("Date"
2394               ["Local" gnus-article-date-local t]
2395               ["ISO8601" gnus-article-date-iso8601 t]
2396               ["UT" gnus-article-date-ut t]
2397               ["Original" gnus-article-date-original t]
2398               ["Lapsed" gnus-article-date-lapsed t]
2399               ["User-defined" gnus-article-date-user t])
2400              ("Display"
2401               ["Remove images" gnus-article-remove-images t]
2402               ["Toggle smiley" gnus-treat-smiley t]
2403               ["Show X-Face" gnus-article-display-x-face t]
2404               ["Show picons in From" gnus-treat-from-picon t]
2405               ["Show picons in mail headers" gnus-treat-mail-picon t]
2406               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2407               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2408               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2409               ("View as different encoding"
2410                ,@(gnus-summary-menu-split
2411                   (mapcar
2412                    (lambda (cs)
2413                      ;; Since easymenu under Emacs doesn't allow
2414                      ;; lambda forms for menu commands, we should
2415                      ;; provide intern'ed function symbols.
2416                      (let ((command (intern (format "\
2417 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2418                        (fset command
2419                              `(lambda ()
2420                                 (interactive)
2421                                 (let ((gnus-summary-show-article-charset-alist
2422                                        '((1 . ,cs))))
2423                                   (gnus-summary-show-article 1))))
2424                        `[,(symbol-name cs) ,command t]))
2425                    (sort (if (fboundp 'coding-system-list)
2426                              (coding-system-list)
2427                            (mapcar 'car mm-mime-mule-charset-alist))
2428                          'string<)))))
2429              ("Washing"
2430               ("Remove Blanks"
2431                ["Leading" gnus-article-strip-leading-blank-lines t]
2432                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2433                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2434                ["All of the above" gnus-article-strip-blank-lines t]
2435                ["All" gnus-article-strip-all-blank-lines t]
2436                ["Leading space" gnus-article-strip-leading-space t]
2437                ["Trailing space" gnus-article-strip-trailing-space t]
2438                ["Leading space in headers"
2439                 gnus-article-remove-leading-whitespace t])
2440               ["Overstrike" gnus-article-treat-overstrike t]
2441               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2442               ["Non-ASCII" gnus-article-treat-non-ascii t]
2443               ["Emphasis" gnus-article-emphasize t]
2444               ["Word wrap" gnus-article-fill-cited-article t]
2445               ["Fill long lines" gnus-article-fill-long-lines t]
2446               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2447               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2448               ["Remove CR" gnus-article-remove-cr t]
2449               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2450               ["Base64" gnus-article-de-base64-unreadable t]
2451               ["Rot 13" gnus-summary-caesar-message
2452                ,@(if (featurep 'xemacs) '(t)
2453                    '(:help "\"Caesar rotate\" article by 13"))]
2454               ["De-IDNA" gnus-summary-idna-message t]
2455               ["Morse decode" gnus-summary-morse-message t]
2456               ["Unix pipe..." gnus-summary-pipe-message t]
2457               ["Add buttons" gnus-article-add-buttons t]
2458               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2459               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2460               ["Verbose header" gnus-summary-verbose-headers t]
2461               ["Toggle header" gnus-summary-toggle-header t]
2462               ["Unfold headers" gnus-article-treat-unfold-headers t]
2463               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2464               ["Html" gnus-article-wash-html t]
2465               ["Unsplit URLs" gnus-article-unsplit-urls t]
2466               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2467               ["Decode HZ" gnus-article-decode-HZ t]
2468               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2469               ("(Outlook) Deuglify"
2470                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2471                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2472                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2473                ["Full (Outlook) deuglify"
2474                 gnus-article-outlook-deuglify-article t])
2475               )
2476              ("Output"
2477               ["Save in default format..." gnus-summary-save-article
2478                ,@(if (featurep 'xemacs) '(t)
2479                    '(:help "Save article using default method"))]
2480               ["Save in file..." gnus-summary-save-article-file
2481                ,@(if (featurep 'xemacs) '(t)
2482                    '(:help "Save article in file"))]
2483               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2484               ["Save in MH folder..." gnus-summary-save-article-folder t]
2485               ["Save in VM folder..." gnus-summary-save-article-vm t]
2486               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2487               ["Save body in file..." gnus-summary-save-article-body-file t]
2488               ["Pipe through a filter..." gnus-summary-pipe-output t]
2489               ["Print with Muttprint..." gnus-summary-muttprint t]
2490               ["Print" gnus-summary-print-article
2491                ,@(if (featurep 'xemacs) '(t)
2492                    '(:help "Generate and print a PostScript image"))])
2493              ("Copy, move,... (Backend)"
2494               ,@(if (featurep 'xemacs) nil
2495                   '(:help "Copying, moving, expiring articles..."))
2496               ["Respool article..." gnus-summary-respool-article t]
2497               ["Move article..." gnus-summary-move-article
2498                (gnus-check-backend-function
2499                 'request-move-article gnus-newsgroup-name)]
2500               ["Copy article..." gnus-summary-copy-article t]
2501               ["Crosspost article..." gnus-summary-crosspost-article
2502                (gnus-check-backend-function
2503                 'request-replace-article gnus-newsgroup-name)]
2504               ["Import file..." gnus-summary-import-article
2505                (gnus-check-backend-function
2506                 'request-accept-article gnus-newsgroup-name)]
2507               ["Create article..." gnus-summary-create-article
2508                (gnus-check-backend-function
2509                 'request-accept-article gnus-newsgroup-name)]
2510               ["Check if posted" gnus-summary-article-posted-p t]
2511               ["Edit article" gnus-summary-edit-article
2512                (not (gnus-group-read-only-p))]
2513               ["Delete article" gnus-summary-delete-article
2514                (gnus-check-backend-function
2515                 'request-expire-articles gnus-newsgroup-name)]
2516               ["Query respool" gnus-summary-respool-query t]
2517               ["Trace respool" gnus-summary-respool-trace t]
2518               ["Delete expirable articles" gnus-summary-expire-articles-now
2519                (gnus-check-backend-function
2520                 'request-expire-articles gnus-newsgroup-name)])
2521              ("Extract"
2522               ["Uudecode" gnus-uu-decode-uu
2523                ,@(if (featurep 'xemacs) '(t)
2524                    '(:help "Decode uuencoded article(s)"))]
2525               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2526               ["Unshar" gnus-uu-decode-unshar t]
2527               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2528               ["Save" gnus-uu-decode-save t]
2529               ["Binhex" gnus-uu-decode-binhex t]
2530               ["PostScript" gnus-uu-decode-postscript t]
2531               ["All MIME parts" gnus-summary-save-parts t])
2532              ("Cache"
2533               ["Enter article" gnus-cache-enter-article t]
2534               ["Remove article" gnus-cache-remove-article t])
2535              ["Translate" gnus-article-babel t]
2536              ["Select article buffer" gnus-summary-select-article-buffer t]
2537              ["Make article buffer sticky" gnus-sticky-article t]
2538              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2539              ["Isearch article..." gnus-summary-isearch-article t]
2540              ["Beginning of the article" gnus-summary-beginning-of-article t]
2541              ["End of the article" gnus-summary-end-of-article t]
2542              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2543              ["Fetch referenced articles" gnus-summary-refer-references t]
2544              ["Fetch current thread" gnus-summary-refer-thread t]
2545              ["Fetch article with id..." gnus-summary-refer-article t]
2546              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2547              ["Redisplay" gnus-summary-show-article t]
2548              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2549       (easy-menu-define
2550         gnus-summary-article-menu gnus-summary-mode-map ""
2551         (cons "Article" innards))
2552
2553       (if (not (keymapp gnus-summary-article-menu))
2554           (easy-menu-define
2555             gnus-article-commands-menu gnus-article-mode-map ""
2556             (cons "Commands" innards))
2557         ;; in Emacs, don't share menu.
2558         (setq gnus-article-commands-menu
2559               (copy-keymap gnus-summary-article-menu))
2560         (define-key gnus-article-mode-map [menu-bar commands]
2561           (cons "Commands" gnus-article-commands-menu))))
2562
2563     (easy-menu-define
2564       gnus-summary-thread-menu gnus-summary-mode-map ""
2565       '("Threads"
2566         ["Find all messages in thread" gnus-summary-refer-thread t]
2567         ["Toggle threading" gnus-summary-toggle-threads t]
2568         ["Hide threads" gnus-summary-hide-all-threads t]
2569         ["Show threads" gnus-summary-show-all-threads t]
2570         ["Hide thread" gnus-summary-hide-thread t]
2571         ["Show thread" gnus-summary-show-thread t]
2572         ["Go to next thread" gnus-summary-next-thread t]
2573         ["Go to previous thread" gnus-summary-prev-thread t]
2574         ["Go down thread" gnus-summary-down-thread t]
2575         ["Go up thread" gnus-summary-up-thread t]
2576         ["Top of thread" gnus-summary-top-thread t]
2577         ["Mark thread as read" gnus-summary-kill-thread t]
2578         ["Mark thread as expired" gnus-summary-expire-thread t]
2579         ["Lower thread score" gnus-summary-lower-thread t]
2580         ["Raise thread score" gnus-summary-raise-thread t]
2581         ["Rethread current" gnus-summary-rethread-current t]))
2582
2583     (easy-menu-define
2584       gnus-summary-post-menu gnus-summary-mode-map ""
2585       `("Post"
2586         ["Send a message (mail or news)" gnus-summary-post-news
2587          ,@(if (featurep 'xemacs) '(t)
2588              '(:help "Compose a new message (mail or news)"))]
2589         ["Followup" gnus-summary-followup
2590          ,@(if (featurep 'xemacs) '(t)
2591              '(:help "Post followup to this article"))]
2592         ["Followup and yank" gnus-summary-followup-with-original
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Post followup to this article, quoting its contents"))]
2595         ["Supersede article" gnus-summary-supersede-article t]
2596         ["Cancel article" gnus-summary-cancel-article
2597          ,@(if (featurep 'xemacs) '(t)
2598              '(:help "Cancel an article you posted"))]
2599         ["Reply" gnus-summary-reply t]
2600         ["Reply and yank" gnus-summary-reply-with-original t]
2601         ["Wide reply" gnus-summary-wide-reply t]
2602         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2603          ,@(if (featurep 'xemacs) '(t)
2604              '(:help "Mail a reply, quoting this article"))]
2605         ["Very wide reply" gnus-summary-very-wide-reply t]
2606         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2607          ,@(if (featurep 'xemacs) '(t)
2608              '(:help "Mail a very wide reply, quoting this article"))]
2609         ["Mail forward" gnus-summary-mail-forward t]
2610         ["Post forward" gnus-summary-post-forward t]
2611         ["Digest and mail" gnus-uu-digest-mail-forward t]
2612         ["Digest and post" gnus-uu-digest-post-forward t]
2613         ["Resend message" gnus-summary-resend-message t]
2614         ["Resend message edit" gnus-summary-resend-message-edit t]
2615         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2616         ["Send a mail" gnus-summary-mail-other-window t]
2617         ["Create a local message" gnus-summary-news-other-window t]
2618         ["Uuencode and post" gnus-uu-post-news
2619          ,@(if (featurep 'xemacs) '(t)
2620              '(:help "Post a uuencoded article"))]
2621         ["Followup via news" gnus-summary-followup-to-mail t]
2622         ["Followup via news and yank"
2623          gnus-summary-followup-to-mail-with-original t]
2624         ["Strip signature on reply"
2625          (lambda ()
2626            (interactive)
2627            (if (not (memq message-cite-function
2628                           '(message-cite-original-without-signature
2629                             message-cite-original)))
2630                ;; Stupid workaround for XEmacs not honoring :visible.
2631                (message "Can't toggle this value of `message-cite-function'")
2632              (setq message-cite-function
2633                    (if (eq message-cite-function
2634                            'message-cite-original-without-signature)
2635                        'message-cite-original
2636                      'message-cite-original-without-signature))))
2637          ;; XEmacs barfs on :visible.
2638          ,@(if (featurep 'xemacs) nil
2639              '(:visible (memq message-cite-function
2640                               '(message-cite-original-without-signature
2641                                 message-cite-original))))
2642          :style toggle
2643          :selected (eq message-cite-function
2644                        'message-cite-original-without-signature)
2645          ,@(if (featurep 'xemacs) nil
2646              '(:help "Strip signature from cited article when replying."))]
2647         ;;("Draft"
2648         ;;["Send" gnus-summary-send-draft t]
2649         ;;["Send bounced" gnus-resend-bounced-mail t])
2650         ))
2651
2652     (cond
2653      ((not (keymapp gnus-summary-post-menu))
2654       (setq gnus-article-post-menu gnus-summary-post-menu))
2655      ((not gnus-article-post-menu)
2656       ;; Don't share post menu.
2657       (setq gnus-article-post-menu
2658             (copy-keymap gnus-summary-post-menu))))
2659     (define-key gnus-article-mode-map [menu-bar post]
2660       (cons "Post" gnus-article-post-menu))
2661
2662     (easy-menu-define
2663       gnus-summary-misc-menu gnus-summary-mode-map ""
2664       `("Gnus"
2665         ("Mark Read"
2666          ["Mark as read" gnus-summary-mark-as-read-forward t]
2667          ["Mark same subject and select"
2668           gnus-summary-kill-same-subject-and-select t]
2669          ["Mark same subject" gnus-summary-kill-same-subject t]
2670          ["Catchup" gnus-summary-catchup
2671           ,@(if (featurep 'xemacs) '(t)
2672               '(:help "Mark unread articles in this group as read"))]
2673          ["Catchup all" gnus-summary-catchup-all t]
2674          ["Catchup to here" gnus-summary-catchup-to-here t]
2675          ["Catchup from here" gnus-summary-catchup-from-here t]
2676          ["Catchup region" gnus-summary-mark-region-as-read
2677           (gnus-mark-active-p)]
2678          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2679         ("Mark Various"
2680          ["Tick" gnus-summary-tick-article-forward t]
2681          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2682          ["Remove marks" gnus-summary-clear-mark-forward t]
2683          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2684          ["Set bookmark" gnus-summary-set-bookmark t]
2685          ["Remove bookmark" gnus-summary-remove-bookmark t])
2686         ("Limit to"
2687          ["Marks..." gnus-summary-limit-to-marks t]
2688          ["Subject..." gnus-summary-limit-to-subject t]
2689          ["Author..." gnus-summary-limit-to-author t]
2690          ["Recipient..." gnus-summary-limit-to-recipient t]
2691          ["Address..." gnus-summary-limit-to-address t]
2692          ["Age..." gnus-summary-limit-to-age t]
2693          ["Extra..." gnus-summary-limit-to-extra t]
2694          ["Score..." gnus-summary-limit-to-score t]
2695          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2696          ["Unread" gnus-summary-limit-to-unread t]
2697          ["Unseen" gnus-summary-limit-to-unseen t]
2698          ["Singletons" gnus-summary-limit-to-singletons t]
2699          ["Replied" gnus-summary-limit-to-replied t]
2700          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2701          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2702          ["Pop limit" gnus-summary-pop-limit t]
2703          ["Show dormant" gnus-summary-limit-include-dormant t]
2704          ["Hide childless dormant"
2705           gnus-summary-limit-exclude-childless-dormant t]
2706          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2707          ["Hide marked" gnus-summary-limit-exclude-marks t]
2708          ["Show expunged" gnus-summary-limit-include-expunged t])
2709         ("Process Mark"
2710          ["Set mark" gnus-summary-mark-as-processable t]
2711          ["Remove mark" gnus-summary-unmark-as-processable t]
2712          ["Remove all marks" gnus-summary-unmark-all-processable t]
2713          ["Invert marks" gnus-uu-invert-processable t]
2714          ["Mark above" gnus-uu-mark-over t]
2715          ["Mark series" gnus-uu-mark-series t]
2716          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2717          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2718          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2719          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2720          ["Mark all" gnus-uu-mark-all t]
2721          ["Mark buffer" gnus-uu-mark-buffer t]
2722          ["Mark sparse" gnus-uu-mark-sparse t]
2723          ["Mark thread" gnus-uu-mark-thread t]
2724          ["Unmark thread" gnus-uu-unmark-thread t]
2725          ("Process Mark Sets"
2726           ["Kill" gnus-summary-kill-process-mark t]
2727           ["Yank" gnus-summary-yank-process-mark
2728            gnus-newsgroup-process-stack]
2729           ["Save" gnus-summary-save-process-mark t]
2730           ["Run command on marked..." gnus-summary-universal-argument t]))
2731         ("Registry Marks")
2732         ("Scroll article"
2733          ["Page forward" gnus-summary-next-page
2734           ,@(if (featurep 'xemacs) '(t)
2735               '(:help "Show next page of article"))]
2736          ["Page backward" gnus-summary-prev-page
2737           ,@(if (featurep 'xemacs) '(t)
2738               '(:help "Show previous page of article"))]
2739          ["Line forward" gnus-summary-scroll-up t])
2740         ("Move"
2741          ["Next unread article" gnus-summary-next-unread-article t]
2742          ["Previous unread article" gnus-summary-prev-unread-article t]
2743          ["Next article" gnus-summary-next-article t]
2744          ["Previous article" gnus-summary-prev-article t]
2745          ["Next unread subject" gnus-summary-next-unread-subject t]
2746          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2747          ["Next article same subject" gnus-summary-next-same-subject t]
2748          ["Previous article same subject" gnus-summary-prev-same-subject t]
2749          ["First unread article" gnus-summary-first-unread-article t]
2750          ["Best unread article" gnus-summary-best-unread-article t]
2751          ["Go to subject number..." gnus-summary-goto-subject t]
2752          ["Go to article number..." gnus-summary-goto-article t]
2753          ["Go to the last article" gnus-summary-goto-last-article t]
2754          ["Pop article off history" gnus-summary-pop-article t])
2755         ("Sort"
2756          ["Sort by number" gnus-summary-sort-by-number t]
2757          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2758          ["Sort by author" gnus-summary-sort-by-author t]
2759          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2760          ["Sort by subject" gnus-summary-sort-by-subject t]
2761          ["Sort by date" gnus-summary-sort-by-date t]
2762          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2763          ["Sort by score" gnus-summary-sort-by-score t]
2764          ["Sort by lines" gnus-summary-sort-by-lines t]
2765          ["Sort by characters" gnus-summary-sort-by-chars t]
2766          ["Randomize" gnus-summary-sort-by-random t]
2767          ["Original sort" gnus-summary-sort-by-original t])
2768         ("Help"
2769          ["Describe group" gnus-summary-describe-group t]
2770          ["Read manual" gnus-info-find-node t])
2771         ("Modes"
2772          ["Pick and read" gnus-pick-mode t]
2773          ["Binary" gnus-binary-mode t])
2774         ("Regeneration"
2775          ["Regenerate" gnus-summary-prepare t]
2776          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2777          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2778          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2779          ["Toggle threading" gnus-summary-toggle-threads t])
2780         ["See old articles" gnus-summary-insert-old-articles t]
2781         ["See new articles" gnus-summary-insert-new-articles t]
2782         ["Filter articles..." gnus-summary-execute-command t]
2783         ["Run command on articles..." gnus-summary-universal-argument t]
2784         ["Search articles forward..." gnus-summary-search-article-forward t]
2785         ["Search articles backward..." gnus-summary-search-article-backward t]
2786         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2787         ["Expand window" gnus-summary-expand-window t]
2788         ["Expire expirable articles" gnus-summary-expire-articles
2789          (gnus-check-backend-function
2790           'request-expire-articles gnus-newsgroup-name)]
2791         ["Edit local kill file" gnus-summary-edit-local-kill t]
2792         ["Edit main kill file" gnus-summary-edit-global-kill t]
2793         ["Edit group parameters" gnus-summary-edit-parameters t]
2794         ["Customize group parameters" gnus-summary-customize-parameters t]
2795         ["Send a bug report" gnus-bug t]
2796         ("Exit"
2797          ["Catchup and exit" gnus-summary-catchup-and-exit
2798           ,@(if (featurep 'xemacs) '(t)
2799               '(:help "Mark unread articles in this group as read, then exit"))]
2800          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2801          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2802          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2803          ["Exit group" gnus-summary-exit
2804           ,@(if (featurep 'xemacs) '(t)
2805               '(:help "Exit current group, return to group selection mode"))]
2806          ["Exit group without updating" gnus-summary-exit-no-update t]
2807          ["Exit and goto next group" gnus-summary-next-group t]
2808          ["Exit and goto prev group" gnus-summary-prev-group t]
2809          ["Reselect group" gnus-summary-reselect-current-group t]
2810          ["Rescan group" gnus-summary-rescan-group t]
2811          ["Update dribble" gnus-summary-save-newsrc t])))
2812
2813     (gnus-run-hooks 'gnus-summary-menu-hook)))
2814
2815 (defvar gnus-summary-tool-bar-map nil)
2816
2817 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2818 ;; affect _new_ message buffers.  We might add a function that walks thru all
2819 ;; summary-mode buffers and force the update.
2820 (defun gnus-summary-tool-bar-update (&optional symbol value)
2821   "Update summary mode toolbar.
2822 Setter function for custom variables."
2823   (setq-default gnus-summary-tool-bar-map nil)
2824   (when symbol
2825     ;; When used as ":set" function:
2826     (set-default symbol value))
2827   (when (gnus-buffer-live-p gnus-summary-buffer)
2828     (with-current-buffer gnus-summary-buffer
2829       (gnus-summary-make-tool-bar))))
2830
2831 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2832                                      'gnus-summary-tool-bar-gnome
2833                                    'gnus-summary-tool-bar-retro)
2834   "Specifies the Gnus summary tool bar.
2835
2836 It can be either a list or a symbol referring to a list.  See
2837 `gmm-tool-bar-from-list' for the format of the list.  The
2838 default key map is `gnus-summary-mode-map'.
2839
2840 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2841 `gnus-summary-tool-bar-retro'."
2842   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2843                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2844                  (repeat :tag "User defined list" gmm-tool-bar-item)
2845                  (symbol))
2846   :version "23.1" ;; No Gnus
2847   :initialize 'custom-initialize-default
2848   :set 'gnus-summary-tool-bar-update
2849   :group 'gnus-summary)
2850
2851 (defcustom gnus-summary-tool-bar-gnome
2852   '((gnus-summary-post-news "mail/compose" nil)
2853     (gnus-summary-insert-new-articles "mail/inbox" nil
2854                                       :visible (or (not gnus-agent)
2855                                                    gnus-plugged))
2856     (gnus-summary-reply-with-original "mail/reply")
2857     (gnus-summary-reply "mail/reply" nil :visible nil)
2858     (gnus-summary-followup-with-original "mail/reply-all")
2859     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2860     (gnus-summary-mail-forward "mail/forward")
2861     (gnus-summary-save-article "mail/save")
2862     (gnus-summary-search-article-forward "search" nil :visible nil)
2863     (gnus-summary-print-article "print")
2864     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2865     ;; Some new commands that may need more suitable icons:
2866     (gnus-summary-save-newsrc "save" nil :visible nil)
2867     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2868     (gnus-summary-prev-article "left-arrow")
2869     (gnus-summary-next-article "right-arrow")
2870     (gnus-summary-next-page "next-page")
2871     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2872     ;;
2873     ;; Maybe some sort-by-... could be added:
2874     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2875     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2876     (gnus-summary-mark-as-expirable
2877      "delete" nil
2878      :visible (gnus-check-backend-function 'request-expire-articles
2879                                            gnus-newsgroup-name))
2880     (gnus-summary-mark-as-spam
2881      "mail/spam" t
2882      :visible (and (fboundp 'spam-group-ham-contents-p)
2883                    (spam-group-ham-contents-p gnus-newsgroup-name))
2884      :help "Mark as spam")
2885     (gnus-summary-mark-as-read-forward
2886      "mail/not-spam" nil
2887      :visible (and (fboundp 'spam-group-spam-contents-p)
2888                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2889     ;;
2890     (gnus-summary-exit "exit")
2891     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2892     (gnus-info-find-node "help"))
2893   "List of functions for the summary tool bar (GNOME style).
2894
2895 See `gmm-tool-bar-from-list' for the format of the list."
2896   :type '(repeat gmm-tool-bar-item)
2897   :version "23.1" ;; No Gnus
2898   :initialize 'custom-initialize-default
2899   :set 'gnus-summary-tool-bar-update
2900   :group 'gnus-summary)
2901
2902 (defcustom gnus-summary-tool-bar-retro
2903   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2904     (gnus-summary-next-unread-article "gnus/next-ur")
2905     (gnus-summary-post-news "gnus/post")
2906     (gnus-summary-followup-with-original "gnus/fuwo")
2907     (gnus-summary-followup "gnus/followup")
2908     (gnus-summary-reply-with-original "gnus/reply-wo")
2909     (gnus-summary-reply "gnus/reply")
2910     (gnus-summary-caesar-message "gnus/rot13")
2911     (gnus-uu-decode-uu "gnus/uu-decode")
2912     (gnus-summary-save-article-file "gnus/save-aif")
2913     (gnus-summary-save-article "gnus/save-art")
2914     (gnus-uu-post-news "gnus/uu-post")
2915     (gnus-summary-catchup "gnus/catchup")
2916     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2917     (gnus-summary-exit "gnus/exit-summ")
2918     ;; Some new command that may need more suitable icons:
2919     (gnus-summary-print-article "gnus/print" nil :visible nil)
2920     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2921     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2922     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2923     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2924     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2925     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2926     ;;
2927     (gnus-info-find-node "gnus/help" nil :visible nil))
2928   "List of functions for the summary tool bar (retro look).
2929
2930 See `gmm-tool-bar-from-list' for the format of the list."
2931   :type '(repeat gmm-tool-bar-item)
2932   :version "23.1" ;; No Gnus
2933   :initialize 'custom-initialize-default
2934   :set 'gnus-summary-tool-bar-update
2935   :group 'gnus-summary)
2936
2937 (defcustom gnus-summary-tool-bar-zap-list t
2938   "List of icon items from the global tool bar.
2939 These items are not displayed in the Gnus summary mode tool bar.
2940
2941 See `gmm-tool-bar-from-list' for the format of the list."
2942   :type 'gmm-tool-bar-zap-list
2943   :version "23.1" ;; No Gnus
2944   :initialize 'custom-initialize-default
2945   :set 'gnus-summary-tool-bar-update
2946   :group 'gnus-summary)
2947
2948 (defvar image-load-path)
2949 (defvar tool-bar-map)
2950
2951 (defun gnus-summary-make-tool-bar (&optional force)
2952   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2953 When FORCE, rebuild the tool bar."
2954   (when (and (not (featurep 'xemacs))
2955              (boundp 'tool-bar-mode)
2956              tool-bar-mode
2957              (or (not gnus-summary-tool-bar-map) force))
2958     (let* ((load-path
2959             (gmm-image-load-path-for-library "gnus"
2960                                              "mail/save.xpm"
2961                                              nil t))
2962            (image-load-path (cons (car load-path)
2963                                   (when (boundp 'image-load-path)
2964                                     image-load-path)))
2965            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2966                                         gnus-summary-tool-bar-zap-list
2967                                         'gnus-summary-mode-map)))
2968       (when map
2969         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2970         ;; uses its value.
2971         (setq gnus-summary-tool-bar-map map))))
2972   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2973
2974 (defun gnus-make-score-map (type)
2975   "Make a summary score map of type TYPE."
2976   (if t
2977       nil
2978     (let ((headers '(("author" "from" string)
2979                      ("subject" "subject" string)
2980                      ("article body" "body" string)
2981                      ("article head" "head" string)
2982                      ("xref" "xref" string)
2983                      ("extra header" "extra" string)
2984                      ("lines" "lines" number)
2985                      ("followups to author" "followup" string)))
2986           (types '((number ("less than" <)
2987                            ("greater than" >)
2988                            ("equal" =))
2989                    (string ("substring" s)
2990                            ("exact string" e)
2991                            ("fuzzy string" f)
2992                            ("regexp" r))))
2993           (perms '(("temporary" (current-time-string))
2994                    ("permanent" nil)
2995                    ("immediate" now)))
2996           header)
2997       (list
2998        (apply
2999         'nconc
3000         (list
3001          (if (eq type 'lower)
3002              "Lower score"
3003            "Increase score"))
3004         (let (outh)
3005           (while headers
3006             (setq header (car headers))
3007             (setq outh
3008                   (cons
3009                    (apply
3010                     'nconc
3011                     (list (car header))
3012                     (let ((ts (cdr (assoc (nth 2 header) types)))
3013                           outt)
3014                       (while ts
3015                         (setq outt
3016                               (cons
3017                                (apply
3018                                 'nconc
3019                                 (list (caar ts))
3020                                 (let ((ps perms)
3021                                       outp)
3022                                   (while ps
3023                                     (setq outp
3024                                           (cons
3025                                            (vector
3026                                             (caar ps)
3027                                             (list
3028                                              'gnus-summary-score-entry
3029                                              (nth 1 header)
3030                                              (if (or (string= (nth 1 header)
3031                                                               "head")
3032                                                      (string= (nth 1 header)
3033                                                               "body"))
3034                                                  ""
3035                                                (list 'gnus-summary-header
3036                                                      (nth 1 header)))
3037                                              (list 'quote (nth 1 (car ts)))
3038                                              (list 'gnus-score-delta-default
3039                                                    nil)
3040                                              (nth 1 (car ps))
3041                                              t)
3042                                             t)
3043                                            outp))
3044                                     (setq ps (cdr ps)))
3045                                   (list (nreverse outp))))
3046                                outt))
3047                         (setq ts (cdr ts)))
3048                       (list (nreverse outt))))
3049                    outh))
3050             (setq headers (cdr headers)))
3051           (list (nreverse outh))))))))
3052
3053
3054 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3055 (defvar bookmark-make-record-function)
3056 \f
3057 (defvar bidi-paragraph-direction)
3058
3059 (defun gnus-summary-mode (&optional group)
3060   "Major mode for reading articles.
3061
3062 All normal editing commands are switched off.
3063 \\<gnus-summary-mode-map>
3064 Each line in this buffer represents one article.  To read an
3065 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3066 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3067 respectively.
3068
3069 You can also post articles and send mail from this buffer.  To
3070 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3071 of an article, type `\\[gnus-summary-reply]'.
3072
3073 There are approx. one gazillion commands you can execute in this
3074 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3075
3076 The following commands are available:
3077
3078 \\{gnus-summary-mode-map}"
3079   (interactive)
3080   (kill-all-local-variables)
3081   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3082     (gnus-summary-make-local-variables))
3083   (gnus-summary-make-local-variables)
3084   (setq gnus-newsgroup-name group)
3085   (when (gnus-visual-p 'summary-menu 'menu)
3086     (gnus-summary-make-menu-bar)
3087     (gnus-summary-make-tool-bar))
3088   (gnus-make-thread-indent-array)
3089   (gnus-simplify-mode-line)
3090   (setq major-mode 'gnus-summary-mode)
3091   (setq mode-name "Summary")
3092   (use-local-map gnus-summary-mode-map)
3093   (buffer-disable-undo)
3094   (setq buffer-read-only t              ;Disable modification
3095         show-trailing-whitespace nil)
3096   (setq truncate-lines t)
3097   ;; Force paragraph direction to be left-to-right.  Don't make it
3098   ;; bound globally in old Emacsen and XEmacsen.
3099   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3100   (add-to-invisibility-spec '(gnus-sum . t))
3101   (gnus-summary-set-display-table)
3102   (gnus-set-default-directory)
3103   (make-local-variable 'gnus-summary-line-format)
3104   (make-local-variable 'gnus-summary-line-format-spec)
3105   (make-local-variable 'gnus-summary-dummy-line-format)
3106   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3107   (make-local-variable 'gnus-summary-mark-positions)
3108   (gnus-make-local-hook 'pre-command-hook)
3109   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3110   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3111   (turn-on-gnus-mailing-list-mode)
3112   (mm-enable-multibyte)
3113   (set (make-local-variable 'bookmark-make-record-function)
3114        'gnus-summary-bookmark-make-record)
3115   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3116   (gnus-update-summary-mark-positions))
3117
3118 (defun gnus-summary-make-local-variables ()
3119   "Make all the local summary buffer variables."
3120   (let (global)
3121     (dolist (local gnus-summary-local-variables)
3122       (if (consp local)
3123           (progn
3124             (if (eq (cdr local) 'global)
3125                 ;; Copy the global value of the variable.
3126                 (setq global (symbol-value (car local)))
3127               ;; Use the value from the list.
3128               (setq global (eval (cdr local))))
3129             (set (make-local-variable (car local)) global))
3130         ;; Simple nil-valued local variable.
3131         (set (make-local-variable local) nil)))))
3132
3133 ;; Summary data functions.
3134
3135 (defmacro gnus-data-number (data)
3136   `(car ,data))
3137
3138 (defmacro gnus-data-set-number (data number)
3139   `(setcar ,data ,number))
3140
3141 (defmacro gnus-data-mark (data)
3142   `(nth 1 ,data))
3143
3144 (defmacro gnus-data-set-mark (data mark)
3145   `(setcar (nthcdr 1 ,data) ,mark))
3146
3147 (defmacro gnus-data-pos (data)
3148   `(nth 2 ,data))
3149
3150 (defmacro gnus-data-set-pos (data pos)
3151   `(setcar (nthcdr 2 ,data) ,pos))
3152
3153 (defmacro gnus-data-header (data)
3154   `(nth 3 ,data))
3155
3156 (defmacro gnus-data-set-header (data header)
3157   `(setf (nth 3 ,data) ,header))
3158
3159 (defmacro gnus-data-level (data)
3160   `(nth 4 ,data))
3161
3162 (defmacro gnus-data-unread-p (data)
3163   `(= (nth 1 ,data) gnus-unread-mark))
3164
3165 (defmacro gnus-data-read-p (data)
3166   `(/= (nth 1 ,data) gnus-unread-mark))
3167
3168 (defmacro gnus-data-pseudo-p (data)
3169   `(consp (nth 3 ,data)))
3170
3171 (defmacro gnus-data-find (number)
3172   `(assq ,number gnus-newsgroup-data))
3173
3174 (defmacro gnus-data-find-list (number &optional data)
3175   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3176      (memq (assq ,number bdata)
3177            bdata)))
3178
3179 (defmacro gnus-data-make (number mark pos header level)
3180   `(list ,number ,mark ,pos ,header ,level))
3181
3182 (defun gnus-data-enter (after-article number mark pos header level offset)
3183   (let ((data (gnus-data-find-list after-article)))
3184     (unless data
3185       (error "No such article: %d" after-article))
3186     (setcdr data (cons (gnus-data-make number mark pos header level)
3187                        (cdr data)))
3188     (setq gnus-newsgroup-data-reverse nil)
3189     (gnus-data-update-list (cddr data) offset)))
3190
3191 (defun gnus-data-enter-list (after-article list &optional offset)
3192   (when list
3193     (let ((data (and after-article (gnus-data-find-list after-article)))
3194           (ilist list))
3195       (if (not (or data
3196                    after-article))
3197           (let ((odata gnus-newsgroup-data))
3198             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3199             (when offset
3200               (gnus-data-update-list odata offset)))
3201         ;; Find the last element in the list to be spliced into the main
3202         ;; list.
3203         (setq list (last list))
3204         (if (not data)
3205             (progn
3206               (setcdr list gnus-newsgroup-data)
3207               (setq gnus-newsgroup-data ilist)
3208               (when offset
3209                 (gnus-data-update-list (cdr list) offset)))
3210           (setcdr list (cdr data))
3211           (setcdr data ilist)
3212           (when offset
3213             (gnus-data-update-list (cdr list) offset))))
3214       (setq gnus-newsgroup-data-reverse nil))))
3215
3216 (defun gnus-data-remove (article &optional offset)
3217   (let ((data gnus-newsgroup-data))
3218     (if (= (gnus-data-number (car data)) article)
3219         (progn
3220           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3221                 gnus-newsgroup-data-reverse nil)
3222           (when offset
3223             (gnus-data-update-list gnus-newsgroup-data offset)))
3224       (while (cdr data)
3225         (when (= (gnus-data-number (cadr data)) article)
3226           (setcdr data (cddr data))
3227           (when offset
3228             (gnus-data-update-list (cdr data) offset))
3229           (setq data nil
3230                 gnus-newsgroup-data-reverse nil))
3231         (setq data (cdr data))))))
3232
3233 (defmacro gnus-data-list (backward)
3234   `(if ,backward
3235        (or gnus-newsgroup-data-reverse
3236            (setq gnus-newsgroup-data-reverse
3237                  (reverse gnus-newsgroup-data)))
3238      gnus-newsgroup-data))
3239
3240 (defun gnus-data-update-list (data offset)
3241   "Add OFFSET to the POS of all data entries in DATA."
3242   (setq gnus-newsgroup-data-reverse nil)
3243   (while data
3244     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3245     (setq data (cdr data))))
3246
3247 (defun gnus-summary-article-pseudo-p (article)
3248   "Say whether this article is a pseudo article or not."
3249   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3250
3251 (defmacro gnus-summary-article-sparse-p (article)
3252   "Say whether this article is a sparse article or not."
3253   `(memq ,article gnus-newsgroup-sparse))
3254
3255 (defmacro gnus-summary-article-ancient-p (article)
3256   "Say whether this article is a sparse article or not."
3257   `(memq ,article gnus-newsgroup-ancient))
3258
3259 (defun gnus-article-children (number)
3260   "Return a list of all children to NUMBER."
3261   (let* ((data (gnus-data-find-list number))
3262          (level (gnus-data-level (car data)))
3263          children)
3264     (setq data (cdr data))
3265     (while (and data
3266                 (= (gnus-data-level (car data)) (1+ level)))
3267       (push (gnus-data-number (car data)) children)
3268       (setq data (cdr data)))
3269     children))
3270
3271 (defmacro gnus-summary-skip-intangible ()
3272   "If the current article is intangible, then jump to a different article."
3273   '(let ((to (get-text-property (point) 'gnus-intangible)))
3274      (and to (gnus-summary-goto-subject to))))
3275
3276 (defmacro gnus-summary-article-intangible-p ()
3277   "Say whether this article is intangible or not."
3278   '(get-text-property (point) 'gnus-intangible))
3279
3280 ;; Some summary mode macros.
3281
3282 (defmacro gnus-summary-article-number ()
3283   "The article number of the article on the current line.
3284 If there isn't an article number here, then we return the current
3285 article number."
3286   '(progn
3287      (gnus-summary-skip-intangible)
3288      (or (get-text-property (point) 'gnus-number)
3289          (gnus-summary-last-subject))))
3290
3291 (defmacro gnus-summary-article-header (&optional number)
3292   "Return the header of article NUMBER."
3293   `(gnus-data-header (gnus-data-find
3294                       ,(or number '(gnus-summary-article-number)))))
3295
3296 (defmacro gnus-summary-thread-level (&optional number)
3297   "Return the level of thread that starts with article NUMBER."
3298   `(if (and (eq gnus-summary-make-false-root 'dummy)
3299             (get-text-property (point) 'gnus-intangible))
3300        0
3301      (gnus-data-level (gnus-data-find
3302                        ,(or number '(gnus-summary-article-number))))))
3303
3304 (defmacro gnus-summary-article-mark (&optional number)
3305   "Return the mark of article NUMBER."
3306   `(gnus-data-mark (gnus-data-find
3307                     ,(or number '(gnus-summary-article-number)))))
3308
3309 (defmacro gnus-summary-article-pos (&optional number)
3310   "Return the position of the line of article NUMBER."
3311   `(gnus-data-pos (gnus-data-find
3312                    ,(or number '(gnus-summary-article-number)))))
3313
3314 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3315 (defmacro gnus-summary-article-subject (&optional number)
3316   "Return current subject string or nil if nothing."
3317   `(let ((headers
3318           ,(if number
3319                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3320              '(gnus-data-header (assq (gnus-summary-article-number)
3321                                       gnus-newsgroup-data)))))
3322      (and headers
3323           (vectorp headers)
3324           (mail-header-subject headers))))
3325
3326 (defmacro gnus-summary-article-score (&optional number)
3327   "Return current article score."
3328   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3329                   gnus-newsgroup-scored))
3330        gnus-summary-default-score 0))
3331
3332 (defun gnus-summary-article-children (&optional number)
3333   "Return a list of article numbers that are children of article NUMBER."
3334   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3335          (level (gnus-data-level (car data)))
3336          l children)
3337     (while (and (setq data (cdr data))
3338                 (> (setq l (gnus-data-level (car data))) level))
3339       (and (= (1+ level) l)
3340            (push (gnus-data-number (car data))
3341                  children)))
3342     (nreverse children)))
3343
3344 (defun gnus-summary-article-parent (&optional number)
3345   "Return the article number of the parent of article NUMBER."
3346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3347                                     (gnus-data-list t)))
3348          (level (gnus-data-level (car data))))
3349     (if (zerop level)
3350         ()                              ; This is a root.
3351       ;; We search until we find an article with a level less than
3352       ;; this one.  That function has to be the parent.
3353       (while (and (setq data (cdr data))
3354                   (not (< (gnus-data-level (car data)) level))))
3355       (and data (gnus-data-number (car data))))))
3356
3357 (defun gnus-unread-mark-p (mark)
3358   "Say whether MARK is the unread mark."
3359   (= mark gnus-unread-mark))
3360
3361 (defun gnus-read-mark-p (mark)
3362   "Say whether MARK is one of the marks that mark as read.
3363 This is all marks except unread, ticked, dormant, and expirable."
3364   (not (or (= mark gnus-unread-mark)
3365            (= mark gnus-ticked-mark)
3366            (= mark gnus-spam-mark)
3367            (= mark gnus-dormant-mark)
3368            (= mark gnus-expirable-mark))))
3369
3370 (defmacro gnus-article-mark (number)
3371   "Return the MARK of article NUMBER.
3372 This macro should only be used when computing the mark the \"first\"
3373 time; i.e., when generating the summary lines.  After that,
3374 `gnus-summary-article-mark' should be used to examine the
3375 marks of articles."
3376   `(cond
3377     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3378     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3379     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3380     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3381     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3382     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3383     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3384     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3385            gnus-ancient-mark))))
3386
3387 ;; Saving hidden threads.
3388
3389 (defmacro gnus-save-hidden-threads (&rest forms)
3390   "Save hidden threads, eval FORMS, and restore the hidden threads."
3391   (let ((config (make-symbol "config")))
3392     `(let ((,config (gnus-hidden-threads-configuration)))
3393        (unwind-protect
3394            (save-excursion
3395              ,@forms)
3396          (gnus-restore-hidden-threads-configuration ,config)))))
3397 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3398 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3399
3400 (defun gnus-data-compute-positions ()
3401   "Compute the positions of all articles."
3402   (setq gnus-newsgroup-data-reverse nil)
3403   (let ((data gnus-newsgroup-data))
3404     (save-excursion
3405       (gnus-save-hidden-threads
3406         (gnus-summary-show-all-threads)
3407         (goto-char (point-min))
3408         (while data
3409           (while (get-text-property (point) 'gnus-intangible)
3410             (forward-line 1))
3411           (gnus-data-set-pos (car data) (+ (point) 3))
3412           (setq data (cdr data))
3413           (forward-line 1))))))
3414
3415 (defun gnus-hidden-threads-configuration ()
3416   "Return the current hidden threads configuration."
3417   (save-excursion
3418     (let (config)
3419       (goto-char (point-min))
3420       (while (not (eobp))
3421         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3422           (push (save-excursion (forward-line 0) (point)) config))
3423         (forward-line 1))
3424       config)))
3425
3426 (defun gnus-restore-hidden-threads-configuration (config)
3427   "Restore hidden threads configuration from CONFIG."
3428   (save-excursion
3429     (let (point (inhibit-read-only t))
3430       (while (setq point (pop config))
3431         (goto-char point)
3432         (gnus-summary-hide-thread)))))
3433
3434 ;; Various summary mode internalish functions.
3435
3436 (defun gnus-mouse-pick-article (e)
3437   (interactive "e")
3438   (mouse-set-point e)
3439   (gnus-summary-next-page nil t))
3440
3441 (defun gnus-summary-set-display-table ()
3442   "Change the display table.
3443 Odd characters have a tendency to mess
3444 up nicely formatted displays - we make all possible glyphs
3445 display only a single character."
3446
3447   ;; We start from the standard display table, if any.
3448   (let ((table (or (copy-sequence standard-display-table)
3449                    (make-display-table)))
3450         (i 32))
3451     ;; Nix out all the control chars...
3452     (while (>= (setq i (1- i)) 0)
3453       (gnus-put-display-table i [??] table))
3454    ;; ... but not newline and cr, of course.  (cr is necessary for the
3455     ;; selective display).
3456     (gnus-put-display-table ?\n nil table)
3457     (gnus-put-display-table ?\r nil table)
3458     ;; We keep TAB as well.
3459     (gnus-put-display-table ?\t nil table)
3460     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3461     ;; Emacs 23 (unicode), that are not set already.
3462     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3463                  160
3464                256)))
3465       (while (>= (setq i (1- i)) 127)
3466         ;; Only modify if the entry is nil.
3467         (unless (gnus-get-display-table i table)
3468           (gnus-put-display-table i [??] table))))
3469     (setq buffer-display-table table)))
3470
3471 (defun gnus-summary-set-article-display-arrow (pos)
3472   "Update the overlay arrow to point to line at position POS."
3473   (when gnus-summary-display-arrow
3474     (make-local-variable 'overlay-arrow-position)
3475     (make-local-variable 'overlay-arrow-string)
3476     (save-excursion
3477       (goto-char pos)
3478       (beginning-of-line)
3479       (unless overlay-arrow-position
3480         (setq overlay-arrow-position (make-marker)))
3481       (setq overlay-arrow-string "=>"
3482             overlay-arrow-position (set-marker overlay-arrow-position
3483                                                (point)
3484                                                (current-buffer))))))
3485
3486 (defun gnus-summary-setup-buffer (group)
3487   "Initialize summary buffer.
3488 If the setup was successful, non-nil is returned."
3489   (let ((buffer (gnus-summary-buffer-name group))
3490         (dead-name (concat "*Dead Summary "
3491                            (gnus-group-decoded-name group) "*")))
3492     ;; If a dead summary buffer exists, we kill it.
3493     (when (gnus-buffer-live-p dead-name)
3494       (gnus-kill-buffer dead-name))
3495     (if (get-buffer buffer)
3496         (progn
3497           (set-buffer buffer)
3498           (setq gnus-summary-buffer (current-buffer))
3499           (not gnus-newsgroup-prepared))
3500       (set-buffer (gnus-get-buffer-create buffer))
3501       (setq gnus-summary-buffer (current-buffer))
3502       (gnus-summary-mode group)
3503       (when (gnus-group-quit-config group)
3504         (set (make-local-variable 'gnus-single-article-buffer) nil))
3505       (make-local-variable 'gnus-article-buffer)
3506       (make-local-variable 'gnus-article-current)
3507       (make-local-variable 'gnus-original-article-buffer)
3508       (setq gnus-newsgroup-name group)
3509       ;; Set any local variables in the group parameters.
3510       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3511       t)))
3512
3513 (defun gnus-set-global-variables ()
3514   "Set the global equivalents of the buffer-local variables.
3515 They are set to the latest values they had.  These reflect the summary
3516 buffer that was in action when the last article was fetched."
3517   (when (eq major-mode 'gnus-summary-mode)
3518     (setq gnus-summary-buffer (current-buffer))
3519     (let ((name gnus-newsgroup-name)
3520           (marked gnus-newsgroup-marked)
3521           (spam gnus-newsgroup-spam-marked)
3522           (unread gnus-newsgroup-unreads)
3523           (headers gnus-current-headers)
3524           (data gnus-newsgroup-data)
3525           (summary gnus-summary-buffer)
3526           (article-buffer gnus-article-buffer)
3527           (original gnus-original-article-buffer)
3528           (gac gnus-article-current)
3529           (reffed gnus-reffed-article-number)
3530           (score-file gnus-current-score-file)
3531           (default-charset gnus-newsgroup-charset)
3532           vlist)
3533       (let ((locals gnus-newsgroup-variables))
3534         (while locals
3535           (if (consp (car locals))
3536               (push (eval (caar locals)) vlist)
3537             (push (eval (car locals)) vlist))
3538           (setq locals (cdr locals)))
3539         (setq vlist (nreverse vlist)))
3540       (with-temp-buffer
3541         (setq gnus-newsgroup-name name
3542               gnus-newsgroup-marked marked
3543               gnus-newsgroup-spam-marked spam
3544               gnus-newsgroup-unreads unread
3545               gnus-current-headers headers
3546               gnus-newsgroup-data data
3547               gnus-article-current gac
3548               gnus-summary-buffer summary
3549               gnus-article-buffer article-buffer
3550               gnus-original-article-buffer original
3551               gnus-reffed-article-number reffed
3552               gnus-current-score-file score-file
3553               gnus-newsgroup-charset default-charset)
3554         (let ((locals gnus-newsgroup-variables))
3555           (while locals
3556             (if (consp (car locals))
3557                 (set (caar locals) (pop vlist))
3558               (set (car locals) (pop vlist)))
3559             (setq locals (cdr locals))))))))
3560
3561 (defun gnus-summary-article-unread-p (article)
3562   "Say whether ARTICLE is unread or not."
3563   (memq article gnus-newsgroup-unreads))
3564
3565 (defun gnus-summary-first-article-p (&optional article)
3566   "Return whether ARTICLE is the first article in the buffer."
3567   (if (not (setq article (or article (gnus-summary-article-number))))
3568       nil
3569     (eq article (caar gnus-newsgroup-data))))
3570
3571 (defun gnus-summary-last-article-p (&optional article)
3572   "Return whether ARTICLE is the last article in the buffer."
3573   (if (not (setq article (or article (gnus-summary-article-number))))
3574       ;; All non-existent numbers are the last article.  :-)
3575       t
3576     (not (cdr (gnus-data-find-list article)))))
3577
3578 (defun gnus-make-thread-indent-array (&optional n)
3579   (when (or n
3580             (progn (setq n 200) nil)
3581             (null gnus-thread-indent-array)
3582             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3583     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3584           gnus-thread-indent-array-level gnus-thread-indent-level)
3585     (while (>= n 0)
3586       (aset gnus-thread-indent-array n
3587             (make-string (* n gnus-thread-indent-level) ? ))
3588       (setq n (1- n)))))
3589
3590 (defun gnus-update-summary-mark-positions ()
3591   "Compute where the summary marks are to go."
3592   (save-excursion
3593     (when (gnus-buffer-exists-p gnus-summary-buffer)
3594       (set-buffer gnus-summary-buffer))
3595     (let ((spec gnus-summary-line-format-spec)
3596           pos)
3597       (save-excursion
3598         (gnus-set-work-buffer)
3599         (let ((gnus-tmp-unread ?Z)
3600               (gnus-replied-mark ?Z)
3601               (gnus-score-below-mark ?Z)
3602               (gnus-score-over-mark ?Z)
3603               (gnus-undownloaded-mark ?Z)
3604               (gnus-summary-line-format-spec spec)
3605               (gnus-newsgroup-downloadable '(0))
3606               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3607               case-fold-search ignores)
3608           ;; Here, all marks are bound to Z.
3609           (gnus-summary-insert-line header
3610                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3611           (goto-char (point-min))
3612           ;; Memorize the positions of the same characters as dummy marks.
3613           (while (re-search-forward "[A-D]" nil t)
3614             (push (point) ignores))
3615           (erase-buffer)
3616           ;; We use A-D as dummy marks in order to know column positions
3617           ;; where marks should be inserted.
3618           (setq gnus-tmp-unread ?A
3619                 gnus-replied-mark ?B
3620                 gnus-score-below-mark ?C
3621                 gnus-score-over-mark ?C
3622                 gnus-undownloaded-mark ?D)
3623           (gnus-summary-insert-line header
3624                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3625           ;; Ignore characters which aren't dummy marks.
3626           (dolist (p ignores)
3627             (delete-region (goto-char (1- p)) p)
3628             (insert ?Z))
3629           (goto-char (point-min))
3630           (setq pos (list (cons 'unread
3631                                 (and (search-forward "A" nil t)
3632                                      (- (point) (point-min) 1)))))
3633           (goto-char (point-min))
3634           (push (cons 'replied (and (search-forward "B" nil t)
3635                                     (- (point) (point-min) 1)))
3636                 pos)
3637           (goto-char (point-min))
3638           (push (cons 'score (and (search-forward "C" nil t)
3639                                   (- (point) (point-min) 1)))
3640                 pos)
3641           (goto-char (point-min))
3642           (push (cons 'download (and (search-forward "D" nil t)
3643                                      (- (point) (point-min) 1)))
3644                 pos)))
3645       (setq gnus-summary-mark-positions pos))))
3646
3647 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3648   "Insert a dummy root in the summary buffer."
3649   (beginning-of-line)
3650   (gnus-add-text-properties
3651    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3652    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3653
3654 (defun gnus-summary-extract-address-component (from)
3655   (or (car (funcall gnus-extract-address-components from))
3656       from))
3657
3658 (defun gnus-summary-from-or-to-or-newsgroups (header from)
3659   (let ((mail-parse-charset gnus-newsgroup-charset)
3660         (ignored-from-addresses (gnus-ignored-from-addresses))
3661         ;; Is it really necessary to do this next part for each summary line?
3662         ;; Luckily, doesn't seem to slow things down much.
3663         (mail-parse-ignored-charsets
3664          (with-current-buffer gnus-summary-buffer
3665            gnus-newsgroup-ignored-charsets))
3666         (address (cadr (gnus-extract-address-components from))))
3667     (or
3668      (and ignored-from-addresses
3669           (string-match ignored-from-addresses address)
3670           (let ((extra-headers (mail-header-extra header))
3671                 to
3672                 newsgroups)
3673             (cond
3674              ((setq to (cdr (assq 'To extra-headers)))
3675               (concat gnus-summary-to-prefix
3676                       (inline
3677                         (gnus-summary-extract-address-component
3678                          (funcall gnus-decode-encoded-address-function to)))))
3679              ((setq newsgroups
3680                     (or
3681                      (cdr (assq 'Newsgroups extra-headers))
3682                      (and
3683                       (memq 'Newsgroups gnus-extra-headers)
3684                       (eq (car (gnus-find-method-for-group
3685                                 gnus-newsgroup-name)) 'nntp)
3686                       (gnus-group-real-name gnus-newsgroup-name))))
3687               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3688      (gnus-string-mark-left-to-right (gnus-summary-extract-address-component from)))))
3689
3690 (defun gnus-summary-insert-line (gnus-tmp-header
3691                                  gnus-tmp-level gnus-tmp-current
3692                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3693                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3694                                  &optional gnus-tmp-dummy gnus-tmp-score
3695                                  gnus-tmp-process)
3696   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3697       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3698                                           gnus-tmp-level)))
3699   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3700          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3701          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3702          (gnus-tmp-score-char
3703           (if (or (null gnus-summary-default-score)
3704                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3705                       gnus-summary-zcore-fuzz))
3706               ?                         ;Whitespace
3707             (if (< gnus-tmp-score gnus-summary-default-score)
3708                 gnus-score-below-mark gnus-score-over-mark)))
3709          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3710          (gnus-tmp-replied
3711           (cond (gnus-tmp-process gnus-process-mark)
3712                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3713                  gnus-cached-mark)
3714                 (gnus-tmp-replied gnus-replied-mark)
3715                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3716                  gnus-forwarded-mark)
3717                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3718                  gnus-saved-mark)
3719                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3720                  gnus-unseen-mark)
3721                 (t gnus-no-mark)))
3722          (gnus-tmp-downloaded
3723           (cond (undownloaded
3724                  gnus-undownloaded-mark)
3725                 (gnus-newsgroup-agentized
3726                  gnus-downloaded-mark)
3727                 (t
3728                  gnus-no-mark)))
3729          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3730          (gnus-tmp-name
3731           (cond
3732            ((string-match "<[^>]+> *$" gnus-tmp-from)
3733             (let ((beg (match-beginning 0)))
3734               (or (and (string-match "^\".+\"" gnus-tmp-from)
3735                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3736                   (substring gnus-tmp-from 0 beg))))
3737            ((string-match "(.+)" gnus-tmp-from)
3738             (substring gnus-tmp-from
3739                        (1+ (match-beginning 0)) (1- (match-end 0))))
3740            (t gnus-tmp-from)))
3741          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3742          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3743          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3744          (inhibit-read-only t))
3745     (when (string= gnus-tmp-name "")
3746       (setq gnus-tmp-name gnus-tmp-from))
3747     (unless (numberp gnus-tmp-lines)
3748       (setq gnus-tmp-lines -1))
3749     (if (= gnus-tmp-lines -1)
3750         (setq gnus-tmp-lines "?")
3751       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3752     (condition-case ()
3753         (gnus-put-text-property
3754          (point)
3755          (progn (eval gnus-summary-line-format-spec) (point))
3756          'gnus-number gnus-tmp-number)
3757       (error (gnus-message 5 "Error updating the summary line")))
3758     (when (gnus-visual-p 'summary-highlight 'highlight)
3759       (forward-line -1)
3760       (gnus-summary-highlight-line)
3761       (gnus-run-hooks 'gnus-summary-update-hook)
3762       (forward-line 1))))
3763
3764 (defun gnus-summary-update-line (&optional dont-update)
3765   "Update summary line after change."
3766   (when (and gnus-summary-default-score
3767              (not gnus-summary-inhibit-highlight))
3768     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3769            (article (gnus-summary-article-number))
3770            (score (gnus-summary-article-score article)))
3771       (unless dont-update
3772         (if (and gnus-summary-mark-below
3773                  (< (gnus-summary-article-score)
3774                     gnus-summary-mark-below))
3775             ;; This article has a low score, so we mark it as read.
3776             (when (memq article gnus-newsgroup-unreads)
3777               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3778           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3779             ;; This article was previously marked as read on account
3780             ;; of a low score, but now it has risen, so we mark it as
3781             ;; unread.
3782             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3783         (gnus-summary-update-mark
3784          (if (or (null gnus-summary-default-score)
3785                  (<= (abs (- score gnus-summary-default-score))
3786                      gnus-summary-zcore-fuzz))
3787              ?                          ;Whitespace
3788            (if (< score gnus-summary-default-score)
3789                gnus-score-below-mark gnus-score-over-mark))
3790          'score))
3791       ;; Do visual highlighting.
3792       (when (gnus-visual-p 'summary-highlight 'highlight)
3793         (gnus-summary-highlight-line)
3794         (gnus-run-hooks 'gnus-summary-update-hook)))))
3795
3796 (defvar gnus-tmp-new-adopts nil)
3797
3798 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3799   "Return the number of articles in THREAD.
3800 This may be 0 in some cases -- if none of the articles in
3801 the thread are to be displayed."
3802   (let* ((number
3803          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3804           (cond
3805            ((not (listp thread))
3806             1)
3807            ((and (consp thread) (cdr thread))
3808             (apply
3809              '+ 1 (mapcar
3810                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3811            ((null thread)
3812             1)
3813            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3814             1)
3815            (t 0))))
3816     (when (and level (zerop level) gnus-tmp-new-adopts)
3817       (incf number
3818             (apply '+ (mapcar
3819                        'gnus-summary-number-of-articles-in-thread
3820                        gnus-tmp-new-adopts))))
3821     (if char
3822         (if (> number 1) gnus-not-empty-thread-mark
3823           gnus-empty-thread-mark)
3824       number)))
3825
3826 (defsubst gnus-summary-line-message-size (head)
3827   "Return pretty-printed version of message size.
3828 This function is intended to be used in
3829 `gnus-summary-line-format-alist'."
3830   (let ((c (or (mail-header-chars head) -1)))
3831     (cond ((< c 0) "n/a")               ; chars not available
3832           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3833           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3834           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3835           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3836
3837 (defcustom gnus-user-date-format-alist
3838   '(((gnus-seconds-today) . "Today, %H:%M")
3839     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3840     (604800 . "%A %H:%M")               ; That's one week
3841     ((gnus-seconds-month) . "%A %d")
3842     ((gnus-seconds-year) . "%B %d")
3843     (t . "%b %d %Y"))                   ; This one is used when no other
3844                                         ; does match
3845   "Specifies date format depending on age of article.
3846 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3847 seconds) or a Lisp expression evaluating to a number.  When the age of
3848 the article is less than this number, then use `format-time-string'
3849 with the corresponding FORMAT for displaying the date of the article.
3850 If AGE is not a number or a Lisp expression evaluating to a
3851 non-number, then the corresponding FORMAT is used as a default value.
3852
3853 Note that the list is processed from the beginning, so it should be
3854 sorted by ascending AGE.  Also note that items following the first
3855 non-number AGE will be ignored.
3856
3857 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3858 and `gnus-seconds-year' in the AGE spec.  They return the number of
3859 seconds passed since the start of today, of this month, of this year,
3860 respectively."
3861   :version "24.1"
3862   :group 'gnus-summary-format
3863   :type '(alist :key-type sexp :value-type string))
3864
3865 (defun gnus-user-date (messy-date)
3866   "Format the messy-date according to `gnus-user-date-format-alist'.
3867 Returns \"  ?  \" if there's bad input or if another error occurs.
3868 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3869   (condition-case ()
3870       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3871              (now (gnus-float-time))
3872              ;;If we don't find something suitable we'll use this one
3873              (my-format "%b %d '%y"))
3874         (let* ((difference (- now messy-date))
3875                (templist gnus-user-date-format-alist)
3876                (top (eval (caar templist))))
3877           (while (if (numberp top) (< top difference) (not top))
3878             (progn
3879               (setq templist (cdr templist))
3880               (setq top (eval (caar templist)))))
3881           (if (stringp (cdr (car templist)))
3882               (setq my-format (cdr (car templist)))))
3883         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3884     (error "  ?   ")))
3885
3886 (defun gnus-summary-set-local-parameters (group)
3887   "Go through the local params of GROUP and set all variable specs in that list."
3888   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3889                                         ; really variables.
3890     (dolist (elem (gnus-group-find-parameter group))
3891       (and (consp elem)                 ; Has to be a cons.
3892            (consp (cdr elem))           ; The cdr has to be a list.
3893            (symbolp (car elem))         ; Has to be a symbol in there.
3894            (not (memq (car elem) vars))
3895            (ignore-errors
3896              (push (car elem) vars)
3897              ;; Variables like `gnus-show-threads' that are globally
3898              ;; bound, if used as group parameters, need to get to be
3899              ;; buffer-local, whereas just parameters like `gcc-self',
3900              ;; `timestamp', etc. should not be bound as variables.
3901              (if (boundp (car elem))
3902                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3903                (eval (nth 1 elem))))))))
3904
3905 (defun gnus-summary-read-group (group &optional show-all no-article
3906                                       kill-buffer no-display backward
3907                                       select-articles)
3908   "Start reading news in newsgroup GROUP.
3909 If SHOW-ALL is non-nil, already read articles are also listed.
3910 If NO-ARTICLE is non-nil, no article is selected initially.
3911 If NO-DISPLAY, don't generate the summary buffer contents.
3912 If KILL-BUFFER, it should be a buffer that's killed once the new
3913 summary buffer has been generated.
3914 If BACKWARD, move point to the previous group in the group buffer
3915 If SELECT-ARTICLES, only select those articles from GROUP."
3916   (let (result)
3917     (while (and group
3918                 (null (setq result
3919                             (let ((gnus-auto-select-next nil))
3920                               (or (gnus-summary-read-group-1
3921                                    group show-all no-article
3922                                    kill-buffer no-display
3923                                    select-articles)
3924                                   (setq show-all nil
3925                                         select-articles nil)))))
3926                 (eq gnus-auto-select-next 'quietly))
3927       (set-buffer gnus-group-buffer)
3928       ;; The entry function called above goes to the next
3929       ;; group automatically, so we go two groups back
3930       ;; if we are searching for the previous group.
3931       (when backward
3932         (gnus-group-prev-unread-group 2))
3933       (if (not (equal group (gnus-group-group-name)))
3934           (setq group (gnus-group-group-name))
3935         (setq group nil)))
3936     result))
3937
3938 (defun gnus-summary-read-group-1 (group show-all no-article
3939                                         kill-buffer no-display
3940                                         &optional select-articles)
3941   ;; Killed foreign groups can't be entered.
3942   ;;  (when (and (not (gnus-group-native-p group))
3943   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3944   ;;    (error "Dead non-native groups can't be entered"))
3945   (gnus-message 7 "Retrieving newsgroup: %s..."
3946                 (gnus-group-decoded-name group))
3947   (let* ((new-group (gnus-summary-setup-buffer group))
3948          (quit-config (gnus-group-quit-config group))
3949          (did-select (and new-group (gnus-select-newsgroup
3950                                      group show-all select-articles))))
3951     (cond
3952      ;; This summary buffer exists already, so we just select it.
3953      ((not new-group)
3954       (gnus-set-global-variables)
3955       (when kill-buffer
3956         (gnus-kill-or-deaden-summary kill-buffer))
3957       (gnus-configure-windows 'summary 'force)
3958       (gnus-set-mode-line 'summary)
3959       (gnus-summary-position-point)
3960       (message "")
3961       t)
3962      ;; We couldn't select this group.
3963      ((null did-select)
3964       (when (and (eq major-mode 'gnus-summary-mode)
3965                  (not (equal (current-buffer) kill-buffer)))
3966         (kill-buffer (current-buffer))
3967         (if (not quit-config)
3968             (progn
3969               ;; Update the info -- marks might need to be removed,
3970               ;; for instance.
3971               (gnus-summary-update-info)
3972               (set-buffer gnus-group-buffer)
3973               (gnus-group-jump-to-group group)
3974               (gnus-group-next-unread-group 1))
3975           (gnus-handle-ephemeral-exit quit-config)))
3976       (if (null (gnus-list-of-unread-articles group))
3977           (gnus-message 3 "Group %s contains no messages" group)
3978         (gnus-message 3 "Can't select group"))
3979       nil)
3980      ;; The user did a `C-g' while prompting for number of articles,
3981      ;; so we exit this group.
3982      ((eq did-select 'quit)
3983       (and (eq major-mode 'gnus-summary-mode)
3984            (not (equal (current-buffer) kill-buffer))
3985            (kill-buffer (current-buffer)))
3986       (when kill-buffer
3987         (gnus-kill-or-deaden-summary kill-buffer))
3988       (if (not quit-config)
3989           (progn
3990             (set-buffer gnus-group-buffer)
3991             (gnus-group-jump-to-group group)
3992             (gnus-configure-windows 'group 'force))
3993         (gnus-handle-ephemeral-exit quit-config))
3994       ;; Finally signal the quit.
3995       (signal 'quit nil))
3996      ;; The group was successfully selected.
3997      (t
3998       (gnus-set-global-variables)
3999       ;; Save the active value in effect when the group was entered.
4000       (setq gnus-newsgroup-active
4001             (gnus-copy-sequence
4002              (gnus-active gnus-newsgroup-name)))
4003       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4004       ;; You can change the summary buffer in some way with this hook.
4005       (gnus-run-hooks 'gnus-select-group-hook)
4006       (when (memq 'summary (gnus-update-format-specifications
4007                             nil 'summary 'summary-mode 'summary-dummy))
4008         ;; The format specification for the summary line was updated,
4009         ;; so we need to update the mark positions as well.
4010         (gnus-update-summary-mark-positions))
4011       ;; Do score processing.
4012       (when gnus-use-scoring
4013         (gnus-possibly-score-headers))
4014       ;; Check whether to fill in the gaps in the threads.
4015       (when gnus-build-sparse-threads
4016         (gnus-build-sparse-threads))
4017       ;; Find the initial limit.
4018       (if show-all
4019           (let ((gnus-newsgroup-dormant nil))
4020             (gnus-summary-initial-limit show-all))
4021         (gnus-summary-initial-limit show-all))
4022       ;; Generate the summary buffer.
4023       (unless no-display
4024         (gnus-summary-prepare))
4025       (when gnus-use-trees
4026         (gnus-tree-open group)
4027         (setq gnus-summary-highlight-line-function
4028               'gnus-tree-highlight-article))
4029       ;; If the summary buffer is empty, but there are some low-scored
4030       ;; articles or some excluded dormants, we include these in the
4031       ;; buffer.
4032       (when (and (zerop (buffer-size))
4033                  (not no-display))
4034         (cond (gnus-newsgroup-dormant
4035                (gnus-summary-limit-include-dormant))
4036               ((and gnus-newsgroup-scored show-all)
4037                (gnus-summary-limit-include-expunged t))))
4038       ;; Function `gnus-apply-kill-file' must be called in this hook.
4039       (gnus-run-hooks 'gnus-apply-kill-hook)
4040       (if (and (zerop (buffer-size))
4041                (not no-display))
4042           (progn
4043             ;; This newsgroup is empty.
4044             (gnus-summary-catchup-and-exit nil t)
4045             (gnus-message 6 "No unread news")
4046             (when kill-buffer
4047               (gnus-kill-or-deaden-summary kill-buffer))
4048             ;; Return nil from this function.
4049             nil)
4050         ;; Hide conversation thread subtrees.  We cannot do this in
4051         ;; gnus-summary-prepare-hook since kill processing may not
4052         ;; work with hidden articles.
4053         (gnus-summary-maybe-hide-threads)
4054         (gnus-configure-windows 'summary)
4055         (when kill-buffer
4056           (gnus-kill-or-deaden-summary kill-buffer))
4057         (gnus-summary-auto-select-subject)
4058         ;; Show first unread article if requested.
4059         (if (and (not no-article)
4060                  (not no-display)
4061                  gnus-newsgroup-unreads
4062                  gnus-auto-select-first)
4063             (progn
4064               (let ((art (gnus-summary-article-number)))
4065                 (unless (and (not gnus-plugged)
4066                              (or (memq art gnus-newsgroup-undownloaded)
4067                                  (memq art gnus-newsgroup-downloadable)))
4068                   (gnus-summary-goto-article art))))
4069           ;; Don't select any articles.
4070           (gnus-summary-position-point)
4071           (gnus-configure-windows 'summary 'force)
4072           (gnus-set-mode-line 'summary))
4073         (when (and gnus-auto-center-group
4074                    (get-buffer-window gnus-group-buffer t))
4075           ;; Gotta use windows, because recenter does weird stuff if
4076           ;; the current buffer ain't the displayed window.
4077           (let ((owin (selected-window)))
4078             (select-window (get-buffer-window gnus-group-buffer t))
4079             (when (gnus-group-goto-group group)
4080               (recenter))
4081             (select-window owin)))
4082         ;; Mark this buffer as "prepared".
4083         (setq gnus-newsgroup-prepared t)
4084         (gnus-run-hooks 'gnus-summary-prepared-hook)
4085         (unless (gnus-ephemeral-group-p group)
4086           (gnus-group-update-group group nil t))
4087         t)))))
4088
4089 (defun gnus-summary-auto-select-subject ()
4090   "Select the subject line on initial group entry."
4091   (goto-char (point-min))
4092   (cond
4093    ((eq gnus-auto-select-subject 'best)
4094     (gnus-summary-best-unread-subject))
4095    ((eq gnus-auto-select-subject 'unread)
4096     (gnus-summary-first-unread-subject))
4097    ((eq gnus-auto-select-subject 'unseen)
4098     (gnus-summary-first-unseen-subject))
4099    ((eq gnus-auto-select-subject 'unseen-or-unread)
4100     (gnus-summary-first-unseen-or-unread-subject))
4101    ((eq gnus-auto-select-subject 'first)
4102     ;; Do nothing.
4103     )
4104    ((functionp gnus-auto-select-subject)
4105     (funcall gnus-auto-select-subject))))
4106
4107 (defun gnus-summary-prepare ()
4108   "Generate the summary buffer."
4109   (interactive)
4110   (let ((inhibit-read-only t))
4111     (erase-buffer)
4112     (setq gnus-newsgroup-data nil
4113           gnus-newsgroup-data-reverse nil)
4114     (gnus-run-hooks 'gnus-summary-generate-hook)
4115     ;; Generate the buffer, either with threads or without.
4116     (when gnus-newsgroup-headers
4117       (gnus-summary-prepare-threads
4118        (if gnus-show-threads
4119            (gnus-sort-gathered-threads
4120             (funcall gnus-summary-thread-gathering-function
4121                      (gnus-sort-threads
4122                       (gnus-cut-threads (gnus-make-threads)))))
4123          ;; Unthreaded display.
4124          (gnus-sort-articles gnus-newsgroup-headers))))
4125     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4126     ;; Call hooks for modifying summary buffer.
4127     (goto-char (point-min))
4128     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4129
4130 (defsubst gnus-general-simplify-subject (subject)
4131   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4132   (setq subject
4133         (cond
4134          ;; Truncate the subject.
4135          (gnus-simplify-subject-functions
4136           (gnus-map-function gnus-simplify-subject-functions subject))
4137          ((numberp gnus-summary-gather-subject-limit)
4138           (setq subject (gnus-simplify-subject-re subject))
4139           (if (> (length subject) gnus-summary-gather-subject-limit)
4140               (substring subject 0 gnus-summary-gather-subject-limit)
4141             subject))
4142          ;; Fuzzily simplify it.
4143          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4144           (gnus-simplify-subject-fuzzy subject))
4145          ;; Just remove the leading "Re:".
4146          (t
4147           (gnus-simplify-subject-re subject))))
4148
4149   (if (and gnus-summary-gather-exclude-subject
4150            (string-match gnus-summary-gather-exclude-subject subject))
4151       nil                         ; This article shouldn't be gathered
4152     subject))
4153
4154 (defun gnus-summary-simplify-subject-query ()
4155   "Query where the respool algorithm would put this article."
4156   (interactive)
4157   (gnus-summary-select-article)
4158   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4159
4160 (defun gnus-gather-threads-by-subject (threads)
4161   "Gather threads by looking at Subject headers."
4162   (if (not gnus-summary-make-false-root)
4163       threads
4164     (let ((hashtb (gnus-make-hashtable 1024))
4165           (prev threads)
4166           (result threads)
4167           subject hthread whole-subject)
4168       (while threads
4169         (setq subject (gnus-general-simplify-subject
4170                        (setq whole-subject (mail-header-subject
4171                                             (caar threads)))))
4172         (when subject
4173           (if (setq hthread (gnus-gethash subject hashtb))
4174               (progn
4175                 ;; We enter a dummy root into the thread, if we
4176                 ;; haven't done that already.
4177                 (unless (stringp (caar hthread))
4178                   (setcar hthread (list whole-subject (car hthread))))
4179                 ;; We add this new gathered thread to this gathered
4180                 ;; thread.
4181                 (setcdr (car hthread)
4182                         (nconc (cdar hthread) (list (car threads))))
4183                 ;; Remove it from the list of threads.
4184                 (setcdr prev (cdr threads))
4185                 (setq threads prev))
4186             ;; Enter this thread into the hash table.
4187             (gnus-sethash subject
4188                           (if gnus-summary-make-false-root-always
4189                               (progn
4190                                 ;; If you want a dummy root above all
4191                                 ;; threads...
4192                                 (setcar threads (list whole-subject
4193                                                       (car threads)))
4194                                 threads)
4195                             threads)
4196                           hashtb)))
4197         (setq prev threads)
4198         (setq threads (cdr threads)))
4199       result)))
4200
4201 (defun gnus-gather-threads-by-references (threads)
4202   "Gather threads by looking at References headers."
4203   (let ((idhashtb (gnus-make-hashtable 1024))
4204         (thhashtb (gnus-make-hashtable 1024))
4205         (prev threads)
4206         (result threads)
4207         ids references id gthread gid entered ref)
4208     (while threads
4209       (when (setq references (mail-header-references (caar threads)))
4210         (setq id (mail-header-id (caar threads))
4211               ids (inline (gnus-split-references references))
4212               entered nil)
4213         (while (setq ref (pop ids))
4214           (setq ids (delete ref ids))
4215           (if (not (setq gid (gnus-gethash ref idhashtb)))
4216               (progn
4217                 (gnus-sethash ref id idhashtb)
4218                 (gnus-sethash id threads thhashtb))
4219             (setq gthread (gnus-gethash gid thhashtb))
4220             (unless entered
4221               ;; We enter a dummy root into the thread, if we
4222               ;; haven't done that already.
4223               (unless (stringp (caar gthread))
4224                 (setcar gthread (list (mail-header-subject (caar gthread))
4225                                       (car gthread))))
4226               ;; We add this new gathered thread to this gathered
4227               ;; thread.
4228               (setcdr (car gthread)
4229                       (nconc (cdar gthread) (list (car threads)))))
4230             ;; Add it into the thread hash table.
4231             (gnus-sethash id gthread thhashtb)
4232             (setq entered t)
4233             ;; Remove it from the list of threads.
4234             (setcdr prev (cdr threads))
4235             (setq threads prev))))
4236       (setq prev threads)
4237       (setq threads (cdr threads)))
4238     result))
4239
4240 (defun gnus-sort-gathered-threads (threads)
4241   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4242   (let ((result threads))
4243     (while threads
4244       (when (stringp (caar threads))
4245         (setcdr (car threads)
4246                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4247       (setq threads (cdr threads)))
4248     result))
4249
4250 (defun gnus-thread-loop-p (root thread)
4251   "Say whether ROOT is in THREAD."
4252   (let ((stack (list thread))
4253         (infloop 0)
4254         th)
4255     (while (setq thread (pop stack))
4256       (setq th (cdr thread))
4257       (while (and th
4258                   (not (eq (caar th) root)))
4259         (pop th))
4260       (if th
4261           ;; We have found a loop.
4262           (let (ref-dep)
4263             (setcdr thread (delq (car th) (cdr thread)))
4264             (if (boundp (setq ref-dep (intern "none"
4265                                               gnus-newsgroup-dependencies)))
4266                 (setcdr (symbol-value ref-dep)
4267                         (nconc (cdr (symbol-value ref-dep))
4268                                (list (car th))))
4269               (set ref-dep (list nil (car th))))
4270             (setq infloop 1
4271                   stack nil))
4272         ;; Push all the subthreads onto the stack.
4273         (push (cdr thread) stack)))
4274     infloop))
4275
4276 (defun gnus-make-threads ()
4277   "Go through the dependency hashtb and find the roots.  Return all threads."
4278   (let (threads)
4279     (while (catch 'infloop
4280              (mapatoms
4281               (lambda (refs)
4282                 ;; Deal with self-referencing References loops.
4283                 (when (and (car (symbol-value refs))
4284                            (not (zerop
4285                                  (apply
4286                                   '+
4287                                   (mapcar
4288                                    (lambda (thread)
4289                                      (gnus-thread-loop-p
4290                                       (car (symbol-value refs)) thread))
4291                                    (cdr (symbol-value refs)))))))
4292                   (setq threads nil)
4293                   (throw 'infloop t))
4294                 (unless (car (symbol-value refs))
4295                   ;; These threads do not refer back to any other
4296                   ;; articles, so they're roots.
4297                   (setq threads (append (cdr (symbol-value refs)) threads))))
4298               gnus-newsgroup-dependencies)))
4299     threads))
4300
4301 ;; Build the thread tree.
4302 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4303   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4304
4305 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4306 if it was already present.
4307
4308 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4309 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4310 Message-IDs will be renamed to a unique Message-ID before being
4311 entered.
4312
4313 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4314   (let* ((id (mail-header-id header))
4315          (id-dep (and id (intern id dependencies)))
4316          parent-id ref ref-dep ref-header replaced)
4317     ;; Enter this `header' in the `dependencies' table.
4318     (cond
4319      ((not id-dep)
4320       (setq header nil))
4321      ;; The first two cases do the normal part: enter a new `header'
4322      ;; in the `dependencies' table.
4323      ((not (boundp id-dep))
4324       (set id-dep (list header)))
4325      ((null (car (symbol-value id-dep)))
4326       (setcar (symbol-value id-dep) header))
4327
4328      ;; From here the `header' was already present in the
4329      ;; `dependencies' table.
4330      (force-new
4331       ;; Overrides an existing entry;
4332       ;; just set the header part of the entry.
4333       (setcar (symbol-value id-dep) header)
4334       (setq replaced t))
4335
4336      ;; Renames the existing `header' to a unique Message-ID.
4337      ((not gnus-summary-ignore-duplicates)
4338       ;; An article with this Message-ID has already been seen.
4339       ;; We rename the Message-ID.
4340       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4341            (list header))
4342       (mail-header-set-id header id))
4343
4344      ;; The last case ignores an existing entry, except it adds any
4345      ;; additional Xrefs (in case the two articles came from different
4346      ;; servers.
4347      ;; Also sets `header' to `nil' meaning that the `dependencies'
4348      ;; table was *not* modified.
4349      (t
4350       (mail-header-set-xref
4351        (car (symbol-value id-dep))
4352        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4353                    "")
4354                (or (mail-header-xref header) "")))
4355       (setq header nil)))
4356
4357     (when (and header (not replaced))
4358       ;; First check that we are not creating a References loop.
4359       (setq parent-id (gnus-parent-id (mail-header-references header)))
4360       (setq ref parent-id)
4361       (while (and ref
4362                   (setq ref-dep (intern-soft ref dependencies))
4363                   (boundp ref-dep)
4364                   (setq ref-header (car (symbol-value ref-dep))))
4365         (if (string= id ref)
4366             ;; Yuk!  This is a reference loop.  Make the article be a
4367             ;; root article.
4368             (progn
4369               (mail-header-set-references (car (symbol-value id-dep)) "none")
4370               (setq ref nil)
4371               (setq parent-id nil))
4372           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4373       (setq ref-dep (intern (or parent-id "none") dependencies))
4374       (if (boundp ref-dep)
4375           (setcdr (symbol-value ref-dep)
4376                   (nconc (cdr (symbol-value ref-dep))
4377                          (list (symbol-value id-dep))))
4378         (set ref-dep (list nil (symbol-value id-dep)))))
4379     header))
4380
4381 (defun gnus-extract-message-id-from-in-reply-to (string)
4382   (if (string-match "<[^>]+>" string)
4383       (substring string (match-beginning 0) (match-end 0))
4384     nil))
4385
4386 (defun gnus-build-sparse-threads ()
4387   (let ((headers gnus-newsgroup-headers)
4388         (mail-parse-charset gnus-newsgroup-charset)
4389         (gnus-summary-ignore-duplicates t)
4390         header references generation relations
4391         subject child end new-child date)
4392     ;; First we create an alist of generations/relations, where
4393     ;; generations is how much we trust the relation, and the relation
4394     ;; is parent/child.
4395     (gnus-message 7 "Making sparse threads...")
4396     (save-excursion
4397       (nnheader-set-temp-buffer " *gnus sparse threads*")
4398       (while (setq header (pop headers))
4399         (when (and (setq references (mail-header-references header))
4400                    (not (string= references "")))
4401           (insert references)
4402           (setq child (mail-header-id header)
4403                 subject (mail-header-subject header)
4404                 date (mail-header-date header)
4405                 generation 0)
4406           (while (search-backward ">" nil t)
4407             (setq end (1+ (point)))
4408             (when (search-backward "<" nil t)
4409               (setq new-child (buffer-substring (point) end))
4410               (push (list (incf generation)
4411                           child (setq child new-child)
4412                           subject date)
4413                     relations)))
4414           (when child
4415             (push (list (1+ generation) child nil subject) relations))
4416           (erase-buffer)))
4417       (kill-buffer (current-buffer)))
4418     ;; Sort over trustworthiness.
4419     (dolist (relation (sort relations 'car-less-than-car))
4420       (when (gnus-dependencies-add-header
4421              (make-full-mail-header
4422               gnus-reffed-article-number
4423               (nth 3 relation) "" (or (nth 4 relation) "")
4424               (nth 1 relation)
4425               (or (nth 2 relation) "") 0 0 "")
4426              gnus-newsgroup-dependencies nil)
4427         (push gnus-reffed-article-number gnus-newsgroup-limit)
4428         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4429         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4430               gnus-newsgroup-reads)
4431         (decf gnus-reffed-article-number)))
4432     (gnus-message 7 "Making sparse threads...done")))
4433
4434 (defun gnus-build-old-threads ()
4435   ;; Look at all the articles that refer back to old articles, and
4436   ;; fetch the headers for the articles that aren't there.  This will
4437   ;; build complete threads - if the roots haven't been expired by the
4438   ;; server, that is.
4439   (let ((mail-parse-charset gnus-newsgroup-charset)
4440         id heads)
4441     (mapatoms
4442      (lambda (refs)
4443        (when (not (car (symbol-value refs)))
4444          (setq heads (cdr (symbol-value refs)))
4445          (while heads
4446            (if (memq (mail-header-number (caar heads))
4447                      gnus-newsgroup-dormant)
4448                (setq heads (cdr heads))
4449              (setq id (symbol-name refs))
4450              (while (and (setq id (gnus-build-get-header id))
4451                          (not (car (gnus-id-to-thread id)))))
4452              (setq heads nil)))))
4453      gnus-newsgroup-dependencies)))
4454
4455 (defsubst gnus-remove-odd-characters (string)
4456   "Translate STRING into something that doesn't contain weird characters."
4457   (mm-subst-char-in-string
4458    ?\r ?\-
4459    (mm-subst-char-in-string ?\n ?\- string t) t))
4460
4461 ;; This function has to be called with point after the article number
4462 ;; on the beginning of the line.
4463 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4464   (let ((eol (point-at-eol))
4465         (buffer (current-buffer))
4466         header references in-reply-to)
4467
4468     ;; overview: [num subject from date id refs chars lines misc]
4469     (unwind-protect
4470         (let (x)
4471           (narrow-to-region (point) eol)
4472           (unless (eobp)
4473             (forward-char))
4474
4475           (setq header
4476                 (make-full-mail-header
4477                  number                 ; number
4478                  (condition-case ()     ; subject
4479                      (gnus-remove-odd-characters
4480                       (funcall gnus-decode-encoded-word-function
4481                                (setq x (nnheader-nov-field))))
4482                    (error x))
4483                  (condition-case ()     ; from
4484                      (gnus-remove-odd-characters
4485                       (funcall gnus-decode-encoded-address-function
4486                                (setq x (nnheader-nov-field))))
4487                    (error x))
4488                  (nnheader-nov-field)   ; date
4489                  (nnheader-nov-read-message-id number)  ; id
4490                  (setq references (nnheader-nov-field)) ; refs
4491                  (nnheader-nov-read-integer) ; chars
4492                  (nnheader-nov-read-integer) ; lines
4493                  (unless (eobp)
4494                    (if (looking-at "Xref: ")
4495                        (goto-char (match-end 0)))
4496                    (nnheader-nov-field)) ; Xref
4497                  (nnheader-nov-parse-extra)))) ; extra
4498
4499       (widen))
4500
4501     (when (and (string= references "")
4502                (setq in-reply-to (mail-header-extra header))
4503                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4504       (mail-header-set-references
4505        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4506
4507     (when gnus-alter-header-function
4508       (funcall gnus-alter-header-function header))
4509     (gnus-dependencies-add-header header dependencies force-new)))
4510
4511 (defun gnus-build-get-header (id)
4512   "Look through the buffer of NOV lines and find the header to ID.
4513 Enter this line into the dependencies hash table, and return
4514 the id of the parent article (if any)."
4515   (let ((deps gnus-newsgroup-dependencies)
4516         found header)
4517     (prog1
4518         (with-current-buffer nntp-server-buffer
4519           (let ((case-fold-search nil))
4520             (goto-char (point-min))
4521             (while (and (not found)
4522                         (search-forward id nil t))
4523               (beginning-of-line)
4524               (setq found (looking-at
4525                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4526                                    (regexp-quote id))))
4527               (or found (beginning-of-line 2)))
4528             (when found
4529               (beginning-of-line)
4530               (and
4531                (setq header (gnus-nov-parse-line
4532                              (read (current-buffer)) deps))
4533                (gnus-parent-id (mail-header-references header))))))
4534       (when header
4535         (let ((number (mail-header-number header)))
4536           (push number gnus-newsgroup-limit)
4537           (push header gnus-newsgroup-headers)
4538           (if (memq number gnus-newsgroup-unselected)
4539               (progn
4540                 (setq gnus-newsgroup-unreads
4541                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4542                                                number))
4543                 (setq gnus-newsgroup-unselected
4544                       (delq number gnus-newsgroup-unselected)))
4545             (push number gnus-newsgroup-ancient)))))))
4546
4547 (defun gnus-build-all-threads ()
4548   "Read all the headers."
4549   (let ((gnus-summary-ignore-duplicates t)
4550         (mail-parse-charset gnus-newsgroup-charset)
4551         (dependencies gnus-newsgroup-dependencies)
4552         header article)
4553     (with-current-buffer nntp-server-buffer
4554       (let ((case-fold-search nil))
4555         (goto-char (point-min))
4556         (while (not (eobp))
4557           (ignore-errors
4558             (setq article (read (current-buffer))
4559                   header (gnus-nov-parse-line article dependencies t)))
4560           (when header
4561             (with-current-buffer gnus-summary-buffer
4562               (push header gnus-newsgroup-headers)
4563               (if (memq (setq article (mail-header-number header))
4564                         gnus-newsgroup-unselected)
4565                   (progn
4566                     (setq gnus-newsgroup-unreads
4567                           (gnus-add-to-sorted-list
4568                            gnus-newsgroup-unreads article))
4569                     (setq gnus-newsgroup-unselected
4570                           (delq article gnus-newsgroup-unselected)))
4571                 (push article gnus-newsgroup-ancient)))
4572             (forward-line 1)))))))
4573
4574 (defun gnus-summary-update-article-line (article header)
4575   "Update the line for ARTICLE using HEADER."
4576   (let* ((id (mail-header-id header))
4577          (thread (gnus-id-to-thread id)))
4578     (unless thread
4579       (error "Article in no thread"))
4580     ;; Update the thread.
4581     (setcar thread header)
4582     (gnus-summary-goto-subject article)
4583     (let* ((datal (gnus-data-find-list article))
4584            (data (car datal))
4585            (inhibit-read-only t)
4586            (level (gnus-summary-thread-level)))
4587       (gnus-delete-line)
4588       (let ((inserted (- (point)
4589                          (progn
4590                            (gnus-summary-insert-line
4591                             header level nil
4592                             (memq article gnus-newsgroup-undownloaded)
4593                             (gnus-article-mark article)
4594                             (memq article gnus-newsgroup-replied)
4595                             (memq article gnus-newsgroup-expirable)
4596                             ;; Only insert the Subject string when it's different
4597                             ;; from the previous Subject string.
4598                             (if (and
4599                                  gnus-show-threads
4600                                  (gnus-subject-equal
4601                                   (condition-case ()
4602                                       (mail-header-subject
4603                                        (gnus-data-header
4604                                         (cadr
4605                                          (gnus-data-find-list
4606                                           article
4607                                           (gnus-data-list t)))))
4608                                     ;; Error on the side of excessive subjects.
4609                                     (error ""))
4610                                   (mail-header-subject header)))
4611                                 ""
4612                               (mail-header-subject header))
4613                             nil (cdr (assq article gnus-newsgroup-scored))
4614                             (memq article gnus-newsgroup-processable))
4615                            (point)))))
4616         (when (cdr datal)
4617           (gnus-data-update-list
4618            (cdr datal)
4619            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4620
4621 (defun gnus-summary-update-article (article &optional iheader)
4622   "Update ARTICLE in the summary buffer."
4623   (set-buffer gnus-summary-buffer)
4624   (let* ((header (gnus-summary-article-header article))
4625          (id (mail-header-id header))
4626          (data (gnus-data-find article))
4627          (thread (gnus-id-to-thread id))
4628          (references (mail-header-references header))
4629          (parent
4630           (gnus-id-to-thread
4631            (or (gnus-parent-id
4632                 (when (and references
4633                            (not (equal "" references)))
4634                   references))
4635                "none")))
4636          (inhibit-read-only t)
4637          (old (car thread)))
4638     (when thread
4639       (unless iheader
4640         (setcar thread nil)
4641         (when parent
4642           (delq thread parent)))
4643       (if (gnus-summary-insert-subject id header)
4644           ;; Set the (possibly) new article number in the data structure.
4645           (gnus-data-set-number data (gnus-id-to-article id))
4646         (setcar thread old)
4647         nil))))
4648
4649 (defun gnus-rebuild-thread (id &optional line)
4650   "Rebuild the thread containing ID.
4651 If LINE, insert the rebuilt thread starting on line LINE."
4652   (let ((inhibit-read-only t)
4653         old-pos current thread data)
4654     (if (not gnus-show-threads)
4655         (setq thread (list (car (gnus-id-to-thread id))))
4656       ;; Get the thread this article is part of.
4657       (setq thread (gnus-remove-thread id)))
4658     (setq old-pos (point-at-bol))
4659     (setq current (save-excursion
4660                     (and (re-search-backward "[\r\n]" nil t)
4661                          (gnus-summary-article-number))))
4662     ;; If this is a gathered thread, we have to go some re-gathering.
4663     (when (stringp (car thread))
4664       (let ((subject (car thread))
4665             roots thr)
4666         (setq thread (cdr thread))
4667         (while thread
4668           (unless (memq (setq thr (gnus-id-to-thread
4669                                    (gnus-root-id
4670                                     (mail-header-id (caar thread)))))
4671                         roots)
4672             (push thr roots))
4673           (setq thread (cdr thread)))
4674         ;; We now have all (unique) roots.
4675         (if (= (length roots) 1)
4676             ;; All the loose roots are now one solid root.
4677             (setq thread (car roots))
4678           (setq thread (cons subject (gnus-sort-threads roots))))))
4679     (let (threads)
4680       ;; We then insert this thread into the summary buffer.
4681       (when line
4682         (goto-char (point-min))
4683         (forward-line (1- line)))
4684       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4685         (if gnus-show-threads
4686             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4687           (gnus-summary-prepare-unthreaded thread))
4688         (setq data (nreverse gnus-newsgroup-data))
4689         (setq threads gnus-newsgroup-threads))
4690       ;; We splice the new data into the data structure.
4691       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4692       ;;!!! then we want to insert at the beginning of the buffer.
4693       ;;!!! That happens to be true with Gnus now, but that may
4694       ;;!!! change in the future.  Perhaps.
4695       (gnus-data-enter-list
4696        (if line nil current) data (- (point) old-pos))
4697       (setq gnus-newsgroup-threads
4698             (nconc threads gnus-newsgroup-threads))
4699       (gnus-data-compute-positions))))
4700
4701 (defun gnus-number-to-header (number)
4702   "Return the header for article NUMBER."
4703   (let ((headers gnus-newsgroup-headers))
4704     (while (and headers
4705                 (not (= number (mail-header-number (car headers)))))
4706       (pop headers))
4707     (when headers
4708       (car headers))))
4709
4710 (defun gnus-parent-headers (in-headers &optional generation)
4711   "Return the headers of the GENERATIONth parent of HEADERS."
4712   (unless generation
4713     (setq generation 1))
4714   (let ((parent t)
4715         (headers in-headers)
4716         references)
4717     (while (and parent
4718                 (not (zerop generation))
4719                 (setq references (mail-header-references headers)))
4720       (setq headers (if (and references
4721                              (setq parent (gnus-parent-id references)))
4722                         (car (gnus-id-to-thread parent))
4723                       nil))
4724       (decf generation))
4725     (and (not (eq headers in-headers))
4726          headers)))
4727
4728 (defun gnus-id-to-thread (id)
4729   "Return the (sub-)thread where ID appears."
4730   (gnus-gethash id gnus-newsgroup-dependencies))
4731
4732 (defun gnus-id-to-article (id)
4733   "Return the article number of ID."
4734   (let ((thread (gnus-id-to-thread id)))
4735     (when (and thread
4736                (car thread))
4737       (mail-header-number (car thread)))))
4738
4739 (defun gnus-id-to-header (id)
4740   "Return the article headers of ID."
4741   (car (gnus-id-to-thread id)))
4742
4743 (defun gnus-article-displayed-root-p (article)
4744   "Say whether ARTICLE is a root(ish) article."
4745   (let ((level (gnus-summary-thread-level article))
4746         (refs (mail-header-references  (gnus-summary-article-header article)))
4747         particle)
4748     (cond
4749      ((null level) nil)
4750      ((zerop level) t)
4751      ((null refs) t)
4752      ((null (gnus-parent-id refs)) t)
4753      ((and (= 1 level)
4754            (null (setq particle (gnus-id-to-article
4755                                  (gnus-parent-id refs))))
4756            (null (gnus-summary-thread-level particle)))))))
4757
4758 (defun gnus-root-id (id)
4759   "Return the id of the root of the thread where ID appears."
4760   (let (last-id prev)
4761     (while (and id (setq prev (car (gnus-id-to-thread id))))
4762       (setq last-id id
4763             id (gnus-parent-id (mail-header-references prev))))
4764     last-id))
4765
4766 (defun gnus-articles-in-thread (thread)
4767   "Return the list of articles in THREAD."
4768   (cons (mail-header-number (car thread))
4769         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4770
4771 (defun gnus-remove-thread (id &optional dont-remove)
4772   "Remove the thread that has ID in it."
4773   (let (headers thread last-id)
4774     ;; First go up in this thread until we find the root.
4775     (setq last-id (gnus-root-id id)
4776           headers (message-flatten-list (gnus-id-to-thread last-id)))
4777     ;; We have now found the real root of this thread.  It might have
4778     ;; been gathered into some loose thread, so we have to search
4779     ;; through the threads to find the thread we wanted.
4780     (let ((threads gnus-newsgroup-threads)
4781           sub)
4782       (while threads
4783         (setq sub (car threads))
4784         (if (stringp (car sub))
4785             ;; This is a gathered thread, so we look at the roots
4786             ;; below it to find whether this article is in this
4787             ;; gathered root.
4788             (progn
4789               (setq sub (cdr sub))
4790               (while sub
4791                 (when (member (caar sub) headers)
4792                   (setq thread (car threads)
4793                         threads nil
4794                         sub nil))
4795                 (setq sub (cdr sub))))
4796           ;; It's an ordinary thread, so we check it.
4797           (when (eq (car sub) (car headers))
4798             (setq thread sub
4799                   threads nil)))
4800         (setq threads (cdr threads)))
4801       ;; If this article is in no thread, then it's a root.
4802       (if thread
4803           (unless dont-remove
4804             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4805         (setq thread (gnus-id-to-thread last-id)))
4806       (when thread
4807         (prog1
4808             thread                      ; We return this thread.
4809           (unless dont-remove
4810             (if (stringp (car thread))
4811                 (progn
4812                   ;; If we use dummy roots, then we have to remove the
4813                   ;; dummy root as well.
4814                   (when (eq gnus-summary-make-false-root 'dummy)
4815                     ;; We go to the dummy root by going to
4816                     ;; the first sub-"thread", and then one line up.
4817                     (gnus-summary-goto-article
4818                      (mail-header-number (caadr thread)))
4819                     (forward-line -1)
4820                     (gnus-delete-line)
4821                     (gnus-data-compute-positions))
4822                   (setq thread (cdr thread))
4823                   (while thread
4824                     (gnus-remove-thread-1 (car thread))
4825                     (setq thread (cdr thread))))
4826               (gnus-remove-thread-1 thread))))))))
4827
4828 (defun gnus-remove-thread-1 (thread)
4829   "Remove the thread THREAD recursively."
4830   (let ((number (mail-header-number (pop thread)))
4831         d)
4832     (setq thread (reverse thread))
4833     (while thread
4834       (gnus-remove-thread-1 (pop thread)))
4835     (when (setq d (gnus-data-find number))
4836       (goto-char (gnus-data-pos d))
4837       (gnus-summary-show-thread)
4838       (gnus-data-remove
4839        number
4840        (- (point-at-bol)
4841           (prog1
4842               (1+ (point-at-eol))
4843             (gnus-delete-line)))))))
4844
4845 (defun gnus-sort-threads-recursive (threads func)
4846   (sort (mapcar (lambda (thread)
4847                   (cons (car thread)
4848                         (and (cdr thread)
4849                              (gnus-sort-threads-recursive (cdr thread) func))))
4850                 threads) func))
4851
4852 (defun gnus-sort-threads-loop (threads func)
4853   (let* ((superthread (cons nil threads))
4854          (stack (list (cons superthread threads)))
4855          remaining-threads thread)
4856     (while stack
4857       (setq remaining-threads (cdr (car stack)))
4858       (if remaining-threads
4859           (progn (setq thread (car remaining-threads))
4860                  (setcdr (car stack) (cdr remaining-threads))
4861                  (if (cdr thread)
4862                      (push (cons thread (cdr thread)) stack)))
4863         (setq thread (caar stack))
4864         (setcdr thread (sort (cdr thread) func))
4865         (pop stack)))
4866     (cdr superthread)))
4867
4868 (defun gnus-sort-threads (threads)
4869   "Sort THREADS."
4870   (if (not gnus-thread-sort-functions)
4871       threads
4872     (gnus-message 8 "Sorting threads...")
4873     (prog1
4874         (condition-case nil
4875             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4876               (gnus-sort-threads-recursive
4877                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4878           ;; Even after binding max-lisp-eval-depth, the recursive
4879           ;; sorter might fail for very long threads.  In that case,
4880           ;; try using a (less well-tested) non-recursive sorter.
4881           (error (gnus-message 9 "Sorting threads with loop...")
4882                  (gnus-sort-threads-loop
4883                   threads (gnus-make-sort-function
4884                            gnus-thread-sort-functions))))
4885       (gnus-message 8 "Sorting threads...done"))))
4886
4887 (defun gnus-sort-articles (articles)
4888   "Sort ARTICLES."
4889   (when gnus-article-sort-functions
4890     (gnus-message 7 "Sorting articles...")
4891     (prog1
4892         (setq gnus-newsgroup-headers
4893               (sort articles (gnus-make-sort-function
4894                               gnus-article-sort-functions)))
4895       (gnus-message 7 "Sorting articles...done"))))
4896
4897 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4898 (defmacro gnus-thread-header (thread)
4899   "Return header of first article in THREAD.
4900 Note that THREAD must never, ever be anything else than a variable -
4901 using some other form will lead to serious barfage."
4902   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4903   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4904   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4905         (vector thread) 2))
4906
4907 (defsubst gnus-article-sort-by-number (h1 h2)
4908   "Sort articles by article number."
4909   (< (mail-header-number h1)
4910      (mail-header-number h2)))
4911
4912 (defun gnus-thread-sort-by-number (h1 h2)
4913   "Sort threads by root article number."
4914   (gnus-article-sort-by-number
4915    (gnus-thread-header h1) (gnus-thread-header h2)))
4916
4917 (defsubst gnus-article-sort-by-random (h1 h2)
4918   "Sort articles randomly."
4919   (zerop (random 2)))
4920
4921 (defun gnus-thread-sort-by-random (h1 h2)
4922   "Sort threads randomly."
4923   (gnus-article-sort-by-random
4924    (gnus-thread-header h1) (gnus-thread-header h2)))
4925
4926 (defsubst gnus-article-sort-by-lines (h1 h2)
4927   "Sort articles by article Lines header."
4928   (< (mail-header-lines h1)
4929      (mail-header-lines h2)))
4930
4931 (defun gnus-thread-sort-by-lines (h1 h2)
4932   "Sort threads by root article Lines header."
4933   (gnus-article-sort-by-lines
4934    (gnus-thread-header h1) (gnus-thread-header h2)))
4935
4936 (defsubst gnus-article-sort-by-chars (h1 h2)
4937   "Sort articles by octet length."
4938   (< (mail-header-chars h1)
4939      (mail-header-chars h2)))
4940
4941 (defun gnus-thread-sort-by-chars (h1 h2)
4942   "Sort threads by root article octet length."
4943   (gnus-article-sort-by-chars
4944    (gnus-thread-header h1) (gnus-thread-header h2)))
4945
4946 (defsubst gnus-article-sort-by-author (h1 h2)
4947   "Sort articles by root author."
4948   (gnus-string<
4949    (let ((extract (funcall
4950                    gnus-extract-address-components
4951                    (mail-header-from h1))))
4952      (or (car extract) (cadr extract) ""))
4953    (let ((extract (funcall
4954                    gnus-extract-address-components
4955                    (mail-header-from h2))))
4956      (or (car extract) (cadr extract) ""))))
4957
4958 (defun gnus-thread-sort-by-author (h1 h2)
4959   "Sort threads by root author."
4960   (gnus-article-sort-by-author
4961    (gnus-thread-header h1)  (gnus-thread-header h2)))
4962
4963 (defsubst gnus-article-sort-by-recipient (h1 h2)
4964   "Sort articles by recipient."
4965   (gnus-string<
4966    (let ((extract (funcall
4967                    gnus-extract-address-components
4968                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4969      (or (car extract) (cadr extract)))
4970    (let ((extract (funcall
4971                    gnus-extract-address-components
4972                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4973      (or (car extract) (cadr extract)))))
4974
4975 (defun gnus-thread-sort-by-recipient (h1 h2)
4976   "Sort threads by root recipient."
4977   (gnus-article-sort-by-recipient
4978    (gnus-thread-header h1) (gnus-thread-header h2)))
4979
4980 (defsubst gnus-article-sort-by-subject (h1 h2)
4981   "Sort articles by root subject."
4982   (gnus-string<
4983    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4984    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4985
4986 (defun gnus-thread-sort-by-subject (h1 h2)
4987   "Sort threads by root subject."
4988   (gnus-article-sort-by-subject
4989    (gnus-thread-header h1) (gnus-thread-header h2)))
4990
4991 (defsubst gnus-article-sort-by-date (h1 h2)
4992   "Sort articles by root article date."
4993   (time-less-p
4994    (gnus-date-get-time (mail-header-date h1))
4995    (gnus-date-get-time (mail-header-date h2))))
4996
4997 (defun gnus-thread-sort-by-date (h1 h2)
4998   "Sort threads by root article date."
4999   (gnus-article-sort-by-date
5000    (gnus-thread-header h1) (gnus-thread-header h2)))
5001
5002 (defsubst gnus-article-sort-by-score (h1 h2)
5003   "Sort articles by root article score.
5004 Unscored articles will be counted as having a score of zero."
5005   (> (or (cdr (assq (mail-header-number h1)
5006                     gnus-newsgroup-scored))
5007          gnus-summary-default-score 0)
5008      (or (cdr (assq (mail-header-number h2)
5009                     gnus-newsgroup-scored))
5010          gnus-summary-default-score 0)))
5011
5012 (defun gnus-thread-sort-by-score (h1 h2)
5013   "Sort threads by root article score."
5014   (gnus-article-sort-by-score
5015    (gnus-thread-header h1) (gnus-thread-header h2)))
5016
5017 (defun gnus-thread-sort-by-total-score (h1 h2)
5018   "Sort threads by the sum of all scores in the thread.
5019 Unscored articles will be counted as having a score of zero."
5020   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5021
5022 (defun gnus-thread-total-score (thread)
5023   ;; This function find the total score of THREAD.
5024   (cond
5025    ((null thread)
5026     0)
5027    ((consp thread)
5028     (if (stringp (car thread))
5029         (apply gnus-thread-score-function 0
5030                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5031       (gnus-thread-total-score-1 thread)))
5032    (t
5033     (gnus-thread-total-score-1 (list thread)))))
5034
5035 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5036   "Sort articles by number."
5037   (gnus-article-sort-by-number h1 h2))
5038
5039 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5040   "Sort threads such that the thread with the most recently arrived article comes first."
5041   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5042
5043 (defun gnus-thread-highest-number (thread)
5044   "Return the highest article number in THREAD."
5045   (apply 'max (mapcar (lambda (header)
5046                         (mail-header-number header))
5047                       (message-flatten-list thread))))
5048
5049 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5050   "Sort articles by number."
5051   (gnus-article-sort-by-date h1 h2))
5052
5053 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5054   "Sort threads such that the thread with the most recently dated article comes first."
5055   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5056
5057 ; Since this is called not only to sort the top-level threads, but
5058 ; also in recursive sorts to order the articles within a thread, each
5059 ; article will be processed many times.  Thus it speeds things up
5060 ; quite a bit to use gnus-date-get-time, which caches the time value.
5061 (defun gnus-thread-latest-date (thread)
5062   "Return the highest article date in THREAD."
5063   (apply 'max
5064          (mapcar (lambda (header) (gnus-float-time
5065                                    (gnus-date-get-time
5066                                     (mail-header-date header))))
5067                  (message-flatten-list thread))))
5068
5069 (defun gnus-thread-total-score-1 (root)
5070   ;; This function find the total score of the thread below ROOT.
5071   (setq root (car root))
5072   (apply gnus-thread-score-function
5073          (or (append
5074               (mapcar 'gnus-thread-total-score
5075                       (cdr (gnus-id-to-thread (mail-header-id root))))
5076               (when (> (mail-header-number root) 0)
5077                 (list (or (cdr (assq (mail-header-number root)
5078                                      gnus-newsgroup-scored))
5079                           gnus-summary-default-score 0))))
5080              (list gnus-summary-default-score)
5081              '(0))))
5082
5083 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5084 (defvar gnus-tmp-prev-subject nil)
5085 (defvar gnus-tmp-false-parent nil)
5086 (defvar gnus-tmp-root-expunged nil)
5087 (defvar gnus-tmp-dummy-line nil)
5088
5089 (defun gnus-extra-header (type &optional header)
5090   "Return the extra header of TYPE."
5091   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5092       ""))
5093
5094 (defvar gnus-tmp-thread-tree-header-string "")
5095
5096 (defcustom gnus-sum-thread-tree-root "> "
5097   "With %B spec, used for the root of a thread.
5098 If nil, use subject instead."
5099   :version "22.1"
5100   :type '(radio (const :format "%v  " nil) string)
5101   :group 'gnus-thread)
5102
5103 (defcustom gnus-sum-thread-tree-false-root "> "
5104   "With %B spec, used for a false root of a thread.
5105 If nil, use subject instead."
5106   :version "22.1"
5107   :type '(radio (const :format "%v  " nil) string)
5108   :group 'gnus-thread)
5109
5110 (defcustom gnus-sum-thread-tree-single-indent ""
5111   "With %B spec, used for a thread with just one message.
5112 If nil, use subject instead."
5113   :version "22.1"
5114   :type '(radio (const :format "%v  " nil) string)
5115   :group 'gnus-thread)
5116
5117 (defcustom gnus-sum-thread-tree-vertical "| "
5118   "With %B spec, used for drawing a vertical line."
5119   :version "22.1"
5120   :type 'string
5121   :group 'gnus-thread)
5122
5123 (defcustom gnus-sum-thread-tree-indent "  "
5124   "With %B spec, used for indenting."
5125   :version "22.1"
5126   :type 'string
5127   :group 'gnus-thread)
5128
5129 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5130   "With %B spec, used for a leaf with brothers."
5131   :version "22.1"
5132   :type 'string
5133   :group 'gnus-thread)
5134
5135 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5136   "With %B spec, used for a leaf without brothers."
5137   :version "22.1"
5138   :type 'string
5139   :group 'gnus-thread)
5140
5141 (defcustom gnus-summary-display-while-building nil
5142   "If non-nil, show and update the summary buffer as it's being built.
5143 If the value is t, update the buffer after every line is inserted.  If
5144 the value is an integer (N), update the display every N lines."
5145   :version "22.1"
5146   :group 'gnus-thread
5147   :type '(choice (const :tag "off" nil)
5148                  number
5149                  (const :tag "frequently" t)))
5150
5151 (defun gnus-summary-prepare-threads (threads)
5152   "Prepare summary buffer from THREADS and indentation LEVEL.
5153 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5154 or a straight list of headers."
5155   (gnus-message 7 "Generating summary...")
5156
5157   (setq gnus-newsgroup-threads threads)
5158   (beginning-of-line)
5159
5160   (let ((gnus-tmp-level 0)
5161         (default-score (or gnus-summary-default-score 0))
5162         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5163         (building-line-count gnus-summary-display-while-building)
5164         (building-count (integerp gnus-summary-display-while-building))
5165         thread number subject stack state gnus-tmp-gathered beg-match
5166         new-roots gnus-tmp-new-adopts thread-end simp-subject
5167         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5168         gnus-tmp-replied gnus-tmp-subject-or-nil
5169         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5170         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5171         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5172         tree-stack)
5173
5174     (setq gnus-tmp-prev-subject nil
5175           gnus-tmp-thread-tree-header-string "")
5176
5177     (if (vectorp (car threads))
5178         ;; If this is a straight (sic) list of headers, then a
5179         ;; threaded summary display isn't required, so we just create
5180         ;; an unthreaded one.
5181         (gnus-summary-prepare-unthreaded threads)
5182
5183       ;; Do the threaded display.
5184
5185       (if gnus-summary-display-while-building
5186           (switch-to-buffer (buffer-name)))
5187       (while (or threads stack gnus-tmp-new-adopts new-roots)
5188
5189         (if (and (= gnus-tmp-level 0)
5190                  (or (not stack)
5191                      (= (caar stack) 0))
5192                  (not gnus-tmp-false-parent)
5193                  (or gnus-tmp-new-adopts new-roots))
5194             (if gnus-tmp-new-adopts
5195                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5196                       thread (list (car gnus-tmp-new-adopts))
5197                       gnus-tmp-header (caar thread)
5198                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5199               (when new-roots
5200                 (setq thread (list (car new-roots))
5201                       gnus-tmp-header (caar thread)
5202                       new-roots (cdr new-roots))))
5203
5204           (if threads
5205               ;; If there are some threads, we do them before the
5206               ;; threads on the stack.
5207               (setq thread threads
5208                     gnus-tmp-header (caar thread))
5209             ;; There were no current threads, so we pop something off
5210             ;; the stack.
5211             (setq state (car stack)
5212                   gnus-tmp-level (car state)
5213                   tree-stack (cadr state)
5214                   thread (caddr state)
5215                   stack (cdr stack)
5216                   gnus-tmp-header (caar thread))))
5217
5218         (setq gnus-tmp-false-parent nil)
5219         (setq gnus-tmp-root-expunged nil)
5220         (setq thread-end nil)
5221
5222         (if (stringp gnus-tmp-header)
5223             ;; The header is a dummy root.
5224             (cond
5225              ((eq gnus-summary-make-false-root 'adopt)
5226               ;; We let the first article adopt the rest.
5227               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5228                                                (cddar thread)))
5229               (setq gnus-tmp-gathered
5230                     (nconc (mapcar
5231                             (lambda (h) (mail-header-number (car h)))
5232                             (cddar thread))
5233                            gnus-tmp-gathered))
5234               (setq thread (cons (list (caar thread)
5235                                        (cadar thread))
5236                                  (cdr thread)))
5237               (setq gnus-tmp-level -1
5238                     gnus-tmp-false-parent t))
5239              ((eq gnus-summary-make-false-root 'empty)
5240               ;; We print adopted articles with empty subject fields.
5241               (setq gnus-tmp-gathered
5242                     (nconc (mapcar
5243                             (lambda (h) (mail-header-number (car h)))
5244                             (cddar thread))
5245                            gnus-tmp-gathered))
5246               (setq gnus-tmp-level -1))
5247              ((eq gnus-summary-make-false-root 'dummy)
5248               ;; We remember that we probably want to output a dummy
5249               ;; root.
5250               (setq gnus-tmp-dummy-line gnus-tmp-header)
5251               (setq gnus-tmp-prev-subject gnus-tmp-header))
5252              (t
5253               ;; We do not make a root for the gathered
5254               ;; sub-threads at all.
5255               (setq gnus-tmp-level -1)))
5256
5257           (setq number (mail-header-number gnus-tmp-header)
5258                 subject (mail-header-subject gnus-tmp-header)
5259                 simp-subject (gnus-simplify-subject-fully subject))
5260
5261           (cond
5262            ;; If the thread has changed subject, we might want to make
5263            ;; this subthread into a root.
5264            ((and (null gnus-thread-ignore-subject)
5265                  (not (zerop gnus-tmp-level))
5266                  gnus-tmp-prev-subject
5267                  (not (string= gnus-tmp-prev-subject simp-subject)))
5268             (setq new-roots (nconc new-roots (list (car thread)))
5269                   thread-end t
5270                   gnus-tmp-header nil))
5271            ;; If the article lies outside the current limit,
5272            ;; then we do not display it.
5273            ((not (memq number gnus-newsgroup-limit))
5274             (setq gnus-tmp-gathered
5275                   (nconc (mapcar
5276                           (lambda (h) (mail-header-number (car h)))
5277                           (cdar thread))
5278                          gnus-tmp-gathered))
5279             (setq gnus-tmp-new-adopts (if (cdar thread)
5280                                           (append gnus-tmp-new-adopts
5281                                                   (cdar thread))
5282                                         gnus-tmp-new-adopts)
5283                   thread-end t
5284                   gnus-tmp-header nil)
5285             (when (zerop gnus-tmp-level)
5286               (setq gnus-tmp-root-expunged t)))
5287            ;; Perhaps this article is to be marked as read?
5288            ((and gnus-summary-mark-below
5289                  (< (or (cdr (assq number gnus-newsgroup-scored))
5290                         default-score)
5291                     gnus-summary-mark-below)
5292                  ;; Don't touch sparse articles.
5293                  (not (gnus-summary-article-sparse-p number))
5294                  (not (gnus-summary-article-ancient-p number)))
5295             (setq gnus-newsgroup-unreads
5296                   (delq number gnus-newsgroup-unreads))
5297             (if gnus-newsgroup-auto-expire
5298                 (setq gnus-newsgroup-expirable
5299                       (gnus-add-to-sorted-list
5300                        gnus-newsgroup-expirable number))
5301               (push (cons number gnus-low-score-mark)
5302                     gnus-newsgroup-reads))))
5303
5304           (when gnus-tmp-header
5305             ;; We may have an old dummy line to output before this
5306             ;; article.
5307             (when (and gnus-tmp-dummy-line
5308                        (gnus-subject-equal
5309                         gnus-tmp-dummy-line
5310                         (mail-header-subject gnus-tmp-header)))
5311               (gnus-summary-insert-dummy-line
5312                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5313               (setq gnus-tmp-dummy-line nil))
5314
5315             ;; Compute the mark.
5316             (setq gnus-tmp-unread (gnus-article-mark number))
5317
5318             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5319                                   gnus-tmp-header gnus-tmp-level)
5320                   gnus-newsgroup-data)
5321
5322             ;; Actually insert the line.
5323             (setq
5324              gnus-tmp-subject-or-nil
5325              (cond
5326               ((and gnus-thread-ignore-subject
5327                     gnus-tmp-prev-subject
5328                     (not (string= gnus-tmp-prev-subject simp-subject)))
5329                subject)
5330               ((zerop gnus-tmp-level)
5331                (if (and (eq gnus-summary-make-false-root 'empty)
5332                         (memq number gnus-tmp-gathered)
5333                         gnus-tmp-prev-subject
5334                         (string= gnus-tmp-prev-subject simp-subject))
5335                    gnus-summary-same-subject
5336                  subject))
5337               (t gnus-summary-same-subject)))
5338             (if (and (eq gnus-summary-make-false-root 'adopt)
5339                      (= gnus-tmp-level 1)
5340                      (memq number gnus-tmp-gathered))
5341                 (setq gnus-tmp-opening-bracket ?\<
5342                       gnus-tmp-closing-bracket ?\>)
5343               (setq gnus-tmp-opening-bracket ?\[
5344                     gnus-tmp-closing-bracket ?\]))
5345             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5346                 (gnus-make-thread-indent-array
5347                  (max (* 2 (length gnus-thread-indent-array))
5348                       gnus-tmp-level)))
5349             (setq
5350              gnus-tmp-indentation
5351              (aref gnus-thread-indent-array gnus-tmp-level)
5352              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5353              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5354                                 gnus-summary-default-score 0)
5355              gnus-tmp-score-char
5356              (if (or (null gnus-summary-default-score)
5357                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5358                          gnus-summary-zcore-fuzz))
5359                  ?                      ;Whitespace
5360                (if (< gnus-tmp-score gnus-summary-default-score)
5361                    gnus-score-below-mark gnus-score-over-mark))
5362              gnus-tmp-replied
5363              (cond ((memq number gnus-newsgroup-processable)
5364                     gnus-process-mark)
5365                    ((memq number gnus-newsgroup-cached)
5366                     gnus-cached-mark)
5367                    ((memq number gnus-newsgroup-replied)
5368                     gnus-replied-mark)
5369                    ((memq number gnus-newsgroup-forwarded)
5370                     gnus-forwarded-mark)
5371                    ((memq number gnus-newsgroup-saved)
5372                     gnus-saved-mark)
5373                    ((memq number gnus-newsgroup-unseen)
5374                     gnus-unseen-mark)
5375                    (t gnus-no-mark))
5376              gnus-tmp-downloaded
5377              (cond ((memq number gnus-newsgroup-undownloaded)
5378                     gnus-undownloaded-mark)
5379                    (gnus-newsgroup-agentized
5380                     gnus-downloaded-mark)
5381                    (t
5382                     gnus-no-mark))
5383              gnus-tmp-from (mail-header-from gnus-tmp-header)
5384              gnus-tmp-name
5385              (cond
5386               ((string-match "<[^>]+> *$" gnus-tmp-from)
5387                (setq beg-match (match-beginning 0))
5388                (or (and (string-match "^\".+\"" gnus-tmp-from)
5389                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5390                    (substring gnus-tmp-from 0 beg-match)))
5391               ((string-match "(.+)" gnus-tmp-from)
5392                (substring gnus-tmp-from
5393                           (1+ (match-beginning 0)) (1- (match-end 0))))
5394               (t gnus-tmp-from))
5395
5396              ;; Do the %B string
5397              gnus-tmp-thread-tree-header-string
5398              (cond
5399               ((not gnus-show-threads) "")
5400               ((zerop gnus-tmp-level)
5401                (cond ((cdar thread)
5402                       (or gnus-sum-thread-tree-root subject))
5403                      (gnus-tmp-new-adopts
5404                       (or gnus-sum-thread-tree-false-root subject))
5405                      (t
5406                       (or gnus-sum-thread-tree-single-indent subject))))
5407               (t
5408                (concat (apply 'concat
5409                               (mapcar (lambda (item)
5410                                         (if (= item 1)
5411                                             gnus-sum-thread-tree-vertical
5412                                           gnus-sum-thread-tree-indent))
5413                                       (cdr (reverse tree-stack))))
5414                        (if (nth 1 thread)
5415                            gnus-sum-thread-tree-leaf-with-other
5416                          gnus-sum-thread-tree-single-leaf)))))
5417             (when (string= gnus-tmp-name "")
5418               (setq gnus-tmp-name gnus-tmp-from))
5419             (unless (numberp gnus-tmp-lines)
5420               (setq gnus-tmp-lines -1))
5421             (if (= gnus-tmp-lines -1)
5422                 (setq gnus-tmp-lines "?")
5423               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5424             (gnus-put-text-property
5425              (point)
5426              (progn (eval gnus-summary-line-format-spec) (point))
5427              'gnus-number number)
5428             (when gnus-visual-p
5429               (forward-line -1)
5430               (gnus-summary-highlight-line)
5431               (when gnus-summary-update-hook
5432                 (gnus-run-hooks 'gnus-summary-update-hook))
5433               (forward-line 1))
5434
5435             (setq gnus-tmp-prev-subject simp-subject)))
5436
5437         (when (nth 1 thread)
5438           (push (list (max 0 gnus-tmp-level)
5439                       (copy-sequence tree-stack)
5440                       (nthcdr 1 thread))
5441                 stack))
5442         (push (if (nth 1 thread) 1 0) tree-stack)
5443         (incf gnus-tmp-level)
5444         (setq threads (if thread-end nil (cdar thread)))
5445         (if gnus-summary-display-while-building
5446             (if building-count
5447                 (progn
5448                   ;; use a set frequency
5449                   (setq building-line-count (1- building-line-count))
5450                   (when (= building-line-count 0)
5451                     (sit-for 0)
5452                     (setq building-line-count
5453                           gnus-summary-display-while-building)))
5454               ;; always
5455               (sit-for 0)))
5456         (unless threads
5457           (setq gnus-tmp-level 0)))))
5458   (gnus-message 7 "Generating summary...done"))
5459
5460 (defun gnus-summary-prepare-unthreaded (headers)
5461   "Generate an unthreaded summary buffer based on HEADERS."
5462   (let (header number mark)
5463
5464     (beginning-of-line)
5465
5466     (while headers
5467       ;; We may have to root out some bad articles...
5468       (when (memq (setq number (mail-header-number
5469                                 (setq header (pop headers))))
5470                   gnus-newsgroup-limit)
5471         ;; Mark article as read when it has a low score.
5472         (when (and gnus-summary-mark-below
5473                    (< (or (cdr (assq number gnus-newsgroup-scored))
5474                           gnus-summary-default-score 0)
5475                       gnus-summary-mark-below)
5476                    (not (gnus-summary-article-ancient-p number)))
5477           (setq gnus-newsgroup-unreads
5478                 (delq number gnus-newsgroup-unreads))
5479           (if gnus-newsgroup-auto-expire
5480               (push number gnus-newsgroup-expirable)
5481             (push (cons number gnus-low-score-mark)
5482                   gnus-newsgroup-reads)))
5483
5484         (setq mark (gnus-article-mark number))
5485         (push (gnus-data-make number mark (1+ (point)) header 0)
5486               gnus-newsgroup-data)
5487         (gnus-summary-insert-line
5488          header 0 number
5489          (memq number gnus-newsgroup-undownloaded)
5490          mark (memq number gnus-newsgroup-replied)
5491          (memq number gnus-newsgroup-expirable)
5492          (mail-header-subject header) nil
5493          (cdr (assq number gnus-newsgroup-scored))
5494          (memq number gnus-newsgroup-processable))))))
5495
5496 (defun gnus-group-get-list-identifiers (group)
5497   "Get list identifier regexp for GROUP."
5498   (or (gnus-parameter-list-identifier group)
5499       (if (consp gnus-list-identifiers)
5500           (mapconcat 'identity gnus-list-identifiers " *\\|")
5501         gnus-list-identifiers)))
5502
5503 (defun gnus-summary-remove-list-identifiers ()
5504   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5505   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5506         changed subject)
5507     (when regexp
5508       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5509       (dolist (header gnus-newsgroup-headers)
5510         (setq subject (mail-header-subject header)
5511               changed nil)
5512         (while (string-match regexp subject)
5513           (setq subject
5514                 (concat (substring subject 0 (match-beginning 1))
5515                         (substring subject (match-end 0)))
5516                 changed t))
5517         (when changed
5518           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5519             (setq subject
5520                   (concat (substring subject 0 (match-beginning 1))
5521                           (substring subject (match-end 1)))))
5522           (mail-header-set-subject header subject))))))
5523
5524 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5525   "Fetch headers of ARTICLES."
5526   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5527     (gnus-message 7 "Fetching headers for %s..." name)
5528     (prog1
5529         (if (eq 'nov
5530                 (setq gnus-headers-retrieved-by
5531                       (gnus-retrieve-headers
5532                        articles gnus-newsgroup-name
5533                        (or limit
5534                            ;; We might want to fetch old headers, but
5535                            ;; not if there is only 1 article.
5536                            (and (or (and
5537                                      (not (eq gnus-fetch-old-headers 'some))
5538                                      (not (numberp gnus-fetch-old-headers)))
5539                                     (> (length articles) 1))
5540                                 gnus-fetch-old-headers)))))
5541             (gnus-get-newsgroup-headers-xover
5542              articles force-new dependencies gnus-newsgroup-name t)
5543           (gnus-get-newsgroup-headers dependencies force-new))
5544       (gnus-message 7 "Fetching headers for %s...done" name))))
5545
5546 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5547   "Select newsgroup GROUP.
5548 If READ-ALL is non-nil, all articles in the group are selected.
5549 If SELECT-ARTICLES, only select those articles from GROUP."
5550   (let* ((entry (gnus-group-entry group))
5551          ;;!!! Dirty hack; should be removed.
5552          (gnus-summary-ignore-duplicates
5553           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5554               t
5555             gnus-summary-ignore-duplicates))
5556          (info (nth 2 entry))
5557          charset articles fetched-articles cached)
5558
5559     (unless (gnus-check-server
5560              (set (make-local-variable 'gnus-current-select-method)
5561                   (gnus-find-method-for-group group)))
5562       (error "Couldn't open server"))
5563     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5564
5565     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5566         (gnus-activate-group group)     ; Or we can activate it...
5567         (progn                          ; Or we bug out.
5568           (when (equal major-mode 'gnus-summary-mode)
5569             (gnus-kill-buffer (current-buffer)))
5570           (error
5571            "Couldn't activate group %s: %s"
5572            (mm-decode-coding-string group charset)
5573            (mm-decode-coding-string (gnus-status-message group) charset))))
5574
5575     (unless (gnus-request-group group t)
5576       (when (equal major-mode 'gnus-summary-mode)
5577         (gnus-kill-buffer (current-buffer)))
5578       (error "Couldn't request group %s: %s"
5579              (mm-decode-coding-string group charset)
5580              (mm-decode-coding-string (gnus-status-message group) charset)))
5581
5582     (when (and gnus-agent
5583                (gnus-active group))
5584       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5585
5586       (setq gnus-summary-use-undownloaded-faces
5587             (gnus-agent-find-parameter
5588              group
5589              'agent-enable-undownloaded-faces)))
5590
5591     (setq gnus-newsgroup-name group
5592           gnus-newsgroup-unselected nil
5593           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5594
5595     (let ((display (gnus-group-find-parameter group 'display)))
5596       (setq gnus-newsgroup-display
5597             (cond
5598              ((not (zerop (or (car-safe read-all) 0)))
5599               ;; The user entered the group with C-u SPC/RET, let's show
5600               ;; all articles.
5601               'gnus-not-ignore)
5602              ((eq display 'all)
5603               'gnus-not-ignore)
5604              ((arrayp display)
5605               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5606              ((numberp display)
5607               ;; The following is probably the "correct" solution, but
5608               ;; it makes Gnus fetch all headers and then limit the
5609               ;; articles (which is slow), so instead we hack the
5610               ;; select-articles parameter instead. -- Simon Josefsson
5611               ;; <jas@kth.se>
5612               ;;
5613               ;; (gnus-byte-compile
5614               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5615               ;;                         display)))))
5616               (setq select-articles
5617                     (gnus-uncompress-range
5618                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5619                              (if (> tmp 0)
5620                                  tmp
5621                                1))
5622                            (cdr (gnus-active group)))))
5623               nil)
5624              (t
5625               nil))))
5626
5627     (gnus-summary-setup-default-charset)
5628
5629     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5630     (when (gnus-virtual-group-p group)
5631       (setq cached gnus-newsgroup-cached))
5632
5633     (setq gnus-newsgroup-unreads
5634           (gnus-sorted-ndifference
5635            (gnus-sorted-ndifference gnus-newsgroup-unreads
5636                                     gnus-newsgroup-marked)
5637            gnus-newsgroup-dormant))
5638
5639     (setq gnus-newsgroup-processable nil)
5640
5641     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5642
5643     ;; Adjust and set lists of article marks.
5644     (when info
5645       (gnus-adjust-marked-articles info))
5646     (if (setq articles select-articles)
5647         (setq gnus-newsgroup-unselected
5648               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5649       (setq articles (gnus-articles-to-read group read-all)))
5650
5651     (cond
5652      ((null articles)
5653       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5654       'quit)
5655      ((eq articles 0) nil)
5656      (t
5657       ;; Init the dependencies hash table.
5658       (setq gnus-newsgroup-dependencies
5659             (gnus-make-hashtable (length articles)))
5660       (if (gnus-buffer-live-p gnus-group-buffer)
5661           (gnus-set-global-variables)
5662         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5663       ;; Retrieve the headers and read them in.
5664
5665       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5666
5667       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5668       (when cached
5669         (setq gnus-newsgroup-cached cached))
5670
5671       ;; Suppress duplicates?
5672       (when gnus-suppress-duplicates
5673         (gnus-dup-suppress-articles))
5674
5675       ;; Set the initial limit.
5676       (setq gnus-newsgroup-limit (copy-sequence articles))
5677       ;; Remove canceled articles from the list of unread articles.
5678       (setq fetched-articles
5679             (mapcar (lambda (headers) (mail-header-number headers))
5680                     gnus-newsgroup-headers))
5681       (setq gnus-newsgroup-articles fetched-articles)
5682       (setq gnus-newsgroup-unreads
5683             (gnus-sorted-nintersection
5684              gnus-newsgroup-unreads fetched-articles))
5685       (gnus-compute-unseen-list)
5686
5687       ;; Removed marked articles that do not exist.
5688       (gnus-update-missing-marks
5689        (gnus-sorted-difference articles fetched-articles))
5690       ;; We might want to build some more threads first.
5691       (when (and gnus-fetch-old-headers
5692                  (eq gnus-headers-retrieved-by 'nov))
5693         (if (eq gnus-fetch-old-headers 'invisible)
5694             (gnus-build-all-threads)
5695           (gnus-build-old-threads)))
5696       ;; Let the Gnus agent mark articles as read.
5697       (when gnus-agent
5698         (gnus-agent-get-undownloaded-list))
5699       ;; Remove list identifiers from subject
5700       (gnus-summary-remove-list-identifiers)
5701       ;; Check whether auto-expire is to be done in this group.
5702       (setq gnus-newsgroup-auto-expire
5703             (and (gnus-group-auto-expirable-p group)
5704                  (not (gnus-group-read-only-p group))))
5705       ;; Set up the article buffer now, if necessary.
5706       (unless (and gnus-single-article-buffer
5707                    (equal gnus-article-buffer "*Article*"))
5708         (gnus-article-setup-buffer))
5709       ;; First and last article in this newsgroup.
5710       (when gnus-newsgroup-headers
5711         (setq gnus-newsgroup-begin
5712               (mail-header-number (car gnus-newsgroup-headers))
5713               gnus-newsgroup-end
5714               (mail-header-number
5715                (gnus-last-element gnus-newsgroup-headers))))
5716       ;; GROUP is successfully selected.
5717       (or gnus-newsgroup-headers t)))))
5718
5719 (defun gnus-compute-unseen-list ()
5720   ;; The `seen' marks are treated specially.
5721   (if (not gnus-newsgroup-seen)
5722       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5723     (setq gnus-newsgroup-unseen
5724           (gnus-inverse-list-range-intersection
5725            gnus-newsgroup-articles gnus-newsgroup-seen))))
5726
5727 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5728
5729 (defun gnus-summary-display-make-predicate (display)
5730   (require 'gnus-agent)
5731   (when (= (length display) 1)
5732     (setq display (car display)))
5733   (unless gnus-summary-display-cache
5734     (dolist (elem (append '((unread . unread)
5735                             (read . read)
5736                             (unseen . unseen))
5737                           gnus-article-mark-lists))
5738       (push (cons (cdr elem)
5739                   (gnus-byte-compile    ;Why bother?
5740                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5741             gnus-summary-display-cache)))
5742   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5743         (gnus-category-predicate-cache gnus-summary-display-cache))
5744     (gnus-get-predicate display)))
5745
5746 ;; Uses the dynamically bound `gnus-number' variable.
5747 (defvar gnus-number)
5748 (defun gnus-article-marked-p (type &optional article)
5749   (let ((article (or article gnus-number)))
5750     (cond
5751      ((eq type 'tick)
5752       (memq article gnus-newsgroup-marked))
5753      ((eq type 'spam)
5754       (memq article gnus-newsgroup-spam-marked))
5755      ((eq type 'unsend)
5756       (memq article gnus-newsgroup-unsendable))
5757      ((eq type 'undownload)
5758       (memq article gnus-newsgroup-undownloaded))
5759      ((eq type 'download)
5760       (memq article gnus-newsgroup-downloadable))
5761      ((eq type 'unread)
5762       (memq article gnus-newsgroup-unreads))
5763      ((eq type 'read)
5764       (memq article gnus-newsgroup-reads))
5765      ((eq type 'dormant)
5766       (memq article gnus-newsgroup-dormant) )
5767      ((eq type 'expire)
5768       (memq article gnus-newsgroup-expirable))
5769      ((eq type 'reply)
5770       (memq article gnus-newsgroup-replied))
5771      ((eq type 'killed)
5772       (memq article gnus-newsgroup-killed))
5773      ((eq type 'bookmark)
5774       (assq article gnus-newsgroup-bookmarks))
5775      ((eq type 'score)
5776       (assq article gnus-newsgroup-scored))
5777      ((eq type 'save)
5778       (memq article gnus-newsgroup-saved))
5779      ((eq type 'cache)
5780       (memq article gnus-newsgroup-cached))
5781      ((eq type 'forward)
5782       (memq article gnus-newsgroup-forwarded))
5783      ((eq type 'seen)
5784       (not (memq article gnus-newsgroup-unseen)))
5785      (t t))))
5786
5787 (defun gnus-articles-to-read (group &optional read-all)
5788   "Find out what articles the user wants to read."
5789   (let* ((only-read-p t)
5790          (articles
5791           (gnus-list-range-difference
5792           ;; Select all articles if `read-all' is non-nil, or if there
5793           ;; are no unread articles.
5794           (if (or read-all
5795                   (and (zerop (length gnus-newsgroup-marked))
5796                        (zerop (length gnus-newsgroup-unreads)))
5797                   ;; Fetch all if the predicate is non-nil.
5798                   gnus-newsgroup-display)
5799               ;; We want to select the headers for all the articles in
5800               ;; the group, so we select either all the active
5801               ;; articles in the group, or (if that's nil), the
5802               ;; articles in the cache.
5803               (or
5804                (if gnus-newsgroup-maximum-articles
5805                    (let ((active (gnus-active group)))
5806                      (gnus-uncompress-range
5807                       (cons (max (car active)
5808                                  (- (cdr active)
5809                                     gnus-newsgroup-maximum-articles
5810                                     -1))
5811                             (cdr active))))
5812                  (gnus-uncompress-range (gnus-active group)))
5813                (gnus-cache-articles-in-group group))
5814             ;; Select only the "normal" subset of articles.
5815             (setq only-read-p nil)
5816             (gnus-sorted-nunion
5817              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5818              gnus-newsgroup-unreads))
5819           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5820          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5821          (scored (length scored-list))
5822          (number (length articles))
5823          (marked (+ (length gnus-newsgroup-marked)
5824                     (length gnus-newsgroup-dormant)))
5825          (select
5826           (cond
5827            ((numberp read-all)
5828             read-all)
5829            ((numberp gnus-newsgroup-display)
5830             gnus-newsgroup-display)
5831            (t
5832             (condition-case ()
5833                 (cond
5834                  ((and (or (<= scored marked) (= scored number))
5835                        (numberp gnus-large-newsgroup)
5836                        (> number gnus-large-newsgroup))
5837                   (let* ((cursor-in-echo-area nil)
5838                          (initial (gnus-parameter-large-newsgroup-initial
5839                                    gnus-newsgroup-name))
5840                          (default (if only-read-p
5841                                       (or initial gnus-large-newsgroup)
5842                                     number))
5843                          (input
5844                           (read-string
5845                            (if only-read-p
5846                                (format
5847                                 "How many articles from %s (available %d, default %d): "
5848                                 (gnus-group-decoded-name
5849                                  (gnus-group-real-name gnus-newsgroup-name))
5850                                 number default)
5851                              (format
5852                               "How many articles from %s (%d default): "
5853                               (gnus-group-decoded-name
5854                                (gnus-group-real-name gnus-newsgroup-name))
5855                               default))
5856                            nil
5857                            nil
5858                            (number-to-string default))))
5859                     (if (string-match "^[ \t]*$" input) number input)))
5860                  ((and (> scored marked) (< scored number)
5861                        (> (- scored number) 20))
5862                   (let ((input
5863                          (read-string
5864                           (format "%s %s (%d scored, %d total): "
5865                                   "How many articles from"
5866                                   (gnus-group-decoded-name
5867                                    (gnus-group-real-name gnus-newsgroup-name))
5868                                   scored number))))
5869                     (if (string-match "^[ \t]*$" input)
5870                         number input)))
5871                  (t number))
5872               (quit
5873                (message "Quit getting the articles to read")
5874                nil))))))
5875     (setq select (if (stringp select) (string-to-number select) select))
5876     (if (or (null select) (zerop select))
5877         select
5878       (if (and (not (zerop scored)) (<= (abs select) scored))
5879           (progn
5880             (setq articles (sort scored-list '<))
5881             (setq number (length articles)))
5882         (setq articles (copy-sequence articles)))
5883
5884       (when (< (abs select) number)
5885         (if (< select 0)
5886             ;; Select the N oldest articles.
5887             (setcdr (nthcdr (1- (abs select)) articles) nil)
5888           ;; Select the N most recent articles.
5889           (setq articles (nthcdr (- number select) articles))))
5890       (setq gnus-newsgroup-unselected
5891             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5892       (when gnus-alter-articles-to-read-function
5893         (setq articles
5894               (sort
5895                (funcall gnus-alter-articles-to-read-function
5896                         gnus-newsgroup-name articles)
5897                '<)))
5898       articles)))
5899
5900 (defun gnus-killed-articles (killed articles)
5901   (let (out)
5902     (while articles
5903       (when (inline (gnus-member-of-range (car articles) killed))
5904         (push (car articles) out))
5905       (setq articles (cdr articles)))
5906     out))
5907
5908 (defun gnus-article-mark-to-type (mark)
5909   "Return the type of MARK."
5910   (or (cadr (assq mark gnus-article-special-mark-lists))
5911       'list))
5912
5913 (defun gnus-article-unpropagatable-p (mark)
5914   "Return whether MARK should be propagated to back end."
5915   (memq mark gnus-article-unpropagated-mark-lists))
5916
5917 (defun gnus-adjust-marked-articles (info)
5918   "Set all article lists and remove all marks that are no longer valid."
5919   (let* ((marked-lists (gnus-info-marks info))
5920          (active (gnus-active (gnus-info-group info)))
5921          (min (car active))
5922          (max (cdr active))
5923          (types gnus-article-mark-lists)
5924          marks var articles article mark mark-type
5925          bgn end)
5926     ;; Hack to avoid adjusting marks for imap.
5927     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5928               'nnimap)
5929       (setq min 1))
5930
5931     (dolist (marks marked-lists)
5932       (setq mark (car marks)
5933             mark-type (gnus-article-mark-to-type mark)
5934             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5935       ;; We set the variable according to the type of the marks list,
5936       ;; and then adjust the marks to a subset of the active articles.
5937       (cond
5938        ;; Adjust "simple" lists - compressed yet unsorted
5939        ((eq mark-type 'list)
5940         ;; Simultaneously uncompress and clip to active range
5941         ;; See gnus-uncompress-range for a description of possible marks
5942         (let (l lh)
5943           (if (not (cadr marks))
5944               (set var nil)
5945             (setq articles (if (numberp (cddr marks))
5946                                (list (cdr marks))
5947                              (cdr marks))
5948                   lh (cons nil nil)
5949                   l lh)
5950
5951             (while (setq article (pop articles))
5952               (cond ((consp article)
5953                      (setq bgn (max (car article) min)
5954                            end (min (cdr article) max))
5955                      (while (<= bgn end)
5956                        (setq l (setcdr l (cons bgn nil))
5957                              bgn (1+ bgn))))
5958                     ((and (<= min article)
5959                           (>= max article))
5960                      (setq l (setcdr l (cons article nil))))))
5961             (set var (cdr lh)))))
5962        ;; Adjust assocs.
5963        ((eq mark-type 'tuple)
5964         (set var (setq articles (cdr marks)))
5965         (when (not (listp (cdr (symbol-value var))))
5966           (set var (list (symbol-value var))))
5967         (when (not (listp (cdr articles)))
5968           (setq articles (list articles)))
5969         (while articles
5970           (when (or (not (consp (setq article (pop articles))))
5971                     (< (car article) min)
5972                     (> (car article) max))
5973             (set var (delq article (symbol-value var))))))
5974        ;; Adjust ranges (sloppily).
5975        ((eq mark-type 'range)
5976         (cond
5977          ((eq mark 'seen)
5978           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5979           ;; It should be (seen (NUM1 . NUM2)).
5980           (when (numberp (cddr marks))
5981             (setcdr marks (list (cdr marks))))
5982           (setq articles (cdr marks))
5983           (while (and articles
5984                       (or (and (consp (car articles))
5985                                (> min (cdar articles)))
5986                           (and (numberp (car articles))
5987                                (> min (car articles)))))
5988             (pop articles))
5989           (set var articles))
5990          ((eq mark 'unexist)
5991           (set var (cdr marks)))))))))
5992
5993 (defun gnus-update-missing-marks (missing)
5994   "Go through the list of MISSING articles and remove them from the mark lists."
5995   (when missing
5996     (let (var m)
5997       ;; Go through all types.
5998       (dolist (elem gnus-article-mark-lists)
5999         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6000           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6001           (when (symbol-value var)
6002             ;; This list has articles.  So we delete all missing
6003             ;; articles from it.
6004             (setq m missing)
6005             (while m
6006               (set var (delq (pop m) (symbol-value var))))))))))
6007
6008 (defun gnus-update-marks ()
6009   "Enter the various lists of marked articles into the newsgroup info list."
6010   (let ((types gnus-article-mark-lists)
6011         (info (gnus-get-info gnus-newsgroup-name))
6012         type list newmarked symbol delta-marks)
6013     (when info
6014       ;; Add all marks lists to the list of marks lists.
6015       (while (setq type (pop types))
6016         (setq list (symbol-value
6017                     (setq symbol
6018                           (intern (format "gnus-newsgroup-%s" (car type))))))
6019
6020         (when list
6021           ;; Get rid of the entries of the articles that have the
6022           ;; default score.
6023           (when (and (eq (cdr type) 'score)
6024                      gnus-save-score
6025                      list)
6026             (let* ((arts list)
6027                    (prev (cons nil list))
6028                    (all prev))
6029               (while arts
6030                 (if (or (not (consp (car arts)))
6031                         (= (cdar arts) gnus-summary-default-score))
6032                     (setcdr prev (cdr arts))
6033                   (setq prev arts))
6034                 (setq arts (cdr arts)))
6035               (setq list (cdr all)))))
6036
6037         (when (eq (cdr type) 'seen)
6038           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6039
6040         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6041           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6042
6043         (when (and (gnus-check-backend-function
6044                     'request-set-mark gnus-newsgroup-name)
6045                    (not (gnus-article-unpropagatable-p (cdr type))))
6046           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6047                  ;; Don't do anything about marks for articles we
6048                  ;; didn't actually get any headers for.
6049                  (del
6050                   (gnus-list-range-intersection
6051                    gnus-newsgroup-articles
6052                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6053                  (add
6054                   (gnus-list-range-intersection
6055                    gnus-newsgroup-articles
6056                    (gnus-remove-from-range
6057                     (gnus-copy-sequence list) old))))
6058             (when add
6059               (push (list add 'add (list (cdr type))) delta-marks))
6060             (when del
6061               ;; Don't delete marks from outside the active range.
6062               ;; This shouldn't happen, but is a sanity check.
6063               (setq del (gnus-sorted-range-intersection
6064                          (gnus-active gnus-newsgroup-name) del))
6065               (push (list del 'del (list (cdr type))) delta-marks))))
6066
6067         (when list
6068           (push (cons (cdr type) list) newmarked)))
6069
6070       (when delta-marks
6071         (unless (gnus-check-group gnus-newsgroup-name)
6072           (error "Can't open server for %s" gnus-newsgroup-name))
6073         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6074
6075       ;; Enter these new marks into the info of the group.
6076       (if (nthcdr 3 info)
6077           (setcar (nthcdr 3 info) newmarked)
6078         ;; Add the marks lists to the end of the info.
6079         (when newmarked
6080           (setcdr (nthcdr 2 info) (list newmarked))))
6081
6082       ;; Cut off the end of the info if there's nothing else there.
6083       (let ((i 5))
6084         (while (and (> i 2)
6085                     (not (nth i info)))
6086           (when (nthcdr (decf i) info)
6087             (setcdr (nthcdr i info) nil)))))))
6088
6089 (defun gnus-set-mode-line (where)
6090   "Set the mode line of the article or summary buffers.
6091 If WHERE is `summary', the summary mode line format will be used."
6092   ;; Is this mode line one we keep updated?
6093   (when (and (memq where gnus-updated-mode-lines)
6094              (symbol-value
6095               (intern (format "gnus-%s-mode-line-format-spec" where))))
6096     (let (mode-string)
6097       ;; We evaluate this in the summary buffer since these
6098       ;; variables are buffer-local to that buffer.
6099       (with-current-buffer gnus-summary-buffer
6100         ;; We bind all these variables that are used in the `eval' form
6101         ;; below.
6102         (let* ((mformat (symbol-value
6103                          (intern
6104                           (format "gnus-%s-mode-line-format-spec" where))))
6105                (gnus-tmp-group-name (gnus-mode-string-quote
6106                                      (gnus-group-decoded-name
6107                                       gnus-newsgroup-name)))
6108                (gnus-tmp-article-number (or gnus-current-article 0))
6109                (gnus-tmp-unread gnus-newsgroup-unreads)
6110                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6111                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6112                (gnus-tmp-unread-and-unselected
6113                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6114                             (zerop gnus-tmp-unselected))
6115                        "")
6116                       ((zerop gnus-tmp-unselected)
6117                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6118                       (t (format "{%d(+%d) more}"
6119                                  gnus-tmp-unread-and-unticked
6120                                  gnus-tmp-unselected))))
6121                (gnus-tmp-subject
6122                 (if (and gnus-current-headers
6123                          (vectorp gnus-current-headers))
6124                     (gnus-mode-string-quote
6125                      (mail-header-subject gnus-current-headers))
6126                   ""))
6127                bufname-length max-len
6128                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6129           (setq mode-string (eval mformat))
6130           (setq bufname-length (if (string-match "%b" mode-string)
6131                                    (- (length
6132                                        (buffer-name
6133                                         (if (eq where 'summary)
6134                                             nil
6135                                           (get-buffer gnus-article-buffer))))
6136                                       2)
6137                                  0))
6138           (setq max-len (max 4 (if gnus-mode-non-string-length
6139                                    (- (window-width)
6140                                       gnus-mode-non-string-length
6141                                       bufname-length)
6142                                  (length mode-string))))
6143           ;; We might have to chop a bit of the string off...
6144           (when (> (length mode-string) max-len)
6145             (setq mode-string
6146                   (concat (truncate-string-to-width mode-string (- max-len 3))
6147                           "...")))))
6148       ;; Update the mode line.
6149       (setq mode-line-buffer-identification
6150             (gnus-mode-line-buffer-identification (list mode-string)))
6151       (set-buffer-modified-p t))))
6152
6153 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6154   "Go through the HEADERS list and add all Xrefs to a hash table.
6155 The resulting hash table is returned, or nil if no Xrefs were found."
6156   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6157          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6158          (xref-hashtb (gnus-make-hashtable))
6159          start group entry number xrefs header)
6160     (while headers
6161       (setq header (pop headers))
6162       (when (and (setq xrefs (mail-header-xref header))
6163                  (not (memq (setq number (mail-header-number header))
6164                             unreads)))
6165         (setq start 0)
6166         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6167           (setq start (match-end 0))
6168           (setq group (if prefix
6169                           (concat prefix (substring xrefs (match-beginning 1)
6170                                                     (match-end 1)))
6171                         (substring xrefs (match-beginning 1) (match-end 1))))
6172           (setq number
6173                 (string-to-number (substring xrefs (match-beginning 2)
6174                                           (match-end 2))))
6175           (if (setq entry (gnus-gethash group xref-hashtb))
6176               (setcdr entry (cons number (cdr entry)))
6177             (gnus-sethash group (cons number nil) xref-hashtb)))))
6178     (and start xref-hashtb)))
6179
6180 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6181   "Look through all the headers and mark the Xrefs as read."
6182   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6183         name info xref-hashtb idlist method nth4)
6184     (with-current-buffer gnus-group-buffer
6185       (when (setq xref-hashtb
6186                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6187         (mapatoms
6188          (lambda (group)
6189            (unless (string= from-newsgroup (setq name (symbol-name group)))
6190              (setq idlist (symbol-value group))
6191              ;; Dead groups are not updated.
6192              (and (prog1
6193                       (setq info (gnus-get-info name))
6194                     (when (stringp (setq nth4 (gnus-info-method info)))
6195                       (setq nth4 (gnus-server-to-method nth4))))
6196                   ;; Only do the xrefs if the group has the same
6197                   ;; select method as the group we have just read.
6198                   (or (gnus-methods-equal-p
6199                        nth4 (gnus-find-method-for-group from-newsgroup))
6200                       virtual
6201                       (equal nth4 (setq method (gnus-find-method-for-group
6202                                                 from-newsgroup)))
6203                       (and (equal (car nth4) (car method))
6204                            (equal (nth 1 nth4) (nth 1 method))))
6205                   gnus-use-cross-reference
6206                   (or (not (eq gnus-use-cross-reference t))
6207                       virtual
6208                       ;; Only do cross-references on subscribed
6209                       ;; groups, if that is what is wanted.
6210                       (<= (gnus-info-level info) gnus-level-subscribed))
6211                   (gnus-group-make-articles-read name idlist))))
6212          xref-hashtb)))))
6213
6214 (defun gnus-compute-read-articles (group articles)
6215   (let* ((entry (gnus-group-entry group))
6216          (info (nth 2 entry))
6217          (active (gnus-active group))
6218          ninfo)
6219     (when entry
6220       ;; First peel off all invalid article numbers.
6221       (when active
6222         (let ((ids articles)
6223               id first)
6224           (while (setq id (pop ids))
6225             (when (and first (> id (cdr active)))
6226               ;; We'll end up in this situation in one particular
6227               ;; obscure situation.  If you re-scan a group and get
6228               ;; a new article that is cross-posted to a different
6229               ;; group that has not been re-scanned, you might get
6230               ;; crossposted article that has a higher number than
6231               ;; Gnus believes possible.  So we re-activate this
6232               ;; group as well.  This might mean doing the
6233               ;; crossposting thingy will *increase* the number
6234               ;; of articles in some groups.  Tsk, tsk.
6235               (setq active (or (gnus-activate-group group) active)))
6236             (when (or (> id (cdr active))
6237                       (< id (car active)))
6238               (setq articles (delq id articles))))))
6239       ;; If the read list is nil, we init it.
6240       (if (and active
6241                (null (gnus-info-read info))
6242                (> (car active) 1))
6243           (setq ninfo (cons 1 (1- (car active))))
6244         (setq ninfo (gnus-info-read info)))
6245       ;; Then we add the read articles to the range.
6246       (gnus-add-to-range
6247        ninfo (setq articles (sort articles '<))))))
6248
6249 (defun gnus-group-make-articles-read (group articles)
6250   "Update the info of GROUP to say that ARTICLES are read."
6251   (let* ((num 0)
6252          (entry (gnus-group-entry group))
6253          (info (nth 2 entry))
6254          (active (gnus-active group))
6255          (set-marks
6256           (gnus-method-option-p
6257            (gnus-find-method-for-group group)
6258            'server-marks))
6259          range)
6260     (if (not entry)
6261         ;; Group that Gnus doesn't know exists, but still allow the
6262         ;; backend to set marks.
6263         (when set-marks
6264           (gnus-request-set-mark
6265            group (list (list (gnus-compress-sequence (sort articles #'<))
6266                              'add '(read)))))
6267       ;; Normal, subscribed groups.
6268       (setq range (gnus-compute-read-articles group articles))
6269       (with-current-buffer gnus-group-buffer
6270         (gnus-undo-register
6271           `(progn
6272              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6273              (gnus-info-set-read ',info ',(gnus-info-read info))
6274              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6275              (when ,set-marks
6276                (gnus-request-set-mark
6277                 ,group (list (list ',range 'del '(read)))))
6278              (gnus-group-update-group ,group t))))
6279       ;; Add the read articles to the range.
6280       (gnus-info-set-read info range)
6281       (when set-marks
6282         (gnus-request-set-mark group (list (list range 'add '(read)))))
6283       ;; Then we have to re-compute how many unread
6284       ;; articles there are in this group.
6285       (when active
6286         (cond
6287          ((not range)
6288           (setq num (- (1+ (cdr active)) (car active))))
6289          ((not (listp (cdr range)))
6290           (setq num (- (cdr active) (- (1+ (cdr range))
6291                                        (car range)))))
6292          (t
6293           (while range
6294             (if (numberp (car range))
6295                 (setq num (1+ num))
6296               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6297             (setq range (cdr range)))
6298           (setq num (- (cdr active) num))))
6299         ;; Update the number of unread articles.
6300         (setcar entry num)
6301         ;; Update the group buffer.
6302         (unless (gnus-ephemeral-group-p group)
6303           (gnus-group-update-group group t))))))
6304
6305 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6306   (let ((cur nntp-server-buffer)
6307         (dependencies
6308          (or dependencies
6309              (with-current-buffer gnus-summary-buffer
6310                gnus-newsgroup-dependencies)))
6311         headers id end ref number
6312         (mail-parse-charset gnus-newsgroup-charset)
6313         (mail-parse-ignored-charsets
6314          (save-current-buffer (condition-case nil
6315                                   (set-buffer gnus-summary-buffer)
6316                                 (error))
6317                               gnus-newsgroup-ignored-charsets)))
6318     (with-current-buffer nntp-server-buffer
6319       ;; Translate all TAB characters into SPACE characters.
6320       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6321       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6322       (ietf-drums-unfold-fws)
6323       (gnus-run-hooks 'gnus-parse-headers-hook)
6324       (let ((case-fold-search t)
6325             in-reply-to header p lines chars)
6326         (goto-char (point-min))
6327         ;; Search to the beginning of the next header.  Error messages
6328         ;; do not begin with 2 or 3.
6329         (while (re-search-forward "^[23][0-9]+ " nil t)
6330           (setq id nil
6331                 ref nil)
6332           ;; This implementation of this function, with nine
6333           ;; search-forwards instead of the one re-search-forward and
6334           ;; a case (which basically was the old function) is actually
6335           ;; about twice as fast, even though it looks messier.  You
6336           ;; can't have everything, I guess.  Speed and elegance
6337           ;; doesn't always go hand in hand.
6338           (setq
6339            header
6340            (vector
6341             ;; Number.
6342             (prog1
6343                 (setq number (read cur))
6344               (end-of-line)
6345               (setq p (point))
6346               (narrow-to-region (point)
6347                                 (or (and (search-forward "\n.\n" nil t)
6348                                          (- (point) 2))
6349                                     (point))))
6350             ;; Subject.
6351             (progn
6352               (goto-char p)
6353               (if (search-forward "\nsubject:" nil t)
6354                   (funcall gnus-decode-encoded-word-function
6355                            (nnheader-header-value))
6356                 "(none)"))
6357             ;; From.
6358             (progn
6359               (goto-char p)
6360               (if (search-forward "\nfrom:" nil t)
6361                   (funcall gnus-decode-encoded-address-function
6362                            (nnheader-header-value))
6363                 "(nobody)"))
6364             ;; Date.
6365             (progn
6366               (goto-char p)
6367               (if (search-forward "\ndate:" nil t)
6368                   (nnheader-header-value) ""))
6369             ;; Message-ID.
6370             (progn
6371               (goto-char p)
6372               (setq id (if (re-search-forward
6373                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6374                            ;; We do it this way to make sure the Message-ID
6375                            ;; is (somewhat) syntactically valid.
6376                            (buffer-substring (match-beginning 1)
6377                                              (match-end 1))
6378                          ;; If there was no message-id, we just fake one
6379                          ;; to make subsequent routines simpler.
6380                          (nnheader-generate-fake-message-id number))))
6381             ;; References.
6382             (progn
6383               (goto-char p)
6384               (if (search-forward "\nreferences:" nil t)
6385                   (progn
6386                     (setq end (point))
6387                     (prog1
6388                         (nnheader-header-value)
6389                       (setq ref
6390                             (buffer-substring
6391                              (progn
6392                                (end-of-line)
6393                                (search-backward ">" end t)
6394                                (1+ (point)))
6395                              (progn
6396                                (search-backward "<" end t)
6397                                (point))))))
6398                 ;; Get the references from the in-reply-to header if there
6399                 ;; were no references and the in-reply-to header looks
6400                 ;; promising.
6401                 (if (and (search-forward "\nin-reply-to:" nil t)
6402                          (setq in-reply-to (nnheader-header-value))
6403                          (string-match "<[^>]+>" in-reply-to))
6404                     (let (ref2)
6405                       (setq ref (substring in-reply-to (match-beginning 0)
6406                                            (match-end 0)))
6407                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6408                         (setq ref2 (substring in-reply-to (match-beginning 0)
6409                                               (match-end 0)))
6410                         (when (> (length ref2) (length ref))
6411                           (setq ref ref2)))
6412                       ref)
6413                   (setq ref nil))))
6414             ;; Chars.
6415             (progn
6416               (goto-char p)
6417               (if (search-forward "\nchars: " nil t)
6418                   (if (numberp (setq chars (ignore-errors (read cur))))
6419                       chars -1)
6420                 -1))
6421             ;; Lines.
6422             (progn
6423               (goto-char p)
6424               (if (search-forward "\nlines: " nil t)
6425                   (if (numberp (setq lines (ignore-errors (read cur))))
6426                       lines -1)
6427                 -1))
6428             ;; Xref.
6429             (progn
6430               (goto-char p)
6431               (and (search-forward "\nxref:" nil t)
6432                    (nnheader-header-value)))
6433             ;; Extra.
6434             (when gnus-extra-headers
6435               (let ((extra gnus-extra-headers)
6436                     out)
6437                 (while extra
6438                   (goto-char p)
6439                   (when (search-forward
6440                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6441                     (push (cons (car extra) (nnheader-header-value))
6442                           out))
6443                   (pop extra))
6444                 out))))
6445           (when (equal id ref)
6446             (setq ref nil))
6447
6448           (when gnus-alter-header-function
6449             (funcall gnus-alter-header-function header)
6450             (setq id (mail-header-id header)
6451                   ref (gnus-parent-id (mail-header-references header))))
6452
6453           (when (setq header
6454                       (gnus-dependencies-add-header
6455                        header dependencies force-new))
6456             (push header headers))
6457           (goto-char (point-max))
6458           (widen))
6459         (nreverse headers)))))
6460
6461 ;; Goes through the xover lines and returns a list of vectors
6462 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6463                                                   force-new dependencies
6464                                                   group also-fetch-heads)
6465   "Parse the news overview data in the server buffer.
6466 Return a list of headers that match SEQUENCE (see
6467 `nntp-retrieve-headers')."
6468   ;; Get the Xref when the users reads the articles since most/some
6469   ;; NNTP servers do not include Xrefs when using XOVER.
6470   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6471   (let ((mail-parse-charset gnus-newsgroup-charset)
6472         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6473         (cur nntp-server-buffer)
6474         (dependencies (or dependencies gnus-newsgroup-dependencies))
6475         (allp (cond
6476                ((eq gnus-read-all-available-headers t)
6477                 t)
6478                ((and (stringp gnus-read-all-available-headers)
6479                      group)
6480                 (string-match gnus-read-all-available-headers group))
6481                (t
6482                 nil)))
6483         number headers header)
6484     (with-current-buffer nntp-server-buffer
6485       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6486       ;; Allow the user to mangle the headers before parsing them.
6487       (gnus-run-hooks 'gnus-parse-headers-hook)
6488       (goto-char (point-min))
6489       (gnus-parse-without-error
6490         (while (and (or sequence allp)
6491                     (not (eobp)))
6492           (setq number (read cur))
6493           (when (not allp)
6494             (while (and sequence
6495                         (< (car sequence) number))
6496               (setq sequence (cdr sequence))))
6497           (when (and (or allp
6498                          (and sequence
6499                               (eq number (car sequence))))
6500                      (progn
6501                        (setq sequence (cdr sequence))
6502                        (setq header (inline
6503                                       (gnus-nov-parse-line
6504                                        number dependencies force-new)))))
6505             (push header headers))
6506           (forward-line 1)))
6507       ;; A common bug in inn is that if you have posted an article and
6508       ;; then retrieves the active file, it will answer correctly --
6509       ;; the new article is included.  However, a NOV entry for the
6510       ;; article may not have been generated yet, so this may fail.
6511       ;; We work around this problem by retrieving the last few
6512       ;; headers using HEAD.
6513       (if (or (not also-fetch-heads)
6514               (not sequence))
6515           ;; We (probably) got all the headers.
6516           (nreverse headers)
6517         (let ((gnus-nov-is-evil t))
6518           (nconc
6519            (nreverse headers)
6520            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6521              (gnus-get-newsgroup-headers))))))))
6522
6523 (defun gnus-article-get-xrefs ()
6524   "Fill in the Xref value in `gnus-current-headers', if necessary.
6525 This is meant to be called in `gnus-article-internal-prepare-hook'."
6526   (let ((headers (with-current-buffer gnus-summary-buffer
6527                    gnus-current-headers)))
6528     (or (not gnus-use-cross-reference)
6529         (not headers)
6530         (and (mail-header-xref headers)
6531              (not (string= (mail-header-xref headers) "")))
6532         (let ((case-fold-search t)
6533               xref)
6534           (save-restriction
6535             (nnheader-narrow-to-headers)
6536             (goto-char (point-min))
6537             (when (or (and (not (eobp))
6538                            (eq (downcase (char-after)) ?x)
6539                            (looking-at "Xref:"))
6540                       (search-forward "\nXref:" nil t))
6541               (goto-char (1+ (match-end 0)))
6542               (setq xref (buffer-substring (point) (point-at-eol)))
6543               (mail-header-set-xref headers xref)))))))
6544
6545 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6546   "Find article ID and insert the summary line for that article.
6547 OLD-HEADER can either be a header or a line number to insert
6548 the subject line on.
6549 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6550 and OLD-HEADER will be used when the summary line is inserted,
6551 too, instead of trying to fetch new headers."
6552   (let* ((line (and (numberp old-header) old-header))
6553          (old-header (and (vectorp old-header) old-header))
6554          (header (cond ((and old-header use-old-header)
6555                         old-header)
6556                        ((and (numberp id)
6557                              (gnus-number-to-header id))
6558                         (gnus-number-to-header id))
6559                        (t
6560                         (gnus-read-header id))))
6561          (number (and (numberp id) id))
6562          d)
6563     (when header
6564       ;; Rebuild the thread that this article is part of and go to the
6565       ;; article we have fetched.
6566       (when (and (not gnus-show-threads)
6567                  old-header)
6568         (when (and number
6569                    (setq d (gnus-data-find (mail-header-number old-header))))
6570           (goto-char (gnus-data-pos d))
6571           (gnus-data-remove
6572            number
6573            (- (point-at-bol)
6574               (prog1
6575                   (1+ (point-at-eol))
6576                 (gnus-delete-line))))))
6577       ;; Remove list identifiers from subject.
6578       (let ((gnus-newsgroup-headers (list header)))
6579         (gnus-summary-remove-list-identifiers))
6580       (when old-header
6581         (mail-header-set-number header (mail-header-number old-header)))
6582       (setq gnus-newsgroup-sparse
6583             (delq (setq number (mail-header-number header))
6584                   gnus-newsgroup-sparse))
6585       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6586       (push number gnus-newsgroup-limit)
6587       (gnus-rebuild-thread (mail-header-id header) line)
6588       (gnus-summary-goto-subject number nil t))
6589     (when (and (numberp number)
6590                (> number 0))
6591       ;; We have to update the boundaries even if we can't fetch the
6592       ;; article if ID is a number -- so that the next `P' or `N'
6593       ;; command will fetch the previous (or next) article even
6594       ;; if the one we tried to fetch this time has been canceled.
6595       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6596         (setq gnus-newsgroup-end number))
6597       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6598         (setq gnus-newsgroup-begin number))
6599       (setq gnus-newsgroup-unselected
6600             (delq number gnus-newsgroup-unselected)))
6601     ;; Report back a success?
6602     (and header (mail-header-number header))))
6603
6604 ;;; Process/prefix in the summary buffer
6605
6606 (defun gnus-summary-work-articles (n)
6607   "Return a list of articles to be worked upon.
6608 The prefix argument, the list of process marked articles, and the
6609 current article will be taken into consideration."
6610   (with-current-buffer gnus-summary-buffer
6611     (cond
6612      (n
6613       ;; A numerical prefix has been given.
6614       (setq n (prefix-numeric-value n))
6615       (let ((backward (< n 0))
6616             (n (abs (prefix-numeric-value n)))
6617             articles article)
6618         (save-excursion
6619           (while
6620               (and (> n 0)
6621                    (push (setq article (gnus-summary-article-number))
6622                          articles)
6623                    (if backward
6624                        (gnus-summary-find-prev nil article)
6625                      (gnus-summary-find-next nil article)))
6626             (decf n)))
6627         (nreverse articles)))
6628      ((and (gnus-region-active-p) (mark))
6629       (message "region active")
6630       ;; Work on the region between point and mark.
6631       (let ((max (max (point) (mark)))
6632             articles article)
6633         (save-excursion
6634           (goto-char (min (point) (mark)))
6635           (while
6636               (and
6637                (push (setq article (gnus-summary-article-number)) articles)
6638                (gnus-summary-find-next nil article)
6639                (< (point) max)))
6640           (nreverse articles))))
6641      (gnus-newsgroup-processable
6642       ;; There are process-marked articles present.
6643       ;; Save current state.
6644       (gnus-summary-save-process-mark)
6645       ;; Return the list.
6646       (reverse gnus-newsgroup-processable))
6647      (t
6648       ;; Just return the current article.
6649       (list (gnus-summary-article-number))))))
6650
6651 (defmacro gnus-summary-iterate (arg &rest forms)
6652   "Iterate over the process/prefixed articles and do FORMS.
6653 ARG is the interactive prefix given to the command.  FORMS will be
6654 executed with point over the summary line of the articles."
6655   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6656     `(let ((,articles (gnus-summary-work-articles ,arg)))
6657        (while ,articles
6658          (gnus-summary-goto-subject (car ,articles))
6659          ,@forms
6660          (pop ,articles)))))
6661
6662 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6663 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6664
6665 (defun gnus-summary-save-process-mark ()
6666   "Push the current set of process marked articles on the stack."
6667   (interactive)
6668   (push (copy-sequence gnus-newsgroup-processable)
6669         gnus-newsgroup-process-stack))
6670
6671 (defun gnus-summary-kill-process-mark ()
6672   "Push the current set of process marked articles on the stack and unmark."
6673   (interactive)
6674   (gnus-summary-save-process-mark)
6675   (gnus-summary-unmark-all-processable))
6676
6677 (defun gnus-summary-yank-process-mark ()
6678   "Pop the last process mark state off the stack and restore it."
6679   (interactive)
6680   (unless gnus-newsgroup-process-stack
6681     (error "Empty mark stack"))
6682   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6683
6684 (defun gnus-summary-process-mark-set (set)
6685   "Make SET into the current process marked articles."
6686   (gnus-summary-unmark-all-processable)
6687   (mapc 'gnus-summary-set-process-mark set))
6688
6689 ;;; Searching and stuff
6690
6691 (defun gnus-summary-search-group (&optional backward use-level)
6692   "Search for next unread newsgroup.
6693 If optional argument BACKWARD is non-nil, search backward instead."
6694   (with-current-buffer gnus-group-buffer
6695     (when (gnus-group-search-forward
6696            backward nil (if use-level (gnus-group-group-level) nil))
6697       (gnus-group-group-name))))
6698
6699 (defun gnus-summary-best-group (&optional exclude-group)
6700   "Find the name of the best unread group.
6701 If EXCLUDE-GROUP, do not go to this group."
6702   (with-current-buffer gnus-group-buffer
6703     (save-excursion
6704       (gnus-group-best-unread-group exclude-group))))
6705
6706 (defun gnus-summary-find-next (&optional unread article backward)
6707   (if backward
6708       (gnus-summary-find-prev unread article)
6709     (let* ((dummy (gnus-summary-article-intangible-p))
6710            (article (or article (gnus-summary-article-number)))
6711            (data (gnus-data-find-list article))
6712            result)
6713       (when (and (not dummy)
6714                  (or (not gnus-summary-check-current)
6715                      (not unread)
6716                      (not (gnus-data-unread-p (car data)))))
6717         (setq data (cdr data)))
6718       (when (setq result
6719                   (if unread
6720                       (progn
6721                         (while data
6722                           (unless (memq (gnus-data-number (car data))
6723                                         (cond
6724                                          ((eq gnus-auto-goto-ignores
6725                                               'always-undownloaded)
6726                                           gnus-newsgroup-undownloaded)
6727                                          (gnus-plugged
6728                                           nil)
6729                                          ((eq gnus-auto-goto-ignores
6730                                               'unfetched)
6731                                           gnus-newsgroup-unfetched)
6732                                          ((eq gnus-auto-goto-ignores
6733                                               'undownloaded)
6734                                           gnus-newsgroup-undownloaded)))
6735                             (when (gnus-data-unread-p (car data))
6736                               (setq result (car data)
6737                                     data nil)))
6738                           (setq data (cdr data)))
6739                         result)
6740                     (car data)))
6741         (goto-char (gnus-data-pos result))
6742         (gnus-data-number result)))))
6743
6744 (defun gnus-summary-find-prev (&optional unread article)
6745   (let* ((eobp (eobp))
6746          (article (or article (gnus-summary-article-number)))
6747          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6748          result)
6749     (when (and (not eobp)
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-subject (subject &optional unread backward article)
6781   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6782          (article (or article (gnus-summary-article-number)))
6783          (articles (gnus-data-list backward))
6784          (arts (gnus-data-find-list article articles))
6785          result)
6786     (when (or (not gnus-summary-check-current)
6787               (not unread)
6788               (not (gnus-data-unread-p (car arts))))
6789       (setq arts (cdr arts)))
6790     (while arts
6791       (and (or (not unread)
6792                (gnus-data-unread-p (car arts)))
6793            (vectorp (gnus-data-header (car arts)))
6794            (gnus-subject-equal
6795             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6796            (setq result (car arts)
6797                  arts nil))
6798       (setq arts (cdr arts)))
6799     (and result
6800          (goto-char (gnus-data-pos result))
6801          (gnus-data-number result))))
6802
6803 (defun gnus-summary-search-forward (&optional unread subject backward)
6804   "Search forward for an article.
6805 If UNREAD, look for unread articles.  If SUBJECT, look for
6806 articles with that subject.  If BACKWARD, search backward instead."
6807   (cond (subject (gnus-summary-find-subject subject unread backward))
6808         (backward (gnus-summary-find-prev unread))
6809         (t (gnus-summary-find-next unread))))
6810
6811 (defun gnus-recenter (&optional n)
6812   "Center point in window and redisplay frame.
6813 Also do horizontal recentering."
6814   (interactive "P")
6815   (when (and gnus-auto-center-summary
6816              (not (eq gnus-auto-center-summary 'vertical)))
6817     (gnus-horizontal-recenter))
6818   (if (fboundp 'recenter-top-bottom)
6819       (recenter-top-bottom n)
6820     (recenter n)))
6821
6822 (put 'gnus-recenter 'isearch-scroll t)
6823
6824 (defun gnus-forward-line-ignore-invisible (n)
6825   "Move N lines forward (backward if N is negative).
6826 Like forward-line, but skip over (and don't count) invisible lines."
6827   (let (done)
6828     (while (and (> n 0) (not done))
6829       ;; If the following character is currently invisible,
6830       ;; skip all characters with that same `invisible' property value.
6831       (while (gnus-invisible-p (point))
6832         (goto-char (gnus-next-char-property-change (point))))
6833       (forward-line 1)
6834       (if (eobp)
6835           (setq done t)
6836         (setq n (1- n))))
6837     (while (and (< n 0) (not done))
6838       (forward-line -1)
6839       (if (bobp) (setq done t)
6840         (setq n (1+ n))
6841         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6842           (goto-char (gnus-previous-char-property-change (point))))))))
6843
6844 (defun gnus-summary-recenter ()
6845   "Center point in the summary window.
6846 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6847 displayed, no centering will be performed."
6848   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6849   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6850   (interactive)
6851   ;; The user has to want it.
6852   (when gnus-auto-center-summary
6853     (let* ((top (cond ((< (window-height) 4) 0)
6854                       ((< (window-height) 7) 1)
6855                       (t (if (numberp gnus-auto-center-summary)
6856                              gnus-auto-center-summary
6857                            (/ (1- (window-height)) 2)))))
6858            (height (1- (window-height)))
6859            (bottom (save-excursion
6860                      (goto-char (point-max))
6861                      (gnus-forward-line-ignore-invisible (- height))
6862                      (point)))
6863            (window (get-buffer-window (current-buffer))))
6864       (when (get-buffer-window gnus-article-buffer)
6865         ;; Only do recentering when the article buffer is displayed,
6866         ;; Set the window start to either `bottom', which is the biggest
6867         ;; possible valid number, or the second line from the top,
6868         ;; whichever is the least.
6869         (let ((top-pos (save-excursion
6870                          (gnus-forward-line-ignore-invisible (- top))
6871                          (point))))
6872           (if (> bottom top-pos)
6873               ;; Keep the second line from the top visible
6874               (set-window-start window top-pos)
6875             ;; Try to keep the bottom line visible; if it's partially
6876             ;; obscured, either scroll one more line to make it fully
6877             ;; visible, or revert to using TOP-POS.
6878             (save-excursion
6879               (goto-char (point-max))
6880               (gnus-forward-line-ignore-invisible -1)
6881               (let ((last-line-start (point)))
6882                 (goto-char bottom)
6883                 (set-window-start window (point) t)
6884                 (when (not (pos-visible-in-window-p last-line-start window))
6885                   (gnus-forward-line-ignore-invisible 1)
6886                   (set-window-start window (min (point) top-pos) t)))))))
6887       ;; Do horizontal recentering while we're at it.
6888       (when (and (get-buffer-window (current-buffer) t)
6889                  (not (eq gnus-auto-center-summary 'vertical)))
6890         (let ((selected (selected-window)))
6891           (select-window (get-buffer-window (current-buffer) t))
6892           (gnus-summary-position-point)
6893           (gnus-horizontal-recenter)
6894           (select-window selected))))))
6895
6896 (defun gnus-summary-jump-to-group (newsgroup)
6897   "Move point to NEWSGROUP in group mode buffer."
6898   ;; Keep update point of group mode buffer if visible.
6899   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6900       (save-window-excursion
6901         ;; Take care of tree window mode.
6902         (when (get-buffer-window gnus-group-buffer)
6903           (pop-to-buffer gnus-group-buffer))
6904         (gnus-group-jump-to-group newsgroup))
6905     (save-excursion
6906       ;; Take care of tree window mode.
6907       (if (get-buffer-window gnus-group-buffer 0)
6908           (pop-to-buffer gnus-group-buffer)
6909         (set-buffer gnus-group-buffer))
6910       (gnus-group-jump-to-group newsgroup))))
6911
6912 ;; This function returns a list of article numbers based on the
6913 ;; difference between the ranges of read articles in this group and
6914 ;; the range of active articles.
6915 (defun gnus-list-of-unread-articles (group)
6916   (let* ((read (gnus-info-read (gnus-get-info group)))
6917          (active (or (gnus-active group) (gnus-activate-group group)))
6918          (last (or (cdr active)
6919                    (error "Group %s couldn't be activated " group)))
6920          (bottom (if gnus-newsgroup-maximum-articles
6921                      (max (car active)
6922                           (- last gnus-newsgroup-maximum-articles -1))
6923                    (car active)))
6924          first nlast unread)
6925     ;; If none are read, then all are unread.
6926     (if (not read)
6927         (setq first bottom)
6928       ;; If the range of read articles is a single range, then the
6929       ;; first unread article is the article after the last read
6930       ;; article.  Sounds logical, doesn't it?
6931       (if (and (not (listp (cdr read)))
6932                (or (< (car read) bottom)
6933                    (progn (setq read (list read))
6934                           nil)))
6935           (setq first (max bottom (1+ (cdr read))))
6936         ;; `read' is a list of ranges.
6937         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6938                                   (caar read)))
6939                   1)
6940           (setq first bottom))
6941         (while read
6942           (when first
6943             (while (< first nlast)
6944               (setq unread (cons first unread)
6945                     first (1+ first))))
6946           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6947           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6948           (setq read (cdr read)))))
6949     ;; And add the last unread articles.
6950     (while (<= first last)
6951       (setq unread (cons first unread)
6952             first (1+ first)))
6953     ;; Return the list of unread articles.
6954     (delq 0 (nreverse unread))))
6955
6956 (defun gnus-list-of-read-articles (group)
6957   "Return a list of unread, unticked and non-dormant articles."
6958   (let* ((info (gnus-get-info group))
6959          (marked (gnus-info-marks info))
6960          (active (gnus-active group)))
6961     (and info active
6962          (gnus-list-range-difference
6963           (gnus-list-range-difference
6964            (gnus-sorted-complement
6965             (gnus-uncompress-range
6966              (if gnus-newsgroup-maximum-articles
6967                  (cons (max (car active)
6968                             (- (cdr active)
6969                                gnus-newsgroup-maximum-articles
6970                                -1))
6971                        (cdr active))
6972                active))
6973             (gnus-list-of-unread-articles group))
6974            (cdr (assq 'dormant marked)))
6975           (cdr (assq 'tick marked))))))
6976
6977 ;; This function returns a sequence of article numbers based on the
6978 ;; difference between the ranges of read articles in this group and
6979 ;; the range of active articles.
6980 (defun gnus-sequence-of-unread-articles (group)
6981   (let* ((read (gnus-info-read (gnus-get-info group)))
6982          (active (or (gnus-active group) (gnus-activate-group group)))
6983          (last (cdr active))
6984          (bottom (if gnus-newsgroup-maximum-articles
6985                      (max (car active)
6986                           (- last gnus-newsgroup-maximum-articles -1))
6987                    (car active)))
6988          first nlast unread)
6989     ;; If none are read, then all are unread.
6990     (if (not read)
6991         (setq first bottom)
6992       ;; If the range of read articles is a single range, then the
6993       ;; first unread article is the article after the last read
6994       ;; article.  Sounds logical, doesn't it?
6995       (if (and (not (listp (cdr read)))
6996                (or (< (car read) bottom)
6997                    (progn (setq read (list read))
6998                           nil)))
6999           (setq first (max bottom (1+ (cdr read))))
7000         ;; `read' is a list of ranges.
7001         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7002                                   (caar read)))
7003                   1)
7004           (setq first bottom))
7005         (while read
7006           (when first
7007             (push (cons first nlast) unread))
7008           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7009           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7010           (setq read (cdr read)))))
7011     ;; And add the last unread articles.
7012     (cond ((not (and first last))
7013            nil)
7014           ((< first last)
7015            (push (cons first last) unread))
7016           ((= first last)
7017            (push first unread)))
7018     ;; Return the sequence of unread articles.
7019     (delq 0 (nreverse unread))))
7020
7021 ;; Various summary commands
7022
7023 (defun gnus-summary-select-article-buffer ()
7024   "Reconfigure windows to show the article buffer.
7025 If `gnus-widen-article-window' is set, show only the article
7026 buffer."
7027   (interactive)
7028   (if (not (gnus-buffer-live-p gnus-article-buffer))
7029       (error "There is no article buffer for this summary buffer")
7030     (unless (get-buffer-window gnus-article-buffer)
7031       (gnus-summary-show-article))
7032     (gnus-configure-windows
7033      (if gnus-widen-article-window
7034          'only-article
7035        'article)
7036      t)
7037     (select-window (get-buffer-window gnus-article-buffer))))
7038
7039 (defun gnus-summary-universal-argument (arg)
7040   "Perform any operation on all articles that are process/prefixed."
7041   (interactive "P")
7042   (let ((articles (gnus-summary-work-articles arg))
7043         func article)
7044     (if (eq
7045          (setq
7046           func
7047           (key-binding
7048            (read-key-sequence
7049             (substitute-command-keys
7050              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7051          'undefined)
7052         (gnus-error 1 "Undefined key")
7053       (save-excursion
7054         (while articles
7055           (gnus-summary-goto-subject (setq article (pop articles)))
7056           (let (gnus-newsgroup-processable)
7057             (command-execute func))
7058           (gnus-summary-remove-process-mark article)))))
7059   (gnus-summary-position-point))
7060
7061 (defun gnus-summary-toggle-truncation (&optional arg)
7062   "Toggle truncation of summary lines.
7063 With ARG, turn line truncation on if ARG is positive."
7064   (interactive "P")
7065   (setq truncate-lines
7066         (if (null arg) (not truncate-lines)
7067           (> (prefix-numeric-value arg) 0)))
7068   (redraw-display))
7069
7070 (defun gnus-summary-find-for-reselect ()
7071   "Return the number of an article to stay on across a reselect.
7072 The current article is considered, then following articles, then previous
7073 articles.  An article is sought which is not canceled and isn't a temporary
7074 insertion from another group.  If there's no such then return a dummy 0."
7075   (let (found)
7076     (dolist (rev '(nil t))
7077       (unless found      ; don't demand the reverse list if we don't need it
7078         (let ((data (gnus-data-find-list
7079                      (gnus-summary-article-number) (gnus-data-list rev))))
7080           (while (and data (not found))
7081             (if (and (< 0 (gnus-data-number (car data)))
7082                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7083                 (setq found (gnus-data-number (car data))))
7084             (setq data (cdr data))))))
7085     (or found 0)))
7086
7087 (defun gnus-summary-reselect-current-group (&optional all rescan)
7088   "Exit and then reselect the current newsgroup.
7089 The prefix argument ALL means to select all articles."
7090   (interactive "P")
7091   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7092     (error "Ephemeral groups can't be reselected"))
7093   (let ((current-subject (gnus-summary-find-for-reselect))
7094         (group gnus-newsgroup-name))
7095     (setq gnus-newsgroup-begin nil)
7096     (gnus-summary-exit nil 'leave-hidden)
7097     ;; We have to adjust the point of group mode buffer because
7098     ;; point was moved to the next unread newsgroup by exiting.
7099     (gnus-summary-jump-to-group group)
7100     (when rescan
7101       (save-excursion
7102         (gnus-group-get-new-news-this-group 1)))
7103     (gnus-group-read-group all t)
7104     (gnus-summary-goto-subject current-subject nil t)))
7105
7106 (defun gnus-summary-rescan-group (&optional all)
7107   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7108   (interactive "P")
7109   (let ((config gnus-current-window-configuration))
7110     (gnus-summary-reselect-current-group all t)
7111     (gnus-configure-windows config)
7112     (when (eq config 'article)
7113       (gnus-summary-select-article))))
7114
7115 (defun gnus-summary-update-info (&optional non-destructive)
7116   (save-excursion
7117     (let ((group gnus-newsgroup-name))
7118       (when group
7119         (when gnus-newsgroup-kill-headers
7120           (setq gnus-newsgroup-killed
7121                 (gnus-compress-sequence
7122                  (gnus-sorted-union
7123                   (gnus-list-range-intersection
7124                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7125                   gnus-newsgroup-unreads)
7126                  t)))
7127         (unless (listp (cdr gnus-newsgroup-killed))
7128           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7129         (let ((headers gnus-newsgroup-headers)
7130               (ephemeral-p (gnus-ephemeral-group-p group))
7131               info)
7132           (unless ephemeral-p
7133             (setq info (copy-sequence (gnus-get-info group))
7134                   info (delq (gnus-info-params info) info)))
7135           ;; Set the new ranges of read articles.
7136           (with-current-buffer gnus-group-buffer
7137             (gnus-undo-force-boundary))
7138           (gnus-update-read-articles
7139            group (gnus-sorted-union
7140                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7141           ;; Set the current article marks.
7142           (let ((gnus-newsgroup-scored
7143                  (if (and (not gnus-save-score)
7144                           (not non-destructive))
7145                      nil
7146                    gnus-newsgroup-scored)))
7147             (save-excursion
7148               (gnus-update-marks)))
7149           ;; Do the cross-ref thing.
7150           (when gnus-use-cross-reference
7151             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7152           ;; Do not switch windows but change the buffer to work.
7153           (set-buffer gnus-group-buffer)
7154           (unless ephemeral-p
7155             (gnus-group-update-group
7156              group nil
7157              (equal info
7158                     (setq info (copy-sequence (gnus-get-info group))
7159                           info (delq (gnus-info-params info) info))))))))))
7160
7161 (defun gnus-summary-save-newsrc (&optional force)
7162   "Save the current number of read/marked articles in the dribble buffer.
7163 The dribble buffer will then be saved.
7164 If FORCE (the prefix), also save the .newsrc file(s)."
7165   (interactive "P")
7166   (gnus-summary-update-info t)
7167   (if force
7168       (gnus-save-newsrc-file)
7169     (gnus-dribble-save)))
7170
7171 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7172
7173 (defun gnus-summary-exit (&optional temporary leave-hidden)
7174   "Exit reading current newsgroup, and then return to group selection mode.
7175 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7176   (interactive)
7177   (gnus-set-global-variables)
7178   (when (gnus-buffer-live-p gnus-article-buffer)
7179     (with-current-buffer gnus-article-buffer
7180       (mm-destroy-parts gnus-article-mime-handles)
7181       ;; Set it to nil for safety reason.
7182       (setq gnus-article-mime-handle-alist nil)
7183       (setq gnus-article-mime-handles nil)))
7184   (gnus-kill-save-kill-buffer)
7185   (gnus-async-halt-prefetch)
7186   (let* ((group gnus-newsgroup-name)
7187          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7188          (gnus-group-is-exiting-p t)
7189          (article-buffer gnus-article-buffer)
7190          (original-article-buffer gnus-original-article-buffer)
7191          (mode major-mode)
7192          (group-point nil)
7193          (buf (current-buffer))
7194          ;; `gnus-single-article-buffer' is nil buffer-locally in
7195          ;; ephemeral group of which summary buffer will be killed,
7196          ;; but the global value may be non-nil.
7197          (single-article-buffer gnus-single-article-buffer))
7198     (unless quit-config
7199       ;; Do adaptive scoring, and possibly save score files.
7200       (when gnus-newsgroup-adaptive
7201         (gnus-score-adaptive))
7202       (when gnus-use-scoring
7203         (gnus-score-save)))
7204     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7205     (when gnus-use-cache
7206       (gnus-cache-possibly-remove-articles)
7207       (gnus-cache-save-buffers))
7208     (gnus-async-prefetch-remove-group group)
7209     (when gnus-suppress-duplicates
7210       (gnus-dup-enter-articles))
7211     (when gnus-use-trees
7212       (gnus-tree-close group))
7213     (when gnus-use-cache
7214       (gnus-cache-write-active))
7215     ;; Remove entries for this group.
7216     (nnmail-purge-split-history (gnus-group-real-name group))
7217     ;; Make all changes in this group permanent.
7218     (unless quit-config
7219       (gnus-run-hooks 'gnus-exit-group-hook)
7220       (gnus-summary-update-info))
7221     (gnus-close-group group)
7222     ;; Make sure where we were, and go to next newsgroup.
7223     (when (buffer-live-p (get-buffer gnus-group-buffer))
7224       (set-buffer gnus-group-buffer))
7225     (unless quit-config
7226       (gnus-group-jump-to-group group))
7227     (gnus-run-hooks 'gnus-summary-exit-hook)
7228     (unless (or quit-config
7229                 (not gnus-summary-next-group-on-exit)
7230                 ;; If this group has disappeared from the summary
7231                 ;; buffer, don't skip forwards.
7232                 (not (string= group (gnus-group-group-name))))
7233       (gnus-group-next-unread-group 1))
7234     (setq group-point (point))
7235     (if temporary
7236         nil                             ;Nothing to do.
7237       (set-buffer buf)
7238       (if (not gnus-kill-summary-on-exit)
7239           (progn
7240             (gnus-deaden-summary)
7241             (setq mode nil))
7242         (when (get-buffer gnus-article-buffer)
7243           (bury-buffer gnus-article-buffer))
7244         ;; Return to group mode buffer.
7245         (when (eq mode 'gnus-summary-mode)
7246           (gnus-kill-buffer buf)))
7247
7248       (setq gnus-current-select-method gnus-select-method)
7249       (when (gnus-buffer-live-p gnus-group-buffer)
7250         (set-buffer gnus-group-buffer))
7251       (if quit-config
7252           (gnus-handle-ephemeral-exit quit-config)
7253         (goto-char group-point)
7254         ;; If gnus-group-buffer is already displayed, make sure we also move
7255         ;; the cursor in the window that displays it.
7256         (let ((win (get-buffer-window (current-buffer) 0)))
7257           (if win (set-window-point win (point))))
7258         (unless leave-hidden
7259           (gnus-configure-windows 'group 'force)))
7260
7261       ;; If we have several article buffers, we kill them at exit.
7262       (unless single-article-buffer
7263         (when (gnus-buffer-live-p article-buffer)
7264           (with-current-buffer article-buffer
7265             ;; Don't kill sticky article buffers
7266             (unless (eq major-mode 'gnus-sticky-article-mode)
7267               (gnus-kill-buffer article-buffer)
7268               (setq gnus-article-current nil))))
7269         (gnus-kill-buffer original-article-buffer))
7270
7271       ;; Clear the current group name.
7272       (unless quit-config
7273         (setq gnus-newsgroup-name nil)))))
7274
7275 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7276 (defun gnus-summary-exit-no-update (&optional no-questions)
7277   "Quit reading current newsgroup without updating read article info."
7278   (interactive)
7279   (let* ((group gnus-newsgroup-name)
7280          (gnus-group-is-exiting-p t)
7281          (gnus-group-is-exiting-without-update-p t)
7282          (quit-config (gnus-group-quit-config group)))
7283     (when (or no-questions
7284               gnus-expert-user
7285               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7286       (gnus-async-halt-prefetch)
7287       (run-hooks 'gnus-summary-prepare-exit-hook)
7288       (when (gnus-buffer-live-p gnus-article-buffer)
7289         (with-current-buffer gnus-article-buffer
7290           (gnus-article-stop-animations)
7291           (gnus-stop-downloads)
7292           (mm-destroy-parts gnus-article-mime-handles)
7293           ;; Set it to nil for safety reason.
7294           (setq gnus-article-mime-handle-alist nil)
7295           (setq gnus-article-mime-handles nil)))
7296       ;; If we have several article buffers, we kill them at exit.
7297       (unless gnus-single-article-buffer
7298         (gnus-kill-buffer gnus-article-buffer)
7299         (gnus-kill-buffer gnus-original-article-buffer)
7300         (setq gnus-article-current nil))
7301       ;; Return to the group buffer.
7302       (if (not gnus-kill-summary-on-exit)
7303           (progn
7304             (gnus-deaden-summary)
7305             (gnus-configure-windows 'group 'force))
7306         (gnus-configure-windows 'group 'force)
7307         (gnus-close-group group)
7308         (gnus-kill-buffer gnus-summary-buffer))
7309       (unless gnus-single-article-buffer
7310         (setq gnus-article-current nil))
7311       (when gnus-use-trees
7312         (gnus-tree-close group))
7313       (gnus-async-prefetch-remove-group group)
7314       (when (get-buffer gnus-article-buffer)
7315         (bury-buffer gnus-article-buffer))
7316       ;; Clear the current group name.
7317       (setq gnus-newsgroup-name nil)
7318       (unless (gnus-ephemeral-group-p group)
7319         (gnus-group-update-group group nil t))
7320       (when (equal (gnus-group-group-name) group)
7321         (gnus-group-next-unread-group 1))
7322       (when quit-config
7323         (gnus-handle-ephemeral-exit quit-config)))))
7324
7325 (defun gnus-handle-ephemeral-exit (quit-config)
7326   "Handle movement when leaving an ephemeral group.
7327 The state which existed when entering the ephemeral is reset."
7328   (if (not (buffer-live-p (car quit-config)))
7329       (when (gnus-buffer-live-p gnus-group-buffer)
7330         (gnus-configure-windows 'group 'force))
7331     (set-buffer (car quit-config))
7332     (unless (eq (cdr quit-config) 'group)
7333       (setq gnus-current-select-method
7334             (gnus-find-method-for-group gnus-newsgroup-name)))
7335     (cond ((eq major-mode 'gnus-summary-mode)
7336            (gnus-set-global-variables))
7337           ((eq major-mode 'gnus-article-mode)
7338            (save-current-buffer
7339              ;; The `gnus-summary-buffer' variable may point
7340              ;; to the old summary buffer when using a single
7341              ;; article buffer.
7342              (unless (gnus-buffer-live-p gnus-summary-buffer)
7343                (set-buffer gnus-group-buffer))
7344              (set-buffer gnus-summary-buffer)
7345              (gnus-set-global-variables))))
7346     (if (or (eq (cdr quit-config) 'article)
7347             (eq (cdr quit-config) 'pick))
7348         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7349             (gnus-configure-windows 'pick 'force)
7350           (gnus-configure-windows (cdr quit-config) 'force))
7351       (gnus-configure-windows (cdr quit-config) 'force))
7352     (when (eq major-mode 'gnus-summary-mode)
7353       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7354                                                      next-unread-noselect))
7355           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7356                                   'next-noselect)
7357                               (gnus-summary-next-subject 1 nil t))
7358                              ((eq gnus-auto-select-on-ephemeral-exit
7359                                   'next-unread-noselect)
7360                               (gnus-summary-next-subject 1 t t))))
7361             ;; Hide the article buffer which displays the article different
7362             ;; from the one that the cursor points to in the summary buffer.
7363             (gnus-configure-windows 'summary 'force))
7364         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7365                (gnus-summary-next-subject 1))
7366               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7367                (gnus-summary-next-subject 1 t))))
7368       (gnus-summary-recenter)
7369       (gnus-summary-position-point))))
7370
7371 ;;; Dead summaries.
7372
7373 (defvar gnus-dead-summary-mode-map
7374   (let ((map (make-keymap)))
7375     (suppress-keymap map)
7376     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7377     (dolist (key '("\C-d" "\r" "\177" [delete]))
7378       (define-key map key 'gnus-summary-wake-up-the-dead))
7379     (dolist (key '("q" "Q"))
7380       (define-key map key 'bury-buffer))
7381     map))
7382
7383 (define-minor-mode gnus-dead-summary-mode
7384   "Minor mode for Gnus summary buffers."
7385   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7386   (unless (derived-mode-p 'gnus-summary-mode)
7387     (setq gnus-dead-summary-mode nil)))
7388
7389 (defun gnus-deaden-summary ()
7390   "Make the current summary buffer into a dead summary buffer."
7391   ;; Kill any previous dead summary buffer.
7392   (when (and gnus-dead-summary
7393              (buffer-name gnus-dead-summary))
7394     (with-current-buffer gnus-dead-summary
7395       (when gnus-dead-summary-mode
7396         (kill-buffer (current-buffer)))))
7397   ;; Make this the current dead summary.
7398   (setq gnus-dead-summary (current-buffer))
7399   (gnus-dead-summary-mode 1)
7400   (let ((name (buffer-name)))
7401     (when (string-match "Summary" name)
7402       (rename-buffer
7403        (concat (substring name 0 (match-beginning 0)) "Dead "
7404                (substring name (match-beginning 0)))
7405        t)
7406       (bury-buffer))))
7407
7408 (defun gnus-kill-or-deaden-summary (buffer)
7409   "Kill or deaden the summary BUFFER."
7410   (save-excursion
7411     (when (and (buffer-name buffer)
7412                (not gnus-single-article-buffer))
7413       (with-current-buffer buffer
7414         (gnus-kill-buffer gnus-article-buffer)
7415         (gnus-kill-buffer gnus-original-article-buffer)))
7416     (cond
7417      ;; Kill the buffer.
7418      (gnus-kill-summary-on-exit
7419       (when (and gnus-use-trees
7420                  (gnus-buffer-exists-p buffer))
7421         (with-current-buffer buffer
7422           (gnus-tree-close gnus-newsgroup-name)))
7423       (gnus-kill-buffer buffer))
7424      ;; Deaden the buffer.
7425      ((gnus-buffer-exists-p buffer)
7426       (with-current-buffer buffer
7427         (gnus-deaden-summary))))))
7428
7429 (defun gnus-summary-wake-up-the-dead (&rest args)
7430   "Wake up the dead summary buffer."
7431   (interactive)
7432   (gnus-dead-summary-mode -1)
7433   (let ((name (buffer-name)))
7434     (when (string-match "Dead " name)
7435       (rename-buffer
7436        (concat (substring name 0 (match-beginning 0))
7437                (substring name (match-end 0)))
7438        t)))
7439   (gnus-message 3 "This dead summary is now alive again"))
7440
7441 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7442 (defun gnus-summary-describe-group (&optional force)
7443   "Describe the current newsgroup."
7444   (interactive "P")
7445   (gnus-group-describe-group force gnus-newsgroup-name))
7446
7447 (defun gnus-summary-describe-briefly ()
7448   "Describe summary mode commands briefly."
7449   (interactive)
7450   (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")))
7451
7452 ;; Walking around group mode buffer from summary mode.
7453
7454 (defun gnus-summary-next-group (&optional no-article target-group backward)
7455   "Exit current newsgroup and then select next unread newsgroup.
7456 If prefix argument NO-ARTICLE is non-nil, no article is selected
7457 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7458 previous group instead."
7459   (interactive "P")
7460   ;; Stop pre-fetching.
7461   (gnus-async-halt-prefetch)
7462   (let ((current-group gnus-newsgroup-name)
7463         (current-buffer (current-buffer))
7464         entered)
7465     ;; First we semi-exit this group to update Xrefs and all variables.
7466     ;; We can't do a real exit, because the window conf must remain
7467     ;; the same in case the user is prompted for info, and we don't
7468     ;; want the window conf to change before that...
7469     (gnus-summary-exit t)
7470     (while (not entered)
7471       ;; Then we find what group we are supposed to enter.
7472       (set-buffer gnus-group-buffer)
7473       (gnus-group-jump-to-group current-group)
7474       (setq target-group
7475             (or target-group
7476                 (if (eq gnus-keep-same-level 'best)
7477                     (gnus-summary-best-group gnus-newsgroup-name)
7478                   (gnus-summary-search-group backward gnus-keep-same-level))))
7479       (if (not target-group)
7480           ;; There are no further groups, so we return to the group
7481           ;; buffer.
7482           (progn
7483             (gnus-message 5 "Returning to the group buffer")
7484             (setq entered t)
7485             (when (gnus-buffer-live-p current-buffer)
7486               (set-buffer current-buffer)
7487               (gnus-summary-exit))
7488             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7489         ;; We try to enter the target group.
7490         (gnus-group-jump-to-group target-group)
7491         (let ((unreads (gnus-group-group-unread)))
7492           (if (and (or (eq t unreads)
7493                        (and unreads (not (zerop unreads))))
7494                    (gnus-summary-read-group
7495                     target-group nil no-article
7496                     (and (buffer-name current-buffer) current-buffer)
7497                     nil backward))
7498               (setq entered t)
7499             (setq current-group target-group
7500                   target-group nil)))))))
7501
7502 (defun gnus-summary-prev-group (&optional no-article)
7503   "Exit current newsgroup and then select previous unread newsgroup.
7504 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7505   (interactive "P")
7506   (gnus-summary-next-group no-article nil t))
7507
7508 ;; Walking around summary lines.
7509
7510 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7511   "Go to the first subject satisfying any non-nil constraint.
7512 If UNREAD is non-nil, the article should be unread.
7513 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7514 If UNSEEN is non-nil, the article should be unseen as well as unread.
7515 Returns the article selected or nil if there are no matching articles."
7516   (interactive "P")
7517   (cond
7518    ;; Empty summary.
7519    ((null gnus-newsgroup-data)
7520     (gnus-message 3 "No articles in the group")
7521     nil)
7522    ;; Pick the first article.
7523    ((not (or unread undownloaded unseen))
7524     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7525     (gnus-data-number (car gnus-newsgroup-data)))
7526    ;; Find the first unread article.
7527    (t
7528     (let ((data gnus-newsgroup-data))
7529       (while (and data
7530                   (let ((num (gnus-data-number (car data))))
7531                     (or (memq num gnus-newsgroup-unfetched)
7532                         (not (or (and unread
7533                                       (memq num gnus-newsgroup-unreads))
7534                                  (and undownloaded
7535                                       (memq num gnus-newsgroup-undownloaded))
7536                                  (and unseen
7537                                       (memq num gnus-newsgroup-unseen)
7538                                       (memq num gnus-newsgroup-unreads)))))))
7539         (setq data (cdr data)))
7540       (prog1
7541           (if data
7542               (progn
7543                 (goto-char (gnus-data-pos (car data)))
7544                 (gnus-data-number (car data)))
7545             (gnus-message 3 "No more%s articles"
7546                           (let* ((r (when unread " unread"))
7547                                  (d (when undownloaded " undownloaded"))
7548                                  (s (when unseen " unseen"))
7549                                  (l (delq nil (list r d s))))
7550                             (cond ((= 3 (length l))
7551                                    (concat r "," d ", or" s))
7552                                   ((= 2 (length l))
7553                                    (concat (car l) ", or" (cadr l)))
7554                                   ((= 1 (length l))
7555                                    (car l))
7556                                   (t
7557                                    ""))))
7558             nil
7559             )
7560         (gnus-summary-position-point))))))
7561
7562 (defun gnus-summary-next-subject (n &optional unread dont-display)
7563   "Go to next N'th summary line.
7564 If N is negative, go to the previous N'th subject line.
7565 If UNREAD is non-nil, only unread articles are selected.
7566 The difference between N and the actual number of steps taken is
7567 returned."
7568   (interactive "p")
7569   (let ((backward (< n 0))
7570         (n (abs n)))
7571     (while (and (> n 0)
7572                 (if backward
7573                     (gnus-summary-find-prev unread)
7574                   (gnus-summary-find-next unread)))
7575       (unless (zerop (setq n (1- n)))
7576         (gnus-summary-show-thread)))
7577     (when (/= 0 n)
7578       (gnus-message 7 "No more%s articles"
7579                     (if unread " unread" "")))
7580     (unless dont-display
7581       (gnus-summary-recenter)
7582       (gnus-summary-position-point))
7583     n))
7584
7585 (defun gnus-summary-next-unread-subject (n)
7586   "Go to next N'th unread summary line."
7587   (interactive "p")
7588   (gnus-summary-next-subject n t))
7589
7590 (defun gnus-summary-prev-subject (n &optional unread)
7591   "Go to previous N'th summary line.
7592 If optional argument UNREAD is non-nil, only unread article is selected."
7593   (interactive "p")
7594   (gnus-summary-next-subject (- n) unread))
7595
7596 (defun gnus-summary-prev-unread-subject (n)
7597   "Go to previous N'th unread summary line."
7598   (interactive "p")
7599   (gnus-summary-next-subject (- n) t))
7600
7601 (defun gnus-summary-goto-subjects (articles)
7602   "Insert the subject header for ARTICLES in the current buffer."
7603   (save-excursion
7604     (dolist (article articles)
7605       (gnus-summary-goto-subject article t)))
7606   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7607   (gnus-summary-position-point))
7608
7609 (defun gnus-summary-goto-subject (article &optional force silent)
7610   "Go to the subject line of ARTICLE.
7611 If FORCE, also allow jumping to articles not currently shown."
7612   (interactive "nArticle number: ")
7613   (unless (numberp article)
7614     (error "Article %s is not a number" article))
7615   (let ((b (point))
7616         (data (gnus-data-find article)))
7617     ;; We read in the article if we have to.
7618     (and (not data)
7619          force
7620          (gnus-summary-insert-subject
7621           article
7622           (if (or (numberp force) (vectorp force)) force)
7623           t)
7624          (setq data (gnus-data-find article)))
7625     (goto-char b)
7626     (if (not data)
7627         (progn
7628           (unless silent
7629             (gnus-message 3 "Can't find article %d" article))
7630           nil)
7631       (let ((pt (gnus-data-pos data)))
7632         (goto-char pt)
7633         (gnus-summary-set-article-display-arrow pt))
7634       (gnus-summary-position-point)
7635       article)))
7636
7637 ;; Walking around summary lines with displaying articles.
7638
7639 (defun gnus-summary-expand-window (&optional arg)
7640   "Make the summary buffer take up the entire Emacs frame.
7641 Given a prefix, will force an `article' buffer configuration."
7642   (interactive "P")
7643   (if arg
7644       (gnus-configure-windows 'article 'force)
7645     (gnus-configure-windows 'summary 'force)))
7646
7647 (defun gnus-summary-display-article (article &optional all-header)
7648   "Display ARTICLE in article buffer."
7649   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7650                (with-current-buffer gnus-article-buffer
7651                  (eq major-mode 'gnus-article-mode)))
7652     (gnus-article-setup-buffer))
7653   (gnus-set-global-variables)
7654   (with-current-buffer gnus-article-buffer
7655     (setq gnus-article-charset gnus-newsgroup-charset)
7656     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7657     (mm-enable-multibyte))
7658   (if (null article)
7659       nil
7660     (prog1
7661         (if gnus-summary-display-article-function
7662             (funcall gnus-summary-display-article-function article all-header)
7663           (gnus-article-prepare article all-header))
7664       (gnus-run-hooks 'gnus-select-article-hook)
7665       (when (and gnus-current-article
7666                  (not (zerop gnus-current-article)))
7667         (gnus-summary-goto-subject gnus-current-article))
7668       (gnus-summary-recenter)
7669       (when (and gnus-use-trees gnus-show-threads)
7670         (gnus-possibly-generate-tree article)
7671         (gnus-highlight-selected-tree article))
7672       ;; Successfully display article.
7673       (gnus-article-set-window-start
7674        (cdr (assq article gnus-newsgroup-bookmarks))))))
7675
7676 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7677   "Select the current article.
7678 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7679 non-nil, the article will be re-fetched even if it already present in
7680 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7681 be displayed."
7682   ;; Make sure we are in the summary buffer to work around bbdb bug.
7683   (unless (eq major-mode 'gnus-summary-mode)
7684     (set-buffer gnus-summary-buffer))
7685   (let ((article (or article (gnus-summary-article-number)))
7686         (all-headers (not (not all-headers))) ;Must be t or nil.
7687         gnus-summary-display-article-function)
7688     (and (not pseudo)
7689          (gnus-summary-article-pseudo-p article)
7690          (error "This is a pseudo-article"))
7691     (with-current-buffer gnus-summary-buffer
7692       (if (or (and gnus-single-article-buffer
7693                    (or (null gnus-current-article)
7694                        (null gnus-article-current)
7695                        (null (get-buffer gnus-article-buffer))
7696                        (not (eq article (cdr gnus-article-current)))
7697                        (not (equal (car gnus-article-current)
7698                                    gnus-newsgroup-name))
7699                        (not (get-buffer gnus-original-article-buffer))))
7700               (and (not gnus-single-article-buffer)
7701                    (or (null gnus-current-article)
7702                        (not (get-buffer gnus-original-article-buffer))
7703                        (not (eq gnus-current-article article))))
7704               force)
7705           ;; The requested article is different from the current article.
7706           (progn
7707             (gnus-summary-display-article article all-headers)
7708             (when (gnus-buffer-live-p gnus-article-buffer)
7709               (with-current-buffer gnus-article-buffer
7710                 (if (not gnus-article-decoded-p) ;; a local variable
7711                     (mm-disable-multibyte))))
7712             (gnus-article-set-window-start
7713              (cdr (assq article gnus-newsgroup-bookmarks)))
7714             article)
7715         'old))))
7716
7717 (defun gnus-summary-force-verify-and-decrypt ()
7718   "Display buttons for signed/encrypted parts and verify/decrypt them."
7719   (interactive)
7720   (let ((mm-verify-option 'known)
7721         (mm-decrypt-option 'known)
7722         (gnus-article-emulate-mime t)
7723         (gnus-buttonized-mime-types (append (list "multipart/signed"
7724                                                   "multipart/encrypted")
7725                                             gnus-buttonized-mime-types)))
7726     (gnus-summary-select-article nil 'force)))
7727
7728 (defun gnus-summary-next-article (&optional unread subject backward push)
7729   "Select the next article.
7730 If UNREAD, only unread articles are selected.
7731 If SUBJECT, only articles with SUBJECT are selected.
7732 If BACKWARD, the previous article is selected instead of the next."
7733   (interactive "P")
7734   ;; Make sure we are in the summary buffer.
7735   (unless (eq major-mode 'gnus-summary-mode)
7736     (set-buffer gnus-summary-buffer))
7737   (cond
7738    ;; Is there such an article?
7739    ((and (gnus-summary-search-forward unread subject backward)
7740          (or (gnus-summary-display-article (gnus-summary-article-number))
7741              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7742     (gnus-summary-position-point))
7743    ;; If not, we try the first unread, if that is wanted.
7744    ((and subject
7745          gnus-auto-select-same
7746          (gnus-summary-first-unread-article))
7747     (gnus-summary-position-point)
7748     (gnus-message 6 "Wrapped"))
7749    ;; Try to get next/previous article not displayed in this group.
7750    ((and gnus-auto-extend-newsgroup
7751          (not unread) (not subject))
7752     (gnus-summary-goto-article
7753      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7754      nil (count-lines (point-min) (point))))
7755    ;; Go to next/previous group.
7756    (t
7757     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7758       (gnus-summary-jump-to-group gnus-newsgroup-name))
7759     (let ((cmd (if (featurep 'xemacs)
7760                    last-command-char
7761                  last-command-event))
7762           (point
7763            (with-current-buffer gnus-group-buffer
7764              (point)))
7765           (current-summary (current-buffer))
7766           (group
7767            (if (eq gnus-keep-same-level 'best)
7768                (gnus-summary-best-group gnus-newsgroup-name)
7769              (gnus-summary-search-group backward gnus-keep-same-level))))
7770       ;; Select next unread newsgroup automagically.
7771       (cond
7772        ((or (not gnus-auto-select-next)
7773             (not cmd))
7774         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7775        ((or (eq gnus-auto-select-next 'quietly)
7776             (and (eq gnus-auto-select-next 'slightly-quietly)
7777                  push)
7778             (and (eq gnus-auto-select-next 'almost-quietly)
7779                  (gnus-summary-last-article-p)))
7780         ;; Select quietly.
7781         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7782             (gnus-summary-exit)
7783           (gnus-message 7 "No more%s articles (%s)..."
7784                         (if unread " unread" "")
7785                         (if group (concat "selecting " group)
7786                           "exiting"))
7787           (gnus-summary-next-group nil group backward)))
7788        (t
7789         (when (gnus-key-press-event-p last-input-event)
7790           ;; Somehow or other, we may now have selected a different
7791           ;; window.  Make point go back to the summary buffer.
7792           (when (eq current-summary (current-buffer))
7793             ;; FIXME: This burps when get-buffer-window returns nil.
7794             (select-window (get-buffer-window current-summary 0)))
7795           (gnus-summary-walk-group-buffer
7796            gnus-newsgroup-name cmd unread backward point))))))))
7797
7798 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7799   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7800                       (?\C-p (gnus-group-prev-unread-group 1))))
7801         (cursor-in-echo-area t)
7802         keve key group ended prompt)
7803     (with-current-buffer gnus-group-buffer
7804       (goto-char start)
7805       (setq group
7806             (if (eq gnus-keep-same-level 'best)
7807                 (gnus-summary-best-group gnus-newsgroup-name)
7808               (gnus-summary-search-group backward gnus-keep-same-level))))
7809     (while (not ended)
7810       (setq prompt
7811             (format
7812              "No more%s articles%s " (if unread " unread" "")
7813              (if (and group
7814                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7815                  (format " (Type %s for %s [%s])"
7816                          (single-key-description cmd)
7817                          (gnus-group-decoded-name group)
7818                          (gnus-group-unread group))
7819                (format " (Type %s to exit %s)"
7820                        (single-key-description cmd)
7821                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7822       ;; Confirm auto selection.
7823       (setq key (car (setq keve (gnus-read-event-char prompt)))
7824             ended t)
7825       (cond
7826        ((assq key keystrokes)
7827         (let ((obuf (current-buffer)))
7828           (switch-to-buffer gnus-group-buffer)
7829           (when group
7830             (gnus-group-jump-to-group group))
7831           (eval (cadr (assq key keystrokes)))
7832           (setq group (gnus-group-group-name))
7833           (switch-to-buffer obuf))
7834         (setq ended nil))
7835        ((equal key cmd)
7836         (if (or (not group)
7837                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7838             (gnus-summary-exit)
7839           (gnus-summary-next-group nil group backward)))
7840        (t
7841         (push (cdr keve) unread-command-events))))))
7842
7843 (defun gnus-summary-next-unread-article ()
7844   "Select unread article after current one."
7845   (interactive)
7846   (gnus-summary-next-article
7847    (or (not (eq gnus-summary-goto-unread 'never))
7848        (gnus-summary-last-article-p (gnus-summary-article-number)))
7849    (and gnus-auto-select-same
7850         (gnus-summary-article-subject))))
7851
7852 (defun gnus-summary-prev-article (&optional unread subject)
7853   "Select the article before the current one.
7854 If UNREAD is non-nil, only unread articles are selected."
7855   (interactive "P")
7856   (gnus-summary-next-article unread subject t))
7857
7858 (defun gnus-summary-prev-unread-article ()
7859   "Select unread article before current one."
7860   (interactive)
7861   (gnus-summary-prev-article
7862    (or (not (eq gnus-summary-goto-unread 'never))
7863        (gnus-summary-first-article-p (gnus-summary-article-number)))
7864    (and gnus-auto-select-same
7865         (gnus-summary-article-subject))))
7866
7867 (defun gnus-summary-next-page (&optional lines circular stop)
7868   "Show next page of the selected article.
7869 If at the end of the current article, select the next article.
7870 LINES says how many lines should be scrolled up.
7871
7872 If CIRCULAR is non-nil, go to the start of the article instead of
7873 selecting the next article when reaching the end of the current
7874 article.
7875
7876 If STOP is non-nil, just stop when reaching the end of the message.
7877
7878 Also see the variable `gnus-article-skip-boring'."
7879   (interactive "P")
7880   (gnus-set-global-variables)
7881   (let ((article (gnus-summary-article-number))
7882         (article-window (get-buffer-window gnus-article-buffer t))
7883         endp)
7884     ;; If the buffer is empty, we have no article.
7885     (unless article
7886       (error "No article to select"))
7887     (gnus-configure-windows 'article)
7888     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7889         (if (and (eq gnus-summary-goto-unread 'never)
7890                  (not (gnus-summary-last-article-p article)))
7891             (gnus-summary-next-article)
7892           (gnus-summary-next-unread-article))
7893       (if (or (null gnus-current-article)
7894               (null gnus-article-current)
7895               (/= article (cdr gnus-article-current))
7896               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7897           ;; Selected subject is different from current article's.
7898           (gnus-summary-display-article article)
7899         (when article-window
7900           (gnus-eval-in-buffer-window gnus-article-buffer
7901             (setq endp (or (gnus-article-next-page lines)
7902                            (gnus-article-only-boring-p))))
7903           (when endp
7904             (cond ((or stop gnus-summary-stop-at-end-of-message)
7905                    (gnus-message 3 "End of message"))
7906                   (circular
7907                    (gnus-summary-beginning-of-article))
7908                   (lines
7909                    (gnus-message 3 "End of message"))
7910                   ((null lines)
7911                    (if (and (eq gnus-summary-goto-unread 'never)
7912                             (not (gnus-summary-last-article-p article)))
7913                        (gnus-summary-next-article)
7914                      (gnus-summary-next-unread-article))))))))
7915     (gnus-summary-recenter)
7916     (gnus-summary-position-point)))
7917
7918 (defun gnus-summary-prev-page (&optional lines move)
7919   "Show previous page of selected article.
7920 Argument LINES specifies lines to be scrolled down.
7921 If MOVE, move to the previous unread article if point is at
7922 the beginning of the buffer."
7923   (interactive "P")
7924   (let ((article (gnus-summary-article-number))
7925         (article-window (get-buffer-window gnus-article-buffer t))
7926         endp)
7927     (gnus-configure-windows 'article)
7928     (if (or (null gnus-current-article)
7929             (null gnus-article-current)
7930             (/= article (cdr gnus-article-current))
7931             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7932         ;; Selected subject is different from current article's.
7933         (gnus-summary-display-article article)
7934       (gnus-summary-recenter)
7935       (when article-window
7936         (gnus-eval-in-buffer-window gnus-article-buffer
7937           (setq endp (gnus-article-prev-page lines)))
7938         (when (and move endp)
7939           (cond (lines
7940                  (gnus-message 3 "Beginning of message"))
7941                 ((null lines)
7942                  (if (and (eq gnus-summary-goto-unread 'never)
7943                           (not (gnus-summary-first-article-p article)))
7944                      (gnus-summary-prev-article)
7945                    (gnus-summary-prev-unread-article))))))))
7946   (gnus-summary-position-point))
7947
7948 (defun gnus-summary-prev-page-or-article (&optional lines)
7949   "Show previous page of selected article.
7950 Argument LINES specifies lines to be scrolled down.
7951 If at the beginning of the article, go to the next article."
7952   (interactive "P")
7953   (gnus-summary-prev-page lines t))
7954
7955 (defun gnus-summary-scroll-up (lines)
7956   "Scroll up (or down) one line current article.
7957 Argument LINES specifies lines to be scrolled up (or down if negative).
7958 If no article is selected, then the current article will be selected first."
7959   (interactive "p")
7960   (gnus-configure-windows 'article)
7961   (gnus-summary-show-thread)
7962   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7963     (gnus-eval-in-buffer-window gnus-article-buffer
7964       (cond ((> lines 0)
7965              (when (gnus-article-next-page lines)
7966                (gnus-message 3 "End of message")))
7967             ((< lines 0)
7968              (gnus-article-prev-page (- lines))))))
7969   (gnus-summary-recenter)
7970   (gnus-summary-position-point))
7971
7972 (defun gnus-summary-scroll-down (lines)
7973   "Scroll down (or up) one line current article.
7974 Argument LINES specifies lines to be scrolled down (or up if negative).
7975 If no article is selected, then the current article will be selected first."
7976   (interactive "p")
7977   (gnus-summary-scroll-up (- lines)))
7978
7979 (defun gnus-summary-next-same-subject ()
7980   "Select next article which has the same subject as current one."
7981   (interactive)
7982   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7983
7984 (defun gnus-summary-prev-same-subject ()
7985   "Select previous article which has the same subject as current one."
7986   (interactive)
7987   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7988
7989 (defun gnus-summary-next-unread-same-subject ()
7990   "Select next unread article which has the same subject as current one."
7991   (interactive)
7992   (gnus-summary-next-article t (gnus-summary-article-subject)))
7993
7994 (defun gnus-summary-prev-unread-same-subject ()
7995   "Select previous unread article which has the same subject as current one."
7996   (interactive)
7997   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7998
7999 (defun gnus-summary-first-unread-article ()
8000   "Select the first unread article.
8001 Return nil if there are no unread articles."
8002   (interactive)
8003   (prog1
8004       (when (gnus-summary-first-subject t)
8005         (gnus-summary-show-thread)
8006         (gnus-summary-first-subject t)
8007         (gnus-summary-display-article (gnus-summary-article-number)))
8008     (gnus-summary-position-point)))
8009
8010 (defun gnus-summary-first-unread-subject ()
8011   "Place the point on the subject line of the first unread article.
8012 Return nil if there are no unread articles."
8013   (interactive)
8014   (prog1
8015       (when (gnus-summary-first-subject t)
8016         (gnus-summary-show-thread)
8017         (gnus-summary-first-subject t))
8018     (gnus-summary-position-point)))
8019
8020 (defun gnus-summary-first-unseen-subject ()
8021   "Place the point on the subject line of the first unseen article.
8022 Return nil if there are no unseen articles."
8023   (interactive)
8024   (prog1
8025       (when (gnus-summary-first-subject nil nil t)
8026         (gnus-summary-show-thread)
8027         (gnus-summary-first-subject nil nil t))
8028     (gnus-summary-position-point)))
8029
8030 (defun gnus-summary-first-unseen-or-unread-subject ()
8031   "Place the point on the subject line of the first unseen and unread article.
8032 If all article have been seen, on the subject line of the first unread
8033 article."
8034   (interactive)
8035   (prog1
8036       (unless (when (gnus-summary-first-subject nil nil t)
8037                 (gnus-summary-show-thread)
8038                 (gnus-summary-first-subject nil nil t))
8039         (when (gnus-summary-first-subject t)
8040           (gnus-summary-show-thread)
8041           (gnus-summary-first-subject t)))
8042     (gnus-summary-position-point)))
8043
8044 (defun gnus-summary-first-article ()
8045   "Select the first article.
8046 Return nil if there are no articles."
8047   (interactive)
8048   (prog1
8049       (when (gnus-summary-first-subject)
8050         (gnus-summary-show-thread)
8051         (gnus-summary-first-subject)
8052         (gnus-summary-display-article (gnus-summary-article-number)))
8053     (gnus-summary-position-point)))
8054
8055 (defun gnus-summary-best-unread-article (&optional arg)
8056   "Select the unread article with the highest score.
8057 If given a prefix argument, select the next unread article that has a
8058 score higher than the default score."
8059   (interactive "P")
8060   (let ((article (if arg
8061                      (gnus-summary-better-unread-subject)
8062                    (gnus-summary-best-unread-subject))))
8063     (if article
8064         (gnus-summary-goto-article article)
8065       (error "No unread articles"))))
8066
8067 (defun gnus-summary-best-unread-subject ()
8068   "Select the unread subject with the highest score."
8069   (interactive)
8070   (let ((best -1000000)
8071         (data gnus-newsgroup-data)
8072         article score)
8073     (while data
8074       (and (gnus-data-unread-p (car data))
8075            (> (setq score
8076                     (gnus-summary-article-score (gnus-data-number (car data))))
8077               best)
8078            (setq best score
8079                  article (gnus-data-number (car data))))
8080       (setq data (cdr data)))
8081     (when article
8082       (gnus-summary-goto-subject article))
8083     (gnus-summary-position-point)
8084     article))
8085
8086 (defun gnus-summary-better-unread-subject ()
8087   "Select the first unread subject that has a score over the default score."
8088   (interactive)
8089   (let ((data gnus-newsgroup-data)
8090         article score)
8091     (while (and (setq article (gnus-data-number (car data)))
8092                 (or (gnus-data-read-p (car data))
8093                     (not (> (gnus-summary-article-score article)
8094                             gnus-summary-default-score))))
8095       (setq data (cdr data)))
8096     (when article
8097       (gnus-summary-goto-subject article))
8098     (gnus-summary-position-point)
8099     article))
8100
8101 (defun gnus-summary-last-subject ()
8102   "Go to the last displayed subject line in the group."
8103   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8104     (when article
8105       (gnus-summary-goto-subject article))))
8106
8107 (defun gnus-summary-goto-article (article &optional all-headers force)
8108   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8109 If ALL-HEADERS is non-nil, no header lines are hidden.
8110 If FORCE, go to the article even if it isn't displayed.  If FORCE
8111 is a number, it is the line the article is to be displayed on."
8112   (interactive
8113    (list
8114     (gnus-completing-read
8115      "Article number or Message-ID"
8116      (mapcar 'int-to-string gnus-newsgroup-limit))
8117     current-prefix-arg
8118     t))
8119   (prog1
8120       (if (and (stringp article)
8121                (string-match "@\\|%40" article))
8122           (gnus-summary-refer-article article)
8123         (when (stringp article)
8124           (setq article (string-to-number article)))
8125         (if (gnus-summary-goto-subject article force)
8126             (gnus-summary-display-article article all-headers)
8127           (gnus-message 4 "Couldn't go to article %s" article) nil))
8128     (gnus-summary-position-point)))
8129
8130 (defun gnus-summary-goto-last-article ()
8131   "Go to the previously read article."
8132   (interactive)
8133   (prog1
8134       (when gnus-last-article
8135         (gnus-summary-goto-article gnus-last-article nil t))
8136     (gnus-summary-position-point)))
8137
8138 (defun gnus-summary-pop-article (number)
8139   "Pop one article off the history and go to the previous.
8140 NUMBER articles will be popped off."
8141   (interactive "p")
8142   (let (to)
8143     (setq gnus-newsgroup-history
8144           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8145     (if to
8146         (gnus-summary-goto-article (car to) nil t)
8147       (error "Article history empty")))
8148   (gnus-summary-position-point))
8149
8150 ;; Summary commands and functions for limiting the summary buffer.
8151
8152 (defun gnus-summary-limit-to-articles (n)
8153   "Limit the summary buffer to the next N articles.
8154 If not given a prefix, use the process marked articles instead."
8155   (interactive "P")
8156   (prog1
8157       (let ((articles (gnus-summary-work-articles n)))
8158         (setq gnus-newsgroup-processable nil)
8159         (gnus-summary-limit articles))
8160     (gnus-summary-position-point)))
8161
8162 (defun gnus-summary-pop-limit (&optional total)
8163   "Restore the previous limit.
8164 If given a prefix, remove all limits."
8165   (interactive "P")
8166   (when total
8167     (setq gnus-newsgroup-limits
8168           (list (mapcar (lambda (h) (mail-header-number h))
8169                         gnus-newsgroup-headers))))
8170   (unless gnus-newsgroup-limits
8171     (error "No limit to pop"))
8172   (prog1
8173       (gnus-summary-limit nil 'pop)
8174     (gnus-summary-position-point)))
8175
8176 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8177   "Limit the summary buffer to articles that have subjects that match a regexp.
8178 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8179   (interactive
8180    (list (read-string (if current-prefix-arg
8181                           "Exclude subject (regexp): "
8182                         "Limit to subject (regexp): "))
8183          nil current-prefix-arg))
8184   (unless header
8185     (setq header "subject"))
8186   (when (not (equal "" subject))
8187     (prog1
8188         (let ((articles (gnus-summary-find-matching
8189                          (or header "subject") subject 'all nil nil
8190                          not-matching)))
8191           (unless articles
8192             (error "Found no matches for \"%s\"" subject))
8193           (gnus-summary-limit articles))
8194       (gnus-summary-position-point))))
8195
8196 (defun gnus-summary-limit-to-author (from &optional not-matching)
8197   "Limit the summary buffer to articles that have authors that match a regexp.
8198 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8199   (interactive
8200    (list (let* ((header (gnus-summary-article-header))
8201                 (default (and header (car (mail-header-parse-address
8202                                            (mail-header-from header))))))
8203            (read-string (concat (if current-prefix-arg
8204                                     "Exclude author (regexp"
8205                                   "Limit to author (regexp")
8206                                 (if default
8207                                     (concat ", default \"" default "\"): ")
8208                                   "): "))
8209                         nil nil
8210                         default))
8211          current-prefix-arg))
8212   (gnus-summary-limit-to-subject from "from" not-matching))
8213
8214 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8215   "Limit the summary buffer to articles with the given RECIPIENT.
8216
8217 If NOT-MATCHING, exclude RECIPIENT.
8218
8219 To and Cc headers are checked.  You need to include them in
8220 `nnmail-extra-headers'."
8221   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8222   (interactive
8223    (list (read-string (format "%s recipient (regexp): "
8224                               (if current-prefix-arg "Exclude" "Limit to")))
8225          current-prefix-arg))
8226   (when (not (equal "" recipient))
8227     (prog1 (let* ((to
8228                    (if (memq 'To nnmail-extra-headers)
8229                        (gnus-summary-find-matching
8230                         (cons 'extra 'To) recipient 'all nil nil
8231                         not-matching)
8232                      (gnus-message
8233                       1 "`To' isn't present in `nnmail-extra-headers'")
8234                      (sit-for 1)
8235                      nil))
8236                   (cc
8237                    (if (memq 'Cc nnmail-extra-headers)
8238                        (gnus-summary-find-matching
8239                         (cons 'extra 'Cc) recipient 'all nil nil
8240                         not-matching)
8241                      (gnus-message
8242                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8243                      (sit-for 1)
8244                      nil))
8245                   (articles
8246                    (if not-matching
8247                        ;; We need the numbers that are in both lists:
8248                        (mapcar (lambda (a)
8249                                  (and (memq a to) a))
8250                                cc)
8251                      (nconc to cc))))
8252              (unless articles
8253                (error "Found no matches for \"%s\"" recipient))
8254              (gnus-summary-limit articles))
8255       (gnus-summary-position-point))))
8256
8257 (defun gnus-summary-limit-to-address (address &optional not-matching)
8258   "Limit the summary buffer to articles with the given ADDRESS.
8259
8260 If NOT-MATCHING, exclude ADDRESS.
8261
8262 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8263 in `nnmail-extra-headers'."
8264   (interactive
8265    (list (read-string (format "%s address (regexp): "
8266                               (if current-prefix-arg "Exclude" "Limit to")))
8267          current-prefix-arg))
8268   (when (not (equal "" address))
8269     (prog1 (let* ((to
8270                    (if (memq 'To nnmail-extra-headers)
8271                        (gnus-summary-find-matching
8272                         (cons 'extra 'To) address 'all nil nil
8273                         not-matching)
8274                      (gnus-message
8275                       1 "`To' isn't present in `nnmail-extra-headers'")
8276                      (sit-for 1)
8277                      t))
8278                   (cc
8279                    (if (memq 'Cc nnmail-extra-headers)
8280                        (gnus-summary-find-matching
8281                         (cons 'extra 'Cc) address 'all nil nil
8282                         not-matching)
8283                      (gnus-message
8284                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8285                      (sit-for 1)
8286                      t))
8287                   (from
8288                    (gnus-summary-find-matching "from" address
8289                                                'all nil nil not-matching))
8290                   (articles
8291                    (if not-matching
8292                        ;; We need the numbers that are in all lists:
8293                        (if (eq cc t)
8294                            (if (eq to t)
8295                                from
8296                              (mapcar (lambda (a) (car (memq a from))) to))
8297                          (if (eq to t)
8298                              (mapcar (lambda (a) (car (memq a from))) cc)
8299                            (mapcar (lambda (a) (car (memq a from)))
8300                                    (mapcar (lambda (a) (car (memq a to)))
8301                                            cc))))
8302                      (nconc (if (eq to t) nil to)
8303                             (if (eq cc t) nil cc)
8304                             from))))
8305              (unless articles
8306                (error "Found no matches for \"%s\"" address))
8307              (gnus-summary-limit articles))
8308       (gnus-summary-position-point))))
8309
8310 (defun gnus-summary-limit-strange-charsets-predicate (header)
8311   (when (fboundp 'char-charset)
8312     (let ((string (concat (mail-header-subject header)
8313                           (mail-header-from header)))
8314           charset found)
8315       (dotimes (i (1- (length string)))
8316         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8317         (when (string-match "unicode\\|big\\|japanese" charset)
8318           (setq found t)))
8319       found)))
8320
8321 (defun gnus-summary-limit-to-predicate (predicate)
8322   "Limit to articles where PREDICATE returns non-nil.
8323 PREDICATE will be called with the header structures of the
8324 articles."
8325   (let ((articles nil)
8326         (case-fold-search t))
8327     (dolist (header gnus-newsgroup-headers)
8328       (when (funcall predicate header)
8329         (push (mail-header-number header) articles)))
8330     (gnus-summary-limit (nreverse articles))))
8331
8332 (defun gnus-summary-limit-to-age (age &optional younger-p)
8333   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8334 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8335 articles that are younger than AGE days."
8336   (interactive
8337    (let ((younger current-prefix-arg)
8338          (days-got nil)
8339          days)
8340      (while (not days-got)
8341        (setq days (if younger
8342                       (read-string "Limit to articles younger than (in days, older when negative): ")
8343                     (read-string
8344                      "Limit to articles older than (in days, younger when negative): ")))
8345        (when (> (length days) 0)
8346          (setq days (read days)))
8347        (if (numberp days)
8348            (progn
8349              (setq days-got t)
8350              (when (< days 0)
8351                (setq younger (not younger))
8352                (setq days (* days -1))))
8353          (message "Please enter a number.")
8354          (sleep-for 1)))
8355      (list days younger)))
8356   (prog1
8357       (let ((data gnus-newsgroup-data)
8358             (cutoff (days-to-time age))
8359             articles d date is-younger)
8360         (while (setq d (pop data))
8361           (when (and (vectorp (gnus-data-header d))
8362                      (setq date (mail-header-date (gnus-data-header d))))
8363             (setq is-younger (time-less-p
8364                               (time-since (gnus-date-get-time date))
8365                               cutoff))
8366             (when (if younger-p
8367                       is-younger
8368                     (not is-younger))
8369               (push (gnus-data-number d) articles))))
8370         (gnus-summary-limit (nreverse articles)))
8371     (gnus-summary-position-point)))
8372
8373 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8374   "Limit the summary buffer to articles that match an 'extra' header."
8375   (interactive
8376    (let ((header
8377           (intern
8378            (gnus-completing-read
8379             (if current-prefix-arg
8380                 "Exclude extra header"
8381               "Limit extra header")
8382             (mapcar 'symbol-name gnus-extra-headers)
8383             t nil nil
8384             (symbol-name (car gnus-extra-headers))))))
8385      (list header
8386            (read-string (format "%s header %s (regexp): "
8387                                 (if current-prefix-arg "Exclude" "Limit to")
8388                                 header))
8389            current-prefix-arg)))
8390   (when (not (equal "" regexp))
8391     (prog1
8392         (let ((articles (gnus-summary-find-matching
8393                          (cons 'extra header) regexp 'all nil nil
8394                          not-matching)))
8395           (unless articles
8396             (error "Found no matches for \"%s\"" regexp))
8397           (gnus-summary-limit articles))
8398       (gnus-summary-position-point))))
8399
8400 (defun gnus-summary-limit-to-display-predicate ()
8401   "Limit the summary buffer to the predicated in the `display' group parameter."
8402   (interactive)
8403   (unless gnus-newsgroup-display
8404     (error "There is no `display' group parameter"))
8405   (let (articles)
8406     (dolist (gnus-number gnus-newsgroup-articles)
8407       (when (funcall gnus-newsgroup-display)
8408         (push gnus-number articles)))
8409     (gnus-summary-limit articles))
8410   (gnus-summary-position-point))
8411
8412 (defun gnus-summary-limit-to-unread (&optional all)
8413   "Limit the summary buffer to articles that are not marked as read.
8414 If ALL is non-nil, limit strictly to unread articles."
8415   (interactive "P")
8416   (if all
8417       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8418     (gnus-summary-limit-to-marks
8419      ;; Concat all the marks that say that an article is read and have
8420      ;; those removed.
8421      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8422            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8423            gnus-low-score-mark gnus-expirable-mark
8424            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8425            gnus-duplicate-mark)
8426      'reverse)))
8427
8428 (defun gnus-summary-limit-to-headers (match &optional reverse)
8429   "Limit the summary buffer to articles that have headers that match MATCH.
8430 If REVERSE (the prefix), limit to articles that don't match."
8431   (interactive "sMatch headers (regexp): \nP")
8432   (gnus-summary-limit-to-bodies match reverse t))
8433
8434 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8435   "Limit the summary buffer to articles that have bodies that match MATCH.
8436 If REVERSE (the prefix), limit to articles that don't match."
8437   (interactive "sMatch body (regexp): \nP")
8438   (let ((articles nil)
8439         (gnus-select-article-hook nil)  ;Disable hook.
8440         (gnus-article-prepare-hook nil)
8441         (gnus-use-article-prefetch nil)
8442         (gnus-keep-backlog nil)
8443         (gnus-break-pages nil)
8444         (gnus-summary-display-arrow nil)
8445         (gnus-updated-mode-lines nil)
8446         (gnus-auto-center-summary nil)
8447         (gnus-display-mime-function nil))
8448     (dolist (data gnus-newsgroup-data)
8449       (let (gnus-mark-article-hook)
8450         (gnus-summary-select-article t t nil (gnus-data-number data)))
8451       (with-current-buffer gnus-article-buffer
8452         (article-goto-body)
8453         (let* ((case-fold-search t)
8454                (found (if headersp
8455                           (re-search-backward match nil t)
8456                         (re-search-forward match nil t))))
8457           (when (or (and found
8458                          (not reverse))
8459                     (and (not found)
8460                          reverse))
8461             (push (gnus-data-number data) articles)))))
8462     (if (not articles)
8463         (message "No messages matched")
8464       (gnus-summary-limit articles)))
8465   (gnus-summary-position-point))
8466
8467 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8468   "Limit the summary buffer to articles that aren't part on any thread.
8469 If THREADSP (the prefix), limit to articles that are in threads."
8470   (interactive "P")
8471   (let ((articles nil)
8472         thread-articles
8473         threads)
8474     (dolist (thread gnus-newsgroup-threads)
8475       (if (stringp (car thread))
8476           (dolist (thread (cdr thread))
8477             (push thread threads))
8478         (push thread threads)))
8479     (dolist (thread threads)
8480       (setq thread-articles (gnus-articles-in-thread thread))
8481       (when (or (and threadsp
8482                      (> (length thread-articles) 1))
8483                 (and (not threadsp)
8484                      (= (length thread-articles) 1)))
8485         (setq articles (nconc thread-articles articles))))
8486     (if (not articles)
8487         (message "No messages matched")
8488       (gnus-summary-limit articles))
8489     (gnus-summary-position-point)))
8490
8491 (defun gnus-summary-limit-to-replied (&optional unreplied)
8492   "Limit the summary buffer to replied articles.
8493 If UNREPLIED (the prefix), limit to unreplied articles."
8494   (interactive "P")
8495   (if unreplied
8496       (gnus-summary-limit
8497        (gnus-set-difference gnus-newsgroup-articles
8498         gnus-newsgroup-replied))
8499     (gnus-summary-limit gnus-newsgroup-replied))
8500   (gnus-summary-position-point))
8501
8502 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8503   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8504 If REVERSE, limit the summary buffer to articles that are marked
8505 with MARKS.  MARKS can either be a string of marks or a list of marks.
8506 Returns how many articles were removed."
8507   (interactive "sMarks: ")
8508   (gnus-summary-limit-to-marks marks t))
8509
8510 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8511   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8512 If REVERSE (the prefix), limit the summary buffer to articles that are
8513 not marked with MARKS.  MARKS can either be a string of marks or a
8514 list of marks.
8515 Returns how many articles were removed."
8516   (interactive "sMarks: \nP")
8517   (prog1
8518       (let ((data gnus-newsgroup-data)
8519             (marks (if (listp marks) marks
8520                      (append marks nil))) ; Transform to list.
8521             articles)
8522         (while data
8523           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8524                   (memq (gnus-data-mark (car data)) marks))
8525             (push (gnus-data-number (car data)) articles))
8526           (setq data (cdr data)))
8527         (gnus-summary-limit articles))
8528     (gnus-summary-position-point)))
8529
8530 (defun gnus-summary-limit-to-score (score)
8531   "Limit to articles with score at or above SCORE."
8532   (interactive "NLimit to articles with score of at least: ")
8533   (let ((data gnus-newsgroup-data)
8534         articles)
8535     (while data
8536       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8537                 score)
8538         (push (gnus-data-number (car data)) articles))
8539       (setq data (cdr data)))
8540     (prog1
8541         (gnus-summary-limit articles)
8542       (gnus-summary-position-point))))
8543
8544 (defun gnus-summary-limit-to-unseen ()
8545   "Limit to unseen articles."
8546   (interactive)
8547   (prog1
8548       (gnus-summary-limit gnus-newsgroup-unseen)
8549     (gnus-summary-position-point)))
8550
8551 (defun gnus-summary-limit-include-thread (id)
8552   "Display all the hidden articles that is in the thread with ID in it.
8553 When called interactively, ID is the Message-ID of the current
8554 article."
8555   (interactive (list (mail-header-id (gnus-summary-article-header))))
8556   (let ((articles (gnus-articles-in-thread
8557                    (gnus-id-to-thread (gnus-root-id id))))
8558         ;;we REALLY want the whole thread---this prevents cut-threads
8559         ;;from removing the thread we want to include.
8560         (gnus-fetch-old-headers nil)
8561         (gnus-build-sparse-threads nil))
8562     (prog1
8563         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8564       (gnus-summary-limit-include-matching-articles
8565        "subject"
8566        (regexp-quote (gnus-simplify-subject-re
8567                       (mail-header-subject (gnus-id-to-header id)))))
8568       (gnus-summary-position-point))))
8569
8570 (defun gnus-summary-limit-include-matching-articles (header regexp)
8571   "Display all the hidden articles that have HEADERs that match REGEXP."
8572   (interactive (list (read-string "Match on header: ")
8573                      (read-string "Regexp: ")))
8574   (let ((articles (gnus-find-matching-articles header regexp)))
8575     (prog1
8576         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8577       (gnus-summary-position-point))))
8578
8579 (defun gnus-summary-insert-dormant-articles ()
8580   "Insert all the dormant articles for this group into the current buffer."
8581   (interactive)
8582   (let ((gnus-verbose (max 6 gnus-verbose)))
8583     (if (not gnus-newsgroup-dormant)
8584         (gnus-message 3 "No dormant articles for this group")
8585       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8586
8587 (defun gnus-summary-insert-ticked-articles ()
8588   "Insert ticked articles for this group into the current buffer."
8589   (interactive)
8590   (let ((gnus-verbose (max 6 gnus-verbose)))
8591     (if (not gnus-newsgroup-marked)
8592         (gnus-message 3 "No ticked articles for this group")
8593       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8594
8595 (defun gnus-summary-limit-include-dormant ()
8596   "Display all the hidden articles that are marked as dormant.
8597 Note that this command only works on a subset of the articles currently
8598 fetched for this group."
8599   (interactive)
8600   (unless gnus-newsgroup-dormant
8601     (error "There are no dormant articles in this group"))
8602   (prog1
8603       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8604     (gnus-summary-position-point)))
8605
8606 (defun gnus-summary-include-articles (articles)
8607   "Fetch the headers for ARTICLES and then display the summary lines."
8608   (let ((gnus-inhibit-demon t)
8609         (gnus-agent nil)
8610         (gnus-read-all-available-headers t))
8611     (setq gnus-newsgroup-headers
8612           (gnus-merge
8613            'list gnus-newsgroup-headers
8614            (gnus-fetch-headers articles nil t)
8615            'gnus-article-sort-by-number))
8616     (setq gnus-newsgroup-articles
8617           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8618     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8619
8620 (defun gnus-summary-limit-exclude-dormant ()
8621   "Hide all dormant articles."
8622   (interactive)
8623   (prog1
8624       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8625     (gnus-summary-position-point)))
8626
8627 (defun gnus-summary-limit-exclude-childless-dormant ()
8628   "Hide all dormant articles that have no children."
8629   (interactive)
8630   (let ((data (gnus-data-list t))
8631         articles d children)
8632     ;; Find all articles that are either not dormant or have
8633     ;; children.
8634     (while (setq d (pop data))
8635       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8636                 (and (setq children
8637                            (gnus-article-children (gnus-data-number d)))
8638                      (let (found)
8639                        (while children
8640                          (when (memq (car children) articles)
8641                            (setq children nil
8642                                  found t))
8643                          (pop children))
8644                        found)))
8645         (push (gnus-data-number d) articles)))
8646     ;; Do the limiting.
8647     (prog1
8648         (gnus-summary-limit articles)
8649       (gnus-summary-position-point))))
8650
8651 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8652   "Mark all unread excluded articles as read.
8653 If ALL, mark even excluded ticked and dormants as read."
8654   (interactive "P")
8655   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8656   (let ((articles (gnus-sorted-ndifference
8657                    (sort
8658                     (mapcar (lambda (h) (mail-header-number h))
8659                             gnus-newsgroup-headers)
8660                     '<)
8661                    gnus-newsgroup-limit))
8662         article)
8663     (setq gnus-newsgroup-unreads
8664           (gnus-sorted-intersection gnus-newsgroup-unreads
8665                                     gnus-newsgroup-limit))
8666     (if all
8667         (setq gnus-newsgroup-dormant nil
8668               gnus-newsgroup-marked nil
8669               gnus-newsgroup-reads
8670               (nconc
8671                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8672                gnus-newsgroup-reads))
8673       (while (setq article (pop articles))
8674         (unless (or (memq article gnus-newsgroup-dormant)
8675                     (memq article gnus-newsgroup-marked))
8676           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8677
8678 (defun gnus-summary-limit (articles &optional pop)
8679   (if pop
8680       ;; We pop the previous limit off the stack and use that.
8681       (setq articles (car gnus-newsgroup-limits)
8682             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8683     ;; We use the new limit, so we push the old limit on the stack.
8684     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8685   ;; Set the limit.
8686   (setq gnus-newsgroup-limit articles)
8687   (let ((total (length gnus-newsgroup-data))
8688         (data (gnus-data-find-list (gnus-summary-article-number)))
8689         (gnus-summary-mark-below nil)   ; Inhibit this.
8690         found)
8691     ;; This will do all the work of generating the new summary buffer
8692     ;; according to the new limit.
8693     (gnus-summary-prepare)
8694     ;; Hide any threads, possibly.
8695     (gnus-summary-maybe-hide-threads)
8696     ;; Try to return to the article you were at, or one in the
8697     ;; neighborhood.
8698     (when data
8699       ;; We try to find some article after the current one.
8700       (while data
8701         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8702           (setq data nil
8703                 found t))
8704         (setq data (cdr data))))
8705     (unless found
8706       ;; If there is no data, that means that we were after the last
8707       ;; article.  The same goes when we can't find any articles
8708       ;; after the current one.
8709       (goto-char (point-max))
8710       (gnus-summary-find-prev))
8711     (gnus-set-mode-line 'summary)
8712     ;; We return how many articles were removed from the summary
8713     ;; buffer as a result of the new limit.
8714     (- total (length gnus-newsgroup-data))))
8715
8716 (defsubst gnus-invisible-cut-children (threads)
8717   (let ((num 0))
8718     (while threads
8719       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8720         (incf num))
8721       (pop threads))
8722     (< num 2)))
8723
8724 (defsubst gnus-cut-thread (thread)
8725   "Go forwards in the thread until we find an article that we want to display."
8726   (when (or (eq gnus-fetch-old-headers 'some)
8727             (eq gnus-fetch-old-headers 'invisible)
8728             (numberp gnus-fetch-old-headers)
8729             (eq gnus-build-sparse-threads 'some)
8730             (eq gnus-build-sparse-threads 'more))
8731     ;; Deal with old-fetched headers and sparse threads.
8732     (while (and
8733             thread
8734             (or
8735              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8736              (gnus-summary-article-ancient-p
8737               (mail-header-number (car thread))))
8738             (if (or (<= (length (cdr thread)) 1)
8739                     (eq gnus-fetch-old-headers 'invisible))
8740                 (setq gnus-newsgroup-limit
8741                       (delq (mail-header-number (car thread))
8742                             gnus-newsgroup-limit)
8743                       thread (cadr thread))
8744               (when (gnus-invisible-cut-children (cdr thread))
8745                 (let ((th (cdr thread)))
8746                   (while th
8747                     (if (memq (mail-header-number (caar th))
8748                               gnus-newsgroup-limit)
8749                         (setq thread (car th)
8750                               th nil)
8751                       (setq th (cdr th))))))))))
8752   thread)
8753
8754 (defun gnus-cut-threads (threads)
8755   "Cut off all uninteresting articles from the beginning of THREADS."
8756   (when (or (eq gnus-fetch-old-headers 'some)
8757             (eq gnus-fetch-old-headers 'invisible)
8758             (numberp gnus-fetch-old-headers)
8759             (eq gnus-build-sparse-threads 'some)
8760             (eq gnus-build-sparse-threads 'more))
8761     (let ((th threads))
8762       (while th
8763         (setcar th (gnus-cut-thread (car th)))
8764         (setq th (cdr th)))))
8765   ;; Remove nixed out threads.
8766   (delq nil threads))
8767
8768 (defun gnus-summary-initial-limit (&optional show-if-empty)
8769   "Figure out what the initial limit is supposed to be on group entry.
8770 This entails weeding out unwanted dormants, low-scored articles,
8771 fetch-old-headers verbiage, and so on."
8772   ;; Most groups have nothing to remove.
8773   (unless (or gnus-inhibit-limiting
8774               (and (null gnus-newsgroup-dormant)
8775                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8776                    (not (eq gnus-fetch-old-headers 'some))
8777                    (not (numberp gnus-fetch-old-headers))
8778                    (not (eq gnus-fetch-old-headers 'invisible))
8779                    (null gnus-summary-expunge-below)
8780                    (not (eq gnus-build-sparse-threads 'some))
8781                    (not (eq gnus-build-sparse-threads 'more))
8782                    (null gnus-thread-expunge-below)))
8783     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8784     (setq gnus-newsgroup-limit nil)
8785     (mapatoms
8786      (lambda (node)
8787        (unless (car (symbol-value node))
8788          ;; These threads have no parents -- they are roots.
8789          (let ((nodes (cdr (symbol-value node)))
8790                thread)
8791            (while nodes
8792              (if (and gnus-thread-expunge-below
8793                       (< (gnus-thread-total-score (car nodes))
8794                          gnus-thread-expunge-below))
8795                  (gnus-expunge-thread (pop nodes))
8796                (setq thread (pop nodes))
8797                (gnus-summary-limit-children thread))))))
8798      gnus-newsgroup-dependencies)
8799     ;; If this limitation resulted in an empty group, we might
8800     ;; pop the previous limit and use it instead.
8801     (when (and (not gnus-newsgroup-limit)
8802                show-if-empty)
8803       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8804     gnus-newsgroup-limit))
8805
8806 (defun gnus-summary-limit-children (thread)
8807   "Return 1 if this subthread is visible and 0 if it is not."
8808   ;; First we get the number of visible children to this thread.  This
8809   ;; is done by recursing down the thread using this function, so this
8810   ;; will really go down to a leaf article first, before slowly
8811   ;; working its way up towards the root.
8812   (when thread
8813     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8814            (children
8815            (if (cdr thread)
8816                (apply '+ (mapcar 'gnus-summary-limit-children
8817                                  (cdr thread)))
8818              0))
8819            (number (mail-header-number (car thread)))
8820            score)
8821       (if (and
8822            (not (memq number gnus-newsgroup-marked))
8823            (or
8824             ;; If this article is dormant and has absolutely no visible
8825             ;; children, then this article isn't visible.
8826             (and (memq number gnus-newsgroup-dormant)
8827                  (zerop children))
8828             ;; If this is "fetch-old-headered" and there is no
8829             ;; visible children, then we don't want this article.
8830             (and (or (eq gnus-fetch-old-headers 'some)
8831                      (numberp gnus-fetch-old-headers))
8832                  (gnus-summary-article-ancient-p number)
8833                  (zerop children))
8834             ;; If this is "fetch-old-headered" and `invisible', then
8835             ;; we don't want this article.
8836             (and (eq gnus-fetch-old-headers 'invisible)
8837                  (gnus-summary-article-ancient-p number))
8838             ;; If this is a sparsely inserted article with no children,
8839             ;; we don't want it.
8840             (and (eq gnus-build-sparse-threads 'some)
8841                  (gnus-summary-article-sparse-p number)
8842                  (zerop children))
8843             ;; If we use expunging, and this article is really
8844             ;; low-scored, then we don't want this article.
8845             (when (and gnus-summary-expunge-below
8846                        (< (setq score
8847                                 (or (cdr (assq number gnus-newsgroup-scored))
8848                                     gnus-summary-default-score))
8849                           gnus-summary-expunge-below))
8850               ;; We increase the expunge-tally here, but that has
8851               ;; nothing to do with the limits, really.
8852               (incf gnus-newsgroup-expunged-tally)
8853               ;; We also mark as read here, if that's wanted.
8854               (when (and gnus-summary-mark-below
8855                          (< score gnus-summary-mark-below))
8856                 (setq gnus-newsgroup-unreads
8857                       (delq number gnus-newsgroup-unreads))
8858                 (if gnus-newsgroup-auto-expire
8859                     (push number gnus-newsgroup-expirable)
8860                   (push (cons number gnus-low-score-mark)
8861                         gnus-newsgroup-reads)))
8862               t)
8863             ;; Do the `display' group parameter.
8864             (and gnus-newsgroup-display
8865                  (let ((gnus-number number))
8866                    (not (funcall gnus-newsgroup-display))))))
8867           ;; Nope, invisible article.
8868           0
8869         ;; Ok, this article is to be visible, so we add it to the limit
8870         ;; and return 1.
8871         (push number gnus-newsgroup-limit)
8872         1))))
8873
8874 (defun gnus-expunge-thread (thread)
8875   "Mark all articles in THREAD as read."
8876   (let* ((number (mail-header-number (car thread))))
8877     (incf gnus-newsgroup-expunged-tally)
8878     ;; We also mark as read here, if that's wanted.
8879     (setq gnus-newsgroup-unreads
8880           (delq number gnus-newsgroup-unreads))
8881     (if gnus-newsgroup-auto-expire
8882         (push number gnus-newsgroup-expirable)
8883       (push (cons number gnus-low-score-mark)
8884             gnus-newsgroup-reads)))
8885   ;; Go recursively through all subthreads.
8886   (mapcar 'gnus-expunge-thread (cdr thread)))
8887
8888 ;; Summary article oriented commands
8889
8890 (defun gnus-summary-refer-parent-article (n)
8891   "Refer parent article N times.
8892 If N is negative, go to ancestor -N instead.
8893 The difference between N and the number of articles fetched is returned."
8894   (interactive "p")
8895   (let ((skip 1)
8896         error header ref)
8897     (when (not (natnump n))
8898       (setq skip (abs n)
8899             n 1))
8900     (while (and (> n 0)
8901                 (not error))
8902       (setq header (gnus-summary-article-header))
8903       (if (and (eq (mail-header-number header)
8904                    (cdr gnus-article-current))
8905                (equal gnus-newsgroup-name
8906                       (car gnus-article-current)))
8907           ;; If we try to find the parent of the currently
8908           ;; displayed article, then we take a look at the actual
8909           ;; References header, since this is slightly more
8910           ;; reliable than the References field we got from the
8911           ;; server.
8912           (with-current-buffer gnus-original-article-buffer
8913             (nnheader-narrow-to-headers)
8914             (unless (setq ref (message-fetch-field "references"))
8915               (when (setq ref (message-fetch-field "in-reply-to"))
8916                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8917             (widen))
8918         (setq ref
8919               ;; It's not the current article, so we take a bet on
8920               ;; the value we got from the server.
8921               (mail-header-references header)))
8922       (if (and ref
8923                (not (equal ref "")))
8924           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8925             (gnus-message 1 "Couldn't find parent"))
8926         (gnus-message 1 "No references in article %d"
8927                       (gnus-summary-article-number))
8928         (setq error t))
8929       (decf n))
8930     (gnus-summary-position-point)
8931     n))
8932
8933 (defun gnus-summary-refer-references ()
8934   "Fetch all articles mentioned in the References header.
8935 Return the number of articles fetched."
8936   (interactive)
8937   (let ((ref (mail-header-references (gnus-summary-article-header)))
8938         (current (gnus-summary-article-number))
8939         (n 0))
8940     (if (or (not ref)
8941             (equal ref ""))
8942         (error "No References in the current article")
8943       ;; For each Message-ID in the References header...
8944       (while (string-match "<[^>]*>" ref)
8945         (incf n)
8946         ;; ... fetch that article.
8947         (gnus-summary-refer-article
8948          (prog1 (match-string 0 ref)
8949            (setq ref (substring ref (match-end 0))))))
8950       (gnus-summary-goto-subject current)
8951       (gnus-summary-position-point)
8952       n)))
8953
8954 (defun gnus-delete-duplicate-headers (headers)
8955   ;; First remove leading duplicates.
8956   (while (and (> (length headers) 1)
8957               (= (mail-header-number (car headers))
8958                  (mail-header-number (cadr headers))))
8959     (pop headers))
8960   ;; Then the rest.
8961   (let ((result headers))
8962     (while (> (length headers) 1)
8963       (if (= (mail-header-number (car headers))
8964              (mail-header-number (cadr headers)))
8965           (setcdr headers (cddr headers))
8966         (pop headers)))
8967     result))
8968
8969 (defun gnus-summary-refer-thread (&optional limit)
8970   "Fetch all articles in the current thread. For backends that
8971 know how to search for threads (currently only 'nnimap) a
8972 non-numeric prefix arg will use nnir to search the entire
8973 server; without a prefix arg only the current group is
8974 searched. If the variable `gnus-refer-thread-use-nnir' is
8975 non-nil the prefix arg has the reverse meaning. If no
8976 backend-specific 'request-thread function is available fetch
8977 LIMIT (the numerical prefix) old headers. If LIMIT is
8978 non-numeric or nil fetch the number specified by the
8979 `gnus-refer-thread-limit' variable."
8980   (interactive "P")
8981   (gnus-warp-to-article)
8982   (let* ((header (gnus-summary-article-header))
8983          (id (mail-header-id header))
8984          (gnus-inhibit-demon t)
8985          (gnus-summary-ignore-duplicates t)
8986          (gnus-read-all-available-headers t)
8987          (gnus-refer-thread-use-nnir
8988           (if (and (not (null limit)) (listp limit))
8989               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
8990          (new-headers
8991           (if (gnus-check-backend-function
8992                'request-thread gnus-newsgroup-name)
8993               (gnus-request-thread header gnus-newsgroup-name)
8994             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
8995                             gnus-refer-thread-limit))
8996                    (last (if (numberp limit)
8997                              (min (+ (mail-header-number header)
8998                                      limit)
8999                                   gnus-newsgroup-highest)
9000                            gnus-newsgroup-highest))
9001                    (subject (gnus-simplify-subject
9002                              (mail-header-subject header)))
9003                    (refs (split-string (or (mail-header-references header)
9004                                            "")))
9005                    (gnus-parse-headers-hook
9006                     `(lambda () (goto-char (point-min))
9007                       (keep-lines
9008                        (regexp-opt ',(append refs (list id subject)))))))
9009               (gnus-fetch-headers (list last) (if (numberp limit)
9010                                                   (* 2 limit) limit) t))))
9011          article-ids)
9012     (when (listp new-headers)
9013       (dolist (header new-headers)
9014         (push (mail-header-number header) article-ids)
9015         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9016           (push (mail-header-number header) gnus-newsgroup-unreads)
9017           (setq gnus-newsgroup-unselected
9018                 (delete (mail-header-number header)
9019                         gnus-newsgroup-unselected))))
9020       (setq gnus-newsgroup-headers
9021             (gnus-delete-duplicate-headers
9022              (gnus-merge
9023               'list gnus-newsgroup-headers new-headers
9024               'gnus-article-sort-by-number)))
9025       (setq gnus-newsgroup-articles
9026             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9027       (gnus-summary-limit-include-thread id)))
9028   (gnus-summary-show-thread))
9029
9030 (defun gnus-summary-refer-article (message-id)
9031   "Fetch an article specified by MESSAGE-ID."
9032   (interactive "sMessage-ID: ")
9033   (gnus-warp-to-article)
9034   (when (and (stringp message-id)
9035              (not (zerop (length message-id))))
9036     (setq message-id (gnus-replace-in-string message-id " " ""))
9037     ;; Construct the correct Message-ID if necessary.
9038     ;; Suggested by tale@pawl.rpi.edu.
9039     (unless (string-match "^<" message-id)
9040       (setq message-id (concat "<" message-id)))
9041     (unless (string-match ">$" message-id)
9042       (setq message-id (concat message-id ">")))
9043     ;; People often post MIDs from URLs, so unhex it:
9044     (unless (string-match "@" message-id)
9045       (setq message-id (gnus-url-unhex-string message-id)))
9046     (let* ((header (gnus-id-to-header message-id))
9047            (sparse (and header
9048                         (gnus-summary-article-sparse-p
9049                          (mail-header-number header))
9050                         (memq (mail-header-number header)
9051                               gnus-newsgroup-limit)))
9052            number)
9053       (cond
9054        ;; If the article is present in the buffer we just go to it.
9055        ((and header
9056              (or (not (gnus-summary-article-sparse-p
9057                        (mail-header-number header)))
9058                  sparse))
9059         (prog1
9060             (gnus-summary-goto-article
9061              (mail-header-number header) nil t)
9062           (when sparse
9063             (gnus-summary-update-article (mail-header-number header)))))
9064        (t
9065         ;; We fetch the article.
9066         (catch 'found
9067           (dolist (gnus-override-method (gnus-refer-article-methods))
9068             (when (and (gnus-check-server gnus-override-method)
9069                        ;; Fetch the header,
9070                        (setq number (gnus-summary-insert-subject message-id)))
9071               ;; and display the article.
9072               (gnus-summary-select-article nil nil nil number)
9073               (throw 'found t)))
9074           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9075
9076 (defun gnus-refer-article-methods ()
9077   "Return a list of referable methods."
9078   (cond
9079    ;; No method, so we default to current and native.
9080    ((null gnus-refer-article-method)
9081     (list gnus-current-select-method gnus-select-method))
9082    ;; Current.
9083    ((eq 'current gnus-refer-article-method)
9084     (list gnus-current-select-method))
9085    ;; List of select methods.
9086    ((not (and (symbolp (car gnus-refer-article-method))
9087               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9088     (let (out)
9089       (dolist (method gnus-refer-article-method)
9090         (push (if (eq 'current method)
9091                   gnus-current-select-method
9092                 (if (eq 'nnir (car method))
9093                     (list
9094                      'nnir
9095                      (or (cadr method)
9096                          (gnus-method-to-server gnus-current-select-method)))
9097                   method))
9098               out))
9099       (nreverse out)))
9100    ;; One single select method.
9101    (t
9102     (list gnus-refer-article-method))))
9103
9104 (defun gnus-summary-edit-parameters ()
9105   "Edit the group parameters of the current group."
9106   (interactive)
9107   (gnus-group-edit-group gnus-newsgroup-name 'params))
9108
9109 (defun gnus-summary-customize-parameters ()
9110   "Customize the group parameters of the current group."
9111   (interactive)
9112   (gnus-group-customize gnus-newsgroup-name))
9113
9114 (defun gnus-summary-enter-digest-group (&optional force)
9115   "Enter an nndoc group based on the current article.
9116 If FORCE, force a digest interpretation.  If not, try to guess
9117 what the document format is.
9118
9119 To control what happens when you exit the group, see the
9120 `gnus-auto-select-on-ephemeral-exit' variable."
9121   (interactive "P")
9122   (let ((conf gnus-current-window-configuration))
9123     (save-window-excursion
9124       (save-excursion
9125         (let (gnus-article-prepare-hook
9126               gnus-display-mime-function
9127               gnus-break-pages)
9128           (gnus-summary-select-article))))
9129     (setq gnus-current-window-configuration conf)
9130     (let* ((name (format "%s-%d"
9131                          (gnus-group-prefixed-name
9132                           gnus-newsgroup-name (list 'nndoc ""))
9133                          (with-current-buffer gnus-summary-buffer
9134                            gnus-current-article)))
9135            (ogroup gnus-newsgroup-name)
9136            (params (append (gnus-info-params (gnus-get-info ogroup))
9137                            (list (cons 'to-group ogroup))
9138                            (list (cons 'parent-group ogroup))
9139                            (list (cons 'save-article-group ogroup))))
9140            (case-fold-search t)
9141            (buf (current-buffer))
9142            dig to-address charset)
9143       (with-current-buffer gnus-original-article-buffer
9144         ;; Have the digest group inherit the main mail address of
9145         ;; the parent article.
9146         (when (setq to-address (or (gnus-fetch-field "reply-to")
9147                                    (gnus-fetch-field "from")))
9148           (setq params
9149                 (append
9150                  (list (cons 'to-address
9151                              (funcall gnus-decode-encoded-address-function
9152                                       to-address))))))
9153         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9154         (insert-buffer-substring gnus-original-article-buffer)
9155         (narrow-to-region
9156          (goto-char (point-min))
9157          (or (search-forward "\n\n" nil t) (point)))
9158         ;; Remove lines that may lead nndoc to misinterpret the
9159         ;; document type.
9160         (goto-char (point-min))
9161         (delete-matching-lines "^Path:\\|^From ")
9162         ;; Parse charset, and decode content transfer encoding.
9163         (setq charset (mail-content-type-get
9164                        (mail-header-parse-content-type
9165                         (or (gnus-fetch-field "content-type") ""))
9166                        'charset))
9167         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9168           (when encoding
9169             (message-remove-header "content-transfer-encoding")
9170             (goto-char (point-max))
9171             (widen)
9172             (narrow-to-region (point) (point-max))
9173             (mm-decode-content-transfer-encoding
9174              (intern (downcase (mail-header-strip encoding))))))
9175         (widen))
9176       (unwind-protect
9177           (if (let ((gnus-newsgroup-ephemeral-charset
9178                      (if charset
9179                          (intern (downcase (gnus-strip-whitespace charset)))
9180                        gnus-newsgroup-charset))
9181                     (gnus-newsgroup-ephemeral-ignored-charsets
9182                      gnus-newsgroup-ignored-charsets))
9183                 (gnus-group-read-ephemeral-group
9184                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9185                               (nndoc-article-type
9186                                ,(if force 'mbox 'guess)))
9187                  t nil nil nil
9188                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9189                                                         "ADAPT")))))
9190               ;; Make all postings to this group go to the parent group.
9191               (nconc (gnus-info-params (gnus-get-info name))
9192                      params)
9193             ;; Couldn't select this doc group.
9194             (switch-to-buffer buf)
9195             (gnus-set-global-variables)
9196             (gnus-configure-windows 'summary)
9197             (gnus-message 3 "Article couldn't be entered?"))
9198         (kill-buffer dig)))))
9199
9200 (defun gnus-summary-read-document (n)
9201   "Open a new group based on the current article(s).
9202 This will allow you to read digests and other similar
9203 documents as newsgroups.
9204 Obeys the standard process/prefix convention."
9205   (interactive "P")
9206   (let* ((ogroup gnus-newsgroup-name)
9207          (params (append (gnus-info-params (gnus-get-info ogroup))
9208                          (list (cons 'to-group ogroup))))
9209          group egroup groups vgroup)
9210     (dolist (article (gnus-summary-work-articles n))
9211       (setq group (format "%s-%d" gnus-newsgroup-name article))
9212       (gnus-summary-remove-process-mark article)
9213       (when (gnus-summary-display-article article)
9214         (save-excursion ;;What for?
9215           (with-temp-buffer
9216             (insert-buffer-substring gnus-original-article-buffer)
9217             ;; Remove some headers that may lead nndoc to make
9218             ;; the wrong guess.
9219             (message-narrow-to-head)
9220             (goto-char (point-min))
9221             (delete-matching-lines "^Path:\\|^From ")
9222             (widen)
9223             (if (setq egroup
9224                       (gnus-group-read-ephemeral-group
9225                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9226                                      (nndoc-article-type guess))
9227                        t nil t))
9228                 (progn
9229                   ;; Make all postings to this group go to the parent group.
9230                   (nconc (gnus-info-params (gnus-get-info egroup))
9231                          params)
9232                   (push egroup groups))
9233               ;; Couldn't select this doc group.
9234               (gnus-error 3 "Article couldn't be entered"))))))
9235     ;; Now we have selected all the documents.
9236     (cond
9237      ((not groups)
9238       (error "None of the articles could be interpreted as documents"))
9239      ((gnus-group-read-ephemeral-group
9240        (setq vgroup (format
9241                      "nnvirtual:%s-%s" gnus-newsgroup-name
9242                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9243        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9244        t
9245        (cons (current-buffer) 'summary)))
9246      (t
9247       (error "Couldn't select virtual nndoc group")))))
9248
9249 (defun gnus-summary-widget-forward (arg)
9250   "Move point to the next field or button in the article.
9251 With optional ARG, move across that many fields."
9252   (interactive "p")
9253   (gnus-summary-select-article)
9254   (gnus-configure-windows 'article)
9255   (select-window (gnus-get-buffer-window gnus-article-buffer))
9256   (widget-forward arg))
9257
9258 (defun gnus-summary-widget-backward (arg)
9259   "Move point to the previous field or button in the article.
9260 With optional ARG, move across that many fields."
9261   (interactive "p")
9262   (gnus-summary-select-article)
9263   (gnus-configure-windows 'article)
9264   (select-window (gnus-get-buffer-window gnus-article-buffer))
9265   (unless (widget-at (point))
9266     (goto-char (point-max)))
9267   (widget-backward arg))
9268
9269 (defun gnus-summary-isearch-article (&optional regexp-p)
9270   "Do incremental search forward on the current article.
9271 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9272   (interactive "P")
9273   (gnus-summary-select-article)
9274   (gnus-configure-windows 'article)
9275   (gnus-eval-in-buffer-window gnus-article-buffer
9276     (save-restriction
9277       (widen)
9278       (isearch-forward regexp-p))))
9279
9280 (defun gnus-summary-repeat-search-article-forward ()
9281   "Repeat the previous search forwards."
9282   (interactive)
9283   (unless gnus-last-search-regexp
9284     (error "No previous search"))
9285   (gnus-summary-search-article-forward gnus-last-search-regexp))
9286
9287 (defun gnus-summary-repeat-search-article-backward ()
9288   "Repeat the previous search backwards."
9289   (interactive)
9290   (unless gnus-last-search-regexp
9291     (error "No previous search"))
9292   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9293
9294 (defun gnus-summary-search-article-forward (regexp &optional backward)
9295   "Search for an article containing REGEXP forward.
9296 If BACKWARD, search backward instead."
9297   (interactive
9298    (list (read-string
9299           (format "Search article %s (regexp%s): "
9300                   (if current-prefix-arg "backward" "forward")
9301                   (if gnus-last-search-regexp
9302                       (concat ", default " gnus-last-search-regexp)
9303                     "")))
9304          current-prefix-arg))
9305   (if (string-equal regexp "")
9306       (setq regexp (or gnus-last-search-regexp ""))
9307     (setq gnus-last-search-regexp regexp)
9308     (setq gnus-article-before-search gnus-current-article))
9309   ;; Intentionally set gnus-last-article.
9310   (setq gnus-last-article gnus-article-before-search)
9311   (let ((gnus-last-article gnus-last-article))
9312     (if (gnus-summary-search-article regexp backward)
9313         (gnus-summary-show-thread)
9314       (signal 'search-failed (list regexp)))))
9315
9316 (defun gnus-summary-search-article-backward (regexp)
9317   "Search for an article containing REGEXP backward."
9318   (interactive
9319    (list (read-string
9320           (format "Search article backward (regexp%s): "
9321                   (if gnus-last-search-regexp
9322                       (concat ", default " gnus-last-search-regexp)
9323                     "")))))
9324   (gnus-summary-search-article-forward regexp 'backward))
9325
9326 (defun gnus-summary-search-article (regexp &optional backward)
9327   "Search for an article containing REGEXP.
9328 Optional argument BACKWARD means do search for backward.
9329 `gnus-select-article-hook' is not called during the search."
9330   ;; We have to require this here to make sure that the following
9331   ;; dynamic binding isn't shadowed by autoloading.
9332   (require 'gnus-async)
9333   (require 'gnus-art)
9334   (let ((gnus-select-article-hook nil)  ;Disable hook.
9335         (gnus-article-prepare-hook nil)
9336         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9337         (gnus-use-article-prefetch nil)
9338         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9339         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9340         (gnus-visual nil)
9341         (gnus-keep-backlog nil)
9342         (gnus-break-pages nil)
9343         (gnus-summary-display-arrow nil)
9344         (gnus-updated-mode-lines nil)
9345         (gnus-auto-center-summary nil)
9346         (sum (current-buffer))
9347         (gnus-display-mime-function nil)
9348         (found nil)
9349         point)
9350     (gnus-save-hidden-threads
9351       (gnus-summary-select-article)
9352       (set-buffer gnus-article-buffer)
9353       (goto-char (window-point (get-buffer-window (current-buffer))))
9354       (when backward
9355         (forward-line -1))
9356       (while (not found)
9357         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9358         (if (if backward
9359                 (re-search-backward regexp nil t)
9360               (re-search-forward regexp nil t))
9361             ;; We found the regexp.
9362             (progn
9363               (setq found 'found)
9364               (beginning-of-line)
9365               (set-window-start
9366                (get-buffer-window (current-buffer))
9367                (point))
9368               (forward-line 1)
9369               (set-window-point
9370                (get-buffer-window (current-buffer))
9371                (point))
9372               (set-buffer sum)
9373               (setq point (point)))
9374           ;; We didn't find it, so we go to the next article.
9375           (set-buffer sum)
9376           (setq found 'not)
9377           (while (eq found 'not)
9378             (if (not (if backward (gnus-summary-find-prev)
9379                        (gnus-summary-find-next)))
9380                 ;; No more articles.
9381                 (setq found t)
9382               ;; Select the next article and adjust point.
9383               (unless (gnus-summary-article-sparse-p
9384                        (gnus-summary-article-number))
9385                 (setq found nil)
9386                 (gnus-summary-select-article)
9387                 (set-buffer gnus-article-buffer)
9388                 (widen)
9389                 (goto-char (if backward (point-max) (point-min))))))))
9390       (gnus-message 7 ""))
9391     ;; Return whether we found the regexp.
9392     (when (eq found 'found)
9393       (goto-char point)
9394       (gnus-summary-show-thread)
9395       (gnus-summary-goto-subject gnus-current-article)
9396       (gnus-summary-position-point)
9397       t)))
9398
9399 (defun gnus-find-matching-articles (header regexp)
9400   "Return a list of all articles that match REGEXP on HEADER.
9401 This search includes all articles in the current group that Gnus has
9402 fetched headers for, whether they are displayed or not."
9403   (let ((articles nil)
9404         ;; Can't eta-reduce because it's a macro.
9405         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9406         (case-fold-search t))
9407     (dolist (header gnus-newsgroup-headers)
9408       (when (string-match regexp (funcall func header))
9409         (push (mail-header-number header) articles)))
9410     (nreverse articles)))
9411
9412 (defun gnus-summary-find-matching (header regexp &optional backward unread
9413                                           not-case-fold not-matching)
9414   "Return a list of all articles that match REGEXP on HEADER.
9415 The search stars on the current article and goes forwards unless
9416 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9417 If UNREAD is non-nil, only unread articles will
9418 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9419 in the comparisons. If NOT-MATCHING, return a list of all articles that
9420 not match REGEXP on HEADER."
9421   (let ((case-fold-search (not not-case-fold))
9422         articles d func)
9423     (if (consp header)
9424         (if (eq (car header) 'extra)
9425             (setq func
9426                   `(lambda (h)
9427                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9428                          "")))
9429           (error "%s is an invalid header" header))
9430       (unless (fboundp (intern (concat "mail-header-" header)))
9431         (error "%s is not a valid header" header))
9432       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9433     (dolist (d (if (eq backward 'all)
9434                    gnus-newsgroup-data
9435                  (gnus-data-find-list
9436                   (gnus-summary-article-number)
9437                   (gnus-data-list backward))))
9438       (when (and (or (not unread)       ; We want all articles...
9439                      (gnus-data-unread-p d)) ; Or just unreads.
9440                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9441                  (if not-matching
9442                      (not (string-match
9443                            regexp
9444                            (funcall func (gnus-data-header d))))
9445                    (string-match regexp
9446                                  (funcall func (gnus-data-header d)))))
9447         (push (gnus-data-number d) articles))) ; Success!
9448     (nreverse articles)))
9449
9450 (defun gnus-summary-execute-command (header regexp command &optional backward)
9451   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9452 If HEADER is an empty string (or nil), the match is done on the entire
9453 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9454   (interactive
9455    (list (let ((completion-ignore-case t))
9456            (gnus-completing-read
9457             "Header name"
9458             (mapcar 'symbol-name
9459                     (append
9460                      '(Number Subject From Lines Date
9461                        Message-ID Xref References Body)
9462                      gnus-extra-headers))
9463             'require-match))
9464          (read-string "Regexp: ")
9465          (read-key-sequence "Command: ")
9466          current-prefix-arg))
9467   (when (equal header "Body")
9468     (setq header ""))
9469   ;; Hidden thread subtrees must be searched as well.
9470   (gnus-summary-show-all-threads)
9471   ;; We don't want to change current point nor window configuration.
9472   (save-excursion
9473     (save-window-excursion
9474       (let (gnus-visual
9475             gnus-treat-strip-trailing-blank-lines
9476             gnus-treat-strip-leading-blank-lines
9477             gnus-treat-strip-multiple-blank-lines
9478             gnus-treat-hide-boring-headers
9479             gnus-treat-fold-newsgroups
9480             gnus-article-prepare-hook)
9481         (gnus-message 6 "Executing %s..." (key-description command))
9482         ;; We'd like to execute COMMAND interactively so as to give arguments.
9483         (gnus-execute header regexp
9484                       `(call-interactively ',(key-binding command))
9485                       backward)
9486         (gnus-message 6 "Executing %s...done" (key-description command))))))
9487
9488 (defun gnus-summary-beginning-of-article ()
9489   "Scroll the article back to the beginning."
9490   (interactive)
9491   (gnus-summary-select-article)
9492   (gnus-configure-windows 'article)
9493   (gnus-eval-in-buffer-window gnus-article-buffer
9494     (widen)
9495     (goto-char (point-min))
9496     (when gnus-break-pages
9497       (gnus-narrow-to-page))))
9498
9499 (defun gnus-summary-end-of-article ()
9500   "Scroll to the end of the article."
9501   (interactive)
9502   (gnus-summary-select-article)
9503   (gnus-configure-windows 'article)
9504   (gnus-eval-in-buffer-window gnus-article-buffer
9505     (widen)
9506     (goto-char (point-max))
9507     (recenter -3)
9508     (when gnus-break-pages
9509       (gnus-narrow-to-page))))
9510
9511 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9512   "Truncate to LEN and quote all \"(\"'s in STRING."
9513   (gnus-replace-in-string (if (and len (> (length string) len))
9514                               (substring string 0 len)
9515                             string)
9516                           "[()]" "\\\\\\&"))
9517
9518 (defun gnus-summary-print-article (&optional filename n)
9519   "Generate and print a PostScript image of the process-marked (mail) articles.
9520
9521 If used interactively, print the current article if none are
9522 process-marked.  With prefix arg, prompt the user for the name of the
9523 file to save in.
9524
9525 When used from Lisp, accept two optional args FILENAME and N.  N means
9526 to print the next N articles.  If N is negative, print the N previous
9527 articles.  If N is nil and articles have been marked with the process
9528 mark, print these instead.
9529
9530 If the optional first argument FILENAME is nil, send the image to the
9531 printer.  If FILENAME is a string, save the PostScript image in a file with
9532 that name.  If FILENAME is a number, prompt the user for the name of the file
9533 to save in."
9534   (interactive (list (ps-print-preprint current-prefix-arg)))
9535   (dolist (article (gnus-summary-work-articles n))
9536     (gnus-summary-select-article nil nil 'pseudo article)
9537     (gnus-eval-in-buffer-window gnus-article-buffer
9538       (gnus-print-buffer))
9539     (gnus-summary-remove-process-mark article))
9540   (ps-despool filename))
9541
9542 (defun gnus-print-buffer ()
9543   (let ((ps-left-header
9544          (list
9545           (concat "("
9546                   (gnus-summary-print-truncate-and-quote
9547                    (mail-header-subject gnus-current-headers)
9548                    66) ")")
9549           (concat "("
9550                   (gnus-summary-print-truncate-and-quote
9551                    (mail-header-from gnus-current-headers)
9552                    45) ")")))
9553         (ps-right-header
9554          (list
9555           "/pagenumberstring load"
9556           (concat "("
9557                   (mail-header-date gnus-current-headers) ")"))))
9558     (gnus-run-hooks 'gnus-ps-print-hook)
9559     (save-excursion
9560       (if ps-print-color-p
9561           (ps-spool-buffer-with-faces)
9562         (ps-spool-buffer)))))
9563
9564 (defun gnus-summary-show-complete-article ()
9565   "Show a complete version of the current article.
9566 This is only useful if you're looking at a partial version of the
9567 article currently."
9568   (interactive)
9569   (let ((gnus-keep-backlog nil)
9570         (gnus-use-cache nil)
9571         (gnus-agent nil)
9572         (variable (intern
9573                    (format "%s-fetch-partial-articles"
9574                            (car (gnus-find-method-for-group
9575                                  gnus-newsgroup-name)))
9576                    obarray))
9577         old-val)
9578     (unwind-protect
9579         (progn
9580           (setq old-val (symbol-value variable))
9581           (set variable nil)
9582           (gnus-flush-original-article-buffer)
9583           (gnus-summary-show-article))
9584       (set variable old-val))))
9585
9586 (defun gnus-summary-show-article (&optional arg)
9587   "Force redisplaying of the current article.
9588 If ARG (the prefix) is a number, show the article with the charset
9589 defined in `gnus-summary-show-article-charset-alist', or the charset
9590 input.
9591 If ARG (the prefix) is non-nil and not a number, show the article,
9592 but without running any of the article treatment functions
9593 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9594 C-u g', show the raw article."
9595   (interactive "P")
9596   (cond
9597    ((numberp arg)
9598     (gnus-summary-show-article t)
9599     (let ((gnus-newsgroup-charset
9600            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9601                (mm-read-coding-system
9602                 "View as charset: " ;; actually it is coding system.
9603                 (with-current-buffer gnus-article-buffer
9604                   (mm-detect-coding-region (point) (point-max))))))
9605           (gnus-newsgroup-ignored-charsets 'gnus-all))
9606       (gnus-summary-select-article nil 'force)
9607       (let ((deps gnus-newsgroup-dependencies)
9608             head header lines)
9609         (with-current-buffer gnus-original-article-buffer
9610           (save-restriction
9611             (message-narrow-to-head)
9612             (setq head (buffer-string))
9613             (goto-char (point-min))
9614             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9615               (goto-char (point-max))
9616               (widen)
9617               (setq lines (1- (count-lines (point) (point-max))))))
9618           (with-temp-buffer
9619             (insert (format "211 %d Article retrieved.\n"
9620                             (cdr gnus-article-current)))
9621             (insert head)
9622             (if lines (insert (format "Lines: %d\n" lines)))
9623             (insert ".\n")
9624             (let ((nntp-server-buffer (current-buffer)))
9625               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9626         (gnus-data-set-header
9627          (gnus-data-find (cdr gnus-article-current))
9628          header)
9629         (gnus-summary-update-article-line
9630          (cdr gnus-article-current) header)
9631         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9632           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9633    ((not arg)
9634     ;; Select the article the normal way.
9635     (if (eq mm-text-html-renderer 'shr)
9636         (progn
9637           (require 'shr)
9638           (let ((shr-ignore-cache t))
9639             (gnus-summary-select-article nil 'force)))
9640       (gnus-summary-select-article nil 'force)))
9641    ((equal arg '(16))
9642     ;; C-u C-u g
9643     (let ((gnus-inhibit-article-treatments t))
9644       (gnus-summary-select-article nil 'force)))
9645    (t
9646     ;; We have to require this here to make sure that the following
9647     ;; dynamic binding isn't shadowed by autoloading.
9648     (require 'gnus-async)
9649     (require 'gnus-art)
9650     ;; Bind the article treatment functions to nil.
9651     (let ((gnus-have-all-headers t)
9652           gnus-article-prepare-hook
9653           gnus-article-decode-hook
9654           gnus-display-mime-function
9655           gnus-break-pages)
9656       ;; Destroy any MIME parts.
9657       (when (gnus-buffer-live-p gnus-article-buffer)
9658         (with-current-buffer gnus-article-buffer
9659           (gnus-article-stop-animations)
9660           (gnus-stop-downloads)
9661           (mm-destroy-parts gnus-article-mime-handles)
9662           ;; Set it to nil for safety reason.
9663           (setq gnus-article-mime-handle-alist nil)
9664           (setq gnus-article-mime-handles nil)))
9665       (gnus-summary-select-article nil 'force))))
9666   (gnus-summary-goto-subject gnus-current-article)
9667   (gnus-summary-position-point))
9668
9669 (defun gnus-summary-show-raw-article ()
9670   "Show the raw article without any article massaging functions being run."
9671   (interactive)
9672   (gnus-summary-show-article t))
9673
9674 (defun gnus-summary-verbose-headers (&optional arg)
9675   "Toggle permanent full header display.
9676 If ARG is a positive number, turn header display on.
9677 If ARG is a negative number, turn header display off."
9678   (interactive "P")
9679   (setq gnus-show-all-headers
9680         (cond ((or (not (numberp arg))
9681                    (zerop arg))
9682                (not gnus-show-all-headers))
9683               ((natnump arg)
9684                t)))
9685   (gnus-summary-show-article))
9686
9687 (defun gnus-summary-toggle-header (&optional arg)
9688   "Show the headers if they are hidden, or hide them if they are shown.
9689 If ARG is a positive number, show the entire header.
9690 If ARG is a negative number, hide the unwanted header lines."
9691   (interactive "P")
9692   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9693                      (get-buffer-window gnus-article-buffer t))))
9694     (with-current-buffer gnus-article-buffer
9695       (widen)
9696       (article-narrow-to-head)
9697       (let* ((inhibit-read-only t)
9698              (inhibit-point-motion-hooks t)
9699              (hidden (if (numberp arg)
9700                          (>= arg 0)
9701                        (or (not (looking-at "[^ \t\n]+:"))
9702                            (gnus-article-hidden-text-p 'headers))))
9703              s e)
9704         (delete-region (point-min) (point-max))
9705         (with-current-buffer gnus-original-article-buffer
9706           (goto-char (setq s (point-min)))
9707           (setq e (if (search-forward "\n\n" nil t)
9708                       (1- (point))
9709                     (point-max))))
9710         (insert-buffer-substring gnus-original-article-buffer s e)
9711         (run-hooks 'gnus-article-decode-hook)
9712         (if hidden
9713             (let ((gnus-treat-hide-headers nil)
9714                   (gnus-treat-hide-boring-headers nil))
9715               (gnus-delete-wash-type 'headers)
9716               (gnus-treat-article 'head))
9717           (gnus-treat-article 'head))
9718         (widen)
9719         (if window
9720             (set-window-start window (goto-char (point-min))))
9721         (if gnus-break-pages
9722             (gnus-narrow-to-page)
9723           (when (gnus-visual-p 'page-marker)
9724             (let ((inhibit-read-only t))
9725               (gnus-remove-text-with-property 'gnus-prev)
9726               (gnus-remove-text-with-property 'gnus-next))))
9727         (gnus-set-mode-line 'article)))))
9728
9729 (defun gnus-summary-show-all-headers ()
9730   "Make all header lines visible."
9731   (interactive)
9732   (gnus-summary-toggle-header 1))
9733
9734 (defun gnus-summary-caesar-message (&optional arg)
9735   "Caesar rotate the current article by 13.
9736 With a non-numerical prefix, also rotate headers.  A numerical
9737 prefix specifies how many places to rotate each letter forward."
9738   (interactive "P")
9739   (gnus-summary-select-article)
9740   (let ((mail-header-separator ""))
9741     (gnus-eval-in-buffer-window gnus-article-buffer
9742       (save-restriction
9743         (widen)
9744         (let ((start (window-start))
9745               (inhibit-read-only t))
9746           (if (equal arg '(4))
9747               (message-caesar-buffer-body nil t)
9748             (message-caesar-buffer-body arg))
9749           (set-window-start (get-buffer-window (current-buffer)) start)))))
9750   ;; Create buttons and stuff...
9751   (gnus-treat-article nil))
9752
9753 (declare-function idna-to-unicode "ext:idna" (str))
9754
9755 (defun gnus-summary-idna-message (&optional arg)
9756   "Decode IDNA encoded domain names in the current articles.
9757 IDNA encoded domain names looks like `xn--bar'.  If a string
9758 remain unencoded after running this function, it is likely an
9759 invalid IDNA string (`xn--bar' is invalid).
9760
9761 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9762 installed for this command to work."
9763   (interactive "P")
9764   (if (not (and (condition-case nil (require 'idna)
9765                   (file-error))
9766                 (mm-coding-system-p 'utf-8)
9767                 (executable-find (symbol-value 'idna-program))))
9768       (gnus-message
9769        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9770     (gnus-summary-select-article)
9771     (let ((mail-header-separator ""))
9772       (gnus-eval-in-buffer-window gnus-article-buffer
9773         (save-restriction
9774           (widen)
9775           (let ((start (window-start))
9776                 buffer-read-only)
9777             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9778               (replace-match (idna-to-unicode (match-string 1))))
9779             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9780
9781 (defun gnus-summary-morse-message (&optional arg)
9782   "Morse decode the current article."
9783   (interactive "P")
9784   (gnus-summary-select-article)
9785   (let ((mail-header-separator ""))
9786     (gnus-eval-in-buffer-window gnus-article-buffer
9787       (save-excursion
9788         (save-restriction
9789           (widen)
9790           (let ((pos (window-start))
9791                 (inhibit-read-only t))
9792             (goto-char (point-min))
9793             (when (message-goto-body)
9794               (gnus-narrow-to-body))
9795             (goto-char (point-min))
9796             (while (search-forward "·" (point-max) t)
9797               (replace-match "."))
9798             (unmorse-region (point-min) (point-max))
9799             (widen)
9800             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9801
9802 (defun gnus-summary-stop-page-breaking ()
9803   "Stop page breaking in the current article."
9804   (interactive)
9805   (gnus-summary-select-article)
9806   (gnus-eval-in-buffer-window gnus-article-buffer
9807     (widen)
9808     (when (gnus-visual-p 'page-marker)
9809       (let ((inhibit-read-only t))
9810         (gnus-remove-text-with-property 'gnus-prev)
9811         (gnus-remove-text-with-property 'gnus-next))
9812       (setq gnus-page-broken nil))))
9813
9814 (defun gnus-summary-move-article (&optional n to-newsgroup
9815                                             select-method action)
9816   "Move the current article to a different newsgroup.
9817 If N is a positive number, move the N next articles.
9818 If N is a negative number, move the N previous articles.
9819 If N is nil and any articles have been marked with the process mark,
9820 move those articles instead.
9821 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9822 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9823 re-spool using this method.
9824
9825 When called interactively with TO-NEWSGROUP being nil, the value of
9826 the variable `gnus-move-split-methods' is used for finding a default
9827 for the target newsgroup.
9828
9829 For this function to work, both the current newsgroup and the
9830 newsgroup that you want to move to have to support the `request-move'
9831 and `request-accept' functions.
9832
9833 ACTION can be either `move' (the default), `crosspost' or `copy'."
9834   (interactive "P")
9835   (unless action
9836     (setq action 'move))
9837   ;; Check whether the source group supports the required functions.
9838   (cond ((and (eq action 'move)
9839               (not (gnus-check-backend-function
9840                     'request-move-article gnus-newsgroup-name)))
9841          (error "The current group does not support article moving"))
9842         ((and (eq action 'crosspost)
9843               (not (gnus-check-backend-function
9844                     'request-replace-article gnus-newsgroup-name)))
9845          (error "The current group does not support article editing")))
9846   (let ((articles (gnus-summary-work-articles n))
9847         (prefix (if (gnus-check-backend-function
9848                      'request-move-article gnus-newsgroup-name)
9849                     (funcall gnus-move-group-prefix-function
9850                              gnus-newsgroup-name)
9851                   ""))
9852         (names '((move "Move" "Moving")
9853                  (copy "Copy" "Copying")
9854                  (crosspost "Crosspost" "Crossposting")))
9855         (copy-buf (save-excursion
9856                     (nnheader-set-temp-buffer " *copy article*")))
9857         art-group to-method new-xref article to-groups
9858         articles-to-update-marks encoded)
9859     (unless (assq action names)
9860       (error "Unknown action %s" action))
9861     ;; Read the newsgroup name.
9862     (when (and (not to-newsgroup)
9863                (not select-method))
9864       (if (and gnus-move-split-methods
9865                (not
9866                 (and (memq gnus-current-article articles)
9867                      (gnus-buffer-live-p gnus-original-article-buffer))))
9868           ;; When `gnus-move-split-methods' is non-nil, we have to
9869           ;; select an article to give `gnus-read-move-group-name' an
9870           ;; opportunity to suggest an appropriate default.  However,
9871           ;; we needn't render or mark the article.
9872           (let ((gnus-display-mime-function nil)
9873                 (gnus-article-prepare-hook nil)
9874                 (gnus-mark-article-hook nil))
9875             (gnus-summary-select-article nil nil nil (car articles))))
9876       (setq to-newsgroup (gnus-read-move-group-name
9877                           (cadr (assq action names))
9878                           (symbol-value
9879                            (intern (format "gnus-current-%s-group" action)))
9880                           articles prefix)
9881             encoded to-newsgroup
9882             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9883       (set (intern (format "gnus-current-%s-group" action))
9884            (mm-decode-coding-string
9885             to-newsgroup
9886             (gnus-group-name-charset to-method to-newsgroup))))
9887     (unless to-method
9888       (setq to-method (or select-method
9889                           (gnus-server-to-method
9890                            (gnus-group-method to-newsgroup)))))
9891     (setq to-newsgroup
9892           (or encoded
9893               (and to-newsgroup
9894                    (mm-encode-coding-string
9895                     to-newsgroup
9896                     (gnus-group-name-charset to-method to-newsgroup)))))
9897     ;; Check the method we are to move this article to...
9898     (unless (gnus-check-backend-function
9899              'request-accept-article (car to-method))
9900       (error "%s does not support article copying" (car to-method)))
9901     (unless (gnus-check-server to-method)
9902       (error "Can't open server %s" (car to-method)))
9903     (gnus-message 6 "%s to %s: %s..."
9904                   (caddr (assq action names))
9905                   (or (car select-method)
9906                       (gnus-group-decoded-name to-newsgroup))
9907                   articles)
9908     (while articles
9909       (setq article (pop articles))
9910       ;; Set any marks that may have changed in the summary buffer.
9911       (when gnus-preserve-marks
9912         (gnus-summary-push-marks-to-backend article))
9913       (setq
9914        art-group
9915        (cond
9916         ;; Move the article.
9917         ((eq action 'move)
9918          ;; Remove this article from future suppression.
9919          (gnus-dup-unsuppress-article article)
9920          (let* ((from-method (gnus-find-method-for-group
9921                               gnus-newsgroup-name))
9922                 (to-method (or select-method
9923                                (gnus-find-method-for-group to-newsgroup)))
9924                 (move-is-internal (gnus-server-equal from-method to-method)))
9925            (gnus-request-move-article
9926             article                     ; Article to move
9927             gnus-newsgroup-name         ; From newsgroup
9928             (nth 1 (gnus-find-method-for-group
9929                     gnus-newsgroup-name)) ; Server
9930             (list 'gnus-request-accept-article
9931                   to-newsgroup (list 'quote select-method)
9932                   (not articles) t)     ; Accept form
9933             (not articles)              ; Only save nov last time
9934             (and move-is-internal
9935                  to-newsgroup           ; Not respooling
9936                                         ; Is this move internal?
9937                  (gnus-group-real-name to-newsgroup)))))
9938         ;; Copy the article.
9939         ((eq action 'copy)
9940          (with-current-buffer copy-buf
9941            (when (gnus-request-article-this-buffer article
9942                                                    gnus-newsgroup-name)
9943              (save-restriction
9944                (nnheader-narrow-to-headers)
9945                (dolist (hdr gnus-copy-article-ignored-headers)
9946                  (message-remove-header hdr t)))
9947              (gnus-request-accept-article
9948               to-newsgroup select-method (not articles) t))))
9949         ;; Crosspost the article.
9950         ((eq action 'crosspost)
9951          (let ((xref (message-tokenize-header
9952                       (mail-header-xref (gnus-summary-article-header
9953                                          article))
9954                       " ")))
9955            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9956                                   ":" (number-to-string article)))
9957            (unless xref
9958              (setq xref (list (system-name))))
9959            (setq new-xref
9960                  (concat
9961                   (mapconcat 'identity
9962                              (delete "Xref:" (delete new-xref xref))
9963                              " ")
9964                   " " new-xref))
9965            (with-current-buffer copy-buf
9966              ;; First put the article in the destination group.
9967              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9968              (when (consp (setq art-group
9969                                 (gnus-request-accept-article
9970                                  to-newsgroup select-method (not articles)
9971                                  t)))
9972                (setq new-xref (concat new-xref " " (car art-group)
9973                                       ":"
9974                                       (number-to-string (cdr art-group))))
9975                ;; Now we have the new Xrefs header, so we insert
9976                ;; it and replace the new article.
9977                (nnheader-replace-header "Xref" new-xref)
9978                (gnus-request-replace-article
9979                 (cdr art-group) to-newsgroup (current-buffer) t)
9980                art-group))))))
9981       (cond
9982        ((not art-group)
9983         (gnus-message 1 "Couldn't %s article %s: %s"
9984                       (cadr (assq action names)) article
9985                       (nnheader-get-report (car to-method))))
9986        ((eq art-group 'junk)
9987         (when (eq action 'move)
9988           (gnus-summary-mark-article article gnus-canceled-mark)
9989           (gnus-message 4 "Deleted article %s" article)
9990           ;; run the delete hook
9991           (run-hook-with-args 'gnus-summary-article-delete-hook
9992                               action
9993                               (gnus-data-header
9994                                (assoc article (gnus-data-list nil)))
9995                               gnus-newsgroup-name nil
9996                               select-method)))
9997        (t
9998         (let* ((pto-group (gnus-group-prefixed-name
9999                            (car art-group) to-method))
10000                (info (gnus-get-info pto-group))
10001                (to-group (gnus-info-group info))
10002                to-marks)
10003           ;; Update the group that has been moved to.
10004           (when (and info
10005                      (memq action '(move copy)))
10006             (unless (member to-group to-groups)
10007               (push to-group to-groups))
10008
10009             (when (and (not (memq article gnus-newsgroup-unreads))
10010                        (cdr art-group))
10011               (push 'read to-marks)
10012               (gnus-info-set-read
10013                info (gnus-add-to-range (gnus-info-read info)
10014                                        (list (cdr art-group)))))
10015
10016             ;; See whether the article is to be put in the cache.
10017             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10018                    (marks (if expirable
10019                               gnus-article-mark-lists
10020                             (delete '(expirable . expire)
10021                                     (copy-sequence
10022                                      gnus-article-mark-lists))))
10023                    (to-article (cdr art-group)))
10024
10025               ;; Enter the article into the cache in the new group,
10026               ;; if that is required.
10027               (when (and to-article
10028                          gnus-use-cache)
10029                 (gnus-cache-possibly-enter-article
10030                  to-group to-article
10031                  (memq article gnus-newsgroup-marked)
10032                  (memq article gnus-newsgroup-dormant)
10033                  (memq article gnus-newsgroup-unreads)))
10034
10035               (when (and gnus-preserve-marks
10036                          to-article)
10037                 ;; Copy any marks over to the new group.
10038                 (when (and (equal to-group gnus-newsgroup-name)
10039                            (not (memq article gnus-newsgroup-unreads)))
10040                   ;; Mark this article as read in this group.
10041                   (push (cons to-article gnus-read-mark)
10042                         gnus-newsgroup-reads)
10043                   ;; Increase the active status of this group.
10044                   (setcdr (gnus-active to-group) to-article)
10045                   (setcdr gnus-newsgroup-active to-article))
10046
10047                 (while marks
10048                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10049                     (when (memq article (symbol-value
10050                                          (intern (format "gnus-newsgroup-%s"
10051                                                          (caar marks)))))
10052                       (push (cdar marks) to-marks)
10053                       ;; If the other group is the same as this group,
10054                       ;; then we have to add the mark to the list.
10055                       (when (equal to-group gnus-newsgroup-name)
10056                         (set (intern (format "gnus-newsgroup-%s"
10057                                              (caar marks)))
10058                              (cons to-article
10059                                    (symbol-value
10060                                     (intern (format "gnus-newsgroup-%s"
10061                                                     (caar marks)))))))
10062                       ;; Copy the marks to other group.
10063                       (gnus-add-marked-articles
10064                        to-group (cdar marks) (list to-article) info)))
10065                   (setq marks (cdr marks)))
10066
10067                 (when (and expirable
10068                            gnus-mark-copied-or-moved-articles-as-expirable
10069                            (not (memq 'expire to-marks)))
10070                   ;; Mark this article as expirable.
10071                   (push 'expire to-marks)
10072                   (when (equal to-group gnus-newsgroup-name)
10073                     (push to-article gnus-newsgroup-expirable))
10074                   ;; Copy the expirable mark to other group.
10075                   (gnus-add-marked-articles
10076                    to-group 'expire (list to-article) info))
10077
10078                 (when (and to-marks
10079                            (gnus-method-option-p
10080                             (gnus-find-method-for-group to-group)
10081                             'server-marks))
10082                   (gnus-request-set-mark
10083                    to-group (list (list (list to-article) 'add to-marks)))))
10084
10085               (gnus-dribble-enter
10086                (concat "(gnus-group-set-info '"
10087                        (gnus-prin1-to-string (gnus-get-info to-group))
10088                        ")")
10089                (concat "^(gnus-group-set-info '(\""
10090                        (regexp-quote to-group) "\""))))
10091
10092           ;; Update the Xref header in this article to point to
10093           ;; the new crossposted article we have just created.
10094           (when (eq action 'crosspost)
10095             (with-current-buffer copy-buf
10096               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10097               (nnheader-replace-header "Xref" new-xref)
10098               (gnus-request-replace-article
10099                article gnus-newsgroup-name (current-buffer) t)))
10100
10101           ;; run the move/copy/crosspost/respool hook
10102           (run-hook-with-args 'gnus-summary-article-move-hook
10103                               action
10104                               (gnus-data-header
10105                                (assoc article (gnus-data-list nil)))
10106                               gnus-newsgroup-name
10107                               to-newsgroup
10108                               select-method))
10109
10110         ;;;!!!Why is this necessary?
10111         (set-buffer gnus-summary-buffer)
10112
10113         (when (eq action 'move)
10114           (save-excursion
10115             (gnus-summary-goto-subject article)
10116             (gnus-summary-mark-article article gnus-canceled-mark)))))
10117       (push article articles-to-update-marks))
10118
10119     (save-excursion
10120       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10121     ;; Re-activate all groups that have been moved to.
10122     (with-current-buffer gnus-group-buffer
10123       (let ((gnus-group-marked to-groups))
10124         (gnus-group-get-new-news-this-group nil t)))
10125
10126     (gnus-kill-buffer copy-buf)
10127     (gnus-summary-position-point)
10128     (gnus-set-mode-line 'summary)))
10129
10130 (defun gnus-summary-push-marks-to-backend (article)
10131   (let ((set nil)
10132         (del nil)
10133         (marks gnus-article-mark-lists))
10134     (unless (memq article gnus-newsgroup-unreads)
10135       (push 'read set))
10136     (while marks
10137       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10138                (memq article (symbol-value
10139                               (intern (format "gnus-newsgroup-%s"
10140                                               (caar marks))))))
10141           (push (cdar marks) set)
10142         (push (cdar marks) del))
10143       (pop marks))
10144     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10145                                                  ((,article) del ,del)))))
10146
10147 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10148   "Copy the current article to some other group.
10149 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10150 When called interactively, if TO-NEWSGROUP is nil, use the value of
10151 the variable `gnus-move-split-methods' for finding a default target
10152 newsgroup.
10153 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10154 re-spool using this method."
10155   (interactive "P")
10156   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10157
10158 (defun gnus-summary-crosspost-article (&optional n)
10159   "Crosspost the current article to some other group."
10160   (interactive "P")
10161   (gnus-summary-move-article n nil nil 'crosspost))
10162
10163 (defcustom gnus-summary-respool-default-method nil
10164   "Default method type for respooling an article.
10165 If nil, use to the current newsgroup method."
10166   :type 'symbol
10167   :group 'gnus-summary-mail)
10168
10169 (defun gnus-summary-respool-article (&optional n method)
10170   "Respool the current article.
10171 The article will be squeezed through the mail spooling process again,
10172 which means that it will be put in some mail newsgroup or other
10173 depending on `nnmail-split-methods'.
10174 If N is a positive number, respool the N next articles.
10175 If N is a negative number, respool the N previous articles.
10176 If N is nil and any articles have been marked with the process mark,
10177 respool those articles instead.
10178
10179 Respooling can be done both from mail groups and \"real\" newsgroups.
10180 In the former case, the articles in question will be moved from the
10181 current group into whatever groups they are destined to.  In the
10182 latter case, they will be copied into the relevant groups."
10183   (interactive
10184    (list current-prefix-arg
10185          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10186                 (methname
10187                  (symbol-name (or gnus-summary-respool-default-method
10188                                   (car (gnus-find-method-for-group
10189                                         gnus-newsgroup-name)))))
10190                 (method
10191                  (gnus-completing-read
10192                   "Backend to use when respooling"
10193                   methods t nil 'gnus-mail-method-history methname))
10194                 ms)
10195            (cond
10196             ((zerop (length (setq ms (gnus-servers-using-backend
10197                                       (intern method)))))
10198              (list (intern method) ""))
10199             ((= 1 (length ms))
10200              (car ms))
10201             (t
10202              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10203                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10204                            ms-alist))))))))
10205   (unless method
10206     (error "No method given for respooling"))
10207   (if (assoc (symbol-name
10208               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10209              (gnus-methods-using 'respool))
10210       (gnus-summary-move-article n nil method)
10211     (gnus-summary-copy-article n nil method)))
10212
10213 (defun gnus-summary-import-article (file &optional edit)
10214   "Import an arbitrary file into a mail newsgroup."
10215   (interactive "fImport file: \nP")
10216   (let ((group gnus-newsgroup-name)
10217         (now (current-time))
10218         atts lines group-art)
10219     (unless (gnus-check-backend-function 'request-accept-article group)
10220       (error "%s does not support article importing" group))
10221     (or (file-readable-p file)
10222         (not (file-regular-p file))
10223         (error "Can't read %s" file))
10224     (with-current-buffer (gnus-get-buffer-create " *import file*")
10225       (erase-buffer)
10226       (nnheader-insert-file-contents file)
10227       (goto-char (point-min))
10228       (if (nnheader-article-p)
10229           (save-restriction
10230             (goto-char (point-min))
10231             (search-forward "\n\n" nil t)
10232             (narrow-to-region (point-min) (1- (point)))
10233             (goto-char (point-min))
10234             (unless (re-search-forward "^date:" nil t)
10235               (goto-char (point-max))
10236               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10237        ;; This doesn't look like an article, so we fudge some headers.
10238         (setq atts (file-attributes file)
10239               lines (count-lines (point-min) (point-max)))
10240         (insert "From: " (read-string "From: ") "\n"
10241                 "Subject: " (read-string "Subject: ") "\n"
10242                 "Date: " (message-make-date (nth 5 atts)) "\n"
10243                 "Message-ID: " (message-make-message-id) "\n"
10244                 "Lines: " (int-to-string lines) "\n"
10245                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10246       (setq group-art (gnus-request-accept-article group nil t))
10247       (kill-buffer (current-buffer)))
10248     (setq gnus-newsgroup-active (gnus-activate-group group))
10249     (forward-line 1)
10250     (gnus-summary-goto-article (cdr group-art) nil t)
10251     (when edit
10252       (gnus-summary-edit-article))))
10253
10254 (defun gnus-summary-create-article ()
10255   "Create an article in a mail newsgroup."
10256   (interactive)
10257   (let ((group gnus-newsgroup-name)
10258         (now (current-time))
10259         group-art)
10260     (unless (gnus-check-backend-function 'request-accept-article group)
10261       (error "%s does not support article importing" group))
10262     (with-current-buffer (gnus-get-buffer-create " *import file*")
10263       (erase-buffer)
10264       (goto-char (point-min))
10265       ;; This doesn't look like an article, so we fudge some headers.
10266       (insert "From: " (read-string "From: ") "\n"
10267               "Subject: " (read-string "Subject: ") "\n"
10268               "Date: " (message-make-date now) "\n"
10269               "Message-ID: " (message-make-message-id) "\n")
10270       (setq group-art (gnus-request-accept-article group nil t))
10271       (kill-buffer (current-buffer)))
10272     (setq gnus-newsgroup-active (gnus-activate-group group))
10273     (forward-line 1)
10274     (gnus-summary-goto-article (cdr group-art) nil t)
10275     (gnus-summary-edit-article)))
10276
10277 (defun gnus-summary-article-posted-p ()
10278   "Say whether the current (mail) article is available from news as well.
10279 This will be the case if the article has both been mailed and posted."
10280   (interactive)
10281   (let ((id (mail-header-references (gnus-summary-article-header)))
10282         (gnus-override-method (car (gnus-refer-article-methods))))
10283     (if (gnus-request-head id "")
10284         (gnus-message 2 "The current message was found on %s"
10285                       gnus-override-method)
10286       (gnus-message 2 "The current message couldn't be found on %s"
10287                     gnus-override-method)
10288       nil)))
10289
10290 (defun gnus-summary-expire-articles (&optional now)
10291   "Expire all articles that are marked as expirable in the current group."
10292   (interactive)
10293   (when (and (not gnus-group-is-exiting-without-update-p)
10294              (gnus-check-backend-function
10295               'request-expire-articles gnus-newsgroup-name))
10296     ;; This backend supports expiry.
10297     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10298            (expirable
10299             (gnus-list-range-difference
10300              (if total
10301                  (progn
10302                    ;; We need to update the info for
10303                    ;; this group for `gnus-list-of-read-articles'
10304                    ;; to give us the right answer.
10305                    (gnus-run-hooks 'gnus-exit-group-hook)
10306                    (gnus-summary-update-info)
10307                    (gnus-list-of-read-articles gnus-newsgroup-name))
10308                (setq gnus-newsgroup-expirable
10309                      (sort gnus-newsgroup-expirable '<)))
10310              gnus-newsgroup-unexist))
10311            (expiry-wait (if now 'immediate
10312                           (gnus-group-find-parameter
10313                            gnus-newsgroup-name 'expiry-wait)))
10314            (nnmail-expiry-target
10315             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10316                 nnmail-expiry-target))
10317            es)
10318       (when expirable
10319         ;; There are expirable articles in this group, so we run them
10320         ;; through the expiry process.
10321         (gnus-message 6 "Expiring articles...")
10322         (when (gnus-check-group gnus-newsgroup-name)
10323           ;; The list of articles that weren't expired is returned.
10324           (save-excursion
10325             (if expiry-wait
10326                 (let ((nnmail-expiry-wait-function nil)
10327                       (nnmail-expiry-wait expiry-wait))
10328                   (setq es (gnus-request-expire-articles
10329                             expirable gnus-newsgroup-name)))
10330               (setq es (gnus-request-expire-articles
10331                         expirable gnus-newsgroup-name)))
10332             (unless total
10333               (setq gnus-newsgroup-expirable es))
10334             ;; We go through the old list of expirable, and mark all
10335             ;; really expired articles as nonexistent.
10336             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10337               (let ((gnus-use-cache nil))
10338                 (dolist (article expirable)
10339                   (when (and (not (memq article es))
10340                              (gnus-data-find article))
10341                     (gnus-summary-mark-article article gnus-canceled-mark)
10342                     (run-hook-with-args 'gnus-summary-article-expire-hook
10343                                         'delete
10344                                         (gnus-data-header
10345                                          (assoc article (gnus-data-list nil)))
10346                                         gnus-newsgroup-name
10347                                         nil
10348                                         nil)))))))
10349         (gnus-message 6 "Expiring articles...done")))))
10350
10351 (defun gnus-summary-expire-articles-now ()
10352   "Expunge all expirable articles in the current group.
10353 This means that *all* articles that are marked as expirable will be
10354 deleted forever, right now."
10355   (interactive)
10356   (or gnus-expert-user
10357       (gnus-yes-or-no-p
10358        "Are you really, really sure you want to delete all expirable messages? ")
10359       (error "Phew!"))
10360   (gnus-summary-expire-articles t))
10361
10362 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10363 (defun gnus-summary-delete-article (&optional n)
10364   "Delete the N next (mail) articles.
10365 This command actually deletes articles.  This is not a marking
10366 command.  The article will disappear forever from your life, never to
10367 return.
10368
10369 If N is negative, delete backwards.
10370 If N is nil and articles have been marked with the process mark,
10371 delete these instead.
10372
10373 If `gnus-novice-user' is non-nil you will be asked for
10374 confirmation before the articles are deleted."
10375   (interactive "P")
10376   (unless (gnus-check-backend-function 'request-expire-articles
10377                                        gnus-newsgroup-name)
10378     (error "The current newsgroup does not support article deletion"))
10379   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10380     (error "Couldn't open server"))
10381   ;; Compute the list of articles to delete.
10382   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10383         (nnmail-expiry-target 'delete)
10384         not-deleted)
10385     (if (and gnus-novice-user
10386              (not (gnus-yes-or-no-p
10387                    (format "Do you really want to delete %s forever? "
10388                            (if (> (length articles) 1)
10389                                (format "these %s articles" (length articles))
10390                              "this article")))))
10391         ()
10392       ;; Delete the articles.
10393       (setq not-deleted (gnus-request-expire-articles
10394                          articles gnus-newsgroup-name 'force))
10395       (save-excursion
10396         (while articles
10397           (gnus-summary-remove-process-mark (car articles))
10398           ;; The backend might not have been able to delete the article
10399           ;; after all.
10400           (unless (memq (car articles) not-deleted)
10401             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10402             (let* ((article (car articles))
10403                    (ghead  (gnus-data-header
10404                             (assoc article (gnus-data-list nil)))))
10405               (run-hook-with-args 'gnus-summary-article-delete-hook
10406                                   'delete ghead gnus-newsgroup-name nil
10407                                   nil)))
10408           (setq articles (cdr articles))))
10409       (when not-deleted
10410         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10411     (gnus-summary-position-point)
10412     (gnus-set-mode-line 'summary)
10413     not-deleted))
10414
10415 (defun gnus-summary-edit-article (&optional arg)
10416   "Edit the current article.
10417 This will have permanent effect only in mail groups.
10418 If ARG is nil, edit the decoded articles.
10419 If ARG is 1, edit the raw articles.
10420 If ARG is 2, edit the raw articles even in read-only groups.
10421 If ARG is 3, edit the articles with the current handles.
10422 Otherwise, allow editing of articles even in read-only
10423 groups."
10424   (interactive "P")
10425   (let (force raw current-handles)
10426     (cond
10427      ((null arg))
10428      ((eq arg 1)
10429       (setq raw t))
10430      ((eq arg 2)
10431       (setq raw t
10432             force t))
10433      ((eq arg 3)
10434       (setq current-handles
10435             (and (gnus-buffer-live-p gnus-article-buffer)
10436                  (with-current-buffer gnus-article-buffer
10437                    (prog1
10438                        gnus-article-mime-handles
10439                      (setq gnus-article-mime-handles nil))))))
10440      (t
10441       (setq force t)))
10442     (when (and raw (not force)
10443                (member gnus-newsgroup-name '("nndraft:delayed"
10444                                              "nndraft:drafts"
10445                                              "nndraft:queue")))
10446       (error "Can't edit the raw article in group %s"
10447              gnus-newsgroup-name))
10448     (with-current-buffer gnus-summary-buffer
10449       (let ((mail-parse-charset gnus-newsgroup-charset)
10450             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10451         (gnus-set-global-variables)
10452         (when (and (not force)
10453                    (gnus-group-read-only-p))
10454           (error "The current newsgroup does not support article editing"))
10455         (gnus-summary-show-article t)
10456         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10457           (with-current-buffer gnus-article-buffer
10458             (mm-enable-multibyte)))
10459         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10460             (setq raw t))
10461         (gnus-article-edit-article
10462          (if raw 'ignore
10463            `(lambda ()
10464               (let ((mbl mml-buffer-list))
10465                 (setq mml-buffer-list nil)
10466                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10467                   (mime-to-mml ,'current-handles))
10468                 (let ((mbl1 mml-buffer-list))
10469                   (setq mml-buffer-list mbl)
10470                   (set (make-local-variable 'mml-buffer-list) mbl1))
10471                 (gnus-make-local-hook 'kill-buffer-hook)
10472                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10473          `(lambda (no-highlight)
10474             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10475                   (message-options message-options)
10476                   (message-options-set-recipient)
10477                   (mail-parse-ignored-charsets
10478                    ',gnus-newsgroup-ignored-charsets)
10479                   (rfc2047-header-encoding-alist
10480                    ',(let ((charset (gnus-group-name-charset
10481                                      (gnus-find-method-for-group
10482                                       gnus-newsgroup-name)
10483                                      gnus-newsgroup-name)))
10484                        (append (list (cons "Newsgroups" charset)
10485                                      (cons "Followup-To" charset)
10486                                      (cons "Xref" charset))
10487                                rfc2047-header-encoding-alist))))
10488               ,(if (not raw) '(progn
10489                                 (mml-to-mime)
10490                                 (mml-destroy-buffers)
10491                                 (remove-hook 'kill-buffer-hook
10492                                              'mml-destroy-buffers t)
10493                                 (kill-local-variable 'mml-buffer-list)))
10494               (gnus-summary-edit-article-done
10495                ,(or (mail-header-references gnus-current-headers) "")
10496                ,(gnus-group-read-only-p)
10497                ,gnus-summary-buffer no-highlight))))))))
10498
10499 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10500
10501 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10502                                                  no-highlight)
10503   "Make edits to the current article permanent."
10504   (interactive)
10505   (save-excursion
10506     ;; The buffer restriction contains the entire article if it exists.
10507     (when (article-goto-body)
10508       (let ((lines (count-lines (point) (point-max)))
10509             (length (- (point-max) (point)))
10510             (case-fold-search t)
10511             (body (copy-marker (point))))
10512         (goto-char (point-min))
10513         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10514           (delete-region (match-beginning 1) (match-end 1))
10515           (insert (number-to-string length)))
10516         (goto-char (point-min))
10517         (when (re-search-forward
10518                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10519           (delete-region (match-beginning 1) (match-end 1))
10520           (insert (number-to-string length)))
10521         (goto-char (point-min))
10522         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10523           (delete-region (match-beginning 1) (match-end 1))
10524           (insert (number-to-string lines))))))
10525   ;; Replace the article.
10526   (let ((buf (current-buffer))
10527         (article (cdr gnus-article-current))
10528         replace-result)
10529     (with-temp-buffer
10530       (insert-buffer-substring buf)
10531       (if (and (not read-only)
10532                (not (setq replace-result
10533                           (gnus-request-replace-article
10534                            article (car gnus-article-current)
10535                            (current-buffer) t))))
10536           (error "Couldn't replace article")
10537         ;; If we got a number back, then that's the new article number
10538         ;; for this article.  Otherwise, the article number didn't change.
10539         (when (numberp replace-result)
10540           (with-current-buffer gnus-summary-buffer
10541             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10542             (gnus-summary-limit gnus-newsgroup-limit)
10543             (setq article replace-result)
10544             (gnus-summary-goto-subject article t)))
10545         ;; Update the summary buffer.
10546         (if (and references
10547                  (equal (message-tokenize-header references " ")
10548                         (message-tokenize-header
10549                          (or (message-fetch-field "references") "") " ")))
10550             ;; We only have to update this line.
10551             (save-excursion
10552               (save-restriction
10553                 (message-narrow-to-head)
10554                 (let ((head (buffer-substring-no-properties
10555                              (point-min) (point-max)))
10556                       header)
10557                   (with-temp-buffer
10558                     (insert (format "211 %d Article retrieved.\n" article))
10559                     (insert head)
10560                     (insert ".\n")
10561                     (let ((nntp-server-buffer (current-buffer)))
10562                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10563                     (with-current-buffer gnus-summary-buffer
10564                       (gnus-data-set-header (gnus-data-find article) header)
10565                       (gnus-summary-update-article-line article header)
10566                       (if (gnus-summary-goto-subject article nil t)
10567                           (gnus-summary-update-secondary-mark article)))))))
10568           ;; Update threads.
10569           (set-buffer (or buffer gnus-summary-buffer))
10570           (gnus-summary-update-article article)
10571           (if (gnus-summary-goto-subject article nil t)
10572               (gnus-summary-update-secondary-mark article)))
10573         ;; Prettify the article buffer again.
10574         (unless no-highlight
10575           (with-current-buffer gnus-article-buffer
10576             ;;!!! Fix this -- article should be rehighlighted.
10577             ;;(gnus-run-hooks 'gnus-article-display-hook)
10578             (set-buffer gnus-original-article-buffer)
10579             (gnus-request-article
10580              article (car gnus-article-current) (current-buffer))))
10581         ;; Prettify the summary buffer line.
10582         (when (gnus-visual-p 'summary-highlight 'highlight)
10583           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10584
10585 (defun gnus-summary-edit-wash (key)
10586   "Perform editing command KEY in the article buffer."
10587   (interactive
10588    (list
10589     (progn
10590       (message "%s" (concat (this-command-keys) "- "))
10591       (read-char))))
10592   (message "")
10593   (gnus-summary-edit-article)
10594   (execute-kbd-macro (concat (this-command-keys) key))
10595   (gnus-article-edit-done))
10596
10597 ;;; Respooling
10598
10599 (defun gnus-summary-respool-query (&optional silent trace)
10600   "Query where the respool algorithm would put this article."
10601   (interactive)
10602   (let (gnus-mark-article-hook)
10603     (gnus-summary-select-article)
10604     (with-current-buffer gnus-original-article-buffer
10605       (let ((groups (nnmail-article-group 'identity trace)))
10606         (unless silent
10607           (if groups
10608               (message "This message would go to %s"
10609                        (mapconcat 'car groups ", "))
10610             (message "This message would go to no groups"))
10611           groups)))))
10612
10613 (defun gnus-summary-respool-trace ()
10614   "Trace where the respool algorithm would put this article.
10615 Display a buffer showing all fancy splitting patterns which matched."
10616   (interactive)
10617   (gnus-summary-respool-query nil t))
10618
10619 ;; Summary marking commands.
10620
10621 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10622   "Mark articles which has the same subject as read, and then select the next.
10623 If UNMARK is positive, remove any kind of mark.
10624 If UNMARK is negative, tick articles."
10625   (interactive "P")
10626   (when unmark
10627     (setq unmark (prefix-numeric-value unmark)))
10628   (let ((count
10629          (gnus-summary-mark-same-subject
10630           (gnus-summary-article-subject) unmark)))
10631     ;; Select next unread article.  If auto-select-same mode, should
10632     ;; select the first unread article.
10633     (gnus-summary-next-article t (and gnus-auto-select-same
10634                                       (gnus-summary-article-subject)))
10635     (gnus-message 7 "%d article%s marked as %s"
10636                   count (if (= count 1) " is" "s are")
10637                   (if unmark "unread" "read"))))
10638
10639 (defun gnus-summary-kill-same-subject (&optional unmark)
10640   "Mark articles which has the same subject as read.
10641 If UNMARK is positive, remove any kind of mark.
10642 If UNMARK is negative, tick articles."
10643   (interactive "P")
10644   (when unmark
10645     (setq unmark (prefix-numeric-value unmark)))
10646   (let ((count
10647          (gnus-summary-mark-same-subject
10648           (gnus-summary-article-subject) unmark)))
10649     ;; If marked as read, go to next unread subject.
10650     (when (null unmark)
10651       ;; Go to next unread subject.
10652       (gnus-summary-next-subject 1 t))
10653     (gnus-message 7 "%d articles are marked as %s"
10654                   count (if unmark "unread" "read"))))
10655
10656 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10657   "Mark articles with same SUBJECT as read, and return marked number.
10658 If optional argument UNMARK is positive, remove any kinds of marks.
10659 If optional argument UNMARK is negative, mark articles as unread instead."
10660   (let ((count 1))
10661     (save-excursion
10662       (cond
10663        ((null unmark)                   ; Mark as read.
10664         (while (and
10665                 (progn
10666                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10667                   (gnus-summary-show-thread) t)
10668                 (gnus-summary-find-subject subject))
10669           (setq count (1+ count))))
10670        ((> unmark 0)                    ; Tick.
10671         (while (and
10672                 (progn
10673                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10674                   (gnus-summary-show-thread) t)
10675                 (gnus-summary-find-subject subject))
10676           (setq count (1+ count))))
10677        (t                               ; Mark as unread.
10678         (while (and
10679                 (progn
10680                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10681                   (gnus-summary-show-thread) t)
10682                 (gnus-summary-find-subject subject))
10683           (setq count (1+ count)))))
10684       (gnus-set-mode-line 'summary)
10685       ;; Return the number of marked articles.
10686       count)))
10687
10688 (defun gnus-summary-mark-as-processable (n &optional unmark)
10689   "Set the process mark on the next N articles.
10690 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10691 the process mark instead.  The difference between N and the actual
10692 number of articles marked is returned."
10693   (interactive "P")
10694   (if (and (null n) (gnus-region-active-p))
10695       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10696     (setq n (prefix-numeric-value n))
10697     (let ((backward (< n 0))
10698           (n (abs n)))
10699       (while (and
10700               (> n 0)
10701               (if unmark
10702                   (gnus-summary-remove-process-mark
10703                    (gnus-summary-article-number))
10704                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10705               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10706         (setq n (1- n)))
10707       (when (/= 0 n)
10708         (gnus-message 7 "No more articles"))
10709       (gnus-summary-recenter)
10710       (gnus-summary-position-point)
10711       n)))
10712
10713 (defun gnus-summary-unmark-as-processable (n)
10714   "Remove the process mark from the next N articles.
10715 If N is negative, unmark backward instead.  The difference between N and
10716 the actual number of articles unmarked is returned."
10717   (interactive "P")
10718   (gnus-summary-mark-as-processable n t))
10719
10720 (defun gnus-summary-unmark-all-processable ()
10721   "Remove the process mark from all articles."
10722   (interactive)
10723   (save-excursion
10724     (while gnus-newsgroup-processable
10725       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10726   (gnus-summary-position-point))
10727
10728 (defun gnus-summary-add-mark (article type)
10729   "Mark ARTICLE with a mark of TYPE."
10730   (let ((vtype (car (assq type gnus-article-mark-lists)))
10731         var)
10732     (if (not vtype)
10733         (error "No such mark type: %s" type)
10734       (setq var (intern (format "gnus-newsgroup-%s" type)))
10735       (set var (cons article (symbol-value var)))
10736       (if (memq type '(processable cached replied forwarded recent saved))
10737           (gnus-summary-update-secondary-mark article)
10738         ;;; !!! This is bogus.  We should find out what primary
10739         ;;; !!! mark we want to set.
10740         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10741
10742 (defun gnus-summary-mark-as-expirable (n)
10743   "Mark N articles forward as expirable.
10744 If N is negative, mark backward instead.  The difference between N and
10745 the actual number of articles marked is returned."
10746   (interactive "p")
10747   (gnus-summary-mark-forward n gnus-expirable-mark))
10748
10749 (defun gnus-summary-mark-as-spam (n)
10750   "Mark N articles forward as spam.
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-spam-mark))
10755
10756 (defun gnus-summary-mark-article-as-replied (article)
10757   "Mark ARTICLE as replied to and update the summary line.
10758 ARTICLE can also be a list of articles."
10759   (interactive (list (gnus-summary-article-number)))
10760   (let ((articles (if (listp article) article (list article))))
10761     (dolist (article articles)
10762       (unless (numberp article)
10763         (error "%s is not a number" article))
10764       (push article gnus-newsgroup-replied)
10765       (let ((inhibit-read-only t))
10766         (when (gnus-summary-goto-subject article nil t)
10767           (gnus-summary-update-secondary-mark article))))))
10768
10769 (defun gnus-summary-mark-article-as-forwarded (article)
10770   "Mark ARTICLE as forwarded and update the summary line.
10771 ARTICLE can also be a list of articles."
10772   (let ((articles (if (listp article) article (list article))))
10773     (dolist (article articles)
10774       (push article gnus-newsgroup-forwarded)
10775       (let ((inhibit-read-only t))
10776         (when (gnus-summary-goto-subject article nil t)
10777           (gnus-summary-update-secondary-mark article))))))
10778
10779 (defun gnus-summary-set-bookmark (article)
10780   "Set a bookmark in current article."
10781   (interactive (list (gnus-summary-article-number)))
10782   (when (or (not (get-buffer gnus-article-buffer))
10783             (not gnus-current-article)
10784             (not gnus-article-current)
10785             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10786     (error "No current article selected"))
10787   ;; Remove old bookmark, if one exists.
10788   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10789   ;; Set the new bookmark, which is on the form
10790   ;; (article-number . line-number-in-body).
10791   (push
10792    (cons article
10793          (with-current-buffer gnus-article-buffer
10794            (count-lines
10795             (min (point)
10796                  (save-excursion
10797                    (article-goto-body)
10798                    (point)))
10799             (point))))
10800    gnus-newsgroup-bookmarks)
10801   (gnus-message 6 "A bookmark has been added to the current article."))
10802
10803 (defun gnus-summary-remove-bookmark (article)
10804   "Remove the bookmark from the current article."
10805   (interactive (list (gnus-summary-article-number)))
10806   ;; Remove old bookmark, if one exists.
10807   (if (not (assq article gnus-newsgroup-bookmarks))
10808       (gnus-message 6 "No bookmark in current article.")
10809     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10810     (gnus-message 6 "Removed bookmark.")))
10811
10812 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10813 (defun gnus-summary-mark-as-dormant (n)
10814   "Mark N articles forward as dormant.
10815 If N is negative, mark backward instead.  The difference between N and
10816 the actual number of articles marked is returned."
10817   (interactive "p")
10818   (gnus-summary-mark-forward n gnus-dormant-mark))
10819
10820 (defun gnus-summary-set-process-mark (article)
10821   "Set the process mark on ARTICLE and update the summary line."
10822   (setq gnus-newsgroup-processable
10823         (cons article
10824               (delq article gnus-newsgroup-processable)))
10825   (when (gnus-summary-goto-subject article)
10826     (gnus-summary-show-thread)
10827     (gnus-summary-goto-subject article)
10828     (gnus-summary-update-secondary-mark article)))
10829
10830 (defun gnus-summary-remove-process-mark (&rest articles)
10831   "Remove the process mark from ARTICLES and update the summary line."
10832   (dolist (article articles)
10833     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10834     (when (gnus-summary-goto-subject article)
10835       (gnus-summary-show-thread)
10836       (gnus-summary-goto-subject article)
10837       (gnus-summary-update-secondary-mark article)))
10838   t)
10839
10840 (defun gnus-summary-set-saved-mark (article)
10841   "Set the process mark on ARTICLE and update the summary line."
10842   (push article gnus-newsgroup-saved)
10843   (when (gnus-summary-goto-subject article)
10844     (gnus-summary-update-secondary-mark article)))
10845
10846 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10847   "Mark N articles as read forwards.
10848 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10849 The difference between N and the actual number of articles marked is
10850 returned.
10851 If NO-EXPIRE, auto-expiry will be inhibited."
10852   (interactive "p")
10853   (gnus-summary-show-thread)
10854   (let ((backward (< n 0))
10855         (gnus-summary-goto-unread
10856          (and gnus-summary-goto-unread
10857               (not (eq gnus-summary-goto-unread 'never))
10858               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10859                                     gnus-ticked-mark gnus-dormant-mark)))))
10860         (n (abs n))
10861         (mark (or mark gnus-del-mark)))
10862     (while (and (> n 0)
10863                 (gnus-summary-mark-article nil mark no-expire)
10864                 (zerop (gnus-summary-next-subject
10865                         (if backward -1 1)
10866                         (and gnus-summary-goto-unread
10867                              (not (eq gnus-summary-goto-unread 'never)))
10868                         t)))
10869       (setq n (1- n)))
10870     (when (/= 0 n)
10871       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10872     (gnus-summary-recenter)
10873     (gnus-summary-position-point)
10874     (gnus-set-mode-line 'summary)
10875     n))
10876
10877 (defun gnus-summary-mark-article-as-read (mark)
10878   "Mark the current article quickly as read with MARK."
10879   (let ((article (gnus-summary-article-number)))
10880     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10881     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10882     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10883     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10884     (push (cons article mark) gnus-newsgroup-reads)
10885     ;; Possibly remove from cache, if that is used.
10886     (when gnus-use-cache
10887       (gnus-cache-enter-remove-article article))
10888     ;; Allow the backend to change the mark.
10889     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10890     ;; Check for auto-expiry.
10891     (when (and gnus-newsgroup-auto-expire
10892                (memq mark gnus-auto-expirable-marks))
10893       (setq mark gnus-expirable-mark)
10894       ;; Let the backend know about the mark change.
10895       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10896       (push article gnus-newsgroup-expirable))
10897     ;; Set the mark in the buffer.
10898     (gnus-summary-update-mark mark 'unread)
10899     t))
10900
10901 (defun gnus-summary-mark-article-as-unread (mark)
10902   "Mark the current article quickly as unread with MARK."
10903   (let* ((article (gnus-summary-article-number))
10904          (old-mark (gnus-summary-article-mark article)))
10905     ;; Allow the backend to change the mark.
10906     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10907     (if (eq mark old-mark)
10908         t
10909       (if (<= article 0)
10910           (progn
10911             (gnus-error 1 "Can't mark negative article numbers")
10912             nil)
10913         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10914         (setq gnus-newsgroup-spam-marked
10915               (delq article gnus-newsgroup-spam-marked))
10916         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10917         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10918         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10919         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10920         (cond ((= mark gnus-ticked-mark)
10921                (setq gnus-newsgroup-marked
10922                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10923                                               article)))
10924               ((= mark gnus-spam-mark)
10925                (setq gnus-newsgroup-spam-marked
10926                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10927                                               article)))
10928               ((= mark gnus-dormant-mark)
10929                (setq gnus-newsgroup-dormant
10930                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10931                                               article)))
10932               (t
10933                (setq gnus-newsgroup-unreads
10934                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10935                                               article))))
10936         (gnus-alist-pull article gnus-newsgroup-reads)
10937
10938         ;; See whether the article is to be put in the cache.
10939         (and gnus-use-cache
10940              (vectorp (gnus-summary-article-header article))
10941              (save-excursion
10942                (gnus-cache-possibly-enter-article
10943                 gnus-newsgroup-name article
10944                 (= mark gnus-ticked-mark)
10945                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10946
10947         ;; Fix the mark.
10948         (gnus-summary-update-mark mark 'unread)
10949         t))))
10950
10951 (defun gnus-summary-mark-article (&optional article mark no-expire)
10952   "Mark ARTICLE with MARK.  MARK can be any character.
10953 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10954 `??' (dormant) and `?E' (expirable).
10955 If MARK is nil, then the default character `?r' is used.
10956 If ARTICLE is nil, then the article on the current line will be
10957 marked.
10958 If NO-EXPIRE, auto-expiry will be inhibited."
10959   ;; The mark might be a string.
10960   (when (stringp mark)
10961     (setq mark (aref mark 0)))
10962   ;; If no mark is given, then we check auto-expiring.
10963   (when (null mark)
10964     (setq mark gnus-del-mark))
10965   (when (and (not no-expire)
10966              gnus-newsgroup-auto-expire
10967              (memq mark gnus-auto-expirable-marks))
10968     (setq mark gnus-expirable-mark))
10969   (let ((article (or article (gnus-summary-article-number)))
10970         (old-mark (gnus-summary-article-mark article)))
10971     ;; Allow the backend to change the mark.
10972     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10973     (if (eq mark old-mark)
10974         t
10975       (unless article
10976         (error "No article on current line"))
10977       (if (not (if (or (= mark gnus-unread-mark)
10978                        (= mark gnus-ticked-mark)
10979                        (= mark gnus-spam-mark)
10980                        (= mark gnus-dormant-mark))
10981                    (gnus-mark-article-as-unread article mark)
10982                  (gnus-mark-article-as-read article mark)))
10983           t
10984         ;; See whether the article is to be put in the cache.
10985         (and gnus-use-cache
10986              (not (= mark gnus-canceled-mark))
10987              (vectorp (gnus-summary-article-header article))
10988              (save-excursion
10989                (gnus-cache-possibly-enter-article
10990                 gnus-newsgroup-name article
10991                 (= mark gnus-ticked-mark)
10992                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10993
10994         (when (gnus-summary-goto-subject article nil t)
10995           (let ((inhibit-read-only t))
10996             (gnus-summary-show-thread)
10997             ;; Fix the mark.
10998             (gnus-summary-update-mark mark 'unread)
10999             t))))))
11000
11001 (defun gnus-summary-update-secondary-mark (article)
11002   "Update the secondary (read, process, cache) mark."
11003   (gnus-summary-update-mark
11004    (cond ((memq article gnus-newsgroup-processable)
11005           gnus-process-mark)
11006          ((memq article gnus-newsgroup-cached)
11007           gnus-cached-mark)
11008          ((memq article gnus-newsgroup-replied)
11009           gnus-replied-mark)
11010          ((memq article gnus-newsgroup-forwarded)
11011           gnus-forwarded-mark)
11012          ((memq article gnus-newsgroup-saved)
11013           gnus-saved-mark)
11014          ((memq article gnus-newsgroup-unseen)
11015           gnus-unseen-mark)
11016          (t gnus-no-mark))
11017    'replied)
11018   (when (gnus-visual-p 'summary-highlight 'highlight)
11019     (gnus-summary-highlight-line)
11020     (gnus-run-hooks 'gnus-summary-update-hook))
11021   t)
11022
11023 (defun gnus-summary-update-download-mark (article)
11024   "Update the download mark."
11025   (gnus-summary-update-mark
11026    (cond ((memq article gnus-newsgroup-undownloaded)
11027           gnus-undownloaded-mark)
11028          (gnus-newsgroup-agentized
11029           gnus-downloaded-mark)
11030          (t
11031           gnus-no-mark))
11032    'download)
11033   (gnus-summary-update-line t)
11034   t)
11035
11036 (defun gnus-summary-update-mark (mark type)
11037   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11038         (inhibit-read-only t))
11039     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11040     (when forward
11041       (when (looking-at "\r")
11042         (incf forward))
11043       (when (<= (+ forward (point)) (point-max))
11044         ;; Go to the right position on the line.
11045         (goto-char (+ forward (point)))
11046         ;; Replace the old mark with the new mark.
11047         (let ((to-insert
11048                (mm-subst-char-in-string
11049                 (char-after) mark
11050                 (buffer-substring (point) (1+ (point))))))
11051           (delete-region (point) (1+ (point)))
11052           (insert to-insert))
11053         ;; Optionally update the marks by some user rule.
11054         (when (eq type 'unread)
11055           (gnus-data-set-mark
11056            (gnus-data-find (gnus-summary-article-number)) mark)
11057           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11058
11059 (defun gnus-mark-article-as-read (article &optional mark)
11060   "Enter ARTICLE in the pertinent lists and remove it from others."
11061   ;; Make the article expirable.
11062   (let ((mark (or mark gnus-del-mark)))
11063     (setq gnus-newsgroup-expirable
11064           (if (= mark gnus-expirable-mark)
11065               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11066             (delq article gnus-newsgroup-expirable)))
11067     ;; Remove from unread and marked lists.
11068     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11069     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11070     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11071     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11072     (push (cons article mark) gnus-newsgroup-reads)
11073     ;; Possibly remove from cache, if that is used.
11074     (when gnus-use-cache
11075       (gnus-cache-enter-remove-article article))
11076     t))
11077
11078 (defun gnus-mark-article-as-unread (article &optional mark)
11079   "Enter ARTICLE in the pertinent lists and remove it from others."
11080   (let ((mark (or mark gnus-ticked-mark)))
11081     (if (<= article 0)
11082         (progn
11083           (gnus-error 1 "Can't mark negative article numbers")
11084           nil)
11085       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11086             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11087             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11088             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11089             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11090
11091       ;; Unsuppress duplicates?
11092       (when gnus-suppress-duplicates
11093         (gnus-dup-unsuppress-article article))
11094
11095       (cond ((= mark gnus-ticked-mark)
11096              (setq gnus-newsgroup-marked
11097                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11098             ((= mark gnus-spam-mark)
11099              (setq gnus-newsgroup-spam-marked
11100                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11101                                             article)))
11102             ((= mark gnus-dormant-mark)
11103              (setq gnus-newsgroup-dormant
11104                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11105             (t
11106              (setq gnus-newsgroup-unreads
11107                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11108       (gnus-alist-pull article gnus-newsgroup-reads)
11109       t)))
11110
11111 (defun gnus-summary-tick-article-forward (n)
11112   "Tick N articles forwards.
11113 If N is negative, tick backwards instead.
11114 The difference between N and the number of articles ticked is returned."
11115   (interactive "p")
11116   (gnus-summary-mark-forward n gnus-ticked-mark))
11117
11118 (defun gnus-summary-tick-article-backward (n)
11119   "Tick N articles backwards.
11120 The difference between N and the number of articles ticked is returned."
11121   (interactive "p")
11122   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11123
11124 (defun gnus-summary-tick-article (&optional article clear-mark)
11125   "Mark current article as unread.
11126 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11127 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11128   (interactive)
11129   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11130                                        gnus-ticked-mark)))
11131
11132 (defun gnus-summary-mark-as-read-forward (n)
11133   "Mark N articles as read forwards.
11134 If N is negative, mark backwards instead.
11135 The difference between N and the actual number of articles marked is
11136 returned."
11137   (interactive "p")
11138   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11139
11140 (defun gnus-summary-mark-as-read-backward (n)
11141   "Mark the N articles as read backwards.
11142 The difference between N and the actual number of articles marked is
11143 returned."
11144   (interactive "p")
11145   (gnus-summary-mark-forward
11146    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11147
11148 (defun gnus-summary-mark-as-read (&optional article mark)
11149   "Mark current article as read.
11150 ARTICLE specifies the article to be marked as read.
11151 MARK specifies a string to be inserted at the beginning of the line."
11152   (gnus-summary-mark-article article mark))
11153
11154 (defun gnus-summary-clear-mark-forward (n)
11155   "Clear marks from N articles forward.
11156 If N is negative, clear backward instead.
11157 The difference between N and the number of marks cleared is returned."
11158   (interactive "p")
11159   (gnus-summary-mark-forward n gnus-unread-mark))
11160
11161 (defun gnus-summary-clear-mark-backward (n)
11162   "Clear marks from N articles backward.
11163 The difference between N and the number of marks cleared is returned."
11164   (interactive "p")
11165   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11166
11167 (defun gnus-summary-mark-unread-as-read ()
11168   "Intended to be used by `gnus-mark-article-hook'."
11169   (when (memq gnus-current-article gnus-newsgroup-unreads)
11170     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11171
11172 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11173   "Intended to be used by `gnus-mark-article-hook'."
11174   (let ((mark (gnus-summary-article-mark)))
11175     (when (or (gnus-unread-mark-p mark)
11176               (gnus-read-mark-p mark))
11177       (gnus-summary-mark-article gnus-current-article
11178                                  (or new-mark gnus-read-mark)))))
11179
11180 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11181   "Intended to be used by `gnus-mark-article-hook'."
11182   (let ((mark (gnus-summary-article-mark)))
11183     (when (or (gnus-unread-mark-p mark)
11184               (gnus-read-mark-p mark))
11185       (gnus-summary-mark-article (gnus-summary-article-number)
11186                                  (or new-mark gnus-read-mark)))))
11187
11188 (defun gnus-summary-mark-unread-as-ticked ()
11189   "Intended to be used by `gnus-mark-article-hook'."
11190   (when (memq gnus-current-article gnus-newsgroup-unreads)
11191     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11192
11193 (defun gnus-summary-mark-region-as-read (point mark all)
11194   "Mark all unread articles between point and mark as read.
11195 If given a prefix, mark all articles between point and mark as read,
11196 even ticked and dormant ones."
11197   (interactive "r\nP")
11198   (save-excursion
11199     (let (article)
11200       (goto-char point)
11201       (beginning-of-line)
11202       (while (and
11203               (< (point) mark)
11204               (progn
11205                 (when (or all
11206                           (memq (setq article (gnus-summary-article-number))
11207                                 gnus-newsgroup-unreads))
11208                   (gnus-summary-mark-article article gnus-del-mark))
11209                 t)
11210               (gnus-summary-find-next))))))
11211
11212 (defun gnus-summary-mark-below (score mark)
11213   "Mark articles with score less than SCORE with MARK."
11214   (interactive "P\ncMark: ")
11215   (setq score (if score
11216                   (prefix-numeric-value score)
11217                 (or gnus-summary-default-score 0)))
11218   (with-current-buffer gnus-summary-buffer
11219     (goto-char (point-min))
11220     (while
11221         (progn
11222           (and (< (gnus-summary-article-score) score)
11223                (gnus-summary-mark-article nil mark))
11224           (gnus-summary-find-next)))))
11225
11226 (defun gnus-summary-kill-below (&optional score)
11227   "Mark articles with score below SCORE as read."
11228   (interactive "P")
11229   (gnus-summary-mark-below score gnus-killed-mark))
11230
11231 (defun gnus-summary-clear-above (&optional score)
11232   "Clear all marks from articles with score above SCORE."
11233   (interactive "P")
11234   (gnus-summary-mark-above score gnus-unread-mark))
11235
11236 (defun gnus-summary-tick-above (&optional score)
11237   "Tick all articles with score above SCORE."
11238   (interactive "P")
11239   (gnus-summary-mark-above score gnus-ticked-mark))
11240
11241 (defun gnus-summary-mark-above (score mark)
11242   "Mark articles with score over SCORE with MARK."
11243   (interactive "P\ncMark: ")
11244   (setq score (if score
11245                   (prefix-numeric-value score)
11246                 (or gnus-summary-default-score 0)))
11247   (with-current-buffer gnus-summary-buffer
11248     (goto-char (point-min))
11249     (while (and (progn
11250                   (when (> (gnus-summary-article-score) score)
11251                     (gnus-summary-mark-article nil mark))
11252                   t)
11253                 (gnus-summary-find-next)))))
11254
11255 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11256 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11257 (defun gnus-summary-limit-include-expunged (&optional no-error)
11258   "Display all the hidden articles that were expunged for low scores."
11259   (interactive)
11260   (let ((inhibit-read-only t))
11261     (let ((scored gnus-newsgroup-scored)
11262           headers h)
11263       (while scored
11264         (unless (gnus-summary-article-header (caar scored))
11265           (and (setq h (gnus-number-to-header (caar scored)))
11266                (< (cdar scored) gnus-summary-expunge-below)
11267                (push h headers)))
11268         (setq scored (cdr scored)))
11269       (if (not headers)
11270           (when (not no-error)
11271             (error "No expunged articles hidden"))
11272         (goto-char (point-min))
11273         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11274         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11275         (dolist (x headers)
11276           (push (mail-header-number x) gnus-newsgroup-limit))
11277         (gnus-summary-prepare-unthreaded (nreverse headers))
11278         (goto-char (point-min))
11279         (gnus-summary-position-point)
11280         t))))
11281
11282 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11283   "Mark all unread articles in this newsgroup as read.
11284 If prefix argument ALL is non-nil, ticked and dormant articles will
11285 also be marked as read.
11286 If QUIETLY is non-nil, no questions will be asked.
11287
11288 If TO-HERE is non-nil, it should be a point in the buffer.  All
11289 articles before (after, if REVERSE is set) this point will be marked
11290 as read.
11291
11292 Note that this function will only catch up the unread article
11293 in the current summary buffer limitation.
11294
11295 The number of articles marked as read is returned."
11296   (interactive "P")
11297   (prog1
11298       (save-excursion
11299         (when (or quietly
11300                   (not gnus-interactive-catchup) ;Without confirmation?
11301                   gnus-expert-user
11302                   (gnus-y-or-n-p
11303                    (if all
11304                        "Mark absolutely all articles as read? "
11305                      "Mark all unread articles as read? ")))
11306           (if (and not-mark
11307                    (not gnus-newsgroup-adaptive)
11308                    (not gnus-newsgroup-auto-expire)
11309                    (not gnus-suppress-duplicates)
11310                    (or (not gnus-use-cache)
11311                        (eq gnus-use-cache 'passive)))
11312               (progn
11313                 (when all
11314                   (setq gnus-newsgroup-marked nil
11315                         gnus-newsgroup-spam-marked nil
11316                         gnus-newsgroup-dormant nil))
11317                 (setq gnus-newsgroup-unreads
11318                       (gnus-sorted-nunion
11319                        (gnus-sorted-intersection gnus-newsgroup-unreads
11320                                                  gnus-newsgroup-downloadable)
11321                        (gnus-sorted-difference gnus-newsgroup-unfetched
11322                                                gnus-newsgroup-cached))))
11323             ;; We actually mark all articles as canceled, which we
11324             ;; have to do when using auto-expiry or adaptive scoring.
11325             (gnus-summary-show-all-threads)
11326             (if (and to-here reverse)
11327                 (progn
11328                   (goto-char to-here)
11329                   (gnus-summary-mark-current-read-and-unread-as-read
11330                    gnus-catchup-mark)
11331                   (while (gnus-summary-find-next (not all))
11332                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11333               (when (gnus-summary-first-subject (not all))
11334                 (while (and
11335                         (if to-here (< (point) to-here) t)
11336                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11337                         (gnus-summary-find-next (not all))))))
11338             (gnus-set-mode-line 'summary))
11339           t))
11340     (gnus-summary-position-point)))
11341
11342 (defun gnus-summary-catchup-to-here (&optional all)
11343   "Mark all unticked articles before the current one as read.
11344 If ALL is non-nil, also mark ticked and dormant articles as read."
11345   (interactive "P")
11346   (save-excursion
11347     (gnus-save-hidden-threads
11348       (let ((beg (point)))
11349         ;; We check that there are unread articles.
11350         (when (or all (gnus-summary-find-prev))
11351           (gnus-summary-catchup all t beg)))))
11352   (gnus-summary-position-point))
11353
11354 (defun gnus-summary-catchup-from-here (&optional all)
11355   "Mark all unticked articles after (and including) the current one as read.
11356 If ALL is non-nil, also mark ticked and dormant articles as read."
11357   (interactive "P")
11358   (save-excursion
11359     (gnus-save-hidden-threads
11360       (let ((beg (point)))
11361         ;; We check that there are unread articles.
11362         (when (or all (gnus-summary-find-next))
11363           (gnus-summary-catchup all t beg nil t)))))
11364   (gnus-summary-position-point))
11365
11366 (defun gnus-summary-catchup-all (&optional quietly)
11367   "Mark all articles in this newsgroup as read.
11368 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11369 instead, which marks only unread articles as read."
11370   (interactive "P")
11371   (gnus-summary-catchup t quietly))
11372
11373 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11374   "Mark all unread articles in this group as read, then exit.
11375 If prefix argument ALL is non-nil, all articles are marked as read.
11376 If QUIETLY is non-nil, no questions will be asked."
11377   (interactive "P")
11378   (when (gnus-summary-catchup all quietly nil 'fast)
11379     ;; Select next newsgroup or exit.
11380     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11381              (eq gnus-auto-select-next 'quietly))
11382         (gnus-summary-next-group nil)
11383       (gnus-summary-exit))))
11384
11385 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11386   "Mark all articles in this newsgroup as read, and then exit.
11387 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11388 instead, which marks only unread articles as read."
11389   (interactive "P")
11390   (gnus-summary-catchup-and-exit t quietly))
11391
11392 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11393   "Mark all articles in this group as read and select the next group.
11394 If given a prefix, mark all articles, unread as well as ticked, as
11395 read."
11396   (interactive "P")
11397   (save-excursion
11398     (gnus-summary-catchup all))
11399   (gnus-summary-next-group))
11400
11401 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11402   "Mark all articles in this group as read and select the previous group.
11403 If given a prefix, mark all articles, unread as well as ticked, as
11404 read."
11405   (interactive "P")
11406   (save-excursion
11407     (gnus-summary-catchup all))
11408   (gnus-summary-next-group nil nil t))
11409
11410 ;;;
11411 ;;; with article
11412 ;;;
11413
11414 (defmacro gnus-with-article (article &rest forms)
11415   "Select ARTICLE and perform FORMS in the original article buffer.
11416 Then replace the article with the result."
11417   `(progn
11418      ;; We don't want the article to be marked as read.
11419      (let (gnus-mark-article-hook)
11420        (gnus-summary-select-article t t nil ,article))
11421      (set-buffer gnus-original-article-buffer)
11422      ,@forms
11423      (if (not (gnus-check-backend-function
11424                'request-replace-article (car gnus-article-current)))
11425          (gnus-message 5 "Read-only group; not replacing")
11426        (unless (gnus-request-replace-article
11427                 ,article (car gnus-article-current)
11428                 (current-buffer) t)
11429          (error "Couldn't replace article")))
11430      ;; The cache and backlog have to be flushed somewhat.
11431      (when gnus-keep-backlog
11432        (gnus-backlog-remove-article
11433         (car gnus-article-current) (cdr gnus-article-current)))
11434      (when gnus-use-cache
11435        (gnus-cache-update-article
11436         (car gnus-article-current) (cdr gnus-article-current)))))
11437
11438 (put 'gnus-with-article 'lisp-indent-function 1)
11439 (put 'gnus-with-article 'edebug-form-spec '(form body))
11440
11441 ;; Thread-based commands.
11442
11443 (defun gnus-summary-articles-in-thread (&optional article)
11444   "Return a list of all articles in the current thread.
11445 If ARTICLE is non-nil, return all articles in the thread that starts
11446 with that article."
11447   (let* ((article (or article (gnus-summary-article-number)))
11448          (data (gnus-data-find-list article))
11449          (top-level (gnus-data-level (car data)))
11450          (top-subject
11451           (cond ((null gnus-thread-operation-ignore-subject)
11452                  (gnus-simplify-subject-re
11453                   (mail-header-subject (gnus-data-header (car data)))))
11454                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11455                  (gnus-simplify-subject-fuzzy
11456                   (mail-header-subject (gnus-data-header (car data)))))
11457                 (t nil)))
11458          (end-point (save-excursion
11459                       (goto-char (gnus-data-pos (car data)))
11460                       (if (gnus-summary-go-to-next-thread)
11461                           (point) (point-max))))
11462          articles)
11463     (while (and data
11464                 (< (gnus-data-pos (car data)) end-point))
11465       (when (or (not top-subject)
11466                 (string= top-subject
11467                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11468                              (gnus-simplify-subject-fuzzy
11469                               (mail-header-subject
11470                                (gnus-data-header (car data))))
11471                            (gnus-simplify-subject-re
11472                             (mail-header-subject
11473                              (gnus-data-header (car data)))))))
11474         (push (gnus-data-number (car data)) articles))
11475       (unless (and (setq data (cdr data))
11476                    (> (gnus-data-level (car data)) top-level))
11477         (setq data nil)))
11478     ;; Return the list of articles.
11479     (nreverse articles)))
11480
11481 (defun gnus-summary-rethread-current ()
11482   "Rethread the thread the current article is part of."
11483   (interactive)
11484   (let* ((gnus-show-threads t)
11485          (article (gnus-summary-article-number))
11486          (id (mail-header-id (gnus-summary-article-header)))
11487          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11488     (unless id
11489       (error "No article on the current line"))
11490     (gnus-rebuild-thread id)
11491     (gnus-summary-goto-subject article)))
11492
11493 (defun gnus-summary-reparent-thread ()
11494   "Make the current article child of the marked (or previous) article.
11495
11496 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11497 is non-nil or the Subject: of both articles are the same."
11498   (interactive)
11499   (unless (not (gnus-group-read-only-p))
11500     (error "The current newsgroup does not support article editing"))
11501   (unless (<= (length gnus-newsgroup-processable) 1)
11502     (error "No more than one article may be marked"))
11503   (let ((child (gnus-summary-article-number))
11504         ;; First grab the marked article, otherwise one line up.
11505         (parent (if (not (null gnus-newsgroup-processable))
11506                     (car gnus-newsgroup-processable)
11507                   (save-excursion
11508                     (if (eq (forward-line -1) 0)
11509                         (gnus-summary-article-number)
11510                       (error "Beginning of summary buffer"))))))
11511     (gnus-summary-reparent-children parent (list child))))
11512
11513 (defun gnus-summary-reparent-children (parent children)
11514   "Make PARENT the parent of CHILDREN.
11515 When called interactively, PARENT is the current article and CHILDREN
11516 are the process-marked articles."
11517   (interactive
11518    (list (gnus-summary-article-number)
11519          (gnus-summary-work-articles nil)))
11520   (dolist (child children)
11521     (save-window-excursion
11522       (let ((gnus-article-buffer " *reparent*"))
11523         (unless (not (eq parent child))
11524           (error "An article may not be self-referential"))
11525         (let ((message-id (mail-header-id
11526                            (gnus-summary-article-header parent))))
11527           (unless (and message-id (not (equal message-id "")))
11528             (error "No message-id in desired parent"))
11529           (gnus-with-article child
11530             (save-restriction
11531               (goto-char (point-min))
11532               (message-narrow-to-head)
11533               (if (re-search-forward "^References: " nil t)
11534                   (progn
11535                     (re-search-forward "^[^ \t]" nil t)
11536                     (forward-line -1)
11537                     (end-of-line)
11538                     (insert " " message-id))
11539                 (insert "References: " message-id "\n"))))
11540           (set-buffer gnus-summary-buffer)
11541           (gnus-summary-unmark-all-processable)
11542           (gnus-summary-update-article child)
11543           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11544             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11545           (gnus-summary-rethread-current)
11546           (gnus-message 3 "Article %d is now the child of article %d"
11547                         child parent))))))
11548
11549 (defun gnus-summary-toggle-threads (&optional arg)
11550   "Toggle showing conversation threads.
11551 If ARG is positive number, turn showing conversation threads on."
11552   (interactive "P")
11553   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11554     (setq gnus-show-threads
11555           (if (null arg) (not gnus-show-threads)
11556             (> (prefix-numeric-value arg) 0)))
11557     (gnus-summary-prepare)
11558     (gnus-summary-goto-subject current)
11559     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11560     (gnus-summary-position-point)))
11561
11562 (eval-and-compile
11563   (if (fboundp 'remove-overlays)
11564       (defalias 'gnus-remove-overlays 'remove-overlays)
11565     (defun gnus-remove-overlays (beg end name val)
11566       "Clear BEG and END of overlays whose property NAME has value VAL.
11567 For compatibility with XEmacs."
11568       (dolist (ov (gnus-overlays-in beg end))
11569         (when (eq (gnus-overlay-get ov name) val)
11570           (gnus-delete-overlay ov))))))
11571
11572 (defun gnus-summary-show-all-threads ()
11573   "Show all threads."
11574   (interactive)
11575   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11576   (gnus-summary-position-point))
11577
11578 (defsubst gnus-summary--inv (p)
11579   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11580
11581 (defun gnus-summary-show-thread ()
11582   "Show thread subtrees.
11583 Returns nil if no thread was there to be shown."
11584   (interactive)
11585   (let* ((orig (point))
11586          (end (point-at-eol))
11587          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11588          ;; Leave point at bol
11589          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11590          (eoi (when end
11591                 (if (fboundp 'next-single-char-property-change)
11592                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11593                     (or (next-single-char-property-change end 'invisible)
11594                         (point-max))
11595                   (while (progn
11596                            (end-of-line 2)
11597                            (and (not (eobp))
11598                                 (eq (get-char-property (point) 'invisible)
11599                                     'gnus-sum))))
11600                   (point)))))
11601     (when eoi
11602       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11603       (goto-char orig)
11604       (gnus-summary-position-point)
11605       eoi)))
11606
11607 (defun gnus-summary-maybe-hide-threads ()
11608   "If requested, hide the threads that should be hidden."
11609   (when (and gnus-show-threads
11610              gnus-thread-hide-subtree)
11611     (gnus-summary-hide-all-threads
11612      (if (or (consp gnus-thread-hide-subtree)
11613              (functionp gnus-thread-hide-subtree))
11614          (gnus-make-predicate gnus-thread-hide-subtree)
11615        nil))))
11616
11617 ;;; Hiding predicates.
11618
11619 (defun gnus-article-unread-p (header)
11620   (memq (mail-header-number header) gnus-newsgroup-unreads))
11621
11622 (defun gnus-article-unseen-p (header)
11623   (memq (mail-header-number header) gnus-newsgroup-unseen))
11624
11625 (defun gnus-map-articles (predicate articles)
11626   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11627   (apply 'gnus-or (mapcar predicate
11628                           (mapcar (lambda (number)
11629                                     (gnus-summary-article-header number))
11630                                   articles))))
11631
11632 (defun gnus-summary-hide-all-threads (&optional predicate)
11633   "Hide all thread subtrees.
11634 If PREDICATE is supplied, threads that satisfy this predicate
11635 will not be hidden."
11636   (interactive)
11637   (save-excursion
11638     (goto-char (point-min))
11639     (let ((end nil)
11640           (count 0))
11641       (while (not end)
11642         (incf count)
11643         (when (zerop (mod count 1000))
11644           (message "Hiding all threads... %d" count))
11645         (when (or (not predicate)
11646                   (gnus-map-articles
11647                    predicate (gnus-summary-article-children)))
11648             (gnus-summary-hide-thread))
11649         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11650   (gnus-summary-position-point))
11651
11652 (defun gnus-summary-hide-thread ()
11653   "Hide thread subtrees.
11654 If PREDICATE is supplied, threads that satisfy this predicate
11655 will not be hidden.
11656 Returns nil if no threads were there to be hidden."
11657   (interactive)
11658   (beginning-of-line)
11659   (let ((start (point))
11660         (starteol (line-end-position))
11661         (article (gnus-summary-article-number)))
11662     ;; Go forward until either the buffer ends or the subthread ends.
11663     (when (and (not (eobp))
11664                (or (zerop (gnus-summary-next-thread 1 t))
11665                    (goto-char (point-max))))
11666       (if (and (> (point) start)
11667                ;; FIXME: this should actually search for a non-invisible \n.
11668                (search-backward "\n" start t))
11669           (progn
11670             (when (> (point) starteol)
11671               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11672               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11673                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11674                 (gnus-overlay-put ol 'evaporate t)))
11675             (gnus-summary-goto-subject article)
11676             (when (> start (point))
11677               (message "Hiding the thread moved us backwards, aborting!")
11678               (goto-char (point-max))))
11679         (goto-char start)
11680         nil))))
11681
11682 (defun gnus-summary-go-to-next-thread (&optional previous)
11683   "Go to the same level (or less) next thread.
11684 If PREVIOUS is non-nil, go to previous thread instead.
11685 Return the article number moved to, or nil if moving was impossible."
11686   (let ((level (gnus-summary-thread-level))
11687         (way (if previous -1 1))
11688         (beg (point)))
11689     (forward-line way)
11690     (while (and (not (eobp))
11691                 (< level (gnus-summary-thread-level)))
11692       (forward-line way))
11693     (if (eobp)
11694         (progn
11695           (goto-char beg)
11696           nil)
11697       (setq beg (point))
11698       (prog1
11699           (gnus-summary-article-number)
11700         (goto-char beg)))))
11701
11702 (defun gnus-summary-next-thread (n &optional silent)
11703   "Go to the same level next N'th thread.
11704 If N is negative, search backward instead.
11705 Returns the difference between N and the number of skips actually
11706 done.
11707
11708 If SILENT, don't output messages."
11709   (interactive "p")
11710   (let ((backward (< n 0))
11711         (n (abs n)))
11712     (while (and (> n 0)
11713                 (gnus-summary-go-to-next-thread backward))
11714       (decf n))
11715     (unless silent
11716       (gnus-summary-position-point))
11717     (when (and (not silent) (/= 0 n))
11718       (gnus-message 7 "No more threads"))
11719     n))
11720
11721 (defun gnus-summary-prev-thread (n)
11722   "Go to the same level previous N'th thread.
11723 Returns the difference between N and the number of skips actually
11724 done."
11725   (interactive "p")
11726   (gnus-summary-next-thread (- n)))
11727
11728 (defun gnus-summary-go-down-thread ()
11729   "Go down one level in the current thread."
11730   (let ((children (gnus-summary-article-children)))
11731     (when children
11732       (gnus-summary-goto-subject (car children)))))
11733
11734 (defun gnus-summary-go-up-thread ()
11735   "Go up one level in the current thread."
11736   (let ((parent (gnus-summary-article-parent)))
11737     (when parent
11738       (gnus-summary-goto-subject parent))))
11739
11740 (defun gnus-summary-down-thread (n)
11741   "Go down thread N steps.
11742 If N is negative, go up instead.
11743 Returns the difference between N and how many steps down that were
11744 taken."
11745   (interactive "p")
11746   (let ((up (< n 0))
11747         (n (abs n)))
11748     (while (and (> n 0)
11749                 (if up (gnus-summary-go-up-thread)
11750                   (gnus-summary-go-down-thread)))
11751       (setq n (1- n)))
11752     (gnus-summary-position-point)
11753     (when (/= 0 n)
11754       (gnus-message 7 "Can't go further"))
11755     n))
11756
11757 (defun gnus-summary-up-thread (n)
11758   "Go up thread N steps.
11759 If N is negative, go down instead.
11760 Returns the difference between N and how many steps down that were
11761 taken."
11762   (interactive "p")
11763   (gnus-summary-down-thread (- n)))
11764
11765 (defun gnus-summary-top-thread ()
11766   "Go to the top of the thread."
11767   (interactive)
11768   (while (gnus-summary-go-up-thread))
11769   (gnus-summary-article-number))
11770
11771 (defun gnus-summary-expire-thread ()
11772   "Mark articles under current thread as expired."
11773   (interactive)
11774   (gnus-summary-kill-thread 0))
11775
11776 (defun gnus-summary-kill-thread (&optional unmark)
11777   "Mark articles under current thread as read.
11778 If the prefix argument is positive, remove any kinds of marks.
11779 If the prefix argument is zero, mark thread as expired.
11780 If the prefix argument is negative, tick articles instead."
11781   (interactive "P")
11782   (when unmark
11783     (setq unmark (prefix-numeric-value unmark)))
11784   (let ((articles (gnus-summary-articles-in-thread))
11785         (hide (or (null unmark) (= unmark 0))))
11786     (save-excursion
11787       ;; Expand the thread.
11788       (gnus-summary-show-thread)
11789       ;; Mark all the articles.
11790       (while articles
11791         (gnus-summary-goto-subject (car articles))
11792         (cond ((null unmark)
11793                (gnus-summary-mark-article-as-read gnus-killed-mark))
11794               ((> unmark 0)
11795                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11796               ((= unmark 0)
11797                (gnus-summary-mark-article nil gnus-expirable-mark))
11798               (t
11799                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11800         (setq articles (cdr articles))))
11801     ;; Hide killed subtrees when hide is true.
11802     (and hide
11803          gnus-thread-hide-killed
11804          (gnus-summary-hide-thread))
11805     ;; If hide is t, go to next unread subject.
11806     (when hide
11807       ;; Go to next unread subject.
11808       (gnus-summary-next-subject 1 t)))
11809   (gnus-set-mode-line 'summary))
11810
11811 ;; Summary sorting commands
11812
11813 (defun gnus-summary-sort-by-number (&optional reverse)
11814   "Sort the summary buffer by article number.
11815 Argument REVERSE means reverse order."
11816   (interactive "P")
11817   (gnus-summary-sort 'number reverse))
11818
11819 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11820   "Sort the summary buffer by most recent article number.
11821 Argument REVERSE means reverse order."
11822   (interactive "P")
11823   (gnus-summary-sort 'most-recent-number reverse))
11824
11825 (defun gnus-summary-sort-by-random (&optional reverse)
11826   "Randomize the order in the summary buffer.
11827 Argument REVERSE means to randomize in reverse order."
11828   (interactive "P")
11829   (gnus-summary-sort 'random reverse))
11830
11831 (defun gnus-summary-sort-by-author (&optional reverse)
11832   "Sort the summary buffer by author name alphabetically.
11833 If `case-fold-search' is non-nil, case of letters is ignored.
11834 Argument REVERSE means reverse order."
11835   (interactive "P")
11836   (gnus-summary-sort 'author reverse))
11837
11838 (defun gnus-summary-sort-by-recipient (&optional reverse)
11839   "Sort the summary buffer by recipient 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 'recipient reverse))
11844
11845 (defun gnus-summary-sort-by-subject (&optional reverse)
11846   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
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 'subject reverse))
11851
11852 (defun gnus-summary-sort-by-date (&optional reverse)
11853   "Sort the summary buffer by date.
11854 Argument REVERSE means reverse order."
11855   (interactive "P")
11856   (gnus-summary-sort 'date reverse))
11857
11858 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11859   "Sort the summary buffer by most recent date.
11860 Argument REVERSE means reverse order."
11861   (interactive "P")
11862   (gnus-summary-sort 'most-recent-date reverse))
11863
11864 (defun gnus-summary-sort-by-score (&optional reverse)
11865   "Sort the summary buffer by score.
11866 Argument REVERSE means reverse order."
11867   (interactive "P")
11868   (gnus-summary-sort 'score reverse))
11869
11870 (defun gnus-summary-sort-by-lines (&optional reverse)
11871   "Sort the summary buffer by the number of lines.
11872 Argument REVERSE means reverse order."
11873   (interactive "P")
11874   (gnus-summary-sort 'lines reverse))
11875
11876 (defun gnus-summary-sort-by-chars (&optional reverse)
11877   "Sort the summary buffer by article length.
11878 Argument REVERSE means reverse order."
11879   (interactive "P")
11880   (gnus-summary-sort 'chars reverse))
11881
11882 (defun gnus-summary-sort-by-original (&optional reverse)
11883   "Sort the summary buffer using the default sorting method.
11884 Argument REVERSE means reverse order."
11885   (interactive "P")
11886   (let* ((inhibit-read-only t)
11887          (gnus-summary-prepare-hook nil))
11888     ;; We do the sorting by regenerating the threads.
11889     (gnus-summary-prepare)
11890     ;; Hide subthreads if needed.
11891     (gnus-summary-maybe-hide-threads)))
11892
11893 (defun gnus-summary-sort (predicate reverse)
11894   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11895   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11896          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11897          (gnus-thread-sort-functions
11898           (if (not reverse)
11899               thread
11900             `(lambda (t1 t2)
11901                (,thread t2 t1))))
11902          (gnus-sort-gathered-threads-function
11903           gnus-thread-sort-functions)
11904          (gnus-article-sort-functions
11905           (if (not reverse)
11906               article
11907             `(lambda (t1 t2)
11908                (,article t2 t1))))
11909          (inhibit-read-only t)
11910          (gnus-summary-prepare-hook nil))
11911     ;; We do the sorting by regenerating the threads.
11912     (gnus-summary-prepare)
11913     ;; Hide subthreads if needed.
11914     (gnus-summary-maybe-hide-threads)))
11915
11916 ;; Summary saving commands.
11917
11918 (defun gnus-summary-save-article (&optional n not-saved)
11919   "Save the current article using the default saver function.
11920 If N is a positive number, save the N next articles.
11921 If N is a negative number, save the N previous articles.
11922 If N is nil and any articles have been marked with the process mark,
11923 save those articles instead.
11924 The variable `gnus-default-article-saver' specifies the saver function.
11925
11926 If the optional second argument NOT-SAVED is non-nil, articles saved
11927 will not be marked as saved."
11928   (interactive "P")
11929   (require 'gnus-art)
11930   (let* ((articles (gnus-summary-work-articles n))
11931          (save-buffer (save-excursion
11932                         (nnheader-set-temp-buffer " *Gnus Save*")))
11933          (num (length articles))
11934          ;; Whether to save decoded articles or raw articles.
11935          (decode (when gnus-article-save-coding-system
11936                    (get gnus-default-article-saver :decode)))
11937          ;; When saving many articles in a single file, use the other
11938          ;; function to save articles other than the first one.
11939          (saver2 (get gnus-default-article-saver :function))
11940          (gnus-prompt-before-saving (if saver2
11941                                         t
11942                                       gnus-prompt-before-saving))
11943          (gnus-default-article-saver gnus-default-article-saver)
11944          header file)
11945     (dolist (article articles)
11946       (setq header (gnus-summary-article-header article))
11947       (if (not (vectorp header))
11948           ;; This is a pseudo-article.
11949           (if (assq 'name header)
11950               (gnus-copy-file (cdr (assq 'name header)))
11951             (gnus-message 1 "Article %d is unsavable" article))
11952         ;; This is a real article.
11953         (save-window-excursion
11954           (gnus-summary-select-article decode decode nil article)
11955           (gnus-summary-goto-subject article))
11956         (with-current-buffer save-buffer
11957           (erase-buffer)
11958           (insert-buffer-substring (if decode
11959                                        gnus-article-buffer
11960                                      gnus-original-article-buffer)))
11961         (setq file (gnus-article-save save-buffer file num))
11962         (gnus-summary-remove-process-mark article)
11963         (unless not-saved
11964           (gnus-summary-set-saved-mark article)))
11965       (when saver2
11966         (setq gnus-default-article-saver saver2
11967               saver2 nil)))
11968     (gnus-kill-buffer save-buffer)
11969     (gnus-summary-position-point)
11970     (gnus-set-mode-line 'summary)
11971     n))
11972
11973 (defun gnus-summary-pipe-output (&optional n sym)
11974   "Pipe the current article to a subprocess.
11975 If N is a positive number, pipe the N next articles.
11976 If N is a negative number, pipe the N previous articles.
11977 If N is nil and any articles have been marked with the process mark,
11978 pipe those articles instead.
11979 The default command to which articles are piped is specified by the
11980 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11981 will be prompted for the command.
11982
11983 The properties `:decode' and `:headers' that are put to the function
11984 symbol `gnus-summary-save-in-pipe' control whether this function
11985 decodes articles and what headers to keep (see the doc string for the
11986 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11987 is neither omitted nor the symbol `r', force including all headers
11988 regardless of the `:headers' property.  If it is the symbol `r',
11989 articles that are not decoded and include all headers will be piped
11990 no matter what the properties `:decode' and `:headers' are."
11991   (interactive (gnus-interactive "P\ny"))
11992   (require 'gnus-art)
11993   (let* ((articles (gnus-summary-work-articles n))
11994          (result-buffer "*Shell Command Output*")
11995          (all-headers (not (memq sym '(nil r))))
11996          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11997          (raw (eq sym 'r))
11998          (headers (get 'gnus-summary-save-in-pipe :headers))
11999          command result)
12000     (unless (numberp (car articles))
12001       (error "No article to pipe"))
12002     (setq command (gnus-read-shell-command
12003                    (concat "Shell command on "
12004                            (if (cdr articles)
12005                                (format "these %d articles" (length articles))
12006                              "this article")
12007                            ": ")
12008                    gnus-summary-pipe-output-default-command))
12009     (when (string-equal command "")
12010       (error "A command is required"))
12011     (when all-headers
12012       (put 'gnus-summary-save-in-pipe :headers nil))
12013     (unwind-protect
12014         (while articles
12015           (gnus-summary-goto-subject (pop articles))
12016           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12017           (when (and (get-buffer result-buffer)
12018                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12019             (setq result (concat result (with-current-buffer result-buffer
12020                                           (buffer-string))))))
12021       (put 'gnus-summary-save-in-pipe :headers headers))
12022     (unless (zerop (length result))
12023       (if (with-current-buffer (get-buffer-create result-buffer)
12024             (erase-buffer)
12025             (insert result)
12026             (prog1
12027                 (and (= (count-lines (point-min) (point)) 1)
12028                      (progn
12029                        (end-of-line 0)
12030                        (<= (current-column)
12031                            (window-width (minibuffer-window)))))
12032               (goto-char (point-min))))
12033           (message "%s" (substring result 0 -1))
12034         (message nil)
12035         (gnus-configure-windows 'pipe)))))
12036
12037 (defun gnus-summary-save-article-mail (&optional arg)
12038   "Append the current article to a Unix mail box file.
12039 If N is a positive number, save the N next articles.
12040 If N is a negative number, save the N previous articles.
12041 If N is nil and any articles have been marked with the process mark,
12042 save those articles instead."
12043   (interactive "P")
12044   (require 'gnus-art)
12045   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12046     (gnus-summary-save-article arg)))
12047
12048 (defun gnus-summary-save-article-rmail (&optional arg)
12049   "Append the current article to an rmail file.
12050 If N is a positive number, save the N next articles.
12051 If N is a negative number, save the N previous articles.
12052 If N is nil and any articles have been marked with the process mark,
12053 save those articles instead."
12054   (interactive "P")
12055   (require 'gnus-art)
12056   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12057     (gnus-summary-save-article arg)))
12058
12059 (defun gnus-summary-save-article-file (&optional arg)
12060   "Append the current article to a file.
12061 If N is a positive number, save the N next articles.
12062 If N is a negative number, save the N previous articles.
12063 If N is nil and any articles have been marked with the process mark,
12064 save those articles instead."
12065   (interactive "P")
12066   (require 'gnus-art)
12067   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12068     (gnus-summary-save-article arg)))
12069
12070 (defun gnus-summary-write-article-file (&optional arg)
12071   "Write the current article to a file, deleting the previous file.
12072 If N is a positive number, save the N next articles.
12073 If N is a negative number, save the N previous articles.
12074 If N is nil and any articles have been marked with the process mark,
12075 save those articles instead."
12076   (interactive "P")
12077   (require 'gnus-art)
12078   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12079     (gnus-summary-save-article arg)))
12080
12081 (defun gnus-summary-save-article-body-file (&optional arg)
12082   "Append the current article body to a file.
12083 If N is a positive number, save the N next articles.
12084 If N is a negative number, save the N previous articles.
12085 If N is nil and any articles have been marked with the process mark,
12086 save those articles instead."
12087   (interactive "P")
12088   (require 'gnus-art)
12089   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12090     (gnus-summary-save-article arg)))
12091
12092 (defun gnus-summary-write-article-body-file (&optional arg)
12093   "Write the current article body to a file, deleting the previous file.
12094 If N is a positive number, save the N next articles.
12095 If N is a negative number, save the N previous articles.
12096 If N is nil and any articles have been marked with the process mark,
12097 save those articles instead."
12098   (interactive "P")
12099   (require 'gnus-art)
12100   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12101     (gnus-summary-save-article arg)))
12102
12103 (defun gnus-summary-muttprint (&optional arg)
12104   "Print the current article using Muttprint.
12105 If N is a positive number, save the N next articles.
12106 If N is a negative number, save the N previous articles.
12107 If N is nil and any articles have been marked with the process mark,
12108 save those articles instead."
12109   (interactive "P")
12110   (require 'gnus-art)
12111   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12112     (gnus-summary-save-article arg t)))
12113
12114 (defun gnus-summary-pipe-message (program)
12115   "Pipe the current article through PROGRAM."
12116   (interactive "sProgram: ")
12117   (gnus-summary-select-article)
12118   (let ((mail-header-separator ""))
12119     (gnus-eval-in-buffer-window gnus-article-buffer
12120       (save-restriction
12121         (widen)
12122         (let ((start (window-start))
12123               (inhibit-read-only t))
12124           (message-pipe-buffer-body program)
12125           (set-window-start (get-buffer-window (current-buffer)) start))))))
12126
12127 (defun gnus-get-split-value (methods)
12128   "Return a value based on the split METHODS."
12129   (let (split-name method result match)
12130     (when methods
12131       (with-current-buffer gnus-original-article-buffer
12132         (save-restriction
12133           (nnheader-narrow-to-headers)
12134           (while (and methods (not split-name))
12135             (goto-char (point-min))
12136             (setq method (pop methods))
12137             (setq match (car method))
12138             (when (cond
12139                    ((stringp match)
12140                     ;; Regular expression.
12141                     (ignore-errors
12142                       (re-search-forward match nil t)))
12143                    ((functionp match)
12144                     ;; Function.
12145                     (save-restriction
12146                       (widen)
12147                       (setq result (funcall match gnus-newsgroup-name))))
12148                    ((consp match)
12149                     ;; Form.
12150                     (save-restriction
12151                       (widen)
12152                       (setq result (eval match)))))
12153               (setq split-name (cdr method))
12154               (cond ((stringp result)
12155                      (push (expand-file-name
12156                             result gnus-article-save-directory)
12157                            split-name))
12158                     ((consp result)
12159                      (setq split-name (append result split-name)))))))))
12160     (nreverse split-name)))
12161
12162 (defun gnus-valid-move-group-p (group)
12163   (and (symbolp group)
12164        (boundp group)
12165        (symbol-name group)
12166        (symbol-value group)
12167        (gnus-get-function (gnus-find-method-for-group
12168                            (symbol-name group)) 'request-accept-article t)))
12169
12170 (defun gnus-read-move-group-name (prompt default articles prefix)
12171   "Read a group name."
12172   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12173          (minibuffer-confirm-incomplete nil) ; XEmacs
12174          (prom
12175           (format "%s %s to"
12176                   prompt
12177                   (if (> (length articles) 1)
12178                       (format "these %d articles" (length articles))
12179                     "this article")))
12180          (to-newsgroup
12181           (cond
12182            ((null split-name)
12183             (gnus-group-completing-read
12184              prom
12185              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12186              nil prefix nil default))
12187            ((= 1 (length split-name))
12188             (gnus-group-completing-read
12189              prom
12190              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12191              nil prefix 'gnus-group-history (car split-name)))
12192            (t
12193             (gnus-completing-read
12194              prom (nreverse split-name) nil nil 'gnus-group-history))))
12195          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12196          encoded)
12197     (when to-newsgroup
12198       (if (or (string= to-newsgroup "")
12199               (string= to-newsgroup prefix))
12200           (setq to-newsgroup default))
12201       (unless to-newsgroup
12202         (error "No group name entered"))
12203       (setq encoded (mm-encode-coding-string
12204                      to-newsgroup
12205                      (gnus-group-name-charset to-method to-newsgroup)))
12206       (or (gnus-active encoded)
12207           (gnus-activate-group encoded nil nil to-method)
12208           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12209                                      to-newsgroup))
12210               (or (and (gnus-request-create-group encoded to-method)
12211                        (gnus-activate-group encoded nil nil to-method)
12212                        (gnus-subscribe-group encoded))
12213                   (error "Couldn't create group %s" to-newsgroup)))
12214           (error "No such group: %s" to-newsgroup))
12215       encoded)))
12216
12217 (defvar gnus-summary-save-parts-counter)
12218 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12219
12220 (defun gnus-summary-save-parts (type dir n &optional reverse)
12221   "Save parts matching TYPE to DIR.
12222 If REVERSE, save parts that do not match TYPE."
12223   (interactive
12224    (list (read-string "Save parts of type: "
12225                       (or (car gnus-summary-save-parts-type-history)
12226                           gnus-summary-save-parts-default-mime)
12227                       'gnus-summary-save-parts-type-history)
12228          (setq gnus-summary-save-parts-last-directory
12229                (read-directory-name "Save to directory: "
12230                                     gnus-summary-save-parts-last-directory
12231                                     nil t))
12232          current-prefix-arg))
12233   (gnus-summary-iterate n
12234     (let ((gnus-display-mime-function nil)
12235           gnus-article-prepare-hook
12236           gnus-article-decode-hook
12237           gnus-display-mime-function
12238           gnus-break-pages
12239           (gnus-inhibit-treatment t))
12240       (gnus-summary-select-article))
12241     (with-current-buffer gnus-article-buffer
12242       (let ((handles (or gnus-article-mime-handles
12243                          (mm-dissect-buffer nil gnus-article-loose-mime)
12244                          (and gnus-article-emulate-mime
12245                               (mm-uu-dissect))))
12246             (gnus-summary-save-parts-counter 1))
12247         (when handles
12248           (gnus-summary-save-parts-1 type dir handles reverse)
12249           (unless gnus-article-mime-handles ;; Don't destroy this case.
12250             (mm-destroy-parts handles)))))))
12251
12252 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12253   (if (stringp (car handle))
12254       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12255               (cdr handle))
12256     (when (if reverse
12257               (not (string-match type (mm-handle-media-type handle)))
12258             (string-match type (mm-handle-media-type handle)))
12259       (let ((file (expand-file-name
12260                    (gnus-map-function
12261                     mm-file-name-rewrite-functions
12262                     (file-name-nondirectory
12263                      (or
12264                       (mm-handle-filename handle)
12265                       (format "%s.%d.%d" gnus-newsgroup-name
12266                               (cdr gnus-article-current)
12267                               gnus-summary-save-parts-counter))))
12268                    dir)))
12269         (incf gnus-summary-save-parts-counter)
12270         (unless (file-exists-p file)
12271           (mm-save-part-to-file handle file))))))
12272
12273 ;; Summary extract commands
12274
12275 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12276   (let ((inhibit-read-only t)
12277         (article (gnus-summary-article-number))
12278         after-article b e)
12279     (unless (gnus-summary-goto-subject article)
12280       (error "No such article: %d" article))
12281     (gnus-summary-position-point)
12282     ;; If all commands are to be bunched up on one line, we collect
12283     ;; them here.
12284     (unless gnus-view-pseudos-separately
12285       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12286             files action)
12287         (while ps
12288           (setq action (cdr (assq 'action (car ps))))
12289           (setq files (list (cdr (assq 'name (car ps)))))
12290           (while (and ps (cdr ps)
12291                       (string= (or action "1")
12292                                (or (cdr (assq 'action (cadr ps))) "2")))
12293             (push (cdr (assq 'name (cadr ps))) files)
12294             (setcdr ps (cddr ps)))
12295           (when files
12296             (when (not (string-match "%s" action))
12297               (push " " files))
12298             (push " " files)
12299             (when (assq 'execute (car ps))
12300               (setcdr (assq 'execute (car ps))
12301                       (funcall (if (string-match "%s" action)
12302                                    'format 'concat)
12303                                action
12304                                (mapconcat
12305                                 (lambda (f)
12306                                   (if (equal f " ")
12307                                       f
12308                                     (shell-quote-argument f)))
12309                                 files " ")))))
12310           (setq ps (cdr ps)))))
12311     (if (and gnus-view-pseudos (not not-view))
12312         (while pslist
12313           (when (assq 'execute (car pslist))
12314             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12315                                   (eq gnus-view-pseudos 'not-confirm)))
12316           (setq pslist (cdr pslist)))
12317       (save-excursion
12318         (while pslist
12319           (setq after-article (or (cdr (assq 'article (car pslist)))
12320                                   (gnus-summary-article-number)))
12321           (gnus-summary-goto-subject after-article)
12322           (forward-line 1)
12323           (setq b (point))
12324           (insert "    " (file-name-nondirectory
12325                           (cdr (assq 'name (car pslist))))
12326                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12327           (setq e (point))
12328           (forward-line -1)             ; back to `b'
12329           (gnus-add-text-properties
12330            b (1- e) (list 'gnus-number gnus-reffed-article-number
12331                           gnus-mouse-face-prop gnus-mouse-face))
12332           (gnus-data-enter
12333            after-article gnus-reffed-article-number
12334            gnus-unread-mark b (car pslist) 0 (- e b))
12335           (setq gnus-newsgroup-unreads
12336                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12337                                          gnus-reffed-article-number))
12338           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12339           (setq pslist (cdr pslist)))))))
12340
12341 (defun gnus-pseudos< (p1 p2)
12342   (let ((c1 (cdr (assq 'action p1)))
12343         (c2 (cdr (assq 'action p2))))
12344     (and c1 c2 (string< c1 c2))))
12345
12346 (defun gnus-request-pseudo-article (props)
12347   (cond ((assq 'execute props)
12348          (gnus-execute-command (cdr (assq 'execute props)))))
12349   (let ((gnus-current-article (gnus-summary-article-number)))
12350     (gnus-run-hooks 'gnus-mark-article-hook)))
12351
12352 (defun gnus-execute-command (command &optional automatic)
12353   (save-excursion
12354     (gnus-article-setup-buffer)
12355     (set-buffer gnus-article-buffer)
12356     (setq buffer-read-only nil)
12357     (let ((command (if automatic command
12358                      (read-string "Command: " (cons command 0)))))
12359       (erase-buffer)
12360       (insert "$ " command "\n\n")
12361       (if gnus-view-pseudo-asynchronously
12362           (start-process "gnus-execute" (current-buffer) shell-file-name
12363                          shell-command-switch command)
12364         (call-process shell-file-name nil t nil
12365                       shell-command-switch command)))))
12366
12367 ;; Summary kill commands.
12368
12369 (defun gnus-summary-edit-global-kill (article)
12370   "Edit the \"global\" kill file."
12371   (interactive (list (gnus-summary-article-number)))
12372   (gnus-group-edit-global-kill article))
12373
12374 (defun gnus-summary-edit-local-kill ()
12375   "Edit a local kill file applied to the current newsgroup."
12376   (interactive)
12377   (setq gnus-current-headers (gnus-summary-article-header))
12378   (gnus-group-edit-local-kill
12379    (gnus-summary-article-number) gnus-newsgroup-name))
12380
12381 ;;; Header reading.
12382
12383 (defun gnus-read-header (id &optional header)
12384   "Read the headers of article ID and enter them into the Gnus system."
12385   (let ((group gnus-newsgroup-name)
12386         (gnus-override-method
12387          (or
12388           gnus-override-method
12389           (and (gnus-news-group-p gnus-newsgroup-name)
12390                (car (gnus-refer-article-methods)))))
12391         where)
12392     ;; First we check to see whether the header in question is already
12393     ;; fetched.
12394     (if (stringp id)
12395         ;; This is a Message-ID.
12396         (setq header (or header (gnus-id-to-header id)))
12397       ;; This is an article number.
12398       (setq header (or header (gnus-summary-article-header id))))
12399     (if (and header
12400              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12401         ;; We have found the header.
12402         header
12403       ;; We have to really fetch the header to this article.
12404       (with-current-buffer nntp-server-buffer
12405         (when (setq where (gnus-request-head id group))
12406           (nnheader-fold-continuation-lines)
12407           (goto-char (point-max))
12408           (insert ".\n")
12409           (goto-char (point-min))
12410           (insert "211 ")
12411           (princ (cond
12412                   ((numberp id) id)
12413                   ((cdr where) (cdr where))
12414                   (header (mail-header-number header))
12415                   (t gnus-reffed-article-number))
12416                  (current-buffer))
12417           (insert " Article retrieved.\n"))
12418         (if (or (not where)
12419                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12420             ()                          ; Malformed head.
12421           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12422             (when (and (bound-and-true-p gnus-registry-enabled)
12423                        (not (gnus-ephemeral-group-p (car where))))
12424               (gnus-registry-handle-action
12425                (mail-header-id header) nil
12426                (gnus-group-prefixed-name (car where) gnus-override-method)
12427                (mail-header-subject header)
12428                (mail-header-from 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 (gnus-get-text-property-excluding-characters-with-faces 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 (gnus-method-option-p
12577                         (gnus-find-method-for-group group)
12578                         'server-marks)
12579                        (gnus-check-backend-function 'request-set-mark group))
12580               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12581                     (add (gnus-remove-from-range read (gnus-info-read info))))
12582                 (when (or add del)
12583                   (unless (gnus-check-group group)
12584                     (error "Can't open server for %s" group))
12585                   (gnus-request-set-mark
12586                    group (delq nil (list (if add (list add 'add '(read)))
12587                                          (if del (list del 'del '(read))))))
12588                   (setq setmarkundo
12589                         `(gnus-request-set-mark
12590                           ,group
12591                           ',(delq nil (list
12592                                        (if del (list del 'add '(read)))
12593                                        (if add (list add 'del '(read))))))))))
12594             (set-buffer gnus-group-buffer)
12595             (gnus-undo-register
12596               `(progn
12597                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12598                  (gnus-info-set-read ',info ',(gnus-info-read info))
12599                  (gnus-get-unread-articles-in-group ',info
12600                                                     (gnus-active ,group))
12601                  (gnus-group-update-group ,group t)
12602                  ,setmarkundo))))
12603         ;; Enter this list into the group info.
12604         (gnus-info-set-read info read)
12605         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12606         (gnus-get-unread-articles-in-group info (gnus-active group))
12607         t))))
12608
12609 (defun gnus-offer-save-summaries ()
12610   "Offer to save all active summary buffers."
12611   (let (buffers)
12612     ;; Go through all buffers and find all summaries.
12613     (dolist (buffer (buffer-list))
12614       (when (and (setq buffer (buffer-name buffer))
12615                  (string-match "Summary" buffer)
12616                  (with-current-buffer buffer
12617                    ;; We check that this is, indeed, a summary buffer.
12618                    (and (eq major-mode 'gnus-summary-mode)
12619                         ;; Also make sure this isn't bogus.
12620                         gnus-newsgroup-prepared
12621                         ;; Also make sure that this isn't a
12622                         ;; dead summary buffer.
12623                         (not gnus-dead-summary-mode))))
12624         (push buffer buffers)))
12625     ;; Go through all these summary buffers and offer to save them.
12626     (when buffers
12627       (save-excursion
12628         (if (eq gnus-interactive-exit 'quiet)
12629             (dolist (buffer buffers)
12630               (switch-to-buffer buffer)
12631               (gnus-summary-exit))
12632           (map-y-or-n-p
12633            "Update summary buffer %s? "
12634            (lambda (buf)
12635              (switch-to-buffer buf)
12636              (gnus-summary-exit))
12637            buffers))))))
12638
12639 (defun gnus-summary-setup-default-charset ()
12640   "Setup newsgroup default charset."
12641   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12642       (setq gnus-newsgroup-charset nil)
12643     (let* ((ignored-charsets
12644             (or gnus-newsgroup-ephemeral-ignored-charsets
12645                 (append
12646                  (and gnus-newsgroup-name
12647                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12648                  gnus-newsgroup-ignored-charsets))))
12649       (setq gnus-newsgroup-charset
12650             (or gnus-newsgroup-ephemeral-charset
12651                 (and gnus-newsgroup-name
12652                      (gnus-parameter-charset gnus-newsgroup-name))
12653                 gnus-default-charset))
12654       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12655            ignored-charsets))))
12656
12657 ;;;
12658 ;;; Mime Commands
12659 ;;;
12660
12661 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12662   "Display the current article buffer fully MIME-buttonized.
12663 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12664 treated as multipart/mixed."
12665   (interactive "P")
12666   (require 'gnus-art)
12667   (let ((gnus-unbuttonized-mime-types nil)
12668         (gnus-mime-display-multipart-as-mixed show-all-parts))
12669     (gnus-summary-show-article)))
12670
12671 (defun gnus-summary-repair-multipart (article)
12672   "Add a Content-Type header to a multipart article without one."
12673   (interactive (list (gnus-summary-article-number)))
12674   (gnus-with-article article
12675     (message-narrow-to-head)
12676     (message-remove-header "Mime-Version")
12677     (goto-char (point-max))
12678     (insert "Mime-Version: 1.0\n")
12679     (widen)
12680     (when (search-forward "\n--" nil t)
12681       (let ((separator (buffer-substring (point) (point-at-eol))))
12682         (message-narrow-to-head)
12683         (message-remove-header "Content-Type")
12684         (goto-char (point-max))
12685         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12686                         separator))
12687         (widen))))
12688   (let (gnus-mark-article-hook)
12689     (gnus-summary-select-article t t nil article)))
12690
12691 (defun gnus-summary-toggle-display-buttonized ()
12692   "Toggle the buttonizing of the article buffer."
12693   (interactive)
12694   (require 'gnus-art)
12695   (if (setq gnus-inhibit-mime-unbuttonizing
12696             (not gnus-inhibit-mime-unbuttonizing))
12697       (let ((gnus-unbuttonized-mime-types nil))
12698         (gnus-summary-show-article))
12699     (gnus-summary-show-article)))
12700
12701 ;;;
12702 ;;; Generic summary marking commands
12703 ;;;
12704
12705 (defvar gnus-summary-marking-alist
12706   '((read gnus-del-mark "d")
12707     (unread gnus-unread-mark "u")
12708     (ticked gnus-ticked-mark "!")
12709     (dormant gnus-dormant-mark "?")
12710     (expirable gnus-expirable-mark "e"))
12711   "An alist of names/marks/keystrokes.")
12712
12713 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12714 (defvar gnus-summary-mark-map)
12715
12716 (defun gnus-summary-make-all-marking-commands ()
12717   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12718   (dolist (elem gnus-summary-marking-alist)
12719     (apply 'gnus-summary-make-marking-command elem)))
12720
12721 (defun gnus-summary-make-marking-command (name mark keystroke)
12722   (let ((map (make-sparse-keymap)))
12723     (define-key gnus-summary-generic-mark-map keystroke map)
12724     (dolist (lway `((next "next" next nil "n")
12725                     (next-unread "next unread" next t "N")
12726                     (prev "previous" prev nil "p")
12727                     (prev-unread "previous unread" prev t "P")
12728                     (nomove "" nil nil ,keystroke)))
12729       (let ((func (gnus-summary-make-marking-command-1
12730                    mark (car lway) lway name)))
12731         (setq func (eval func))
12732         (define-key map (nth 4 lway) func)))))
12733
12734 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12735   `(defun ,(intern
12736             (format "gnus-summary-put-mark-as-%s%s"
12737                     name (if (eq way 'nomove)
12738                              ""
12739                            (concat "-" (symbol-name way)))))
12740      (n)
12741      ,(format
12742        "Mark the current article as %s%s.
12743 If N, the prefix, then repeat N times.
12744 If N is negative, move in reverse order.
12745 The difference between N and the actual number of articles marked is
12746 returned."
12747        name (cadr lway))
12748      (interactive "p")
12749      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12750
12751 (defun gnus-summary-generic-mark (n mark move unread)
12752   "Mark N articles with MARK."
12753   (unless (eq major-mode 'gnus-summary-mode)
12754     (error "This command can only be used in the summary buffer"))
12755   (gnus-summary-show-thread)
12756   (let ((nummove
12757          (cond
12758           ((eq move 'next) 1)
12759           ((eq move 'prev) -1)
12760           (t 0))))
12761     (if (zerop nummove)
12762         (setq n 1)
12763       (when (< n 0)
12764         (setq n (abs n)
12765               nummove (* -1 nummove))))
12766     (while (and (> n 0)
12767                 (gnus-summary-mark-article nil mark)
12768                 (zerop (gnus-summary-next-subject nummove unread t)))
12769       (setq n (1- n)))
12770     (when (/= 0 n)
12771       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12772     (gnus-summary-recenter)
12773     (gnus-summary-position-point)
12774     (gnus-set-mode-line 'summary)
12775     n))
12776
12777 (defun gnus-summary-insert-articles (articles)
12778   (when (setq articles
12779               (gnus-sorted-difference articles
12780                                       (mapcar (lambda (h)
12781                                                 (mail-header-number h))
12782                                               gnus-newsgroup-headers)))
12783     (setq gnus-newsgroup-headers
12784           (gnus-merge 'list
12785                       gnus-newsgroup-headers
12786                       (gnus-fetch-headers articles)
12787                       'gnus-article-sort-by-number))
12788     (setq gnus-newsgroup-articles
12789           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12790     ;; Suppress duplicates?
12791     (when gnus-suppress-duplicates
12792       (gnus-dup-suppress-articles))
12793
12794     (if (and gnus-fetch-old-headers
12795              (eq gnus-headers-retrieved-by 'nov))
12796         ;; We might want to build some more threads first.
12797         (if (eq gnus-fetch-old-headers 'invisible)
12798             (gnus-build-all-threads)
12799           (gnus-build-old-threads))
12800       ;; Mark the inserted articles that are unread as unread.
12801       (setq gnus-newsgroup-unreads
12802             (gnus-sorted-nunion
12803              gnus-newsgroup-unreads
12804              (gnus-sorted-nintersection
12805               (gnus-list-of-unread-articles gnus-newsgroup-name)
12806               articles)))
12807       ;; Mark the inserted articles as selected so that the information
12808       ;; of the marks having been changed by a user may be updated when
12809       ;; exiting this group.  See `gnus-summary-update-info'.
12810       (dolist (art articles)
12811         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12812     ;; Let the Gnus agent mark articles as read.
12813     (when gnus-agent
12814       (gnus-agent-get-undownloaded-list))
12815     ;; Remove list identifiers from subject
12816     (gnus-summary-remove-list-identifiers)
12817     ;; First and last article in this newsgroup.
12818     (when gnus-newsgroup-headers
12819       (setq gnus-newsgroup-begin
12820             (mail-header-number (car gnus-newsgroup-headers))
12821             gnus-newsgroup-end
12822             (mail-header-number
12823              (gnus-last-element gnus-newsgroup-headers))))
12824     (when gnus-use-scoring
12825       (gnus-possibly-score-headers))))
12826
12827 (defun gnus-summary-insert-old-articles (&optional all)
12828   "Insert all old articles in this group.
12829 If ALL is non-nil, already read articles become readable.
12830 If ALL is a number, fetch this number of articles."
12831   (interactive "P")
12832   (prog1
12833       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12834             older len)
12835         (setq older
12836               ;; Some nntp servers lie about their active range.  When
12837               ;; this happens, the active range can be in the millions.
12838               ;; Use a compressed range to avoid creating a huge list.
12839               (gnus-range-difference
12840                (gnus-range-difference (list gnus-newsgroup-active) old)
12841                gnus-newsgroup-unexist))
12842         (setq len (gnus-range-length older))
12843         (cond
12844          ((null older) nil)
12845          ((numberp all)
12846           (if (< all len)
12847               (let ((older-range (nreverse older)))
12848                 (setq older nil)
12849
12850                 (while (> all 0)
12851                   (let* ((r (pop older-range))
12852                          (min (if (numberp r) r (car r)))
12853                          (max (if (numberp r) r (cdr r))))
12854                     (while (and (<= min max)
12855                                 (> all 0))
12856                       (push max older)
12857                       (setq all (1- all)
12858                             max (1- max))))))
12859             (setq older (gnus-uncompress-range older))))
12860          (all
12861           (setq older (gnus-uncompress-range older)))
12862          (t
12863           (when (and (numberp gnus-large-newsgroup)
12864                    (> len gnus-large-newsgroup))
12865               (let* ((cursor-in-echo-area nil)
12866                      (initial (gnus-parameter-large-newsgroup-initial
12867                                gnus-newsgroup-name))
12868                      (input
12869                       (read-string
12870                        (format
12871                         "How many articles from %s (%s %d): "
12872                         (gnus-group-decoded-name gnus-newsgroup-name)
12873                         (if initial "max" "default")
12874                         len)
12875                        nil nil
12876                        (and initial
12877                             (number-to-string initial)))))
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