Fix up partial nnimap fetching.
[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     (when gnus-use-cache
7094       (gnus-cache-possibly-remove-articles)
7095       (gnus-cache-save-buffers))
7096     (gnus-async-prefetch-remove-group group)
7097     (when gnus-suppress-duplicates
7098       (gnus-dup-enter-articles))
7099     (when gnus-use-trees
7100       (gnus-tree-close group))
7101     (when gnus-use-cache
7102       (gnus-cache-write-active))
7103     ;; Remove entries for this group.
7104     (nnmail-purge-split-history (gnus-group-real-name group))
7105     ;; Make all changes in this group permanent.
7106     (unless quit-config
7107       (gnus-run-hooks 'gnus-exit-group-hook)
7108       (gnus-summary-update-info))
7109     (gnus-close-group group)
7110     ;; Make sure where we were, and go to next newsgroup.
7111     (set-buffer gnus-group-buffer)
7112     (unless quit-config
7113       (gnus-group-jump-to-group group))
7114     (gnus-run-hooks 'gnus-summary-exit-hook)
7115     (unless (or quit-config
7116                 (not gnus-summary-next-group-on-exit)
7117                 ;; If this group has disappeared from the summary
7118                 ;; buffer, don't skip forwards.
7119                 (not (string= group (gnus-group-group-name))))
7120       (gnus-group-next-unread-group 1))
7121     (setq group-point (point))
7122     (if temporary
7123         nil                             ;Nothing to do.
7124       (set-buffer buf)
7125       (if (not gnus-kill-summary-on-exit)
7126           (progn
7127             (gnus-deaden-summary)
7128             (setq mode nil))
7129         ;; We set all buffer-local variables to nil.  It is unclear why
7130         ;; this is needed, but if we don't, buffer-local variables are
7131         ;; not garbage-collected, it seems.  This would the lead to en
7132         ;; ever-growing Emacs.
7133         (gnus-summary-clear-local-variables)
7134         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7135           (gnus-summary-clear-local-variables))
7136         (when (get-buffer gnus-article-buffer)
7137           (bury-buffer gnus-article-buffer))
7138         ;; Return to group mode buffer.
7139         (when (eq mode 'gnus-summary-mode)
7140           (gnus-kill-buffer buf)))
7141
7142       ;; If we have several article buffers, we kill them at exit.
7143       (unless gnus-single-article-buffer
7144         (when (gnus-buffer-live-p gnus-article-buffer)
7145           (with-current-buffer gnus-article-buffer
7146             ;; Don't kill sticky article buffers
7147             (unless (eq major-mode 'gnus-sticky-article-mode)
7148               (gnus-kill-buffer gnus-article-buffer)
7149               (setq gnus-article-current nil))))
7150         (gnus-kill-buffer gnus-original-article-buffer))
7151
7152       (setq gnus-current-select-method gnus-select-method)
7153       (set-buffer gnus-group-buffer)
7154       (if quit-config
7155           (gnus-handle-ephemeral-exit quit-config)
7156         (goto-char group-point)
7157         ;; If gnus-group-buffer is already displayed, make sure we also move
7158         ;; the cursor in the window that displays it.
7159         (let ((win (get-buffer-window (current-buffer) 0)))
7160           (if win (set-window-point win (point))))
7161         (unless leave-hidden
7162           (gnus-configure-windows 'group 'force)))
7163       ;; Clear the current group name.
7164       (unless quit-config
7165         (setq gnus-newsgroup-name nil)))))
7166
7167 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7168 (defun gnus-summary-exit-no-update (&optional no-questions)
7169   "Quit reading current newsgroup without updating read article info."
7170   (interactive)
7171   (let* ((group gnus-newsgroup-name)
7172          (gnus-group-is-exiting-p t)
7173          (gnus-group-is-exiting-without-update-p t)
7174          (quit-config (gnus-group-quit-config group)))
7175     (when (or no-questions
7176               gnus-expert-user
7177               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7178       (gnus-async-halt-prefetch)
7179       (run-hooks 'gnus-summary-prepare-exit-hook)
7180       (when (gnus-buffer-live-p gnus-article-buffer)
7181         (with-current-buffer gnus-article-buffer
7182           (mm-destroy-parts gnus-article-mime-handles)
7183           ;; Set it to nil for safety reason.
7184           (setq gnus-article-mime-handle-alist nil)
7185           (setq gnus-article-mime-handles nil)))
7186       ;; If we have several article buffers, we kill them at exit.
7187       (unless gnus-single-article-buffer
7188         (gnus-kill-buffer gnus-article-buffer)
7189         (gnus-kill-buffer gnus-original-article-buffer)
7190         (setq gnus-article-current nil))
7191       (if (not gnus-kill-summary-on-exit)
7192           (gnus-deaden-summary)
7193         (gnus-close-group group)
7194         (gnus-summary-clear-local-variables)
7195         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7196           (gnus-summary-clear-local-variables))
7197         (gnus-kill-buffer gnus-summary-buffer))
7198       (unless gnus-single-article-buffer
7199         (setq gnus-article-current nil))
7200       (when gnus-use-trees
7201         (gnus-tree-close group))
7202       (gnus-async-prefetch-remove-group group)
7203       (when (get-buffer gnus-article-buffer)
7204         (bury-buffer gnus-article-buffer))
7205       ;; Return to the group buffer.
7206       (gnus-configure-windows 'group 'force)
7207       ;; Clear the current group name.
7208       (setq gnus-newsgroup-name nil)
7209       (unless (gnus-ephemeral-group-p group)
7210         (gnus-group-update-group group))
7211       (when (equal (gnus-group-group-name) group)
7212         (gnus-group-next-unread-group 1))
7213       (when quit-config
7214         (gnus-handle-ephemeral-exit quit-config)))))
7215
7216 (defun gnus-handle-ephemeral-exit (quit-config)
7217   "Handle movement when leaving an ephemeral group.
7218 The state which existed when entering the ephemeral is reset."
7219   (if (not (buffer-name (car quit-config)))
7220       (gnus-configure-windows 'group 'force)
7221     (set-buffer (car quit-config))
7222     (cond ((eq major-mode 'gnus-summary-mode)
7223            (gnus-set-global-variables))
7224           ((eq major-mode 'gnus-article-mode)
7225            (save-current-buffer
7226              ;; The `gnus-summary-buffer' variable may point
7227              ;; to the old summary buffer when using a single
7228              ;; article buffer.
7229              (unless (gnus-buffer-live-p gnus-summary-buffer)
7230                (set-buffer gnus-group-buffer))
7231              (set-buffer gnus-summary-buffer)
7232              (gnus-set-global-variables))))
7233     (if (or (eq (cdr quit-config) 'article)
7234             (eq (cdr quit-config) 'pick))
7235         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7236             (gnus-configure-windows 'pick 'force)
7237           (gnus-configure-windows (cdr quit-config) 'force))
7238       (gnus-configure-windows (cdr quit-config) 'force))
7239     (when (eq major-mode 'gnus-summary-mode)
7240       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7241                                                      next-unread-noselect))
7242           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7243                                   'next-noselect)
7244                               (gnus-summary-next-subject 1 nil t))
7245                              ((eq gnus-auto-select-on-ephemeral-exit
7246                                   'next-unread-noselect)
7247                               (gnus-summary-next-subject 1 t t))))
7248             ;; Hide the article buffer which displays the article different
7249             ;; from the one that the cursor points to in the summary buffer.
7250             (gnus-configure-windows 'summary 'force))
7251         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7252                (gnus-summary-next-subject 1))
7253               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7254                (gnus-summary-next-subject 1 t))))
7255       (gnus-summary-recenter)
7256       (gnus-summary-position-point))))
7257
7258 ;;; Dead summaries.
7259
7260 (defvar gnus-dead-summary-mode-map
7261   (let ((map (make-keymap)))
7262     (suppress-keymap map)
7263     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7264     (dolist (key '("\C-d" "\r" "\177" [delete]))
7265       (define-key map key 'gnus-summary-wake-up-the-dead))
7266     (dolist (key '("q" "Q"))
7267       (define-key map key 'bury-buffer))
7268     map))
7269
7270 (define-minor-mode gnus-dead-summary-mode
7271   "Minor mode for Gnus summary buffers."
7272   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7273   (unless (derived-mode-p 'gnus-summary-mode)
7274     (setq gnus-dead-summary-mode nil)))
7275
7276 (defun gnus-deaden-summary ()
7277   "Make the current summary buffer into a dead summary buffer."
7278   ;; Kill any previous dead summary buffer.
7279   (when (and gnus-dead-summary
7280              (buffer-name gnus-dead-summary))
7281     (with-current-buffer gnus-dead-summary
7282       (when gnus-dead-summary-mode
7283         (kill-buffer (current-buffer)))))
7284   ;; Make this the current dead summary.
7285   (setq gnus-dead-summary (current-buffer))
7286   (gnus-dead-summary-mode 1)
7287   (let ((name (buffer-name)))
7288     (when (string-match "Summary" name)
7289       (rename-buffer
7290        (concat (substring name 0 (match-beginning 0)) "Dead "
7291                (substring name (match-beginning 0)))
7292        t)
7293       (bury-buffer))))
7294
7295 (defun gnus-kill-or-deaden-summary (buffer)
7296   "Kill or deaden the summary BUFFER."
7297   (save-excursion
7298     (when (and (buffer-name buffer)
7299                (not gnus-single-article-buffer))
7300       (with-current-buffer buffer
7301         (gnus-kill-buffer gnus-article-buffer)
7302         (gnus-kill-buffer gnus-original-article-buffer)))
7303     (cond
7304      ;; Kill the buffer.
7305      (gnus-kill-summary-on-exit
7306       (when (and gnus-use-trees
7307                  (gnus-buffer-exists-p buffer))
7308         (with-current-buffer buffer
7309           (gnus-tree-close gnus-newsgroup-name)))
7310       (gnus-kill-buffer buffer))
7311      ;; Deaden the buffer.
7312      ((gnus-buffer-exists-p buffer)
7313       (with-current-buffer buffer
7314         (gnus-deaden-summary))))))
7315
7316 (defun gnus-summary-wake-up-the-dead (&rest args)
7317   "Wake up the dead summary buffer."
7318   (interactive)
7319   (gnus-dead-summary-mode -1)
7320   (let ((name (buffer-name)))
7321     (when (string-match "Dead " name)
7322       (rename-buffer
7323        (concat (substring name 0 (match-beginning 0))
7324                (substring name (match-end 0)))
7325        t)))
7326   (gnus-message 3 "This dead summary is now alive again"))
7327
7328 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7329 (defun gnus-summary-describe-group (&optional force)
7330   "Describe the current newsgroup."
7331   (interactive "P")
7332   (gnus-group-describe-group force gnus-newsgroup-name))
7333
7334 (defun gnus-summary-describe-briefly ()
7335   "Describe summary mode commands briefly."
7336   (interactive)
7337   (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")))
7338
7339 ;; Walking around group mode buffer from summary mode.
7340
7341 (defun gnus-summary-next-group (&optional no-article target-group backward)
7342   "Exit current newsgroup and then select next unread newsgroup.
7343 If prefix argument NO-ARTICLE is non-nil, no article is selected
7344 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7345 previous group instead."
7346   (interactive "P")
7347   ;; Stop pre-fetching.
7348   (gnus-async-halt-prefetch)
7349   (let ((current-group gnus-newsgroup-name)
7350         (current-buffer (current-buffer))
7351         entered)
7352     ;; First we semi-exit this group to update Xrefs and all variables.
7353     ;; We can't do a real exit, because the window conf must remain
7354     ;; the same in case the user is prompted for info, and we don't
7355     ;; want the window conf to change before that...
7356     (gnus-summary-exit t)
7357     (while (not entered)
7358       ;; Then we find what group we are supposed to enter.
7359       (set-buffer gnus-group-buffer)
7360       (gnus-group-jump-to-group current-group)
7361       (setq target-group
7362             (or target-group
7363                 (if (eq gnus-keep-same-level 'best)
7364                     (gnus-summary-best-group gnus-newsgroup-name)
7365                   (gnus-summary-search-group backward gnus-keep-same-level))))
7366       (if (not target-group)
7367           ;; There are no further groups, so we return to the group
7368           ;; buffer.
7369           (progn
7370             (gnus-message 5 "Returning to the group buffer")
7371             (setq entered t)
7372             (when (gnus-buffer-live-p current-buffer)
7373               (set-buffer current-buffer)
7374               (gnus-summary-exit))
7375             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7376         ;; We try to enter the target group.
7377         (gnus-group-jump-to-group target-group)
7378         (let ((unreads (gnus-group-group-unread)))
7379           (if (and (or (eq t unreads)
7380                        (and unreads (not (zerop unreads))))
7381                    (gnus-summary-read-group
7382                     target-group nil no-article
7383                     (and (buffer-name current-buffer) current-buffer)
7384                     nil backward))
7385               (setq entered t)
7386             (setq current-group target-group
7387                   target-group nil)))))))
7388
7389 (defun gnus-summary-prev-group (&optional no-article)
7390   "Exit current newsgroup and then select previous unread newsgroup.
7391 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7392   (interactive "P")
7393   (gnus-summary-next-group no-article nil t))
7394
7395 ;; Walking around summary lines.
7396
7397 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7398   "Go to the first subject satisfying any non-nil constraint.
7399 If UNREAD is non-nil, the article should be unread.
7400 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7401 If UNSEEN is non-nil, the article should be unseen as well as unread.
7402 Returns the article selected or nil if there are no matching articles."
7403   (interactive "P")
7404   (cond
7405    ;; Empty summary.
7406    ((null gnus-newsgroup-data)
7407     (gnus-message 3 "No articles in the group")
7408     nil)
7409    ;; Pick the first article.
7410    ((not (or unread undownloaded unseen))
7411     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7412     (gnus-data-number (car gnus-newsgroup-data)))
7413    ;; Find the first unread article.
7414    (t
7415     (let ((data gnus-newsgroup-data))
7416       (while (and data
7417                   (let ((num (gnus-data-number (car data))))
7418                     (or (memq num gnus-newsgroup-unfetched)
7419                         (not (or (and unread
7420                                       (memq num gnus-newsgroup-unreads))
7421                                  (and undownloaded
7422                                       (memq num gnus-newsgroup-undownloaded))
7423                                  (and unseen
7424                                       (memq num gnus-newsgroup-unseen)
7425                                       (memq num gnus-newsgroup-unreads)))))))
7426         (setq data (cdr data)))
7427       (prog1
7428           (if data
7429               (progn
7430                 (goto-char (gnus-data-pos (car data)))
7431                 (gnus-data-number (car data)))
7432             (gnus-message 3 "No more%s articles"
7433                           (let* ((r (when unread " unread"))
7434                                  (d (when undownloaded " undownloaded"))
7435                                  (s (when unseen " unseen"))
7436                                  (l (delq nil (list r d s))))
7437                             (cond ((= 3 (length l))
7438                                    (concat r "," d ", or" s))
7439                                   ((= 2 (length l))
7440                                    (concat (car l) ", or" (cadr l)))
7441                                   ((= 1 (length l))
7442                                    (car l))
7443                                   (t
7444                                    ""))))
7445             nil
7446             )
7447         (gnus-summary-position-point))))))
7448
7449 (defun gnus-summary-next-subject (n &optional unread dont-display)
7450   "Go to next N'th summary line.
7451 If N is negative, go to the previous N'th subject line.
7452 If UNREAD is non-nil, only unread articles are selected.
7453 The difference between N and the actual number of steps taken is
7454 returned."
7455   (interactive "p")
7456   (let ((backward (< n 0))
7457         (n (abs n)))
7458     (while (and (> n 0)
7459                 (if backward
7460                     (gnus-summary-find-prev unread)
7461                   (gnus-summary-find-next unread)))
7462       (unless (zerop (setq n (1- n)))
7463         (gnus-summary-show-thread)))
7464     (when (/= 0 n)
7465       (gnus-message 7 "No more%s articles"
7466                     (if unread " unread" "")))
7467     (unless dont-display
7468       (gnus-summary-recenter)
7469       (gnus-summary-position-point))
7470     n))
7471
7472 (defun gnus-summary-next-unread-subject (n)
7473   "Go to next N'th unread summary line."
7474   (interactive "p")
7475   (gnus-summary-next-subject n t))
7476
7477 (defun gnus-summary-prev-subject (n &optional unread)
7478   "Go to previous N'th summary line.
7479 If optional argument UNREAD is non-nil, only unread article is selected."
7480   (interactive "p")
7481   (gnus-summary-next-subject (- n) unread))
7482
7483 (defun gnus-summary-prev-unread-subject (n)
7484   "Go to previous N'th unread summary line."
7485   (interactive "p")
7486   (gnus-summary-next-subject (- n) t))
7487
7488 (defun gnus-summary-goto-subjects (articles)
7489   "Insert the subject header for ARTICLES in the current buffer."
7490   (save-excursion
7491     (dolist (article articles)
7492       (gnus-summary-goto-subject article t)))
7493   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7494   (gnus-summary-position-point))
7495
7496 (defun gnus-summary-goto-subject (article &optional force silent)
7497   "Go to the subject line of ARTICLE.
7498 If FORCE, also allow jumping to articles not currently shown."
7499   (interactive "nArticle number: ")
7500   (unless (numberp article)
7501     (error "Article %s is not a number" article))
7502   (let ((b (point))
7503         (data (gnus-data-find article)))
7504     ;; We read in the article if we have to.
7505     (and (not data)
7506          force
7507          (gnus-summary-insert-subject
7508           article
7509           (if (or (numberp force) (vectorp force)) force)
7510           t)
7511          (setq data (gnus-data-find article)))
7512     (goto-char b)
7513     (if (not data)
7514         (progn
7515           (unless silent
7516             (gnus-message 3 "Can't find article %d" article))
7517           nil)
7518       (let ((pt (gnus-data-pos data)))
7519         (goto-char pt)
7520         (gnus-summary-set-article-display-arrow pt))
7521       (gnus-summary-position-point)
7522       article)))
7523
7524 ;; Walking around summary lines with displaying articles.
7525
7526 (defun gnus-summary-expand-window (&optional arg)
7527   "Make the summary buffer take up the entire Emacs frame.
7528 Given a prefix, will force an `article' buffer configuration."
7529   (interactive "P")
7530   (if arg
7531       (gnus-configure-windows 'article 'force)
7532     (gnus-configure-windows 'summary 'force)))
7533
7534 (defun gnus-summary-display-article (article &optional all-header)
7535   "Display ARTICLE in article buffer."
7536   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7537                (with-current-buffer gnus-article-buffer
7538                  (eq major-mode 'gnus-article-mode)))
7539     (gnus-article-setup-buffer))
7540   (gnus-set-global-variables)
7541   (with-current-buffer gnus-article-buffer
7542     (setq gnus-article-charset gnus-newsgroup-charset)
7543     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7544     (mm-enable-multibyte))
7545   (if (null article)
7546       nil
7547     (prog1
7548         (if gnus-summary-display-article-function
7549             (funcall gnus-summary-display-article-function article all-header)
7550           (gnus-article-prepare article all-header))
7551       (gnus-run-hooks 'gnus-select-article-hook)
7552       (when (and gnus-current-article
7553                  (not (zerop gnus-current-article)))
7554         (gnus-summary-goto-subject gnus-current-article))
7555       (gnus-summary-recenter)
7556       (when (and gnus-use-trees gnus-show-threads)
7557         (gnus-possibly-generate-tree article)
7558         (gnus-highlight-selected-tree article))
7559       ;; Successfully display article.
7560       (gnus-article-set-window-start
7561        (cdr (assq article gnus-newsgroup-bookmarks))))))
7562
7563 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7564   "Select the current article.
7565 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7566 non-nil, the article will be re-fetched even if it already present in
7567 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7568 be displayed."
7569   ;; Make sure we are in the summary buffer to work around bbdb bug.
7570   (unless (eq major-mode 'gnus-summary-mode)
7571     (set-buffer gnus-summary-buffer))
7572   (let ((article (or article (gnus-summary-article-number)))
7573         (all-headers (not (not all-headers))) ;Must be t or nil.
7574         gnus-summary-display-article-function)
7575     (and (not pseudo)
7576          (gnus-summary-article-pseudo-p article)
7577          (error "This is a pseudo-article"))
7578     (with-current-buffer gnus-summary-buffer
7579       (if (or (and gnus-single-article-buffer
7580                    (or (null gnus-current-article)
7581                        (null gnus-article-current)
7582                        (null (get-buffer gnus-article-buffer))
7583                        (not (eq article (cdr gnus-article-current)))
7584                        (not (equal (car gnus-article-current)
7585                                    gnus-newsgroup-name))))
7586               (and (not gnus-single-article-buffer)
7587                    (or (null gnus-current-article)
7588                        (not (eq gnus-current-article article))))
7589               force)
7590           ;; The requested article is different from the current article.
7591           (progn
7592             (gnus-summary-display-article article all-headers)
7593             (when (gnus-buffer-live-p gnus-article-buffer)
7594               (with-current-buffer gnus-article-buffer
7595                 (if (not gnus-article-decoded-p) ;; a local variable
7596                     (mm-disable-multibyte))))
7597             (gnus-article-set-window-start
7598              (cdr (assq article gnus-newsgroup-bookmarks)))
7599             article)
7600         'old))))
7601
7602 (defun gnus-summary-force-verify-and-decrypt ()
7603   "Display buttons for signed/encrypted parts and verify/decrypt them."
7604   (interactive)
7605   (let ((mm-verify-option 'known)
7606         (mm-decrypt-option 'known)
7607         (gnus-article-emulate-mime t)
7608         (gnus-buttonized-mime-types (append (list "multipart/signed"
7609                                                   "multipart/encrypted")
7610                                             gnus-buttonized-mime-types)))
7611     (gnus-summary-select-article nil 'force)))
7612
7613 (defun gnus-summary-set-current-mark (&optional current-mark)
7614   "Obsolete function."
7615   nil)
7616
7617 (defun gnus-summary-next-article (&optional unread subject backward push)
7618   "Select the next article.
7619 If UNREAD, only unread articles are selected.
7620 If SUBJECT, only articles with SUBJECT are selected.
7621 If BACKWARD, the previous article is selected instead of the next."
7622   (interactive "P")
7623   ;; Make sure we are in the summary buffer.
7624   (unless (eq major-mode 'gnus-summary-mode)
7625     (set-buffer gnus-summary-buffer))
7626   (cond
7627    ;; Is there such an article?
7628    ((and (gnus-summary-search-forward unread subject backward)
7629          (or (gnus-summary-display-article (gnus-summary-article-number))
7630              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7631     (gnus-summary-position-point))
7632    ;; If not, we try the first unread, if that is wanted.
7633    ((and subject
7634          gnus-auto-select-same
7635          (gnus-summary-first-unread-article))
7636     (gnus-summary-position-point)
7637     (gnus-message 6 "Wrapped"))
7638    ;; Try to get next/previous article not displayed in this group.
7639    ((and gnus-auto-extend-newsgroup
7640          (not unread) (not subject))
7641     (gnus-summary-goto-article
7642      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7643      nil (count-lines (point-min) (point))))
7644    ;; Go to next/previous group.
7645    (t
7646     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7647       (gnus-summary-jump-to-group gnus-newsgroup-name))
7648     (let ((cmd (if (featurep 'xemacs)
7649                    last-command-char
7650                  last-command-event))
7651           (point
7652            (with-current-buffer gnus-group-buffer
7653              (point)))
7654           (group
7655            (if (eq gnus-keep-same-level 'best)
7656                (gnus-summary-best-group gnus-newsgroup-name)
7657              (gnus-summary-search-group backward gnus-keep-same-level))))
7658       ;; For some reason, the group window gets selected.  We change
7659       ;; it back.
7660       (select-window (get-buffer-window (current-buffer)))
7661       ;; Select next unread newsgroup automagically.
7662       (cond
7663        ((or (not gnus-auto-select-next)
7664             (not cmd))
7665         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7666        ((or (eq gnus-auto-select-next 'quietly)
7667             (and (eq gnus-auto-select-next 'slightly-quietly)
7668                  push)
7669             (and (eq gnus-auto-select-next 'almost-quietly)
7670                  (gnus-summary-last-article-p)))
7671         ;; Select quietly.
7672         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7673             (gnus-summary-exit)
7674           (gnus-message 7 "No more%s articles (%s)..."
7675                         (if unread " unread" "")
7676                         (if group (concat "selecting " group)
7677                           "exiting"))
7678           (gnus-summary-next-group nil group backward)))
7679        (t
7680         (when (gnus-key-press-event-p last-input-event)
7681           (gnus-summary-walk-group-buffer
7682            gnus-newsgroup-name cmd unread backward point))))))))
7683
7684 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7685   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7686                       (?\C-p (gnus-group-prev-unread-group 1))))
7687         (cursor-in-echo-area t)
7688         keve key group ended prompt)
7689     (with-current-buffer gnus-group-buffer
7690       (goto-char start)
7691       (setq group
7692             (if (eq gnus-keep-same-level 'best)
7693                 (gnus-summary-best-group gnus-newsgroup-name)
7694               (gnus-summary-search-group backward gnus-keep-same-level))))
7695     (while (not ended)
7696       (setq prompt
7697             (format
7698              "No more%s articles%s " (if unread " unread" "")
7699              (if (and group
7700                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7701                  (format " (Type %s for %s [%s])"
7702                          (single-key-description cmd)
7703                          (gnus-group-decoded-name group)
7704                          (gnus-group-unread group))
7705                (format " (Type %s to exit %s)"
7706                        (single-key-description cmd)
7707                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7708       ;; Confirm auto selection.
7709       (setq key (car (setq keve (gnus-read-event-char prompt)))
7710             ended t)
7711       (cond
7712        ((assq key keystrokes)
7713         (let ((obuf (current-buffer)))
7714           (switch-to-buffer gnus-group-buffer)
7715           (when group
7716             (gnus-group-jump-to-group group))
7717           (eval (cadr (assq key keystrokes)))
7718           (setq group (gnus-group-group-name))
7719           (switch-to-buffer obuf))
7720         (setq ended nil))
7721        ((equal key cmd)
7722         (if (or (not group)
7723                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7724             (gnus-summary-exit)
7725           (gnus-summary-next-group nil group backward)))
7726        (t
7727         (push (cdr keve) unread-command-events))))))
7728
7729 (defun gnus-summary-next-unread-article ()
7730   "Select unread article after current one."
7731   (interactive)
7732   (gnus-summary-next-article
7733    (or (not (eq gnus-summary-goto-unread 'never))
7734        (gnus-summary-last-article-p (gnus-summary-article-number)))
7735    (and gnus-auto-select-same
7736         (gnus-summary-article-subject))))
7737
7738 (defun gnus-summary-prev-article (&optional unread subject)
7739   "Select the article before the current one.
7740 If UNREAD is non-nil, only unread articles are selected."
7741   (interactive "P")
7742   (gnus-summary-next-article unread subject t))
7743
7744 (defun gnus-summary-prev-unread-article ()
7745   "Select unread article before current one."
7746   (interactive)
7747   (gnus-summary-prev-article
7748    (or (not (eq gnus-summary-goto-unread 'never))
7749        (gnus-summary-first-article-p (gnus-summary-article-number)))
7750    (and gnus-auto-select-same
7751         (gnus-summary-article-subject))))
7752
7753 (defun gnus-summary-next-page (&optional lines circular stop)
7754   "Show next page of the selected article.
7755 If at the end of the current article, select the next article.
7756 LINES says how many lines should be scrolled up.
7757
7758 If CIRCULAR is non-nil, go to the start of the article instead of
7759 selecting the next article when reaching the end of the current
7760 article.
7761
7762 If STOP is non-nil, just stop when reaching the end of the message.
7763
7764 Also see the variable `gnus-article-skip-boring'."
7765   (interactive "P")
7766   (setq gnus-summary-buffer (current-buffer))
7767   (gnus-set-global-variables)
7768   (let ((article (gnus-summary-article-number))
7769         (article-window (get-buffer-window gnus-article-buffer t))
7770         endp)
7771     ;; If the buffer is empty, we have no article.
7772     (unless article
7773       (error "No article to select"))
7774     (gnus-configure-windows 'article)
7775     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7776         (if (and (eq gnus-summary-goto-unread 'never)
7777                  (not (gnus-summary-last-article-p article)))
7778             (gnus-summary-next-article)
7779           (gnus-summary-next-unread-article))
7780       (if (or (null gnus-current-article)
7781               (null gnus-article-current)
7782               (/= article (cdr gnus-article-current))
7783               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7784           ;; Selected subject is different from current article's.
7785           (gnus-summary-display-article article)
7786         (when article-window
7787           (gnus-eval-in-buffer-window gnus-article-buffer
7788             (setq endp (or (gnus-article-next-page lines)
7789                            (gnus-article-only-boring-p))))
7790           (when endp
7791             (cond ((or stop gnus-summary-stop-at-end-of-message)
7792                    (gnus-message 3 "End of message"))
7793                   (circular
7794                    (gnus-summary-beginning-of-article))
7795                   (lines
7796                    (gnus-message 3 "End of message"))
7797                   ((null lines)
7798                    (if (and (eq gnus-summary-goto-unread 'never)
7799                             (not (gnus-summary-last-article-p article)))
7800                        (gnus-summary-next-article)
7801                      (gnus-summary-next-unread-article))))))))
7802     (gnus-summary-recenter)
7803     (gnus-summary-position-point)))
7804
7805 (defun gnus-summary-prev-page (&optional lines move)
7806   "Show previous page of selected article.
7807 Argument LINES specifies lines to be scrolled down.
7808 If MOVE, move to the previous unread article if point is at
7809 the beginning of the buffer."
7810   (interactive "P")
7811   (let ((article (gnus-summary-article-number))
7812         (article-window (get-buffer-window gnus-article-buffer t))
7813         endp)
7814     (gnus-configure-windows 'article)
7815     (if (or (null gnus-current-article)
7816             (null gnus-article-current)
7817             (/= article (cdr gnus-article-current))
7818             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7819         ;; Selected subject is different from current article's.
7820         (gnus-summary-display-article article)
7821       (gnus-summary-recenter)
7822       (when article-window
7823         (gnus-eval-in-buffer-window gnus-article-buffer
7824           (setq endp (gnus-article-prev-page lines)))
7825         (when (and move endp)
7826           (cond (lines
7827                  (gnus-message 3 "Beginning of message"))
7828                 ((null lines)
7829                  (if (and (eq gnus-summary-goto-unread 'never)
7830                           (not (gnus-summary-first-article-p article)))
7831                      (gnus-summary-prev-article)
7832                    (gnus-summary-prev-unread-article))))))))
7833   (gnus-summary-position-point))
7834
7835 (defun gnus-summary-prev-page-or-article (&optional lines)
7836   "Show previous page of selected article.
7837 Argument LINES specifies lines to be scrolled down.
7838 If at the beginning of the article, go to the next article."
7839   (interactive "P")
7840   (gnus-summary-prev-page lines t))
7841
7842 (defun gnus-summary-scroll-up (lines)
7843   "Scroll up (or down) one line current article.
7844 Argument LINES specifies lines to be scrolled up (or down if negative)."
7845   (interactive "p")
7846   (gnus-configure-windows 'article)
7847   (gnus-summary-show-thread)
7848   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7849     (gnus-eval-in-buffer-window gnus-article-buffer
7850       (cond ((> lines 0)
7851              (when (gnus-article-next-page lines)
7852                (gnus-message 3 "End of message")))
7853             ((< lines 0)
7854              (gnus-article-prev-page (- lines))))))
7855   (gnus-summary-recenter)
7856   (gnus-summary-position-point))
7857
7858 (defun gnus-summary-scroll-down (lines)
7859   "Scroll down (or up) one line current article.
7860 Argument LINES specifies lines to be scrolled down (or up if negative)."
7861   (interactive "p")
7862   (gnus-summary-scroll-up (- lines)))
7863
7864 (defun gnus-summary-next-same-subject ()
7865   "Select next article which has the same subject as current one."
7866   (interactive)
7867   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7868
7869 (defun gnus-summary-prev-same-subject ()
7870   "Select previous article which has the same subject as current one."
7871   (interactive)
7872   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7873
7874 (defun gnus-summary-next-unread-same-subject ()
7875   "Select next unread article which has the same subject as current one."
7876   (interactive)
7877   (gnus-summary-next-article t (gnus-summary-article-subject)))
7878
7879 (defun gnus-summary-prev-unread-same-subject ()
7880   "Select previous unread article which has the same subject as current one."
7881   (interactive)
7882   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7883
7884 (defun gnus-summary-first-unread-article ()
7885   "Select the first unread article.
7886 Return nil if there are no unread articles."
7887   (interactive)
7888   (prog1
7889       (when (gnus-summary-first-subject t)
7890         (gnus-summary-show-thread)
7891         (gnus-summary-first-subject t)
7892         (gnus-summary-display-article (gnus-summary-article-number)))
7893     (gnus-summary-position-point)))
7894
7895 (defun gnus-summary-first-unread-subject ()
7896   "Place the point on the subject line of the first unread article.
7897 Return nil if there are no unread articles."
7898   (interactive)
7899   (prog1
7900       (when (gnus-summary-first-subject t)
7901         (gnus-summary-show-thread)
7902         (gnus-summary-first-subject t))
7903     (gnus-summary-position-point)))
7904
7905 (defun gnus-summary-first-unseen-subject ()
7906   "Place the point on the subject line of the first unseen article.
7907 Return nil if there are no unseen articles."
7908   (interactive)
7909   (prog1
7910       (when (gnus-summary-first-subject nil nil t)
7911         (gnus-summary-show-thread)
7912         (gnus-summary-first-subject nil nil t))
7913     (gnus-summary-position-point)))
7914
7915 (defun gnus-summary-first-unseen-or-unread-subject ()
7916   "Place the point on the subject line of the first unseen and unread article.
7917 If all article have been seen, on the subject line of the first unread
7918 article."
7919   (interactive)
7920   (prog1
7921       (unless (when (gnus-summary-first-subject nil nil t)
7922                 (gnus-summary-show-thread)
7923                 (gnus-summary-first-subject nil nil t))
7924         (when (gnus-summary-first-subject t)
7925           (gnus-summary-show-thread)
7926           (gnus-summary-first-subject t)))
7927     (gnus-summary-position-point)))
7928
7929 (defun gnus-summary-first-article ()
7930   "Select the first article.
7931 Return nil if there are no articles."
7932   (interactive)
7933   (prog1
7934       (when (gnus-summary-first-subject)
7935         (gnus-summary-show-thread)
7936         (gnus-summary-first-subject)
7937         (gnus-summary-display-article (gnus-summary-article-number)))
7938     (gnus-summary-position-point)))
7939
7940 (defun gnus-summary-best-unread-article (&optional arg)
7941   "Select the unread article with the highest score.
7942 If given a prefix argument, select the next unread article that has a
7943 score higher than the default score."
7944   (interactive "P")
7945   (let ((article (if arg
7946                      (gnus-summary-better-unread-subject)
7947                    (gnus-summary-best-unread-subject))))
7948     (if article
7949         (gnus-summary-goto-article article)
7950       (error "No unread articles"))))
7951
7952 (defun gnus-summary-best-unread-subject ()
7953   "Select the unread subject with the highest score."
7954   (interactive)
7955   (let ((best -1000000)
7956         (data gnus-newsgroup-data)
7957         article score)
7958     (while data
7959       (and (gnus-data-unread-p (car data))
7960            (> (setq score
7961                     (gnus-summary-article-score (gnus-data-number (car data))))
7962               best)
7963            (setq best score
7964                  article (gnus-data-number (car data))))
7965       (setq data (cdr data)))
7966     (when article
7967       (gnus-summary-goto-subject article))
7968     (gnus-summary-position-point)
7969     article))
7970
7971 (defun gnus-summary-better-unread-subject ()
7972   "Select the first unread subject that has a score over the default score."
7973   (interactive)
7974   (let ((data gnus-newsgroup-data)
7975         article score)
7976     (while (and (setq article (gnus-data-number (car data)))
7977                 (or (gnus-data-read-p (car data))
7978                     (not (> (gnus-summary-article-score article)
7979                             gnus-summary-default-score))))
7980       (setq data (cdr data)))
7981     (when article
7982       (gnus-summary-goto-subject article))
7983     (gnus-summary-position-point)
7984     article))
7985
7986 (defun gnus-summary-last-subject ()
7987   "Go to the last displayed subject line in the group."
7988   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7989     (when article
7990       (gnus-summary-goto-subject article))))
7991
7992 (defun gnus-summary-goto-article (article &optional all-headers force)
7993   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7994 If ALL-HEADERS is non-nil, no header lines are hidden.
7995 If FORCE, go to the article even if it isn't displayed.  If FORCE
7996 is a number, it is the line the article is to be displayed on."
7997   (interactive
7998    (list
7999     (completing-read
8000      "Article number or Message-ID: "
8001      (mapcar (lambda (number) (list (int-to-string number)))
8002              gnus-newsgroup-limit))
8003     current-prefix-arg
8004     t))
8005   (prog1
8006       (if (and (stringp article)
8007                (string-match "@\\|%40" article))
8008           (gnus-summary-refer-article article)
8009         (when (stringp article)
8010           (setq article (string-to-number article)))
8011         (if (gnus-summary-goto-subject article force)
8012             (gnus-summary-display-article article all-headers)
8013           (gnus-message 4 "Couldn't go to article %s" article) nil))
8014     (gnus-summary-position-point)))
8015
8016 (defun gnus-summary-goto-last-article ()
8017   "Go to the previously read article."
8018   (interactive)
8019   (prog1
8020       (when gnus-last-article
8021         (gnus-summary-goto-article gnus-last-article nil t))
8022     (gnus-summary-position-point)))
8023
8024 (defun gnus-summary-pop-article (number)
8025   "Pop one article off the history and go to the previous.
8026 NUMBER articles will be popped off."
8027   (interactive "p")
8028   (let (to)
8029     (setq gnus-newsgroup-history
8030           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8031     (if to
8032         (gnus-summary-goto-article (car to) nil t)
8033       (error "Article history empty")))
8034   (gnus-summary-position-point))
8035
8036 ;; Summary commands and functions for limiting the summary buffer.
8037
8038 (defun gnus-summary-limit-to-articles (n)
8039   "Limit the summary buffer to the next N articles.
8040 If not given a prefix, use the process marked articles instead."
8041   (interactive "P")
8042   (prog1
8043       (let ((articles (gnus-summary-work-articles n)))
8044         (setq gnus-newsgroup-processable nil)
8045         (gnus-summary-limit articles))
8046     (gnus-summary-position-point)))
8047
8048 (defun gnus-summary-pop-limit (&optional total)
8049   "Restore the previous limit.
8050 If given a prefix, remove all limits."
8051   (interactive "P")
8052   (when total
8053     (setq gnus-newsgroup-limits
8054           (list (mapcar (lambda (h) (mail-header-number h))
8055                         gnus-newsgroup-headers))))
8056   (unless gnus-newsgroup-limits
8057     (error "No limit to pop"))
8058   (prog1
8059       (gnus-summary-limit nil 'pop)
8060     (gnus-summary-position-point)))
8061
8062 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8063   "Limit the summary buffer to articles that have subjects that match a regexp.
8064 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8065   (interactive
8066    (list (read-string (if current-prefix-arg
8067                           "Exclude subject (regexp): "
8068                         "Limit to subject (regexp): "))
8069          nil current-prefix-arg))
8070   (unless header
8071     (setq header "subject"))
8072   (when (not (equal "" subject))
8073     (prog1
8074         (let ((articles (gnus-summary-find-matching
8075                          (or header "subject") subject 'all nil nil
8076                          not-matching)))
8077           (unless articles
8078             (error "Found no matches for \"%s\"" subject))
8079           (gnus-summary-limit articles))
8080       (gnus-summary-position-point))))
8081
8082 (defun gnus-summary-limit-to-author (from &optional not-matching)
8083   "Limit the summary buffer to articles that have authors that match a regexp.
8084 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8085   (interactive
8086    (list (read-string (if current-prefix-arg
8087                           "Exclude author (regexp): "
8088                         "Limit to author (regexp): "))
8089          current-prefix-arg))
8090   (gnus-summary-limit-to-subject from "from" not-matching))
8091
8092 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8093   "Limit the summary buffer to articles with the given RECIPIENT.
8094
8095 If NOT-MATCHING, exclude RECIPIENT.
8096
8097 To and Cc headers are checked.  You need to include them in
8098 `nnmail-extra-headers'."
8099   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8100   (interactive
8101    (list (read-string (format "%s recipient (regexp): "
8102                               (if current-prefix-arg "Exclude" "Limit to")))
8103          current-prefix-arg))
8104   (when (not (equal "" recipient))
8105     (prog1 (let* ((to
8106                    (if (memq 'To nnmail-extra-headers)
8107                        (gnus-summary-find-matching
8108                         (cons 'extra 'To) recipient 'all nil nil
8109                         not-matching)
8110                      (gnus-message
8111                       1 "`To' isn't present in `nnmail-extra-headers'")
8112                      (sit-for 1)
8113                      nil))
8114                   (cc
8115                    (if (memq 'Cc nnmail-extra-headers)
8116                        (gnus-summary-find-matching
8117                         (cons 'extra 'Cc) recipient 'all nil nil
8118                         not-matching)
8119                      (gnus-message
8120                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8121                      (sit-for 1)
8122                      nil))
8123                   (articles
8124                    (if not-matching
8125                        ;; We need the numbers that are in both lists:
8126                        (mapcar (lambda (a)
8127                                  (and (memq a to) a))
8128                                cc)
8129                      (nconc to cc))))
8130              (unless articles
8131                (error "Found no matches for \"%s\"" recipient))
8132              (gnus-summary-limit articles))
8133       (gnus-summary-position-point))))
8134
8135 (defun gnus-summary-limit-to-address (address &optional not-matching)
8136   "Limit the summary buffer to articles with the given ADDRESS.
8137
8138 If NOT-MATCHING, exclude ADDRESS.
8139
8140 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8141 in `nnmail-extra-headers'."
8142   (interactive
8143    (list (read-string (format "%s address (regexp): "
8144                               (if current-prefix-arg "Exclude" "Limit to")))
8145          current-prefix-arg))
8146   (when (not (equal "" address))
8147     (prog1 (let* ((to
8148                    (if (memq 'To nnmail-extra-headers)
8149                        (gnus-summary-find-matching
8150                         (cons 'extra 'To) address 'all nil nil
8151                         not-matching)
8152                      (gnus-message
8153                       1 "`To' isn't present in `nnmail-extra-headers'")
8154                      (sit-for 1)
8155                      t))
8156                   (cc
8157                    (if (memq 'Cc nnmail-extra-headers)
8158                        (gnus-summary-find-matching
8159                         (cons 'extra 'Cc) address 'all nil nil
8160                         not-matching)
8161                      (gnus-message
8162                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8163                      (sit-for 1)
8164                      t))
8165                   (from
8166                    (gnus-summary-find-matching "from" address
8167                                                'all nil nil not-matching))
8168                   (articles
8169                    (if not-matching
8170                        ;; We need the numbers that are in all lists:
8171                        (if (eq cc t)
8172                            (if (eq to t)
8173                                from
8174                              (mapcar (lambda (a) (car (memq a from))) to))
8175                          (if (eq to t)
8176                              (mapcar (lambda (a) (car (memq a from))) cc)
8177                            (mapcar (lambda (a) (car (memq a from)))
8178                                    (mapcar (lambda (a) (car (memq a to)))
8179                                            cc))))
8180                      (nconc (if (eq to t) nil to)
8181                             (if (eq cc t) nil cc)
8182                             from))))
8183              (unless articles
8184                (error "Found no matches for \"%s\"" address))
8185              (gnus-summary-limit articles))
8186       (gnus-summary-position-point))))
8187
8188 (defun gnus-summary-limit-strange-charsets-predicate (header)
8189   (when (fboundp 'char-charset)
8190     (let ((string (concat (mail-header-subject header)
8191                           (mail-header-from header)))
8192           charset found)
8193       (dotimes (i (1- (length string)))
8194         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8195         (when (string-match "unicode\\|big\\|japanese" charset)
8196           (setq found t)))
8197       found)))
8198
8199 (defun gnus-summary-limit-to-predicate (predicate)
8200   "Limit to articles where PREDICATE returns non-nil.
8201 PREDICATE will be called with the header structures of the
8202 articles."
8203   (let ((articles nil)
8204         (case-fold-search t))
8205     (dolist (header gnus-newsgroup-headers)
8206       (when (funcall predicate header)
8207         (push (mail-header-number header) articles)))
8208     (gnus-summary-limit (nreverse articles))))
8209
8210 (defun gnus-summary-limit-to-age (age &optional younger-p)
8211   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8212 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8213 articles that are younger than AGE days."
8214   (interactive
8215    (let ((younger current-prefix-arg)
8216          (days-got nil)
8217          days)
8218      (while (not days-got)
8219        (setq days (if younger
8220                       (read-string "Limit to articles younger than (in days, older when negative): ")
8221                     (read-string
8222                      "Limit to articles older than (in days, younger when negative): ")))
8223        (when (> (length days) 0)
8224          (setq days (read days)))
8225        (if (numberp days)
8226            (progn
8227              (setq days-got t)
8228              (when (< days 0)
8229                (setq younger (not younger))
8230                (setq days (* days -1))))
8231          (message "Please enter a number.")
8232          (sleep-for 1)))
8233      (list days younger)))
8234   (prog1
8235       (let ((data gnus-newsgroup-data)
8236             (cutoff (days-to-time age))
8237             articles d date is-younger)
8238         (while (setq d (pop data))
8239           (when (and (vectorp (gnus-data-header d))
8240                      (setq date (mail-header-date (gnus-data-header d))))
8241             (setq is-younger (time-less-p
8242                               (time-since (gnus-date-get-time date))
8243                               cutoff))
8244             (when (if younger-p
8245                       is-younger
8246                     (not is-younger))
8247               (push (gnus-data-number d) articles))))
8248         (gnus-summary-limit (nreverse articles)))
8249     (gnus-summary-position-point)))
8250
8251 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8252   "Limit the summary buffer to articles that match an 'extra' header."
8253   (interactive
8254    (let ((header
8255           (intern
8256            (gnus-completing-read-with-default
8257             (symbol-name (car gnus-extra-headers))
8258             (if current-prefix-arg
8259                 "Exclude extra header"
8260               "Limit extra header")
8261             (mapcar (lambda (x)
8262                       (cons (symbol-name x) x))
8263                     gnus-extra-headers)
8264             nil
8265             t))))
8266      (list header
8267            (read-string (format "%s header %s (regexp): "
8268                                 (if current-prefix-arg "Exclude" "Limit to")
8269                                 header))
8270            current-prefix-arg)))
8271   (when (not (equal "" regexp))
8272     (prog1
8273         (let ((articles (gnus-summary-find-matching
8274                          (cons 'extra header) regexp 'all nil nil
8275                          not-matching)))
8276           (unless articles
8277             (error "Found no matches for \"%s\"" regexp))
8278           (gnus-summary-limit articles))
8279       (gnus-summary-position-point))))
8280
8281 (defun gnus-summary-limit-to-display-predicate ()
8282   "Limit the summary buffer to the predicated in the `display' group parameter."
8283   (interactive)
8284   (unless gnus-newsgroup-display
8285     (error "There is no `display' group parameter"))
8286   (let (articles)
8287     (dolist (number gnus-newsgroup-articles)
8288       (when (funcall gnus-newsgroup-display)
8289         (push number articles)))
8290     (gnus-summary-limit articles))
8291   (gnus-summary-position-point))
8292
8293 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8294 (make-obsolete
8295  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8296
8297 (defun gnus-summary-limit-to-unread (&optional all)
8298   "Limit the summary buffer to articles that are not marked as read.
8299 If ALL is non-nil, limit strictly to unread articles."
8300   (interactive "P")
8301   (if all
8302       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8303     (gnus-summary-limit-to-marks
8304      ;; Concat all the marks that say that an article is read and have
8305      ;; those removed.
8306      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8307            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8308            gnus-low-score-mark gnus-expirable-mark
8309            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8310            gnus-duplicate-mark)
8311      'reverse)))
8312
8313 (defun gnus-summary-limit-to-headers (match &optional reverse)
8314   "Limit the summary buffer to articles that have headers that match MATCH.
8315 If REVERSE (the prefix), limit to articles that don't match."
8316   (interactive "sMatch headers (regexp): \nP")
8317   (gnus-summary-limit-to-bodies match reverse t))
8318
8319 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8320   "Limit the summary buffer to articles that have bodies that match MATCH.
8321 If REVERSE (the prefix), limit to articles that don't match."
8322   (interactive "sMatch body (regexp): \nP")
8323   (let ((articles nil)
8324         (gnus-select-article-hook nil)  ;Disable hook.
8325         (gnus-article-prepare-hook nil)
8326         (gnus-use-article-prefetch nil)
8327         (gnus-keep-backlog nil)
8328         (gnus-break-pages nil)
8329         (gnus-summary-display-arrow nil)
8330         (gnus-updated-mode-lines nil)
8331         (gnus-auto-center-summary nil)
8332         (gnus-display-mime-function nil))
8333     (dolist (data gnus-newsgroup-data)
8334       (let (gnus-mark-article-hook)
8335         (gnus-summary-select-article t t nil (gnus-data-number data)))
8336       (with-current-buffer gnus-article-buffer
8337         (article-goto-body)
8338         (let* ((case-fold-search t)
8339                (found (if headersp
8340                           (re-search-backward match nil t)
8341                         (re-search-forward match nil t))))
8342           (when (or (and found
8343                          (not reverse))
8344                     (and (not found)
8345                          reverse))
8346             (push (gnus-data-number data) articles)))))
8347     (if (not articles)
8348         (message "No messages matched")
8349       (gnus-summary-limit articles)))
8350   (gnus-summary-position-point))
8351
8352 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8353   "Limit the summary buffer to articles that aren't part on any thread.
8354 If THREADSP (the prefix), limit to articles that are in threads."
8355   (interactive "P")
8356   (let ((articles nil)
8357         thread-articles
8358         threads)
8359     (dolist (thread gnus-newsgroup-threads)
8360       (if (stringp (car thread))
8361           (dolist (thread (cdr thread))
8362             (push thread threads))
8363         (push thread threads)))
8364     (dolist (thread threads)
8365       (setq thread-articles (gnus-articles-in-thread thread))
8366       (when (or (and threadsp
8367                      (> (length thread-articles) 1))
8368                 (and (not threadsp)
8369                      (= (length thread-articles) 1)))
8370         (setq articles (nconc thread-articles articles))))
8371     (if (not articles)
8372         (message "No messages matched")
8373       (gnus-summary-limit articles))
8374     (gnus-summary-position-point)))
8375
8376 (defun gnus-summary-limit-to-replied (&optional unreplied)
8377   "Limit the summary buffer to replied articles.
8378 If UNREPLIED (the prefix), limit to unreplied articles."
8379   (interactive "P")
8380   (if unreplied
8381       (gnus-summary-limit
8382        (gnus-set-difference gnus-newsgroup-articles
8383         gnus-newsgroup-replied))
8384     (gnus-summary-limit gnus-newsgroup-replied))
8385   (gnus-summary-position-point))
8386
8387 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8388 (make-obsolete 'gnus-summary-delete-marked-with
8389                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8390
8391 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8392   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8393 If REVERSE, limit the summary buffer to articles that are marked
8394 with MARKS.  MARKS can either be a string of marks or a list of marks.
8395 Returns how many articles were removed."
8396   (interactive "sMarks: ")
8397   (gnus-summary-limit-to-marks marks t))
8398
8399 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8400   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8401 If REVERSE (the prefix), limit the summary buffer to articles that are
8402 not marked with MARKS.  MARKS can either be a string of marks or a
8403 list of marks.
8404 Returns how many articles were removed."
8405   (interactive "sMarks: \nP")
8406   (prog1
8407       (let ((data gnus-newsgroup-data)
8408             (marks (if (listp marks) marks
8409                      (append marks nil))) ; Transform to list.
8410             articles)
8411         (while data
8412           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8413                   (memq (gnus-data-mark (car data)) marks))
8414             (push (gnus-data-number (car data)) articles))
8415           (setq data (cdr data)))
8416         (gnus-summary-limit articles))
8417     (gnus-summary-position-point)))
8418
8419 (defun gnus-summary-limit-to-score (score)
8420   "Limit to articles with score at or above SCORE."
8421   (interactive "NLimit to articles with score of at least: ")
8422   (let ((data gnus-newsgroup-data)
8423         articles)
8424     (while data
8425       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8426                 score)
8427         (push (gnus-data-number (car data)) articles))
8428       (setq data (cdr data)))
8429     (prog1
8430         (gnus-summary-limit articles)
8431       (gnus-summary-position-point))))
8432
8433 (defun gnus-summary-limit-to-unseen ()
8434   "Limit to unseen articles."
8435   (interactive)
8436   (prog1
8437       (gnus-summary-limit gnus-newsgroup-unseen)
8438     (gnus-summary-position-point)))
8439
8440 (defun gnus-summary-limit-include-thread (id)
8441   "Display all the hidden articles that is in the thread with ID in it.
8442 When called interactively, ID is the Message-ID of the current
8443 article."
8444   (interactive (list (mail-header-id (gnus-summary-article-header))))
8445   (let ((articles (gnus-articles-in-thread
8446                    (gnus-id-to-thread (gnus-root-id id)))))
8447     (prog1
8448         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8449       (gnus-summary-limit-include-matching-articles
8450        "subject"
8451        (regexp-quote (gnus-simplify-subject-re
8452                       (mail-header-subject (gnus-id-to-header id)))))
8453       (gnus-summary-position-point))))
8454
8455 (defun gnus-summary-limit-include-matching-articles (header regexp)
8456   "Display all the hidden articles that have HEADERs that match REGEXP."
8457   (interactive (list (read-string "Match on header: ")
8458                      (read-string "Regexp: ")))
8459   (let ((articles (gnus-find-matching-articles header regexp)))
8460     (prog1
8461         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8462       (gnus-summary-position-point))))
8463
8464 (defun gnus-summary-insert-dormant-articles ()
8465   "Insert all the dormant articles for this group into the current buffer."
8466   (interactive)
8467   (let ((gnus-verbose (max 6 gnus-verbose)))
8468     (if (not gnus-newsgroup-dormant)
8469         (gnus-message 3 "No dormant articles for this group")
8470       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8471
8472 (defun gnus-summary-insert-ticked-articles ()
8473   "Insert ticked articles for this group into the current buffer."
8474   (interactive)
8475   (let ((gnus-verbose (max 6 gnus-verbose)))
8476     (if (not gnus-newsgroup-marked)
8477         (gnus-message 3 "No ticked articles for this group")
8478       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8479
8480 (defun gnus-summary-limit-include-dormant ()
8481   "Display all the hidden articles that are marked as dormant.
8482 Note that this command only works on a subset of the articles currently
8483 fetched for this group."
8484   (interactive)
8485   (unless gnus-newsgroup-dormant
8486     (error "There are no dormant articles in this group"))
8487   (prog1
8488       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8489     (gnus-summary-position-point)))
8490
8491 (defun gnus-summary-limit-exclude-dormant ()
8492   "Hide all dormant articles."
8493   (interactive)
8494   (prog1
8495       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8496     (gnus-summary-position-point)))
8497
8498 (defun gnus-summary-limit-exclude-childless-dormant ()
8499   "Hide all dormant articles that have no children."
8500   (interactive)
8501   (let ((data (gnus-data-list t))
8502         articles d children)
8503     ;; Find all articles that are either not dormant or have
8504     ;; children.
8505     (while (setq d (pop data))
8506       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8507                 (and (setq children
8508                            (gnus-article-children (gnus-data-number d)))
8509                      (let (found)
8510                        (while children
8511                          (when (memq (car children) articles)
8512                            (setq children nil
8513                                  found t))
8514                          (pop children))
8515                        found)))
8516         (push (gnus-data-number d) articles)))
8517     ;; Do the limiting.
8518     (prog1
8519         (gnus-summary-limit articles)
8520       (gnus-summary-position-point))))
8521
8522 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8523   "Mark all unread excluded articles as read.
8524 If ALL, mark even excluded ticked and dormants as read."
8525   (interactive "P")
8526   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8527   (let ((articles (gnus-sorted-ndifference
8528                    (sort
8529                     (mapcar (lambda (h) (mail-header-number h))
8530                             gnus-newsgroup-headers)
8531                     '<)
8532                    gnus-newsgroup-limit))
8533         article)
8534     (setq gnus-newsgroup-unreads
8535           (gnus-sorted-intersection gnus-newsgroup-unreads
8536                                     gnus-newsgroup-limit))
8537     (if all
8538         (setq gnus-newsgroup-dormant nil
8539               gnus-newsgroup-marked nil
8540               gnus-newsgroup-reads
8541               (nconc
8542                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8543                gnus-newsgroup-reads))
8544       (while (setq article (pop articles))
8545         (unless (or (memq article gnus-newsgroup-dormant)
8546                     (memq article gnus-newsgroup-marked))
8547           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8548
8549 (defun gnus-summary-limit (articles &optional pop)
8550   (if pop
8551       ;; We pop the previous limit off the stack and use that.
8552       (setq articles (car gnus-newsgroup-limits)
8553             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8554     ;; We use the new limit, so we push the old limit on the stack.
8555     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8556   ;; Set the limit.
8557   (setq gnus-newsgroup-limit articles)
8558   (let ((total (length gnus-newsgroup-data))
8559         (data (gnus-data-find-list (gnus-summary-article-number)))
8560         (gnus-summary-mark-below nil)   ; Inhibit this.
8561         found)
8562     ;; This will do all the work of generating the new summary buffer
8563     ;; according to the new limit.
8564     (gnus-summary-prepare)
8565     ;; Hide any threads, possibly.
8566     (gnus-summary-maybe-hide-threads)
8567     ;; Try to return to the article you were at, or one in the
8568     ;; neighborhood.
8569     (when data
8570       ;; We try to find some article after the current one.
8571       (while data
8572         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8573           (setq data nil
8574                 found t))
8575         (setq data (cdr data))))
8576     (unless found
8577       ;; If there is no data, that means that we were after the last
8578       ;; article.  The same goes when we can't find any articles
8579       ;; after the current one.
8580       (goto-char (point-max))
8581       (gnus-summary-find-prev))
8582     (gnus-set-mode-line 'summary)
8583     ;; We return how many articles were removed from the summary
8584     ;; buffer as a result of the new limit.
8585     (- total (length gnus-newsgroup-data))))
8586
8587 (defsubst gnus-invisible-cut-children (threads)
8588   (let ((num 0))
8589     (while threads
8590       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8591         (incf num))
8592       (pop threads))
8593     (< num 2)))
8594
8595 (defsubst gnus-cut-thread (thread)
8596   "Go forwards in the thread until we find an article that we want to display."
8597   (when (or (eq gnus-fetch-old-headers 'some)
8598             (eq gnus-fetch-old-headers 'invisible)
8599             (numberp gnus-fetch-old-headers)
8600             (eq gnus-build-sparse-threads 'some)
8601             (eq gnus-build-sparse-threads 'more))
8602     ;; Deal with old-fetched headers and sparse threads.
8603     (while (and
8604             thread
8605             (or
8606              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8607              (gnus-summary-article-ancient-p
8608               (mail-header-number (car thread))))
8609             (if (or (<= (length (cdr thread)) 1)
8610                     (eq gnus-fetch-old-headers 'invisible))
8611                 (setq gnus-newsgroup-limit
8612                       (delq (mail-header-number (car thread))
8613                             gnus-newsgroup-limit)
8614                       thread (cadr thread))
8615               (when (gnus-invisible-cut-children (cdr thread))
8616                 (let ((th (cdr thread)))
8617                   (while th
8618                     (if (memq (mail-header-number (caar th))
8619                               gnus-newsgroup-limit)
8620                         (setq thread (car th)
8621                               th nil)
8622                       (setq th (cdr th))))))))))
8623   thread)
8624
8625 (defun gnus-cut-threads (threads)
8626   "Cut off all uninteresting articles from the beginning of THREADS."
8627   (when (or (eq gnus-fetch-old-headers 'some)
8628             (eq gnus-fetch-old-headers 'invisible)
8629             (numberp gnus-fetch-old-headers)
8630             (eq gnus-build-sparse-threads 'some)
8631             (eq gnus-build-sparse-threads 'more))
8632     (let ((th threads))
8633       (while th
8634         (setcar th (gnus-cut-thread (car th)))
8635         (setq th (cdr th)))))
8636   ;; Remove nixed out threads.
8637   (delq nil threads))
8638
8639 (defun gnus-summary-initial-limit (&optional show-if-empty)
8640   "Figure out what the initial limit is supposed to be on group entry.
8641 This entails weeding out unwanted dormants, low-scored articles,
8642 fetch-old-headers verbiage, and so on."
8643   ;; Most groups have nothing to remove.
8644   (unless (or gnus-inhibit-limiting
8645               (and (null gnus-newsgroup-dormant)
8646                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8647                    (not (eq gnus-fetch-old-headers 'some))
8648                    (not (numberp gnus-fetch-old-headers))
8649                    (not (eq gnus-fetch-old-headers 'invisible))
8650                    (null gnus-summary-expunge-below)
8651                    (not (eq gnus-build-sparse-threads 'some))
8652                    (not (eq gnus-build-sparse-threads 'more))
8653                    (null gnus-thread-expunge-below)))
8654     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8655     (setq gnus-newsgroup-limit nil)
8656     (mapatoms
8657      (lambda (node)
8658        (unless (car (symbol-value node))
8659          ;; These threads have no parents -- they are roots.
8660          (let ((nodes (cdr (symbol-value node)))
8661                thread)
8662            (while nodes
8663              (if (and gnus-thread-expunge-below
8664                       (< (gnus-thread-total-score (car nodes))
8665                          gnus-thread-expunge-below))
8666                  (gnus-expunge-thread (pop nodes))
8667                (setq thread (pop nodes))
8668                (gnus-summary-limit-children thread))))))
8669      gnus-newsgroup-dependencies)
8670     ;; If this limitation resulted in an empty group, we might
8671     ;; pop the previous limit and use it instead.
8672     (when (and (not gnus-newsgroup-limit)
8673                show-if-empty)
8674       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8675     gnus-newsgroup-limit))
8676
8677 (defun gnus-summary-limit-children (thread)
8678   "Return 1 if this subthread is visible and 0 if it is not."
8679   ;; First we get the number of visible children to this thread.  This
8680   ;; is done by recursing down the thread using this function, so this
8681   ;; will really go down to a leaf article first, before slowly
8682   ;; working its way up towards the root.
8683   (when thread
8684     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8685            (children
8686            (if (cdr thread)
8687                (apply '+ (mapcar 'gnus-summary-limit-children
8688                                  (cdr thread)))
8689              0))
8690           (number (mail-header-number (car thread)))
8691           score)
8692       (if (and
8693            (not (memq number gnus-newsgroup-marked))
8694            (or
8695             ;; If this article is dormant and has absolutely no visible
8696             ;; children, then this article isn't visible.
8697             (and (memq number gnus-newsgroup-dormant)
8698                  (zerop children))
8699             ;; If this is "fetch-old-headered" and there is no
8700             ;; visible children, then we don't want this article.
8701             (and (or (eq gnus-fetch-old-headers 'some)
8702                      (numberp gnus-fetch-old-headers))
8703                  (gnus-summary-article-ancient-p number)
8704                  (zerop children))
8705             ;; If this is "fetch-old-headered" and `invisible', then
8706             ;; we don't want this article.
8707             (and (eq gnus-fetch-old-headers 'invisible)
8708                  (gnus-summary-article-ancient-p number))
8709             ;; If this is a sparsely inserted article with no children,
8710             ;; we don't want it.
8711             (and (eq gnus-build-sparse-threads 'some)
8712                  (gnus-summary-article-sparse-p number)
8713                  (zerop children))
8714             ;; If we use expunging, and this article is really
8715             ;; low-scored, then we don't want this article.
8716             (when (and gnus-summary-expunge-below
8717                        (< (setq score
8718                                 (or (cdr (assq number gnus-newsgroup-scored))
8719                                     gnus-summary-default-score))
8720                           gnus-summary-expunge-below))
8721               ;; We increase the expunge-tally here, but that has
8722               ;; nothing to do with the limits, really.
8723               (incf gnus-newsgroup-expunged-tally)
8724               ;; We also mark as read here, if that's wanted.
8725               (when (and gnus-summary-mark-below
8726                          (< score gnus-summary-mark-below))
8727                 (setq gnus-newsgroup-unreads
8728                       (delq number gnus-newsgroup-unreads))
8729                 (if gnus-newsgroup-auto-expire
8730                     (push number gnus-newsgroup-expirable)
8731                   (push (cons number gnus-low-score-mark)
8732                         gnus-newsgroup-reads)))
8733               t)
8734             ;; Do the `display' group parameter.
8735             (and gnus-newsgroup-display
8736                  (not (funcall gnus-newsgroup-display)))))
8737           ;; Nope, invisible article.
8738           0
8739         ;; Ok, this article is to be visible, so we add it to the limit
8740         ;; and return 1.
8741         (push number gnus-newsgroup-limit)
8742         1))))
8743
8744 (defun gnus-expunge-thread (thread)
8745   "Mark all articles in THREAD as read."
8746   (let* ((number (mail-header-number (car thread))))
8747     (incf gnus-newsgroup-expunged-tally)
8748     ;; We also mark as read here, if that's wanted.
8749     (setq gnus-newsgroup-unreads
8750           (delq number gnus-newsgroup-unreads))
8751     (if gnus-newsgroup-auto-expire
8752         (push number gnus-newsgroup-expirable)
8753       (push (cons number gnus-low-score-mark)
8754             gnus-newsgroup-reads)))
8755   ;; Go recursively through all subthreads.
8756   (mapcar 'gnus-expunge-thread (cdr thread)))
8757
8758 ;; Summary article oriented commands
8759
8760 (defun gnus-summary-refer-parent-article (n)
8761   "Refer parent article N times.
8762 If N is negative, go to ancestor -N instead.
8763 The difference between N and the number of articles fetched is returned."
8764   (interactive "p")
8765   (let ((skip 1)
8766         error header ref)
8767     (when (not (natnump n))
8768       (setq skip (abs n)
8769             n 1))
8770     (while (and (> n 0)
8771                 (not error))
8772       (setq header (gnus-summary-article-header))
8773       (if (and (eq (mail-header-number header)
8774                    (cdr gnus-article-current))
8775                (equal gnus-newsgroup-name
8776                       (car gnus-article-current)))
8777           ;; If we try to find the parent of the currently
8778           ;; displayed article, then we take a look at the actual
8779           ;; References header, since this is slightly more
8780           ;; reliable than the References field we got from the
8781           ;; server.
8782           (with-current-buffer gnus-original-article-buffer
8783             (nnheader-narrow-to-headers)
8784             (unless (setq ref (message-fetch-field "references"))
8785               (when (setq ref (message-fetch-field "in-reply-to"))
8786                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8787             (widen))
8788         (setq ref
8789               ;; It's not the current article, so we take a bet on
8790               ;; the value we got from the server.
8791               (mail-header-references header)))
8792       (if (and ref
8793                (not (equal ref "")))
8794           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8795             (gnus-message 1 "Couldn't find parent"))
8796         (gnus-message 1 "No references in article %d"
8797                       (gnus-summary-article-number))
8798         (setq error t))
8799       (decf n))
8800     (gnus-summary-position-point)
8801     n))
8802
8803 (defun gnus-summary-refer-references ()
8804   "Fetch all articles mentioned in the References header.
8805 Return the number of articles fetched."
8806   (interactive)
8807   (let ((ref (mail-header-references (gnus-summary-article-header)))
8808         (current (gnus-summary-article-number))
8809         (n 0))
8810     (if (or (not ref)
8811             (equal ref ""))
8812         (error "No References in the current article")
8813       ;; For each Message-ID in the References header...
8814       (while (string-match "<[^>]*>" ref)
8815         (incf n)
8816         ;; ... fetch that article.
8817         (gnus-summary-refer-article
8818          (prog1 (match-string 0 ref)
8819            (setq ref (substring ref (match-end 0))))))
8820       (gnus-summary-goto-subject current)
8821       (gnus-summary-position-point)
8822       n)))
8823
8824 (defun gnus-summary-refer-thread (&optional limit)
8825   "Fetch all articles in the current thread.
8826 If LIMIT (the numerical prefix), fetch that many old headers instead
8827 of what's specified by the `gnus-refer-thread-limit' variable."
8828   (interactive "P")
8829   (let ((id (mail-header-id (gnus-summary-article-header)))
8830         (limit (if limit (prefix-numeric-value limit)
8831                  gnus-refer-thread-limit)))
8832     (unless (eq gnus-fetch-old-headers 'invisible)
8833       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8834       ;; Retrieve the headers and read them in.
8835       (if (eq (if (numberp limit)
8836                   (gnus-retrieve-headers
8837                    (list (min
8838                           (+ (mail-header-number
8839                               (gnus-summary-article-header))
8840                              limit)
8841                           gnus-newsgroup-end))
8842                    gnus-newsgroup-name (* limit 2))
8843                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8844                 ;; headers.
8845                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8846                                        gnus-newsgroup-name limit))
8847               'nov)
8848           (gnus-build-all-threads)
8849         (error "Can't fetch thread from back ends that don't support NOV"))
8850       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8851     (gnus-summary-limit-include-thread id)))
8852
8853 (defun gnus-summary-refer-article (message-id)
8854   "Fetch an article specified by MESSAGE-ID."
8855   (interactive "sMessage-ID: ")
8856   (when (and (stringp message-id)
8857              (not (zerop (length message-id))))
8858     (setq message-id (gnus-replace-in-string message-id " " ""))
8859     ;; Construct the correct Message-ID if necessary.
8860     ;; Suggested by tale@pawl.rpi.edu.
8861     (unless (string-match "^<" message-id)
8862       (setq message-id (concat "<" message-id)))
8863     (unless (string-match ">$" message-id)
8864       (setq message-id (concat message-id ">")))
8865     ;; People often post MIDs from URLs, so unhex it:
8866     (unless (string-match "@" message-id)
8867       (setq message-id (gnus-url-unhex-string message-id)))
8868     (let* ((header (gnus-id-to-header message-id))
8869            (sparse (and header
8870                         (gnus-summary-article-sparse-p
8871                          (mail-header-number header))
8872                         (memq (mail-header-number header)
8873                               gnus-newsgroup-limit)))
8874            number)
8875       (cond
8876        ;; If the article is present in the buffer we just go to it.
8877        ((and header
8878              (or (not (gnus-summary-article-sparse-p
8879                        (mail-header-number header)))
8880                  sparse))
8881         (prog1
8882             (gnus-summary-goto-article
8883              (mail-header-number header) nil t)
8884           (when sparse
8885             (gnus-summary-update-article (mail-header-number header)))))
8886        (t
8887         ;; We fetch the article.
8888         (catch 'found
8889           (dolist (gnus-override-method (gnus-refer-article-methods))
8890             (when (and (gnus-check-server gnus-override-method)
8891                        ;; Fetch the header,
8892                        (setq number (gnus-summary-insert-subject message-id)))
8893               ;; and display the article.
8894               (gnus-summary-select-article nil nil nil number)
8895               (throw 'found t)))
8896           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8897
8898 (defun gnus-refer-article-methods ()
8899   "Return a list of referable methods."
8900   (cond
8901    ;; No method, so we default to current and native.
8902    ((null gnus-refer-article-method)
8903     (list gnus-current-select-method gnus-select-method))
8904    ;; Current.
8905    ((eq 'current gnus-refer-article-method)
8906     (list gnus-current-select-method))
8907    ;; List of select methods.
8908    ((not (and (symbolp (car gnus-refer-article-method))
8909               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8910     (let (out)
8911       (dolist (method gnus-refer-article-method)
8912         (push (if (eq 'current method)
8913                   gnus-current-select-method
8914                 method)
8915               out))
8916       (nreverse out)))
8917    ;; One single select method.
8918    (t
8919     (list gnus-refer-article-method))))
8920
8921 (defun gnus-summary-edit-parameters ()
8922   "Edit the group parameters of the current group."
8923   (interactive)
8924   (gnus-group-edit-group gnus-newsgroup-name 'params))
8925
8926 (defun gnus-summary-customize-parameters ()
8927   "Customize the group parameters of the current group."
8928   (interactive)
8929   (gnus-group-customize gnus-newsgroup-name))
8930
8931 (defun gnus-summary-enter-digest-group (&optional force)
8932   "Enter an nndoc group based on the current article.
8933 If FORCE, force a digest interpretation.  If not, try
8934 to guess what the document format is."
8935   (interactive "P")
8936   (let ((conf gnus-current-window-configuration))
8937     (save-window-excursion
8938       (save-excursion
8939         (let (gnus-article-prepare-hook
8940               gnus-display-mime-function
8941               gnus-break-pages)
8942           (gnus-summary-select-article))))
8943     (setq gnus-current-window-configuration conf)
8944     (let* ((name (format "%s-%d"
8945                          (gnus-group-prefixed-name
8946                           gnus-newsgroup-name (list 'nndoc ""))
8947                          (with-current-buffer gnus-summary-buffer
8948                            gnus-current-article)))
8949            (ogroup gnus-newsgroup-name)
8950            (params (append (gnus-info-params (gnus-get-info ogroup))
8951                            (list (cons 'to-group ogroup))
8952                            (list (cons 'parent-group ogroup))
8953                            (list (cons 'save-article-group ogroup))))
8954            (case-fold-search t)
8955            (buf (current-buffer))
8956            dig to-address)
8957       (with-current-buffer gnus-original-article-buffer
8958         ;; Have the digest group inherit the main mail address of
8959         ;; the parent article.
8960         (when (setq to-address (or (gnus-fetch-field "reply-to")
8961                                    (gnus-fetch-field "from")))
8962           (setq params
8963                 (append
8964                  (list (cons 'to-address
8965                              (funcall gnus-decode-encoded-address-function
8966                                       to-address))))))
8967         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8968         (insert-buffer-substring gnus-original-article-buffer)
8969         ;; Remove lines that may lead nndoc to misinterpret the
8970         ;; document type.
8971         (narrow-to-region
8972          (goto-char (point-min))
8973          (or (search-forward "\n\n" nil t) (point)))
8974         (goto-char (point-min))
8975         (delete-matching-lines "^Path:\\|^From ")
8976         (widen))
8977       (unwind-protect
8978           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8979                     (gnus-newsgroup-ephemeral-ignored-charsets
8980                      gnus-newsgroup-ignored-charsets))
8981                 (gnus-group-read-ephemeral-group
8982                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8983                               (nndoc-article-type
8984                                ,(if force 'mbox 'guess)))
8985                  t nil nil nil
8986                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8987                                                         "ADAPT")))))
8988               ;; Make all postings to this group go to the parent group.
8989               (nconc (gnus-info-params (gnus-get-info name))
8990                      params)
8991             ;; Couldn't select this doc group.
8992             (switch-to-buffer buf)
8993             (gnus-set-global-variables)
8994             (gnus-configure-windows 'summary)
8995             (gnus-message 3 "Article couldn't be entered?"))
8996         (kill-buffer dig)))))
8997
8998 (defun gnus-summary-read-document (n)
8999   "Open a new group based on the current article(s).
9000 This will allow you to read digests and other similar
9001 documents as newsgroups.
9002 Obeys the standard process/prefix convention."
9003   (interactive "P")
9004   (let* ((ogroup gnus-newsgroup-name)
9005          (params (append (gnus-info-params (gnus-get-info ogroup))
9006                          (list (cons 'to-group ogroup))))
9007          group egroup groups vgroup)
9008     (dolist (article (gnus-summary-work-articles n))
9009       (setq group (format "%s-%d" gnus-newsgroup-name article))
9010       (gnus-summary-remove-process-mark article)
9011       (when (gnus-summary-display-article article)
9012         (save-excursion ;;What for?
9013           (with-temp-buffer
9014             (insert-buffer-substring gnus-original-article-buffer)
9015             ;; Remove some headers that may lead nndoc to make
9016             ;; the wrong guess.
9017             (message-narrow-to-head)
9018             (goto-char (point-min))
9019             (delete-matching-lines "^Path:\\|^From ")
9020             (widen)
9021             (if (setq egroup
9022                       (gnus-group-read-ephemeral-group
9023                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9024                                      (nndoc-article-type guess))
9025                        t nil t))
9026                 (progn
9027                   ;; Make all postings to this group go to the parent group.
9028                   (nconc (gnus-info-params (gnus-get-info egroup))
9029                          params)
9030                   (push egroup groups))
9031               ;; Couldn't select this doc group.
9032               (gnus-error 3 "Article couldn't be entered"))))))
9033     ;; Now we have selected all the documents.
9034     (cond
9035      ((not groups)
9036       (error "None of the articles could be interpreted as documents"))
9037      ((gnus-group-read-ephemeral-group
9038        (setq vgroup (format
9039                      "nnvirtual:%s-%s" gnus-newsgroup-name
9040                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9041        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9042        t
9043        (cons (current-buffer) 'summary)))
9044      (t
9045       (error "Couldn't select virtual nndoc group")))))
9046
9047 (defun gnus-summary-isearch-article (&optional regexp-p)
9048   "Do incremental search forward on the current article.
9049 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9050   (interactive "P")
9051   (gnus-summary-select-article)
9052   (gnus-configure-windows 'article)
9053   (gnus-eval-in-buffer-window gnus-article-buffer
9054     (save-restriction
9055       (widen)
9056       (isearch-forward regexp-p))))
9057
9058 (defun gnus-summary-repeat-search-article-forward ()
9059   "Repeat the previous search forwards."
9060   (interactive)
9061   (unless gnus-last-search-regexp
9062     (error "No previous search"))
9063   (gnus-summary-search-article-forward gnus-last-search-regexp))
9064
9065 (defun gnus-summary-repeat-search-article-backward ()
9066   "Repeat the previous search backwards."
9067   (interactive)
9068   (unless gnus-last-search-regexp
9069     (error "No previous search"))
9070   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9071
9072 (defun gnus-summary-search-article-forward (regexp &optional backward)
9073   "Search for an article containing REGEXP forward.
9074 If BACKWARD, search backward instead."
9075   (interactive
9076    (list (read-string
9077           (format "Search article %s (regexp%s): "
9078                   (if current-prefix-arg "backward" "forward")
9079                   (if gnus-last-search-regexp
9080                       (concat ", default " gnus-last-search-regexp)
9081                     "")))
9082          current-prefix-arg))
9083   (if (string-equal regexp "")
9084       (setq regexp (or gnus-last-search-regexp ""))
9085     (setq gnus-last-search-regexp regexp)
9086     (setq gnus-article-before-search gnus-current-article))
9087   ;; Intentionally set gnus-last-article.
9088   (setq gnus-last-article gnus-article-before-search)
9089   (let ((gnus-last-article gnus-last-article))
9090     (if (gnus-summary-search-article regexp backward)
9091         (gnus-summary-show-thread)
9092       (signal 'search-failed (list regexp)))))
9093
9094 (defun gnus-summary-search-article-backward (regexp)
9095   "Search for an article containing REGEXP backward."
9096   (interactive
9097    (list (read-string
9098           (format "Search article backward (regexp%s): "
9099                   (if gnus-last-search-regexp
9100                       (concat ", default " gnus-last-search-regexp)
9101                     "")))))
9102   (gnus-summary-search-article-forward regexp 'backward))
9103
9104 (defun gnus-summary-search-article (regexp &optional backward)
9105   "Search for an article containing REGEXP.
9106 Optional argument BACKWARD means do search for backward.
9107 `gnus-select-article-hook' is not called during the search."
9108   ;; We have to require this here to make sure that the following
9109   ;; dynamic binding isn't shadowed by autoloading.
9110   (require 'gnus-async)
9111   (require 'gnus-art)
9112   (let ((gnus-select-article-hook nil)  ;Disable hook.
9113         (gnus-article-prepare-hook nil)
9114         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9115         (gnus-use-article-prefetch nil)
9116         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9117         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9118         (gnus-visual nil)
9119         (gnus-keep-backlog nil)
9120         (gnus-break-pages nil)
9121         (gnus-summary-display-arrow nil)
9122         (gnus-updated-mode-lines nil)
9123         (gnus-auto-center-summary nil)
9124         (sum (current-buffer))
9125         (gnus-display-mime-function nil)
9126         (found nil)
9127         point)
9128     (gnus-save-hidden-threads
9129       (gnus-summary-select-article)
9130       (set-buffer gnus-article-buffer)
9131       (goto-char (window-point (get-buffer-window (current-buffer))))
9132       (when backward
9133         (forward-line -1))
9134       (while (not found)
9135         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9136         (if (if backward
9137                 (re-search-backward regexp nil t)
9138               (re-search-forward regexp nil t))
9139             ;; We found the regexp.
9140             (progn
9141               (setq found 'found)
9142               (beginning-of-line)
9143               (set-window-start
9144                (get-buffer-window (current-buffer))
9145                (point))
9146               (forward-line 1)
9147               (set-window-point
9148                (get-buffer-window (current-buffer))
9149                (point))
9150               (set-buffer sum)
9151               (setq point (point)))
9152           ;; We didn't find it, so we go to the next article.
9153           (set-buffer sum)
9154           (setq found 'not)
9155           (while (eq found 'not)
9156             (if (not (if backward (gnus-summary-find-prev)
9157                        (gnus-summary-find-next)))
9158                 ;; No more articles.
9159                 (setq found t)
9160               ;; Select the next article and adjust point.
9161               (unless (gnus-summary-article-sparse-p
9162                        (gnus-summary-article-number))
9163                 (setq found nil)
9164                 (gnus-summary-select-article)
9165                 (set-buffer gnus-article-buffer)
9166                 (widen)
9167                 (goto-char (if backward (point-max) (point-min))))))))
9168       (gnus-message 7 ""))
9169     ;; Return whether we found the regexp.
9170     (when (eq found 'found)
9171       (goto-char point)
9172       (gnus-summary-show-thread)
9173       (gnus-summary-goto-subject gnus-current-article)
9174       (gnus-summary-position-point)
9175       t)))
9176
9177 (defun gnus-find-matching-articles (header regexp)
9178   "Return a list of all articles that match REGEXP on HEADER.
9179 This search includes all articles in the current group that Gnus has
9180 fetched headers for, whether they are displayed or not."
9181   (let ((articles nil)
9182         ;; Can't eta-reduce because it's a macro.
9183         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9184         (case-fold-search t))
9185     (dolist (header gnus-newsgroup-headers)
9186       (when (string-match regexp (funcall func header))
9187         (push (mail-header-number header) articles)))
9188     (nreverse articles)))
9189
9190 (defun gnus-summary-find-matching (header regexp &optional backward unread
9191                                           not-case-fold not-matching)
9192   "Return a list of all articles that match REGEXP on HEADER.
9193 The search stars on the current article and goes forwards unless
9194 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9195 If UNREAD is non-nil, only unread articles will
9196 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9197 in the comparisons. If NOT-MATCHING, return a list of all articles that
9198 not match REGEXP on HEADER."
9199   (let ((case-fold-search (not not-case-fold))
9200         articles d func)
9201     (if (consp header)
9202         (if (eq (car header) 'extra)
9203             (setq func
9204                   `(lambda (h)
9205                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9206                          "")))
9207           (error "%s is an invalid header" header))
9208       (unless (fboundp (intern (concat "mail-header-" header)))
9209         (error "%s is not a valid header" header))
9210       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9211     (dolist (d (if (eq backward 'all)
9212                    gnus-newsgroup-data
9213                  (gnus-data-find-list
9214                   (gnus-summary-article-number)
9215                   (gnus-data-list backward))))
9216       (when (and (or (not unread)       ; We want all articles...
9217                      (gnus-data-unread-p d)) ; Or just unreads.
9218                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9219                  (if not-matching
9220                      (not (string-match
9221                            regexp
9222                            (funcall func (gnus-data-header d))))
9223                    (string-match regexp
9224                                  (funcall func (gnus-data-header d)))))
9225         (push (gnus-data-number d) articles))) ; Success!
9226     (nreverse articles)))
9227
9228 (defun gnus-summary-execute-command (header regexp command &optional backward)
9229   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9230 If HEADER is an empty string (or nil), the match is done on the entire
9231 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9232   (interactive
9233    (list (let ((completion-ignore-case t))
9234            (completing-read
9235             "Header name: "
9236             (mapcar (lambda (header) (list (format "%s" header)))
9237                     (append
9238                      '("Number" "Subject" "From" "Lines" "Date"
9239                        "Message-ID" "Xref" "References" "Body")
9240                      gnus-extra-headers))
9241             nil 'require-match))
9242          (read-string "Regexp: ")
9243          (read-key-sequence "Command: ")
9244          current-prefix-arg))
9245   (when (equal header "Body")
9246     (setq header ""))
9247   ;; Hidden thread subtrees must be searched as well.
9248   (gnus-summary-show-all-threads)
9249   ;; We don't want to change current point nor window configuration.
9250   (save-excursion
9251     (save-window-excursion
9252       (let (gnus-visual
9253             gnus-treat-strip-trailing-blank-lines
9254             gnus-treat-strip-leading-blank-lines
9255             gnus-treat-strip-multiple-blank-lines
9256             gnus-treat-hide-boring-headers
9257             gnus-treat-fold-newsgroups
9258             gnus-article-prepare-hook)
9259         (gnus-message 6 "Executing %s..." (key-description command))
9260         ;; We'd like to execute COMMAND interactively so as to give arguments.
9261         (gnus-execute header regexp
9262                       `(call-interactively ',(key-binding command))
9263                       backward)
9264         (gnus-message 6 "Executing %s...done" (key-description command))))))
9265
9266 (defun gnus-summary-beginning-of-article ()
9267   "Scroll the article back to the beginning."
9268   (interactive)
9269   (gnus-summary-select-article)
9270   (gnus-configure-windows 'article)
9271   (gnus-eval-in-buffer-window gnus-article-buffer
9272     (widen)
9273     (goto-char (point-min))
9274     (when gnus-break-pages
9275       (gnus-narrow-to-page))))
9276
9277 (defun gnus-summary-end-of-article ()
9278   "Scroll to the end of the article."
9279   (interactive)
9280   (gnus-summary-select-article)
9281   (gnus-configure-windows 'article)
9282   (gnus-eval-in-buffer-window gnus-article-buffer
9283     (widen)
9284     (goto-char (point-max))
9285     (recenter -3)
9286     (when gnus-break-pages
9287       (gnus-narrow-to-page))))
9288
9289 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9290   "Truncate to LEN and quote all \"(\"'s in STRING."
9291   (gnus-replace-in-string (if (and len (> (length string) len))
9292                               (substring string 0 len)
9293                             string)
9294                           "[()]" "\\\\\\&"))
9295
9296 (defun gnus-summary-print-article (&optional filename n)
9297   "Generate and print a PostScript image of the process-marked (mail) articles.
9298
9299 If used interactively, print the current article if none are
9300 process-marked.  With prefix arg, prompt the user for the name of the
9301 file to save in.
9302
9303 When used from Lisp, accept two optional args FILENAME and N.  N means
9304 to print the next N articles.  If N is negative, print the N previous
9305 articles.  If N is nil and articles have been marked with the process
9306 mark, print these instead.
9307
9308 If the optional first argument FILENAME is nil, send the image to the
9309 printer.  If FILENAME is a string, save the PostScript image in a file with
9310 that name.  If FILENAME is a number, prompt the user for the name of the file
9311 to save in."
9312   (interactive (list (ps-print-preprint current-prefix-arg)))
9313   (dolist (article (gnus-summary-work-articles n))
9314     (gnus-summary-select-article nil nil 'pseudo article)
9315     (gnus-eval-in-buffer-window gnus-article-buffer
9316       (gnus-print-buffer))
9317     (gnus-summary-remove-process-mark article))
9318   (ps-despool filename))
9319
9320 (defun gnus-print-buffer ()
9321   (let ((buffer (generate-new-buffer " *print*")))
9322     (unwind-protect
9323         (progn
9324           (copy-to-buffer buffer (point-min) (point-max))
9325           (set-buffer buffer)
9326           (gnus-remove-text-with-property 'gnus-decoration)
9327           (when (gnus-visual-p 'article-highlight 'highlight)
9328             ;; Copy-to-buffer doesn't copy overlay.  So redo
9329             ;; highlight.
9330             (let ((gnus-article-buffer buffer))
9331               (gnus-article-highlight-citation t)
9332               (gnus-article-highlight-signature)
9333               (gnus-article-emphasize)
9334               (gnus-article-delete-invisible-text)))
9335           (let ((ps-left-header
9336                  (list
9337                   (concat "("
9338                           (gnus-summary-print-truncate-and-quote
9339                            (mail-header-subject gnus-current-headers)
9340                            66) ")")
9341                   (concat "("
9342                           (gnus-summary-print-truncate-and-quote
9343                            (mail-header-from gnus-current-headers)
9344                            45) ")")))
9345                 (ps-right-header
9346                  (list
9347                   "/pagenumberstring load"
9348                   (concat "("
9349                           (mail-header-date gnus-current-headers) ")"))))
9350             (gnus-run-hooks 'gnus-ps-print-hook)
9351             (save-excursion
9352               (if ps-print-color-p
9353                   (ps-spool-buffer-with-faces)
9354                 (ps-spool-buffer)))))
9355       (kill-buffer buffer))))
9356
9357 (defun gnus-summary-show-complete-article ()
9358   "Show a complete version of the current article.
9359 This is only useful if you're looking at a partial version of the
9360 article currently."
9361   (interactive)
9362   (let ((gnus-keep-backlog nil)
9363         (gnus-use-cache nil)
9364         (gnus-agent nil)
9365         (variable (intern
9366                    (format "%s-fetch-partial-articles"
9367                            (car (gnus-find-method-for-group
9368                                  gnus-newsgroup-name)))
9369                    obarray))
9370         old-val)
9371     (unwind-protect
9372         (progn
9373           (setq old-val (symbol-value variable))
9374           (set variable nil)
9375           (gnus-flush-original-article-buffer)
9376           (gnus-summary-show-article))
9377       (set variable old-val))))
9378
9379 (defun gnus-summary-show-article (&optional arg)
9380   "Force redisplaying of the current article.
9381 If ARG (the prefix) is a number, show the article with the charset
9382 defined in `gnus-summary-show-article-charset-alist', or the charset
9383 input.
9384 If ARG (the prefix) is non-nil and not a number, show the raw article
9385 without any article massaging functions being run.  Normally, the key
9386 strokes are `C-u g'."
9387   (interactive "P")
9388   (cond
9389    ((numberp arg)
9390     (gnus-summary-show-article t)
9391     (let ((gnus-newsgroup-charset
9392            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9393                (mm-read-coding-system
9394                 "View as charset: " ;; actually it is coding system.
9395                 (with-current-buffer gnus-article-buffer
9396                   (mm-detect-coding-region (point) (point-max))))))
9397           (gnus-newsgroup-ignored-charsets 'gnus-all))
9398       (gnus-summary-select-article nil 'force)
9399       (let ((deps gnus-newsgroup-dependencies)
9400             head header lines)
9401         (with-current-buffer gnus-original-article-buffer
9402           (save-restriction
9403             (message-narrow-to-head)
9404             (setq head (buffer-string))
9405             (goto-char (point-min))
9406             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9407               (goto-char (point-max))
9408               (widen)
9409               (setq lines (1- (count-lines (point) (point-max))))))
9410           (with-temp-buffer
9411             (insert (format "211 %d Article retrieved.\n"
9412                             (cdr gnus-article-current)))
9413             (insert head)
9414             (if lines (insert (format "Lines: %d\n" lines)))
9415             (insert ".\n")
9416             (let ((nntp-server-buffer (current-buffer)))
9417               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9418         (gnus-data-set-header
9419          (gnus-data-find (cdr gnus-article-current))
9420          header)
9421         (gnus-summary-update-article-line
9422          (cdr gnus-article-current) header)
9423         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9424           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9425    ((not arg)
9426     ;; Select the article the normal way.
9427     (gnus-summary-select-article nil 'force))
9428    (t
9429     ;; We have to require this here to make sure that the following
9430     ;; dynamic binding isn't shadowed by autoloading.
9431     (require 'gnus-async)
9432     (require 'gnus-art)
9433     ;; Bind the article treatment functions to nil.
9434     (let ((gnus-have-all-headers t)
9435           gnus-article-prepare-hook
9436           gnus-article-decode-hook
9437           gnus-display-mime-function
9438           gnus-break-pages)
9439       ;; Destroy any MIME parts.
9440       (when (gnus-buffer-live-p gnus-article-buffer)
9441         (with-current-buffer gnus-article-buffer
9442           (mm-destroy-parts gnus-article-mime-handles)
9443           ;; Set it to nil for safety reason.
9444           (setq gnus-article-mime-handle-alist nil)
9445           (setq gnus-article-mime-handles nil)))
9446       (gnus-summary-select-article nil 'force))))
9447   (gnus-summary-goto-subject gnus-current-article)
9448   (gnus-summary-position-point))
9449
9450 (defun gnus-summary-show-raw-article ()
9451   "Show the raw article without any article massaging functions being run."
9452   (interactive)
9453   (gnus-summary-show-article t))
9454
9455 (defun gnus-summary-verbose-headers (&optional arg)
9456   "Toggle permanent full header display.
9457 If ARG is a positive number, turn header display on.
9458 If ARG is a negative number, turn header display off."
9459   (interactive "P")
9460   (setq gnus-show-all-headers
9461         (cond ((or (not (numberp arg))
9462                    (zerop arg))
9463                (not gnus-show-all-headers))
9464               ((natnump arg)
9465                t)))
9466   (gnus-summary-show-article))
9467
9468 (defun gnus-summary-toggle-header (&optional arg)
9469   "Show the headers if they are hidden, or hide them if they are shown.
9470 If ARG is a positive number, show the entire header.
9471 If ARG is a negative number, hide the unwanted header lines."
9472   (interactive "P")
9473   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9474                      (get-buffer-window gnus-article-buffer t))))
9475     (with-current-buffer gnus-article-buffer
9476       (widen)
9477       (article-narrow-to-head)
9478       (let* ((inhibit-read-only t)
9479              (inhibit-point-motion-hooks t)
9480              (hidden (if (numberp arg)
9481                          (>= arg 0)
9482                        (or (not (looking-at "[^ \t\n]+:"))
9483                            (gnus-article-hidden-text-p 'headers))))
9484              s e)
9485         (delete-region (point-min) (point-max))
9486         (with-current-buffer gnus-original-article-buffer
9487           (goto-char (setq s (point-min)))
9488           (setq e (if (search-forward "\n\n" nil t)
9489                       (1- (point))
9490                     (point-max))))
9491         (insert-buffer-substring gnus-original-article-buffer s e)
9492         (run-hooks 'gnus-article-decode-hook)
9493         (if hidden
9494             (let ((gnus-treat-hide-headers nil)
9495                   (gnus-treat-hide-boring-headers nil))
9496               (gnus-delete-wash-type 'headers)
9497               (gnus-treat-article 'head))
9498           (gnus-treat-article 'head))
9499         (widen)
9500         (if window
9501             (set-window-start window (goto-char (point-min))))
9502         (if gnus-break-pages
9503             (gnus-narrow-to-page)
9504           (when (gnus-visual-p 'page-marker)
9505             (let ((inhibit-read-only t))
9506               (gnus-remove-text-with-property 'gnus-prev)
9507               (gnus-remove-text-with-property 'gnus-next))))
9508         (gnus-set-mode-line 'article)))))
9509
9510 (defun gnus-summary-show-all-headers ()
9511   "Make all header lines visible."
9512   (interactive)
9513   (gnus-summary-toggle-header 1))
9514
9515 (defun gnus-summary-caesar-message (&optional arg)
9516   "Caesar rotate the current article by 13.
9517 With a non-numerical prefix, also rotate headers.  A numerical
9518 prefix specifies how many places to rotate each letter forward."
9519   (interactive "P")
9520   (gnus-summary-select-article)
9521   (let ((mail-header-separator ""))
9522     (gnus-eval-in-buffer-window gnus-article-buffer
9523       (save-restriction
9524         (widen)
9525         (let ((start (window-start))
9526               (inhibit-read-only t))
9527           (if (equal arg '(4))
9528               (message-caesar-buffer-body nil t)
9529             (message-caesar-buffer-body arg))
9530           (set-window-start (get-buffer-window (current-buffer)) start)))))
9531   ;; Create buttons and stuff...
9532   (gnus-treat-article nil))
9533
9534 (declare-function idna-to-unicode "ext:idna" (str))
9535
9536 (defun gnus-summary-idna-message (&optional arg)
9537   "Decode IDNA encoded domain names in the current articles.
9538 IDNA encoded domain names looks like `xn--bar'.  If a string
9539 remain unencoded after running this function, it is likely an
9540 invalid IDNA string (`xn--bar' is invalid).
9541
9542 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9543 installed for this command to work."
9544   (interactive "P")
9545   (if (not (and (condition-case nil (require 'idna)
9546                   (file-error))
9547                 (mm-coding-system-p 'utf-8)
9548                 (executable-find (symbol-value 'idna-program))))
9549       (gnus-message
9550        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9551     (gnus-summary-select-article)
9552     (let ((mail-header-separator ""))
9553       (gnus-eval-in-buffer-window gnus-article-buffer
9554         (save-restriction
9555           (widen)
9556           (let ((start (window-start))
9557                 buffer-read-only)
9558             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9559               (replace-match (idna-to-unicode (match-string 1))))
9560             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9561
9562 (defun gnus-summary-morse-message (&optional arg)
9563   "Morse decode the current article."
9564   (interactive "P")
9565   (gnus-summary-select-article)
9566   (let ((mail-header-separator ""))
9567     (gnus-eval-in-buffer-window gnus-article-buffer
9568       (save-excursion
9569         (save-restriction
9570           (widen)
9571           (let ((pos (window-start))
9572                 (inhibit-read-only t))
9573             (goto-char (point-min))
9574             (when (message-goto-body)
9575               (gnus-narrow-to-body))
9576             (goto-char (point-min))
9577             (while (search-forward "·" (point-max) t)
9578               (replace-match "."))
9579             (unmorse-region (point-min) (point-max))
9580             (widen)
9581             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9582
9583 (defun gnus-summary-stop-page-breaking ()
9584   "Stop page breaking in the current article."
9585   (interactive)
9586   (gnus-summary-select-article)
9587   (gnus-eval-in-buffer-window gnus-article-buffer
9588     (widen)
9589     (when (gnus-visual-p 'page-marker)
9590       (let ((inhibit-read-only t))
9591         (gnus-remove-text-with-property 'gnus-prev)
9592         (gnus-remove-text-with-property 'gnus-next))
9593       (setq gnus-page-broken nil))))
9594
9595 (defun gnus-summary-move-article (&optional n to-newsgroup
9596                                             select-method action)
9597   "Move the current article to a different newsgroup.
9598 If N is a positive number, move the N next articles.
9599 If N is a negative number, move the N previous articles.
9600 If N is nil and any articles have been marked with the process mark,
9601 move those articles instead.
9602 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9603 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9604 re-spool using this method.
9605
9606 When called interactively with TO-NEWSGROUP being nil, the value of
9607 the variable `gnus-move-split-methods' is used for finding a default
9608 for the target newsgroup.
9609
9610 For this function to work, both the current newsgroup and the
9611 newsgroup that you want to move to have to support the `request-move'
9612 and `request-accept' functions.
9613
9614 ACTION can be either `move' (the default), `crosspost' or `copy'."
9615   (interactive "P")
9616   (unless action
9617     (setq action 'move))
9618   ;; Check whether the source group supports the required functions.
9619   (cond ((and (eq action 'move)
9620               (not (gnus-check-backend-function
9621                     'request-move-article gnus-newsgroup-name)))
9622          (error "The current group does not support article moving"))
9623         ((and (eq action 'crosspost)
9624               (not (gnus-check-backend-function
9625                     'request-replace-article gnus-newsgroup-name)))
9626          (error "The current group does not support article editing")))
9627   (let ((articles (gnus-summary-work-articles n))
9628         (prefix (if (gnus-check-backend-function
9629                      'request-move-article gnus-newsgroup-name)
9630                     (funcall gnus-move-group-prefix-function
9631                              gnus-newsgroup-name)
9632                   ""))
9633         (names '((move "Move" "Moving")
9634                  (copy "Copy" "Copying")
9635                  (crosspost "Crosspost" "Crossposting")))
9636         (copy-buf (save-excursion
9637                     (nnheader-set-temp-buffer " *copy article*")))
9638         art-group to-method new-xref article to-groups
9639         articles-to-update-marks encoded)
9640     (unless (assq action names)
9641       (error "Unknown action %s" action))
9642     ;; Read the newsgroup name.
9643     (when (and (not to-newsgroup)
9644                (not select-method))
9645       (if (and gnus-move-split-methods
9646                (not
9647                 (and (memq gnus-current-article articles)
9648                      (gnus-buffer-live-p gnus-original-article-buffer))))
9649           ;; When `gnus-move-split-methods' is non-nil, we have to
9650           ;; select an article to give `gnus-read-move-group-name' an
9651           ;; opportunity to suggest an appropriate default.  However,
9652           ;; we needn't render or mark the article.
9653           (let ((gnus-display-mime-function nil)
9654                 (gnus-article-prepare-hook nil)
9655                 (gnus-mark-article-hook nil))
9656             (gnus-summary-select-article nil nil nil (car articles))))
9657       (setq to-newsgroup (gnus-read-move-group-name
9658                           (cadr (assq action names))
9659                           (symbol-value
9660                            (intern (format "gnus-current-%s-group" action)))
9661                           articles prefix)
9662             encoded to-newsgroup
9663             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9664       (set (intern (format "gnus-current-%s-group" action))
9665            (mm-decode-coding-string
9666             to-newsgroup
9667             (gnus-group-name-charset to-method to-newsgroup))))
9668     (unless to-method
9669       (setq to-method (or select-method
9670                           (gnus-server-to-method
9671                            (gnus-group-method to-newsgroup)))))
9672     (setq to-newsgroup
9673           (or encoded
9674               (and to-newsgroup
9675                    (mm-encode-coding-string
9676                     to-newsgroup
9677                     (gnus-group-name-charset to-method to-newsgroup)))))
9678     ;; Check the method we are to move this article to...
9679     (unless (gnus-check-backend-function
9680              'request-accept-article (car to-method))
9681       (error "%s does not support article copying" (car to-method)))
9682     (unless (gnus-check-server to-method)
9683       (error "Can't open server %s" (car to-method)))
9684     (gnus-message 6 "%s to %s: %s..."
9685                   (caddr (assq action names))
9686                   (or (car select-method)
9687                       (gnus-group-decoded-name to-newsgroup))
9688                   articles)
9689     (while articles
9690       (setq article (pop articles))
9691       (setq
9692        art-group
9693        (cond
9694         ;; Move the article.
9695         ((eq action 'move)
9696          ;; Remove this article from future suppression.
9697          (gnus-dup-unsuppress-article article)
9698          (let* ((from-method (gnus-find-method-for-group
9699                               gnus-newsgroup-name))
9700                 (to-method (or select-method
9701                                (gnus-find-method-for-group to-newsgroup)))
9702                 (move-is-internal (gnus-server-equal from-method to-method)))
9703            (gnus-request-move-article
9704             article                     ; Article to move
9705             gnus-newsgroup-name         ; From newsgroup
9706             (nth 1 (gnus-find-method-for-group
9707                     gnus-newsgroup-name)) ; Server
9708             (list 'gnus-request-accept-article
9709                   to-newsgroup (list 'quote select-method)
9710                   (not articles) t)     ; Accept form
9711             (not articles)              ; Only save nov last time
9712             (and move-is-internal
9713                  to-newsgroup           ; Not respooling
9714                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9715         ;; Copy the article.
9716         ((eq action 'copy)
9717          (with-current-buffer copy-buf
9718            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9719              (save-restriction
9720                (nnheader-narrow-to-headers)
9721                (dolist (hdr gnus-copy-article-ignored-headers)
9722                  (message-remove-header hdr t)))
9723              (gnus-request-accept-article
9724               to-newsgroup select-method (not articles) t))))
9725         ;; Crosspost the article.
9726         ((eq action 'crosspost)
9727          (let ((xref (message-tokenize-header
9728                       (mail-header-xref (gnus-summary-article-header article))
9729                       " ")))
9730            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9731                                   ":" (number-to-string article)))
9732            (unless xref
9733              (setq xref (list (system-name))))
9734            (setq new-xref
9735                  (concat
9736                   (mapconcat 'identity
9737                              (delete "Xref:" (delete new-xref xref))
9738                              " ")
9739                   " " new-xref))
9740            (with-current-buffer copy-buf
9741              ;; First put the article in the destination group.
9742              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9743              (when (consp (setq art-group
9744                                 (gnus-request-accept-article
9745                                  to-newsgroup select-method (not articles) t)))
9746                (setq new-xref (concat new-xref " " (car art-group)
9747                                       ":"
9748                                       (number-to-string (cdr art-group))))
9749                ;; Now we have the new Xrefs header, so we insert
9750                ;; it and replace the new article.
9751                (nnheader-replace-header "Xref" new-xref)
9752                (gnus-request-replace-article
9753                 (cdr art-group) to-newsgroup (current-buffer) t)
9754                art-group))))))
9755       (cond
9756        ((not art-group)
9757         (gnus-message 1 "Couldn't %s article %s: %s"
9758                       (cadr (assq action names)) article
9759                       (nnheader-get-report (car to-method))))
9760        ((eq art-group 'junk)
9761         (when (eq action 'move)
9762           (gnus-summary-mark-article article gnus-canceled-mark)
9763           (gnus-message 4 "Deleted article %s" article)
9764           ;; run the delete hook
9765           (run-hook-with-args 'gnus-summary-article-delete-hook
9766                               action
9767                               (gnus-data-header
9768                                (assoc article (gnus-data-list nil)))
9769                               gnus-newsgroup-name nil
9770                               select-method)))
9771        (t
9772         (let* ((pto-group (gnus-group-prefixed-name
9773                            (car art-group) to-method))
9774                (info (gnus-get-info pto-group))
9775                (to-group (gnus-info-group info))
9776                to-marks)
9777           ;; Update the group that has been moved to.
9778           (when (and info
9779                      (memq action '(move copy)))
9780             (unless (member to-group to-groups)
9781               (push to-group to-groups))
9782
9783             (unless (memq article gnus-newsgroup-unreads)
9784               (push 'read to-marks)
9785               (gnus-info-set-read
9786                info (gnus-add-to-range (gnus-info-read info)
9787                                        (list (cdr art-group)))))
9788
9789             ;; See whether the article is to be put in the cache.
9790             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9791                    (marks (if expirable
9792                               gnus-article-mark-lists
9793                             (delete '(expirable . expire)
9794                                     (copy-sequence gnus-article-mark-lists))))
9795                    (to-article (cdr art-group)))
9796
9797               ;; Enter the article into the cache in the new group,
9798               ;; if that is required.
9799               (when gnus-use-cache
9800                 (gnus-cache-possibly-enter-article
9801                  to-group to-article
9802                  (memq article gnus-newsgroup-marked)
9803                  (memq article gnus-newsgroup-dormant)
9804                  (memq article gnus-newsgroup-unreads)))
9805
9806               (when gnus-preserve-marks
9807                 ;; Copy any marks over to the new group.
9808                 (when (and (equal to-group gnus-newsgroup-name)
9809                            (not (memq article gnus-newsgroup-unreads)))
9810                   ;; Mark this article as read in this group.
9811                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9812                   ;; Increase the active status of this group.
9813                   (setcdr (gnus-active to-group) to-article))
9814
9815                 (while marks
9816                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9817                     (when (memq article (symbol-value
9818                                          (intern (format "gnus-newsgroup-%s"
9819                                                          (caar marks)))))
9820                       (push (cdar marks) to-marks)
9821                       ;; If the other group is the same as this group,
9822                       ;; then we have to add the mark to the list.
9823                       (when (equal to-group gnus-newsgroup-name)
9824                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9825                              (cons to-article
9826                                    (symbol-value
9827                                     (intern (format "gnus-newsgroup-%s"
9828                                                     (caar marks)))))))
9829                       ;; Copy the marks to other group.
9830                       (gnus-add-marked-articles
9831                        to-group (cdar marks) (list to-article) info)))
9832                   (setq marks (cdr marks)))
9833
9834                 (when (and expirable
9835                            gnus-mark-copied-or-moved-articles-as-expirable
9836                            (not (memq 'expire to-marks)))
9837                   ;; Mark this article as expirable.
9838                   (push 'expire to-marks)
9839                   (when (equal to-group gnus-newsgroup-name)
9840                     (push to-article gnus-newsgroup-expirable))
9841                   ;; Copy the expirable mark to other group.
9842                   (gnus-add-marked-articles
9843                    to-group 'expire (list to-article) info))
9844
9845                 (when to-marks
9846                   (gnus-request-set-mark
9847                    to-group (list (list (list to-article) 'add to-marks)))))
9848
9849               (gnus-dribble-enter
9850                (concat "(gnus-group-set-info '"
9851                        (gnus-prin1-to-string (gnus-get-info to-group))
9852                        ")"))))
9853
9854           ;; Update the Xref header in this article to point to
9855           ;; the new crossposted article we have just created.
9856           (when (eq action 'crosspost)
9857             (with-current-buffer copy-buf
9858               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9859               (nnheader-replace-header "Xref" new-xref)
9860               (gnus-request-replace-article
9861                article gnus-newsgroup-name (current-buffer) t)))
9862
9863           ;; run the move/copy/crosspost/respool hook
9864           (run-hook-with-args 'gnus-summary-article-move-hook
9865                               action
9866                               (gnus-data-header
9867                                (assoc article (gnus-data-list nil)))
9868                               gnus-newsgroup-name
9869                               to-newsgroup
9870                               select-method))
9871
9872         ;;;!!!Why is this necessary?
9873         (set-buffer gnus-summary-buffer)
9874
9875         (when (eq action 'move)
9876           (save-excursion
9877             (gnus-summary-goto-subject article)
9878             (gnus-summary-mark-article article gnus-canceled-mark)))))
9879       (push article articles-to-update-marks))
9880
9881     (save-excursion
9882       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9883     ;; Re-activate all groups that have been moved to.
9884     (with-current-buffer gnus-group-buffer
9885       (let ((gnus-group-marked to-groups))
9886         (gnus-group-get-new-news-this-group nil t)))
9887
9888     (gnus-kill-buffer copy-buf)
9889     (gnus-summary-position-point)
9890     (gnus-set-mode-line 'summary)))
9891
9892 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9893   "Copy the current article to some other group.
9894 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9895 When called interactively, if TO-NEWSGROUP is nil, use the value of
9896 the variable `gnus-move-split-methods' for finding a default target
9897 newsgroup.
9898 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9899 re-spool using this method."
9900   (interactive "P")
9901   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9902
9903 (defun gnus-summary-crosspost-article (&optional n)
9904   "Crosspost the current article to some other group."
9905   (interactive "P")
9906   (gnus-summary-move-article n nil nil 'crosspost))
9907
9908 (defcustom gnus-summary-respool-default-method nil
9909   "Default method type for respooling an article.
9910 If nil, use to the current newsgroup method."
9911   :type 'symbol
9912   :group 'gnus-summary-mail)
9913
9914 (defun gnus-summary-respool-article (&optional n method)
9915   "Respool the current article.
9916 The article will be squeezed through the mail spooling process again,
9917 which means that it will be put in some mail newsgroup or other
9918 depending on `nnmail-split-methods'.
9919 If N is a positive number, respool the N next articles.
9920 If N is a negative number, respool the N previous articles.
9921 If N is nil and any articles have been marked with the process mark,
9922 respool those articles instead.
9923
9924 Respooling can be done both from mail groups and \"real\" newsgroups.
9925 In the former case, the articles in question will be moved from the
9926 current group into whatever groups they are destined to.  In the
9927 latter case, they will be copied into the relevant groups."
9928   (interactive
9929    (list current-prefix-arg
9930          (let* ((methods (gnus-methods-using 'respool))
9931                 (methname
9932                  (symbol-name (or gnus-summary-respool-default-method
9933                                   (car (gnus-find-method-for-group
9934                                         gnus-newsgroup-name)))))
9935                 (method
9936                  (gnus-completing-read-with-default
9937                   methname "Backend to use when respooling"
9938                   methods nil t nil 'gnus-mail-method-history))
9939                 ms)
9940            (cond
9941             ((zerop (length (setq ms (gnus-servers-using-backend
9942                                       (intern method)))))
9943              (list (intern method) ""))
9944             ((= 1 (length ms))
9945              (car ms))
9946             (t
9947              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9948                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9949                            ms-alist))))))))
9950   (unless method
9951     (error "No method given for respooling"))
9952   (if (assoc (symbol-name
9953               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9954              (gnus-methods-using 'respool))
9955       (gnus-summary-move-article n nil method)
9956     (gnus-summary-copy-article n nil method)))
9957
9958 (defun gnus-summary-import-article (file &optional edit)
9959   "Import an arbitrary file into a mail newsgroup."
9960   (interactive "fImport file: \nP")
9961   (let ((group gnus-newsgroup-name)
9962         (now (current-time))
9963         atts lines group-art)
9964     (unless (gnus-check-backend-function 'request-accept-article group)
9965       (error "%s does not support article importing" group))
9966     (or (file-readable-p file)
9967         (not (file-regular-p file))
9968         (error "Can't read %s" file))
9969     (with-current-buffer (gnus-get-buffer-create " *import file*")
9970       (erase-buffer)
9971       (nnheader-insert-file-contents file)
9972       (goto-char (point-min))
9973       (if (nnheader-article-p)
9974           (save-restriction
9975             (goto-char (point-min))
9976             (search-forward "\n\n" nil t)
9977             (narrow-to-region (point-min) (1- (point)))
9978             (goto-char (point-min))
9979             (unless (re-search-forward "^date:" nil t)
9980               (goto-char (point-max))
9981               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9982        ;; This doesn't look like an article, so we fudge some headers.
9983         (setq atts (file-attributes file)
9984               lines (count-lines (point-min) (point-max)))
9985         (insert "From: " (read-string "From: ") "\n"
9986                 "Subject: " (read-string "Subject: ") "\n"
9987                 "Date: " (message-make-date (nth 5 atts)) "\n"
9988                 "Message-ID: " (message-make-message-id) "\n"
9989                 "Lines: " (int-to-string lines) "\n"
9990                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9991       (setq group-art (gnus-request-accept-article group nil t))
9992       (kill-buffer (current-buffer)))
9993     (setq gnus-newsgroup-active (gnus-activate-group group))
9994     (forward-line 1)
9995     (gnus-summary-goto-article (cdr group-art) nil t)
9996     (when edit
9997       (gnus-summary-edit-article))))
9998
9999 (defun gnus-summary-create-article ()
10000   "Create an article in a mail newsgroup."
10001   (interactive)
10002   (let ((group gnus-newsgroup-name)
10003         (now (current-time))
10004         group-art)
10005     (unless (gnus-check-backend-function 'request-accept-article group)
10006       (error "%s does not support article importing" group))
10007     (with-current-buffer (gnus-get-buffer-create " *import file*")
10008       (erase-buffer)
10009       (goto-char (point-min))
10010       ;; This doesn't look like an article, so we fudge some headers.
10011       (insert "From: " (read-string "From: ") "\n"
10012               "Subject: " (read-string "Subject: ") "\n"
10013               "Date: " (message-make-date now) "\n"
10014               "Message-ID: " (message-make-message-id) "\n")
10015       (setq group-art (gnus-request-accept-article group nil t))
10016       (kill-buffer (current-buffer)))
10017     (setq gnus-newsgroup-active (gnus-activate-group group))
10018     (forward-line 1)
10019     (gnus-summary-goto-article (cdr group-art) nil t)
10020     (gnus-summary-edit-article)))
10021
10022 (defun gnus-summary-article-posted-p ()
10023   "Say whether the current (mail) article is available from news as well.
10024 This will be the case if the article has both been mailed and posted."
10025   (interactive)
10026   (let ((id (mail-header-references (gnus-summary-article-header)))
10027         (gnus-override-method (car (gnus-refer-article-methods))))
10028     (if (gnus-request-head id "")
10029         (gnus-message 2 "The current message was found on %s"
10030                       gnus-override-method)
10031       (gnus-message 2 "The current message couldn't be found on %s"
10032                     gnus-override-method)
10033       nil)))
10034
10035 (defun gnus-summary-expire-articles (&optional now)
10036   "Expire all articles that are marked as expirable in the current group."
10037   (interactive)
10038   (when (and (not gnus-group-is-exiting-without-update-p)
10039              (gnus-check-backend-function
10040               'request-expire-articles gnus-newsgroup-name))
10041     ;; This backend supports expiry.
10042     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10043            (expirable (if total
10044                           (progn
10045                             ;; We need to update the info for
10046                             ;; this group for `gnus-list-of-read-articles'
10047                             ;; to give us the right answer.
10048                             (gnus-run-hooks 'gnus-exit-group-hook)
10049                             (gnus-summary-update-info)
10050                             (gnus-list-of-read-articles gnus-newsgroup-name))
10051                         (setq gnus-newsgroup-expirable
10052                               (sort gnus-newsgroup-expirable '<))))
10053            (expiry-wait (if now 'immediate
10054                           (gnus-group-find-parameter
10055                            gnus-newsgroup-name 'expiry-wait)))
10056            (nnmail-expiry-target
10057             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10058                 nnmail-expiry-target))
10059            es)
10060       (when expirable
10061         ;; There are expirable articles in this group, so we run them
10062         ;; through the expiry process.
10063         (gnus-message 6 "Expiring articles...")
10064         (unless (gnus-check-group gnus-newsgroup-name)
10065           (error "Can't open server for %s" gnus-newsgroup-name))
10066         ;; The list of articles that weren't expired is returned.
10067         (save-excursion
10068           (if expiry-wait
10069               (let ((nnmail-expiry-wait-function nil)
10070                     (nnmail-expiry-wait expiry-wait))
10071                 (setq es (gnus-request-expire-articles
10072                           expirable gnus-newsgroup-name)))
10073             (setq es (gnus-request-expire-articles
10074                       expirable gnus-newsgroup-name)))
10075           (unless total
10076             (setq gnus-newsgroup-expirable es))
10077           ;; We go through the old list of expirable, and mark all
10078           ;; really expired articles as nonexistent.
10079           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10080             (let ((gnus-use-cache nil))
10081               (dolist (article expirable)
10082                 (when (and (not (memq article es))
10083                            (gnus-data-find article))
10084                   (gnus-summary-mark-article article gnus-canceled-mark)
10085                   (run-hook-with-args 'gnus-summary-article-expire-hook
10086                                       'delete
10087                                       (gnus-data-header
10088                                        (assoc article (gnus-data-list nil)))
10089                                       gnus-newsgroup-name
10090                                       nil
10091                                       nil))))))
10092         (gnus-message 6 "Expiring articles...done")))))
10093
10094 (defun gnus-summary-expire-articles-now ()
10095   "Expunge all expirable articles in the current group.
10096 This means that *all* articles that are marked as expirable will be
10097 deleted forever, right now."
10098   (interactive)
10099   (or gnus-expert-user
10100       (gnus-yes-or-no-p
10101        "Are you really, really sure you want to delete all expirable messages? ")
10102       (error "Phew!"))
10103   (gnus-summary-expire-articles t))
10104
10105 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10106 (defun gnus-summary-delete-article (&optional n)
10107   "Delete the N next (mail) articles.
10108 This command actually deletes articles.  This is not a marking
10109 command.  The article will disappear forever from your life, never to
10110 return.
10111
10112 If N is negative, delete backwards.
10113 If N is nil and articles have been marked with the process mark,
10114 delete these instead.
10115
10116 If `gnus-novice-user' is non-nil you will be asked for
10117 confirmation before the articles are deleted."
10118   (interactive "P")
10119   (unless (gnus-check-backend-function 'request-expire-articles
10120                                        gnus-newsgroup-name)
10121     (error "The current newsgroup does not support article deletion"))
10122   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10123     (error "Couldn't open server"))
10124   ;; Compute the list of articles to delete.
10125   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10126         (nnmail-expiry-target 'delete)
10127         not-deleted)
10128     (if (and gnus-novice-user
10129              (not (gnus-yes-or-no-p
10130                    (format "Do you really want to delete %s forever? "
10131                            (if (> (length articles) 1)
10132                                (format "these %s articles" (length articles))
10133                              "this article")))))
10134         ()
10135       ;; Delete the articles.
10136       (setq not-deleted (gnus-request-expire-articles
10137                          articles gnus-newsgroup-name 'force))
10138       (save-excursion
10139         (while articles
10140           (gnus-summary-remove-process-mark (car articles))
10141           ;; The backend might not have been able to delete the article
10142           ;; after all.
10143           (unless (memq (car articles) not-deleted)
10144             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10145           (let* ((article (car articles))
10146                  (ghead  (gnus-data-header
10147                           (assoc article (gnus-data-list nil)))))
10148             (run-hook-with-args 'gnus-summary-article-delete-hook
10149                                 'delete ghead gnus-newsgroup-name nil
10150                                 nil))
10151           (setq articles (cdr articles))))
10152       (when not-deleted
10153         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10154     (gnus-summary-position-point)
10155     (gnus-set-mode-line 'summary)
10156     not-deleted))
10157
10158 (defun gnus-summary-edit-article (&optional arg)
10159   "Edit the current article.
10160 This will have permanent effect only in mail groups.
10161 If ARG is nil, edit the decoded articles.
10162 If ARG is 1, edit the raw articles.
10163 If ARG is 2, edit the raw articles even in read-only groups.
10164 If ARG is 3, edit the articles with the current handles.
10165 Otherwise, allow editing of articles even in read-only
10166 groups."
10167   (interactive "P")
10168   (let (force raw current-handles)
10169     (cond
10170      ((null arg))
10171      ((eq arg 1)
10172       (setq raw t))
10173      ((eq arg 2)
10174       (setq raw t
10175             force t))
10176      ((eq arg 3)
10177       (setq current-handles
10178             (and (gnus-buffer-live-p gnus-article-buffer)
10179                  (with-current-buffer gnus-article-buffer
10180                    (prog1
10181                        gnus-article-mime-handles
10182                      (setq gnus-article-mime-handles nil))))))
10183      (t
10184       (setq force t)))
10185     (when (and raw (not force)
10186                (member gnus-newsgroup-name '("nndraft:delayed"
10187                                              "nndraft:drafts"
10188                                              "nndraft:queue")))
10189       (error "Can't edit the raw article in group %s"
10190              gnus-newsgroup-name))
10191     (with-current-buffer gnus-summary-buffer
10192       (let ((mail-parse-charset gnus-newsgroup-charset)
10193             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10194         (gnus-set-global-variables)
10195         (when (and (not force)
10196                    (gnus-group-read-only-p))
10197           (error "The current newsgroup does not support article editing"))
10198         (gnus-summary-show-article t)
10199         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10200           (with-current-buffer gnus-article-buffer
10201             (mm-enable-multibyte)))
10202         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10203             (setq raw t))
10204         (gnus-article-edit-article
10205          (if raw 'ignore
10206            `(lambda ()
10207               (let ((mbl mml-buffer-list))
10208                 (setq mml-buffer-list nil)
10209                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10210                   (mime-to-mml ,'current-handles))
10211                 (let ((mbl1 mml-buffer-list))
10212                   (setq mml-buffer-list mbl)
10213                   (set (make-local-variable 'mml-buffer-list) mbl1))
10214                 (gnus-make-local-hook 'kill-buffer-hook)
10215                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10216          `(lambda (no-highlight)
10217             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10218                   (message-options message-options)
10219                   (message-options-set-recipient)
10220                   (mail-parse-ignored-charsets
10221                    ',gnus-newsgroup-ignored-charsets)
10222                   (rfc2047-header-encoding-alist
10223                    ',(let ((charset (gnus-group-name-charset
10224                                      (gnus-find-method-for-group
10225                                       gnus-newsgroup-name)
10226                                      gnus-newsgroup-name)))
10227                        (append (list (cons "Newsgroups" charset)
10228                                      (cons "Followup-To" charset)
10229                                      (cons "Xref" charset))
10230                                rfc2047-header-encoding-alist))))
10231               ,(if (not raw) '(progn
10232                                 (mml-to-mime)
10233                                 (mml-destroy-buffers)
10234                                 (remove-hook 'kill-buffer-hook
10235                                              'mml-destroy-buffers t)
10236                                 (kill-local-variable 'mml-buffer-list)))
10237               (gnus-summary-edit-article-done
10238                ,(or (mail-header-references gnus-current-headers) "")
10239                ,(gnus-group-read-only-p)
10240                ,gnus-summary-buffer no-highlight))))))))
10241
10242 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10243
10244 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10245                                                  no-highlight)
10246   "Make edits to the current article permanent."
10247   (interactive)
10248   (save-excursion
10249    ;; The buffer restriction contains the entire article if it exists.
10250     (when (article-goto-body)
10251       (let ((lines (count-lines (point) (point-max)))
10252             (length (- (point-max) (point)))
10253             (case-fold-search t)
10254             (body (copy-marker (point))))
10255         (goto-char (point-min))
10256         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10257           (delete-region (match-beginning 1) (match-end 1))
10258           (insert (number-to-string length)))
10259         (goto-char (point-min))
10260         (when (re-search-forward
10261                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10262           (delete-region (match-beginning 1) (match-end 1))
10263           (insert (number-to-string length)))
10264         (goto-char (point-min))
10265         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10266           (delete-region (match-beginning 1) (match-end 1))
10267           (insert (number-to-string lines))))))
10268   ;; Replace the article.
10269   (let ((buf (current-buffer)))
10270     (with-temp-buffer
10271       (insert-buffer-substring buf)
10272
10273       (if (and (not read-only)
10274                (not (gnus-request-replace-article
10275                      (cdr gnus-article-current) (car gnus-article-current)
10276                      (current-buffer) t)))
10277           (error "Couldn't replace article")
10278         ;; Update the summary buffer.
10279         (if (and references
10280                  (equal (message-tokenize-header references " ")
10281                         (message-tokenize-header
10282                          (or (message-fetch-field "references") "") " ")))
10283             ;; We only have to update this line.
10284             (save-excursion
10285               (save-restriction
10286                 (message-narrow-to-head)
10287                 (let ((head (buffer-substring-no-properties
10288                              (point-min) (point-max)))
10289                       header)
10290                   (with-temp-buffer
10291                     (insert (format "211 %d Article retrieved.\n"
10292                                     (cdr gnus-article-current)))
10293                     (insert head)
10294                     (insert ".\n")
10295                     (let ((nntp-server-buffer (current-buffer)))
10296                       (setq header (car (gnus-get-newsgroup-headers
10297                                          nil t))))
10298                     (with-current-buffer gnus-summary-buffer
10299                       (gnus-data-set-header
10300                        (gnus-data-find (cdr gnus-article-current))
10301                        header)
10302                       (gnus-summary-update-article-line
10303                        (cdr gnus-article-current) header)
10304                       (if (gnus-summary-goto-subject
10305                            (cdr gnus-article-current) nil t)
10306                           (gnus-summary-update-secondary-mark
10307                            (cdr gnus-article-current))))))))
10308           ;; Update threads.
10309           (set-buffer (or buffer gnus-summary-buffer))
10310           (gnus-summary-update-article (cdr gnus-article-current))
10311           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10312               (gnus-summary-update-secondary-mark
10313                (cdr gnus-article-current))))
10314         ;; Prettify the article buffer again.
10315         (unless no-highlight
10316           (with-current-buffer gnus-article-buffer
10317             ;;;!!! Fix this -- article should be rehighlighted.
10318             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10319             (set-buffer gnus-original-article-buffer)
10320             (gnus-request-article
10321              (cdr gnus-article-current)
10322              (car gnus-article-current) (current-buffer))))
10323         ;; Prettify the summary buffer line.
10324         (when (gnus-visual-p 'summary-highlight 'highlight)
10325           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10326
10327 (defun gnus-summary-edit-wash (key)
10328   "Perform editing command KEY in the article buffer."
10329   (interactive
10330    (list
10331     (progn
10332       (message "%s" (concat (this-command-keys) "- "))
10333       (read-char))))
10334   (message "")
10335   (gnus-summary-edit-article)
10336   (execute-kbd-macro (concat (this-command-keys) key))
10337   (gnus-article-edit-done))
10338
10339 ;;; Respooling
10340
10341 (defun gnus-summary-respool-query (&optional silent trace)
10342   "Query where the respool algorithm would put this article."
10343   (interactive)
10344   (let (gnus-mark-article-hook)
10345     (gnus-summary-select-article)
10346     (with-current-buffer gnus-original-article-buffer
10347       (let ((groups (nnmail-article-group 'identity trace)))
10348         (unless silent
10349           (if groups
10350               (message "This message would go to %s"
10351                        (mapconcat 'car groups ", "))
10352             (message "This message would go to no groups"))
10353           groups)))))
10354
10355 (defun gnus-summary-respool-trace ()
10356   "Trace where the respool algorithm would put this article.
10357 Display a buffer showing all fancy splitting patterns which matched."
10358   (interactive)
10359   (gnus-summary-respool-query nil t))
10360
10361 ;; Summary marking commands.
10362
10363 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10364   "Mark articles which has the same subject as read, and then select the next.
10365 If UNMARK is positive, remove any kind of mark.
10366 If UNMARK is negative, tick articles."
10367   (interactive "P")
10368   (when unmark
10369     (setq unmark (prefix-numeric-value unmark)))
10370   (let ((count
10371          (gnus-summary-mark-same-subject
10372           (gnus-summary-article-subject) unmark)))
10373     ;; Select next unread article.  If auto-select-same mode, should
10374     ;; select the first unread article.
10375     (gnus-summary-next-article t (and gnus-auto-select-same
10376                                       (gnus-summary-article-subject)))
10377     (gnus-message 7 "%d article%s marked as %s"
10378                   count (if (= count 1) " is" "s are")
10379                   (if unmark "unread" "read"))))
10380
10381 (defun gnus-summary-kill-same-subject (&optional unmark)
10382   "Mark articles which has the same subject as read.
10383 If UNMARK is positive, remove any kind of mark.
10384 If UNMARK is negative, tick articles."
10385   (interactive "P")
10386   (when unmark
10387     (setq unmark (prefix-numeric-value unmark)))
10388   (let ((count
10389          (gnus-summary-mark-same-subject
10390           (gnus-summary-article-subject) unmark)))
10391     ;; If marked as read, go to next unread subject.
10392     (when (null unmark)
10393       ;; Go to next unread subject.
10394       (gnus-summary-next-subject 1 t))
10395     (gnus-message 7 "%d articles are marked as %s"
10396                   count (if unmark "unread" "read"))))
10397
10398 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10399   "Mark articles with same SUBJECT as read, and return marked number.
10400 If optional argument UNMARK is positive, remove any kinds of marks.
10401 If optional argument UNMARK is negative, mark articles as unread instead."
10402   (let ((count 1))
10403     (save-excursion
10404       (cond
10405        ((null unmark)                   ; Mark as read.
10406         (while (and
10407                 (progn
10408                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10409                   (gnus-summary-show-thread) t)
10410                 (gnus-summary-find-subject subject))
10411           (setq count (1+ count))))
10412        ((> unmark 0)                    ; Tick.
10413         (while (and
10414                 (progn
10415                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10416                   (gnus-summary-show-thread) t)
10417                 (gnus-summary-find-subject subject))
10418           (setq count (1+ count))))
10419        (t                               ; Mark as unread.
10420         (while (and
10421                 (progn
10422                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10423                   (gnus-summary-show-thread) t)
10424                 (gnus-summary-find-subject subject))
10425           (setq count (1+ count)))))
10426       (gnus-set-mode-line 'summary)
10427       ;; Return the number of marked articles.
10428       count)))
10429
10430 (defun gnus-summary-mark-as-processable (n &optional unmark)
10431   "Set the process mark on the next N articles.
10432 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10433 the process mark instead.  The difference between N and the actual
10434 number of articles marked is returned."
10435   (interactive "P")
10436   (if (and (null n) (gnus-region-active-p))
10437       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10438     (setq n (prefix-numeric-value n))
10439     (let ((backward (< n 0))
10440           (n (abs n)))
10441       (while (and
10442               (> n 0)
10443               (if unmark
10444                   (gnus-summary-remove-process-mark
10445                    (gnus-summary-article-number))
10446                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10447               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10448         (setq n (1- n)))
10449       (when (/= 0 n)
10450         (gnus-message 7 "No more articles"))
10451       (gnus-summary-recenter)
10452       (gnus-summary-position-point)
10453       n)))
10454
10455 (defun gnus-summary-unmark-as-processable (n)
10456   "Remove the process mark from the next N articles.
10457 If N is negative, unmark backward instead.  The difference between N and
10458 the actual number of articles unmarked is returned."
10459   (interactive "P")
10460   (gnus-summary-mark-as-processable n t))
10461
10462 (defun gnus-summary-unmark-all-processable ()
10463   "Remove the process mark from all articles."
10464   (interactive)
10465   (save-excursion
10466     (while gnus-newsgroup-processable
10467       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10468   (gnus-summary-position-point))
10469
10470 (defun gnus-summary-add-mark (article type)
10471   "Mark ARTICLE with a mark of TYPE."
10472   (let ((vtype (car (assq type gnus-article-mark-lists)))
10473         var)
10474     (if (not vtype)
10475         (error "No such mark type: %s" type)
10476       (setq var (intern (format "gnus-newsgroup-%s" type)))
10477       (set var (cons article (symbol-value var)))
10478       (if (memq type '(processable cached replied forwarded recent saved))
10479           (gnus-summary-update-secondary-mark article)
10480         ;;; !!! This is bogus.  We should find out what primary
10481         ;;; !!! mark we want to set.
10482         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10483
10484 (defun gnus-summary-mark-as-expirable (n)
10485   "Mark N articles forward as expirable.
10486 If N is negative, mark backward instead.  The difference between N and
10487 the actual number of articles marked is returned."
10488   (interactive "p")
10489   (gnus-summary-mark-forward n gnus-expirable-mark))
10490
10491 (defun gnus-summary-mark-as-spam (n)
10492   "Mark N articles forward as spam.
10493 If N is negative, mark backward instead.  The difference between N and
10494 the actual number of articles marked is returned."
10495   (interactive "p")
10496   (gnus-summary-mark-forward n gnus-spam-mark))
10497
10498 (defun gnus-summary-mark-article-as-replied (article)
10499   "Mark ARTICLE as replied to and update the summary line.
10500 ARTICLE can also be a list of articles."
10501   (interactive (list (gnus-summary-article-number)))
10502   (let ((articles (if (listp article) article (list article))))
10503     (dolist (article articles)
10504       (unless (numberp article)
10505         (error "%s is not a number" article))
10506       (push article gnus-newsgroup-replied)
10507       (let ((inhibit-read-only t))
10508         (when (gnus-summary-goto-subject article nil t)
10509           (gnus-summary-update-secondary-mark article))))))
10510
10511 (defun gnus-summary-mark-article-as-forwarded (article)
10512   "Mark ARTICLE as forwarded and update the summary line.
10513 ARTICLE can also be a list of articles."
10514   (let ((articles (if (listp article) article (list article))))
10515     (dolist (article articles)
10516       (push article gnus-newsgroup-forwarded)
10517       (let ((inhibit-read-only t))
10518         (when (gnus-summary-goto-subject article nil t)
10519           (gnus-summary-update-secondary-mark article))))))
10520
10521 (defun gnus-summary-set-bookmark (article)
10522   "Set a bookmark in current article."
10523   (interactive (list (gnus-summary-article-number)))
10524   (when (or (not (get-buffer gnus-article-buffer))
10525             (not gnus-current-article)
10526             (not gnus-article-current)
10527             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10528     (error "No current article selected"))
10529   ;; Remove old bookmark, if one exists.
10530   (gnus-pull article gnus-newsgroup-bookmarks)
10531   ;; Set the new bookmark, which is on the form
10532   ;; (article-number . line-number-in-body).
10533   (push
10534    (cons article
10535          (with-current-buffer gnus-article-buffer
10536            (count-lines
10537             (min (point)
10538                  (save-excursion
10539                    (article-goto-body)
10540                    (point)))
10541             (point))))
10542    gnus-newsgroup-bookmarks)
10543   (gnus-message 6 "A bookmark has been added to the current article."))
10544
10545 (defun gnus-summary-remove-bookmark (article)
10546   "Remove the bookmark from the current article."
10547   (interactive (list (gnus-summary-article-number)))
10548   ;; Remove old bookmark, if one exists.
10549   (if (not (assq article gnus-newsgroup-bookmarks))
10550       (gnus-message 6 "No bookmark in current article.")
10551     (gnus-pull article gnus-newsgroup-bookmarks)
10552     (gnus-message 6 "Removed bookmark.")))
10553
10554 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10555 (defun gnus-summary-mark-as-dormant (n)
10556   "Mark N articles forward as dormant.
10557 If N is negative, mark backward instead.  The difference between N and
10558 the actual number of articles marked is returned."
10559   (interactive "p")
10560   (gnus-summary-mark-forward n gnus-dormant-mark))
10561
10562 (defun gnus-summary-set-process-mark (article)
10563   "Set the process mark on ARTICLE and update the summary line."
10564   (setq gnus-newsgroup-processable
10565         (cons article
10566               (delq article gnus-newsgroup-processable)))
10567   (when (gnus-summary-goto-subject article)
10568     (gnus-summary-show-thread)
10569     (gnus-summary-goto-subject article)
10570     (gnus-summary-update-secondary-mark article)))
10571
10572 (defun gnus-summary-remove-process-mark (&rest articles)
10573   "Remove the process mark from ARTICLES and update the summary line."
10574   (dolist (article articles)
10575     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10576     (when (gnus-summary-goto-subject article)
10577       (gnus-summary-show-thread)
10578       (gnus-summary-goto-subject article)
10579       (gnus-summary-update-secondary-mark article)))
10580   t)
10581
10582 (defun gnus-summary-set-saved-mark (article)
10583   "Set the process mark on ARTICLE and update the summary line."
10584   (push article gnus-newsgroup-saved)
10585   (when (gnus-summary-goto-subject article)
10586     (gnus-summary-update-secondary-mark article)))
10587
10588 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10589   "Mark N articles as read forwards.
10590 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10591 The difference between N and the actual number of articles marked is
10592 returned.
10593 If NO-EXPIRE, auto-expiry will be inhibited."
10594   (interactive "p")
10595   (gnus-summary-show-thread)
10596   (let ((backward (< n 0))
10597         (gnus-summary-goto-unread
10598          (and gnus-summary-goto-unread
10599               (not (eq gnus-summary-goto-unread 'never))
10600               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10601                                     gnus-ticked-mark gnus-dormant-mark)))))
10602         (n (abs n))
10603         (mark (or mark gnus-del-mark)))
10604     (while (and (> n 0)
10605                 (gnus-summary-mark-article nil mark no-expire)
10606                 (zerop (gnus-summary-next-subject
10607                         (if backward -1 1)
10608                         (and gnus-summary-goto-unread
10609                              (not (eq gnus-summary-goto-unread 'never)))
10610                         t)))
10611       (setq n (1- n)))
10612     (when (/= 0 n)
10613       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10614     (gnus-summary-recenter)
10615     (gnus-summary-position-point)
10616     (gnus-set-mode-line 'summary)
10617     n))
10618
10619 (defun gnus-summary-mark-article-as-read (mark)
10620   "Mark the current article quickly as read with MARK."
10621   (let ((article (gnus-summary-article-number)))
10622     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10623     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10624     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10625     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10626     (push (cons article mark) gnus-newsgroup-reads)
10627     ;; Possibly remove from cache, if that is used.
10628     (when gnus-use-cache
10629       (gnus-cache-enter-remove-article article))
10630     ;; Allow the backend to change the mark.
10631     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10632     ;; Check for auto-expiry.
10633     (when (and gnus-newsgroup-auto-expire
10634                (memq mark gnus-auto-expirable-marks))
10635       (setq mark gnus-expirable-mark)
10636       ;; Let the backend know about the mark change.
10637       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10638       (push article gnus-newsgroup-expirable))
10639     ;; Set the mark in the buffer.
10640     (gnus-summary-update-mark mark 'unread)
10641     t))
10642
10643 (defun gnus-summary-mark-article-as-unread (mark)
10644   "Mark the current article quickly as unread with MARK."
10645   (let* ((article (gnus-summary-article-number))
10646          (old-mark (gnus-summary-article-mark article)))
10647     ;; Allow the backend to change the mark.
10648     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10649     (if (eq mark old-mark)
10650         t
10651       (if (<= article 0)
10652           (progn
10653             (gnus-error 1 "Can't mark negative article numbers")
10654             nil)
10655         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10656         (setq gnus-newsgroup-spam-marked
10657               (delq article gnus-newsgroup-spam-marked))
10658         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10659         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10660         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10661         (cond ((= mark gnus-ticked-mark)
10662                (setq gnus-newsgroup-marked
10663                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10664                                               article)))
10665               ((= mark gnus-spam-mark)
10666                (setq gnus-newsgroup-spam-marked
10667                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10668                                               article)))
10669               ((= mark gnus-dormant-mark)
10670                (setq gnus-newsgroup-dormant
10671                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10672                                               article)))
10673               (t
10674                (setq gnus-newsgroup-unreads
10675                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10676                                               article))))
10677         (gnus-pull article gnus-newsgroup-reads)
10678
10679         ;; See whether the article is to be put in the cache.
10680         (and gnus-use-cache
10681              (vectorp (gnus-summary-article-header article))
10682              (save-excursion
10683                (gnus-cache-possibly-enter-article
10684                 gnus-newsgroup-name article
10685                 (= mark gnus-ticked-mark)
10686                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10687
10688         ;; Fix the mark.
10689         (gnus-summary-update-mark mark 'unread)
10690         t))))
10691
10692 (defun gnus-summary-mark-article (&optional article mark no-expire)
10693   "Mark ARTICLE with MARK.  MARK can be any character.
10694 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10695 `??' (dormant) and `?E' (expirable).
10696 If MARK is nil, then the default character `?r' is used.
10697 If ARTICLE is nil, then the article on the current line will be
10698 marked.
10699 If NO-EXPIRE, auto-expiry will be inhibited."
10700   ;; The mark might be a string.
10701   (when (stringp mark)
10702     (setq mark (aref mark 0)))
10703   ;; If no mark is given, then we check auto-expiring.
10704   (when (null mark)
10705     (setq mark gnus-del-mark))
10706   (when (and (not no-expire)
10707              gnus-newsgroup-auto-expire
10708              (memq mark gnus-auto-expirable-marks))
10709     (setq mark gnus-expirable-mark))
10710   (let ((article (or article (gnus-summary-article-number)))
10711         (old-mark (gnus-summary-article-mark article)))
10712     ;; Allow the backend to change the mark.
10713     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10714     (if (eq mark old-mark)
10715         t
10716       (unless article
10717         (error "No article on current line"))
10718       (if (not (if (or (= mark gnus-unread-mark)
10719                        (= mark gnus-ticked-mark)
10720                        (= mark gnus-spam-mark)
10721                        (= mark gnus-dormant-mark))
10722                    (gnus-mark-article-as-unread article mark)
10723                  (gnus-mark-article-as-read article mark)))
10724           t
10725         ;; See whether the article is to be put in the cache.
10726         (and gnus-use-cache
10727              (not (= mark gnus-canceled-mark))
10728              (vectorp (gnus-summary-article-header article))
10729              (save-excursion
10730                (gnus-cache-possibly-enter-article
10731                 gnus-newsgroup-name article
10732                 (= mark gnus-ticked-mark)
10733                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10734
10735         (when (gnus-summary-goto-subject article nil t)
10736           (let ((inhibit-read-only t))
10737             (gnus-summary-show-thread)
10738             ;; Fix the mark.
10739             (gnus-summary-update-mark mark 'unread)
10740             t))))))
10741
10742 (defun gnus-summary-update-secondary-mark (article)
10743   "Update the secondary (read, process, cache) mark."
10744   (gnus-summary-update-mark
10745    (cond ((memq article gnus-newsgroup-processable)
10746           gnus-process-mark)
10747          ((memq article gnus-newsgroup-cached)
10748           gnus-cached-mark)
10749          ((memq article gnus-newsgroup-replied)
10750           gnus-replied-mark)
10751          ((memq article gnus-newsgroup-forwarded)
10752           gnus-forwarded-mark)
10753          ((memq article gnus-newsgroup-saved)
10754           gnus-saved-mark)
10755          ((memq article gnus-newsgroup-recent)
10756           gnus-recent-mark)
10757          ((memq article gnus-newsgroup-unseen)
10758           gnus-unseen-mark)
10759          (t gnus-no-mark))
10760    'replied)
10761   (when (gnus-visual-p 'summary-highlight 'highlight)
10762     (gnus-summary-highlight-line)
10763     (gnus-run-hooks 'gnus-summary-update-hook))
10764   t)
10765
10766 (defun gnus-summary-update-download-mark (article)
10767   "Update the download mark."
10768   (gnus-summary-update-mark
10769    (cond ((memq article gnus-newsgroup-undownloaded)
10770           gnus-undownloaded-mark)
10771          (gnus-newsgroup-agentized
10772           gnus-downloaded-mark)
10773          (t
10774           gnus-no-mark))
10775    'download)
10776   (gnus-summary-update-line t)
10777   t)
10778
10779 (defun gnus-summary-update-mark (mark type)
10780   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10781         (inhibit-read-only t))
10782     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10783     (when forward
10784       (when (looking-at "\r")
10785         (incf forward))
10786       (when (<= (+ forward (point)) (point-max))
10787         ;; Go to the right position on the line.
10788         (goto-char (+ forward (point)))
10789         ;; Replace the old mark with the new mark.
10790         (let ((to-insert
10791                (mm-subst-char-in-string
10792                 (char-after) mark
10793                 (buffer-substring (point) (1+ (point))))))
10794           (delete-region (point) (1+ (point)))
10795           (insert to-insert))
10796         ;; Optionally update the marks by some user rule.
10797         (when (eq type 'unread)
10798           (gnus-data-set-mark
10799            (gnus-data-find (gnus-summary-article-number)) mark)
10800           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10801
10802 (defun gnus-mark-article-as-read (article &optional mark)
10803   "Enter ARTICLE in the pertinent lists and remove it from others."
10804   ;; Make the article expirable.
10805   (let ((mark (or mark gnus-del-mark)))
10806     (setq gnus-newsgroup-expirable
10807           (if (= mark gnus-expirable-mark)
10808               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10809             (delq article gnus-newsgroup-expirable)))
10810     ;; Remove from unread and marked lists.
10811     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10812     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10813     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10814     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10815     (push (cons article mark) gnus-newsgroup-reads)
10816     ;; Possibly remove from cache, if that is used.
10817     (when gnus-use-cache
10818       (gnus-cache-enter-remove-article article))
10819     t))
10820
10821 (defun gnus-mark-article-as-unread (article &optional mark)
10822   "Enter ARTICLE in the pertinent lists and remove it from others."
10823   (let ((mark (or mark gnus-ticked-mark)))
10824     (if (<= article 0)
10825         (progn
10826           (gnus-error 1 "Can't mark negative article numbers")
10827           nil)
10828       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10829             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10830             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10831             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10832             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10833
10834       ;; Unsuppress duplicates?
10835       (when gnus-suppress-duplicates
10836         (gnus-dup-unsuppress-article article))
10837
10838       (cond ((= mark gnus-ticked-mark)
10839              (setq gnus-newsgroup-marked
10840                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10841             ((= mark gnus-spam-mark)
10842              (setq gnus-newsgroup-spam-marked
10843                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10844                                             article)))
10845             ((= mark gnus-dormant-mark)
10846              (setq gnus-newsgroup-dormant
10847                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10848             (t
10849              (setq gnus-newsgroup-unreads
10850                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10851       (gnus-pull article gnus-newsgroup-reads)
10852       t)))
10853
10854 (defalias 'gnus-summary-mark-as-unread-forward
10855   'gnus-summary-tick-article-forward)
10856 (make-obsolete 'gnus-summary-mark-as-unread-forward
10857                'gnus-summary-tick-article-forward "Emacs 20.4")
10858 (defun gnus-summary-tick-article-forward (n)
10859   "Tick N articles forwards.
10860 If N is negative, tick backwards instead.
10861 The difference between N and the number of articles ticked is returned."
10862   (interactive "p")
10863   (gnus-summary-mark-forward n gnus-ticked-mark))
10864
10865 (defalias 'gnus-summary-mark-as-unread-backward
10866   'gnus-summary-tick-article-backward)
10867 (make-obsolete 'gnus-summary-mark-as-unread-backward
10868                'gnus-summary-tick-article-backward "Emacs 20.4")
10869 (defun gnus-summary-tick-article-backward (n)
10870   "Tick N articles backwards.
10871 The difference between N and the number of articles ticked is returned."
10872   (interactive "p")
10873   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10874
10875 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10876 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10877 (defun gnus-summary-tick-article (&optional article clear-mark)
10878   "Mark current article as unread.
10879 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10880 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10881   (interactive)
10882   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10883                                        gnus-ticked-mark)))
10884
10885 (defun gnus-summary-mark-as-read-forward (n)
10886   "Mark N articles as read forwards.
10887 If N is negative, mark backwards instead.
10888 The difference between N and the actual number of articles marked is
10889 returned."
10890   (interactive "p")
10891   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10892
10893 (defun gnus-summary-mark-as-read-backward (n)
10894   "Mark the N articles as read backwards.
10895 The difference between N and the actual number of articles marked is
10896 returned."
10897   (interactive "p")
10898   (gnus-summary-mark-forward
10899    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10900
10901 (defun gnus-summary-mark-as-read (&optional article mark)
10902   "Mark current article as read.
10903 ARTICLE specifies the article to be marked as read.
10904 MARK specifies a string to be inserted at the beginning of the line."
10905   (gnus-summary-mark-article article mark))
10906
10907 (defun gnus-summary-clear-mark-forward (n)
10908   "Clear marks from N articles forward.
10909 If N is negative, clear backward instead.
10910 The difference between N and the number of marks cleared is returned."
10911   (interactive "p")
10912   (gnus-summary-mark-forward n gnus-unread-mark))
10913
10914 (defun gnus-summary-clear-mark-backward (n)
10915   "Clear marks from N articles backward.
10916 The difference between N and the number of marks cleared is returned."
10917   (interactive "p")
10918   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10919
10920 (defun gnus-summary-mark-unread-as-read ()
10921   "Intended to be used by `gnus-mark-article-hook'."
10922   (when (memq gnus-current-article gnus-newsgroup-unreads)
10923     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10924
10925 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10926   "Intended to be used by `gnus-mark-article-hook'."
10927   (let ((mark (gnus-summary-article-mark)))
10928     (when (or (gnus-unread-mark-p mark)
10929               (gnus-read-mark-p mark))
10930       (gnus-summary-mark-article gnus-current-article
10931                                  (or new-mark gnus-read-mark)))))
10932
10933 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10934   "Intended to be used by `gnus-mark-article-hook'."
10935   (let ((mark (gnus-summary-article-mark)))
10936     (when (or (gnus-unread-mark-p mark)
10937               (gnus-read-mark-p mark))
10938       (gnus-summary-mark-article (gnus-summary-article-number)
10939                                  (or new-mark gnus-read-mark)))))
10940
10941 (defun gnus-summary-mark-unread-as-ticked ()
10942   "Intended to be used by `gnus-mark-article-hook'."
10943   (when (memq gnus-current-article gnus-newsgroup-unreads)
10944     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10945
10946 (defun gnus-summary-mark-region-as-read (point mark all)
10947   "Mark all unread articles between point and mark as read.
10948 If given a prefix, mark all articles between point and mark as read,
10949 even ticked and dormant ones."
10950   (interactive "r\nP")
10951   (save-excursion
10952     (let (article)
10953       (goto-char point)
10954       (beginning-of-line)
10955       (while (and
10956               (< (point) mark)
10957               (progn
10958                 (when (or all
10959                           (memq (setq article (gnus-summary-article-number))
10960                                 gnus-newsgroup-unreads))
10961                   (gnus-summary-mark-article article gnus-del-mark))
10962                 t)
10963               (gnus-summary-find-next))))))
10964
10965 (defun gnus-summary-mark-below (score mark)
10966   "Mark articles with score less than SCORE with MARK."
10967   (interactive "P\ncMark: ")
10968   (setq score (if score
10969                   (prefix-numeric-value score)
10970                 (or gnus-summary-default-score 0)))
10971   (with-current-buffer gnus-summary-buffer
10972     (goto-char (point-min))
10973     (while
10974         (progn
10975           (and (< (gnus-summary-article-score) score)
10976                (gnus-summary-mark-article nil mark))
10977           (gnus-summary-find-next)))))
10978
10979 (defun gnus-summary-kill-below (&optional score)
10980   "Mark articles with score below SCORE as read."
10981   (interactive "P")
10982   (gnus-summary-mark-below score gnus-killed-mark))
10983
10984 (defun gnus-summary-clear-above (&optional score)
10985   "Clear all marks from articles with score above SCORE."
10986   (interactive "P")
10987   (gnus-summary-mark-above score gnus-unread-mark))
10988
10989 (defun gnus-summary-tick-above (&optional score)
10990   "Tick all articles with score above SCORE."
10991   (interactive "P")
10992   (gnus-summary-mark-above score gnus-ticked-mark))
10993
10994 (defun gnus-summary-mark-above (score mark)
10995   "Mark articles with score over SCORE with MARK."
10996   (interactive "P\ncMark: ")
10997   (setq score (if score
10998                   (prefix-numeric-value score)
10999                 (or gnus-summary-default-score 0)))
11000   (with-current-buffer gnus-summary-buffer
11001     (goto-char (point-min))
11002     (while (and (progn
11003                   (when (> (gnus-summary-article-score) score)
11004                     (gnus-summary-mark-article nil mark))
11005                   t)
11006                 (gnus-summary-find-next)))))
11007
11008 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11009 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11010 (defun gnus-summary-limit-include-expunged (&optional no-error)
11011   "Display all the hidden articles that were expunged for low scores."
11012   (interactive)
11013   (let ((inhibit-read-only t))
11014     (let ((scored gnus-newsgroup-scored)
11015           headers h)
11016       (while scored
11017         (unless (gnus-summary-article-header (caar scored))
11018           (and (setq h (gnus-number-to-header (caar scored)))
11019                (< (cdar scored) gnus-summary-expunge-below)
11020                (push h headers)))
11021         (setq scored (cdr scored)))
11022       (if (not headers)
11023           (when (not no-error)
11024             (error "No expunged articles hidden"))
11025         (goto-char (point-min))
11026         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11027         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11028         (dolist (x headers)
11029           (push (mail-header-number x) gnus-newsgroup-limit))
11030         (gnus-summary-prepare-unthreaded (nreverse headers))
11031         (goto-char (point-min))
11032         (gnus-summary-position-point)
11033         t))))
11034
11035 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11036   "Mark all unread articles in this newsgroup as read.
11037 If prefix argument ALL is non-nil, ticked and dormant articles will
11038 also be marked as read.
11039 If QUIETLY is non-nil, no questions will be asked.
11040
11041 If TO-HERE is non-nil, it should be a point in the buffer.  All
11042 articles before (after, if REVERSE is set) this point will be marked
11043 as read.
11044
11045 Note that this function will only catch up the unread article
11046 in the current summary buffer limitation.
11047
11048 The number of articles marked as read is returned."
11049   (interactive "P")
11050   (prog1
11051       (save-excursion
11052         (when (or quietly
11053                   (not gnus-interactive-catchup) ;Without confirmation?
11054                   gnus-expert-user
11055                   (gnus-y-or-n-p
11056                    (if all
11057                        "Mark absolutely all articles as read? "
11058                      "Mark all unread articles as read? ")))
11059           (if (and not-mark
11060                    (not gnus-newsgroup-adaptive)
11061                    (not gnus-newsgroup-auto-expire)
11062                    (not gnus-suppress-duplicates)
11063                    (or (not gnus-use-cache)
11064                        (eq gnus-use-cache 'passive)))
11065               (progn
11066                 (when all
11067                   (setq gnus-newsgroup-marked nil
11068                         gnus-newsgroup-spam-marked nil
11069                         gnus-newsgroup-dormant nil))
11070                 (setq gnus-newsgroup-unreads
11071                       (gnus-sorted-nunion
11072                        (gnus-sorted-intersection gnus-newsgroup-unreads
11073                                                  gnus-newsgroup-downloadable)
11074                        (gnus-sorted-difference gnus-newsgroup-unfetched
11075                                                gnus-newsgroup-cached))))
11076             ;; We actually mark all articles as canceled, which we
11077             ;; have to do when using auto-expiry or adaptive scoring.
11078             (gnus-summary-show-all-threads)
11079             (if (and to-here reverse)
11080                 (progn
11081                   (goto-char to-here)
11082                   (gnus-summary-mark-current-read-and-unread-as-read
11083                    gnus-catchup-mark)
11084                   (while (gnus-summary-find-next (not all))
11085                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11086               (when (gnus-summary-first-subject (not all))
11087                 (while (and
11088                         (if to-here (< (point) to-here) t)
11089                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11090                         (gnus-summary-find-next (not all))))))
11091             (gnus-set-mode-line 'summary))
11092           t))
11093     (gnus-summary-position-point)))
11094
11095 (defun gnus-summary-catchup-to-here (&optional all)
11096   "Mark all unticked articles before the current one as read.
11097 If ALL is non-nil, also mark ticked and dormant articles as read."
11098   (interactive "P")
11099   (save-excursion
11100     (gnus-save-hidden-threads
11101       (let ((beg (point)))
11102         ;; We check that there are unread articles.
11103         (when (or all (gnus-summary-find-prev))
11104           (gnus-summary-catchup all t beg)))))
11105   (gnus-summary-position-point))
11106
11107 (defun gnus-summary-catchup-from-here (&optional all)
11108   "Mark all unticked articles after (and including) the current one as read.
11109 If ALL is non-nil, also mark ticked and dormant articles as read."
11110   (interactive "P")
11111   (save-excursion
11112     (gnus-save-hidden-threads
11113       (let ((beg (point)))
11114         ;; We check that there are unread articles.
11115         (when (or all (gnus-summary-find-next))
11116           (gnus-summary-catchup all t beg nil t)))))
11117   (gnus-summary-position-point))
11118
11119 (defun gnus-summary-catchup-all (&optional quietly)
11120   "Mark all articles in this newsgroup as read.
11121 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11122 instead, which marks only unread articles as read."
11123   (interactive "P")
11124   (gnus-summary-catchup t quietly))
11125
11126 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11127   "Mark all unread articles in this group as read, then exit.
11128 If prefix argument ALL is non-nil, all articles are marked as read.
11129 If QUIETLY is non-nil, no questions will be asked."
11130   (interactive "P")
11131   (when (gnus-summary-catchup all quietly nil 'fast)
11132     ;; Select next newsgroup or exit.
11133     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11134              (eq gnus-auto-select-next 'quietly))
11135         (gnus-summary-next-group nil)
11136       (gnus-summary-exit))))
11137
11138 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11139   "Mark all articles in this newsgroup as read, and then exit.
11140 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11141 instead, which marks only unread articles as read."
11142   (interactive "P")
11143   (gnus-summary-catchup-and-exit t quietly))
11144
11145 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11146   "Mark all articles in this group as read and select the next group.
11147 If given a prefix, mark all articles, unread as well as ticked, as
11148 read."
11149   (interactive "P")
11150   (save-excursion
11151     (gnus-summary-catchup all))
11152   (gnus-summary-next-group))
11153
11154 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11155   "Mark all articles in this group as read and select the previous group.
11156 If given a prefix, mark all articles, unread as well as ticked, as
11157 read."
11158   (interactive "P")
11159   (save-excursion
11160     (gnus-summary-catchup all))
11161   (gnus-summary-next-group nil nil t))
11162
11163 ;;;
11164 ;;; with article
11165 ;;;
11166
11167 (defmacro gnus-with-article (article &rest forms)
11168   "Select ARTICLE and perform FORMS in the original article buffer.
11169 Then replace the article with the result."
11170   `(progn
11171      ;; We don't want the article to be marked as read.
11172      (let (gnus-mark-article-hook)
11173        (gnus-summary-select-article t t nil ,article))
11174      (set-buffer gnus-original-article-buffer)
11175      ,@forms
11176      (if (not (gnus-check-backend-function
11177                'request-replace-article (car gnus-article-current)))
11178          (gnus-message 5 "Read-only group; not replacing")
11179        (unless (gnus-request-replace-article
11180                 ,article (car gnus-article-current)
11181                 (current-buffer) t)
11182          (error "Couldn't replace article")))
11183      ;; The cache and backlog have to be flushed somewhat.
11184      (when gnus-keep-backlog
11185        (gnus-backlog-remove-article
11186         (car gnus-article-current) (cdr gnus-article-current)))
11187      (when gnus-use-cache
11188        (gnus-cache-update-article
11189         (car gnus-article-current) (cdr gnus-article-current)))))
11190
11191 (put 'gnus-with-article 'lisp-indent-function 1)
11192 (put 'gnus-with-article 'edebug-form-spec '(form body))
11193
11194 ;; Thread-based commands.
11195
11196 (defun gnus-summary-articles-in-thread (&optional article)
11197   "Return a list of all articles in the current thread.
11198 If ARTICLE is non-nil, return all articles in the thread that starts
11199 with that article."
11200   (let* ((article (or article (gnus-summary-article-number)))
11201          (data (gnus-data-find-list article))
11202          (top-level (gnus-data-level (car data)))
11203          (top-subject
11204           (cond ((null gnus-thread-operation-ignore-subject)
11205                  (gnus-simplify-subject-re
11206                   (mail-header-subject (gnus-data-header (car data)))))
11207                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11208                  (gnus-simplify-subject-fuzzy
11209                   (mail-header-subject (gnus-data-header (car data)))))
11210                 (t nil)))
11211          (end-point (save-excursion
11212                       (if (gnus-summary-go-to-next-thread)
11213                           (point) (point-max))))
11214          articles)
11215     (while (and data
11216                 (< (gnus-data-pos (car data)) end-point))
11217       (when (or (not top-subject)
11218                 (string= top-subject
11219                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11220                              (gnus-simplify-subject-fuzzy
11221                               (mail-header-subject
11222                                (gnus-data-header (car data))))
11223                            (gnus-simplify-subject-re
11224                             (mail-header-subject
11225                              (gnus-data-header (car data)))))))
11226         (push (gnus-data-number (car data)) articles))
11227       (unless (and (setq data (cdr data))
11228                    (> (gnus-data-level (car data)) top-level))
11229         (setq data nil)))
11230     ;; Return the list of articles.
11231     (nreverse articles)))
11232
11233 (defun gnus-summary-rethread-current ()
11234   "Rethread the thread the current article is part of."
11235   (interactive)
11236   (let* ((gnus-show-threads t)
11237          (article (gnus-summary-article-number))
11238          (id (mail-header-id (gnus-summary-article-header)))
11239          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11240     (unless id
11241       (error "No article on the current line"))
11242     (gnus-rebuild-thread id)
11243     (gnus-summary-goto-subject article)))
11244
11245 (defun gnus-summary-reparent-thread ()
11246   "Make the current article child of the marked (or previous) article.
11247
11248 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11249 is non-nil or the Subject: of both articles are the same."
11250   (interactive)
11251   (unless (not (gnus-group-read-only-p))
11252     (error "The current newsgroup does not support article editing"))
11253   (unless (<= (length gnus-newsgroup-processable) 1)
11254     (error "No more than one article may be marked"))
11255   (let ((child (gnus-summary-article-number))
11256         ;; First grab the marked article, otherwise one line up.
11257         (parent (if (not (null gnus-newsgroup-processable))
11258                     (car gnus-newsgroup-processable)
11259                   (save-excursion
11260                     (if (eq (forward-line -1) 0)
11261                         (gnus-summary-article-number)
11262                       (error "Beginning of summary buffer"))))))
11263     (gnus-summary-reparent-children parent (list child))))
11264
11265 (defun gnus-summary-reparent-children (parent children)
11266   "Make PARENT the parent of CHILDREN.
11267 When called interactively, PARENT is the current article and CHILDREN
11268 are the process-marked articles."
11269   (interactive
11270    (list (gnus-summary-article-number)
11271          (gnus-summary-work-articles nil)))
11272   (dolist (child children)
11273     (save-window-excursion
11274       (let ((gnus-article-buffer " *reparent*"))
11275         (unless (not (eq parent child))
11276           (error "An article may not be self-referential"))
11277         (let ((message-id (mail-header-id
11278                            (gnus-summary-article-header parent))))
11279           (unless (and message-id (not (equal message-id "")))
11280             (error "No message-id in desired parent"))
11281           (gnus-with-article child
11282             (save-restriction
11283               (goto-char (point-min))
11284               (message-narrow-to-head)
11285               (if (re-search-forward "^References: " nil t)
11286                   (progn
11287                     (re-search-forward "^[^ \t]" nil t)
11288                     (forward-line -1)
11289                     (end-of-line)
11290                     (insert " " message-id))
11291                 (insert "References: " message-id "\n"))))
11292           (set-buffer gnus-summary-buffer)
11293           (gnus-summary-unmark-all-processable)
11294           (gnus-summary-update-article child)
11295           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11296             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11297           (gnus-summary-rethread-current)
11298           (gnus-message 3 "Article %d is now the child of article %d"
11299                         child parent))))))
11300
11301 (defun gnus-summary-toggle-threads (&optional arg)
11302   "Toggle showing conversation threads.
11303 If ARG is positive number, turn showing conversation threads on."
11304   (interactive "P")
11305   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11306     (setq gnus-show-threads
11307           (if (null arg) (not gnus-show-threads)
11308             (> (prefix-numeric-value arg) 0)))
11309     (gnus-summary-prepare)
11310     (gnus-summary-goto-subject current)
11311     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11312     (gnus-summary-position-point)))
11313
11314 (eval-and-compile
11315   (if (fboundp 'remove-overlays)
11316       (defalias 'gnus-remove-overlays 'remove-overlays)
11317     (defun gnus-remove-overlays (beg end name val)
11318       "Clear BEG and END of overlays whose property NAME has value VAL.
11319 For compatibility with XEmacs."
11320       (dolist (ov (gnus-overlays-in beg end))
11321         (when (eq (gnus-overlay-get ov name) val)
11322           (gnus-delete-overlay ov))))))
11323
11324 (defun gnus-summary-show-all-threads ()
11325   "Show all threads."
11326   (interactive)
11327   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11328   (gnus-summary-position-point))
11329
11330 (defun gnus-summary-show-thread ()
11331   "Show thread subtrees.
11332 Returns nil if no thread was there to be shown."
11333   (interactive)
11334   (let* ((orig (point))
11335          (end (point-at-eol))
11336          ;; Leave point at bol
11337          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11338          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11339                 (if (fboundp 'next-single-char-property-change)
11340                     (or (next-single-char-property-change end 'invisible)
11341                         (point-max))
11342                   (while (progn
11343                            (end-of-line 2)
11344                            (and (not (eobp))
11345                                 (eq (get-char-property (point) 'invisible)
11346                                     'gnus-sum))))
11347                   (point)))))
11348     (when eoi
11349       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11350       (goto-char orig)
11351       (gnus-summary-position-point)
11352       eoi)))
11353
11354 (defun gnus-summary-maybe-hide-threads ()
11355   "If requested, hide the threads that should be hidden."
11356   (when (and gnus-show-threads
11357              gnus-thread-hide-subtree)
11358     (gnus-summary-hide-all-threads
11359      (if (or (consp gnus-thread-hide-subtree)
11360              (functionp gnus-thread-hide-subtree))
11361          (gnus-make-predicate gnus-thread-hide-subtree)
11362        nil))))
11363
11364 ;;; Hiding predicates.
11365
11366 (defun gnus-article-unread-p (header)
11367   (memq (mail-header-number header) gnus-newsgroup-unreads))
11368
11369 (defun gnus-article-unseen-p (header)
11370   (memq (mail-header-number header) gnus-newsgroup-unseen))
11371
11372 (defun gnus-map-articles (predicate articles)
11373   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11374   (apply 'gnus-or (mapcar predicate
11375                           (mapcar (lambda (number)
11376                                     (gnus-summary-article-header number))
11377                                   articles))))
11378
11379 (defun gnus-summary-hide-all-threads (&optional predicate)
11380   "Hide all thread subtrees.
11381 If PREDICATE is supplied, threads that satisfy this predicate
11382 will not be hidden."
11383   (interactive)
11384   (save-excursion
11385     (goto-char (point-min))
11386     (let ((end nil))
11387       (while (not end)
11388         (when (or (not predicate)
11389                   (gnus-map-articles
11390                    predicate (gnus-summary-article-children)))
11391             (gnus-summary-hide-thread))
11392         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11393   (gnus-summary-position-point))
11394
11395 (defun gnus-summary-hide-thread ()
11396   "Hide thread subtrees.
11397 If PREDICATE is supplied, threads that satisfy this predicate
11398 will not be hidden.
11399 Returns nil if no threads were there to be hidden."
11400   (interactive)
11401   (let ((start (point))
11402         (starteol (line-end-position))
11403         (article (gnus-summary-article-number)))
11404     (goto-char start)
11405     ;; Go forward until either the buffer ends or the subthread ends.
11406     (when (and (not (eobp))
11407                (or (zerop (gnus-summary-next-thread 1 t))
11408                    (goto-char (point-max))))
11409       (if (and (> (point) start)
11410                ;; FIXME: this should actually search for a non-invisible \n.
11411                (search-backward "\n" start t))
11412           (progn
11413             (when (> (point) starteol)
11414               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11415               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11416                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11417                 (gnus-overlay-put ol 'evaporate t)))
11418             (gnus-summary-goto-subject article))
11419         (goto-char start)
11420         nil))))
11421
11422 (defun gnus-summary-go-to-next-thread (&optional previous)
11423   "Go to the same level (or less) next thread.
11424 If PREVIOUS is non-nil, go to previous thread instead.
11425 Return the article number moved to, or nil if moving was impossible."
11426   (let ((level (gnus-summary-thread-level))
11427         (way (if previous -1 1))
11428         (beg (point)))
11429     (forward-line way)
11430     (while (and (not (eobp))
11431                 (< level (gnus-summary-thread-level)))
11432       (forward-line way))
11433     (if (eobp)
11434         (progn
11435           (goto-char beg)
11436           nil)
11437       (setq beg (point))
11438       (prog1
11439           (gnus-summary-article-number)
11440         (goto-char beg)))))
11441
11442 (defun gnus-summary-next-thread (n &optional silent)
11443   "Go to the same level next N'th thread.
11444 If N is negative, search backward instead.
11445 Returns the difference between N and the number of skips actually
11446 done.
11447
11448 If SILENT, don't output messages."
11449   (interactive "p")
11450   (let ((backward (< n 0))
11451         (n (abs n)))
11452     (while (and (> n 0)
11453                 (gnus-summary-go-to-next-thread backward))
11454       (decf n))
11455     (unless silent
11456       (gnus-summary-position-point))
11457     (when (and (not silent) (/= 0 n))
11458       (gnus-message 7 "No more threads"))
11459     n))
11460
11461 (defun gnus-summary-prev-thread (n)
11462   "Go to the same level previous N'th thread.
11463 Returns the difference between N and the number of skips actually
11464 done."
11465   (interactive "p")
11466   (gnus-summary-next-thread (- n)))
11467
11468 (defun gnus-summary-go-down-thread ()
11469   "Go down one level in the current thread."
11470   (let ((children (gnus-summary-article-children)))
11471     (when children
11472       (gnus-summary-goto-subject (car children)))))
11473
11474 (defun gnus-summary-go-up-thread ()
11475   "Go up one level in the current thread."
11476   (let ((parent (gnus-summary-article-parent)))
11477     (when parent
11478       (gnus-summary-goto-subject parent))))
11479
11480 (defun gnus-summary-down-thread (n)
11481   "Go down thread N steps.
11482 If N is negative, go up instead.
11483 Returns the difference between N and how many steps down that were
11484 taken."
11485   (interactive "p")
11486   (let ((up (< n 0))
11487         (n (abs n)))
11488     (while (and (> n 0)
11489                 (if up (gnus-summary-go-up-thread)
11490                   (gnus-summary-go-down-thread)))
11491       (setq n (1- n)))
11492     (gnus-summary-position-point)
11493     (when (/= 0 n)
11494       (gnus-message 7 "Can't go further"))
11495     n))
11496
11497 (defun gnus-summary-up-thread (n)
11498   "Go up thread N steps.
11499 If N is negative, go down instead.
11500 Returns the difference between N and how many steps down that were
11501 taken."
11502   (interactive "p")
11503   (gnus-summary-down-thread (- n)))
11504
11505 (defun gnus-summary-top-thread ()
11506   "Go to the top of the thread."
11507   (interactive)
11508   (while (gnus-summary-go-up-thread))
11509   (gnus-summary-article-number))
11510
11511 (defun gnus-summary-expire-thread ()
11512   "Mark articles under current thread as expired."
11513   (interactive)
11514   (gnus-summary-kill-thread 0))
11515
11516 (defun gnus-summary-kill-thread (&optional unmark)
11517   "Mark articles under current thread as read.
11518 If the prefix argument is positive, remove any kinds of marks.
11519 If the prefix argument is zero, mark thread as expired.
11520 If the prefix argument is negative, tick articles instead."
11521   (interactive "P")
11522   (when unmark
11523     (setq unmark (prefix-numeric-value unmark)))
11524   (let ((articles (gnus-summary-articles-in-thread))
11525         (hide (or (null unmark) (= unmark 0))))
11526     (save-excursion
11527       ;; Expand the thread.
11528       (gnus-summary-show-thread)
11529       ;; Mark all the articles.
11530       (while articles
11531         (gnus-summary-goto-subject (car articles))
11532         (cond ((null unmark)
11533                (gnus-summary-mark-article-as-read gnus-killed-mark))
11534               ((> unmark 0)
11535                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11536               ((= unmark 0)
11537                (gnus-summary-mark-article nil gnus-expirable-mark))
11538               (t
11539                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11540         (setq articles (cdr articles))))
11541     ;; Hide killed subtrees when hide is true.
11542     (and hide
11543          gnus-thread-hide-killed
11544          (gnus-summary-hide-thread))
11545     ;; If hide is t, go to next unread subject.
11546     (when hide
11547       ;; Go to next unread subject.
11548       (gnus-summary-next-subject 1 t)))
11549   (gnus-set-mode-line 'summary))
11550
11551 ;; Summary sorting commands
11552
11553 (defun gnus-summary-sort-by-number (&optional reverse)
11554   "Sort the summary buffer by article number.
11555 Argument REVERSE means reverse order."
11556   (interactive "P")
11557   (gnus-summary-sort 'number reverse))
11558
11559 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11560   "Sort the summary buffer by most recent article number.
11561 Argument REVERSE means reverse order."
11562   (interactive "P")
11563   (gnus-summary-sort 'most-recent-number reverse))
11564
11565 (defun gnus-summary-sort-by-random (&optional reverse)
11566   "Randomize the order in the summary buffer.
11567 Argument REVERSE means to randomize in reverse order."
11568   (interactive "P")
11569   (gnus-summary-sort 'random reverse))
11570
11571 (defun gnus-summary-sort-by-author (&optional reverse)
11572   "Sort the summary buffer by author name alphabetically.
11573 If `case-fold-search' is non-nil, case of letters is ignored.
11574 Argument REVERSE means reverse order."
11575   (interactive "P")
11576   (gnus-summary-sort 'author reverse))
11577
11578 (defun gnus-summary-sort-by-recipient (&optional reverse)
11579   "Sort the summary buffer by recipient name alphabetically.
11580 If `case-fold-search' is non-nil, case of letters is ignored.
11581 Argument REVERSE means reverse order."
11582   (interactive "P")
11583   (gnus-summary-sort 'recipient reverse))
11584
11585 (defun gnus-summary-sort-by-subject (&optional reverse)
11586   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11587 If `case-fold-search' is non-nil, case of letters is ignored.
11588 Argument REVERSE means reverse order."
11589   (interactive "P")
11590   (gnus-summary-sort 'subject reverse))
11591
11592 (defun gnus-summary-sort-by-date (&optional reverse)
11593   "Sort the summary buffer by date.
11594 Argument REVERSE means reverse order."
11595   (interactive "P")
11596   (gnus-summary-sort 'date reverse))
11597
11598 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11599   "Sort the summary buffer by most recent date.
11600 Argument REVERSE means reverse order."
11601   (interactive "P")
11602   (gnus-summary-sort 'most-recent-date reverse))
11603
11604 (defun gnus-summary-sort-by-score (&optional reverse)
11605   "Sort the summary buffer by score.
11606 Argument REVERSE means reverse order."
11607   (interactive "P")
11608   (gnus-summary-sort 'score reverse))
11609
11610 (defun gnus-summary-sort-by-lines (&optional reverse)
11611   "Sort the summary buffer by the number of lines.
11612 Argument REVERSE means reverse order."
11613   (interactive "P")
11614   (gnus-summary-sort 'lines reverse))
11615
11616 (defun gnus-summary-sort-by-chars (&optional reverse)
11617   "Sort the summary buffer by article length.
11618 Argument REVERSE means reverse order."
11619   (interactive "P")
11620   (gnus-summary-sort 'chars reverse))
11621
11622 (defun gnus-summary-sort-by-original (&optional reverse)
11623   "Sort the summary buffer using the default sorting method.
11624 Argument REVERSE means reverse order."
11625   (interactive "P")
11626   (let* ((inhibit-read-only t)
11627          (gnus-summary-prepare-hook nil))
11628     ;; We do the sorting by regenerating the threads.
11629     (gnus-summary-prepare)
11630     ;; Hide subthreads if needed.
11631     (gnus-summary-maybe-hide-threads)))
11632
11633 (defun gnus-summary-sort (predicate reverse)
11634   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11635   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11636          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11637          (gnus-thread-sort-functions
11638           (if (not reverse)
11639               thread
11640             `(lambda (t1 t2)
11641                (,thread t2 t1))))
11642          (gnus-sort-gathered-threads-function
11643           gnus-thread-sort-functions)
11644          (gnus-article-sort-functions
11645           (if (not reverse)
11646               article
11647             `(lambda (t1 t2)
11648                (,article t2 t1))))
11649          (inhibit-read-only t)
11650          (gnus-summary-prepare-hook nil))
11651     ;; We do the sorting by regenerating the threads.
11652     (gnus-summary-prepare)
11653     ;; Hide subthreads if needed.
11654     (gnus-summary-maybe-hide-threads)))
11655
11656 ;; Summary saving commands.
11657
11658 (defun gnus-summary-save-article (&optional n not-saved)
11659   "Save the current article using the default saver function.
11660 If N is a positive number, save the N next articles.
11661 If N is a negative number, save the N previous articles.
11662 If N is nil and any articles have been marked with the process mark,
11663 save those articles instead.
11664 The variable `gnus-default-article-saver' specifies the saver function.
11665
11666 If the optional second argument NOT-SAVED is non-nil, articles saved
11667 will not be marked as saved."
11668   (interactive "P")
11669   (require 'gnus-art)
11670   (let* ((articles (gnus-summary-work-articles n))
11671          (save-buffer (save-excursion
11672                         (nnheader-set-temp-buffer " *Gnus Save*")))
11673          (num (length articles))
11674          ;; Whether to save decoded articles or raw articles.
11675          (decode (when gnus-article-save-coding-system
11676                    (get gnus-default-article-saver :decode)))
11677          ;; When saving many articles in a single file, use the other
11678          ;; function to save articles other than the first one.
11679          (saver2 (get gnus-default-article-saver :function))
11680          (gnus-prompt-before-saving (if saver2
11681                                         t
11682                                       gnus-prompt-before-saving))
11683          (gnus-default-article-saver gnus-default-article-saver)
11684          header file)
11685     (dolist (article articles)
11686       (setq header (gnus-summary-article-header article))
11687       (if (not (vectorp header))
11688           ;; This is a pseudo-article.
11689           (if (assq 'name header)
11690               (gnus-copy-file (cdr (assq 'name header)))
11691             (gnus-message 1 "Article %d is unsaveable" article))
11692         ;; This is a real article.
11693         (save-window-excursion
11694           (gnus-summary-select-article decode decode nil article)
11695           (gnus-summary-goto-subject article))
11696         (with-current-buffer save-buffer
11697           (erase-buffer)
11698           (insert-buffer-substring (if decode
11699                                        gnus-article-buffer
11700                                      gnus-original-article-buffer)))
11701         (setq file (gnus-article-save save-buffer file num))
11702         (gnus-summary-remove-process-mark article)
11703         (unless not-saved
11704           (gnus-summary-set-saved-mark article)))
11705       (when saver2
11706         (setq gnus-default-article-saver saver2
11707               saver2 nil)))
11708     (gnus-kill-buffer save-buffer)
11709     (gnus-summary-position-point)
11710     (gnus-set-mode-line 'summary)
11711     n))
11712
11713 (defun gnus-summary-pipe-output (&optional n sym)
11714   "Pipe the current article to a subprocess.
11715 If N is a positive number, pipe the N next articles.
11716 If N is a negative number, pipe the N previous articles.
11717 If N is nil and any articles have been marked with the process mark,
11718 pipe those articles instead.
11719 The default command to which articles are piped is specified by the
11720 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11721 will be prompted for the command.
11722
11723 The properties `:decode' and `:headers' that are put to the function
11724 symbol `gnus-summary-save-in-pipe' control whether this function
11725 decodes articles and what headers to keep (see the doc string for the
11726 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11727 is neither omitted nor the symbol `r', force including all headers
11728 regardless of the `:headers' property.  If it is the symbol `r',
11729 articles that are not decoded and include all headers will be piped
11730 no matter what the properties `:decode' and `:headers' are."
11731   (interactive (gnus-interactive "P\ny"))
11732   (require 'gnus-art)
11733   (let* ((articles (gnus-summary-work-articles n))
11734          (result-buffer "*Shell Command Output*")
11735          (all-headers (not (memq sym '(nil r))))
11736          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11737          (raw (eq sym 'r))
11738          (headers (get 'gnus-summary-save-in-pipe :headers))
11739          command result)
11740     (unless (numberp (car articles))
11741       (error "No article to pipe"))
11742     (setq command (gnus-read-shell-command
11743                    (concat "Shell command on "
11744                            (if (cdr articles)
11745                                (format "these %d articles" (length articles))
11746                              "this article")
11747                            ": ")
11748                    gnus-summary-pipe-output-default-command))
11749     (when (string-equal command "")
11750       (error "A command is required"))
11751     (when all-headers
11752       (put 'gnus-summary-save-in-pipe :headers nil))
11753     (unwind-protect
11754         (while articles
11755           (gnus-summary-goto-subject (pop articles))
11756           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11757           (when (and (get-buffer result-buffer)
11758                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11759             (setq result (concat result (with-current-buffer result-buffer
11760                                           (buffer-string))))))
11761       (put 'gnus-summary-save-in-pipe :headers headers))
11762     (unless (zerop (length result))
11763       (if (with-current-buffer (get-buffer-create result-buffer)
11764             (erase-buffer)
11765             (insert result)
11766             (prog1
11767                 (and (= (count-lines (point-min) (point)) 1)
11768                      (progn
11769                        (end-of-line 0)
11770                        (<= (current-column)
11771                            (window-width (minibuffer-window)))))
11772               (goto-char (point-min))))
11773           (message "%s" (substring result 0 -1))
11774         (message nil)
11775         (gnus-configure-windows 'pipe)))))
11776
11777 (defun gnus-summary-save-article-mail (&optional arg)
11778   "Append the current article to a Unix mail box file.
11779 If N is a positive number, save the N next articles.
11780 If N is a negative number, save the N previous articles.
11781 If N is nil and any articles have been marked with the process mark,
11782 save those articles instead."
11783   (interactive "P")
11784   (require 'gnus-art)
11785   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11786     (gnus-summary-save-article arg)))
11787
11788 (defun gnus-summary-save-article-rmail (&optional arg)
11789   "Append the current article to an rmail file.
11790 If N is a positive number, save the N next articles.
11791 If N is a negative number, save the N previous articles.
11792 If N is nil and any articles have been marked with the process mark,
11793 save those articles instead."
11794   (interactive "P")
11795   (require 'gnus-art)
11796   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11797     (gnus-summary-save-article arg)))
11798
11799 (defun gnus-summary-save-article-file (&optional arg)
11800   "Append the current article to a file.
11801 If N is a positive number, save the N next articles.
11802 If N is a negative number, save the N previous articles.
11803 If N is nil and any articles have been marked with the process mark,
11804 save those articles instead."
11805   (interactive "P")
11806   (require 'gnus-art)
11807   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11808     (gnus-summary-save-article arg)))
11809
11810 (defun gnus-summary-write-article-file (&optional arg)
11811   "Write the current article to a file, deleting the previous file.
11812 If N is a positive number, save the N next articles.
11813 If N is a negative number, save the N previous articles.
11814 If N is nil and any articles have been marked with the process mark,
11815 save those articles instead."
11816   (interactive "P")
11817   (require 'gnus-art)
11818   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11819     (gnus-summary-save-article arg)))
11820
11821 (defun gnus-summary-save-article-body-file (&optional arg)
11822   "Append the current article body to a file.
11823 If N is a positive number, save the N next articles.
11824 If N is a negative number, save the N previous articles.
11825 If N is nil and any articles have been marked with the process mark,
11826 save those articles instead."
11827   (interactive "P")
11828   (require 'gnus-art)
11829   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11830     (gnus-summary-save-article arg)))
11831
11832 (defun gnus-summary-write-article-body-file (&optional arg)
11833   "Write the current article body to a file, deleting the previous file.
11834 If N is a positive number, save the N next articles.
11835 If N is a negative number, save the N previous articles.
11836 If N is nil and any articles have been marked with the process mark,
11837 save those articles instead."
11838   (interactive "P")
11839   (require 'gnus-art)
11840   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11841     (gnus-summary-save-article arg)))
11842
11843 (defun gnus-summary-muttprint (&optional arg)
11844   "Print the current article using Muttprint.
11845 If N is a positive number, save the N next articles.
11846 If N is a negative number, save the N previous articles.
11847 If N is nil and any articles have been marked with the process mark,
11848 save those articles instead."
11849   (interactive "P")
11850   (require 'gnus-art)
11851   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11852     (gnus-summary-save-article arg t)))
11853
11854 (defun gnus-summary-pipe-message (program)
11855   "Pipe the current article through PROGRAM."
11856   (interactive "sProgram: ")
11857   (gnus-summary-select-article)
11858   (let ((mail-header-separator ""))
11859     (gnus-eval-in-buffer-window gnus-article-buffer
11860       (save-restriction
11861         (widen)
11862         (let ((start (window-start))
11863               (inhibit-read-only t))
11864           (message-pipe-buffer-body program)
11865           (set-window-start (get-buffer-window (current-buffer)) start))))))
11866
11867 (defun gnus-get-split-value (methods)
11868   "Return a value based on the split METHODS."
11869   (let (split-name method result match)
11870     (when methods
11871       (with-current-buffer gnus-original-article-buffer
11872         (save-restriction
11873           (nnheader-narrow-to-headers)
11874           (while (and methods (not split-name))
11875             (goto-char (point-min))
11876             (setq method (pop methods))
11877             (setq match (car method))
11878             (when (cond
11879                    ((stringp match)
11880                     ;; Regular expression.
11881                     (ignore-errors
11882                       (re-search-forward match nil t)))
11883                    ((functionp match)
11884                     ;; Function.
11885                     (save-restriction
11886                       (widen)
11887                       (setq result (funcall match gnus-newsgroup-name))))
11888                    ((consp match)
11889                     ;; Form.
11890                     (save-restriction
11891                       (widen)
11892                       (setq result (eval match)))))
11893               (setq split-name (cdr method))
11894               (cond ((stringp result)
11895                      (push (expand-file-name
11896                             result gnus-article-save-directory)
11897                            split-name))
11898                     ((consp result)
11899                      (setq split-name (append result split-name)))))))))
11900     (nreverse split-name)))
11901
11902 (defun gnus-valid-move-group-p (group)
11903   (and (boundp group)
11904        (symbol-name group)
11905        (symbol-value group)
11906        (gnus-get-function (gnus-find-method-for-group
11907                            (symbol-name group)) 'request-accept-article t)))
11908
11909 (defun gnus-read-move-group-name (prompt default articles prefix)
11910   "Read a group name."
11911   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11912          (minibuffer-confirm-incomplete nil) ; XEmacs
11913          (prom
11914           (format "%s %s to"
11915                   prompt
11916                   (if (> (length articles) 1)
11917                       (format "these %d articles" (length articles))
11918                     "this article")))
11919          (to-newsgroup
11920           (let (active group)
11921             (when (or (null split-name) (= 1 (length split-name)))
11922               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11923               (mapatoms (lambda (symbol)
11924                           (setq group (symbol-name symbol))
11925                           (when (string-match "[^\000-\177]" group)
11926                             (setq group (gnus-group-decoded-name group)))
11927                           (set (intern group active) group))
11928                         gnus-active-hashtb))
11929             (cond
11930              ((null split-name)
11931               (gnus-completing-read-with-default
11932                default prom active 'gnus-valid-move-group-p nil prefix
11933                'gnus-group-history))
11934              ((= 1 (length split-name))
11935               (gnus-completing-read-with-default
11936                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11937                'gnus-group-history))
11938              (t
11939               (gnus-completing-read-with-default
11940                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11941                'gnus-group-history)))))
11942          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11943          encoded)
11944     (when to-newsgroup
11945       (if (or (string= to-newsgroup "")
11946               (string= to-newsgroup prefix))
11947           (setq to-newsgroup default))
11948       (unless to-newsgroup
11949         (error "No group name entered"))
11950       (setq encoded (mm-encode-coding-string
11951                      to-newsgroup
11952                      (gnus-group-name-charset to-method to-newsgroup)))
11953       (or (gnus-active encoded)
11954           (gnus-activate-group encoded nil nil to-method)
11955           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11956                                      to-newsgroup))
11957               (or (and (gnus-request-create-group encoded to-method)
11958                        (gnus-activate-group encoded nil nil to-method)
11959                        (gnus-subscribe-group encoded))
11960                   (error "Couldn't create group %s" to-newsgroup)))
11961           (error "No such group: %s" to-newsgroup))
11962       encoded)))
11963
11964 (defvar gnus-summary-save-parts-counter)
11965 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11966
11967 (defun gnus-summary-save-parts (type dir n &optional reverse)
11968   "Save parts matching TYPE to DIR.
11969 If REVERSE, save parts that do not match TYPE."
11970   (interactive
11971    (list (read-string "Save parts of type: "
11972                       (or (car gnus-summary-save-parts-type-history)
11973                           gnus-summary-save-parts-default-mime)
11974                       'gnus-summary-save-parts-type-history)
11975          (setq gnus-summary-save-parts-last-directory
11976                (read-file-name "Save to directory: "
11977                                gnus-summary-save-parts-last-directory
11978                                nil t))
11979          current-prefix-arg))
11980   (gnus-summary-iterate n
11981     (let ((gnus-display-mime-function nil)
11982           gnus-article-prepare-hook
11983           gnus-article-decode-hook
11984           gnus-display-mime-function
11985           gnus-break-pages
11986           (gnus-inhibit-treatment t))
11987       (gnus-summary-select-article))
11988     (with-current-buffer gnus-article-buffer
11989       (let ((handles (or gnus-article-mime-handles
11990                          (mm-dissect-buffer nil gnus-article-loose-mime)
11991                          (and gnus-article-emulate-mime
11992                               (mm-uu-dissect))))
11993             (gnus-summary-save-parts-counter 1))
11994         (when handles
11995           (gnus-summary-save-parts-1 type dir handles reverse)
11996           (unless gnus-article-mime-handles ;; Don't destroy this case.
11997             (mm-destroy-parts handles)))))))
11998
11999 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12000   (if (stringp (car handle))
12001       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12002               (cdr handle))
12003     (when (if reverse
12004               (not (string-match type (mm-handle-media-type handle)))
12005             (string-match type (mm-handle-media-type handle)))
12006       (let ((file (expand-file-name
12007                    (gnus-map-function
12008                     mm-file-name-rewrite-functions
12009                     (file-name-nondirectory
12010                      (or
12011                       (mail-content-type-get
12012                        (mm-handle-disposition handle) 'filename)
12013                       (mail-content-type-get
12014                        (mm-handle-type handle) 'name)
12015                       (format "%s.%d.%d" gnus-newsgroup-name
12016                               (cdr gnus-article-current)
12017                               gnus-summary-save-parts-counter))))
12018                    dir)))
12019         (incf gnus-summary-save-parts-counter)
12020         (unless (file-exists-p file)
12021           (mm-save-part-to-file handle file))))))
12022
12023 ;; Summary extract commands
12024
12025 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12026   (let ((inhibit-read-only t)
12027         (article (gnus-summary-article-number))
12028         after-article b e)
12029     (unless (gnus-summary-goto-subject article)
12030       (error "No such article: %d" article))
12031     (gnus-summary-position-point)
12032     ;; If all commands are to be bunched up on one line, we collect
12033     ;; them here.
12034     (unless gnus-view-pseudos-separately
12035       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12036             files action)
12037         (while ps
12038           (setq action (cdr (assq 'action (car ps))))
12039           (setq files (list (cdr (assq 'name (car ps)))))
12040           (while (and ps (cdr ps)
12041                       (string= (or action "1")
12042                                (or (cdr (assq 'action (cadr ps))) "2")))
12043             (push (cdr (assq 'name (cadr ps))) files)
12044             (setcdr ps (cddr ps)))
12045           (when files
12046             (when (not (string-match "%s" action))
12047               (push " " files))
12048             (push " " files)
12049             (when (assq 'execute (car ps))
12050               (setcdr (assq 'execute (car ps))
12051                       (funcall (if (string-match "%s" action)
12052                                    'format 'concat)
12053                                action
12054                                (mapconcat
12055                                 (lambda (f)
12056                                   (if (equal f " ")
12057                                       f
12058                                     (shell-quote-argument f)))
12059                                 files " ")))))
12060           (setq ps (cdr ps)))))
12061     (if (and gnus-view-pseudos (not not-view))
12062         (while pslist
12063           (when (assq 'execute (car pslist))
12064             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12065                                   (eq gnus-view-pseudos 'not-confirm)))
12066           (setq pslist (cdr pslist)))
12067       (save-excursion
12068         (while pslist
12069           (setq after-article (or (cdr (assq 'article (car pslist)))
12070                                   (gnus-summary-article-number)))
12071           (gnus-summary-goto-subject after-article)
12072           (forward-line 1)
12073           (setq b (point))
12074           (insert "    " (file-name-nondirectory
12075                           (cdr (assq 'name (car pslist))))
12076                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12077           (setq e (point))
12078           (forward-line -1)             ; back to `b'
12079           (gnus-add-text-properties
12080            b (1- e) (list 'gnus-number gnus-reffed-article-number
12081                           gnus-mouse-face-prop gnus-mouse-face))
12082           (gnus-data-enter
12083            after-article gnus-reffed-article-number
12084            gnus-unread-mark b (car pslist) 0 (- e b))
12085           (setq gnus-newsgroup-unreads
12086                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12087                                          gnus-reffed-article-number))
12088           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12089           (setq pslist (cdr pslist)))))))
12090
12091 (defun gnus-pseudos< (p1 p2)
12092   (let ((c1 (cdr (assq 'action p1)))
12093         (c2 (cdr (assq 'action p2))))
12094     (and c1 c2 (string< c1 c2))))
12095
12096 (defun gnus-request-pseudo-article (props)
12097   (cond ((assq 'execute props)
12098          (gnus-execute-command (cdr (assq 'execute props)))))
12099   (let ((gnus-current-article (gnus-summary-article-number)))
12100     (gnus-run-hooks 'gnus-mark-article-hook)))
12101
12102 (defun gnus-execute-command (command &optional automatic)
12103   (save-excursion
12104     (gnus-article-setup-buffer)
12105     (set-buffer gnus-article-buffer)
12106     (setq buffer-read-only nil)
12107     (let ((command (if automatic command
12108                      (read-string "Command: " (cons command 0)))))
12109       (erase-buffer)
12110       (insert "$ " command "\n\n")
12111       (if gnus-view-pseudo-asynchronously
12112           (start-process "gnus-execute" (current-buffer) shell-file-name
12113                          shell-command-switch command)
12114         (call-process shell-file-name nil t nil
12115                       shell-command-switch command)))))
12116
12117 ;; Summary kill commands.
12118
12119 (defun gnus-summary-edit-global-kill (article)
12120   "Edit the \"global\" kill file."
12121   (interactive (list (gnus-summary-article-number)))
12122   (gnus-group-edit-global-kill article))
12123
12124 (defun gnus-summary-edit-local-kill ()
12125   "Edit a local kill file applied to the current newsgroup."
12126   (interactive)
12127   (setq gnus-current-headers (gnus-summary-article-header))
12128   (gnus-group-edit-local-kill
12129    (gnus-summary-article-number) gnus-newsgroup-name))
12130
12131 ;;; Header reading.
12132
12133 (defun gnus-read-header (id &optional header)
12134   "Read the headers of article ID and enter them into the Gnus system."
12135   (let ((group gnus-newsgroup-name)
12136         (gnus-override-method
12137          (or
12138           gnus-override-method
12139           (and (gnus-news-group-p gnus-newsgroup-name)
12140                (car (gnus-refer-article-methods)))))
12141         where)
12142     ;; First we check to see whether the header in question is already
12143     ;; fetched.
12144     (if (stringp id)
12145         ;; This is a Message-ID.
12146         (setq header (or header (gnus-id-to-header id)))
12147       ;; This is an article number.
12148       (setq header (or header (gnus-summary-article-header id))))
12149     (if (and header
12150              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12151         ;; We have found the header.
12152         header
12153       ;; We have to really fetch the header to this article.
12154       (with-current-buffer nntp-server-buffer
12155         (when (setq where (gnus-request-head id group))
12156           (nnheader-fold-continuation-lines)
12157           (goto-char (point-max))
12158           (insert ".\n")
12159           (goto-char (point-min))
12160           (insert "211 ")
12161           (princ (cond
12162                   ((numberp id) id)
12163                   ((cdr where) (cdr where))
12164                   (header (mail-header-number header))
12165                   (t gnus-reffed-article-number))
12166                  (current-buffer))
12167           (insert " Article retrieved.\n"))
12168         (if (or (not where)
12169                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12170             ()                          ; Malformed head.
12171           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12172             (when (and (stringp id)
12173                        (or
12174                         (not (string= (gnus-group-real-name group)
12175                                       (car where)))
12176                         (not (gnus-server-equal gnus-override-method
12177                                                 (gnus-group-method group)))))
12178               ;; If we fetched by Message-ID and the article came from
12179               ;; a different group (or server), we fudge some bogus
12180               ;; article numbers for this article.
12181               (mail-header-set-number header gnus-reffed-article-number))
12182             (with-current-buffer gnus-summary-buffer
12183               (decf gnus-reffed-article-number)
12184               (gnus-remove-header (mail-header-number header))
12185               (push header gnus-newsgroup-headers)
12186               (setq gnus-current-headers header)
12187               (push (mail-header-number header) gnus-newsgroup-limit)))
12188           header)))))
12189
12190 (defun gnus-remove-header (number)
12191   "Remove header NUMBER from `gnus-newsgroup-headers'."
12192   (if (and gnus-newsgroup-headers
12193            (= number (mail-header-number (car gnus-newsgroup-headers))))
12194       (pop gnus-newsgroup-headers)
12195     (let ((headers gnus-newsgroup-headers))
12196       (while (and (cdr headers)
12197                   (not (= number (mail-header-number (cadr headers)))))
12198         (pop headers))
12199       (when (cdr headers)
12200         (setcdr headers (cddr headers))))))
12201
12202 ;;;
12203 ;;; summary highlights
12204 ;;;
12205
12206 (defun gnus-highlight-selected-summary ()
12207   "Highlight selected article in summary buffer."
12208   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12209   (when gnus-summary-selected-face
12210     (save-excursion
12211       (let* ((beg (point-at-bol))
12212              (end (point-at-eol))
12213              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12214              (from (if (get-text-property beg gnus-mouse-face-prop)
12215                        beg
12216                      (or (next-single-property-change
12217                           beg gnus-mouse-face-prop nil end)
12218                          beg)))
12219              (to
12220               (if (= from end)
12221                   (- from 2)
12222                 (or (next-single-property-change
12223                      from gnus-mouse-face-prop nil end)
12224                     end))))
12225         ;; If no mouse-face prop on line we will have to = from = end,
12226         ;; so we highlight the entire line instead.
12227         (when (= (+ to 2) from)
12228           (setq from beg)
12229           (setq to end))
12230         (if gnus-newsgroup-selected-overlay
12231             ;; Move old overlay.
12232             (gnus-move-overlay
12233              gnus-newsgroup-selected-overlay from to (current-buffer))
12234           ;; Create new overlay.
12235           (gnus-overlay-put
12236            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12237            'face gnus-summary-selected-face))))))
12238
12239 (defvar gnus-summary-highlight-line-cached nil)
12240 (defvar gnus-summary-highlight-line-trigger nil)
12241
12242 (defun gnus-summary-highlight-line-0 ()
12243   (if (and (eq gnus-summary-highlight-line-trigger
12244                gnus-summary-highlight)
12245            gnus-summary-highlight-line-cached)
12246       gnus-summary-highlight-line-cached
12247     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12248           gnus-summary-highlight-line-cached
12249           (let* ((cond (list 'cond))
12250                  (c cond)
12251                  (list gnus-summary-highlight))
12252             (while list
12253               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12254                               nil))
12255               (setq c (cdr c)
12256                     list (cdr list)))
12257             (gnus-byte-compile (list 'lambda nil cond))))))
12258
12259 (defun gnus-summary-highlight-line ()
12260   "Highlight current line according to `gnus-summary-highlight'."
12261   (let* ((beg (point-at-bol))
12262          (article (or (gnus-summary-article-number) gnus-current-article))
12263          (score (or (cdr (assq article
12264                                gnus-newsgroup-scored))
12265                     gnus-summary-default-score 0))
12266          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12267          (inhibit-read-only t)
12268          (default gnus-summary-default-score)
12269          (default-high gnus-summary-default-high-score)
12270          (default-low gnus-summary-default-low-score)
12271          (uncached (and gnus-summary-use-undownloaded-faces
12272                         (memq article gnus-newsgroup-undownloaded)
12273                         (not (memq article gnus-newsgroup-cached)))))
12274     (let ((face (funcall (gnus-summary-highlight-line-0))))
12275       (unless (eq face (get-text-property beg 'face))
12276         (gnus-put-text-property-excluding-characters-with-faces
12277          beg (point-at-eol) 'face
12278          (setq face (if (boundp face) (symbol-value face) face)))
12279         (when gnus-summary-highlight-line-function
12280           (funcall gnus-summary-highlight-line-function article face))))))
12281
12282 (defun gnus-update-read-articles (group unread &optional compute)
12283   "Update the list of read articles in GROUP.
12284 UNREAD is a sorted list."
12285   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12286         (info (gnus-get-info group))
12287         (prev 1)
12288         read)
12289     (if (or (not info) (not active))
12290         ;; There is no info on this group if it was, in fact,
12291         ;; killed.  Gnus stores no information on killed groups, so
12292         ;; there's nothing to be done.
12293         ;; One could store the information somewhere temporarily,
12294         ;; perhaps...  Hmmm...
12295         ()
12296       ;; Remove any negative articles numbers.
12297       (while (and unread (< (car unread) 0))
12298         (setq unread (cdr unread)))
12299       ;; Remove any expired article numbers
12300       (while (and unread (< (car unread) (car active)))
12301         (setq unread (cdr unread)))
12302       ;; Compute the ranges of read articles by looking at the list of
12303       ;; unread articles.
12304       (while unread
12305         (when (/= (car unread) prev)
12306           (push (if (= prev (1- (car unread))) prev
12307                   (cons prev (1- (car unread))))
12308                 read))
12309         (setq prev (1+ (car unread)))
12310         (setq unread (cdr unread)))
12311       (when (<= prev (cdr active))
12312         (push (cons prev (cdr active)) read))
12313       (setq read (if (> (length read) 1) (nreverse read) read))
12314       (if compute
12315           read
12316         (save-excursion
12317           (let (setmarkundo)
12318             ;; Propagate the read marks to the backend.
12319             (when (and gnus-propagate-marks
12320                        (gnus-check-backend-function 'request-set-mark group))
12321               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12322                     (add (gnus-remove-from-range read (gnus-info-read info))))
12323                 (when (or add del)
12324                   (unless (gnus-check-group group)
12325                     (error "Can't open server for %s" group))
12326                   (gnus-request-set-mark
12327                    group (delq nil (list (if add (list add 'add '(read)))
12328                                          (if del (list del 'del '(read))))))
12329                   (setq setmarkundo
12330                         `(gnus-request-set-mark
12331                           ,group
12332                           ',(delq nil (list
12333                                        (if del (list del 'add '(read)))
12334                                        (if add (list add 'del '(read))))))))))
12335             (set-buffer gnus-group-buffer)
12336             (gnus-undo-register
12337               `(progn
12338                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12339                  (gnus-info-set-read ',info ',(gnus-info-read info))
12340                  (gnus-get-unread-articles-in-group ',info
12341                                                     (gnus-active ,group))
12342                  (gnus-group-update-group ,group t)
12343                  ,setmarkundo))))
12344         ;; Enter this list into the group info.
12345         (gnus-info-set-read info read)
12346         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12347         (gnus-get-unread-articles-in-group info (gnus-active group))
12348         t))))
12349
12350 (defun gnus-offer-save-summaries ()
12351   "Offer to save all active summary buffers."
12352   (let (buffers)
12353     ;; Go through all buffers and find all summaries.
12354     (dolist (buffer (buffer-list))
12355       (when (and (setq buffer (buffer-name buffer))
12356                  (string-match "Summary" buffer)
12357                  (with-current-buffer buffer
12358                    ;; We check that this is, indeed, a summary buffer.
12359                    (and (eq major-mode 'gnus-summary-mode)
12360                         ;; Also make sure this isn't bogus.
12361                         gnus-newsgroup-prepared
12362                         ;; Also make sure that this isn't a
12363                         ;; dead summary buffer.
12364                         (not gnus-dead-summary-mode))))
12365         (push buffer buffers)))
12366     ;; Go through all these summary buffers and offer to save them.
12367     (when buffers
12368       (save-excursion
12369         (map-y-or-n-p
12370          "Update summary buffer %s? "
12371          (lambda (buf)
12372            (switch-to-buffer buf)
12373            (gnus-summary-exit))
12374          buffers)))))
12375
12376 (defun gnus-summary-setup-default-charset ()
12377   "Setup newsgroup default charset."
12378   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12379       (setq gnus-newsgroup-charset nil)
12380     (let* ((ignored-charsets
12381             (or gnus-newsgroup-ephemeral-ignored-charsets
12382                 (append
12383                  (and gnus-newsgroup-name
12384                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12385                  gnus-newsgroup-ignored-charsets))))
12386       (setq gnus-newsgroup-charset
12387             (or gnus-newsgroup-ephemeral-charset
12388                 (and gnus-newsgroup-name
12389                      (gnus-parameter-charset gnus-newsgroup-name))
12390                 gnus-default-charset))
12391       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12392            ignored-charsets))))
12393
12394 ;;;
12395 ;;; Mime Commands
12396 ;;;
12397
12398 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12399   "Display the current article buffer fully MIME-buttonized.
12400 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12401 treated as multipart/mixed."
12402   (interactive "P")
12403   (require 'gnus-art)
12404   (let ((gnus-unbuttonized-mime-types nil)
12405         (gnus-mime-display-multipart-as-mixed show-all-parts))
12406     (gnus-summary-show-article)))
12407
12408 (defun gnus-summary-repair-multipart (article)
12409   "Add a Content-Type header to a multipart article without one."
12410   (interactive (list (gnus-summary-article-number)))
12411   (gnus-with-article article
12412     (message-narrow-to-head)
12413     (message-remove-header "Mime-Version")
12414     (goto-char (point-max))
12415     (insert "Mime-Version: 1.0\n")
12416     (widen)
12417     (when (search-forward "\n--" nil t)
12418       (let ((separator (buffer-substring (point) (point-at-eol))))
12419         (message-narrow-to-head)
12420         (message-remove-header "Content-Type")
12421         (goto-char (point-max))
12422         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12423                         separator))
12424         (widen))))
12425   (let (gnus-mark-article-hook)
12426     (gnus-summary-select-article t t nil article)))
12427
12428 (defun gnus-summary-toggle-display-buttonized ()
12429   "Toggle the buttonizing of the article buffer."
12430   (interactive)
12431   (require 'gnus-art)
12432   (if (setq gnus-inhibit-mime-unbuttonizing
12433             (not gnus-inhibit-mime-unbuttonizing))
12434       (let ((gnus-unbuttonized-mime-types nil))
12435         (gnus-summary-show-article))
12436     (gnus-summary-show-article)))
12437
12438 ;;;
12439 ;;; Generic summary marking commands
12440 ;;;
12441
12442 (defvar gnus-summary-marking-alist
12443   '((read gnus-del-mark "d")
12444     (unread gnus-unread-mark "u")
12445     (ticked gnus-ticked-mark "!")
12446     (dormant gnus-dormant-mark "?")
12447     (expirable gnus-expirable-mark "e"))
12448   "An alist of names/marks/keystrokes.")
12449
12450 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12451 (defvar gnus-summary-mark-map)
12452
12453 (defun gnus-summary-make-all-marking-commands ()
12454   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12455   (dolist (elem gnus-summary-marking-alist)
12456     (apply 'gnus-summary-make-marking-command elem)))
12457
12458 (defun gnus-summary-make-marking-command (name mark keystroke)
12459   (let ((map (make-sparse-keymap)))
12460     (define-key gnus-summary-generic-mark-map keystroke map)
12461     (dolist (lway `((next "next" next nil "n")
12462                     (next-unread "next unread" next t "N")
12463                     (prev "previous" prev nil "p")
12464                     (prev-unread "previous unread" prev t "P")
12465                     (nomove "" nil nil ,keystroke)))
12466       (let ((func (gnus-summary-make-marking-command-1
12467                    mark (car lway) lway name)))
12468         (setq func (eval func))
12469         (define-key map (nth 4 lway) func)))))
12470
12471 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12472   `(defun ,(intern
12473             (format "gnus-summary-put-mark-as-%s%s"
12474                     name (if (eq way 'nomove)
12475                              ""
12476                            (concat "-" (symbol-name way)))))
12477      (n)
12478      ,(format
12479        "Mark the current article as %s%s.
12480 If N, the prefix, then repeat N times.
12481 If N is negative, move in reverse order.
12482 The difference between N and the actual number of articles marked is
12483 returned."
12484        name (cadr lway))
12485      (interactive "p")
12486      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12487
12488 (defun gnus-summary-generic-mark (n mark move unread)
12489   "Mark N articles with MARK."
12490   (unless (eq major-mode 'gnus-summary-mode)
12491     (error "This command can only be used in the summary buffer"))
12492   (gnus-summary-show-thread)
12493   (let ((nummove
12494          (cond
12495           ((eq move 'next) 1)
12496           ((eq move 'prev) -1)
12497           (t 0))))
12498     (if (zerop nummove)
12499         (setq n 1)
12500       (when (< n 0)
12501         (setq n (abs n)
12502               nummove (* -1 nummove))))
12503     (while (and (> n 0)
12504                 (gnus-summary-mark-article nil mark)
12505                 (zerop (gnus-summary-next-subject nummove unread t)))
12506       (setq n (1- n)))
12507     (when (/= 0 n)
12508       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12509     (gnus-summary-recenter)
12510     (gnus-summary-position-point)
12511     (gnus-set-mode-line 'summary)
12512     n))
12513
12514 (defun gnus-summary-insert-articles (articles)
12515   (when (setq articles
12516               (gnus-sorted-difference articles
12517                                       (mapcar (lambda (h)
12518                                                 (mail-header-number h))
12519                                               gnus-newsgroup-headers)))
12520     (setq gnus-newsgroup-headers
12521           (gnus-merge 'list
12522                       gnus-newsgroup-headers
12523                       (gnus-fetch-headers articles)
12524                       'gnus-article-sort-by-number))
12525     ;; Suppress duplicates?
12526     (when gnus-suppress-duplicates
12527       (gnus-dup-suppress-articles))
12528
12529     (if (and gnus-fetch-old-headers
12530              (eq gnus-headers-retrieved-by 'nov))
12531         ;; We might want to build some more threads first.
12532         (if (eq gnus-fetch-old-headers 'invisible)
12533             (gnus-build-all-threads)
12534           (gnus-build-old-threads))
12535       ;; Mark the inserted articles that are unread as unread.
12536       (setq gnus-newsgroup-unreads
12537             (gnus-sorted-nunion
12538              gnus-newsgroup-unreads
12539              (gnus-sorted-nintersection
12540               (gnus-list-of-unread-articles gnus-newsgroup-name)
12541               articles)))
12542       ;; Mark the inserted articles as selected so that the information
12543       ;; of the marks having been changed by a user may be updated when
12544       ;; exiting this group.  See `gnus-summary-update-info'.
12545       (dolist (art articles)
12546         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12547     ;; Let the Gnus agent mark articles as read.
12548     (when gnus-agent
12549       (gnus-agent-get-undownloaded-list))
12550     ;; Remove list identifiers from subject
12551     (when gnus-list-identifiers
12552       (gnus-summary-remove-list-identifiers))
12553     ;; First and last article in this newsgroup.
12554     (when gnus-newsgroup-headers
12555       (setq gnus-newsgroup-begin
12556             (mail-header-number (car gnus-newsgroup-headers))
12557             gnus-newsgroup-end
12558             (mail-header-number
12559              (gnus-last-element gnus-newsgroup-headers))))
12560     (when gnus-use-scoring
12561       (gnus-possibly-score-headers))))
12562
12563 (defun gnus-summary-insert-old-articles (&optional all)
12564   "Insert all old articles in this group.
12565 If ALL is non-nil, already read articles become readable.
12566 If ALL is a number, fetch this number of articles."
12567   (interactive "P")
12568   (prog1
12569       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12570             older len)
12571         (setq older
12572               ;; Some nntp servers lie about their active range.  When
12573               ;; this happens, the active range can be in the millions.
12574               ;; Use a compressed range to avoid creating a huge list.
12575               (gnus-range-difference (list gnus-newsgroup-active) old))
12576         (setq len (gnus-range-length older))
12577         (cond
12578          ((null older) nil)
12579          ((numberp all)
12580           (if (< all len)
12581               (let ((older-range (nreverse older)))
12582                 (setq older nil)
12583
12584                 (while (> all 0)
12585                   (let* ((r (pop older-range))
12586                          (min (if (numberp r) r (car r)))
12587                          (max (if (numberp r) r (cdr r))))
12588                     (while (and (<= min max)
12589                                 (> all 0))
12590                       (push max older)
12591                       (setq all (1- all)
12592                             max (1- max))))))
12593             (setq older (gnus-uncompress-range older))))
12594          (all
12595           (setq older (gnus-uncompress-range older)))
12596          (t
12597           (when (and (numberp gnus-large-newsgroup)
12598                    (> len gnus-large-newsgroup))
12599               (let* ((cursor-in-echo-area nil)
12600                      (initial (gnus-parameter-large-newsgroup-initial
12601                                gnus-newsgroup-name))
12602                      (input
12603                       (read-string
12604                        (format
12605                         "How many articles from %s (%s %d): "
12606                         (gnus-group-decoded-name gnus-newsgroup-name)
12607                         (if initial "max" "default")
12608                         len)
12609                        (if initial
12610                            (cons (number-to-string initial)
12611                                  0)))))
12612                 (unless (string-match "^[ \t]*$" input)
12613                   (setq all (string-to-number input))
12614                   (if (< all len)
12615                       (let ((older-range (nreverse older)))
12616                         (setq older nil)
12617
12618                         (while (> all 0)
12619                           (let* ((r (pop older-range))
12620                                  (min (if (numberp r) r (car r)))
12621                                  (max (if (numberp r) r (cdr r))))
12622                             (while (and (<= min max)
12623                                         (> all 0))
12624                               (push max older)
12625                               (setq all (1- all)
12626                                     max (1- max))))))))))
12627           (setq older (gnus-uncompress-range older))))
12628         (if (not older)
12629             (message "No old news.")
12630           (gnus-summary-insert-articles older)
12631           (gnus-summary-limit (gnus-sorted-nunion old older))))
12632     (gnus-summary-position-point)))
12633
12634 (defun gnus-summary-insert-new-articles ()
12635   "Insert all new articles in this group."
12636   (interactive)
12637   (prog1
12638       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12639             (old-high (cdr gnus-newsgroup-active))
12640             (nnmail-fetched-sources (list t))
12641             i new)
12642         (setq gnus-newsgroup-active
12643               (gnus-copy-sequence
12644                (gnus-activate-group gnus-newsgroup-name 'scan)))
12645         (setq i (cdr gnus-newsgroup-active))
12646         (while (> i old-high)
12647           (push i new)
12648           (decf i))
12649         (if (not new)
12650             (message "No gnus is bad news")
12651           (gnus-summary-insert-articles new)
12652           (setq gnus-newsgroup-unreads
12653                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12654           (gnus-summary-limit (gnus-sorted-nunion old new))))
12655     (gnus-summary-position-point)))
12656
12657 ;;; Bookmark support for Gnus.
12658 (declare-function bookmark-make-record-default
12659                   "bookmark" (&optional no-file no-context posn))
12660 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12661 (declare-function bookmark-default-handler "bookmark" (bmk))
12662 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12663 (defvar bookmark-yank-point)
12664 (defvar bookmark-current-buffer)
12665
12666 (defun gnus-summary-bookmark-make-record ()
12667   "Make a bookmark entry for a Gnus summary buffer."
12668   (let (pos buf)
12669     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12670       (save-restriction              ; FIXME is it necessary to widen?
12671         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12672       (setq buf "art") ; We are recording bookmark from article buffer.
12673       (setq bookmark-yank-point (point))
12674       (setq bookmark-current-buffer (current-buffer))
12675       (gnus-article-show-summary))      ; Go back in summary buffer.
12676     ;; We are now recording bookmark from summary buffer.
12677     (unless buf (setq buf "sum"))
12678     (let* ((subject (elt (gnus-summary-article-header) 1))
12679            (grp     (car gnus-article-current))
12680            (art     (cdr gnus-article-current))
12681            (head    (gnus-summary-article-header art))
12682            (id      (mail-header-id head)))
12683       `(,subject
12684         ,@(condition-case nil
12685               (bookmark-make-record-default 'no-file 'no-context pos)
12686             (wrong-number-of-arguments
12687              (bookmark-make-record-default 'point-only)))
12688         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12689         (group . ,grp) (article . ,art)
12690         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12691
12692 ;;;###autoload
12693 (defun gnus-summary-bookmark-jump (bookmark)
12694   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12695 BOOKMARK is a bookmark name or a bookmark record."
12696   (let ((group    (bookmark-prop-get bookmark 'group))
12697         (article  (bookmark-prop-get bookmark 'article))
12698         (id       (bookmark-prop-get bookmark 'message-id))
12699         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12700     (gnus-fetch-group group (list article))
12701     (gnus-summary-insert-cached-articles)
12702     (gnus-summary-goto-article id nil 'force)
12703     ;; FIXME we have to wait article buffer is ready (only large buffer)
12704     ;; Is there a better solution to know that?
12705     ;; If we don't wait `bookmark-default-handler' will have no chance
12706     ;; to set position. However there is no error, just wrong pos.
12707     (sit-for 1)
12708     (when (string= buf "Gnus-art")
12709       (other-window 1))
12710     (bookmark-default-handler
12711      `(""
12712        (buffer . ,(current-buffer))
12713        . ,(bookmark-get-bookmark-record bookmark)))))
12714
12715 (gnus-summary-make-all-marking-commands)
12716
12717 (gnus-ems-redefine)
12718
12719 (provide 'gnus-sum)
12720
12721 (run-hooks 'gnus-sum-load-hook)
12722
12723 ;; Local Variables:
12724 ;; coding: iso-8859-1
12725 ;; End:
12726
12727 ;;; gnus-sum.el ends here