Allow setting the partial fetch per server instead of globally.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (eval-when-compile
34   (when (featurep 'xemacs)
35     (require 'easy-mmode))) ; for `define-minor-mode'
36
37 (defvar tool-bar-mode)
38 (defvar gnus-tmp-header)
39
40 (require 'gnus)
41 (require 'gnus-group)
42 (require 'gnus-spec)
43 (require 'gnus-range)
44 (require 'gnus-int)
45 (require 'gnus-undo)
46 (require 'gnus-util)
47 (require 'gmm-utils)
48 (require 'mm-decode)
49 (require 'nnoo)
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
54 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
55 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-summary-next-group-on-exit t
72   "If non-nil, go to the next unread newsgroup on summary exit.
73 See `gnus-group-goto-unread'."
74   :link '(custom-manual "(gnus)Group Maneuvering")
75   :group 'gnus-summary-exit
76   :version "23.1" ;; No Gnus
77   :type 'boolean)
78
79 (defcustom gnus-summary-stop-at-end-of-message nil
80   "If non-nil, don't select the next message when using `SPC'."
81   :link '(custom-manual "(gnus)Group Maneuvering")
82   :group 'gnus-summary-maneuvering
83   :version "24.1"
84   :type 'boolean)
85
86 (defcustom gnus-fetch-old-headers nil
87   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
88 If an unread article in the group refers to an older, already
89 read (or just marked as read) article, the old article will not
90 normally be displayed in the Summary buffer.  If this variable is
91 t, Gnus will attempt to grab the headers to the old articles, and
92 thereby build complete threads.  If it has the value `some', all
93 old headers will be fetched but only enough headers to connect
94 otherwise loose threads will be displayed.  This variable can
95 also be a number.  In that case, no more than that number of old
96 headers will be fetched.  If it has the value `invisible', all
97 old headers will be fetched, but none will be displayed.
98
99 The server has to support NOV for any of this to work.
100
101 This feature can seriously impact performance it ignores all
102 locally cached header entries.  Setting it to t for groups for a
103 server that doesn't expire articles (such as news.gmane.org),
104 leads to very slow summary generation."
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const :tag "on" t)
108                  (const some)
109                  (const invisible)
110                  number
111                  (sexp :menu-tag "other" t)))
112
113 (defcustom gnus-refer-thread-limit 500
114   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
115 If t, fetch all the available old headers."
116   :group 'gnus-thread
117   :type '(choice number
118                  (sexp :menu-tag "other" t)))
119
120 (defcustom gnus-summary-make-false-root 'adopt
121   "*nil means that Gnus won't gather loose threads.
122 If the root of a thread has expired or been read in a previous
123 session, the information necessary to build a complete thread has been
124 lost.  Instead of having many small sub-threads from this original thread
125 scattered all over the summary buffer, Gnus can gather them.
126
127 If non-nil, Gnus will try to gather all loose sub-threads from an
128 original thread into one large thread.
129
130 If this variable is non-nil, it should be one of `none', `adopt',
131 `dummy' or `empty'.
132
133 If this variable is `none', Gnus will not make a false root, but just
134 present the sub-threads after another.
135 If this variable is `dummy', Gnus will create a dummy root that will
136 have all the sub-threads as children.
137 If this variable is `adopt', Gnus will make one of the \"children\"
138 the parent and mark all the step-children as such.
139 If this variable is `empty', the \"children\" are printed with empty
140 subject fields.  (Or rather, they will be printed with a string
141 given by the `gnus-summary-same-subject' variable.)"
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const none)
145                  (const dummy)
146                  (const adopt)
147                  (const empty)))
148
149 (defcustom gnus-summary-make-false-root-always nil
150   "Always make a false dummy root."
151   :version "22.1"
152   :group 'gnus-thread
153   :type 'boolean)
154
155 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
156   "*A regexp to match subjects to be excluded from loose thread gathering.
157 As loose thread gathering is done on subjects only, that means that
158 there can be many false gatherings performed.  By rooting out certain
159 common subjects, gathering might become saner."
160   :group 'gnus-thread
161   :type 'regexp)
162
163 (defcustom gnus-summary-gather-subject-limit nil
164   "*Maximum length of subject comparisons when gathering loose threads.
165 Use nil to compare full subjects.  Setting this variable to a low
166 number will help gather threads that have been corrupted by
167 newsreaders chopping off subject lines, but it might also mean that
168 unrelated articles that have subject that happen to begin with the
169 same few characters will be incorrectly gathered.
170
171 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
172 comparing subjects."
173   :group 'gnus-thread
174   :type '(choice (const :tag "off" nil)
175                  (const fuzzy)
176                  (sexp :menu-tag "on" t)))
177
178 (defcustom gnus-simplify-subject-functions nil
179   "List of functions taking a string argument that simplify subjects.
180 The functions are applied recursively.
181
182 Useful functions to put in this list include:
183 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
184 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
185   :group 'gnus-thread
186   :type '(repeat function))
187
188 (defcustom gnus-simplify-ignored-prefixes nil
189   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
190   :group 'gnus-thread
191   :type '(choice (const :tag "off" nil)
192                  regexp))
193
194 (defcustom gnus-build-sparse-threads nil
195   "*If non-nil, fill in the gaps in threads.
196 If `some', only fill in the gaps that are needed to tie loose threads
197 together.  If `more', fill in all leaf nodes that Gnus can find.  If
198 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  (const some)
202                  (const more)
203                  (sexp :menu-tag "all" t)))
204
205 (defcustom gnus-summary-thread-gathering-function
206   'gnus-gather-threads-by-subject
207   "*Function used for gathering loose threads.
208 There are two pre-defined functions: `gnus-gather-threads-by-subject',
209 which only takes Subjects into consideration; and
210 `gnus-gather-threads-by-references', which compared the References
211 headers of the articles to find matches."
212   :group 'gnus-thread
213   :type '(radio (function-item gnus-gather-threads-by-subject)
214                 (function-item gnus-gather-threads-by-references)
215                 (function :tag "other")))
216
217 (defcustom gnus-summary-same-subject ""
218   "*String indicating that the current article has the same subject as the previous.
219 This variable will only be used if the value of
220 `gnus-summary-make-false-root' is `empty'."
221   :group 'gnus-summary-format
222   :type 'string)
223
224 (defcustom gnus-summary-goto-unread nil
225   "*If t, many commands will go to the next unread article.
226 This applies to marking commands as well as other commands that
227 \"naturally\" select the next article, like, for instance, `SPC' at
228 the end of an article.
229
230 If nil, the marking commands do NOT go to the next unread article
231 \(they go to the next article instead).  If `never', commands that
232 usually go to the next unread article, will go to the next article,
233 whether it is read or not."
234   :version "24.1"
235   :group 'gnus-summary-marks
236   :link '(custom-manual "(gnus)Setting Marks")
237   :type '(choice (const :tag "off" nil)
238                  (const never)
239                  (sexp :menu-tag "on" t)))
240
241 (defcustom gnus-summary-default-score 0
242   "*Default article score level.
243 All scores generated by the score files will be added to this score.
244 If this variable is nil, scoring will be disabled."
245   :group 'gnus-score-default
246   :type '(choice (const :tag "disable")
247                  integer))
248
249 (defcustom gnus-summary-default-high-score 0
250   "*Default threshold for a high scored article.
251 An article will be highlighted as high scored if its score is greater
252 than this score."
253   :version "22.1"
254   :group 'gnus-score-default
255   :type 'integer)
256
257 (defcustom gnus-summary-default-low-score 0
258   "*Default threshold for a low scored article.
259 An article will be highlighted as low scored if its score is smaller
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-zcore-fuzz 0
266   "*Fuzziness factor for the zcore in the summary buffer.
267 Articles with scores closer than this to `gnus-summary-default-score'
268 will not be marked."
269   :group 'gnus-summary-format
270   :type 'integer)
271
272 (defcustom gnus-simplify-subject-fuzzy-regexp nil
273   "*Strings to be removed when doing fuzzy matches.
274 This can either be a regular expression or list of regular expressions
275 that will be removed from subject strings if fuzzy subject
276 simplification is selected."
277   :group 'gnus-thread
278   :type '(repeat regexp))
279
280 (defcustom gnus-show-threads t
281   "*If non-nil, display threads in summary mode."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-hide-subtree nil
286   "*If non-nil, hide all threads initially.
287 This can be a predicate specifier which says which threads to hide.
288 If threads are hidden, you have to run the command
289 `gnus-summary-show-thread' by hand or select an article."
290   :group 'gnus-thread
291   :type '(radio (sexp :format "Non-nil\n"
292                       :match (lambda (widget value)
293                                (not (or (consp value) (functionp value))))
294                       :value t)
295                 (const nil)
296                 (sexp :tag "Predicate specifier")))
297
298 (defcustom gnus-thread-hide-killed t
299   "*If non-nil, hide killed threads automatically."
300   :group 'gnus-thread
301   :type 'boolean)
302
303 (defcustom gnus-thread-ignore-subject t
304   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
305 If nil, articles that have different subjects from their parents will
306 start separate threads."
307   :group 'gnus-thread
308   :type 'boolean)
309
310 (defcustom gnus-thread-operation-ignore-subject t
311   "*If non-nil, subjects will be ignored when doing thread commands.
312 This affects commands like `gnus-summary-kill-thread' and
313 `gnus-summary-lower-thread'.
314
315 If this variable is nil, articles in the same thread with different
316 subjects will not be included in the operation in question.  If this
317 variable is `fuzzy', only articles that have subjects that are fuzzily
318 equal will be included."
319   :group 'gnus-thread
320   :type '(choice (const :tag "off" nil)
321                  (const fuzzy)
322                  (sexp :tag "on" t)))
323
324 (defcustom gnus-thread-indent-level 4
325   "*Number that says how much each sub-thread should be indented."
326   :group 'gnus-thread
327   :type 'integer)
328
329 (defcustom gnus-auto-extend-newsgroup t
330   "*If non-nil, extend newsgroup forward and backward when requested."
331   :group 'gnus-summary-choose
332   :type 'boolean)
333
334 (defcustom gnus-auto-select-first t
335   "If non-nil, select an article on group entry.
336 An article is selected automatically when entering a group
337 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
338 `gnus-summary-catchup-and-goto-next-group'.
339
340 Which article is selected is controlled by the variable
341 `gnus-auto-select-subject'.
342
343 If you want to prevent automatic selection of articles in some
344 newsgroups, set the variable to nil in `gnus-select-group-hook'."
345   ;; Commands include...
346   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
347   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
349   :group 'gnus-group-select
350   :type '(choice (const :tag "none" nil)
351                  (sexp :menu-tag "first" t)))
352
353 (defcustom gnus-auto-select-subject 'unseen-or-unread
354   "*Says what subject to place under point when entering a group.
355
356 This variable can either be the symbols `first' (place point on the
357 first subject), `unread' (place point on the subject line of the first
358 unread article), `best' (place point on the subject line of the
359 higest-scored article), `unseen' (place point on the subject line of
360 the first unseen article), `unseen-or-unread' (place point on the subject
361 line of the first unseen article or, if all article have been seen, on the
362 subject line of the first unread article), or a function to be called to
363 place point on some subject line."
364   :version "24.1"
365   :group 'gnus-group-select
366   :type '(choice (const best)
367                  (const unread)
368                  (const first)
369                  (const unseen)
370                  (const unseen-or-unread)))
371
372 (defcustom gnus-auto-select-next t
373   "*If non-nil, offer to go to the next group from the end of the previous.
374 If the value is t and the next newsgroup is empty, Gnus will exit
375 summary mode and go back to group mode.  If the value is neither nil
376 nor t, Gnus will select the following unread newsgroup.  In
377 particular, if the value is the symbol `quietly', the next unread
378 newsgroup will be selected without any confirmation, and if it is
379 `almost-quietly', the next group will be selected without any
380 confirmation if you are located on the last article in the group.
381 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
382 will go to the next group without confirmation."
383   :group 'gnus-summary-maneuvering
384   :type '(choice (const :tag "off" nil)
385                  (const quietly)
386                  (const almost-quietly)
387                  (const slightly-quietly)
388                  (sexp :menu-tag "on" t)))
389
390 (defcustom gnus-auto-select-same nil
391   "*If non-nil, select the next article with the same subject.
392 If there are no more articles with the same subject, go to
393 the first unread article."
394   :group 'gnus-summary-maneuvering
395   :type 'boolean)
396
397 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
398   "What article should be selected after exiting an ephemeral group.
399 Valid values include:
400
401 `next'
402   Select the next article.
403 `next-unread'
404   Select the next unread article.
405 `next-noselect'
406   Move the cursor to the next article.  This is the default.
407 `next-unread-noselect'
408   Move the cursor to the next unread article.
409
410 If it has any other value or there is no next (unread) article, the
411 article selected before entering to the ephemeral group will appear."
412   :version "23.1" ;; No Gnus
413   :group 'gnus-summary-maneuvering
414   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
415                  (const next) (const next-unread)
416                  (const next-noselect) (const next-unread-noselect)
417                  (sexp :tag "other" :value nil)))
418
419 (defcustom gnus-auto-goto-ignores 'unfetched
420   "*Says how to handle unfetched articles when maneuvering.
421
422 This variable can either be the symbols nil (maneuver to any
423 article), `undownloaded' (maneuvering while unplugged ignores articles
424 that have not been fetched), `always-undownloaded' (maneuvering always
425 ignores articles that have not been fetched), `unfetched' (maneuvering
426 ignores articles whose headers have not been fetched).
427
428 NOTE: The list of unfetched articles will always be nil when plugged
429 and, when unplugged, a subset of the undownloaded article list."
430   :version "22.1"
431   :group 'gnus-summary-maneuvering
432   :type '(choice (const :tag "None" nil)
433                  (const :tag "Undownloaded when unplugged" undownloaded)
434                  (const :tag "Undownloaded" always-undownloaded)
435                  (const :tag "Unfetched" unfetched)))
436
437 (defcustom gnus-summary-check-current nil
438   "*If non-nil, consider the current article when moving.
439 The \"unread\" movement commands will stay on the same line if the
440 current article is unread."
441   :group 'gnus-summary-maneuvering
442   :type 'boolean)
443
444 (defcustom gnus-auto-center-summary 2
445   "*If non-nil, always center the current summary buffer.
446 In particular, if `vertical' do only vertical recentering.  If non-nil
447 and non-`vertical', do both horizontal and vertical recentering."
448   :group 'gnus-summary-maneuvering
449   :type '(choice (const :tag "none" nil)
450                  (const vertical)
451                  (integer :tag "height")
452                  (sexp :menu-tag "both" t)))
453
454 (defcustom gnus-auto-center-group t
455   "If non-nil, always center the group buffer."
456   :group 'gnus-summary-maneuvering
457   :type 'boolean)
458
459 (defcustom gnus-show-all-headers nil
460   "*If non-nil, don't hide any headers."
461   :group 'gnus-article-hiding
462   :group 'gnus-article-headers
463   :type 'boolean)
464
465 (defcustom gnus-summary-ignore-duplicates nil
466   "*If non-nil, ignore articles with identical Message-ID headers."
467   :group 'gnus-summary
468   :type 'boolean)
469
470 (defcustom gnus-single-article-buffer nil
471   "*If non-nil, display all articles in the same buffer.
472 If nil, each group will get its own article buffer."
473   :version "24.1"
474   :group 'gnus-article-various
475   :type 'boolean)
476
477 (defcustom gnus-break-pages t
478   "*If non-nil, do page breaking on articles.
479 The page delimiter is specified by the `gnus-page-delimiter'
480 variable."
481   :group 'gnus-article-various
482   :type 'boolean)
483
484 (defcustom gnus-move-split-methods nil
485   "*Variable used to suggest where articles are to be moved to.
486 It uses the same syntax as the `gnus-split-methods' variable.
487 However, whereas `gnus-split-methods' specifies file names as targets,
488 this variable specifies group names."
489   :group 'gnus-summary-mail
490   :type '(repeat (choice (list :value (fun) function)
491                          (cons :value ("" "") regexp (repeat string))
492                          (sexp :value nil))))
493
494 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
495   "Function used to compute default prefix for article move/copy/etc prompts.
496 The function should take one argument, a group name, and return a
497 string with the suggested prefix."
498   :group 'gnus-summary-mail
499   :type 'function)
500
501 ;; FIXME: Although the custom type is `character' for the following variables,
502 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
503
504 (defcustom gnus-unread-mark ?           ;Whitespace
505   "*Mark used for unread articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-ticked-mark ?!
510   "*Mark used for ticked articles."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-dormant-mark ??
515   "*Mark used for dormant articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-del-mark ?r
520   "*Mark used for del'd articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-read-mark ?R
525   "*Mark used for read articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-expirable-mark ?E
530   "*Mark used for expirable articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-killed-mark ?K
535   "*Mark used for killed articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-spam-mark ?$
540   "*Mark used for spam articles."
541   :version "22.1"
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-kill-file-mark ?X
546   "*Mark used for articles killed by kill files."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-low-score-mark ?Y
551   "*Mark used for articles with a low score."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-catchup-mark ?C
556   "*Mark used for articles that are caught up."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-replied-mark ?A
561   "*Mark used for articles that have been replied to."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-forwarded-mark ?F
566   "*Mark used for articles that have been forwarded."
567   :version "22.1"
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-recent-mark ?N
572   "*Mark used for articles that are recent."
573   :version "22.1"
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-cached-mark ?*
578   "*Mark used for articles that are in the cache."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-saved-mark ?S
583   "*Mark used for articles that have been saved."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-unseen-mark ?.
588   "*Mark used for articles that haven't been seen."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-no-mark ?               ;Whitespace
594   "*Mark used for articles that have no other secondary mark."
595   :version "22.1"
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-ancient-mark ?O
600   "*Mark used for ancient articles."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-sparse-mark ?Q
605   "*Mark used for sparsely reffed articles."
606   :group 'gnus-summary-marks
607   :type 'character)
608
609 (defcustom gnus-canceled-mark ?G
610   "*Mark used for canceled articles."
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-duplicate-mark ?M
615   "*Mark used for duplicate articles."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-undownloaded-mark ?-
620   "*Mark used for articles that weren't downloaded."
621   :version "22.1"
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-downloaded-mark ?+
626   "*Mark used for articles that were downloaded."
627   :group 'gnus-summary-marks
628   :type 'character)
629
630 (defcustom gnus-downloadable-mark ?%
631   "*Mark used for articles that are to be downloaded."
632   :group 'gnus-summary-marks
633   :type 'character)
634
635 (defcustom gnus-unsendable-mark ?=
636   "*Mark used for articles that won't be sent."
637   :group 'gnus-summary-marks
638   :type 'character)
639
640 (defcustom gnus-score-over-mark ?+
641   "*Score mark used for articles with high scores."
642   :group 'gnus-summary-marks
643   :type 'character)
644
645 (defcustom gnus-score-below-mark ?-
646   "*Score mark used for articles with low scores."
647   :group 'gnus-summary-marks
648   :type 'character)
649
650 (defcustom gnus-empty-thread-mark ?     ;Whitespace
651   "*There is no thread under the article."
652   :group 'gnus-summary-marks
653   :type 'character)
654
655 (defcustom gnus-not-empty-thread-mark ?=
656   "*There is a thread under the article."
657   :group 'gnus-summary-marks
658   :type 'character)
659
660 (defcustom gnus-view-pseudo-asynchronously nil
661   "*If non-nil, Gnus will view pseudo-articles asynchronously."
662   :group 'gnus-extract-view
663   :type 'boolean)
664
665 (defcustom gnus-auto-expirable-marks
666   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
667         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
668         gnus-duplicate-mark)
669   "*The list of marks converted into expiration if a group is auto-expirable."
670   :version "24.1"
671   :group 'gnus-summary
672   :type '(repeat character))
673
674 (defcustom gnus-inhibit-user-auto-expire t
675   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
676   :version "21.1"
677   :group 'gnus-summary
678   :type 'boolean)
679
680 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
681   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
682 If nil, the expirable marks will be unchanged except that the marks
683 will be removed when copying or moving articles to a group that has
684 not turned auto-expire on.  If non-nil, articles that have been read
685 will be marked as expirable when being copied or moved to a group in
686 which auto-expire is turned on."
687   :version "23.2"
688   :type 'boolean
689   :group 'gnus-summary-marks)
690
691 (defcustom gnus-view-pseudos nil
692   "*If `automatic', pseudo-articles will be viewed automatically.
693 If `not-confirm', pseudos will be viewed automatically, and the user
694 will not be asked to confirm the command."
695   :group 'gnus-extract-view
696   :type '(choice (const :tag "off" nil)
697                  (const automatic)
698                  (const not-confirm)))
699
700 (defcustom gnus-view-pseudos-separately t
701   "*If non-nil, one pseudo-article will be created for each file to be viewed.
702 If nil, all files that use the same viewing command will be given as a
703 list of parameters to that command."
704   :group 'gnus-extract-view
705   :type 'boolean)
706
707 (defcustom gnus-insert-pseudo-articles t
708   "*If non-nil, insert pseudo-articles when decoding articles."
709   :group 'gnus-extract-view
710   :type 'boolean)
711
712 (defcustom gnus-summary-dummy-line-format
713   "   %(:                             :%) %S\n"
714   "*The format specification for the dummy roots in the summary buffer.
715 It works along the same lines as a normal formatting string,
716 with some simple extensions.
717
718 %S  The subject
719
720 General format specifiers can also be used.
721 See `(gnus)Formatting Variables'."
722   :link '(custom-manual "(gnus)Formatting Variables")
723   :group 'gnus-threading
724   :type 'string)
725
726 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
727   "*The format specification for the summary mode line.
728 It works along the same lines as a normal formatting string,
729 with some simple extensions:
730
731 %G  Group name
732 %p  Unprefixed group name
733 %A  Current article number
734 %z  Current article score
735 %V  Gnus version
736 %U  Number of unread articles in the group
737 %e  Number of unselected articles in the group
738 %Z  A string with unread/unselected article counts
739 %g  Shortish group name
740 %S  Subject of the current article
741 %u  User-defined spec
742 %s  Current score file name
743 %d  Number of dormant articles
744 %r  Number of articles that have been marked as read in this session
745 %E  Number of articles expunged by the score files"
746   :group 'gnus-summary-format
747   :type 'string)
748
749 (defcustom gnus-list-identifiers nil
750   "Regexp that matches list identifiers to be removed from subject.
751 This can also be a list of regexps."
752   :version "21.1"
753   :group 'gnus-summary-format
754   :group 'gnus-article-hiding
755   :type '(choice (const :tag "none" nil)
756                  (regexp :value ".*")
757                  (repeat :value (".*") regexp)))
758
759 (defcustom gnus-summary-mark-below 0
760   "*Mark all articles with a score below this variable as read.
761 This variable is local to each summary buffer and usually set by the
762 score file."
763   :group 'gnus-score-default
764   :type 'integer)
765
766 (defun gnus-widget-reversible-match (widget value)
767   "Ignoring WIDGET, convert VALUE to internal form.
768 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
769   ;; (debug value)
770   (or (symbolp value)
771       (and (listp value)
772            (eq (length value) 2)
773            (eq (nth 0 value) 'not)
774            (symbolp (nth 1 value)))))
775
776 (defun gnus-widget-reversible-to-internal (widget value)
777   "Ignoring WIDGET, convert VALUE to internal form.
778 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
779 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
780   ;; (debug value)
781   (if (atom value)
782       (list value nil)
783     (list (nth 1 value) t)))
784
785 (defun gnus-widget-reversible-to-external (widget value)
786   "Ignoring WIDGET, convert VALUE to external form.
787 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
788 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
789   ;; (debug value)
790   (if (nth 1 value)
791       (list 'not (nth 0 value))
792     (nth 0 value)))
793
794 (define-widget 'gnus-widget-reversible 'group
795   "A `group' that convert values."
796   :match 'gnus-widget-reversible-match
797   :value-to-internal 'gnus-widget-reversible-to-internal
798   :value-to-external 'gnus-widget-reversible-to-external)
799
800 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
801   "*List of functions used for sorting articles in the summary buffer.
802
803 Each function takes two articles and returns non-nil if the first
804 article should be sorted before the other.  If you use more than one
805 function, the primary sort function should be the last.  You should
806 probably always include `gnus-article-sort-by-number' in the list of
807 sorting functions -- preferably first.  Also note that sorting by date
808 is often much slower than sorting by number, and the sorting order is
809 very similar.  (Sorting by date means sorting by the time the message
810 was sent, sorting by number means sorting by arrival time.)
811
812 Each item can also be a list `(not F)' where F is a function;
813 this reverses the sort order.
814
815 Ready-made functions include `gnus-article-sort-by-number',
816 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
817 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
818 and `gnus-article-sort-by-score'.
819
820 When threading is turned on, the variable `gnus-thread-sort-functions'
821 controls how articles are sorted."
822   :group 'gnus-summary-sort
823   :type '(repeat (gnus-widget-reversible
824                   (choice (function-item gnus-article-sort-by-number)
825                           (function-item gnus-article-sort-by-author)
826                           (function-item gnus-article-sort-by-subject)
827                           (function-item gnus-article-sort-by-date)
828                           (function-item gnus-article-sort-by-score)
829                           (function-item gnus-article-sort-by-random)
830                           (function :tag "other"))
831                   (boolean :tag "Reverse order"))))
832
833
834 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
835   "*List of functions used for sorting threads in the summary buffer.
836 By default, threads are sorted by article number.
837
838 Each function takes two threads and returns non-nil if the first
839 thread should be sorted before the other.  If you use more than one
840 function, the primary sort function should be the last.  You should
841 probably always include `gnus-thread-sort-by-number' in the list of
842 sorting functions -- preferably first.  Also note that sorting by date
843 is often much slower than sorting by number, and the sorting order is
844 very similar.  (Sorting by date means sorting by the time the message
845 was sent, sorting by number means sorting by arrival time.)
846
847 Each list item can also be a list `(not F)' where F is a
848 function; this specifies reversed sort order.
849
850 Ready-made functions include `gnus-thread-sort-by-number',
851 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
852 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
853 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
854 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
855 and `gnus-thread-sort-by-total-score' (see
856 `gnus-thread-score-function').
857
858 When threading is turned off, the variable
859 `gnus-article-sort-functions' controls how articles are sorted."
860   :group 'gnus-summary-sort
861   :type '(repeat
862           (gnus-widget-reversible
863            (choice (function-item gnus-thread-sort-by-number)
864                    (function-item gnus-thread-sort-by-author)
865                    (function-item gnus-thread-sort-by-recipient)
866                    (function-item gnus-thread-sort-by-subject)
867                    (function-item gnus-thread-sort-by-date)
868                    (function-item gnus-thread-sort-by-score)
869                    (function-item gnus-thread-sort-by-most-recent-number)
870                    (function-item gnus-thread-sort-by-most-recent-date)
871                    (function-item gnus-thread-sort-by-random)
872                    (function-item gnus-thread-sort-by-total-score)
873                    (function :tag "other"))
874            (boolean :tag "Reverse order"))))
875
876 (defcustom gnus-thread-score-function '+
877   "*Function used for calculating the total score of a thread.
878
879 The function is called with the scores of the article and each
880 subthread and should then return the score of the thread.
881
882 Some functions you can use are `+', `max', or `min'."
883   :group 'gnus-summary-sort
884   :type 'function)
885
886 (defcustom gnus-summary-expunge-below nil
887   "All articles that have a score less than this variable will be expunged.
888 This variable is local to the summary buffers."
889   :group 'gnus-score-default
890   :type '(choice (const :tag "off" nil)
891                  integer))
892
893 (defcustom gnus-thread-expunge-below nil
894   "All threads that have a total score less than this variable will be expunged.
895 See `gnus-thread-score-function' for en explanation of what a
896 \"thread score\" is.
897
898 This variable is local to the summary buffers."
899   :group 'gnus-threading
900   :group 'gnus-score-default
901   :type '(choice (const :tag "off" nil)
902                  integer))
903
904 (defcustom gnus-summary-mode-hook nil
905   "*A hook for Gnus summary mode.
906 This hook is run before any variables are set in the summary buffer."
907   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
908   :group 'gnus-summary-various
909   :type 'hook)
910
911 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
912 (when (featurep 'xemacs)
913   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
914   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
915   (add-hook 'gnus-summary-mode-hook
916             'gnus-xmas-switch-horizontal-scrollbar-off))
917
918 (defcustom gnus-summary-menu-hook nil
919   "*Hook run after the creation of the summary mode menu."
920   :group 'gnus-summary-visual
921   :type 'hook)
922
923 (defcustom gnus-summary-exit-hook nil
924   "*A hook called on exit from the summary buffer.
925 It will be called with point in the group buffer."
926   :group 'gnus-summary-exit
927   :type 'hook)
928
929 (defcustom gnus-summary-prepare-hook nil
930   "*A hook called after the summary buffer has been generated.
931 If you want to modify the summary buffer, you can use this hook."
932   :group 'gnus-summary-various
933   :type 'hook)
934
935 (defcustom gnus-summary-prepared-hook nil
936   "*A hook called as the last thing after the summary buffer has been generated."
937   :group 'gnus-summary-various
938   :type 'hook)
939
940 (defcustom gnus-summary-generate-hook nil
941   "*A hook run just before generating the summary buffer.
942 This hook is commonly used to customize threading variables and the
943 like."
944   :group 'gnus-summary-various
945   :type 'hook)
946
947 (defcustom gnus-select-group-hook nil
948   "*A hook called when a newsgroup is selected.
949
950 If you'd like to simplify subjects like the
951 `gnus-summary-next-same-subject' command does, you can use the
952 following hook:
953
954  (add-hook gnus-select-group-hook
955            (lambda ()
956              (mapcar (lambda (header)
957                        (mail-header-set-subject
958                         header
959                         (gnus-simplify-subject
960                          (mail-header-subject header) 're-only)))
961                      gnus-newsgroup-headers)))"
962   :group 'gnus-group-select
963   :type 'hook)
964
965 (defcustom gnus-select-article-hook nil
966   "*A hook called when an article is selected."
967   :group 'gnus-summary-choose
968   :options '(gnus-agent-fetch-selected-article)
969   :type 'hook)
970
971 (defcustom gnus-visual-mark-article-hook
972   (list 'gnus-highlight-selected-summary)
973   "*Hook run after selecting an article in the summary buffer.
974 It is meant to be used for highlighting the article in some way.  It
975 is not run if `gnus-visual' is nil."
976   :group 'gnus-summary-visual
977   :type 'hook)
978
979 (defcustom gnus-parse-headers-hook nil
980   "*A hook called before parsing the headers."
981   :group 'gnus-various
982   :type 'hook)
983
984 (defcustom gnus-exit-group-hook nil
985   "*A hook called when exiting summary mode.
986 This hook is not called from the non-updating exit commands like `Q'."
987   :group 'gnus-various
988   :type 'hook)
989
990 (defcustom gnus-summary-update-hook nil
991   "*A hook called when a summary line is changed.
992 The hook will not be called if `gnus-visual' is nil.
993
994 The default function `gnus-summary-highlight-line' will
995 highlight the line according to the `gnus-summary-highlight'
996 variable."
997   :group 'gnus-summary-visual
998   :type 'hook)
999
1000 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1001   "*A hook called when an article is selected for the first time.
1002 The hook is intended to mark an article as read (or unread)
1003 automatically when it is selected."
1004   :group 'gnus-summary-choose
1005   :type 'hook)
1006
1007 (defcustom gnus-group-no-more-groups-hook nil
1008   "*A hook run when returning to group mode having no more (unread) groups."
1009   :group 'gnus-group-select
1010   :type 'hook)
1011
1012 (defcustom gnus-ps-print-hook nil
1013   "*A hook run before ps-printing something from Gnus."
1014   :group 'gnus-summary
1015   :type 'hook)
1016
1017 (defcustom gnus-summary-article-move-hook nil
1018   "*A hook called after an article is moved, copied, respooled, or crossposted."
1019   :version "22.1"
1020   :group 'gnus-summary
1021   :type 'hook)
1022
1023 (defcustom gnus-summary-article-delete-hook nil
1024   "*A hook called after an article is deleted."
1025   :version "22.1"
1026   :group 'gnus-summary
1027   :type 'hook)
1028
1029 (defcustom gnus-summary-article-expire-hook nil
1030   "*A hook called after an article is expired."
1031   :version "22.1"
1032   :group 'gnus-summary
1033   :type 'hook)
1034
1035 (defcustom gnus-summary-display-arrow
1036   (and (fboundp 'display-graphic-p)
1037        (display-graphic-p))
1038   "*If non-nil, display an arrow highlighting the current article."
1039   :version "22.1"
1040   :group 'gnus-summary
1041   :type 'boolean)
1042
1043 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1044   "Face used for highlighting the current article in the summary buffer."
1045   :group 'gnus-summary-visual
1046   :type 'face)
1047
1048 (defvar gnus-tmp-downloaded nil)
1049
1050 (defcustom gnus-summary-highlight
1051   '(((eq mark gnus-canceled-mark)
1052      . gnus-summary-cancelled)
1053     ((and uncached (> score default-high))
1054      . gnus-summary-high-undownloaded)
1055     ((and uncached (< score default-low))
1056      . gnus-summary-low-undownloaded)
1057     (uncached
1058      . gnus-summary-normal-undownloaded)
1059     ((and (> score default-high)
1060           (or (eq mark gnus-dormant-mark)
1061               (eq mark gnus-ticked-mark)))
1062      . gnus-summary-high-ticked)
1063     ((and (< score default-low)
1064           (or (eq mark gnus-dormant-mark)
1065               (eq mark gnus-ticked-mark)))
1066      . gnus-summary-low-ticked)
1067     ((or (eq mark gnus-dormant-mark)
1068          (eq mark gnus-ticked-mark))
1069      . gnus-summary-normal-ticked)
1070     ((and (> score default-high) (eq mark gnus-ancient-mark))
1071      . gnus-summary-high-ancient)
1072     ((and (< score default-low) (eq mark gnus-ancient-mark))
1073      . gnus-summary-low-ancient)
1074     ((eq mark gnus-ancient-mark)
1075      . gnus-summary-normal-ancient)
1076     ((and (> score default-high) (eq mark gnus-unread-mark))
1077      . gnus-summary-high-unread)
1078     ((and (< score default-low) (eq mark gnus-unread-mark))
1079      . gnus-summary-low-unread)
1080     ((eq mark gnus-unread-mark)
1081      . gnus-summary-normal-unread)
1082     ((> score default-high)
1083      . gnus-summary-high-read)
1084     ((< score default-low)
1085      . gnus-summary-low-read)
1086     (t
1087      . gnus-summary-normal-read))
1088   "*Controls the highlighting of summary buffer lines.
1089
1090 A list of (FORM . FACE) pairs.  When deciding how a particular
1091 summary line should be displayed, each form is evaluated.  The content
1092 of the face field after the first true form is used.  You can change
1093 how those summary lines are displayed, by editing the face field.
1094
1095 You can use the following variables in the FORM field.
1096
1097 score:        The article's score.
1098 default:      The default article score.
1099 default-high: The default score for high scored articles.
1100 default-low:  The default score for low scored articles.
1101 below:        The score below which articles are automatically marked as read.
1102 mark:         The article's mark.
1103 uncached:     Non-nil if the article is uncached."
1104   :group 'gnus-summary-visual
1105   :type '(repeat (cons (sexp :tag "Form" nil)
1106                        face)))
1107 (put 'gnus-summary-highlight 'risky-local-variable t)
1108
1109 (defcustom gnus-alter-header-function nil
1110   "Function called to allow alteration of article header structures.
1111 The function is called with one parameter, the article header vector,
1112 which it may alter in any way."
1113   :type '(choice (const :tag "None" nil)
1114                  function)
1115   :group 'gnus-summary)
1116
1117 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1118   "Function used to decode a string with encoded words.")
1119
1120 (defvar gnus-decode-encoded-address-function
1121   'mail-decode-encoded-address-string
1122   "Function used to decode addresses with encoded words.")
1123
1124 (defcustom gnus-extra-headers '(To Newsgroups)
1125   "*Extra headers to parse."
1126   :version "21.1"
1127   :group 'gnus-summary
1128   :type '(repeat symbol))
1129
1130 (defcustom gnus-ignored-from-addresses
1131   (and user-mail-address
1132        (not (string= user-mail-address ""))
1133        (regexp-quote user-mail-address))
1134   "*From headers that may be suppressed in favor of To headers.
1135 This can be a regexp or a list of regexps."
1136   :version "21.1"
1137   :group 'gnus-summary
1138   :type '(choice regexp
1139                  (repeat :tag "Regexp List" regexp)))
1140
1141 (defsubst gnus-ignored-from-addresses ()
1142   (gmm-regexp-concat gnus-ignored-from-addresses))
1143
1144 (defcustom gnus-summary-to-prefix "-> "
1145   "*String prefixed to the To field in the summary line when
1146 using `gnus-ignored-from-addresses'."
1147   :version "22.1"
1148   :group 'gnus-summary
1149   :type 'string)
1150
1151 (defcustom gnus-summary-newsgroup-prefix "=> "
1152   "*String prefixed to the Newsgroup field in the summary
1153 line when using `gnus-ignored-from-addresses'."
1154   :version "22.1"
1155   :group 'gnus-summary
1156   :type 'string)
1157
1158 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1159   "List of charsets that should be ignored.
1160 When these charsets are used in the \"charset\" parameter, the
1161 default charset will be used instead."
1162   :version "21.1"
1163   :type '(repeat symbol)
1164   :group 'gnus-charset)
1165
1166 (defcustom gnus-newsgroup-maximum-articles nil
1167   "The maximum number of articles a newsgroup.
1168 If this is a number, old articles in a newsgroup exceeding this number
1169 are silently ignored.  If it is nil, no article is ignored.  Note that
1170 setting this variable to a number might prevent you from reading very
1171 old articles."
1172   :group 'gnus-group-select
1173   :version "22.2"
1174   :type '(choice (const :tag "No limit" nil)
1175                  integer))
1176
1177 (gnus-define-group-parameter
1178  ignored-charsets
1179  :type list
1180  :function-document
1181  "Return the ignored charsets of GROUP."
1182  :variable gnus-group-ignored-charsets-alist
1183  :variable-default
1184  '(("alt\\.chinese\\.text" iso-8859-1))
1185  :variable-document
1186  "Alist of regexps (to match group names) and charsets that should be ignored.
1187 When these charsets are used in the \"charset\" parameter, the
1188 default charset will be used instead."
1189  :variable-group gnus-charset
1190  :variable-type '(repeat (cons (regexp :tag "Group")
1191                                (repeat symbol)))
1192  :parameter-type '(choice :tag "Ignored charsets"
1193                           :value nil
1194                           (repeat (symbol)))
1195  :parameter-document       "\
1196 List of charsets that should be ignored.
1197
1198 When these charsets are used in the \"charset\" parameter, the
1199 default charset will be used instead.")
1200
1201 (defcustom gnus-group-highlight-words-alist nil
1202   "Alist of group regexps and highlight regexps.
1203 This variable uses the same syntax as `gnus-emphasis-alist'."
1204   :version "21.1"
1205   :type '(repeat (cons (regexp :tag "Group")
1206                        (repeat (list (regexp :tag "Highlight regexp")
1207                                      (number :tag "Group for entire word" 0)
1208                                      (number :tag "Group for displayed part" 0)
1209                                      (symbol :tag "Face"
1210                                              gnus-emphasis-highlight-words)))))
1211   :group 'gnus-summary-visual)
1212
1213 (defcustom gnus-summary-show-article-charset-alist
1214   nil
1215   "Alist of number and charset.
1216 The article will be shown with the charset corresponding to the
1217 numbered argument.
1218 For example: ((1 . cn-gb-2312) (2 . big5))."
1219   :version "21.1"
1220   :type '(repeat (cons (number :tag "Argument" 1)
1221                        (symbol :tag "Charset")))
1222   :group 'gnus-charset)
1223
1224 (defcustom gnus-preserve-marks t
1225   "Whether marks are preserved when moving, copying and respooling messages."
1226   :version "21.1"
1227   :type 'boolean
1228   :group 'gnus-summary-marks)
1229
1230 (defcustom gnus-propagate-marks t
1231   "If non-nil, do not propagate marks to the backends."
1232   :version "23.1" ;; No Gnus
1233   :type 'boolean
1234   :group 'gnus-summary-marks)
1235
1236 (defcustom gnus-alter-articles-to-read-function nil
1237   "Function to be called to alter the list of articles to be selected."
1238   :type '(choice (const nil) function)
1239   :group 'gnus-summary)
1240
1241 (defcustom gnus-orphan-score nil
1242   "*All orphans get this score added.  Set in the score file."
1243   :group 'gnus-score-default
1244   :type '(choice (const nil)
1245                  integer))
1246
1247 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1248   "*A regexp to match MIME parts when saving multiple parts of a
1249 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1250 This regexp will be used by default when prompting the user for which
1251 type of files to save."
1252   :group 'gnus-summary
1253   :type 'regexp)
1254
1255 (defcustom gnus-read-all-available-headers nil
1256   "Whether Gnus should parse all headers made available to it.
1257 This is mostly relevant for slow back ends where the user may
1258 wish to widen the summary buffer to include all headers
1259 that were fetched."
1260   :version "22.1"
1261   :group 'gnus-summary
1262   :type '(choice boolean regexp))
1263
1264 (defcustom gnus-summary-pipe-output-default-command nil
1265   "Command (and optional arguments) used to pipe article to subprocess.
1266 This will be used as the default command if it is non-nil.  The value
1267 will be updated if you modify it when executing the command
1268 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1269   :version "23.1" ;; No Gnus
1270   :group 'gnus-summary
1271   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1272
1273 (defcustom gnus-summary-muttprint-program "muttprint"
1274   "Command (and optional arguments) used to run Muttprint.
1275 The value will be updated if you modify it when executing the command
1276 `gnus-summary-muttprint'."
1277   :version "22.1"
1278   :group 'gnus-summary
1279   :type 'string)
1280
1281 (defcustom gnus-article-loose-mime t
1282   "If non-nil, don't require MIME-Version header.
1283 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1284 supply the MIME-Version header or deliberately strip it from the mail.
1285 If non-nil (the default), Gnus will treat some articles as MIME
1286 even if the MIME-Version header is missing."
1287   :version "22.1"
1288   :type 'boolean
1289   :group 'gnus-article-mime)
1290
1291 (defcustom gnus-article-emulate-mime t
1292   "If non-nil, use MIME emulation for uuencode and the like.
1293 This means that Gnus will search message bodies for text that look
1294 like uuencoded bits, yEncoded bits, and so on, and present that using
1295 the normal Gnus MIME machinery."
1296   :version "22.1"
1297   :type 'boolean
1298   :group 'gnus-article-mime)
1299
1300 ;;; Internal variables
1301
1302 (defvar gnus-summary-display-cache nil)
1303 (defvar gnus-article-mime-handles nil)
1304 (defvar gnus-article-decoded-p nil)
1305 (defvar gnus-article-charset nil)
1306 (defvar gnus-article-ignored-charsets nil)
1307 (defvar gnus-scores-exclude-files nil)
1308 (defvar gnus-page-broken nil)
1309
1310 (defvar gnus-original-article nil)
1311 (defvar gnus-article-internal-prepare-hook nil)
1312 (defvar gnus-newsgroup-process-stack nil)
1313
1314 (defvar gnus-thread-indent-array nil)
1315 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1316 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1317   "Function called to sort the articles within a thread after it has been gathered together.")
1318
1319 (defvar gnus-summary-save-parts-type-history nil)
1320 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1321
1322 ;; Avoid highlighting in kill files.
1323 (defvar gnus-summary-inhibit-highlight nil)
1324 (defvar gnus-newsgroup-selected-overlay nil)
1325 (defvar gnus-inhibit-limiting nil)
1326 (defvar gnus-newsgroup-adaptive-score-file nil)
1327 (defvar gnus-current-score-file nil)
1328 (defvar gnus-current-move-group nil)
1329 (defvar gnus-current-copy-group nil)
1330 (defvar gnus-current-crosspost-group nil)
1331 (defvar gnus-newsgroup-display nil)
1332
1333 (defvar gnus-newsgroup-dependencies nil)
1334 (defvar gnus-newsgroup-adaptive nil)
1335 (defvar gnus-summary-display-article-function nil)
1336 (defvar gnus-summary-highlight-line-function nil
1337   "Function called after highlighting a summary line.")
1338
1339 (defvar gnus-summary-line-format-alist
1340   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1341     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1342     (?s gnus-tmp-subject-or-nil ?s)
1343     (?n gnus-tmp-name ?s)
1344     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1345         ?s)
1346     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1347             gnus-tmp-from) ?s)
1348     (?F gnus-tmp-from ?s)
1349     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1350     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1351     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1352     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1353     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1354     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1355     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1356     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1357     (?L gnus-tmp-lines ?s)
1358     (?O gnus-tmp-downloaded ?c)
1359     (?I gnus-tmp-indentation ?s)
1360     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1361     (?R gnus-tmp-replied ?c)
1362     (?\[ gnus-tmp-opening-bracket ?c)
1363     (?\] gnus-tmp-closing-bracket ?c)
1364     (?\> (make-string gnus-tmp-level ? ) ?s)
1365     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1366     (?i gnus-tmp-score ?d)
1367     (?z gnus-tmp-score-char ?c)
1368     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1369     (?U gnus-tmp-unread ?c)
1370     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1371         ?s)
1372     (?t (gnus-summary-number-of-articles-in-thread
1373          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1374         ?d)
1375     (?e (gnus-summary-number-of-articles-in-thread
1376          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1377         ?c)
1378     (?u gnus-tmp-user-defined ?s)
1379     (?P (gnus-pick-line-number) ?d)
1380     (?B gnus-tmp-thread-tree-header-string ?s)
1381     (user-date (gnus-user-date
1382                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1383   "An alist of format specifications that can appear in summary lines.
1384 These are paired with what variables they correspond with, along with
1385 the type of the variable (string, integer, character, etc).")
1386
1387 (defvar gnus-summary-dummy-line-format-alist
1388   `((?S gnus-tmp-subject ?s)
1389     (?N gnus-tmp-number ?d)
1390     (?u gnus-tmp-user-defined ?s)))
1391
1392 (defvar gnus-summary-mode-line-format-alist
1393   `((?G gnus-tmp-group-name ?s)
1394     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1395     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1396     (?A gnus-tmp-article-number ?d)
1397     (?Z gnus-tmp-unread-and-unselected ?s)
1398     (?V gnus-version ?s)
1399     (?U gnus-tmp-unread-and-unticked ?d)
1400     (?S gnus-tmp-subject ?s)
1401     (?e gnus-tmp-unselected ?d)
1402     (?u gnus-tmp-user-defined ?s)
1403     (?d (length gnus-newsgroup-dormant) ?d)
1404     (?t (length gnus-newsgroup-marked) ?d)
1405     (?h (length gnus-newsgroup-spam-marked) ?d)
1406     (?r (length gnus-newsgroup-reads) ?d)
1407     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1408     (?E gnus-newsgroup-expunged-tally ?d)
1409     (?s (gnus-current-score-file-nondirectory) ?s)))
1410
1411 ;; This is here rather than in gnus-art for compilation reasons.
1412 (defvar gnus-article-mode-line-format-alist
1413   (nconc '((?w (gnus-article-wash-status) ?s)
1414            (?m (gnus-article-mime-part-status) ?s))
1415          gnus-summary-mode-line-format-alist))
1416
1417 (defvar gnus-last-search-regexp nil
1418   "Default regexp for article search command.")
1419
1420 (defvar gnus-last-shell-command nil
1421   "Default shell command on article.")
1422
1423 (defvar gnus-newsgroup-agentized nil
1424   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1425 (defvar gnus-newsgroup-begin nil)
1426 (defvar gnus-newsgroup-end nil)
1427 (defvar gnus-newsgroup-last-rmail nil)
1428 (defvar gnus-newsgroup-last-mail nil)
1429 (defvar gnus-newsgroup-last-folder nil)
1430 (defvar gnus-newsgroup-last-file nil)
1431 (defvar gnus-newsgroup-last-directory nil)
1432 (defvar gnus-newsgroup-auto-expire nil)
1433 (defvar gnus-newsgroup-active nil)
1434
1435 (defvar gnus-newsgroup-data nil)
1436 (defvar gnus-newsgroup-data-reverse nil)
1437 (defvar gnus-newsgroup-limit nil)
1438 (defvar gnus-newsgroup-limits nil)
1439 (defvar gnus-summary-use-undownloaded-faces nil)
1440
1441 (defvar gnus-newsgroup-unreads nil
1442   "Sorted list of unread articles in the current newsgroup.")
1443
1444 (defvar gnus-newsgroup-unselected nil
1445   "Sorted list of unselected unread articles in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-reads nil
1448   "Alist of read articles and article marks in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-expunged-tally nil)
1451
1452 (defvar gnus-newsgroup-marked nil
1453   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1454
1455 (defvar gnus-newsgroup-spam-marked nil
1456   "List of ranges of articles that have been marked as spam.")
1457
1458 (defvar gnus-newsgroup-killed nil
1459   "List of ranges of articles that have been through the scoring process.")
1460
1461 (defvar gnus-newsgroup-cached nil
1462   "Sorted list of articles that come from the article cache.")
1463
1464 (defvar gnus-newsgroup-saved nil
1465   "List of articles that have been saved.")
1466
1467 (defvar gnus-newsgroup-kill-headers nil)
1468
1469 (defvar gnus-newsgroup-replied nil
1470   "List of articles that have been replied to in the current newsgroup.")
1471
1472 (defvar gnus-newsgroup-forwarded nil
1473   "List of articles that have been forwarded in the current newsgroup.")
1474
1475 (defvar gnus-newsgroup-recent nil
1476   "List of articles that have are recent in the current newsgroup.")
1477
1478 (defvar gnus-newsgroup-expirable nil
1479   "Sorted list of articles in the current newsgroup that can be expired.")
1480
1481 (defvar gnus-newsgroup-processable nil
1482   "List of articles in the current newsgroup that can be processed.")
1483
1484 (defvar gnus-newsgroup-downloadable nil
1485   "Sorted list of articles in the current newsgroup that can be processed.")
1486
1487 (defvar gnus-newsgroup-unfetched nil
1488   "Sorted list of articles in the current newsgroup whose headers have
1489 not been fetched into the agent.
1490
1491 This list will always be a subset of gnus-newsgroup-undownloaded.")
1492
1493 (defvar gnus-newsgroup-undownloaded nil
1494   "List of articles in the current newsgroup that haven't been downloaded.")
1495
1496 (defvar gnus-newsgroup-unsendable nil
1497   "List of articles in the current newsgroup that won't be sent.")
1498
1499 (defvar gnus-newsgroup-bookmarks nil
1500   "List of articles in the current newsgroup that have bookmarks.")
1501
1502 (defvar gnus-newsgroup-dormant nil
1503   "Sorted list of dormant articles in the current newsgroup.")
1504
1505 (defvar gnus-newsgroup-unseen nil
1506   "List of unseen articles in the current newsgroup.")
1507
1508 (defvar gnus-newsgroup-seen nil
1509   "Range of seen articles in the current newsgroup.")
1510
1511 (defvar gnus-newsgroup-articles nil
1512   "List of articles in the current newsgroup.")
1513
1514 (defvar gnus-newsgroup-scored nil
1515   "List of scored articles in the current newsgroup.")
1516
1517 (defvar gnus-newsgroup-headers nil
1518   "List of article headers in the current newsgroup.")
1519
1520 (defvar gnus-newsgroup-threads nil)
1521
1522 (defvar gnus-newsgroup-prepared nil
1523   "Whether the current group has been prepared properly.")
1524
1525 (defvar gnus-newsgroup-ancient nil
1526   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1527
1528 (defvar gnus-newsgroup-sparse nil)
1529
1530 (defvar gnus-current-article nil)
1531 (defvar gnus-article-current nil)
1532 (defvar gnus-current-headers nil)
1533 (defvar gnus-have-all-headers nil)
1534 (defvar gnus-last-article nil)
1535 (defvar gnus-newsgroup-history nil)
1536 (defvar gnus-newsgroup-charset nil)
1537 (defvar gnus-newsgroup-ephemeral-charset nil)
1538 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1539
1540 (defvar gnus-article-before-search nil)
1541
1542 (defvar gnus-summary-local-variables
1543   '(gnus-newsgroup-name
1544
1545     ;; Marks lists
1546     gnus-newsgroup-unreads
1547     gnus-newsgroup-unselected
1548     gnus-newsgroup-marked
1549     gnus-newsgroup-spam-marked
1550     gnus-newsgroup-reads
1551     gnus-newsgroup-saved
1552     gnus-newsgroup-replied
1553     gnus-newsgroup-forwarded
1554     gnus-newsgroup-recent
1555     gnus-newsgroup-expirable
1556     gnus-newsgroup-killed
1557     gnus-newsgroup-unseen
1558     gnus-newsgroup-seen
1559     gnus-newsgroup-cached
1560     gnus-newsgroup-downloadable
1561     gnus-newsgroup-undownloaded
1562     gnus-newsgroup-unsendable
1563
1564     gnus-newsgroup-begin gnus-newsgroup-end
1565     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1566     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1567     gnus-newsgroup-last-directory
1568     gnus-newsgroup-auto-expire
1569     gnus-newsgroup-processable
1570     gnus-newsgroup-unfetched
1571     gnus-newsgroup-articles
1572     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1573     gnus-newsgroup-headers gnus-newsgroup-threads
1574     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1575     gnus-current-article gnus-current-headers gnus-have-all-headers
1576     gnus-last-article gnus-article-internal-prepare-hook
1577     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1578     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1579     gnus-thread-expunge-below
1580     gnus-score-alist gnus-current-score-file
1581     (gnus-summary-expunge-below . global)
1582     (gnus-summary-mark-below . global)
1583     (gnus-orphan-score . global)
1584     gnus-newsgroup-active gnus-scores-exclude-files
1585     gnus-newsgroup-history gnus-newsgroup-ancient
1586     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1587     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1588     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1589     (gnus-newsgroup-expunged-tally . 0)
1590     gnus-cache-removable-articles
1591     gnus-newsgroup-data gnus-newsgroup-data-reverse
1592     gnus-newsgroup-limit gnus-newsgroup-limits
1593     gnus-newsgroup-charset gnus-newsgroup-display
1594     gnus-summary-use-undownloaded-faces)
1595   "Variables that are buffer-local to the summary buffers.")
1596
1597 (defvar gnus-newsgroup-variables nil
1598   "A list of variables that have separate values in different newsgroups.
1599 A list of newsgroup (summary buffer) local variables, or cons of
1600 variables and their default expressions to be evalled (when the default
1601 values are not nil), that should be made global while the summary buffer
1602 is active.
1603
1604 Note: The default expressions will be evaluated (using function `eval')
1605 before assignment to the local variable rather than just assigned to it.
1606 If the default expression is the symbol `global', that symbol will not
1607 be evaluated but the global value of the local variable will be used
1608 instead.
1609
1610 These variables can be used to set variables in the group parameters
1611 while still allowing them to affect operations done in other buffers.
1612 For example:
1613
1614 \(setq gnus-newsgroup-variables
1615      '(message-use-followup-to
1616        (gnus-visible-headers .
1617          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1618 ")
1619
1620 (eval-when-compile
1621   ;; Bind features so that require will believe that gnus-sum has
1622   ;; already been loaded (avoids infinite recursion)
1623   (let ((features (cons 'gnus-sum features)))
1624     (require 'gnus-art)))
1625
1626 ;; MIME stuff.
1627
1628 (defvar gnus-decode-encoded-word-methods
1629   '(mail-decode-encoded-word-string)
1630   "List of methods used to decode encoded words.
1631
1632 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1633 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1634 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1635 whose names match REGEXP.
1636
1637 For example:
1638 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1639  mail-decode-encoded-word-string
1640  (\"chinese\" . rfc1843-decode-string))")
1641
1642 (defvar gnus-decode-encoded-word-methods-cache nil)
1643
1644 (defun gnus-multi-decode-encoded-word-string (string)
1645   "Apply the functions from `gnus-encoded-word-methods' that match."
1646   (unless (and gnus-decode-encoded-word-methods-cache
1647                (eq gnus-newsgroup-name
1648                    (car gnus-decode-encoded-word-methods-cache)))
1649     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1650     (dolist (method gnus-decode-encoded-word-methods)
1651       (if (symbolp method)
1652           (nconc gnus-decode-encoded-word-methods-cache (list method))
1653         (if (and gnus-newsgroup-name
1654                  (string-match (car method) gnus-newsgroup-name))
1655             (nconc gnus-decode-encoded-word-methods-cache
1656                    (list (cdr method)))))))
1657   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1658     (setq string (funcall method string))))
1659
1660 ;; Subject simplification.
1661
1662 (defun gnus-simplify-whitespace (str)
1663   "Remove excessive whitespace from STR."
1664   ;; Multiple spaces.
1665   (while (string-match "[ \t][ \t]+" str)
1666     (setq str (concat (substring str 0 (match-beginning 0))
1667                         " "
1668                         (substring str (match-end 0)))))
1669   ;; Leading spaces.
1670   (when (string-match "^[ \t]+" str)
1671     (setq str (substring str (match-end 0))))
1672   ;; Trailing spaces.
1673   (when (string-match "[ \t]+$" str)
1674     (setq str (substring str 0 (match-beginning 0))))
1675   str)
1676
1677 (defun gnus-simplify-all-whitespace (str)
1678   "Remove all whitespace from STR."
1679   (while (string-match "[ \t\n]+" str)
1680     (setq str (replace-match "" nil nil str)))
1681   str)
1682
1683 (defsubst gnus-simplify-subject-re (subject)
1684   "Remove \"Re:\" from subject lines."
1685   (if (string-match message-subject-re-regexp subject)
1686       (substring subject (match-end 0))
1687     subject))
1688
1689 (defun gnus-simplify-subject (subject &optional re-only)
1690   "Remove `Re:' and words in parentheses.
1691 If RE-ONLY is non-nil, strip leading `Re:'s only."
1692   (let ((case-fold-search t))           ;Ignore case.
1693     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1694     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1695       (setq subject (substring subject (match-end 0))))
1696     ;; Remove uninteresting prefixes.
1697     (when (and (not re-only)
1698                gnus-simplify-ignored-prefixes
1699                (string-match gnus-simplify-ignored-prefixes subject))
1700       (setq subject (substring subject (match-end 0))))
1701     ;; Remove words in parentheses from end.
1702     (unless re-only
1703       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1704         (setq subject (substring subject 0 (match-beginning 0)))))
1705     ;; Return subject string.
1706     subject))
1707
1708 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1709 ;; all whitespace.
1710 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1711   (goto-char (point-min))
1712   (while (re-search-forward regexp nil t)
1713     (replace-match (or newtext ""))))
1714
1715 (defun gnus-simplify-buffer-fuzzy ()
1716   "Simplify string in the buffer fuzzily.
1717 The string in the accessible portion of the current buffer is simplified.
1718 It is assumed to be a single-line subject.
1719 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1720 matter is removed.  Additional things can be deleted by setting
1721 `gnus-simplify-subject-fuzzy-regexp'."
1722   (let ((case-fold-search t)
1723         (modified-tick))
1724     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1725
1726     (while (not (eq modified-tick (buffer-modified-tick)))
1727       (setq modified-tick (buffer-modified-tick))
1728       (cond
1729        ((listp gnus-simplify-subject-fuzzy-regexp)
1730         (mapc 'gnus-simplify-buffer-fuzzy-step
1731               gnus-simplify-subject-fuzzy-regexp))
1732        (gnus-simplify-subject-fuzzy-regexp
1733         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1734       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1735       (gnus-simplify-buffer-fuzzy-step
1736        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1737       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1738
1739     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1740     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1741     (gnus-simplify-buffer-fuzzy-step " $")
1742     (gnus-simplify-buffer-fuzzy-step "^ +")))
1743
1744 (defun gnus-simplify-subject-fuzzy (subject)
1745   "Simplify a subject string fuzzily.
1746 See `gnus-simplify-buffer-fuzzy' for details."
1747   (save-excursion
1748     (gnus-set-work-buffer)
1749     (let ((case-fold-search t))
1750       ;; Remove uninteresting prefixes.
1751       (when (and gnus-simplify-ignored-prefixes
1752                  (string-match gnus-simplify-ignored-prefixes subject))
1753         (setq subject (substring subject (match-end 0))))
1754       (insert subject)
1755       (inline (gnus-simplify-buffer-fuzzy))
1756       (buffer-string))))
1757
1758 (defsubst gnus-simplify-subject-fully (subject)
1759   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1760   (cond
1761    (gnus-simplify-subject-functions
1762     (gnus-map-function gnus-simplify-subject-functions subject))
1763    ((null gnus-summary-gather-subject-limit)
1764     (gnus-simplify-subject-re subject))
1765    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1766     (gnus-simplify-subject-fuzzy subject))
1767    ((numberp gnus-summary-gather-subject-limit)
1768     (truncate-string-to-width (gnus-simplify-subject-re subject)
1769                               gnus-summary-gather-subject-limit))
1770    (t
1771     subject)))
1772
1773 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1774   "Check whether two subjects are equal.
1775 If optional argument SIMPLE-FIRST is t, first argument is already
1776 simplified."
1777   (cond
1778    ((null simple-first)
1779     (equal (gnus-simplify-subject-fully s1)
1780            (gnus-simplify-subject-fully s2)))
1781    (t
1782     (equal s1
1783            (gnus-simplify-subject-fully s2)))))
1784
1785 (defun gnus-summary-bubble-group ()
1786   "Increase the score of the current group.
1787 This is a handy function to add to `gnus-summary-exit-hook' to
1788 increase the score of each group you read."
1789   (gnus-group-add-score gnus-newsgroup-name))
1790
1791 \f
1792 ;;;
1793 ;;; Gnus summary mode
1794 ;;;
1795
1796 (put 'gnus-summary-mode 'mode-class 'special)
1797
1798 (defvar gnus-article-commands-menu)
1799
1800 ;; Non-orthogonal keys
1801
1802 (gnus-define-keys gnus-summary-mode-map
1803   " " gnus-summary-next-page
1804   "\177" gnus-summary-prev-page
1805   [delete] gnus-summary-prev-page
1806   [backspace] gnus-summary-prev-page
1807   "\r" gnus-summary-scroll-up
1808   "\M-\r" gnus-summary-scroll-down
1809   "n" gnus-summary-next-unread-article
1810   "p" gnus-summary-prev-unread-article
1811   "N" gnus-summary-next-article
1812   "P" gnus-summary-prev-article
1813   "\M-\C-n" gnus-summary-next-same-subject
1814   "\M-\C-p" gnus-summary-prev-same-subject
1815   "\M-n" gnus-summary-next-unread-subject
1816   "\M-p" gnus-summary-prev-unread-subject
1817   "." gnus-summary-first-unread-article
1818   "," gnus-summary-best-unread-article
1819   "\M-s" gnus-summary-search-article-forward
1820   "\M-r" gnus-summary-search-article-backward
1821   "\M-S" gnus-summary-repeat-search-article-forward
1822   "\M-R" gnus-summary-repeat-search-article-backward
1823   "<" gnus-summary-beginning-of-article
1824   ">" gnus-summary-end-of-article
1825   "j" gnus-summary-goto-article
1826   "^" gnus-summary-refer-parent-article
1827   "\M-^" gnus-summary-refer-article
1828   "u" gnus-summary-tick-article-forward
1829   "!" gnus-summary-tick-article-forward
1830   "U" gnus-summary-tick-article-backward
1831   "d" gnus-summary-mark-as-read-forward
1832   "D" gnus-summary-mark-as-read-backward
1833   "E" gnus-summary-mark-as-expirable
1834   "\M-u" gnus-summary-clear-mark-forward
1835   "\M-U" gnus-summary-clear-mark-backward
1836   "k" gnus-summary-kill-same-subject-and-select
1837   "\C-k" gnus-summary-kill-same-subject
1838   "\M-\C-k" gnus-summary-kill-thread
1839   "\M-\C-l" gnus-summary-lower-thread
1840   "e" gnus-summary-edit-article
1841   "#" gnus-summary-mark-as-processable
1842   "\M-#" gnus-summary-unmark-as-processable
1843   "\M-\C-t" gnus-summary-toggle-threads
1844   "\M-\C-s" gnus-summary-show-thread
1845   "\M-\C-h" gnus-summary-hide-thread
1846   "\M-\C-f" gnus-summary-next-thread
1847   "\M-\C-b" gnus-summary-prev-thread
1848   [(meta down)] gnus-summary-next-thread
1849   [(meta up)] gnus-summary-prev-thread
1850   "\M-\C-u" gnus-summary-up-thread
1851   "\M-\C-d" gnus-summary-down-thread
1852   "&" gnus-summary-execute-command
1853   "c" gnus-summary-catchup-and-exit
1854   "\C-w" gnus-summary-mark-region-as-read
1855   "\C-t" gnus-summary-toggle-truncation
1856   "?" gnus-summary-mark-as-dormant
1857   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1858   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1859   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1860   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1861   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1862   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1863   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1864   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1865   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1866   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1867   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1868   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1869   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1870   "=" gnus-summary-expand-window
1871   "\C-x\C-s" gnus-summary-reselect-current-group
1872   "\M-g" gnus-summary-rescan-group
1873   "\C-c\C-r" gnus-summary-caesar-message
1874   "f" gnus-summary-followup
1875   "F" gnus-summary-followup-with-original
1876   "C" gnus-summary-cancel-article
1877   "r" gnus-summary-reply
1878   "R" gnus-summary-reply-with-original
1879   "\C-c\C-f" gnus-summary-mail-forward
1880   "o" gnus-summary-save-article
1881   "\C-o" gnus-summary-save-article-mail
1882   "|" gnus-summary-pipe-output
1883   "\M-k" gnus-summary-edit-local-kill
1884   "\M-K" gnus-summary-edit-global-kill
1885   ;; "V" gnus-version
1886   "\C-c\C-d" gnus-summary-describe-group
1887   "q" gnus-summary-exit
1888   "Q" gnus-summary-exit-no-update
1889   "\C-c\C-i" gnus-info-find-node
1890   gnus-mouse-2 gnus-mouse-pick-article
1891   [follow-link] mouse-face
1892   "m" gnus-summary-mail-other-window
1893   "a" gnus-summary-post-news
1894   "x" gnus-summary-limit-to-unread
1895   "s" gnus-summary-isearch-article
1896   "t" gnus-summary-toggle-header
1897   "g" gnus-summary-show-article
1898   "l" gnus-summary-goto-last-article
1899   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1900   "\C-d" gnus-summary-enter-digest-group
1901   "\M-\C-d" gnus-summary-read-document
1902   "\M-\C-e" gnus-summary-edit-parameters
1903   "\M-\C-a" gnus-summary-customize-parameters
1904   "\C-c\C-b" gnus-bug
1905   "*" gnus-cache-enter-article
1906   "\M-*" gnus-cache-remove-article
1907   "\M-&" gnus-summary-universal-argument
1908   "\C-l" gnus-recenter
1909   "I" gnus-summary-increase-score
1910   "L" gnus-summary-lower-score
1911   "\M-i" gnus-symbolic-argument
1912   "h" gnus-summary-select-article-buffer
1913
1914   "b" gnus-article-view-part
1915   "\M-t" gnus-summary-toggle-display-buttonized
1916
1917   "V" gnus-summary-score-map
1918   "X" gnus-uu-extract-map
1919   "S" gnus-summary-send-map)
1920
1921 ;; Sort of orthogonal keymap
1922 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1923   "t" gnus-summary-tick-article-forward
1924   "!" gnus-summary-tick-article-forward
1925   "d" gnus-summary-mark-as-read-forward
1926   "r" gnus-summary-mark-as-read-forward
1927   "c" gnus-summary-clear-mark-forward
1928   " " gnus-summary-clear-mark-forward
1929   "e" gnus-summary-mark-as-expirable
1930   "x" gnus-summary-mark-as-expirable
1931   "?" gnus-summary-mark-as-dormant
1932   "b" gnus-summary-set-bookmark
1933   "B" gnus-summary-remove-bookmark
1934   "#" gnus-summary-mark-as-processable
1935   "\M-#" gnus-summary-unmark-as-processable
1936   "S" gnus-summary-limit-include-expunged
1937   "C" gnus-summary-catchup
1938   "H" gnus-summary-catchup-to-here
1939   "h" gnus-summary-catchup-from-here
1940   "\C-c" gnus-summary-catchup-all
1941   "k" gnus-summary-kill-same-subject-and-select
1942   "K" gnus-summary-kill-same-subject
1943   "P" gnus-uu-mark-map)
1944
1945 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1946   "c" gnus-summary-clear-above
1947   "u" gnus-summary-tick-above
1948   "m" gnus-summary-mark-above
1949   "k" gnus-summary-kill-below)
1950
1951 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1952   "/" gnus-summary-limit-to-subject
1953   "n" gnus-summary-limit-to-articles
1954   "b" gnus-summary-limit-to-bodies
1955   "h" gnus-summary-limit-to-headers
1956   "w" gnus-summary-pop-limit
1957   "s" gnus-summary-limit-to-subject
1958   "a" gnus-summary-limit-to-author
1959   "u" gnus-summary-limit-to-unread
1960   "m" gnus-summary-limit-to-marks
1961   "M" gnus-summary-limit-exclude-marks
1962   "v" gnus-summary-limit-to-score
1963   "*" gnus-summary-limit-include-cached
1964   "D" gnus-summary-limit-include-dormant
1965   "T" gnus-summary-limit-include-thread
1966   "d" gnus-summary-limit-exclude-dormant
1967   "t" gnus-summary-limit-to-age
1968   "." gnus-summary-limit-to-unseen
1969   "x" gnus-summary-limit-to-extra
1970   "p" gnus-summary-limit-to-display-predicate
1971   "E" gnus-summary-limit-include-expunged
1972   "c" gnus-summary-limit-exclude-childless-dormant
1973   "C" gnus-summary-limit-mark-excluded-as-read
1974   "o" gnus-summary-insert-old-articles
1975   "N" gnus-summary-insert-new-articles
1976   "S" gnus-summary-limit-to-singletons
1977   "r" gnus-summary-limit-to-replied
1978   "R" gnus-summary-limit-to-recipient
1979   "A" gnus-summary-limit-to-address)
1980
1981 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1982   "n" gnus-summary-next-unread-article
1983   "p" gnus-summary-prev-unread-article
1984   "N" gnus-summary-next-article
1985   "P" gnus-summary-prev-article
1986   "\C-n" gnus-summary-next-same-subject
1987   "\C-p" gnus-summary-prev-same-subject
1988   "\M-n" gnus-summary-next-unread-subject
1989   "\M-p" gnus-summary-prev-unread-subject
1990   "f" gnus-summary-first-unread-article
1991   "b" gnus-summary-best-unread-article
1992   "j" gnus-summary-goto-article
1993   "g" gnus-summary-goto-subject
1994   "l" gnus-summary-goto-last-article
1995   "o" gnus-summary-pop-article)
1996
1997 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1998   "k" gnus-summary-kill-thread
1999   "E" gnus-summary-expire-thread
2000   "l" gnus-summary-lower-thread
2001   "i" gnus-summary-raise-thread
2002   "T" gnus-summary-toggle-threads
2003   "t" gnus-summary-rethread-current
2004   "^" gnus-summary-reparent-thread
2005   "\M-^" gnus-summary-reparent-children
2006   "s" gnus-summary-show-thread
2007   "S" gnus-summary-show-all-threads
2008   "h" gnus-summary-hide-thread
2009   "H" gnus-summary-hide-all-threads
2010   "n" gnus-summary-next-thread
2011   "p" gnus-summary-prev-thread
2012   "u" gnus-summary-up-thread
2013   "o" gnus-summary-top-thread
2014   "d" gnus-summary-down-thread
2015   "#" gnus-uu-mark-thread
2016   "\M-#" gnus-uu-unmark-thread)
2017
2018 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2019   "g" gnus-summary-prepare
2020   "c" gnus-summary-insert-cached-articles
2021   "d" gnus-summary-insert-dormant-articles
2022   "t" gnus-summary-insert-ticked-articles)
2023
2024 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2025   "c" gnus-summary-catchup-and-exit
2026   "C" gnus-summary-catchup-all-and-exit
2027   "E" gnus-summary-exit-no-update
2028   "Q" gnus-summary-exit
2029   "Z" gnus-summary-exit
2030   "n" gnus-summary-catchup-and-goto-next-group
2031   "p" gnus-summary-catchup-and-goto-prev-group
2032   "R" gnus-summary-reselect-current-group
2033   "G" gnus-summary-rescan-group
2034   "N" gnus-summary-next-group
2035   "s" gnus-summary-save-newsrc
2036   "P" gnus-summary-prev-group)
2037
2038 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2039   " " gnus-summary-next-page
2040   "n" gnus-summary-next-page
2041   "\177" gnus-summary-prev-page
2042   [delete] gnus-summary-prev-page
2043   "p" gnus-summary-prev-page
2044   "\r" gnus-summary-scroll-up
2045   "\M-\r" gnus-summary-scroll-down
2046   "<" gnus-summary-beginning-of-article
2047   ">" gnus-summary-end-of-article
2048   "b" gnus-summary-beginning-of-article
2049   "e" gnus-summary-end-of-article
2050   "^" gnus-summary-refer-parent-article
2051   "r" gnus-summary-refer-parent-article
2052   "C" gnus-summary-show-complete-article
2053   "D" gnus-summary-enter-digest-group
2054   "R" gnus-summary-refer-references
2055   "T" gnus-summary-refer-thread
2056   "g" gnus-summary-show-article
2057   "s" gnus-summary-isearch-article
2058   "P" gnus-summary-print-article
2059   "S" gnus-sticky-article
2060   "M" gnus-mailing-list-insinuate
2061   "t" gnus-article-babel)
2062
2063 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2064   "b" gnus-article-add-buttons
2065   "B" gnus-article-add-buttons-to-head
2066   "o" gnus-article-treat-overstrike
2067   "e" gnus-article-emphasize
2068   "w" gnus-article-fill-cited-article
2069   "Q" gnus-article-fill-long-lines
2070   "L" gnus-article-toggle-truncate-lines
2071   "C" gnus-article-capitalize-sentences
2072   "c" gnus-article-remove-cr
2073   "q" gnus-article-de-quoted-unreadable
2074   "6" gnus-article-de-base64-unreadable
2075   "Z" gnus-article-decode-HZ
2076   "A" gnus-article-treat-ansi-sequences
2077   "h" gnus-article-wash-html
2078   "u" gnus-article-unsplit-urls
2079   "s" gnus-summary-force-verify-and-decrypt
2080   "f" gnus-article-display-x-face
2081   "l" gnus-summary-stop-page-breaking
2082   "r" gnus-summary-caesar-message
2083   "m" gnus-summary-morse-message
2084   "t" gnus-summary-toggle-header
2085   "g" gnus-treat-smiley
2086   "v" gnus-summary-verbose-headers
2087   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2088   "p" gnus-article-verify-x-pgp-sig
2089   "d" gnus-article-treat-dumbquotes
2090   "i" gnus-summary-idna-message)
2091
2092 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2093   ;; mnemonic: deuglif*Y*
2094   "u" gnus-article-outlook-unwrap-lines
2095   "a" gnus-article-outlook-repair-attribution
2096   "c" gnus-article-outlook-rearrange-citation
2097   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2098
2099 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2100   "a" gnus-article-hide
2101   "h" gnus-article-hide-headers
2102   "b" gnus-article-hide-boring-headers
2103   "s" gnus-article-hide-signature
2104   "c" gnus-article-hide-citation
2105   "C" gnus-article-hide-citation-in-followups
2106   "l" gnus-article-hide-list-identifiers
2107   "B" gnus-article-strip-banner
2108   "P" gnus-article-hide-pem
2109   "\C-c" gnus-article-hide-citation-maybe)
2110
2111 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2112   "a" gnus-article-highlight
2113   "h" gnus-article-highlight-headers
2114   "c" gnus-article-highlight-citation
2115   "s" gnus-article-highlight-signature)
2116
2117 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2118   "f" gnus-article-treat-fold-headers
2119   "u" gnus-article-treat-unfold-headers
2120   "n" gnus-article-treat-fold-newsgroups)
2121
2122 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2123   "x" gnus-article-display-x-face
2124   "d" gnus-article-display-face
2125   "s" gnus-treat-smiley
2126   "D" gnus-article-remove-images
2127   "W" gnus-html-show-images
2128   "f" gnus-treat-from-picon
2129   "m" gnus-treat-mail-picon
2130   "n" gnus-treat-newsgroups-picon
2131   "g" gnus-treat-from-gravatar
2132   "h" gnus-treat-mail-gravatar)
2133
2134 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2135   "w" gnus-article-decode-mime-words
2136   "c" gnus-article-decode-charset
2137   "v" gnus-mime-view-all-parts
2138   "b" gnus-article-view-part)
2139
2140 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2141   "z" gnus-article-date-ut
2142   "u" gnus-article-date-ut
2143   "l" gnus-article-date-local
2144   "p" gnus-article-date-english
2145   "e" gnus-article-date-lapsed
2146   "o" gnus-article-date-original
2147   "i" gnus-article-date-iso8601
2148   "s" gnus-article-date-user)
2149
2150 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2151   "t" gnus-article-remove-trailing-blank-lines
2152   "l" gnus-article-strip-leading-blank-lines
2153   "m" gnus-article-strip-multiple-blank-lines
2154   "a" gnus-article-strip-blank-lines
2155   "A" gnus-article-strip-all-blank-lines
2156   "s" gnus-article-strip-leading-space
2157   "e" gnus-article-strip-trailing-space
2158   "w" gnus-article-remove-leading-whitespace)
2159
2160 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2161   "v" gnus-version
2162   "d" gnus-summary-describe-group
2163   "h" gnus-summary-describe-briefly
2164   "i" gnus-info-find-node
2165   "C" gnus-group-fetch-control)
2166
2167 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2168   "e" gnus-summary-expire-articles
2169   "\M-\C-e" gnus-summary-expire-articles-now
2170   "\177" gnus-summary-delete-article
2171   [delete] gnus-summary-delete-article
2172   [backspace] gnus-summary-delete-article
2173   "m" gnus-summary-move-article
2174   "r" gnus-summary-respool-article
2175   "w" gnus-summary-edit-article
2176   "c" gnus-summary-copy-article
2177   "B" gnus-summary-crosspost-article
2178   "q" gnus-summary-respool-query
2179   "t" gnus-summary-respool-trace
2180   "i" gnus-summary-import-article
2181   "I" gnus-summary-create-article
2182   "p" gnus-summary-article-posted-p)
2183
2184 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2185   "o" gnus-summary-save-article
2186   "m" gnus-summary-save-article-mail
2187   "F" gnus-summary-write-article-file
2188   "r" gnus-summary-save-article-rmail
2189   "f" gnus-summary-save-article-file
2190   "b" gnus-summary-save-article-body-file
2191   "B" gnus-summary-write-article-body-file
2192   "h" gnus-summary-save-article-folder
2193   "v" gnus-summary-save-article-vm
2194   "p" gnus-summary-pipe-output
2195   "P" gnus-summary-muttprint)
2196
2197 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2198   "b" gnus-summary-display-buttonized
2199   "m" gnus-summary-repair-multipart
2200   "v" gnus-article-view-part
2201   "o" gnus-article-save-part
2202   "O" gnus-article-save-part-and-strip
2203   "r" gnus-article-replace-part
2204   "d" gnus-article-delete-part
2205   "t" gnus-article-view-part-as-type
2206   "j" gnus-article-jump-to-part
2207   "c" gnus-article-copy-part
2208   "C" gnus-article-view-part-as-charset
2209   "e" gnus-article-view-part-externally
2210   "H" gnus-article-browse-html-article
2211   "E" gnus-article-encrypt-body
2212   "i" gnus-article-inline-part
2213   "|" gnus-article-pipe-part)
2214
2215 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2216   "p" gnus-summary-mark-as-processable
2217   "u" gnus-summary-unmark-as-processable
2218   "U" gnus-summary-unmark-all-processable
2219   "v" gnus-uu-mark-over
2220   "s" gnus-uu-mark-series
2221   "r" gnus-uu-mark-region
2222   "g" gnus-uu-unmark-region
2223   "R" gnus-uu-mark-by-regexp
2224   "G" gnus-uu-unmark-by-regexp
2225   "t" gnus-uu-mark-thread
2226   "T" gnus-uu-unmark-thread
2227   "a" gnus-uu-mark-all
2228   "b" gnus-uu-mark-buffer
2229   "S" gnus-uu-mark-sparse
2230   "k" gnus-summary-kill-process-mark
2231   "y" gnus-summary-yank-process-mark
2232   "w" gnus-summary-save-process-mark
2233   "i" gnus-uu-invert-processable)
2234
2235 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2236   ;;"x" gnus-uu-extract-any
2237   "m" gnus-summary-save-parts
2238   "u" gnus-uu-decode-uu
2239   "U" gnus-uu-decode-uu-and-save
2240   "s" gnus-uu-decode-unshar
2241   "S" gnus-uu-decode-unshar-and-save
2242   "o" gnus-uu-decode-save
2243   "O" gnus-uu-decode-save
2244   "b" gnus-uu-decode-binhex
2245   "B" gnus-uu-decode-binhex
2246   "Y" gnus-uu-decode-yenc
2247   "p" gnus-uu-decode-postscript
2248   "P" gnus-uu-decode-postscript-and-save)
2249
2250 (gnus-define-keys
2251     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2252   "u" gnus-uu-decode-uu-view
2253   "U" gnus-uu-decode-uu-and-save-view
2254   "s" gnus-uu-decode-unshar-view
2255   "S" gnus-uu-decode-unshar-and-save-view
2256   "o" gnus-uu-decode-save-view
2257   "O" gnus-uu-decode-save-view
2258   "b" gnus-uu-decode-binhex-view
2259   "B" gnus-uu-decode-binhex-view
2260   "p" gnus-uu-decode-postscript-view
2261   "P" gnus-uu-decode-postscript-and-save-view)
2262
2263 (defvar gnus-article-post-menu nil)
2264
2265 (defconst gnus-summary-menu-maxlen 20)
2266
2267 (defun gnus-summary-menu-split (menu)
2268   ;; If we have lots of elements, divide them into groups of 20
2269   ;; and make a pane (or submenu) for each one.
2270   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2271       (let ((menu menu) sublists next
2272             (i 1))
2273         (while menu
2274           ;; Pull off the next gnus-summary-menu-maxlen elements
2275           ;; and make them the next element of sublist.
2276           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2277           (if next
2278               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2279                       nil))
2280           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2281                                              (aref (car (last menu)) 0)) menu)
2282                                sublists))
2283           (setq i (1+ i))
2284           (setq menu next))
2285         (nreverse sublists))
2286     ;; Few elements--put them all in one pane.
2287     menu))
2288
2289 (defun gnus-summary-make-menu-bar ()
2290   (gnus-turn-off-edit-menu 'summary)
2291
2292   (unless (boundp 'gnus-summary-misc-menu)
2293
2294     (easy-menu-define
2295       gnus-summary-kill-menu gnus-summary-mode-map ""
2296       (cons
2297        "Score"
2298        (nconc
2299         (list
2300          ["Customize" gnus-score-customize t])
2301         (gnus-make-score-map 'increase)
2302         (gnus-make-score-map 'lower)
2303         '(("Mark"
2304            ["Kill below" gnus-summary-kill-below t]
2305            ["Mark above" gnus-summary-mark-above t]
2306            ["Tick above" gnus-summary-tick-above t]
2307            ["Clear above" gnus-summary-clear-above t])
2308           ["Current score" gnus-summary-current-score t]
2309           ["Set score" gnus-summary-set-score t]
2310           ["Switch current score file..." gnus-score-change-score-file t]
2311           ["Set mark below..." gnus-score-set-mark-below t]
2312           ["Set expunge below..." gnus-score-set-expunge-below t]
2313           ["Edit current score file" gnus-score-edit-current-scores t]
2314           ["Edit score file..." gnus-score-edit-file t]
2315           ["Trace score" gnus-score-find-trace t]
2316           ["Find words" gnus-score-find-favourite-words t]
2317           ["Rescore buffer" gnus-summary-rescore t]
2318           ["Increase score..." gnus-summary-increase-score t]
2319           ["Lower score..." gnus-summary-lower-score t]))))
2320
2321     ;; Define both the Article menu in the summary buffer and the
2322     ;; equivalent Commands menu in the article buffer here for
2323     ;; consistency.
2324     (let ((innards
2325            `(("Hide"
2326               ["All" gnus-article-hide t]
2327               ["Headers" gnus-article-hide-headers t]
2328               ["Signature" gnus-article-hide-signature t]
2329               ["Citation" gnus-article-hide-citation t]
2330               ["List identifiers" gnus-article-hide-list-identifiers t]
2331               ["Banner" gnus-article-strip-banner t]
2332               ["Boring headers" gnus-article-hide-boring-headers t])
2333              ("Highlight"
2334               ["All" gnus-article-highlight t]
2335               ["Headers" gnus-article-highlight-headers t]
2336               ["Signature" gnus-article-highlight-signature t]
2337               ["Citation" gnus-article-highlight-citation t])
2338              ("MIME"
2339               ["Words" gnus-article-decode-mime-words t]
2340               ["Charset" gnus-article-decode-charset t]
2341               ["QP" gnus-article-de-quoted-unreadable t]
2342               ["Base64" gnus-article-de-base64-unreadable t]
2343               ["View MIME buttons" gnus-summary-display-buttonized t]
2344               ["View all" gnus-mime-view-all-parts t]
2345               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2346               ["Encrypt body" gnus-article-encrypt-body
2347                :active (not (gnus-group-read-only-p))
2348                ,@(if (featurep 'xemacs) nil
2349                    '(:help "Encrypt the message body on disk"))]
2350               ["Extract all parts..." gnus-summary-save-parts t]
2351               ("Multipart"
2352                ["Repair multipart" gnus-summary-repair-multipart t]
2353                ["Pipe part..." gnus-article-pipe-part t]
2354                ["Inline part" gnus-article-inline-part t]
2355                ["View part as type..." gnus-article-view-part-as-type t]
2356                ["Encrypt body" gnus-article-encrypt-body
2357                 :active (not (gnus-group-read-only-p))
2358                ,@(if (featurep 'xemacs) nil
2359                    '(:help "Encrypt the message body on disk"))]
2360                ["View part externally" gnus-article-view-part-externally t]
2361                ["View HTML parts in browser" gnus-article-browse-html-article t]
2362                ["View part with charset..." gnus-article-view-part-as-charset t]
2363                ["Copy part" gnus-article-copy-part t]
2364                ["Save part..." gnus-article-save-part t]
2365                ["View part" gnus-article-view-part t]))
2366              ("Date"
2367               ["Local" gnus-article-date-local t]
2368               ["ISO8601" gnus-article-date-iso8601 t]
2369               ["UT" gnus-article-date-ut t]
2370               ["Original" gnus-article-date-original t]
2371               ["Lapsed" gnus-article-date-lapsed t]
2372               ["User-defined" gnus-article-date-user t])
2373              ("Display"
2374               ["Remove images" gnus-article-remove-images t]
2375               ["Toggle smiley" gnus-treat-smiley t]
2376               ["Show X-Face" gnus-article-display-x-face t]
2377               ["Show picons in From" gnus-treat-from-picon t]
2378               ["Show picons in mail headers" gnus-treat-mail-picon t]
2379               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2380               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2381               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2382               ("View as different encoding"
2383                ,@(gnus-summary-menu-split
2384                   (mapcar
2385                    (lambda (cs)
2386                      ;; Since easymenu under Emacs doesn't allow
2387                      ;; lambda forms for menu commands, we should
2388                      ;; provide intern'ed function symbols.
2389                      (let ((command (intern (format "\
2390 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2391                        (fset command
2392                              `(lambda ()
2393                                 (interactive)
2394                                 (let ((gnus-summary-show-article-charset-alist
2395                                        '((1 . ,cs))))
2396                                   (gnus-summary-show-article 1))))
2397                        `[,(symbol-name cs) ,command t]))
2398                    (sort (if (fboundp 'coding-system-list)
2399                              (coding-system-list)
2400                            (mapcar 'car mm-mime-mule-charset-alist))
2401                          'string<)))))
2402              ("Washing"
2403               ("Remove Blanks"
2404                ["Leading" gnus-article-strip-leading-blank-lines t]
2405                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2406                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2407                ["All of the above" gnus-article-strip-blank-lines t]
2408                ["All" gnus-article-strip-all-blank-lines t]
2409                ["Leading space" gnus-article-strip-leading-space t]
2410                ["Trailing space" gnus-article-strip-trailing-space t]
2411                ["Leading space in headers"
2412                 gnus-article-remove-leading-whitespace t])
2413               ["Overstrike" gnus-article-treat-overstrike t]
2414               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2415               ["Emphasis" gnus-article-emphasize t]
2416               ["Word wrap" gnus-article-fill-cited-article t]
2417               ["Fill long lines" gnus-article-fill-long-lines t]
2418               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2419               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2420               ["Remove CR" gnus-article-remove-cr t]
2421               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2422               ["Base64" gnus-article-de-base64-unreadable t]
2423               ["Rot 13" gnus-summary-caesar-message
2424                ,@(if (featurep 'xemacs) '(t)
2425                    '(:help "\"Caesar rotate\" article by 13"))]
2426               ["De-IDNA" gnus-summary-idna-message t]
2427               ["Morse decode" gnus-summary-morse-message t]
2428               ["Unix pipe..." gnus-summary-pipe-message t]
2429               ["Add buttons" gnus-article-add-buttons t]
2430               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2431               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2432               ["Verbose header" gnus-summary-verbose-headers t]
2433               ["Toggle header" gnus-summary-toggle-header t]
2434               ["Unfold headers" gnus-article-treat-unfold-headers t]
2435               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2436               ["Html" gnus-article-wash-html t]
2437               ["Unsplit URLs" gnus-article-unsplit-urls t]
2438               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2439               ["Decode HZ" gnus-article-decode-HZ t]
2440               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2441               ("(Outlook) Deuglify"
2442                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2443                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2444                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2445                ["Full (Outlook) deuglify"
2446                 gnus-article-outlook-deuglify-article t])
2447               )
2448              ("Output"
2449               ["Save in default format..." gnus-summary-save-article
2450                ,@(if (featurep 'xemacs) '(t)
2451                    '(:help "Save article using default method"))]
2452               ["Save in file..." gnus-summary-save-article-file
2453                ,@(if (featurep 'xemacs) '(t)
2454                    '(:help "Save article in file"))]
2455               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2456               ["Save in MH folder..." gnus-summary-save-article-folder t]
2457               ["Save in VM folder..." gnus-summary-save-article-vm t]
2458               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2459               ["Save body in file..." gnus-summary-save-article-body-file t]
2460               ["Pipe through a filter..." gnus-summary-pipe-output t]
2461               ["Print with Muttprint..." gnus-summary-muttprint t]
2462               ["Print" gnus-summary-print-article
2463                ,@(if (featurep 'xemacs) '(t)
2464                    '(:help "Generate and print a PostScript image"))])
2465              ("Copy, move,... (Backend)"
2466               ,@(if (featurep 'xemacs) nil
2467                   '(:help "Copying, moving, expiring articles..."))
2468               ["Respool article..." gnus-summary-respool-article t]
2469               ["Move article..." gnus-summary-move-article
2470                (gnus-check-backend-function
2471                 'request-move-article gnus-newsgroup-name)]
2472               ["Copy article..." gnus-summary-copy-article t]
2473               ["Crosspost article..." gnus-summary-crosspost-article
2474                (gnus-check-backend-function
2475                 'request-replace-article gnus-newsgroup-name)]
2476               ["Import file..." gnus-summary-import-article
2477                (gnus-check-backend-function
2478                 'request-accept-article gnus-newsgroup-name)]
2479               ["Create article..." gnus-summary-create-article
2480                (gnus-check-backend-function
2481                 'request-accept-article gnus-newsgroup-name)]
2482               ["Check if posted" gnus-summary-article-posted-p t]
2483               ["Edit article" gnus-summary-edit-article
2484                (not (gnus-group-read-only-p))]
2485               ["Delete article" gnus-summary-delete-article
2486                (gnus-check-backend-function
2487                 'request-expire-articles gnus-newsgroup-name)]
2488               ["Query respool" gnus-summary-respool-query t]
2489               ["Trace respool" gnus-summary-respool-trace t]
2490               ["Delete expirable articles" gnus-summary-expire-articles-now
2491                (gnus-check-backend-function
2492                 'request-expire-articles gnus-newsgroup-name)])
2493              ("Extract"
2494               ["Uudecode" gnus-uu-decode-uu
2495                ,@(if (featurep 'xemacs) '(t)
2496                    '(:help "Decode uuencoded article(s)"))]
2497               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2498               ["Unshar" gnus-uu-decode-unshar t]
2499               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2500               ["Save" gnus-uu-decode-save t]
2501               ["Binhex" gnus-uu-decode-binhex t]
2502               ["Postscript" gnus-uu-decode-postscript t]
2503               ["All MIME parts" gnus-summary-save-parts t])
2504              ("Cache"
2505               ["Enter article" gnus-cache-enter-article t]
2506               ["Remove article" gnus-cache-remove-article t])
2507              ["Translate" gnus-article-babel t]
2508              ["Select article buffer" gnus-summary-select-article-buffer t]
2509              ["Make article buffer sticky" gnus-sticky-article t]
2510              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2511              ["Isearch article..." gnus-summary-isearch-article t]
2512              ["Beginning of the article" gnus-summary-beginning-of-article t]
2513              ["End of the article" gnus-summary-end-of-article t]
2514              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2515              ["Fetch referenced articles" gnus-summary-refer-references t]
2516              ["Fetch current thread" gnus-summary-refer-thread t]
2517              ["Fetch article with id..." gnus-summary-refer-article t]
2518              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2519              ["Redisplay" gnus-summary-show-article t]
2520              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2521       (easy-menu-define
2522         gnus-summary-article-menu gnus-summary-mode-map ""
2523         (cons "Article" innards))
2524
2525       (if (not (keymapp gnus-summary-article-menu))
2526           (easy-menu-define
2527             gnus-article-commands-menu gnus-article-mode-map ""
2528             (cons "Commands" innards))
2529         ;; in Emacs, don't share menu.
2530         (setq gnus-article-commands-menu
2531               (copy-keymap gnus-summary-article-menu))
2532         (define-key gnus-article-mode-map [menu-bar commands]
2533           (cons "Commands" gnus-article-commands-menu))))
2534
2535     (easy-menu-define
2536       gnus-summary-thread-menu gnus-summary-mode-map ""
2537       '("Threads"
2538         ["Find all messages in thread" gnus-summary-refer-thread t]
2539         ["Toggle threading" gnus-summary-toggle-threads t]
2540         ["Hide threads" gnus-summary-hide-all-threads t]
2541         ["Show threads" gnus-summary-show-all-threads t]
2542         ["Hide thread" gnus-summary-hide-thread t]
2543         ["Show thread" gnus-summary-show-thread t]
2544         ["Go to next thread" gnus-summary-next-thread t]
2545         ["Go to previous thread" gnus-summary-prev-thread t]
2546         ["Go down thread" gnus-summary-down-thread t]
2547         ["Go up thread" gnus-summary-up-thread t]
2548         ["Top of thread" gnus-summary-top-thread t]
2549         ["Mark thread as read" gnus-summary-kill-thread t]
2550         ["Mark thread as expired" gnus-summary-expire-thread t]
2551         ["Lower thread score" gnus-summary-lower-thread t]
2552         ["Raise thread score" gnus-summary-raise-thread t]
2553         ["Rethread current" gnus-summary-rethread-current t]))
2554
2555     (easy-menu-define
2556       gnus-summary-post-menu gnus-summary-mode-map ""
2557       `("Post"
2558         ["Send a message (mail or news)" gnus-summary-post-news
2559          ,@(if (featurep 'xemacs) '(t)
2560              '(:help "Compose a new message (mail or news)"))]
2561         ["Followup" gnus-summary-followup
2562          ,@(if (featurep 'xemacs) '(t)
2563              '(:help "Post followup to this article"))]
2564         ["Followup and yank" gnus-summary-followup-with-original
2565          ,@(if (featurep 'xemacs) '(t)
2566              '(:help "Post followup to this article, quoting its contents"))]
2567         ["Supersede article" gnus-summary-supersede-article t]
2568         ["Cancel article" gnus-summary-cancel-article
2569          ,@(if (featurep 'xemacs) '(t)
2570              '(:help "Cancel an article you posted"))]
2571         ["Reply" gnus-summary-reply t]
2572         ["Reply and yank" gnus-summary-reply-with-original t]
2573         ["Wide reply" gnus-summary-wide-reply t]
2574         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2575          ,@(if (featurep 'xemacs) '(t)
2576              '(:help "Mail a reply, quoting this article"))]
2577         ["Very wide reply" gnus-summary-very-wide-reply t]
2578         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2579          ,@(if (featurep 'xemacs) '(t)
2580              '(:help "Mail a very wide reply, quoting this article"))]
2581         ["Mail forward" gnus-summary-mail-forward t]
2582         ["Post forward" gnus-summary-post-forward t]
2583         ["Digest and mail" gnus-uu-digest-mail-forward t]
2584         ["Digest and post" gnus-uu-digest-post-forward t]
2585         ["Resend message" gnus-summary-resend-message t]
2586         ["Resend message edit" gnus-summary-resend-message-edit t]
2587         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2588         ["Send a mail" gnus-summary-mail-other-window t]
2589         ["Create a local message" gnus-summary-news-other-window t]
2590         ["Uuencode and post" gnus-uu-post-news
2591          ,@(if (featurep 'xemacs) '(t)
2592              '(:help "Post a uuencoded article"))]
2593         ["Followup via news" gnus-summary-followup-to-mail t]
2594         ["Followup via news and yank"
2595          gnus-summary-followup-to-mail-with-original t]
2596         ["Strip signature on reply"
2597          (lambda ()
2598            (interactive)
2599            (if (not (memq message-cite-function
2600                           '(message-cite-original-without-signature
2601                             message-cite-original)))
2602                ;; Stupid workaround for XEmacs not honoring :visible.
2603                (message "Can't toggle this value of `message-cite-function'")
2604              (setq message-cite-function
2605                    (if (eq message-cite-function
2606                            'message-cite-original-without-signature)
2607                        'message-cite-original
2608                      'message-cite-original-without-signature))))
2609          ;; XEmacs barfs on :visible.
2610          ,@(if (featurep 'xemacs) nil
2611              '(:visible (memq message-cite-function
2612                               '(message-cite-original-without-signature
2613                                 message-cite-original))))
2614          :style toggle
2615          :selected (eq message-cite-function
2616                        'message-cite-original-without-signature)
2617          ,@(if (featurep 'xemacs) nil
2618              '(:help "Strip signature from cited article when replying."))]
2619         ;;("Draft"
2620         ;;["Send" gnus-summary-send-draft t]
2621         ;;["Send bounced" gnus-resend-bounced-mail t])
2622         ))
2623
2624     (cond
2625      ((not (keymapp gnus-summary-post-menu))
2626       (setq gnus-article-post-menu gnus-summary-post-menu))
2627      ((not gnus-article-post-menu)
2628       ;; Don't share post menu.
2629       (setq gnus-article-post-menu
2630             (copy-keymap gnus-summary-post-menu))))
2631     (define-key gnus-article-mode-map [menu-bar post]
2632       (cons "Post" gnus-article-post-menu))
2633
2634     (easy-menu-define
2635       gnus-summary-misc-menu gnus-summary-mode-map ""
2636       `("Gnus"
2637         ("Mark Read"
2638          ["Mark as read" gnus-summary-mark-as-read-forward t]
2639          ["Mark same subject and select"
2640           gnus-summary-kill-same-subject-and-select t]
2641          ["Mark same subject" gnus-summary-kill-same-subject t]
2642          ["Catchup" gnus-summary-catchup
2643           ,@(if (featurep 'xemacs) '(t)
2644               '(:help "Mark unread articles in this group as read"))]
2645          ["Catchup all" gnus-summary-catchup-all t]
2646          ["Catchup to here" gnus-summary-catchup-to-here t]
2647          ["Catchup from here" gnus-summary-catchup-from-here t]
2648          ["Catchup region" gnus-summary-mark-region-as-read
2649           (gnus-mark-active-p)]
2650          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2651         ("Mark Various"
2652          ["Tick" gnus-summary-tick-article-forward t]
2653          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2654          ["Remove marks" gnus-summary-clear-mark-forward t]
2655          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2656          ["Set bookmark" gnus-summary-set-bookmark t]
2657          ["Remove bookmark" gnus-summary-remove-bookmark t])
2658         ("Limit to"
2659          ["Marks..." gnus-summary-limit-to-marks t]
2660          ["Subject..." gnus-summary-limit-to-subject t]
2661          ["Author..." gnus-summary-limit-to-author t]
2662          ["Recipient..." gnus-summary-limit-to-recipient t]
2663          ["Address..." gnus-summary-limit-to-address t]
2664          ["Age..." gnus-summary-limit-to-age t]
2665          ["Extra..." gnus-summary-limit-to-extra t]
2666          ["Score..." gnus-summary-limit-to-score t]
2667          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2668          ["Unread" gnus-summary-limit-to-unread t]
2669          ["Unseen" gnus-summary-limit-to-unseen t]
2670          ["Singletons" gnus-summary-limit-to-singletons t]
2671          ["Replied" gnus-summary-limit-to-replied t]
2672          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2673          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2674          ["Pop limit" gnus-summary-pop-limit t]
2675          ["Show dormant" gnus-summary-limit-include-dormant t]
2676          ["Hide childless dormant"
2677           gnus-summary-limit-exclude-childless-dormant t]
2678          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2679          ["Hide marked" gnus-summary-limit-exclude-marks t]
2680          ["Show expunged" gnus-summary-limit-include-expunged t])
2681         ("Process Mark"
2682          ["Set mark" gnus-summary-mark-as-processable t]
2683          ["Remove mark" gnus-summary-unmark-as-processable t]
2684          ["Remove all marks" gnus-summary-unmark-all-processable t]
2685          ["Invert marks" gnus-uu-invert-processable t]
2686          ["Mark above" gnus-uu-mark-over t]
2687          ["Mark series" gnus-uu-mark-series t]
2688          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2689          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2690          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2691          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2692          ["Mark all" gnus-uu-mark-all t]
2693          ["Mark buffer" gnus-uu-mark-buffer t]
2694          ["Mark sparse" gnus-uu-mark-sparse t]
2695          ["Mark thread" gnus-uu-mark-thread t]
2696          ["Unmark thread" gnus-uu-unmark-thread t]
2697          ("Process Mark Sets"
2698           ["Kill" gnus-summary-kill-process-mark t]
2699           ["Yank" gnus-summary-yank-process-mark
2700            gnus-newsgroup-process-stack]
2701           ["Save" gnus-summary-save-process-mark t]
2702           ["Run command on marked..." gnus-summary-universal-argument t]))
2703         ("Registry Marks")
2704         ("Scroll article"
2705          ["Page forward" gnus-summary-next-page
2706           ,@(if (featurep 'xemacs) '(t)
2707               '(:help "Show next page of article"))]
2708          ["Page backward" gnus-summary-prev-page
2709           ,@(if (featurep 'xemacs) '(t)
2710               '(:help "Show previous page of article"))]
2711          ["Line forward" gnus-summary-scroll-up t])
2712         ("Move"
2713          ["Next unread article" gnus-summary-next-unread-article t]
2714          ["Previous unread article" gnus-summary-prev-unread-article t]
2715          ["Next article" gnus-summary-next-article t]
2716          ["Previous article" gnus-summary-prev-article t]
2717          ["Next unread subject" gnus-summary-next-unread-subject t]
2718          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2719          ["Next article same subject" gnus-summary-next-same-subject t]
2720          ["Previous article same subject" gnus-summary-prev-same-subject t]
2721          ["First unread article" gnus-summary-first-unread-article t]
2722          ["Best unread article" gnus-summary-best-unread-article t]
2723          ["Go to subject number..." gnus-summary-goto-subject t]
2724          ["Go to article number..." gnus-summary-goto-article t]
2725          ["Go to the last article" gnus-summary-goto-last-article t]
2726          ["Pop article off history" gnus-summary-pop-article t])
2727         ("Sort"
2728          ["Sort by number" gnus-summary-sort-by-number t]
2729          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2730          ["Sort by author" gnus-summary-sort-by-author t]
2731          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2732          ["Sort by subject" gnus-summary-sort-by-subject t]
2733          ["Sort by date" gnus-summary-sort-by-date t]
2734          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2735          ["Sort by score" gnus-summary-sort-by-score t]
2736          ["Sort by lines" gnus-summary-sort-by-lines t]
2737          ["Sort by characters" gnus-summary-sort-by-chars t]
2738          ["Randomize" gnus-summary-sort-by-random t]
2739          ["Original sort" gnus-summary-sort-by-original t])
2740         ("Help"
2741          ["Describe group" gnus-summary-describe-group t]
2742          ["Fetch control message" gnus-group-fetch-control
2743           ,@(if (featurep 'xemacs) nil
2744               '(:help "Display the archived control message for the current group"))]
2745          ["Read manual" gnus-info-find-node t])
2746         ("Modes"
2747          ["Pick and read" gnus-pick-mode t]
2748          ["Binary" gnus-binary-mode t])
2749         ("Regeneration"
2750          ["Regenerate" gnus-summary-prepare t]
2751          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2752          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2753          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2754          ["Toggle threading" gnus-summary-toggle-threads t])
2755         ["See old articles" gnus-summary-insert-old-articles t]
2756         ["See new articles" gnus-summary-insert-new-articles t]
2757         ["Filter articles..." gnus-summary-execute-command t]
2758         ["Run command on articles..." gnus-summary-universal-argument t]
2759         ["Search articles forward..." gnus-summary-search-article-forward t]
2760         ["Search articles backward..." gnus-summary-search-article-backward t]
2761         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2762         ["Expand window" gnus-summary-expand-window t]
2763         ["Expire expirable articles" gnus-summary-expire-articles
2764          (gnus-check-backend-function
2765           'request-expire-articles gnus-newsgroup-name)]
2766         ["Edit local kill file" gnus-summary-edit-local-kill t]
2767         ["Edit main kill file" gnus-summary-edit-global-kill t]
2768         ["Edit group parameters" gnus-summary-edit-parameters t]
2769         ["Customize group parameters" gnus-summary-customize-parameters t]
2770         ["Send a bug report" gnus-bug t]
2771         ("Exit"
2772          ["Catchup and exit" gnus-summary-catchup-and-exit
2773           ,@(if (featurep 'xemacs) '(t)
2774               '(:help "Mark unread articles in this group as read, then exit"))]
2775          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2776          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2777          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2778          ["Exit group" gnus-summary-exit
2779           ,@(if (featurep 'xemacs) '(t)
2780               '(:help "Exit current group, return to group selection mode"))]
2781          ["Exit group without updating" gnus-summary-exit-no-update t]
2782          ["Exit and goto next group" gnus-summary-next-group t]
2783          ["Exit and goto prev group" gnus-summary-prev-group t]
2784          ["Reselect group" gnus-summary-reselect-current-group t]
2785          ["Rescan group" gnus-summary-rescan-group t]
2786          ["Update dribble" gnus-summary-save-newsrc t])))
2787
2788     (gnus-run-hooks 'gnus-summary-menu-hook)))
2789
2790 (defvar gnus-summary-tool-bar-map nil)
2791
2792 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2793 ;; affect _new_ message buffers.  We might add a function that walks thru all
2794 ;; summary-mode buffers and force the update.
2795 (defun gnus-summary-tool-bar-update (&optional symbol value)
2796   "Update summary mode toolbar.
2797 Setter function for custom variables."
2798   (setq-default gnus-summary-tool-bar-map nil)
2799   (when symbol
2800     ;; When used as ":set" function:
2801     (set-default symbol value))
2802   (when (gnus-buffer-live-p gnus-summary-buffer)
2803     (with-current-buffer gnus-summary-buffer
2804       (gnus-summary-make-tool-bar))))
2805
2806 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2807                                      'gnus-summary-tool-bar-gnome
2808                                    'gnus-summary-tool-bar-retro)
2809   "Specifies the Gnus summary tool bar.
2810
2811 It can be either a list or a symbol refering to a list.  See
2812 `gmm-tool-bar-from-list' for the format of the list.  The
2813 default key map is `gnus-summary-mode-map'.
2814
2815 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2816 `gnus-summary-tool-bar-retro'."
2817   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2818                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2819                  (repeat :tag "User defined list" gmm-tool-bar-item)
2820                  (symbol))
2821   :version "23.1" ;; No Gnus
2822   :initialize 'custom-initialize-default
2823   :set 'gnus-summary-tool-bar-update
2824   :group 'gnus-summary)
2825
2826 (defcustom gnus-summary-tool-bar-gnome
2827   '((gnus-summary-post-news "mail/compose" nil)
2828     (gnus-summary-insert-new-articles "mail/inbox" nil
2829                                       :visible (or (not gnus-agent)
2830                                                    gnus-plugged))
2831     (gnus-summary-reply-with-original "mail/reply")
2832     (gnus-summary-reply "mail/reply" nil :visible nil)
2833     (gnus-summary-followup-with-original "mail/reply-all")
2834     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2835     (gnus-summary-mail-forward "mail/forward")
2836     (gnus-summary-save-article "mail/save")
2837     (gnus-summary-search-article-forward "search" nil :visible nil)
2838     (gnus-summary-print-article "print")
2839     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2840     ;; Some new commands that may need more suitable icons:
2841     (gnus-summary-save-newsrc "save" nil :visible nil)
2842     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2843     (gnus-summary-prev-article "left-arrow")
2844     (gnus-summary-next-article "right-arrow")
2845     (gnus-summary-next-page "next-page")
2846     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2847     ;;
2848     ;; Maybe some sort-by-... could be added:
2849     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2850     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2851     (gnus-summary-mark-as-expirable
2852      "delete" nil
2853      :visible (gnus-check-backend-function 'request-expire-articles
2854                                            gnus-newsgroup-name))
2855     (gnus-summary-mark-as-spam
2856      "mail/spam" t
2857      :visible (and (fboundp 'spam-group-ham-contents-p)
2858                    (spam-group-ham-contents-p gnus-newsgroup-name))
2859      :help "Mark as spam")
2860     (gnus-summary-mark-as-read-forward
2861      "mail/not-spam" nil
2862      :visible (and (fboundp 'spam-group-spam-contents-p)
2863                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2864     ;;
2865     (gnus-summary-exit "exit")
2866     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2867     (gnus-info-find-node "help"))
2868   "List of functions for the summary tool bar (GNOME style).
2869
2870 See `gmm-tool-bar-from-list' for the format of the list."
2871   :type '(repeat gmm-tool-bar-item)
2872   :version "23.1" ;; No Gnus
2873   :initialize 'custom-initialize-default
2874   :set 'gnus-summary-tool-bar-update
2875   :group 'gnus-summary)
2876
2877 (defcustom gnus-summary-tool-bar-retro
2878   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2879     (gnus-summary-next-unread-article "gnus/next-ur")
2880     (gnus-summary-post-news "gnus/post")
2881     (gnus-summary-followup-with-original "gnus/fuwo")
2882     (gnus-summary-followup "gnus/followup")
2883     (gnus-summary-reply-with-original "gnus/reply-wo")
2884     (gnus-summary-reply "gnus/reply")
2885     (gnus-summary-caesar-message "gnus/rot13")
2886     (gnus-uu-decode-uu "gnus/uu-decode")
2887     (gnus-summary-save-article-file "gnus/save-aif")
2888     (gnus-summary-save-article "gnus/save-art")
2889     (gnus-uu-post-news "gnus/uu-post")
2890     (gnus-summary-catchup "gnus/catchup")
2891     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2892     (gnus-summary-exit "gnus/exit-summ")
2893     ;; Some new command that may need more suitable icons:
2894     (gnus-summary-print-article "gnus/print" nil :visible nil)
2895     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2896     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2897     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2898     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2899     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2900     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2901     ;;
2902     (gnus-info-find-node "gnus/help" nil :visible nil))
2903   "List of functions for the summary tool bar (retro look).
2904
2905 See `gmm-tool-bar-from-list' for the format of the list."
2906   :type '(repeat gmm-tool-bar-item)
2907   :version "23.1" ;; No Gnus
2908   :initialize 'custom-initialize-default
2909   :set 'gnus-summary-tool-bar-update
2910   :group 'gnus-summary)
2911
2912 (defcustom gnus-summary-tool-bar-zap-list t
2913   "List of icon items from the global tool bar.
2914 These items are not displayed in the Gnus summary mode tool bar.
2915
2916 See `gmm-tool-bar-from-list' for the format of the list."
2917   :type 'gmm-tool-bar-zap-list
2918   :version "23.1" ;; No Gnus
2919   :initialize 'custom-initialize-default
2920   :set 'gnus-summary-tool-bar-update
2921   :group 'gnus-summary)
2922
2923 (defvar image-load-path)
2924 (defvar tool-bar-map)
2925
2926 (defun gnus-summary-make-tool-bar (&optional force)
2927   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2928 When FORCE, rebuild the tool bar."
2929   (when (and (not (featurep 'xemacs))
2930              (boundp 'tool-bar-mode)
2931              tool-bar-mode
2932              (or (not gnus-summary-tool-bar-map) force))
2933     (let* ((load-path
2934             (gmm-image-load-path-for-library "gnus"
2935                                              "mail/save.xpm"
2936                                              nil t))
2937            (image-load-path (cons (car load-path)
2938                                   (when (boundp 'image-load-path)
2939                                     image-load-path)))
2940            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2941                                         gnus-summary-tool-bar-zap-list
2942                                         'gnus-summary-mode-map)))
2943       (when map
2944         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2945         ;; uses it's value.
2946         (setq gnus-summary-tool-bar-map map))))
2947   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2948
2949 (defun gnus-score-set-default (var value)
2950   "A version of set that updates the GNU Emacs menu-bar."
2951   (set var value)
2952   ;; It is the message that forces the active status to be updated.
2953   (message ""))
2954
2955 (defun gnus-make-score-map (type)
2956   "Make a summary score map of type TYPE."
2957   (if t
2958       nil
2959     (let ((headers '(("author" "from" string)
2960                      ("subject" "subject" string)
2961                      ("article body" "body" string)
2962                      ("article head" "head" string)
2963                      ("xref" "xref" string)
2964                      ("extra header" "extra" string)
2965                      ("lines" "lines" number)
2966                      ("followups to author" "followup" string)))
2967           (types '((number ("less than" <)
2968                            ("greater than" >)
2969                            ("equal" =))
2970                    (string ("substring" s)
2971                            ("exact string" e)
2972                            ("fuzzy string" f)
2973                            ("regexp" r))))
2974           (perms '(("temporary" (current-time-string))
2975                    ("permanent" nil)
2976                    ("immediate" now)))
2977           header)
2978       (list
2979        (apply
2980         'nconc
2981         (list
2982          (if (eq type 'lower)
2983              "Lower score"
2984            "Increase score"))
2985         (let (outh)
2986           (while headers
2987             (setq header (car headers))
2988             (setq outh
2989                   (cons
2990                    (apply
2991                     'nconc
2992                     (list (car header))
2993                     (let ((ts (cdr (assoc (nth 2 header) types)))
2994                           outt)
2995                       (while ts
2996                         (setq outt
2997                               (cons
2998                                (apply
2999                                 'nconc
3000                                 (list (caar ts))
3001                                 (let ((ps perms)
3002                                       outp)
3003                                   (while ps
3004                                     (setq outp
3005                                           (cons
3006                                            (vector
3007                                             (caar ps)
3008                                             (list
3009                                              'gnus-summary-score-entry
3010                                              (nth 1 header)
3011                                              (if (or (string= (nth 1 header)
3012                                                               "head")
3013                                                      (string= (nth 1 header)
3014                                                               "body"))
3015                                                  ""
3016                                                (list 'gnus-summary-header
3017                                                      (nth 1 header)))
3018                                              (list 'quote (nth 1 (car ts)))
3019                                              (list 'gnus-score-delta-default
3020                                                    nil)
3021                                              (nth 1 (car ps))
3022                                              t)
3023                                             t)
3024                                            outp))
3025                                     (setq ps (cdr ps)))
3026                                   (list (nreverse outp))))
3027                                outt))
3028                         (setq ts (cdr ts)))
3029                       (list (nreverse outt))))
3030                    outh))
3031             (setq headers (cdr headers)))
3032           (list (nreverse outh))))))))
3033
3034
3035 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3036 (defvar bookmark-make-record-function)
3037 \f
3038
3039 (defun gnus-summary-mode (&optional group)
3040   "Major mode for reading articles.
3041
3042 All normal editing commands are switched off.
3043 \\<gnus-summary-mode-map>
3044 Each line in this buffer represents one article.  To read an
3045 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3046 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3047 respectively.
3048
3049 You can also post articles and send mail from this buffer.  To
3050 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3051 of an article, type `\\[gnus-summary-reply]'.
3052
3053 There are approx. one gazillion commands you can execute in this
3054 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3055
3056 The following commands are available:
3057
3058 \\{gnus-summary-mode-map}"
3059   (interactive)
3060   (kill-all-local-variables)
3061   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3062     (gnus-summary-make-local-variables))
3063   (gnus-summary-make-local-variables)
3064   (setq gnus-newsgroup-name group)
3065   (when (gnus-visual-p 'summary-menu 'menu)
3066     (gnus-summary-make-menu-bar)
3067     (gnus-summary-make-tool-bar))
3068   (gnus-make-thread-indent-array)
3069   (gnus-simplify-mode-line)
3070   (setq major-mode 'gnus-summary-mode)
3071   (setq mode-name "Summary")
3072   (use-local-map gnus-summary-mode-map)
3073   (buffer-disable-undo)
3074   (setq buffer-read-only t              ;Disable modification
3075         show-trailing-whitespace nil)
3076   (setq truncate-lines t)
3077   (add-to-invisibility-spec '(gnus-sum . t))
3078   (gnus-summary-set-display-table)
3079   (gnus-set-default-directory)
3080   (make-local-variable 'gnus-summary-line-format)
3081   (make-local-variable 'gnus-summary-line-format-spec)
3082   (make-local-variable 'gnus-summary-dummy-line-format)
3083   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3084   (make-local-variable 'gnus-summary-mark-positions)
3085   (gnus-make-local-hook 'pre-command-hook)
3086   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3087   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3088   (turn-on-gnus-mailing-list-mode)
3089   (mm-enable-multibyte)
3090   (set (make-local-variable 'bookmark-make-record-function)
3091        'gnus-summary-bookmark-make-record)
3092   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3093   (gnus-update-summary-mark-positions))
3094
3095 (defun gnus-summary-make-local-variables ()
3096   "Make all the local summary buffer variables."
3097   (let (global)
3098     (dolist (local gnus-summary-local-variables)
3099       (if (consp local)
3100           (progn
3101             (if (eq (cdr local) 'global)
3102                 ;; Copy the global value of the variable.
3103                 (setq global (symbol-value (car local)))
3104               ;; Use the value from the list.
3105               (setq global (eval (cdr local))))
3106             (set (make-local-variable (car local)) global))
3107         ;; Simple nil-valued local variable.
3108         (set (make-local-variable local) nil)))))
3109
3110 (defun gnus-summary-clear-local-variables ()
3111   (let ((locals gnus-summary-local-variables))
3112     (while locals
3113       (if (consp (car locals))
3114           (and (symbolp (caar locals))
3115                (set (caar locals) nil))
3116         (and (symbolp (car locals))
3117              (set (car locals) nil)))
3118       (setq locals (cdr locals)))))
3119
3120 ;; Summary data functions.
3121
3122 (defmacro gnus-data-number (data)
3123   `(car ,data))
3124
3125 (defmacro gnus-data-set-number (data number)
3126   `(setcar ,data ,number))
3127
3128 (defmacro gnus-data-mark (data)
3129   `(nth 1 ,data))
3130
3131 (defmacro gnus-data-set-mark (data mark)
3132   `(setcar (nthcdr 1 ,data) ,mark))
3133
3134 (defmacro gnus-data-pos (data)
3135   `(nth 2 ,data))
3136
3137 (defmacro gnus-data-set-pos (data pos)
3138   `(setcar (nthcdr 2 ,data) ,pos))
3139
3140 (defmacro gnus-data-header (data)
3141   `(nth 3 ,data))
3142
3143 (defmacro gnus-data-set-header (data header)
3144   `(setf (nth 3 ,data) ,header))
3145
3146 (defmacro gnus-data-level (data)
3147   `(nth 4 ,data))
3148
3149 (defmacro gnus-data-unread-p (data)
3150   `(= (nth 1 ,data) gnus-unread-mark))
3151
3152 (defmacro gnus-data-read-p (data)
3153   `(/= (nth 1 ,data) gnus-unread-mark))
3154
3155 (defmacro gnus-data-pseudo-p (data)
3156   `(consp (nth 3 ,data)))
3157
3158 (defmacro gnus-data-find (number)
3159   `(assq ,number gnus-newsgroup-data))
3160
3161 (defmacro gnus-data-find-list (number &optional data)
3162   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3163      (memq (assq ,number bdata)
3164            bdata)))
3165
3166 (defmacro gnus-data-make (number mark pos header level)
3167   `(list ,number ,mark ,pos ,header ,level))
3168
3169 (defun gnus-data-enter (after-article number mark pos header level offset)
3170   (let ((data (gnus-data-find-list after-article)))
3171     (unless data
3172       (error "No such article: %d" after-article))
3173     (setcdr data (cons (gnus-data-make number mark pos header level)
3174                        (cdr data)))
3175     (setq gnus-newsgroup-data-reverse nil)
3176     (gnus-data-update-list (cddr data) offset)))
3177
3178 (defun gnus-data-enter-list (after-article list &optional offset)
3179   (when list
3180     (let ((data (and after-article (gnus-data-find-list after-article)))
3181           (ilist list))
3182       (if (not (or data
3183                    after-article))
3184           (let ((odata gnus-newsgroup-data))
3185             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3186             (when offset
3187               (gnus-data-update-list odata offset)))
3188         ;; Find the last element in the list to be spliced into the main
3189         ;; list.
3190         (setq list (last list))
3191         (if (not data)
3192             (progn
3193               (setcdr list gnus-newsgroup-data)
3194               (setq gnus-newsgroup-data ilist)
3195               (when offset
3196                 (gnus-data-update-list (cdr list) offset)))
3197           (setcdr list (cdr data))
3198           (setcdr data ilist)
3199           (when offset
3200             (gnus-data-update-list (cdr list) offset))))
3201       (setq gnus-newsgroup-data-reverse nil))))
3202
3203 (defun gnus-data-remove (article &optional offset)
3204   (let ((data gnus-newsgroup-data))
3205     (if (= (gnus-data-number (car data)) article)
3206         (progn
3207           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3208                 gnus-newsgroup-data-reverse nil)
3209           (when offset
3210             (gnus-data-update-list gnus-newsgroup-data offset)))
3211       (while (cdr data)
3212         (when (= (gnus-data-number (cadr data)) article)
3213           (setcdr data (cddr data))
3214           (when offset
3215             (gnus-data-update-list (cdr data) offset))
3216           (setq data nil
3217                 gnus-newsgroup-data-reverse nil))
3218         (setq data (cdr data))))))
3219
3220 (defmacro gnus-data-list (backward)
3221   `(if ,backward
3222        (or gnus-newsgroup-data-reverse
3223            (setq gnus-newsgroup-data-reverse
3224                  (reverse gnus-newsgroup-data)))
3225      gnus-newsgroup-data))
3226
3227 (defun gnus-data-update-list (data offset)
3228   "Add OFFSET to the POS of all data entries in DATA."
3229   (setq gnus-newsgroup-data-reverse nil)
3230   (while data
3231     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3232     (setq data (cdr data))))
3233
3234 (defun gnus-summary-article-pseudo-p (article)
3235   "Say whether this article is a pseudo article or not."
3236   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3237
3238 (defmacro gnus-summary-article-sparse-p (article)
3239   "Say whether this article is a sparse article or not."
3240   `(memq ,article gnus-newsgroup-sparse))
3241
3242 (defmacro gnus-summary-article-ancient-p (article)
3243   "Say whether this article is a sparse article or not."
3244   `(memq ,article gnus-newsgroup-ancient))
3245
3246 (defun gnus-article-parent-p (number)
3247   "Say whether this article is a parent or not."
3248   (let ((data (gnus-data-find-list number)))
3249     (and (cdr data)              ; There has to be an article after...
3250          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3251             (gnus-data-level (nth 1 data))))))
3252
3253 (defun gnus-article-children (number)
3254   "Return a list of all children to NUMBER."
3255   (let* ((data (gnus-data-find-list number))
3256          (level (gnus-data-level (car data)))
3257          children)
3258     (setq data (cdr data))
3259     (while (and data
3260                 (= (gnus-data-level (car data)) (1+ level)))
3261       (push (gnus-data-number (car data)) children)
3262       (setq data (cdr data)))
3263     children))
3264
3265 (defmacro gnus-summary-skip-intangible ()
3266   "If the current article is intangible, then jump to a different article."
3267   '(let ((to (get-text-property (point) 'gnus-intangible)))
3268      (and to (gnus-summary-goto-subject to))))
3269
3270 (defmacro gnus-summary-article-intangible-p ()
3271   "Say whether this article is intangible or not."
3272   '(get-text-property (point) 'gnus-intangible))
3273
3274 (defun gnus-article-read-p (article)
3275   "Say whether ARTICLE is read or not."
3276   (not (or (memq article gnus-newsgroup-marked)
3277            (memq article gnus-newsgroup-spam-marked)
3278            (memq article gnus-newsgroup-unreads)
3279            (memq article gnus-newsgroup-unselected)
3280            (memq article gnus-newsgroup-dormant))))
3281
3282 ;; Some summary mode macros.
3283
3284 (defmacro gnus-summary-article-number ()
3285   "The article number of the article on the current line.
3286 If there isn't an article number here, then we return the current
3287 article number."
3288   '(progn
3289      (gnus-summary-skip-intangible)
3290      (or (get-text-property (point) 'gnus-number)
3291          (gnus-summary-last-subject))))
3292
3293 (defmacro gnus-summary-article-header (&optional number)
3294   "Return the header of article NUMBER."
3295   `(gnus-data-header (gnus-data-find
3296                       ,(or number '(gnus-summary-article-number)))))
3297
3298 (defmacro gnus-summary-thread-level (&optional number)
3299   "Return the level of thread that starts with article NUMBER."
3300   `(if (and (eq gnus-summary-make-false-root 'dummy)
3301             (get-text-property (point) 'gnus-intangible))
3302        0
3303      (gnus-data-level (gnus-data-find
3304                        ,(or number '(gnus-summary-article-number))))))
3305
3306 (defmacro gnus-summary-article-mark (&optional number)
3307   "Return the mark of article NUMBER."
3308   `(gnus-data-mark (gnus-data-find
3309                     ,(or number '(gnus-summary-article-number)))))
3310
3311 (defmacro gnus-summary-article-pos (&optional number)
3312   "Return the position of the line of article NUMBER."
3313   `(gnus-data-pos (gnus-data-find
3314                    ,(or number '(gnus-summary-article-number)))))
3315
3316 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3317 (defmacro gnus-summary-article-subject (&optional number)
3318   "Return current subject string or nil if nothing."
3319   `(let ((headers
3320           ,(if number
3321                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3322              '(gnus-data-header (assq (gnus-summary-article-number)
3323                                       gnus-newsgroup-data)))))
3324      (and headers
3325           (vectorp headers)
3326           (mail-header-subject headers))))
3327
3328 (defmacro gnus-summary-article-score (&optional number)
3329   "Return current article score."
3330   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3331                   gnus-newsgroup-scored))
3332        gnus-summary-default-score 0))
3333
3334 (defun gnus-summary-article-children (&optional number)
3335   "Return a list of article numbers that are children of article NUMBER."
3336   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3337          (level (gnus-data-level (car data)))
3338          l children)
3339     (while (and (setq data (cdr data))
3340                 (> (setq l (gnus-data-level (car data))) level))
3341       (and (= (1+ level) l)
3342            (push (gnus-data-number (car data))
3343                  children)))
3344     (nreverse children)))
3345
3346 (defun gnus-summary-article-parent (&optional number)
3347   "Return the article number of the parent of article NUMBER."
3348   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3349                                     (gnus-data-list t)))
3350          (level (gnus-data-level (car data))))
3351     (if (zerop level)
3352         ()                              ; This is a root.
3353       ;; We search until we find an article with a level less than
3354       ;; this one.  That function has to be the parent.
3355       (while (and (setq data (cdr data))
3356                   (not (< (gnus-data-level (car data)) level))))
3357       (and data (gnus-data-number (car data))))))
3358
3359 (defun gnus-unread-mark-p (mark)
3360   "Say whether MARK is the unread mark."
3361   (= mark gnus-unread-mark))
3362
3363 (defun gnus-read-mark-p (mark)
3364   "Say whether MARK is one of the marks that mark as read.
3365 This is all marks except unread, ticked, dormant, and expirable."
3366   (not (or (= mark gnus-unread-mark)
3367            (= mark gnus-ticked-mark)
3368            (= mark gnus-spam-mark)
3369            (= mark gnus-dormant-mark)
3370            (= mark gnus-expirable-mark))))
3371
3372 (defmacro gnus-article-mark (number)
3373   "Return the MARK of article NUMBER.
3374 This macro should only be used when computing the mark the \"first\"
3375 time; i.e., when generating the summary lines.  After that,
3376 `gnus-summary-article-mark' should be used to examine the
3377 marks of articles."
3378   `(cond
3379     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3380     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3381     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3382     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3383     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3384     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3385     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3386     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3387            gnus-ancient-mark))))
3388
3389 ;; Saving hidden threads.
3390
3391 (defmacro gnus-save-hidden-threads (&rest forms)
3392   "Save hidden threads, eval FORMS, and restore the hidden threads."
3393   (let ((config (make-symbol "config")))
3394     `(let ((,config (gnus-hidden-threads-configuration)))
3395        (unwind-protect
3396            (save-excursion
3397              ,@forms)
3398          (gnus-restore-hidden-threads-configuration ,config)))))
3399 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3400 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3401
3402 (defun gnus-data-compute-positions ()
3403   "Compute the positions of all articles."
3404   (setq gnus-newsgroup-data-reverse nil)
3405   (let ((data gnus-newsgroup-data))
3406     (save-excursion
3407       (gnus-save-hidden-threads
3408         (gnus-summary-show-all-threads)
3409         (goto-char (point-min))
3410         (while data
3411           (while (get-text-property (point) 'gnus-intangible)
3412             (forward-line 1))
3413           (gnus-data-set-pos (car data) (+ (point) 3))
3414           (setq data (cdr data))
3415           (forward-line 1))))))
3416
3417 (defun gnus-hidden-threads-configuration ()
3418   "Return the current hidden threads configuration."
3419   (save-excursion
3420     (let (config)
3421       (goto-char (point-min))
3422       (while (not (eobp))
3423         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3424           (push (save-excursion (forward-line 0) (point)) config))
3425         (forward-line 1))
3426       config)))
3427
3428 (defun gnus-restore-hidden-threads-configuration (config)
3429   "Restore hidden threads configuration from CONFIG."
3430   (save-excursion
3431     (let (point (inhibit-read-only t))
3432       (while (setq point (pop config))
3433         (goto-char point)
3434         (gnus-summary-hide-thread)))))
3435
3436 ;; Various summary mode internalish functions.
3437
3438 (defun gnus-mouse-pick-article (e)
3439   (interactive "e")
3440   (mouse-set-point e)
3441   (gnus-summary-next-page nil t))
3442
3443 (defun gnus-summary-set-display-table ()
3444   "Change the display table.
3445 Odd characters have a tendency to mess
3446 up nicely formatted displays - we make all possible glyphs
3447 display only a single character."
3448
3449   ;; We start from the standard display table, if any.
3450   (let ((table (or (copy-sequence standard-display-table)
3451                    (make-display-table)))
3452         (i 32))
3453     ;; Nix out all the control chars...
3454     (while (>= (setq i (1- i)) 0)
3455       (gnus-put-display-table i [??] table))
3456    ;; ... but not newline and cr, of course.  (cr is necessary for the
3457     ;; selective display).
3458     (gnus-put-display-table ?\n nil table)
3459     (gnus-put-display-table ?\r nil table)
3460     ;; We keep TAB as well.
3461     (gnus-put-display-table ?\t nil table)
3462     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3463     ;; Emacs 23 (unicode), that are not set already.
3464     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3465                  160
3466                256)))
3467       (while (>= (setq i (1- i)) 127)
3468         ;; Only modify if the entry is nil.
3469         (unless (gnus-get-display-table i table)
3470           (gnus-put-display-table i [??] table))))
3471     (setq buffer-display-table table)))
3472
3473 (defun gnus-summary-set-article-display-arrow (pos)
3474   "Update the overlay arrow to point to line at position POS."
3475   (when gnus-summary-display-arrow
3476     (make-local-variable 'overlay-arrow-position)
3477     (make-local-variable 'overlay-arrow-string)
3478     (save-excursion
3479       (goto-char pos)
3480       (beginning-of-line)
3481       (unless overlay-arrow-position
3482         (setq overlay-arrow-position (make-marker)))
3483       (setq overlay-arrow-string "=>"
3484             overlay-arrow-position (set-marker overlay-arrow-position
3485                                                (point)
3486                                                (current-buffer))))))
3487
3488 (defun gnus-summary-setup-buffer (group)
3489   "Initialize summary buffer."
3490   (let ((buffer (gnus-summary-buffer-name group))
3491         (dead-name (concat "*Dead Summary "
3492                            (gnus-group-decoded-name group) "*")))
3493     ;; If a dead summary buffer exists, we kill it.
3494     (when (gnus-buffer-live-p dead-name)
3495       (gnus-kill-buffer dead-name))
3496     (if (get-buffer buffer)
3497         (progn
3498           (set-buffer buffer)
3499           (setq gnus-summary-buffer (current-buffer))
3500           (not gnus-newsgroup-prepared))
3501       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3502       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3503       (gnus-summary-mode group)
3504       (when gnus-carpal
3505         (gnus-carpal-setup-buffer 'summary))
3506       (when (gnus-group-quit-config group)
3507         (set (make-local-variable 'gnus-single-article-buffer) nil))
3508       (make-local-variable 'gnus-article-buffer)
3509       (make-local-variable 'gnus-article-current)
3510       (make-local-variable 'gnus-original-article-buffer)
3511       (setq gnus-newsgroup-name group)
3512       ;; Set any local variables in the group parameters.
3513       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3514       t)))
3515
3516 (defun gnus-set-global-variables ()
3517   "Set the global equivalents of the buffer-local variables.
3518 They are set to the latest values they had.  These reflect the summary
3519 buffer that was in action when the last article was fetched."
3520   (when (eq major-mode 'gnus-summary-mode)
3521     (setq gnus-summary-buffer (current-buffer))
3522     (let ((name gnus-newsgroup-name)
3523           (marked gnus-newsgroup-marked)
3524           (spam gnus-newsgroup-spam-marked)
3525           (unread gnus-newsgroup-unreads)
3526           (headers gnus-current-headers)
3527           (data gnus-newsgroup-data)
3528           (summary gnus-summary-buffer)
3529           (article-buffer gnus-article-buffer)
3530           (original gnus-original-article-buffer)
3531           (gac gnus-article-current)
3532           (reffed gnus-reffed-article-number)
3533           (score-file gnus-current-score-file)
3534           (default-charset gnus-newsgroup-charset)
3535           vlist)
3536       (let ((locals gnus-newsgroup-variables))
3537         (while locals
3538           (if (consp (car locals))
3539               (push (eval (caar locals)) vlist)
3540             (push (eval (car locals)) vlist))
3541           (setq locals (cdr locals)))
3542         (setq vlist (nreverse vlist)))
3543       (with-current-buffer gnus-group-buffer
3544         (setq gnus-newsgroup-name name
3545               gnus-newsgroup-marked marked
3546               gnus-newsgroup-spam-marked spam
3547               gnus-newsgroup-unreads unread
3548               gnus-current-headers headers
3549               gnus-newsgroup-data data
3550               gnus-article-current gac
3551               gnus-summary-buffer summary
3552               gnus-article-buffer article-buffer
3553               gnus-original-article-buffer original
3554               gnus-reffed-article-number reffed
3555               gnus-current-score-file score-file
3556               gnus-newsgroup-charset default-charset)
3557         (let ((locals gnus-newsgroup-variables))
3558           (while locals
3559             (if (consp (car locals))
3560                 (set (caar locals) (pop vlist))
3561               (set (car locals) (pop vlist)))
3562             (setq locals (cdr locals))))
3563         ;; The article buffer also has local variables.
3564         (when (gnus-buffer-live-p gnus-article-buffer)
3565           (set-buffer gnus-article-buffer)
3566           (setq gnus-summary-buffer summary))))))
3567
3568 (defun gnus-summary-article-unread-p (article)
3569   "Say whether ARTICLE is unread or not."
3570   (memq article gnus-newsgroup-unreads))
3571
3572 (defun gnus-summary-first-article-p (&optional article)
3573   "Return whether ARTICLE is the first article in the buffer."
3574   (if (not (setq article (or article (gnus-summary-article-number))))
3575       nil
3576     (eq article (caar gnus-newsgroup-data))))
3577
3578 (defun gnus-summary-last-article-p (&optional article)
3579   "Return whether ARTICLE is the last article in the buffer."
3580   (if (not (setq article (or article (gnus-summary-article-number))))
3581       ;; All non-existent numbers are the last article.  :-)
3582       t
3583     (not (cdr (gnus-data-find-list article)))))
3584
3585 (defun gnus-make-thread-indent-array (&optional n)
3586   (when (or n
3587             (progn (setq n 200) nil)
3588             (null gnus-thread-indent-array)
3589             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3590     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3591           gnus-thread-indent-array-level gnus-thread-indent-level)
3592     (while (>= n 0)
3593       (aset gnus-thread-indent-array n
3594             (make-string (* n gnus-thread-indent-level) ? ))
3595       (setq n (1- n)))))
3596
3597 (defun gnus-update-summary-mark-positions ()
3598   "Compute where the summary marks are to go."
3599   (save-excursion
3600     (when (gnus-buffer-exists-p gnus-summary-buffer)
3601       (set-buffer gnus-summary-buffer))
3602     (let ((spec gnus-summary-line-format-spec)
3603           pos)
3604       (save-excursion
3605         (gnus-set-work-buffer)
3606         (let ((gnus-tmp-unread ?Z)
3607               (gnus-replied-mark ?Z)
3608               (gnus-score-below-mark ?Z)
3609               (gnus-score-over-mark ?Z)
3610               (gnus-undownloaded-mark ?Z)
3611               (gnus-summary-line-format-spec spec)
3612               (gnus-newsgroup-downloadable '(0))
3613               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3614               case-fold-search ignores)
3615           ;; Here, all marks are bound to Z.
3616           (gnus-summary-insert-line header
3617                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3618           (goto-char (point-min))
3619           ;; Memorize the positions of the same characters as dummy marks.
3620           (while (re-search-forward "[A-D]" nil t)
3621             (push (point) ignores))
3622           (erase-buffer)
3623           ;; We use A-D as dummy marks in order to know column positions
3624           ;; where marks should be inserted.
3625           (setq gnus-tmp-unread ?A
3626                 gnus-replied-mark ?B
3627                 gnus-score-below-mark ?C
3628                 gnus-score-over-mark ?C
3629                 gnus-undownloaded-mark ?D)
3630           (gnus-summary-insert-line header
3631                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3632           ;; Ignore characters which aren't dummy marks.
3633           (dolist (p ignores)
3634             (delete-region (goto-char (1- p)) p)
3635             (insert ?Z))
3636           (goto-char (point-min))
3637           (setq pos (list (cons 'unread
3638                                 (and (search-forward "A" nil t)
3639                                      (- (point) (point-min) 1)))))
3640           (goto-char (point-min))
3641           (push (cons 'replied (and (search-forward "B" nil t)
3642                                     (- (point) (point-min) 1)))
3643                 pos)
3644           (goto-char (point-min))
3645           (push (cons 'score (and (search-forward "C" nil t)
3646                                   (- (point) (point-min) 1)))
3647                 pos)
3648           (goto-char (point-min))
3649           (push (cons 'download (and (search-forward "D" nil t)
3650                                      (- (point) (point-min) 1)))
3651                 pos)))
3652       (setq gnus-summary-mark-positions pos))))
3653
3654 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3655   "Insert a dummy root in the summary buffer."
3656   (beginning-of-line)
3657   (gnus-add-text-properties
3658    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3659    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3660
3661 (defun gnus-summary-extract-address-component (from)
3662   (or (car (funcall gnus-extract-address-components from))
3663       from))
3664
3665 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3666   (let ((mail-parse-charset gnus-newsgroup-charset)
3667         (ignored-from-addresses (gnus-ignored-from-addresses))
3668         ; Is it really necessary to do this next part for each summary line?
3669         ; Luckily, doesn't seem to slow things down much.
3670         (mail-parse-ignored-charsets
3671          (with-current-buffer gnus-summary-buffer
3672            gnus-newsgroup-ignored-charsets)))
3673     (or
3674      (and ignored-from-addresses
3675           (string-match ignored-from-addresses gnus-tmp-from)
3676           (let ((extra-headers (mail-header-extra header))
3677                 to
3678                 newsgroups)
3679             (cond
3680              ((setq to (cdr (assq 'To extra-headers)))
3681               (concat gnus-summary-to-prefix
3682                       (inline
3683                         (gnus-summary-extract-address-component
3684                          (funcall gnus-decode-encoded-address-function to)))))
3685              ((setq newsgroups
3686                     (or
3687                      (cdr (assq 'Newsgroups extra-headers))
3688                      (and
3689                       (memq 'Newsgroups gnus-extra-headers)
3690                       (eq (car (gnus-find-method-for-group
3691                                 gnus-newsgroup-name)) 'nntp)
3692                       (gnus-group-real-name gnus-newsgroup-name))))
3693               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3694      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3695
3696 (defun gnus-summary-insert-line (gnus-tmp-header
3697                                  gnus-tmp-level gnus-tmp-current
3698                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3699                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3700                                  &optional gnus-tmp-dummy gnus-tmp-score
3701                                  gnus-tmp-process)
3702   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3703       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3704                                           gnus-tmp-level)))
3705   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3706          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3707          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3708          (gnus-tmp-score-char
3709           (if (or (null gnus-summary-default-score)
3710                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3711                       gnus-summary-zcore-fuzz))
3712               ?                         ;Whitespace
3713             (if (< gnus-tmp-score gnus-summary-default-score)
3714                 gnus-score-below-mark gnus-score-over-mark)))
3715          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3716          (gnus-tmp-replied
3717           (cond (gnus-tmp-process gnus-process-mark)
3718                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3719                  gnus-cached-mark)
3720                 (gnus-tmp-replied gnus-replied-mark)
3721                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3722                  gnus-forwarded-mark)
3723                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3724                  gnus-saved-mark)
3725                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3726                  gnus-recent-mark)
3727                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3728                  gnus-unseen-mark)
3729                 (t gnus-no-mark)))
3730          (gnus-tmp-downloaded
3731           (cond (undownloaded
3732                  gnus-undownloaded-mark)
3733                 (gnus-newsgroup-agentized
3734                  gnus-downloaded-mark)
3735                 (t
3736                  gnus-no-mark)))
3737          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3738          (gnus-tmp-name
3739           (cond
3740            ((string-match "<[^>]+> *$" gnus-tmp-from)
3741             (let ((beg (match-beginning 0)))
3742               (or (and (string-match "^\".+\"" gnus-tmp-from)
3743                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3744                   (substring gnus-tmp-from 0 beg))))
3745            ((string-match "(.+)" gnus-tmp-from)
3746             (substring gnus-tmp-from
3747                        (1+ (match-beginning 0)) (1- (match-end 0))))
3748            (t gnus-tmp-from)))
3749          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3750          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3751          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3752          (inhibit-read-only t))
3753     (when (string= gnus-tmp-name "")
3754       (setq gnus-tmp-name gnus-tmp-from))
3755     (unless (numberp gnus-tmp-lines)
3756       (setq gnus-tmp-lines -1))
3757     (if (= gnus-tmp-lines -1)
3758         (setq gnus-tmp-lines "?")
3759       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3760     (condition-case ()
3761         (gnus-put-text-property
3762          (point)
3763          (progn (eval gnus-summary-line-format-spec) (point))
3764          'gnus-number gnus-tmp-number)
3765       (error (gnus-message 5 "Error updating the summary line")))
3766     (when (gnus-visual-p 'summary-highlight 'highlight)
3767       (forward-line -1)
3768       (gnus-summary-highlight-line)
3769       (gnus-run-hooks 'gnus-summary-update-hook)
3770       (forward-line 1))))
3771
3772 (defun gnus-summary-update-line (&optional dont-update)
3773   "Update summary line after change."
3774   (when (and gnus-summary-default-score
3775              (not gnus-summary-inhibit-highlight))
3776     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3777            (article (gnus-summary-article-number))
3778            (score (gnus-summary-article-score article)))
3779       (unless dont-update
3780         (if (and gnus-summary-mark-below
3781                  (< (gnus-summary-article-score)
3782                     gnus-summary-mark-below))
3783             ;; This article has a low score, so we mark it as read.
3784             (when (memq article gnus-newsgroup-unreads)
3785               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3786           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3787             ;; This article was previously marked as read on account
3788             ;; of a low score, but now it has risen, so we mark it as
3789             ;; unread.
3790             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3791         (gnus-summary-update-mark
3792          (if (or (null gnus-summary-default-score)
3793                  (<= (abs (- score gnus-summary-default-score))
3794                      gnus-summary-zcore-fuzz))
3795              ?                          ;Whitespace
3796            (if (< score gnus-summary-default-score)
3797                gnus-score-below-mark gnus-score-over-mark))
3798          'score))
3799       ;; Do visual highlighting.
3800       (when (gnus-visual-p 'summary-highlight 'highlight)
3801         (gnus-summary-highlight-line)
3802         (gnus-run-hooks 'gnus-summary-update-hook)))))
3803
3804 (defvar gnus-tmp-new-adopts nil)
3805
3806 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3807   "Return the number of articles in THREAD.
3808 This may be 0 in some cases -- if none of the articles in
3809 the thread are to be displayed."
3810   (let* ((number
3811          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3812           (cond
3813            ((not (listp thread))
3814             1)
3815            ((and (consp thread) (cdr thread))
3816             (apply
3817              '+ 1 (mapcar
3818                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3819            ((null thread)
3820             1)
3821            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3822             1)
3823            (t 0))))
3824     (when (and level (zerop level) gnus-tmp-new-adopts)
3825       (incf number
3826             (apply '+ (mapcar
3827                        'gnus-summary-number-of-articles-in-thread
3828                        gnus-tmp-new-adopts))))
3829     (if char
3830         (if (> number 1) gnus-not-empty-thread-mark
3831           gnus-empty-thread-mark)
3832       number)))
3833
3834 (defsubst gnus-summary-line-message-size (head)
3835   "Return pretty-printed version of message size.
3836 This function is intended to be used in
3837 `gnus-summary-line-format-alist'."
3838   (let ((c (or (mail-header-chars head) -1)))
3839     (cond ((< c 0) "n/a")               ; chars not available
3840           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3841           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3842           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3843           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3844
3845
3846 (defun gnus-summary-set-local-parameters (group)
3847   "Go through the local params of GROUP and set all variable specs in that list."
3848   (let ((vars '(quit-config)))          ; Ignore quit-config.
3849     (dolist (elem (gnus-group-find-parameter group))
3850       (and (consp elem)                 ; Has to be a cons.
3851            (consp (cdr elem))           ; The cdr has to be a list.
3852            (symbolp (car elem))         ; Has to be a symbol in there.
3853            (not (memq (car elem) vars))
3854            (ignore-errors
3855              (push (car elem) vars)
3856              ;; Variables like `gnus-show-threads' that are globally
3857              ;; bound, if used as group parameters, need to get to be
3858              ;; buffer-local, whereas just parameters like `gcc-self',
3859              ;; `timestamp', etc. should not be bound as variables.
3860              (if (boundp (car elem))
3861                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3862                (eval (nth 1 elem))))))))
3863
3864 (defun gnus-summary-read-group (group &optional show-all no-article
3865                                       kill-buffer no-display backward
3866                                       select-articles)
3867   "Start reading news in newsgroup GROUP.
3868 If SHOW-ALL is non-nil, already read articles are also listed.
3869 If NO-ARTICLE is non-nil, no article is selected initially.
3870 If NO-DISPLAY, don't generate a summary buffer."
3871   (let (result)
3872     (while (and group
3873                 (null (setq result
3874                             (let ((gnus-auto-select-next nil))
3875                               (or (gnus-summary-read-group-1
3876                                    group show-all no-article
3877                                    kill-buffer no-display
3878                                    select-articles)
3879                                   (setq show-all nil
3880                                         select-articles nil)))))
3881                 (eq gnus-auto-select-next 'quietly))
3882       (set-buffer gnus-group-buffer)
3883       ;; The entry function called above goes to the next
3884       ;; group automatically, so we go two groups back
3885       ;; if we are searching for the previous group.
3886       (when backward
3887         (gnus-group-prev-unread-group 2))
3888       (if (not (equal group (gnus-group-group-name)))
3889           (setq group (gnus-group-group-name))
3890         (setq group nil)))
3891     result))
3892
3893 (defun gnus-summary-read-group-1 (group show-all no-article
3894                                         kill-buffer no-display
3895                                         &optional select-articles)
3896   ;; Killed foreign groups can't be entered.
3897   ;;  (when (and (not (gnus-group-native-p group))
3898   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3899   ;;    (error "Dead non-native groups can't be entered"))
3900   (gnus-message 5 "Retrieving newsgroup: %s..."
3901                 (gnus-group-decoded-name group))
3902   (let* ((new-group (gnus-summary-setup-buffer group))
3903          (quit-config (gnus-group-quit-config group))
3904          (did-select (and new-group (gnus-select-newsgroup
3905                                      group show-all select-articles))))
3906     (cond
3907      ;; This summary buffer exists already, so we just select it.
3908      ((not new-group)
3909       (gnus-set-global-variables)
3910       (when kill-buffer
3911         (gnus-kill-or-deaden-summary kill-buffer))
3912       (gnus-configure-windows 'summary 'force)
3913       (gnus-set-mode-line 'summary)
3914       (gnus-summary-position-point)
3915       (message "")
3916       t)
3917      ;; We couldn't select this group.
3918      ((null did-select)
3919       (when (and (eq major-mode 'gnus-summary-mode)
3920                  (not (equal (current-buffer) kill-buffer)))
3921         (kill-buffer (current-buffer))
3922         (if (not quit-config)
3923             (progn
3924               ;; Update the info -- marks might need to be removed,
3925               ;; for instance.
3926               (gnus-summary-update-info)
3927               (set-buffer gnus-group-buffer)
3928               (gnus-group-jump-to-group group)
3929               (gnus-group-next-unread-group 1))
3930           (gnus-handle-ephemeral-exit quit-config)))
3931       (let ((grpinfo (gnus-get-info group)))
3932         (if (null (gnus-info-read grpinfo))
3933             (gnus-message 3 "Group %s contains no messages"
3934                           (gnus-group-decoded-name group))
3935           (gnus-message 3 "Can't select group")))
3936       nil)
3937      ;; The user did a `C-g' while prompting for number of articles,
3938      ;; so we exit this group.
3939      ((eq did-select 'quit)
3940       (and (eq major-mode 'gnus-summary-mode)
3941            (not (equal (current-buffer) kill-buffer))
3942            (kill-buffer (current-buffer)))
3943       (when kill-buffer
3944         (gnus-kill-or-deaden-summary kill-buffer))
3945       (if (not quit-config)
3946           (progn
3947             (set-buffer gnus-group-buffer)
3948             (gnus-group-jump-to-group group)
3949             (gnus-configure-windows 'group 'force))
3950         (gnus-handle-ephemeral-exit quit-config))
3951       ;; Finally signal the quit.
3952       (signal 'quit nil))
3953      ;; The group was successfully selected.
3954      (t
3955       (gnus-set-global-variables)
3956       ;; Save the active value in effect when the group was entered.
3957       (setq gnus-newsgroup-active
3958             (gnus-copy-sequence
3959              (gnus-active gnus-newsgroup-name)))
3960       ;; You can change the summary buffer in some way with this hook.
3961       (gnus-run-hooks 'gnus-select-group-hook)
3962       (when (memq 'summary (gnus-update-format-specifications
3963                             nil 'summary 'summary-mode 'summary-dummy))
3964         ;; The format specification for the summary line was updated,
3965         ;; so we need to update the mark positions as well.
3966         (gnus-update-summary-mark-positions))
3967       ;; Do score processing.
3968       (when gnus-use-scoring
3969         (gnus-possibly-score-headers))
3970       ;; Check whether to fill in the gaps in the threads.
3971       (when gnus-build-sparse-threads
3972         (gnus-build-sparse-threads))
3973       ;; Find the initial limit.
3974       (if show-all
3975           (let ((gnus-newsgroup-dormant nil))
3976             (gnus-summary-initial-limit show-all))
3977         (gnus-summary-initial-limit show-all))
3978       ;; Generate the summary buffer.
3979       (unless no-display
3980         (gnus-summary-prepare))
3981       (when gnus-use-trees
3982         (gnus-tree-open group)
3983         (setq gnus-summary-highlight-line-function
3984               'gnus-tree-highlight-article))
3985       ;; If the summary buffer is empty, but there are some low-scored
3986       ;; articles or some excluded dormants, we include these in the
3987       ;; buffer.
3988       (when (and (zerop (buffer-size))
3989                  (not no-display))
3990         (cond (gnus-newsgroup-dormant
3991                (gnus-summary-limit-include-dormant))
3992               ((and gnus-newsgroup-scored show-all)
3993                (gnus-summary-limit-include-expunged t))))
3994       ;; Function `gnus-apply-kill-file' must be called in this hook.
3995       (gnus-run-hooks 'gnus-apply-kill-hook)
3996       (if (and (zerop (buffer-size))
3997                (not no-display))
3998           (progn
3999             ;; This newsgroup is empty.
4000             (gnus-summary-catchup-and-exit nil t)
4001             (gnus-message 6 "No unread news")
4002             (when kill-buffer
4003               (gnus-kill-or-deaden-summary kill-buffer))
4004             ;; Return nil from this function.
4005             nil)
4006         ;; Hide conversation thread subtrees.  We cannot do this in
4007         ;; gnus-summary-prepare-hook since kill processing may not
4008         ;; work with hidden articles.
4009         (gnus-summary-maybe-hide-threads)
4010         (when kill-buffer
4011           (gnus-kill-or-deaden-summary kill-buffer))
4012         (gnus-summary-auto-select-subject)
4013         ;; Show first unread article if requested.
4014         (if (and (not no-article)
4015                  (not no-display)
4016                  gnus-newsgroup-unreads
4017                  gnus-auto-select-first)
4018             (progn
4019               (gnus-configure-windows 'summary)
4020               (let ((art (gnus-summary-article-number)))
4021                 (unless (and (not gnus-plugged)
4022                              (or (memq art gnus-newsgroup-undownloaded)
4023                                  (memq art gnus-newsgroup-downloadable)))
4024                   (gnus-summary-goto-article art))))
4025           ;; Don't select any articles.
4026           (gnus-summary-position-point)
4027           (gnus-configure-windows 'summary 'force)
4028           (gnus-set-mode-line 'summary))
4029         (when (and gnus-auto-center-group
4030                    (get-buffer-window gnus-group-buffer t))
4031           ;; Gotta use windows, because recenter does weird stuff if
4032           ;; the current buffer ain't the displayed window.
4033           (let ((owin (selected-window)))
4034             (select-window (get-buffer-window gnus-group-buffer t))
4035             (when (gnus-group-goto-group group)
4036               (recenter))
4037             (select-window owin)))
4038         ;; Mark this buffer as "prepared".
4039         (setq gnus-newsgroup-prepared t)
4040         (gnus-run-hooks 'gnus-summary-prepared-hook)
4041         (unless (gnus-ephemeral-group-p group)
4042           (gnus-group-update-group group))
4043         t)))))
4044
4045 (defun gnus-summary-auto-select-subject ()
4046   "Select the subject line on initial group entry."
4047   (goto-char (point-min))
4048   (cond
4049    ((eq gnus-auto-select-subject 'best)
4050     (gnus-summary-best-unread-subject))
4051    ((eq gnus-auto-select-subject 'unread)
4052     (gnus-summary-first-unread-subject))
4053    ((eq gnus-auto-select-subject 'unseen)
4054     (gnus-summary-first-unseen-subject))
4055    ((eq gnus-auto-select-subject 'unseen-or-unread)
4056     (gnus-summary-first-unseen-or-unread-subject))
4057    ((eq gnus-auto-select-subject 'first)
4058     ;; Do nothing.
4059     )
4060    ((functionp gnus-auto-select-subject)
4061     (funcall gnus-auto-select-subject))))
4062
4063 (defun gnus-summary-prepare ()
4064   "Generate the summary buffer."
4065   (interactive)
4066   (let ((inhibit-read-only t))
4067     (erase-buffer)
4068     (setq gnus-newsgroup-data nil
4069           gnus-newsgroup-data-reverse nil)
4070     (gnus-run-hooks 'gnus-summary-generate-hook)
4071     ;; Generate the buffer, either with threads or without.
4072     (when gnus-newsgroup-headers
4073       (gnus-summary-prepare-threads
4074        (if gnus-show-threads
4075            (gnus-sort-gathered-threads
4076             (funcall gnus-summary-thread-gathering-function
4077                      (gnus-sort-threads
4078                       (gnus-cut-threads (gnus-make-threads)))))
4079          ;; Unthreaded display.
4080          (gnus-sort-articles gnus-newsgroup-headers))))
4081     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4082     ;; Call hooks for modifying summary buffer.
4083     (goto-char (point-min))
4084     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4085
4086 (defsubst gnus-general-simplify-subject (subject)
4087   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4088   (setq subject
4089         (cond
4090          ;; Truncate the subject.
4091          (gnus-simplify-subject-functions
4092           (gnus-map-function gnus-simplify-subject-functions subject))
4093          ((numberp gnus-summary-gather-subject-limit)
4094           (setq subject (gnus-simplify-subject-re subject))
4095           (if (> (length subject) gnus-summary-gather-subject-limit)
4096               (substring subject 0 gnus-summary-gather-subject-limit)
4097             subject))
4098          ;; Fuzzily simplify it.
4099          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4100           (gnus-simplify-subject-fuzzy subject))
4101          ;; Just remove the leading "Re:".
4102          (t
4103           (gnus-simplify-subject-re subject))))
4104
4105   (if (and gnus-summary-gather-exclude-subject
4106            (string-match gnus-summary-gather-exclude-subject subject))
4107       nil                         ; This article shouldn't be gathered
4108     subject))
4109
4110 (defun gnus-summary-simplify-subject-query ()
4111   "Query where the respool algorithm would put this article."
4112   (interactive)
4113   (gnus-summary-select-article)
4114   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4115
4116 (defun gnus-gather-threads-by-subject (threads)
4117   "Gather threads by looking at Subject headers."
4118   (if (not gnus-summary-make-false-root)
4119       threads
4120     (let ((hashtb (gnus-make-hashtable 1024))
4121           (prev threads)
4122           (result threads)
4123           subject hthread whole-subject)
4124       (while threads
4125         (setq subject (gnus-general-simplify-subject
4126                        (setq whole-subject (mail-header-subject
4127                                             (caar threads)))))
4128         (when subject
4129           (if (setq hthread (gnus-gethash subject hashtb))
4130               (progn
4131                 ;; We enter a dummy root into the thread, if we
4132                 ;; haven't done that already.
4133                 (unless (stringp (caar hthread))
4134                   (setcar hthread (list whole-subject (car hthread))))
4135                 ;; We add this new gathered thread to this gathered
4136                 ;; thread.
4137                 (setcdr (car hthread)
4138                         (nconc (cdar hthread) (list (car threads))))
4139                 ;; Remove it from the list of threads.
4140                 (setcdr prev (cdr threads))
4141                 (setq threads prev))
4142             ;; Enter this thread into the hash table.
4143             (gnus-sethash subject
4144                           (if gnus-summary-make-false-root-always
4145                               (progn
4146                                 ;; If you want a dummy root above all
4147                                 ;; threads...
4148                                 (setcar threads (list whole-subject
4149                                                       (car threads)))
4150                                 threads)
4151                             threads)
4152                           hashtb)))
4153         (setq prev threads)
4154         (setq threads (cdr threads)))
4155       result)))
4156
4157 (defun gnus-gather-threads-by-references (threads)
4158   "Gather threads by looking at References headers."
4159   (let ((idhashtb (gnus-make-hashtable 1024))
4160         (thhashtb (gnus-make-hashtable 1024))
4161         (prev threads)
4162         (result threads)
4163         ids references id gthread gid entered ref)
4164     (while threads
4165       (when (setq references (mail-header-references (caar threads)))
4166         (setq id (mail-header-id (caar threads))
4167               ids (inline (gnus-split-references references))
4168               entered nil)
4169         (while (setq ref (pop ids))
4170           (setq ids (delete ref ids))
4171           (if (not (setq gid (gnus-gethash ref idhashtb)))
4172               (progn
4173                 (gnus-sethash ref id idhashtb)
4174                 (gnus-sethash id threads thhashtb))
4175             (setq gthread (gnus-gethash gid thhashtb))
4176             (unless entered
4177               ;; We enter a dummy root into the thread, if we
4178               ;; haven't done that already.
4179               (unless (stringp (caar gthread))
4180                 (setcar gthread (list (mail-header-subject (caar gthread))
4181                                       (car gthread))))
4182               ;; We add this new gathered thread to this gathered
4183               ;; thread.
4184               (setcdr (car gthread)
4185                       (nconc (cdar gthread) (list (car threads)))))
4186             ;; Add it into the thread hash table.
4187             (gnus-sethash id gthread thhashtb)
4188             (setq entered t)
4189             ;; Remove it from the list of threads.
4190             (setcdr prev (cdr threads))
4191             (setq threads prev))))
4192       (setq prev threads)
4193       (setq threads (cdr threads)))
4194     result))
4195
4196 (defun gnus-sort-gathered-threads (threads)
4197   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4198   (let ((result threads))
4199     (while threads
4200       (when (stringp (caar threads))
4201         (setcdr (car threads)
4202                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4203       (setq threads (cdr threads)))
4204     result))
4205
4206 (defun gnus-thread-loop-p (root thread)
4207   "Say whether ROOT is in THREAD."
4208   (let ((stack (list thread))
4209         (infloop 0)
4210         th)
4211     (while (setq thread (pop stack))
4212       (setq th (cdr thread))
4213       (while (and th
4214                   (not (eq (caar th) root)))
4215         (pop th))
4216       (if th
4217           ;; We have found a loop.
4218           (let (ref-dep)
4219             (setcdr thread (delq (car th) (cdr thread)))
4220             (if (boundp (setq ref-dep (intern "none"
4221                                               gnus-newsgroup-dependencies)))
4222                 (setcdr (symbol-value ref-dep)
4223                         (nconc (cdr (symbol-value ref-dep))
4224                                (list (car th))))
4225               (set ref-dep (list nil (car th))))
4226             (setq infloop 1
4227                   stack nil))
4228         ;; Push all the subthreads onto the stack.
4229         (push (cdr thread) stack)))
4230     infloop))
4231
4232 (defun gnus-make-threads ()
4233   "Go through the dependency hashtb and find the roots.  Return all threads."
4234   (let (threads)
4235     (while (catch 'infloop
4236              (mapatoms
4237               (lambda (refs)
4238                 ;; Deal with self-referencing References loops.
4239                 (when (and (car (symbol-value refs))
4240                            (not (zerop
4241                                  (apply
4242                                   '+
4243                                   (mapcar
4244                                    (lambda (thread)
4245                                      (gnus-thread-loop-p
4246                                       (car (symbol-value refs)) thread))
4247                                    (cdr (symbol-value refs)))))))
4248                   (setq threads nil)
4249                   (throw 'infloop t))
4250                 (unless (car (symbol-value refs))
4251                   ;; These threads do not refer back to any other
4252                   ;; articles, so they're roots.
4253                   (setq threads (append (cdr (symbol-value refs)) threads))))
4254               gnus-newsgroup-dependencies)))
4255     threads))
4256
4257 ;; Build the thread tree.
4258 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4259   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4260
4261 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4262 if it was already present.
4263
4264 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4265 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4266 Message-IDs will be renamed to a unique Message-ID before being
4267 entered.
4268
4269 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4270   (let* ((id (mail-header-id header))
4271          (id-dep (and id (intern id dependencies)))
4272          parent-id ref ref-dep ref-header replaced)
4273     ;; Enter this `header' in the `dependencies' table.
4274     (cond
4275      ((not id-dep)
4276       (setq header nil))
4277      ;; The first two cases do the normal part: enter a new `header'
4278      ;; in the `dependencies' table.
4279      ((not (boundp id-dep))
4280       (set id-dep (list header)))
4281      ((null (car (symbol-value id-dep)))
4282       (setcar (symbol-value id-dep) header))
4283
4284      ;; From here the `header' was already present in the
4285      ;; `dependencies' table.
4286      (force-new
4287       ;; Overrides an existing entry;
4288       ;; just set the header part of the entry.
4289       (setcar (symbol-value id-dep) header)
4290       (setq replaced t))
4291
4292      ;; Renames the existing `header' to a unique Message-ID.
4293      ((not gnus-summary-ignore-duplicates)
4294       ;; An article with this Message-ID has already been seen.
4295       ;; We rename the Message-ID.
4296       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4297            (list header))
4298       (mail-header-set-id header id))
4299
4300      ;; The last case ignores an existing entry, except it adds any
4301      ;; additional Xrefs (in case the two articles came from different
4302      ;; servers.
4303      ;; Also sets `header' to `nil' meaning that the `dependencies'
4304      ;; table was *not* modified.
4305      (t
4306       (mail-header-set-xref
4307        (car (symbol-value id-dep))
4308        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4309                    "")
4310                (or (mail-header-xref header) "")))
4311       (setq header nil)))
4312
4313     (when (and header (not replaced))
4314       ;; First check that we are not creating a References loop.
4315       (setq parent-id (gnus-parent-id (mail-header-references header)))
4316       (setq ref parent-id)
4317       (while (and ref
4318                   (setq ref-dep (intern-soft ref dependencies))
4319                   (boundp ref-dep)
4320                   (setq ref-header (car (symbol-value ref-dep))))
4321         (if (string= id ref)
4322             ;; Yuk!  This is a reference loop.  Make the article be a
4323             ;; root article.
4324             (progn
4325               (mail-header-set-references (car (symbol-value id-dep)) "none")
4326               (setq ref nil)
4327               (setq parent-id nil))
4328           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4329       (setq ref-dep (intern (or parent-id "none") dependencies))
4330       (if (boundp ref-dep)
4331           (setcdr (symbol-value ref-dep)
4332                   (nconc (cdr (symbol-value ref-dep))
4333                          (list (symbol-value id-dep))))
4334         (set ref-dep (list nil (symbol-value id-dep)))))
4335     header))
4336
4337 (defun gnus-extract-message-id-from-in-reply-to (string)
4338   (if (string-match "<[^>]+>" string)
4339       (substring string (match-beginning 0) (match-end 0))
4340     nil))
4341
4342 (defun gnus-build-sparse-threads ()
4343   (let ((headers gnus-newsgroup-headers)
4344         (mail-parse-charset gnus-newsgroup-charset)
4345         (gnus-summary-ignore-duplicates t)
4346         header references generation relations
4347         subject child end new-child date)
4348     ;; First we create an alist of generations/relations, where
4349     ;; generations is how much we trust the relation, and the relation
4350     ;; is parent/child.
4351     (gnus-message 7 "Making sparse threads...")
4352     (save-excursion
4353       (nnheader-set-temp-buffer " *gnus sparse threads*")
4354       (while (setq header (pop headers))
4355         (when (and (setq references (mail-header-references header))
4356                    (not (string= references "")))
4357           (insert references)
4358           (setq child (mail-header-id header)
4359                 subject (mail-header-subject header)
4360                 date (mail-header-date header)
4361                 generation 0)
4362           (while (search-backward ">" nil t)
4363             (setq end (1+ (point)))
4364             (when (search-backward "<" nil t)
4365               (setq new-child (buffer-substring (point) end))
4366               (push (list (incf generation)
4367                           child (setq child new-child)
4368                           subject date)
4369                     relations)))
4370           (when child
4371             (push (list (1+ generation) child nil subject) relations))
4372           (erase-buffer)))
4373       (kill-buffer (current-buffer)))
4374     ;; Sort over trustworthiness.
4375     (dolist (relation (sort relations 'car-less-than-car))
4376       (when (gnus-dependencies-add-header
4377              (make-full-mail-header
4378               gnus-reffed-article-number
4379               (nth 3 relation) "" (or (nth 4 relation) "")
4380               (nth 1 relation)
4381               (or (nth 2 relation) "") 0 0 "")
4382              gnus-newsgroup-dependencies nil)
4383         (push gnus-reffed-article-number gnus-newsgroup-limit)
4384         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4385         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4386               gnus-newsgroup-reads)
4387         (decf gnus-reffed-article-number)))
4388     (gnus-message 7 "Making sparse threads...done")))
4389
4390 (defun gnus-build-old-threads ()
4391   ;; Look at all the articles that refer back to old articles, and
4392   ;; fetch the headers for the articles that aren't there.  This will
4393   ;; build complete threads - if the roots haven't been expired by the
4394   ;; server, that is.
4395   (let ((mail-parse-charset gnus-newsgroup-charset)
4396         id heads)
4397     (mapatoms
4398      (lambda (refs)
4399        (when (not (car (symbol-value refs)))
4400          (setq heads (cdr (symbol-value refs)))
4401          (while heads
4402            (if (memq (mail-header-number (caar heads))
4403                      gnus-newsgroup-dormant)
4404                (setq heads (cdr heads))
4405              (setq id (symbol-name refs))
4406              (while (and (setq id (gnus-build-get-header id))
4407                          (not (car (gnus-id-to-thread id)))))
4408              (setq heads nil)))))
4409      gnus-newsgroup-dependencies)))
4410
4411 (defsubst gnus-remove-odd-characters (string)
4412   "Translate STRING into something that doesn't contain weird characters."
4413   (mm-subst-char-in-string
4414    ?\r ?\-
4415    (mm-subst-char-in-string ?\n ?\- string t) t))
4416
4417 ;; This function has to be called with point after the article number
4418 ;; on the beginning of the line.
4419 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4420   (let ((eol (point-at-eol))
4421         (buffer (current-buffer))
4422         header references in-reply-to)
4423
4424     ;; overview: [num subject from date id refs chars lines misc]
4425     (unwind-protect
4426         (let (x)
4427           (narrow-to-region (point) eol)
4428           (unless (eobp)
4429             (forward-char))
4430
4431           (setq header
4432                 (make-full-mail-header
4433                  number                 ; number
4434                  (condition-case ()     ; subject
4435                      (gnus-remove-odd-characters
4436                       (funcall gnus-decode-encoded-word-function
4437                                (setq x (nnheader-nov-field))))
4438                    (error x))
4439                  (condition-case ()     ; from
4440                      (gnus-remove-odd-characters
4441                       (funcall gnus-decode-encoded-address-function
4442                                (setq x (nnheader-nov-field))))
4443                    (error x))
4444                  (nnheader-nov-field)   ; date
4445                  (nnheader-nov-read-message-id number)  ; id
4446                  (setq references (nnheader-nov-field)) ; refs
4447                  (nnheader-nov-read-integer) ; chars
4448                  (nnheader-nov-read-integer) ; lines
4449                  (unless (eobp)
4450                    (if (looking-at "Xref: ")
4451                        (goto-char (match-end 0)))
4452                    (nnheader-nov-field)) ; Xref
4453                  (nnheader-nov-parse-extra)))) ; extra
4454
4455       (widen))
4456
4457     (when (and (string= references "")
4458                (setq in-reply-to (mail-header-extra header))
4459                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4460       (mail-header-set-references
4461        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4462
4463     (when gnus-alter-header-function
4464       (funcall gnus-alter-header-function header))
4465     (gnus-dependencies-add-header header dependencies force-new)))
4466
4467 (defun gnus-build-get-header (id)
4468   "Look through the buffer of NOV lines and find the header to ID.
4469 Enter this line into the dependencies hash table, and return
4470 the id of the parent article (if any)."
4471   (let ((deps gnus-newsgroup-dependencies)
4472         found header)
4473     (prog1
4474         (with-current-buffer nntp-server-buffer
4475           (let ((case-fold-search nil))
4476             (goto-char (point-min))
4477             (while (and (not found)
4478                         (search-forward id nil t))
4479               (beginning-of-line)
4480               (setq found (looking-at
4481                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4482                                    (regexp-quote id))))
4483               (or found (beginning-of-line 2)))
4484             (when found
4485               (beginning-of-line)
4486               (and
4487                (setq header (gnus-nov-parse-line
4488                              (read (current-buffer)) deps))
4489                (gnus-parent-id (mail-header-references header))))))
4490       (when header
4491         (let ((number (mail-header-number header)))
4492           (push number gnus-newsgroup-limit)
4493           (push header gnus-newsgroup-headers)
4494           (if (memq number gnus-newsgroup-unselected)
4495               (progn
4496                 (setq gnus-newsgroup-unreads
4497                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4498                                                number))
4499                 (setq gnus-newsgroup-unselected
4500                       (delq number gnus-newsgroup-unselected)))
4501             (push number gnus-newsgroup-ancient)))))))
4502
4503 (defun gnus-build-all-threads ()
4504   "Read all the headers."
4505   (let ((gnus-summary-ignore-duplicates t)
4506         (mail-parse-charset gnus-newsgroup-charset)
4507         (dependencies gnus-newsgroup-dependencies)
4508         header article)
4509     (with-current-buffer nntp-server-buffer
4510       (let ((case-fold-search nil))
4511         (goto-char (point-min))
4512         (while (not (eobp))
4513           (ignore-errors
4514             (setq article (read (current-buffer))
4515                   header (gnus-nov-parse-line article dependencies)))
4516           (when header
4517             (with-current-buffer gnus-summary-buffer
4518               (push header gnus-newsgroup-headers)
4519               (if (memq (setq article (mail-header-number header))
4520                         gnus-newsgroup-unselected)
4521                   (progn
4522                     (setq gnus-newsgroup-unreads
4523                           (gnus-add-to-sorted-list
4524                            gnus-newsgroup-unreads article))
4525                     (setq gnus-newsgroup-unselected
4526                           (delq article gnus-newsgroup-unselected)))
4527                 (push article gnus-newsgroup-ancient)))
4528             (forward-line 1)))))))
4529
4530 (defun gnus-summary-update-article-line (article header)
4531   "Update the line for ARTICLE using HEADER."
4532   (let* ((id (mail-header-id header))
4533          (thread (gnus-id-to-thread id)))
4534     (unless thread
4535       (error "Article in no thread"))
4536     ;; Update the thread.
4537     (setcar thread header)
4538     (gnus-summary-goto-subject article)
4539     (let* ((datal (gnus-data-find-list article))
4540            (data (car datal))
4541            (inhibit-read-only t)
4542            (level (gnus-summary-thread-level)))
4543       (gnus-delete-line)
4544       (let ((inserted (- (point)
4545                          (progn
4546                            (gnus-summary-insert-line
4547                             header level nil
4548                             (memq article gnus-newsgroup-undownloaded)
4549                             (gnus-article-mark article)
4550                             (memq article gnus-newsgroup-replied)
4551                             (memq article gnus-newsgroup-expirable)
4552                             ;; Only insert the Subject string when it's different
4553                             ;; from the previous Subject string.
4554                             (if (and
4555                                  gnus-show-threads
4556                                  (gnus-subject-equal
4557                                   (condition-case ()
4558                                       (mail-header-subject
4559                                        (gnus-data-header
4560                                         (cadr
4561                                          (gnus-data-find-list
4562                                           article
4563                                           (gnus-data-list t)))))
4564                                     ;; Error on the side of excessive subjects.
4565                                     (error ""))
4566                                   (mail-header-subject header)))
4567                                 ""
4568                               (mail-header-subject header))
4569                             nil (cdr (assq article gnus-newsgroup-scored))
4570                             (memq article gnus-newsgroup-processable))
4571                            (point)))))
4572         (when (cdr datal)
4573           (gnus-data-update-list
4574            (cdr datal)
4575            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4576
4577 (defun gnus-summary-update-article (article &optional iheader)
4578   "Update ARTICLE in the summary buffer."
4579   (set-buffer gnus-summary-buffer)
4580   (let* ((header (gnus-summary-article-header article))
4581          (id (mail-header-id header))
4582          (data (gnus-data-find article))
4583          (thread (gnus-id-to-thread id))
4584          (references (mail-header-references header))
4585          (parent
4586           (gnus-id-to-thread
4587            (or (gnus-parent-id
4588                 (when (and references
4589                            (not (equal "" references)))
4590                   references))
4591                "none")))
4592          (inhibit-read-only t)
4593          (old (car thread)))
4594     (when thread
4595       (unless iheader
4596         (setcar thread nil)
4597         (when parent
4598           (delq thread parent)))
4599       (if (gnus-summary-insert-subject id header)
4600           ;; Set the (possibly) new article number in the data structure.
4601           (gnus-data-set-number data (gnus-id-to-article id))
4602         (setcar thread old)
4603         nil))))
4604
4605 (defun gnus-rebuild-thread (id &optional line)
4606   "Rebuild the thread containing ID.
4607 If LINE, insert the rebuilt thread starting on line LINE."
4608   (let ((inhibit-read-only t)
4609         old-pos current thread data)
4610     (if (not gnus-show-threads)
4611         (setq thread (list (car (gnus-id-to-thread id))))
4612       ;; Get the thread this article is part of.
4613       (setq thread (gnus-remove-thread id)))
4614     (setq old-pos (point-at-bol))
4615     (setq current (save-excursion
4616                     (and (re-search-backward "[\r\n]" nil t)
4617                          (gnus-summary-article-number))))
4618     ;; If this is a gathered thread, we have to go some re-gathering.
4619     (when (stringp (car thread))
4620       (let ((subject (car thread))
4621             roots thr)
4622         (setq thread (cdr thread))
4623         (while thread
4624           (unless (memq (setq thr (gnus-id-to-thread
4625                                    (gnus-root-id
4626                                     (mail-header-id (caar thread)))))
4627                         roots)
4628             (push thr roots))
4629           (setq thread (cdr thread)))
4630         ;; We now have all (unique) roots.
4631         (if (= (length roots) 1)
4632             ;; All the loose roots are now one solid root.
4633             (setq thread (car roots))
4634           (setq thread (cons subject (gnus-sort-threads roots))))))
4635     (let (threads)
4636       ;; We then insert this thread into the summary buffer.
4637       (when line
4638         (goto-char (point-min))
4639         (forward-line (1- line)))
4640       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4641         (if gnus-show-threads
4642             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4643           (gnus-summary-prepare-unthreaded thread))
4644         (setq data (nreverse gnus-newsgroup-data))
4645         (setq threads gnus-newsgroup-threads))
4646       ;; We splice the new data into the data structure.
4647       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4648       ;;!!! then we want to insert at the beginning of the buffer.
4649       ;;!!! That happens to be true with Gnus now, but that may
4650       ;;!!! change in the future.  Perhaps.
4651       (gnus-data-enter-list
4652        (if line nil current) data (- (point) old-pos))
4653       (setq gnus-newsgroup-threads
4654             (nconc threads gnus-newsgroup-threads))
4655       (gnus-data-compute-positions))))
4656
4657 (defun gnus-number-to-header (number)
4658   "Return the header for article NUMBER."
4659   (let ((headers gnus-newsgroup-headers))
4660     (while (and headers
4661                 (not (= number (mail-header-number (car headers)))))
4662       (pop headers))
4663     (when headers
4664       (car headers))))
4665
4666 (defun gnus-parent-headers (in-headers &optional generation)
4667   "Return the headers of the GENERATIONeth parent of HEADERS."
4668   (unless generation
4669     (setq generation 1))
4670   (let ((parent t)
4671         (headers in-headers)
4672         references)
4673     (while (and parent
4674                 (not (zerop generation))
4675                 (setq references (mail-header-references headers)))
4676       (setq headers (if (and references
4677                              (setq parent (gnus-parent-id references)))
4678                         (car (gnus-id-to-thread parent))
4679                       nil))
4680       (decf generation))
4681     (and (not (eq headers in-headers))
4682          headers)))
4683
4684 (defun gnus-id-to-thread (id)
4685   "Return the (sub-)thread where ID appears."
4686   (gnus-gethash id gnus-newsgroup-dependencies))
4687
4688 (defun gnus-id-to-article (id)
4689   "Return the article number of ID."
4690   (let ((thread (gnus-id-to-thread id)))
4691     (when (and thread
4692                (car thread))
4693       (mail-header-number (car thread)))))
4694
4695 (defun gnus-id-to-header (id)
4696   "Return the article headers of ID."
4697   (car (gnus-id-to-thread id)))
4698
4699 (defun gnus-article-displayed-root-p (article)
4700   "Say whether ARTICLE is a root(ish) article."
4701   (let ((level (gnus-summary-thread-level article))
4702         (refs (mail-header-references  (gnus-summary-article-header article)))
4703         particle)
4704     (cond
4705      ((null level) nil)
4706      ((zerop level) t)
4707      ((null refs) t)
4708      ((null (gnus-parent-id refs)) t)
4709      ((and (= 1 level)
4710            (null (setq particle (gnus-id-to-article
4711                                  (gnus-parent-id refs))))
4712            (null (gnus-summary-thread-level particle)))))))
4713
4714 (defun gnus-root-id (id)
4715   "Return the id of the root of the thread where ID appears."
4716   (let (last-id prev)
4717     (while (and id (setq prev (car (gnus-id-to-thread id))))
4718       (setq last-id id
4719             id (gnus-parent-id (mail-header-references prev))))
4720     last-id))
4721
4722 (defun gnus-articles-in-thread (thread)
4723   "Return the list of articles in THREAD."
4724   (cons (mail-header-number (car thread))
4725         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4726
4727 (defun gnus-remove-thread (id &optional dont-remove)
4728   "Remove the thread that has ID in it."
4729   (let (headers thread last-id)
4730     ;; First go up in this thread until we find the root.
4731     (setq last-id (gnus-root-id id)
4732           headers (message-flatten-list (gnus-id-to-thread last-id)))
4733     ;; We have now found the real root of this thread.  It might have
4734     ;; been gathered into some loose thread, so we have to search
4735     ;; through the threads to find the thread we wanted.
4736     (let ((threads gnus-newsgroup-threads)
4737           sub)
4738       (while threads
4739         (setq sub (car threads))
4740         (if (stringp (car sub))
4741             ;; This is a gathered thread, so we look at the roots
4742             ;; below it to find whether this article is in this
4743             ;; gathered root.
4744             (progn
4745               (setq sub (cdr sub))
4746               (while sub
4747                 (when (member (caar sub) headers)
4748                   (setq thread (car threads)
4749                         threads nil
4750                         sub nil))
4751                 (setq sub (cdr sub))))
4752           ;; It's an ordinary thread, so we check it.
4753           (when (eq (car sub) (car headers))
4754             (setq thread sub
4755                   threads nil)))
4756         (setq threads (cdr threads)))
4757       ;; If this article is in no thread, then it's a root.
4758       (if thread
4759           (unless dont-remove
4760             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4761         (setq thread (gnus-id-to-thread last-id)))
4762       (when thread
4763         (prog1
4764             thread                      ; We return this thread.
4765           (unless dont-remove
4766             (if (stringp (car thread))
4767                 (progn
4768                   ;; If we use dummy roots, then we have to remove the
4769                   ;; dummy root as well.
4770                   (when (eq gnus-summary-make-false-root 'dummy)
4771                     ;; We go to the dummy root by going to
4772                     ;; the first sub-"thread", and then one line up.
4773                     (gnus-summary-goto-article
4774                      (mail-header-number (caadr thread)))
4775                     (forward-line -1)
4776                     (gnus-delete-line)
4777                     (gnus-data-compute-positions))
4778                   (setq thread (cdr thread))
4779                   (while thread
4780                     (gnus-remove-thread-1 (car thread))
4781                     (setq thread (cdr thread))))
4782               (gnus-remove-thread-1 thread))))))))
4783
4784 (defun gnus-remove-thread-1 (thread)
4785   "Remove the thread THREAD recursively."
4786   (let ((number (mail-header-number (pop thread)))
4787         d)
4788     (setq thread (reverse thread))
4789     (while thread
4790       (gnus-remove-thread-1 (pop thread)))
4791     (when (setq d (gnus-data-find number))
4792       (goto-char (gnus-data-pos d))
4793       (gnus-summary-show-thread)
4794       (gnus-data-remove
4795        number
4796        (- (point-at-bol)
4797           (prog1
4798               (1+ (point-at-eol))
4799             (gnus-delete-line)))))))
4800
4801 (defun gnus-sort-threads-recursive (threads func)
4802   (sort (mapcar (lambda (thread)
4803                   (cons (car thread)
4804                         (and (cdr thread)
4805                              (gnus-sort-threads-recursive (cdr thread) func))))
4806                 threads) func))
4807
4808 (defun gnus-sort-threads-loop (threads func)
4809   (let* ((superthread (cons nil threads))
4810          (stack (list (cons superthread threads)))
4811          remaining-threads thread)
4812     (while stack
4813       (setq remaining-threads (cdr (car stack)))
4814       (if remaining-threads
4815           (progn (setq thread (car remaining-threads))
4816                  (setcdr (car stack) (cdr remaining-threads))
4817                  (if (cdr thread)
4818                      (push (cons thread (cdr thread)) stack)))
4819         (setq thread (caar stack))
4820         (setcdr thread (sort (cdr thread) func))
4821         (pop stack)))
4822     (cdr superthread)))
4823
4824 (defun gnus-sort-threads (threads)
4825   "Sort THREADS."
4826   (if (not gnus-thread-sort-functions)
4827       threads
4828     (gnus-message 8 "Sorting threads...")
4829     (prog1
4830         (condition-case nil
4831             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4832               (gnus-sort-threads-recursive
4833                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4834           ;; Even after binding max-lisp-eval-depth, the recursive
4835           ;; sorter might fail for very long threads.  In that case,
4836           ;; try using a (less well-tested) non-recursive sorter.
4837           (error (gnus-message 9 "Sorting threads with loop...")
4838                  (gnus-sort-threads-loop
4839                   threads (gnus-make-sort-function
4840                            gnus-thread-sort-functions))))
4841       (gnus-message 8 "Sorting threads...done"))))
4842
4843 (defun gnus-sort-articles (articles)
4844   "Sort ARTICLES."
4845   (when gnus-article-sort-functions
4846     (gnus-message 7 "Sorting articles...")
4847     (prog1
4848         (setq gnus-newsgroup-headers
4849               (sort articles (gnus-make-sort-function
4850                               gnus-article-sort-functions)))
4851       (gnus-message 7 "Sorting articles...done"))))
4852
4853 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4854 (defmacro gnus-thread-header (thread)
4855   "Return header of first article in THREAD.
4856 Note that THREAD must never, ever be anything else than a variable -
4857 using some other form will lead to serious barfage."
4858   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4859   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4860   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4861         (vector thread) 2))
4862
4863 (defsubst gnus-article-sort-by-number (h1 h2)
4864   "Sort articles by article number."
4865   (< (mail-header-number h1)
4866      (mail-header-number h2)))
4867
4868 (defun gnus-thread-sort-by-number (h1 h2)
4869   "Sort threads by root article number."
4870   (gnus-article-sort-by-number
4871    (gnus-thread-header h1) (gnus-thread-header h2)))
4872
4873 (defsubst gnus-article-sort-by-random (h1 h2)
4874   "Sort articles randomly."
4875   (zerop (random 2)))
4876
4877 (defun gnus-thread-sort-by-random (h1 h2)
4878   "Sort threads randomly."
4879   (gnus-article-sort-by-random
4880    (gnus-thread-header h1) (gnus-thread-header h2)))
4881
4882 (defsubst gnus-article-sort-by-lines (h1 h2)
4883   "Sort articles by article Lines header."
4884   (< (mail-header-lines h1)
4885      (mail-header-lines h2)))
4886
4887 (defun gnus-thread-sort-by-lines (h1 h2)
4888   "Sort threads by root article Lines header."
4889   (gnus-article-sort-by-lines
4890    (gnus-thread-header h1) (gnus-thread-header h2)))
4891
4892 (defsubst gnus-article-sort-by-chars (h1 h2)
4893   "Sort articles by octet length."
4894   (< (mail-header-chars h1)
4895      (mail-header-chars h2)))
4896
4897 (defun gnus-thread-sort-by-chars (h1 h2)
4898   "Sort threads by root article octet length."
4899   (gnus-article-sort-by-chars
4900    (gnus-thread-header h1) (gnus-thread-header h2)))
4901
4902 (defsubst gnus-article-sort-by-author (h1 h2)
4903   "Sort articles by root author."
4904   (gnus-string<
4905    (let ((extract (funcall
4906                    gnus-extract-address-components
4907                    (mail-header-from h1))))
4908      (or (car extract) (cadr extract) ""))
4909    (let ((extract (funcall
4910                    gnus-extract-address-components
4911                    (mail-header-from h2))))
4912      (or (car extract) (cadr extract) ""))))
4913
4914 (defun gnus-thread-sort-by-author (h1 h2)
4915   "Sort threads by root author."
4916   (gnus-article-sort-by-author
4917    (gnus-thread-header h1)  (gnus-thread-header h2)))
4918
4919 (defsubst gnus-article-sort-by-recipient (h1 h2)
4920   "Sort articles by recipient."
4921   (gnus-string<
4922    (let ((extract (funcall
4923                    gnus-extract-address-components
4924                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4925      (or (car extract) (cadr extract)))
4926    (let ((extract (funcall
4927                    gnus-extract-address-components
4928                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4929      (or (car extract) (cadr extract)))))
4930
4931 (defun gnus-thread-sort-by-recipient (h1 h2)
4932   "Sort threads by root recipient."
4933   (gnus-article-sort-by-recipient
4934    (gnus-thread-header h1) (gnus-thread-header h2)))
4935
4936 (defsubst gnus-article-sort-by-subject (h1 h2)
4937   "Sort articles by root subject."
4938   (gnus-string<
4939    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4940    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4941
4942 (defun gnus-thread-sort-by-subject (h1 h2)
4943   "Sort threads by root subject."
4944   (gnus-article-sort-by-subject
4945    (gnus-thread-header h1) (gnus-thread-header h2)))
4946
4947 (defsubst gnus-article-sort-by-date (h1 h2)
4948   "Sort articles by root article date."
4949   (time-less-p
4950    (gnus-date-get-time (mail-header-date h1))
4951    (gnus-date-get-time (mail-header-date h2))))
4952
4953 (defun gnus-thread-sort-by-date (h1 h2)
4954   "Sort threads by root article date."
4955   (gnus-article-sort-by-date
4956    (gnus-thread-header h1) (gnus-thread-header h2)))
4957
4958 (defsubst gnus-article-sort-by-score (h1 h2)
4959   "Sort articles by root article score.
4960 Unscored articles will be counted as having a score of zero."
4961   (> (or (cdr (assq (mail-header-number h1)
4962                     gnus-newsgroup-scored))
4963          gnus-summary-default-score 0)
4964      (or (cdr (assq (mail-header-number h2)
4965                     gnus-newsgroup-scored))
4966          gnus-summary-default-score 0)))
4967
4968 (defun gnus-thread-sort-by-score (h1 h2)
4969   "Sort threads by root article score."
4970   (gnus-article-sort-by-score
4971    (gnus-thread-header h1) (gnus-thread-header h2)))
4972
4973 (defun gnus-thread-sort-by-total-score (h1 h2)
4974   "Sort threads by the sum of all scores in the thread.
4975 Unscored articles will be counted as having a score of zero."
4976   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4977
4978 (defun gnus-thread-total-score (thread)
4979   ;; This function find the total score of THREAD.
4980   (cond
4981    ((null thread)
4982     0)
4983    ((consp thread)
4984     (if (stringp (car thread))
4985         (apply gnus-thread-score-function 0
4986                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4987       (gnus-thread-total-score-1 thread)))
4988    (t
4989     (gnus-thread-total-score-1 (list thread)))))
4990
4991 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4992   "Sort threads such that the thread with the most recently arrived article comes first."
4993   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4994
4995 (defun gnus-thread-highest-number (thread)
4996   "Return the highest article number in THREAD."
4997   (apply 'max (mapcar (lambda (header)
4998                         (mail-header-number header))
4999                       (message-flatten-list thread))))
5000
5001 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5002   "Sort threads such that the thread with the most recently dated article comes first."
5003   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5004
5005 ; Since this is called not only to sort the top-level threads, but
5006 ; also in recursive sorts to order the articles within a thread, each
5007 ; article will be processed many times.  Thus it speeds things up
5008 ; quite a bit to use gnus-date-get-time, which caches the time value.
5009 (defun gnus-thread-latest-date (thread)
5010   "Return the highest article date in THREAD."
5011   (apply 'max
5012          (mapcar (lambda (header) (gnus-float-time
5013                                    (gnus-date-get-time
5014                                     (mail-header-date header))))
5015                  (message-flatten-list thread))))
5016
5017 (defun gnus-thread-total-score-1 (root)
5018   ;; This function find the total score of the thread below ROOT.
5019   (setq root (car root))
5020   (apply gnus-thread-score-function
5021          (or (append
5022               (mapcar 'gnus-thread-total-score
5023                       (cdr (gnus-id-to-thread (mail-header-id root))))
5024               (when (> (mail-header-number root) 0)
5025                 (list (or (cdr (assq (mail-header-number root)
5026                                      gnus-newsgroup-scored))
5027                           gnus-summary-default-score 0))))
5028              (list gnus-summary-default-score)
5029              '(0))))
5030
5031 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5032 (defvar gnus-tmp-prev-subject nil)
5033 (defvar gnus-tmp-false-parent nil)
5034 (defvar gnus-tmp-root-expunged nil)
5035 (defvar gnus-tmp-dummy-line nil)
5036
5037 (defun gnus-extra-header (type &optional header)
5038   "Return the extra header of TYPE."
5039   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5040       ""))
5041
5042 (defvar gnus-tmp-thread-tree-header-string "")
5043
5044 (defcustom gnus-sum-thread-tree-root "> "
5045   "With %B spec, used for the root of a thread.
5046 If nil, use subject instead."
5047   :version "22.1"
5048   :type '(radio (const :format "%v  " nil) string)
5049   :group 'gnus-thread)
5050
5051 (defcustom gnus-sum-thread-tree-false-root "> "
5052   "With %B spec, used for a false root of a thread.
5053 If nil, use subject instead."
5054   :version "22.1"
5055   :type '(radio (const :format "%v  " nil) string)
5056   :group 'gnus-thread)
5057
5058 (defcustom gnus-sum-thread-tree-single-indent ""
5059   "With %B spec, used for a thread with just one message.
5060 If nil, use subject instead."
5061   :version "22.1"
5062   :type '(radio (const :format "%v  " nil) string)
5063   :group 'gnus-thread)
5064
5065 (defcustom gnus-sum-thread-tree-vertical "| "
5066   "With %B spec, used for drawing a vertical line."
5067   :version "22.1"
5068   :type 'string
5069   :group 'gnus-thread)
5070
5071 (defcustom gnus-sum-thread-tree-indent "  "
5072   "With %B spec, used for indenting."
5073   :version "22.1"
5074   :type 'string
5075   :group 'gnus-thread)
5076
5077 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5078   "With %B spec, used for a leaf with brothers."
5079   :version "22.1"
5080   :type 'string
5081   :group 'gnus-thread)
5082
5083 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5084   "With %B spec, used for a leaf without brothers."
5085   :version "22.1"
5086   :type 'string
5087   :group 'gnus-thread)
5088
5089 (defcustom gnus-summary-display-while-building nil
5090   "If non-nil, show and update the summary buffer as it's being built.
5091 If the value is t, update the buffer after every line is inserted.  If
5092 the value is an integer (N), update the display every N lines."
5093   :version "22.1"
5094   :group 'gnus-thread
5095   :type '(choice (const :tag "off" nil)
5096                  number
5097                  (const :tag "frequently" t)))
5098
5099 (defun gnus-summary-prepare-threads (threads)
5100   "Prepare summary buffer from THREADS and indentation LEVEL.
5101 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5102 or a straight list of headers."
5103   (gnus-message 7 "Generating summary...")
5104
5105   (setq gnus-newsgroup-threads threads)
5106   (beginning-of-line)
5107
5108   (let ((gnus-tmp-level 0)
5109         (default-score (or gnus-summary-default-score 0))
5110         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5111         (building-line-count gnus-summary-display-while-building)
5112         (building-count (integerp gnus-summary-display-while-building))
5113         thread number subject stack state gnus-tmp-gathered beg-match
5114         new-roots gnus-tmp-new-adopts thread-end simp-subject
5115         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5116         gnus-tmp-replied gnus-tmp-subject-or-nil
5117         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5118         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5119         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5120         tree-stack)
5121
5122     (setq gnus-tmp-prev-subject nil
5123           gnus-tmp-thread-tree-header-string "")
5124
5125     (if (vectorp (car threads))
5126         ;; If this is a straight (sic) list of headers, then a
5127         ;; threaded summary display isn't required, so we just create
5128         ;; an unthreaded one.
5129         (gnus-summary-prepare-unthreaded threads)
5130
5131       ;; Do the threaded display.
5132
5133       (if gnus-summary-display-while-building
5134           (switch-to-buffer (buffer-name)))
5135       (while (or threads stack gnus-tmp-new-adopts new-roots)
5136
5137         (if (and (= gnus-tmp-level 0)
5138                  (or (not stack)
5139                      (= (caar stack) 0))
5140                  (not gnus-tmp-false-parent)
5141                  (or gnus-tmp-new-adopts new-roots))
5142             (if gnus-tmp-new-adopts
5143                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5144                       thread (list (car gnus-tmp-new-adopts))
5145                       gnus-tmp-header (caar thread)
5146                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5147               (when new-roots
5148                 (setq thread (list (car new-roots))
5149                       gnus-tmp-header (caar thread)
5150                       new-roots (cdr new-roots))))
5151
5152           (if threads
5153               ;; If there are some threads, we do them before the
5154               ;; threads on the stack.
5155               (setq thread threads
5156                     gnus-tmp-header (caar thread))
5157             ;; There were no current threads, so we pop something off
5158             ;; the stack.
5159             (setq state (car stack)
5160                   gnus-tmp-level (car state)
5161                   tree-stack (cadr state)
5162                   thread (caddr state)
5163                   stack (cdr stack)
5164                   gnus-tmp-header (caar thread))))
5165
5166         (setq gnus-tmp-false-parent nil)
5167         (setq gnus-tmp-root-expunged nil)
5168         (setq thread-end nil)
5169
5170         (if (stringp gnus-tmp-header)
5171             ;; The header is a dummy root.
5172             (cond
5173              ((eq gnus-summary-make-false-root 'adopt)
5174               ;; We let the first article adopt the rest.
5175               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5176                                                (cddar thread)))
5177               (setq gnus-tmp-gathered
5178                     (nconc (mapcar
5179                             (lambda (h) (mail-header-number (car h)))
5180                             (cddar thread))
5181                            gnus-tmp-gathered))
5182               (setq thread (cons (list (caar thread)
5183                                        (cadar thread))
5184                                  (cdr thread)))
5185               (setq gnus-tmp-level -1
5186                     gnus-tmp-false-parent t))
5187              ((eq gnus-summary-make-false-root 'empty)
5188               ;; We print adopted articles with empty subject fields.
5189               (setq gnus-tmp-gathered
5190                     (nconc (mapcar
5191                             (lambda (h) (mail-header-number (car h)))
5192                             (cddar thread))
5193                            gnus-tmp-gathered))
5194               (setq gnus-tmp-level -1))
5195              ((eq gnus-summary-make-false-root 'dummy)
5196               ;; We remember that we probably want to output a dummy
5197               ;; root.
5198               (setq gnus-tmp-dummy-line gnus-tmp-header)
5199               (setq gnus-tmp-prev-subject gnus-tmp-header))
5200              (t
5201               ;; We do not make a root for the gathered
5202               ;; sub-threads at all.
5203               (setq gnus-tmp-level -1)))
5204
5205           (setq number (mail-header-number gnus-tmp-header)
5206                 subject (mail-header-subject gnus-tmp-header)
5207                 simp-subject (gnus-simplify-subject-fully subject))
5208
5209           (cond
5210            ;; If the thread has changed subject, we might want to make
5211            ;; this subthread into a root.
5212            ((and (null gnus-thread-ignore-subject)
5213                  (not (zerop gnus-tmp-level))
5214                  gnus-tmp-prev-subject
5215                  (not (string= gnus-tmp-prev-subject simp-subject)))
5216             (setq new-roots (nconc new-roots (list (car thread)))
5217                   thread-end t
5218                   gnus-tmp-header nil))
5219            ;; If the article lies outside the current limit,
5220            ;; then we do not display it.
5221            ((not (memq number gnus-newsgroup-limit))
5222             (setq gnus-tmp-gathered
5223                   (nconc (mapcar
5224                           (lambda (h) (mail-header-number (car h)))
5225                           (cdar thread))
5226                          gnus-tmp-gathered))
5227             (setq gnus-tmp-new-adopts (if (cdar thread)
5228                                           (append gnus-tmp-new-adopts
5229                                                   (cdar thread))
5230                                         gnus-tmp-new-adopts)
5231                   thread-end t
5232                   gnus-tmp-header nil)
5233             (when (zerop gnus-tmp-level)
5234               (setq gnus-tmp-root-expunged t)))
5235            ;; Perhaps this article is to be marked as read?
5236            ((and gnus-summary-mark-below
5237                  (< (or (cdr (assq number gnus-newsgroup-scored))
5238                         default-score)
5239                     gnus-summary-mark-below)
5240                  ;; Don't touch sparse articles.
5241                  (not (gnus-summary-article-sparse-p number))
5242                  (not (gnus-summary-article-ancient-p number)))
5243             (setq gnus-newsgroup-unreads
5244                   (delq number gnus-newsgroup-unreads))
5245             (if gnus-newsgroup-auto-expire
5246                 (setq gnus-newsgroup-expirable
5247                       (gnus-add-to-sorted-list
5248                        gnus-newsgroup-expirable number))
5249               (push (cons number gnus-low-score-mark)
5250                     gnus-newsgroup-reads))))
5251
5252           (when gnus-tmp-header
5253             ;; We may have an old dummy line to output before this
5254             ;; article.
5255             (when (and gnus-tmp-dummy-line
5256                        (gnus-subject-equal
5257                         gnus-tmp-dummy-line
5258                         (mail-header-subject gnus-tmp-header)))
5259               (gnus-summary-insert-dummy-line
5260                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5261               (setq gnus-tmp-dummy-line nil))
5262
5263             ;; Compute the mark.
5264             (setq gnus-tmp-unread (gnus-article-mark number))
5265
5266             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5267                                   gnus-tmp-header gnus-tmp-level)
5268                   gnus-newsgroup-data)
5269
5270             ;; Actually insert the line.
5271             (setq
5272              gnus-tmp-subject-or-nil
5273              (cond
5274               ((and gnus-thread-ignore-subject
5275                     gnus-tmp-prev-subject
5276                     (not (string= gnus-tmp-prev-subject simp-subject)))
5277                subject)
5278               ((zerop gnus-tmp-level)
5279                (if (and (eq gnus-summary-make-false-root 'empty)
5280                         (memq number gnus-tmp-gathered)
5281                         gnus-tmp-prev-subject
5282                         (string= gnus-tmp-prev-subject simp-subject))
5283                    gnus-summary-same-subject
5284                  subject))
5285               (t gnus-summary-same-subject)))
5286             (if (and (eq gnus-summary-make-false-root 'adopt)
5287                      (= gnus-tmp-level 1)
5288                      (memq number gnus-tmp-gathered))
5289                 (setq gnus-tmp-opening-bracket ?\<
5290                       gnus-tmp-closing-bracket ?\>)
5291               (setq gnus-tmp-opening-bracket ?\[
5292                     gnus-tmp-closing-bracket ?\]))
5293             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5294                 (gnus-make-thread-indent-array
5295                  (max (* 2 (length gnus-thread-indent-array))
5296                       gnus-tmp-level)))
5297             (setq
5298              gnus-tmp-indentation
5299              (aref gnus-thread-indent-array gnus-tmp-level)
5300              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5301              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5302                                 gnus-summary-default-score 0)
5303              gnus-tmp-score-char
5304              (if (or (null gnus-summary-default-score)
5305                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5306                          gnus-summary-zcore-fuzz))
5307                  ?                      ;Whitespace
5308                (if (< gnus-tmp-score gnus-summary-default-score)
5309                    gnus-score-below-mark gnus-score-over-mark))
5310              gnus-tmp-replied
5311              (cond ((memq number gnus-newsgroup-processable)
5312                     gnus-process-mark)
5313                    ((memq number gnus-newsgroup-cached)
5314                     gnus-cached-mark)
5315                    ((memq number gnus-newsgroup-replied)
5316                     gnus-replied-mark)
5317                    ((memq number gnus-newsgroup-forwarded)
5318                     gnus-forwarded-mark)
5319                    ((memq number gnus-newsgroup-saved)
5320                     gnus-saved-mark)
5321                    ((memq number gnus-newsgroup-recent)
5322                     gnus-recent-mark)
5323                    ((memq number gnus-newsgroup-unseen)
5324                     gnus-unseen-mark)
5325                    (t gnus-no-mark))
5326              gnus-tmp-downloaded
5327              (cond ((memq number gnus-newsgroup-undownloaded)
5328                     gnus-undownloaded-mark)
5329                    (gnus-newsgroup-agentized
5330                     gnus-downloaded-mark)
5331                    (t
5332                     gnus-no-mark))
5333              gnus-tmp-from (mail-header-from gnus-tmp-header)
5334              gnus-tmp-name
5335              (cond
5336               ((string-match "<[^>]+> *$" gnus-tmp-from)
5337                (setq beg-match (match-beginning 0))
5338                (or (and (string-match "^\".+\"" gnus-tmp-from)
5339                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5340                    (substring gnus-tmp-from 0 beg-match)))
5341               ((string-match "(.+)" gnus-tmp-from)
5342                (substring gnus-tmp-from
5343                           (1+ (match-beginning 0)) (1- (match-end 0))))
5344               (t gnus-tmp-from))
5345
5346              ;; Do the %B string
5347              gnus-tmp-thread-tree-header-string
5348              (cond
5349               ((not gnus-show-threads) "")
5350               ((zerop gnus-tmp-level)
5351                (cond ((cdar thread)
5352                       (or gnus-sum-thread-tree-root subject))
5353                      (gnus-tmp-new-adopts
5354                       (or gnus-sum-thread-tree-false-root subject))
5355                      (t
5356                       (or gnus-sum-thread-tree-single-indent subject))))
5357               (t
5358                (concat (apply 'concat
5359                               (mapcar (lambda (item)
5360                                         (if (= item 1)
5361                                             gnus-sum-thread-tree-vertical
5362                                           gnus-sum-thread-tree-indent))
5363                                       (cdr (reverse tree-stack))))
5364                        (if (nth 1 thread)
5365                            gnus-sum-thread-tree-leaf-with-other
5366                          gnus-sum-thread-tree-single-leaf)))))
5367             (when (string= gnus-tmp-name "")
5368               (setq gnus-tmp-name gnus-tmp-from))
5369             (unless (numberp gnus-tmp-lines)
5370               (setq gnus-tmp-lines -1))
5371             (if (= gnus-tmp-lines -1)
5372                 (setq gnus-tmp-lines "?")
5373               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5374             (gnus-put-text-property
5375              (point)
5376              (progn (eval gnus-summary-line-format-spec) (point))
5377              'gnus-number number)
5378             (when gnus-visual-p
5379               (forward-line -1)
5380               (gnus-summary-highlight-line)
5381               (when gnus-summary-update-hook
5382                 (gnus-run-hooks 'gnus-summary-update-hook))
5383               (forward-line 1))
5384
5385             (setq gnus-tmp-prev-subject simp-subject)))
5386
5387         (when (nth 1 thread)
5388           (push (list (max 0 gnus-tmp-level)
5389                       (copy-sequence tree-stack)
5390                       (nthcdr 1 thread))
5391                 stack))
5392         (push (if (nth 1 thread) 1 0) tree-stack)
5393         (incf gnus-tmp-level)
5394         (setq threads (if thread-end nil (cdar thread)))
5395         (if gnus-summary-display-while-building
5396             (if building-count
5397                 (progn
5398                   ;; use a set frequency
5399                   (setq building-line-count (1- building-line-count))
5400                   (when (= building-line-count 0)
5401                     (sit-for 0)
5402                     (setq building-line-count
5403                           gnus-summary-display-while-building)))
5404               ;; always
5405               (sit-for 0)))
5406         (unless threads
5407           (setq gnus-tmp-level 0)))))
5408   (gnus-message 7 "Generating summary...done"))
5409
5410 (defun gnus-summary-prepare-unthreaded (headers)
5411   "Generate an unthreaded summary buffer based on HEADERS."
5412   (let (header number mark)
5413
5414     (beginning-of-line)
5415
5416     (while headers
5417       ;; We may have to root out some bad articles...
5418       (when (memq (setq number (mail-header-number
5419                                 (setq header (pop headers))))
5420                   gnus-newsgroup-limit)
5421         ;; Mark article as read when it has a low score.
5422         (when (and gnus-summary-mark-below
5423                    (< (or (cdr (assq number gnus-newsgroup-scored))
5424                           gnus-summary-default-score 0)
5425                       gnus-summary-mark-below)
5426                    (not (gnus-summary-article-ancient-p number)))
5427           (setq gnus-newsgroup-unreads
5428                 (delq number gnus-newsgroup-unreads))
5429           (if gnus-newsgroup-auto-expire
5430               (push number gnus-newsgroup-expirable)
5431             (push (cons number gnus-low-score-mark)
5432                   gnus-newsgroup-reads)))
5433
5434         (setq mark (gnus-article-mark number))
5435         (push (gnus-data-make number mark (1+ (point)) header 0)
5436               gnus-newsgroup-data)
5437         (gnus-summary-insert-line
5438          header 0 number
5439          (memq number gnus-newsgroup-undownloaded)
5440          mark (memq number gnus-newsgroup-replied)
5441          (memq number gnus-newsgroup-expirable)
5442          (mail-header-subject header) nil
5443          (cdr (assq number gnus-newsgroup-scored))
5444          (memq number gnus-newsgroup-processable))))))
5445
5446 (defun gnus-summary-remove-list-identifiers ()
5447   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5448   (let ((regexp (if (consp gnus-list-identifiers)
5449                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5450                   gnus-list-identifiers))
5451         changed subject)
5452     (when regexp
5453       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5454       (dolist (header gnus-newsgroup-headers)
5455         (setq subject (mail-header-subject header)
5456               changed nil)
5457         (while (string-match regexp subject)
5458           (setq subject
5459                 (concat (substring subject 0 (match-beginning 1))
5460                         (substring subject (match-end 0)))
5461                 changed t))
5462         (when changed
5463           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5464             (setq subject
5465                   (concat (substring subject 0 (match-beginning 1))
5466                           (substring subject (match-end 1)))))
5467           (mail-header-set-subject header subject))))))
5468
5469 (defun gnus-fetch-headers (articles)
5470   "Fetch headers of ARTICLES."
5471   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5472     (gnus-message 5 "Fetching headers for %s..." name)
5473     (prog1
5474         (if (eq 'nov
5475                 (setq gnus-headers-retrieved-by
5476                       (gnus-retrieve-headers
5477                        articles gnus-newsgroup-name
5478                        ;; We might want to fetch old headers, but
5479                        ;; not if there is only 1 article.
5480                        (and (or (and
5481                                  (not (eq gnus-fetch-old-headers 'some))
5482                                  (not (numberp gnus-fetch-old-headers)))
5483                                 (> (length articles) 1))
5484                             gnus-fetch-old-headers))))
5485             (gnus-get-newsgroup-headers-xover
5486              articles nil nil gnus-newsgroup-name t)
5487           (gnus-get-newsgroup-headers))
5488       (gnus-message 5 "Fetching headers for %s...done" name))))
5489
5490 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5491   "Select newsgroup GROUP.
5492 If READ-ALL is non-nil, all articles in the group are selected.
5493 If SELECT-ARTICLES, only select those articles from GROUP."
5494   (let* ((entry (gnus-group-entry group))
5495          ;;!!! Dirty hack; should be removed.
5496          (gnus-summary-ignore-duplicates
5497           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5498               t
5499             gnus-summary-ignore-duplicates))
5500          (info (nth 2 entry))
5501          charset articles fetched-articles cached)
5502
5503     (unless (gnus-check-server
5504              (set (make-local-variable 'gnus-current-select-method)
5505                   (gnus-find-method-for-group group)))
5506       (error "Couldn't open server"))
5507     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5508
5509     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5510         (gnus-activate-group group)     ; Or we can activate it...
5511         (progn                          ; Or we bug out.
5512           (when (equal major-mode 'gnus-summary-mode)
5513             (gnus-kill-buffer (current-buffer)))
5514           (error
5515            "Couldn't activate group %s: %s"
5516            (mm-decode-coding-string group charset)
5517            (mm-decode-coding-string (gnus-status-message group) charset))))
5518
5519     (unless (gnus-request-group group t)
5520       (when (equal major-mode 'gnus-summary-mode)
5521         (gnus-kill-buffer (current-buffer)))
5522       (error "Couldn't request group %s: %s"
5523              (mm-decode-coding-string group charset)
5524              (mm-decode-coding-string (gnus-status-message group) charset)))
5525
5526     (when gnus-agent
5527       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5528
5529       (setq gnus-summary-use-undownloaded-faces
5530             (gnus-agent-find-parameter
5531              group
5532              'agent-enable-undownloaded-faces)))
5533
5534     (setq gnus-newsgroup-name group
5535           gnus-newsgroup-unselected nil
5536           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5537
5538     (let ((display (gnus-group-find-parameter group 'display)))
5539       (setq gnus-newsgroup-display
5540             (cond
5541              ((not (zerop (or (car-safe read-all) 0)))
5542               ;; The user entered the group with C-u SPC/RET, let's show
5543               ;; all articles.
5544               'gnus-not-ignore)
5545              ((eq display 'all)
5546               'gnus-not-ignore)
5547              ((arrayp display)
5548               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5549              ((numberp display)
5550               ;; The following is probably the "correct" solution, but
5551               ;; it makes Gnus fetch all headers and then limit the
5552               ;; articles (which is slow), so instead we hack the
5553               ;; select-articles parameter instead. -- Simon Josefsson
5554               ;; <jas@kth.se>
5555               ;;
5556               ;; (gnus-byte-compile
5557               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5558               ;;                         display)))))
5559               (setq select-articles
5560                     (gnus-uncompress-range
5561                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5562                              (if (> tmp 0)
5563                                  tmp
5564                                1))
5565                            (cdr (gnus-active group)))))
5566               nil)
5567              (t
5568               nil))))
5569
5570     (gnus-summary-setup-default-charset)
5571
5572     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5573     (when (gnus-virtual-group-p group)
5574       (setq cached gnus-newsgroup-cached))
5575
5576     (setq gnus-newsgroup-unreads
5577           (gnus-sorted-ndifference
5578            (gnus-sorted-ndifference gnus-newsgroup-unreads
5579                                     gnus-newsgroup-marked)
5580            gnus-newsgroup-dormant))
5581
5582     (setq gnus-newsgroup-processable nil)
5583
5584     (gnus-update-read-articles group gnus-newsgroup-unreads)
5585
5586     ;; Adjust and set lists of article marks.
5587     (when info
5588       (gnus-adjust-marked-articles info))
5589     (if (setq articles select-articles)
5590         (setq gnus-newsgroup-unselected
5591               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5592       (setq articles (gnus-articles-to-read group read-all)))
5593
5594     (cond
5595      ((null articles)
5596       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5597       'quit)
5598      ((eq articles 0) nil)
5599      (t
5600       ;; Init the dependencies hash table.
5601       (setq gnus-newsgroup-dependencies
5602             (gnus-make-hashtable (length articles)))
5603       (gnus-set-global-variables)
5604       ;; Retrieve the headers and read them in.
5605
5606       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5607
5608       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5609       (when cached
5610         (setq gnus-newsgroup-cached cached))
5611
5612       ;; Suppress duplicates?
5613       (when gnus-suppress-duplicates
5614         (gnus-dup-suppress-articles))
5615
5616       ;; Set the initial limit.
5617       (setq gnus-newsgroup-limit (copy-sequence articles))
5618       ;; Remove canceled articles from the list of unread articles.
5619       (setq fetched-articles
5620             (mapcar (lambda (headers) (mail-header-number headers))
5621                     gnus-newsgroup-headers))
5622       (setq gnus-newsgroup-articles fetched-articles)
5623       (setq gnus-newsgroup-unreads
5624             (gnus-sorted-nintersection
5625              gnus-newsgroup-unreads fetched-articles))
5626       (gnus-compute-unseen-list)
5627
5628       ;; Removed marked articles that do not exist.
5629       (gnus-update-missing-marks
5630        (gnus-sorted-difference articles fetched-articles))
5631       ;; We might want to build some more threads first.
5632       (when (and gnus-fetch-old-headers
5633                  (eq gnus-headers-retrieved-by 'nov))
5634         (if (eq gnus-fetch-old-headers 'invisible)
5635             (gnus-build-all-threads)
5636           (gnus-build-old-threads)))
5637       ;; Let the Gnus agent mark articles as read.
5638       (when gnus-agent
5639         (gnus-agent-get-undownloaded-list))
5640       ;; Remove list identifiers from subject
5641       (when gnus-list-identifiers
5642         (gnus-summary-remove-list-identifiers))
5643       ;; Check whether auto-expire is to be done in this group.
5644       (setq gnus-newsgroup-auto-expire
5645             (gnus-group-auto-expirable-p group))
5646       ;; Set up the article buffer now, if necessary.
5647       (unless (and gnus-single-article-buffer
5648                    (equal gnus-article-buffer "*Article*"))
5649         (gnus-article-setup-buffer))
5650       ;; First and last article in this newsgroup.
5651       (when gnus-newsgroup-headers
5652         (setq gnus-newsgroup-begin
5653               (mail-header-number (car gnus-newsgroup-headers))
5654               gnus-newsgroup-end
5655               (mail-header-number
5656                (gnus-last-element gnus-newsgroup-headers))))
5657       ;; GROUP is successfully selected.
5658       (or gnus-newsgroup-headers t)))))
5659
5660 (defun gnus-compute-unseen-list ()
5661   ;; The `seen' marks are treated specially.
5662   (if (not gnus-newsgroup-seen)
5663       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5664     (setq gnus-newsgroup-unseen
5665           (gnus-inverse-list-range-intersection
5666            gnus-newsgroup-articles gnus-newsgroup-seen))))
5667
5668 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5669
5670 (defun gnus-summary-display-make-predicate (display)
5671   (require 'gnus-agent)
5672   (when (= (length display) 1)
5673     (setq display (car display)))
5674   (unless gnus-summary-display-cache
5675     (dolist (elem (append '((unread . unread)
5676                             (read . read)
5677                             (unseen . unseen))
5678                           gnus-article-mark-lists))
5679       (push (cons (cdr elem)
5680                   (gnus-byte-compile
5681                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5682             gnus-summary-display-cache)))
5683   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5684         (gnus-category-predicate-cache gnus-summary-display-cache))
5685     (gnus-get-predicate display)))
5686
5687 ;; Uses the dynamically bound `number' variable.
5688 (defvar number)
5689 (defun gnus-article-marked-p (type &optional article)
5690   (let ((article (or article number)))
5691     (cond
5692      ((eq type 'tick)
5693       (memq article gnus-newsgroup-marked))
5694      ((eq type 'spam)
5695       (memq article gnus-newsgroup-spam-marked))
5696      ((eq type 'unsend)
5697       (memq article gnus-newsgroup-unsendable))
5698      ((eq type 'undownload)
5699       (memq article gnus-newsgroup-undownloaded))
5700      ((eq type 'download)
5701       (memq article gnus-newsgroup-downloadable))
5702      ((eq type 'unread)
5703       (memq article gnus-newsgroup-unreads))
5704      ((eq type 'read)
5705       (memq article gnus-newsgroup-reads))
5706      ((eq type 'dormant)
5707       (memq article gnus-newsgroup-dormant) )
5708      ((eq type 'expire)
5709       (memq article gnus-newsgroup-expirable))
5710      ((eq type 'reply)
5711       (memq article gnus-newsgroup-replied))
5712      ((eq type 'killed)
5713       (memq article gnus-newsgroup-killed))
5714      ((eq type 'bookmark)
5715       (assq article gnus-newsgroup-bookmarks))
5716      ((eq type 'score)
5717       (assq article gnus-newsgroup-scored))
5718      ((eq type 'save)
5719       (memq article gnus-newsgroup-saved))
5720      ((eq type 'cache)
5721       (memq article gnus-newsgroup-cached))
5722      ((eq type 'forward)
5723       (memq article gnus-newsgroup-forwarded))
5724      ((eq type 'seen)
5725       (not (memq article gnus-newsgroup-unseen)))
5726      ((eq type 'recent)
5727       (memq article gnus-newsgroup-recent))
5728      (t t))))
5729
5730 (defun gnus-articles-to-read (group &optional read-all)
5731   "Find out what articles the user wants to read."
5732   (let* ((articles
5733           ;; Select all articles if `read-all' is non-nil, or if there
5734           ;; are no unread articles.
5735           (if (or read-all
5736                   (and (zerop (length gnus-newsgroup-marked))
5737                        (zerop (length gnus-newsgroup-unreads)))
5738                   ;; Fetch all if the predicate is non-nil.
5739                   gnus-newsgroup-display)
5740               ;; We want to select the headers for all the articles in
5741               ;; the group, so we select either all the active
5742               ;; articles in the group, or (if that's nil), the
5743               ;; articles in the cache.
5744               (or
5745                (if gnus-newsgroup-maximum-articles
5746                    (let ((active (gnus-active group)))
5747                      (gnus-uncompress-range
5748                       (cons (max (car active)
5749                                  (- (cdr active)
5750                                     gnus-newsgroup-maximum-articles
5751                                     -1))
5752                             (cdr active))))
5753                  (gnus-uncompress-range (gnus-active group)))
5754                (gnus-cache-articles-in-group group))
5755             ;; Select only the "normal" subset of articles.
5756             (gnus-sorted-nunion
5757              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5758              gnus-newsgroup-unreads)))
5759          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5760          (scored (length scored-list))
5761          (number (length articles))
5762          (marked (+ (length gnus-newsgroup-marked)
5763                     (length gnus-newsgroup-dormant)))
5764          (select
5765           (cond
5766            ((numberp read-all)
5767             read-all)
5768            ((numberp gnus-newsgroup-display)
5769             gnus-newsgroup-display)
5770            (t
5771             (condition-case ()
5772                 (cond
5773                  ((and (or (<= scored marked) (= scored number))
5774                        (numberp gnus-large-newsgroup)
5775                        (> number gnus-large-newsgroup))
5776                   (let* ((cursor-in-echo-area nil)
5777                          (initial (gnus-parameter-large-newsgroup-initial
5778                                    gnus-newsgroup-name))
5779                          (input
5780                           (read-string
5781                            (format
5782                             "How many articles from %s (%s %d): "
5783                             (gnus-group-decoded-name gnus-newsgroup-name)
5784                             (if initial "max" "default")
5785                             number)
5786                            (if initial
5787                                (cons (number-to-string initial)
5788                                      0)))))
5789                     (if (string-match "^[ \t]*$" input) number input)))
5790                  ((and (> scored marked) (< scored number)
5791                        (> (- scored number) 20))
5792                   (let ((input
5793                          (read-string
5794                           (format "%s %s (%d scored, %d total): "
5795                                   "How many articles from"
5796                                   (gnus-group-decoded-name group)
5797                                   scored number))))
5798                     (if (string-match "^[ \t]*$" input)
5799                         number input)))
5800                  (t number))
5801               (quit
5802                (message "Quit getting the articles to read")
5803                nil))))))
5804     (setq select (if (stringp select) (string-to-number select) select))
5805     (if (or (null select) (zerop select))
5806         select
5807       (if (and (not (zerop scored)) (<= (abs select) scored))
5808           (progn
5809             (setq articles (sort scored-list '<))
5810             (setq number (length articles)))
5811         (setq articles (copy-sequence articles)))
5812
5813       (when (< (abs select) number)
5814         (if (< select 0)
5815             ;; Select the N oldest articles.
5816             (setcdr (nthcdr (1- (abs select)) articles) nil)
5817           ;; Select the N most recent articles.
5818           (setq articles (nthcdr (- number select) articles))))
5819       (setq gnus-newsgroup-unselected
5820             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5821       (when gnus-alter-articles-to-read-function
5822         (setq articles
5823               (sort
5824                (funcall gnus-alter-articles-to-read-function
5825                         gnus-newsgroup-name articles)
5826                '<)))
5827       articles)))
5828
5829 (defun gnus-killed-articles (killed articles)
5830   (let (out)
5831     (while articles
5832       (when (inline (gnus-member-of-range (car articles) killed))
5833         (push (car articles) out))
5834       (setq articles (cdr articles)))
5835     out))
5836
5837 (defun gnus-uncompress-marks (marks)
5838   "Uncompress the mark ranges in MARKS."
5839   (let ((uncompressed '(score bookmark))
5840         out)
5841     (while marks
5842       (if (memq (caar marks) uncompressed)
5843           (push (car marks) out)
5844         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5845       (setq marks (cdr marks)))
5846     out))
5847
5848 (defun gnus-article-mark-to-type (mark)
5849   "Return the type of MARK."
5850   (or (cadr (assq mark gnus-article-special-mark-lists))
5851       'list))
5852
5853 (defun gnus-article-unpropagatable-p (mark)
5854   "Return whether MARK should be propagated to back end."
5855   (memq mark gnus-article-unpropagated-mark-lists))
5856
5857 (defun gnus-adjust-marked-articles (info)
5858   "Set all article lists and remove all marks that are no longer valid."
5859   (let* ((marked-lists (gnus-info-marks info))
5860          (active (gnus-active (gnus-info-group info)))
5861          (min (car active))
5862          (max (cdr active))
5863          (types gnus-article-mark-lists)
5864          marks var articles article mark mark-type
5865          bgn end)
5866     ;; Hack to avoid adjusting marks for imap.
5867     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5868               'nnimap)
5869       (setq min 1))
5870
5871     (dolist (marks marked-lists)
5872       (setq mark (car marks)
5873             mark-type (gnus-article-mark-to-type mark)
5874             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5875
5876       ;; We set the variable according to the type of the marks list,
5877       ;; and then adjust the marks to a subset of the active articles.
5878       (cond
5879        ;; Adjust "simple" lists - compressed yet unsorted
5880        ((eq mark-type 'list)
5881         ;; Simultaneously uncompress and clip to active range
5882         ;; See gnus-uncompress-range for a description of possible marks
5883         (let (l lh)
5884           (if (not (cadr marks))
5885               (set var nil)
5886             (setq articles (if (numberp (cddr marks))
5887                                (list (cdr marks))
5888                              (cdr marks))
5889                   lh (cons nil nil)
5890                   l lh)
5891
5892             (while (setq article (pop articles))
5893               (cond ((consp article)
5894                      (setq bgn (max (car article) min)
5895                            end (min (cdr article) max))
5896                      (while (<= bgn end)
5897                        (setq l (setcdr l (cons bgn nil))
5898                              bgn (1+ bgn))))
5899                     ((and (<= min article)
5900                           (>= max article))
5901                      (setq l (setcdr l (cons article nil))))))
5902             (set var (cdr lh)))))
5903        ;; Adjust assocs.
5904        ((eq mark-type 'tuple)
5905         (set var (setq articles (cdr marks)))
5906         (when (not (listp (cdr (symbol-value var))))
5907           (set var (list (symbol-value var))))
5908         (when (not (listp (cdr articles)))
5909           (setq articles (list articles)))
5910         (while articles
5911           (when (or (not (consp (setq article (pop articles))))
5912                     (< (car article) min)
5913                     (> (car article) max))
5914             (set var (delq article (symbol-value var))))))
5915        ;; Adjust ranges (sloppily).
5916        ((eq mark-type 'range)
5917         (cond
5918          ((eq mark 'seen)
5919           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5920           ;; It should be (seen (NUM1 . NUM2)).
5921           (when (numberp (cddr marks))
5922             (setcdr marks (list (cdr marks))))
5923           (setq articles (cdr marks))
5924           (while (and articles
5925                       (or (and (consp (car articles))
5926                                (> min (cdar articles)))
5927                           (and (numberp (car articles))
5928                                (> min (car articles)))))
5929             (pop articles))
5930           (set var articles))))))))
5931
5932 (defun gnus-update-missing-marks (missing)
5933   "Go through the list of MISSING articles and remove them from the mark lists."
5934   (when missing
5935     (let (var m)
5936       ;; Go through all types.
5937       (dolist (elem gnus-article-mark-lists)
5938         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5939           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5940           (when (symbol-value var)
5941             ;; This list has articles.  So we delete all missing
5942             ;; articles from it.
5943             (setq m missing)
5944             (while m
5945               (set var (delq (pop m) (symbol-value var))))))))))
5946
5947 (defun gnus-update-marks ()
5948   "Enter the various lists of marked articles into the newsgroup info list."
5949   (let ((types gnus-article-mark-lists)
5950         (info (gnus-get-info gnus-newsgroup-name))
5951         type list newmarked symbol delta-marks)
5952     (when info
5953       ;; Add all marks lists to the list of marks lists.
5954       (while (setq type (pop types))
5955         (setq list (symbol-value
5956                     (setq symbol
5957                           (intern (format "gnus-newsgroup-%s" (car type))))))
5958
5959         (when list
5960           ;; Get rid of the entries of the articles that have the
5961           ;; default score.
5962           (when (and (eq (cdr type) 'score)
5963                      gnus-save-score
5964                      list)
5965             (let* ((arts list)
5966                    (prev (cons nil list))
5967                    (all prev))
5968               (while arts
5969                 (if (or (not (consp (car arts)))
5970                         (= (cdar arts) gnus-summary-default-score))
5971                     (setcdr prev (cdr arts))
5972                   (setq prev arts))
5973                 (setq arts (cdr arts)))
5974               (setq list (cdr all)))))
5975
5976         (when (eq (cdr type) 'seen)
5977           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5978
5979         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5980           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5981
5982         (when (and (gnus-check-backend-function
5983                     'request-set-mark gnus-newsgroup-name)
5984                    (not (gnus-article-unpropagatable-p (cdr type))))
5985           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5986                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5987                  (add (gnus-remove-from-range
5988                        (gnus-copy-sequence list) old)))
5989             (when add
5990               (push (list add 'add (list (cdr type))) delta-marks))
5991             (when del
5992               ;; Don't delete marks from outside the active range.  This
5993               ;; shouldn't happen, but is a sanity check.
5994               (setq del (gnus-sorted-range-intersection
5995                          (gnus-active gnus-newsgroup-name) del))
5996               (push (list del 'del (list (cdr type))) delta-marks))))
5997
5998         (when list
5999           (push (cons (cdr type) list) newmarked)))
6000
6001       (when delta-marks
6002         (unless (gnus-check-group gnus-newsgroup-name)
6003           (error "Can't open server for %s" gnus-newsgroup-name))
6004         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6005
6006       ;; Enter these new marks into the info of the group.
6007       (if (nthcdr 3 info)
6008           (setcar (nthcdr 3 info) newmarked)
6009         ;; Add the marks lists to the end of the info.
6010         (when newmarked
6011           (setcdr (nthcdr 2 info) (list newmarked))))
6012
6013       ;; Cut off the end of the info if there's nothing else there.
6014       (let ((i 5))
6015         (while (and (> i 2)
6016                     (not (nth i info)))
6017           (when (nthcdr (decf i) info)
6018             (setcdr (nthcdr i info) nil)))))))
6019
6020 (defun gnus-set-mode-line (where)
6021   "Set the mode line of the article or summary buffers.
6022 If WHERE is `summary', the summary mode line format will be used."
6023   ;; Is this mode line one we keep updated?
6024   (when (and (memq where gnus-updated-mode-lines)
6025              (symbol-value
6026               (intern (format "gnus-%s-mode-line-format-spec" where))))
6027     (let (mode-string)
6028       ;; We evaluate this in the summary buffer since these
6029       ;; variables are buffer-local to that buffer.
6030       (with-current-buffer gnus-summary-buffer
6031         ;; We bind all these variables that are used in the `eval' form
6032         ;; below.
6033         (let* ((mformat (symbol-value
6034                          (intern
6035                           (format "gnus-%s-mode-line-format-spec" where))))
6036                (gnus-tmp-group-name (gnus-mode-string-quote
6037                                      (gnus-group-decoded-name
6038                                       gnus-newsgroup-name)))
6039                (gnus-tmp-article-number (or gnus-current-article 0))
6040                (gnus-tmp-unread gnus-newsgroup-unreads)
6041                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6042                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6043                (gnus-tmp-unread-and-unselected
6044                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6045                             (zerop gnus-tmp-unselected))
6046                        "")
6047                       ((zerop gnus-tmp-unselected)
6048                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6049                       (t (format "{%d(+%d) more}"
6050                                  gnus-tmp-unread-and-unticked
6051                                  gnus-tmp-unselected))))
6052                (gnus-tmp-subject
6053                 (if (and gnus-current-headers
6054                          (vectorp gnus-current-headers))
6055                     (gnus-mode-string-quote
6056                      (mail-header-subject gnus-current-headers))
6057                   ""))
6058                bufname-length max-len
6059                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6060           (setq mode-string (eval mformat))
6061           (setq bufname-length (if (string-match "%b" mode-string)
6062                                    (- (length
6063                                        (buffer-name
6064                                         (if (eq where 'summary)
6065                                             nil
6066                                           (get-buffer gnus-article-buffer))))
6067                                       2)
6068                                  0))
6069           (setq max-len (max 4 (if gnus-mode-non-string-length
6070                                    (- (window-width)
6071                                       gnus-mode-non-string-length
6072                                       bufname-length)
6073                                  (length mode-string))))
6074           ;; We might have to chop a bit of the string off...
6075           (when (> (length mode-string) max-len)
6076             (setq mode-string
6077                   (concat (truncate-string-to-width mode-string (- max-len 3))
6078                           "...")))))
6079       ;; Update the mode line.
6080       (setq mode-line-buffer-identification
6081             (gnus-mode-line-buffer-identification (list mode-string)))
6082       (set-buffer-modified-p t))))
6083
6084 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6085   "Go through the HEADERS list and add all Xrefs to a hash table.
6086 The resulting hash table is returned, or nil if no Xrefs were found."
6087   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6088          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6089          (xref-hashtb (gnus-make-hashtable))
6090          start group entry number xrefs header)
6091     (while headers
6092       (setq header (pop headers))
6093       (when (and (setq xrefs (mail-header-xref header))
6094                  (not (memq (setq number (mail-header-number header))
6095                             unreads)))
6096         (setq start 0)
6097         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6098           (setq start (match-end 0))
6099           (setq group (if prefix
6100                           (concat prefix (substring xrefs (match-beginning 1)
6101                                                     (match-end 1)))
6102                         (substring xrefs (match-beginning 1) (match-end 1))))
6103           (setq number
6104                 (string-to-number (substring xrefs (match-beginning 2)
6105                                           (match-end 2))))
6106           (if (setq entry (gnus-gethash group xref-hashtb))
6107               (setcdr entry (cons number (cdr entry)))
6108             (gnus-sethash group (cons number nil) xref-hashtb)))))
6109     (and start xref-hashtb)))
6110
6111 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6112   "Look through all the headers and mark the Xrefs as read."
6113   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6114         name info xref-hashtb idlist method nth4)
6115     (with-current-buffer gnus-group-buffer
6116       (when (setq xref-hashtb
6117                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6118         (mapatoms
6119          (lambda (group)
6120            (unless (string= from-newsgroup (setq name (symbol-name group)))
6121              (setq idlist (symbol-value group))
6122              ;; Dead groups are not updated.
6123              (and (prog1
6124                       (setq info (gnus-get-info name))
6125                     (when (stringp (setq nth4 (gnus-info-method info)))
6126                       (setq nth4 (gnus-server-to-method nth4))))
6127                   ;; Only do the xrefs if the group has the same
6128                   ;; select method as the group we have just read.
6129                   (or (gnus-methods-equal-p
6130                        nth4 (gnus-find-method-for-group from-newsgroup))
6131                       virtual
6132                       (equal nth4 (setq method (gnus-find-method-for-group
6133                                                 from-newsgroup)))
6134                       (and (equal (car nth4) (car method))
6135                            (equal (nth 1 nth4) (nth 1 method))))
6136                   gnus-use-cross-reference
6137                   (or (not (eq gnus-use-cross-reference t))
6138                       virtual
6139                       ;; Only do cross-references on subscribed
6140                       ;; groups, if that is what is wanted.
6141                       (<= (gnus-info-level info) gnus-level-subscribed))
6142                   (gnus-group-make-articles-read name idlist))))
6143          xref-hashtb)))))
6144
6145 (defun gnus-compute-read-articles (group articles)
6146   (let* ((entry (gnus-group-entry group))
6147          (info (nth 2 entry))
6148          (active (gnus-active group))
6149          ninfo)
6150     (when entry
6151       ;; First peel off all invalid article numbers.
6152       (when active
6153         (let ((ids articles)
6154               id first)
6155           (while (setq id (pop ids))
6156             (when (and first (> id (cdr active)))
6157               ;; We'll end up in this situation in one particular
6158               ;; obscure situation.  If you re-scan a group and get
6159               ;; a new article that is cross-posted to a different
6160               ;; group that has not been re-scanned, you might get
6161               ;; crossposted article that has a higher number than
6162               ;; Gnus believes possible.  So we re-activate this
6163               ;; group as well.  This might mean doing the
6164               ;; crossposting thingy will *increase* the number
6165               ;; of articles in some groups.  Tsk, tsk.
6166               (setq active (or (gnus-activate-group group) active)))
6167             (when (or (> id (cdr active))
6168                       (< id (car active)))
6169               (setq articles (delq id articles))))))
6170       ;; If the read list is nil, we init it.
6171       (if (and active
6172                (null (gnus-info-read info))
6173                (> (car active) 1))
6174           (setq ninfo (cons 1 (1- (car active))))
6175         (setq ninfo (gnus-info-read info)))
6176       ;; Then we add the read articles to the range.
6177       (gnus-add-to-range
6178        ninfo (setq articles (sort articles '<))))))
6179
6180 (defun gnus-group-make-articles-read (group articles)
6181   "Update the info of GROUP to say that ARTICLES are read."
6182   (let* ((num 0)
6183          (entry (gnus-group-entry group))
6184          (info (nth 2 entry))
6185          (active (gnus-active group))
6186          range)
6187     (when entry
6188       (setq range (gnus-compute-read-articles group articles))
6189       (with-current-buffer gnus-group-buffer
6190         (gnus-undo-register
6191           `(progn
6192              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6193              (gnus-info-set-read ',info ',(gnus-info-read info))
6194              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6195              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6196              (gnus-group-update-group ,group t))))
6197       ;; Add the read articles to the range.
6198       (gnus-info-set-read info range)
6199       (gnus-request-set-mark group (list (list range 'add '(read))))
6200       ;; Then we have to re-compute how many unread
6201       ;; articles there are in this group.
6202       (when active
6203         (cond
6204          ((not range)
6205           (setq num (- (1+ (cdr active)) (car active))))
6206          ((not (listp (cdr range)))
6207           (setq num (- (cdr active) (- (1+ (cdr range))
6208                                        (car range)))))
6209          (t
6210           (while range
6211             (if (numberp (car range))
6212                 (setq num (1+ num))
6213               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6214             (setq range (cdr range)))
6215           (setq num (- (cdr active) num))))
6216         ;; Update the number of unread articles.
6217         (setcar entry num)
6218         ;; Update the group buffer.
6219         (unless (gnus-ephemeral-group-p group)
6220           (gnus-group-update-group group t))))))
6221
6222 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6223   (let ((cur nntp-server-buffer)
6224         (dependencies
6225          (or dependencies
6226              (with-current-buffer gnus-summary-buffer
6227                gnus-newsgroup-dependencies)))
6228         headers id end ref number
6229         (mail-parse-charset gnus-newsgroup-charset)
6230         (mail-parse-ignored-charsets
6231          (save-current-buffer (condition-case nil
6232                                   (set-buffer gnus-summary-buffer)
6233                                 (error))
6234                               gnus-newsgroup-ignored-charsets)))
6235     (with-current-buffer nntp-server-buffer
6236       ;; Translate all TAB characters into SPACE characters.
6237       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6238       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6239       (ietf-drums-unfold-fws)
6240       (gnus-run-hooks 'gnus-parse-headers-hook)
6241       (let ((case-fold-search t)
6242             in-reply-to header p lines chars)
6243         (goto-char (point-min))
6244         ;; Search to the beginning of the next header.  Error messages
6245         ;; do not begin with 2 or 3.
6246         (while (re-search-forward "^[23][0-9]+ " nil t)
6247           (setq id nil
6248                 ref nil)
6249           ;; This implementation of this function, with nine
6250           ;; search-forwards instead of the one re-search-forward and
6251           ;; a case (which basically was the old function) is actually
6252           ;; about twice as fast, even though it looks messier.  You
6253           ;; can't have everything, I guess.  Speed and elegance
6254           ;; doesn't always go hand in hand.
6255           (setq
6256            header
6257            (vector
6258             ;; Number.
6259             (prog1
6260                 (setq number (read cur))
6261               (end-of-line)
6262               (setq p (point))
6263               (narrow-to-region (point)
6264                                 (or (and (search-forward "\n.\n" nil t)
6265                                          (- (point) 2))
6266                                     (point))))
6267             ;; Subject.
6268             (progn
6269               (goto-char p)
6270               (if (search-forward "\nsubject:" nil t)
6271                   (funcall gnus-decode-encoded-word-function
6272                            (nnheader-header-value))
6273                 "(none)"))
6274             ;; From.
6275             (progn
6276               (goto-char p)
6277               (if (search-forward "\nfrom:" nil t)
6278                   (funcall gnus-decode-encoded-address-function
6279                            (nnheader-header-value))
6280                 "(nobody)"))
6281             ;; Date.
6282             (progn
6283               (goto-char p)
6284               (if (search-forward "\ndate:" nil t)
6285                   (nnheader-header-value) ""))
6286             ;; Message-ID.
6287             (progn
6288               (goto-char p)
6289               (setq id (if (re-search-forward
6290                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6291                            ;; We do it this way to make sure the Message-ID
6292                            ;; is (somewhat) syntactically valid.
6293                            (buffer-substring (match-beginning 1)
6294                                              (match-end 1))
6295                          ;; If there was no message-id, we just fake one
6296                          ;; to make subsequent routines simpler.
6297                          (nnheader-generate-fake-message-id number))))
6298             ;; References.
6299             (progn
6300               (goto-char p)
6301               (if (search-forward "\nreferences:" nil t)
6302                   (progn
6303                     (setq end (point))
6304                     (prog1
6305                         (nnheader-header-value)
6306                       (setq ref
6307                             (buffer-substring
6308                              (progn
6309                                (end-of-line)
6310                                (search-backward ">" end t)
6311                                (1+ (point)))
6312                              (progn
6313                                (search-backward "<" end t)
6314                                (point))))))
6315                 ;; Get the references from the in-reply-to header if there
6316                 ;; were no references and the in-reply-to header looks
6317                 ;; promising.
6318                 (if (and (search-forward "\nin-reply-to:" nil t)
6319                          (setq in-reply-to (nnheader-header-value))
6320                          (string-match "<[^>]+>" in-reply-to))
6321                     (let (ref2)
6322                       (setq ref (substring in-reply-to (match-beginning 0)
6323                                            (match-end 0)))
6324                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6325                         (setq ref2 (substring in-reply-to (match-beginning 0)
6326                                               (match-end 0)))
6327                         (when (> (length ref2) (length ref))
6328                           (setq ref ref2)))
6329                       ref)
6330                   (setq ref nil))))
6331             ;; Chars.
6332             (progn
6333               (goto-char p)
6334               (if (search-forward "\nchars: " nil t)
6335                   (if (numberp (setq chars (ignore-errors (read cur))))
6336                       chars -1)
6337                 -1))
6338             ;; Lines.
6339             (progn
6340               (goto-char p)
6341               (if (search-forward "\nlines: " nil t)
6342                   (if (numberp (setq lines (ignore-errors (read cur))))
6343                       lines -1)
6344                 -1))
6345             ;; Xref.
6346             (progn
6347               (goto-char p)
6348               (and (search-forward "\nxref:" nil t)
6349                    (nnheader-header-value)))
6350             ;; Extra.
6351             (when gnus-extra-headers
6352               (let ((extra gnus-extra-headers)
6353                     out)
6354                 (while extra
6355                   (goto-char p)
6356                   (when (search-forward
6357                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6358                     (push (cons (car extra) (nnheader-header-value))
6359                           out))
6360                   (pop extra))
6361                 out))))
6362           (when (equal id ref)
6363             (setq ref nil))
6364
6365           (when gnus-alter-header-function
6366             (funcall gnus-alter-header-function header)
6367             (setq id (mail-header-id header)
6368                   ref (gnus-parent-id (mail-header-references header))))
6369
6370           (when (setq header
6371                       (gnus-dependencies-add-header
6372                        header dependencies force-new))
6373             (push header headers))
6374           (goto-char (point-max))
6375           (widen))
6376         (nreverse headers)))))
6377
6378 ;; Goes through the xover lines and returns a list of vectors
6379 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6380                                                   force-new dependencies
6381                                                   group also-fetch-heads)
6382   "Parse the news overview data in the server buffer.
6383 Return a list of headers that match SEQUENCE (see
6384 `nntp-retrieve-headers')."
6385   ;; Get the Xref when the users reads the articles since most/some
6386   ;; NNTP servers do not include Xrefs when using XOVER.
6387   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6388   (let ((mail-parse-charset gnus-newsgroup-charset)
6389         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6390         (cur nntp-server-buffer)
6391         (dependencies (or dependencies gnus-newsgroup-dependencies))
6392         (allp (cond
6393                ((eq gnus-read-all-available-headers t)
6394                 t)
6395                ((and (stringp gnus-read-all-available-headers)
6396                      group)
6397                 (string-match gnus-read-all-available-headers group))
6398                (t
6399                 nil)))
6400         number headers header)
6401     (with-current-buffer nntp-server-buffer
6402       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6403       ;; Allow the user to mangle the headers before parsing them.
6404       (gnus-run-hooks 'gnus-parse-headers-hook)
6405       (goto-char (point-min))
6406       (gnus-parse-without-error
6407         (while (and (or sequence allp)
6408                     (not (eobp)))
6409           (setq number (read cur))
6410           (when (not allp)
6411             (while (and sequence
6412                         (< (car sequence) number))
6413               (setq sequence (cdr sequence))))
6414           (when (and (or allp
6415                          (and sequence
6416                               (eq number (car sequence))))
6417                      (progn
6418                        (setq sequence (cdr sequence))
6419                        (setq header (inline
6420                                       (gnus-nov-parse-line
6421                                        number dependencies force-new)))))
6422             (push header headers))
6423           (forward-line 1)))
6424       ;; A common bug in inn is that if you have posted an article and
6425       ;; then retrieves the active file, it will answer correctly --
6426       ;; the new article is included.  However, a NOV entry for the
6427       ;; article may not have been generated yet, so this may fail.
6428       ;; We work around this problem by retrieving the last few
6429       ;; headers using HEAD.
6430       (if (or (not also-fetch-heads)
6431               (not sequence))
6432           ;; We (probably) got all the headers.
6433           (nreverse headers)
6434         (let ((gnus-nov-is-evil t))
6435           (nconc
6436            (nreverse headers)
6437            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6438              (gnus-get-newsgroup-headers))))))))
6439
6440 (defun gnus-article-get-xrefs ()
6441   "Fill in the Xref value in `gnus-current-headers', if necessary.
6442 This is meant to be called in `gnus-article-internal-prepare-hook'."
6443   (let ((headers (with-current-buffer gnus-summary-buffer
6444                    gnus-current-headers)))
6445     (or (not gnus-use-cross-reference)
6446         (not headers)
6447         (and (mail-header-xref headers)
6448              (not (string= (mail-header-xref headers) "")))
6449         (let ((case-fold-search t)
6450               xref)
6451           (save-restriction
6452             (nnheader-narrow-to-headers)
6453             (goto-char (point-min))
6454             (when (or (and (not (eobp))
6455                            (eq (downcase (char-after)) ?x)
6456                            (looking-at "Xref:"))
6457                       (search-forward "\nXref:" nil t))
6458               (goto-char (1+ (match-end 0)))
6459               (setq xref (buffer-substring (point) (point-at-eol)))
6460               (mail-header-set-xref headers xref)))))))
6461
6462 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6463   "Find article ID and insert the summary line for that article.
6464 OLD-HEADER can either be a header or a line number to insert
6465 the subject line on."
6466   (let* ((line (and (numberp old-header) old-header))
6467          (old-header (and (vectorp old-header) old-header))
6468          (header (cond ((and old-header use-old-header)
6469                         old-header)
6470                        ((and (numberp id)
6471                              (gnus-number-to-header id))
6472                         (gnus-number-to-header id))
6473                        (t
6474                         (gnus-read-header id))))
6475          (number (and (numberp id) id))
6476          d)
6477     (when header
6478       ;; Rebuild the thread that this article is part of and go to the
6479       ;; article we have fetched.
6480       (when (and (not gnus-show-threads)
6481                  old-header)
6482         (when (and number
6483                    (setq d (gnus-data-find (mail-header-number old-header))))
6484           (goto-char (gnus-data-pos d))
6485           (gnus-data-remove
6486            number
6487            (- (point-at-bol)
6488               (prog1
6489                   (1+ (point-at-eol))
6490                 (gnus-delete-line))))))
6491       ;; Remove list identifiers from subject.
6492       (when gnus-list-identifiers
6493         (let ((gnus-newsgroup-headers (list header)))
6494           (gnus-summary-remove-list-identifiers)))
6495       (when old-header
6496         (mail-header-set-number header (mail-header-number old-header)))
6497       (setq gnus-newsgroup-sparse
6498             (delq (setq number (mail-header-number header))
6499                   gnus-newsgroup-sparse))
6500       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6501       (push number gnus-newsgroup-limit)
6502       (gnus-rebuild-thread (mail-header-id header) line)
6503       (gnus-summary-goto-subject number nil t))
6504     (when (and (numberp number)
6505                (> number 0))
6506       ;; We have to update the boundaries even if we can't fetch the
6507       ;; article if ID is a number -- so that the next `P' or `N'
6508       ;; command will fetch the previous (or next) article even
6509       ;; if the one we tried to fetch this time has been canceled.
6510       (when (> number gnus-newsgroup-end)
6511         (setq gnus-newsgroup-end number))
6512       (when (< number gnus-newsgroup-begin)
6513         (setq gnus-newsgroup-begin number))
6514       (setq gnus-newsgroup-unselected
6515             (delq number gnus-newsgroup-unselected)))
6516     ;; Report back a success?
6517     (and header (mail-header-number header))))
6518
6519 ;;; Process/prefix in the summary buffer
6520
6521 (defun gnus-summary-work-articles (n)
6522   "Return a list of articles to be worked upon.
6523 The prefix argument, the list of process marked articles, and the
6524 current article will be taken into consideration."
6525   (with-current-buffer gnus-summary-buffer
6526     (cond
6527      (n
6528       ;; A numerical prefix has been given.
6529       (setq n (prefix-numeric-value n))
6530       (let ((backward (< n 0))
6531             (n (abs (prefix-numeric-value n)))
6532             articles article)
6533         (save-excursion
6534           (while
6535               (and (> n 0)
6536                    (push (setq article (gnus-summary-article-number))
6537                          articles)
6538                    (if backward
6539                        (gnus-summary-find-prev nil article)
6540                      (gnus-summary-find-next nil article)))
6541             (decf n)))
6542         (nreverse articles)))
6543      ((and (gnus-region-active-p) (mark))
6544       (message "region active")
6545       ;; Work on the region between point and mark.
6546       (let ((max (max (point) (mark)))
6547             articles article)
6548         (save-excursion
6549           (goto-char (min (point) (mark)))
6550           (while
6551               (and
6552                (push (setq article (gnus-summary-article-number)) articles)
6553                (gnus-summary-find-next nil article)
6554                (< (point) max)))
6555           (nreverse articles))))
6556      (gnus-newsgroup-processable
6557       ;; There are process-marked articles present.
6558       ;; Save current state.
6559       (gnus-summary-save-process-mark)
6560       ;; Return the list.
6561       (reverse gnus-newsgroup-processable))
6562      (t
6563       ;; Just return the current article.
6564       (list (gnus-summary-article-number))))))
6565
6566 (defmacro gnus-summary-iterate (arg &rest forms)
6567   "Iterate over the process/prefixed articles and do FORMS.
6568 ARG is the interactive prefix given to the command.  FORMS will be
6569 executed with point over the summary line of the articles."
6570   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6571     `(let ((,articles (gnus-summary-work-articles ,arg)))
6572        (while ,articles
6573          (gnus-summary-goto-subject (car ,articles))
6574          ,@forms
6575          (pop ,articles)))))
6576
6577 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6578 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6579
6580 (defun gnus-summary-save-process-mark ()
6581   "Push the current set of process marked articles on the stack."
6582   (interactive)
6583   (push (copy-sequence gnus-newsgroup-processable)
6584         gnus-newsgroup-process-stack))
6585
6586 (defun gnus-summary-kill-process-mark ()
6587   "Push the current set of process marked articles on the stack and unmark."
6588   (interactive)
6589   (gnus-summary-save-process-mark)
6590   (gnus-summary-unmark-all-processable))
6591
6592 (defun gnus-summary-yank-process-mark ()
6593   "Pop the last process mark state off the stack and restore it."
6594   (interactive)
6595   (unless gnus-newsgroup-process-stack
6596     (error "Empty mark stack"))
6597   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6598
6599 (defun gnus-summary-process-mark-set (set)
6600   "Make SET into the current process marked articles."
6601   (gnus-summary-unmark-all-processable)
6602   (mapc 'gnus-summary-set-process-mark set))
6603
6604 ;;; Searching and stuff
6605
6606 (defun gnus-summary-search-group (&optional backward use-level)
6607   "Search for next unread newsgroup.
6608 If optional argument BACKWARD is non-nil, search backward instead."
6609   (with-current-buffer gnus-group-buffer
6610     (when (gnus-group-search-forward
6611            backward nil (if use-level (gnus-group-group-level) nil))
6612       (gnus-group-group-name))))
6613
6614 (defun gnus-summary-best-group (&optional exclude-group)
6615   "Find the name of the best unread group.
6616 If EXCLUDE-GROUP, do not go to this group."
6617   (with-current-buffer gnus-group-buffer
6618     (save-excursion
6619       (gnus-group-best-unread-group exclude-group))))
6620
6621 (defun gnus-summary-find-next (&optional unread article backward)
6622   (if backward
6623       (gnus-summary-find-prev unread article)
6624     (let* ((dummy (gnus-summary-article-intangible-p))
6625            (article (or article (gnus-summary-article-number)))
6626            (data (gnus-data-find-list article))
6627            result)
6628       (when (and (not dummy)
6629                  (or (not gnus-summary-check-current)
6630                      (not unread)
6631                      (not (gnus-data-unread-p (car data)))))
6632         (setq data (cdr data)))
6633       (when (setq result
6634                   (if unread
6635                       (progn
6636                         (while data
6637                           (unless (memq (gnus-data-number (car data))
6638                                         (cond
6639                                          ((eq gnus-auto-goto-ignores
6640                                               'always-undownloaded)
6641                                           gnus-newsgroup-undownloaded)
6642                                          (gnus-plugged
6643                                           nil)
6644                                          ((eq gnus-auto-goto-ignores
6645                                               'unfetched)
6646                                           gnus-newsgroup-unfetched)
6647                                          ((eq gnus-auto-goto-ignores
6648                                               'undownloaded)
6649                                           gnus-newsgroup-undownloaded)))
6650                             (when (gnus-data-unread-p (car data))
6651                               (setq result (car data)
6652                                     data nil)))
6653                           (setq data (cdr data)))
6654                         result)
6655                     (car data)))
6656         (goto-char (gnus-data-pos result))
6657         (gnus-data-number result)))))
6658
6659 (defun gnus-summary-find-prev (&optional unread article)
6660   (let* ((eobp (eobp))
6661          (article (or article (gnus-summary-article-number)))
6662          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6663          result)
6664     (when (and (not eobp)
6665                (or (not gnus-summary-check-current)
6666                    (not unread)
6667                    (not (gnus-data-unread-p (car data)))))
6668       (setq data (cdr data)))
6669     (when (setq result
6670                 (if unread
6671                     (progn
6672                       (while data
6673                         (unless (memq (gnus-data-number (car data))
6674                                       (cond
6675                                        ((eq gnus-auto-goto-ignores
6676                                             'always-undownloaded)
6677                                         gnus-newsgroup-undownloaded)
6678                                        (gnus-plugged
6679                                         nil)
6680                                        ((eq gnus-auto-goto-ignores
6681                                             'unfetched)
6682                                         gnus-newsgroup-unfetched)
6683                                        ((eq gnus-auto-goto-ignores
6684                                             'undownloaded)
6685                                         gnus-newsgroup-undownloaded)))
6686                           (when (gnus-data-unread-p (car data))
6687                             (setq result (car data)
6688                                   data nil)))
6689                         (setq data (cdr data)))
6690                       result)
6691                   (car data)))
6692       (goto-char (gnus-data-pos result))
6693       (gnus-data-number result))))
6694
6695 (defun gnus-summary-find-subject (subject &optional unread backward article)
6696   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6697          (article (or article (gnus-summary-article-number)))
6698          (articles (gnus-data-list backward))
6699          (arts (gnus-data-find-list article articles))
6700          result)
6701     (when (or (not gnus-summary-check-current)
6702               (not unread)
6703               (not (gnus-data-unread-p (car arts))))
6704       (setq arts (cdr arts)))
6705     (while arts
6706       (and (or (not unread)
6707                (gnus-data-unread-p (car arts)))
6708            (vectorp (gnus-data-header (car arts)))
6709            (gnus-subject-equal
6710             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6711            (setq result (car arts)
6712                  arts nil))
6713       (setq arts (cdr arts)))
6714     (and result
6715          (goto-char (gnus-data-pos result))
6716          (gnus-data-number result))))
6717
6718 (defun gnus-summary-search-forward (&optional unread subject backward)
6719   "Search forward for an article.
6720 If UNREAD, look for unread articles.  If SUBJECT, look for
6721 articles with that subject.  If BACKWARD, search backward instead."
6722   (cond (subject (gnus-summary-find-subject subject unread backward))
6723         (backward (gnus-summary-find-prev unread))
6724         (t (gnus-summary-find-next unread))))
6725
6726 (defun gnus-recenter (&optional n)
6727   "Center point in window and redisplay frame.
6728 Also do horizontal recentering."
6729   (interactive "P")
6730   (when (and gnus-auto-center-summary
6731              (not (eq gnus-auto-center-summary 'vertical)))
6732     (gnus-horizontal-recenter))
6733   (if (fboundp 'recenter-top-bottom)
6734       (recenter-top-bottom n)
6735     (recenter n)))
6736
6737 (put 'gnus-recenter 'isearch-scroll t)
6738
6739 (defun gnus-forward-line-ignore-invisible (n)
6740   "Move N lines forward (backward if N is negative).
6741 Like forward-line, but skip over (and don't count) invisible lines."
6742   (let (done)
6743     (while (and (> n 0) (not done))
6744       ;; If the following character is currently invisible,
6745       ;; skip all characters with that same `invisible' property value.
6746       (while (gnus-invisible-p (point))
6747         (goto-char (gnus-next-char-property-change (point))))
6748       (forward-line 1)
6749       (if (eobp)
6750           (setq done t)
6751         (setq n (1- n))))
6752     (while (and (< n 0) (not done))
6753       (forward-line -1)
6754       (if (bobp) (setq done t)
6755         (setq n (1+ n))
6756         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6757           (goto-char (gnus-previous-char-property-change (point))))))))
6758
6759 (defun gnus-summary-recenter ()
6760   "Center point in the summary window.
6761 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6762 displayed, no centering will be performed."
6763   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6764   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6765   (interactive)
6766   ;; The user has to want it.
6767   (when gnus-auto-center-summary
6768     (let* ((top (cond ((< (window-height) 4) 0)
6769                       ((< (window-height) 7) 1)
6770                       (t (if (numberp gnus-auto-center-summary)
6771                              gnus-auto-center-summary
6772                            (/ (1- (window-height)) 2)))))
6773            (height (1- (window-height)))
6774            (bottom (save-excursion
6775                      (goto-char (point-max))
6776                      (gnus-forward-line-ignore-invisible (- height))
6777                      (point)))
6778            (window (get-buffer-window (current-buffer))))
6779       (when (get-buffer-window gnus-article-buffer)
6780         ;; Only do recentering when the article buffer is displayed,
6781         ;; Set the window start to either `bottom', which is the biggest
6782         ;; possible valid number, or the second line from the top,
6783         ;; whichever is the least.
6784         (let ((top-pos (save-excursion
6785                          (gnus-forward-line-ignore-invisible (- top))
6786                          (point))))
6787           (if (> bottom top-pos)
6788               ;; Keep the second line from the top visible
6789               (set-window-start window top-pos)
6790             ;; Try to keep the bottom line visible; if it's partially
6791             ;; obscured, either scroll one more line to make it fully
6792             ;; visible, or revert to using TOP-POS.
6793             (save-excursion
6794               (goto-char (point-max))
6795               (gnus-forward-line-ignore-invisible -1)
6796               (let ((last-line-start (point)))
6797                 (goto-char bottom)
6798                 (set-window-start window (point) t)
6799                 (when (not (pos-visible-in-window-p last-line-start window))
6800                   (gnus-forward-line-ignore-invisible 1)
6801                   (set-window-start window (min (point) top-pos) t)))))))
6802       ;; Do horizontal recentering while we're at it.
6803       (when (and (get-buffer-window (current-buffer) t)
6804                  (not (eq gnus-auto-center-summary 'vertical)))
6805         (let ((selected (selected-window)))
6806           (select-window (get-buffer-window (current-buffer) t))
6807           (gnus-summary-position-point)
6808           (gnus-horizontal-recenter)
6809           (select-window selected))))))
6810
6811 (defun gnus-summary-jump-to-group (newsgroup)
6812   "Move point to NEWSGROUP in group mode buffer."
6813   ;; Keep update point of group mode buffer if visible.
6814   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6815       (save-window-excursion
6816         ;; Take care of tree window mode.
6817         (when (get-buffer-window gnus-group-buffer)
6818           (pop-to-buffer gnus-group-buffer))
6819         (gnus-group-jump-to-group newsgroup))
6820     (save-excursion
6821       ;; Take care of tree window mode.
6822       (if (get-buffer-window gnus-group-buffer 0)
6823           (pop-to-buffer gnus-group-buffer)
6824         (set-buffer gnus-group-buffer))
6825       (gnus-group-jump-to-group newsgroup))))
6826
6827 ;; This function returns a list of article numbers based on the
6828 ;; difference between the ranges of read articles in this group and
6829 ;; the range of active articles.
6830 (defun gnus-list-of-unread-articles (group)
6831   (let* ((read (gnus-info-read (gnus-get-info group)))
6832          (active (or (gnus-active group) (gnus-activate-group group)))
6833          (last (or (cdr active)
6834                    (error "Group %s couldn't be activated " group)))
6835          (bottom (if gnus-newsgroup-maximum-articles
6836                      (max (car active)
6837                           (- last gnus-newsgroup-maximum-articles -1))
6838                    (car active)))
6839          first nlast unread)
6840     ;; If none are read, then all are unread.
6841     (if (not read)
6842         (setq first bottom)
6843       ;; If the range of read articles is a single range, then the
6844       ;; first unread article is the article after the last read
6845       ;; article.  Sounds logical, doesn't it?
6846       (if (and (not (listp (cdr read)))
6847                (or (< (car read) bottom)
6848                    (progn (setq read (list read))
6849                           nil)))
6850           (setq first (max bottom (1+ (cdr read))))
6851         ;; `read' is a list of ranges.
6852         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6853                                   (caar read)))
6854                   1)
6855           (setq first bottom))
6856         (while read
6857           (when first
6858             (while (< first nlast)
6859               (setq unread (cons first unread)
6860                     first (1+ first))))
6861           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6862           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6863           (setq read (cdr read)))))
6864     ;; And add the last unread articles.
6865     (while (<= first last)
6866       (setq unread (cons first unread)
6867             first (1+ first)))
6868     ;; Return the list of unread articles.
6869     (delq 0 (nreverse unread))))
6870
6871 (defun gnus-list-of-read-articles (group)
6872   "Return a list of unread, unticked and non-dormant articles."
6873   (let* ((info (gnus-get-info group))
6874          (marked (gnus-info-marks info))
6875          (active (gnus-active group)))
6876     (and info active
6877          (gnus-list-range-difference
6878           (gnus-list-range-difference
6879            (gnus-sorted-complement
6880             (gnus-uncompress-range
6881              (if gnus-newsgroup-maximum-articles
6882                  (cons (max (car active)
6883                             (- (cdr active)
6884                                gnus-newsgroup-maximum-articles
6885                                -1))
6886                        (cdr active))
6887                active))
6888             (gnus-list-of-unread-articles group))
6889            (cdr (assq 'dormant marked)))
6890           (cdr (assq 'tick marked))))))
6891
6892 ;; This function returns a sequence of article numbers based on the
6893 ;; difference between the ranges of read articles in this group and
6894 ;; the range of active articles.
6895 (defun gnus-sequence-of-unread-articles (group)
6896   (let* ((read (gnus-info-read (gnus-get-info group)))
6897          (active (or (gnus-active group) (gnus-activate-group group)))
6898          (last (cdr active))
6899          (bottom (if gnus-newsgroup-maximum-articles
6900                      (max (car active)
6901                           (- last gnus-newsgroup-maximum-articles -1))
6902                    (car active)))
6903          first nlast unread)
6904     ;; If none are read, then all are unread.
6905     (if (not read)
6906         (setq first bottom)
6907       ;; If the range of read articles is a single range, then the
6908       ;; first unread article is the article after the last read
6909       ;; article.  Sounds logical, doesn't it?
6910       (if (and (not (listp (cdr read)))
6911                (or (< (car read) bottom)
6912                    (progn (setq read (list read))
6913                           nil)))
6914           (setq first (max bottom (1+ (cdr read))))
6915         ;; `read' is a list of ranges.
6916         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6917                                   (caar read)))
6918                   1)
6919           (setq first bottom))
6920         (while read
6921           (when first
6922             (push (cons first nlast) unread))
6923           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6924           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6925           (setq read (cdr read)))))
6926     ;; And add the last unread articles.
6927     (cond ((not (and first last))
6928            nil)
6929           ((< first last)
6930            (push (cons first last) unread))
6931           ((= first last)
6932            (push first unread)))
6933     ;; Return the sequence of unread articles.
6934     (delq 0 (nreverse unread))))
6935
6936 ;; Various summary commands
6937
6938 (defun gnus-summary-select-article-buffer ()
6939   "Reconfigure windows to show article buffer."
6940   (interactive)
6941   (if (not (gnus-buffer-live-p gnus-article-buffer))
6942       (error "There is no article buffer for this summary buffer")
6943     (gnus-configure-windows 'article)
6944     (select-window (get-buffer-window gnus-article-buffer))))
6945
6946 (defun gnus-summary-universal-argument (arg)
6947   "Perform any operation on all articles that are process/prefixed."
6948   (interactive "P")
6949   (let ((articles (gnus-summary-work-articles arg))
6950         func article)
6951     (if (eq
6952          (setq
6953           func
6954           (key-binding
6955            (read-key-sequence
6956             (substitute-command-keys
6957              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6958          'undefined)
6959         (gnus-error 1 "Undefined key")
6960       (save-excursion
6961         (while articles
6962           (gnus-summary-goto-subject (setq article (pop articles)))
6963           (let (gnus-newsgroup-processable)
6964             (command-execute func))
6965           (gnus-summary-remove-process-mark article)))))
6966   (gnus-summary-position-point))
6967
6968 (defun gnus-summary-toggle-truncation (&optional arg)
6969   "Toggle truncation of summary lines.
6970 With ARG, turn line truncation on if ARG is positive."
6971   (interactive "P")
6972   (setq truncate-lines
6973         (if (null arg) (not truncate-lines)
6974           (> (prefix-numeric-value arg) 0)))
6975   (redraw-display))
6976
6977 (defun gnus-summary-find-for-reselect ()
6978   "Return the number of an article to stay on across a reselect.
6979 The current article is considered, then following articles, then previous
6980 articles.  An article is sought which is not cancelled and isn't a temporary
6981 insertion from another group.  If there's no such then return a dummy 0."
6982   (let (found)
6983     (dolist (rev '(nil t))
6984       (unless found      ; don't demand the reverse list if we don't need it
6985         (let ((data (gnus-data-find-list
6986                      (gnus-summary-article-number) (gnus-data-list rev))))
6987           (while (and data (not found))
6988             (if (and (< 0 (gnus-data-number (car data)))
6989                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6990                 (setq found (gnus-data-number (car data))))
6991             (setq data (cdr data))))))
6992     (or found 0)))
6993
6994 (defun gnus-summary-reselect-current-group (&optional all rescan)
6995   "Exit and then reselect the current newsgroup.
6996 The prefix argument ALL means to select all articles."
6997   (interactive "P")
6998   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6999     (error "Ephemeral groups can't be reselected"))
7000   (let ((current-subject (gnus-summary-find-for-reselect))
7001         (group gnus-newsgroup-name))
7002     (setq gnus-newsgroup-begin nil)
7003     (gnus-summary-exit nil 'leave-hidden)
7004     ;; We have to adjust the point of group mode buffer because
7005     ;; point was moved to the next unread newsgroup by exiting.
7006     (gnus-summary-jump-to-group group)
7007     (when rescan
7008       (save-excursion
7009         (gnus-group-get-new-news-this-group 1)))
7010     (gnus-group-read-group all t)
7011     (gnus-summary-goto-subject current-subject nil t)))
7012
7013 (defun gnus-summary-rescan-group (&optional all)
7014   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7015   (interactive "P")
7016   (gnus-summary-reselect-current-group all t))
7017
7018 (defun gnus-summary-update-info (&optional non-destructive)
7019   (save-excursion
7020     (let ((group gnus-newsgroup-name))
7021       (when group
7022         (when gnus-newsgroup-kill-headers
7023           (setq gnus-newsgroup-killed
7024                 (gnus-compress-sequence
7025                  (gnus-sorted-union
7026                   (gnus-list-range-intersection
7027                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7028                   gnus-newsgroup-unreads)
7029                  t)))
7030         (unless (listp (cdr gnus-newsgroup-killed))
7031           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7032         (let ((headers gnus-newsgroup-headers))
7033           ;; Set the new ranges of read articles.
7034           (with-current-buffer gnus-group-buffer
7035             (gnus-undo-force-boundary))
7036           (gnus-update-read-articles
7037            group (gnus-sorted-union
7038                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7039           ;; Set the current article marks.
7040           (let ((gnus-newsgroup-scored
7041                  (if (and (not gnus-save-score)
7042                           (not non-destructive))
7043                      nil
7044                    gnus-newsgroup-scored)))
7045             (save-excursion
7046               (gnus-update-marks)))
7047           ;; Do the cross-ref thing.
7048           (when gnus-use-cross-reference
7049             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7050           ;; Do not switch windows but change the buffer to work.
7051           (set-buffer gnus-group-buffer)
7052           (unless (gnus-ephemeral-group-p group)
7053             (gnus-group-update-group group)))))))
7054
7055 (defun gnus-summary-save-newsrc (&optional force)
7056   "Save the current number of read/marked articles in the dribble buffer.
7057 The dribble buffer will then be saved.
7058 If FORCE (the prefix), also save the .newsrc file(s)."
7059   (interactive "P")
7060   (gnus-summary-update-info t)
7061   (if force
7062       (gnus-save-newsrc-file)
7063     (gnus-dribble-save)))
7064
7065 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7066
7067 (defun gnus-summary-exit (&optional temporary leave-hidden)
7068   "Exit reading current newsgroup, and then return to group selection mode.
7069 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7070   (interactive)
7071   (gnus-set-global-variables)
7072   (when (gnus-buffer-live-p gnus-article-buffer)
7073     (with-current-buffer gnus-article-buffer
7074       (mm-destroy-parts gnus-article-mime-handles)
7075       ;; Set it to nil for safety reason.
7076       (setq gnus-article-mime-handle-alist nil)
7077       (setq gnus-article-mime-handles nil)))
7078   (gnus-kill-save-kill-buffer)
7079   (gnus-async-halt-prefetch)
7080   (let* ((group gnus-newsgroup-name)
7081          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7082          (gnus-group-is-exiting-p t)
7083          (mode major-mode)
7084          (group-point nil)
7085          (buf (current-buffer)))
7086     (unless quit-config
7087       ;; Do adaptive scoring, and possibly save score files.
7088       (when gnus-newsgroup-adaptive
7089         (gnus-score-adaptive))
7090       (when gnus-use-scoring
7091         (gnus-score-save)))
7092     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7093     ;; If we have several article buffers, we kill them at exit.
7094     (unless gnus-single-article-buffer
7095       (when (gnus-buffer-live-p gnus-article-buffer)
7096         (with-current-buffer gnus-article-buffer
7097           ;; Don't kill sticky article buffers
7098           (unless (eq major-mode 'gnus-sticky-article-mode)
7099             (gnus-kill-buffer gnus-article-buffer)
7100             (setq gnus-article-current nil))))
7101       (gnus-kill-buffer gnus-original-article-buffer))
7102     (when gnus-use-cache
7103       (gnus-cache-possibly-remove-articles)
7104       (gnus-cache-save-buffers))
7105     (gnus-async-prefetch-remove-group group)
7106     (when gnus-suppress-duplicates
7107       (gnus-dup-enter-articles))
7108     (when gnus-use-trees
7109       (gnus-tree-close group))
7110     (when gnus-use-cache
7111       (gnus-cache-write-active))
7112     ;; Remove entries for this group.
7113     (nnmail-purge-split-history (gnus-group-real-name group))
7114     ;; Make all changes in this group permanent.
7115     (unless quit-config
7116       (gnus-run-hooks 'gnus-exit-group-hook)
7117       (gnus-summary-update-info))
7118     (gnus-close-group group)
7119     ;; Make sure where we were, and go to next newsgroup.
7120     (set-buffer gnus-group-buffer)
7121     (unless quit-config
7122       (gnus-group-jump-to-group group))
7123     (gnus-run-hooks 'gnus-summary-exit-hook)
7124     (unless (or quit-config
7125                 (not gnus-summary-next-group-on-exit)
7126                 ;; If this group has disappeared from the summary
7127                 ;; buffer, don't skip forwards.
7128                 (not (string= group (gnus-group-group-name))))
7129       (gnus-group-next-unread-group 1))
7130     (setq group-point (point))
7131     (if temporary
7132         nil                             ;Nothing to do.
7133       (set-buffer buf)
7134       (if (not gnus-kill-summary-on-exit)
7135           (progn
7136             (gnus-deaden-summary)
7137             (setq mode nil))
7138         ;; We set all buffer-local variables to nil.  It is unclear why
7139         ;; this is needed, but if we don't, buffer-local variables are
7140         ;; not garbage-collected, it seems.  This would the lead to en
7141         ;; ever-growing Emacs.
7142         (gnus-summary-clear-local-variables)
7143         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7144           (gnus-summary-clear-local-variables))
7145         (when (get-buffer gnus-article-buffer)
7146           (bury-buffer gnus-article-buffer))
7147         ;; Return to group mode buffer.
7148         (when (eq mode 'gnus-summary-mode)
7149           (gnus-kill-buffer buf)))
7150       (setq gnus-current-select-method gnus-select-method)
7151       (set-buffer gnus-group-buffer)
7152       (if quit-config
7153           (gnus-handle-ephemeral-exit quit-config)
7154         (goto-char group-point)
7155         ;; If gnus-group-buffer is already displayed, make sure we also move
7156         ;; the cursor in the window that displays it.
7157         (let ((win (get-buffer-window (current-buffer) 0)))
7158           (if win (set-window-point win (point))))
7159         (unless leave-hidden
7160           (gnus-configure-windows 'group 'force)))
7161       ;; Clear the current group name.
7162       (unless quit-config
7163         (setq gnus-newsgroup-name nil)))))
7164
7165 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7166 (defun gnus-summary-exit-no-update (&optional no-questions)
7167   "Quit reading current newsgroup without updating read article info."
7168   (interactive)
7169   (let* ((group gnus-newsgroup-name)
7170          (gnus-group-is-exiting-p t)
7171          (gnus-group-is-exiting-without-update-p t)
7172          (quit-config (gnus-group-quit-config group)))
7173     (when (or no-questions
7174               gnus-expert-user
7175               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7176       (gnus-async-halt-prefetch)
7177       (run-hooks 'gnus-summary-prepare-exit-hook)
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       ;; If we have several article buffers, we kill them at exit.
7185       (unless gnus-single-article-buffer
7186         (gnus-kill-buffer gnus-article-buffer)
7187         (gnus-kill-buffer gnus-original-article-buffer)
7188         (setq gnus-article-current nil))
7189       (if (not gnus-kill-summary-on-exit)
7190           (gnus-deaden-summary)
7191         (gnus-close-group group)
7192         (gnus-summary-clear-local-variables)
7193         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7194           (gnus-summary-clear-local-variables))
7195         (gnus-kill-buffer gnus-summary-buffer))
7196       (unless gnus-single-article-buffer
7197         (setq gnus-article-current nil))
7198       (when gnus-use-trees
7199         (gnus-tree-close group))
7200       (gnus-async-prefetch-remove-group group)
7201       (when (get-buffer gnus-article-buffer)
7202         (bury-buffer gnus-article-buffer))
7203       ;; Return to the group buffer.
7204       (gnus-configure-windows 'group 'force)
7205       ;; Clear the current group name.
7206       (setq gnus-newsgroup-name nil)
7207       (unless (gnus-ephemeral-group-p group)
7208         (gnus-group-update-group group))
7209       (when (equal (gnus-group-group-name) group)
7210         (gnus-group-next-unread-group 1))
7211       (when quit-config
7212         (gnus-handle-ephemeral-exit quit-config)))))
7213
7214 (defun gnus-handle-ephemeral-exit (quit-config)
7215   "Handle movement when leaving an ephemeral group.
7216 The state which existed when entering the ephemeral is reset."
7217   (if (not (buffer-name (car quit-config)))
7218       (gnus-configure-windows 'group 'force)
7219     (set-buffer (car quit-config))
7220     (cond ((eq major-mode 'gnus-summary-mode)
7221            (gnus-set-global-variables))
7222           ((eq major-mode 'gnus-article-mode)
7223            (save-current-buffer
7224              ;; The `gnus-summary-buffer' variable may point
7225              ;; to the old summary buffer when using a single
7226              ;; article buffer.
7227              (unless (gnus-buffer-live-p gnus-summary-buffer)
7228                (set-buffer gnus-group-buffer))
7229              (set-buffer gnus-summary-buffer)
7230              (gnus-set-global-variables))))
7231     (if (or (eq (cdr quit-config) 'article)
7232             (eq (cdr quit-config) 'pick))
7233         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7234             (gnus-configure-windows 'pick 'force)
7235           (gnus-configure-windows (cdr quit-config) 'force))
7236       (gnus-configure-windows (cdr quit-config) 'force))
7237     (when (eq major-mode 'gnus-summary-mode)
7238       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7239                                                      next-unread-noselect))
7240           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7241                                   'next-noselect)
7242                               (gnus-summary-next-subject 1 nil t))
7243                              ((eq gnus-auto-select-on-ephemeral-exit
7244                                   'next-unread-noselect)
7245                               (gnus-summary-next-subject 1 t t))))
7246             ;; Hide the article buffer which displays the article different
7247             ;; from the one that the cursor points to in the summary buffer.
7248             (gnus-configure-windows 'summary 'force))
7249         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7250                (gnus-summary-next-subject 1))
7251               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7252                (gnus-summary-next-subject 1 t))))
7253       (gnus-summary-recenter)
7254       (gnus-summary-position-point))))
7255
7256 ;;; Dead summaries.
7257
7258 (defvar gnus-dead-summary-mode-map
7259   (let ((map (make-keymap)))
7260     (suppress-keymap map)
7261     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7262     (dolist (key '("\C-d" "\r" "\177" [delete]))
7263       (define-key map key 'gnus-summary-wake-up-the-dead))
7264     (dolist (key '("q" "Q"))
7265       (define-key map key 'bury-buffer))
7266     map))
7267
7268 (define-minor-mode gnus-dead-summary-mode
7269   "Minor mode for Gnus summary buffers."
7270   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7271   (unless (derived-mode-p 'gnus-summary-mode)
7272     (setq gnus-dead-summary-mode nil)))
7273
7274 (defun gnus-deaden-summary ()
7275   "Make the current summary buffer into a dead summary buffer."
7276   ;; Kill any previous dead summary buffer.
7277   (when (and gnus-dead-summary
7278              (buffer-name gnus-dead-summary))
7279     (with-current-buffer gnus-dead-summary
7280       (when gnus-dead-summary-mode
7281         (kill-buffer (current-buffer)))))
7282   ;; Make this the current dead summary.
7283   (setq gnus-dead-summary (current-buffer))
7284   (gnus-dead-summary-mode 1)
7285   (let ((name (buffer-name)))
7286     (when (string-match "Summary" name)
7287       (rename-buffer
7288        (concat (substring name 0 (match-beginning 0)) "Dead "
7289                (substring name (match-beginning 0)))
7290        t)
7291       (bury-buffer))))
7292
7293 (defun gnus-kill-or-deaden-summary (buffer)
7294   "Kill or deaden the summary BUFFER."
7295   (save-excursion
7296     (when (and (buffer-name buffer)
7297                (not gnus-single-article-buffer))
7298       (with-current-buffer buffer
7299         (gnus-kill-buffer gnus-article-buffer)
7300         (gnus-kill-buffer gnus-original-article-buffer)))
7301     (cond
7302      ;; Kill the buffer.
7303      (gnus-kill-summary-on-exit
7304       (when (and gnus-use-trees
7305                  (gnus-buffer-exists-p buffer))
7306         (with-current-buffer buffer
7307           (gnus-tree-close gnus-newsgroup-name)))
7308       (gnus-kill-buffer buffer))
7309      ;; Deaden the buffer.
7310      ((gnus-buffer-exists-p buffer)
7311       (with-current-buffer buffer
7312         (gnus-deaden-summary))))))
7313
7314 (defun gnus-summary-wake-up-the-dead (&rest args)
7315   "Wake up the dead summary buffer."
7316   (interactive)
7317   (gnus-dead-summary-mode -1)
7318   (let ((name (buffer-name)))
7319     (when (string-match "Dead " name)
7320       (rename-buffer
7321        (concat (substring name 0 (match-beginning 0))
7322                (substring name (match-end 0)))
7323        t)))
7324   (gnus-message 3 "This dead summary is now alive again"))
7325
7326 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7327 (defun gnus-summary-describe-group (&optional force)
7328   "Describe the current newsgroup."
7329   (interactive "P")
7330   (gnus-group-describe-group force gnus-newsgroup-name))
7331
7332 (defun gnus-summary-describe-briefly ()
7333   "Describe summary mode commands briefly."
7334   (interactive)
7335   (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")))
7336
7337 ;; Walking around group mode buffer from summary mode.
7338
7339 (defun gnus-summary-next-group (&optional no-article target-group backward)
7340   "Exit current newsgroup and then select next unread newsgroup.
7341 If prefix argument NO-ARTICLE is non-nil, no article is selected
7342 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7343 previous group instead."
7344   (interactive "P")
7345   ;; Stop pre-fetching.
7346   (gnus-async-halt-prefetch)
7347   (let ((current-group gnus-newsgroup-name)
7348         (current-buffer (current-buffer))
7349         entered)
7350     ;; First we semi-exit this group to update Xrefs and all variables.
7351     ;; We can't do a real exit, because the window conf must remain
7352     ;; the same in case the user is prompted for info, and we don't
7353     ;; want the window conf to change before that...
7354     (gnus-summary-exit t)
7355     (while (not entered)
7356       ;; Then we find what group we are supposed to enter.
7357       (set-buffer gnus-group-buffer)
7358       (gnus-group-jump-to-group current-group)
7359       (setq target-group
7360             (or target-group
7361                 (if (eq gnus-keep-same-level 'best)
7362                     (gnus-summary-best-group gnus-newsgroup-name)
7363                   (gnus-summary-search-group backward gnus-keep-same-level))))
7364       (if (not target-group)
7365           ;; There are no further groups, so we return to the group
7366           ;; buffer.
7367           (progn
7368             (gnus-message 5 "Returning to the group buffer")
7369             (setq entered t)
7370             (when (gnus-buffer-live-p current-buffer)
7371               (set-buffer current-buffer)
7372               (gnus-summary-exit))
7373             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7374         ;; We try to enter the target group.
7375         (gnus-group-jump-to-group target-group)
7376         (let ((unreads (gnus-group-group-unread)))
7377           (if (and (or (eq t unreads)
7378                        (and unreads (not (zerop unreads))))
7379                    (gnus-summary-read-group
7380                     target-group nil no-article
7381                     (and (buffer-name current-buffer) current-buffer)
7382                     nil backward))
7383               (setq entered t)
7384             (setq current-group target-group
7385                   target-group nil)))))))
7386
7387 (defun gnus-summary-prev-group (&optional no-article)
7388   "Exit current newsgroup and then select previous unread newsgroup.
7389 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7390   (interactive "P")
7391   (gnus-summary-next-group no-article nil t))
7392
7393 ;; Walking around summary lines.
7394
7395 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7396   "Go to the first subject satisfying any non-nil constraint.
7397 If UNREAD is non-nil, the article should be unread.
7398 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7399 If UNSEEN is non-nil, the article should be unseen as well as unread.
7400 Returns the article selected or nil if there are no matching articles."
7401   (interactive "P")
7402   (cond
7403    ;; Empty summary.
7404    ((null gnus-newsgroup-data)
7405     (gnus-message 3 "No articles in the group")
7406     nil)
7407    ;; Pick the first article.
7408    ((not (or unread undownloaded unseen))
7409     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7410     (gnus-data-number (car gnus-newsgroup-data)))
7411    ;; Find the first unread article.
7412    (t
7413     (let ((data gnus-newsgroup-data))
7414       (while (and data
7415                   (let ((num (gnus-data-number (car data))))
7416                     (or (memq num gnus-newsgroup-unfetched)
7417                         (not (or (and unread
7418                                       (memq num gnus-newsgroup-unreads))
7419                                  (and undownloaded
7420                                       (memq num gnus-newsgroup-undownloaded))
7421                                  (and unseen
7422                                       (memq num gnus-newsgroup-unseen)
7423                                       (memq num gnus-newsgroup-unreads)))))))
7424         (setq data (cdr data)))
7425       (prog1
7426           (if data
7427               (progn
7428                 (goto-char (gnus-data-pos (car data)))
7429                 (gnus-data-number (car data)))
7430             (gnus-message 3 "No more%s articles"
7431                           (let* ((r (when unread " unread"))
7432                                  (d (when undownloaded " undownloaded"))
7433                                  (s (when unseen " unseen"))
7434                                  (l (delq nil (list r d s))))
7435                             (cond ((= 3 (length l))
7436                                    (concat r "," d ", or" s))
7437                                   ((= 2 (length l))
7438                                    (concat (car l) ", or" (cadr l)))
7439                                   ((= 1 (length l))
7440                                    (car l))
7441                                   (t
7442                                    ""))))
7443             nil
7444             )
7445         (gnus-summary-position-point))))))
7446
7447 (defun gnus-summary-next-subject (n &optional unread dont-display)
7448   "Go to next N'th summary line.
7449 If N is negative, go to the previous N'th subject line.
7450 If UNREAD is non-nil, only unread articles are selected.
7451 The difference between N and the actual number of steps taken is
7452 returned."
7453   (interactive "p")
7454   (let ((backward (< n 0))
7455         (n (abs n)))
7456     (while (and (> n 0)
7457                 (if backward
7458                     (gnus-summary-find-prev unread)
7459                   (gnus-summary-find-next unread)))
7460       (unless (zerop (setq n (1- n)))
7461         (gnus-summary-show-thread)))
7462     (when (/= 0 n)
7463       (gnus-message 7 "No more%s articles"
7464                     (if unread " unread" "")))
7465     (unless dont-display
7466       (gnus-summary-recenter)
7467       (gnus-summary-position-point))
7468     n))
7469
7470 (defun gnus-summary-next-unread-subject (n)
7471   "Go to next N'th unread summary line."
7472   (interactive "p")
7473   (gnus-summary-next-subject n t))
7474
7475 (defun gnus-summary-prev-subject (n &optional unread)
7476   "Go to previous N'th summary line.
7477 If optional argument UNREAD is non-nil, only unread article is selected."
7478   (interactive "p")
7479   (gnus-summary-next-subject (- n) unread))
7480
7481 (defun gnus-summary-prev-unread-subject (n)
7482   "Go to previous N'th unread summary line."
7483   (interactive "p")
7484   (gnus-summary-next-subject (- n) t))
7485
7486 (defun gnus-summary-goto-subjects (articles)
7487   "Insert the subject header for ARTICLES in the current buffer."
7488   (save-excursion
7489     (dolist (article articles)
7490       (gnus-summary-goto-subject article t)))
7491   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7492   (gnus-summary-position-point))
7493
7494 (defun gnus-summary-goto-subject (article &optional force silent)
7495   "Go to the subject line of ARTICLE.
7496 If FORCE, also allow jumping to articles not currently shown."
7497   (interactive "nArticle number: ")
7498   (unless (numberp article)
7499     (error "Article %s is not a number" article))
7500   (let ((b (point))
7501         (data (gnus-data-find article)))
7502     ;; We read in the article if we have to.
7503     (and (not data)
7504          force
7505          (gnus-summary-insert-subject
7506           article
7507           (if (or (numberp force) (vectorp force)) force)
7508           t)
7509          (setq data (gnus-data-find article)))
7510     (goto-char b)
7511     (if (not data)
7512         (progn
7513           (unless silent
7514             (gnus-message 3 "Can't find article %d" article))
7515           nil)
7516       (let ((pt (gnus-data-pos data)))
7517         (goto-char pt)
7518         (gnus-summary-set-article-display-arrow pt))
7519       (gnus-summary-position-point)
7520       article)))
7521
7522 ;; Walking around summary lines with displaying articles.
7523
7524 (defun gnus-summary-expand-window (&optional arg)
7525   "Make the summary buffer take up the entire Emacs frame.
7526 Given a prefix, will force an `article' buffer configuration."
7527   (interactive "P")
7528   (if arg
7529       (gnus-configure-windows 'article 'force)
7530     (gnus-configure-windows 'summary 'force)))
7531
7532 (defun gnus-summary-display-article (article &optional all-header)
7533   "Display ARTICLE in article buffer."
7534   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7535                (with-current-buffer gnus-article-buffer
7536                  (eq major-mode 'gnus-article-mode)))
7537     (gnus-article-setup-buffer))
7538   (gnus-set-global-variables)
7539   (with-current-buffer gnus-article-buffer
7540     (setq gnus-article-charset gnus-newsgroup-charset)
7541     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7542     (mm-enable-multibyte))
7543   (if (null article)
7544       nil
7545     (prog1
7546         (if gnus-summary-display-article-function
7547             (funcall gnus-summary-display-article-function article all-header)
7548           (gnus-article-prepare article all-header))
7549       (gnus-run-hooks 'gnus-select-article-hook)
7550       (when (and gnus-current-article
7551                  (not (zerop gnus-current-article)))
7552         (gnus-summary-goto-subject gnus-current-article))
7553       (gnus-summary-recenter)
7554       (when (and gnus-use-trees gnus-show-threads)
7555         (gnus-possibly-generate-tree article)
7556         (gnus-highlight-selected-tree article))
7557       ;; Successfully display article.
7558       (gnus-article-set-window-start
7559        (cdr (assq article gnus-newsgroup-bookmarks))))))
7560
7561 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7562   "Select the current article.
7563 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7564 non-nil, the article will be re-fetched even if it already present in
7565 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7566 be displayed."
7567   ;; Make sure we are in the summary buffer to work around bbdb bug.
7568   (unless (eq major-mode 'gnus-summary-mode)
7569     (set-buffer gnus-summary-buffer))
7570   (let ((article (or article (gnus-summary-article-number)))
7571         (all-headers (not (not all-headers))) ;Must be t or nil.
7572         gnus-summary-display-article-function)
7573     (and (not pseudo)
7574          (gnus-summary-article-pseudo-p article)
7575          (error "This is a pseudo-article"))
7576     (with-current-buffer gnus-summary-buffer
7577       (if (or (and gnus-single-article-buffer
7578                    (or (null gnus-current-article)
7579                        (null gnus-article-current)
7580                        (null (get-buffer gnus-article-buffer))
7581                        (not (eq article (cdr gnus-article-current)))
7582                        (not (equal (car gnus-article-current)
7583                                    gnus-newsgroup-name))))
7584               (and (not gnus-single-article-buffer)
7585                    (or (null gnus-current-article)
7586                        (not (eq gnus-current-article article))))
7587               force)
7588           ;; The requested article is different from the current article.
7589           (progn
7590             (gnus-summary-display-article article all-headers)
7591             (when (gnus-buffer-live-p gnus-article-buffer)
7592               (with-current-buffer gnus-article-buffer
7593                 (if (not gnus-article-decoded-p) ;; a local variable
7594                     (mm-disable-multibyte))))
7595             (gnus-article-set-window-start
7596              (cdr (assq article gnus-newsgroup-bookmarks)))
7597             article)
7598         'old))))
7599
7600 (defun gnus-summary-force-verify-and-decrypt ()
7601   "Display buttons for signed/encrypted parts and verify/decrypt them."
7602   (interactive)
7603   (let ((mm-verify-option 'known)
7604         (mm-decrypt-option 'known)
7605         (gnus-article-emulate-mime t)
7606         (gnus-buttonized-mime-types (append (list "multipart/signed"
7607                                                   "multipart/encrypted")
7608                                             gnus-buttonized-mime-types)))
7609     (gnus-summary-select-article nil 'force)))
7610
7611 (defun gnus-summary-set-current-mark (&optional current-mark)
7612   "Obsolete function."
7613   nil)
7614
7615 (defun gnus-summary-next-article (&optional unread subject backward push)
7616   "Select the next article.
7617 If UNREAD, only unread articles are selected.
7618 If SUBJECT, only articles with SUBJECT are selected.
7619 If BACKWARD, the previous article is selected instead of the next."
7620   (interactive "P")
7621   ;; Make sure we are in the summary buffer.
7622   (unless (eq major-mode 'gnus-summary-mode)
7623     (set-buffer gnus-summary-buffer))
7624   (cond
7625    ;; Is there such an article?
7626    ((and (gnus-summary-search-forward unread subject backward)
7627          (or (gnus-summary-display-article (gnus-summary-article-number))
7628              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7629     (gnus-summary-position-point))
7630    ;; If not, we try the first unread, if that is wanted.
7631    ((and subject
7632          gnus-auto-select-same
7633          (gnus-summary-first-unread-article))
7634     (gnus-summary-position-point)
7635     (gnus-message 6 "Wrapped"))
7636    ;; Try to get next/previous article not displayed in this group.
7637    ((and gnus-auto-extend-newsgroup
7638          (not unread) (not subject))
7639     (gnus-summary-goto-article
7640      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7641      nil (count-lines (point-min) (point))))
7642    ;; Go to next/previous group.
7643    (t
7644     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7645       (gnus-summary-jump-to-group gnus-newsgroup-name))
7646     (let ((cmd (if (featurep 'xemacs)
7647                    last-command-char
7648                  last-command-event))
7649           (point
7650            (with-current-buffer gnus-group-buffer
7651              (point)))
7652           (group
7653            (if (eq gnus-keep-same-level 'best)
7654                (gnus-summary-best-group gnus-newsgroup-name)
7655              (gnus-summary-search-group backward gnus-keep-same-level))))
7656       ;; For some reason, the group window gets selected.  We change
7657       ;; it back.
7658       (select-window (get-buffer-window (current-buffer)))
7659       ;; Select next unread newsgroup automagically.
7660       (cond
7661        ((or (not gnus-auto-select-next)
7662             (not cmd))
7663         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7664        ((or (eq gnus-auto-select-next 'quietly)
7665             (and (eq gnus-auto-select-next 'slightly-quietly)
7666                  push)
7667             (and (eq gnus-auto-select-next 'almost-quietly)
7668                  (gnus-summary-last-article-p)))
7669         ;; Select quietly.
7670         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7671             (gnus-summary-exit)
7672           (gnus-message 7 "No more%s articles (%s)..."
7673                         (if unread " unread" "")
7674                         (if group (concat "selecting " group)
7675                           "exiting"))
7676           (gnus-summary-next-group nil group backward)))
7677        (t
7678         (when (gnus-key-press-event-p last-input-event)
7679           (gnus-summary-walk-group-buffer
7680            gnus-newsgroup-name cmd unread backward point))))))))
7681
7682 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7683   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7684                       (?\C-p (gnus-group-prev-unread-group 1))))
7685         (cursor-in-echo-area t)
7686         keve key group ended prompt)
7687     (with-current-buffer gnus-group-buffer
7688       (goto-char start)
7689       (setq group
7690             (if (eq gnus-keep-same-level 'best)
7691                 (gnus-summary-best-group gnus-newsgroup-name)
7692               (gnus-summary-search-group backward gnus-keep-same-level))))
7693     (while (not ended)
7694       (setq prompt
7695             (format
7696              "No more%s articles%s " (if unread " unread" "")
7697              (if (and group
7698                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7699                  (format " (Type %s for %s [%s])"
7700                          (single-key-description cmd)
7701                          (gnus-group-decoded-name group)
7702                          (gnus-group-unread group))
7703                (format " (Type %s to exit %s)"
7704                        (single-key-description cmd)
7705                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7706       ;; Confirm auto selection.
7707       (setq key (car (setq keve (gnus-read-event-char prompt)))
7708             ended t)
7709       (cond
7710        ((assq key keystrokes)
7711         (let ((obuf (current-buffer)))
7712           (switch-to-buffer gnus-group-buffer)
7713           (when group
7714             (gnus-group-jump-to-group group))
7715           (eval (cadr (assq key keystrokes)))
7716           (setq group (gnus-group-group-name))
7717           (switch-to-buffer obuf))
7718         (setq ended nil))
7719        ((equal key cmd)
7720         (if (or (not group)
7721                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7722             (gnus-summary-exit)
7723           (gnus-summary-next-group nil group backward)))
7724        (t
7725         (push (cdr keve) unread-command-events))))))
7726
7727 (defun gnus-summary-next-unread-article ()
7728   "Select unread article after current one."
7729   (interactive)
7730   (gnus-summary-next-article
7731    (or (not (eq gnus-summary-goto-unread 'never))
7732        (gnus-summary-last-article-p (gnus-summary-article-number)))
7733    (and gnus-auto-select-same
7734         (gnus-summary-article-subject))))
7735
7736 (defun gnus-summary-prev-article (&optional unread subject)
7737   "Select the article before the current one.
7738 If UNREAD is non-nil, only unread articles are selected."
7739   (interactive "P")
7740   (gnus-summary-next-article unread subject t))
7741
7742 (defun gnus-summary-prev-unread-article ()
7743   "Select unread article before current one."
7744   (interactive)
7745   (gnus-summary-prev-article
7746    (or (not (eq gnus-summary-goto-unread 'never))
7747        (gnus-summary-first-article-p (gnus-summary-article-number)))
7748    (and gnus-auto-select-same
7749         (gnus-summary-article-subject))))
7750
7751 (defun gnus-summary-next-page (&optional lines circular stop)
7752   "Show next page of the selected article.
7753 If at the end of the current article, select the next article.
7754 LINES says how many lines should be scrolled up.
7755
7756 If CIRCULAR is non-nil, go to the start of the article instead of
7757 selecting the next article when reaching the end of the current
7758 article.
7759
7760 If STOP is non-nil, just stop when reaching the end of the message.
7761
7762 Also see the variable `gnus-article-skip-boring'."
7763   (interactive "P")
7764   (setq gnus-summary-buffer (current-buffer))
7765   (gnus-set-global-variables)
7766   (let ((article (gnus-summary-article-number))
7767         (article-window (get-buffer-window gnus-article-buffer t))
7768         endp)
7769     ;; If the buffer is empty, we have no article.
7770     (unless article
7771       (error "No article to select"))
7772     (gnus-configure-windows 'article)
7773     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7774         (if (and (eq gnus-summary-goto-unread 'never)
7775                  (not (gnus-summary-last-article-p article)))
7776             (gnus-summary-next-article)
7777           (gnus-summary-next-unread-article))
7778       (if (or (null gnus-current-article)
7779               (null gnus-article-current)
7780               (/= article (cdr gnus-article-current))
7781               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7782           ;; Selected subject is different from current article's.
7783           (gnus-summary-display-article article)
7784         (when article-window
7785           (gnus-eval-in-buffer-window gnus-article-buffer
7786             (setq endp (or (gnus-article-next-page lines)
7787                            (gnus-article-only-boring-p))))
7788           (when endp
7789             (cond ((or stop gnus-summary-stop-at-end-of-message)
7790                    (gnus-message 3 "End of message"))
7791                   (circular
7792                    (gnus-summary-beginning-of-article))
7793                   (lines
7794                    (gnus-message 3 "End of message"))
7795                   ((null lines)
7796                    (if (and (eq gnus-summary-goto-unread 'never)
7797                             (not (gnus-summary-last-article-p article)))
7798                        (gnus-summary-next-article)
7799                      (gnus-summary-next-unread-article))))))))
7800     (gnus-summary-recenter)
7801     (gnus-summary-position-point)))
7802
7803 (defun gnus-summary-prev-page (&optional lines move)
7804   "Show previous page of selected article.
7805 Argument LINES specifies lines to be scrolled down.
7806 If MOVE, move to the previous unread article if point is at
7807 the beginning of the buffer."
7808   (interactive "P")
7809   (let ((article (gnus-summary-article-number))
7810         (article-window (get-buffer-window gnus-article-buffer t))
7811         endp)
7812     (gnus-configure-windows 'article)
7813     (if (or (null gnus-current-article)
7814             (null gnus-article-current)
7815             (/= article (cdr gnus-article-current))
7816             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7817         ;; Selected subject is different from current article's.
7818         (gnus-summary-display-article article)
7819       (gnus-summary-recenter)
7820       (when article-window
7821         (gnus-eval-in-buffer-window gnus-article-buffer
7822           (setq endp (gnus-article-prev-page lines)))
7823         (when (and move endp)
7824           (cond (lines
7825                  (gnus-message 3 "Beginning of message"))
7826                 ((null lines)
7827                  (if (and (eq gnus-summary-goto-unread 'never)
7828                           (not (gnus-summary-first-article-p article)))
7829                      (gnus-summary-prev-article)
7830                    (gnus-summary-prev-unread-article))))))))
7831   (gnus-summary-position-point))
7832
7833 (defun gnus-summary-prev-page-or-article (&optional lines)
7834   "Show previous page of selected article.
7835 Argument LINES specifies lines to be scrolled down.
7836 If at the beginning of the article, go to the next article."
7837   (interactive "P")
7838   (gnus-summary-prev-page lines t))
7839
7840 (defun gnus-summary-scroll-up (lines)
7841   "Scroll up (or down) one line current article.
7842 Argument LINES specifies lines to be scrolled up (or down if negative)."
7843   (interactive "p")
7844   (gnus-configure-windows 'article)
7845   (gnus-summary-show-thread)
7846   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7847     (gnus-eval-in-buffer-window gnus-article-buffer
7848       (cond ((> lines 0)
7849              (when (gnus-article-next-page lines)
7850                (gnus-message 3 "End of message")))
7851             ((< lines 0)
7852              (gnus-article-prev-page (- lines))))))
7853   (gnus-summary-recenter)
7854   (gnus-summary-position-point))
7855
7856 (defun gnus-summary-scroll-down (lines)
7857   "Scroll down (or up) one line current article.
7858 Argument LINES specifies lines to be scrolled down (or up if negative)."
7859   (interactive "p")
7860   (gnus-summary-scroll-up (- lines)))
7861
7862 (defun gnus-summary-next-same-subject ()
7863   "Select next article which has the same subject as current one."
7864   (interactive)
7865   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7866
7867 (defun gnus-summary-prev-same-subject ()
7868   "Select previous article which has the same subject as current one."
7869   (interactive)
7870   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7871
7872 (defun gnus-summary-next-unread-same-subject ()
7873   "Select next unread article which has the same subject as current one."
7874   (interactive)
7875   (gnus-summary-next-article t (gnus-summary-article-subject)))
7876
7877 (defun gnus-summary-prev-unread-same-subject ()
7878   "Select previous unread article which has the same subject as current one."
7879   (interactive)
7880   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7881
7882 (defun gnus-summary-first-unread-article ()
7883   "Select the first unread article.
7884 Return nil if there are no unread articles."
7885   (interactive)
7886   (prog1
7887       (when (gnus-summary-first-subject t)
7888         (gnus-summary-show-thread)
7889         (gnus-summary-first-subject t)
7890         (gnus-summary-display-article (gnus-summary-article-number)))
7891     (gnus-summary-position-point)))
7892
7893 (defun gnus-summary-first-unread-subject ()
7894   "Place the point on the subject line of the first unread article.
7895 Return nil if there are no unread articles."
7896   (interactive)
7897   (prog1
7898       (when (gnus-summary-first-subject t)
7899         (gnus-summary-show-thread)
7900         (gnus-summary-first-subject t))
7901     (gnus-summary-position-point)))
7902
7903 (defun gnus-summary-first-unseen-subject ()
7904   "Place the point on the subject line of the first unseen article.
7905 Return nil if there are no unseen articles."
7906   (interactive)
7907   (prog1
7908       (when (gnus-summary-first-subject nil nil t)
7909         (gnus-summary-show-thread)
7910         (gnus-summary-first-subject nil nil t))
7911     (gnus-summary-position-point)))
7912
7913 (defun gnus-summary-first-unseen-or-unread-subject ()
7914   "Place the point on the subject line of the first unseen and unread article.
7915 If all article have been seen, on the subject line of the first unread
7916 article."
7917   (interactive)
7918   (prog1
7919       (unless (when (gnus-summary-first-subject nil nil t)
7920                 (gnus-summary-show-thread)
7921                 (gnus-summary-first-subject nil nil t))
7922         (when (gnus-summary-first-subject t)
7923           (gnus-summary-show-thread)
7924           (gnus-summary-first-subject t)))
7925     (gnus-summary-position-point)))
7926
7927 (defun gnus-summary-first-article ()
7928   "Select the first article.
7929 Return nil if there are no articles."
7930   (interactive)
7931   (prog1
7932       (when (gnus-summary-first-subject)
7933         (gnus-summary-show-thread)
7934         (gnus-summary-first-subject)
7935         (gnus-summary-display-article (gnus-summary-article-number)))
7936     (gnus-summary-position-point)))
7937
7938 (defun gnus-summary-best-unread-article (&optional arg)
7939   "Select the unread article with the highest score.
7940 If given a prefix argument, select the next unread article that has a
7941 score higher than the default score."
7942   (interactive "P")
7943   (let ((article (if arg
7944                      (gnus-summary-better-unread-subject)
7945                    (gnus-summary-best-unread-subject))))
7946     (if article
7947         (gnus-summary-goto-article article)
7948       (error "No unread articles"))))
7949
7950 (defun gnus-summary-best-unread-subject ()
7951   "Select the unread subject with the highest score."
7952   (interactive)
7953   (let ((best -1000000)
7954         (data gnus-newsgroup-data)
7955         article score)
7956     (while data
7957       (and (gnus-data-unread-p (car data))
7958            (> (setq score
7959                     (gnus-summary-article-score (gnus-data-number (car data))))
7960               best)
7961            (setq best score
7962                  article (gnus-data-number (car data))))
7963       (setq data (cdr data)))
7964     (when article
7965       (gnus-summary-goto-subject article))
7966     (gnus-summary-position-point)
7967     article))
7968
7969 (defun gnus-summary-better-unread-subject ()
7970   "Select the first unread subject that has a score over the default score."
7971   (interactive)
7972   (let ((data gnus-newsgroup-data)
7973         article score)
7974     (while (and (setq article (gnus-data-number (car data)))
7975                 (or (gnus-data-read-p (car data))
7976                     (not (> (gnus-summary-article-score article)
7977                             gnus-summary-default-score))))
7978       (setq data (cdr data)))
7979     (when article
7980       (gnus-summary-goto-subject article))
7981     (gnus-summary-position-point)
7982     article))
7983
7984 (defun gnus-summary-last-subject ()
7985   "Go to the last displayed subject line in the group."
7986   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7987     (when article
7988       (gnus-summary-goto-subject article))))
7989
7990 (defun gnus-summary-goto-article (article &optional all-headers force)
7991   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7992 If ALL-HEADERS is non-nil, no header lines are hidden.
7993 If FORCE, go to the article even if it isn't displayed.  If FORCE
7994 is a number, it is the line the article is to be displayed on."
7995   (interactive
7996    (list
7997     (completing-read
7998      "Article number or Message-ID: "
7999      (mapcar (lambda (number) (list (int-to-string number)))
8000              gnus-newsgroup-limit))
8001     current-prefix-arg
8002     t))
8003   (prog1
8004       (if (and (stringp article)
8005                (string-match "@\\|%40" article))
8006           (gnus-summary-refer-article article)
8007         (when (stringp article)
8008           (setq article (string-to-number article)))
8009         (if (gnus-summary-goto-subject article force)
8010             (gnus-summary-display-article article all-headers)
8011           (gnus-message 4 "Couldn't go to article %s" article) nil))
8012     (gnus-summary-position-point)))
8013
8014 (defun gnus-summary-goto-last-article ()
8015   "Go to the previously read article."
8016   (interactive)
8017   (prog1
8018       (when gnus-last-article
8019         (gnus-summary-goto-article gnus-last-article nil t))
8020     (gnus-summary-position-point)))
8021
8022 (defun gnus-summary-pop-article (number)
8023   "Pop one article off the history and go to the previous.
8024 NUMBER articles will be popped off."
8025   (interactive "p")
8026   (let (to)
8027     (setq gnus-newsgroup-history
8028           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8029     (if to
8030         (gnus-summary-goto-article (car to) nil t)
8031       (error "Article history empty")))
8032   (gnus-summary-position-point))
8033
8034 ;; Summary commands and functions for limiting the summary buffer.
8035
8036 (defun gnus-summary-limit-to-articles (n)
8037   "Limit the summary buffer to the next N articles.
8038 If not given a prefix, use the process marked articles instead."
8039   (interactive "P")
8040   (prog1
8041       (let ((articles (gnus-summary-work-articles n)))
8042         (setq gnus-newsgroup-processable nil)
8043         (gnus-summary-limit articles))
8044     (gnus-summary-position-point)))
8045
8046 (defun gnus-summary-pop-limit (&optional total)
8047   "Restore the previous limit.
8048 If given a prefix, remove all limits."
8049   (interactive "P")
8050   (when total
8051     (setq gnus-newsgroup-limits
8052           (list (mapcar (lambda (h) (mail-header-number h))
8053                         gnus-newsgroup-headers))))
8054   (unless gnus-newsgroup-limits
8055     (error "No limit to pop"))
8056   (prog1
8057       (gnus-summary-limit nil 'pop)
8058     (gnus-summary-position-point)))
8059
8060 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8061   "Limit the summary buffer to articles that have subjects that match a regexp.
8062 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8063   (interactive
8064    (list (read-string (if current-prefix-arg
8065                           "Exclude subject (regexp): "
8066                         "Limit to subject (regexp): "))
8067          nil current-prefix-arg))
8068   (unless header
8069     (setq header "subject"))
8070   (when (not (equal "" subject))
8071     (prog1
8072         (let ((articles (gnus-summary-find-matching
8073                          (or header "subject") subject 'all nil nil
8074                          not-matching)))
8075           (unless articles
8076             (error "Found no matches for \"%s\"" subject))
8077           (gnus-summary-limit articles))
8078       (gnus-summary-position-point))))
8079
8080 (defun gnus-summary-limit-to-author (from &optional not-matching)
8081   "Limit the summary buffer to articles that have authors that match a regexp.
8082 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8083   (interactive
8084    (list (read-string (if current-prefix-arg
8085                           "Exclude author (regexp): "
8086                         "Limit to author (regexp): "))
8087          current-prefix-arg))
8088   (gnus-summary-limit-to-subject from "from" not-matching))
8089
8090 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8091   "Limit the summary buffer to articles with the given RECIPIENT.
8092
8093 If NOT-MATCHING, exclude RECIPIENT.
8094
8095 To and Cc headers are checked.  You need to include them in
8096 `nnmail-extra-headers'."
8097   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8098   (interactive
8099    (list (read-string (format "%s recipient (regexp): "
8100                               (if current-prefix-arg "Exclude" "Limit to")))
8101          current-prefix-arg))
8102   (when (not (equal "" recipient))
8103     (prog1 (let* ((to
8104                    (if (memq 'To nnmail-extra-headers)
8105                        (gnus-summary-find-matching
8106                         (cons 'extra 'To) recipient 'all nil nil
8107                         not-matching)
8108                      (gnus-message
8109                       1 "`To' isn't present in `nnmail-extra-headers'")
8110                      (sit-for 1)
8111                      nil))
8112                   (cc
8113                    (if (memq 'Cc nnmail-extra-headers)
8114                        (gnus-summary-find-matching
8115                         (cons 'extra 'Cc) recipient 'all nil nil
8116                         not-matching)
8117                      (gnus-message
8118                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8119                      (sit-for 1)
8120                      nil))
8121                   (articles
8122                    (if not-matching
8123                        ;; We need the numbers that are in both lists:
8124                        (mapcar (lambda (a)
8125                                  (and (memq a to) a))
8126                                cc)
8127                      (nconc to cc))))
8128              (unless articles
8129                (error "Found no matches for \"%s\"" recipient))
8130              (gnus-summary-limit articles))
8131       (gnus-summary-position-point))))
8132
8133 (defun gnus-summary-limit-to-address (address &optional not-matching)
8134   "Limit the summary buffer to articles with the given ADDRESS.
8135
8136 If NOT-MATCHING, exclude ADDRESS.
8137
8138 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8139 in `nnmail-extra-headers'."
8140   (interactive
8141    (list (read-string (format "%s address (regexp): "
8142                               (if current-prefix-arg "Exclude" "Limit to")))
8143          current-prefix-arg))
8144   (when (not (equal "" address))
8145     (prog1 (let* ((to
8146                    (if (memq 'To nnmail-extra-headers)
8147                        (gnus-summary-find-matching
8148                         (cons 'extra 'To) address 'all nil nil
8149                         not-matching)
8150                      (gnus-message
8151                       1 "`To' isn't present in `nnmail-extra-headers'")
8152                      (sit-for 1)
8153                      t))
8154                   (cc
8155                    (if (memq 'Cc nnmail-extra-headers)
8156                        (gnus-summary-find-matching
8157                         (cons 'extra 'Cc) address 'all nil nil
8158                         not-matching)
8159                      (gnus-message
8160                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8161                      (sit-for 1)
8162                      t))
8163                   (from
8164                    (gnus-summary-find-matching "from" address
8165                                                'all nil nil not-matching))
8166                   (articles
8167                    (if not-matching
8168                        ;; We need the numbers that are in all lists:
8169                        (if (eq cc t)
8170                            (if (eq to t)
8171                                from
8172                              (mapcar (lambda (a) (car (memq a from))) to))
8173                          (if (eq to t)
8174                              (mapcar (lambda (a) (car (memq a from))) cc)
8175                            (mapcar (lambda (a) (car (memq a from)))
8176                                    (mapcar (lambda (a) (car (memq a to)))
8177                                            cc))))
8178                      (nconc (if (eq to t) nil to)
8179                             (if (eq cc t) nil cc)
8180                             from))))
8181              (unless articles
8182                (error "Found no matches for \"%s\"" address))
8183              (gnus-summary-limit articles))
8184       (gnus-summary-position-point))))
8185
8186 (defun gnus-summary-limit-strange-charsets-predicate (header)
8187   (when (fboundp 'char-charset)
8188     (let ((string (concat (mail-header-subject header)
8189                           (mail-header-from header)))
8190           charset found)
8191       (dotimes (i (1- (length string)))
8192         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8193         (when (string-match "unicode\\|big\\|japanese" charset)
8194           (setq found t)))
8195       found)))
8196
8197 (defun gnus-summary-limit-to-predicate (predicate)
8198   "Limit to articles where PREDICATE returns non-nil.
8199 PREDICATE will be called with the header structures of the
8200 articles."
8201   (let ((articles nil)
8202         (case-fold-search t))
8203     (dolist (header gnus-newsgroup-headers)
8204       (when (funcall predicate header)
8205         (push (mail-header-number header) articles)))
8206     (gnus-summary-limit (nreverse articles))))
8207
8208 (defun gnus-summary-limit-to-age (age &optional younger-p)
8209   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8210 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8211 articles that are younger than AGE days."
8212   (interactive
8213    (let ((younger current-prefix-arg)
8214          (days-got nil)
8215          days)
8216      (while (not days-got)
8217        (setq days (if younger
8218                       (read-string "Limit to articles younger than (in days, older when negative): ")
8219                     (read-string
8220                      "Limit to articles older than (in days, younger when negative): ")))
8221        (when (> (length days) 0)
8222          (setq days (read days)))
8223        (if (numberp days)
8224            (progn
8225              (setq days-got t)
8226              (when (< days 0)
8227                (setq younger (not younger))
8228                (setq days (* days -1))))
8229          (message "Please enter a number.")
8230          (sleep-for 1)))
8231      (list days younger)))
8232   (prog1
8233       (let ((data gnus-newsgroup-data)
8234             (cutoff (days-to-time age))
8235             articles d date is-younger)
8236         (while (setq d (pop data))
8237           (when (and (vectorp (gnus-data-header d))
8238                      (setq date (mail-header-date (gnus-data-header d))))
8239             (setq is-younger (time-less-p
8240                               (time-since (gnus-date-get-time date))
8241                               cutoff))
8242             (when (if younger-p
8243                       is-younger
8244                     (not is-younger))
8245               (push (gnus-data-number d) articles))))
8246         (gnus-summary-limit (nreverse articles)))
8247     (gnus-summary-position-point)))
8248
8249 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8250   "Limit the summary buffer to articles that match an 'extra' header."
8251   (interactive
8252    (let ((header
8253           (intern
8254            (gnus-completing-read-with-default
8255             (symbol-name (car gnus-extra-headers))
8256             (if current-prefix-arg
8257                 "Exclude extra header"
8258               "Limit extra header")
8259             (mapcar (lambda (x)
8260                       (cons (symbol-name x) x))
8261                     gnus-extra-headers)
8262             nil
8263             t))))
8264      (list header
8265            (read-string (format "%s header %s (regexp): "
8266                                 (if current-prefix-arg "Exclude" "Limit to")
8267                                 header))
8268            current-prefix-arg)))
8269   (when (not (equal "" regexp))
8270     (prog1
8271         (let ((articles (gnus-summary-find-matching
8272                          (cons 'extra header) regexp 'all nil nil
8273                          not-matching)))
8274           (unless articles
8275             (error "Found no matches for \"%s\"" regexp))
8276           (gnus-summary-limit articles))
8277       (gnus-summary-position-point))))
8278
8279 (defun gnus-summary-limit-to-display-predicate ()
8280   "Limit the summary buffer to the predicated in the `display' group parameter."
8281   (interactive)
8282   (unless gnus-newsgroup-display
8283     (error "There is no `display' group parameter"))
8284   (let (articles)
8285     (dolist (number gnus-newsgroup-articles)
8286       (when (funcall gnus-newsgroup-display)
8287         (push number articles)))
8288     (gnus-summary-limit articles))
8289   (gnus-summary-position-point))
8290
8291 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8292 (make-obsolete
8293  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8294
8295 (defun gnus-summary-limit-to-unread (&optional all)
8296   "Limit the summary buffer to articles that are not marked as read.
8297 If ALL is non-nil, limit strictly to unread articles."
8298   (interactive "P")
8299   (if all
8300       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8301     (gnus-summary-limit-to-marks
8302      ;; Concat all the marks that say that an article is read and have
8303      ;; those removed.
8304      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8305            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8306            gnus-low-score-mark gnus-expirable-mark
8307            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8308            gnus-duplicate-mark)
8309      'reverse)))
8310
8311 (defun gnus-summary-limit-to-headers (match &optional reverse)
8312   "Limit the summary buffer to articles that have headers that match MATCH.
8313 If REVERSE (the prefix), limit to articles that don't match."
8314   (interactive "sMatch headers (regexp): \nP")
8315   (gnus-summary-limit-to-bodies match reverse t))
8316
8317 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8318   "Limit the summary buffer to articles that have bodies that match MATCH.
8319 If REVERSE (the prefix), limit to articles that don't match."
8320   (interactive "sMatch body (regexp): \nP")
8321   (let ((articles nil)
8322         (gnus-select-article-hook nil)  ;Disable hook.
8323         (gnus-article-prepare-hook nil)
8324         (gnus-use-article-prefetch nil)
8325         (gnus-keep-backlog nil)
8326         (gnus-break-pages nil)
8327         (gnus-summary-display-arrow nil)
8328         (gnus-updated-mode-lines nil)
8329         (gnus-auto-center-summary nil)
8330         (gnus-display-mime-function nil))
8331     (dolist (data gnus-newsgroup-data)
8332       (let (gnus-mark-article-hook)
8333         (gnus-summary-select-article t t nil (gnus-data-number data)))
8334       (with-current-buffer gnus-article-buffer
8335         (article-goto-body)
8336         (let* ((case-fold-search t)
8337                (found (if headersp
8338                           (re-search-backward match nil t)
8339                         (re-search-forward match nil t))))
8340           (when (or (and found
8341                          (not reverse))
8342                     (and (not found)
8343                          reverse))
8344             (push (gnus-data-number data) articles)))))
8345     (if (not articles)
8346         (message "No messages matched")
8347       (gnus-summary-limit articles)))
8348   (gnus-summary-position-point))
8349
8350 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8351   "Limit the summary buffer to articles that aren't part on any thread.
8352 If THREADSP (the prefix), limit to articles that are in threads."
8353   (interactive "P")
8354   (let ((articles nil)
8355         thread-articles
8356         threads)
8357     (dolist (thread gnus-newsgroup-threads)
8358       (if (stringp (car thread))
8359           (dolist (thread (cdr thread))
8360             (push thread threads))
8361         (push thread threads)))
8362     (dolist (thread threads)
8363       (setq thread-articles (gnus-articles-in-thread thread))
8364       (when (or (and threadsp
8365                      (> (length thread-articles) 1))
8366                 (and (not threadsp)
8367                      (= (length thread-articles) 1)))
8368         (setq articles (nconc thread-articles articles))))
8369     (if (not articles)
8370         (message "No messages matched")
8371       (gnus-summary-limit articles))
8372     (gnus-summary-position-point)))
8373
8374 (defun gnus-summary-limit-to-replied (&optional unreplied)
8375   "Limit the summary buffer to replied articles.
8376 If UNREPLIED (the prefix), limit to unreplied articles."
8377   (interactive "P")
8378   (if unreplied
8379       (gnus-summary-limit
8380        (gnus-set-difference gnus-newsgroup-articles
8381         gnus-newsgroup-replied))
8382     (gnus-summary-limit gnus-newsgroup-replied))
8383   (gnus-summary-position-point))
8384
8385 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8386 (make-obsolete 'gnus-summary-delete-marked-with
8387                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8388
8389 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8390   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8391 If REVERSE, limit the summary buffer to articles that are marked
8392 with MARKS.  MARKS can either be a string of marks or a list of marks.
8393 Returns how many articles were removed."
8394   (interactive "sMarks: ")
8395   (gnus-summary-limit-to-marks marks t))
8396
8397 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8398   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8399 If REVERSE (the prefix), limit the summary buffer to articles that are
8400 not marked with MARKS.  MARKS can either be a string of marks or a
8401 list of marks.
8402 Returns how many articles were removed."
8403   (interactive "sMarks: \nP")
8404   (prog1
8405       (let ((data gnus-newsgroup-data)
8406             (marks (if (listp marks) marks
8407                      (append marks nil))) ; Transform to list.
8408             articles)
8409         (while data
8410           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8411                   (memq (gnus-data-mark (car data)) marks))
8412             (push (gnus-data-number (car data)) articles))
8413           (setq data (cdr data)))
8414         (gnus-summary-limit articles))
8415     (gnus-summary-position-point)))
8416
8417 (defun gnus-summary-limit-to-score (score)
8418   "Limit to articles with score at or above SCORE."
8419   (interactive "NLimit to articles with score of at least: ")
8420   (let ((data gnus-newsgroup-data)
8421         articles)
8422     (while data
8423       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8424                 score)
8425         (push (gnus-data-number (car data)) articles))
8426       (setq data (cdr data)))
8427     (prog1
8428         (gnus-summary-limit articles)
8429       (gnus-summary-position-point))))
8430
8431 (defun gnus-summary-limit-to-unseen ()
8432   "Limit to unseen articles."
8433   (interactive)
8434   (prog1
8435       (gnus-summary-limit gnus-newsgroup-unseen)
8436     (gnus-summary-position-point)))
8437
8438 (defun gnus-summary-limit-include-thread (id)
8439   "Display all the hidden articles that is in the thread with ID in it.
8440 When called interactively, ID is the Message-ID of the current
8441 article."
8442   (interactive (list (mail-header-id (gnus-summary-article-header))))
8443   (let ((articles (gnus-articles-in-thread
8444                    (gnus-id-to-thread (gnus-root-id id)))))
8445     (prog1
8446         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8447       (gnus-summary-limit-include-matching-articles
8448        "subject"
8449        (regexp-quote (gnus-simplify-subject-re
8450                       (mail-header-subject (gnus-id-to-header id)))))
8451       (gnus-summary-position-point))))
8452
8453 (defun gnus-summary-limit-include-matching-articles (header regexp)
8454   "Display all the hidden articles that have HEADERs that match REGEXP."
8455   (interactive (list (read-string "Match on header: ")
8456                      (read-string "Regexp: ")))
8457   (let ((articles (gnus-find-matching-articles header regexp)))
8458     (prog1
8459         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8460       (gnus-summary-position-point))))
8461
8462 (defun gnus-summary-insert-dormant-articles ()
8463   "Insert all the dormant articles for this group into the current buffer."
8464   (interactive)
8465   (let ((gnus-verbose (max 6 gnus-verbose)))
8466     (if (not gnus-newsgroup-dormant)
8467         (gnus-message 3 "No dormant articles for this group")
8468       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8469
8470 (defun gnus-summary-insert-ticked-articles ()
8471   "Insert ticked articles for this group into the current buffer."
8472   (interactive)
8473   (let ((gnus-verbose (max 6 gnus-verbose)))
8474     (if (not gnus-newsgroup-marked)
8475         (gnus-message 3 "No ticked articles for this group")
8476       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8477
8478 (defun gnus-summary-limit-include-dormant ()
8479   "Display all the hidden articles that are marked as dormant.
8480 Note that this command only works on a subset of the articles currently
8481 fetched for this group."
8482   (interactive)
8483   (unless gnus-newsgroup-dormant
8484     (error "There are no dormant articles in this group"))
8485   (prog1
8486       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8487     (gnus-summary-position-point)))
8488
8489 (defun gnus-summary-limit-exclude-dormant ()
8490   "Hide all dormant articles."
8491   (interactive)
8492   (prog1
8493       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8494     (gnus-summary-position-point)))
8495
8496 (defun gnus-summary-limit-exclude-childless-dormant ()
8497   "Hide all dormant articles that have no children."
8498   (interactive)
8499   (let ((data (gnus-data-list t))
8500         articles d children)
8501     ;; Find all articles that are either not dormant or have
8502     ;; children.
8503     (while (setq d (pop data))
8504       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8505                 (and (setq children
8506                            (gnus-article-children (gnus-data-number d)))
8507                      (let (found)
8508                        (while children
8509                          (when (memq (car children) articles)
8510                            (setq children nil
8511                                  found t))
8512                          (pop children))
8513                        found)))
8514         (push (gnus-data-number d) articles)))
8515     ;; Do the limiting.
8516     (prog1
8517         (gnus-summary-limit articles)
8518       (gnus-summary-position-point))))
8519
8520 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8521   "Mark all unread excluded articles as read.
8522 If ALL, mark even excluded ticked and dormants as read."
8523   (interactive "P")
8524   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8525   (let ((articles (gnus-sorted-ndifference
8526                    (sort
8527                     (mapcar (lambda (h) (mail-header-number h))
8528                             gnus-newsgroup-headers)
8529                     '<)
8530                    gnus-newsgroup-limit))
8531         article)
8532     (setq gnus-newsgroup-unreads
8533           (gnus-sorted-intersection gnus-newsgroup-unreads
8534                                     gnus-newsgroup-limit))
8535     (if all
8536         (setq gnus-newsgroup-dormant nil
8537               gnus-newsgroup-marked nil
8538               gnus-newsgroup-reads
8539               (nconc
8540                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8541                gnus-newsgroup-reads))
8542       (while (setq article (pop articles))
8543         (unless (or (memq article gnus-newsgroup-dormant)
8544                     (memq article gnus-newsgroup-marked))
8545           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8546
8547 (defun gnus-summary-limit (articles &optional pop)
8548   (if pop
8549       ;; We pop the previous limit off the stack and use that.
8550       (setq articles (car gnus-newsgroup-limits)
8551             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8552     ;; We use the new limit, so we push the old limit on the stack.
8553     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8554   ;; Set the limit.
8555   (setq gnus-newsgroup-limit articles)
8556   (let ((total (length gnus-newsgroup-data))
8557         (data (gnus-data-find-list (gnus-summary-article-number)))
8558         (gnus-summary-mark-below nil)   ; Inhibit this.
8559         found)
8560     ;; This will do all the work of generating the new summary buffer
8561     ;; according to the new limit.
8562     (gnus-summary-prepare)
8563     ;; Hide any threads, possibly.
8564     (gnus-summary-maybe-hide-threads)
8565     ;; Try to return to the article you were at, or one in the
8566     ;; neighborhood.
8567     (when data
8568       ;; We try to find some article after the current one.
8569       (while data
8570         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8571           (setq data nil
8572                 found t))
8573         (setq data (cdr data))))
8574     (unless found
8575       ;; If there is no data, that means that we were after the last
8576       ;; article.  The same goes when we can't find any articles
8577       ;; after the current one.
8578       (goto-char (point-max))
8579       (gnus-summary-find-prev))
8580     (gnus-set-mode-line 'summary)
8581     ;; We return how many articles were removed from the summary
8582     ;; buffer as a result of the new limit.
8583     (- total (length gnus-newsgroup-data))))
8584
8585 (defsubst gnus-invisible-cut-children (threads)
8586   (let ((num 0))
8587     (while threads
8588       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8589         (incf num))
8590       (pop threads))
8591     (< num 2)))
8592
8593 (defsubst gnus-cut-thread (thread)
8594   "Go forwards in the thread until we find an article that we want to display."
8595   (when (or (eq gnus-fetch-old-headers 'some)
8596             (eq gnus-fetch-old-headers 'invisible)
8597             (numberp gnus-fetch-old-headers)
8598             (eq gnus-build-sparse-threads 'some)
8599             (eq gnus-build-sparse-threads 'more))
8600     ;; Deal with old-fetched headers and sparse threads.
8601     (while (and
8602             thread
8603             (or
8604              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8605              (gnus-summary-article-ancient-p
8606               (mail-header-number (car thread))))
8607             (if (or (<= (length (cdr thread)) 1)
8608                     (eq gnus-fetch-old-headers 'invisible))
8609                 (setq gnus-newsgroup-limit
8610                       (delq (mail-header-number (car thread))
8611                             gnus-newsgroup-limit)
8612                       thread (cadr thread))
8613               (when (gnus-invisible-cut-children (cdr thread))
8614                 (let ((th (cdr thread)))
8615                   (while th
8616                     (if (memq (mail-header-number (caar th))
8617                               gnus-newsgroup-limit)
8618                         (setq thread (car th)
8619                               th nil)
8620                       (setq th (cdr th))))))))))
8621   thread)
8622
8623 (defun gnus-cut-threads (threads)
8624   "Cut off all uninteresting articles from the beginning of THREADS."
8625   (when (or (eq gnus-fetch-old-headers 'some)
8626             (eq gnus-fetch-old-headers 'invisible)
8627             (numberp gnus-fetch-old-headers)
8628             (eq gnus-build-sparse-threads 'some)
8629             (eq gnus-build-sparse-threads 'more))
8630     (let ((th threads))
8631       (while th
8632         (setcar th (gnus-cut-thread (car th)))
8633         (setq th (cdr th)))))
8634   ;; Remove nixed out threads.
8635   (delq nil threads))
8636
8637 (defun gnus-summary-initial-limit (&optional show-if-empty)
8638   "Figure out what the initial limit is supposed to be on group entry.
8639 This entails weeding out unwanted dormants, low-scored articles,
8640 fetch-old-headers verbiage, and so on."
8641   ;; Most groups have nothing to remove.
8642   (unless (or gnus-inhibit-limiting
8643               (and (null gnus-newsgroup-dormant)
8644                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8645                    (not (eq gnus-fetch-old-headers 'some))
8646                    (not (numberp gnus-fetch-old-headers))
8647                    (not (eq gnus-fetch-old-headers 'invisible))
8648                    (null gnus-summary-expunge-below)
8649                    (not (eq gnus-build-sparse-threads 'some))
8650                    (not (eq gnus-build-sparse-threads 'more))
8651                    (null gnus-thread-expunge-below)))
8652     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8653     (setq gnus-newsgroup-limit nil)
8654     (mapatoms
8655      (lambda (node)
8656        (unless (car (symbol-value node))
8657          ;; These threads have no parents -- they are roots.
8658          (let ((nodes (cdr (symbol-value node)))
8659                thread)
8660            (while nodes
8661              (if (and gnus-thread-expunge-below
8662                       (< (gnus-thread-total-score (car nodes))
8663                          gnus-thread-expunge-below))
8664                  (gnus-expunge-thread (pop nodes))
8665                (setq thread (pop nodes))
8666                (gnus-summary-limit-children thread))))))
8667      gnus-newsgroup-dependencies)
8668     ;; If this limitation resulted in an empty group, we might
8669     ;; pop the previous limit and use it instead.
8670     (when (and (not gnus-newsgroup-limit)
8671                show-if-empty)
8672       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8673     gnus-newsgroup-limit))
8674
8675 (defun gnus-summary-limit-children (thread)
8676   "Return 1 if this subthread is visible and 0 if it is not."
8677   ;; First we get the number of visible children to this thread.  This
8678   ;; is done by recursing down the thread using this function, so this
8679   ;; will really go down to a leaf article first, before slowly
8680   ;; working its way up towards the root.
8681   (when thread
8682     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8683            (children
8684            (if (cdr thread)
8685                (apply '+ (mapcar 'gnus-summary-limit-children
8686                                  (cdr thread)))
8687              0))
8688           (number (mail-header-number (car thread)))
8689           score)
8690       (if (and
8691            (not (memq number gnus-newsgroup-marked))
8692            (or
8693             ;; If this article is dormant and has absolutely no visible
8694             ;; children, then this article isn't visible.
8695             (and (memq number gnus-newsgroup-dormant)
8696                  (zerop children))
8697             ;; If this is "fetch-old-headered" and there is no
8698             ;; visible children, then we don't want this article.
8699             (and (or (eq gnus-fetch-old-headers 'some)
8700                      (numberp gnus-fetch-old-headers))
8701                  (gnus-summary-article-ancient-p number)
8702                  (zerop children))
8703             ;; If this is "fetch-old-headered" and `invisible', then
8704             ;; we don't want this article.
8705             (and (eq gnus-fetch-old-headers 'invisible)
8706                  (gnus-summary-article-ancient-p number))
8707             ;; If this is a sparsely inserted article with no children,
8708             ;; we don't want it.
8709             (and (eq gnus-build-sparse-threads 'some)
8710                  (gnus-summary-article-sparse-p number)
8711                  (zerop children))
8712             ;; If we use expunging, and this article is really
8713             ;; low-scored, then we don't want this article.
8714             (when (and gnus-summary-expunge-below
8715                        (< (setq score
8716                                 (or (cdr (assq number gnus-newsgroup-scored))
8717                                     gnus-summary-default-score))
8718                           gnus-summary-expunge-below))
8719               ;; We increase the expunge-tally here, but that has
8720               ;; nothing to do with the limits, really.
8721               (incf gnus-newsgroup-expunged-tally)
8722               ;; We also mark as read here, if that's wanted.
8723               (when (and gnus-summary-mark-below
8724                          (< score gnus-summary-mark-below))
8725                 (setq gnus-newsgroup-unreads
8726                       (delq number gnus-newsgroup-unreads))
8727                 (if gnus-newsgroup-auto-expire
8728                     (push number gnus-newsgroup-expirable)
8729                   (push (cons number gnus-low-score-mark)
8730                         gnus-newsgroup-reads)))
8731               t)
8732             ;; Do the `display' group parameter.
8733             (and gnus-newsgroup-display
8734                  (not (funcall gnus-newsgroup-display)))))
8735           ;; Nope, invisible article.
8736           0
8737         ;; Ok, this article is to be visible, so we add it to the limit
8738         ;; and return 1.
8739         (push number gnus-newsgroup-limit)
8740         1))))
8741
8742 (defun gnus-expunge-thread (thread)
8743   "Mark all articles in THREAD as read."
8744   (let* ((number (mail-header-number (car thread))))
8745     (incf gnus-newsgroup-expunged-tally)
8746     ;; We also mark as read here, if that's wanted.
8747     (setq gnus-newsgroup-unreads
8748           (delq number gnus-newsgroup-unreads))
8749     (if gnus-newsgroup-auto-expire
8750         (push number gnus-newsgroup-expirable)
8751       (push (cons number gnus-low-score-mark)
8752             gnus-newsgroup-reads)))
8753   ;; Go recursively through all subthreads.
8754   (mapcar 'gnus-expunge-thread (cdr thread)))
8755
8756 ;; Summary article oriented commands
8757
8758 (defun gnus-summary-refer-parent-article (n)
8759   "Refer parent article N times.
8760 If N is negative, go to ancestor -N instead.
8761 The difference between N and the number of articles fetched is returned."
8762   (interactive "p")
8763   (let ((skip 1)
8764         error header ref)
8765     (when (not (natnump n))
8766       (setq skip (abs n)
8767             n 1))
8768     (while (and (> n 0)
8769                 (not error))
8770       (setq header (gnus-summary-article-header))
8771       (if (and (eq (mail-header-number header)
8772                    (cdr gnus-article-current))
8773                (equal gnus-newsgroup-name
8774                       (car gnus-article-current)))
8775           ;; If we try to find the parent of the currently
8776           ;; displayed article, then we take a look at the actual
8777           ;; References header, since this is slightly more
8778           ;; reliable than the References field we got from the
8779           ;; server.
8780           (with-current-buffer gnus-original-article-buffer
8781             (nnheader-narrow-to-headers)
8782             (unless (setq ref (message-fetch-field "references"))
8783               (when (setq ref (message-fetch-field "in-reply-to"))
8784                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8785             (widen))
8786         (setq ref
8787               ;; It's not the current article, so we take a bet on
8788               ;; the value we got from the server.
8789               (mail-header-references header)))
8790       (if (and ref
8791                (not (equal ref "")))
8792           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8793             (gnus-message 1 "Couldn't find parent"))
8794         (gnus-message 1 "No references in article %d"
8795                       (gnus-summary-article-number))
8796         (setq error t))
8797       (decf n))
8798     (gnus-summary-position-point)
8799     n))
8800
8801 (defun gnus-summary-refer-references ()
8802   "Fetch all articles mentioned in the References header.
8803 Return the number of articles fetched."
8804   (interactive)
8805   (let ((ref (mail-header-references (gnus-summary-article-header)))
8806         (current (gnus-summary-article-number))
8807         (n 0))
8808     (if (or (not ref)
8809             (equal ref ""))
8810         (error "No References in the current article")
8811       ;; For each Message-ID in the References header...
8812       (while (string-match "<[^>]*>" ref)
8813         (incf n)
8814         ;; ... fetch that article.
8815         (gnus-summary-refer-article
8816          (prog1 (match-string 0 ref)
8817            (setq ref (substring ref (match-end 0))))))
8818       (gnus-summary-goto-subject current)
8819       (gnus-summary-position-point)
8820       n)))
8821
8822 (defun gnus-summary-refer-thread (&optional limit)
8823   "Fetch all articles in the current thread.
8824 If LIMIT (the numerical prefix), fetch that many old headers instead
8825 of what's specified by the `gnus-refer-thread-limit' variable."
8826   (interactive "P")
8827   (let ((id (mail-header-id (gnus-summary-article-header)))
8828         (limit (if limit (prefix-numeric-value limit)
8829                  gnus-refer-thread-limit)))
8830     (unless (eq gnus-fetch-old-headers 'invisible)
8831       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8832       ;; Retrieve the headers and read them in.
8833       (if (eq (if (numberp limit)
8834                   (gnus-retrieve-headers
8835                    (list (min
8836                           (+ (mail-header-number
8837                               (gnus-summary-article-header))
8838                              limit)
8839                           gnus-newsgroup-end))
8840                    gnus-newsgroup-name (* limit 2))
8841                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8842                 ;; headers.
8843                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8844                                        gnus-newsgroup-name limit))
8845               'nov)
8846           (gnus-build-all-threads)
8847         (error "Can't fetch thread from back ends that don't support NOV"))
8848       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8849     (gnus-summary-limit-include-thread id)))
8850
8851 (defun gnus-summary-refer-article (message-id)
8852   "Fetch an article specified by MESSAGE-ID."
8853   (interactive "sMessage-ID: ")
8854   (when (and (stringp message-id)
8855              (not (zerop (length message-id))))
8856     (setq message-id (gnus-replace-in-string message-id " " ""))
8857     ;; Construct the correct Message-ID if necessary.
8858     ;; Suggested by tale@pawl.rpi.edu.
8859     (unless (string-match "^<" message-id)
8860       (setq message-id (concat "<" message-id)))
8861     (unless (string-match ">$" message-id)
8862       (setq message-id (concat message-id ">")))
8863     ;; People often post MIDs from URLs, so unhex it:
8864     (unless (string-match "@" message-id)
8865       (setq message-id (gnus-url-unhex-string message-id)))
8866     (let* ((header (gnus-id-to-header message-id))
8867            (sparse (and header
8868                         (gnus-summary-article-sparse-p
8869                          (mail-header-number header))
8870                         (memq (mail-header-number header)
8871                               gnus-newsgroup-limit)))
8872            number)
8873       (cond
8874        ;; If the article is present in the buffer we just go to it.
8875        ((and header
8876              (or (not (gnus-summary-article-sparse-p
8877                        (mail-header-number header)))
8878                  sparse))
8879         (prog1
8880             (gnus-summary-goto-article
8881              (mail-header-number header) nil t)
8882           (when sparse
8883             (gnus-summary-update-article (mail-header-number header)))))
8884        (t
8885         ;; We fetch the article.
8886         (catch 'found
8887           (dolist (gnus-override-method (gnus-refer-article-methods))
8888             (when (and (gnus-check-server gnus-override-method)
8889                        ;; Fetch the header,
8890                        (setq number (gnus-summary-insert-subject message-id)))
8891               ;; and display the article.
8892               (gnus-summary-select-article nil nil nil number)
8893               (throw 'found t)))
8894           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8895
8896 (defun gnus-refer-article-methods ()
8897   "Return a list of referable methods."
8898   (cond
8899    ;; No method, so we default to current and native.
8900    ((null gnus-refer-article-method)
8901     (list gnus-current-select-method gnus-select-method))
8902    ;; Current.
8903    ((eq 'current gnus-refer-article-method)
8904     (list gnus-current-select-method))
8905    ;; List of select methods.
8906    ((not (and (symbolp (car gnus-refer-article-method))
8907               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8908     (let (out)
8909       (dolist (method gnus-refer-article-method)
8910         (push (if (eq 'current method)
8911                   gnus-current-select-method
8912                 method)
8913               out))
8914       (nreverse out)))
8915    ;; One single select method.
8916    (t
8917     (list gnus-refer-article-method))))
8918
8919 (defun gnus-summary-edit-parameters ()
8920   "Edit the group parameters of the current group."
8921   (interactive)
8922   (gnus-group-edit-group gnus-newsgroup-name 'params))
8923
8924 (defun gnus-summary-customize-parameters ()
8925   "Customize the group parameters of the current group."
8926   (interactive)
8927   (gnus-group-customize gnus-newsgroup-name))
8928
8929 (defun gnus-summary-enter-digest-group (&optional force)
8930   "Enter an nndoc group based on the current article.
8931 If FORCE, force a digest interpretation.  If not, try
8932 to guess what the document format is."
8933   (interactive "P")
8934   (let ((conf gnus-current-window-configuration))
8935     (save-window-excursion
8936       (save-excursion
8937         (let (gnus-article-prepare-hook
8938               gnus-display-mime-function
8939               gnus-break-pages)
8940           (gnus-summary-select-article))))
8941     (setq gnus-current-window-configuration conf)
8942     (let* ((name (format "%s-%d"
8943                          (gnus-group-prefixed-name
8944                           gnus-newsgroup-name (list 'nndoc ""))
8945                          (with-current-buffer gnus-summary-buffer
8946                            gnus-current-article)))
8947            (ogroup gnus-newsgroup-name)
8948            (params (append (gnus-info-params (gnus-get-info ogroup))
8949                            (list (cons 'to-group ogroup))
8950                            (list (cons 'parent-group ogroup))
8951                            (list (cons 'save-article-group ogroup))))
8952            (case-fold-search t)
8953            (buf (current-buffer))
8954            dig to-address)
8955       (with-current-buffer gnus-original-article-buffer
8956         ;; Have the digest group inherit the main mail address of
8957         ;; the parent article.
8958         (when (setq to-address (or (gnus-fetch-field "reply-to")
8959                                    (gnus-fetch-field "from")))
8960           (setq params
8961                 (append
8962                  (list (cons 'to-address
8963                              (funcall gnus-decode-encoded-address-function
8964                                       to-address))))))
8965         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8966         (insert-buffer-substring gnus-original-article-buffer)
8967         ;; Remove lines that may lead nndoc to misinterpret the
8968         ;; document type.
8969         (narrow-to-region
8970          (goto-char (point-min))
8971          (or (search-forward "\n\n" nil t) (point)))
8972         (goto-char (point-min))
8973         (delete-matching-lines "^Path:\\|^From ")
8974         (widen))
8975       (unwind-protect
8976           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8977                     (gnus-newsgroup-ephemeral-ignored-charsets
8978                      gnus-newsgroup-ignored-charsets))
8979                 (gnus-group-read-ephemeral-group
8980                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8981                               (nndoc-article-type
8982                                ,(if force 'mbox 'guess)))
8983                  t nil nil nil
8984                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8985                                                         "ADAPT")))))
8986               ;; Make all postings to this group go to the parent group.
8987               (nconc (gnus-info-params (gnus-get-info name))
8988                      params)
8989             ;; Couldn't select this doc group.
8990             (switch-to-buffer buf)
8991             (gnus-set-global-variables)
8992             (gnus-configure-windows 'summary)
8993             (gnus-message 3 "Article couldn't be entered?"))
8994         (kill-buffer dig)))))
8995
8996 (defun gnus-summary-read-document (n)
8997   "Open a new group based on the current article(s).
8998 This will allow you to read digests and other similar
8999 documents as newsgroups.
9000 Obeys the standard process/prefix convention."
9001   (interactive "P")
9002   (let* ((ogroup gnus-newsgroup-name)
9003          (params (append (gnus-info-params (gnus-get-info ogroup))
9004                          (list (cons 'to-group ogroup))))
9005          group egroup groups vgroup)
9006     (dolist (article (gnus-summary-work-articles n))
9007       (setq group (format "%s-%d" gnus-newsgroup-name article))
9008       (gnus-summary-remove-process-mark article)
9009       (when (gnus-summary-display-article article)
9010         (save-excursion ;;What for?
9011           (with-temp-buffer
9012             (insert-buffer-substring gnus-original-article-buffer)
9013             ;; Remove some headers that may lead nndoc to make
9014             ;; the wrong guess.
9015             (message-narrow-to-head)
9016             (goto-char (point-min))
9017             (delete-matching-lines "^Path:\\|^From ")
9018             (widen)
9019             (if (setq egroup
9020                       (gnus-group-read-ephemeral-group
9021                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9022                                      (nndoc-article-type guess))
9023                        t nil t))
9024                 (progn
9025                   ;; Make all postings to this group go to the parent group.
9026                   (nconc (gnus-info-params (gnus-get-info egroup))
9027                          params)
9028                   (push egroup groups))
9029               ;; Couldn't select this doc group.
9030               (gnus-error 3 "Article couldn't be entered"))))))
9031     ;; Now we have selected all the documents.
9032     (cond
9033      ((not groups)
9034       (error "None of the articles could be interpreted as documents"))
9035      ((gnus-group-read-ephemeral-group
9036        (setq vgroup (format
9037                      "nnvirtual:%s-%s" gnus-newsgroup-name
9038                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9039        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9040        t
9041        (cons (current-buffer) 'summary)))
9042      (t
9043       (error "Couldn't select virtual nndoc group")))))
9044
9045 (defun gnus-summary-isearch-article (&optional regexp-p)
9046   "Do incremental search forward on the current article.
9047 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9048   (interactive "P")
9049   (gnus-summary-select-article)
9050   (gnus-configure-windows 'article)
9051   (gnus-eval-in-buffer-window gnus-article-buffer
9052     (save-restriction
9053       (widen)
9054       (isearch-forward regexp-p))))
9055
9056 (defun gnus-summary-repeat-search-article-forward ()
9057   "Repeat the previous search forwards."
9058   (interactive)
9059   (unless gnus-last-search-regexp
9060     (error "No previous search"))
9061   (gnus-summary-search-article-forward gnus-last-search-regexp))
9062
9063 (defun gnus-summary-repeat-search-article-backward ()
9064   "Repeat the previous search backwards."
9065   (interactive)
9066   (unless gnus-last-search-regexp
9067     (error "No previous search"))
9068   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9069
9070 (defun gnus-summary-search-article-forward (regexp &optional backward)
9071   "Search for an article containing REGEXP forward.
9072 If BACKWARD, search backward instead."
9073   (interactive
9074    (list (read-string
9075           (format "Search article %s (regexp%s): "
9076                   (if current-prefix-arg "backward" "forward")
9077                   (if gnus-last-search-regexp
9078                       (concat ", default " gnus-last-search-regexp)
9079                     "")))
9080          current-prefix-arg))
9081   (if (string-equal regexp "")
9082       (setq regexp (or gnus-last-search-regexp ""))
9083     (setq gnus-last-search-regexp regexp)
9084     (setq gnus-article-before-search gnus-current-article))
9085   ;; Intentionally set gnus-last-article.
9086   (setq gnus-last-article gnus-article-before-search)
9087   (let ((gnus-last-article gnus-last-article))
9088     (if (gnus-summary-search-article regexp backward)
9089         (gnus-summary-show-thread)
9090       (signal 'search-failed (list regexp)))))
9091
9092 (defun gnus-summary-search-article-backward (regexp)
9093   "Search for an article containing REGEXP backward."
9094   (interactive
9095    (list (read-string
9096           (format "Search article backward (regexp%s): "
9097                   (if gnus-last-search-regexp
9098                       (concat ", default " gnus-last-search-regexp)
9099                     "")))))
9100   (gnus-summary-search-article-forward regexp 'backward))
9101
9102 (defun gnus-summary-search-article (regexp &optional backward)
9103   "Search for an article containing REGEXP.
9104 Optional argument BACKWARD means do search for backward.
9105 `gnus-select-article-hook' is not called during the search."
9106   ;; We have to require this here to make sure that the following
9107   ;; dynamic binding isn't shadowed by autoloading.
9108   (require 'gnus-async)
9109   (require 'gnus-art)
9110   (let ((gnus-select-article-hook nil)  ;Disable hook.
9111         (gnus-article-prepare-hook nil)
9112         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9113         (gnus-use-article-prefetch nil)
9114         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9115         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9116         (gnus-visual nil)
9117         (gnus-keep-backlog nil)
9118         (gnus-break-pages nil)
9119         (gnus-summary-display-arrow nil)
9120         (gnus-updated-mode-lines nil)
9121         (gnus-auto-center-summary nil)
9122         (sum (current-buffer))
9123         (gnus-display-mime-function nil)
9124         (found nil)
9125         point)
9126     (gnus-save-hidden-threads
9127       (gnus-summary-select-article)
9128       (set-buffer gnus-article-buffer)
9129       (goto-char (window-point (get-buffer-window (current-buffer))))
9130       (when backward
9131         (forward-line -1))
9132       (while (not found)
9133         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9134         (if (if backward
9135                 (re-search-backward regexp nil t)
9136               (re-search-forward regexp nil t))
9137             ;; We found the regexp.
9138             (progn
9139               (setq found 'found)
9140               (beginning-of-line)
9141               (set-window-start
9142                (get-buffer-window (current-buffer))
9143                (point))
9144               (forward-line 1)
9145               (set-window-point
9146                (get-buffer-window (current-buffer))
9147                (point))
9148               (set-buffer sum)
9149               (setq point (point)))
9150           ;; We didn't find it, so we go to the next article.
9151           (set-buffer sum)
9152           (setq found 'not)
9153           (while (eq found 'not)
9154             (if (not (if backward (gnus-summary-find-prev)
9155                        (gnus-summary-find-next)))
9156                 ;; No more articles.
9157                 (setq found t)
9158               ;; Select the next article and adjust point.
9159               (unless (gnus-summary-article-sparse-p
9160                        (gnus-summary-article-number))
9161                 (setq found nil)
9162                 (gnus-summary-select-article)
9163                 (set-buffer gnus-article-buffer)
9164                 (widen)
9165                 (goto-char (if backward (point-max) (point-min))))))))
9166       (gnus-message 7 ""))
9167     ;; Return whether we found the regexp.
9168     (when (eq found 'found)
9169       (goto-char point)
9170       (gnus-summary-show-thread)
9171       (gnus-summary-goto-subject gnus-current-article)
9172       (gnus-summary-position-point)
9173       t)))
9174
9175 (defun gnus-find-matching-articles (header regexp)
9176   "Return a list of all articles that match REGEXP on HEADER.
9177 This search includes all articles in the current group that Gnus has
9178 fetched headers for, whether they are displayed or not."
9179   (let ((articles nil)
9180         ;; Can't eta-reduce because it's a macro.
9181         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9182         (case-fold-search t))
9183     (dolist (header gnus-newsgroup-headers)
9184       (when (string-match regexp (funcall func header))
9185         (push (mail-header-number header) articles)))
9186     (nreverse articles)))
9187
9188 (defun gnus-summary-find-matching (header regexp &optional backward unread
9189                                           not-case-fold not-matching)
9190   "Return a list of all articles that match REGEXP on HEADER.
9191 The search stars on the current article and goes forwards unless
9192 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9193 If UNREAD is non-nil, only unread articles will
9194 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9195 in the comparisons. If NOT-MATCHING, return a list of all articles that
9196 not match REGEXP on HEADER."
9197   (let ((case-fold-search (not not-case-fold))
9198         articles d func)
9199     (if (consp header)
9200         (if (eq (car header) 'extra)
9201             (setq func
9202                   `(lambda (h)
9203                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9204                          "")))
9205           (error "%s is an invalid header" header))
9206       (unless (fboundp (intern (concat "mail-header-" header)))
9207         (error "%s is not a valid header" header))
9208       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9209     (dolist (d (if (eq backward 'all)
9210                    gnus-newsgroup-data
9211                  (gnus-data-find-list
9212                   (gnus-summary-article-number)
9213                   (gnus-data-list backward))))
9214       (when (and (or (not unread)       ; We want all articles...
9215                      (gnus-data-unread-p d)) ; Or just unreads.
9216                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9217                  (if not-matching
9218                      (not (string-match
9219                            regexp
9220                            (funcall func (gnus-data-header d))))
9221                    (string-match regexp
9222                                  (funcall func (gnus-data-header d)))))
9223         (push (gnus-data-number d) articles))) ; Success!
9224     (nreverse articles)))
9225
9226 (defun gnus-summary-execute-command (header regexp command &optional backward)
9227   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9228 If HEADER is an empty string (or nil), the match is done on the entire
9229 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9230   (interactive
9231    (list (let ((completion-ignore-case t))
9232            (completing-read
9233             "Header name: "
9234             (mapcar (lambda (header) (list (format "%s" header)))
9235                     (append
9236                      '("Number" "Subject" "From" "Lines" "Date"
9237                        "Message-ID" "Xref" "References" "Body")
9238                      gnus-extra-headers))
9239             nil 'require-match))
9240          (read-string "Regexp: ")
9241          (read-key-sequence "Command: ")
9242          current-prefix-arg))
9243   (when (equal header "Body")
9244     (setq header ""))
9245   ;; Hidden thread subtrees must be searched as well.
9246   (gnus-summary-show-all-threads)
9247   ;; We don't want to change current point nor window configuration.
9248   (save-excursion
9249     (save-window-excursion
9250       (let (gnus-visual
9251             gnus-treat-strip-trailing-blank-lines
9252             gnus-treat-strip-leading-blank-lines
9253             gnus-treat-strip-multiple-blank-lines
9254             gnus-treat-hide-boring-headers
9255             gnus-treat-fold-newsgroups
9256             gnus-article-prepare-hook)
9257         (gnus-message 6 "Executing %s..." (key-description command))
9258         ;; We'd like to execute COMMAND interactively so as to give arguments.
9259         (gnus-execute header regexp
9260                       `(call-interactively ',(key-binding command))
9261                       backward)
9262         (gnus-message 6 "Executing %s...done" (key-description command))))))
9263
9264 (defun gnus-summary-beginning-of-article ()
9265   "Scroll the article back to the beginning."
9266   (interactive)
9267   (gnus-summary-select-article)
9268   (gnus-configure-windows 'article)
9269   (gnus-eval-in-buffer-window gnus-article-buffer
9270     (widen)
9271     (goto-char (point-min))
9272     (when gnus-break-pages
9273       (gnus-narrow-to-page))))
9274
9275 (defun gnus-summary-end-of-article ()
9276   "Scroll to the end of the article."
9277   (interactive)
9278   (gnus-summary-select-article)
9279   (gnus-configure-windows 'article)
9280   (gnus-eval-in-buffer-window gnus-article-buffer
9281     (widen)
9282     (goto-char (point-max))
9283     (recenter -3)
9284     (when gnus-break-pages
9285       (gnus-narrow-to-page))))
9286
9287 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9288   "Truncate to LEN and quote all \"(\"'s in STRING."
9289   (gnus-replace-in-string (if (and len (> (length string) len))
9290                               (substring string 0 len)
9291                             string)
9292                           "[()]" "\\\\\\&"))
9293
9294 (defun gnus-summary-print-article (&optional filename n)
9295   "Generate and print a PostScript image of the process-marked (mail) articles.
9296
9297 If used interactively, print the current article if none are
9298 process-marked.  With prefix arg, prompt the user for the name of the
9299 file to save in.
9300
9301 When used from Lisp, accept two optional args FILENAME and N.  N means
9302 to print the next N articles.  If N is negative, print the N previous
9303 articles.  If N is nil and articles have been marked with the process
9304 mark, print these instead.
9305
9306 If the optional first argument FILENAME is nil, send the image to the
9307 printer.  If FILENAME is a string, save the PostScript image in a file with
9308 that name.  If FILENAME is a number, prompt the user for the name of the file
9309 to save in."
9310   (interactive (list (ps-print-preprint current-prefix-arg)))
9311   (dolist (article (gnus-summary-work-articles n))
9312     (gnus-summary-select-article nil nil 'pseudo article)
9313     (gnus-eval-in-buffer-window gnus-article-buffer
9314       (gnus-print-buffer))
9315     (gnus-summary-remove-process-mark article))
9316   (ps-despool filename))
9317
9318 (defun gnus-print-buffer ()
9319   (let ((buffer (generate-new-buffer " *print*")))
9320     (unwind-protect
9321         (progn
9322           (copy-to-buffer buffer (point-min) (point-max))
9323           (set-buffer buffer)
9324           (gnus-remove-text-with-property 'gnus-decoration)
9325           (when (gnus-visual-p 'article-highlight 'highlight)
9326             ;; Copy-to-buffer doesn't copy overlay.  So redo
9327             ;; highlight.
9328             (let ((gnus-article-buffer buffer))
9329               (gnus-article-highlight-citation t)
9330               (gnus-article-highlight-signature)
9331               (gnus-article-emphasize)
9332               (gnus-article-delete-invisible-text)))
9333           (let ((ps-left-header
9334                  (list
9335                   (concat "("
9336                           (gnus-summary-print-truncate-and-quote
9337                            (mail-header-subject gnus-current-headers)
9338                            66) ")")
9339                   (concat "("
9340                           (gnus-summary-print-truncate-and-quote
9341                            (mail-header-from gnus-current-headers)
9342                            45) ")")))
9343                 (ps-right-header
9344                  (list
9345                   "/pagenumberstring load"
9346                   (concat "("
9347                           (mail-header-date gnus-current-headers) ")"))))
9348             (gnus-run-hooks 'gnus-ps-print-hook)
9349             (save-excursion
9350               (if ps-print-color-p
9351                   (ps-spool-buffer-with-faces)
9352                 (ps-spool-buffer)))))
9353       (kill-buffer buffer))))
9354
9355 (defun gnus-summary-show-complete-article ()
9356   "Show a complete version of the current article.
9357 This is only useful if you're looking at a partial version of the
9358 article currently."
9359   (interactive)
9360   (let ((gnus-keep-backlog nil)
9361         (gnus-use-cache nil)
9362         (gnus-agent nil)
9363         (variable (format "%s-fetch-partial-articles"
9364                           (car (gnus-find-method-for-group
9365                                 gnus-newsgroup-name))))
9366         old-val)
9367     (unwind-protect
9368         (progn
9369           (setq old-val (symbol-value variable))
9370           (set variable nil)
9371           (gnus-flush-original-article-buffer)
9372           (gnus-summary-show-article))
9373       (set variable old-val))))
9374
9375 (defun gnus-summary-show-article (&optional arg)
9376   "Force redisplaying of the current article.
9377 If ARG (the prefix) is a number, show the article with the charset
9378 defined in `gnus-summary-show-article-charset-alist', or the charset
9379 input.
9380 If ARG (the prefix) is non-nil and not a number, show the raw article
9381 without any article massaging functions being run.  Normally, the key
9382 strokes are `C-u g'."
9383   (interactive "P")
9384   (cond
9385    ((numberp arg)
9386     (gnus-summary-show-article t)
9387     (let ((gnus-newsgroup-charset
9388            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9389                (mm-read-coding-system
9390                 "View as charset: " ;; actually it is coding system.
9391                 (with-current-buffer gnus-article-buffer
9392                   (mm-detect-coding-region (point) (point-max))))))
9393           (gnus-newsgroup-ignored-charsets 'gnus-all))
9394       (gnus-summary-select-article nil 'force)
9395       (let ((deps gnus-newsgroup-dependencies)
9396             head header lines)
9397         (with-current-buffer gnus-original-article-buffer
9398           (save-restriction
9399             (message-narrow-to-head)
9400             (setq head (buffer-string))
9401             (goto-char (point-min))
9402             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9403               (goto-char (point-max))
9404               (widen)
9405               (setq lines (1- (count-lines (point) (point-max))))))
9406           (with-temp-buffer
9407             (insert (format "211 %d Article retrieved.\n"
9408                             (cdr gnus-article-current)))
9409             (insert head)
9410             (if lines (insert (format "Lines: %d\n" lines)))
9411             (insert ".\n")
9412             (let ((nntp-server-buffer (current-buffer)))
9413               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9414         (gnus-data-set-header
9415          (gnus-data-find (cdr gnus-article-current))
9416          header)
9417         (gnus-summary-update-article-line
9418          (cdr gnus-article-current) header)
9419         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9420           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9421    ((not arg)
9422     ;; Select the article the normal way.
9423     (gnus-summary-select-article nil 'force))
9424    (t
9425     ;; We have to require this here to make sure that the following
9426     ;; dynamic binding isn't shadowed by autoloading.
9427     (require 'gnus-async)
9428     (require 'gnus-art)
9429     ;; Bind the article treatment functions to nil.
9430     (let ((gnus-have-all-headers t)
9431           gnus-article-prepare-hook
9432           gnus-article-decode-hook
9433           gnus-display-mime-function
9434           gnus-break-pages)
9435       ;; Destroy any MIME parts.
9436       (when (gnus-buffer-live-p gnus-article-buffer)
9437         (with-current-buffer gnus-article-buffer
9438           (mm-destroy-parts gnus-article-mime-handles)
9439           ;; Set it to nil for safety reason.
9440           (setq gnus-article-mime-handle-alist nil)
9441           (setq gnus-article-mime-handles nil)))
9442       (gnus-summary-select-article nil 'force))))
9443   (gnus-summary-goto-subject gnus-current-article)
9444   (gnus-summary-position-point))
9445
9446 (defun gnus-summary-show-raw-article ()
9447   "Show the raw article without any article massaging functions being run."
9448   (interactive)
9449   (gnus-summary-show-article t))
9450
9451 (defun gnus-summary-verbose-headers (&optional arg)
9452   "Toggle permanent full header display.
9453 If ARG is a positive number, turn header display on.
9454 If ARG is a negative number, turn header display off."
9455   (interactive "P")
9456   (setq gnus-show-all-headers
9457         (cond ((or (not (numberp arg))
9458                    (zerop arg))
9459                (not gnus-show-all-headers))
9460               ((natnump arg)
9461                t)))
9462   (gnus-summary-show-article))
9463
9464 (defun gnus-summary-toggle-header (&optional arg)
9465   "Show the headers if they are hidden, or hide them if they are shown.
9466 If ARG is a positive number, show the entire header.
9467 If ARG is a negative number, hide the unwanted header lines."
9468   (interactive "P")
9469   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9470                      (get-buffer-window gnus-article-buffer t))))
9471     (with-current-buffer gnus-article-buffer
9472       (widen)
9473       (article-narrow-to-head)
9474       (let* ((inhibit-read-only t)
9475              (inhibit-point-motion-hooks t)
9476              (hidden (if (numberp arg)
9477                          (>= arg 0)
9478                        (or (not (looking-at "[^ \t\n]+:"))
9479                            (gnus-article-hidden-text-p 'headers))))
9480              s e)
9481         (delete-region (point-min) (point-max))
9482         (with-current-buffer gnus-original-article-buffer
9483           (goto-char (setq s (point-min)))
9484           (setq e (if (search-forward "\n\n" nil t)
9485                       (1- (point))
9486                     (point-max))))
9487         (insert-buffer-substring gnus-original-article-buffer s e)
9488         (run-hooks 'gnus-article-decode-hook)
9489         (if hidden
9490             (let ((gnus-treat-hide-headers nil)
9491                   (gnus-treat-hide-boring-headers nil))
9492               (gnus-delete-wash-type 'headers)
9493               (gnus-treat-article 'head))
9494           (gnus-treat-article 'head))
9495         (widen)
9496         (if window
9497             (set-window-start window (goto-char (point-min))))
9498         (if gnus-break-pages
9499             (gnus-narrow-to-page)
9500           (when (gnus-visual-p 'page-marker)
9501             (let ((inhibit-read-only t))
9502               (gnus-remove-text-with-property 'gnus-prev)
9503               (gnus-remove-text-with-property 'gnus-next))))
9504         (gnus-set-mode-line 'article)))))
9505
9506 (defun gnus-summary-show-all-headers ()
9507   "Make all header lines visible."
9508   (interactive)
9509   (gnus-summary-toggle-header 1))
9510
9511 (defun gnus-summary-caesar-message (&optional arg)
9512   "Caesar rotate the current article by 13.
9513 With a non-numerical prefix, also rotate headers.  A numerical
9514 prefix specifies how many places to rotate each letter forward."
9515   (interactive "P")
9516   (gnus-summary-select-article)
9517   (let ((mail-header-separator ""))
9518     (gnus-eval-in-buffer-window gnus-article-buffer
9519       (save-restriction
9520         (widen)
9521         (let ((start (window-start))
9522               (inhibit-read-only t))
9523           (if (equal arg '(4))
9524               (message-caesar-buffer-body nil t)
9525             (message-caesar-buffer-body arg))
9526           (set-window-start (get-buffer-window (current-buffer)) start)))))
9527   ;; Create buttons and stuff...
9528   (gnus-treat-article nil))
9529
9530 (declare-function idna-to-unicode "ext:idna" (str))
9531
9532 (defun gnus-summary-idna-message (&optional arg)
9533   "Decode IDNA encoded domain names in the current articles.
9534 IDNA encoded domain names looks like `xn--bar'.  If a string
9535 remain unencoded after running this function, it is likely an
9536 invalid IDNA string (`xn--bar' is invalid).
9537
9538 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9539 installed for this command to work."
9540   (interactive "P")
9541   (if (not (and (condition-case nil (require 'idna)
9542                   (file-error))
9543                 (mm-coding-system-p 'utf-8)
9544                 (executable-find (symbol-value 'idna-program))))
9545       (gnus-message
9546        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9547     (gnus-summary-select-article)
9548     (let ((mail-header-separator ""))
9549       (gnus-eval-in-buffer-window gnus-article-buffer
9550         (save-restriction
9551           (widen)
9552           (let ((start (window-start))
9553                 buffer-read-only)
9554             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9555               (replace-match (idna-to-unicode (match-string 1))))
9556             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9557
9558 (defun gnus-summary-morse-message (&optional arg)
9559   "Morse decode the current article."
9560   (interactive "P")
9561   (gnus-summary-select-article)
9562   (let ((mail-header-separator ""))
9563     (gnus-eval-in-buffer-window gnus-article-buffer
9564       (save-excursion
9565         (save-restriction
9566           (widen)
9567           (let ((pos (window-start))
9568                 (inhibit-read-only t))
9569             (goto-char (point-min))
9570             (when (message-goto-body)
9571               (gnus-narrow-to-body))
9572             (goto-char (point-min))
9573             (while (search-forward "·" (point-max) t)
9574               (replace-match "."))
9575             (unmorse-region (point-min) (point-max))
9576             (widen)
9577             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9578
9579 (defun gnus-summary-stop-page-breaking ()
9580   "Stop page breaking in the current article."
9581   (interactive)
9582   (gnus-summary-select-article)
9583   (gnus-eval-in-buffer-window gnus-article-buffer
9584     (widen)
9585     (when (gnus-visual-p 'page-marker)
9586       (let ((inhibit-read-only t))
9587         (gnus-remove-text-with-property 'gnus-prev)
9588         (gnus-remove-text-with-property 'gnus-next))
9589       (setq gnus-page-broken nil))))
9590
9591 (defun gnus-summary-move-article (&optional n to-newsgroup
9592                                             select-method action)
9593   "Move the current article to a different newsgroup.
9594 If N is a positive number, move the N next articles.
9595 If N is a negative number, move the N previous articles.
9596 If N is nil and any articles have been marked with the process mark,
9597 move those articles instead.
9598 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9599 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9600 re-spool using this method.
9601
9602 When called interactively with TO-NEWSGROUP being nil, the value of
9603 the variable `gnus-move-split-methods' is used for finding a default
9604 for the target newsgroup.
9605
9606 For this function to work, both the current newsgroup and the
9607 newsgroup that you want to move to have to support the `request-move'
9608 and `request-accept' functions.
9609
9610 ACTION can be either `move' (the default), `crosspost' or `copy'."
9611   (interactive "P")
9612   (unless action
9613     (setq action 'move))
9614   ;; Check whether the source group supports the required functions.
9615   (cond ((and (eq action 'move)
9616               (not (gnus-check-backend-function
9617                     'request-move-article gnus-newsgroup-name)))
9618          (error "The current group does not support article moving"))
9619         ((and (eq action 'crosspost)
9620               (not (gnus-check-backend-function
9621                     'request-replace-article gnus-newsgroup-name)))
9622          (error "The current group does not support article editing")))
9623   (let ((articles (gnus-summary-work-articles n))
9624         (prefix (if (gnus-check-backend-function
9625                      'request-move-article gnus-newsgroup-name)
9626                     (funcall gnus-move-group-prefix-function
9627                              gnus-newsgroup-name)
9628                   ""))
9629         (names '((move "Move" "Moving")
9630                  (copy "Copy" "Copying")
9631                  (crosspost "Crosspost" "Crossposting")))
9632         (copy-buf (save-excursion
9633                     (nnheader-set-temp-buffer " *copy article*")))
9634         art-group to-method new-xref article to-groups
9635         articles-to-update-marks encoded)
9636     (unless (assq action names)
9637       (error "Unknown action %s" action))
9638     ;; Read the newsgroup name.
9639     (when (and (not to-newsgroup)
9640                (not select-method))
9641       (if (and gnus-move-split-methods
9642                (not
9643                 (and (memq gnus-current-article articles)
9644                      (gnus-buffer-live-p gnus-original-article-buffer))))
9645           ;; When `gnus-move-split-methods' is non-nil, we have to
9646           ;; select an article to give `gnus-read-move-group-name' an
9647           ;; opportunity to suggest an appropriate default.  However,
9648           ;; we needn't render or mark the article.
9649           (let ((gnus-display-mime-function nil)
9650                 (gnus-article-prepare-hook nil)
9651                 (gnus-mark-article-hook nil))
9652             (gnus-summary-select-article nil nil nil (car articles))))
9653       (setq to-newsgroup (gnus-read-move-group-name
9654                           (cadr (assq action names))
9655                           (symbol-value
9656                            (intern (format "gnus-current-%s-group" action)))
9657                           articles prefix)
9658             encoded to-newsgroup
9659             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9660       (set (intern (format "gnus-current-%s-group" action))
9661            (mm-decode-coding-string
9662             to-newsgroup
9663             (gnus-group-name-charset to-method to-newsgroup))))
9664     (unless to-method
9665       (setq to-method (or select-method
9666                           (gnus-server-to-method
9667                            (gnus-group-method to-newsgroup)))))
9668     (setq to-newsgroup
9669           (or encoded
9670               (and to-newsgroup
9671                    (mm-encode-coding-string
9672                     to-newsgroup
9673                     (gnus-group-name-charset to-method to-newsgroup)))))
9674     ;; Check the method we are to move this article to...
9675     (unless (gnus-check-backend-function
9676              'request-accept-article (car to-method))
9677       (error "%s does not support article copying" (car to-method)))
9678     (unless (gnus-check-server to-method)
9679       (error "Can't open server %s" (car to-method)))
9680     (gnus-message 6 "%s to %s: %s..."
9681                   (caddr (assq action names))
9682                   (or (car select-method)
9683                       (gnus-group-decoded-name to-newsgroup))
9684                   articles)
9685     (while articles
9686       (setq article (pop articles))
9687       (setq
9688        art-group
9689        (cond
9690         ;; Move the article.
9691         ((eq action 'move)
9692          ;; Remove this article from future suppression.
9693          (gnus-dup-unsuppress-article article)
9694          (let* ((from-method (gnus-find-method-for-group
9695                               gnus-newsgroup-name))
9696                 (to-method (or select-method
9697                                (gnus-find-method-for-group to-newsgroup)))
9698                 (move-is-internal (gnus-server-equal from-method to-method)))
9699            (gnus-request-move-article
9700             article                     ; Article to move
9701             gnus-newsgroup-name         ; From newsgroup
9702             (nth 1 (gnus-find-method-for-group
9703                     gnus-newsgroup-name)) ; Server
9704             (list 'gnus-request-accept-article
9705                   to-newsgroup (list 'quote select-method)
9706                   (not articles) t)     ; Accept form
9707             (not articles)              ; Only save nov last time
9708             (and move-is-internal
9709                  to-newsgroup           ; Not respooling
9710                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9711         ;; Copy the article.
9712         ((eq action 'copy)
9713          (with-current-buffer copy-buf
9714            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9715              (save-restriction
9716                (nnheader-narrow-to-headers)
9717                (dolist (hdr gnus-copy-article-ignored-headers)
9718                  (message-remove-header hdr t)))
9719              (gnus-request-accept-article
9720               to-newsgroup select-method (not articles) t))))
9721         ;; Crosspost the article.
9722         ((eq action 'crosspost)
9723          (let ((xref (message-tokenize-header
9724                       (mail-header-xref (gnus-summary-article-header article))
9725                       " ")))
9726            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9727                                   ":" (number-to-string article)))
9728            (unless xref
9729              (setq xref (list (system-name))))
9730            (setq new-xref
9731                  (concat
9732                   (mapconcat 'identity
9733                              (delete "Xref:" (delete new-xref xref))
9734                              " ")
9735                   " " new-xref))
9736            (with-current-buffer copy-buf
9737              ;; First put the article in the destination group.
9738              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9739              (when (consp (setq art-group
9740                                 (gnus-request-accept-article
9741                                  to-newsgroup select-method (not articles) t)))
9742                (setq new-xref (concat new-xref " " (car art-group)
9743                                       ":"
9744                                       (number-to-string (cdr art-group))))
9745                ;; Now we have the new Xrefs header, so we insert
9746                ;; it and replace the new article.
9747                (nnheader-replace-header "Xref" new-xref)
9748                (gnus-request-replace-article
9749                 (cdr art-group) to-newsgroup (current-buffer) t)
9750                art-group))))))
9751       (cond
9752        ((not art-group)
9753         (gnus-message 1 "Couldn't %s article %s: %s"
9754                       (cadr (assq action names)) article
9755                       (nnheader-get-report (car to-method))))
9756        ((eq art-group 'junk)
9757         (when (eq action 'move)
9758           (gnus-summary-mark-article article gnus-canceled-mark)
9759           (gnus-message 4 "Deleted article %s" article)
9760           ;; run the delete hook
9761           (run-hook-with-args 'gnus-summary-article-delete-hook
9762                               action
9763                               (gnus-data-header
9764                                (assoc article (gnus-data-list nil)))
9765                               gnus-newsgroup-name nil
9766                               select-method)))
9767        (t
9768         (let* ((pto-group (gnus-group-prefixed-name
9769                            (car art-group) to-method))
9770                (info (gnus-get-info pto-group))
9771                (to-group (gnus-info-group info))
9772                to-marks)
9773           ;; Update the group that has been moved to.
9774           (when (and info
9775                      (memq action '(move copy)))
9776             (unless (member to-group to-groups)
9777               (push to-group to-groups))
9778
9779             (unless (memq article gnus-newsgroup-unreads)
9780               (push 'read to-marks)
9781               (gnus-info-set-read
9782                info (gnus-add-to-range (gnus-info-read info)
9783                                        (list (cdr art-group)))))
9784
9785             ;; See whether the article is to be put in the cache.
9786             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9787                    (marks (if expirable
9788                               gnus-article-mark-lists
9789                             (delete '(expirable . expire)
9790                                     (copy-sequence gnus-article-mark-lists))))
9791                    (to-article (cdr art-group)))
9792
9793               ;; Enter the article into the cache in the new group,
9794               ;; if that is required.
9795               (when gnus-use-cache
9796                 (gnus-cache-possibly-enter-article
9797                  to-group to-article
9798                  (memq article gnus-newsgroup-marked)
9799                  (memq article gnus-newsgroup-dormant)
9800                  (memq article gnus-newsgroup-unreads)))
9801
9802               (when gnus-preserve-marks
9803                 ;; Copy any marks over to the new group.
9804                 (when (and (equal to-group gnus-newsgroup-name)
9805                            (not (memq article gnus-newsgroup-unreads)))
9806                   ;; Mark this article as read in this group.
9807                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9808                   ;; Increase the active status of this group.
9809                   (setcdr (gnus-active to-group) to-article)
9810                   (setcdr gnus-newsgroup-active to-article))
9811
9812                 (while marks
9813                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9814                     (when (memq article (symbol-value
9815                                          (intern (format "gnus-newsgroup-%s"
9816                                                          (caar marks)))))
9817                       (push (cdar marks) to-marks)
9818                       ;; If the other group is the same as this group,
9819                       ;; then we have to add the mark to the list.
9820                       (when (equal to-group gnus-newsgroup-name)
9821                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9822                              (cons to-article
9823                                    (symbol-value
9824                                     (intern (format "gnus-newsgroup-%s"
9825                                                     (caar marks)))))))
9826                       ;; Copy the marks to other group.
9827                       (gnus-add-marked-articles
9828                        to-group (cdar marks) (list to-article) info)))
9829                   (setq marks (cdr marks)))
9830
9831                 (when (and expirable
9832                            gnus-mark-copied-or-moved-articles-as-expirable
9833                            (not (memq 'expire to-marks)))
9834                   ;; Mark this article as expirable.
9835                   (push 'expire to-marks)
9836                   (when (equal to-group gnus-newsgroup-name)
9837                     (push to-article gnus-newsgroup-expirable))
9838                   ;; Copy the expirable mark to other group.
9839                   (gnus-add-marked-articles
9840                    to-group 'expire (list to-article) info))
9841
9842                 (when to-marks
9843                   (gnus-request-set-mark
9844                    to-group (list (list (list to-article) 'add to-marks)))))
9845
9846               (gnus-dribble-enter
9847                (concat "(gnus-group-set-info '"
9848                        (gnus-prin1-to-string (gnus-get-info to-group))
9849                        ")"))))
9850
9851           ;; Update the Xref header in this article to point to
9852           ;; the new crossposted article we have just created.
9853           (when (eq action 'crosspost)
9854             (with-current-buffer copy-buf
9855               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9856               (nnheader-replace-header "Xref" new-xref)
9857               (gnus-request-replace-article
9858                article gnus-newsgroup-name (current-buffer) t)))
9859
9860           ;; run the move/copy/crosspost/respool hook
9861           (run-hook-with-args 'gnus-summary-article-move-hook
9862                               action
9863                               (gnus-data-header
9864                                (assoc article (gnus-data-list nil)))
9865                               gnus-newsgroup-name
9866                               to-newsgroup
9867                               select-method))
9868
9869         ;;;!!!Why is this necessary?
9870         (set-buffer gnus-summary-buffer)
9871
9872         (when (eq action 'move)
9873           (save-excursion
9874             (gnus-summary-goto-subject article)
9875             (gnus-summary-mark-article article gnus-canceled-mark)))))
9876       (push article articles-to-update-marks))
9877
9878     (save-excursion
9879       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9880     ;; Re-activate all groups that have been moved to.
9881     (with-current-buffer gnus-group-buffer
9882       (let ((gnus-group-marked to-groups))
9883         (gnus-group-get-new-news-this-group nil t)))
9884
9885     (gnus-kill-buffer copy-buf)
9886     (gnus-summary-position-point)
9887     (gnus-set-mode-line 'summary)))
9888
9889 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9890   "Copy the current article to some other group.
9891 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9892 When called interactively, if TO-NEWSGROUP is nil, use the value of
9893 the variable `gnus-move-split-methods' for finding a default target
9894 newsgroup.
9895 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9896 re-spool using this method."
9897   (interactive "P")
9898   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9899
9900 (defun gnus-summary-crosspost-article (&optional n)
9901   "Crosspost the current article to some other group."
9902   (interactive "P")
9903   (gnus-summary-move-article n nil nil 'crosspost))
9904
9905 (defcustom gnus-summary-respool-default-method nil
9906   "Default method type for respooling an article.
9907 If nil, use to the current newsgroup method."
9908   :type 'symbol
9909   :group 'gnus-summary-mail)
9910
9911 (defun gnus-summary-respool-article (&optional n method)
9912   "Respool the current article.
9913 The article will be squeezed through the mail spooling process again,
9914 which means that it will be put in some mail newsgroup or other
9915 depending on `nnmail-split-methods'.
9916 If N is a positive number, respool the N next articles.
9917 If N is a negative number, respool the N previous articles.
9918 If N is nil and any articles have been marked with the process mark,
9919 respool those articles instead.
9920
9921 Respooling can be done both from mail groups and \"real\" newsgroups.
9922 In the former case, the articles in question will be moved from the
9923 current group into whatever groups they are destined to.  In the
9924 latter case, they will be copied into the relevant groups."
9925   (interactive
9926    (list current-prefix-arg
9927          (let* ((methods (gnus-methods-using 'respool))
9928                 (methname
9929                  (symbol-name (or gnus-summary-respool-default-method
9930                                   (car (gnus-find-method-for-group
9931                                         gnus-newsgroup-name)))))
9932                 (method
9933                  (gnus-completing-read-with-default
9934                   methname "Backend to use when respooling"
9935                   methods nil t nil 'gnus-mail-method-history))
9936                 ms)
9937            (cond
9938             ((zerop (length (setq ms (gnus-servers-using-backend
9939                                       (intern method)))))
9940              (list (intern method) ""))
9941             ((= 1 (length ms))
9942              (car ms))
9943             (t
9944              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9945                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9946                            ms-alist))))))))
9947   (unless method
9948     (error "No method given for respooling"))
9949   (if (assoc (symbol-name
9950               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9951              (gnus-methods-using 'respool))
9952       (gnus-summary-move-article n nil method)
9953     (gnus-summary-copy-article n nil method)))
9954
9955 (defun gnus-summary-import-article (file &optional edit)
9956   "Import an arbitrary file into a mail newsgroup."
9957   (interactive "fImport file: \nP")
9958   (let ((group gnus-newsgroup-name)
9959         (now (current-time))
9960         atts lines group-art)
9961     (unless (gnus-check-backend-function 'request-accept-article group)
9962       (error "%s does not support article importing" group))
9963     (or (file-readable-p file)
9964         (not (file-regular-p file))
9965         (error "Can't read %s" file))
9966     (with-current-buffer (gnus-get-buffer-create " *import file*")
9967       (erase-buffer)
9968       (nnheader-insert-file-contents file)
9969       (goto-char (point-min))
9970       (if (nnheader-article-p)
9971           (save-restriction
9972             (goto-char (point-min))
9973             (search-forward "\n\n" nil t)
9974             (narrow-to-region (point-min) (1- (point)))
9975             (goto-char (point-min))
9976             (unless (re-search-forward "^date:" nil t)
9977               (goto-char (point-max))
9978               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9979        ;; This doesn't look like an article, so we fudge some headers.
9980         (setq atts (file-attributes file)
9981               lines (count-lines (point-min) (point-max)))
9982         (insert "From: " (read-string "From: ") "\n"
9983                 "Subject: " (read-string "Subject: ") "\n"
9984                 "Date: " (message-make-date (nth 5 atts)) "\n"
9985                 "Message-ID: " (message-make-message-id) "\n"
9986                 "Lines: " (int-to-string lines) "\n"
9987                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9988       (setq group-art (gnus-request-accept-article group nil t))
9989       (kill-buffer (current-buffer)))
9990     (setq gnus-newsgroup-active (gnus-activate-group group))
9991     (forward-line 1)
9992     (gnus-summary-goto-article (cdr group-art) nil t)
9993     (when edit
9994       (gnus-summary-edit-article))))
9995
9996 (defun gnus-summary-create-article ()
9997   "Create an article in a mail newsgroup."
9998   (interactive)
9999   (let ((group gnus-newsgroup-name)
10000         (now (current-time))
10001         group-art)
10002     (unless (gnus-check-backend-function 'request-accept-article group)
10003       (error "%s does not support article importing" group))
10004     (with-current-buffer (gnus-get-buffer-create " *import file*")
10005       (erase-buffer)
10006       (goto-char (point-min))
10007       ;; This doesn't look like an article, so we fudge some headers.
10008       (insert "From: " (read-string "From: ") "\n"
10009               "Subject: " (read-string "Subject: ") "\n"
10010               "Date: " (message-make-date now) "\n"
10011               "Message-ID: " (message-make-message-id) "\n")
10012       (setq group-art (gnus-request-accept-article group nil t))
10013       (kill-buffer (current-buffer)))
10014     (setq gnus-newsgroup-active (gnus-activate-group group))
10015     (forward-line 1)
10016     (gnus-summary-goto-article (cdr group-art) nil t)
10017     (gnus-summary-edit-article)))
10018
10019 (defun gnus-summary-article-posted-p ()
10020   "Say whether the current (mail) article is available from news as well.
10021 This will be the case if the article has both been mailed and posted."
10022   (interactive)
10023   (let ((id (mail-header-references (gnus-summary-article-header)))
10024         (gnus-override-method (car (gnus-refer-article-methods))))
10025     (if (gnus-request-head id "")
10026         (gnus-message 2 "The current message was found on %s"
10027                       gnus-override-method)
10028       (gnus-message 2 "The current message couldn't be found on %s"
10029                     gnus-override-method)
10030       nil)))
10031
10032 (defun gnus-summary-expire-articles (&optional now)
10033   "Expire all articles that are marked as expirable in the current group."
10034   (interactive)
10035   (when (and (not gnus-group-is-exiting-without-update-p)
10036              (gnus-check-backend-function
10037               'request-expire-articles gnus-newsgroup-name))
10038     ;; This backend supports expiry.
10039     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10040            (expirable (if total
10041                           (progn
10042                             ;; We need to update the info for
10043                             ;; this group for `gnus-list-of-read-articles'
10044                             ;; to give us the right answer.
10045                             (gnus-run-hooks 'gnus-exit-group-hook)
10046                             (gnus-summary-update-info)
10047                             (gnus-list-of-read-articles gnus-newsgroup-name))
10048                         (setq gnus-newsgroup-expirable
10049                               (sort gnus-newsgroup-expirable '<))))
10050            (expiry-wait (if now 'immediate
10051                           (gnus-group-find-parameter
10052                            gnus-newsgroup-name 'expiry-wait)))
10053            (nnmail-expiry-target
10054             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10055                 nnmail-expiry-target))
10056            es)
10057       (when expirable
10058         ;; There are expirable articles in this group, so we run them
10059         ;; through the expiry process.
10060         (gnus-message 6 "Expiring articles...")
10061         (unless (gnus-check-group gnus-newsgroup-name)
10062           (error "Can't open server for %s" gnus-newsgroup-name))
10063         ;; The list of articles that weren't expired is returned.
10064         (save-excursion
10065           (if expiry-wait
10066               (let ((nnmail-expiry-wait-function nil)
10067                     (nnmail-expiry-wait expiry-wait))
10068                 (setq es (gnus-request-expire-articles
10069                           expirable gnus-newsgroup-name)))
10070             (setq es (gnus-request-expire-articles
10071                       expirable gnus-newsgroup-name)))
10072           (unless total
10073             (setq gnus-newsgroup-expirable es))
10074           ;; We go through the old list of expirable, and mark all
10075           ;; really expired articles as nonexistent.
10076           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10077             (let ((gnus-use-cache nil))
10078               (dolist (article expirable)
10079                 (when (and (not (memq article es))
10080                            (gnus-data-find article))
10081                   (gnus-summary-mark-article article gnus-canceled-mark)
10082                   (run-hook-with-args 'gnus-summary-article-expire-hook
10083                                       'delete
10084                                       (gnus-data-header
10085                                        (assoc article (gnus-data-list nil)))
10086                                       gnus-newsgroup-name
10087                                       nil
10088                                       nil))))))
10089         (gnus-message 6 "Expiring articles...done")))))
10090
10091 (defun gnus-summary-expire-articles-now ()
10092   "Expunge all expirable articles in the current group.
10093 This means that *all* articles that are marked as expirable will be
10094 deleted forever, right now."
10095   (interactive)
10096   (or gnus-expert-user
10097       (gnus-yes-or-no-p
10098        "Are you really, really sure you want to delete all expirable messages? ")
10099       (error "Phew!"))
10100   (gnus-summary-expire-articles t))
10101
10102 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10103 (defun gnus-summary-delete-article (&optional n)
10104   "Delete the N next (mail) articles.
10105 This command actually deletes articles.  This is not a marking
10106 command.  The article will disappear forever from your life, never to
10107 return.
10108
10109 If N is negative, delete backwards.
10110 If N is nil and articles have been marked with the process mark,
10111 delete these instead.
10112
10113 If `gnus-novice-user' is non-nil you will be asked for
10114 confirmation before the articles are deleted."
10115   (interactive "P")
10116   (unless (gnus-check-backend-function 'request-expire-articles
10117                                        gnus-newsgroup-name)
10118     (error "The current newsgroup does not support article deletion"))
10119   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10120     (error "Couldn't open server"))
10121   ;; Compute the list of articles to delete.
10122   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10123         (nnmail-expiry-target 'delete)
10124         not-deleted)
10125     (if (and gnus-novice-user
10126              (not (gnus-yes-or-no-p
10127                    (format "Do you really want to delete %s forever? "
10128                            (if (> (length articles) 1)
10129                                (format "these %s articles" (length articles))
10130                              "this article")))))
10131         ()
10132       ;; Delete the articles.
10133       (setq not-deleted (gnus-request-expire-articles
10134                          articles gnus-newsgroup-name 'force))
10135       (save-excursion
10136         (while articles
10137           (gnus-summary-remove-process-mark (car articles))
10138           ;; The backend might not have been able to delete the article
10139           ;; after all.
10140           (unless (memq (car articles) not-deleted)
10141             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10142           (let* ((article (car articles))
10143                  (ghead  (gnus-data-header
10144                           (assoc article (gnus-data-list nil)))))
10145             (run-hook-with-args 'gnus-summary-article-delete-hook
10146                                 'delete ghead gnus-newsgroup-name nil
10147                                 nil))
10148           (setq articles (cdr articles))))
10149       (when not-deleted
10150         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10151     (gnus-summary-position-point)
10152     (gnus-set-mode-line 'summary)
10153     not-deleted))
10154
10155 (defun gnus-summary-edit-article (&optional arg)
10156   "Edit the current article.
10157 This will have permanent effect only in mail groups.
10158 If ARG is nil, edit the decoded articles.
10159 If ARG is 1, edit the raw articles.
10160 If ARG is 2, edit the raw articles even in read-only groups.
10161 If ARG is 3, edit the articles with the current handles.
10162 Otherwise, allow editing of articles even in read-only
10163 groups."
10164   (interactive "P")
10165   (let (force raw current-handles)
10166     (cond
10167      ((null arg))
10168      ((eq arg 1)
10169       (setq raw t))
10170      ((eq arg 2)
10171       (setq raw t
10172             force t))
10173      ((eq arg 3)
10174       (setq current-handles
10175             (and (gnus-buffer-live-p gnus-article-buffer)
10176                  (with-current-buffer gnus-article-buffer
10177                    (prog1
10178                        gnus-article-mime-handles
10179                      (setq gnus-article-mime-handles nil))))))
10180      (t
10181       (setq force t)))
10182     (when (and raw (not force)
10183                (member gnus-newsgroup-name '("nndraft:delayed"
10184                                              "nndraft:drafts"
10185                                              "nndraft:queue")))
10186       (error "Can't edit the raw article in group %s"
10187              gnus-newsgroup-name))
10188     (with-current-buffer gnus-summary-buffer
10189       (let ((mail-parse-charset gnus-newsgroup-charset)
10190             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10191         (gnus-set-global-variables)
10192         (when (and (not force)
10193                    (gnus-group-read-only-p))
10194           (error "The current newsgroup does not support article editing"))
10195         (gnus-summary-show-article t)
10196         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10197           (with-current-buffer gnus-article-buffer
10198             (mm-enable-multibyte)))
10199         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10200             (setq raw t))
10201         (gnus-article-edit-article
10202          (if raw 'ignore
10203            `(lambda ()
10204               (let ((mbl mml-buffer-list))
10205                 (setq mml-buffer-list nil)
10206                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10207                   (mime-to-mml ,'current-handles))
10208                 (let ((mbl1 mml-buffer-list))
10209                   (setq mml-buffer-list mbl)
10210                   (set (make-local-variable 'mml-buffer-list) mbl1))
10211                 (gnus-make-local-hook 'kill-buffer-hook)
10212                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10213          `(lambda (no-highlight)
10214             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10215                   (message-options message-options)
10216                   (message-options-set-recipient)
10217                   (mail-parse-ignored-charsets
10218                    ',gnus-newsgroup-ignored-charsets)
10219                   (rfc2047-header-encoding-alist
10220                    ',(let ((charset (gnus-group-name-charset
10221                                      (gnus-find-method-for-group
10222                                       gnus-newsgroup-name)
10223                                      gnus-newsgroup-name)))
10224                        (append (list (cons "Newsgroups" charset)
10225                                      (cons "Followup-To" charset)
10226                                      (cons "Xref" charset))
10227                                rfc2047-header-encoding-alist))))
10228               ,(if (not raw) '(progn
10229                                 (mml-to-mime)
10230                                 (mml-destroy-buffers)
10231                                 (remove-hook 'kill-buffer-hook
10232                                              'mml-destroy-buffers t)
10233                                 (kill-local-variable 'mml-buffer-list)))
10234               (gnus-summary-edit-article-done
10235                ,(or (mail-header-references gnus-current-headers) "")
10236                ,(gnus-group-read-only-p)
10237                ,gnus-summary-buffer no-highlight))))))))
10238
10239 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10240
10241 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10242                                                  no-highlight)
10243   "Make edits to the current article permanent."
10244   (interactive)
10245   (save-excursion
10246    ;; The buffer restriction contains the entire article if it exists.
10247     (when (article-goto-body)
10248       (let ((lines (count-lines (point) (point-max)))
10249             (length (- (point-max) (point)))
10250             (case-fold-search t)
10251             (body (copy-marker (point))))
10252         (goto-char (point-min))
10253         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10254           (delete-region (match-beginning 1) (match-end 1))
10255           (insert (number-to-string length)))
10256         (goto-char (point-min))
10257         (when (re-search-forward
10258                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10259           (delete-region (match-beginning 1) (match-end 1))
10260           (insert (number-to-string length)))
10261         (goto-char (point-min))
10262         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10263           (delete-region (match-beginning 1) (match-end 1))
10264           (insert (number-to-string lines))))))
10265   ;; Replace the article.
10266   (let ((buf (current-buffer)))
10267     (with-temp-buffer
10268       (insert-buffer-substring buf)
10269
10270       (if (and (not read-only)
10271                (not (gnus-request-replace-article
10272                      (cdr gnus-article-current) (car gnus-article-current)
10273                      (current-buffer) t)))
10274           (error "Couldn't replace article")
10275         ;; Update the summary buffer.
10276         (if (and references
10277                  (equal (message-tokenize-header references " ")
10278                         (message-tokenize-header
10279                          (or (message-fetch-field "references") "") " ")))
10280             ;; We only have to update this line.
10281             (save-excursion
10282               (save-restriction
10283                 (message-narrow-to-head)
10284                 (let ((head (buffer-substring-no-properties
10285                              (point-min) (point-max)))
10286                       header)
10287                   (with-temp-buffer
10288                     (insert (format "211 %d Article retrieved.\n"
10289                                     (cdr gnus-article-current)))
10290                     (insert head)
10291                     (insert ".\n")
10292                     (let ((nntp-server-buffer (current-buffer)))
10293                       (setq header (car (gnus-get-newsgroup-headers
10294                                          nil t))))
10295                     (with-current-buffer gnus-summary-buffer
10296                       (gnus-data-set-header
10297                        (gnus-data-find (cdr gnus-article-current))
10298                        header)
10299                       (gnus-summary-update-article-line
10300                        (cdr gnus-article-current) header)
10301                       (if (gnus-summary-goto-subject
10302                            (cdr gnus-article-current) nil t)
10303                           (gnus-summary-update-secondary-mark
10304                            (cdr gnus-article-current))))))))
10305           ;; Update threads.
10306           (set-buffer (or buffer gnus-summary-buffer))
10307           (gnus-summary-update-article (cdr gnus-article-current))
10308           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10309               (gnus-summary-update-secondary-mark
10310                (cdr gnus-article-current))))
10311         ;; Prettify the article buffer again.
10312         (unless no-highlight
10313           (with-current-buffer gnus-article-buffer
10314             ;;;!!! Fix this -- article should be rehighlighted.
10315             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10316             (set-buffer gnus-original-article-buffer)
10317             (gnus-request-article
10318              (cdr gnus-article-current)
10319              (car gnus-article-current) (current-buffer))))
10320         ;; Prettify the summary buffer line.
10321         (when (gnus-visual-p 'summary-highlight 'highlight)
10322           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10323
10324 (defun gnus-summary-edit-wash (key)
10325   "Perform editing command KEY in the article buffer."
10326   (interactive
10327    (list
10328     (progn
10329       (message "%s" (concat (this-command-keys) "- "))
10330       (read-char))))
10331   (message "")
10332   (gnus-summary-edit-article)
10333   (execute-kbd-macro (concat (this-command-keys) key))
10334   (gnus-article-edit-done))
10335
10336 ;;; Respooling
10337
10338 (defun gnus-summary-respool-query (&optional silent trace)
10339   "Query where the respool algorithm would put this article."
10340   (interactive)
10341   (let (gnus-mark-article-hook)
10342     (gnus-summary-select-article)
10343     (with-current-buffer gnus-original-article-buffer
10344       (let ((groups (nnmail-article-group 'identity trace)))
10345         (unless silent
10346           (if groups
10347               (message "This message would go to %s"
10348                        (mapconcat 'car groups ", "))
10349             (message "This message would go to no groups"))
10350           groups)))))
10351
10352 (defun gnus-summary-respool-trace ()
10353   "Trace where the respool algorithm would put this article.
10354 Display a buffer showing all fancy splitting patterns which matched."
10355   (interactive)
10356   (gnus-summary-respool-query nil t))
10357
10358 ;; Summary marking commands.
10359
10360 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10361   "Mark articles which has the same subject as read, and then select the next.
10362 If UNMARK is positive, remove any kind of mark.
10363 If UNMARK is negative, tick articles."
10364   (interactive "P")
10365   (when unmark
10366     (setq unmark (prefix-numeric-value unmark)))
10367   (let ((count
10368          (gnus-summary-mark-same-subject
10369           (gnus-summary-article-subject) unmark)))
10370     ;; Select next unread article.  If auto-select-same mode, should
10371     ;; select the first unread article.
10372     (gnus-summary-next-article t (and gnus-auto-select-same
10373                                       (gnus-summary-article-subject)))
10374     (gnus-message 7 "%d article%s marked as %s"
10375                   count (if (= count 1) " is" "s are")
10376                   (if unmark "unread" "read"))))
10377
10378 (defun gnus-summary-kill-same-subject (&optional unmark)
10379   "Mark articles which has the same subject as read.
10380 If UNMARK is positive, remove any kind of mark.
10381 If UNMARK is negative, tick articles."
10382   (interactive "P")
10383   (when unmark
10384     (setq unmark (prefix-numeric-value unmark)))
10385   (let ((count
10386          (gnus-summary-mark-same-subject
10387           (gnus-summary-article-subject) unmark)))
10388     ;; If marked as read, go to next unread subject.
10389     (when (null unmark)
10390       ;; Go to next unread subject.
10391       (gnus-summary-next-subject 1 t))
10392     (gnus-message 7 "%d articles are marked as %s"
10393                   count (if unmark "unread" "read"))))
10394
10395 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10396   "Mark articles with same SUBJECT as read, and return marked number.
10397 If optional argument UNMARK is positive, remove any kinds of marks.
10398 If optional argument UNMARK is negative, mark articles as unread instead."
10399   (let ((count 1))
10400     (save-excursion
10401       (cond
10402        ((null unmark)                   ; Mark as read.
10403         (while (and
10404                 (progn
10405                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10406                   (gnus-summary-show-thread) t)
10407                 (gnus-summary-find-subject subject))
10408           (setq count (1+ count))))
10409        ((> unmark 0)                    ; Tick.
10410         (while (and
10411                 (progn
10412                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10413                   (gnus-summary-show-thread) t)
10414                 (gnus-summary-find-subject subject))
10415           (setq count (1+ count))))
10416        (t                               ; Mark as unread.
10417         (while (and
10418                 (progn
10419                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10420                   (gnus-summary-show-thread) t)
10421                 (gnus-summary-find-subject subject))
10422           (setq count (1+ count)))))
10423       (gnus-set-mode-line 'summary)
10424       ;; Return the number of marked articles.
10425       count)))
10426
10427 (defun gnus-summary-mark-as-processable (n &optional unmark)
10428   "Set the process mark on the next N articles.
10429 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10430 the process mark instead.  The difference between N and the actual
10431 number of articles marked is returned."
10432   (interactive "P")
10433   (if (and (null n) (gnus-region-active-p))
10434       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10435     (setq n (prefix-numeric-value n))
10436     (let ((backward (< n 0))
10437           (n (abs n)))
10438       (while (and
10439               (> n 0)
10440               (if unmark
10441                   (gnus-summary-remove-process-mark
10442                    (gnus-summary-article-number))
10443                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10444               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10445         (setq n (1- n)))
10446       (when (/= 0 n)
10447         (gnus-message 7 "No more articles"))
10448       (gnus-summary-recenter)
10449       (gnus-summary-position-point)
10450       n)))
10451
10452 (defun gnus-summary-unmark-as-processable (n)
10453   "Remove the process mark from the next N articles.
10454 If N is negative, unmark backward instead.  The difference between N and
10455 the actual number of articles unmarked is returned."
10456   (interactive "P")
10457   (gnus-summary-mark-as-processable n t))
10458
10459 (defun gnus-summary-unmark-all-processable ()
10460   "Remove the process mark from all articles."
10461   (interactive)
10462   (save-excursion
10463     (while gnus-newsgroup-processable
10464       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10465   (gnus-summary-position-point))
10466
10467 (defun gnus-summary-add-mark (article type)
10468   "Mark ARTICLE with a mark of TYPE."
10469   (let ((vtype (car (assq type gnus-article-mark-lists)))
10470         var)
10471     (if (not vtype)
10472         (error "No such mark type: %s" type)
10473       (setq var (intern (format "gnus-newsgroup-%s" type)))
10474       (set var (cons article (symbol-value var)))
10475       (if (memq type '(processable cached replied forwarded recent saved))
10476           (gnus-summary-update-secondary-mark article)
10477         ;;; !!! This is bogus.  We should find out what primary
10478         ;;; !!! mark we want to set.
10479         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10480
10481 (defun gnus-summary-mark-as-expirable (n)
10482   "Mark N articles forward as expirable.
10483 If N is negative, mark backward instead.  The difference between N and
10484 the actual number of articles marked is returned."
10485   (interactive "p")
10486   (gnus-summary-mark-forward n gnus-expirable-mark))
10487
10488 (defun gnus-summary-mark-as-spam (n)
10489   "Mark N articles forward as spam.
10490 If N is negative, mark backward instead.  The difference between N and
10491 the actual number of articles marked is returned."
10492   (interactive "p")
10493   (gnus-summary-mark-forward n gnus-spam-mark))
10494
10495 (defun gnus-summary-mark-article-as-replied (article)
10496   "Mark ARTICLE as replied to and update the summary line.
10497 ARTICLE can also be a list of articles."
10498   (interactive (list (gnus-summary-article-number)))
10499   (let ((articles (if (listp article) article (list article))))
10500     (dolist (article articles)
10501       (unless (numberp article)
10502         (error "%s is not a number" article))
10503       (push article gnus-newsgroup-replied)
10504       (let ((inhibit-read-only t))
10505         (when (gnus-summary-goto-subject article nil t)
10506           (gnus-summary-update-secondary-mark article))))))
10507
10508 (defun gnus-summary-mark-article-as-forwarded (article)
10509   "Mark ARTICLE as forwarded and update the summary line.
10510 ARTICLE can also be a list of articles."
10511   (let ((articles (if (listp article) article (list article))))
10512     (dolist (article articles)
10513       (push article gnus-newsgroup-forwarded)
10514       (let ((inhibit-read-only t))
10515         (when (gnus-summary-goto-subject article nil t)
10516           (gnus-summary-update-secondary-mark article))))))
10517
10518 (defun gnus-summary-set-bookmark (article)
10519   "Set a bookmark in current article."
10520   (interactive (list (gnus-summary-article-number)))
10521   (when (or (not (get-buffer gnus-article-buffer))
10522             (not gnus-current-article)
10523             (not gnus-article-current)
10524             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10525     (error "No current article selected"))
10526   ;; Remove old bookmark, if one exists.
10527   (gnus-pull article gnus-newsgroup-bookmarks)
10528   ;; Set the new bookmark, which is on the form
10529   ;; (article-number . line-number-in-body).
10530   (push
10531    (cons article
10532          (with-current-buffer gnus-article-buffer
10533            (count-lines
10534             (min (point)
10535                  (save-excursion
10536                    (article-goto-body)
10537                    (point)))
10538             (point))))
10539    gnus-newsgroup-bookmarks)
10540   (gnus-message 6 "A bookmark has been added to the current article."))
10541
10542 (defun gnus-summary-remove-bookmark (article)
10543   "Remove the bookmark from the current article."
10544   (interactive (list (gnus-summary-article-number)))
10545   ;; Remove old bookmark, if one exists.
10546   (if (not (assq article gnus-newsgroup-bookmarks))
10547       (gnus-message 6 "No bookmark in current article.")
10548     (gnus-pull article gnus-newsgroup-bookmarks)
10549     (gnus-message 6 "Removed bookmark.")))
10550
10551 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10552 (defun gnus-summary-mark-as-dormant (n)
10553   "Mark N articles forward as dormant.
10554 If N is negative, mark backward instead.  The difference between N and
10555 the actual number of articles marked is returned."
10556   (interactive "p")
10557   (gnus-summary-mark-forward n gnus-dormant-mark))
10558
10559 (defun gnus-summary-set-process-mark (article)
10560   "Set the process mark on ARTICLE and update the summary line."
10561   (setq gnus-newsgroup-processable
10562         (cons article
10563               (delq article gnus-newsgroup-processable)))
10564   (when (gnus-summary-goto-subject article)
10565     (gnus-summary-show-thread)
10566     (gnus-summary-goto-subject article)
10567     (gnus-summary-update-secondary-mark article)))
10568
10569 (defun gnus-summary-remove-process-mark (&rest articles)
10570   "Remove the process mark from ARTICLES and update the summary line."
10571   (dolist (article articles)
10572     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10573     (when (gnus-summary-goto-subject article)
10574       (gnus-summary-show-thread)
10575       (gnus-summary-goto-subject article)
10576       (gnus-summary-update-secondary-mark article)))
10577   t)
10578
10579 (defun gnus-summary-set-saved-mark (article)
10580   "Set the process mark on ARTICLE and update the summary line."
10581   (push article gnus-newsgroup-saved)
10582   (when (gnus-summary-goto-subject article)
10583     (gnus-summary-update-secondary-mark article)))
10584
10585 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10586   "Mark N articles as read forwards.
10587 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10588 The difference between N and the actual number of articles marked is
10589 returned.
10590 If NO-EXPIRE, auto-expiry will be inhibited."
10591   (interactive "p")
10592   (gnus-summary-show-thread)
10593   (let ((backward (< n 0))
10594         (gnus-summary-goto-unread
10595          (and gnus-summary-goto-unread
10596               (not (eq gnus-summary-goto-unread 'never))
10597               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10598                                     gnus-ticked-mark gnus-dormant-mark)))))
10599         (n (abs n))
10600         (mark (or mark gnus-del-mark)))
10601     (while (and (> n 0)
10602                 (gnus-summary-mark-article nil mark no-expire)
10603                 (zerop (gnus-summary-next-subject
10604                         (if backward -1 1)
10605                         (and gnus-summary-goto-unread
10606                              (not (eq gnus-summary-goto-unread 'never)))
10607                         t)))
10608       (setq n (1- n)))
10609     (when (/= 0 n)
10610       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10611     (gnus-summary-recenter)
10612     (gnus-summary-position-point)
10613     (gnus-set-mode-line 'summary)
10614     n))
10615
10616 (defun gnus-summary-mark-article-as-read (mark)
10617   "Mark the current article quickly as read with MARK."
10618   (let ((article (gnus-summary-article-number)))
10619     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10620     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10621     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10622     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10623     (push (cons article mark) gnus-newsgroup-reads)
10624     ;; Possibly remove from cache, if that is used.
10625     (when gnus-use-cache
10626       (gnus-cache-enter-remove-article article))
10627     ;; Allow the backend to change the mark.
10628     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10629     ;; Check for auto-expiry.
10630     (when (and gnus-newsgroup-auto-expire
10631                (memq mark gnus-auto-expirable-marks))
10632       (setq mark gnus-expirable-mark)
10633       ;; Let the backend know about the mark change.
10634       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10635       (push article gnus-newsgroup-expirable))
10636     ;; Set the mark in the buffer.
10637     (gnus-summary-update-mark mark 'unread)
10638     t))
10639
10640 (defun gnus-summary-mark-article-as-unread (mark)
10641   "Mark the current article quickly as unread with MARK."
10642   (let* ((article (gnus-summary-article-number))
10643          (old-mark (gnus-summary-article-mark article)))
10644     ;; Allow the backend to change the mark.
10645     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10646     (if (eq mark old-mark)
10647         t
10648       (if (<= article 0)
10649           (progn
10650             (gnus-error 1 "Can't mark negative article numbers")
10651             nil)
10652         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10653         (setq gnus-newsgroup-spam-marked
10654               (delq article gnus-newsgroup-spam-marked))
10655         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10656         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10657         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10658         (cond ((= mark gnus-ticked-mark)
10659                (setq gnus-newsgroup-marked
10660                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10661                                               article)))
10662               ((= mark gnus-spam-mark)
10663                (setq gnus-newsgroup-spam-marked
10664                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10665                                               article)))
10666               ((= mark gnus-dormant-mark)
10667                (setq gnus-newsgroup-dormant
10668                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10669                                               article)))
10670               (t
10671                (setq gnus-newsgroup-unreads
10672                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10673                                               article))))
10674         (gnus-pull article gnus-newsgroup-reads)
10675
10676         ;; See whether the article is to be put in the cache.
10677         (and gnus-use-cache
10678              (vectorp (gnus-summary-article-header article))
10679              (save-excursion
10680                (gnus-cache-possibly-enter-article
10681                 gnus-newsgroup-name article
10682                 (= mark gnus-ticked-mark)
10683                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10684
10685         ;; Fix the mark.
10686         (gnus-summary-update-mark mark 'unread)
10687         t))))
10688
10689 (defun gnus-summary-mark-article (&optional article mark no-expire)
10690   "Mark ARTICLE with MARK.  MARK can be any character.
10691 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10692 `??' (dormant) and `?E' (expirable).
10693 If MARK is nil, then the default character `?r' is used.
10694 If ARTICLE is nil, then the article on the current line will be
10695 marked.
10696 If NO-EXPIRE, auto-expiry will be inhibited."
10697   ;; The mark might be a string.
10698   (when (stringp mark)
10699     (setq mark (aref mark 0)))
10700   ;; If no mark is given, then we check auto-expiring.
10701   (when (null mark)
10702     (setq mark gnus-del-mark))
10703   (when (and (not no-expire)
10704              gnus-newsgroup-auto-expire
10705              (memq mark gnus-auto-expirable-marks))
10706     (setq mark gnus-expirable-mark))
10707   (let ((article (or article (gnus-summary-article-number)))
10708         (old-mark (gnus-summary-article-mark article)))
10709     ;; Allow the backend to change the mark.
10710     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10711     (if (eq mark old-mark)
10712         t
10713       (unless article
10714         (error "No article on current line"))
10715       (if (not (if (or (= mark gnus-unread-mark)
10716                        (= mark gnus-ticked-mark)
10717                        (= mark gnus-spam-mark)
10718                        (= mark gnus-dormant-mark))
10719                    (gnus-mark-article-as-unread article mark)
10720                  (gnus-mark-article-as-read article mark)))
10721           t
10722         ;; See whether the article is to be put in the cache.
10723         (and gnus-use-cache
10724              (not (= mark gnus-canceled-mark))
10725              (vectorp (gnus-summary-article-header article))
10726              (save-excursion
10727                (gnus-cache-possibly-enter-article
10728                 gnus-newsgroup-name article
10729                 (= mark gnus-ticked-mark)
10730                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10731
10732         (when (gnus-summary-goto-subject article nil t)
10733           (let ((inhibit-read-only t))
10734             (gnus-summary-show-thread)
10735             ;; Fix the mark.
10736             (gnus-summary-update-mark mark 'unread)
10737             t))))))
10738
10739 (defun gnus-summary-update-secondary-mark (article)
10740   "Update the secondary (read, process, cache) mark."
10741   (gnus-summary-update-mark
10742    (cond ((memq article gnus-newsgroup-processable)
10743           gnus-process-mark)
10744          ((memq article gnus-newsgroup-cached)
10745           gnus-cached-mark)
10746          ((memq article gnus-newsgroup-replied)
10747           gnus-replied-mark)
10748          ((memq article gnus-newsgroup-forwarded)
10749           gnus-forwarded-mark)
10750          ((memq article gnus-newsgroup-saved)
10751           gnus-saved-mark)
10752          ((memq article gnus-newsgroup-recent)
10753           gnus-recent-mark)
10754          ((memq article gnus-newsgroup-unseen)
10755           gnus-unseen-mark)
10756          (t gnus-no-mark))
10757    'replied)
10758   (when (gnus-visual-p 'summary-highlight 'highlight)
10759     (gnus-summary-highlight-line)
10760     (gnus-run-hooks 'gnus-summary-update-hook))
10761   t)
10762
10763 (defun gnus-summary-update-download-mark (article)
10764   "Update the download mark."
10765   (gnus-summary-update-mark
10766    (cond ((memq article gnus-newsgroup-undownloaded)
10767           gnus-undownloaded-mark)
10768          (gnus-newsgroup-agentized
10769           gnus-downloaded-mark)
10770          (t
10771           gnus-no-mark))
10772    'download)
10773   (gnus-summary-update-line t)
10774   t)
10775
10776 (defun gnus-summary-update-mark (mark type)
10777   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10778         (inhibit-read-only t))
10779     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10780     (when forward
10781       (when (looking-at "\r")
10782         (incf forward))
10783       (when (<= (+ forward (point)) (point-max))
10784         ;; Go to the right position on the line.
10785         (goto-char (+ forward (point)))
10786         ;; Replace the old mark with the new mark.
10787         (let ((to-insert
10788                (mm-subst-char-in-string
10789                 (char-after) mark
10790                 (buffer-substring (point) (1+ (point))))))
10791           (delete-region (point) (1+ (point)))
10792           (insert to-insert))
10793         ;; Optionally update the marks by some user rule.
10794         (when (eq type 'unread)
10795           (gnus-data-set-mark
10796            (gnus-data-find (gnus-summary-article-number)) mark)
10797           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10798
10799 (defun gnus-mark-article-as-read (article &optional mark)
10800   "Enter ARTICLE in the pertinent lists and remove it from others."
10801   ;; Make the article expirable.
10802   (let ((mark (or mark gnus-del-mark)))
10803     (setq gnus-newsgroup-expirable
10804           (if (= mark gnus-expirable-mark)
10805               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10806             (delq article gnus-newsgroup-expirable)))
10807     ;; Remove from unread and marked lists.
10808     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10809     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10810     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10811     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10812     (push (cons article mark) gnus-newsgroup-reads)
10813     ;; Possibly remove from cache, if that is used.
10814     (when gnus-use-cache
10815       (gnus-cache-enter-remove-article article))
10816     t))
10817
10818 (defun gnus-mark-article-as-unread (article &optional mark)
10819   "Enter ARTICLE in the pertinent lists and remove it from others."
10820   (let ((mark (or mark gnus-ticked-mark)))
10821     (if (<= article 0)
10822         (progn
10823           (gnus-error 1 "Can't mark negative article numbers")
10824           nil)
10825       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10826             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10827             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10828             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10829             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10830
10831       ;; Unsuppress duplicates?
10832       (when gnus-suppress-duplicates
10833         (gnus-dup-unsuppress-article article))
10834
10835       (cond ((= mark gnus-ticked-mark)
10836              (setq gnus-newsgroup-marked
10837                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10838             ((= mark gnus-spam-mark)
10839              (setq gnus-newsgroup-spam-marked
10840                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10841                                             article)))
10842             ((= mark gnus-dormant-mark)
10843              (setq gnus-newsgroup-dormant
10844                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10845             (t
10846              (setq gnus-newsgroup-unreads
10847                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10848       (gnus-pull article gnus-newsgroup-reads)
10849       t)))
10850
10851 (defalias 'gnus-summary-mark-as-unread-forward
10852   'gnus-summary-tick-article-forward)
10853 (make-obsolete 'gnus-summary-mark-as-unread-forward
10854                'gnus-summary-tick-article-forward "Emacs 20.4")
10855 (defun gnus-summary-tick-article-forward (n)
10856   "Tick N articles forwards.
10857 If N is negative, tick backwards instead.
10858 The difference between N and the number of articles ticked is returned."
10859   (interactive "p")
10860   (gnus-summary-mark-forward n gnus-ticked-mark))
10861
10862 (defalias 'gnus-summary-mark-as-unread-backward
10863   'gnus-summary-tick-article-backward)
10864 (make-obsolete 'gnus-summary-mark-as-unread-backward
10865                'gnus-summary-tick-article-backward "Emacs 20.4")
10866 (defun gnus-summary-tick-article-backward (n)
10867   "Tick N articles backwards.
10868 The difference between N and the number of articles ticked is returned."
10869   (interactive "p")
10870   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10871
10872 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10873 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10874 (defun gnus-summary-tick-article (&optional article clear-mark)
10875   "Mark current article as unread.
10876 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10877 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10878   (interactive)
10879   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10880                                        gnus-ticked-mark)))
10881
10882 (defun gnus-summary-mark-as-read-forward (n)
10883   "Mark N articles as read forwards.
10884 If N is negative, mark backwards instead.
10885 The difference between N and the actual number of articles marked is
10886 returned."
10887   (interactive "p")
10888   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10889
10890 (defun gnus-summary-mark-as-read-backward (n)
10891   "Mark the N articles as read backwards.
10892 The difference between N and the actual number of articles marked is
10893 returned."
10894   (interactive "p")
10895   (gnus-summary-mark-forward
10896    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10897
10898 (defun gnus-summary-mark-as-read (&optional article mark)
10899   "Mark current article as read.
10900 ARTICLE specifies the article to be marked as read.
10901 MARK specifies a string to be inserted at the beginning of the line."
10902   (gnus-summary-mark-article article mark))
10903
10904 (defun gnus-summary-clear-mark-forward (n)
10905   "Clear marks from N articles forward.
10906 If N is negative, clear backward instead.
10907 The difference between N and the number of marks cleared is returned."
10908   (interactive "p")
10909   (gnus-summary-mark-forward n gnus-unread-mark))
10910
10911 (defun gnus-summary-clear-mark-backward (n)
10912   "Clear marks from N articles backward.
10913 The difference between N and the number of marks cleared is returned."
10914   (interactive "p")
10915   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10916
10917 (defun gnus-summary-mark-unread-as-read ()
10918   "Intended to be used by `gnus-mark-article-hook'."
10919   (when (memq gnus-current-article gnus-newsgroup-unreads)
10920     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10921
10922 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10923   "Intended to be used by `gnus-mark-article-hook'."
10924   (let ((mark (gnus-summary-article-mark)))
10925     (when (or (gnus-unread-mark-p mark)
10926               (gnus-read-mark-p mark))
10927       (gnus-summary-mark-article gnus-current-article
10928                                  (or new-mark gnus-read-mark)))))
10929
10930 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10931   "Intended to be used by `gnus-mark-article-hook'."
10932   (let ((mark (gnus-summary-article-mark)))
10933     (when (or (gnus-unread-mark-p mark)
10934               (gnus-read-mark-p mark))
10935       (gnus-summary-mark-article (gnus-summary-article-number)
10936                                  (or new-mark gnus-read-mark)))))
10937
10938 (defun gnus-summary-mark-unread-as-ticked ()
10939   "Intended to be used by `gnus-mark-article-hook'."
10940   (when (memq gnus-current-article gnus-newsgroup-unreads)
10941     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10942
10943 (defun gnus-summary-mark-region-as-read (point mark all)
10944   "Mark all unread articles between point and mark as read.
10945 If given a prefix, mark all articles between point and mark as read,
10946 even ticked and dormant ones."
10947   (interactive "r\nP")
10948   (save-excursion
10949     (let (article)
10950       (goto-char point)
10951       (beginning-of-line)
10952       (while (and
10953               (< (point) mark)
10954               (progn
10955                 (when (or all
10956                           (memq (setq article (gnus-summary-article-number))
10957                                 gnus-newsgroup-unreads))
10958                   (gnus-summary-mark-article article gnus-del-mark))
10959                 t)
10960               (gnus-summary-find-next))))))
10961
10962 (defun gnus-summary-mark-below (score mark)
10963   "Mark articles with score less than SCORE with MARK."
10964   (interactive "P\ncMark: ")
10965   (setq score (if score
10966                   (prefix-numeric-value score)
10967                 (or gnus-summary-default-score 0)))
10968   (with-current-buffer gnus-summary-buffer
10969     (goto-char (point-min))
10970     (while
10971         (progn
10972           (and (< (gnus-summary-article-score) score)
10973                (gnus-summary-mark-article nil mark))
10974           (gnus-summary-find-next)))))
10975
10976 (defun gnus-summary-kill-below (&optional score)
10977   "Mark articles with score below SCORE as read."
10978   (interactive "P")
10979   (gnus-summary-mark-below score gnus-killed-mark))
10980
10981 (defun gnus-summary-clear-above (&optional score)
10982   "Clear all marks from articles with score above SCORE."
10983   (interactive "P")
10984   (gnus-summary-mark-above score gnus-unread-mark))
10985
10986 (defun gnus-summary-tick-above (&optional score)
10987   "Tick all articles with score above SCORE."
10988   (interactive "P")
10989   (gnus-summary-mark-above score gnus-ticked-mark))
10990
10991 (defun gnus-summary-mark-above (score mark)
10992   "Mark articles with score over SCORE with MARK."
10993   (interactive "P\ncMark: ")
10994   (setq score (if score
10995                   (prefix-numeric-value score)
10996                 (or gnus-summary-default-score 0)))
10997   (with-current-buffer gnus-summary-buffer
10998     (goto-char (point-min))
10999     (while (and (progn
11000                   (when (> (gnus-summary-article-score) score)
11001                     (gnus-summary-mark-article nil mark))
11002                   t)
11003                 (gnus-summary-find-next)))))
11004
11005 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11006 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11007 (defun gnus-summary-limit-include-expunged (&optional no-error)
11008   "Display all the hidden articles that were expunged for low scores."
11009   (interactive)
11010   (let ((inhibit-read-only t))
11011     (let ((scored gnus-newsgroup-scored)
11012           headers h)
11013       (while scored
11014         (unless (gnus-summary-article-header (caar scored))
11015           (and (setq h (gnus-number-to-header (caar scored)))
11016                (< (cdar scored) gnus-summary-expunge-below)
11017                (push h headers)))
11018         (setq scored (cdr scored)))
11019       (if (not headers)
11020           (when (not no-error)
11021             (error "No expunged articles hidden"))
11022         (goto-char (point-min))
11023         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11024         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11025         (dolist (x headers)
11026           (push (mail-header-number x) gnus-newsgroup-limit))
11027         (gnus-summary-prepare-unthreaded (nreverse headers))
11028         (goto-char (point-min))
11029         (gnus-summary-position-point)
11030         t))))
11031
11032 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11033   "Mark all unread articles in this newsgroup as read.
11034 If prefix argument ALL is non-nil, ticked and dormant articles will
11035 also be marked as read.
11036 If QUIETLY is non-nil, no questions will be asked.
11037
11038 If TO-HERE is non-nil, it should be a point in the buffer.  All
11039 articles before (after, if REVERSE is set) this point will be marked
11040 as read.
11041
11042 Note that this function will only catch up the unread article
11043 in the current summary buffer limitation.
11044
11045 The number of articles marked as read is returned."
11046   (interactive "P")
11047   (prog1
11048       (save-excursion
11049         (when (or quietly
11050                   (not gnus-interactive-catchup) ;Without confirmation?
11051                   gnus-expert-user
11052                   (gnus-y-or-n-p
11053                    (if all
11054                        "Mark absolutely all articles as read? "
11055                      "Mark all unread articles as read? ")))
11056           (if (and not-mark
11057                    (not gnus-newsgroup-adaptive)
11058                    (not gnus-newsgroup-auto-expire)
11059                    (not gnus-suppress-duplicates)
11060                    (or (not gnus-use-cache)
11061                        (eq gnus-use-cache 'passive)))
11062               (progn
11063                 (when all
11064                   (setq gnus-newsgroup-marked nil
11065                         gnus-newsgroup-spam-marked nil
11066                         gnus-newsgroup-dormant nil))
11067                 (setq gnus-newsgroup-unreads
11068                       (gnus-sorted-nunion
11069                        (gnus-sorted-intersection gnus-newsgroup-unreads
11070                                                  gnus-newsgroup-downloadable)
11071                        (gnus-sorted-difference gnus-newsgroup-unfetched
11072                                                gnus-newsgroup-cached))))
11073             ;; We actually mark all articles as canceled, which we
11074             ;; have to do when using auto-expiry or adaptive scoring.
11075             (gnus-summary-show-all-threads)
11076             (if (and to-here reverse)
11077                 (progn
11078                   (goto-char to-here)
11079                   (gnus-summary-mark-current-read-and-unread-as-read
11080                    gnus-catchup-mark)
11081                   (while (gnus-summary-find-next (not all))
11082                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11083               (when (gnus-summary-first-subject (not all))
11084                 (while (and
11085                         (if to-here (< (point) to-here) t)
11086                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11087                         (gnus-summary-find-next (not all))))))
11088             (gnus-set-mode-line 'summary))
11089           t))
11090     (gnus-summary-position-point)))
11091
11092 (defun gnus-summary-catchup-to-here (&optional all)
11093   "Mark all unticked articles before the current one as read.
11094 If ALL is non-nil, also mark ticked and dormant articles as read."
11095   (interactive "P")
11096   (save-excursion
11097     (gnus-save-hidden-threads
11098       (let ((beg (point)))
11099         ;; We check that there are unread articles.
11100         (when (or all (gnus-summary-find-prev))
11101           (gnus-summary-catchup all t beg)))))
11102   (gnus-summary-position-point))
11103
11104 (defun gnus-summary-catchup-from-here (&optional all)
11105   "Mark all unticked articles after (and including) the current one as read.
11106 If ALL is non-nil, also mark ticked and dormant articles as read."
11107   (interactive "P")
11108   (save-excursion
11109     (gnus-save-hidden-threads
11110       (let ((beg (point)))
11111         ;; We check that there are unread articles.
11112         (when (or all (gnus-summary-find-next))
11113           (gnus-summary-catchup all t beg nil t)))))
11114   (gnus-summary-position-point))
11115
11116 (defun gnus-summary-catchup-all (&optional quietly)
11117   "Mark all articles in this newsgroup as read.
11118 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11119 instead, which marks only unread articles as read."
11120   (interactive "P")
11121   (gnus-summary-catchup t quietly))
11122
11123 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11124   "Mark all unread articles in this group as read, then exit.
11125 If prefix argument ALL is non-nil, all articles are marked as read.
11126 If QUIETLY is non-nil, no questions will be asked."
11127   (interactive "P")
11128   (when (gnus-summary-catchup all quietly nil 'fast)
11129     ;; Select next newsgroup or exit.
11130     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11131              (eq gnus-auto-select-next 'quietly))
11132         (gnus-summary-next-group nil)
11133       (gnus-summary-exit))))
11134
11135 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11136   "Mark all articles in this newsgroup as read, and then exit.
11137 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11138 instead, which marks only unread articles as read."
11139   (interactive "P")
11140   (gnus-summary-catchup-and-exit t quietly))
11141
11142 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11143   "Mark all articles in this group as read and select the next group.
11144 If given a prefix, mark all articles, unread as well as ticked, as
11145 read."
11146   (interactive "P")
11147   (save-excursion
11148     (gnus-summary-catchup all))
11149   (gnus-summary-next-group))
11150
11151 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11152   "Mark all articles in this group as read and select the previous group.
11153 If given a prefix, mark all articles, unread as well as ticked, as
11154 read."
11155   (interactive "P")
11156   (save-excursion
11157     (gnus-summary-catchup all))
11158   (gnus-summary-next-group nil nil t))
11159
11160 ;;;
11161 ;;; with article
11162 ;;;
11163
11164 (defmacro gnus-with-article (article &rest forms)
11165   "Select ARTICLE and perform FORMS in the original article buffer.
11166 Then replace the article with the result."
11167   `(progn
11168      ;; We don't want the article to be marked as read.
11169      (let (gnus-mark-article-hook)
11170        (gnus-summary-select-article t t nil ,article))
11171      (set-buffer gnus-original-article-buffer)
11172      ,@forms
11173      (if (not (gnus-check-backend-function
11174                'request-replace-article (car gnus-article-current)))
11175          (gnus-message 5 "Read-only group; not replacing")
11176        (unless (gnus-request-replace-article
11177                 ,article (car gnus-article-current)
11178                 (current-buffer) t)
11179          (error "Couldn't replace article")))
11180      ;; The cache and backlog have to be flushed somewhat.
11181      (when gnus-keep-backlog
11182        (gnus-backlog-remove-article
11183         (car gnus-article-current) (cdr gnus-article-current)))
11184      (when gnus-use-cache
11185        (gnus-cache-update-article
11186         (car gnus-article-current) (cdr gnus-article-current)))))
11187
11188 (put 'gnus-with-article 'lisp-indent-function 1)
11189 (put 'gnus-with-article 'edebug-form-spec '(form body))
11190
11191 ;; Thread-based commands.
11192
11193 (defun gnus-summary-articles-in-thread (&optional article)
11194   "Return a list of all articles in the current thread.
11195 If ARTICLE is non-nil, return all articles in the thread that starts
11196 with that article."
11197   (let* ((article (or article (gnus-summary-article-number)))
11198          (data (gnus-data-find-list article))
11199          (top-level (gnus-data-level (car data)))
11200          (top-subject
11201           (cond ((null gnus-thread-operation-ignore-subject)
11202                  (gnus-simplify-subject-re
11203                   (mail-header-subject (gnus-data-header (car data)))))
11204                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11205                  (gnus-simplify-subject-fuzzy
11206                   (mail-header-subject (gnus-data-header (car data)))))
11207                 (t nil)))
11208          (end-point (save-excursion
11209                       (if (gnus-summary-go-to-next-thread)
11210                           (point) (point-max))))
11211          articles)
11212     (while (and data
11213                 (< (gnus-data-pos (car data)) end-point))
11214       (when (or (not top-subject)
11215                 (string= top-subject
11216                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11217                              (gnus-simplify-subject-fuzzy
11218                               (mail-header-subject
11219                                (gnus-data-header (car data))))
11220                            (gnus-simplify-subject-re
11221                             (mail-header-subject
11222                              (gnus-data-header (car data)))))))
11223         (push (gnus-data-number (car data)) articles))
11224       (unless (and (setq data (cdr data))
11225                    (> (gnus-data-level (car data)) top-level))
11226         (setq data nil)))
11227     ;; Return the list of articles.
11228     (nreverse articles)))
11229
11230 (defun gnus-summary-rethread-current ()
11231   "Rethread the thread the current article is part of."
11232   (interactive)
11233   (let* ((gnus-show-threads t)
11234          (article (gnus-summary-article-number))
11235          (id (mail-header-id (gnus-summary-article-header)))
11236          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11237     (unless id
11238       (error "No article on the current line"))
11239     (gnus-rebuild-thread id)
11240     (gnus-summary-goto-subject article)))
11241
11242 (defun gnus-summary-reparent-thread ()
11243   "Make the current article child of the marked (or previous) article.
11244
11245 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11246 is non-nil or the Subject: of both articles are the same."
11247   (interactive)
11248   (unless (not (gnus-group-read-only-p))
11249     (error "The current newsgroup does not support article editing"))
11250   (unless (<= (length gnus-newsgroup-processable) 1)
11251     (error "No more than one article may be marked"))
11252   (let ((child (gnus-summary-article-number))
11253         ;; First grab the marked article, otherwise one line up.
11254         (parent (if (not (null gnus-newsgroup-processable))
11255                     (car gnus-newsgroup-processable)
11256                   (save-excursion
11257                     (if (eq (forward-line -1) 0)
11258                         (gnus-summary-article-number)
11259                       (error "Beginning of summary buffer"))))))
11260     (gnus-summary-reparent-children parent (list child))))
11261
11262 (defun gnus-summary-reparent-children (parent children)
11263   "Make PARENT the parent of CHILDREN.
11264 When called interactively, PARENT is the current article and CHILDREN
11265 are the process-marked articles."
11266   (interactive
11267    (list (gnus-summary-article-number)
11268          (gnus-summary-work-articles nil)))
11269   (dolist (child children)
11270     (save-window-excursion
11271       (let ((gnus-article-buffer " *reparent*"))
11272         (unless (not (eq parent child))
11273           (error "An article may not be self-referential"))
11274         (let ((message-id (mail-header-id
11275                            (gnus-summary-article-header parent))))
11276           (unless (and message-id (not (equal message-id "")))
11277             (error "No message-id in desired parent"))
11278           (gnus-with-article child
11279             (save-restriction
11280               (goto-char (point-min))
11281               (message-narrow-to-head)
11282               (if (re-search-forward "^References: " nil t)
11283                   (progn
11284                     (re-search-forward "^[^ \t]" nil t)
11285                     (forward-line -1)
11286                     (end-of-line)
11287                     (insert " " message-id))
11288                 (insert "References: " message-id "\n"))))
11289           (set-buffer gnus-summary-buffer)
11290           (gnus-summary-unmark-all-processable)
11291           (gnus-summary-update-article child)
11292           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11293             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11294           (gnus-summary-rethread-current)
11295           (gnus-message 3 "Article %d is now the child of article %d"
11296                         child parent))))))
11297
11298 (defun gnus-summary-toggle-threads (&optional arg)
11299   "Toggle showing conversation threads.
11300 If ARG is positive number, turn showing conversation threads on."
11301   (interactive "P")
11302   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11303     (setq gnus-show-threads
11304           (if (null arg) (not gnus-show-threads)
11305             (> (prefix-numeric-value arg) 0)))
11306     (gnus-summary-prepare)
11307     (gnus-summary-goto-subject current)
11308     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11309     (gnus-summary-position-point)))
11310
11311 (eval-and-compile
11312   (if (fboundp 'remove-overlays)
11313       (defalias 'gnus-remove-overlays 'remove-overlays)
11314     (defun gnus-remove-overlays (beg end name val)
11315       "Clear BEG and END of overlays whose property NAME has value VAL.
11316 For compatibility with XEmacs."
11317       (dolist (ov (gnus-overlays-in beg end))
11318         (when (eq (gnus-overlay-get ov name) val)
11319           (gnus-delete-overlay ov))))))
11320
11321 (defun gnus-summary-show-all-threads ()
11322   "Show all threads."
11323   (interactive)
11324   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11325   (gnus-summary-position-point))
11326
11327 (defun gnus-summary-show-thread ()
11328   "Show thread subtrees.
11329 Returns nil if no thread was there to be shown."
11330   (interactive)
11331   (let* ((orig (point))
11332          (end (point-at-eol))
11333          ;; Leave point at bol
11334          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11335          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11336                 (if (fboundp 'next-single-char-property-change)
11337                     (or (next-single-char-property-change end 'invisible)
11338                         (point-max))
11339                   (while (progn
11340                            (end-of-line 2)
11341                            (and (not (eobp))
11342                                 (eq (get-char-property (point) 'invisible)
11343                                     'gnus-sum))))
11344                   (point)))))
11345     (when eoi
11346       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11347       (goto-char orig)
11348       (gnus-summary-position-point)
11349       eoi)))
11350
11351 (defun gnus-summary-maybe-hide-threads ()
11352   "If requested, hide the threads that should be hidden."
11353   (when (and gnus-show-threads
11354              gnus-thread-hide-subtree)
11355     (gnus-summary-hide-all-threads
11356      (if (or (consp gnus-thread-hide-subtree)
11357              (functionp gnus-thread-hide-subtree))
11358          (gnus-make-predicate gnus-thread-hide-subtree)
11359        nil))))
11360
11361 ;;; Hiding predicates.
11362
11363 (defun gnus-article-unread-p (header)
11364   (memq (mail-header-number header) gnus-newsgroup-unreads))
11365
11366 (defun gnus-article-unseen-p (header)
11367   (memq (mail-header-number header) gnus-newsgroup-unseen))
11368
11369 (defun gnus-map-articles (predicate articles)
11370   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11371   (apply 'gnus-or (mapcar predicate
11372                           (mapcar (lambda (number)
11373                                     (gnus-summary-article-header number))
11374                                   articles))))
11375
11376 (defun gnus-summary-hide-all-threads (&optional predicate)
11377   "Hide all thread subtrees.
11378 If PREDICATE is supplied, threads that satisfy this predicate
11379 will not be hidden."
11380   (interactive)
11381   (save-excursion
11382     (goto-char (point-min))
11383     (let ((end nil))
11384       (while (not end)
11385         (when (or (not predicate)
11386                   (gnus-map-articles
11387                    predicate (gnus-summary-article-children)))
11388             (gnus-summary-hide-thread))
11389         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11390   (gnus-summary-position-point))
11391
11392 (defun gnus-summary-hide-thread ()
11393   "Hide thread subtrees.
11394 If PREDICATE is supplied, threads that satisfy this predicate
11395 will not be hidden.
11396 Returns nil if no threads were there to be hidden."
11397   (interactive)
11398   (let ((start (point))
11399         (starteol (line-end-position))
11400         (article (gnus-summary-article-number)))
11401     (goto-char start)
11402     ;; Go forward until either the buffer ends or the subthread ends.
11403     (when (and (not (eobp))
11404                (or (zerop (gnus-summary-next-thread 1 t))
11405                    (goto-char (point-max))))
11406       (if (and (> (point) start)
11407                ;; FIXME: this should actually search for a non-invisible \n.
11408                (search-backward "\n" start t))
11409           (progn
11410             (when (> (point) starteol)
11411               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11412               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11413                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11414                 (gnus-overlay-put ol 'evaporate t)))
11415             (gnus-summary-goto-subject article))
11416         (goto-char start)
11417         nil))))
11418
11419 (defun gnus-summary-go-to-next-thread (&optional previous)
11420   "Go to the same level (or less) next thread.
11421 If PREVIOUS is non-nil, go to previous thread instead.
11422 Return the article number moved to, or nil if moving was impossible."
11423   (let ((level (gnus-summary-thread-level))
11424         (way (if previous -1 1))
11425         (beg (point)))
11426     (forward-line way)
11427     (while (and (not (eobp))
11428                 (< level (gnus-summary-thread-level)))
11429       (forward-line way))
11430     (if (eobp)
11431         (progn
11432           (goto-char beg)
11433           nil)
11434       (setq beg (point))
11435       (prog1
11436           (gnus-summary-article-number)
11437         (goto-char beg)))))
11438
11439 (defun gnus-summary-next-thread (n &optional silent)
11440   "Go to the same level next N'th thread.
11441 If N is negative, search backward instead.
11442 Returns the difference between N and the number of skips actually
11443 done.
11444
11445 If SILENT, don't output messages."
11446   (interactive "p")
11447   (let ((backward (< n 0))
11448         (n (abs n)))
11449     (while (and (> n 0)
11450                 (gnus-summary-go-to-next-thread backward))
11451       (decf n))
11452     (unless silent
11453       (gnus-summary-position-point))
11454     (when (and (not silent) (/= 0 n))
11455       (gnus-message 7 "No more threads"))
11456     n))
11457
11458 (defun gnus-summary-prev-thread (n)
11459   "Go to the same level previous N'th thread.
11460 Returns the difference between N and the number of skips actually
11461 done."
11462   (interactive "p")
11463   (gnus-summary-next-thread (- n)))
11464
11465 (defun gnus-summary-go-down-thread ()
11466   "Go down one level in the current thread."
11467   (let ((children (gnus-summary-article-children)))
11468     (when children
11469       (gnus-summary-goto-subject (car children)))))
11470
11471 (defun gnus-summary-go-up-thread ()
11472   "Go up one level in the current thread."
11473   (let ((parent (gnus-summary-article-parent)))
11474     (when parent
11475       (gnus-summary-goto-subject parent))))
11476
11477 (defun gnus-summary-down-thread (n)
11478   "Go down thread N steps.
11479 If N is negative, go up instead.
11480 Returns the difference between N and how many steps down that were
11481 taken."
11482   (interactive "p")
11483   (let ((up (< n 0))
11484         (n (abs n)))
11485     (while (and (> n 0)
11486                 (if up (gnus-summary-go-up-thread)
11487                   (gnus-summary-go-down-thread)))
11488       (setq n (1- n)))
11489     (gnus-summary-position-point)
11490     (when (/= 0 n)
11491       (gnus-message 7 "Can't go further"))
11492     n))
11493
11494 (defun gnus-summary-up-thread (n)
11495   "Go up thread N steps.
11496 If N is negative, go down instead.
11497 Returns the difference between N and how many steps down that were
11498 taken."
11499   (interactive "p")
11500   (gnus-summary-down-thread (- n)))
11501
11502 (defun gnus-summary-top-thread ()
11503   "Go to the top of the thread."
11504   (interactive)
11505   (while (gnus-summary-go-up-thread))
11506   (gnus-summary-article-number))
11507
11508 (defun gnus-summary-expire-thread ()
11509   "Mark articles under current thread as expired."
11510   (interactive)
11511   (gnus-summary-kill-thread 0))
11512
11513 (defun gnus-summary-kill-thread (&optional unmark)
11514   "Mark articles under current thread as read.
11515 If the prefix argument is positive, remove any kinds of marks.
11516 If the prefix argument is zero, mark thread as expired.
11517 If the prefix argument is negative, tick articles instead."
11518   (interactive "P")
11519   (when unmark
11520     (setq unmark (prefix-numeric-value unmark)))
11521   (let ((articles (gnus-summary-articles-in-thread))
11522         (hide (or (null unmark) (= unmark 0))))
11523     (save-excursion
11524       ;; Expand the thread.
11525       (gnus-summary-show-thread)
11526       ;; Mark all the articles.
11527       (while articles
11528         (gnus-summary-goto-subject (car articles))
11529         (cond ((null unmark)
11530                (gnus-summary-mark-article-as-read gnus-killed-mark))
11531               ((> unmark 0)
11532                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11533               ((= unmark 0)
11534                (gnus-summary-mark-article nil gnus-expirable-mark))
11535               (t
11536                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11537         (setq articles (cdr articles))))
11538     ;; Hide killed subtrees when hide is true.
11539     (and hide
11540          gnus-thread-hide-killed
11541          (gnus-summary-hide-thread))
11542     ;; If hide is t, go to next unread subject.
11543     (when hide
11544       ;; Go to next unread subject.
11545       (gnus-summary-next-subject 1 t)))
11546   (gnus-set-mode-line 'summary))
11547
11548 ;; Summary sorting commands
11549
11550 (defun gnus-summary-sort-by-number (&optional reverse)
11551   "Sort the summary buffer by article number.
11552 Argument REVERSE means reverse order."
11553   (interactive "P")
11554   (gnus-summary-sort 'number reverse))
11555
11556 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11557   "Sort the summary buffer by most recent article number.
11558 Argument REVERSE means reverse order."
11559   (interactive "P")
11560   (gnus-summary-sort 'most-recent-number reverse))
11561
11562 (defun gnus-summary-sort-by-random (&optional reverse)
11563   "Randomize the order in the summary buffer.
11564 Argument REVERSE means to randomize in reverse order."
11565   (interactive "P")
11566   (gnus-summary-sort 'random reverse))
11567
11568 (defun gnus-summary-sort-by-author (&optional reverse)
11569   "Sort the summary buffer by author name alphabetically.
11570 If `case-fold-search' is non-nil, case of letters is ignored.
11571 Argument REVERSE means reverse order."
11572   (interactive "P")
11573   (gnus-summary-sort 'author reverse))
11574
11575 (defun gnus-summary-sort-by-recipient (&optional reverse)
11576   "Sort the summary buffer by recipient name alphabetically.
11577 If `case-fold-search' is non-nil, case of letters is ignored.
11578 Argument REVERSE means reverse order."
11579   (interactive "P")
11580   (gnus-summary-sort 'recipient reverse))
11581
11582 (defun gnus-summary-sort-by-subject (&optional reverse)
11583   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11584 If `case-fold-search' is non-nil, case of letters is ignored.
11585 Argument REVERSE means reverse order."
11586   (interactive "P")
11587   (gnus-summary-sort 'subject reverse))
11588
11589 (defun gnus-summary-sort-by-date (&optional reverse)
11590   "Sort the summary buffer by date.
11591 Argument REVERSE means reverse order."
11592   (interactive "P")
11593   (gnus-summary-sort 'date reverse))
11594
11595 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11596   "Sort the summary buffer by most recent date.
11597 Argument REVERSE means reverse order."
11598   (interactive "P")
11599   (gnus-summary-sort 'most-recent-date reverse))
11600
11601 (defun gnus-summary-sort-by-score (&optional reverse)
11602   "Sort the summary buffer by score.
11603 Argument REVERSE means reverse order."
11604   (interactive "P")
11605   (gnus-summary-sort 'score reverse))
11606
11607 (defun gnus-summary-sort-by-lines (&optional reverse)
11608   "Sort the summary buffer by the number of lines.
11609 Argument REVERSE means reverse order."
11610   (interactive "P")
11611   (gnus-summary-sort 'lines reverse))
11612
11613 (defun gnus-summary-sort-by-chars (&optional reverse)
11614   "Sort the summary buffer by article length.
11615 Argument REVERSE means reverse order."
11616   (interactive "P")
11617   (gnus-summary-sort 'chars reverse))
11618
11619 (defun gnus-summary-sort-by-original (&optional reverse)
11620   "Sort the summary buffer using the default sorting method.
11621 Argument REVERSE means reverse order."
11622   (interactive "P")
11623   (let* ((inhibit-read-only t)
11624          (gnus-summary-prepare-hook nil))
11625     ;; We do the sorting by regenerating the threads.
11626     (gnus-summary-prepare)
11627     ;; Hide subthreads if needed.
11628     (gnus-summary-maybe-hide-threads)))
11629
11630 (defun gnus-summary-sort (predicate reverse)
11631   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11632   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11633          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11634          (gnus-thread-sort-functions
11635           (if (not reverse)
11636               thread
11637             `(lambda (t1 t2)
11638                (,thread t2 t1))))
11639          (gnus-sort-gathered-threads-function
11640           gnus-thread-sort-functions)
11641          (gnus-article-sort-functions
11642           (if (not reverse)
11643               article
11644             `(lambda (t1 t2)
11645                (,article t2 t1))))
11646          (inhibit-read-only t)
11647          (gnus-summary-prepare-hook nil))
11648     ;; We do the sorting by regenerating the threads.
11649     (gnus-summary-prepare)
11650     ;; Hide subthreads if needed.
11651     (gnus-summary-maybe-hide-threads)))
11652
11653 ;; Summary saving commands.
11654
11655 (defun gnus-summary-save-article (&optional n not-saved)
11656   "Save the current article using the default saver function.
11657 If N is a positive number, save the N next articles.
11658 If N is a negative number, save the N previous articles.
11659 If N is nil and any articles have been marked with the process mark,
11660 save those articles instead.
11661 The variable `gnus-default-article-saver' specifies the saver function.
11662
11663 If the optional second argument NOT-SAVED is non-nil, articles saved
11664 will not be marked as saved."
11665   (interactive "P")
11666   (require 'gnus-art)
11667   (let* ((articles (gnus-summary-work-articles n))
11668          (save-buffer (save-excursion
11669                         (nnheader-set-temp-buffer " *Gnus Save*")))
11670          (num (length articles))
11671          ;; Whether to save decoded articles or raw articles.
11672          (decode (when gnus-article-save-coding-system
11673                    (get gnus-default-article-saver :decode)))
11674          ;; When saving many articles in a single file, use the other
11675          ;; function to save articles other than the first one.
11676          (saver2 (get gnus-default-article-saver :function))
11677          (gnus-prompt-before-saving (if saver2
11678                                         t
11679                                       gnus-prompt-before-saving))
11680          (gnus-default-article-saver gnus-default-article-saver)
11681          header file)
11682     (dolist (article articles)
11683       (setq header (gnus-summary-article-header article))
11684       (if (not (vectorp header))
11685           ;; This is a pseudo-article.
11686           (if (assq 'name header)
11687               (gnus-copy-file (cdr (assq 'name header)))
11688             (gnus-message 1 "Article %d is unsaveable" article))
11689         ;; This is a real article.
11690         (save-window-excursion
11691           (gnus-summary-select-article decode decode nil article)
11692           (gnus-summary-goto-subject article))
11693         (with-current-buffer save-buffer
11694           (erase-buffer)
11695           (insert-buffer-substring (if decode
11696                                        gnus-article-buffer
11697                                      gnus-original-article-buffer)))
11698         (setq file (gnus-article-save save-buffer file num))
11699         (gnus-summary-remove-process-mark article)
11700         (unless not-saved
11701           (gnus-summary-set-saved-mark article)))
11702       (when saver2
11703         (setq gnus-default-article-saver saver2
11704               saver2 nil)))
11705     (gnus-kill-buffer save-buffer)
11706     (gnus-summary-position-point)
11707     (gnus-set-mode-line 'summary)
11708     n))
11709
11710 (defun gnus-summary-pipe-output (&optional n sym)
11711   "Pipe the current article to a subprocess.
11712 If N is a positive number, pipe the N next articles.
11713 If N is a negative number, pipe the N previous articles.
11714 If N is nil and any articles have been marked with the process mark,
11715 pipe those articles instead.
11716 The default command to which articles are piped is specified by the
11717 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11718 will be prompted for the command.
11719
11720 The properties `:decode' and `:headers' that are put to the function
11721 symbol `gnus-summary-save-in-pipe' control whether this function
11722 decodes articles and what headers to keep (see the doc string for the
11723 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11724 is neither omitted nor the symbol `r', force including all headers
11725 regardless of the `:headers' property.  If it is the symbol `r',
11726 articles that are not decoded and include all headers will be piped
11727 no matter what the properties `:decode' and `:headers' are."
11728   (interactive (gnus-interactive "P\ny"))
11729   (require 'gnus-art)
11730   (let* ((articles (gnus-summary-work-articles n))
11731          (result-buffer "*Shell Command Output*")
11732          (all-headers (not (memq sym '(nil r))))
11733          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11734          (raw (eq sym 'r))
11735          (headers (get 'gnus-summary-save-in-pipe :headers))
11736          command result)
11737     (unless (numberp (car articles))
11738       (error "No article to pipe"))
11739     (setq command (gnus-read-shell-command
11740                    (concat "Shell command on "
11741                            (if (cdr articles)
11742                                (format "these %d articles" (length articles))
11743                              "this article")
11744                            ": ")
11745                    gnus-summary-pipe-output-default-command))
11746     (when (string-equal command "")
11747       (error "A command is required"))
11748     (when all-headers
11749       (put 'gnus-summary-save-in-pipe :headers nil))
11750     (unwind-protect
11751         (while articles
11752           (gnus-summary-goto-subject (pop articles))
11753           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11754           (when (and (get-buffer result-buffer)
11755                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11756             (setq result (concat result (with-current-buffer result-buffer
11757                                           (buffer-string))))))
11758       (put 'gnus-summary-save-in-pipe :headers headers))
11759     (unless (zerop (length result))
11760       (if (with-current-buffer (get-buffer-create result-buffer)
11761             (erase-buffer)
11762             (insert result)
11763             (prog1
11764                 (and (= (count-lines (point-min) (point)) 1)
11765                      (progn
11766                        (end-of-line 0)
11767                        (<= (current-column)
11768                            (window-width (minibuffer-window)))))
11769               (goto-char (point-min))))
11770           (message "%s" (substring result 0 -1))
11771         (message nil)
11772         (gnus-configure-windows 'pipe)))))
11773
11774 (defun gnus-summary-save-article-mail (&optional arg)
11775   "Append the current article to a Unix mail box file.
11776 If N is a positive number, save the N next articles.
11777 If N is a negative number, save the N previous articles.
11778 If N is nil and any articles have been marked with the process mark,
11779 save those articles instead."
11780   (interactive "P")
11781   (require 'gnus-art)
11782   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11783     (gnus-summary-save-article arg)))
11784
11785 (defun gnus-summary-save-article-rmail (&optional arg)
11786   "Append the current article to an rmail file.
11787 If N is a positive number, save the N next articles.
11788 If N is a negative number, save the N previous articles.
11789 If N is nil and any articles have been marked with the process mark,
11790 save those articles instead."
11791   (interactive "P")
11792   (require 'gnus-art)
11793   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11794     (gnus-summary-save-article arg)))
11795
11796 (defun gnus-summary-save-article-file (&optional arg)
11797   "Append the current article to a file.
11798 If N is a positive number, save the N next articles.
11799 If N is a negative number, save the N previous articles.
11800 If N is nil and any articles have been marked with the process mark,
11801 save those articles instead."
11802   (interactive "P")
11803   (require 'gnus-art)
11804   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11805     (gnus-summary-save-article arg)))
11806
11807 (defun gnus-summary-write-article-file (&optional arg)
11808   "Write the current article to a file, deleting the previous file.
11809 If N is a positive number, save the N next articles.
11810 If N is a negative number, save the N previous articles.
11811 If N is nil and any articles have been marked with the process mark,
11812 save those articles instead."
11813   (interactive "P")
11814   (require 'gnus-art)
11815   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11816     (gnus-summary-save-article arg)))
11817
11818 (defun gnus-summary-save-article-body-file (&optional arg)
11819   "Append the current article body to a file.
11820 If N is a positive number, save the N next articles.
11821 If N is a negative number, save the N previous articles.
11822 If N is nil and any articles have been marked with the process mark,
11823 save those articles instead."
11824   (interactive "P")
11825   (require 'gnus-art)
11826   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11827     (gnus-summary-save-article arg)))
11828
11829 (defun gnus-summary-write-article-body-file (&optional arg)
11830   "Write the current article body to a file, deleting the previous file.
11831 If N is a positive number, save the N next articles.
11832 If N is a negative number, save the N previous articles.
11833 If N is nil and any articles have been marked with the process mark,
11834 save those articles instead."
11835   (interactive "P")
11836   (require 'gnus-art)
11837   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11838     (gnus-summary-save-article arg)))
11839
11840 (defun gnus-summary-muttprint (&optional arg)
11841   "Print the current article using Muttprint.
11842 If N is a positive number, save the N next articles.
11843 If N is a negative number, save the N previous articles.
11844 If N is nil and any articles have been marked with the process mark,
11845 save those articles instead."
11846   (interactive "P")
11847   (require 'gnus-art)
11848   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11849     (gnus-summary-save-article arg t)))
11850
11851 (defun gnus-summary-pipe-message (program)
11852   "Pipe the current article through PROGRAM."
11853   (interactive "sProgram: ")
11854   (gnus-summary-select-article)
11855   (let ((mail-header-separator ""))
11856     (gnus-eval-in-buffer-window gnus-article-buffer
11857       (save-restriction
11858         (widen)
11859         (let ((start (window-start))
11860               (inhibit-read-only t))
11861           (message-pipe-buffer-body program)
11862           (set-window-start (get-buffer-window (current-buffer)) start))))))
11863
11864 (defun gnus-get-split-value (methods)
11865   "Return a value based on the split METHODS."
11866   (let (split-name method result match)
11867     (when methods
11868       (with-current-buffer gnus-original-article-buffer
11869         (save-restriction
11870           (nnheader-narrow-to-headers)
11871           (while (and methods (not split-name))
11872             (goto-char (point-min))
11873             (setq method (pop methods))
11874             (setq match (car method))
11875             (when (cond
11876                    ((stringp match)
11877                     ;; Regular expression.
11878                     (ignore-errors
11879                       (re-search-forward match nil t)))
11880                    ((functionp match)
11881                     ;; Function.
11882                     (save-restriction
11883                       (widen)
11884                       (setq result (funcall match gnus-newsgroup-name))))
11885                    ((consp match)
11886                     ;; Form.
11887                     (save-restriction
11888                       (widen)
11889                       (setq result (eval match)))))
11890               (setq split-name (cdr method))
11891               (cond ((stringp result)
11892                      (push (expand-file-name
11893                             result gnus-article-save-directory)
11894                            split-name))
11895                     ((consp result)
11896                      (setq split-name (append result split-name)))))))))
11897     (nreverse split-name)))
11898
11899 (defun gnus-valid-move-group-p (group)
11900   (and (boundp group)
11901        (symbol-name group)
11902        (symbol-value group)
11903        (gnus-get-function (gnus-find-method-for-group
11904                            (symbol-name group)) 'request-accept-article t)))
11905
11906 (defun gnus-read-move-group-name (prompt default articles prefix)
11907   "Read a group name."
11908   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11909          (minibuffer-confirm-incomplete nil) ; XEmacs
11910          (prom
11911           (format "%s %s to"
11912                   prompt
11913                   (if (> (length articles) 1)
11914                       (format "these %d articles" (length articles))
11915                     "this article")))
11916          (to-newsgroup
11917           (let (active group)
11918             (when (or (null split-name) (= 1 (length split-name)))
11919               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11920               (mapatoms (lambda (symbol)
11921                           (setq group (symbol-name symbol))
11922                           (when (string-match "[^\000-\177]" group)
11923                             (setq group (gnus-group-decoded-name group)))
11924                           (set (intern group active) group))
11925                         gnus-active-hashtb))
11926             (cond
11927              ((null split-name)
11928               (gnus-completing-read-with-default
11929                default prom active 'gnus-valid-move-group-p nil prefix
11930                'gnus-group-history))
11931              ((= 1 (length split-name))
11932               (gnus-completing-read-with-default
11933                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11934                'gnus-group-history))
11935              (t
11936               (gnus-completing-read-with-default
11937                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11938                'gnus-group-history)))))
11939          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11940          encoded)
11941     (when to-newsgroup
11942       (if (or (string= to-newsgroup "")
11943               (string= to-newsgroup prefix))
11944           (setq to-newsgroup default))
11945       (unless to-newsgroup
11946         (error "No group name entered"))
11947       (setq encoded (mm-encode-coding-string
11948                      to-newsgroup
11949                      (gnus-group-name-charset to-method to-newsgroup)))
11950       (or (gnus-active encoded)
11951           (gnus-activate-group encoded nil nil to-method)
11952           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11953                                      to-newsgroup))
11954               (or (and (gnus-request-create-group encoded to-method)
11955                        (gnus-activate-group encoded nil nil to-method)
11956                        (gnus-subscribe-group encoded))
11957                   (error "Couldn't create group %s" to-newsgroup)))
11958           (error "No such group: %s" to-newsgroup))
11959       encoded)))
11960
11961 (defvar gnus-summary-save-parts-counter)
11962 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11963
11964 (defun gnus-summary-save-parts (type dir n &optional reverse)
11965   "Save parts matching TYPE to DIR.
11966 If REVERSE, save parts that do not match TYPE."
11967   (interactive
11968    (list (read-string "Save parts of type: "
11969                       (or (car gnus-summary-save-parts-type-history)
11970                           gnus-summary-save-parts-default-mime)
11971                       'gnus-summary-save-parts-type-history)
11972          (setq gnus-summary-save-parts-last-directory
11973                (read-file-name "Save to directory: "
11974                                gnus-summary-save-parts-last-directory
11975                                nil t))
11976          current-prefix-arg))
11977   (gnus-summary-iterate n
11978     (let ((gnus-display-mime-function nil)
11979           gnus-article-prepare-hook
11980           gnus-article-decode-hook
11981           gnus-display-mime-function
11982           gnus-break-pages
11983           (gnus-inhibit-treatment t))
11984       (gnus-summary-select-article))
11985     (with-current-buffer gnus-article-buffer
11986       (let ((handles (or gnus-article-mime-handles
11987                          (mm-dissect-buffer nil gnus-article-loose-mime)
11988                          (and gnus-article-emulate-mime
11989                               (mm-uu-dissect))))
11990             (gnus-summary-save-parts-counter 1))
11991         (when handles
11992           (gnus-summary-save-parts-1 type dir handles reverse)
11993           (unless gnus-article-mime-handles ;; Don't destroy this case.
11994             (mm-destroy-parts handles)))))))
11995
11996 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11997   (if (stringp (car handle))
11998       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11999               (cdr handle))
12000     (when (if reverse
12001               (not (string-match type (mm-handle-media-type handle)))
12002             (string-match type (mm-handle-media-type handle)))
12003       (let ((file (expand-file-name
12004                    (gnus-map-function
12005                     mm-file-name-rewrite-functions
12006                     (file-name-nondirectory
12007                      (or
12008                       (mail-content-type-get
12009                        (mm-handle-disposition handle) 'filename)
12010                       (mail-content-type-get
12011                        (mm-handle-type handle) 'name)
12012                       (format "%s.%d.%d" gnus-newsgroup-name
12013                               (cdr gnus-article-current)
12014                               gnus-summary-save-parts-counter))))
12015                    dir)))
12016         (incf gnus-summary-save-parts-counter)
12017         (unless (file-exists-p file)
12018           (mm-save-part-to-file handle file))))))
12019
12020 ;; Summary extract commands
12021
12022 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12023   (let ((inhibit-read-only t)
12024         (article (gnus-summary-article-number))
12025         after-article b e)
12026     (unless (gnus-summary-goto-subject article)
12027       (error "No such article: %d" article))
12028     (gnus-summary-position-point)
12029     ;; If all commands are to be bunched up on one line, we collect
12030     ;; them here.
12031     (unless gnus-view-pseudos-separately
12032       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12033             files action)
12034         (while ps
12035           (setq action (cdr (assq 'action (car ps))))
12036           (setq files (list (cdr (assq 'name (car ps)))))
12037           (while (and ps (cdr ps)
12038                       (string= (or action "1")
12039                                (or (cdr (assq 'action (cadr ps))) "2")))
12040             (push (cdr (assq 'name (cadr ps))) files)
12041             (setcdr ps (cddr ps)))
12042           (when files
12043             (when (not (string-match "%s" action))
12044               (push " " files))
12045             (push " " files)
12046             (when (assq 'execute (car ps))
12047               (setcdr (assq 'execute (car ps))
12048                       (funcall (if (string-match "%s" action)
12049                                    'format 'concat)
12050                                action
12051                                (mapconcat
12052                                 (lambda (f)
12053                                   (if (equal f " ")
12054                                       f
12055                                     (shell-quote-argument f)))
12056                                 files " ")))))
12057           (setq ps (cdr ps)))))
12058     (if (and gnus-view-pseudos (not not-view))
12059         (while pslist
12060           (when (assq 'execute (car pslist))
12061             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12062                                   (eq gnus-view-pseudos 'not-confirm)))
12063           (setq pslist (cdr pslist)))
12064       (save-excursion
12065         (while pslist
12066           (setq after-article (or (cdr (assq 'article (car pslist)))
12067                                   (gnus-summary-article-number)))
12068           (gnus-summary-goto-subject after-article)
12069           (forward-line 1)
12070           (setq b (point))
12071           (insert "    " (file-name-nondirectory
12072                           (cdr (assq 'name (car pslist))))
12073                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12074           (setq e (point))
12075           (forward-line -1)             ; back to `b'
12076           (gnus-add-text-properties
12077            b (1- e) (list 'gnus-number gnus-reffed-article-number
12078                           gnus-mouse-face-prop gnus-mouse-face))
12079           (gnus-data-enter
12080            after-article gnus-reffed-article-number
12081            gnus-unread-mark b (car pslist) 0 (- e b))
12082           (setq gnus-newsgroup-unreads
12083                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12084                                          gnus-reffed-article-number))
12085           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12086           (setq pslist (cdr pslist)))))))
12087
12088 (defun gnus-pseudos< (p1 p2)
12089   (let ((c1 (cdr (assq 'action p1)))
12090         (c2 (cdr (assq 'action p2))))
12091     (and c1 c2 (string< c1 c2))))
12092
12093 (defun gnus-request-pseudo-article (props)
12094   (cond ((assq 'execute props)
12095          (gnus-execute-command (cdr (assq 'execute props)))))
12096   (let ((gnus-current-article (gnus-summary-article-number)))
12097     (gnus-run-hooks 'gnus-mark-article-hook)))
12098
12099 (defun gnus-execute-command (command &optional automatic)
12100   (save-excursion
12101     (gnus-article-setup-buffer)
12102     (set-buffer gnus-article-buffer)
12103     (setq buffer-read-only nil)
12104     (let ((command (if automatic command
12105                      (read-string "Command: " (cons command 0)))))
12106       (erase-buffer)
12107       (insert "$ " command "\n\n")
12108       (if gnus-view-pseudo-asynchronously
12109           (start-process "gnus-execute" (current-buffer) shell-file-name
12110                          shell-command-switch command)
12111         (call-process shell-file-name nil t nil
12112                       shell-command-switch command)))))
12113
12114 ;; Summary kill commands.
12115
12116 (defun gnus-summary-edit-global-kill (article)
12117   "Edit the \"global\" kill file."
12118   (interactive (list (gnus-summary-article-number)))
12119   (gnus-group-edit-global-kill article))
12120
12121 (defun gnus-summary-edit-local-kill ()
12122   "Edit a local kill file applied to the current newsgroup."
12123   (interactive)
12124   (setq gnus-current-headers (gnus-summary-article-header))
12125   (gnus-group-edit-local-kill
12126    (gnus-summary-article-number) gnus-newsgroup-name))
12127
12128 ;;; Header reading.
12129
12130 (defun gnus-read-header (id &optional header)
12131   "Read the headers of article ID and enter them into the Gnus system."
12132   (let ((group gnus-newsgroup-name)
12133         (gnus-override-method
12134          (or
12135           gnus-override-method
12136           (and (gnus-news-group-p gnus-newsgroup-name)
12137                (car (gnus-refer-article-methods)))))
12138         where)
12139     ;; First we check to see whether the header in question is already
12140     ;; fetched.
12141     (if (stringp id)
12142         ;; This is a Message-ID.
12143         (setq header (or header (gnus-id-to-header id)))
12144       ;; This is an article number.
12145       (setq header (or header (gnus-summary-article-header id))))
12146     (if (and header
12147              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12148         ;; We have found the header.
12149         header
12150       ;; We have to really fetch the header to this article.
12151       (with-current-buffer nntp-server-buffer
12152         (when (setq where (gnus-request-head id group))
12153           (nnheader-fold-continuation-lines)
12154           (goto-char (point-max))
12155           (insert ".\n")
12156           (goto-char (point-min))
12157           (insert "211 ")
12158           (princ (cond
12159                   ((numberp id) id)
12160                   ((cdr where) (cdr where))
12161                   (header (mail-header-number header))
12162                   (t gnus-reffed-article-number))
12163                  (current-buffer))
12164           (insert " Article retrieved.\n"))
12165         (if (or (not where)
12166                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12167             ()                          ; Malformed head.
12168           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12169             (when (and (stringp id)
12170                        (or
12171                         (not (string= (gnus-group-real-name group)
12172                                       (car where)))
12173                         (not (gnus-server-equal gnus-override-method
12174                                                 (gnus-group-method group)))))
12175               ;; If we fetched by Message-ID and the article came from
12176               ;; a different group (or server), we fudge some bogus
12177               ;; article numbers for this article.
12178               (mail-header-set-number header gnus-reffed-article-number))
12179             (with-current-buffer gnus-summary-buffer
12180               (decf gnus-reffed-article-number)
12181               (gnus-remove-header (mail-header-number header))
12182               (push header gnus-newsgroup-headers)
12183               (setq gnus-current-headers header)
12184               (push (mail-header-number header) gnus-newsgroup-limit)))
12185           header)))))
12186
12187 (defun gnus-remove-header (number)
12188   "Remove header NUMBER from `gnus-newsgroup-headers'."
12189   (if (and gnus-newsgroup-headers
12190            (= number (mail-header-number (car gnus-newsgroup-headers))))
12191       (pop gnus-newsgroup-headers)
12192     (let ((headers gnus-newsgroup-headers))
12193       (while (and (cdr headers)
12194                   (not (= number (mail-header-number (cadr headers)))))
12195         (pop headers))
12196       (when (cdr headers)
12197         (setcdr headers (cddr headers))))))
12198
12199 ;;;
12200 ;;; summary highlights
12201 ;;;
12202
12203 (defun gnus-highlight-selected-summary ()
12204   "Highlight selected article in summary buffer."
12205   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12206   (when gnus-summary-selected-face
12207     (save-excursion
12208       (let* ((beg (point-at-bol))
12209              (end (point-at-eol))
12210              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12211              (from (if (get-text-property beg gnus-mouse-face-prop)
12212                        beg
12213                      (or (next-single-property-change
12214                           beg gnus-mouse-face-prop nil end)
12215                          beg)))
12216              (to
12217               (if (= from end)
12218                   (- from 2)
12219                 (or (next-single-property-change
12220                      from gnus-mouse-face-prop nil end)
12221                     end))))
12222         ;; If no mouse-face prop on line we will have to = from = end,
12223         ;; so we highlight the entire line instead.
12224         (when (= (+ to 2) from)
12225           (setq from beg)
12226           (setq to end))
12227         (if gnus-newsgroup-selected-overlay
12228             ;; Move old overlay.
12229             (gnus-move-overlay
12230              gnus-newsgroup-selected-overlay from to (current-buffer))
12231           ;; Create new overlay.
12232           (gnus-overlay-put
12233            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12234            'face gnus-summary-selected-face))))))
12235
12236 (defvar gnus-summary-highlight-line-cached nil)
12237 (defvar gnus-summary-highlight-line-trigger nil)
12238
12239 (defun gnus-summary-highlight-line-0 ()
12240   (if (and (eq gnus-summary-highlight-line-trigger
12241                gnus-summary-highlight)
12242            gnus-summary-highlight-line-cached)
12243       gnus-summary-highlight-line-cached
12244     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12245           gnus-summary-highlight-line-cached
12246           (let* ((cond (list 'cond))
12247                  (c cond)
12248                  (list gnus-summary-highlight))
12249             (while list
12250               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12251                               nil))
12252               (setq c (cdr c)
12253                     list (cdr list)))
12254             (gnus-byte-compile (list 'lambda nil cond))))))
12255
12256 (defun gnus-summary-highlight-line ()
12257   "Highlight current line according to `gnus-summary-highlight'."
12258   (let* ((beg (point-at-bol))
12259          (article (or (gnus-summary-article-number) gnus-current-article))
12260          (score (or (cdr (assq article
12261                                gnus-newsgroup-scored))
12262                     gnus-summary-default-score 0))
12263          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12264          (inhibit-read-only t)
12265          (default gnus-summary-default-score)
12266          (default-high gnus-summary-default-high-score)
12267          (default-low gnus-summary-default-low-score)
12268          (uncached (and gnus-summary-use-undownloaded-faces
12269                         (memq article gnus-newsgroup-undownloaded)
12270                         (not (memq article gnus-newsgroup-cached)))))
12271     (let ((face (funcall (gnus-summary-highlight-line-0))))
12272       (unless (eq face (get-text-property beg 'face))
12273         (gnus-put-text-property-excluding-characters-with-faces
12274          beg (point-at-eol) 'face
12275          (setq face (if (boundp face) (symbol-value face) face)))
12276         (when gnus-summary-highlight-line-function
12277           (funcall gnus-summary-highlight-line-function article face))))))
12278
12279 (defun gnus-update-read-articles (group unread &optional compute)
12280   "Update the list of read articles in GROUP.
12281 UNREAD is a sorted list."
12282   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12283         (info (gnus-get-info group))
12284         (prev 1)
12285         read)
12286     (if (or (not info) (not active))
12287         ;; There is no info on this group if it was, in fact,
12288         ;; killed.  Gnus stores no information on killed groups, so
12289         ;; there's nothing to be done.
12290         ;; One could store the information somewhere temporarily,
12291         ;; perhaps...  Hmmm...
12292         ()
12293       ;; Remove any negative articles numbers.
12294       (while (and unread (< (car unread) 0))
12295         (setq unread (cdr unread)))
12296       ;; Remove any expired article numbers
12297       (while (and unread (< (car unread) (car active)))
12298         (setq unread (cdr unread)))
12299       ;; Compute the ranges of read articles by looking at the list of
12300       ;; unread articles.
12301       (while unread
12302         (when (/= (car unread) prev)
12303           (push (if (= prev (1- (car unread))) prev
12304                   (cons prev (1- (car unread))))
12305                 read))
12306         (setq prev (1+ (car unread)))
12307         (setq unread (cdr unread)))
12308       (when (<= prev (cdr active))
12309         (push (cons prev (cdr active)) read))
12310       (setq read (if (> (length read) 1) (nreverse read) read))
12311       (if compute
12312           read
12313         (save-excursion
12314           (let (setmarkundo)
12315             ;; Propagate the read marks to the backend.
12316             (when (and gnus-propagate-marks
12317                        (gnus-check-backend-function 'request-set-mark group))
12318               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12319                     (add (gnus-remove-from-range read (gnus-info-read info))))
12320                 (when (or add del)
12321                   (unless (gnus-check-group group)
12322                     (error "Can't open server for %s" group))
12323                   (gnus-request-set-mark
12324                    group (delq nil (list (if add (list add 'add '(read)))
12325                                          (if del (list del 'del '(read))))))
12326                   (setq setmarkundo
12327                         `(gnus-request-set-mark
12328                           ,group
12329                           ',(delq nil (list
12330                                        (if del (list del 'add '(read)))
12331                                        (if add (list add 'del '(read))))))))))
12332             (set-buffer gnus-group-buffer)
12333             (gnus-undo-register
12334               `(progn
12335                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12336                  (gnus-info-set-read ',info ',(gnus-info-read info))
12337                  (gnus-get-unread-articles-in-group ',info
12338                                                     (gnus-active ,group))
12339                  (gnus-group-update-group ,group t)
12340                  ,setmarkundo))))
12341         ;; Enter this list into the group info.
12342         (gnus-info-set-read info read)
12343         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12344         (gnus-get-unread-articles-in-group info (gnus-active group))
12345         t))))
12346
12347 (defun gnus-offer-save-summaries ()
12348   "Offer to save all active summary buffers."
12349   (let (buffers)
12350     ;; Go through all buffers and find all summaries.
12351     (dolist (buffer (buffer-list))
12352       (when (and (setq buffer (buffer-name buffer))
12353                  (string-match "Summary" buffer)
12354                  (with-current-buffer buffer
12355                    ;; We check that this is, indeed, a summary buffer.
12356                    (and (eq major-mode 'gnus-summary-mode)
12357                         ;; Also make sure this isn't bogus.
12358                         gnus-newsgroup-prepared
12359                         ;; Also make sure that this isn't a
12360                         ;; dead summary buffer.
12361                         (not gnus-dead-summary-mode))))
12362         (push buffer buffers)))
12363     ;; Go through all these summary buffers and offer to save them.
12364     (when buffers
12365       (save-excursion
12366         (map-y-or-n-p
12367          "Update summary buffer %s? "
12368          (lambda (buf)
12369            (switch-to-buffer buf)
12370            (gnus-summary-exit))
12371          buffers)))))
12372
12373 (defun gnus-summary-setup-default-charset ()
12374   "Setup newsgroup default charset."
12375   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12376       (setq gnus-newsgroup-charset nil)
12377     (let* ((ignored-charsets
12378             (or gnus-newsgroup-ephemeral-ignored-charsets
12379                 (append
12380                  (and gnus-newsgroup-name
12381                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12382                  gnus-newsgroup-ignored-charsets))))
12383       (setq gnus-newsgroup-charset
12384             (or gnus-newsgroup-ephemeral-charset
12385                 (and gnus-newsgroup-name
12386                      (gnus-parameter-charset gnus-newsgroup-name))
12387                 gnus-default-charset))
12388       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12389            ignored-charsets))))
12390
12391 ;;;
12392 ;;; Mime Commands
12393 ;;;
12394
12395 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12396   "Display the current article buffer fully MIME-buttonized.
12397 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12398 treated as multipart/mixed."
12399   (interactive "P")
12400   (require 'gnus-art)
12401   (let ((gnus-unbuttonized-mime-types nil)
12402         (gnus-mime-display-multipart-as-mixed show-all-parts))
12403     (gnus-summary-show-article)))
12404
12405 (defun gnus-summary-repair-multipart (article)
12406   "Add a Content-Type header to a multipart article without one."
12407   (interactive (list (gnus-summary-article-number)))
12408   (gnus-with-article article
12409     (message-narrow-to-head)
12410     (message-remove-header "Mime-Version")
12411     (goto-char (point-max))
12412     (insert "Mime-Version: 1.0\n")
12413     (widen)
12414     (when (search-forward "\n--" nil t)
12415       (let ((separator (buffer-substring (point) (point-at-eol))))
12416         (message-narrow-to-head)
12417         (message-remove-header "Content-Type")
12418         (goto-char (point-max))
12419         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12420                         separator))
12421         (widen))))
12422   (let (gnus-mark-article-hook)
12423     (gnus-summary-select-article t t nil article)))
12424
12425 (defun gnus-summary-toggle-display-buttonized ()
12426   "Toggle the buttonizing of the article buffer."
12427   (interactive)
12428   (require 'gnus-art)
12429   (if (setq gnus-inhibit-mime-unbuttonizing
12430             (not gnus-inhibit-mime-unbuttonizing))
12431       (let ((gnus-unbuttonized-mime-types nil))
12432         (gnus-summary-show-article))
12433     (gnus-summary-show-article)))
12434
12435 ;;;
12436 ;;; Generic summary marking commands
12437 ;;;
12438
12439 (defvar gnus-summary-marking-alist
12440   '((read gnus-del-mark "d")
12441     (unread gnus-unread-mark "u")
12442     (ticked gnus-ticked-mark "!")
12443     (dormant gnus-dormant-mark "?")
12444     (expirable gnus-expirable-mark "e"))
12445   "An alist of names/marks/keystrokes.")
12446
12447 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12448 (defvar gnus-summary-mark-map)
12449
12450 (defun gnus-summary-make-all-marking-commands ()
12451   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12452   (dolist (elem gnus-summary-marking-alist)
12453     (apply 'gnus-summary-make-marking-command elem)))
12454
12455 (defun gnus-summary-make-marking-command (name mark keystroke)
12456   (let ((map (make-sparse-keymap)))
12457     (define-key gnus-summary-generic-mark-map keystroke map)
12458     (dolist (lway `((next "next" next nil "n")
12459                     (next-unread "next unread" next t "N")
12460                     (prev "previous" prev nil "p")
12461                     (prev-unread "previous unread" prev t "P")
12462                     (nomove "" nil nil ,keystroke)))
12463       (let ((func (gnus-summary-make-marking-command-1
12464                    mark (car lway) lway name)))
12465         (setq func (eval func))
12466         (define-key map (nth 4 lway) func)))))
12467
12468 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12469   `(defun ,(intern
12470             (format "gnus-summary-put-mark-as-%s%s"
12471                     name (if (eq way 'nomove)
12472                              ""
12473                            (concat "-" (symbol-name way)))))
12474      (n)
12475      ,(format
12476        "Mark the current article as %s%s.
12477 If N, the prefix, then repeat N times.
12478 If N is negative, move in reverse order.
12479 The difference between N and the actual number of articles marked is
12480 returned."
12481        name (cadr lway))
12482      (interactive "p")
12483      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12484
12485 (defun gnus-summary-generic-mark (n mark move unread)
12486   "Mark N articles with MARK."
12487   (unless (eq major-mode 'gnus-summary-mode)
12488     (error "This command can only be used in the summary buffer"))
12489   (gnus-summary-show-thread)
12490   (let ((nummove
12491          (cond
12492           ((eq move 'next) 1)
12493           ((eq move 'prev) -1)
12494           (t 0))))
12495     (if (zerop nummove)
12496         (setq n 1)
12497       (when (< n 0)
12498         (setq n (abs n)
12499               nummove (* -1 nummove))))
12500     (while (and (> n 0)
12501                 (gnus-summary-mark-article nil mark)
12502                 (zerop (gnus-summary-next-subject nummove unread t)))
12503       (setq n (1- n)))
12504     (when (/= 0 n)
12505       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12506     (gnus-summary-recenter)
12507     (gnus-summary-position-point)
12508     (gnus-set-mode-line 'summary)
12509     n))
12510
12511 (defun gnus-summary-insert-articles (articles)
12512   (when (setq articles
12513               (gnus-sorted-difference articles
12514                                       (mapcar (lambda (h)
12515                                                 (mail-header-number h))
12516                                               gnus-newsgroup-headers)))
12517     (setq gnus-newsgroup-headers
12518           (gnus-merge 'list
12519                       gnus-newsgroup-headers
12520                       (gnus-fetch-headers articles)
12521                       'gnus-article-sort-by-number))
12522     ;; Suppress duplicates?
12523     (when gnus-suppress-duplicates
12524       (gnus-dup-suppress-articles))
12525
12526     (if (and gnus-fetch-old-headers
12527              (eq gnus-headers-retrieved-by 'nov))
12528         ;; We might want to build some more threads first.
12529         (if (eq gnus-fetch-old-headers 'invisible)
12530             (gnus-build-all-threads)
12531           (gnus-build-old-threads))
12532       ;; Mark the inserted articles that are unread as unread.
12533       (setq gnus-newsgroup-unreads
12534             (gnus-sorted-nunion
12535              gnus-newsgroup-unreads
12536              (gnus-sorted-nintersection
12537               (gnus-list-of-unread-articles gnus-newsgroup-name)
12538               articles)))
12539       ;; Mark the inserted articles as selected so that the information
12540       ;; of the marks having been changed by a user may be updated when
12541       ;; exiting this group.  See `gnus-summary-update-info'.
12542       (dolist (art articles)
12543         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12544     ;; Let the Gnus agent mark articles as read.
12545     (when gnus-agent
12546       (gnus-agent-get-undownloaded-list))
12547     ;; Remove list identifiers from subject
12548     (when gnus-list-identifiers
12549       (gnus-summary-remove-list-identifiers))
12550     ;; First and last article in this newsgroup.
12551     (when gnus-newsgroup-headers
12552       (setq gnus-newsgroup-begin
12553             (mail-header-number (car gnus-newsgroup-headers))
12554             gnus-newsgroup-end
12555             (mail-header-number
12556              (gnus-last-element gnus-newsgroup-headers))))
12557     (when gnus-use-scoring
12558       (gnus-possibly-score-headers))))
12559
12560 (defun gnus-summary-insert-old-articles (&optional all)
12561   "Insert all old articles in this group.
12562 If ALL is non-nil, already read articles become readable.
12563 If ALL is a number, fetch this number of articles."
12564   (interactive "P")
12565   (prog1
12566       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12567             older len)
12568         (setq older
12569               ;; Some nntp servers lie about their active range.  When
12570               ;; this happens, the active range can be in the millions.
12571               ;; Use a compressed range to avoid creating a huge list.
12572               (gnus-range-difference (list gnus-newsgroup-active) old))
12573         (setq len (gnus-range-length older))
12574         (cond
12575          ((null older) nil)
12576          ((numberp all)
12577           (if (< all len)
12578               (let ((older-range (nreverse older)))
12579                 (setq older nil)
12580
12581                 (while (> all 0)
12582                   (let* ((r (pop older-range))
12583                          (min (if (numberp r) r (car r)))
12584                          (max (if (numberp r) r (cdr r))))
12585                     (while (and (<= min max)
12586                                 (> all 0))
12587                       (push max older)
12588                       (setq all (1- all)
12589                             max (1- max))))))
12590             (setq older (gnus-uncompress-range older))))
12591          (all
12592           (setq older (gnus-uncompress-range older)))
12593          (t
12594           (when (and (numberp gnus-large-newsgroup)
12595                    (> len gnus-large-newsgroup))
12596               (let* ((cursor-in-echo-area nil)
12597                      (initial (gnus-parameter-large-newsgroup-initial
12598                                gnus-newsgroup-name))
12599                      (input
12600                       (read-string
12601                        (format
12602                         "How many articles from %s (%s %d): "
12603                         (gnus-group-decoded-name gnus-newsgroup-name)
12604                         (if initial "max" "default")
12605                         len)
12606                        (if initial
12607                            (cons (number-to-string initial)
12608                                  0)))))
12609                 (unless (string-match "^[ \t]*$" input)
12610                   (setq all (string-to-number input))
12611                   (if (< all len)
12612                       (let ((older-range (nreverse older)))
12613                         (setq older nil)
12614
12615                         (while (> all 0)
12616                           (let* ((r (pop older-range))
12617                                  (min (if (numberp r) r (car r)))
12618                                  (max (if (numberp r) r (cdr r))))
12619                             (while (and (<= min max)
12620                                         (> all 0))
12621                               (push max older)
12622                               (setq all (1- all)
12623                                     max (1- max))))))))))
12624           (setq older (gnus-uncompress-range older))))
12625         (if (not older)
12626             (message "No old news.")
12627           (gnus-summary-insert-articles older)
12628           (gnus-summary-limit (gnus-sorted-nunion old older))))
12629     (gnus-summary-position-point)))
12630
12631 (defun gnus-summary-insert-new-articles ()
12632   "Insert all new articles in this group."
12633   (interactive)
12634   (prog1
12635       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12636             (old-high (cdr gnus-newsgroup-active))
12637             (nnmail-fetched-sources (list t))
12638             i new)
12639         (setq gnus-newsgroup-active
12640               (gnus-activate-group gnus-newsgroup-name 'scan))
12641         (setq i (cdr gnus-newsgroup-active))
12642         (while (> i old-high)
12643           (push i new)
12644           (decf i))
12645         (if (not new)
12646             (message "No gnus is bad news")
12647           (gnus-summary-insert-articles new)
12648           (setq gnus-newsgroup-unreads
12649                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12650           (gnus-summary-limit (gnus-sorted-nunion old new))))
12651     (gnus-summary-position-point)))
12652
12653 ;;; Bookmark support for Gnus.
12654 (declare-function bookmark-make-record-default
12655                   "bookmark" (&optional no-file no-context posn))
12656 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12657 (declare-function bookmark-default-handler "bookmark" (bmk))
12658 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12659 (defvar bookmark-yank-point)
12660 (defvar bookmark-current-buffer)
12661
12662 (defun gnus-summary-bookmark-make-record ()
12663   "Make a bookmark entry for a Gnus summary buffer."
12664   (let (pos buf)
12665     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12666       (save-restriction              ; FIXME is it necessary to widen?
12667         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12668       (setq buf "art") ; We are recording bookmark from article buffer.
12669       (setq bookmark-yank-point (point))
12670       (setq bookmark-current-buffer (current-buffer))
12671       (gnus-article-show-summary))      ; Go back in summary buffer.
12672     ;; We are now recording bookmark from summary buffer.
12673     (unless buf (setq buf "sum"))
12674     (let* ((subject (elt (gnus-summary-article-header) 1))
12675            (grp     (car gnus-article-current))
12676            (art     (cdr gnus-article-current))
12677            (head    (gnus-summary-article-header art))
12678            (id      (mail-header-id head)))
12679       `(,subject
12680         ,@(condition-case nil
12681               (bookmark-make-record-default 'no-file 'no-context pos)
12682             (wrong-number-of-arguments
12683              (bookmark-make-record-default 'point-only)))
12684         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12685         (group . ,grp) (article . ,art)
12686         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12687
12688 ;;;###autoload
12689 (defun gnus-summary-bookmark-jump (bookmark)
12690   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12691 BOOKMARK is a bookmark name or a bookmark record."
12692   (let ((group    (bookmark-prop-get bookmark 'group))
12693         (article  (bookmark-prop-get bookmark 'article))
12694         (id       (bookmark-prop-get bookmark 'message-id))
12695         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12696     (gnus-fetch-group group (list article))
12697     (gnus-summary-insert-cached-articles)
12698     (gnus-summary-goto-article id nil 'force)
12699     ;; FIXME we have to wait article buffer is ready (only large buffer)
12700     ;; Is there a better solution to know that?
12701     ;; If we don't wait `bookmark-default-handler' will have no chance
12702     ;; to set position. However there is no error, just wrong pos.
12703     (sit-for 1)
12704     (when (string= buf "Gnus-art")
12705       (other-window 1))
12706     (bookmark-default-handler
12707      `(""
12708        (buffer . ,(current-buffer))
12709        . ,(bookmark-get-bookmark-record bookmark)))))
12710
12711 (gnus-summary-make-all-marking-commands)
12712
12713 (gnus-ems-redefine)
12714
12715 (provide 'gnus-sum)
12716
12717 (run-hooks 'gnus-sum-load-hook)
12718
12719 ;; Local Variables:
12720 ;; coding: iso-8859-1
12721 ;; End:
12722
12723 ;;; gnus-sum.el ends here