37cf4a1147206c92a60d008cc4f0b3250f66702d
[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 (defvar gnus-auto-center-group t
455   "*If non-nil, always center the group buffer.")
456
457 (defcustom gnus-show-all-headers nil
458   "*If non-nil, don't hide any headers."
459   :group 'gnus-article-hiding
460   :group 'gnus-article-headers
461   :type 'boolean)
462
463 (defcustom gnus-summary-ignore-duplicates nil
464   "*If non-nil, ignore articles with identical Message-ID headers."
465   :group 'gnus-summary
466   :type 'boolean)
467
468 (defcustom gnus-single-article-buffer nil
469   "*If non-nil, display all articles in the same buffer.
470 If nil, each group will get its own article buffer."
471   :version "24.1"
472   :group 'gnus-article-various
473   :type 'boolean)
474
475 (defcustom gnus-break-pages t
476   "*If non-nil, do page breaking on articles.
477 The page delimiter is specified by the `gnus-page-delimiter'
478 variable."
479   :group 'gnus-article-various
480   :type 'boolean)
481
482 (defcustom gnus-move-split-methods nil
483   "*Variable used to suggest where articles are to be moved to.
484 It uses the same syntax as the `gnus-split-methods' variable.
485 However, whereas `gnus-split-methods' specifies file names as targets,
486 this variable specifies group names."
487   :group 'gnus-summary-mail
488   :type '(repeat (choice (list :value (fun) function)
489                          (cons :value ("" "") regexp (repeat string))
490                          (sexp :value nil))))
491
492 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
493   "Function used to compute default prefix for article move/copy/etc prompts.
494 The function should take one argument, a group name, and return a
495 string with the suggested prefix."
496   :group 'gnus-summary-mail
497   :type 'function)
498
499 ;; FIXME: Although the custom type is `character' for the following variables,
500 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
501
502 (defcustom gnus-unread-mark ?           ;Whitespace
503   "*Mark used for unread articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-ticked-mark ?!
508   "*Mark used for ticked articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-dormant-mark ??
513   "*Mark used for dormant articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-del-mark ?r
518   "*Mark used for del'd articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-read-mark ?R
523   "*Mark used for read articles."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-expirable-mark ?E
528   "*Mark used for expirable articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-killed-mark ?K
533   "*Mark used for killed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-spam-mark ?$
538   "*Mark used for spam articles."
539   :version "22.1"
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-kill-file-mark ?X
544   "*Mark used for articles killed by kill files."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-low-score-mark ?Y
549   "*Mark used for articles with a low score."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-catchup-mark ?C
554   "*Mark used for articles that are caught up."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-replied-mark ?A
559   "*Mark used for articles that have been replied to."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-forwarded-mark ?F
564   "*Mark used for articles that have been forwarded."
565   :version "22.1"
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-recent-mark ?N
570   "*Mark used for articles that are recent."
571   :version "22.1"
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-cached-mark ?*
576   "*Mark used for articles that are in the cache."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-saved-mark ?S
581   "*Mark used for articles that have been saved."
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-unseen-mark ?.
586   "*Mark used for articles that haven't been seen."
587   :version "22.1"
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-no-mark ?               ;Whitespace
592   "*Mark used for articles that have no other secondary mark."
593   :version "22.1"
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-ancient-mark ?O
598   "*Mark used for ancient articles."
599   :group 'gnus-summary-marks
600   :type 'character)
601
602 (defcustom gnus-sparse-mark ?Q
603   "*Mark used for sparsely reffed articles."
604   :group 'gnus-summary-marks
605   :type 'character)
606
607 (defcustom gnus-canceled-mark ?G
608   "*Mark used for canceled articles."
609   :group 'gnus-summary-marks
610   :type 'character)
611
612 (defcustom gnus-duplicate-mark ?M
613   "*Mark used for duplicate articles."
614   :group 'gnus-summary-marks
615   :type 'character)
616
617 (defcustom gnus-undownloaded-mark ?-
618   "*Mark used for articles that weren't downloaded."
619   :version "22.1"
620   :group 'gnus-summary-marks
621   :type 'character)
622
623 (defcustom gnus-downloaded-mark ?+
624   "*Mark used for articles that were downloaded."
625   :group 'gnus-summary-marks
626   :type 'character)
627
628 (defcustom gnus-downloadable-mark ?%
629   "*Mark used for articles that are to be downloaded."
630   :group 'gnus-summary-marks
631   :type 'character)
632
633 (defcustom gnus-unsendable-mark ?=
634   "*Mark used for articles that won't be sent."
635   :group 'gnus-summary-marks
636   :type 'character)
637
638 (defcustom gnus-score-over-mark ?+
639   "*Score mark used for articles with high scores."
640   :group 'gnus-summary-marks
641   :type 'character)
642
643 (defcustom gnus-score-below-mark ?-
644   "*Score mark used for articles with low scores."
645   :group 'gnus-summary-marks
646   :type 'character)
647
648 (defcustom gnus-empty-thread-mark ?     ;Whitespace
649   "*There is no thread under the article."
650   :group 'gnus-summary-marks
651   :type 'character)
652
653 (defcustom gnus-not-empty-thread-mark ?=
654   "*There is a thread under the article."
655   :group 'gnus-summary-marks
656   :type 'character)
657
658 (defcustom gnus-view-pseudo-asynchronously nil
659   "*If non-nil, Gnus will view pseudo-articles asynchronously."
660   :group 'gnus-extract-view
661   :type 'boolean)
662
663 (defcustom gnus-auto-expirable-marks
664   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
665         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
666         gnus-duplicate-mark)
667   "*The list of marks converted into expiration if a group is auto-expirable."
668   :version "24.1"
669   :group 'gnus-summary
670   :type '(repeat character))
671
672 (defcustom gnus-inhibit-user-auto-expire t
673   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
674   :version "21.1"
675   :group 'gnus-summary
676   :type 'boolean)
677
678 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
679   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
680 If nil, the expirable marks will be unchanged except that the marks
681 will be removed when copying or moving articles to a group that has
682 not turned auto-expire on.  If non-nil, articles that have been read
683 will be marked as expirable when being copied or moved to a group in
684 which auto-expire is turned on."
685   :version "23.2"
686   :type 'boolean
687   :group 'gnus-summary-marks)
688
689 (defcustom gnus-view-pseudos nil
690   "*If `automatic', pseudo-articles will be viewed automatically.
691 If `not-confirm', pseudos will be viewed automatically, and the user
692 will not be asked to confirm the command."
693   :group 'gnus-extract-view
694   :type '(choice (const :tag "off" nil)
695                  (const automatic)
696                  (const not-confirm)))
697
698 (defcustom gnus-view-pseudos-separately t
699   "*If non-nil, one pseudo-article will be created for each file to be viewed.
700 If nil, all files that use the same viewing command will be given as a
701 list of parameters to that command."
702   :group 'gnus-extract-view
703   :type 'boolean)
704
705 (defcustom gnus-insert-pseudo-articles t
706   "*If non-nil, insert pseudo-articles when decoding articles."
707   :group 'gnus-extract-view
708   :type 'boolean)
709
710 (defcustom gnus-summary-dummy-line-format
711   "   %(:                             :%) %S\n"
712   "*The format specification for the dummy roots in the summary buffer.
713 It works along the same lines as a normal formatting string,
714 with some simple extensions.
715
716 %S  The subject
717
718 General format specifiers can also be used.
719 See `(gnus)Formatting Variables'."
720   :link '(custom-manual "(gnus)Formatting Variables")
721   :group 'gnus-threading
722   :type 'string)
723
724 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
725   "*The format specification for the summary mode line.
726 It works along the same lines as a normal formatting string,
727 with some simple extensions:
728
729 %G  Group name
730 %p  Unprefixed group name
731 %A  Current article number
732 %z  Current article score
733 %V  Gnus version
734 %U  Number of unread articles in the group
735 %e  Number of unselected articles in the group
736 %Z  A string with unread/unselected article counts
737 %g  Shortish group name
738 %S  Subject of the current article
739 %u  User-defined spec
740 %s  Current score file name
741 %d  Number of dormant articles
742 %r  Number of articles that have been marked as read in this session
743 %E  Number of articles expunged by the score files"
744   :group 'gnus-summary-format
745   :type 'string)
746
747 (defcustom gnus-list-identifiers nil
748   "Regexp that matches list identifiers to be removed from subject.
749 This can also be a list of regexps."
750   :version "21.1"
751   :group 'gnus-summary-format
752   :group 'gnus-article-hiding
753   :type '(choice (const :tag "none" nil)
754                  (regexp :value ".*")
755                  (repeat :value (".*") regexp)))
756
757 (defcustom gnus-summary-mark-below 0
758   "*Mark all articles with a score below this variable as read.
759 This variable is local to each summary buffer and usually set by the
760 score file."
761   :group 'gnus-score-default
762   :type 'integer)
763
764 (defun gnus-widget-reversible-match (widget value)
765   "Ignoring WIDGET, convert VALUE to internal form.
766 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
767   ;; (debug value)
768   (or (symbolp value)
769       (and (listp value)
770            (eq (length value) 2)
771            (eq (nth 0 value) 'not)
772            (symbolp (nth 1 value)))))
773
774 (defun gnus-widget-reversible-to-internal (widget value)
775   "Ignoring WIDGET, convert VALUE to internal form.
776 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
777 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
778   ;; (debug value)
779   (if (atom value)
780       (list value nil)
781     (list (nth 1 value) t)))
782
783 (defun gnus-widget-reversible-to-external (widget value)
784   "Ignoring WIDGET, convert VALUE to external form.
785 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
786 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
787   ;; (debug value)
788   (if (nth 1 value)
789       (list 'not (nth 0 value))
790     (nth 0 value)))
791
792 (define-widget 'gnus-widget-reversible 'group
793   "A `group' that convert values."
794   :match 'gnus-widget-reversible-match
795   :value-to-internal 'gnus-widget-reversible-to-internal
796   :value-to-external 'gnus-widget-reversible-to-external)
797
798 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
799   "*List of functions used for sorting articles in the summary buffer.
800
801 Each function takes two articles and returns non-nil if the first
802 article should be sorted before the other.  If you use more than one
803 function, the primary sort function should be the last.  You should
804 probably always include `gnus-article-sort-by-number' in the list of
805 sorting functions -- preferably first.  Also note that sorting by date
806 is often much slower than sorting by number, and the sorting order is
807 very similar.  (Sorting by date means sorting by the time the message
808 was sent, sorting by number means sorting by arrival time.)
809
810 Each item can also be a list `(not F)' where F is a function;
811 this reverses the sort order.
812
813 Ready-made functions include `gnus-article-sort-by-number',
814 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
815 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
816 and `gnus-article-sort-by-score'.
817
818 When threading is turned on, the variable `gnus-thread-sort-functions'
819 controls how articles are sorted."
820   :group 'gnus-summary-sort
821   :type '(repeat (gnus-widget-reversible
822                   (choice (function-item gnus-article-sort-by-number)
823                           (function-item gnus-article-sort-by-author)
824                           (function-item gnus-article-sort-by-subject)
825                           (function-item gnus-article-sort-by-date)
826                           (function-item gnus-article-sort-by-score)
827                           (function-item gnus-article-sort-by-random)
828                           (function :tag "other"))
829                   (boolean :tag "Reverse order"))))
830
831
832 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
833   "*List of functions used for sorting threads in the summary buffer.
834 By default, threads are sorted by article number.
835
836 Each function takes two threads and returns non-nil if the first
837 thread should be sorted before the other.  If you use more than one
838 function, the primary sort function should be the last.  You should
839 probably always include `gnus-thread-sort-by-number' in the list of
840 sorting functions -- preferably first.  Also note that sorting by date
841 is often much slower than sorting by number, and the sorting order is
842 very similar.  (Sorting by date means sorting by the time the message
843 was sent, sorting by number means sorting by arrival time.)
844
845 Each list item can also be a list `(not F)' where F is a
846 function; this specifies reversed sort order.
847
848 Ready-made functions include `gnus-thread-sort-by-number',
849 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
850 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
851 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
852 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
853 and `gnus-thread-sort-by-total-score' (see
854 `gnus-thread-score-function').
855
856 When threading is turned off, the variable
857 `gnus-article-sort-functions' controls how articles are sorted."
858   :group 'gnus-summary-sort
859   :type '(repeat
860           (gnus-widget-reversible
861            (choice (function-item gnus-thread-sort-by-number)
862                    (function-item gnus-thread-sort-by-author)
863                    (function-item gnus-thread-sort-by-recipient)
864                    (function-item gnus-thread-sort-by-subject)
865                    (function-item gnus-thread-sort-by-date)
866                    (function-item gnus-thread-sort-by-score)
867                    (function-item gnus-thread-sort-by-most-recent-number)
868                    (function-item gnus-thread-sort-by-most-recent-date)
869                    (function-item gnus-thread-sort-by-random)
870                    (function-item gnus-thread-sort-by-total-score)
871                    (function :tag "other"))
872            (boolean :tag "Reverse order"))))
873
874 (defcustom gnus-thread-score-function '+
875   "*Function used for calculating the total score of a thread.
876
877 The function is called with the scores of the article and each
878 subthread and should then return the score of the thread.
879
880 Some functions you can use are `+', `max', or `min'."
881   :group 'gnus-summary-sort
882   :type 'function)
883
884 (defcustom gnus-summary-expunge-below nil
885   "All articles that have a score less than this variable will be expunged.
886 This variable is local to the summary buffers."
887   :group 'gnus-score-default
888   :type '(choice (const :tag "off" nil)
889                  integer))
890
891 (defcustom gnus-thread-expunge-below nil
892   "All threads that have a total score less than this variable will be expunged.
893 See `gnus-thread-score-function' for en explanation of what a
894 \"thread score\" is.
895
896 This variable is local to the summary buffers."
897   :group 'gnus-threading
898   :group 'gnus-score-default
899   :type '(choice (const :tag "off" nil)
900                  integer))
901
902 (defcustom gnus-summary-mode-hook nil
903   "*A hook for Gnus summary mode.
904 This hook is run before any variables are set in the summary buffer."
905   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
906   :group 'gnus-summary-various
907   :type 'hook)
908
909 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
910 (when (featurep 'xemacs)
911   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
912   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
913   (add-hook 'gnus-summary-mode-hook
914             'gnus-xmas-switch-horizontal-scrollbar-off))
915
916 (defcustom gnus-summary-menu-hook nil
917   "*Hook run after the creation of the summary mode menu."
918   :group 'gnus-summary-visual
919   :type 'hook)
920
921 (defcustom gnus-summary-exit-hook nil
922   "*A hook called on exit from the summary buffer.
923 It will be called with point in the group buffer."
924   :group 'gnus-summary-exit
925   :type 'hook)
926
927 (defcustom gnus-summary-prepare-hook nil
928   "*A hook called after the summary buffer has been generated.
929 If you want to modify the summary buffer, you can use this hook."
930   :group 'gnus-summary-various
931   :type 'hook)
932
933 (defcustom gnus-summary-prepared-hook nil
934   "*A hook called as the last thing after the summary buffer has been generated."
935   :group 'gnus-summary-various
936   :type 'hook)
937
938 (defcustom gnus-summary-generate-hook nil
939   "*A hook run just before generating the summary buffer.
940 This hook is commonly used to customize threading variables and the
941 like."
942   :group 'gnus-summary-various
943   :type 'hook)
944
945 (defcustom gnus-select-group-hook nil
946   "*A hook called when a newsgroup is selected.
947
948 If you'd like to simplify subjects like the
949 `gnus-summary-next-same-subject' command does, you can use the
950 following hook:
951
952  (add-hook gnus-select-group-hook
953            (lambda ()
954              (mapcar (lambda (header)
955                        (mail-header-set-subject
956                         header
957                         (gnus-simplify-subject
958                          (mail-header-subject header) 're-only)))
959                      gnus-newsgroup-headers)))"
960   :group 'gnus-group-select
961   :type 'hook)
962
963 (defcustom gnus-select-article-hook nil
964   "*A hook called when an article is selected."
965   :group 'gnus-summary-choose
966   :options '(gnus-agent-fetch-selected-article)
967   :type 'hook)
968
969 (defcustom gnus-visual-mark-article-hook
970   (list 'gnus-highlight-selected-summary)
971   "*Hook run after selecting an article in the summary buffer.
972 It is meant to be used for highlighting the article in some way.  It
973 is not run if `gnus-visual' is nil."
974   :group 'gnus-summary-visual
975   :type 'hook)
976
977 (defcustom gnus-parse-headers-hook nil
978   "*A hook called before parsing the headers."
979   :group 'gnus-various
980   :type 'hook)
981
982 (defcustom gnus-exit-group-hook nil
983   "*A hook called when exiting summary mode.
984 This hook is not called from the non-updating exit commands like `Q'."
985   :group 'gnus-various
986   :type 'hook)
987
988 (defcustom gnus-summary-update-hook
989   (list 'gnus-summary-highlight-line)
990   "*A hook called when a summary line is changed.
991 The hook will not be called if `gnus-visual' is nil.
992
993 The default function `gnus-summary-highlight-line' will
994 highlight the line according to the `gnus-summary-highlight'
995 variable."
996   :group 'gnus-summary-visual
997   :type 'hook)
998
999 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1000   "*A hook called when an article is selected for the first time.
1001 The hook is intended to mark an article as read (or unread)
1002 automatically when it is selected."
1003   :group 'gnus-summary-choose
1004   :type 'hook)
1005
1006 (defcustom gnus-group-no-more-groups-hook nil
1007   "*A hook run when returning to group mode having no more (unread) groups."
1008   :group 'gnus-group-select
1009   :type 'hook)
1010
1011 (defcustom gnus-ps-print-hook nil
1012   "*A hook run before ps-printing something from Gnus."
1013   :group 'gnus-summary
1014   :type 'hook)
1015
1016 (defcustom gnus-summary-article-move-hook nil
1017   "*A hook called after an article is moved, copied, respooled, or crossposted."
1018   :version "22.1"
1019   :group 'gnus-summary
1020   :type 'hook)
1021
1022 (defcustom gnus-summary-article-delete-hook nil
1023   "*A hook called after an article is deleted."
1024   :version "22.1"
1025   :group 'gnus-summary
1026   :type 'hook)
1027
1028 (defcustom gnus-summary-article-expire-hook nil
1029   "*A hook called after an article is expired."
1030   :version "22.1"
1031   :group 'gnus-summary
1032   :type 'hook)
1033
1034 (defcustom gnus-summary-display-arrow
1035   (and (fboundp 'display-graphic-p)
1036        (display-graphic-p))
1037   "*If non-nil, display an arrow highlighting the current article."
1038   :version "22.1"
1039   :group 'gnus-summary
1040   :type 'boolean)
1041
1042 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1043   "Face used for highlighting the current article in the summary buffer."
1044   :group 'gnus-summary-visual
1045   :type 'face)
1046
1047 (defvar gnus-tmp-downloaded nil)
1048
1049 (defcustom gnus-summary-highlight
1050   '(((eq mark gnus-canceled-mark)
1051      . gnus-summary-cancelled)
1052     ((and uncached (> score default-high))
1053      . gnus-summary-high-undownloaded)
1054     ((and uncached (< score default-low))
1055      . gnus-summary-low-undownloaded)
1056     (uncached
1057      . gnus-summary-normal-undownloaded)
1058     ((and (> score default-high)
1059           (or (eq mark gnus-dormant-mark)
1060               (eq mark gnus-ticked-mark)))
1061      . gnus-summary-high-ticked)
1062     ((and (< score default-low)
1063           (or (eq mark gnus-dormant-mark)
1064               (eq mark gnus-ticked-mark)))
1065      . gnus-summary-low-ticked)
1066     ((or (eq mark gnus-dormant-mark)
1067          (eq mark gnus-ticked-mark))
1068      . gnus-summary-normal-ticked)
1069     ((and (> score default-high) (eq mark gnus-ancient-mark))
1070      . gnus-summary-high-ancient)
1071     ((and (< score default-low) (eq mark gnus-ancient-mark))
1072      . gnus-summary-low-ancient)
1073     ((eq mark gnus-ancient-mark)
1074      . gnus-summary-normal-ancient)
1075     ((and (> score default-high) (eq mark gnus-unread-mark))
1076      . gnus-summary-high-unread)
1077     ((and (< score default-low) (eq mark gnus-unread-mark))
1078      . gnus-summary-low-unread)
1079     ((eq mark gnus-unread-mark)
1080      . gnus-summary-normal-unread)
1081     ((> score default-high)
1082      . gnus-summary-high-read)
1083     ((< score default-low)
1084      . gnus-summary-low-read)
1085     (t
1086      . gnus-summary-normal-read))
1087   "*Controls the highlighting of summary buffer lines.
1088
1089 A list of (FORM . FACE) pairs.  When deciding how a particular
1090 summary line should be displayed, each form is evaluated.  The content
1091 of the face field after the first true form is used.  You can change
1092 how those summary lines are displayed, by editing the face field.
1093
1094 You can use the following variables in the FORM field.
1095
1096 score:        The article's score.
1097 default:      The default article score.
1098 default-high: The default score for high scored articles.
1099 default-low:  The default score for low scored articles.
1100 below:        The score below which articles are automatically marked as read.
1101 mark:         The article's mark.
1102 uncached:     Non-nil if the article is uncached."
1103   :group 'gnus-summary-visual
1104   :type '(repeat (cons (sexp :tag "Form" nil)
1105                        face)))
1106 (put 'gnus-summary-highlight 'risky-local-variable t)
1107
1108 (defcustom gnus-alter-header-function nil
1109   "Function called to allow alteration of article header structures.
1110 The function is called with one parameter, the article header vector,
1111 which it may alter in any way."
1112   :type '(choice (const :tag "None" nil)
1113                  function)
1114   :group 'gnus-summary)
1115
1116 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1117   "Function used to decode a string with encoded words.")
1118
1119 (defvar gnus-decode-encoded-address-function
1120   'mail-decode-encoded-address-string
1121   "Function used to decode addresses with encoded words.")
1122
1123 (defcustom gnus-extra-headers '(To Newsgroups)
1124   "*Extra headers to parse."
1125   :version "21.1"
1126   :group 'gnus-summary
1127   :type '(repeat symbol))
1128
1129 (defcustom gnus-ignored-from-addresses
1130   (and user-mail-address
1131        (not (string= user-mail-address ""))
1132        (regexp-quote user-mail-address))
1133   "*From headers that may be suppressed in favor of To headers.
1134 This can be a regexp or a list of regexps."
1135   :version "21.1"
1136   :group 'gnus-summary
1137   :type '(choice regexp
1138                  (repeat :tag "Regexp List" regexp)))
1139
1140 (defsubst gnus-ignored-from-addresses ()
1141   (gmm-regexp-concat gnus-ignored-from-addresses))
1142
1143 (defcustom gnus-summary-to-prefix "-> "
1144   "*String prefixed to the To field in the summary line when
1145 using `gnus-ignored-from-addresses'."
1146   :version "22.1"
1147   :group 'gnus-summary
1148   :type 'string)
1149
1150 (defcustom gnus-summary-newsgroup-prefix "=> "
1151   "*String prefixed to the Newsgroup field in the summary
1152 line when using `gnus-ignored-from-addresses'."
1153   :version "22.1"
1154   :group 'gnus-summary
1155   :type 'string)
1156
1157 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1158   "List of charsets that should be ignored.
1159 When these charsets are used in the \"charset\" parameter, the
1160 default charset will be used instead."
1161   :version "21.1"
1162   :type '(repeat symbol)
1163   :group 'gnus-charset)
1164
1165 (defcustom gnus-newsgroup-maximum-articles nil
1166   "The maximum number of articles a newsgroup.
1167 If this is a number, old articles in a newsgroup exceeding this number
1168 are silently ignored.  If it is nil, no article is ignored.  Note that
1169 setting this variable to a number might prevent you from reading very
1170 old articles."
1171   :group 'gnus-group-select
1172   :version "22.2"
1173   :type '(choice (const :tag "No limit" nil)
1174                  integer))
1175
1176 (gnus-define-group-parameter
1177  ignored-charsets
1178  :type list
1179  :function-document
1180  "Return the ignored charsets of GROUP."
1181  :variable gnus-group-ignored-charsets-alist
1182  :variable-default
1183  '(("alt\\.chinese\\.text" iso-8859-1))
1184  :variable-document
1185  "Alist of regexps (to match group names) and charsets that should be ignored.
1186 When these charsets are used in the \"charset\" parameter, the
1187 default charset will be used instead."
1188  :variable-group gnus-charset
1189  :variable-type '(repeat (cons (regexp :tag "Group")
1190                                (repeat symbol)))
1191  :parameter-type '(choice :tag "Ignored charsets"
1192                           :value nil
1193                           (repeat (symbol)))
1194  :parameter-document       "\
1195 List of charsets that should be ignored.
1196
1197 When these charsets are used in the \"charset\" parameter, the
1198 default charset will be used instead.")
1199
1200 (defcustom gnus-group-highlight-words-alist nil
1201   "Alist of group regexps and highlight regexps.
1202 This variable uses the same syntax as `gnus-emphasis-alist'."
1203   :version "21.1"
1204   :type '(repeat (cons (regexp :tag "Group")
1205                        (repeat (list (regexp :tag "Highlight regexp")
1206                                      (number :tag "Group for entire word" 0)
1207                                      (number :tag "Group for displayed part" 0)
1208                                      (symbol :tag "Face"
1209                                              gnus-emphasis-highlight-words)))))
1210   :group 'gnus-summary-visual)
1211
1212 (defcustom gnus-summary-show-article-charset-alist
1213   nil
1214   "Alist of number and charset.
1215 The article will be shown with the charset corresponding to the
1216 numbered argument.
1217 For example: ((1 . cn-gb-2312) (2 . big5))."
1218   :version "21.1"
1219   :type '(repeat (cons (number :tag "Argument" 1)
1220                        (symbol :tag "Charset")))
1221   :group 'gnus-charset)
1222
1223 (defcustom gnus-preserve-marks t
1224   "Whether marks are preserved when moving, copying and respooling messages."
1225   :version "21.1"
1226   :type 'boolean
1227   :group 'gnus-summary-marks)
1228
1229 (defcustom gnus-propagate-marks t
1230   "If non-nil, do not propagate marks to the backends."
1231   :version "23.1" ;; No Gnus
1232   :type 'boolean
1233   :group 'gnus-summary-marks)
1234
1235 (defcustom gnus-alter-articles-to-read-function nil
1236   "Function to be called to alter the list of articles to be selected."
1237   :type '(choice (const nil) function)
1238   :group 'gnus-summary)
1239
1240 (defcustom gnus-orphan-score nil
1241   "*All orphans get this score added.  Set in the score file."
1242   :group 'gnus-score-default
1243   :type '(choice (const nil)
1244                  integer))
1245
1246 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1247   "*A regexp to match MIME parts when saving multiple parts of a
1248 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1249 This regexp will be used by default when prompting the user for which
1250 type of files to save."
1251   :group 'gnus-summary
1252   :type 'regexp)
1253
1254 (defcustom gnus-read-all-available-headers nil
1255   "Whether Gnus should parse all headers made available to it.
1256 This is mostly relevant for slow back ends where the user may
1257 wish to widen the summary buffer to include all headers
1258 that were fetched."
1259   :version "22.1"
1260   :group 'gnus-summary
1261   :type '(choice boolean regexp))
1262
1263 (defcustom gnus-summary-pipe-output-default-command nil
1264   "Command (and optional arguments) used to pipe article to subprocess.
1265 This will be used as the default command if it is non-nil.  The value
1266 will be updated if you modify it when executing the command
1267 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1268   :version "23.1" ;; No Gnus
1269   :group 'gnus-summary
1270   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1271
1272 (defcustom gnus-summary-muttprint-program "muttprint"
1273   "Command (and optional arguments) used to run Muttprint.
1274 The value will be updated if you modify it when executing the command
1275 `gnus-summary-muttprint'."
1276   :version "22.1"
1277   :group 'gnus-summary
1278   :type 'string)
1279
1280 (defcustom gnus-article-loose-mime t
1281   "If non-nil, don't require MIME-Version header.
1282 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1283 supply the MIME-Version header or deliberately strip it from the mail.
1284 If non-nil (the default), Gnus will treat some articles as MIME
1285 even if the MIME-Version header is missing."
1286   :version "22.1"
1287   :type 'boolean
1288   :group 'gnus-article-mime)
1289
1290 (defcustom gnus-article-emulate-mime t
1291   "If non-nil, use MIME emulation for uuencode and the like.
1292 This means that Gnus will search message bodies for text that look
1293 like uuencoded bits, yEncoded bits, and so on, and present that using
1294 the normal Gnus MIME machinery."
1295   :version "22.1"
1296   :type 'boolean
1297   :group 'gnus-article-mime)
1298
1299 ;;; Internal variables
1300
1301 (defvar gnus-summary-display-cache nil)
1302 (defvar gnus-article-mime-handles nil)
1303 (defvar gnus-article-decoded-p nil)
1304 (defvar gnus-article-charset nil)
1305 (defvar gnus-article-ignored-charsets nil)
1306 (defvar gnus-scores-exclude-files nil)
1307 (defvar gnus-page-broken nil)
1308
1309 (defvar gnus-original-article nil)
1310 (defvar gnus-article-internal-prepare-hook nil)
1311 (defvar gnus-newsgroup-process-stack nil)
1312
1313 (defvar gnus-thread-indent-array nil)
1314 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1315 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1316   "Function called to sort the articles within a thread after it has been gathered together.")
1317
1318 (defvar gnus-summary-save-parts-type-history nil)
1319 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1320
1321 ;; Avoid highlighting in kill files.
1322 (defvar gnus-summary-inhibit-highlight nil)
1323 (defvar gnus-newsgroup-selected-overlay nil)
1324 (defvar gnus-inhibit-limiting nil)
1325 (defvar gnus-newsgroup-adaptive-score-file nil)
1326 (defvar gnus-current-score-file nil)
1327 (defvar gnus-current-move-group nil)
1328 (defvar gnus-current-copy-group nil)
1329 (defvar gnus-current-crosspost-group nil)
1330 (defvar gnus-newsgroup-display nil)
1331
1332 (defvar gnus-newsgroup-dependencies nil)
1333 (defvar gnus-newsgroup-adaptive nil)
1334 (defvar gnus-summary-display-article-function nil)
1335 (defvar gnus-summary-highlight-line-function nil
1336   "Function called after highlighting a summary line.")
1337
1338 (defvar gnus-summary-line-format-alist
1339   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1340     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1341     (?s gnus-tmp-subject-or-nil ?s)
1342     (?n gnus-tmp-name ?s)
1343     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1344         ?s)
1345     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1346             gnus-tmp-from) ?s)
1347     (?F gnus-tmp-from ?s)
1348     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1349     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1350     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1351     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1352     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1353     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1354     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1355     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1356     (?L gnus-tmp-lines ?s)
1357     (?O gnus-tmp-downloaded ?c)
1358     (?I gnus-tmp-indentation ?s)
1359     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1360     (?R gnus-tmp-replied ?c)
1361     (?\[ gnus-tmp-opening-bracket ?c)
1362     (?\] gnus-tmp-closing-bracket ?c)
1363     (?\> (make-string gnus-tmp-level ? ) ?s)
1364     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1365     (?i gnus-tmp-score ?d)
1366     (?z gnus-tmp-score-char ?c)
1367     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1368     (?U gnus-tmp-unread ?c)
1369     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1370         ?s)
1371     (?t (gnus-summary-number-of-articles-in-thread
1372          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1373         ?d)
1374     (?e (gnus-summary-number-of-articles-in-thread
1375          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1376         ?c)
1377     (?u gnus-tmp-user-defined ?s)
1378     (?P (gnus-pick-line-number) ?d)
1379     (?B gnus-tmp-thread-tree-header-string ?s)
1380     (user-date (gnus-user-date
1381                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1382   "An alist of format specifications that can appear in summary lines.
1383 These are paired with what variables they correspond with, along with
1384 the type of the variable (string, integer, character, etc).")
1385
1386 (defvar gnus-summary-dummy-line-format-alist
1387   `((?S gnus-tmp-subject ?s)
1388     (?N gnus-tmp-number ?d)
1389     (?u gnus-tmp-user-defined ?s)))
1390
1391 (defvar gnus-summary-mode-line-format-alist
1392   `((?G gnus-tmp-group-name ?s)
1393     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1394     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1395     (?A gnus-tmp-article-number ?d)
1396     (?Z gnus-tmp-unread-and-unselected ?s)
1397     (?V gnus-version ?s)
1398     (?U gnus-tmp-unread-and-unticked ?d)
1399     (?S gnus-tmp-subject ?s)
1400     (?e gnus-tmp-unselected ?d)
1401     (?u gnus-tmp-user-defined ?s)
1402     (?d (length gnus-newsgroup-dormant) ?d)
1403     (?t (length gnus-newsgroup-marked) ?d)
1404     (?h (length gnus-newsgroup-spam-marked) ?d)
1405     (?r (length gnus-newsgroup-reads) ?d)
1406     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1407     (?E gnus-newsgroup-expunged-tally ?d)
1408     (?s (gnus-current-score-file-nondirectory) ?s)))
1409
1410 ;; This is here rather than in gnus-art for compilation reasons.
1411 (defvar gnus-article-mode-line-format-alist
1412   (nconc '((?w (gnus-article-wash-status) ?s)
1413            (?m (gnus-article-mime-part-status) ?s))
1414          gnus-summary-mode-line-format-alist))
1415
1416 (defvar gnus-last-search-regexp nil
1417   "Default regexp for article search command.")
1418
1419 (defvar gnus-last-shell-command nil
1420   "Default shell command on article.")
1421
1422 (defvar gnus-newsgroup-agentized nil
1423   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1424 (defvar gnus-newsgroup-begin nil)
1425 (defvar gnus-newsgroup-end nil)
1426 (defvar gnus-newsgroup-last-rmail nil)
1427 (defvar gnus-newsgroup-last-mail nil)
1428 (defvar gnus-newsgroup-last-folder nil)
1429 (defvar gnus-newsgroup-last-file nil)
1430 (defvar gnus-newsgroup-last-directory nil)
1431 (defvar gnus-newsgroup-auto-expire nil)
1432 (defvar gnus-newsgroup-active nil)
1433
1434 (defvar gnus-newsgroup-data nil)
1435 (defvar gnus-newsgroup-data-reverse nil)
1436 (defvar gnus-newsgroup-limit nil)
1437 (defvar gnus-newsgroup-limits nil)
1438 (defvar gnus-summary-use-undownloaded-faces nil)
1439
1440 (defvar gnus-newsgroup-unreads nil
1441   "Sorted list of unread articles in the current newsgroup.")
1442
1443 (defvar gnus-newsgroup-unselected nil
1444   "Sorted list of unselected unread articles in the current newsgroup.")
1445
1446 (defvar gnus-newsgroup-reads nil
1447   "Alist of read articles and article marks in the current newsgroup.")
1448
1449 (defvar gnus-newsgroup-expunged-tally nil)
1450
1451 (defvar gnus-newsgroup-marked nil
1452   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1453
1454 (defvar gnus-newsgroup-spam-marked nil
1455   "List of ranges of articles that have been marked as spam.")
1456
1457 (defvar gnus-newsgroup-killed nil
1458   "List of ranges of articles that have been through the scoring process.")
1459
1460 (defvar gnus-newsgroup-cached nil
1461   "Sorted list of articles that come from the article cache.")
1462
1463 (defvar gnus-newsgroup-saved nil
1464   "List of articles that have been saved.")
1465
1466 (defvar gnus-newsgroup-kill-headers nil)
1467
1468 (defvar gnus-newsgroup-replied nil
1469   "List of articles that have been replied to in the current newsgroup.")
1470
1471 (defvar gnus-newsgroup-forwarded nil
1472   "List of articles that have been forwarded in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-recent nil
1475   "List of articles that have are recent in the current newsgroup.")
1476
1477 (defvar gnus-newsgroup-expirable nil
1478   "Sorted list of articles in the current newsgroup that can be expired.")
1479
1480 (defvar gnus-newsgroup-processable nil
1481   "List of articles in the current newsgroup that can be processed.")
1482
1483 (defvar gnus-newsgroup-downloadable nil
1484   "Sorted list of articles in the current newsgroup that can be processed.")
1485
1486 (defvar gnus-newsgroup-unfetched nil
1487   "Sorted list of articles in the current newsgroup whose headers have
1488 not been fetched into the agent.
1489
1490 This list will always be a subset of gnus-newsgroup-undownloaded.")
1491
1492 (defvar gnus-newsgroup-undownloaded nil
1493   "List of articles in the current newsgroup that haven't been downloaded.")
1494
1495 (defvar gnus-newsgroup-unsendable nil
1496   "List of articles in the current newsgroup that won't be sent.")
1497
1498 (defvar gnus-newsgroup-bookmarks nil
1499   "List of articles in the current newsgroup that have bookmarks.")
1500
1501 (defvar gnus-newsgroup-dormant nil
1502   "Sorted list of dormant articles in the current newsgroup.")
1503
1504 (defvar gnus-newsgroup-unseen nil
1505   "List of unseen articles in the current newsgroup.")
1506
1507 (defvar gnus-newsgroup-seen nil
1508   "Range of seen articles in the current newsgroup.")
1509
1510 (defvar gnus-newsgroup-articles nil
1511   "List of articles in the current newsgroup.")
1512
1513 (defvar gnus-newsgroup-scored nil
1514   "List of scored articles in the current newsgroup.")
1515
1516 (defvar gnus-newsgroup-headers nil
1517   "List of article headers in the current newsgroup.")
1518
1519 (defvar gnus-newsgroup-threads nil)
1520
1521 (defvar gnus-newsgroup-prepared nil
1522   "Whether the current group has been prepared properly.")
1523
1524 (defvar gnus-newsgroup-ancient nil
1525   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-sparse nil)
1528
1529 (defvar gnus-current-article nil)
1530 (defvar gnus-article-current nil)
1531 (defvar gnus-current-headers nil)
1532 (defvar gnus-have-all-headers nil)
1533 (defvar gnus-last-article nil)
1534 (defvar gnus-newsgroup-history nil)
1535 (defvar gnus-newsgroup-charset nil)
1536 (defvar gnus-newsgroup-ephemeral-charset nil)
1537 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1538
1539 (defvar gnus-article-before-search nil)
1540
1541 (defvar gnus-summary-local-variables
1542   '(gnus-newsgroup-name
1543     gnus-newsgroup-begin gnus-newsgroup-end
1544     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1545     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1546     gnus-newsgroup-last-directory
1547     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1548     gnus-newsgroup-unselected gnus-newsgroup-marked
1549     gnus-newsgroup-spam-marked
1550     gnus-newsgroup-reads gnus-newsgroup-saved
1551     gnus-newsgroup-replied gnus-newsgroup-forwarded
1552     gnus-newsgroup-recent
1553     gnus-newsgroup-expirable
1554     gnus-newsgroup-processable gnus-newsgroup-killed
1555     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1556     gnus-newsgroup-unfetched
1557     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1558     gnus-newsgroup-seen gnus-newsgroup-articles
1559     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1560     gnus-newsgroup-headers gnus-newsgroup-threads
1561     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1562     gnus-current-article gnus-current-headers gnus-have-all-headers
1563     gnus-last-article gnus-article-internal-prepare-hook
1564     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1565     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1566     gnus-thread-expunge-below
1567     gnus-score-alist gnus-current-score-file
1568     (gnus-summary-expunge-below . global)
1569     (gnus-summary-mark-below . global)
1570     (gnus-orphan-score . global)
1571     gnus-newsgroup-active gnus-scores-exclude-files
1572     gnus-newsgroup-history gnus-newsgroup-ancient
1573     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1574     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1575     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1576     (gnus-newsgroup-expunged-tally . 0)
1577     gnus-cache-removable-articles gnus-newsgroup-cached
1578     gnus-newsgroup-data gnus-newsgroup-data-reverse
1579     gnus-newsgroup-limit gnus-newsgroup-limits
1580     gnus-newsgroup-charset gnus-newsgroup-display
1581     gnus-summary-use-undownloaded-faces)
1582   "Variables that are buffer-local to the summary buffers.")
1583
1584 (defvar gnus-newsgroup-variables nil
1585   "A list of variables that have separate values in different newsgroups.
1586 A list of newsgroup (summary buffer) local variables, or cons of
1587 variables and their default expressions to be evalled (when the default
1588 values are not nil), that should be made global while the summary buffer
1589 is active.
1590
1591 Note: The default expressions will be evaluated (using function `eval')
1592 before assignment to the local variable rather than just assigned to it.
1593 If the default expression is the symbol `global', that symbol will not
1594 be evaluated but the global value of the local variable will be used
1595 instead.
1596
1597 These variables can be used to set variables in the group parameters
1598 while still allowing them to affect operations done in other buffers.
1599 For example:
1600
1601 \(setq gnus-newsgroup-variables
1602      '(message-use-followup-to
1603        (gnus-visible-headers .
1604          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1605 ")
1606
1607 (eval-when-compile
1608   ;; Bind features so that require will believe that gnus-sum has
1609   ;; already been loaded (avoids infinite recursion)
1610   (let ((features (cons 'gnus-sum features)))
1611     (require 'gnus-art)))
1612
1613 ;; MIME stuff.
1614
1615 (defvar gnus-decode-encoded-word-methods
1616   '(mail-decode-encoded-word-string)
1617   "List of methods used to decode encoded words.
1618
1619 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1620 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1621 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1622 whose names match REGEXP.
1623
1624 For example:
1625 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1626  mail-decode-encoded-word-string
1627  (\"chinese\" . rfc1843-decode-string))")
1628
1629 (defvar gnus-decode-encoded-word-methods-cache nil)
1630
1631 (defun gnus-multi-decode-encoded-word-string (string)
1632   "Apply the functions from `gnus-encoded-word-methods' that match."
1633   (unless (and gnus-decode-encoded-word-methods-cache
1634                (eq gnus-newsgroup-name
1635                    (car gnus-decode-encoded-word-methods-cache)))
1636     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1637     (dolist (method gnus-decode-encoded-word-methods)
1638       (if (symbolp method)
1639           (nconc gnus-decode-encoded-word-methods-cache (list method))
1640         (if (and gnus-newsgroup-name
1641                  (string-match (car method) gnus-newsgroup-name))
1642             (nconc gnus-decode-encoded-word-methods-cache
1643                    (list (cdr method)))))))
1644   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1645     (setq string (funcall method string))))
1646
1647 ;; Subject simplification.
1648
1649 (defun gnus-simplify-whitespace (str)
1650   "Remove excessive whitespace from STR."
1651   ;; Multiple spaces.
1652   (while (string-match "[ \t][ \t]+" str)
1653     (setq str (concat (substring str 0 (match-beginning 0))
1654                         " "
1655                         (substring str (match-end 0)))))
1656   ;; Leading spaces.
1657   (when (string-match "^[ \t]+" str)
1658     (setq str (substring str (match-end 0))))
1659   ;; Trailing spaces.
1660   (when (string-match "[ \t]+$" str)
1661     (setq str (substring str 0 (match-beginning 0))))
1662   str)
1663
1664 (defun gnus-simplify-all-whitespace (str)
1665   "Remove all whitespace from STR."
1666   (while (string-match "[ \t\n]+" str)
1667     (setq str (replace-match "" nil nil str)))
1668   str)
1669
1670 (defsubst gnus-simplify-subject-re (subject)
1671   "Remove \"Re:\" from subject lines."
1672   (if (string-match message-subject-re-regexp subject)
1673       (substring subject (match-end 0))
1674     subject))
1675
1676 (defun gnus-simplify-subject (subject &optional re-only)
1677   "Remove `Re:' and words in parentheses.
1678 If RE-ONLY is non-nil, strip leading `Re:'s only."
1679   (let ((case-fold-search t))           ;Ignore case.
1680     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1681     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1682       (setq subject (substring subject (match-end 0))))
1683     ;; Remove uninteresting prefixes.
1684     (when (and (not re-only)
1685                gnus-simplify-ignored-prefixes
1686                (string-match gnus-simplify-ignored-prefixes subject))
1687       (setq subject (substring subject (match-end 0))))
1688     ;; Remove words in parentheses from end.
1689     (unless re-only
1690       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1691         (setq subject (substring subject 0 (match-beginning 0)))))
1692     ;; Return subject string.
1693     subject))
1694
1695 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1696 ;; all whitespace.
1697 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1698   (goto-char (point-min))
1699   (while (re-search-forward regexp nil t)
1700     (replace-match (or newtext ""))))
1701
1702 (defun gnus-simplify-buffer-fuzzy ()
1703   "Simplify string in the buffer fuzzily.
1704 The string in the accessible portion of the current buffer is simplified.
1705 It is assumed to be a single-line subject.
1706 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1707 matter is removed.  Additional things can be deleted by setting
1708 `gnus-simplify-subject-fuzzy-regexp'."
1709   (let ((case-fold-search t)
1710         (modified-tick))
1711     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1712
1713     (while (not (eq modified-tick (buffer-modified-tick)))
1714       (setq modified-tick (buffer-modified-tick))
1715       (cond
1716        ((listp gnus-simplify-subject-fuzzy-regexp)
1717         (mapc 'gnus-simplify-buffer-fuzzy-step
1718               gnus-simplify-subject-fuzzy-regexp))
1719        (gnus-simplify-subject-fuzzy-regexp
1720         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1721       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1722       (gnus-simplify-buffer-fuzzy-step
1723        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1724       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1725
1726     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1727     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1728     (gnus-simplify-buffer-fuzzy-step " $")
1729     (gnus-simplify-buffer-fuzzy-step "^ +")))
1730
1731 (defun gnus-simplify-subject-fuzzy (subject)
1732   "Simplify a subject string fuzzily.
1733 See `gnus-simplify-buffer-fuzzy' for details."
1734   (save-excursion
1735     (gnus-set-work-buffer)
1736     (let ((case-fold-search t))
1737       ;; Remove uninteresting prefixes.
1738       (when (and gnus-simplify-ignored-prefixes
1739                  (string-match gnus-simplify-ignored-prefixes subject))
1740         (setq subject (substring subject (match-end 0))))
1741       (insert subject)
1742       (inline (gnus-simplify-buffer-fuzzy))
1743       (buffer-string))))
1744
1745 (defsubst gnus-simplify-subject-fully (subject)
1746   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1747   (cond
1748    (gnus-simplify-subject-functions
1749     (gnus-map-function gnus-simplify-subject-functions subject))
1750    ((null gnus-summary-gather-subject-limit)
1751     (gnus-simplify-subject-re subject))
1752    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1753     (gnus-simplify-subject-fuzzy subject))
1754    ((numberp gnus-summary-gather-subject-limit)
1755     (truncate-string-to-width (gnus-simplify-subject-re subject)
1756                               gnus-summary-gather-subject-limit))
1757    (t
1758     subject)))
1759
1760 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1761   "Check whether two subjects are equal.
1762 If optional argument SIMPLE-FIRST is t, first argument is already
1763 simplified."
1764   (cond
1765    ((null simple-first)
1766     (equal (gnus-simplify-subject-fully s1)
1767            (gnus-simplify-subject-fully s2)))
1768    (t
1769     (equal s1
1770            (gnus-simplify-subject-fully s2)))))
1771
1772 (defun gnus-summary-bubble-group ()
1773   "Increase the score of the current group.
1774 This is a handy function to add to `gnus-summary-exit-hook' to
1775 increase the score of each group you read."
1776   (gnus-group-add-score gnus-newsgroup-name))
1777
1778 \f
1779 ;;;
1780 ;;; Gnus summary mode
1781 ;;;
1782
1783 (put 'gnus-summary-mode 'mode-class 'special)
1784
1785 (defvar gnus-article-commands-menu)
1786
1787 ;; Non-orthogonal keys
1788
1789 (gnus-define-keys gnus-summary-mode-map
1790   " " gnus-summary-next-page
1791   "\177" gnus-summary-prev-page
1792   [delete] gnus-summary-prev-page
1793   [backspace] gnus-summary-prev-page
1794   "\r" gnus-summary-scroll-up
1795   "\M-\r" gnus-summary-scroll-down
1796   "n" gnus-summary-next-unread-article
1797   "p" gnus-summary-prev-unread-article
1798   "N" gnus-summary-next-article
1799   "P" gnus-summary-prev-article
1800   "\M-\C-n" gnus-summary-next-same-subject
1801   "\M-\C-p" gnus-summary-prev-same-subject
1802   "\M-n" gnus-summary-next-unread-subject
1803   "\M-p" gnus-summary-prev-unread-subject
1804   "." gnus-summary-first-unread-article
1805   "," gnus-summary-best-unread-article
1806   "\M-s" gnus-summary-search-article-forward
1807   "\M-r" gnus-summary-search-article-backward
1808   "\M-S" gnus-summary-repeat-search-article-forward
1809   "\M-R" gnus-summary-repeat-search-article-backward
1810   "<" gnus-summary-beginning-of-article
1811   ">" gnus-summary-end-of-article
1812   "j" gnus-summary-goto-article
1813   "^" gnus-summary-refer-parent-article
1814   "\M-^" gnus-summary-refer-article
1815   "u" gnus-summary-tick-article-forward
1816   "!" gnus-summary-tick-article-forward
1817   "U" gnus-summary-tick-article-backward
1818   "d" gnus-summary-mark-as-read-forward
1819   "D" gnus-summary-mark-as-read-backward
1820   "E" gnus-summary-mark-as-expirable
1821   "\M-u" gnus-summary-clear-mark-forward
1822   "\M-U" gnus-summary-clear-mark-backward
1823   "k" gnus-summary-kill-same-subject-and-select
1824   "\C-k" gnus-summary-kill-same-subject
1825   "\M-\C-k" gnus-summary-kill-thread
1826   "\M-\C-l" gnus-summary-lower-thread
1827   "e" gnus-summary-edit-article
1828   "#" gnus-summary-mark-as-processable
1829   "\M-#" gnus-summary-unmark-as-processable
1830   "\M-\C-t" gnus-summary-toggle-threads
1831   "\M-\C-s" gnus-summary-show-thread
1832   "\M-\C-h" gnus-summary-hide-thread
1833   "\M-\C-f" gnus-summary-next-thread
1834   "\M-\C-b" gnus-summary-prev-thread
1835   [(meta down)] gnus-summary-next-thread
1836   [(meta up)] gnus-summary-prev-thread
1837   "\M-\C-u" gnus-summary-up-thread
1838   "\M-\C-d" gnus-summary-down-thread
1839   "&" gnus-summary-execute-command
1840   "c" gnus-summary-catchup-and-exit
1841   "\C-w" gnus-summary-mark-region-as-read
1842   "\C-t" gnus-summary-toggle-truncation
1843   "?" gnus-summary-mark-as-dormant
1844   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1845   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1846   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1847   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1848   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1849   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1850   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1851   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1852   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1853   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1854   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1855   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1856   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1857   "=" gnus-summary-expand-window
1858   "\C-x\C-s" gnus-summary-reselect-current-group
1859   "\M-g" gnus-summary-rescan-group
1860   "\C-c\C-r" gnus-summary-caesar-message
1861   "f" gnus-summary-followup
1862   "F" gnus-summary-followup-with-original
1863   "C" gnus-summary-cancel-article
1864   "r" gnus-summary-reply
1865   "R" gnus-summary-reply-with-original
1866   "\C-c\C-f" gnus-summary-mail-forward
1867   "o" gnus-summary-save-article
1868   "\C-o" gnus-summary-save-article-mail
1869   "|" gnus-summary-pipe-output
1870   "\M-k" gnus-summary-edit-local-kill
1871   "\M-K" gnus-summary-edit-global-kill
1872   ;; "V" gnus-version
1873   "\C-c\C-d" gnus-summary-describe-group
1874   "q" gnus-summary-exit
1875   "Q" gnus-summary-exit-no-update
1876   "\C-c\C-i" gnus-info-find-node
1877   gnus-mouse-2 gnus-mouse-pick-article
1878   [follow-link] mouse-face
1879   "m" gnus-summary-mail-other-window
1880   "a" gnus-summary-post-news
1881   "x" gnus-summary-limit-to-unread
1882   "s" gnus-summary-isearch-article
1883   "t" gnus-summary-toggle-header
1884   "g" gnus-summary-show-article
1885   "l" gnus-summary-goto-last-article
1886   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1887   "\C-d" gnus-summary-enter-digest-group
1888   "\M-\C-d" gnus-summary-read-document
1889   "\M-\C-e" gnus-summary-edit-parameters
1890   "\M-\C-a" gnus-summary-customize-parameters
1891   "\C-c\C-b" gnus-bug
1892   "*" gnus-cache-enter-article
1893   "\M-*" gnus-cache-remove-article
1894   "\M-&" gnus-summary-universal-argument
1895   "\C-l" gnus-recenter
1896   "I" gnus-summary-increase-score
1897   "L" gnus-summary-lower-score
1898   "\M-i" gnus-symbolic-argument
1899   "h" gnus-summary-select-article-buffer
1900
1901   "b" gnus-article-view-part
1902   "\M-t" gnus-summary-toggle-display-buttonized
1903
1904   "V" gnus-summary-score-map
1905   "X" gnus-uu-extract-map
1906   "S" gnus-summary-send-map)
1907
1908 ;; Sort of orthogonal keymap
1909 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1910   "t" gnus-summary-tick-article-forward
1911   "!" gnus-summary-tick-article-forward
1912   "d" gnus-summary-mark-as-read-forward
1913   "r" gnus-summary-mark-as-read-forward
1914   "c" gnus-summary-clear-mark-forward
1915   " " gnus-summary-clear-mark-forward
1916   "e" gnus-summary-mark-as-expirable
1917   "x" gnus-summary-mark-as-expirable
1918   "?" gnus-summary-mark-as-dormant
1919   "b" gnus-summary-set-bookmark
1920   "B" gnus-summary-remove-bookmark
1921   "#" gnus-summary-mark-as-processable
1922   "\M-#" gnus-summary-unmark-as-processable
1923   "S" gnus-summary-limit-include-expunged
1924   "C" gnus-summary-catchup
1925   "H" gnus-summary-catchup-to-here
1926   "h" gnus-summary-catchup-from-here
1927   "\C-c" gnus-summary-catchup-all
1928   "k" gnus-summary-kill-same-subject-and-select
1929   "K" gnus-summary-kill-same-subject
1930   "P" gnus-uu-mark-map)
1931
1932 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1933   "c" gnus-summary-clear-above
1934   "u" gnus-summary-tick-above
1935   "m" gnus-summary-mark-above
1936   "k" gnus-summary-kill-below)
1937
1938 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1939   "/" gnus-summary-limit-to-subject
1940   "n" gnus-summary-limit-to-articles
1941   "b" gnus-summary-limit-to-bodies
1942   "h" gnus-summary-limit-to-headers
1943   "w" gnus-summary-pop-limit
1944   "s" gnus-summary-limit-to-subject
1945   "a" gnus-summary-limit-to-author
1946   "u" gnus-summary-limit-to-unread
1947   "m" gnus-summary-limit-to-marks
1948   "M" gnus-summary-limit-exclude-marks
1949   "v" gnus-summary-limit-to-score
1950   "*" gnus-summary-limit-include-cached
1951   "D" gnus-summary-limit-include-dormant
1952   "T" gnus-summary-limit-include-thread
1953   "d" gnus-summary-limit-exclude-dormant
1954   "t" gnus-summary-limit-to-age
1955   "." gnus-summary-limit-to-unseen
1956   "x" gnus-summary-limit-to-extra
1957   "p" gnus-summary-limit-to-display-predicate
1958   "E" gnus-summary-limit-include-expunged
1959   "c" gnus-summary-limit-exclude-childless-dormant
1960   "C" gnus-summary-limit-mark-excluded-as-read
1961   "o" gnus-summary-insert-old-articles
1962   "N" gnus-summary-insert-new-articles
1963   "S" gnus-summary-limit-to-singletons
1964   "r" gnus-summary-limit-to-replied
1965   "R" gnus-summary-limit-to-recipient
1966   "A" gnus-summary-limit-to-address)
1967
1968 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1969   "n" gnus-summary-next-unread-article
1970   "p" gnus-summary-prev-unread-article
1971   "N" gnus-summary-next-article
1972   "P" gnus-summary-prev-article
1973   "\C-n" gnus-summary-next-same-subject
1974   "\C-p" gnus-summary-prev-same-subject
1975   "\M-n" gnus-summary-next-unread-subject
1976   "\M-p" gnus-summary-prev-unread-subject
1977   "f" gnus-summary-first-unread-article
1978   "b" gnus-summary-best-unread-article
1979   "j" gnus-summary-goto-article
1980   "g" gnus-summary-goto-subject
1981   "l" gnus-summary-goto-last-article
1982   "o" gnus-summary-pop-article)
1983
1984 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1985   "k" gnus-summary-kill-thread
1986   "E" gnus-summary-expire-thread
1987   "l" gnus-summary-lower-thread
1988   "i" gnus-summary-raise-thread
1989   "T" gnus-summary-toggle-threads
1990   "t" gnus-summary-rethread-current
1991   "^" gnus-summary-reparent-thread
1992   "\M-^" gnus-summary-reparent-children
1993   "s" gnus-summary-show-thread
1994   "S" gnus-summary-show-all-threads
1995   "h" gnus-summary-hide-thread
1996   "H" gnus-summary-hide-all-threads
1997   "n" gnus-summary-next-thread
1998   "p" gnus-summary-prev-thread
1999   "u" gnus-summary-up-thread
2000   "o" gnus-summary-top-thread
2001   "d" gnus-summary-down-thread
2002   "#" gnus-uu-mark-thread
2003   "\M-#" gnus-uu-unmark-thread)
2004
2005 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2006   "g" gnus-summary-prepare
2007   "c" gnus-summary-insert-cached-articles
2008   "d" gnus-summary-insert-dormant-articles
2009   "t" gnus-summary-insert-ticked-articles)
2010
2011 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2012   "c" gnus-summary-catchup-and-exit
2013   "C" gnus-summary-catchup-all-and-exit
2014   "E" gnus-summary-exit-no-update
2015   "Q" gnus-summary-exit
2016   "Z" gnus-summary-exit
2017   "n" gnus-summary-catchup-and-goto-next-group
2018   "p" gnus-summary-catchup-and-goto-prev-group
2019   "R" gnus-summary-reselect-current-group
2020   "G" gnus-summary-rescan-group
2021   "N" gnus-summary-next-group
2022   "s" gnus-summary-save-newsrc
2023   "P" gnus-summary-prev-group)
2024
2025 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2026   " " gnus-summary-next-page
2027   "n" gnus-summary-next-page
2028   "\177" gnus-summary-prev-page
2029   [delete] gnus-summary-prev-page
2030   "p" gnus-summary-prev-page
2031   "\r" gnus-summary-scroll-up
2032   "\M-\r" gnus-summary-scroll-down
2033   "<" gnus-summary-beginning-of-article
2034   ">" gnus-summary-end-of-article
2035   "b" gnus-summary-beginning-of-article
2036   "e" gnus-summary-end-of-article
2037   "^" gnus-summary-refer-parent-article
2038   "r" gnus-summary-refer-parent-article
2039   "D" gnus-summary-enter-digest-group
2040   "R" gnus-summary-refer-references
2041   "T" gnus-summary-refer-thread
2042   "g" gnus-summary-show-article
2043   "s" gnus-summary-isearch-article
2044   "P" gnus-summary-print-article
2045   "S" gnus-sticky-article
2046   "M" gnus-mailing-list-insinuate
2047   "t" gnus-article-babel)
2048
2049 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2050   "b" gnus-article-add-buttons
2051   "B" gnus-article-add-buttons-to-head
2052   "o" gnus-article-treat-overstrike
2053   "e" gnus-article-emphasize
2054   "w" gnus-article-fill-cited-article
2055   "Q" gnus-article-fill-long-lines
2056   "L" gnus-article-toggle-truncate-lines
2057   "C" gnus-article-capitalize-sentences
2058   "c" gnus-article-remove-cr
2059   "q" gnus-article-de-quoted-unreadable
2060   "6" gnus-article-de-base64-unreadable
2061   "Z" gnus-article-decode-HZ
2062   "A" gnus-article-treat-ansi-sequences
2063   "h" gnus-article-wash-html
2064   "u" gnus-article-unsplit-urls
2065   "s" gnus-summary-force-verify-and-decrypt
2066   "f" gnus-article-display-x-face
2067   "l" gnus-summary-stop-page-breaking
2068   "r" gnus-summary-caesar-message
2069   "m" gnus-summary-morse-message
2070   "t" gnus-summary-toggle-header
2071   "g" gnus-treat-smiley
2072   "v" gnus-summary-verbose-headers
2073   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2074   "p" gnus-article-verify-x-pgp-sig
2075   "d" gnus-article-treat-dumbquotes
2076   "i" gnus-summary-idna-message)
2077
2078 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2079   ;; mnemonic: deuglif*Y*
2080   "u" gnus-article-outlook-unwrap-lines
2081   "a" gnus-article-outlook-repair-attribution
2082   "c" gnus-article-outlook-rearrange-citation
2083   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2084
2085 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2086   "a" gnus-article-hide
2087   "h" gnus-article-hide-headers
2088   "b" gnus-article-hide-boring-headers
2089   "s" gnus-article-hide-signature
2090   "c" gnus-article-hide-citation
2091   "C" gnus-article-hide-citation-in-followups
2092   "l" gnus-article-hide-list-identifiers
2093   "B" gnus-article-strip-banner
2094   "P" gnus-article-hide-pem
2095   "\C-c" gnus-article-hide-citation-maybe)
2096
2097 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2098   "a" gnus-article-highlight
2099   "h" gnus-article-highlight-headers
2100   "c" gnus-article-highlight-citation
2101   "s" gnus-article-highlight-signature)
2102
2103 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2104   "f" gnus-article-treat-fold-headers
2105   "u" gnus-article-treat-unfold-headers
2106   "n" gnus-article-treat-fold-newsgroups)
2107
2108 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2109   "x" gnus-article-display-x-face
2110   "d" gnus-article-display-face
2111   "s" gnus-treat-smiley
2112   "D" gnus-article-remove-images
2113   "W" gnus-html-show-images
2114   "f" gnus-treat-from-picon
2115   "m" gnus-treat-mail-picon
2116   "n" gnus-treat-newsgroups-picon)
2117
2118 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2119   "w" gnus-article-decode-mime-words
2120   "c" gnus-article-decode-charset
2121   "v" gnus-mime-view-all-parts
2122   "b" gnus-article-view-part)
2123
2124 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2125   "z" gnus-article-date-ut
2126   "u" gnus-article-date-ut
2127   "l" gnus-article-date-local
2128   "p" gnus-article-date-english
2129   "e" gnus-article-date-lapsed
2130   "o" gnus-article-date-original
2131   "i" gnus-article-date-iso8601
2132   "s" gnus-article-date-user)
2133
2134 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2135   "t" gnus-article-remove-trailing-blank-lines
2136   "l" gnus-article-strip-leading-blank-lines
2137   "m" gnus-article-strip-multiple-blank-lines
2138   "a" gnus-article-strip-blank-lines
2139   "A" gnus-article-strip-all-blank-lines
2140   "s" gnus-article-strip-leading-space
2141   "e" gnus-article-strip-trailing-space
2142   "w" gnus-article-remove-leading-whitespace)
2143
2144 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2145   "v" gnus-version
2146   "f" gnus-summary-fetch-faq
2147   "d" gnus-summary-describe-group
2148   "h" gnus-summary-describe-briefly
2149   "i" gnus-info-find-node
2150   "c" gnus-group-fetch-charter
2151   "C" gnus-group-fetch-control)
2152
2153 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2154   "e" gnus-summary-expire-articles
2155   "\M-\C-e" gnus-summary-expire-articles-now
2156   "\177" gnus-summary-delete-article
2157   [delete] gnus-summary-delete-article
2158   [backspace] gnus-summary-delete-article
2159   "m" gnus-summary-move-article
2160   "r" gnus-summary-respool-article
2161   "w" gnus-summary-edit-article
2162   "c" gnus-summary-copy-article
2163   "B" gnus-summary-crosspost-article
2164   "q" gnus-summary-respool-query
2165   "t" gnus-summary-respool-trace
2166   "i" gnus-summary-import-article
2167   "I" gnus-summary-create-article
2168   "p" gnus-summary-article-posted-p)
2169
2170 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2171   "o" gnus-summary-save-article
2172   "m" gnus-summary-save-article-mail
2173   "F" gnus-summary-write-article-file
2174   "r" gnus-summary-save-article-rmail
2175   "f" gnus-summary-save-article-file
2176   "b" gnus-summary-save-article-body-file
2177   "B" gnus-summary-write-article-body-file
2178   "h" gnus-summary-save-article-folder
2179   "v" gnus-summary-save-article-vm
2180   "p" gnus-summary-pipe-output
2181   "P" gnus-summary-muttprint)
2182
2183 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2184   "b" gnus-summary-display-buttonized
2185   "m" gnus-summary-repair-multipart
2186   "v" gnus-article-view-part
2187   "o" gnus-article-save-part
2188   "O" gnus-article-save-part-and-strip
2189   "r" gnus-article-replace-part
2190   "d" gnus-article-delete-part
2191   "t" gnus-article-view-part-as-type
2192   "j" gnus-article-jump-to-part
2193   "c" gnus-article-copy-part
2194   "C" gnus-article-view-part-as-charset
2195   "e" gnus-article-view-part-externally
2196   "H" gnus-article-browse-html-article
2197   "E" gnus-article-encrypt-body
2198   "i" gnus-article-inline-part
2199   "|" gnus-article-pipe-part)
2200
2201 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2202   "p" gnus-summary-mark-as-processable
2203   "u" gnus-summary-unmark-as-processable
2204   "U" gnus-summary-unmark-all-processable
2205   "v" gnus-uu-mark-over
2206   "s" gnus-uu-mark-series
2207   "r" gnus-uu-mark-region
2208   "g" gnus-uu-unmark-region
2209   "R" gnus-uu-mark-by-regexp
2210   "G" gnus-uu-unmark-by-regexp
2211   "t" gnus-uu-mark-thread
2212   "T" gnus-uu-unmark-thread
2213   "a" gnus-uu-mark-all
2214   "b" gnus-uu-mark-buffer
2215   "S" gnus-uu-mark-sparse
2216   "k" gnus-summary-kill-process-mark
2217   "y" gnus-summary-yank-process-mark
2218   "w" gnus-summary-save-process-mark
2219   "i" gnus-uu-invert-processable)
2220
2221 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2222   ;;"x" gnus-uu-extract-any
2223   "m" gnus-summary-save-parts
2224   "u" gnus-uu-decode-uu
2225   "U" gnus-uu-decode-uu-and-save
2226   "s" gnus-uu-decode-unshar
2227   "S" gnus-uu-decode-unshar-and-save
2228   "o" gnus-uu-decode-save
2229   "O" gnus-uu-decode-save
2230   "b" gnus-uu-decode-binhex
2231   "B" gnus-uu-decode-binhex
2232   "Y" gnus-uu-decode-yenc
2233   "p" gnus-uu-decode-postscript
2234   "P" gnus-uu-decode-postscript-and-save)
2235
2236 (gnus-define-keys
2237     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2238   "u" gnus-uu-decode-uu-view
2239   "U" gnus-uu-decode-uu-and-save-view
2240   "s" gnus-uu-decode-unshar-view
2241   "S" gnus-uu-decode-unshar-and-save-view
2242   "o" gnus-uu-decode-save-view
2243   "O" gnus-uu-decode-save-view
2244   "b" gnus-uu-decode-binhex-view
2245   "B" gnus-uu-decode-binhex-view
2246   "p" gnus-uu-decode-postscript-view
2247   "P" gnus-uu-decode-postscript-and-save-view)
2248
2249 (defvar gnus-article-post-menu nil)
2250
2251 (defconst gnus-summary-menu-maxlen 20)
2252
2253 (defun gnus-summary-menu-split (menu)
2254   ;; If we have lots of elements, divide them into groups of 20
2255   ;; and make a pane (or submenu) for each one.
2256   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2257       (let ((menu menu) sublists next
2258             (i 1))
2259         (while menu
2260           ;; Pull off the next gnus-summary-menu-maxlen elements
2261           ;; and make them the next element of sublist.
2262           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2263           (if next
2264               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2265                       nil))
2266           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2267                                              (aref (car (last menu)) 0)) menu)
2268                                sublists))
2269           (setq i (1+ i))
2270           (setq menu next))
2271         (nreverse sublists))
2272     ;; Few elements--put them all in one pane.
2273     menu))
2274
2275 (defun gnus-summary-make-menu-bar ()
2276   (gnus-turn-off-edit-menu 'summary)
2277
2278   (unless (boundp 'gnus-summary-misc-menu)
2279
2280     (easy-menu-define
2281       gnus-summary-kill-menu gnus-summary-mode-map ""
2282       (cons
2283        "Score"
2284        (nconc
2285         (list
2286          ["Customize" gnus-score-customize t])
2287         (gnus-make-score-map 'increase)
2288         (gnus-make-score-map 'lower)
2289         '(("Mark"
2290            ["Kill below" gnus-summary-kill-below t]
2291            ["Mark above" gnus-summary-mark-above t]
2292            ["Tick above" gnus-summary-tick-above t]
2293            ["Clear above" gnus-summary-clear-above t])
2294           ["Current score" gnus-summary-current-score t]
2295           ["Set score" gnus-summary-set-score t]
2296           ["Switch current score file..." gnus-score-change-score-file t]
2297           ["Set mark below..." gnus-score-set-mark-below t]
2298           ["Set expunge below..." gnus-score-set-expunge-below t]
2299           ["Edit current score file" gnus-score-edit-current-scores t]
2300           ["Edit score file..." gnus-score-edit-file t]
2301           ["Trace score" gnus-score-find-trace t]
2302           ["Find words" gnus-score-find-favourite-words t]
2303           ["Rescore buffer" gnus-summary-rescore t]
2304           ["Increase score..." gnus-summary-increase-score t]
2305           ["Lower score..." gnus-summary-lower-score t]))))
2306
2307     ;; Define both the Article menu in the summary buffer and the
2308     ;; equivalent Commands menu in the article buffer here for
2309     ;; consistency.
2310     (let ((innards
2311            `(("Hide"
2312               ["All" gnus-article-hide t]
2313               ["Headers" gnus-article-hide-headers t]
2314               ["Signature" gnus-article-hide-signature t]
2315               ["Citation" gnus-article-hide-citation t]
2316               ["List identifiers" gnus-article-hide-list-identifiers t]
2317               ["Banner" gnus-article-strip-banner t]
2318               ["Boring headers" gnus-article-hide-boring-headers t])
2319              ("Highlight"
2320               ["All" gnus-article-highlight t]
2321               ["Headers" gnus-article-highlight-headers t]
2322               ["Signature" gnus-article-highlight-signature t]
2323               ["Citation" gnus-article-highlight-citation t])
2324              ("MIME"
2325               ["Words" gnus-article-decode-mime-words t]
2326               ["Charset" gnus-article-decode-charset t]
2327               ["QP" gnus-article-de-quoted-unreadable t]
2328               ["Base64" gnus-article-de-base64-unreadable t]
2329               ["View MIME buttons" gnus-summary-display-buttonized t]
2330               ["View all" gnus-mime-view-all-parts t]
2331               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2332               ["Encrypt body" gnus-article-encrypt-body
2333                :active (not (gnus-group-read-only-p))
2334                ,@(if (featurep 'xemacs) nil
2335                    '(:help "Encrypt the message body on disk"))]
2336               ["Extract all parts..." gnus-summary-save-parts t]
2337               ("Multipart"
2338                ["Repair multipart" gnus-summary-repair-multipart t]
2339                ["Pipe part..." gnus-article-pipe-part t]
2340                ["Inline part" gnus-article-inline-part t]
2341                ["View part as type..." gnus-article-view-part-as-type t]
2342                ["Encrypt body" gnus-article-encrypt-body
2343                 :active (not (gnus-group-read-only-p))
2344                ,@(if (featurep 'xemacs) nil
2345                    '(:help "Encrypt the message body on disk"))]
2346                ["View part externally" gnus-article-view-part-externally t]
2347                ["View HTML parts in browser" gnus-article-browse-html-article t]
2348                ["View part with charset..." gnus-article-view-part-as-charset t]
2349                ["Copy part" gnus-article-copy-part t]
2350                ["Save part..." gnus-article-save-part t]
2351                ["View part" gnus-article-view-part t]))
2352              ("Date"
2353               ["Local" gnus-article-date-local t]
2354               ["ISO8601" gnus-article-date-iso8601 t]
2355               ["UT" gnus-article-date-ut t]
2356               ["Original" gnus-article-date-original t]
2357               ["Lapsed" gnus-article-date-lapsed t]
2358               ["User-defined" gnus-article-date-user t])
2359              ("Display"
2360               ["Remove images" gnus-article-remove-images t]
2361               ["Toggle smiley" gnus-treat-smiley t]
2362               ["Show X-Face" gnus-article-display-x-face t]
2363               ["Show picons in From" gnus-treat-from-picon t]
2364               ["Show picons in mail headers" gnus-treat-mail-picon t]
2365               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2366               ("View as different encoding"
2367                ,@(gnus-summary-menu-split
2368                   (mapcar
2369                    (lambda (cs)
2370                      ;; Since easymenu under Emacs doesn't allow
2371                      ;; lambda forms for menu commands, we should
2372                      ;; provide intern'ed function symbols.
2373                      (let ((command (intern (format "\
2374 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2375                        (fset command
2376                              `(lambda ()
2377                                 (interactive)
2378                                 (let ((gnus-summary-show-article-charset-alist
2379                                        '((1 . ,cs))))
2380                                   (gnus-summary-show-article 1))))
2381                        `[,(symbol-name cs) ,command t]))
2382                    (sort (if (fboundp 'coding-system-list)
2383                              (coding-system-list)
2384                            (mapcar 'car mm-mime-mule-charset-alist))
2385                          'string<)))))
2386              ("Washing"
2387               ("Remove Blanks"
2388                ["Leading" gnus-article-strip-leading-blank-lines t]
2389                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2390                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2391                ["All of the above" gnus-article-strip-blank-lines t]
2392                ["All" gnus-article-strip-all-blank-lines t]
2393                ["Leading space" gnus-article-strip-leading-space t]
2394                ["Trailing space" gnus-article-strip-trailing-space t]
2395                ["Leading space in headers"
2396                 gnus-article-remove-leading-whitespace t])
2397               ["Overstrike" gnus-article-treat-overstrike t]
2398               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2399               ["Emphasis" gnus-article-emphasize t]
2400               ["Word wrap" gnus-article-fill-cited-article t]
2401               ["Fill long lines" gnus-article-fill-long-lines t]
2402               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2403               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2404               ["Remove CR" gnus-article-remove-cr t]
2405               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2406               ["Base64" gnus-article-de-base64-unreadable t]
2407               ["Rot 13" gnus-summary-caesar-message
2408                ,@(if (featurep 'xemacs) '(t)
2409                    '(:help "\"Caesar rotate\" article by 13"))]
2410               ["De-IDNA" gnus-summary-idna-message t]
2411               ["Morse decode" gnus-summary-morse-message t]
2412               ["Unix pipe..." gnus-summary-pipe-message t]
2413               ["Add buttons" gnus-article-add-buttons t]
2414               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2415               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2416               ["Verbose header" gnus-summary-verbose-headers t]
2417               ["Toggle header" gnus-summary-toggle-header t]
2418               ["Unfold headers" gnus-article-treat-unfold-headers t]
2419               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2420               ["Html" gnus-article-wash-html t]
2421               ["Unsplit URLs" gnus-article-unsplit-urls t]
2422               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2423               ["Decode HZ" gnus-article-decode-HZ t]
2424               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2425               ("(Outlook) Deuglify"
2426                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2427                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2428                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2429                ["Full (Outlook) deuglify"
2430                 gnus-article-outlook-deuglify-article t])
2431               )
2432              ("Output"
2433               ["Save in default format..." gnus-summary-save-article
2434                ,@(if (featurep 'xemacs) '(t)
2435                    '(:help "Save article using default method"))]
2436               ["Save in file..." gnus-summary-save-article-file
2437                ,@(if (featurep 'xemacs) '(t)
2438                    '(:help "Save article in file"))]
2439               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2440               ["Save in MH folder..." gnus-summary-save-article-folder t]
2441               ["Save in VM folder..." gnus-summary-save-article-vm t]
2442               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2443               ["Save body in file..." gnus-summary-save-article-body-file t]
2444               ["Pipe through a filter..." gnus-summary-pipe-output t]
2445               ["Print with Muttprint..." gnus-summary-muttprint t]
2446               ["Print" gnus-summary-print-article
2447                ,@(if (featurep 'xemacs) '(t)
2448                    '(:help "Generate and print a PostScript image"))])
2449              ("Copy, move,... (Backend)"
2450               ,@(if (featurep 'xemacs) nil
2451                   '(:help "Copying, moving, expiring articles..."))
2452               ["Respool article..." gnus-summary-respool-article t]
2453               ["Move article..." gnus-summary-move-article
2454                (gnus-check-backend-function
2455                 'request-move-article gnus-newsgroup-name)]
2456               ["Copy article..." gnus-summary-copy-article t]
2457               ["Crosspost article..." gnus-summary-crosspost-article
2458                (gnus-check-backend-function
2459                 'request-replace-article gnus-newsgroup-name)]
2460               ["Import file..." gnus-summary-import-article
2461                (gnus-check-backend-function
2462                 'request-accept-article gnus-newsgroup-name)]
2463               ["Create article..." gnus-summary-create-article
2464                (gnus-check-backend-function
2465                 'request-accept-article gnus-newsgroup-name)]
2466               ["Check if posted" gnus-summary-article-posted-p t]
2467               ["Edit article" gnus-summary-edit-article
2468                (not (gnus-group-read-only-p))]
2469               ["Delete article" gnus-summary-delete-article
2470                (gnus-check-backend-function
2471                 'request-expire-articles gnus-newsgroup-name)]
2472               ["Query respool" gnus-summary-respool-query t]
2473               ["Trace respool" gnus-summary-respool-trace t]
2474               ["Delete expirable articles" gnus-summary-expire-articles-now
2475                (gnus-check-backend-function
2476                 'request-expire-articles gnus-newsgroup-name)])
2477              ("Extract"
2478               ["Uudecode" gnus-uu-decode-uu
2479                ,@(if (featurep 'xemacs) '(t)
2480                    '(:help "Decode uuencoded article(s)"))]
2481               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2482               ["Unshar" gnus-uu-decode-unshar t]
2483               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2484               ["Save" gnus-uu-decode-save t]
2485               ["Binhex" gnus-uu-decode-binhex t]
2486               ["Postscript" gnus-uu-decode-postscript t]
2487               ["All MIME parts" gnus-summary-save-parts t])
2488              ("Cache"
2489               ["Enter article" gnus-cache-enter-article t]
2490               ["Remove article" gnus-cache-remove-article t])
2491              ["Translate" gnus-article-babel t]
2492              ["Select article buffer" gnus-summary-select-article-buffer t]
2493              ["Make article buffer sticky" gnus-sticky-article t]
2494              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2495              ["Isearch article..." gnus-summary-isearch-article t]
2496              ["Beginning of the article" gnus-summary-beginning-of-article t]
2497              ["End of the article" gnus-summary-end-of-article t]
2498              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2499              ["Fetch referenced articles" gnus-summary-refer-references t]
2500              ["Fetch current thread" gnus-summary-refer-thread t]
2501              ["Fetch article with id..." gnus-summary-refer-article t]
2502              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2503              ["Redisplay" gnus-summary-show-article t]
2504              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2505       (easy-menu-define
2506         gnus-summary-article-menu gnus-summary-mode-map ""
2507         (cons "Article" innards))
2508
2509       (if (not (keymapp gnus-summary-article-menu))
2510           (easy-menu-define
2511             gnus-article-commands-menu gnus-article-mode-map ""
2512             (cons "Commands" innards))
2513         ;; in Emacs, don't share menu.
2514         (setq gnus-article-commands-menu
2515               (copy-keymap gnus-summary-article-menu))
2516         (define-key gnus-article-mode-map [menu-bar commands]
2517           (cons "Commands" gnus-article-commands-menu))))
2518
2519     (easy-menu-define
2520       gnus-summary-thread-menu gnus-summary-mode-map ""
2521       '("Threads"
2522         ["Find all messages in thread" gnus-summary-refer-thread t]
2523         ["Toggle threading" gnus-summary-toggle-threads t]
2524         ["Hide threads" gnus-summary-hide-all-threads t]
2525         ["Show threads" gnus-summary-show-all-threads t]
2526         ["Hide thread" gnus-summary-hide-thread t]
2527         ["Show thread" gnus-summary-show-thread t]
2528         ["Go to next thread" gnus-summary-next-thread t]
2529         ["Go to previous thread" gnus-summary-prev-thread t]
2530         ["Go down thread" gnus-summary-down-thread t]
2531         ["Go up thread" gnus-summary-up-thread t]
2532         ["Top of thread" gnus-summary-top-thread t]
2533         ["Mark thread as read" gnus-summary-kill-thread t]
2534         ["Mark thread as expired" gnus-summary-expire-thread t]
2535         ["Lower thread score" gnus-summary-lower-thread t]
2536         ["Raise thread score" gnus-summary-raise-thread t]
2537         ["Rethread current" gnus-summary-rethread-current t]))
2538
2539     (easy-menu-define
2540       gnus-summary-post-menu gnus-summary-mode-map ""
2541       `("Post"
2542         ["Send a message (mail or news)" gnus-summary-post-news
2543          ,@(if (featurep 'xemacs) '(t)
2544              '(:help "Compose a new message (mail or news)"))]
2545         ["Followup" gnus-summary-followup
2546          ,@(if (featurep 'xemacs) '(t)
2547              '(:help "Post followup to this article"))]
2548         ["Followup and yank" gnus-summary-followup-with-original
2549          ,@(if (featurep 'xemacs) '(t)
2550              '(:help "Post followup to this article, quoting its contents"))]
2551         ["Supersede article" gnus-summary-supersede-article t]
2552         ["Cancel article" gnus-summary-cancel-article
2553          ,@(if (featurep 'xemacs) '(t)
2554              '(:help "Cancel an article you posted"))]
2555         ["Reply" gnus-summary-reply t]
2556         ["Reply and yank" gnus-summary-reply-with-original t]
2557         ["Wide reply" gnus-summary-wide-reply t]
2558         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2559          ,@(if (featurep 'xemacs) '(t)
2560              '(:help "Mail a reply, quoting this article"))]
2561         ["Very wide reply" gnus-summary-very-wide-reply t]
2562         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2563          ,@(if (featurep 'xemacs) '(t)
2564              '(:help "Mail a very wide reply, quoting this article"))]
2565         ["Mail forward" gnus-summary-mail-forward t]
2566         ["Post forward" gnus-summary-post-forward t]
2567         ["Digest and mail" gnus-uu-digest-mail-forward t]
2568         ["Digest and post" gnus-uu-digest-post-forward t]
2569         ["Resend message" gnus-summary-resend-message t]
2570         ["Resend message edit" gnus-summary-resend-message-edit t]
2571         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2572         ["Send a mail" gnus-summary-mail-other-window t]
2573         ["Create a local message" gnus-summary-news-other-window t]
2574         ["Uuencode and post" gnus-uu-post-news
2575          ,@(if (featurep 'xemacs) '(t)
2576              '(:help "Post a uuencoded article"))]
2577         ["Followup via news" gnus-summary-followup-to-mail t]
2578         ["Followup via news and yank"
2579          gnus-summary-followup-to-mail-with-original t]
2580         ["Strip signature on reply"
2581          (lambda ()
2582            (interactive)
2583            (if (not (memq message-cite-function
2584                           '(message-cite-original-without-signature
2585                             message-cite-original)))
2586                ;; Stupid workaround for XEmacs not honoring :visible.
2587                (message "Can't toggle this value of `message-cite-function'")
2588              (setq message-cite-function
2589                    (if (eq message-cite-function
2590                            'message-cite-original-without-signature)
2591                        'message-cite-original
2592                      'message-cite-original-without-signature))))
2593          ;; XEmacs barfs on :visible.
2594          ,@(if (featurep 'xemacs) nil
2595              '(:visible (memq message-cite-function
2596                               '(message-cite-original-without-signature
2597                                 message-cite-original))))
2598          :style toggle
2599          :selected (eq message-cite-function
2600                        'message-cite-original-without-signature)
2601          ,@(if (featurep 'xemacs) nil
2602              '(:help "Strip signature from cited article when replying."))]
2603         ;;("Draft"
2604         ;;["Send" gnus-summary-send-draft t]
2605         ;;["Send bounced" gnus-resend-bounced-mail t])
2606         ))
2607
2608     (cond
2609      ((not (keymapp gnus-summary-post-menu))
2610       (setq gnus-article-post-menu gnus-summary-post-menu))
2611      ((not gnus-article-post-menu)
2612       ;; Don't share post menu.
2613       (setq gnus-article-post-menu
2614             (copy-keymap gnus-summary-post-menu))))
2615     (define-key gnus-article-mode-map [menu-bar post]
2616       (cons "Post" gnus-article-post-menu))
2617
2618     (easy-menu-define
2619       gnus-summary-misc-menu gnus-summary-mode-map ""
2620       `("Gnus"
2621         ("Mark Read"
2622          ["Mark as read" gnus-summary-mark-as-read-forward t]
2623          ["Mark same subject and select"
2624           gnus-summary-kill-same-subject-and-select t]
2625          ["Mark same subject" gnus-summary-kill-same-subject t]
2626          ["Catchup" gnus-summary-catchup
2627           ,@(if (featurep 'xemacs) '(t)
2628               '(:help "Mark unread articles in this group as read"))]
2629          ["Catchup all" gnus-summary-catchup-all t]
2630          ["Catchup to here" gnus-summary-catchup-to-here t]
2631          ["Catchup from here" gnus-summary-catchup-from-here t]
2632          ["Catchup region" gnus-summary-mark-region-as-read
2633           (gnus-mark-active-p)]
2634          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2635         ("Mark Various"
2636          ["Tick" gnus-summary-tick-article-forward t]
2637          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2638          ["Remove marks" gnus-summary-clear-mark-forward t]
2639          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2640          ["Set bookmark" gnus-summary-set-bookmark t]
2641          ["Remove bookmark" gnus-summary-remove-bookmark t])
2642         ("Limit to"
2643          ["Marks..." gnus-summary-limit-to-marks t]
2644          ["Subject..." gnus-summary-limit-to-subject t]
2645          ["Author..." gnus-summary-limit-to-author t]
2646          ["Recipient..." gnus-summary-limit-to-recipient t]
2647          ["Address..." gnus-summary-limit-to-address t]
2648          ["Age..." gnus-summary-limit-to-age t]
2649          ["Extra..." gnus-summary-limit-to-extra t]
2650          ["Score..." gnus-summary-limit-to-score t]
2651          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2652          ["Unread" gnus-summary-limit-to-unread t]
2653          ["Unseen" gnus-summary-limit-to-unseen t]
2654          ["Singletons" gnus-summary-limit-to-singletons t]
2655          ["Replied" gnus-summary-limit-to-replied t]
2656          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2657          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2658          ["Pop limit" gnus-summary-pop-limit t]
2659          ["Show dormant" gnus-summary-limit-include-dormant t]
2660          ["Hide childless dormant"
2661           gnus-summary-limit-exclude-childless-dormant t]
2662          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2663          ["Hide marked" gnus-summary-limit-exclude-marks t]
2664          ["Show expunged" gnus-summary-limit-include-expunged t])
2665         ("Process Mark"
2666          ["Set mark" gnus-summary-mark-as-processable t]
2667          ["Remove mark" gnus-summary-unmark-as-processable t]
2668          ["Remove all marks" gnus-summary-unmark-all-processable t]
2669          ["Invert marks" gnus-uu-invert-processable t]
2670          ["Mark above" gnus-uu-mark-over t]
2671          ["Mark series" gnus-uu-mark-series t]
2672          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2673          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2674          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2675          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2676          ["Mark all" gnus-uu-mark-all t]
2677          ["Mark buffer" gnus-uu-mark-buffer t]
2678          ["Mark sparse" gnus-uu-mark-sparse t]
2679          ["Mark thread" gnus-uu-mark-thread t]
2680          ["Unmark thread" gnus-uu-unmark-thread t]
2681          ("Process Mark Sets"
2682           ["Kill" gnus-summary-kill-process-mark t]
2683           ["Yank" gnus-summary-yank-process-mark
2684            gnus-newsgroup-process-stack]
2685           ["Save" gnus-summary-save-process-mark t]
2686           ["Run command on marked..." gnus-summary-universal-argument t]))
2687         ("Registry Marks")
2688         ("Scroll article"
2689          ["Page forward" gnus-summary-next-page
2690           ,@(if (featurep 'xemacs) '(t)
2691               '(:help "Show next page of article"))]
2692          ["Page backward" gnus-summary-prev-page
2693           ,@(if (featurep 'xemacs) '(t)
2694               '(:help "Show previous page of article"))]
2695          ["Line forward" gnus-summary-scroll-up t])
2696         ("Move"
2697          ["Next unread article" gnus-summary-next-unread-article t]
2698          ["Previous unread article" gnus-summary-prev-unread-article t]
2699          ["Next article" gnus-summary-next-article t]
2700          ["Previous article" gnus-summary-prev-article t]
2701          ["Next unread subject" gnus-summary-next-unread-subject t]
2702          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2703          ["Next article same subject" gnus-summary-next-same-subject t]
2704          ["Previous article same subject" gnus-summary-prev-same-subject t]
2705          ["First unread article" gnus-summary-first-unread-article t]
2706          ["Best unread article" gnus-summary-best-unread-article t]
2707          ["Go to subject number..." gnus-summary-goto-subject t]
2708          ["Go to article number..." gnus-summary-goto-article t]
2709          ["Go to the last article" gnus-summary-goto-last-article t]
2710          ["Pop article off history" gnus-summary-pop-article t])
2711         ("Sort"
2712          ["Sort by number" gnus-summary-sort-by-number t]
2713          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2714          ["Sort by author" gnus-summary-sort-by-author t]
2715          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2716          ["Sort by subject" gnus-summary-sort-by-subject t]
2717          ["Sort by date" gnus-summary-sort-by-date t]
2718          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2719          ["Sort by score" gnus-summary-sort-by-score t]
2720          ["Sort by lines" gnus-summary-sort-by-lines t]
2721          ["Sort by characters" gnus-summary-sort-by-chars t]
2722          ["Randomize" gnus-summary-sort-by-random t]
2723          ["Original sort" gnus-summary-sort-by-original t])
2724         ("Help"
2725          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2726          ["Describe group" gnus-summary-describe-group t]
2727          ["Fetch charter" gnus-group-fetch-charter
2728           ,@(if (featurep 'xemacs) nil
2729               '(:help "Display the charter of the current group"))]
2730          ["Fetch control message" gnus-group-fetch-control
2731           ,@(if (featurep 'xemacs) nil
2732               '(:help "Display the archived control message for the current group"))]
2733          ["Read manual" gnus-info-find-node t])
2734         ("Modes"
2735          ["Pick and read" gnus-pick-mode t]
2736          ["Binary" gnus-binary-mode t])
2737         ("Regeneration"
2738          ["Regenerate" gnus-summary-prepare t]
2739          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2740          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2741          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2742          ["Toggle threading" gnus-summary-toggle-threads t])
2743         ["See old articles" gnus-summary-insert-old-articles t]
2744         ["See new articles" gnus-summary-insert-new-articles t]
2745         ["Filter articles..." gnus-summary-execute-command t]
2746         ["Run command on articles..." gnus-summary-universal-argument t]
2747         ["Search articles forward..." gnus-summary-search-article-forward t]
2748         ["Search articles backward..." gnus-summary-search-article-backward t]
2749         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2750         ["Expand window" gnus-summary-expand-window t]
2751         ["Expire expirable articles" gnus-summary-expire-articles
2752          (gnus-check-backend-function
2753           'request-expire-articles gnus-newsgroup-name)]
2754         ["Edit local kill file" gnus-summary-edit-local-kill t]
2755         ["Edit main kill file" gnus-summary-edit-global-kill t]
2756         ["Edit group parameters" gnus-summary-edit-parameters t]
2757         ["Customize group parameters" gnus-summary-customize-parameters t]
2758         ["Send a bug report" gnus-bug t]
2759         ("Exit"
2760          ["Catchup and exit" gnus-summary-catchup-and-exit
2761           ,@(if (featurep 'xemacs) '(t)
2762               '(:help "Mark unread articles in this group as read, then exit"))]
2763          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2764          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2765          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2766          ["Exit group" gnus-summary-exit
2767           ,@(if (featurep 'xemacs) '(t)
2768               '(:help "Exit current group, return to group selection mode"))]
2769          ["Exit group without updating" gnus-summary-exit-no-update t]
2770          ["Exit and goto next group" gnus-summary-next-group t]
2771          ["Exit and goto prev group" gnus-summary-prev-group t]
2772          ["Reselect group" gnus-summary-reselect-current-group t]
2773          ["Rescan group" gnus-summary-rescan-group t]
2774          ["Update dribble" gnus-summary-save-newsrc t])))
2775
2776     (gnus-run-hooks 'gnus-summary-menu-hook)))
2777
2778 (defvar gnus-summary-tool-bar-map nil)
2779
2780 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2781 ;; affect _new_ message buffers.  We might add a function that walks thru all
2782 ;; summary-mode buffers and force the update.
2783 (defun gnus-summary-tool-bar-update (&optional symbol value)
2784   "Update summary mode toolbar.
2785 Setter function for custom variables."
2786   (setq-default gnus-summary-tool-bar-map nil)
2787   (when symbol
2788     ;; When used as ":set" function:
2789     (set-default symbol value))
2790   (when (gnus-buffer-live-p gnus-summary-buffer)
2791     (with-current-buffer gnus-summary-buffer
2792       (gnus-summary-make-tool-bar))))
2793
2794 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2795                                      'gnus-summary-tool-bar-gnome
2796                                    'gnus-summary-tool-bar-retro)
2797   "Specifies the Gnus summary tool bar.
2798
2799 It can be either a list or a symbol refering to a list.  See
2800 `gmm-tool-bar-from-list' for the format of the list.  The
2801 default key map is `gnus-summary-mode-map'.
2802
2803 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2804 `gnus-summary-tool-bar-retro'."
2805   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2806                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2807                  (repeat :tag "User defined list" gmm-tool-bar-item)
2808                  (symbol))
2809   :version "23.1" ;; No Gnus
2810   :initialize 'custom-initialize-default
2811   :set 'gnus-summary-tool-bar-update
2812   :group 'gnus-summary)
2813
2814 (defcustom gnus-summary-tool-bar-gnome
2815   '((gnus-summary-post-news "mail/compose" nil)
2816     (gnus-summary-insert-new-articles "mail/inbox" nil
2817                                       :visible (or (not gnus-agent)
2818                                                    gnus-plugged))
2819     (gnus-summary-reply-with-original "mail/reply")
2820     (gnus-summary-reply "mail/reply" nil :visible nil)
2821     (gnus-summary-followup-with-original "mail/reply-all")
2822     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2823     (gnus-summary-mail-forward "mail/forward")
2824     (gnus-summary-save-article "mail/save")
2825     (gnus-summary-search-article-forward "search" nil :visible nil)
2826     (gnus-summary-print-article "print")
2827     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2828     ;; Some new commands that may need more suitable icons:
2829     (gnus-summary-save-newsrc "save" nil :visible nil)
2830     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2831     (gnus-summary-prev-article "left-arrow")
2832     (gnus-summary-next-article "right-arrow")
2833     (gnus-summary-next-page "next-page")
2834     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2835     ;;
2836     ;; Maybe some sort-by-... could be added:
2837     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2838     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2839     (gnus-summary-mark-as-expirable
2840      "delete" nil
2841      :visible (gnus-check-backend-function 'request-expire-articles
2842                                            gnus-newsgroup-name))
2843     (gnus-summary-mark-as-spam
2844      "mail/spam" t
2845      :visible (and (fboundp 'spam-group-ham-contents-p)
2846                    (spam-group-ham-contents-p gnus-newsgroup-name))
2847      :help "Mark as spam")
2848     (gnus-summary-mark-as-read-forward
2849      "mail/not-spam" nil
2850      :visible (and (fboundp 'spam-group-spam-contents-p)
2851                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2852     ;;
2853     (gnus-summary-exit "exit")
2854     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2855     (gnus-info-find-node "help"))
2856   "List of functions for the summary tool bar (GNOME style).
2857
2858 See `gmm-tool-bar-from-list' for the format of the list."
2859   :type '(repeat gmm-tool-bar-item)
2860   :version "23.1" ;; No Gnus
2861   :initialize 'custom-initialize-default
2862   :set 'gnus-summary-tool-bar-update
2863   :group 'gnus-summary)
2864
2865 (defcustom gnus-summary-tool-bar-retro
2866   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2867     (gnus-summary-next-unread-article "gnus/next-ur")
2868     (gnus-summary-post-news "gnus/post")
2869     (gnus-summary-followup-with-original "gnus/fuwo")
2870     (gnus-summary-followup "gnus/followup")
2871     (gnus-summary-reply-with-original "gnus/reply-wo")
2872     (gnus-summary-reply "gnus/reply")
2873     (gnus-summary-caesar-message "gnus/rot13")
2874     (gnus-uu-decode-uu "gnus/uu-decode")
2875     (gnus-summary-save-article-file "gnus/save-aif")
2876     (gnus-summary-save-article "gnus/save-art")
2877     (gnus-uu-post-news "gnus/uu-post")
2878     (gnus-summary-catchup "gnus/catchup")
2879     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2880     (gnus-summary-exit "gnus/exit-summ")
2881     ;; Some new command that may need more suitable icons:
2882     (gnus-summary-print-article "gnus/print" nil :visible nil)
2883     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2884     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2885     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2886     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2887     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2888     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2889     ;;
2890     (gnus-info-find-node "gnus/help" nil :visible nil))
2891   "List of functions for the summary tool bar (retro look).
2892
2893 See `gmm-tool-bar-from-list' for the format of the list."
2894   :type '(repeat gmm-tool-bar-item)
2895   :version "23.1" ;; No Gnus
2896   :initialize 'custom-initialize-default
2897   :set 'gnus-summary-tool-bar-update
2898   :group 'gnus-summary)
2899
2900 (defcustom gnus-summary-tool-bar-zap-list t
2901   "List of icon items from the global tool bar.
2902 These items are not displayed in the Gnus summary mode tool bar.
2903
2904 See `gmm-tool-bar-from-list' for the format of the list."
2905   :type 'gmm-tool-bar-zap-list
2906   :version "23.1" ;; No Gnus
2907   :initialize 'custom-initialize-default
2908   :set 'gnus-summary-tool-bar-update
2909   :group 'gnus-summary)
2910
2911 (defvar image-load-path)
2912 (defvar tool-bar-map)
2913
2914 (defun gnus-summary-make-tool-bar (&optional force)
2915   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2916 When FORCE, rebuild the tool bar."
2917   (when (and (not (featurep 'xemacs))
2918              (boundp 'tool-bar-mode)
2919              tool-bar-mode
2920              (or (not gnus-summary-tool-bar-map) force))
2921     (let* ((load-path
2922             (gmm-image-load-path-for-library "gnus"
2923                                              "mail/save.xpm"
2924                                              nil t))
2925            (image-load-path (cons (car load-path)
2926                                   (when (boundp 'image-load-path)
2927                                     image-load-path)))
2928            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2929                                         gnus-summary-tool-bar-zap-list
2930                                         'gnus-summary-mode-map)))
2931       (when map
2932         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2933         ;; uses it's value.
2934         (setq gnus-summary-tool-bar-map map))))
2935   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2936
2937 (defun gnus-score-set-default (var value)
2938   "A version of set that updates the GNU Emacs menu-bar."
2939   (set var value)
2940   ;; It is the message that forces the active status to be updated.
2941   (message ""))
2942
2943 (defun gnus-make-score-map (type)
2944   "Make a summary score map of type TYPE."
2945   (if t
2946       nil
2947     (let ((headers '(("author" "from" string)
2948                      ("subject" "subject" string)
2949                      ("article body" "body" string)
2950                      ("article head" "head" string)
2951                      ("xref" "xref" string)
2952                      ("extra header" "extra" string)
2953                      ("lines" "lines" number)
2954                      ("followups to author" "followup" string)))
2955           (types '((number ("less than" <)
2956                            ("greater than" >)
2957                            ("equal" =))
2958                    (string ("substring" s)
2959                            ("exact string" e)
2960                            ("fuzzy string" f)
2961                            ("regexp" r))))
2962           (perms '(("temporary" (current-time-string))
2963                    ("permanent" nil)
2964                    ("immediate" now)))
2965           header)
2966       (list
2967        (apply
2968         'nconc
2969         (list
2970          (if (eq type 'lower)
2971              "Lower score"
2972            "Increase score"))
2973         (let (outh)
2974           (while headers
2975             (setq header (car headers))
2976             (setq outh
2977                   (cons
2978                    (apply
2979                     'nconc
2980                     (list (car header))
2981                     (let ((ts (cdr (assoc (nth 2 header) types)))
2982                           outt)
2983                       (while ts
2984                         (setq outt
2985                               (cons
2986                                (apply
2987                                 'nconc
2988                                 (list (caar ts))
2989                                 (let ((ps perms)
2990                                       outp)
2991                                   (while ps
2992                                     (setq outp
2993                                           (cons
2994                                            (vector
2995                                             (caar ps)
2996                                             (list
2997                                              'gnus-summary-score-entry
2998                                              (nth 1 header)
2999                                              (if (or (string= (nth 1 header)
3000                                                               "head")
3001                                                      (string= (nth 1 header)
3002                                                               "body"))
3003                                                  ""
3004                                                (list 'gnus-summary-header
3005                                                      (nth 1 header)))
3006                                              (list 'quote (nth 1 (car ts)))
3007                                              (list 'gnus-score-delta-default
3008                                                    nil)
3009                                              (nth 1 (car ps))
3010                                              t)
3011                                             t)
3012                                            outp))
3013                                     (setq ps (cdr ps)))
3014                                   (list (nreverse outp))))
3015                                outt))
3016                         (setq ts (cdr ts)))
3017                       (list (nreverse outt))))
3018                    outh))
3019             (setq headers (cdr headers)))
3020           (list (nreverse outh))))))))
3021
3022
3023 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3024 (defvar bookmark-make-record-function)
3025 \f
3026
3027 (defun gnus-summary-mode (&optional group)
3028   "Major mode for reading articles.
3029
3030 All normal editing commands are switched off.
3031 \\<gnus-summary-mode-map>
3032 Each line in this buffer represents one article.  To read an
3033 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3034 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3035 respectively.
3036
3037 You can also post articles and send mail from this buffer.  To
3038 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3039 of an article, type `\\[gnus-summary-reply]'.
3040
3041 There are approx. one gazillion commands you can execute in this
3042 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3043
3044 The following commands are available:
3045
3046 \\{gnus-summary-mode-map}"
3047   (interactive)
3048   (kill-all-local-variables)
3049   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3050     (gnus-summary-make-local-variables))
3051   (gnus-summary-make-local-variables)
3052   (setq gnus-newsgroup-name group)
3053   (when (gnus-visual-p 'summary-menu 'menu)
3054     (gnus-summary-make-menu-bar)
3055     (gnus-summary-make-tool-bar))
3056   (gnus-make-thread-indent-array)
3057   (gnus-simplify-mode-line)
3058   (setq major-mode 'gnus-summary-mode)
3059   (setq mode-name "Summary")
3060   (use-local-map gnus-summary-mode-map)
3061   (buffer-disable-undo)
3062   (setq buffer-read-only t              ;Disable modification
3063         show-trailing-whitespace nil)
3064   (setq truncate-lines t)
3065   (add-to-invisibility-spec '(gnus-sum . t))
3066   (gnus-summary-set-display-table)
3067   (gnus-set-default-directory)
3068   (make-local-variable 'gnus-summary-line-format)
3069   (make-local-variable 'gnus-summary-line-format-spec)
3070   (make-local-variable 'gnus-summary-dummy-line-format)
3071   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3072   (make-local-variable 'gnus-summary-mark-positions)
3073   (gnus-make-local-hook 'pre-command-hook)
3074   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3075   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3076   (turn-on-gnus-mailing-list-mode)
3077   (mm-enable-multibyte)
3078   (set (make-local-variable 'bookmark-make-record-function)
3079        'gnus-summary-bookmark-make-record)
3080   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3081   (gnus-update-summary-mark-positions))
3082
3083 (defun gnus-summary-make-local-variables ()
3084   "Make all the local summary buffer variables."
3085   (let (global)
3086     (dolist (local gnus-summary-local-variables)
3087       (if (consp local)
3088           (progn
3089             (if (eq (cdr local) 'global)
3090                 ;; Copy the global value of the variable.
3091                 (setq global (symbol-value (car local)))
3092               ;; Use the value from the list.
3093               (setq global (eval (cdr local))))
3094             (set (make-local-variable (car local)) global))
3095         ;; Simple nil-valued local variable.
3096         (set (make-local-variable local) nil)))))
3097
3098 (defun gnus-summary-clear-local-variables ()
3099   (let ((locals gnus-summary-local-variables))
3100     (while locals
3101       (if (consp (car locals))
3102           (and (symbolp (caar locals))
3103                (set (caar locals) nil))
3104         (and (symbolp (car locals))
3105              (set (car locals) nil)))
3106       (setq locals (cdr locals)))))
3107
3108 ;; Summary data functions.
3109
3110 (defmacro gnus-data-number (data)
3111   `(car ,data))
3112
3113 (defmacro gnus-data-set-number (data number)
3114   `(setcar ,data ,number))
3115
3116 (defmacro gnus-data-mark (data)
3117   `(nth 1 ,data))
3118
3119 (defmacro gnus-data-set-mark (data mark)
3120   `(setcar (nthcdr 1 ,data) ,mark))
3121
3122 (defmacro gnus-data-pos (data)
3123   `(nth 2 ,data))
3124
3125 (defmacro gnus-data-set-pos (data pos)
3126   `(setcar (nthcdr 2 ,data) ,pos))
3127
3128 (defmacro gnus-data-header (data)
3129   `(nth 3 ,data))
3130
3131 (defmacro gnus-data-set-header (data header)
3132   `(setf (nth 3 ,data) ,header))
3133
3134 (defmacro gnus-data-level (data)
3135   `(nth 4 ,data))
3136
3137 (defmacro gnus-data-unread-p (data)
3138   `(= (nth 1 ,data) gnus-unread-mark))
3139
3140 (defmacro gnus-data-read-p (data)
3141   `(/= (nth 1 ,data) gnus-unread-mark))
3142
3143 (defmacro gnus-data-pseudo-p (data)
3144   `(consp (nth 3 ,data)))
3145
3146 (defmacro gnus-data-find (number)
3147   `(assq ,number gnus-newsgroup-data))
3148
3149 (defmacro gnus-data-find-list (number &optional data)
3150   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3151      (memq (assq ,number bdata)
3152            bdata)))
3153
3154 (defmacro gnus-data-make (number mark pos header level)
3155   `(list ,number ,mark ,pos ,header ,level))
3156
3157 (defun gnus-data-enter (after-article number mark pos header level offset)
3158   (let ((data (gnus-data-find-list after-article)))
3159     (unless data
3160       (error "No such article: %d" after-article))
3161     (setcdr data (cons (gnus-data-make number mark pos header level)
3162                        (cdr data)))
3163     (setq gnus-newsgroup-data-reverse nil)
3164     (gnus-data-update-list (cddr data) offset)))
3165
3166 (defun gnus-data-enter-list (after-article list &optional offset)
3167   (when list
3168     (let ((data (and after-article (gnus-data-find-list after-article)))
3169           (ilist list))
3170       (if (not (or data
3171                    after-article))
3172           (let ((odata gnus-newsgroup-data))
3173             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3174             (when offset
3175               (gnus-data-update-list odata offset)))
3176         ;; Find the last element in the list to be spliced into the main
3177         ;; list.
3178         (setq list (last list))
3179         (if (not data)
3180             (progn
3181               (setcdr list gnus-newsgroup-data)
3182               (setq gnus-newsgroup-data ilist)
3183               (when offset
3184                 (gnus-data-update-list (cdr list) offset)))
3185           (setcdr list (cdr data))
3186           (setcdr data ilist)
3187           (when offset
3188             (gnus-data-update-list (cdr list) offset))))
3189       (setq gnus-newsgroup-data-reverse nil))))
3190
3191 (defun gnus-data-remove (article &optional offset)
3192   (let ((data gnus-newsgroup-data))
3193     (if (= (gnus-data-number (car data)) article)
3194         (progn
3195           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3196                 gnus-newsgroup-data-reverse nil)
3197           (when offset
3198             (gnus-data-update-list gnus-newsgroup-data offset)))
3199       (while (cdr data)
3200         (when (= (gnus-data-number (cadr data)) article)
3201           (setcdr data (cddr data))
3202           (when offset
3203             (gnus-data-update-list (cdr data) offset))
3204           (setq data nil
3205                 gnus-newsgroup-data-reverse nil))
3206         (setq data (cdr data))))))
3207
3208 (defmacro gnus-data-list (backward)
3209   `(if ,backward
3210        (or gnus-newsgroup-data-reverse
3211            (setq gnus-newsgroup-data-reverse
3212                  (reverse gnus-newsgroup-data)))
3213      gnus-newsgroup-data))
3214
3215 (defun gnus-data-update-list (data offset)
3216   "Add OFFSET to the POS of all data entries in DATA."
3217   (setq gnus-newsgroup-data-reverse nil)
3218   (while data
3219     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3220     (setq data (cdr data))))
3221
3222 (defun gnus-summary-article-pseudo-p (article)
3223   "Say whether this article is a pseudo article or not."
3224   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3225
3226 (defmacro gnus-summary-article-sparse-p (article)
3227   "Say whether this article is a sparse article or not."
3228   `(memq ,article gnus-newsgroup-sparse))
3229
3230 (defmacro gnus-summary-article-ancient-p (article)
3231   "Say whether this article is a sparse article or not."
3232   `(memq ,article gnus-newsgroup-ancient))
3233
3234 (defun gnus-article-parent-p (number)
3235   "Say whether this article is a parent or not."
3236   (let ((data (gnus-data-find-list number)))
3237     (and (cdr data)              ; There has to be an article after...
3238          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3239             (gnus-data-level (nth 1 data))))))
3240
3241 (defun gnus-article-children (number)
3242   "Return a list of all children to NUMBER."
3243   (let* ((data (gnus-data-find-list number))
3244          (level (gnus-data-level (car data)))
3245          children)
3246     (setq data (cdr data))
3247     (while (and data
3248                 (= (gnus-data-level (car data)) (1+ level)))
3249       (push (gnus-data-number (car data)) children)
3250       (setq data (cdr data)))
3251     children))
3252
3253 (defmacro gnus-summary-skip-intangible ()
3254   "If the current article is intangible, then jump to a different article."
3255   '(let ((to (get-text-property (point) 'gnus-intangible)))
3256      (and to (gnus-summary-goto-subject to))))
3257
3258 (defmacro gnus-summary-article-intangible-p ()
3259   "Say whether this article is intangible or not."
3260   '(get-text-property (point) 'gnus-intangible))
3261
3262 (defun gnus-article-read-p (article)
3263   "Say whether ARTICLE is read or not."
3264   (not (or (memq article gnus-newsgroup-marked)
3265            (memq article gnus-newsgroup-spam-marked)
3266            (memq article gnus-newsgroup-unreads)
3267            (memq article gnus-newsgroup-unselected)
3268            (memq article gnus-newsgroup-dormant))))
3269
3270 ;; Some summary mode macros.
3271
3272 (defmacro gnus-summary-article-number ()
3273   "The article number of the article on the current line.
3274 If there isn't an article number here, then we return the current
3275 article number."
3276   '(progn
3277      (gnus-summary-skip-intangible)
3278      (or (get-text-property (point) 'gnus-number)
3279          (gnus-summary-last-subject))))
3280
3281 (defmacro gnus-summary-article-header (&optional number)
3282   "Return the header of article NUMBER."
3283   `(gnus-data-header (gnus-data-find
3284                       ,(or number '(gnus-summary-article-number)))))
3285
3286 (defmacro gnus-summary-thread-level (&optional number)
3287   "Return the level of thread that starts with article NUMBER."
3288   `(if (and (eq gnus-summary-make-false-root 'dummy)
3289             (get-text-property (point) 'gnus-intangible))
3290        0
3291      (gnus-data-level (gnus-data-find
3292                        ,(or number '(gnus-summary-article-number))))))
3293
3294 (defmacro gnus-summary-article-mark (&optional number)
3295   "Return the mark of article NUMBER."
3296   `(gnus-data-mark (gnus-data-find
3297                     ,(or number '(gnus-summary-article-number)))))
3298
3299 (defmacro gnus-summary-article-pos (&optional number)
3300   "Return the position of the line of article NUMBER."
3301   `(gnus-data-pos (gnus-data-find
3302                    ,(or number '(gnus-summary-article-number)))))
3303
3304 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3305 (defmacro gnus-summary-article-subject (&optional number)
3306   "Return current subject string or nil if nothing."
3307   `(let ((headers
3308           ,(if number
3309                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3310              '(gnus-data-header (assq (gnus-summary-article-number)
3311                                       gnus-newsgroup-data)))))
3312      (and headers
3313           (vectorp headers)
3314           (mail-header-subject headers))))
3315
3316 (defmacro gnus-summary-article-score (&optional number)
3317   "Return current article score."
3318   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3319                   gnus-newsgroup-scored))
3320        gnus-summary-default-score 0))
3321
3322 (defun gnus-summary-article-children (&optional number)
3323   "Return a list of article numbers that are children of article NUMBER."
3324   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3325          (level (gnus-data-level (car data)))
3326          l children)
3327     (while (and (setq data (cdr data))
3328                 (> (setq l (gnus-data-level (car data))) level))
3329       (and (= (1+ level) l)
3330            (push (gnus-data-number (car data))
3331                  children)))
3332     (nreverse children)))
3333
3334 (defun gnus-summary-article-parent (&optional number)
3335   "Return the article number of the parent of article NUMBER."
3336   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3337                                     (gnus-data-list t)))
3338          (level (gnus-data-level (car data))))
3339     (if (zerop level)
3340         ()                              ; This is a root.
3341       ;; We search until we find an article with a level less than
3342       ;; this one.  That function has to be the parent.
3343       (while (and (setq data (cdr data))
3344                   (not (< (gnus-data-level (car data)) level))))
3345       (and data (gnus-data-number (car data))))))
3346
3347 (defun gnus-unread-mark-p (mark)
3348   "Say whether MARK is the unread mark."
3349   (= mark gnus-unread-mark))
3350
3351 (defun gnus-read-mark-p (mark)
3352   "Say whether MARK is one of the marks that mark as read.
3353 This is all marks except unread, ticked, dormant, and expirable."
3354   (not (or (= mark gnus-unread-mark)
3355            (= mark gnus-ticked-mark)
3356            (= mark gnus-spam-mark)
3357            (= mark gnus-dormant-mark)
3358            (= mark gnus-expirable-mark))))
3359
3360 (defmacro gnus-article-mark (number)
3361   "Return the MARK of article NUMBER.
3362 This macro should only be used when computing the mark the \"first\"
3363 time; i.e., when generating the summary lines.  After that,
3364 `gnus-summary-article-mark' should be used to examine the
3365 marks of articles."
3366   `(cond
3367     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3368     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3369     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3370     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3371     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3372     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3373     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3374     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3375            gnus-ancient-mark))))
3376
3377 ;; Saving hidden threads.
3378
3379 (defmacro gnus-save-hidden-threads (&rest forms)
3380   "Save hidden threads, eval FORMS, and restore the hidden threads."
3381   (let ((config (make-symbol "config")))
3382     `(let ((,config (gnus-hidden-threads-configuration)))
3383        (unwind-protect
3384            (save-excursion
3385              ,@forms)
3386          (gnus-restore-hidden-threads-configuration ,config)))))
3387 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3388 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3389
3390 (defun gnus-data-compute-positions ()
3391   "Compute the positions of all articles."
3392   (setq gnus-newsgroup-data-reverse nil)
3393   (let ((data gnus-newsgroup-data))
3394     (save-excursion
3395       (gnus-save-hidden-threads
3396         (gnus-summary-show-all-threads)
3397         (goto-char (point-min))
3398         (while data
3399           (while (get-text-property (point) 'gnus-intangible)
3400             (forward-line 1))
3401           (gnus-data-set-pos (car data) (+ (point) 3))
3402           (setq data (cdr data))
3403           (forward-line 1))))))
3404
3405 (defun gnus-hidden-threads-configuration ()
3406   "Return the current hidden threads configuration."
3407   (save-excursion
3408     (let (config)
3409       (goto-char (point-min))
3410       (while (not (eobp))
3411         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3412           (push (save-excursion (forward-line 0) (point)) config))
3413         (forward-line 1))
3414       config)))
3415
3416 (defun gnus-restore-hidden-threads-configuration (config)
3417   "Restore hidden threads configuration from CONFIG."
3418   (save-excursion
3419     (let (point (inhibit-read-only t))
3420       (while (setq point (pop config))
3421         (goto-char point)
3422         (gnus-summary-hide-thread)))))
3423
3424 ;; Various summary mode internalish functions.
3425
3426 (defun gnus-mouse-pick-article (e)
3427   (interactive "e")
3428   (mouse-set-point e)
3429   (gnus-summary-next-page nil t))
3430
3431 (defun gnus-summary-set-display-table ()
3432   "Change the display table.
3433 Odd characters have a tendency to mess
3434 up nicely formatted displays - we make all possible glyphs
3435 display only a single character."
3436
3437   ;; We start from the standard display table, if any.
3438   (let ((table (or (copy-sequence standard-display-table)
3439                    (make-display-table)))
3440         (i 32))
3441     ;; Nix out all the control chars...
3442     (while (>= (setq i (1- i)) 0)
3443       (gnus-put-display-table i [??] table))
3444    ;; ... but not newline and cr, of course.  (cr is necessary for the
3445     ;; selective display).
3446     (gnus-put-display-table ?\n nil table)
3447     (gnus-put-display-table ?\r nil table)
3448     ;; We keep TAB as well.
3449     (gnus-put-display-table ?\t nil table)
3450     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3451     ;; Emacs 23 (unicode), that are not set already.
3452     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3453                  160
3454                256)))
3455       (while (>= (setq i (1- i)) 127)
3456         ;; Only modify if the entry is nil.
3457         (unless (gnus-get-display-table i table)
3458           (gnus-put-display-table i [??] table))))
3459     (setq buffer-display-table table)))
3460
3461 (defun gnus-summary-set-article-display-arrow (pos)
3462   "Update the overlay arrow to point to line at position POS."
3463   (when gnus-summary-display-arrow
3464     (make-local-variable 'overlay-arrow-position)
3465     (make-local-variable 'overlay-arrow-string)
3466     (save-excursion
3467       (goto-char pos)
3468       (beginning-of-line)
3469       (unless overlay-arrow-position
3470         (setq overlay-arrow-position (make-marker)))
3471       (setq overlay-arrow-string "=>"
3472             overlay-arrow-position (set-marker overlay-arrow-position
3473                                                (point)
3474                                                (current-buffer))))))
3475
3476 (defun gnus-summary-setup-buffer (group)
3477   "Initialize summary buffer."
3478   (let ((buffer (gnus-summary-buffer-name group))
3479         (dead-name (concat "*Dead Summary "
3480                            (gnus-group-decoded-name group) "*")))
3481     ;; If a dead summary buffer exists, we kill it.
3482     (when (gnus-buffer-live-p dead-name)
3483       (gnus-kill-buffer dead-name))
3484     (if (get-buffer buffer)
3485         (progn
3486           (set-buffer buffer)
3487           (setq gnus-summary-buffer (current-buffer))
3488           (not gnus-newsgroup-prepared))
3489       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3490       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3491       (gnus-summary-mode group)
3492       (when gnus-carpal
3493         (gnus-carpal-setup-buffer 'summary))
3494       (when (gnus-group-quit-config group)
3495         (set (make-local-variable 'gnus-single-article-buffer) nil))
3496       (make-local-variable 'gnus-article-buffer)
3497       (make-local-variable 'gnus-article-current)
3498       (make-local-variable 'gnus-original-article-buffer)
3499       (setq gnus-newsgroup-name group)
3500       ;; Set any local variables in the group parameters.
3501       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3502       t)))
3503
3504 (defun gnus-set-global-variables ()
3505   "Set the global equivalents of the buffer-local variables.
3506 They are set to the latest values they had.  These reflect the summary
3507 buffer that was in action when the last article was fetched."
3508   (when (eq major-mode 'gnus-summary-mode)
3509     (setq gnus-summary-buffer (current-buffer))
3510     (let ((name gnus-newsgroup-name)
3511           (marked gnus-newsgroup-marked)
3512           (spam gnus-newsgroup-spam-marked)
3513           (unread gnus-newsgroup-unreads)
3514           (headers gnus-current-headers)
3515           (data gnus-newsgroup-data)
3516           (summary gnus-summary-buffer)
3517           (article-buffer gnus-article-buffer)
3518           (original gnus-original-article-buffer)
3519           (gac gnus-article-current)
3520           (reffed gnus-reffed-article-number)
3521           (score-file gnus-current-score-file)
3522           (default-charset gnus-newsgroup-charset)
3523           vlist)
3524       (let ((locals gnus-newsgroup-variables))
3525         (while locals
3526           (if (consp (car locals))
3527               (push (eval (caar locals)) vlist)
3528             (push (eval (car locals)) vlist))
3529           (setq locals (cdr locals)))
3530         (setq vlist (nreverse vlist)))
3531       (with-current-buffer gnus-group-buffer
3532         (setq gnus-newsgroup-name name
3533               gnus-newsgroup-marked marked
3534               gnus-newsgroup-spam-marked spam
3535               gnus-newsgroup-unreads unread
3536               gnus-current-headers headers
3537               gnus-newsgroup-data data
3538               gnus-article-current gac
3539               gnus-summary-buffer summary
3540               gnus-article-buffer article-buffer
3541               gnus-original-article-buffer original
3542               gnus-reffed-article-number reffed
3543               gnus-current-score-file score-file
3544               gnus-newsgroup-charset default-charset)
3545         (let ((locals gnus-newsgroup-variables))
3546           (while locals
3547             (if (consp (car locals))
3548                 (set (caar locals) (pop vlist))
3549               (set (car locals) (pop vlist)))
3550             (setq locals (cdr locals))))
3551         ;; The article buffer also has local variables.
3552         (when (gnus-buffer-live-p gnus-article-buffer)
3553           (set-buffer gnus-article-buffer)
3554           (setq gnus-summary-buffer summary))))))
3555
3556 (defun gnus-summary-article-unread-p (article)
3557   "Say whether ARTICLE is unread or not."
3558   (memq article gnus-newsgroup-unreads))
3559
3560 (defun gnus-summary-first-article-p (&optional article)
3561   "Return whether ARTICLE is the first article in the buffer."
3562   (if (not (setq article (or article (gnus-summary-article-number))))
3563       nil
3564     (eq article (caar gnus-newsgroup-data))))
3565
3566 (defun gnus-summary-last-article-p (&optional article)
3567   "Return whether ARTICLE is the last article in the buffer."
3568   (if (not (setq article (or article (gnus-summary-article-number))))
3569       ;; All non-existent numbers are the last article.  :-)
3570       t
3571     (not (cdr (gnus-data-find-list article)))))
3572
3573 (defun gnus-make-thread-indent-array (&optional n)
3574   (when (or n
3575             (progn (setq n 200) nil)
3576             (null gnus-thread-indent-array)
3577             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3578     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3579           gnus-thread-indent-array-level gnus-thread-indent-level)
3580     (while (>= n 0)
3581       (aset gnus-thread-indent-array n
3582             (make-string (* n gnus-thread-indent-level) ? ))
3583       (setq n (1- n)))))
3584
3585 (defun gnus-update-summary-mark-positions ()
3586   "Compute where the summary marks are to go."
3587   (save-excursion
3588     (when (gnus-buffer-exists-p gnus-summary-buffer)
3589       (set-buffer gnus-summary-buffer))
3590     (let ((spec gnus-summary-line-format-spec)
3591           pos)
3592       (save-excursion
3593         (gnus-set-work-buffer)
3594         (let ((gnus-tmp-unread ?Z)
3595               (gnus-replied-mark ?Z)
3596               (gnus-score-below-mark ?Z)
3597               (gnus-score-over-mark ?Z)
3598               (gnus-undownloaded-mark ?Z)
3599               (gnus-summary-line-format-spec spec)
3600               (gnus-newsgroup-downloadable '(0))
3601               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3602               case-fold-search ignores)
3603           ;; Here, all marks are bound to Z.
3604           (gnus-summary-insert-line header
3605                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3606           (goto-char (point-min))
3607           ;; Memorize the positions of the same characters as dummy marks.
3608           (while (re-search-forward "[A-D]" nil t)
3609             (push (point) ignores))
3610           (erase-buffer)
3611           ;; We use A-D as dummy marks in order to know column positions
3612           ;; where marks should be inserted.
3613           (setq gnus-tmp-unread ?A
3614                 gnus-replied-mark ?B
3615                 gnus-score-below-mark ?C
3616                 gnus-score-over-mark ?C
3617                 gnus-undownloaded-mark ?D)
3618           (gnus-summary-insert-line header
3619                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3620           ;; Ignore characters which aren't dummy marks.
3621           (dolist (p ignores)
3622             (delete-region (goto-char (1- p)) p)
3623             (insert ?Z))
3624           (goto-char (point-min))
3625           (setq pos (list (cons 'unread
3626                                 (and (search-forward "A" nil t)
3627                                      (- (point) (point-min) 1)))))
3628           (goto-char (point-min))
3629           (push (cons 'replied (and (search-forward "B" nil t)
3630                                     (- (point) (point-min) 1)))
3631                 pos)
3632           (goto-char (point-min))
3633           (push (cons 'score (and (search-forward "C" nil t)
3634                                   (- (point) (point-min) 1)))
3635                 pos)
3636           (goto-char (point-min))
3637           (push (cons 'download (and (search-forward "D" nil t)
3638                                      (- (point) (point-min) 1)))
3639                 pos)))
3640       (setq gnus-summary-mark-positions pos))))
3641
3642 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3643   "Insert a dummy root in the summary buffer."
3644   (beginning-of-line)
3645   (gnus-add-text-properties
3646    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3647    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3648
3649 (defun gnus-summary-extract-address-component (from)
3650   (or (car (funcall gnus-extract-address-components from))
3651       from))
3652
3653 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3654   (let ((mail-parse-charset gnus-newsgroup-charset)
3655         (ignored-from-addresses (gnus-ignored-from-addresses))
3656         ; Is it really necessary to do this next part for each summary line?
3657         ; Luckily, doesn't seem to slow things down much.
3658         (mail-parse-ignored-charsets
3659          (with-current-buffer gnus-summary-buffer
3660            gnus-newsgroup-ignored-charsets)))
3661     (or
3662      (and ignored-from-addresses
3663           (string-match ignored-from-addresses gnus-tmp-from)
3664           (let ((extra-headers (mail-header-extra header))
3665                 to
3666                 newsgroups)
3667             (cond
3668              ((setq to (cdr (assq 'To extra-headers)))
3669               (concat gnus-summary-to-prefix
3670                       (inline
3671                         (gnus-summary-extract-address-component
3672                          (funcall gnus-decode-encoded-address-function to)))))
3673              ((setq newsgroups
3674                     (or
3675                      (cdr (assq 'Newsgroups extra-headers))
3676                      (and
3677                       (memq 'Newsgroups gnus-extra-headers)
3678                       (eq (car (gnus-find-method-for-group
3679                                 gnus-newsgroup-name)) 'nntp)
3680                       (gnus-group-real-name gnus-newsgroup-name))))
3681               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3682      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3683
3684 (defun gnus-summary-insert-line (gnus-tmp-header
3685                                  gnus-tmp-level gnus-tmp-current
3686                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3687                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3688                                  &optional gnus-tmp-dummy gnus-tmp-score
3689                                  gnus-tmp-process)
3690   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3691       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3692                                           gnus-tmp-level)))
3693   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3694          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3695          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3696          (gnus-tmp-score-char
3697           (if (or (null gnus-summary-default-score)
3698                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3699                       gnus-summary-zcore-fuzz))
3700               ?                         ;Whitespace
3701             (if (< gnus-tmp-score gnus-summary-default-score)
3702                 gnus-score-below-mark gnus-score-over-mark)))
3703          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3704          (gnus-tmp-replied
3705           (cond (gnus-tmp-process gnus-process-mark)
3706                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3707                  gnus-cached-mark)
3708                 (gnus-tmp-replied gnus-replied-mark)
3709                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3710                  gnus-forwarded-mark)
3711                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3712                  gnus-saved-mark)
3713                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3714                  gnus-recent-mark)
3715                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3716                  gnus-unseen-mark)
3717                 (t gnus-no-mark)))
3718          (gnus-tmp-downloaded
3719           (cond (undownloaded
3720                  gnus-undownloaded-mark)
3721                 (gnus-newsgroup-agentized
3722                  gnus-downloaded-mark)
3723                 (t
3724                  gnus-no-mark)))
3725          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3726          (gnus-tmp-name
3727           (cond
3728            ((string-match "<[^>]+> *$" gnus-tmp-from)
3729             (let ((beg (match-beginning 0)))
3730               (or (and (string-match "^\".+\"" gnus-tmp-from)
3731                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3732                   (substring gnus-tmp-from 0 beg))))
3733            ((string-match "(.+)" gnus-tmp-from)
3734             (substring gnus-tmp-from
3735                        (1+ (match-beginning 0)) (1- (match-end 0))))
3736            (t gnus-tmp-from)))
3737          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3738          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3739          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3740          (inhibit-read-only t))
3741     (when (string= gnus-tmp-name "")
3742       (setq gnus-tmp-name gnus-tmp-from))
3743     (unless (numberp gnus-tmp-lines)
3744       (setq gnus-tmp-lines -1))
3745     (if (= gnus-tmp-lines -1)
3746         (setq gnus-tmp-lines "?")
3747       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3748     (condition-case ()
3749         (gnus-put-text-property
3750          (point)
3751          (progn (eval gnus-summary-line-format-spec) (point))
3752          'gnus-number gnus-tmp-number)
3753       (error (gnus-message 5 "Error updating the summary line")))
3754     (when (gnus-visual-p 'summary-highlight 'highlight)
3755       (forward-line -1)
3756       (gnus-run-hooks 'gnus-summary-update-hook)
3757       (forward-line 1))))
3758
3759 (defun gnus-summary-update-line (&optional dont-update)
3760   "Update summary line after change."
3761   (when (and gnus-summary-default-score
3762              (not gnus-summary-inhibit-highlight))
3763     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3764            (article (gnus-summary-article-number))
3765            (score (gnus-summary-article-score article)))
3766       (unless dont-update
3767         (if (and gnus-summary-mark-below
3768                  (< (gnus-summary-article-score)
3769                     gnus-summary-mark-below))
3770             ;; This article has a low score, so we mark it as read.
3771             (when (memq article gnus-newsgroup-unreads)
3772               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3773           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3774             ;; This article was previously marked as read on account
3775             ;; of a low score, but now it has risen, so we mark it as
3776             ;; unread.
3777             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3778         (gnus-summary-update-mark
3779          (if (or (null gnus-summary-default-score)
3780                  (<= (abs (- score gnus-summary-default-score))
3781                      gnus-summary-zcore-fuzz))
3782              ?                          ;Whitespace
3783            (if (< score gnus-summary-default-score)
3784                gnus-score-below-mark gnus-score-over-mark))
3785          'score))
3786       ;; Do visual highlighting.
3787       (when (gnus-visual-p 'summary-highlight 'highlight)
3788         (gnus-run-hooks 'gnus-summary-update-hook)))))
3789
3790 (defvar gnus-tmp-new-adopts nil)
3791
3792 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3793   "Return the number of articles in THREAD.
3794 This may be 0 in some cases -- if none of the articles in
3795 the thread are to be displayed."
3796   (let* ((number
3797          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3798           (cond
3799            ((not (listp thread))
3800             1)
3801            ((and (consp thread) (cdr thread))
3802             (apply
3803              '+ 1 (mapcar
3804                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3805            ((null thread)
3806             1)
3807            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3808             1)
3809            (t 0))))
3810     (when (and level (zerop level) gnus-tmp-new-adopts)
3811       (incf number
3812             (apply '+ (mapcar
3813                        'gnus-summary-number-of-articles-in-thread
3814                        gnus-tmp-new-adopts))))
3815     (if char
3816         (if (> number 1) gnus-not-empty-thread-mark
3817           gnus-empty-thread-mark)
3818       number)))
3819
3820 (defsubst gnus-summary-line-message-size (head)
3821   "Return pretty-printed version of message size.
3822 This function is intended to be used in
3823 `gnus-summary-line-format-alist'."
3824   (let ((c (or (mail-header-chars head) -1)))
3825     (cond ((< c 0) "n/a")               ; chars not available
3826           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3827           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3828           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3829           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3830
3831
3832 (defun gnus-summary-set-local-parameters (group)
3833   "Go through the local params of GROUP and set all variable specs in that list."
3834   (let ((vars '(quit-config)))          ; Ignore quit-config.
3835     (dolist (elem (gnus-group-find-parameter group))
3836       (and (consp elem)                 ; Has to be a cons.
3837            (consp (cdr elem))           ; The cdr has to be a list.
3838            (symbolp (car elem))         ; Has to be a symbol in there.
3839            (not (memq (car elem) vars))
3840            (ignore-errors
3841              (push (car elem) vars)
3842              ;; Variables like `gnus-show-threads' that are globally
3843              ;; bound, if used as group parameters, need to get to be
3844              ;; buffer-local, whereas just parameters like `gcc-self',
3845              ;; `timestamp', etc. should not be bound as variables.
3846              (if (boundp (car elem))
3847                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3848                (eval (nth 1 elem))))))))
3849
3850 (defun gnus-summary-read-group (group &optional show-all no-article
3851                                       kill-buffer no-display backward
3852                                       select-articles)
3853   "Start reading news in newsgroup GROUP.
3854 If SHOW-ALL is non-nil, already read articles are also listed.
3855 If NO-ARTICLE is non-nil, no article is selected initially.
3856 If NO-DISPLAY, don't generate a summary buffer."
3857   (let (result)
3858     (while (and group
3859                 (null (setq result
3860                             (let ((gnus-auto-select-next nil))
3861                               (or (gnus-summary-read-group-1
3862                                    group show-all no-article
3863                                    kill-buffer no-display
3864                                    select-articles)
3865                                   (setq show-all nil
3866                                         select-articles nil)))))
3867                 (eq gnus-auto-select-next 'quietly))
3868       (set-buffer gnus-group-buffer)
3869       ;; The entry function called above goes to the next
3870       ;; group automatically, so we go two groups back
3871       ;; if we are searching for the previous group.
3872       (when backward
3873         (gnus-group-prev-unread-group 2))
3874       (if (not (equal group (gnus-group-group-name)))
3875           (setq group (gnus-group-group-name))
3876         (setq group nil)))
3877     result))
3878
3879 (defun gnus-summary-read-group-1 (group show-all no-article
3880                                         kill-buffer no-display
3881                                         &optional select-articles)
3882   ;; Killed foreign groups can't be entered.
3883   ;;  (when (and (not (gnus-group-native-p group))
3884   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3885   ;;    (error "Dead non-native groups can't be entered"))
3886   (gnus-message 5 "Retrieving newsgroup: %s..."
3887                 (gnus-group-decoded-name group))
3888   (let* ((new-group (gnus-summary-setup-buffer group))
3889          (quit-config (gnus-group-quit-config group))
3890          (did-select (and new-group (gnus-select-newsgroup
3891                                      group show-all select-articles))))
3892     (cond
3893      ;; This summary buffer exists already, so we just select it.
3894      ((not new-group)
3895       (gnus-set-global-variables)
3896       (when kill-buffer
3897         (gnus-kill-or-deaden-summary kill-buffer))
3898       (gnus-configure-windows 'summary 'force)
3899       (gnus-set-mode-line 'summary)
3900       (gnus-summary-position-point)
3901       (message "")
3902       t)
3903      ;; We couldn't select this group.
3904      ((null did-select)
3905       (when (and (eq major-mode 'gnus-summary-mode)
3906                  (not (equal (current-buffer) kill-buffer)))
3907         (kill-buffer (current-buffer))
3908         (if (not quit-config)
3909             (progn
3910               ;; Update the info -- marks might need to be removed,
3911               ;; for instance.
3912               (gnus-summary-update-info)
3913               (set-buffer gnus-group-buffer)
3914               (gnus-group-jump-to-group group)
3915               (gnus-group-next-unread-group 1))
3916           (gnus-handle-ephemeral-exit quit-config)))
3917       (let ((grpinfo (gnus-get-info group)))
3918         (if (null (gnus-info-read grpinfo))
3919             (gnus-message 3 "Group %s contains no messages"
3920                           (gnus-group-decoded-name group))
3921           (gnus-message 3 "Can't select group")))
3922       nil)
3923      ;; The user did a `C-g' while prompting for number of articles,
3924      ;; so we exit this group.
3925      ((eq did-select 'quit)
3926       (and (eq major-mode 'gnus-summary-mode)
3927            (not (equal (current-buffer) kill-buffer))
3928            (kill-buffer (current-buffer)))
3929       (when kill-buffer
3930         (gnus-kill-or-deaden-summary kill-buffer))
3931       (if (not quit-config)
3932           (progn
3933             (set-buffer gnus-group-buffer)
3934             (gnus-group-jump-to-group group)
3935             (gnus-configure-windows 'group 'force))
3936         (gnus-handle-ephemeral-exit quit-config))
3937       ;; Finally signal the quit.
3938       (signal 'quit nil))
3939      ;; The group was successfully selected.
3940      (t
3941       (gnus-set-global-variables)
3942       ;; Save the active value in effect when the group was entered.
3943       (setq gnus-newsgroup-active
3944             (gnus-copy-sequence
3945              (gnus-active gnus-newsgroup-name)))
3946       ;; You can change the summary buffer in some way with this hook.
3947       (gnus-run-hooks 'gnus-select-group-hook)
3948       (when (memq 'summary (gnus-update-format-specifications
3949                             nil 'summary 'summary-mode 'summary-dummy))
3950         ;; The format specification for the summary line was updated,
3951         ;; so we need to update the mark positions as well.
3952         (gnus-update-summary-mark-positions))
3953       ;; Do score processing.
3954       (when gnus-use-scoring
3955         (gnus-possibly-score-headers))
3956       ;; Check whether to fill in the gaps in the threads.
3957       (when gnus-build-sparse-threads
3958         (gnus-build-sparse-threads))
3959       ;; Find the initial limit.
3960       (if show-all
3961           (let ((gnus-newsgroup-dormant nil))
3962             (gnus-summary-initial-limit show-all))
3963         (gnus-summary-initial-limit show-all))
3964       ;; Generate the summary buffer.
3965       (unless no-display
3966         (gnus-summary-prepare))
3967       (when gnus-use-trees
3968         (gnus-tree-open group)
3969         (setq gnus-summary-highlight-line-function
3970               'gnus-tree-highlight-article))
3971       ;; If the summary buffer is empty, but there are some low-scored
3972       ;; articles or some excluded dormants, we include these in the
3973       ;; buffer.
3974       (when (and (zerop (buffer-size))
3975                  (not no-display))
3976         (cond (gnus-newsgroup-dormant
3977                (gnus-summary-limit-include-dormant))
3978               ((and gnus-newsgroup-scored show-all)
3979                (gnus-summary-limit-include-expunged t))))
3980       ;; Function `gnus-apply-kill-file' must be called in this hook.
3981       (gnus-run-hooks 'gnus-apply-kill-hook)
3982       (if (and (zerop (buffer-size))
3983                (not no-display))
3984           (progn
3985             ;; This newsgroup is empty.
3986             (gnus-summary-catchup-and-exit nil t)
3987             (gnus-message 6 "No unread news")
3988             (when kill-buffer
3989               (gnus-kill-or-deaden-summary kill-buffer))
3990             ;; Return nil from this function.
3991             nil)
3992         ;; Hide conversation thread subtrees.  We cannot do this in
3993         ;; gnus-summary-prepare-hook since kill processing may not
3994         ;; work with hidden articles.
3995         (gnus-summary-maybe-hide-threads)
3996         (when kill-buffer
3997           (gnus-kill-or-deaden-summary kill-buffer))
3998         (gnus-summary-auto-select-subject)
3999         ;; Show first unread article if requested.
4000         (if (and (not no-article)
4001                  (not no-display)
4002                  gnus-newsgroup-unreads
4003                  gnus-auto-select-first)
4004             (progn
4005               (gnus-configure-windows 'summary)
4006               (let ((art (gnus-summary-article-number)))
4007                 (unless (and (not gnus-plugged)
4008                              (or (memq art gnus-newsgroup-undownloaded)
4009                                  (memq art gnus-newsgroup-downloadable)))
4010                   (gnus-summary-goto-article art))))
4011           ;; Don't select any articles.
4012           (gnus-summary-position-point)
4013           (gnus-configure-windows 'summary 'force)
4014           (gnus-set-mode-line 'summary))
4015         (when (and gnus-auto-center-group
4016                    (get-buffer-window gnus-group-buffer t))
4017           ;; Gotta use windows, because recenter does weird stuff if
4018           ;; the current buffer ain't the displayed window.
4019           (let ((owin (selected-window)))
4020             (select-window (get-buffer-window gnus-group-buffer t))
4021             (when (gnus-group-goto-group group)
4022               (recenter))
4023             (select-window owin)))
4024         ;; Mark this buffer as "prepared".
4025         (setq gnus-newsgroup-prepared t)
4026         (gnus-run-hooks 'gnus-summary-prepared-hook)
4027         (unless (gnus-ephemeral-group-p group)
4028           (gnus-group-update-group group))
4029         t)))))
4030
4031 (defun gnus-summary-auto-select-subject ()
4032   "Select the subject line on initial group entry."
4033   (goto-char (point-min))
4034   (cond
4035    ((eq gnus-auto-select-subject 'best)
4036     (gnus-summary-best-unread-subject))
4037    ((eq gnus-auto-select-subject 'unread)
4038     (gnus-summary-first-unread-subject))
4039    ((eq gnus-auto-select-subject 'unseen)
4040     (gnus-summary-first-unseen-subject))
4041    ((eq gnus-auto-select-subject 'unseen-or-unread)
4042     (gnus-summary-first-unseen-or-unread-subject))
4043    ((eq gnus-auto-select-subject 'first)
4044     ;; Do nothing.
4045     )
4046    ((functionp gnus-auto-select-subject)
4047     (funcall gnus-auto-select-subject))))
4048
4049 (defun gnus-summary-prepare ()
4050   "Generate the summary buffer."
4051   (interactive)
4052   (let ((inhibit-read-only t))
4053     (erase-buffer)
4054     (setq gnus-newsgroup-data nil
4055           gnus-newsgroup-data-reverse nil)
4056     (gnus-run-hooks 'gnus-summary-generate-hook)
4057     ;; Generate the buffer, either with threads or without.
4058     (when gnus-newsgroup-headers
4059       (gnus-summary-prepare-threads
4060        (if gnus-show-threads
4061            (gnus-sort-gathered-threads
4062             (funcall gnus-summary-thread-gathering-function
4063                      (gnus-sort-threads
4064                       (gnus-cut-threads (gnus-make-threads)))))
4065          ;; Unthreaded display.
4066          (gnus-sort-articles gnus-newsgroup-headers))))
4067     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4068     ;; Call hooks for modifying summary buffer.
4069     (goto-char (point-min))
4070     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4071
4072 (defsubst gnus-general-simplify-subject (subject)
4073   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4074   (setq subject
4075         (cond
4076          ;; Truncate the subject.
4077          (gnus-simplify-subject-functions
4078           (gnus-map-function gnus-simplify-subject-functions subject))
4079          ((numberp gnus-summary-gather-subject-limit)
4080           (setq subject (gnus-simplify-subject-re subject))
4081           (if (> (length subject) gnus-summary-gather-subject-limit)
4082               (substring subject 0 gnus-summary-gather-subject-limit)
4083             subject))
4084          ;; Fuzzily simplify it.
4085          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4086           (gnus-simplify-subject-fuzzy subject))
4087          ;; Just remove the leading "Re:".
4088          (t
4089           (gnus-simplify-subject-re subject))))
4090
4091   (if (and gnus-summary-gather-exclude-subject
4092            (string-match gnus-summary-gather-exclude-subject subject))
4093       nil                         ; This article shouldn't be gathered
4094     subject))
4095
4096 (defun gnus-summary-simplify-subject-query ()
4097   "Query where the respool algorithm would put this article."
4098   (interactive)
4099   (gnus-summary-select-article)
4100   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4101
4102 (defun gnus-gather-threads-by-subject (threads)
4103   "Gather threads by looking at Subject headers."
4104   (if (not gnus-summary-make-false-root)
4105       threads
4106     (let ((hashtb (gnus-make-hashtable 1024))
4107           (prev threads)
4108           (result threads)
4109           subject hthread whole-subject)
4110       (while threads
4111         (setq subject (gnus-general-simplify-subject
4112                        (setq whole-subject (mail-header-subject
4113                                             (caar threads)))))
4114         (when subject
4115           (if (setq hthread (gnus-gethash subject hashtb))
4116               (progn
4117                 ;; We enter a dummy root into the thread, if we
4118                 ;; haven't done that already.
4119                 (unless (stringp (caar hthread))
4120                   (setcar hthread (list whole-subject (car hthread))))
4121                 ;; We add this new gathered thread to this gathered
4122                 ;; thread.
4123                 (setcdr (car hthread)
4124                         (nconc (cdar hthread) (list (car threads))))
4125                 ;; Remove it from the list of threads.
4126                 (setcdr prev (cdr threads))
4127                 (setq threads prev))
4128             ;; Enter this thread into the hash table.
4129             (gnus-sethash subject
4130                           (if gnus-summary-make-false-root-always
4131                               (progn
4132                                 ;; If you want a dummy root above all
4133                                 ;; threads...
4134                                 (setcar threads (list whole-subject
4135                                                       (car threads)))
4136                                 threads)
4137                             threads)
4138                           hashtb)))
4139         (setq prev threads)
4140         (setq threads (cdr threads)))
4141       result)))
4142
4143 (defun gnus-gather-threads-by-references (threads)
4144   "Gather threads by looking at References headers."
4145   (let ((idhashtb (gnus-make-hashtable 1024))
4146         (thhashtb (gnus-make-hashtable 1024))
4147         (prev threads)
4148         (result threads)
4149         ids references id gthread gid entered ref)
4150     (while threads
4151       (when (setq references (mail-header-references (caar threads)))
4152         (setq id (mail-header-id (caar threads))
4153               ids (inline (gnus-split-references references))
4154               entered nil)
4155         (while (setq ref (pop ids))
4156           (setq ids (delete ref ids))
4157           (if (not (setq gid (gnus-gethash ref idhashtb)))
4158               (progn
4159                 (gnus-sethash ref id idhashtb)
4160                 (gnus-sethash id threads thhashtb))
4161             (setq gthread (gnus-gethash gid thhashtb))
4162             (unless entered
4163               ;; We enter a dummy root into the thread, if we
4164               ;; haven't done that already.
4165               (unless (stringp (caar gthread))
4166                 (setcar gthread (list (mail-header-subject (caar gthread))
4167                                       (car gthread))))
4168               ;; We add this new gathered thread to this gathered
4169               ;; thread.
4170               (setcdr (car gthread)
4171                       (nconc (cdar gthread) (list (car threads)))))
4172             ;; Add it into the thread hash table.
4173             (gnus-sethash id gthread thhashtb)
4174             (setq entered t)
4175             ;; Remove it from the list of threads.
4176             (setcdr prev (cdr threads))
4177             (setq threads prev))))
4178       (setq prev threads)
4179       (setq threads (cdr threads)))
4180     result))
4181
4182 (defun gnus-sort-gathered-threads (threads)
4183   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4184   (let ((result threads))
4185     (while threads
4186       (when (stringp (caar threads))
4187         (setcdr (car threads)
4188                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4189       (setq threads (cdr threads)))
4190     result))
4191
4192 (defun gnus-thread-loop-p (root thread)
4193   "Say whether ROOT is in THREAD."
4194   (let ((stack (list thread))
4195         (infloop 0)
4196         th)
4197     (while (setq thread (pop stack))
4198       (setq th (cdr thread))
4199       (while (and th
4200                   (not (eq (caar th) root)))
4201         (pop th))
4202       (if th
4203           ;; We have found a loop.
4204           (let (ref-dep)
4205             (setcdr thread (delq (car th) (cdr thread)))
4206             (if (boundp (setq ref-dep (intern "none"
4207                                               gnus-newsgroup-dependencies)))
4208                 (setcdr (symbol-value ref-dep)
4209                         (nconc (cdr (symbol-value ref-dep))
4210                                (list (car th))))
4211               (set ref-dep (list nil (car th))))
4212             (setq infloop 1
4213                   stack nil))
4214         ;; Push all the subthreads onto the stack.
4215         (push (cdr thread) stack)))
4216     infloop))
4217
4218 (defun gnus-make-threads ()
4219   "Go through the dependency hashtb and find the roots.  Return all threads."
4220   (let (threads)
4221     (while (catch 'infloop
4222              (mapatoms
4223               (lambda (refs)
4224                 ;; Deal with self-referencing References loops.
4225                 (when (and (car (symbol-value refs))
4226                            (not (zerop
4227                                  (apply
4228                                   '+
4229                                   (mapcar
4230                                    (lambda (thread)
4231                                      (gnus-thread-loop-p
4232                                       (car (symbol-value refs)) thread))
4233                                    (cdr (symbol-value refs)))))))
4234                   (setq threads nil)
4235                   (throw 'infloop t))
4236                 (unless (car (symbol-value refs))
4237                   ;; These threads do not refer back to any other
4238                   ;; articles, so they're roots.
4239                   (setq threads (append (cdr (symbol-value refs)) threads))))
4240               gnus-newsgroup-dependencies)))
4241     threads))
4242
4243 ;; Build the thread tree.
4244 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4245   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4246
4247 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4248 if it was already present.
4249
4250 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4251 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4252 Message-IDs will be renamed to a unique Message-ID before being
4253 entered.
4254
4255 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4256   (let* ((id (mail-header-id header))
4257          (id-dep (and id (intern id dependencies)))
4258          parent-id ref ref-dep ref-header replaced)
4259     ;; Enter this `header' in the `dependencies' table.
4260     (cond
4261      ((not id-dep)
4262       (setq header nil))
4263      ;; The first two cases do the normal part: enter a new `header'
4264      ;; in the `dependencies' table.
4265      ((not (boundp id-dep))
4266       (set id-dep (list header)))
4267      ((null (car (symbol-value id-dep)))
4268       (setcar (symbol-value id-dep) header))
4269
4270      ;; From here the `header' was already present in the
4271      ;; `dependencies' table.
4272      (force-new
4273       ;; Overrides an existing entry;
4274       ;; just set the header part of the entry.
4275       (setcar (symbol-value id-dep) header)
4276       (setq replaced t))
4277
4278      ;; Renames the existing `header' to a unique Message-ID.
4279      ((not gnus-summary-ignore-duplicates)
4280       ;; An article with this Message-ID has already been seen.
4281       ;; We rename the Message-ID.
4282       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4283            (list header))
4284       (mail-header-set-id header id))
4285
4286      ;; The last case ignores an existing entry, except it adds any
4287      ;; additional Xrefs (in case the two articles came from different
4288      ;; servers.
4289      ;; Also sets `header' to `nil' meaning that the `dependencies'
4290      ;; table was *not* modified.
4291      (t
4292       (mail-header-set-xref
4293        (car (symbol-value id-dep))
4294        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4295                    "")
4296                (or (mail-header-xref header) "")))
4297       (setq header nil)))
4298
4299     (when (and header (not replaced))
4300       ;; First check that we are not creating a References loop.
4301       (setq parent-id (gnus-parent-id (mail-header-references header)))
4302       (setq ref parent-id)
4303       (while (and ref
4304                   (setq ref-dep (intern-soft ref dependencies))
4305                   (boundp ref-dep)
4306                   (setq ref-header (car (symbol-value ref-dep))))
4307         (if (string= id ref)
4308             ;; Yuk!  This is a reference loop.  Make the article be a
4309             ;; root article.
4310             (progn
4311               (mail-header-set-references (car (symbol-value id-dep)) "none")
4312               (setq ref nil)
4313               (setq parent-id nil))
4314           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4315       (setq ref-dep (intern (or parent-id "none") dependencies))
4316       (if (boundp ref-dep)
4317           (setcdr (symbol-value ref-dep)
4318                   (nconc (cdr (symbol-value ref-dep))
4319                          (list (symbol-value id-dep))))
4320         (set ref-dep (list nil (symbol-value id-dep)))))
4321     header))
4322
4323 (defun gnus-extract-message-id-from-in-reply-to (string)
4324   (if (string-match "<[^>]+>" string)
4325       (substring string (match-beginning 0) (match-end 0))
4326     nil))
4327
4328 (defun gnus-build-sparse-threads ()
4329   (let ((headers gnus-newsgroup-headers)
4330         (mail-parse-charset gnus-newsgroup-charset)
4331         (gnus-summary-ignore-duplicates t)
4332         header references generation relations
4333         subject child end new-child date)
4334     ;; First we create an alist of generations/relations, where
4335     ;; generations is how much we trust the relation, and the relation
4336     ;; is parent/child.
4337     (gnus-message 7 "Making sparse threads...")
4338     (save-excursion
4339       (nnheader-set-temp-buffer " *gnus sparse threads*")
4340       (while (setq header (pop headers))
4341         (when (and (setq references (mail-header-references header))
4342                    (not (string= references "")))
4343           (insert references)
4344           (setq child (mail-header-id header)
4345                 subject (mail-header-subject header)
4346                 date (mail-header-date header)
4347                 generation 0)
4348           (while (search-backward ">" nil t)
4349             (setq end (1+ (point)))
4350             (when (search-backward "<" nil t)
4351               (setq new-child (buffer-substring (point) end))
4352               (push (list (incf generation)
4353                           child (setq child new-child)
4354                           subject date)
4355                     relations)))
4356           (when child
4357             (push (list (1+ generation) child nil subject) relations))
4358           (erase-buffer)))
4359       (kill-buffer (current-buffer)))
4360     ;; Sort over trustworthiness.
4361     (dolist (relation (sort relations 'car-less-than-car))
4362       (when (gnus-dependencies-add-header
4363              (make-full-mail-header
4364               gnus-reffed-article-number
4365               (nth 3 relation) "" (or (nth 4 relation) "")
4366               (nth 1 relation)
4367               (or (nth 2 relation) "") 0 0 "")
4368              gnus-newsgroup-dependencies nil)
4369         (push gnus-reffed-article-number gnus-newsgroup-limit)
4370         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4371         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4372               gnus-newsgroup-reads)
4373         (decf gnus-reffed-article-number)))
4374     (gnus-message 7 "Making sparse threads...done")))
4375
4376 (defun gnus-build-old-threads ()
4377   ;; Look at all the articles that refer back to old articles, and
4378   ;; fetch the headers for the articles that aren't there.  This will
4379   ;; build complete threads - if the roots haven't been expired by the
4380   ;; server, that is.
4381   (let ((mail-parse-charset gnus-newsgroup-charset)
4382         id heads)
4383     (mapatoms
4384      (lambda (refs)
4385        (when (not (car (symbol-value refs)))
4386          (setq heads (cdr (symbol-value refs)))
4387          (while heads
4388            (if (memq (mail-header-number (caar heads))
4389                      gnus-newsgroup-dormant)
4390                (setq heads (cdr heads))
4391              (setq id (symbol-name refs))
4392              (while (and (setq id (gnus-build-get-header id))
4393                          (not (car (gnus-id-to-thread id)))))
4394              (setq heads nil)))))
4395      gnus-newsgroup-dependencies)))
4396
4397 (defsubst gnus-remove-odd-characters (string)
4398   "Translate STRING into something that doesn't contain weird characters."
4399   (mm-subst-char-in-string
4400    ?\r ?\-
4401    (mm-subst-char-in-string ?\n ?\- string t) t))
4402
4403 ;; This function has to be called with point after the article number
4404 ;; on the beginning of the line.
4405 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4406   (let ((eol (point-at-eol))
4407         (buffer (current-buffer))
4408         header references in-reply-to)
4409
4410     ;; overview: [num subject from date id refs chars lines misc]
4411     (unwind-protect
4412         (let (x)
4413           (narrow-to-region (point) eol)
4414           (unless (eobp)
4415             (forward-char))
4416
4417           (setq header
4418                 (make-full-mail-header
4419                  number                 ; number
4420                  (condition-case ()     ; subject
4421                      (gnus-remove-odd-characters
4422                       (funcall gnus-decode-encoded-word-function
4423                                (setq x (nnheader-nov-field))))
4424                    (error x))
4425                  (condition-case ()     ; from
4426                      (gnus-remove-odd-characters
4427                       (funcall gnus-decode-encoded-address-function
4428                                (setq x (nnheader-nov-field))))
4429                    (error x))
4430                  (nnheader-nov-field)   ; date
4431                  (nnheader-nov-read-message-id number)  ; id
4432                  (setq references (nnheader-nov-field)) ; refs
4433                  (nnheader-nov-read-integer) ; chars
4434                  (nnheader-nov-read-integer) ; lines
4435                  (unless (eobp)
4436                    (if (looking-at "Xref: ")
4437                        (goto-char (match-end 0)))
4438                    (nnheader-nov-field)) ; Xref
4439                  (nnheader-nov-parse-extra)))) ; extra
4440
4441       (widen))
4442
4443     (when (and (string= references "")
4444                (setq in-reply-to (mail-header-extra header))
4445                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4446       (mail-header-set-references
4447        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4448
4449     (when gnus-alter-header-function
4450       (funcall gnus-alter-header-function header))
4451     (gnus-dependencies-add-header header dependencies force-new)))
4452
4453 (defun gnus-build-get-header (id)
4454   "Look through the buffer of NOV lines and find the header to ID.
4455 Enter this line into the dependencies hash table, and return
4456 the id of the parent article (if any)."
4457   (let ((deps gnus-newsgroup-dependencies)
4458         found header)
4459     (prog1
4460         (with-current-buffer nntp-server-buffer
4461           (let ((case-fold-search nil))
4462             (goto-char (point-min))
4463             (while (and (not found)
4464                         (search-forward id nil t))
4465               (beginning-of-line)
4466               (setq found (looking-at
4467                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4468                                    (regexp-quote id))))
4469               (or found (beginning-of-line 2)))
4470             (when found
4471               (beginning-of-line)
4472               (and
4473                (setq header (gnus-nov-parse-line
4474                              (read (current-buffer)) deps))
4475                (gnus-parent-id (mail-header-references header))))))
4476       (when header
4477         (let ((number (mail-header-number header)))
4478           (push number gnus-newsgroup-limit)
4479           (push header gnus-newsgroup-headers)
4480           (if (memq number gnus-newsgroup-unselected)
4481               (progn
4482                 (setq gnus-newsgroup-unreads
4483                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4484                                                number))
4485                 (setq gnus-newsgroup-unselected
4486                       (delq number gnus-newsgroup-unselected)))
4487             (push number gnus-newsgroup-ancient)))))))
4488
4489 (defun gnus-build-all-threads ()
4490   "Read all the headers."
4491   (let ((gnus-summary-ignore-duplicates t)
4492         (mail-parse-charset gnus-newsgroup-charset)
4493         (dependencies gnus-newsgroup-dependencies)
4494         header article)
4495     (with-current-buffer nntp-server-buffer
4496       (let ((case-fold-search nil))
4497         (goto-char (point-min))
4498         (while (not (eobp))
4499           (ignore-errors
4500             (setq article (read (current-buffer))
4501                   header (gnus-nov-parse-line article dependencies)))
4502           (when header
4503             (with-current-buffer gnus-summary-buffer
4504               (push header gnus-newsgroup-headers)
4505               (if (memq (setq article (mail-header-number header))
4506                         gnus-newsgroup-unselected)
4507                   (progn
4508                     (setq gnus-newsgroup-unreads
4509                           (gnus-add-to-sorted-list
4510                            gnus-newsgroup-unreads article))
4511                     (setq gnus-newsgroup-unselected
4512                           (delq article gnus-newsgroup-unselected)))
4513                 (push article gnus-newsgroup-ancient)))
4514             (forward-line 1)))))))
4515
4516 (defun gnus-summary-update-article-line (article header)
4517   "Update the line for ARTICLE using HEADER."
4518   (let* ((id (mail-header-id header))
4519          (thread (gnus-id-to-thread id)))
4520     (unless thread
4521       (error "Article in no thread"))
4522     ;; Update the thread.
4523     (setcar thread header)
4524     (gnus-summary-goto-subject article)
4525     (let* ((datal (gnus-data-find-list article))
4526            (data (car datal))
4527            (inhibit-read-only t)
4528            (level (gnus-summary-thread-level)))
4529       (gnus-delete-line)
4530       (let ((inserted (- (point)
4531                          (progn
4532                            (gnus-summary-insert-line
4533                             header level nil
4534                             (memq article gnus-newsgroup-undownloaded)
4535                             (gnus-article-mark article)
4536                             (memq article gnus-newsgroup-replied)
4537                             (memq article gnus-newsgroup-expirable)
4538                             ;; Only insert the Subject string when it's different
4539                             ;; from the previous Subject string.
4540                             (if (and
4541                                  gnus-show-threads
4542                                  (gnus-subject-equal
4543                                   (condition-case ()
4544                                       (mail-header-subject
4545                                        (gnus-data-header
4546                                         (cadr
4547                                          (gnus-data-find-list
4548                                           article
4549                                           (gnus-data-list t)))))
4550                                     ;; Error on the side of excessive subjects.
4551                                     (error ""))
4552                                   (mail-header-subject header)))
4553                                 ""
4554                               (mail-header-subject header))
4555                             nil (cdr (assq article gnus-newsgroup-scored))
4556                             (memq article gnus-newsgroup-processable))
4557                            (point)))))
4558         (when (cdr datal)
4559           (gnus-data-update-list
4560            (cdr datal)
4561            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4562
4563 (defun gnus-summary-update-article (article &optional iheader)
4564   "Update ARTICLE in the summary buffer."
4565   (set-buffer gnus-summary-buffer)
4566   (let* ((header (gnus-summary-article-header article))
4567          (id (mail-header-id header))
4568          (data (gnus-data-find article))
4569          (thread (gnus-id-to-thread id))
4570          (references (mail-header-references header))
4571          (parent
4572           (gnus-id-to-thread
4573            (or (gnus-parent-id
4574                 (when (and references
4575                            (not (equal "" references)))
4576                   references))
4577                "none")))
4578          (inhibit-read-only t)
4579          (old (car thread)))
4580     (when thread
4581       (unless iheader
4582         (setcar thread nil)
4583         (when parent
4584           (delq thread parent)))
4585       (if (gnus-summary-insert-subject id header)
4586           ;; Set the (possibly) new article number in the data structure.
4587           (gnus-data-set-number data (gnus-id-to-article id))
4588         (setcar thread old)
4589         nil))))
4590
4591 (defun gnus-rebuild-thread (id &optional line)
4592   "Rebuild the thread containing ID.
4593 If LINE, insert the rebuilt thread starting on line LINE."
4594   (let ((inhibit-read-only t)
4595         old-pos current thread data)
4596     (if (not gnus-show-threads)
4597         (setq thread (list (car (gnus-id-to-thread id))))
4598       ;; Get the thread this article is part of.
4599       (setq thread (gnus-remove-thread id)))
4600     (setq old-pos (point-at-bol))
4601     (setq current (save-excursion
4602                     (and (re-search-backward "[\r\n]" nil t)
4603                          (gnus-summary-article-number))))
4604     ;; If this is a gathered thread, we have to go some re-gathering.
4605     (when (stringp (car thread))
4606       (let ((subject (car thread))
4607             roots thr)
4608         (setq thread (cdr thread))
4609         (while thread
4610           (unless (memq (setq thr (gnus-id-to-thread
4611                                    (gnus-root-id
4612                                     (mail-header-id (caar thread)))))
4613                         roots)
4614             (push thr roots))
4615           (setq thread (cdr thread)))
4616         ;; We now have all (unique) roots.
4617         (if (= (length roots) 1)
4618             ;; All the loose roots are now one solid root.
4619             (setq thread (car roots))
4620           (setq thread (cons subject (gnus-sort-threads roots))))))
4621     (let (threads)
4622       ;; We then insert this thread into the summary buffer.
4623       (when line
4624         (goto-char (point-min))
4625         (forward-line (1- line)))
4626       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4627         (if gnus-show-threads
4628             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4629           (gnus-summary-prepare-unthreaded thread))
4630         (setq data (nreverse gnus-newsgroup-data))
4631         (setq threads gnus-newsgroup-threads))
4632       ;; We splice the new data into the data structure.
4633       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4634       ;;!!! then we want to insert at the beginning of the buffer.
4635       ;;!!! That happens to be true with Gnus now, but that may
4636       ;;!!! change in the future.  Perhaps.
4637       (gnus-data-enter-list
4638        (if line nil current) data (- (point) old-pos))
4639       (setq gnus-newsgroup-threads
4640             (nconc threads gnus-newsgroup-threads))
4641       (gnus-data-compute-positions))))
4642
4643 (defun gnus-number-to-header (number)
4644   "Return the header for article NUMBER."
4645   (let ((headers gnus-newsgroup-headers))
4646     (while (and headers
4647                 (not (= number (mail-header-number (car headers)))))
4648       (pop headers))
4649     (when headers
4650       (car headers))))
4651
4652 (defun gnus-parent-headers (in-headers &optional generation)
4653   "Return the headers of the GENERATIONeth parent of HEADERS."
4654   (unless generation
4655     (setq generation 1))
4656   (let ((parent t)
4657         (headers in-headers)
4658         references)
4659     (while (and parent
4660                 (not (zerop generation))
4661                 (setq references (mail-header-references headers)))
4662       (setq headers (if (and references
4663                              (setq parent (gnus-parent-id references)))
4664                         (car (gnus-id-to-thread parent))
4665                       nil))
4666       (decf generation))
4667     (and (not (eq headers in-headers))
4668          headers)))
4669
4670 (defun gnus-id-to-thread (id)
4671   "Return the (sub-)thread where ID appears."
4672   (gnus-gethash id gnus-newsgroup-dependencies))
4673
4674 (defun gnus-id-to-article (id)
4675   "Return the article number of ID."
4676   (let ((thread (gnus-id-to-thread id)))
4677     (when (and thread
4678                (car thread))
4679       (mail-header-number (car thread)))))
4680
4681 (defun gnus-id-to-header (id)
4682   "Return the article headers of ID."
4683   (car (gnus-id-to-thread id)))
4684
4685 (defun gnus-article-displayed-root-p (article)
4686   "Say whether ARTICLE is a root(ish) article."
4687   (let ((level (gnus-summary-thread-level article))
4688         (refs (mail-header-references  (gnus-summary-article-header article)))
4689         particle)
4690     (cond
4691      ((null level) nil)
4692      ((zerop level) t)
4693      ((null refs) t)
4694      ((null (gnus-parent-id refs)) t)
4695      ((and (= 1 level)
4696            (null (setq particle (gnus-id-to-article
4697                                  (gnus-parent-id refs))))
4698            (null (gnus-summary-thread-level particle)))))))
4699
4700 (defun gnus-root-id (id)
4701   "Return the id of the root of the thread where ID appears."
4702   (let (last-id prev)
4703     (while (and id (setq prev (car (gnus-id-to-thread id))))
4704       (setq last-id id
4705             id (gnus-parent-id (mail-header-references prev))))
4706     last-id))
4707
4708 (defun gnus-articles-in-thread (thread)
4709   "Return the list of articles in THREAD."
4710   (cons (mail-header-number (car thread))
4711         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4712
4713 (defun gnus-remove-thread (id &optional dont-remove)
4714   "Remove the thread that has ID in it."
4715   (let (headers thread last-id)
4716     ;; First go up in this thread until we find the root.
4717     (setq last-id (gnus-root-id id)
4718           headers (message-flatten-list (gnus-id-to-thread last-id)))
4719     ;; We have now found the real root of this thread.  It might have
4720     ;; been gathered into some loose thread, so we have to search
4721     ;; through the threads to find the thread we wanted.
4722     (let ((threads gnus-newsgroup-threads)
4723           sub)
4724       (while threads
4725         (setq sub (car threads))
4726         (if (stringp (car sub))
4727             ;; This is a gathered thread, so we look at the roots
4728             ;; below it to find whether this article is in this
4729             ;; gathered root.
4730             (progn
4731               (setq sub (cdr sub))
4732               (while sub
4733                 (when (member (caar sub) headers)
4734                   (setq thread (car threads)
4735                         threads nil
4736                         sub nil))
4737                 (setq sub (cdr sub))))
4738           ;; It's an ordinary thread, so we check it.
4739           (when (eq (car sub) (car headers))
4740             (setq thread sub
4741                   threads nil)))
4742         (setq threads (cdr threads)))
4743       ;; If this article is in no thread, then it's a root.
4744       (if thread
4745           (unless dont-remove
4746             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4747         (setq thread (gnus-id-to-thread last-id)))
4748       (when thread
4749         (prog1
4750             thread                      ; We return this thread.
4751           (unless dont-remove
4752             (if (stringp (car thread))
4753                 (progn
4754                   ;; If we use dummy roots, then we have to remove the
4755                   ;; dummy root as well.
4756                   (when (eq gnus-summary-make-false-root 'dummy)
4757                     ;; We go to the dummy root by going to
4758                     ;; the first sub-"thread", and then one line up.
4759                     (gnus-summary-goto-article
4760                      (mail-header-number (caadr thread)))
4761                     (forward-line -1)
4762                     (gnus-delete-line)
4763                     (gnus-data-compute-positions))
4764                   (setq thread (cdr thread))
4765                   (while thread
4766                     (gnus-remove-thread-1 (car thread))
4767                     (setq thread (cdr thread))))
4768               (gnus-remove-thread-1 thread))))))))
4769
4770 (defun gnus-remove-thread-1 (thread)
4771   "Remove the thread THREAD recursively."
4772   (let ((number (mail-header-number (pop thread)))
4773         d)
4774     (setq thread (reverse thread))
4775     (while thread
4776       (gnus-remove-thread-1 (pop thread)))
4777     (when (setq d (gnus-data-find number))
4778       (goto-char (gnus-data-pos d))
4779       (gnus-summary-show-thread)
4780       (gnus-data-remove
4781        number
4782        (- (point-at-bol)
4783           (prog1
4784               (1+ (point-at-eol))
4785             (gnus-delete-line)))))))
4786
4787 (defun gnus-sort-threads-recursive (threads func)
4788   (sort (mapcar (lambda (thread)
4789                   (cons (car thread)
4790                         (and (cdr thread)
4791                              (gnus-sort-threads-recursive (cdr thread) func))))
4792                 threads) func))
4793
4794 (defun gnus-sort-threads-loop (threads func)
4795   (let* ((superthread (cons nil threads))
4796          (stack (list (cons superthread threads)))
4797          remaining-threads thread)
4798     (while stack
4799       (setq remaining-threads (cdr (car stack)))
4800       (if remaining-threads
4801           (progn (setq thread (car remaining-threads))
4802                  (setcdr (car stack) (cdr remaining-threads))
4803                  (if (cdr thread)
4804                      (push (cons thread (cdr thread)) stack)))
4805         (setq thread (caar stack))
4806         (setcdr thread (sort (cdr thread) func))
4807         (pop stack)))
4808     (cdr superthread)))
4809
4810 (defun gnus-sort-threads (threads)
4811   "Sort THREADS."
4812   (if (not gnus-thread-sort-functions)
4813       threads
4814     (gnus-message 8 "Sorting threads...")
4815     (prog1
4816         (condition-case nil
4817             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4818               (gnus-sort-threads-recursive
4819                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4820           ;; Even after binding max-lisp-eval-depth, the recursive
4821           ;; sorter might fail for very long threads.  In that case,
4822           ;; try using a (less well-tested) non-recursive sorter.
4823           (error (gnus-message 9 "Sorting threads with loop...")
4824                  (gnus-sort-threads-loop
4825                   threads (gnus-make-sort-function
4826                            gnus-thread-sort-functions))))
4827       (gnus-message 8 "Sorting threads...done"))))
4828
4829 (defun gnus-sort-articles (articles)
4830   "Sort ARTICLES."
4831   (when gnus-article-sort-functions
4832     (gnus-message 7 "Sorting articles...")
4833     (prog1
4834         (setq gnus-newsgroup-headers
4835               (sort articles (gnus-make-sort-function
4836                               gnus-article-sort-functions)))
4837       (gnus-message 7 "Sorting articles...done"))))
4838
4839 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4840 (defmacro gnus-thread-header (thread)
4841   "Return header of first article in THREAD.
4842 Note that THREAD must never, ever be anything else than a variable -
4843 using some other form will lead to serious barfage."
4844   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4845   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4846   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4847         (vector thread) 2))
4848
4849 (defsubst gnus-article-sort-by-number (h1 h2)
4850   "Sort articles by article number."
4851   (< (mail-header-number h1)
4852      (mail-header-number h2)))
4853
4854 (defun gnus-thread-sort-by-number (h1 h2)
4855   "Sort threads by root article number."
4856   (gnus-article-sort-by-number
4857    (gnus-thread-header h1) (gnus-thread-header h2)))
4858
4859 (defsubst gnus-article-sort-by-random (h1 h2)
4860   "Sort articles randomly."
4861   (zerop (random 2)))
4862
4863 (defun gnus-thread-sort-by-random (h1 h2)
4864   "Sort threads randomly."
4865   (gnus-article-sort-by-random
4866    (gnus-thread-header h1) (gnus-thread-header h2)))
4867
4868 (defsubst gnus-article-sort-by-lines (h1 h2)
4869   "Sort articles by article Lines header."
4870   (< (mail-header-lines h1)
4871      (mail-header-lines h2)))
4872
4873 (defun gnus-thread-sort-by-lines (h1 h2)
4874   "Sort threads by root article Lines header."
4875   (gnus-article-sort-by-lines
4876    (gnus-thread-header h1) (gnus-thread-header h2)))
4877
4878 (defsubst gnus-article-sort-by-chars (h1 h2)
4879   "Sort articles by octet length."
4880   (< (mail-header-chars h1)
4881      (mail-header-chars h2)))
4882
4883 (defun gnus-thread-sort-by-chars (h1 h2)
4884   "Sort threads by root article octet length."
4885   (gnus-article-sort-by-chars
4886    (gnus-thread-header h1) (gnus-thread-header h2)))
4887
4888 (defsubst gnus-article-sort-by-author (h1 h2)
4889   "Sort articles by root author."
4890   (gnus-string<
4891    (let ((extract (funcall
4892                    gnus-extract-address-components
4893                    (mail-header-from h1))))
4894      (or (car extract) (cadr extract) ""))
4895    (let ((extract (funcall
4896                    gnus-extract-address-components
4897                    (mail-header-from h2))))
4898      (or (car extract) (cadr extract) ""))))
4899
4900 (defun gnus-thread-sort-by-author (h1 h2)
4901   "Sort threads by root author."
4902   (gnus-article-sort-by-author
4903    (gnus-thread-header h1)  (gnus-thread-header h2)))
4904
4905 (defsubst gnus-article-sort-by-recipient (h1 h2)
4906   "Sort articles by recipient."
4907   (gnus-string<
4908    (let ((extract (funcall
4909                    gnus-extract-address-components
4910                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4911      (or (car extract) (cadr extract)))
4912    (let ((extract (funcall
4913                    gnus-extract-address-components
4914                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4915      (or (car extract) (cadr extract)))))
4916
4917 (defun gnus-thread-sort-by-recipient (h1 h2)
4918   "Sort threads by root recipient."
4919   (gnus-article-sort-by-recipient
4920    (gnus-thread-header h1) (gnus-thread-header h2)))
4921
4922 (defsubst gnus-article-sort-by-subject (h1 h2)
4923   "Sort articles by root subject."
4924   (gnus-string<
4925    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4926    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4927
4928 (defun gnus-thread-sort-by-subject (h1 h2)
4929   "Sort threads by root subject."
4930   (gnus-article-sort-by-subject
4931    (gnus-thread-header h1) (gnus-thread-header h2)))
4932
4933 (defsubst gnus-article-sort-by-date (h1 h2)
4934   "Sort articles by root article date."
4935   (time-less-p
4936    (gnus-date-get-time (mail-header-date h1))
4937    (gnus-date-get-time (mail-header-date h2))))
4938
4939 (defun gnus-thread-sort-by-date (h1 h2)
4940   "Sort threads by root article date."
4941   (gnus-article-sort-by-date
4942    (gnus-thread-header h1) (gnus-thread-header h2)))
4943
4944 (defsubst gnus-article-sort-by-score (h1 h2)
4945   "Sort articles by root article score.
4946 Unscored articles will be counted as having a score of zero."
4947   (> (or (cdr (assq (mail-header-number h1)
4948                     gnus-newsgroup-scored))
4949          gnus-summary-default-score 0)
4950      (or (cdr (assq (mail-header-number h2)
4951                     gnus-newsgroup-scored))
4952          gnus-summary-default-score 0)))
4953
4954 (defun gnus-thread-sort-by-score (h1 h2)
4955   "Sort threads by root article score."
4956   (gnus-article-sort-by-score
4957    (gnus-thread-header h1) (gnus-thread-header h2)))
4958
4959 (defun gnus-thread-sort-by-total-score (h1 h2)
4960   "Sort threads by the sum of all scores in the thread.
4961 Unscored articles will be counted as having a score of zero."
4962   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4963
4964 (defun gnus-thread-total-score (thread)
4965   ;; This function find the total score of THREAD.
4966   (cond
4967    ((null thread)
4968     0)
4969    ((consp thread)
4970     (if (stringp (car thread))
4971         (apply gnus-thread-score-function 0
4972                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4973       (gnus-thread-total-score-1 thread)))
4974    (t
4975     (gnus-thread-total-score-1 (list thread)))))
4976
4977 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4978   "Sort threads such that the thread with the most recently arrived article comes first."
4979   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4980
4981 (defun gnus-thread-highest-number (thread)
4982   "Return the highest article number in THREAD."
4983   (apply 'max (mapcar (lambda (header)
4984                         (mail-header-number header))
4985                       (message-flatten-list thread))))
4986
4987 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4988   "Sort threads such that the thread with the most recently dated article comes first."
4989   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4990
4991 ; Since this is called not only to sort the top-level threads, but
4992 ; also in recursive sorts to order the articles within a thread, each
4993 ; article will be processed many times.  Thus it speeds things up
4994 ; quite a bit to use gnus-date-get-time, which caches the time value.
4995 (defun gnus-thread-latest-date (thread)
4996   "Return the highest article date in THREAD."
4997   (apply 'max
4998          (mapcar (lambda (header) (gnus-float-time
4999                                    (gnus-date-get-time
5000                                     (mail-header-date header))))
5001                  (message-flatten-list thread))))
5002
5003 (defun gnus-thread-total-score-1 (root)
5004   ;; This function find the total score of the thread below ROOT.
5005   (setq root (car root))
5006   (apply gnus-thread-score-function
5007          (or (append
5008               (mapcar 'gnus-thread-total-score
5009                       (cdr (gnus-id-to-thread (mail-header-id root))))
5010               (when (> (mail-header-number root) 0)
5011                 (list (or (cdr (assq (mail-header-number root)
5012                                      gnus-newsgroup-scored))
5013                           gnus-summary-default-score 0))))
5014              (list gnus-summary-default-score)
5015              '(0))))
5016
5017 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5018 (defvar gnus-tmp-prev-subject nil)
5019 (defvar gnus-tmp-false-parent nil)
5020 (defvar gnus-tmp-root-expunged nil)
5021 (defvar gnus-tmp-dummy-line nil)
5022
5023 (defun gnus-extra-header (type &optional header)
5024   "Return the extra header of TYPE."
5025   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5026       ""))
5027
5028 (defvar gnus-tmp-thread-tree-header-string "")
5029
5030 (defcustom gnus-sum-thread-tree-root "> "
5031   "With %B spec, used for the root of a thread.
5032 If nil, use subject instead."
5033   :version "22.1"
5034   :type '(radio (const :format "%v  " nil) string)
5035   :group 'gnus-thread)
5036
5037 (defcustom gnus-sum-thread-tree-false-root "> "
5038   "With %B spec, used for a false root of a thread.
5039 If nil, use subject instead."
5040   :version "22.1"
5041   :type '(radio (const :format "%v  " nil) string)
5042   :group 'gnus-thread)
5043
5044 (defcustom gnus-sum-thread-tree-single-indent ""
5045   "With %B spec, used for a thread with just one message.
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-vertical "| "
5052   "With %B spec, used for drawing a vertical line."
5053   :version "22.1"
5054   :type 'string
5055   :group 'gnus-thread)
5056
5057 (defcustom gnus-sum-thread-tree-indent "  "
5058   "With %B spec, used for indenting."
5059   :version "22.1"
5060   :type 'string
5061   :group 'gnus-thread)
5062
5063 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5064   "With %B spec, used for a leaf with brothers."
5065   :version "22.1"
5066   :type 'string
5067   :group 'gnus-thread)
5068
5069 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5070   "With %B spec, used for a leaf without brothers."
5071   :version "22.1"
5072   :type 'string
5073   :group 'gnus-thread)
5074
5075 (defcustom gnus-summary-display-while-building nil
5076   "If non-nil, show and update the summary buffer as it's being built.
5077 If the value is t, update the buffer after every line is inserted.  If
5078 the value is an integer (N), update the display every N lines."
5079   :version "22.1"
5080   :group 'gnus-thread
5081   :type '(choice (const :tag "off" nil)
5082                  number
5083                  (const :tag "frequently" t)))
5084
5085 (defun gnus-summary-prepare-threads (threads)
5086   "Prepare summary buffer from THREADS and indentation LEVEL.
5087 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5088 or a straight list of headers."
5089   (gnus-message 7 "Generating summary...")
5090
5091   (setq gnus-newsgroup-threads threads)
5092   (beginning-of-line)
5093
5094   (let ((gnus-tmp-level 0)
5095         (default-score (or gnus-summary-default-score 0))
5096         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5097         (building-line-count gnus-summary-display-while-building)
5098         (building-count (integerp gnus-summary-display-while-building))
5099         thread number subject stack state gnus-tmp-gathered beg-match
5100         new-roots gnus-tmp-new-adopts thread-end simp-subject
5101         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5102         gnus-tmp-replied gnus-tmp-subject-or-nil
5103         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5104         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5105         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5106         tree-stack)
5107
5108     (setq gnus-tmp-prev-subject nil
5109           gnus-tmp-thread-tree-header-string "")
5110
5111     (if (vectorp (car threads))
5112         ;; If this is a straight (sic) list of headers, then a
5113         ;; threaded summary display isn't required, so we just create
5114         ;; an unthreaded one.
5115         (gnus-summary-prepare-unthreaded threads)
5116
5117       ;; Do the threaded display.
5118
5119       (if gnus-summary-display-while-building
5120           (switch-to-buffer (buffer-name)))
5121       (while (or threads stack gnus-tmp-new-adopts new-roots)
5122
5123         (if (and (= gnus-tmp-level 0)
5124                  (or (not stack)
5125                      (= (caar stack) 0))
5126                  (not gnus-tmp-false-parent)
5127                  (or gnus-tmp-new-adopts new-roots))
5128             (if gnus-tmp-new-adopts
5129                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5130                       thread (list (car gnus-tmp-new-adopts))
5131                       gnus-tmp-header (caar thread)
5132                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5133               (when new-roots
5134                 (setq thread (list (car new-roots))
5135                       gnus-tmp-header (caar thread)
5136                       new-roots (cdr new-roots))))
5137
5138           (if threads
5139               ;; If there are some threads, we do them before the
5140               ;; threads on the stack.
5141               (setq thread threads
5142                     gnus-tmp-header (caar thread))
5143             ;; There were no current threads, so we pop something off
5144             ;; the stack.
5145             (setq state (car stack)
5146                   gnus-tmp-level (car state)
5147                   tree-stack (cadr state)
5148                   thread (caddr state)
5149                   stack (cdr stack)
5150                   gnus-tmp-header (caar thread))))
5151
5152         (setq gnus-tmp-false-parent nil)
5153         (setq gnus-tmp-root-expunged nil)
5154         (setq thread-end nil)
5155
5156         (if (stringp gnus-tmp-header)
5157             ;; The header is a dummy root.
5158             (cond
5159              ((eq gnus-summary-make-false-root 'adopt)
5160               ;; We let the first article adopt the rest.
5161               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5162                                                (cddar thread)))
5163               (setq gnus-tmp-gathered
5164                     (nconc (mapcar
5165                             (lambda (h) (mail-header-number (car h)))
5166                             (cddar thread))
5167                            gnus-tmp-gathered))
5168               (setq thread (cons (list (caar thread)
5169                                        (cadar thread))
5170                                  (cdr thread)))
5171               (setq gnus-tmp-level -1
5172                     gnus-tmp-false-parent t))
5173              ((eq gnus-summary-make-false-root 'empty)
5174               ;; We print adopted articles with empty subject fields.
5175               (setq gnus-tmp-gathered
5176                     (nconc (mapcar
5177                             (lambda (h) (mail-header-number (car h)))
5178                             (cddar thread))
5179                            gnus-tmp-gathered))
5180               (setq gnus-tmp-level -1))
5181              ((eq gnus-summary-make-false-root 'dummy)
5182               ;; We remember that we probably want to output a dummy
5183               ;; root.
5184               (setq gnus-tmp-dummy-line gnus-tmp-header)
5185               (setq gnus-tmp-prev-subject gnus-tmp-header))
5186              (t
5187               ;; We do not make a root for the gathered
5188               ;; sub-threads at all.
5189               (setq gnus-tmp-level -1)))
5190
5191           (setq number (mail-header-number gnus-tmp-header)
5192                 subject (mail-header-subject gnus-tmp-header)
5193                 simp-subject (gnus-simplify-subject-fully subject))
5194
5195           (cond
5196            ;; If the thread has changed subject, we might want to make
5197            ;; this subthread into a root.
5198            ((and (null gnus-thread-ignore-subject)
5199                  (not (zerop gnus-tmp-level))
5200                  gnus-tmp-prev-subject
5201                  (not (string= gnus-tmp-prev-subject simp-subject)))
5202             (setq new-roots (nconc new-roots (list (car thread)))
5203                   thread-end t
5204                   gnus-tmp-header nil))
5205            ;; If the article lies outside the current limit,
5206            ;; then we do not display it.
5207            ((not (memq number gnus-newsgroup-limit))
5208             (setq gnus-tmp-gathered
5209                   (nconc (mapcar
5210                           (lambda (h) (mail-header-number (car h)))
5211                           (cdar thread))
5212                          gnus-tmp-gathered))
5213             (setq gnus-tmp-new-adopts (if (cdar thread)
5214                                           (append gnus-tmp-new-adopts
5215                                                   (cdar thread))
5216                                         gnus-tmp-new-adopts)
5217                   thread-end t
5218                   gnus-tmp-header nil)
5219             (when (zerop gnus-tmp-level)
5220               (setq gnus-tmp-root-expunged t)))
5221            ;; Perhaps this article is to be marked as read?
5222            ((and gnus-summary-mark-below
5223                  (< (or (cdr (assq number gnus-newsgroup-scored))
5224                         default-score)
5225                     gnus-summary-mark-below)
5226                  ;; Don't touch sparse articles.
5227                  (not (gnus-summary-article-sparse-p number))
5228                  (not (gnus-summary-article-ancient-p number)))
5229             (setq gnus-newsgroup-unreads
5230                   (delq number gnus-newsgroup-unreads))
5231             (if gnus-newsgroup-auto-expire
5232                 (setq gnus-newsgroup-expirable
5233                       (gnus-add-to-sorted-list
5234                        gnus-newsgroup-expirable number))
5235               (push (cons number gnus-low-score-mark)
5236                     gnus-newsgroup-reads))))
5237
5238           (when gnus-tmp-header
5239             ;; We may have an old dummy line to output before this
5240             ;; article.
5241             (when (and gnus-tmp-dummy-line
5242                        (gnus-subject-equal
5243                         gnus-tmp-dummy-line
5244                         (mail-header-subject gnus-tmp-header)))
5245               (gnus-summary-insert-dummy-line
5246                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5247               (setq gnus-tmp-dummy-line nil))
5248
5249             ;; Compute the mark.
5250             (setq gnus-tmp-unread (gnus-article-mark number))
5251
5252             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5253                                   gnus-tmp-header gnus-tmp-level)
5254                   gnus-newsgroup-data)
5255
5256             ;; Actually insert the line.
5257             (setq
5258              gnus-tmp-subject-or-nil
5259              (cond
5260               ((and gnus-thread-ignore-subject
5261                     gnus-tmp-prev-subject
5262                     (not (string= gnus-tmp-prev-subject simp-subject)))
5263                subject)
5264               ((zerop gnus-tmp-level)
5265                (if (and (eq gnus-summary-make-false-root 'empty)
5266                         (memq number gnus-tmp-gathered)
5267                         gnus-tmp-prev-subject
5268                         (string= gnus-tmp-prev-subject simp-subject))
5269                    gnus-summary-same-subject
5270                  subject))
5271               (t gnus-summary-same-subject)))
5272             (if (and (eq gnus-summary-make-false-root 'adopt)
5273                      (= gnus-tmp-level 1)
5274                      (memq number gnus-tmp-gathered))
5275                 (setq gnus-tmp-opening-bracket ?\<
5276                       gnus-tmp-closing-bracket ?\>)
5277               (setq gnus-tmp-opening-bracket ?\[
5278                     gnus-tmp-closing-bracket ?\]))
5279             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5280                 (gnus-make-thread-indent-array
5281                  (max (* 2 (length gnus-thread-indent-array))
5282                       gnus-tmp-level)))
5283             (setq
5284              gnus-tmp-indentation
5285              (aref gnus-thread-indent-array gnus-tmp-level)
5286              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5287              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5288                                 gnus-summary-default-score 0)
5289              gnus-tmp-score-char
5290              (if (or (null gnus-summary-default-score)
5291                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5292                          gnus-summary-zcore-fuzz))
5293                  ?                      ;Whitespace
5294                (if (< gnus-tmp-score gnus-summary-default-score)
5295                    gnus-score-below-mark gnus-score-over-mark))
5296              gnus-tmp-replied
5297              (cond ((memq number gnus-newsgroup-processable)
5298                     gnus-process-mark)
5299                    ((memq number gnus-newsgroup-cached)
5300                     gnus-cached-mark)
5301                    ((memq number gnus-newsgroup-replied)
5302                     gnus-replied-mark)
5303                    ((memq number gnus-newsgroup-forwarded)
5304                     gnus-forwarded-mark)
5305                    ((memq number gnus-newsgroup-saved)
5306                     gnus-saved-mark)
5307                    ((memq number gnus-newsgroup-recent)
5308                     gnus-recent-mark)
5309                    ((memq number gnus-newsgroup-unseen)
5310                     gnus-unseen-mark)
5311                    (t gnus-no-mark))
5312              gnus-tmp-downloaded
5313              (cond ((memq number gnus-newsgroup-undownloaded)
5314                     gnus-undownloaded-mark)
5315                    (gnus-newsgroup-agentized
5316                     gnus-downloaded-mark)
5317                    (t
5318                     gnus-no-mark))
5319              gnus-tmp-from (mail-header-from gnus-tmp-header)
5320              gnus-tmp-name
5321              (cond
5322               ((string-match "<[^>]+> *$" gnus-tmp-from)
5323                (setq beg-match (match-beginning 0))
5324                (or (and (string-match "^\".+\"" gnus-tmp-from)
5325                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5326                    (substring gnus-tmp-from 0 beg-match)))
5327               ((string-match "(.+)" gnus-tmp-from)
5328                (substring gnus-tmp-from
5329                           (1+ (match-beginning 0)) (1- (match-end 0))))
5330               (t gnus-tmp-from))
5331
5332              ;; Do the %B string
5333              gnus-tmp-thread-tree-header-string
5334              (cond
5335               ((not gnus-show-threads) "")
5336               ((zerop gnus-tmp-level)
5337                (cond ((cdar thread)
5338                       (or gnus-sum-thread-tree-root subject))
5339                      (gnus-tmp-new-adopts
5340                       (or gnus-sum-thread-tree-false-root subject))
5341                      (t
5342                       (or gnus-sum-thread-tree-single-indent subject))))
5343               (t
5344                (concat (apply 'concat
5345                               (mapcar (lambda (item)
5346                                         (if (= item 1)
5347                                             gnus-sum-thread-tree-vertical
5348                                           gnus-sum-thread-tree-indent))
5349                                       (cdr (reverse tree-stack))))
5350                        (if (nth 1 thread)
5351                            gnus-sum-thread-tree-leaf-with-other
5352                          gnus-sum-thread-tree-single-leaf)))))
5353             (when (string= gnus-tmp-name "")
5354               (setq gnus-tmp-name gnus-tmp-from))
5355             (unless (numberp gnus-tmp-lines)
5356               (setq gnus-tmp-lines -1))
5357             (if (= gnus-tmp-lines -1)
5358                 (setq gnus-tmp-lines "?")
5359               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5360               (gnus-put-text-property
5361                (point)
5362                (progn (eval gnus-summary-line-format-spec) (point))
5363                'gnus-number number)
5364               (when gnus-visual-p
5365                 (forward-line -1)
5366                 (gnus-run-hooks 'gnus-summary-update-hook)
5367                 (forward-line 1))
5368
5369               (setq gnus-tmp-prev-subject simp-subject)))
5370
5371         (when (nth 1 thread)
5372           (push (list (max 0 gnus-tmp-level)
5373                       (copy-sequence tree-stack)
5374                       (nthcdr 1 thread))
5375                 stack))
5376         (push (if (nth 1 thread) 1 0) tree-stack)
5377         (incf gnus-tmp-level)
5378         (setq threads (if thread-end nil (cdar thread)))
5379         (if gnus-summary-display-while-building
5380             (if building-count
5381                 (progn
5382                   ;; use a set frequency
5383                   (setq building-line-count (1- building-line-count))
5384                   (when (= building-line-count 0)
5385                     (sit-for 0)
5386                     (setq building-line-count
5387                           gnus-summary-display-while-building)))
5388               ;; always
5389               (sit-for 0)))
5390         (unless threads
5391           (setq gnus-tmp-level 0)))))
5392   (gnus-message 7 "Generating summary...done"))
5393
5394 (defun gnus-summary-prepare-unthreaded (headers)
5395   "Generate an unthreaded summary buffer based on HEADERS."
5396   (let (header number mark)
5397
5398     (beginning-of-line)
5399
5400     (while headers
5401       ;; We may have to root out some bad articles...
5402       (when (memq (setq number (mail-header-number
5403                                 (setq header (pop headers))))
5404                   gnus-newsgroup-limit)
5405         ;; Mark article as read when it has a low score.
5406         (when (and gnus-summary-mark-below
5407                    (< (or (cdr (assq number gnus-newsgroup-scored))
5408                           gnus-summary-default-score 0)
5409                       gnus-summary-mark-below)
5410                    (not (gnus-summary-article-ancient-p number)))
5411           (setq gnus-newsgroup-unreads
5412                 (delq number gnus-newsgroup-unreads))
5413           (if gnus-newsgroup-auto-expire
5414               (push number gnus-newsgroup-expirable)
5415             (push (cons number gnus-low-score-mark)
5416                   gnus-newsgroup-reads)))
5417
5418         (setq mark (gnus-article-mark number))
5419         (push (gnus-data-make number mark (1+ (point)) header 0)
5420               gnus-newsgroup-data)
5421         (gnus-summary-insert-line
5422          header 0 number
5423          (memq number gnus-newsgroup-undownloaded)
5424          mark (memq number gnus-newsgroup-replied)
5425          (memq number gnus-newsgroup-expirable)
5426          (mail-header-subject header) nil
5427          (cdr (assq number gnus-newsgroup-scored))
5428          (memq number gnus-newsgroup-processable))))))
5429
5430 (defun gnus-summary-remove-list-identifiers ()
5431   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5432   (let ((regexp (if (consp gnus-list-identifiers)
5433                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5434                   gnus-list-identifiers))
5435         changed subject)
5436     (when regexp
5437       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5438       (dolist (header gnus-newsgroup-headers)
5439         (setq subject (mail-header-subject header)
5440               changed nil)
5441         (while (string-match regexp subject)
5442           (setq subject
5443                 (concat (substring subject 0 (match-beginning 1))
5444                         (substring subject (match-end 0)))
5445                 changed t))
5446         (when changed
5447           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5448             (setq subject
5449                   (concat (substring subject 0 (match-beginning 1))
5450                           (substring subject (match-end 1)))))
5451           (mail-header-set-subject header subject))))))
5452
5453 (defun gnus-fetch-headers (articles)
5454   "Fetch headers of ARTICLES."
5455   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5456     (gnus-message 5 "Fetching headers for %s..." name)
5457     (prog1
5458         (if (eq 'nov
5459                 (setq gnus-headers-retrieved-by
5460                       (gnus-retrieve-headers
5461                        articles gnus-newsgroup-name
5462                        ;; We might want to fetch old headers, but
5463                        ;; not if there is only 1 article.
5464                        (and (or (and
5465                                  (not (eq gnus-fetch-old-headers 'some))
5466                                  (not (numberp gnus-fetch-old-headers)))
5467                                 (> (length articles) 1))
5468                             gnus-fetch-old-headers))))
5469             (gnus-get-newsgroup-headers-xover
5470              articles nil nil gnus-newsgroup-name t)
5471           (gnus-get-newsgroup-headers))
5472       (gnus-message 5 "Fetching headers for %s...done" name))))
5473
5474 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5475   "Select newsgroup GROUP.
5476 If READ-ALL is non-nil, all articles in the group are selected.
5477 If SELECT-ARTICLES, only select those articles from GROUP."
5478   (let* ((entry (gnus-group-entry group))
5479          ;;!!! Dirty hack; should be removed.
5480          (gnus-summary-ignore-duplicates
5481           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5482               t
5483             gnus-summary-ignore-duplicates))
5484          (info (nth 2 entry))
5485          charset articles fetched-articles cached)
5486
5487     (unless (gnus-check-server
5488              (set (make-local-variable 'gnus-current-select-method)
5489                   (gnus-find-method-for-group group)))
5490       (error "Couldn't open server"))
5491     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5492
5493     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5494         (gnus-activate-group group)     ; Or we can activate it...
5495         (progn                          ; Or we bug out.
5496           (when (equal major-mode 'gnus-summary-mode)
5497             (gnus-kill-buffer (current-buffer)))
5498           (error
5499            "Couldn't activate group %s: %s"
5500            (mm-decode-coding-string group charset)
5501            (mm-decode-coding-string (gnus-status-message group) charset))))
5502
5503     (unless (gnus-request-group group t)
5504         (when (equal major-mode 'gnus-summary-mode)
5505           (gnus-kill-buffer (current-buffer)))
5506         (error "Couldn't request group %s: %s"
5507                (mm-decode-coding-string group charset)
5508                (mm-decode-coding-string (gnus-status-message group) charset)))
5509
5510     (when gnus-agent
5511       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5512
5513       (setq gnus-summary-use-undownloaded-faces
5514             (gnus-agent-find-parameter
5515              group
5516              'agent-enable-undownloaded-faces)))
5517
5518     (setq gnus-newsgroup-name group
5519           gnus-newsgroup-unselected nil
5520           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5521
5522     (let ((display (gnus-group-find-parameter group 'display)))
5523       (setq gnus-newsgroup-display
5524             (cond
5525              ((not (zerop (or (car-safe read-all) 0)))
5526               ;; The user entered the group with C-u SPC/RET, let's show
5527               ;; all articles.
5528               'gnus-not-ignore)
5529              ((eq display 'all)
5530               'gnus-not-ignore)
5531              ((arrayp display)
5532               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5533              ((numberp display)
5534               ;; The following is probably the "correct" solution, but
5535               ;; it makes Gnus fetch all headers and then limit the
5536               ;; articles (which is slow), so instead we hack the
5537               ;; select-articles parameter instead. -- Simon Josefsson
5538               ;; <jas@kth.se>
5539               ;;
5540               ;; (gnus-byte-compile
5541               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5542               ;;                         display)))))
5543               (setq select-articles
5544                     (gnus-uncompress-range
5545                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5546                              (if (> tmp 0)
5547                                  tmp
5548                                1))
5549                            (cdr (gnus-active group)))))
5550               nil)
5551              (t
5552               nil))))
5553
5554     (gnus-summary-setup-default-charset)
5555
5556     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5557     (when (gnus-virtual-group-p group)
5558       (setq cached gnus-newsgroup-cached))
5559
5560     (setq gnus-newsgroup-unreads
5561           (gnus-sorted-ndifference
5562            (gnus-sorted-ndifference gnus-newsgroup-unreads
5563                                     gnus-newsgroup-marked)
5564            gnus-newsgroup-dormant))
5565
5566     (setq gnus-newsgroup-processable nil)
5567
5568     (gnus-update-read-articles group gnus-newsgroup-unreads)
5569
5570     ;; Adjust and set lists of article marks.
5571     (when info
5572       (gnus-adjust-marked-articles info))
5573     (if (setq articles select-articles)
5574         (setq gnus-newsgroup-unselected
5575               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5576       (setq articles (gnus-articles-to-read group read-all)))
5577
5578     (cond
5579      ((null articles)
5580       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5581       'quit)
5582      ((eq articles 0) nil)
5583      (t
5584       ;; Init the dependencies hash table.
5585       (setq gnus-newsgroup-dependencies
5586             (gnus-make-hashtable (length articles)))
5587       (gnus-set-global-variables)
5588       ;; Retrieve the headers and read them in.
5589
5590       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5591
5592       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5593       (when cached
5594         (setq gnus-newsgroup-cached cached))
5595
5596       ;; Suppress duplicates?
5597       (when gnus-suppress-duplicates
5598         (gnus-dup-suppress-articles))
5599
5600       ;; Set the initial limit.
5601       (setq gnus-newsgroup-limit (copy-sequence articles))
5602       ;; Remove canceled articles from the list of unread articles.
5603       (setq fetched-articles
5604             (mapcar (lambda (headers) (mail-header-number headers))
5605                     gnus-newsgroup-headers))
5606       (setq gnus-newsgroup-articles fetched-articles)
5607       (setq gnus-newsgroup-unreads
5608             (gnus-sorted-nintersection
5609              gnus-newsgroup-unreads fetched-articles))
5610       (gnus-compute-unseen-list)
5611
5612       ;; Removed marked articles that do not exist.
5613       (gnus-update-missing-marks
5614        (gnus-sorted-difference articles fetched-articles))
5615       ;; We might want to build some more threads first.
5616       (when (and gnus-fetch-old-headers
5617                  (eq gnus-headers-retrieved-by 'nov))
5618         (if (eq gnus-fetch-old-headers 'invisible)
5619             (gnus-build-all-threads)
5620           (gnus-build-old-threads)))
5621       ;; Let the Gnus agent mark articles as read.
5622       (when gnus-agent
5623         (gnus-agent-get-undownloaded-list))
5624       ;; Remove list identifiers from subject
5625       (when gnus-list-identifiers
5626         (gnus-summary-remove-list-identifiers))
5627       ;; Check whether auto-expire is to be done in this group.
5628       (setq gnus-newsgroup-auto-expire
5629             (gnus-group-auto-expirable-p group))
5630       ;; Set up the article buffer now, if necessary.
5631       (unless (and gnus-single-article-buffer
5632                    (equal gnus-article-buffer "*Article*"))
5633         (gnus-article-setup-buffer))
5634       ;; First and last article in this newsgroup.
5635       (when gnus-newsgroup-headers
5636         (setq gnus-newsgroup-begin
5637               (mail-header-number (car gnus-newsgroup-headers))
5638               gnus-newsgroup-end
5639               (mail-header-number
5640                (gnus-last-element gnus-newsgroup-headers))))
5641       ;; GROUP is successfully selected.
5642       (or gnus-newsgroup-headers t)))))
5643
5644 (defun gnus-compute-unseen-list ()
5645   ;; The `seen' marks are treated specially.
5646   (if (not gnus-newsgroup-seen)
5647       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5648     (setq gnus-newsgroup-unseen
5649           (gnus-inverse-list-range-intersection
5650            gnus-newsgroup-articles gnus-newsgroup-seen))))
5651
5652 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5653
5654 (defun gnus-summary-display-make-predicate (display)
5655   (require 'gnus-agent)
5656   (when (= (length display) 1)
5657     (setq display (car display)))
5658   (unless gnus-summary-display-cache
5659     (dolist (elem (append '((unread . unread)
5660                             (read . read)
5661                             (unseen . unseen))
5662                           gnus-article-mark-lists))
5663       (push (cons (cdr elem)
5664                   (gnus-byte-compile
5665                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5666             gnus-summary-display-cache)))
5667   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5668         (gnus-category-predicate-cache gnus-summary-display-cache))
5669     (gnus-get-predicate display)))
5670
5671 ;; Uses the dynamically bound `number' variable.
5672 (defvar number)
5673 (defun gnus-article-marked-p (type &optional article)
5674   (let ((article (or article number)))
5675     (cond
5676      ((eq type 'tick)
5677       (memq article gnus-newsgroup-marked))
5678      ((eq type 'spam)
5679       (memq article gnus-newsgroup-spam-marked))
5680      ((eq type 'unsend)
5681       (memq article gnus-newsgroup-unsendable))
5682      ((eq type 'undownload)
5683       (memq article gnus-newsgroup-undownloaded))
5684      ((eq type 'download)
5685       (memq article gnus-newsgroup-downloadable))
5686      ((eq type 'unread)
5687       (memq article gnus-newsgroup-unreads))
5688      ((eq type 'read)
5689       (memq article gnus-newsgroup-reads))
5690      ((eq type 'dormant)
5691       (memq article gnus-newsgroup-dormant) )
5692      ((eq type 'expire)
5693       (memq article gnus-newsgroup-expirable))
5694      ((eq type 'reply)
5695       (memq article gnus-newsgroup-replied))
5696      ((eq type 'killed)
5697       (memq article gnus-newsgroup-killed))
5698      ((eq type 'bookmark)
5699       (assq article gnus-newsgroup-bookmarks))
5700      ((eq type 'score)
5701       (assq article gnus-newsgroup-scored))
5702      ((eq type 'save)
5703       (memq article gnus-newsgroup-saved))
5704      ((eq type 'cache)
5705       (memq article gnus-newsgroup-cached))
5706      ((eq type 'forward)
5707       (memq article gnus-newsgroup-forwarded))
5708      ((eq type 'seen)
5709       (not (memq article gnus-newsgroup-unseen)))
5710      ((eq type 'recent)
5711       (memq article gnus-newsgroup-recent))
5712      (t t))))
5713
5714 (defun gnus-articles-to-read (group &optional read-all)
5715   "Find out what articles the user wants to read."
5716   (let* ((articles
5717           ;; Select all articles if `read-all' is non-nil, or if there
5718           ;; are no unread articles.
5719           (if (or read-all
5720                   (and (zerop (length gnus-newsgroup-marked))
5721                        (zerop (length gnus-newsgroup-unreads)))
5722                   ;; Fetch all if the predicate is non-nil.
5723                   gnus-newsgroup-display)
5724               ;; We want to select the headers for all the articles in
5725               ;; the group, so we select either all the active
5726               ;; articles in the group, or (if that's nil), the
5727               ;; articles in the cache.
5728               (or
5729                (if gnus-newsgroup-maximum-articles
5730                    (let ((active (gnus-active group)))
5731                      (gnus-uncompress-range
5732                       (cons (max (car active)
5733                                  (- (cdr active)
5734                                     gnus-newsgroup-maximum-articles
5735                                     -1))
5736                             (cdr active))))
5737                  (gnus-uncompress-range (gnus-active group)))
5738                (gnus-cache-articles-in-group group))
5739             ;; Select only the "normal" subset of articles.
5740             (gnus-sorted-nunion
5741              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5742              gnus-newsgroup-unreads)))
5743          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5744          (scored (length scored-list))
5745          (number (length articles))
5746          (marked (+ (length gnus-newsgroup-marked)
5747                     (length gnus-newsgroup-dormant)))
5748          (select
5749           (cond
5750            ((numberp read-all)
5751             read-all)
5752            ((numberp gnus-newsgroup-display)
5753             gnus-newsgroup-display)
5754            (t
5755             (condition-case ()
5756                 (cond
5757                  ((and (or (<= scored marked) (= scored number))
5758                        (numberp gnus-large-newsgroup)
5759                        (> number gnus-large-newsgroup))
5760                   (let* ((cursor-in-echo-area nil)
5761                          (initial (gnus-parameter-large-newsgroup-initial
5762                                    gnus-newsgroup-name))
5763                          (input
5764                           (read-string
5765                            (format
5766                             "How many articles from %s (%s %d): "
5767                             (gnus-group-decoded-name gnus-newsgroup-name)
5768                             (if initial "max" "default")
5769                             number)
5770                            (if initial
5771                                (cons (number-to-string initial)
5772                                      0)))))
5773                     (if (string-match "^[ \t]*$" input) number input)))
5774                  ((and (> scored marked) (< scored number)
5775                        (> (- scored number) 20))
5776                   (let ((input
5777                          (read-string
5778                           (format "%s %s (%d scored, %d total): "
5779                                   "How many articles from"
5780                                   (gnus-group-decoded-name group)
5781                                   scored number))))
5782                     (if (string-match "^[ \t]*$" input)
5783                         number input)))
5784                  (t number))
5785               (quit
5786                (message "Quit getting the articles to read")
5787                nil))))))
5788     (setq select (if (stringp select) (string-to-number select) select))
5789     (if (or (null select) (zerop select))
5790         select
5791       (if (and (not (zerop scored)) (<= (abs select) scored))
5792           (progn
5793             (setq articles (sort scored-list '<))
5794             (setq number (length articles)))
5795         (setq articles (copy-sequence articles)))
5796
5797       (when (< (abs select) number)
5798         (if (< select 0)
5799             ;; Select the N oldest articles.
5800             (setcdr (nthcdr (1- (abs select)) articles) nil)
5801           ;; Select the N most recent articles.
5802           (setq articles (nthcdr (- number select) articles))))
5803       (setq gnus-newsgroup-unselected
5804             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5805       (when gnus-alter-articles-to-read-function
5806         (setq articles
5807               (sort
5808                (funcall gnus-alter-articles-to-read-function
5809                         gnus-newsgroup-name articles)
5810                '<)))
5811       articles)))
5812
5813 (defun gnus-killed-articles (killed articles)
5814   (let (out)
5815     (while articles
5816       (when (inline (gnus-member-of-range (car articles) killed))
5817         (push (car articles) out))
5818       (setq articles (cdr articles)))
5819     out))
5820
5821 (defun gnus-uncompress-marks (marks)
5822   "Uncompress the mark ranges in MARKS."
5823   (let ((uncompressed '(score bookmark))
5824         out)
5825     (while marks
5826       (if (memq (caar marks) uncompressed)
5827           (push (car marks) out)
5828         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5829       (setq marks (cdr marks)))
5830     out))
5831
5832 (defun gnus-article-mark-to-type (mark)
5833   "Return the type of MARK."
5834   (or (cadr (assq mark gnus-article-special-mark-lists))
5835       'list))
5836
5837 (defun gnus-article-unpropagatable-p (mark)
5838   "Return whether MARK should be propagated to back end."
5839   (memq mark gnus-article-unpropagated-mark-lists))
5840
5841 (defun gnus-adjust-marked-articles (info)
5842   "Set all article lists and remove all marks that are no longer valid."
5843   (let* ((marked-lists (gnus-info-marks info))
5844          (active (gnus-active (gnus-info-group info)))
5845          (min (car active))
5846          (max (cdr active))
5847          (types gnus-article-mark-lists)
5848          marks var articles article mark mark-type
5849          bgn end)
5850
5851     (dolist (marks marked-lists)
5852       (setq mark (car marks)
5853             mark-type (gnus-article-mark-to-type mark)
5854             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5855
5856       ;; We set the variable according to the type of the marks list,
5857       ;; and then adjust the marks to a subset of the active articles.
5858       (cond
5859        ;; Adjust "simple" lists - compressed yet unsorted
5860        ((eq mark-type 'list)
5861         ;; Simultaneously uncompress and clip to active range
5862         ;; See gnus-uncompress-range for a description of possible marks
5863         (let (l lh)
5864           (if (not (cadr marks))
5865               (set var nil)
5866             (setq articles (if (numberp (cddr marks))
5867                                (list (cdr marks))
5868                              (cdr marks))
5869                   lh (cons nil nil)
5870                   l lh)
5871
5872             (while (setq article (pop articles))
5873               (cond ((consp article)
5874                      (setq bgn (max (car article) min)
5875                            end (min (cdr article) max))
5876                      (while (<= bgn end)
5877                        (setq l (setcdr l (cons bgn nil))
5878                              bgn (1+ bgn))))
5879                     ((and (<= min article)
5880                           (>= max article))
5881                      (setq l (setcdr l (cons article nil))))))
5882             (set var (cdr lh)))))
5883        ;; Adjust assocs.
5884        ((eq mark-type 'tuple)
5885         (set var (setq articles (cdr marks)))
5886         (when (not (listp (cdr (symbol-value var))))
5887           (set var (list (symbol-value var))))
5888         (when (not (listp (cdr articles)))
5889           (setq articles (list articles)))
5890         (while articles
5891           (when (or (not (consp (setq article (pop articles))))
5892                     (< (car article) min)
5893                     (> (car article) max))
5894             (set var (delq article (symbol-value var))))))
5895        ;; Adjust ranges (sloppily).
5896        ((eq mark-type 'range)
5897         (cond
5898          ((eq mark 'seen)
5899           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5900           ;; It should be (seen (NUM1 . NUM2)).
5901           (when (numberp (cddr marks))
5902             (setcdr marks (list (cdr marks))))
5903           (setq articles (cdr marks))
5904           (while (and articles
5905                       (or (and (consp (car articles))
5906                                (> min (cdar articles)))
5907                           (and (numberp (car articles))
5908                                (> min (car articles)))))
5909             (pop articles))
5910           (set var articles))))))))
5911
5912 (defun gnus-update-missing-marks (missing)
5913   "Go through the list of MISSING articles and remove them from the mark lists."
5914   (when missing
5915     (let (var m)
5916       ;; Go through all types.
5917       (dolist (elem gnus-article-mark-lists)
5918         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5919           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5920           (when (symbol-value var)
5921             ;; This list has articles.  So we delete all missing
5922             ;; articles from it.
5923             (setq m missing)
5924             (while m
5925               (set var (delq (pop m) (symbol-value var))))))))))
5926
5927 (defun gnus-update-marks ()
5928   "Enter the various lists of marked articles into the newsgroup info list."
5929   (let ((types gnus-article-mark-lists)
5930         (info (gnus-get-info gnus-newsgroup-name))
5931         type list newmarked symbol delta-marks)
5932     (when info
5933       ;; Add all marks lists to the list of marks lists.
5934       (while (setq type (pop types))
5935         (setq list (symbol-value
5936                     (setq symbol
5937                           (intern (format "gnus-newsgroup-%s" (car type))))))
5938
5939         (when list
5940           ;; Get rid of the entries of the articles that have the
5941           ;; default score.
5942           (when (and (eq (cdr type) 'score)
5943                      gnus-save-score
5944                      list)
5945             (let* ((arts list)
5946                    (prev (cons nil list))
5947                    (all prev))
5948               (while arts
5949                 (if (or (not (consp (car arts)))
5950                         (= (cdar arts) gnus-summary-default-score))
5951                     (setcdr prev (cdr arts))
5952                   (setq prev arts))
5953                 (setq arts (cdr arts)))
5954               (setq list (cdr all)))))
5955
5956         (when (eq (cdr type) 'seen)
5957           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5958
5959         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5960           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5961
5962         (when (and (gnus-check-backend-function
5963                     'request-set-mark gnus-newsgroup-name)
5964                    (not (gnus-article-unpropagatable-p (cdr type))))
5965           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5966                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5967                  (add (gnus-remove-from-range
5968                        (gnus-copy-sequence list) old)))
5969             (when add
5970               (push (list add 'add (list (cdr type))) delta-marks))
5971             (when del
5972               (push (list del 'del (list (cdr type))) delta-marks))))
5973
5974         (when list
5975           (push (cons (cdr type) list) newmarked)))
5976
5977       (when delta-marks
5978         (unless (gnus-check-group gnus-newsgroup-name)
5979           (error "Can't open server for %s" gnus-newsgroup-name))
5980         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5981
5982       ;; Enter these new marks into the info of the group.
5983       (if (nthcdr 3 info)
5984           (setcar (nthcdr 3 info) newmarked)
5985         ;; Add the marks lists to the end of the info.
5986         (when newmarked
5987           (setcdr (nthcdr 2 info) (list newmarked))))
5988
5989       ;; Cut off the end of the info if there's nothing else there.
5990       (let ((i 5))
5991         (while (and (> i 2)
5992                     (not (nth i info)))
5993           (when (nthcdr (decf i) info)
5994             (setcdr (nthcdr i info) nil)))))))
5995
5996 (defun gnus-set-mode-line (where)
5997   "Set the mode line of the article or summary buffers.
5998 If WHERE is `summary', the summary mode line format will be used."
5999   ;; Is this mode line one we keep updated?
6000   (when (and (memq where gnus-updated-mode-lines)
6001              (symbol-value
6002               (intern (format "gnus-%s-mode-line-format-spec" where))))
6003     (let (mode-string)
6004       ;; We evaluate this in the summary buffer since these
6005       ;; variables are buffer-local to that buffer.
6006       (with-current-buffer gnus-summary-buffer
6007         ;; We bind all these variables that are used in the `eval' form
6008         ;; below.
6009         (let* ((mformat (symbol-value
6010                          (intern
6011                           (format "gnus-%s-mode-line-format-spec" where))))
6012                (gnus-tmp-group-name (gnus-mode-string-quote
6013                                      (gnus-group-decoded-name
6014                                       gnus-newsgroup-name)))
6015                (gnus-tmp-article-number (or gnus-current-article 0))
6016                (gnus-tmp-unread gnus-newsgroup-unreads)
6017                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6018                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6019                (gnus-tmp-unread-and-unselected
6020                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6021                             (zerop gnus-tmp-unselected))
6022                        "")
6023                       ((zerop gnus-tmp-unselected)
6024                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6025                       (t (format "{%d(+%d) more}"
6026                                  gnus-tmp-unread-and-unticked
6027                                  gnus-tmp-unselected))))
6028                (gnus-tmp-subject
6029                 (if (and gnus-current-headers
6030                          (vectorp gnus-current-headers))
6031                     (gnus-mode-string-quote
6032                      (mail-header-subject gnus-current-headers))
6033                   ""))
6034                bufname-length max-len
6035                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6036           (setq mode-string (eval mformat))
6037           (setq bufname-length (if (string-match "%b" mode-string)
6038                                    (- (length
6039                                        (buffer-name
6040                                         (if (eq where 'summary)
6041                                             nil
6042                                           (get-buffer gnus-article-buffer))))
6043                                       2)
6044                                  0))
6045           (setq max-len (max 4 (if gnus-mode-non-string-length
6046                                    (- (window-width)
6047                                       gnus-mode-non-string-length
6048                                       bufname-length)
6049                                  (length mode-string))))
6050           ;; We might have to chop a bit of the string off...
6051           (when (> (length mode-string) max-len)
6052             (setq mode-string
6053                   (concat (truncate-string-to-width mode-string (- max-len 3))
6054                           "...")))
6055           ;; Pad the mode string a bit.
6056           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6057       ;; Update the mode line.
6058       (setq mode-line-buffer-identification
6059             (gnus-mode-line-buffer-identification (list mode-string)))
6060       (set-buffer-modified-p t))))
6061
6062 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6063   "Go through the HEADERS list and add all Xrefs to a hash table.
6064 The resulting hash table is returned, or nil if no Xrefs were found."
6065   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6066          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6067          (xref-hashtb (gnus-make-hashtable))
6068          start group entry number xrefs header)
6069     (while headers
6070       (setq header (pop headers))
6071       (when (and (setq xrefs (mail-header-xref header))
6072                  (not (memq (setq number (mail-header-number header))
6073                             unreads)))
6074         (setq start 0)
6075         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6076           (setq start (match-end 0))
6077           (setq group (if prefix
6078                           (concat prefix (substring xrefs (match-beginning 1)
6079                                                     (match-end 1)))
6080                         (substring xrefs (match-beginning 1) (match-end 1))))
6081           (setq number
6082                 (string-to-number (substring xrefs (match-beginning 2)
6083                                           (match-end 2))))
6084           (if (setq entry (gnus-gethash group xref-hashtb))
6085               (setcdr entry (cons number (cdr entry)))
6086             (gnus-sethash group (cons number nil) xref-hashtb)))))
6087     (and start xref-hashtb)))
6088
6089 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6090   "Look through all the headers and mark the Xrefs as read."
6091   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6092         name info xref-hashtb idlist method nth4)
6093     (with-current-buffer gnus-group-buffer
6094       (when (setq xref-hashtb
6095                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6096         (mapatoms
6097          (lambda (group)
6098            (unless (string= from-newsgroup (setq name (symbol-name group)))
6099              (setq idlist (symbol-value group))
6100              ;; Dead groups are not updated.
6101              (and (prog1
6102                       (setq info (gnus-get-info name))
6103                     (when (stringp (setq nth4 (gnus-info-method info)))
6104                       (setq nth4 (gnus-server-to-method nth4))))
6105                   ;; Only do the xrefs if the group has the same
6106                   ;; select method as the group we have just read.
6107                   (or (gnus-methods-equal-p
6108                        nth4 (gnus-find-method-for-group from-newsgroup))
6109                       virtual
6110                       (equal nth4 (setq method (gnus-find-method-for-group
6111                                                 from-newsgroup)))
6112                       (and (equal (car nth4) (car method))
6113                            (equal (nth 1 nth4) (nth 1 method))))
6114                   gnus-use-cross-reference
6115                   (or (not (eq gnus-use-cross-reference t))
6116                       virtual
6117                       ;; Only do cross-references on subscribed
6118                       ;; groups, if that is what is wanted.
6119                       (<= (gnus-info-level info) gnus-level-subscribed))
6120                   (gnus-group-make-articles-read name idlist))))
6121          xref-hashtb)))))
6122
6123 (defun gnus-compute-read-articles (group articles)
6124   (let* ((entry (gnus-group-entry group))
6125          (info (nth 2 entry))
6126          (active (gnus-active group))
6127          ninfo)
6128     (when entry
6129       ;; First peel off all invalid article numbers.
6130       (when active
6131         (let ((ids articles)
6132               id first)
6133           (while (setq id (pop ids))
6134             (when (and first (> id (cdr active)))
6135               ;; We'll end up in this situation in one particular
6136               ;; obscure situation.  If you re-scan a group and get
6137               ;; a new article that is cross-posted to a different
6138               ;; group that has not been re-scanned, you might get
6139               ;; crossposted article that has a higher number than
6140               ;; Gnus believes possible.  So we re-activate this
6141               ;; group as well.  This might mean doing the
6142               ;; crossposting thingy will *increase* the number
6143               ;; of articles in some groups.  Tsk, tsk.
6144               (setq active (or (gnus-activate-group group) active)))
6145             (when (or (> id (cdr active))
6146                       (< id (car active)))
6147               (setq articles (delq id articles))))))
6148       ;; If the read list is nil, we init it.
6149       (if (and active
6150                (null (gnus-info-read info))
6151                (> (car active) 1))
6152           (setq ninfo (cons 1 (1- (car active))))
6153         (setq ninfo (gnus-info-read info)))
6154       ;; Then we add the read articles to the range.
6155       (gnus-add-to-range
6156        ninfo (setq articles (sort articles '<))))))
6157
6158 (defun gnus-group-make-articles-read (group articles)
6159   "Update the info of GROUP to say that ARTICLES are read."
6160   (let* ((num 0)
6161          (entry (gnus-group-entry group))
6162          (info (nth 2 entry))
6163          (active (gnus-active group))
6164          range)
6165     (when entry
6166       (setq range (gnus-compute-read-articles group articles))
6167       (with-current-buffer gnus-group-buffer
6168         (gnus-undo-register
6169           `(progn
6170              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6171              (gnus-info-set-read ',info ',(gnus-info-read info))
6172              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6173              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6174              (gnus-group-update-group ,group t))))
6175       ;; Add the read articles to the range.
6176       (gnus-info-set-read info range)
6177       (gnus-request-set-mark group (list (list range 'add '(read))))
6178       ;; Then we have to re-compute how many unread
6179       ;; articles there are in this group.
6180       (when active
6181         (cond
6182          ((not range)
6183           (setq num (- (1+ (cdr active)) (car active))))
6184          ((not (listp (cdr range)))
6185           (setq num (- (cdr active) (- (1+ (cdr range))
6186                                        (car range)))))
6187          (t
6188           (while range
6189             (if (numberp (car range))
6190                 (setq num (1+ num))
6191               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6192             (setq range (cdr range)))
6193           (setq num (- (cdr active) num))))
6194         ;; Update the number of unread articles.
6195         (setcar entry num)
6196         ;; Update the group buffer.
6197         (unless (gnus-ephemeral-group-p group)
6198           (gnus-group-update-group group t))))))
6199
6200 (defvar gnus-newsgroup-none-id 0)
6201
6202 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6203   (let ((cur nntp-server-buffer)
6204         (dependencies
6205          (or dependencies
6206              (with-current-buffer gnus-summary-buffer
6207                gnus-newsgroup-dependencies)))
6208         headers id end ref number
6209         (mail-parse-charset gnus-newsgroup-charset)
6210         (mail-parse-ignored-charsets
6211          (save-current-buffer (condition-case nil
6212                                   (set-buffer gnus-summary-buffer)
6213                                 (error))
6214                               gnus-newsgroup-ignored-charsets)))
6215     (with-current-buffer nntp-server-buffer
6216       ;; Translate all TAB characters into SPACE characters.
6217       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6218       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6219       (ietf-drums-unfold-fws)
6220       (gnus-run-hooks 'gnus-parse-headers-hook)
6221       (let ((case-fold-search t)
6222             in-reply-to header p lines chars)
6223         (goto-char (point-min))
6224         ;; Search to the beginning of the next header.  Error messages
6225         ;; do not begin with 2 or 3.
6226         (while (re-search-forward "^[23][0-9]+ " nil t)
6227           (setq id nil
6228                 ref nil)
6229           ;; This implementation of this function, with nine
6230           ;; search-forwards instead of the one re-search-forward and
6231           ;; a case (which basically was the old function) is actually
6232           ;; about twice as fast, even though it looks messier.  You
6233           ;; can't have everything, I guess.  Speed and elegance
6234           ;; doesn't always go hand in hand.
6235           (setq
6236            header
6237            (vector
6238             ;; Number.
6239             (prog1
6240                 (setq number (read cur))
6241               (end-of-line)
6242               (setq p (point))
6243               (narrow-to-region (point)
6244                                 (or (and (search-forward "\n.\n" nil t)
6245                                          (- (point) 2))
6246                                     (point))))
6247             ;; Subject.
6248             (progn
6249               (goto-char p)
6250               (if (search-forward "\nsubject:" nil t)
6251                   (funcall gnus-decode-encoded-word-function
6252                            (nnheader-header-value))
6253                 "(none)"))
6254             ;; From.
6255             (progn
6256               (goto-char p)
6257               (if (search-forward "\nfrom:" nil t)
6258                   (funcall gnus-decode-encoded-address-function
6259                            (nnheader-header-value))
6260                 "(nobody)"))
6261             ;; Date.
6262             (progn
6263               (goto-char p)
6264               (if (search-forward "\ndate:" nil t)
6265                   (nnheader-header-value) ""))
6266             ;; Message-ID.
6267             (progn
6268               (goto-char p)
6269               (setq id (if (re-search-forward
6270                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6271                            ;; We do it this way to make sure the Message-ID
6272                            ;; is (somewhat) syntactically valid.
6273                            (buffer-substring (match-beginning 1)
6274                                              (match-end 1))
6275                          ;; If there was no message-id, we just fake one
6276                          ;; to make subsequent routines simpler.
6277                          (nnheader-generate-fake-message-id number))))
6278             ;; References.
6279             (progn
6280               (goto-char p)
6281               (if (search-forward "\nreferences:" nil t)
6282                   (progn
6283                     (setq end (point))
6284                     (prog1
6285                         (nnheader-header-value)
6286                       (setq ref
6287                             (buffer-substring
6288                              (progn
6289                                (end-of-line)
6290                                (search-backward ">" end t)
6291                                (1+ (point)))
6292                              (progn
6293                                (search-backward "<" end t)
6294                                (point))))))
6295                 ;; Get the references from the in-reply-to header if there
6296                 ;; were no references and the in-reply-to header looks
6297                 ;; promising.
6298                 (if (and (search-forward "\nin-reply-to:" nil t)
6299                          (setq in-reply-to (nnheader-header-value))
6300                          (string-match "<[^>]+>" in-reply-to))
6301                     (let (ref2)
6302                       (setq ref (substring in-reply-to (match-beginning 0)
6303                                            (match-end 0)))
6304                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6305                         (setq ref2 (substring in-reply-to (match-beginning 0)
6306                                               (match-end 0)))
6307                         (when (> (length ref2) (length ref))
6308                           (setq ref ref2)))
6309                       ref)
6310                   (setq ref nil))))
6311             ;; Chars.
6312             (progn
6313               (goto-char p)
6314               (if (search-forward "\nchars: " nil t)
6315                   (if (numberp (setq chars (ignore-errors (read cur))))
6316                       chars -1)
6317                 -1))
6318             ;; Lines.
6319             (progn
6320               (goto-char p)
6321               (if (search-forward "\nlines: " nil t)
6322                   (if (numberp (setq lines (ignore-errors (read cur))))
6323                       lines -1)
6324                 -1))
6325             ;; Xref.
6326             (progn
6327               (goto-char p)
6328               (and (search-forward "\nxref:" nil t)
6329                    (nnheader-header-value)))
6330             ;; Extra.
6331             (when gnus-extra-headers
6332               (let ((extra gnus-extra-headers)
6333                     out)
6334                 (while extra
6335                   (goto-char p)
6336                   (when (search-forward
6337                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6338                     (push (cons (car extra) (nnheader-header-value))
6339                           out))
6340                   (pop extra))
6341                 out))))
6342           (when (equal id ref)
6343             (setq ref nil))
6344
6345           (when gnus-alter-header-function
6346             (funcall gnus-alter-header-function header)
6347             (setq id (mail-header-id header)
6348                   ref (gnus-parent-id (mail-header-references header))))
6349
6350           (when (setq header
6351                       (gnus-dependencies-add-header
6352                        header dependencies force-new))
6353             (push header headers))
6354           (goto-char (point-max))
6355           (widen))
6356         (nreverse headers)))))
6357
6358 ;; Goes through the xover lines and returns a list of vectors
6359 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6360                                                   force-new dependencies
6361                                                   group also-fetch-heads)
6362   "Parse the news overview data in the server buffer.
6363 Return a list of headers that match SEQUENCE (see
6364 `nntp-retrieve-headers')."
6365   ;; Get the Xref when the users reads the articles since most/some
6366   ;; NNTP servers do not include Xrefs when using XOVER.
6367   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6368   (let ((mail-parse-charset gnus-newsgroup-charset)
6369         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6370         (cur nntp-server-buffer)
6371         (dependencies (or dependencies gnus-newsgroup-dependencies))
6372         (allp (cond
6373                ((eq gnus-read-all-available-headers t)
6374                 t)
6375                ((and (stringp gnus-read-all-available-headers)
6376                      group)
6377                 (string-match gnus-read-all-available-headers group))
6378                (t
6379                 nil)))
6380         number headers header)
6381     (with-current-buffer nntp-server-buffer
6382       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6383       ;; Allow the user to mangle the headers before parsing them.
6384       (gnus-run-hooks 'gnus-parse-headers-hook)
6385       (goto-char (point-min))
6386       (gnus-parse-without-error
6387         (while (and (or sequence allp)
6388                     (not (eobp)))
6389           (setq number (read cur))
6390           (when (not allp)
6391             (while (and sequence
6392                         (< (car sequence) number))
6393               (setq sequence (cdr sequence))))
6394           (when (and (or allp
6395                          (and sequence
6396                               (eq number (car sequence))))
6397                      (progn
6398                        (setq sequence (cdr sequence))
6399                        (setq header (inline
6400                                       (gnus-nov-parse-line
6401                                        number dependencies force-new)))))
6402             (push header headers))
6403           (forward-line 1)))
6404       ;; A common bug in inn is that if you have posted an article and
6405       ;; then retrieves the active file, it will answer correctly --
6406       ;; the new article is included.  However, a NOV entry for the
6407       ;; article may not have been generated yet, so this may fail.
6408       ;; We work around this problem by retrieving the last few
6409       ;; headers using HEAD.
6410       (if (or (not also-fetch-heads)
6411               (not sequence))
6412           ;; We (probably) got all the headers.
6413           (nreverse headers)
6414         (let ((gnus-nov-is-evil t))
6415           (nconc
6416            (nreverse headers)
6417            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6418              (gnus-get-newsgroup-headers))))))))
6419
6420 (defun gnus-article-get-xrefs ()
6421   "Fill in the Xref value in `gnus-current-headers', if necessary.
6422 This is meant to be called in `gnus-article-internal-prepare-hook'."
6423   (let ((headers (with-current-buffer gnus-summary-buffer
6424                    gnus-current-headers)))
6425     (or (not gnus-use-cross-reference)
6426         (not headers)
6427         (and (mail-header-xref headers)
6428              (not (string= (mail-header-xref headers) "")))
6429         (let ((case-fold-search t)
6430               xref)
6431           (save-restriction
6432             (nnheader-narrow-to-headers)
6433             (goto-char (point-min))
6434             (when (or (and (not (eobp))
6435                            (eq (downcase (char-after)) ?x)
6436                            (looking-at "Xref:"))
6437                       (search-forward "\nXref:" nil t))
6438               (goto-char (1+ (match-end 0)))
6439               (setq xref (buffer-substring (point) (point-at-eol)))
6440               (mail-header-set-xref headers xref)))))))
6441
6442 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6443   "Find article ID and insert the summary line for that article.
6444 OLD-HEADER can either be a header or a line number to insert
6445 the subject line on."
6446   (let* ((line (and (numberp old-header) old-header))
6447          (old-header (and (vectorp old-header) old-header))
6448          (header (cond ((and old-header use-old-header)
6449                         old-header)
6450                        ((and (numberp id)
6451                              (gnus-number-to-header id))
6452                         (gnus-number-to-header id))
6453                        (t
6454                         (gnus-read-header id))))
6455          (number (and (numberp id) id))
6456          d)
6457     (when header
6458       ;; Rebuild the thread that this article is part of and go to the
6459       ;; article we have fetched.
6460       (when (and (not gnus-show-threads)
6461                  old-header)
6462         (when (and number
6463                    (setq d (gnus-data-find (mail-header-number old-header))))
6464           (goto-char (gnus-data-pos d))
6465           (gnus-data-remove
6466            number
6467            (- (point-at-bol)
6468               (prog1
6469                   (1+ (point-at-eol))
6470                 (gnus-delete-line))))))
6471       ;; Remove list identifiers from subject.
6472       (when gnus-list-identifiers
6473         (let ((gnus-newsgroup-headers (list header)))
6474           (gnus-summary-remove-list-identifiers)))
6475       (when old-header
6476         (mail-header-set-number header (mail-header-number old-header)))
6477       (setq gnus-newsgroup-sparse
6478             (delq (setq number (mail-header-number header))
6479                   gnus-newsgroup-sparse))
6480       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6481       (push number gnus-newsgroup-limit)
6482       (gnus-rebuild-thread (mail-header-id header) line)
6483       (gnus-summary-goto-subject number nil t))
6484     (when (and (numberp number)
6485                (> number 0))
6486       ;; We have to update the boundaries even if we can't fetch the
6487       ;; article if ID is a number -- so that the next `P' or `N'
6488       ;; command will fetch the previous (or next) article even
6489       ;; if the one we tried to fetch this time has been canceled.
6490       (when (> number gnus-newsgroup-end)
6491         (setq gnus-newsgroup-end number))
6492       (when (< number gnus-newsgroup-begin)
6493         (setq gnus-newsgroup-begin number))
6494       (setq gnus-newsgroup-unselected
6495             (delq number gnus-newsgroup-unselected)))
6496     ;; Report back a success?
6497     (and header (mail-header-number header))))
6498
6499 ;;; Process/prefix in the summary buffer
6500
6501 (defun gnus-summary-work-articles (n)
6502   "Return a list of articles to be worked upon.
6503 The prefix argument, the list of process marked articles, and the
6504 current article will be taken into consideration."
6505   (with-current-buffer gnus-summary-buffer
6506     (cond
6507      (n
6508       ;; A numerical prefix has been given.
6509       (setq n (prefix-numeric-value n))
6510       (let ((backward (< n 0))
6511             (n (abs (prefix-numeric-value n)))
6512             articles article)
6513         (save-excursion
6514           (while
6515               (and (> n 0)
6516                    (push (setq article (gnus-summary-article-number))
6517                          articles)
6518                    (if backward
6519                        (gnus-summary-find-prev nil article)
6520                      (gnus-summary-find-next nil article)))
6521             (decf n)))
6522         (nreverse articles)))
6523      ((and (gnus-region-active-p) (mark))
6524       (message "region active")
6525       ;; Work on the region between point and mark.
6526       (let ((max (max (point) (mark)))
6527             articles article)
6528         (save-excursion
6529           (goto-char (min (point) (mark)))
6530           (while
6531               (and
6532                (push (setq article (gnus-summary-article-number)) articles)
6533                (gnus-summary-find-next nil article)
6534                (< (point) max)))
6535           (nreverse articles))))
6536      (gnus-newsgroup-processable
6537       ;; There are process-marked articles present.
6538       ;; Save current state.
6539       (gnus-summary-save-process-mark)
6540       ;; Return the list.
6541       (reverse gnus-newsgroup-processable))
6542      (t
6543       ;; Just return the current article.
6544       (list (gnus-summary-article-number))))))
6545
6546 (defmacro gnus-summary-iterate (arg &rest forms)
6547   "Iterate over the process/prefixed articles and do FORMS.
6548 ARG is the interactive prefix given to the command.  FORMS will be
6549 executed with point over the summary line of the articles."
6550   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6551     `(let ((,articles (gnus-summary-work-articles ,arg)))
6552        (while ,articles
6553          (gnus-summary-goto-subject (car ,articles))
6554          ,@forms
6555          (pop ,articles)))))
6556
6557 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6558 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6559
6560 (defun gnus-summary-save-process-mark ()
6561   "Push the current set of process marked articles on the stack."
6562   (interactive)
6563   (push (copy-sequence gnus-newsgroup-processable)
6564         gnus-newsgroup-process-stack))
6565
6566 (defun gnus-summary-kill-process-mark ()
6567   "Push the current set of process marked articles on the stack and unmark."
6568   (interactive)
6569   (gnus-summary-save-process-mark)
6570   (gnus-summary-unmark-all-processable))
6571
6572 (defun gnus-summary-yank-process-mark ()
6573   "Pop the last process mark state off the stack and restore it."
6574   (interactive)
6575   (unless gnus-newsgroup-process-stack
6576     (error "Empty mark stack"))
6577   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6578
6579 (defun gnus-summary-process-mark-set (set)
6580   "Make SET into the current process marked articles."
6581   (gnus-summary-unmark-all-processable)
6582   (mapc 'gnus-summary-set-process-mark set))
6583
6584 ;;; Searching and stuff
6585
6586 (defun gnus-summary-search-group (&optional backward use-level)
6587   "Search for next unread newsgroup.
6588 If optional argument BACKWARD is non-nil, search backward instead."
6589   (with-current-buffer gnus-group-buffer
6590     (when (gnus-group-search-forward
6591            backward nil (if use-level (gnus-group-group-level) nil))
6592       (gnus-group-group-name))))
6593
6594 (defun gnus-summary-best-group (&optional exclude-group)
6595   "Find the name of the best unread group.
6596 If EXCLUDE-GROUP, do not go to this group."
6597   (with-current-buffer gnus-group-buffer
6598     (save-excursion
6599       (gnus-group-best-unread-group exclude-group))))
6600
6601 (defun gnus-summary-find-next (&optional unread article backward)
6602   (if backward
6603       (gnus-summary-find-prev unread article)
6604     (let* ((dummy (gnus-summary-article-intangible-p))
6605            (article (or article (gnus-summary-article-number)))
6606            (data (gnus-data-find-list article))
6607            result)
6608       (when (and (not dummy)
6609                  (or (not gnus-summary-check-current)
6610                      (not unread)
6611                      (not (gnus-data-unread-p (car data)))))
6612         (setq data (cdr data)))
6613       (when (setq result
6614                   (if unread
6615                       (progn
6616                         (while data
6617                           (unless (memq (gnus-data-number (car data))
6618                                         (cond
6619                                          ((eq gnus-auto-goto-ignores
6620                                               'always-undownloaded)
6621                                           gnus-newsgroup-undownloaded)
6622                                          (gnus-plugged
6623                                           nil)
6624                                          ((eq gnus-auto-goto-ignores
6625                                               'unfetched)
6626                                           gnus-newsgroup-unfetched)
6627                                          ((eq gnus-auto-goto-ignores
6628                                               'undownloaded)
6629                                           gnus-newsgroup-undownloaded)))
6630                             (when (gnus-data-unread-p (car data))
6631                               (setq result (car data)
6632                                     data nil)))
6633                           (setq data (cdr data)))
6634                         result)
6635                     (car data)))
6636         (goto-char (gnus-data-pos result))
6637         (gnus-data-number result)))))
6638
6639 (defun gnus-summary-find-prev (&optional unread article)
6640   (let* ((eobp (eobp))
6641          (article (or article (gnus-summary-article-number)))
6642          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6643          result)
6644     (when (and (not eobp)
6645                (or (not gnus-summary-check-current)
6646                    (not unread)
6647                    (not (gnus-data-unread-p (car data)))))
6648       (setq data (cdr data)))
6649     (when (setq result
6650                 (if unread
6651                     (progn
6652                       (while data
6653                         (unless (memq (gnus-data-number (car data))
6654                                       (cond
6655                                        ((eq gnus-auto-goto-ignores
6656                                             'always-undownloaded)
6657                                         gnus-newsgroup-undownloaded)
6658                                        (gnus-plugged
6659                                         nil)
6660                                        ((eq gnus-auto-goto-ignores
6661                                             'unfetched)
6662                                         gnus-newsgroup-unfetched)
6663                                        ((eq gnus-auto-goto-ignores
6664                                             'undownloaded)
6665                                         gnus-newsgroup-undownloaded)))
6666                           (when (gnus-data-unread-p (car data))
6667                             (setq result (car data)
6668                                   data nil)))
6669                         (setq data (cdr data)))
6670                       result)
6671                   (car data)))
6672       (goto-char (gnus-data-pos result))
6673       (gnus-data-number result))))
6674
6675 (defun gnus-summary-find-subject (subject &optional unread backward article)
6676   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6677          (article (or article (gnus-summary-article-number)))
6678          (articles (gnus-data-list backward))
6679          (arts (gnus-data-find-list article articles))
6680          result)
6681     (when (or (not gnus-summary-check-current)
6682               (not unread)
6683               (not (gnus-data-unread-p (car arts))))
6684       (setq arts (cdr arts)))
6685     (while arts
6686       (and (or (not unread)
6687                (gnus-data-unread-p (car arts)))
6688            (vectorp (gnus-data-header (car arts)))
6689            (gnus-subject-equal
6690             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6691            (setq result (car arts)
6692                  arts nil))
6693       (setq arts (cdr arts)))
6694     (and result
6695          (goto-char (gnus-data-pos result))
6696          (gnus-data-number result))))
6697
6698 (defun gnus-summary-search-forward (&optional unread subject backward)
6699   "Search forward for an article.
6700 If UNREAD, look for unread articles.  If SUBJECT, look for
6701 articles with that subject.  If BACKWARD, search backward instead."
6702   (cond (subject (gnus-summary-find-subject subject unread backward))
6703         (backward (gnus-summary-find-prev unread))
6704         (t (gnus-summary-find-next unread))))
6705
6706 (defun gnus-recenter (&optional n)
6707   "Center point in window and redisplay frame.
6708 Also do horizontal recentering."
6709   (interactive "P")
6710   (when (and gnus-auto-center-summary
6711              (not (eq gnus-auto-center-summary 'vertical)))
6712     (gnus-horizontal-recenter))
6713   (if (fboundp 'recenter-top-bottom)
6714       (recenter-top-bottom n)
6715     (recenter n)))
6716
6717 (put 'gnus-recenter 'isearch-scroll t)
6718
6719 (defun gnus-forward-line-ignore-invisible (n)
6720   "Move N lines forward (backward if N is negative).
6721 Like forward-line, but skip over (and don't count) invisible lines."
6722   (let (done)
6723     (while (and (> n 0) (not done))
6724       ;; If the following character is currently invisible,
6725       ;; skip all characters with that same `invisible' property value.
6726       (while (gnus-invisible-p (point))
6727         (goto-char (gnus-next-char-property-change (point))))
6728       (forward-line 1)
6729       (if (eobp)
6730           (setq done t)
6731         (setq n (1- n))))
6732     (while (and (< n 0) (not done))
6733       (forward-line -1)
6734       (if (bobp) (setq done t)
6735         (setq n (1+ n))
6736         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6737           (goto-char (gnus-previous-char-property-change (point))))))))
6738
6739 (defun gnus-summary-recenter ()
6740   "Center point in the summary window.
6741 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6742 displayed, no centering will be performed."
6743   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6744   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6745   (interactive)
6746   ;; The user has to want it.
6747   (when gnus-auto-center-summary
6748     (let* ((top (cond ((< (window-height) 4) 0)
6749                       ((< (window-height) 7) 1)
6750                       (t (if (numberp gnus-auto-center-summary)
6751                              gnus-auto-center-summary
6752                            (/ (1- (window-height)) 2)))))
6753            (height (1- (window-height)))
6754            (bottom (save-excursion
6755                      (goto-char (point-max))
6756                      (gnus-forward-line-ignore-invisible (- height))
6757                      (point)))
6758            (window (get-buffer-window (current-buffer))))
6759       (when (get-buffer-window gnus-article-buffer)
6760         ;; Only do recentering when the article buffer is displayed,
6761         ;; Set the window start to either `bottom', which is the biggest
6762         ;; possible valid number, or the second line from the top,
6763         ;; whichever is the least.
6764         (let ((top-pos (save-excursion
6765                          (gnus-forward-line-ignore-invisible (- top))
6766                          (point))))
6767           (if (> bottom top-pos)
6768               ;; Keep the second line from the top visible
6769               (set-window-start window top-pos)
6770             ;; Try to keep the bottom line visible; if it's partially
6771             ;; obscured, either scroll one more line to make it fully
6772             ;; visible, or revert to using TOP-POS.
6773             (save-excursion
6774               (goto-char (point-max))
6775               (gnus-forward-line-ignore-invisible -1)
6776               (let ((last-line-start (point)))
6777                 (goto-char bottom)
6778                 (set-window-start window (point) t)
6779                 (when (not (pos-visible-in-window-p last-line-start window))
6780                   (gnus-forward-line-ignore-invisible 1)
6781                   (set-window-start window (min (point) top-pos) t)))))))
6782       ;; Do horizontal recentering while we're at it.
6783       (when (and (get-buffer-window (current-buffer) t)
6784                  (not (eq gnus-auto-center-summary 'vertical)))
6785         (let ((selected (selected-window)))
6786           (select-window (get-buffer-window (current-buffer) t))
6787           (gnus-summary-position-point)
6788           (gnus-horizontal-recenter)
6789           (select-window selected))))))
6790
6791 (defun gnus-summary-jump-to-group (newsgroup)
6792   "Move point to NEWSGROUP in group mode buffer."
6793   ;; Keep update point of group mode buffer if visible.
6794   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6795       (save-window-excursion
6796         ;; Take care of tree window mode.
6797         (when (get-buffer-window gnus-group-buffer)
6798           (pop-to-buffer gnus-group-buffer))
6799         (gnus-group-jump-to-group newsgroup))
6800     (save-excursion
6801       ;; Take care of tree window mode.
6802       (if (get-buffer-window gnus-group-buffer 0)
6803           (pop-to-buffer gnus-group-buffer)
6804         (set-buffer gnus-group-buffer))
6805       (gnus-group-jump-to-group newsgroup))))
6806
6807 ;; This function returns a list of article numbers based on the
6808 ;; difference between the ranges of read articles in this group and
6809 ;; the range of active articles.
6810 (defun gnus-list-of-unread-articles (group)
6811   (let* ((read (gnus-info-read (gnus-get-info group)))
6812          (active (or (gnus-active group) (gnus-activate-group group)))
6813          (last (or (cdr active)
6814                    (error "Group %s couldn't be activated " group)))
6815          (bottom (if gnus-newsgroup-maximum-articles
6816                      (max (car active)
6817                           (- last gnus-newsgroup-maximum-articles -1))
6818                    (car active)))
6819          first nlast unread)
6820     ;; If none are read, then all are unread.
6821     (if (not read)
6822         (setq first bottom)
6823       ;; If the range of read articles is a single range, then the
6824       ;; first unread article is the article after the last read
6825       ;; article.  Sounds logical, doesn't it?
6826       (if (and (not (listp (cdr read)))
6827                (or (< (car read) bottom)
6828                    (progn (setq read (list read))
6829                           nil)))
6830           (setq first (max bottom (1+ (cdr read))))
6831         ;; `read' is a list of ranges.
6832         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6833                                   (caar read)))
6834                   1)
6835           (setq first bottom))
6836         (while read
6837           (when first
6838             (while (< first nlast)
6839               (setq unread (cons first unread)
6840                     first (1+ first))))
6841           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6842           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6843           (setq read (cdr read)))))
6844     ;; And add the last unread articles.
6845     (while (<= first last)
6846       (setq unread (cons first unread)
6847             first (1+ first)))
6848     ;; Return the list of unread articles.
6849     (delq 0 (nreverse unread))))
6850
6851 (defun gnus-list-of-read-articles (group)
6852   "Return a list of unread, unticked and non-dormant articles."
6853   (let* ((info (gnus-get-info group))
6854          (marked (gnus-info-marks info))
6855          (active (gnus-active group)))
6856     (and info active
6857          (gnus-list-range-difference
6858           (gnus-list-range-difference
6859            (gnus-sorted-complement
6860             (gnus-uncompress-range
6861              (if gnus-newsgroup-maximum-articles
6862                  (cons (max (car active)
6863                             (- (cdr active)
6864                                gnus-newsgroup-maximum-articles
6865                                -1))
6866                        (cdr active))
6867                active))
6868             (gnus-list-of-unread-articles group))
6869            (cdr (assq 'dormant marked)))
6870           (cdr (assq 'tick marked))))))
6871
6872 ;; This function returns a sequence of article numbers based on the
6873 ;; difference between the ranges of read articles in this group and
6874 ;; the range of active articles.
6875 (defun gnus-sequence-of-unread-articles (group)
6876   (let* ((read (gnus-info-read (gnus-get-info group)))
6877          (active (or (gnus-active group) (gnus-activate-group group)))
6878          (last (cdr active))
6879          (bottom (if gnus-newsgroup-maximum-articles
6880                      (max (car active)
6881                           (- last gnus-newsgroup-maximum-articles -1))
6882                    (car active)))
6883          first nlast unread)
6884     ;; If none are read, then all are unread.
6885     (if (not read)
6886         (setq first bottom)
6887       ;; If the range of read articles is a single range, then the
6888       ;; first unread article is the article after the last read
6889       ;; article.  Sounds logical, doesn't it?
6890       (if (and (not (listp (cdr read)))
6891                (or (< (car read) bottom)
6892                    (progn (setq read (list read))
6893                           nil)))
6894           (setq first (max bottom (1+ (cdr read))))
6895         ;; `read' is a list of ranges.
6896         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6897                                   (caar read)))
6898                   1)
6899           (setq first bottom))
6900         (while read
6901           (when first
6902             (push (cons first nlast) unread))
6903           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6904           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6905           (setq read (cdr read)))))
6906     ;; And add the last unread articles.
6907     (cond ((not (and first last))
6908            nil)
6909           ((< first last)
6910            (push (cons first last) unread))
6911           ((= first last)
6912            (push first unread)))
6913     ;; Return the sequence of unread articles.
6914     (delq 0 (nreverse unread))))
6915
6916 ;; Various summary commands
6917
6918 (defun gnus-summary-select-article-buffer ()
6919   "Reconfigure windows to show article buffer."
6920   (interactive)
6921   (if (not (gnus-buffer-live-p gnus-article-buffer))
6922       (error "There is no article buffer for this summary buffer")
6923     (gnus-configure-windows 'article)
6924     (select-window (get-buffer-window gnus-article-buffer))))
6925
6926 (defun gnus-summary-universal-argument (arg)
6927   "Perform any operation on all articles that are process/prefixed."
6928   (interactive "P")
6929   (let ((articles (gnus-summary-work-articles arg))
6930         func article)
6931     (if (eq
6932          (setq
6933           func
6934           (key-binding
6935            (read-key-sequence
6936             (substitute-command-keys
6937              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6938          'undefined)
6939         (gnus-error 1 "Undefined key")
6940       (save-excursion
6941         (while articles
6942           (gnus-summary-goto-subject (setq article (pop articles)))
6943           (let (gnus-newsgroup-processable)
6944             (command-execute func))
6945           (gnus-summary-remove-process-mark article)))))
6946   (gnus-summary-position-point))
6947
6948 (defun gnus-summary-toggle-truncation (&optional arg)
6949   "Toggle truncation of summary lines.
6950 With ARG, turn line truncation on if ARG is positive."
6951   (interactive "P")
6952   (setq truncate-lines
6953         (if (null arg) (not truncate-lines)
6954           (> (prefix-numeric-value arg) 0)))
6955   (redraw-display))
6956
6957 (defun gnus-summary-find-for-reselect ()
6958   "Return the number of an article to stay on across a reselect.
6959 The current article is considered, then following articles, then previous
6960 articles.  An article is sought which is not cancelled and isn't a temporary
6961 insertion from another group.  If there's no such then return a dummy 0."
6962   (let (found)
6963     (dolist (rev '(nil t))
6964       (unless found      ; don't demand the reverse list if we don't need it
6965         (let ((data (gnus-data-find-list
6966                      (gnus-summary-article-number) (gnus-data-list rev))))
6967           (while (and data (not found))
6968             (if (and (< 0 (gnus-data-number (car data)))
6969                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6970                 (setq found (gnus-data-number (car data))))
6971             (setq data (cdr data))))))
6972     (or found 0)))
6973
6974 (defun gnus-summary-reselect-current-group (&optional all rescan)
6975   "Exit and then reselect the current newsgroup.
6976 The prefix argument ALL means to select all articles."
6977   (interactive "P")
6978   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6979     (error "Ephemeral groups can't be reselected"))
6980   (let ((current-subject (gnus-summary-find-for-reselect))
6981         (group gnus-newsgroup-name))
6982     (setq gnus-newsgroup-begin nil)
6983     (gnus-summary-exit nil 'leave-hidden)
6984     ;; We have to adjust the point of group mode buffer because
6985     ;; point was moved to the next unread newsgroup by exiting.
6986     (gnus-summary-jump-to-group group)
6987     (when rescan
6988       (save-excursion
6989         (gnus-group-get-new-news-this-group 1)))
6990     (gnus-group-read-group all t)
6991     (gnus-summary-goto-subject current-subject nil t)))
6992
6993 (defun gnus-summary-rescan-group (&optional all)
6994   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6995   (interactive "P")
6996   (gnus-summary-reselect-current-group all t))
6997
6998 (defun gnus-summary-update-info (&optional non-destructive)
6999   (save-excursion
7000     (let ((group gnus-newsgroup-name))
7001       (when group
7002         (when gnus-newsgroup-kill-headers
7003           (setq gnus-newsgroup-killed
7004                 (gnus-compress-sequence
7005                  (gnus-sorted-union
7006                   (gnus-list-range-intersection
7007                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7008                   gnus-newsgroup-unreads)
7009                  t)))
7010         (unless (listp (cdr gnus-newsgroup-killed))
7011           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7012         (let ((headers gnus-newsgroup-headers))
7013           ;; Set the new ranges of read articles.
7014           (with-current-buffer gnus-group-buffer
7015             (gnus-undo-force-boundary))
7016           (gnus-update-read-articles
7017            group (gnus-sorted-union
7018                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7019           ;; Set the current article marks.
7020           (let ((gnus-newsgroup-scored
7021                  (if (and (not gnus-save-score)
7022                           (not non-destructive))
7023                      nil
7024                    gnus-newsgroup-scored)))
7025             (save-excursion
7026               (gnus-update-marks)))
7027           ;; Do the cross-ref thing.
7028           (when gnus-use-cross-reference
7029             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7030           ;; Do not switch windows but change the buffer to work.
7031           (set-buffer gnus-group-buffer)
7032           (unless (gnus-ephemeral-group-p group)
7033             (gnus-group-update-group group)))))))
7034
7035 (defun gnus-summary-save-newsrc (&optional force)
7036   "Save the current number of read/marked articles in the dribble buffer.
7037 The dribble buffer will then be saved.
7038 If FORCE (the prefix), also save the .newsrc file(s)."
7039   (interactive "P")
7040   (gnus-summary-update-info t)
7041   (if force
7042       (gnus-save-newsrc-file)
7043     (gnus-dribble-save)))
7044
7045 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7046
7047 (defun gnus-summary-exit (&optional temporary leave-hidden)
7048   "Exit reading current newsgroup, and then return to group selection mode.
7049 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7050   (interactive)
7051   (gnus-set-global-variables)
7052   (when (gnus-buffer-live-p gnus-article-buffer)
7053     (with-current-buffer gnus-article-buffer
7054       (mm-destroy-parts gnus-article-mime-handles)
7055       ;; Set it to nil for safety reason.
7056       (setq gnus-article-mime-handle-alist nil)
7057       (setq gnus-article-mime-handles nil)))
7058   (gnus-kill-save-kill-buffer)
7059   (gnus-async-halt-prefetch)
7060   (let* ((group gnus-newsgroup-name)
7061          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7062          (gnus-group-is-exiting-p t)
7063          (mode major-mode)
7064          (group-point nil)
7065          (buf (current-buffer)))
7066     (unless quit-config
7067       ;; Do adaptive scoring, and possibly save score files.
7068       (when gnus-newsgroup-adaptive
7069         (gnus-score-adaptive))
7070       (when gnus-use-scoring
7071         (gnus-score-save)))
7072     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7073     ;; If we have several article buffers, we kill them at exit.
7074     (unless gnus-single-article-buffer
7075       (when (gnus-buffer-live-p gnus-article-buffer)
7076         (with-current-buffer gnus-article-buffer
7077           ;; Don't kill sticky article buffers
7078           (unless (eq major-mode 'gnus-sticky-article-mode)
7079             (gnus-kill-buffer gnus-article-buffer)
7080             (setq gnus-article-current nil))))
7081       (gnus-kill-buffer gnus-original-article-buffer))
7082     (when gnus-use-cache
7083       (gnus-cache-possibly-remove-articles)
7084       (gnus-cache-save-buffers))
7085     (gnus-async-prefetch-remove-group group)
7086     (when gnus-suppress-duplicates
7087       (gnus-dup-enter-articles))
7088     (when gnus-use-trees
7089       (gnus-tree-close group))
7090     (when gnus-use-cache
7091       (gnus-cache-write-active))
7092     ;; Remove entries for this group.
7093     (nnmail-purge-split-history (gnus-group-real-name group))
7094     ;; Make all changes in this group permanent.
7095     (unless quit-config
7096       (gnus-run-hooks 'gnus-exit-group-hook)
7097       (gnus-summary-update-info))
7098     (gnus-close-group group)
7099     ;; Make sure where we were, and go to next newsgroup.
7100     (set-buffer gnus-group-buffer)
7101     (unless quit-config
7102       (gnus-group-jump-to-group group))
7103     (gnus-run-hooks 'gnus-summary-exit-hook)
7104     (unless (or quit-config
7105                 (not gnus-summary-next-group-on-exit)
7106                 ;; If this group has disappeared from the summary
7107                 ;; buffer, don't skip forwards.
7108                 (not (string= group (gnus-group-group-name))))
7109       (gnus-group-next-unread-group 1))
7110     (setq group-point (point))
7111     (if temporary
7112         nil                             ;Nothing to do.
7113       (set-buffer buf)
7114       (if (not gnus-kill-summary-on-exit)
7115           (progn
7116             (gnus-deaden-summary)
7117             (setq mode nil))
7118         ;; We set all buffer-local variables to nil.  It is unclear why
7119         ;; this is needed, but if we don't, buffer-local variables are
7120         ;; not garbage-collected, it seems.  This would the lead to en
7121         ;; ever-growing Emacs.
7122         (gnus-summary-clear-local-variables)
7123         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7124           (gnus-summary-clear-local-variables))
7125         (when (get-buffer gnus-article-buffer)
7126           (bury-buffer gnus-article-buffer))
7127         ;; Return to group mode buffer.
7128         (when (eq mode 'gnus-summary-mode)
7129           (gnus-kill-buffer buf)))
7130       (setq gnus-current-select-method gnus-select-method)
7131       (set-buffer gnus-group-buffer)
7132       (if quit-config
7133           (gnus-handle-ephemeral-exit quit-config)
7134         (goto-char group-point)
7135         ;; If gnus-group-buffer is already displayed, make sure we also move
7136         ;; the cursor in the window that displays it.
7137         (let ((win (get-buffer-window (current-buffer) 0)))
7138           (if win (set-window-point win (point))))
7139         (unless leave-hidden
7140           (gnus-configure-windows 'group 'force)))
7141       ;; Clear the current group name.
7142       (unless quit-config
7143         (setq gnus-newsgroup-name nil)))))
7144
7145 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7146 (defun gnus-summary-exit-no-update (&optional no-questions)
7147   "Quit reading current newsgroup without updating read article info."
7148   (interactive)
7149   (let* ((group gnus-newsgroup-name)
7150          (gnus-group-is-exiting-p t)
7151          (gnus-group-is-exiting-without-update-p t)
7152          (quit-config (gnus-group-quit-config group)))
7153     (when (or no-questions
7154               gnus-expert-user
7155               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7156       (gnus-async-halt-prefetch)
7157       (run-hooks 'gnus-summary-prepare-exit-hook)
7158       (when (gnus-buffer-live-p gnus-article-buffer)
7159         (with-current-buffer gnus-article-buffer
7160           (mm-destroy-parts gnus-article-mime-handles)
7161           ;; Set it to nil for safety reason.
7162           (setq gnus-article-mime-handle-alist nil)
7163           (setq gnus-article-mime-handles nil)))
7164       ;; If we have several article buffers, we kill them at exit.
7165       (unless gnus-single-article-buffer
7166         (gnus-kill-buffer gnus-article-buffer)
7167         (gnus-kill-buffer gnus-original-article-buffer)
7168         (setq gnus-article-current nil))
7169       (if (not gnus-kill-summary-on-exit)
7170           (gnus-deaden-summary)
7171         (gnus-close-group group)
7172         (gnus-summary-clear-local-variables)
7173         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7174           (gnus-summary-clear-local-variables))
7175         (gnus-kill-buffer gnus-summary-buffer))
7176       (unless gnus-single-article-buffer
7177         (setq gnus-article-current nil))
7178       (when gnus-use-trees
7179         (gnus-tree-close group))
7180       (gnus-async-prefetch-remove-group group)
7181       (when (get-buffer gnus-article-buffer)
7182         (bury-buffer gnus-article-buffer))
7183       ;; Return to the group buffer.
7184       (gnus-configure-windows 'group 'force)
7185       ;; Clear the current group name.
7186       (setq gnus-newsgroup-name nil)
7187       (unless (gnus-ephemeral-group-p group)
7188         (gnus-group-update-group group))
7189       (when (equal (gnus-group-group-name) group)
7190         (gnus-group-next-unread-group 1))
7191       (when quit-config
7192         (gnus-handle-ephemeral-exit quit-config)))))
7193
7194 (defun gnus-handle-ephemeral-exit (quit-config)
7195   "Handle movement when leaving an ephemeral group.
7196 The state which existed when entering the ephemeral is reset."
7197   (if (not (buffer-name (car quit-config)))
7198       (gnus-configure-windows 'group 'force)
7199     (set-buffer (car quit-config))
7200     (cond ((eq major-mode 'gnus-summary-mode)
7201            (gnus-set-global-variables))
7202           ((eq major-mode 'gnus-article-mode)
7203            (save-current-buffer
7204              ;; The `gnus-summary-buffer' variable may point
7205              ;; to the old summary buffer when using a single
7206              ;; article buffer.
7207              (unless (gnus-buffer-live-p gnus-summary-buffer)
7208                (set-buffer gnus-group-buffer))
7209              (set-buffer gnus-summary-buffer)
7210              (gnus-set-global-variables))))
7211     (if (or (eq (cdr quit-config) 'article)
7212             (eq (cdr quit-config) 'pick))
7213         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7214             (gnus-configure-windows 'pick 'force)
7215           (gnus-configure-windows (cdr quit-config) 'force))
7216       (gnus-configure-windows (cdr quit-config) 'force))
7217     (when (eq major-mode 'gnus-summary-mode)
7218       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7219                                                      next-unread-noselect))
7220           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7221                                   'next-noselect)
7222                               (gnus-summary-next-subject 1 nil t))
7223                              ((eq gnus-auto-select-on-ephemeral-exit
7224                                   'next-unread-noselect)
7225                               (gnus-summary-next-subject 1 t t))))
7226             ;; Hide the article buffer which displays the article different
7227             ;; from the one that the cursor points to in the summary buffer.
7228             (gnus-configure-windows 'summary 'force))
7229         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7230                (gnus-summary-next-subject 1))
7231               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7232                (gnus-summary-next-subject 1 t))))
7233       (gnus-summary-recenter)
7234       (gnus-summary-position-point))))
7235
7236 ;;; Dead summaries.
7237
7238 (defvar gnus-dead-summary-mode-map
7239   (let ((map (make-keymap)))
7240     (suppress-keymap map)
7241     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7242     (dolist (key '("\C-d" "\r" "\177" [delete]))
7243       (define-key map key 'gnus-summary-wake-up-the-dead))
7244     (dolist (key '("q" "Q"))
7245       (define-key map key 'bury-buffer))
7246     map))
7247
7248 (define-minor-mode gnus-dead-summary-mode
7249   "Minor mode for Gnus summary buffers."
7250   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7251   (unless (derived-mode-p 'gnus-summary-mode)
7252     (setq gnus-dead-summary-mode nil)))
7253
7254 (defun gnus-deaden-summary ()
7255   "Make the current summary buffer into a dead summary buffer."
7256   ;; Kill any previous dead summary buffer.
7257   (when (and gnus-dead-summary
7258              (buffer-name gnus-dead-summary))
7259     (with-current-buffer gnus-dead-summary
7260       (when gnus-dead-summary-mode
7261         (kill-buffer (current-buffer)))))
7262   ;; Make this the current dead summary.
7263   (setq gnus-dead-summary (current-buffer))
7264   (gnus-dead-summary-mode 1)
7265   (let ((name (buffer-name)))
7266     (when (string-match "Summary" name)
7267       (rename-buffer
7268        (concat (substring name 0 (match-beginning 0)) "Dead "
7269                (substring name (match-beginning 0)))
7270        t)
7271       (bury-buffer))))
7272
7273 (defun gnus-kill-or-deaden-summary (buffer)
7274   "Kill or deaden the summary BUFFER."
7275   (save-excursion
7276     (when (and (buffer-name buffer)
7277                (not gnus-single-article-buffer))
7278       (with-current-buffer buffer
7279         (gnus-kill-buffer gnus-article-buffer)
7280         (gnus-kill-buffer gnus-original-article-buffer)))
7281     (cond
7282      ;; Kill the buffer.
7283      (gnus-kill-summary-on-exit
7284       (when (and gnus-use-trees
7285                  (gnus-buffer-exists-p buffer))
7286         (with-current-buffer buffer
7287           (gnus-tree-close gnus-newsgroup-name)))
7288       (gnus-kill-buffer buffer))
7289      ;; Deaden the buffer.
7290      ((gnus-buffer-exists-p buffer)
7291       (with-current-buffer buffer
7292         (gnus-deaden-summary))))))
7293
7294 (defun gnus-summary-wake-up-the-dead (&rest args)
7295   "Wake up the dead summary buffer."
7296   (interactive)
7297   (gnus-dead-summary-mode -1)
7298   (let ((name (buffer-name)))
7299     (when (string-match "Dead " name)
7300       (rename-buffer
7301        (concat (substring name 0 (match-beginning 0))
7302                (substring name (match-end 0)))
7303        t)))
7304   (gnus-message 3 "This dead summary is now alive again"))
7305
7306 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7307 (defun gnus-summary-fetch-faq (&optional faq-dir)
7308   "Fetch the FAQ for the current group.
7309 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7310 in."
7311   (interactive
7312    (list
7313     (when current-prefix-arg
7314       (completing-read
7315        "FAQ dir: " (and (listp gnus-group-faq-directory)
7316                         (mapcar 'list
7317                                 gnus-group-faq-directory))))))
7318   (let (gnus-faq-buffer)
7319     (when (setq gnus-faq-buffer
7320                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7321       (gnus-configure-windows 'summary-faq))))
7322
7323 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7324 (defun gnus-summary-describe-group (&optional force)
7325   "Describe the current newsgroup."
7326   (interactive "P")
7327   (gnus-group-describe-group force gnus-newsgroup-name))
7328
7329 (defun gnus-summary-describe-briefly ()
7330   "Describe summary mode commands briefly."
7331   (interactive)
7332   (gnus-message 6 (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")))
7333
7334 ;; Walking around group mode buffer from summary mode.
7335
7336 (defun gnus-summary-next-group (&optional no-article target-group backward)
7337   "Exit current newsgroup and then select next unread newsgroup.
7338 If prefix argument NO-ARTICLE is non-nil, no article is selected
7339 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7340 previous group instead."
7341   (interactive "P")
7342   ;; Stop pre-fetching.
7343   (gnus-async-halt-prefetch)
7344   (let ((current-group gnus-newsgroup-name)
7345         (current-buffer (current-buffer))
7346         entered)
7347     ;; First we semi-exit this group to update Xrefs and all variables.
7348     ;; We can't do a real exit, because the window conf must remain
7349     ;; the same in case the user is prompted for info, and we don't
7350     ;; want the window conf to change before that...
7351     (gnus-summary-exit t)
7352     (while (not entered)
7353       ;; Then we find what group we are supposed to enter.
7354       (set-buffer gnus-group-buffer)
7355       (gnus-group-jump-to-group current-group)
7356       (setq target-group
7357             (or target-group
7358                 (if (eq gnus-keep-same-level 'best)
7359                     (gnus-summary-best-group gnus-newsgroup-name)
7360                   (gnus-summary-search-group backward gnus-keep-same-level))))
7361       (if (not target-group)
7362           ;; There are no further groups, so we return to the group
7363           ;; buffer.
7364           (progn
7365             (gnus-message 5 "Returning to the group buffer")
7366             (setq entered t)
7367             (when (gnus-buffer-live-p current-buffer)
7368               (set-buffer current-buffer)
7369               (gnus-summary-exit))
7370             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7371         ;; We try to enter the target group.
7372         (gnus-group-jump-to-group target-group)
7373         (let ((unreads (gnus-group-group-unread)))
7374           (if (and (or (eq t unreads)
7375                        (and unreads (not (zerop unreads))))
7376                    (gnus-summary-read-group
7377                     target-group nil no-article
7378                     (and (buffer-name current-buffer) current-buffer)
7379                     nil backward))
7380               (setq entered t)
7381             (setq current-group target-group
7382                   target-group nil)))))))
7383
7384 (defun gnus-summary-prev-group (&optional no-article)
7385   "Exit current newsgroup and then select previous unread newsgroup.
7386 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7387   (interactive "P")
7388   (gnus-summary-next-group no-article nil t))
7389
7390 ;; Walking around summary lines.
7391
7392 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7393   "Go to the first subject satisfying any non-nil constraint.
7394 If UNREAD is non-nil, the article should be unread.
7395 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7396 If UNSEEN is non-nil, the article should be unseen.
7397 Returns the article selected or nil if there are no matching articles."
7398   (interactive "P")
7399   (cond
7400    ;; Empty summary.
7401    ((null gnus-newsgroup-data)
7402     (gnus-message 3 "No articles in the group")
7403     nil)
7404    ;; Pick the first article.
7405    ((not (or unread undownloaded unseen))
7406     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7407     (gnus-data-number (car gnus-newsgroup-data)))
7408    ;; Find the first unread article.
7409    (t
7410     (let ((data gnus-newsgroup-data))
7411       (while (and data
7412                   (let ((num (gnus-data-number (car data))))
7413                     (or (memq num gnus-newsgroup-unfetched)
7414                         (not (or (and unread
7415                                       (memq num gnus-newsgroup-unreads))
7416                                  (and undownloaded
7417                                       (memq num gnus-newsgroup-undownloaded))
7418                                  (and unseen
7419                                       (memq num gnus-newsgroup-unseen)))))))
7420         (setq data (cdr data)))
7421       (prog1
7422           (if data
7423               (progn
7424                 (goto-char (gnus-data-pos (car data)))
7425                 (gnus-data-number (car data)))
7426             (gnus-message 3 "No more%s articles"
7427                           (let* ((r (when unread " unread"))
7428                                  (d (when undownloaded " undownloaded"))
7429                                  (s (when unseen " unseen"))
7430                                  (l (delq nil (list r d s))))
7431                             (cond ((= 3 (length l))
7432                                    (concat r "," d ", or" s))
7433                                   ((= 2 (length l))
7434                                    (concat (car l) ", or" (cadr l)))
7435                                   ((= 1 (length l))
7436                                    (car l))
7437                                   (t
7438                                    ""))))
7439             nil
7440             )
7441         (gnus-summary-position-point))))))
7442
7443 (defun gnus-summary-next-subject (n &optional unread dont-display)
7444   "Go to next N'th summary line.
7445 If N is negative, go to the previous N'th subject line.
7446 If UNREAD is non-nil, only unread articles are selected.
7447 The difference between N and the actual number of steps taken is
7448 returned."
7449   (interactive "p")
7450   (let ((backward (< n 0))
7451         (n (abs n)))
7452     (while (and (> n 0)
7453                 (if backward
7454                     (gnus-summary-find-prev unread)
7455                   (gnus-summary-find-next unread)))
7456       (unless (zerop (setq n (1- n)))
7457         (gnus-summary-show-thread)))
7458     (when (/= 0 n)
7459       (gnus-message 7 "No more%s articles"
7460                     (if unread " unread" "")))
7461     (unless dont-display
7462       (gnus-summary-recenter)
7463       (gnus-summary-position-point))
7464     n))
7465
7466 (defun gnus-summary-next-unread-subject (n)
7467   "Go to next N'th unread summary line."
7468   (interactive "p")
7469   (gnus-summary-next-subject n t))
7470
7471 (defun gnus-summary-prev-subject (n &optional unread)
7472   "Go to previous N'th summary line.
7473 If optional argument UNREAD is non-nil, only unread article is selected."
7474   (interactive "p")
7475   (gnus-summary-next-subject (- n) unread))
7476
7477 (defun gnus-summary-prev-unread-subject (n)
7478   "Go to previous N'th unread summary line."
7479   (interactive "p")
7480   (gnus-summary-next-subject (- n) t))
7481
7482 (defun gnus-summary-goto-subjects (articles)
7483   "Insert the subject header for ARTICLES in the current buffer."
7484   (save-excursion
7485     (dolist (article articles)
7486       (gnus-summary-goto-subject article t)))
7487   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7488   (gnus-summary-position-point))
7489
7490 (defun gnus-summary-goto-subject (article &optional force silent)
7491   "Go to the subject line of ARTICLE.
7492 If FORCE, also allow jumping to articles not currently shown."
7493   (interactive "nArticle number: ")
7494   (unless (numberp article)
7495     (error "Article %s is not a number" article))
7496   (let ((b (point))
7497         (data (gnus-data-find article)))
7498     ;; We read in the article if we have to.
7499     (and (not data)
7500          force
7501          (gnus-summary-insert-subject
7502           article
7503           (if (or (numberp force) (vectorp force)) force)
7504           t)
7505          (setq data (gnus-data-find article)))
7506     (goto-char b)
7507     (if (not data)
7508         (progn
7509           (unless silent
7510             (gnus-message 3 "Can't find article %d" article))
7511           nil)
7512       (let ((pt (gnus-data-pos data)))
7513         (goto-char pt)
7514         (gnus-summary-set-article-display-arrow pt))
7515       (gnus-summary-position-point)
7516       article)))
7517
7518 ;; Walking around summary lines with displaying articles.
7519
7520 (defun gnus-summary-expand-window (&optional arg)
7521   "Make the summary buffer take up the entire Emacs frame.
7522 Given a prefix, will force an `article' buffer configuration."
7523   (interactive "P")
7524   (if arg
7525       (gnus-configure-windows 'article 'force)
7526     (gnus-configure-windows 'summary 'force)))
7527
7528 (defun gnus-summary-display-article (article &optional all-header)
7529   "Display ARTICLE in article buffer."
7530   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7531                (with-current-buffer gnus-article-buffer
7532                  (eq major-mode 'gnus-article-mode)))
7533     (gnus-article-setup-buffer))
7534   (gnus-set-global-variables)
7535   (with-current-buffer gnus-article-buffer
7536     (setq gnus-article-charset gnus-newsgroup-charset)
7537     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7538     (mm-enable-multibyte))
7539   (if (null article)
7540       nil
7541     (prog1
7542         (if gnus-summary-display-article-function
7543             (funcall gnus-summary-display-article-function article all-header)
7544           (gnus-article-prepare article all-header))
7545       (gnus-run-hooks 'gnus-select-article-hook)
7546       (when (and gnus-current-article
7547                  (not (zerop gnus-current-article)))
7548         (gnus-summary-goto-subject gnus-current-article))
7549       (gnus-summary-recenter)
7550       (when (and gnus-use-trees gnus-show-threads)
7551         (gnus-possibly-generate-tree article)
7552         (gnus-highlight-selected-tree article))
7553       ;; Successfully display article.
7554       (gnus-article-set-window-start
7555        (cdr (assq article gnus-newsgroup-bookmarks))))))
7556
7557 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7558   "Select the current article.
7559 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7560 non-nil, the article will be re-fetched even if it already present in
7561 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7562 be displayed."
7563   ;; Make sure we are in the summary buffer to work around bbdb bug.
7564   (unless (eq major-mode 'gnus-summary-mode)
7565     (set-buffer gnus-summary-buffer))
7566   (let ((article (or article (gnus-summary-article-number)))
7567         (all-headers (not (not all-headers))) ;Must be t or nil.
7568         gnus-summary-display-article-function)
7569     (and (not pseudo)
7570          (gnus-summary-article-pseudo-p article)
7571          (error "This is a pseudo-article"))
7572     (with-current-buffer gnus-summary-buffer
7573       (if (or (and gnus-single-article-buffer
7574                    (or (null gnus-current-article)
7575                        (null gnus-article-current)
7576                        (null (get-buffer gnus-article-buffer))
7577                        (not (eq article (cdr gnus-article-current)))
7578                        (not (equal (car gnus-article-current)
7579                                    gnus-newsgroup-name))))
7580               (and (not gnus-single-article-buffer)
7581                    (or (null gnus-current-article)
7582                        (not (eq gnus-current-article article))))
7583               force)
7584           ;; The requested article is different from the current article.
7585           (progn
7586             (gnus-summary-display-article article all-headers)
7587             (when (gnus-buffer-live-p gnus-article-buffer)
7588               (with-current-buffer gnus-article-buffer
7589                 (if (not gnus-article-decoded-p) ;; a local variable
7590                     (mm-disable-multibyte))))
7591             (gnus-article-set-window-start
7592              (cdr (assq article gnus-newsgroup-bookmarks)))
7593             article)
7594         'old))))
7595
7596 (defun gnus-summary-force-verify-and-decrypt ()
7597   "Display buttons for signed/encrypted parts and verify/decrypt them."
7598   (interactive)
7599   (let ((mm-verify-option 'known)
7600         (mm-decrypt-option 'known)
7601         (gnus-article-emulate-mime t)
7602         (gnus-buttonized-mime-types (append (list "multipart/signed"
7603                                                   "multipart/encrypted")
7604                                             gnus-buttonized-mime-types)))
7605     (gnus-summary-select-article nil 'force)))
7606
7607 (defun gnus-summary-set-current-mark (&optional current-mark)
7608   "Obsolete function."
7609   nil)
7610
7611 (defun gnus-summary-next-article (&optional unread subject backward push)
7612   "Select the next article.
7613 If UNREAD, only unread articles are selected.
7614 If SUBJECT, only articles with SUBJECT are selected.
7615 If BACKWARD, the previous article is selected instead of the next."
7616   (interactive "P")
7617   ;; Make sure we are in the summary buffer.
7618   (unless (eq major-mode 'gnus-summary-mode)
7619     (set-buffer gnus-summary-buffer))
7620   (cond
7621    ;; Is there such an article?
7622    ((and (gnus-summary-search-forward unread subject backward)
7623          (or (gnus-summary-display-article (gnus-summary-article-number))
7624              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7625     (gnus-summary-position-point))
7626    ;; If not, we try the first unread, if that is wanted.
7627    ((and subject
7628          gnus-auto-select-same
7629          (gnus-summary-first-unread-article))
7630     (gnus-summary-position-point)
7631     (gnus-message 6 "Wrapped"))
7632    ;; Try to get next/previous article not displayed in this group.
7633    ((and gnus-auto-extend-newsgroup
7634          (not unread) (not subject))
7635     (gnus-summary-goto-article
7636      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7637      nil (count-lines (point-min) (point))))
7638    ;; Go to next/previous group.
7639    (t
7640     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7641       (gnus-summary-jump-to-group gnus-newsgroup-name))
7642     (let ((cmd (if (featurep 'xemacs)
7643                    last-command-char
7644                  last-command-event))
7645           (point
7646            (with-current-buffer gnus-group-buffer
7647              (point)))
7648           (group
7649            (if (eq gnus-keep-same-level 'best)
7650                (gnus-summary-best-group gnus-newsgroup-name)
7651              (gnus-summary-search-group backward gnus-keep-same-level))))
7652       ;; For some reason, the group window gets selected.  We change
7653       ;; it back.
7654       (select-window (get-buffer-window (current-buffer)))
7655       ;; Select next unread newsgroup automagically.
7656       (cond
7657        ((or (not gnus-auto-select-next)
7658             (not cmd))
7659         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7660        ((or (eq gnus-auto-select-next 'quietly)
7661             (and (eq gnus-auto-select-next 'slightly-quietly)
7662                  push)
7663             (and (eq gnus-auto-select-next 'almost-quietly)
7664                  (gnus-summary-last-article-p)))
7665         ;; Select quietly.
7666         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7667             (gnus-summary-exit)
7668           (gnus-message 7 "No more%s articles (%s)..."
7669                         (if unread " unread" "")
7670                         (if group (concat "selecting " group)
7671                           "exiting"))
7672           (gnus-summary-next-group nil group backward)))
7673        (t
7674         (when (gnus-key-press-event-p last-input-event)
7675           (gnus-summary-walk-group-buffer
7676            gnus-newsgroup-name cmd unread backward point))))))))
7677
7678 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7679   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7680                       (?\C-p (gnus-group-prev-unread-group 1))))
7681         (cursor-in-echo-area t)
7682         keve key group ended prompt)
7683     (with-current-buffer gnus-group-buffer
7684       (goto-char start)
7685       (setq group
7686             (if (eq gnus-keep-same-level 'best)
7687                 (gnus-summary-best-group gnus-newsgroup-name)
7688               (gnus-summary-search-group backward gnus-keep-same-level))))
7689     (while (not ended)
7690       (setq prompt
7691             (format
7692              "No more%s articles%s " (if unread " unread" "")
7693              (if (and group
7694                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7695                  (format " (Type %s for %s [%s])"
7696                          (single-key-description cmd)
7697                          (gnus-group-decoded-name group)
7698                          (gnus-group-unread group))
7699                (format " (Type %s to exit %s)"
7700                        (single-key-description cmd)
7701                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7702       ;; Confirm auto selection.
7703       (setq key (car (setq keve (gnus-read-event-char prompt)))
7704             ended t)
7705       (cond
7706        ((assq key keystrokes)
7707         (let ((obuf (current-buffer)))
7708           (switch-to-buffer gnus-group-buffer)
7709           (when group
7710             (gnus-group-jump-to-group group))
7711           (eval (cadr (assq key keystrokes)))
7712           (setq group (gnus-group-group-name))
7713           (switch-to-buffer obuf))
7714         (setq ended nil))
7715        ((equal key cmd)
7716         (if (or (not group)
7717                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7718             (gnus-summary-exit)
7719           (gnus-summary-next-group nil group backward)))
7720        (t
7721         (push (cdr keve) unread-command-events))))))
7722
7723 (defun gnus-summary-next-unread-article ()
7724   "Select unread article after current one."
7725   (interactive)
7726   (gnus-summary-next-article
7727    (or (not (eq gnus-summary-goto-unread 'never))
7728        (gnus-summary-last-article-p (gnus-summary-article-number)))
7729    (and gnus-auto-select-same
7730         (gnus-summary-article-subject))))
7731
7732 (defun gnus-summary-prev-article (&optional unread subject)
7733   "Select the article before the current one.
7734 If UNREAD is non-nil, only unread articles are selected."
7735   (interactive "P")
7736   (gnus-summary-next-article unread subject t))
7737
7738 (defun gnus-summary-prev-unread-article ()
7739   "Select unread article before current one."
7740   (interactive)
7741   (gnus-summary-prev-article
7742    (or (not (eq gnus-summary-goto-unread 'never))
7743        (gnus-summary-first-article-p (gnus-summary-article-number)))
7744    (and gnus-auto-select-same
7745         (gnus-summary-article-subject))))
7746
7747 (defun gnus-summary-next-page (&optional lines circular stop)
7748   "Show next page of the selected article.
7749 If at the end of the current article, select the next article.
7750 LINES says how many lines should be scrolled up.
7751
7752 If CIRCULAR is non-nil, go to the start of the article instead of
7753 selecting the next article when reaching the end of the current
7754 article.
7755
7756 If STOP is non-nil, just stop when reaching the end of the message.
7757
7758 Also see the variable `gnus-article-skip-boring'."
7759   (interactive "P")
7760   (setq gnus-summary-buffer (current-buffer))
7761   (gnus-set-global-variables)
7762   (let ((article (gnus-summary-article-number))
7763         (article-window (get-buffer-window gnus-article-buffer t))
7764         endp)
7765     ;; If the buffer is empty, we have no article.
7766     (unless article
7767       (error "No article to select"))
7768     (gnus-configure-windows 'article)
7769     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7770         (if (and (eq gnus-summary-goto-unread 'never)
7771                  (not (gnus-summary-last-article-p article)))
7772             (gnus-summary-next-article)
7773           (gnus-summary-next-unread-article))
7774       (if (or (null gnus-current-article)
7775               (null gnus-article-current)
7776               (/= article (cdr gnus-article-current))
7777               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7778           ;; Selected subject is different from current article's.
7779           (gnus-summary-display-article article)
7780         (when article-window
7781           (gnus-eval-in-buffer-window gnus-article-buffer
7782             (setq endp (or (gnus-article-next-page lines)
7783                            (gnus-article-only-boring-p))))
7784           (when endp
7785             (cond ((or stop gnus-summary-stop-at-end-of-message)
7786                    (gnus-message 3 "End of message"))
7787                   (circular
7788                    (gnus-summary-beginning-of-article))
7789                   (lines
7790                    (gnus-message 3 "End of message"))
7791                   ((null lines)
7792                    (if (and (eq gnus-summary-goto-unread 'never)
7793                             (not (gnus-summary-last-article-p article)))
7794                        (gnus-summary-next-article)
7795                      (gnus-summary-next-unread-article))))))))
7796     (gnus-summary-recenter)
7797     (gnus-summary-position-point)))
7798
7799 (defun gnus-summary-prev-page (&optional lines move)
7800   "Show previous page of selected article.
7801 Argument LINES specifies lines to be scrolled down.
7802 If MOVE, move to the previous unread article if point is at
7803 the beginning of the buffer."
7804   (interactive "P")
7805   (let ((article (gnus-summary-article-number))
7806         (article-window (get-buffer-window gnus-article-buffer t))
7807         endp)
7808     (gnus-configure-windows 'article)
7809     (if (or (null gnus-current-article)
7810             (null gnus-article-current)
7811             (/= article (cdr gnus-article-current))
7812             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7813         ;; Selected subject is different from current article's.
7814         (gnus-summary-display-article article)
7815       (gnus-summary-recenter)
7816       (when article-window
7817         (gnus-eval-in-buffer-window gnus-article-buffer
7818           (setq endp (gnus-article-prev-page lines)))
7819         (when (and move endp)
7820           (cond (lines
7821                  (gnus-message 3 "Beginning of message"))
7822                 ((null lines)
7823                  (if (and (eq gnus-summary-goto-unread 'never)
7824                           (not (gnus-summary-first-article-p article)))
7825                      (gnus-summary-prev-article)
7826                    (gnus-summary-prev-unread-article))))))))
7827   (gnus-summary-position-point))
7828
7829 (defun gnus-summary-prev-page-or-article (&optional lines)
7830   "Show previous page of selected article.
7831 Argument LINES specifies lines to be scrolled down.
7832 If at the beginning of the article, go to the next article."
7833   (interactive "P")
7834   (gnus-summary-prev-page lines t))
7835
7836 (defun gnus-summary-scroll-up (lines)
7837   "Scroll up (or down) one line current article.
7838 Argument LINES specifies lines to be scrolled up (or down if negative)."
7839   (interactive "p")
7840   (gnus-configure-windows 'article)
7841   (gnus-summary-show-thread)
7842   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7843     (gnus-eval-in-buffer-window gnus-article-buffer
7844       (cond ((> lines 0)
7845              (when (gnus-article-next-page lines)
7846                (gnus-message 3 "End of message")))
7847             ((< lines 0)
7848              (gnus-article-prev-page (- lines))))))
7849   (gnus-summary-recenter)
7850   (gnus-summary-position-point))
7851
7852 (defun gnus-summary-scroll-down (lines)
7853   "Scroll down (or up) one line current article.
7854 Argument LINES specifies lines to be scrolled down (or up if negative)."
7855   (interactive "p")
7856   (gnus-summary-scroll-up (- lines)))
7857
7858 (defun gnus-summary-next-same-subject ()
7859   "Select next article which has the same subject as current one."
7860   (interactive)
7861   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7862
7863 (defun gnus-summary-prev-same-subject ()
7864   "Select previous article which has the same subject as current one."
7865   (interactive)
7866   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7867
7868 (defun gnus-summary-next-unread-same-subject ()
7869   "Select next unread article which has the same subject as current one."
7870   (interactive)
7871   (gnus-summary-next-article t (gnus-summary-article-subject)))
7872
7873 (defun gnus-summary-prev-unread-same-subject ()
7874   "Select previous unread article which has the same subject as current one."
7875   (interactive)
7876   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7877
7878 (defun gnus-summary-first-unread-article ()
7879   "Select the first unread article.
7880 Return nil if there are no unread articles."
7881   (interactive)
7882   (prog1
7883       (when (gnus-summary-first-subject t)
7884         (gnus-summary-show-thread)
7885         (gnus-summary-first-subject t)
7886         (gnus-summary-display-article (gnus-summary-article-number)))
7887     (gnus-summary-position-point)))
7888
7889 (defun gnus-summary-first-unread-subject ()
7890   "Place the point on the subject line of the first unread article.
7891 Return nil if there are no unread articles."
7892   (interactive)
7893   (prog1
7894       (when (gnus-summary-first-subject t)
7895         (gnus-summary-show-thread)
7896         (gnus-summary-first-subject t))
7897     (gnus-summary-position-point)))
7898
7899 (defun gnus-summary-first-unseen-subject ()
7900   "Place the point on the subject line of the first unseen article.
7901 Return nil if there are no unseen articles."
7902   (interactive)
7903   (prog1
7904       (when (gnus-summary-first-subject nil nil t)
7905         (gnus-summary-show-thread)
7906         (gnus-summary-first-subject nil nil t))
7907     (gnus-summary-position-point)))
7908
7909 (defun gnus-summary-first-unseen-or-unread-subject ()
7910   "Place the point on the subject line of the first unseen article or,
7911 if all article have been seen, on the subject line of the first unread
7912 article."
7913   (interactive)
7914   (prog1
7915       (unless (when (gnus-summary-first-subject nil nil t)
7916                 (gnus-summary-show-thread)
7917                 (gnus-summary-first-subject nil nil t))
7918         (when (gnus-summary-first-subject t)
7919           (gnus-summary-show-thread)
7920           (gnus-summary-first-subject t)))
7921     (gnus-summary-position-point)))
7922
7923 (defun gnus-summary-first-article ()
7924   "Select the first article.
7925 Return nil if there are no articles."
7926   (interactive)
7927   (prog1
7928       (when (gnus-summary-first-subject)
7929         (gnus-summary-show-thread)
7930         (gnus-summary-first-subject)
7931         (gnus-summary-display-article (gnus-summary-article-number)))
7932     (gnus-summary-position-point)))
7933
7934 (defun gnus-summary-best-unread-article (&optional arg)
7935   "Select the unread article with the highest score.
7936 If given a prefix argument, select the next unread article that has a
7937 score higher than the default score."
7938   (interactive "P")
7939   (let ((article (if arg
7940                      (gnus-summary-better-unread-subject)
7941                    (gnus-summary-best-unread-subject))))
7942     (if article
7943         (gnus-summary-goto-article article)
7944       (error "No unread articles"))))
7945
7946 (defun gnus-summary-best-unread-subject ()
7947   "Select the unread subject with the highest score."
7948   (interactive)
7949   (let ((best -1000000)
7950         (data gnus-newsgroup-data)
7951         article score)
7952     (while data
7953       (and (gnus-data-unread-p (car data))
7954            (> (setq score
7955                     (gnus-summary-article-score (gnus-data-number (car data))))
7956               best)
7957            (setq best score
7958                  article (gnus-data-number (car data))))
7959       (setq data (cdr data)))
7960     (when article
7961       (gnus-summary-goto-subject article))
7962     (gnus-summary-position-point)
7963     article))
7964
7965 (defun gnus-summary-better-unread-subject ()
7966   "Select the first unread subject that has a score over the default score."
7967   (interactive)
7968   (let ((data gnus-newsgroup-data)
7969         article score)
7970     (while (and (setq article (gnus-data-number (car data)))
7971                 (or (gnus-data-read-p (car data))
7972                     (not (> (gnus-summary-article-score article)
7973                             gnus-summary-default-score))))
7974       (setq data (cdr data)))
7975     (when article
7976       (gnus-summary-goto-subject article))
7977     (gnus-summary-position-point)
7978     article))
7979
7980 (defun gnus-summary-last-subject ()
7981   "Go to the last displayed subject line in the group."
7982   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7983     (when article
7984       (gnus-summary-goto-subject article))))
7985
7986 (defun gnus-summary-goto-article (article &optional all-headers force)
7987   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7988 If ALL-HEADERS is non-nil, no header lines are hidden.
7989 If FORCE, go to the article even if it isn't displayed.  If FORCE
7990 is a number, it is the line the article is to be displayed on."
7991   (interactive
7992    (list
7993     (completing-read
7994      "Article number or Message-ID: "
7995      (mapcar (lambda (number) (list (int-to-string number)))
7996              gnus-newsgroup-limit))
7997     current-prefix-arg
7998     t))
7999   (prog1
8000       (if (and (stringp article)
8001                (string-match "@\\|%40" article))
8002           (gnus-summary-refer-article article)
8003         (when (stringp article)
8004           (setq article (string-to-number article)))
8005         (if (gnus-summary-goto-subject article force)
8006             (gnus-summary-display-article article all-headers)
8007           (gnus-message 4 "Couldn't go to article %s" article) nil))
8008     (gnus-summary-position-point)))
8009
8010 (defun gnus-summary-goto-last-article ()
8011   "Go to the previously read article."
8012   (interactive)
8013   (prog1
8014       (when gnus-last-article
8015         (gnus-summary-goto-article gnus-last-article nil t))
8016     (gnus-summary-position-point)))
8017
8018 (defun gnus-summary-pop-article (number)
8019   "Pop one article off the history and go to the previous.
8020 NUMBER articles will be popped off."
8021   (interactive "p")
8022   (let (to)
8023     (setq gnus-newsgroup-history
8024           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8025     (if to
8026         (gnus-summary-goto-article (car to) nil t)
8027       (error "Article history empty")))
8028   (gnus-summary-position-point))
8029
8030 ;; Summary commands and functions for limiting the summary buffer.
8031
8032 (defun gnus-summary-limit-to-articles (n)
8033   "Limit the summary buffer to the next N articles.
8034 If not given a prefix, use the process marked articles instead."
8035   (interactive "P")
8036   (prog1
8037       (let ((articles (gnus-summary-work-articles n)))
8038         (setq gnus-newsgroup-processable nil)
8039         (gnus-summary-limit articles))
8040     (gnus-summary-position-point)))
8041
8042 (defun gnus-summary-pop-limit (&optional total)
8043   "Restore the previous limit.
8044 If given a prefix, remove all limits."
8045   (interactive "P")
8046   (when total
8047     (setq gnus-newsgroup-limits
8048           (list (mapcar (lambda (h) (mail-header-number h))
8049                         gnus-newsgroup-headers))))
8050   (unless gnus-newsgroup-limits
8051     (error "No limit to pop"))
8052   (prog1
8053       (gnus-summary-limit nil 'pop)
8054     (gnus-summary-position-point)))
8055
8056 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8057   "Limit the summary buffer to articles that have subjects that match a regexp.
8058 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8059   (interactive
8060    (list (read-string (if current-prefix-arg
8061                           "Exclude subject (regexp): "
8062                         "Limit to subject (regexp): "))
8063          nil current-prefix-arg))
8064   (unless header
8065     (setq header "subject"))
8066   (when (not (equal "" subject))
8067     (prog1
8068         (let ((articles (gnus-summary-find-matching
8069                          (or header "subject") subject 'all nil nil
8070                          not-matching)))
8071           (unless articles
8072             (error "Found no matches for \"%s\"" subject))
8073           (gnus-summary-limit articles))
8074       (gnus-summary-position-point))))
8075
8076 (defun gnus-summary-limit-to-author (from &optional not-matching)
8077   "Limit the summary buffer to articles that have authors that match a regexp.
8078 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8079   (interactive
8080    (list (read-string (if current-prefix-arg
8081                           "Exclude author (regexp): "
8082                         "Limit to author (regexp): "))
8083          current-prefix-arg))
8084   (gnus-summary-limit-to-subject from "from" not-matching))
8085
8086 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8087   "Limit the summary buffer to articles with the given RECIPIENT.
8088
8089 If NOT-MATCHING, exclude RECIPIENT.
8090
8091 To and Cc headers are checked.  You need to include them in
8092 `nnmail-extra-headers'."
8093   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8094   (interactive
8095    (list (read-string (format "%s recipient (regexp): "
8096                               (if current-prefix-arg "Exclude" "Limit to")))
8097          current-prefix-arg))
8098   (when (not (equal "" recipient))
8099     (prog1 (let* ((to
8100                    (if (memq 'To nnmail-extra-headers)
8101                        (gnus-summary-find-matching
8102                         (cons 'extra 'To) recipient 'all nil nil
8103                         not-matching)
8104                      (gnus-message
8105                       1 "`To' isn't present in `nnmail-extra-headers'")
8106                      (sit-for 1)
8107                      nil))
8108                   (cc
8109                    (if (memq 'Cc nnmail-extra-headers)
8110                        (gnus-summary-find-matching
8111                         (cons 'extra 'Cc) recipient 'all nil nil
8112                         not-matching)
8113                      (gnus-message
8114                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8115                      (sit-for 1)
8116                      nil))
8117                   (articles
8118                    (if not-matching
8119                        ;; We need the numbers that are in both lists:
8120                        (mapcar (lambda (a)
8121                                  (and (memq a to) a))
8122                                cc)
8123                      (nconc to cc))))
8124              (unless articles
8125                (error "Found no matches for \"%s\"" recipient))
8126              (gnus-summary-limit articles))
8127       (gnus-summary-position-point))))
8128
8129 (defun gnus-summary-limit-to-address (address &optional not-matching)
8130   "Limit the summary buffer to articles with the given ADDRESS.
8131
8132 If NOT-MATCHING, exclude ADDRESS.
8133
8134 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8135 in `nnmail-extra-headers'."
8136   (interactive
8137    (list (read-string (format "%s address (regexp): "
8138                               (if current-prefix-arg "Exclude" "Limit to")))
8139          current-prefix-arg))
8140   (when (not (equal "" address))
8141     (prog1 (let* ((to
8142                    (if (memq 'To nnmail-extra-headers)
8143                        (gnus-summary-find-matching
8144                         (cons 'extra 'To) address 'all nil nil
8145                         not-matching)
8146                      (gnus-message
8147                       1 "`To' isn't present in `nnmail-extra-headers'")
8148                      (sit-for 1)
8149                      t))
8150                   (cc
8151                    (if (memq 'Cc nnmail-extra-headers)
8152                        (gnus-summary-find-matching
8153                         (cons 'extra 'Cc) address 'all nil nil
8154                         not-matching)
8155                      (gnus-message
8156                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8157                      (sit-for 1)
8158                      t))
8159                   (from
8160                    (gnus-summary-find-matching "from" address
8161                                                'all nil nil not-matching))
8162                   (articles
8163                    (if not-matching
8164                        ;; We need the numbers that are in all lists:
8165                        (if (eq cc t)
8166                            (if (eq to t)
8167                                from
8168                              (mapcar (lambda (a) (car (memq a from))) to))
8169                          (if (eq to t)
8170                              (mapcar (lambda (a) (car (memq a from))) cc)
8171                            (mapcar (lambda (a) (car (memq a from)))
8172                                    (mapcar (lambda (a) (car (memq a to)))
8173                                            cc))))
8174                      (nconc (if (eq to t) nil to)
8175                             (if (eq cc t) nil cc)
8176                             from))))
8177              (unless articles
8178                (error "Found no matches for \"%s\"" address))
8179              (gnus-summary-limit articles))
8180       (gnus-summary-position-point))))
8181
8182 (defun gnus-summary-limit-strange-charsets-predicate (header)
8183   (when (fboundp 'char-charset)
8184     (let ((string (concat (mail-header-subject header)
8185                           (mail-header-from header)))
8186           charset found)
8187       (dotimes (i (1- (length string)))
8188         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8189         (when (string-match "unicode\\|big\\|japanese" charset)
8190           (setq found t)))
8191       found)))
8192
8193 (defun gnus-summary-limit-to-predicate (predicate)
8194   "Limit to articles where PREDICATE returns non-nil.
8195 PREDICATE will be called with the header structures of the
8196 articles."
8197   (let ((articles nil)
8198         (case-fold-search t))
8199     (dolist (header gnus-newsgroup-headers)
8200       (when (funcall predicate header)
8201         (push (mail-header-number header) articles)))
8202     (gnus-summary-limit (nreverse articles))))
8203
8204 (defun gnus-summary-limit-to-age (age &optional younger-p)
8205   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8206 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8207 articles that are younger than AGE days."
8208   (interactive
8209    (let ((younger current-prefix-arg)
8210          (days-got nil)
8211          days)
8212      (while (not days-got)
8213        (setq days (if younger
8214                       (read-string "Limit to articles younger than (in days, older when negative): ")
8215                     (read-string
8216                      "Limit to articles older than (in days, younger when negative): ")))
8217        (when (> (length days) 0)
8218          (setq days (read days)))
8219        (if (numberp days)
8220            (progn
8221              (setq days-got t)
8222              (when (< days 0)
8223                (setq younger (not younger))
8224                (setq days (* days -1))))
8225          (message "Please enter a number.")
8226          (sleep-for 1)))
8227      (list days younger)))
8228   (prog1
8229       (let ((data gnus-newsgroup-data)
8230             (cutoff (days-to-time age))
8231             articles d date is-younger)
8232         (while (setq d (pop data))
8233           (when (and (vectorp (gnus-data-header d))
8234                      (setq date (mail-header-date (gnus-data-header d))))
8235             (setq is-younger (time-less-p
8236                               (time-since (gnus-date-get-time date))
8237                               cutoff))
8238             (when (if younger-p
8239                       is-younger
8240                     (not is-younger))
8241               (push (gnus-data-number d) articles))))
8242         (gnus-summary-limit (nreverse articles)))
8243     (gnus-summary-position-point)))
8244
8245 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8246   "Limit the summary buffer to articles that match an 'extra' header."
8247   (interactive
8248    (let ((header
8249           (intern
8250            (gnus-completing-read-with-default
8251             (symbol-name (car gnus-extra-headers))
8252             (if current-prefix-arg
8253                 "Exclude extra header"
8254               "Limit extra header")
8255             (mapcar (lambda (x)
8256                       (cons (symbol-name x) x))
8257                     gnus-extra-headers)
8258             nil
8259             t))))
8260      (list header
8261            (read-string (format "%s header %s (regexp): "
8262                                 (if current-prefix-arg "Exclude" "Limit to")
8263                                 header))
8264            current-prefix-arg)))
8265   (when (not (equal "" regexp))
8266     (prog1
8267         (let ((articles (gnus-summary-find-matching
8268                          (cons 'extra header) regexp 'all nil nil
8269                          not-matching)))
8270           (unless articles
8271             (error "Found no matches for \"%s\"" regexp))
8272           (gnus-summary-limit articles))
8273       (gnus-summary-position-point))))
8274
8275 (defun gnus-summary-limit-to-display-predicate ()
8276   "Limit the summary buffer to the predicated in the `display' group parameter."
8277   (interactive)
8278   (unless gnus-newsgroup-display
8279     (error "There is no `display' group parameter"))
8280   (let (articles)
8281     (dolist (number gnus-newsgroup-articles)
8282       (when (funcall gnus-newsgroup-display)
8283         (push number articles)))
8284     (gnus-summary-limit articles))
8285   (gnus-summary-position-point))
8286
8287 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8288 (make-obsolete
8289  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8290
8291 (defun gnus-summary-limit-to-unread (&optional all)
8292   "Limit the summary buffer to articles that are not marked as read.
8293 If ALL is non-nil, limit strictly to unread articles."
8294   (interactive "P")
8295   (if all
8296       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8297     (gnus-summary-limit-to-marks
8298      ;; Concat all the marks that say that an article is read and have
8299      ;; those removed.
8300      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8301            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8302            gnus-low-score-mark gnus-expirable-mark
8303            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8304            gnus-duplicate-mark)
8305      'reverse)))
8306
8307 (defun gnus-summary-limit-to-headers (match &optional reverse)
8308   "Limit the summary buffer to articles that have headers that match MATCH.
8309 If REVERSE (the prefix), limit to articles that don't match."
8310   (interactive "sMatch headers (regexp): \nP")
8311   (gnus-summary-limit-to-bodies match reverse t))
8312
8313 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8314   "Limit the summary buffer to articles that have bodies that match MATCH.
8315 If REVERSE (the prefix), limit to articles that don't match."
8316   (interactive "sMatch body (regexp): \nP")
8317   (let ((articles nil)
8318         (gnus-select-article-hook nil)  ;Disable hook.
8319         (gnus-article-prepare-hook nil)
8320         (gnus-use-article-prefetch nil)
8321         (gnus-keep-backlog nil)
8322         (gnus-break-pages nil)
8323         (gnus-summary-display-arrow nil)
8324         (gnus-updated-mode-lines nil)
8325         (gnus-auto-center-summary nil)
8326         (gnus-display-mime-function nil))
8327     (dolist (data gnus-newsgroup-data)
8328       (let (gnus-mark-article-hook)
8329         (gnus-summary-select-article t t nil (gnus-data-number data)))
8330       (with-current-buffer gnus-article-buffer
8331         (article-goto-body)
8332         (let* ((case-fold-search t)
8333                (found (if headersp
8334                           (re-search-backward match nil t)
8335                         (re-search-forward match nil t))))
8336           (when (or (and found
8337                          (not reverse))
8338                     (and (not found)
8339                          reverse))
8340             (push (gnus-data-number data) articles)))))
8341     (if (not articles)
8342         (message "No messages matched")
8343       (gnus-summary-limit articles)))
8344   (gnus-summary-position-point))
8345
8346 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8347   "Limit the summary buffer to articles that aren't part on any thread.
8348 If THREADSP (the prefix), limit to articles that are in threads."
8349   (interactive "P")
8350   (let ((articles nil)
8351         thread-articles
8352         threads)
8353     (dolist (thread gnus-newsgroup-threads)
8354       (if (stringp (car thread))
8355           (dolist (thread (cdr thread))
8356             (push thread threads))
8357         (push thread threads)))
8358     (dolist (thread threads)
8359       (setq thread-articles (gnus-articles-in-thread thread))
8360       (when (or (and threadsp
8361                      (> (length thread-articles) 1))
8362                 (and (not threadsp)
8363                      (= (length thread-articles) 1)))
8364         (setq articles (nconc thread-articles articles))))
8365     (if (not articles)
8366         (message "No messages matched")
8367       (gnus-summary-limit articles))
8368     (gnus-summary-position-point)))
8369
8370 (defun gnus-summary-limit-to-replied (&optional unreplied)
8371   "Limit the summary buffer to replied articles.
8372 If UNREPLIED (the prefix), limit to unreplied articles."
8373   (interactive "P")
8374   (if unreplied
8375       (gnus-summary-limit
8376        (gnus-set-difference gnus-newsgroup-articles
8377         gnus-newsgroup-replied))
8378     (gnus-summary-limit gnus-newsgroup-replied))
8379   (gnus-summary-position-point))
8380
8381 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8382 (make-obsolete 'gnus-summary-delete-marked-with
8383                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8384
8385 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8386   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8387 If REVERSE, limit the summary buffer to articles that are marked
8388 with MARKS.  MARKS can either be a string of marks or a list of marks.
8389 Returns how many articles were removed."
8390   (interactive "sMarks: ")
8391   (gnus-summary-limit-to-marks marks t))
8392
8393 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8394   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8395 If REVERSE (the prefix), limit the summary buffer to articles that are
8396 not marked with MARKS.  MARKS can either be a string of marks or a
8397 list of marks.
8398 Returns how many articles were removed."
8399   (interactive "sMarks: \nP")
8400   (prog1
8401       (let ((data gnus-newsgroup-data)
8402             (marks (if (listp marks) marks
8403                      (append marks nil))) ; Transform to list.
8404             articles)
8405         (while data
8406           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8407                   (memq (gnus-data-mark (car data)) marks))
8408             (push (gnus-data-number (car data)) articles))
8409           (setq data (cdr data)))
8410         (gnus-summary-limit articles))
8411     (gnus-summary-position-point)))
8412
8413 (defun gnus-summary-limit-to-score (score)
8414   "Limit to articles with score at or above SCORE."
8415   (interactive "NLimit to articles with score of at least: ")
8416   (let ((data gnus-newsgroup-data)
8417         articles)
8418     (while data
8419       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8420                 score)
8421         (push (gnus-data-number (car data)) articles))
8422       (setq data (cdr data)))
8423     (prog1
8424         (gnus-summary-limit articles)
8425       (gnus-summary-position-point))))
8426
8427 (defun gnus-summary-limit-to-unseen ()
8428   "Limit to unseen articles."
8429   (interactive)
8430   (prog1
8431       (gnus-summary-limit gnus-newsgroup-unseen)
8432     (gnus-summary-position-point)))
8433
8434 (defun gnus-summary-limit-include-thread (id)
8435   "Display all the hidden articles that is in the thread with ID in it.
8436 When called interactively, ID is the Message-ID of the current
8437 article."
8438   (interactive (list (mail-header-id (gnus-summary-article-header))))
8439   (let ((articles (gnus-articles-in-thread
8440                    (gnus-id-to-thread (gnus-root-id id)))))
8441     (prog1
8442         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8443       (gnus-summary-limit-include-matching-articles
8444        "subject"
8445        (regexp-quote (gnus-simplify-subject-re
8446                       (mail-header-subject (gnus-id-to-header id)))))
8447       (gnus-summary-position-point))))
8448
8449 (defun gnus-summary-limit-include-matching-articles (header regexp)
8450   "Display all the hidden articles that have HEADERs that match REGEXP."
8451   (interactive (list (read-string "Match on header: ")
8452                      (read-string "Regexp: ")))
8453   (let ((articles (gnus-find-matching-articles header regexp)))
8454     (prog1
8455         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8456       (gnus-summary-position-point))))
8457
8458 (defun gnus-summary-insert-dormant-articles ()
8459   "Insert all the dormant articles for this group into the current buffer."
8460   (interactive)
8461   (let ((gnus-verbose (max 6 gnus-verbose)))
8462     (if (not gnus-newsgroup-dormant)
8463         (gnus-message 3 "No dormant articles for this group")
8464       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8465
8466 (defun gnus-summary-insert-ticked-articles ()
8467   "Insert ticked articles for this group into the current buffer."
8468   (interactive)
8469   (let ((gnus-verbose (max 6 gnus-verbose)))
8470     (if (not gnus-newsgroup-marked)
8471         (gnus-message 3 "No ticked articles for this group")
8472       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8473
8474 (defun gnus-summary-limit-include-dormant ()
8475   "Display all the hidden articles that are marked as dormant.
8476 Note that this command only works on a subset of the articles currently
8477 fetched for this group."
8478   (interactive)
8479   (unless gnus-newsgroup-dormant
8480     (error "There are no dormant articles in this group"))
8481   (prog1
8482       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8483     (gnus-summary-position-point)))
8484
8485 (defun gnus-summary-limit-exclude-dormant ()
8486   "Hide all dormant articles."
8487   (interactive)
8488   (prog1
8489       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8490     (gnus-summary-position-point)))
8491
8492 (defun gnus-summary-limit-exclude-childless-dormant ()
8493   "Hide all dormant articles that have no children."
8494   (interactive)
8495   (let ((data (gnus-data-list t))
8496         articles d children)
8497     ;; Find all articles that are either not dormant or have
8498     ;; children.
8499     (while (setq d (pop data))
8500       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8501                 (and (setq children
8502                            (gnus-article-children (gnus-data-number d)))
8503                      (let (found)
8504                        (while children
8505                          (when (memq (car children) articles)
8506                            (setq children nil
8507                                  found t))
8508                          (pop children))
8509                        found)))
8510         (push (gnus-data-number d) articles)))
8511     ;; Do the limiting.
8512     (prog1
8513         (gnus-summary-limit articles)
8514       (gnus-summary-position-point))))
8515
8516 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8517   "Mark all unread excluded articles as read.
8518 If ALL, mark even excluded ticked and dormants as read."
8519   (interactive "P")
8520   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8521   (let ((articles (gnus-sorted-ndifference
8522                    (sort
8523                     (mapcar (lambda (h) (mail-header-number h))
8524                             gnus-newsgroup-headers)
8525                     '<)
8526                    gnus-newsgroup-limit))
8527         article)
8528     (setq gnus-newsgroup-unreads
8529           (gnus-sorted-intersection gnus-newsgroup-unreads
8530                                     gnus-newsgroup-limit))
8531     (if all
8532         (setq gnus-newsgroup-dormant nil
8533               gnus-newsgroup-marked nil
8534               gnus-newsgroup-reads
8535               (nconc
8536                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8537                gnus-newsgroup-reads))
8538       (while (setq article (pop articles))
8539         (unless (or (memq article gnus-newsgroup-dormant)
8540                     (memq article gnus-newsgroup-marked))
8541           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8542
8543 (defun gnus-summary-limit (articles &optional pop)
8544   (if pop
8545       ;; We pop the previous limit off the stack and use that.
8546       (setq articles (car gnus-newsgroup-limits)
8547             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8548     ;; We use the new limit, so we push the old limit on the stack.
8549     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8550   ;; Set the limit.
8551   (setq gnus-newsgroup-limit articles)
8552   (let ((total (length gnus-newsgroup-data))
8553         (data (gnus-data-find-list (gnus-summary-article-number)))
8554         (gnus-summary-mark-below nil)   ; Inhibit this.
8555         found)
8556     ;; This will do all the work of generating the new summary buffer
8557     ;; according to the new limit.
8558     (gnus-summary-prepare)
8559     ;; Hide any threads, possibly.
8560     (gnus-summary-maybe-hide-threads)
8561     ;; Try to return to the article you were at, or one in the
8562     ;; neighborhood.
8563     (when data
8564       ;; We try to find some article after the current one.
8565       (while data
8566         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8567           (setq data nil
8568                 found t))
8569         (setq data (cdr data))))
8570     (unless found
8571       ;; If there is no data, that means that we were after the last
8572       ;; article.  The same goes when we can't find any articles
8573       ;; after the current one.
8574       (goto-char (point-max))
8575       (gnus-summary-find-prev))
8576     (gnus-set-mode-line 'summary)
8577     ;; We return how many articles were removed from the summary
8578     ;; buffer as a result of the new limit.
8579     (- total (length gnus-newsgroup-data))))
8580
8581 (defsubst gnus-invisible-cut-children (threads)
8582   (let ((num 0))
8583     (while threads
8584       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8585         (incf num))
8586       (pop threads))
8587     (< num 2)))
8588
8589 (defsubst gnus-cut-thread (thread)
8590   "Go forwards in the thread until we find an article that we want to display."
8591   (when (or (eq gnus-fetch-old-headers 'some)
8592             (eq gnus-fetch-old-headers 'invisible)
8593             (numberp gnus-fetch-old-headers)
8594             (eq gnus-build-sparse-threads 'some)
8595             (eq gnus-build-sparse-threads 'more))
8596     ;; Deal with old-fetched headers and sparse threads.
8597     (while (and
8598             thread
8599             (or
8600              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8601              (gnus-summary-article-ancient-p
8602               (mail-header-number (car thread))))
8603             (if (or (<= (length (cdr thread)) 1)
8604                     (eq gnus-fetch-old-headers 'invisible))
8605                 (setq gnus-newsgroup-limit
8606                       (delq (mail-header-number (car thread))
8607                             gnus-newsgroup-limit)
8608                       thread (cadr thread))
8609               (when (gnus-invisible-cut-children (cdr thread))
8610                 (let ((th (cdr thread)))
8611                   (while th
8612                     (if (memq (mail-header-number (caar th))
8613                               gnus-newsgroup-limit)
8614                         (setq thread (car th)
8615                               th nil)
8616                       (setq th (cdr th))))))))))
8617   thread)
8618
8619 (defun gnus-cut-threads (threads)
8620   "Cut off all uninteresting articles from the beginning of THREADS."
8621   (when (or (eq gnus-fetch-old-headers 'some)
8622             (eq gnus-fetch-old-headers 'invisible)
8623             (numberp gnus-fetch-old-headers)
8624             (eq gnus-build-sparse-threads 'some)
8625             (eq gnus-build-sparse-threads 'more))
8626     (let ((th threads))
8627       (while th
8628         (setcar th (gnus-cut-thread (car th)))
8629         (setq th (cdr th)))))
8630   ;; Remove nixed out threads.
8631   (delq nil threads))
8632
8633 (defun gnus-summary-initial-limit (&optional show-if-empty)
8634   "Figure out what the initial limit is supposed to be on group entry.
8635 This entails weeding out unwanted dormants, low-scored articles,
8636 fetch-old-headers verbiage, and so on."
8637   ;; Most groups have nothing to remove.
8638   (unless (or gnus-inhibit-limiting
8639               (and (null gnus-newsgroup-dormant)
8640                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8641                    (not (eq gnus-fetch-old-headers 'some))
8642                    (not (numberp gnus-fetch-old-headers))
8643                    (not (eq gnus-fetch-old-headers 'invisible))
8644                    (null gnus-summary-expunge-below)
8645                    (not (eq gnus-build-sparse-threads 'some))
8646                    (not (eq gnus-build-sparse-threads 'more))
8647                    (null gnus-thread-expunge-below)
8648                    (not gnus-use-nocem)))
8649     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8650     (setq gnus-newsgroup-limit nil)
8651     (mapatoms
8652      (lambda (node)
8653        (unless (car (symbol-value node))
8654          ;; These threads have no parents -- they are roots.
8655          (let ((nodes (cdr (symbol-value node)))
8656                thread)
8657            (while nodes
8658              (if (and gnus-thread-expunge-below
8659                       (< (gnus-thread-total-score (car nodes))
8660                          gnus-thread-expunge-below))
8661                  (gnus-expunge-thread (pop nodes))
8662                (setq thread (pop nodes))
8663                (gnus-summary-limit-children thread))))))
8664      gnus-newsgroup-dependencies)
8665     ;; If this limitation resulted in an empty group, we might
8666     ;; pop the previous limit and use it instead.
8667     (when (and (not gnus-newsgroup-limit)
8668                show-if-empty)
8669       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8670     gnus-newsgroup-limit))
8671
8672 (defun gnus-summary-limit-children (thread)
8673   "Return 1 if this subthread is visible and 0 if it is not."
8674   ;; First we get the number of visible children to this thread.  This
8675   ;; is done by recursing down the thread using this function, so this
8676   ;; will really go down to a leaf article first, before slowly
8677   ;; working its way up towards the root.
8678   (when thread
8679     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8680            (children
8681            (if (cdr thread)
8682                (apply '+ (mapcar 'gnus-summary-limit-children
8683                                  (cdr thread)))
8684              0))
8685           (number (mail-header-number (car thread)))
8686           score)
8687       (if (and
8688            (not (memq number gnus-newsgroup-marked))
8689            (or
8690             ;; If this article is dormant and has absolutely no visible
8691             ;; children, then this article isn't visible.
8692             (and (memq number gnus-newsgroup-dormant)
8693                  (zerop children))
8694             ;; If this is "fetch-old-headered" and there is no
8695             ;; visible children, then we don't want this article.
8696             (and (or (eq gnus-fetch-old-headers 'some)
8697                      (numberp gnus-fetch-old-headers))
8698                  (gnus-summary-article-ancient-p number)
8699                  (zerop children))
8700             ;; If this is "fetch-old-headered" and `invisible', then
8701             ;; we don't want this article.
8702             (and (eq gnus-fetch-old-headers 'invisible)
8703                  (gnus-summary-article-ancient-p number))
8704             ;; If this is a sparsely inserted article with no children,
8705             ;; we don't want it.
8706             (and (eq gnus-build-sparse-threads 'some)
8707                  (gnus-summary-article-sparse-p number)
8708                  (zerop children))
8709             ;; If we use expunging, and this article is really
8710             ;; low-scored, then we don't want this article.
8711             (when (and gnus-summary-expunge-below
8712                        (< (setq score
8713                                 (or (cdr (assq number gnus-newsgroup-scored))
8714                                     gnus-summary-default-score))
8715                           gnus-summary-expunge-below))
8716               ;; We increase the expunge-tally here, but that has
8717               ;; nothing to do with the limits, really.
8718               (incf gnus-newsgroup-expunged-tally)
8719               ;; We also mark as read here, if that's wanted.
8720               (when (and gnus-summary-mark-below
8721                          (< score gnus-summary-mark-below))
8722                 (setq gnus-newsgroup-unreads
8723                       (delq number gnus-newsgroup-unreads))
8724                 (if gnus-newsgroup-auto-expire
8725                     (push number gnus-newsgroup-expirable)
8726                   (push (cons number gnus-low-score-mark)
8727                         gnus-newsgroup-reads)))
8728               t)
8729             ;; Do the `display' group parameter.
8730             (and gnus-newsgroup-display
8731                  (not (funcall gnus-newsgroup-display)))
8732             ;; Check NoCeM things.
8733             (when (and gnus-use-nocem
8734                        (gnus-nocem-unwanted-article-p
8735                         (mail-header-id (car thread))))
8736               (setq gnus-newsgroup-unreads
8737                     (delq number gnus-newsgroup-unreads))
8738               t)))
8739           ;; Nope, invisible article.
8740           0
8741         ;; Ok, this article is to be visible, so we add it to the limit
8742         ;; and return 1.
8743         (push number gnus-newsgroup-limit)
8744         1))))
8745
8746 (defun gnus-expunge-thread (thread)
8747   "Mark all articles in THREAD as read."
8748   (let* ((number (mail-header-number (car thread))))
8749     (incf gnus-newsgroup-expunged-tally)
8750     ;; We also mark as read here, if that's wanted.
8751     (setq gnus-newsgroup-unreads
8752           (delq number gnus-newsgroup-unreads))
8753     (if gnus-newsgroup-auto-expire
8754         (push number gnus-newsgroup-expirable)
8755       (push (cons number gnus-low-score-mark)
8756             gnus-newsgroup-reads)))
8757   ;; Go recursively through all subthreads.
8758   (mapcar 'gnus-expunge-thread (cdr thread)))
8759
8760 ;; Summary article oriented commands
8761
8762 (defun gnus-summary-refer-parent-article (n)
8763   "Refer parent article N times.
8764 If N is negative, go to ancestor -N instead.
8765 The difference between N and the number of articles fetched is returned."
8766   (interactive "p")
8767   (let ((skip 1)
8768         error header ref)
8769     (when (not (natnump n))
8770       (setq skip (abs n)
8771             n 1))
8772     (while (and (> n 0)
8773                 (not error))
8774       (setq header (gnus-summary-article-header))
8775       (if (and (eq (mail-header-number header)
8776                    (cdr gnus-article-current))
8777                (equal gnus-newsgroup-name
8778                       (car gnus-article-current)))
8779           ;; If we try to find the parent of the currently
8780           ;; displayed article, then we take a look at the actual
8781           ;; References header, since this is slightly more
8782           ;; reliable than the References field we got from the
8783           ;; server.
8784           (with-current-buffer gnus-original-article-buffer
8785             (nnheader-narrow-to-headers)
8786             (unless (setq ref (message-fetch-field "references"))
8787               (when (setq ref (message-fetch-field "in-reply-to"))
8788                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8789             (widen))
8790         (setq ref
8791               ;; It's not the current article, so we take a bet on
8792               ;; the value we got from the server.
8793               (mail-header-references header)))
8794       (if (and ref
8795                (not (equal ref "")))
8796           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8797             (gnus-message 1 "Couldn't find parent"))
8798         (gnus-message 1 "No references in article %d"
8799                       (gnus-summary-article-number))
8800         (setq error t))
8801       (decf n))
8802     (gnus-summary-position-point)
8803     n))
8804
8805 (defun gnus-summary-refer-references ()
8806   "Fetch all articles mentioned in the References header.
8807 Return the number of articles fetched."
8808   (interactive)
8809   (let ((ref (mail-header-references (gnus-summary-article-header)))
8810         (current (gnus-summary-article-number))
8811         (n 0))
8812     (if (or (not ref)
8813             (equal ref ""))
8814         (error "No References in the current article")
8815       ;; For each Message-ID in the References header...
8816       (while (string-match "<[^>]*>" ref)
8817         (incf n)
8818         ;; ... fetch that article.
8819         (gnus-summary-refer-article
8820          (prog1 (match-string 0 ref)
8821            (setq ref (substring ref (match-end 0))))))
8822       (gnus-summary-goto-subject current)
8823       (gnus-summary-position-point)
8824       n)))
8825
8826 (defun gnus-summary-refer-thread (&optional limit)
8827   "Fetch all articles in the current thread.
8828 If LIMIT (the numerical prefix), fetch that many old headers instead
8829 of what's specified by the `gnus-refer-thread-limit' variable."
8830   (interactive "P")
8831   (let ((id (mail-header-id (gnus-summary-article-header)))
8832         (limit (if limit (prefix-numeric-value limit)
8833                  gnus-refer-thread-limit)))
8834     (unless (eq gnus-fetch-old-headers 'invisible)
8835       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8836       ;; Retrieve the headers and read them in.
8837       (if (eq (if (numberp limit)
8838                   (gnus-retrieve-headers
8839                    (list (min
8840                           (+ (mail-header-number
8841                               (gnus-summary-article-header))
8842                              limit)
8843                           gnus-newsgroup-end))
8844                    gnus-newsgroup-name (* limit 2))
8845                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8846                 ;; headers.
8847                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8848                                        gnus-newsgroup-name limit))
8849               'nov)
8850           (gnus-build-all-threads)
8851         (error "Can't fetch thread from back ends that don't support NOV"))
8852       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8853     (gnus-summary-limit-include-thread id)))
8854
8855 (defun gnus-summary-refer-article (message-id)
8856   "Fetch an article specified by MESSAGE-ID."
8857   (interactive "sMessage-ID: ")
8858   (when (and (stringp message-id)
8859              (not (zerop (length message-id))))
8860     (setq message-id (gnus-replace-in-string message-id " " ""))
8861     ;; Construct the correct Message-ID if necessary.
8862     ;; Suggested by tale@pawl.rpi.edu.
8863     (unless (string-match "^<" message-id)
8864       (setq message-id (concat "<" message-id)))
8865     (unless (string-match ">$" message-id)
8866       (setq message-id (concat message-id ">")))
8867     ;; People often post MIDs from URLs, so unhex it:
8868     (unless (string-match "@" message-id)
8869       (setq message-id (gnus-url-unhex-string message-id)))
8870     (let* ((header (gnus-id-to-header message-id))
8871            (sparse (and header
8872                         (gnus-summary-article-sparse-p
8873                          (mail-header-number header))
8874                         (memq (mail-header-number header)
8875                               gnus-newsgroup-limit)))
8876            number)
8877       (cond
8878        ;; If the article is present in the buffer we just go to it.
8879        ((and header
8880              (or (not (gnus-summary-article-sparse-p
8881                        (mail-header-number header)))
8882                  sparse))
8883         (prog1
8884             (gnus-summary-goto-article
8885              (mail-header-number header) nil t)
8886           (when sparse
8887             (gnus-summary-update-article (mail-header-number header)))))
8888        (t
8889         ;; We fetch the article.
8890         (catch 'found
8891           (dolist (gnus-override-method (gnus-refer-article-methods))
8892             (when (and (gnus-check-server gnus-override-method)
8893                        ;; Fetch the header,
8894                        (setq number (gnus-summary-insert-subject message-id)))
8895               ;; and display the article.
8896               (gnus-summary-select-article nil nil nil number)
8897               (throw 'found t)))
8898           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8899
8900 (defun gnus-refer-article-methods ()
8901   "Return a list of referable methods."
8902   (cond
8903    ;; No method, so we default to current and native.
8904    ((null gnus-refer-article-method)
8905     (list gnus-current-select-method gnus-select-method))
8906    ;; Current.
8907    ((eq 'current gnus-refer-article-method)
8908     (list gnus-current-select-method))
8909    ;; List of select methods.
8910    ((not (and (symbolp (car gnus-refer-article-method))
8911               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8912     (let (out)
8913       (dolist (method gnus-refer-article-method)
8914         (push (if (eq 'current method)
8915                   gnus-current-select-method
8916                 method)
8917               out))
8918       (nreverse out)))
8919    ;; One single select method.
8920    (t
8921     (list gnus-refer-article-method))))
8922
8923 (defun gnus-summary-edit-parameters ()
8924   "Edit the group parameters of the current group."
8925   (interactive)
8926   (gnus-group-edit-group gnus-newsgroup-name 'params))
8927
8928 (defun gnus-summary-customize-parameters ()
8929   "Customize the group parameters of the current group."
8930   (interactive)
8931   (gnus-group-customize gnus-newsgroup-name))
8932
8933 (defun gnus-summary-enter-digest-group (&optional force)
8934   "Enter an nndoc group based on the current article.
8935 If FORCE, force a digest interpretation.  If not, try
8936 to guess what the document format is."
8937   (interactive "P")
8938   (let ((conf gnus-current-window-configuration))
8939     (save-window-excursion
8940       (save-excursion
8941         (let (gnus-article-prepare-hook
8942               gnus-display-mime-function
8943               gnus-break-pages)
8944           (gnus-summary-select-article))))
8945     (setq gnus-current-window-configuration conf)
8946     (let* ((name (format "%s-%d"
8947                          (gnus-group-prefixed-name
8948                           gnus-newsgroup-name (list 'nndoc ""))
8949                          (with-current-buffer gnus-summary-buffer
8950                            gnus-current-article)))
8951            (ogroup gnus-newsgroup-name)
8952            (params (append (gnus-info-params (gnus-get-info ogroup))
8953                            (list (cons 'to-group ogroup))
8954                            (list (cons 'parent-group ogroup))
8955                            (list (cons 'save-article-group ogroup))))
8956            (case-fold-search t)
8957            (buf (current-buffer))
8958            dig to-address)
8959       (with-current-buffer gnus-original-article-buffer
8960         ;; Have the digest group inherit the main mail address of
8961         ;; the parent article.
8962         (when (setq to-address (or (gnus-fetch-field "reply-to")
8963                                    (gnus-fetch-field "from")))
8964           (setq params
8965                 (append
8966                  (list (cons 'to-address
8967                              (funcall gnus-decode-encoded-address-function
8968                                       to-address))))))
8969         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8970         (insert-buffer-substring gnus-original-article-buffer)
8971         ;; Remove lines that may lead nndoc to misinterpret the
8972         ;; document type.
8973         (narrow-to-region
8974          (goto-char (point-min))
8975          (or (search-forward "\n\n" nil t) (point)))
8976         (goto-char (point-min))
8977         (delete-matching-lines "^Path:\\|^From ")
8978         (widen))
8979       (unwind-protect
8980           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8981                     (gnus-newsgroup-ephemeral-ignored-charsets
8982                      gnus-newsgroup-ignored-charsets))
8983                 (gnus-group-read-ephemeral-group
8984                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8985                               (nndoc-article-type
8986                                ,(if force 'mbox 'guess)))
8987                  t nil nil nil
8988                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8989                                                         "ADAPT")))))
8990               ;; Make all postings to this group go to the parent group.
8991               (nconc (gnus-info-params (gnus-get-info name))
8992                      params)
8993             ;; Couldn't select this doc group.
8994             (switch-to-buffer buf)
8995             (gnus-set-global-variables)
8996             (gnus-configure-windows 'summary)
8997             (gnus-message 3 "Article couldn't be entered?"))
8998         (kill-buffer dig)))))
8999
9000 (defun gnus-summary-read-document (n)
9001   "Open a new group based on the current article(s).
9002 This will allow you to read digests and other similar
9003 documents as newsgroups.
9004 Obeys the standard process/prefix convention."
9005   (interactive "P")
9006   (let* ((ogroup gnus-newsgroup-name)
9007          (params (append (gnus-info-params (gnus-get-info ogroup))
9008                          (list (cons 'to-group ogroup))))
9009          group egroup groups vgroup)
9010     (dolist (article (gnus-summary-work-articles n))
9011       (setq group (format "%s-%d" gnus-newsgroup-name article))
9012       (gnus-summary-remove-process-mark article)
9013       (when (gnus-summary-display-article article)
9014         (save-excursion ;;What for?
9015           (with-temp-buffer
9016             (insert-buffer-substring gnus-original-article-buffer)
9017             ;; Remove some headers that may lead nndoc to make
9018             ;; the wrong guess.
9019             (message-narrow-to-head)
9020             (goto-char (point-min))
9021             (delete-matching-lines "^Path:\\|^From ")
9022             (widen)
9023             (if (setq egroup
9024                       (gnus-group-read-ephemeral-group
9025                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9026                                      (nndoc-article-type guess))
9027                        t nil t))
9028                 (progn
9029                   ;; Make all postings to this group go to the parent group.
9030                   (nconc (gnus-info-params (gnus-get-info egroup))
9031                          params)
9032                   (push egroup groups))
9033               ;; Couldn't select this doc group.
9034               (gnus-error 3 "Article couldn't be entered"))))))
9035     ;; Now we have selected all the documents.
9036     (cond
9037      ((not groups)
9038       (error "None of the articles could be interpreted as documents"))
9039      ((gnus-group-read-ephemeral-group
9040        (setq vgroup (format
9041                      "nnvirtual:%s-%s" gnus-newsgroup-name
9042                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9043        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9044        t
9045        (cons (current-buffer) 'summary)))
9046      (t
9047       (error "Couldn't select virtual nndoc group")))))
9048
9049 (defun gnus-summary-isearch-article (&optional regexp-p)
9050   "Do incremental search forward on the current article.
9051 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9052   (interactive "P")
9053   (gnus-summary-select-article)
9054   (gnus-configure-windows 'article)
9055   (gnus-eval-in-buffer-window gnus-article-buffer
9056     (save-restriction
9057       (widen)
9058       (isearch-forward regexp-p))))
9059
9060 (defun gnus-summary-repeat-search-article-forward ()
9061   "Repeat the previous search forwards."
9062   (interactive)
9063   (unless gnus-last-search-regexp
9064     (error "No previous search"))
9065   (gnus-summary-search-article-forward gnus-last-search-regexp))
9066
9067 (defun gnus-summary-repeat-search-article-backward ()
9068   "Repeat the previous search backwards."
9069   (interactive)
9070   (unless gnus-last-search-regexp
9071     (error "No previous search"))
9072   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9073
9074 (defun gnus-summary-search-article-forward (regexp &optional backward)
9075   "Search for an article containing REGEXP forward.
9076 If BACKWARD, search backward instead."
9077   (interactive
9078    (list (read-string
9079           (format "Search article %s (regexp%s): "
9080                   (if current-prefix-arg "backward" "forward")
9081                   (if gnus-last-search-regexp
9082                       (concat ", default " gnus-last-search-regexp)
9083                     "")))
9084          current-prefix-arg))
9085   (if (string-equal regexp "")
9086       (setq regexp (or gnus-last-search-regexp ""))
9087     (setq gnus-last-search-regexp regexp)
9088     (setq gnus-article-before-search gnus-current-article))
9089   ;; Intentionally set gnus-last-article.
9090   (setq gnus-last-article gnus-article-before-search)
9091   (let ((gnus-last-article gnus-last-article))
9092     (if (gnus-summary-search-article regexp backward)
9093         (gnus-summary-show-thread)
9094       (signal 'search-failed (list regexp)))))
9095
9096 (defun gnus-summary-search-article-backward (regexp)
9097   "Search for an article containing REGEXP backward."
9098   (interactive
9099    (list (read-string
9100           (format "Search article backward (regexp%s): "
9101                   (if gnus-last-search-regexp
9102                       (concat ", default " gnus-last-search-regexp)
9103                     "")))))
9104   (gnus-summary-search-article-forward regexp 'backward))
9105
9106 (defun gnus-summary-search-article (regexp &optional backward)
9107   "Search for an article containing REGEXP.
9108 Optional argument BACKWARD means do search for backward.
9109 `gnus-select-article-hook' is not called during the search."
9110   ;; We have to require this here to make sure that the following
9111   ;; dynamic binding isn't shadowed by autoloading.
9112   (require 'gnus-async)
9113   (require 'gnus-art)
9114   (let ((gnus-select-article-hook nil)  ;Disable hook.
9115         (gnus-article-prepare-hook nil)
9116         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9117         (gnus-use-article-prefetch nil)
9118         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9119         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9120         (gnus-visual nil)
9121         (gnus-keep-backlog nil)
9122         (gnus-break-pages nil)
9123         (gnus-summary-display-arrow nil)
9124         (gnus-updated-mode-lines nil)
9125         (gnus-auto-center-summary nil)
9126         (sum (current-buffer))
9127         (gnus-display-mime-function nil)
9128         (found nil)
9129         point)
9130     (gnus-save-hidden-threads
9131       (gnus-summary-select-article)
9132       (set-buffer gnus-article-buffer)
9133       (goto-char (window-point (get-buffer-window (current-buffer))))
9134       (when backward
9135         (forward-line -1))
9136       (while (not found)
9137         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9138         (if (if backward
9139                 (re-search-backward regexp nil t)
9140               (re-search-forward regexp nil t))
9141             ;; We found the regexp.
9142             (progn
9143               (setq found 'found)
9144               (beginning-of-line)
9145               (set-window-start
9146                (get-buffer-window (current-buffer))
9147                (point))
9148               (forward-line 1)
9149               (set-window-point
9150                (get-buffer-window (current-buffer))
9151                (point))
9152               (set-buffer sum)
9153               (setq point (point)))
9154           ;; We didn't find it, so we go to the next article.
9155           (set-buffer sum)
9156           (setq found 'not)
9157           (while (eq found 'not)
9158             (if (not (if backward (gnus-summary-find-prev)
9159                        (gnus-summary-find-next)))
9160                 ;; No more articles.
9161                 (setq found t)
9162               ;; Select the next article and adjust point.
9163               (unless (gnus-summary-article-sparse-p
9164                        (gnus-summary-article-number))
9165                 (setq found nil)
9166                 (gnus-summary-select-article)
9167                 (set-buffer gnus-article-buffer)
9168                 (widen)
9169                 (goto-char (if backward (point-max) (point-min))))))))
9170       (gnus-message 7 ""))
9171     ;; Return whether we found the regexp.
9172     (when (eq found 'found)
9173       (goto-char point)
9174       (gnus-summary-show-thread)
9175       (gnus-summary-goto-subject gnus-current-article)
9176       (gnus-summary-position-point)
9177       t)))
9178
9179 (defun gnus-find-matching-articles (header regexp)
9180   "Return a list of all articles that match REGEXP on HEADER.
9181 This search includes all articles in the current group that Gnus has
9182 fetched headers for, whether they are displayed or not."
9183   (let ((articles nil)
9184         ;; Can't eta-reduce because it's a macro.
9185         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9186         (case-fold-search t))
9187     (dolist (header gnus-newsgroup-headers)
9188       (when (string-match regexp (funcall func header))
9189         (push (mail-header-number header) articles)))
9190     (nreverse articles)))
9191
9192 (defun gnus-summary-find-matching (header regexp &optional backward unread
9193                                           not-case-fold not-matching)
9194   "Return a list of all articles that match REGEXP on HEADER.
9195 The search stars on the current article and goes forwards unless
9196 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9197 If UNREAD is non-nil, only unread articles will
9198 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9199 in the comparisons. If NOT-MATCHING, return a list of all articles that
9200 not match REGEXP on HEADER."
9201   (let ((case-fold-search (not not-case-fold))
9202         articles d func)
9203     (if (consp header)
9204         (if (eq (car header) 'extra)
9205             (setq func
9206                   `(lambda (h)
9207                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9208                          "")))
9209           (error "%s is an invalid header" header))
9210       (unless (fboundp (intern (concat "mail-header-" header)))
9211         (error "%s is not a valid header" header))
9212       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9213     (dolist (d (if (eq backward 'all)
9214                    gnus-newsgroup-data
9215                  (gnus-data-find-list
9216                   (gnus-summary-article-number)
9217                   (gnus-data-list backward))))
9218       (when (and (or (not unread)       ; We want all articles...
9219                      (gnus-data-unread-p d)) ; Or just unreads.
9220                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9221                  (if not-matching
9222                      (not (string-match
9223                            regexp
9224                            (funcall func (gnus-data-header d))))
9225                    (string-match regexp
9226                                  (funcall func (gnus-data-header d)))))
9227         (push (gnus-data-number d) articles))) ; Success!
9228     (nreverse articles)))
9229
9230 (defun gnus-summary-execute-command (header regexp command &optional backward)
9231   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9232 If HEADER is an empty string (or nil), the match is done on the entire
9233 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9234   (interactive
9235    (list (let ((completion-ignore-case t))
9236            (completing-read
9237             "Header name: "
9238             (mapcar (lambda (header) (list (format "%s" header)))
9239                     (append
9240                      '("Number" "Subject" "From" "Lines" "Date"
9241                        "Message-ID" "Xref" "References" "Body")
9242                      gnus-extra-headers))
9243             nil 'require-match))
9244          (read-string "Regexp: ")
9245          (read-key-sequence "Command: ")
9246          current-prefix-arg))
9247   (when (equal header "Body")
9248     (setq header ""))
9249   ;; Hidden thread subtrees must be searched as well.
9250   (gnus-summary-show-all-threads)
9251   ;; We don't want to change current point nor window configuration.
9252   (save-excursion
9253     (save-window-excursion
9254       (let (gnus-visual
9255             gnus-treat-strip-trailing-blank-lines
9256             gnus-treat-strip-leading-blank-lines
9257             gnus-treat-strip-multiple-blank-lines
9258             gnus-treat-hide-boring-headers
9259             gnus-treat-fold-newsgroups
9260             gnus-article-prepare-hook)
9261         (gnus-message 6 "Executing %s..." (key-description command))
9262         ;; We'd like to execute COMMAND interactively so as to give arguments.
9263         (gnus-execute header regexp
9264                       `(call-interactively ',(key-binding command))
9265                       backward)
9266         (gnus-message 6 "Executing %s...done" (key-description command))))))
9267
9268 (defun gnus-summary-beginning-of-article ()
9269   "Scroll the article back to the beginning."
9270   (interactive)
9271   (gnus-summary-select-article)
9272   (gnus-configure-windows 'article)
9273   (gnus-eval-in-buffer-window gnus-article-buffer
9274     (widen)
9275     (goto-char (point-min))
9276     (when gnus-break-pages
9277       (gnus-narrow-to-page))))
9278
9279 (defun gnus-summary-end-of-article ()
9280   "Scroll to the end of the article."
9281   (interactive)
9282   (gnus-summary-select-article)
9283   (gnus-configure-windows 'article)
9284   (gnus-eval-in-buffer-window gnus-article-buffer
9285     (widen)
9286     (goto-char (point-max))
9287     (recenter -3)
9288     (when gnus-break-pages
9289       (gnus-narrow-to-page))))
9290
9291 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9292   "Truncate to LEN and quote all \"(\"'s in STRING."
9293   (gnus-replace-in-string (if (and len (> (length string) len))
9294                               (substring string 0 len)
9295                             string)
9296                           "[()]" "\\\\\\&"))
9297
9298 (defun gnus-summary-print-article (&optional filename n)
9299   "Generate and print a PostScript image of the process-marked (mail) articles.
9300
9301 If used interactively, print the current article if none are
9302 process-marked.  With prefix arg, prompt the user for the name of the
9303 file to save in.
9304
9305 When used from Lisp, accept two optional args FILENAME and N.  N means
9306 to print the next N articles.  If N is negative, print the N previous
9307 articles.  If N is nil and articles have been marked with the process
9308 mark, print these instead.
9309
9310 If the optional first argument FILENAME is nil, send the image to the
9311 printer.  If FILENAME is a string, save the PostScript image in a file with
9312 that name.  If FILENAME is a number, prompt the user for the name of the file
9313 to save in."
9314   (interactive (list (ps-print-preprint current-prefix-arg)))
9315   (dolist (article (gnus-summary-work-articles n))
9316     (gnus-summary-select-article nil nil 'pseudo article)
9317     (gnus-eval-in-buffer-window gnus-article-buffer
9318       (gnus-print-buffer))
9319     (gnus-summary-remove-process-mark article))
9320   (ps-despool filename))
9321
9322 (defun gnus-print-buffer ()
9323   (let ((buffer (generate-new-buffer " *print*")))
9324     (unwind-protect
9325         (progn
9326           (copy-to-buffer buffer (point-min) (point-max))
9327           (set-buffer buffer)
9328           (gnus-remove-text-with-property 'gnus-decoration)
9329           (when (gnus-visual-p 'article-highlight 'highlight)
9330             ;; Copy-to-buffer doesn't copy overlay.  So redo
9331             ;; highlight.
9332             (let ((gnus-article-buffer buffer))
9333               (gnus-article-highlight-citation t)
9334               (gnus-article-highlight-signature)
9335               (gnus-article-emphasize)
9336               (gnus-article-delete-invisible-text)))
9337           (let ((ps-left-header
9338                  (list
9339                   (concat "("
9340                           (gnus-summary-print-truncate-and-quote
9341                            (mail-header-subject gnus-current-headers)
9342                            66) ")")
9343                   (concat "("
9344                           (gnus-summary-print-truncate-and-quote
9345                            (mail-header-from gnus-current-headers)
9346                            45) ")")))
9347                 (ps-right-header
9348                  (list
9349                   "/pagenumberstring load"
9350                   (concat "("
9351                           (mail-header-date gnus-current-headers) ")"))))
9352             (gnus-run-hooks 'gnus-ps-print-hook)
9353             (save-excursion
9354               (if ps-print-color-p
9355                   (ps-spool-buffer-with-faces)
9356                 (ps-spool-buffer)))))
9357       (kill-buffer buffer))))
9358
9359 (defun gnus-summary-show-article (&optional arg)
9360   "Force redisplaying of the current article.
9361 If ARG (the prefix) is a number, show the article with the charset
9362 defined in `gnus-summary-show-article-charset-alist', or the charset
9363 input.
9364 If ARG (the prefix) is non-nil and not a number, show the raw article
9365 without any article massaging functions being run.  Normally, the key
9366 strokes are `C-u g'."
9367   (interactive "P")
9368   (cond
9369    ((numberp arg)
9370     (gnus-summary-show-article t)
9371     (let ((gnus-newsgroup-charset
9372            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9373                (mm-read-coding-system
9374                 "View as charset: " ;; actually it is coding system.
9375                 (with-current-buffer gnus-article-buffer
9376                   (mm-detect-coding-region (point) (point-max))))))
9377           (gnus-newsgroup-ignored-charsets 'gnus-all))
9378       (gnus-summary-select-article nil 'force)
9379       (let ((deps gnus-newsgroup-dependencies)
9380             head header lines)
9381         (with-current-buffer gnus-original-article-buffer
9382           (save-restriction
9383             (message-narrow-to-head)
9384             (setq head (buffer-string))
9385             (goto-char (point-min))
9386             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9387               (goto-char (point-max))
9388               (widen)
9389               (setq lines (1- (count-lines (point) (point-max))))))
9390           (with-temp-buffer
9391             (insert (format "211 %d Article retrieved.\n"
9392                             (cdr gnus-article-current)))
9393             (insert head)
9394             (if lines (insert (format "Lines: %d\n" lines)))
9395             (insert ".\n")
9396             (let ((nntp-server-buffer (current-buffer)))
9397               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9398         (gnus-data-set-header
9399          (gnus-data-find (cdr gnus-article-current))
9400          header)
9401         (gnus-summary-update-article-line
9402          (cdr gnus-article-current) header)
9403         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9404           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9405    ((not arg)
9406     ;; Select the article the normal way.
9407     (gnus-summary-select-article nil 'force))
9408    (t
9409     ;; We have to require this here to make sure that the following
9410     ;; dynamic binding isn't shadowed by autoloading.
9411     (require 'gnus-async)
9412     (require 'gnus-art)
9413     ;; Bind the article treatment functions to nil.
9414     (let ((gnus-have-all-headers t)
9415           gnus-article-prepare-hook
9416           gnus-article-decode-hook
9417           gnus-display-mime-function
9418           gnus-break-pages)
9419       ;; Destroy any MIME parts.
9420       (when (gnus-buffer-live-p gnus-article-buffer)
9421         (with-current-buffer gnus-article-buffer
9422           (mm-destroy-parts gnus-article-mime-handles)
9423           ;; Set it to nil for safety reason.
9424           (setq gnus-article-mime-handle-alist nil)
9425           (setq gnus-article-mime-handles nil)))
9426       (gnus-summary-select-article nil 'force))))
9427   (gnus-summary-goto-subject gnus-current-article)
9428   (gnus-summary-position-point))
9429
9430 (defun gnus-summary-show-raw-article ()
9431   "Show the raw article without any article massaging functions being run."
9432   (interactive)
9433   (gnus-summary-show-article t))
9434
9435 (defun gnus-summary-verbose-headers (&optional arg)
9436   "Toggle permanent full header display.
9437 If ARG is a positive number, turn header display on.
9438 If ARG is a negative number, turn header display off."
9439   (interactive "P")
9440   (setq gnus-show-all-headers
9441         (cond ((or (not (numberp arg))
9442                    (zerop arg))
9443                (not gnus-show-all-headers))
9444               ((natnump arg)
9445                t)))
9446   (gnus-summary-show-article))
9447
9448 (defun gnus-summary-toggle-header (&optional arg)
9449   "Show the headers if they are hidden, or hide them if they are shown.
9450 If ARG is a positive number, show the entire header.
9451 If ARG is a negative number, hide the unwanted header lines."
9452   (interactive "P")
9453   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9454                      (get-buffer-window gnus-article-buffer t))))
9455     (with-current-buffer gnus-article-buffer
9456       (widen)
9457       (article-narrow-to-head)
9458       (let* ((inhibit-read-only t)
9459              (inhibit-point-motion-hooks t)
9460              (hidden (if (numberp arg)
9461                          (>= arg 0)
9462                        (or (not (looking-at "[^ \t\n]+:"))
9463                            (gnus-article-hidden-text-p 'headers))))
9464              s e)
9465         (delete-region (point-min) (point-max))
9466         (with-current-buffer gnus-original-article-buffer
9467           (goto-char (setq s (point-min)))
9468           (setq e (if (search-forward "\n\n" nil t)
9469                       (1- (point))
9470                     (point-max))))
9471         (insert-buffer-substring gnus-original-article-buffer s e)
9472         (run-hooks 'gnus-article-decode-hook)
9473         (if hidden
9474             (let ((gnus-treat-hide-headers nil)
9475                   (gnus-treat-hide-boring-headers nil))
9476               (gnus-delete-wash-type 'headers)
9477               (gnus-treat-article 'head))
9478           (gnus-treat-article 'head))
9479         (widen)
9480         (if window
9481             (set-window-start window (goto-char (point-min))))
9482         (if gnus-break-pages
9483             (gnus-narrow-to-page)
9484           (when (gnus-visual-p 'page-marker)
9485             (let ((inhibit-read-only t))
9486               (gnus-remove-text-with-property 'gnus-prev)
9487               (gnus-remove-text-with-property 'gnus-next))))
9488         (gnus-set-mode-line 'article)))))
9489
9490 (defun gnus-summary-show-all-headers ()
9491   "Make all header lines visible."
9492   (interactive)
9493   (gnus-summary-toggle-header 1))
9494
9495 (defun gnus-summary-caesar-message (&optional arg)
9496   "Caesar rotate the current article by 13.
9497 With a non-numerical prefix, also rotate headers.  A numerical
9498 prefix specifies how many places to rotate each letter forward."
9499   (interactive "P")
9500   (gnus-summary-select-article)
9501   (let ((mail-header-separator ""))
9502     (gnus-eval-in-buffer-window gnus-article-buffer
9503       (save-restriction
9504         (widen)
9505         (let ((start (window-start))
9506               (inhibit-read-only t))
9507           (if (equal arg '(4))
9508               (message-caesar-buffer-body nil t)
9509             (message-caesar-buffer-body arg))
9510           (set-window-start (get-buffer-window (current-buffer)) start)))))
9511   ;; Create buttons and stuff...
9512   (gnus-treat-article nil))
9513
9514 (declare-function idna-to-unicode "ext:idna" (str))
9515
9516 (defun gnus-summary-idna-message (&optional arg)
9517   "Decode IDNA encoded domain names in the current articles.
9518 IDNA encoded domain names looks like `xn--bar'.  If a string
9519 remain unencoded after running this function, it is likely an
9520 invalid IDNA string (`xn--bar' is invalid).
9521
9522 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9523 installed for this command to work."
9524   (interactive "P")
9525   (if (not (and (condition-case nil (require 'idna)
9526                   (file-error))
9527                 (mm-coding-system-p 'utf-8)
9528                 (executable-find (symbol-value 'idna-program))))
9529       (gnus-message
9530        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9531     (gnus-summary-select-article)
9532     (let ((mail-header-separator ""))
9533       (gnus-eval-in-buffer-window gnus-article-buffer
9534         (save-restriction
9535           (widen)
9536           (let ((start (window-start))
9537                 buffer-read-only)
9538             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9539               (replace-match (idna-to-unicode (match-string 1))))
9540             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9541
9542 (defun gnus-summary-morse-message (&optional arg)
9543   "Morse decode the current article."
9544   (interactive "P")
9545   (gnus-summary-select-article)
9546   (let ((mail-header-separator ""))
9547     (gnus-eval-in-buffer-window gnus-article-buffer
9548       (save-excursion
9549         (save-restriction
9550           (widen)
9551           (let ((pos (window-start))
9552                 (inhibit-read-only t))
9553             (goto-char (point-min))
9554             (when (message-goto-body)
9555               (gnus-narrow-to-body))
9556             (goto-char (point-min))
9557             (while (search-forward "·" (point-max) t)
9558               (replace-match "."))
9559             (unmorse-region (point-min) (point-max))
9560             (widen)
9561             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9562
9563 (defun gnus-summary-stop-page-breaking ()
9564   "Stop page breaking in the current article."
9565   (interactive)
9566   (gnus-summary-select-article)
9567   (gnus-eval-in-buffer-window gnus-article-buffer
9568     (widen)
9569     (when (gnus-visual-p 'page-marker)
9570       (let ((inhibit-read-only t))
9571         (gnus-remove-text-with-property 'gnus-prev)
9572         (gnus-remove-text-with-property 'gnus-next))
9573       (setq gnus-page-broken nil))))
9574
9575 (defun gnus-summary-move-article (&optional n to-newsgroup
9576                                             select-method action)
9577   "Move the current article to a different newsgroup.
9578 If N is a positive number, move the N next articles.
9579 If N is a negative number, move the N previous articles.
9580 If N is nil and any articles have been marked with the process mark,
9581 move those articles instead.
9582 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9583 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9584 re-spool using this method.
9585
9586 When called interactively with TO-NEWSGROUP being nil, the value of
9587 the variable `gnus-move-split-methods' is used for finding a default
9588 for the target newsgroup.
9589
9590 For this function to work, both the current newsgroup and the
9591 newsgroup that you want to move to have to support the `request-move'
9592 and `request-accept' functions.
9593
9594 ACTION can be either `move' (the default), `crosspost' or `copy'."
9595   (interactive "P")
9596   (unless action
9597     (setq action 'move))
9598   ;; Check whether the source group supports the required functions.
9599   (cond ((and (eq action 'move)
9600               (not (gnus-check-backend-function
9601                     'request-move-article gnus-newsgroup-name)))
9602          (error "The current group does not support article moving"))
9603         ((and (eq action 'crosspost)
9604               (not (gnus-check-backend-function
9605                     'request-replace-article gnus-newsgroup-name)))
9606          (error "The current group does not support article editing")))
9607   (let ((articles (gnus-summary-work-articles n))
9608         (prefix (if (gnus-check-backend-function
9609                      'request-move-article gnus-newsgroup-name)
9610                     (funcall gnus-move-group-prefix-function
9611                              gnus-newsgroup-name)
9612                   ""))
9613         (names '((move "Move" "Moving")
9614                  (copy "Copy" "Copying")
9615                  (crosspost "Crosspost" "Crossposting")))
9616         (copy-buf (save-excursion
9617                     (nnheader-set-temp-buffer " *copy article*")))
9618         art-group to-method new-xref article to-groups
9619         articles-to-update-marks encoded)
9620     (unless (assq action names)
9621       (error "Unknown action %s" action))
9622     ;; Read the newsgroup name.
9623     (when (and (not to-newsgroup)
9624                (not select-method))
9625       (if (and gnus-move-split-methods
9626                (not
9627                 (and (memq gnus-current-article articles)
9628                      (gnus-buffer-live-p gnus-original-article-buffer))))
9629           ;; When `gnus-move-split-methods' is non-nil, we have to
9630           ;; select an article to give `gnus-read-move-group-name' an
9631           ;; opportunity to suggest an appropriate default.  However,
9632           ;; we needn't render or mark the article.
9633           (let ((gnus-display-mime-function nil)
9634                 (gnus-article-prepare-hook nil)
9635                 (gnus-mark-article-hook nil))
9636             (gnus-summary-select-article nil nil nil (car articles))))
9637       (setq to-newsgroup (gnus-read-move-group-name
9638                           (cadr (assq action names))
9639                           (symbol-value
9640                            (intern (format "gnus-current-%s-group" action)))
9641                           articles prefix)
9642             encoded to-newsgroup
9643             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9644       (set (intern (format "gnus-current-%s-group" action))
9645            (mm-decode-coding-string
9646             to-newsgroup
9647             (gnus-group-name-charset to-method to-newsgroup))))
9648     (unless to-method
9649       (setq to-method (or select-method
9650                           (gnus-server-to-method
9651                            (gnus-group-method to-newsgroup)))))
9652     (setq to-newsgroup
9653           (or encoded
9654               (and to-newsgroup
9655                    (mm-encode-coding-string
9656                     to-newsgroup
9657                     (gnus-group-name-charset to-method to-newsgroup)))))
9658     ;; Check the method we are to move this article to...
9659     (unless (gnus-check-backend-function
9660              'request-accept-article (car to-method))
9661       (error "%s does not support article copying" (car to-method)))
9662     (unless (gnus-check-server to-method)
9663       (error "Can't open server %s" (car to-method)))
9664     (gnus-message 6 "%s to %s: %s..."
9665                   (caddr (assq action names))
9666                   (or (car select-method)
9667                       (gnus-group-decoded-name to-newsgroup))
9668                   articles)
9669     (while articles
9670       (setq article (pop articles))
9671       (setq
9672        art-group
9673        (cond
9674         ;; Move the article.
9675         ((eq action 'move)
9676          ;; Remove this article from future suppression.
9677          (gnus-dup-unsuppress-article article)
9678          (let* ((from-method (gnus-find-method-for-group
9679                               gnus-newsgroup-name))
9680                 (to-method (or select-method
9681                                (gnus-find-method-for-group to-newsgroup)))
9682                 (move-is-internal (gnus-method-equal from-method to-method)))
9683            (gnus-request-move-article
9684             article                     ; Article to move
9685             gnus-newsgroup-name         ; From newsgroup
9686             (nth 1 (gnus-find-method-for-group
9687                     gnus-newsgroup-name)) ; Server
9688             (list 'gnus-request-accept-article
9689                   to-newsgroup (list 'quote select-method)
9690                   (not articles) t)     ; Accept form
9691             (not articles)              ; Only save nov last time
9692             move-is-internal)))         ; is this move internal?
9693         ;; Copy the article.
9694         ((eq action 'copy)
9695          (with-current-buffer copy-buf
9696            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9697              (save-restriction
9698                (nnheader-narrow-to-headers)
9699                (dolist (hdr gnus-copy-article-ignored-headers)
9700                  (message-remove-header hdr t)))
9701              (gnus-request-accept-article
9702               to-newsgroup select-method (not articles) t))))
9703         ;; Crosspost the article.
9704         ((eq action 'crosspost)
9705          (let ((xref (message-tokenize-header
9706                       (mail-header-xref (gnus-summary-article-header article))
9707                       " ")))
9708            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9709                                   ":" (number-to-string article)))
9710            (unless xref
9711              (setq xref (list (system-name))))
9712            (setq new-xref
9713                  (concat
9714                   (mapconcat 'identity
9715                              (delete "Xref:" (delete new-xref xref))
9716                              " ")
9717                   " " new-xref))
9718            (with-current-buffer copy-buf
9719              ;; First put the article in the destination group.
9720              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9721              (when (consp (setq art-group
9722                                 (gnus-request-accept-article
9723                                  to-newsgroup select-method (not articles) t)))
9724                (setq new-xref (concat new-xref " " (car art-group)
9725                                       ":"
9726                                       (number-to-string (cdr art-group))))
9727                ;; Now we have the new Xrefs header, so we insert
9728                ;; it and replace the new article.
9729                (nnheader-replace-header "Xref" new-xref)
9730                (gnus-request-replace-article
9731                 (cdr art-group) to-newsgroup (current-buffer) t)
9732                art-group))))))
9733       (cond
9734        ((not art-group)
9735         (gnus-message 1 "Couldn't %s article %s: %s"
9736                       (cadr (assq action names)) article
9737                       (nnheader-get-report (car to-method))))
9738        ((eq art-group 'junk)
9739         (when (eq action 'move)
9740           (gnus-summary-mark-article article gnus-canceled-mark)
9741           (gnus-message 4 "Deleted article %s" article)
9742           ;; run the delete hook
9743           (run-hook-with-args 'gnus-summary-article-delete-hook
9744                               action
9745                               (gnus-data-header
9746                                (assoc article (gnus-data-list nil)))
9747                               gnus-newsgroup-name nil
9748                               select-method)))
9749        (t
9750         (let* ((pto-group (gnus-group-prefixed-name
9751                            (car art-group) to-method))
9752                (info (gnus-get-info pto-group))
9753                (to-group (gnus-info-group info))
9754                to-marks)
9755           ;; Update the group that has been moved to.
9756           (when (and info
9757                      (memq action '(move copy)))
9758             (unless (member to-group to-groups)
9759               (push to-group to-groups))
9760
9761             (unless (memq article gnus-newsgroup-unreads)
9762               (push 'read to-marks)
9763               (gnus-info-set-read
9764                info (gnus-add-to-range (gnus-info-read info)
9765                                        (list (cdr art-group)))))
9766
9767             ;; See whether the article is to be put in the cache.
9768             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9769                    (marks (if expirable
9770                               gnus-article-mark-lists
9771                             (delete '(expirable . expire)
9772                                     (copy-sequence gnus-article-mark-lists))))
9773                    (to-article (cdr art-group)))
9774
9775               ;; Enter the article into the cache in the new group,
9776               ;; if that is required.
9777               (when gnus-use-cache
9778                 (gnus-cache-possibly-enter-article
9779                  to-group to-article
9780                  (memq article gnus-newsgroup-marked)
9781                  (memq article gnus-newsgroup-dormant)
9782                  (memq article gnus-newsgroup-unreads)))
9783
9784               (when gnus-preserve-marks
9785                 ;; Copy any marks over to the new group.
9786                 (when (and (equal to-group gnus-newsgroup-name)
9787                            (not (memq article gnus-newsgroup-unreads)))
9788                   ;; Mark this article as read in this group.
9789                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9790                   (setcdr (gnus-active to-group) to-article)
9791                   (setcdr gnus-newsgroup-active to-article))
9792
9793                 (while marks
9794                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9795                     (when (memq article (symbol-value
9796                                          (intern (format "gnus-newsgroup-%s"
9797                                                          (caar marks)))))
9798                       (push (cdar marks) to-marks)
9799                       ;; If the other group is the same as this group,
9800                       ;; then we have to add the mark to the list.
9801                       (when (equal to-group gnus-newsgroup-name)
9802                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9803                              (cons to-article
9804                                    (symbol-value
9805                                     (intern (format "gnus-newsgroup-%s"
9806                                                     (caar marks)))))))
9807                       ;; Copy the marks to other group.
9808                       (gnus-add-marked-articles
9809                        to-group (cdar marks) (list to-article) info)))
9810                   (setq marks (cdr marks)))
9811
9812                 (when (and expirable
9813                            gnus-mark-copied-or-moved-articles-as-expirable
9814                            (not (memq 'expire to-marks)))
9815                   ;; Mark this article as expirable.
9816                   (push 'expire to-marks)
9817                   (when (equal to-group gnus-newsgroup-name)
9818                     (push to-article gnus-newsgroup-expirable))
9819                   ;; Copy the expirable mark to other group.
9820                   (gnus-add-marked-articles
9821                    to-group 'expire (list to-article) info))
9822
9823                 (gnus-request-set-mark
9824                  to-group (list (list (list to-article) 'add to-marks))))
9825
9826               (gnus-dribble-enter
9827                (concat "(gnus-group-set-info '"
9828                        (gnus-prin1-to-string (gnus-get-info to-group))
9829                        ")"))))
9830
9831           ;; Update the Xref header in this article to point to
9832           ;; the new crossposted article we have just created.
9833           (when (eq action 'crosspost)
9834             (with-current-buffer copy-buf
9835               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9836               (nnheader-replace-header "Xref" new-xref)
9837               (gnus-request-replace-article
9838                article gnus-newsgroup-name (current-buffer) t)))
9839
9840           ;; run the move/copy/crosspost/respool hook
9841           (run-hook-with-args 'gnus-summary-article-move-hook
9842                               action
9843                               (gnus-data-header
9844                                (assoc article (gnus-data-list nil)))
9845                               gnus-newsgroup-name
9846                               to-newsgroup
9847                               select-method))
9848
9849         ;;;!!!Why is this necessary?
9850         (set-buffer gnus-summary-buffer)
9851
9852         (when (eq action 'move)
9853           (save-excursion
9854             (gnus-summary-goto-subject article)
9855             (gnus-summary-mark-article article gnus-canceled-mark)))))
9856       (push article articles-to-update-marks))
9857
9858     (save-excursion
9859       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9860     ;; Re-activate all groups that have been moved to.
9861     (with-current-buffer gnus-group-buffer
9862       (let ((gnus-group-marked to-groups))
9863         (gnus-group-get-new-news-this-group nil t)))
9864
9865     (gnus-kill-buffer copy-buf)
9866     (gnus-summary-position-point)
9867     (gnus-set-mode-line 'summary)))
9868
9869 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9870   "Copy the current article to some other group.
9871 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9872 When called interactively, if TO-NEWSGROUP is nil, use the value of
9873 the variable `gnus-move-split-methods' for finding a default target
9874 newsgroup.
9875 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9876 re-spool using this method."
9877   (interactive "P")
9878   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9879
9880 (defun gnus-summary-crosspost-article (&optional n)
9881   "Crosspost the current article to some other group."
9882   (interactive "P")
9883   (gnus-summary-move-article n nil nil 'crosspost))
9884
9885 (defcustom gnus-summary-respool-default-method nil
9886   "Default method type for respooling an article.
9887 If nil, use to the current newsgroup method."
9888   :type 'symbol
9889   :group 'gnus-summary-mail)
9890
9891 (defun gnus-summary-respool-article (&optional n method)
9892   "Respool the current article.
9893 The article will be squeezed through the mail spooling process again,
9894 which means that it will be put in some mail newsgroup or other
9895 depending on `nnmail-split-methods'.
9896 If N is a positive number, respool the N next articles.
9897 If N is a negative number, respool the N previous articles.
9898 If N is nil and any articles have been marked with the process mark,
9899 respool those articles instead.
9900
9901 Respooling can be done both from mail groups and \"real\" newsgroups.
9902 In the former case, the articles in question will be moved from the
9903 current group into whatever groups they are destined to.  In the
9904 latter case, they will be copied into the relevant groups."
9905   (interactive
9906    (list current-prefix-arg
9907          (let* ((methods (gnus-methods-using 'respool))
9908                 (methname
9909                  (symbol-name (or gnus-summary-respool-default-method
9910                                   (car (gnus-find-method-for-group
9911                                         gnus-newsgroup-name)))))
9912                 (method
9913                  (gnus-completing-read-with-default
9914                   methname "Backend to use when respooling"
9915                   methods nil t nil 'gnus-mail-method-history))
9916                 ms)
9917            (cond
9918             ((zerop (length (setq ms (gnus-servers-using-backend
9919                                       (intern method)))))
9920              (list (intern method) ""))
9921             ((= 1 (length ms))
9922              (car ms))
9923             (t
9924              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9925                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9926                            ms-alist))))))))
9927   (unless method
9928     (error "No method given for respooling"))
9929   (if (assoc (symbol-name
9930               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9931              (gnus-methods-using 'respool))
9932       (gnus-summary-move-article n nil method)
9933     (gnus-summary-copy-article n nil method)))
9934
9935 (defun gnus-summary-import-article (file &optional edit)
9936   "Import an arbitrary file into a mail newsgroup."
9937   (interactive "fImport file: \nP")
9938   (let ((group gnus-newsgroup-name)
9939         (now (current-time))
9940         atts lines group-art)
9941     (unless (gnus-check-backend-function 'request-accept-article group)
9942       (error "%s does not support article importing" group))
9943     (or (file-readable-p file)
9944         (not (file-regular-p file))
9945         (error "Can't read %s" file))
9946     (with-current-buffer (gnus-get-buffer-create " *import file*")
9947       (erase-buffer)
9948       (nnheader-insert-file-contents file)
9949       (goto-char (point-min))
9950       (if (nnheader-article-p)
9951           (save-restriction
9952             (goto-char (point-min))
9953             (search-forward "\n\n" nil t)
9954             (narrow-to-region (point-min) (1- (point)))
9955             (goto-char (point-min))
9956             (unless (re-search-forward "^date:" nil t)
9957               (goto-char (point-max))
9958               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9959        ;; This doesn't look like an article, so we fudge some headers.
9960         (setq atts (file-attributes file)
9961               lines (count-lines (point-min) (point-max)))
9962         (insert "From: " (read-string "From: ") "\n"
9963                 "Subject: " (read-string "Subject: ") "\n"
9964                 "Date: " (message-make-date (nth 5 atts)) "\n"
9965                 "Message-ID: " (message-make-message-id) "\n"
9966                 "Lines: " (int-to-string lines) "\n"
9967                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9968       (setq group-art (gnus-request-accept-article group nil t))
9969       (kill-buffer (current-buffer)))
9970     (setq gnus-newsgroup-active (gnus-activate-group group))
9971     (forward-line 1)
9972     (gnus-summary-goto-article (cdr group-art) nil t)
9973     (when edit
9974       (gnus-summary-edit-article))))
9975
9976 (defun gnus-summary-create-article ()
9977   "Create an article in a mail newsgroup."
9978   (interactive)
9979   (let ((group gnus-newsgroup-name)
9980         (now (current-time))
9981         group-art)
9982     (unless (gnus-check-backend-function 'request-accept-article group)
9983       (error "%s does not support article importing" group))
9984     (with-current-buffer (gnus-get-buffer-create " *import file*")
9985       (erase-buffer)
9986       (goto-char (point-min))
9987       ;; This doesn't look like an article, so we fudge some headers.
9988       (insert "From: " (read-string "From: ") "\n"
9989               "Subject: " (read-string "Subject: ") "\n"
9990               "Date: " (message-make-date now) "\n"
9991               "Message-ID: " (message-make-message-id) "\n")
9992       (setq group-art (gnus-request-accept-article group nil t))
9993       (kill-buffer (current-buffer)))
9994     (setq gnus-newsgroup-active (gnus-activate-group group))
9995     (forward-line 1)
9996     (gnus-summary-goto-article (cdr group-art) nil t)
9997     (gnus-summary-edit-article)))
9998
9999 (defun gnus-summary-article-posted-p ()
10000   "Say whether the current (mail) article is available from news as well.
10001 This will be the case if the article has both been mailed and posted."
10002   (interactive)
10003   (let ((id (mail-header-references (gnus-summary-article-header)))
10004         (gnus-override-method (car (gnus-refer-article-methods))))
10005     (if (gnus-request-head id "")
10006         (gnus-message 2 "The current message was found on %s"
10007                       gnus-override-method)
10008       (gnus-message 2 "The current message couldn't be found on %s"
10009                     gnus-override-method)
10010       nil)))
10011
10012 (defun gnus-summary-expire-articles (&optional now)
10013   "Expire all articles that are marked as expirable in the current group."
10014   (interactive)
10015   (when (and (not gnus-group-is-exiting-without-update-p)
10016              (gnus-check-backend-function
10017               'request-expire-articles gnus-newsgroup-name))
10018     ;; This backend supports expiry.
10019     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10020            (expirable (if total
10021                           (progn
10022                             ;; We need to update the info for
10023                             ;; this group for `gnus-list-of-read-articles'
10024                             ;; to give us the right answer.
10025                             (gnus-run-hooks 'gnus-exit-group-hook)
10026                             (gnus-summary-update-info)
10027                             (gnus-list-of-read-articles gnus-newsgroup-name))
10028                         (setq gnus-newsgroup-expirable
10029                               (sort gnus-newsgroup-expirable '<))))
10030            (expiry-wait (if now 'immediate
10031                           (gnus-group-find-parameter
10032                            gnus-newsgroup-name 'expiry-wait)))
10033            (nnmail-expiry-target
10034             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10035                 nnmail-expiry-target))
10036            es)
10037       (when expirable
10038         ;; There are expirable articles in this group, so we run them
10039         ;; through the expiry process.
10040         (gnus-message 6 "Expiring articles...")
10041         (unless (gnus-check-group gnus-newsgroup-name)
10042           (error "Can't open server for %s" gnus-newsgroup-name))
10043         ;; The list of articles that weren't expired is returned.
10044         (save-excursion
10045           (if expiry-wait
10046               (let ((nnmail-expiry-wait-function nil)
10047                     (nnmail-expiry-wait expiry-wait))
10048                 (setq es (gnus-request-expire-articles
10049                           expirable gnus-newsgroup-name)))
10050             (setq es (gnus-request-expire-articles
10051                       expirable gnus-newsgroup-name)))
10052           (unless total
10053             (setq gnus-newsgroup-expirable es))
10054           ;; We go through the old list of expirable, and mark all
10055           ;; really expired articles as nonexistent.
10056           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10057             (let ((gnus-use-cache nil))
10058               (dolist (article expirable)
10059                 (when (and (not (memq article es))
10060                            (gnus-data-find article))
10061                   (gnus-summary-mark-article article gnus-canceled-mark)
10062                   (run-hook-with-args 'gnus-summary-article-expire-hook
10063                                       'delete
10064                                       (gnus-data-header
10065                                        (assoc article (gnus-data-list nil)))
10066                                       gnus-newsgroup-name
10067                                       nil
10068                                       nil))))))
10069         (gnus-message 6 "Expiring articles...done")))))
10070
10071 (defun gnus-summary-expire-articles-now ()
10072   "Expunge all expirable articles in the current group.
10073 This means that *all* articles that are marked as expirable will be
10074 deleted forever, right now."
10075   (interactive)
10076   (or gnus-expert-user
10077       (gnus-yes-or-no-p
10078        "Are you really, really sure you want to delete all expirable messages? ")
10079       (error "Phew!"))
10080   (gnus-summary-expire-articles t))
10081
10082 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10083 (defun gnus-summary-delete-article (&optional n)
10084   "Delete the N next (mail) articles.
10085 This command actually deletes articles.  This is not a marking
10086 command.  The article will disappear forever from your life, never to
10087 return.
10088
10089 If N is negative, delete backwards.
10090 If N is nil and articles have been marked with the process mark,
10091 delete these instead.
10092
10093 If `gnus-novice-user' is non-nil you will be asked for
10094 confirmation before the articles are deleted."
10095   (interactive "P")
10096   (unless (gnus-check-backend-function 'request-expire-articles
10097                                        gnus-newsgroup-name)
10098     (error "The current newsgroup does not support article deletion"))
10099   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10100     (error "Couldn't open server"))
10101   ;; Compute the list of articles to delete.
10102   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10103         (nnmail-expiry-target 'delete)
10104         not-deleted)
10105     (if (and gnus-novice-user
10106              (not (gnus-yes-or-no-p
10107                    (format "Do you really want to delete %s forever? "
10108                            (if (> (length articles) 1)
10109                                (format "these %s articles" (length articles))
10110                              "this article")))))
10111         ()
10112       ;; Delete the articles.
10113       (setq not-deleted (gnus-request-expire-articles
10114                          articles gnus-newsgroup-name 'force))
10115       (save-excursion
10116         (while articles
10117           (gnus-summary-remove-process-mark (car articles))
10118           ;; The backend might not have been able to delete the article
10119           ;; after all.
10120           (unless (memq (car articles) not-deleted)
10121             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10122           (let* ((article (car articles))
10123                  (ghead  (gnus-data-header
10124                           (assoc article (gnus-data-list nil)))))
10125             (run-hook-with-args 'gnus-summary-article-delete-hook
10126                                 'delete ghead gnus-newsgroup-name nil
10127                                 nil))
10128           (setq articles (cdr articles))))
10129       (when not-deleted
10130         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10131     (gnus-summary-position-point)
10132     (gnus-set-mode-line 'summary)
10133     not-deleted))
10134
10135 (defun gnus-summary-edit-article (&optional arg)
10136   "Edit the current article.
10137 This will have permanent effect only in mail groups.
10138 If ARG is nil, edit the decoded articles.
10139 If ARG is 1, edit the raw articles.
10140 If ARG is 2, edit the raw articles even in read-only groups.
10141 If ARG is 3, edit the articles with the current handles.
10142 Otherwise, allow editing of articles even in read-only
10143 groups."
10144   (interactive "P")
10145   (let (force raw current-handles)
10146     (cond
10147      ((null arg))
10148      ((eq arg 1)
10149       (setq raw t))
10150      ((eq arg 2)
10151       (setq raw t
10152             force t))
10153      ((eq arg 3)
10154       (setq current-handles
10155             (and (gnus-buffer-live-p gnus-article-buffer)
10156                  (with-current-buffer gnus-article-buffer
10157                    (prog1
10158                        gnus-article-mime-handles
10159                      (setq gnus-article-mime-handles nil))))))
10160      (t
10161       (setq force t)))
10162     (when (and raw (not force)
10163                (member gnus-newsgroup-name '("nndraft:delayed"
10164                                              "nndraft:drafts"
10165                                              "nndraft:queue")))
10166       (error "Can't edit the raw article in group %s"
10167              gnus-newsgroup-name))
10168     (with-current-buffer gnus-summary-buffer
10169       (let ((mail-parse-charset gnus-newsgroup-charset)
10170             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10171         (gnus-set-global-variables)
10172         (when (and (not force)
10173                    (gnus-group-read-only-p))
10174           (error "The current newsgroup does not support article editing"))
10175         (gnus-summary-show-article t)
10176         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10177           (with-current-buffer gnus-article-buffer
10178             (mm-enable-multibyte)))
10179         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10180             (setq raw t))
10181         (gnus-article-edit-article
10182          (if raw 'ignore
10183            `(lambda ()
10184               (let ((mbl mml-buffer-list))
10185                 (setq mml-buffer-list nil)
10186                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10187                   (mime-to-mml ,'current-handles))
10188                 (let ((mbl1 mml-buffer-list))
10189                   (setq mml-buffer-list mbl)
10190                   (set (make-local-variable 'mml-buffer-list) mbl1))
10191                 (gnus-make-local-hook 'kill-buffer-hook)
10192                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10193          `(lambda (no-highlight)
10194             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10195                   (message-options message-options)
10196                   (message-options-set-recipient)
10197                   (mail-parse-ignored-charsets
10198                    ',gnus-newsgroup-ignored-charsets)
10199                   (rfc2047-header-encoding-alist
10200                    ',(let ((charset (gnus-group-name-charset
10201                                      (gnus-find-method-for-group
10202                                       gnus-newsgroup-name)
10203                                      gnus-newsgroup-name)))
10204                        (append (list (cons "Newsgroups" charset)
10205                                      (cons "Followup-To" charset)
10206                                      (cons "Xref" charset))
10207                                rfc2047-header-encoding-alist))))
10208               ,(if (not raw) '(progn
10209                                 (mml-to-mime)
10210                                 (mml-destroy-buffers)
10211                                 (remove-hook 'kill-buffer-hook
10212                                              'mml-destroy-buffers t)
10213                                 (kill-local-variable 'mml-buffer-list)))
10214               (gnus-summary-edit-article-done
10215                ,(or (mail-header-references gnus-current-headers) "")
10216                ,(gnus-group-read-only-p)
10217                ,gnus-summary-buffer no-highlight))))))))
10218
10219 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10220
10221 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10222                                                  no-highlight)
10223   "Make edits to the current article permanent."
10224   (interactive)
10225   (save-excursion
10226    ;; The buffer restriction contains the entire article if it exists.
10227     (when (article-goto-body)
10228       (let ((lines (count-lines (point) (point-max)))
10229             (length (- (point-max) (point)))
10230             (case-fold-search t)
10231             (body (copy-marker (point))))
10232         (goto-char (point-min))
10233         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10234           (delete-region (match-beginning 1) (match-end 1))
10235           (insert (number-to-string length)))
10236         (goto-char (point-min))
10237         (when (re-search-forward
10238                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10239           (delete-region (match-beginning 1) (match-end 1))
10240           (insert (number-to-string length)))
10241         (goto-char (point-min))
10242         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10243           (delete-region (match-beginning 1) (match-end 1))
10244           (insert (number-to-string lines))))))
10245   ;; Replace the article.
10246   (let ((buf (current-buffer)))
10247     (with-temp-buffer
10248       (insert-buffer-substring buf)
10249
10250       (if (and (not read-only)
10251                (not (gnus-request-replace-article
10252                      (cdr gnus-article-current) (car gnus-article-current)
10253                      (current-buffer) t)))
10254           (error "Couldn't replace article")
10255         ;; Update the summary buffer.
10256         (if (and references
10257                  (equal (message-tokenize-header references " ")
10258                         (message-tokenize-header
10259                          (or (message-fetch-field "references") "") " ")))
10260             ;; We only have to update this line.
10261             (save-excursion
10262               (save-restriction
10263                 (message-narrow-to-head)
10264                 (let ((head (buffer-substring-no-properties
10265                              (point-min) (point-max)))
10266                       header)
10267                   (with-temp-buffer
10268                     (insert (format "211 %d Article retrieved.\n"
10269                                     (cdr gnus-article-current)))
10270                     (insert head)
10271                     (insert ".\n")
10272                     (let ((nntp-server-buffer (current-buffer)))
10273                       (setq header (car (gnus-get-newsgroup-headers
10274                                          nil t))))
10275                     (with-current-buffer gnus-summary-buffer
10276                       (gnus-data-set-header
10277                        (gnus-data-find (cdr gnus-article-current))
10278                        header)
10279                       (gnus-summary-update-article-line
10280                        (cdr gnus-article-current) header)
10281                       (if (gnus-summary-goto-subject
10282                            (cdr gnus-article-current) nil t)
10283                           (gnus-summary-update-secondary-mark
10284                            (cdr gnus-article-current))))))))
10285           ;; Update threads.
10286           (set-buffer (or buffer gnus-summary-buffer))
10287           (gnus-summary-update-article (cdr gnus-article-current))
10288           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10289               (gnus-summary-update-secondary-mark
10290                (cdr gnus-article-current))))
10291         ;; Prettify the article buffer again.
10292         (unless no-highlight
10293           (with-current-buffer gnus-article-buffer
10294             ;;;!!! Fix this -- article should be rehighlighted.
10295             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10296             (set-buffer gnus-original-article-buffer)
10297             (gnus-request-article
10298              (cdr gnus-article-current)
10299              (car gnus-article-current) (current-buffer))))
10300         ;; Prettify the summary buffer line.
10301         (when (gnus-visual-p 'summary-highlight 'highlight)
10302           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10303
10304 (defun gnus-summary-edit-wash (key)
10305   "Perform editing command KEY in the article buffer."
10306   (interactive
10307    (list
10308     (progn
10309       (message "%s" (concat (this-command-keys) "- "))
10310       (read-char))))
10311   (message "")
10312   (gnus-summary-edit-article)
10313   (execute-kbd-macro (concat (this-command-keys) key))
10314   (gnus-article-edit-done))
10315
10316 ;;; Respooling
10317
10318 (defun gnus-summary-respool-query (&optional silent trace)
10319   "Query where the respool algorithm would put this article."
10320   (interactive)
10321   (let (gnus-mark-article-hook)
10322     (gnus-summary-select-article)
10323     (with-current-buffer gnus-original-article-buffer
10324       (let ((groups (nnmail-article-group 'identity trace)))
10325         (unless silent
10326           (if groups
10327               (message "This message would go to %s"
10328                        (mapconcat 'car groups ", "))
10329             (message "This message would go to no groups"))
10330           groups)))))
10331
10332 (defun gnus-summary-respool-trace ()
10333   "Trace where the respool algorithm would put this article.
10334 Display a buffer showing all fancy splitting patterns which matched."
10335   (interactive)
10336   (gnus-summary-respool-query nil t))
10337
10338 ;; Summary marking commands.
10339
10340 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10341   "Mark articles which has the same subject as read, and then select the next.
10342 If UNMARK is positive, remove any kind of mark.
10343 If UNMARK is negative, tick articles."
10344   (interactive "P")
10345   (when unmark
10346     (setq unmark (prefix-numeric-value unmark)))
10347   (let ((count
10348          (gnus-summary-mark-same-subject
10349           (gnus-summary-article-subject) unmark)))
10350     ;; Select next unread article.  If auto-select-same mode, should
10351     ;; select the first unread article.
10352     (gnus-summary-next-article t (and gnus-auto-select-same
10353                                       (gnus-summary-article-subject)))
10354     (gnus-message 7 "%d article%s marked as %s"
10355                   count (if (= count 1) " is" "s are")
10356                   (if unmark "unread" "read"))))
10357
10358 (defun gnus-summary-kill-same-subject (&optional unmark)
10359   "Mark articles which has the same subject as read.
10360 If UNMARK is positive, remove any kind of mark.
10361 If UNMARK is negative, tick articles."
10362   (interactive "P")
10363   (when unmark
10364     (setq unmark (prefix-numeric-value unmark)))
10365   (let ((count
10366          (gnus-summary-mark-same-subject
10367           (gnus-summary-article-subject) unmark)))
10368     ;; If marked as read, go to next unread subject.
10369     (when (null unmark)
10370       ;; Go to next unread subject.
10371       (gnus-summary-next-subject 1 t))
10372     (gnus-message 7 "%d articles are marked as %s"
10373                   count (if unmark "unread" "read"))))
10374
10375 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10376   "Mark articles with same SUBJECT as read, and return marked number.
10377 If optional argument UNMARK is positive, remove any kinds of marks.
10378 If optional argument UNMARK is negative, mark articles as unread instead."
10379   (let ((count 1))
10380     (save-excursion
10381       (cond
10382        ((null unmark)                   ; Mark as read.
10383         (while (and
10384                 (progn
10385                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10386                   (gnus-summary-show-thread) t)
10387                 (gnus-summary-find-subject subject))
10388           (setq count (1+ count))))
10389        ((> unmark 0)                    ; Tick.
10390         (while (and
10391                 (progn
10392                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10393                   (gnus-summary-show-thread) t)
10394                 (gnus-summary-find-subject subject))
10395           (setq count (1+ count))))
10396        (t                               ; Mark as unread.
10397         (while (and
10398                 (progn
10399                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10400                   (gnus-summary-show-thread) t)
10401                 (gnus-summary-find-subject subject))
10402           (setq count (1+ count)))))
10403       (gnus-set-mode-line 'summary)
10404       ;; Return the number of marked articles.
10405       count)))
10406
10407 (defun gnus-summary-mark-as-processable (n &optional unmark)
10408   "Set the process mark on the next N articles.
10409 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10410 the process mark instead.  The difference between N and the actual
10411 number of articles marked is returned."
10412   (interactive "P")
10413   (if (and (null n) (gnus-region-active-p))
10414       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10415     (setq n (prefix-numeric-value n))
10416     (let ((backward (< n 0))
10417           (n (abs n)))
10418       (while (and
10419               (> n 0)
10420               (if unmark
10421                   (gnus-summary-remove-process-mark
10422                    (gnus-summary-article-number))
10423                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10424               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10425         (setq n (1- n)))
10426       (when (/= 0 n)
10427         (gnus-message 7 "No more articles"))
10428       (gnus-summary-recenter)
10429       (gnus-summary-position-point)
10430       n)))
10431
10432 (defun gnus-summary-unmark-as-processable (n)
10433   "Remove the process mark from the next N articles.
10434 If N is negative, unmark backward instead.  The difference between N and
10435 the actual number of articles unmarked is returned."
10436   (interactive "P")
10437   (gnus-summary-mark-as-processable n t))
10438
10439 (defun gnus-summary-unmark-all-processable ()
10440   "Remove the process mark from all articles."
10441   (interactive)
10442   (save-excursion
10443     (while gnus-newsgroup-processable
10444       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10445   (gnus-summary-position-point))
10446
10447 (defun gnus-summary-add-mark (article type)
10448   "Mark ARTICLE with a mark of TYPE."
10449   (let ((vtype (car (assq type gnus-article-mark-lists)))
10450         var)
10451     (if (not vtype)
10452         (error "No such mark type: %s" type)
10453       (setq var (intern (format "gnus-newsgroup-%s" type)))
10454       (set var (cons article (symbol-value var)))
10455       (if (memq type '(processable cached replied forwarded recent saved))
10456           (gnus-summary-update-secondary-mark article)
10457         ;;; !!! This is bogus.  We should find out what primary
10458         ;;; !!! mark we want to set.
10459         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10460
10461 (defun gnus-summary-mark-as-expirable (n)
10462   "Mark N articles forward as expirable.
10463 If N is negative, mark backward instead.  The difference between N and
10464 the actual number of articles marked is returned."
10465   (interactive "p")
10466   (gnus-summary-mark-forward n gnus-expirable-mark))
10467
10468 (defun gnus-summary-mark-as-spam (n)
10469   "Mark N articles forward as spam.
10470 If N is negative, mark backward instead.  The difference between N and
10471 the actual number of articles marked is returned."
10472   (interactive "p")
10473   (gnus-summary-mark-forward n gnus-spam-mark))
10474
10475 (defun gnus-summary-mark-article-as-replied (article)
10476   "Mark ARTICLE as replied to and update the summary line.
10477 ARTICLE can also be a list of articles."
10478   (interactive (list (gnus-summary-article-number)))
10479   (let ((articles (if (listp article) article (list article))))
10480     (dolist (article articles)
10481       (unless (numberp article)
10482         (error "%s is not a number" article))
10483       (push article gnus-newsgroup-replied)
10484       (let ((inhibit-read-only t))
10485         (when (gnus-summary-goto-subject article nil t)
10486           (gnus-summary-update-secondary-mark article))))))
10487
10488 (defun gnus-summary-mark-article-as-forwarded (article)
10489   "Mark ARTICLE as forwarded and update the summary line.
10490 ARTICLE can also be a list of articles."
10491   (let ((articles (if (listp article) article (list article))))
10492     (dolist (article articles)
10493       (push article gnus-newsgroup-forwarded)
10494       (let ((inhibit-read-only t))
10495         (when (gnus-summary-goto-subject article nil t)
10496           (gnus-summary-update-secondary-mark article))))))
10497
10498 (defun gnus-summary-set-bookmark (article)
10499   "Set a bookmark in current article."
10500   (interactive (list (gnus-summary-article-number)))
10501   (when (or (not (get-buffer gnus-article-buffer))
10502             (not gnus-current-article)
10503             (not gnus-article-current)
10504             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10505     (error "No current article selected"))
10506   ;; Remove old bookmark, if one exists.
10507   (gnus-pull article gnus-newsgroup-bookmarks)
10508   ;; Set the new bookmark, which is on the form
10509   ;; (article-number . line-number-in-body).
10510   (push
10511    (cons article
10512          (with-current-buffer gnus-article-buffer
10513            (count-lines
10514             (min (point)
10515                  (save-excursion
10516                    (article-goto-body)
10517                    (point)))
10518             (point))))
10519    gnus-newsgroup-bookmarks)
10520   (gnus-message 6 "A bookmark has been added to the current article."))
10521
10522 (defun gnus-summary-remove-bookmark (article)
10523   "Remove the bookmark from the current article."
10524   (interactive (list (gnus-summary-article-number)))
10525   ;; Remove old bookmark, if one exists.
10526   (if (not (assq article gnus-newsgroup-bookmarks))
10527       (gnus-message 6 "No bookmark in current article.")
10528     (gnus-pull article gnus-newsgroup-bookmarks)
10529     (gnus-message 6 "Removed bookmark.")))
10530
10531 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10532 (defun gnus-summary-mark-as-dormant (n)
10533   "Mark N articles forward as dormant.
10534 If N is negative, mark backward instead.  The difference between N and
10535 the actual number of articles marked is returned."
10536   (interactive "p")
10537   (gnus-summary-mark-forward n gnus-dormant-mark))
10538
10539 (defun gnus-summary-set-process-mark (article)
10540   "Set the process mark on ARTICLE and update the summary line."
10541   (setq gnus-newsgroup-processable
10542         (cons article
10543               (delq article gnus-newsgroup-processable)))
10544   (when (gnus-summary-goto-subject article)
10545     (gnus-summary-show-thread)
10546     (gnus-summary-goto-subject article)
10547     (gnus-summary-update-secondary-mark article)))
10548
10549 (defun gnus-summary-remove-process-mark (&rest articles)
10550   "Remove the process mark from ARTICLES and update the summary line."
10551   (dolist (article articles)
10552     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10553     (when (gnus-summary-goto-subject article)
10554       (gnus-summary-show-thread)
10555       (gnus-summary-goto-subject article)
10556       (gnus-summary-update-secondary-mark article)))
10557   t)
10558
10559 (defun gnus-summary-set-saved-mark (article)
10560   "Set the process mark on ARTICLE and update the summary line."
10561   (push article gnus-newsgroup-saved)
10562   (when (gnus-summary-goto-subject article)
10563     (gnus-summary-update-secondary-mark article)))
10564
10565 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10566   "Mark N articles as read forwards.
10567 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10568 The difference between N and the actual number of articles marked is
10569 returned.
10570 If NO-EXPIRE, auto-expiry will be inhibited."
10571   (interactive "p")
10572   (gnus-summary-show-thread)
10573   (let ((backward (< n 0))
10574         (gnus-summary-goto-unread
10575          (and gnus-summary-goto-unread
10576               (not (eq gnus-summary-goto-unread 'never))
10577               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10578                                     gnus-ticked-mark gnus-dormant-mark)))))
10579         (n (abs n))
10580         (mark (or mark gnus-del-mark)))
10581     (while (and (> n 0)
10582                 (gnus-summary-mark-article nil mark no-expire)
10583                 (zerop (gnus-summary-next-subject
10584                         (if backward -1 1)
10585                         (and gnus-summary-goto-unread
10586                              (not (eq gnus-summary-goto-unread 'never)))
10587                         t)))
10588       (setq n (1- n)))
10589     (when (/= 0 n)
10590       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10591     (gnus-summary-recenter)
10592     (gnus-summary-position-point)
10593     (gnus-set-mode-line 'summary)
10594     n))
10595
10596 (defun gnus-summary-mark-article-as-read (mark)
10597   "Mark the current article quickly as read with MARK."
10598   (let ((article (gnus-summary-article-number)))
10599     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10600     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10601     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10602     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10603     (push (cons article mark) gnus-newsgroup-reads)
10604     ;; Possibly remove from cache, if that is used.
10605     (when gnus-use-cache
10606       (gnus-cache-enter-remove-article article))
10607     ;; Allow the backend to change the mark.
10608     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10609     ;; Check for auto-expiry.
10610     (when (and gnus-newsgroup-auto-expire
10611                (memq mark gnus-auto-expirable-marks))
10612       (setq mark gnus-expirable-mark)
10613       ;; Let the backend know about the mark change.
10614       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10615       (push article gnus-newsgroup-expirable))
10616     ;; Set the mark in the buffer.
10617     (gnus-summary-update-mark mark 'unread)
10618     t))
10619
10620 (defun gnus-summary-mark-article-as-unread (mark)
10621   "Mark the current article quickly as unread with MARK."
10622   (let* ((article (gnus-summary-article-number))
10623          (old-mark (gnus-summary-article-mark article)))
10624     ;; Allow the backend to change the mark.
10625     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10626     (if (eq mark old-mark)
10627         t
10628       (if (<= article 0)
10629           (progn
10630             (gnus-error 1 "Can't mark negative article numbers")
10631             nil)
10632         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10633         (setq gnus-newsgroup-spam-marked
10634               (delq article gnus-newsgroup-spam-marked))
10635         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10636         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10637         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10638         (cond ((= mark gnus-ticked-mark)
10639                (setq gnus-newsgroup-marked
10640                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10641                                               article)))
10642               ((= mark gnus-spam-mark)
10643                (setq gnus-newsgroup-spam-marked
10644                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10645                                               article)))
10646               ((= mark gnus-dormant-mark)
10647                (setq gnus-newsgroup-dormant
10648                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10649                                               article)))
10650               (t
10651                (setq gnus-newsgroup-unreads
10652                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10653                                               article))))
10654         (gnus-pull article gnus-newsgroup-reads)
10655
10656         ;; See whether the article is to be put in the cache.
10657         (and gnus-use-cache
10658              (vectorp (gnus-summary-article-header article))
10659              (save-excursion
10660                (gnus-cache-possibly-enter-article
10661                 gnus-newsgroup-name article
10662                 (= mark gnus-ticked-mark)
10663                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10664
10665         ;; Fix the mark.
10666         (gnus-summary-update-mark mark 'unread)
10667         t))))
10668
10669 (defun gnus-summary-mark-article (&optional article mark no-expire)
10670   "Mark ARTICLE with MARK.  MARK can be any character.
10671 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10672 `??' (dormant) and `?E' (expirable).
10673 If MARK is nil, then the default character `?r' is used.
10674 If ARTICLE is nil, then the article on the current line will be
10675 marked.
10676 If NO-EXPIRE, auto-expiry will be inhibited."
10677   ;; The mark might be a string.
10678   (when (stringp mark)
10679     (setq mark (aref mark 0)))
10680   ;; If no mark is given, then we check auto-expiring.
10681   (when (null mark)
10682     (setq mark gnus-del-mark))
10683   (when (and (not no-expire)
10684              gnus-newsgroup-auto-expire
10685              (memq mark gnus-auto-expirable-marks))
10686     (setq mark gnus-expirable-mark))
10687   (let ((article (or article (gnus-summary-article-number)))
10688         (old-mark (gnus-summary-article-mark article)))
10689     ;; Allow the backend to change the mark.
10690     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10691     (if (eq mark old-mark)
10692         t
10693       (unless article
10694         (error "No article on current line"))
10695       (if (not (if (or (= mark gnus-unread-mark)
10696                        (= mark gnus-ticked-mark)
10697                        (= mark gnus-spam-mark)
10698                        (= mark gnus-dormant-mark))
10699                    (gnus-mark-article-as-unread article mark)
10700                  (gnus-mark-article-as-read article mark)))
10701           t
10702         ;; See whether the article is to be put in the cache.
10703         (and gnus-use-cache
10704              (not (= mark gnus-canceled-mark))
10705              (vectorp (gnus-summary-article-header article))
10706              (save-excursion
10707                (gnus-cache-possibly-enter-article
10708                 gnus-newsgroup-name article
10709                 (= mark gnus-ticked-mark)
10710                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10711
10712         (when (gnus-summary-goto-subject article nil t)
10713           (let ((inhibit-read-only t))
10714             (gnus-summary-show-thread)
10715             ;; Fix the mark.
10716             (gnus-summary-update-mark mark 'unread)
10717             t))))))
10718
10719 (defun gnus-summary-update-secondary-mark (article)
10720   "Update the secondary (read, process, cache) mark."
10721   (gnus-summary-update-mark
10722    (cond ((memq article gnus-newsgroup-processable)
10723           gnus-process-mark)
10724          ((memq article gnus-newsgroup-cached)
10725           gnus-cached-mark)
10726          ((memq article gnus-newsgroup-replied)
10727           gnus-replied-mark)
10728          ((memq article gnus-newsgroup-forwarded)
10729           gnus-forwarded-mark)
10730          ((memq article gnus-newsgroup-saved)
10731           gnus-saved-mark)
10732          ((memq article gnus-newsgroup-recent)
10733           gnus-recent-mark)
10734          ((memq article gnus-newsgroup-unseen)
10735           gnus-unseen-mark)
10736          (t gnus-no-mark))
10737    'replied)
10738   (when (gnus-visual-p 'summary-highlight 'highlight)
10739     (gnus-run-hooks 'gnus-summary-update-hook))
10740   t)
10741
10742 (defun gnus-summary-update-download-mark (article)
10743   "Update the download mark."
10744   (gnus-summary-update-mark
10745    (cond ((memq article gnus-newsgroup-undownloaded)
10746           gnus-undownloaded-mark)
10747          (gnus-newsgroup-agentized
10748           gnus-downloaded-mark)
10749          (t
10750           gnus-no-mark))
10751    'download)
10752   (gnus-summary-update-line t)
10753   t)
10754
10755 (defun gnus-summary-update-mark (mark type)
10756   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10757         (inhibit-read-only t))
10758     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10759     (when forward
10760       (when (looking-at "\r")
10761         (incf forward))
10762       (when (<= (+ forward (point)) (point-max))
10763         ;; Go to the right position on the line.
10764         (goto-char (+ forward (point)))
10765         ;; Replace the old mark with the new mark.
10766         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10767         ;; Optionally update the marks by some user rule.
10768         (when (eq type 'unread)
10769           (gnus-data-set-mark
10770            (gnus-data-find (gnus-summary-article-number)) mark)
10771           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10772
10773 (defun gnus-mark-article-as-read (article &optional mark)
10774   "Enter ARTICLE in the pertinent lists and remove it from others."
10775   ;; Make the article expirable.
10776   (let ((mark (or mark gnus-del-mark)))
10777     (setq gnus-newsgroup-expirable
10778           (if (= mark gnus-expirable-mark)
10779               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10780             (delq article gnus-newsgroup-expirable)))
10781     ;; Remove from unread and marked lists.
10782     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10783     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10784     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10785     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10786     (push (cons article mark) gnus-newsgroup-reads)
10787     ;; Possibly remove from cache, if that is used.
10788     (when gnus-use-cache
10789       (gnus-cache-enter-remove-article article))
10790     t))
10791
10792 (defun gnus-mark-article-as-unread (article &optional mark)
10793   "Enter ARTICLE in the pertinent lists and remove it from others."
10794   (let ((mark (or mark gnus-ticked-mark)))
10795     (if (<= article 0)
10796         (progn
10797           (gnus-error 1 "Can't mark negative article numbers")
10798           nil)
10799       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10800             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10801             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10802             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10803             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10804
10805       ;; Unsuppress duplicates?
10806       (when gnus-suppress-duplicates
10807         (gnus-dup-unsuppress-article article))
10808
10809       (cond ((= mark gnus-ticked-mark)
10810              (setq gnus-newsgroup-marked
10811                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10812             ((= mark gnus-spam-mark)
10813              (setq gnus-newsgroup-spam-marked
10814                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10815                                             article)))
10816             ((= mark gnus-dormant-mark)
10817              (setq gnus-newsgroup-dormant
10818                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10819             (t
10820              (setq gnus-newsgroup-unreads
10821                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10822       (gnus-pull article gnus-newsgroup-reads)
10823       t)))
10824
10825 (defalias 'gnus-summary-mark-as-unread-forward
10826   'gnus-summary-tick-article-forward)
10827 (make-obsolete 'gnus-summary-mark-as-unread-forward
10828                'gnus-summary-tick-article-forward "Emacs 20.4")
10829 (defun gnus-summary-tick-article-forward (n)
10830   "Tick N articles forwards.
10831 If N is negative, tick backwards instead.
10832 The difference between N and the number of articles ticked is returned."
10833   (interactive "p")
10834   (gnus-summary-mark-forward n gnus-ticked-mark))
10835
10836 (defalias 'gnus-summary-mark-as-unread-backward
10837   'gnus-summary-tick-article-backward)
10838 (make-obsolete 'gnus-summary-mark-as-unread-backward
10839                'gnus-summary-tick-article-backward "Emacs 20.4")
10840 (defun gnus-summary-tick-article-backward (n)
10841   "Tick N articles backwards.
10842 The difference between N and the number of articles ticked is returned."
10843   (interactive "p")
10844   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10845
10846 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10847 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10848 (defun gnus-summary-tick-article (&optional article clear-mark)
10849   "Mark current article as unread.
10850 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10851 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10852   (interactive)
10853   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10854                                        gnus-ticked-mark)))
10855
10856 (defun gnus-summary-mark-as-read-forward (n)
10857   "Mark N articles as read forwards.
10858 If N is negative, mark backwards instead.
10859 The difference between N and the actual number of articles marked is
10860 returned."
10861   (interactive "p")
10862   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10863
10864 (defun gnus-summary-mark-as-read-backward (n)
10865   "Mark the N articles as read backwards.
10866 The difference between N and the actual number of articles marked is
10867 returned."
10868   (interactive "p")
10869   (gnus-summary-mark-forward
10870    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10871
10872 (defun gnus-summary-mark-as-read (&optional article mark)
10873   "Mark current article as read.
10874 ARTICLE specifies the article to be marked as read.
10875 MARK specifies a string to be inserted at the beginning of the line."
10876   (gnus-summary-mark-article article mark))
10877
10878 (defun gnus-summary-clear-mark-forward (n)
10879   "Clear marks from N articles forward.
10880 If N is negative, clear backward instead.
10881 The difference between N and the number of marks cleared is returned."
10882   (interactive "p")
10883   (gnus-summary-mark-forward n gnus-unread-mark))
10884
10885 (defun gnus-summary-clear-mark-backward (n)
10886   "Clear marks from N articles backward.
10887 The difference between N and the number of marks cleared is returned."
10888   (interactive "p")
10889   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10890
10891 (defun gnus-summary-mark-unread-as-read ()
10892   "Intended to be used by `gnus-mark-article-hook'."
10893   (when (memq gnus-current-article gnus-newsgroup-unreads)
10894     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10895
10896 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10897   "Intended to be used by `gnus-mark-article-hook'."
10898   (let ((mark (gnus-summary-article-mark)))
10899     (when (or (gnus-unread-mark-p mark)
10900               (gnus-read-mark-p mark))
10901       (gnus-summary-mark-article gnus-current-article
10902                                  (or new-mark gnus-read-mark)))))
10903
10904 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10905   "Intended to be used by `gnus-mark-article-hook'."
10906   (let ((mark (gnus-summary-article-mark)))
10907     (when (or (gnus-unread-mark-p mark)
10908               (gnus-read-mark-p mark))
10909       (gnus-summary-mark-article (gnus-summary-article-number)
10910                                  (or new-mark gnus-read-mark)))))
10911
10912 (defun gnus-summary-mark-unread-as-ticked ()
10913   "Intended to be used by `gnus-mark-article-hook'."
10914   (when (memq gnus-current-article gnus-newsgroup-unreads)
10915     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10916
10917 (defun gnus-summary-mark-region-as-read (point mark all)
10918   "Mark all unread articles between point and mark as read.
10919 If given a prefix, mark all articles between point and mark as read,
10920 even ticked and dormant ones."
10921   (interactive "r\nP")
10922   (save-excursion
10923     (let (article)
10924       (goto-char point)
10925       (beginning-of-line)
10926       (while (and
10927               (< (point) mark)
10928               (progn
10929                 (when (or all
10930                           (memq (setq article (gnus-summary-article-number))
10931                                 gnus-newsgroup-unreads))
10932                   (gnus-summary-mark-article article gnus-del-mark))
10933                 t)
10934               (gnus-summary-find-next))))))
10935
10936 (defun gnus-summary-mark-below (score mark)
10937   "Mark articles with score less than SCORE with MARK."
10938   (interactive "P\ncMark: ")
10939   (setq score (if score
10940                   (prefix-numeric-value score)
10941                 (or gnus-summary-default-score 0)))
10942   (with-current-buffer gnus-summary-buffer
10943     (goto-char (point-min))
10944     (while
10945         (progn
10946           (and (< (gnus-summary-article-score) score)
10947                (gnus-summary-mark-article nil mark))
10948           (gnus-summary-find-next)))))
10949
10950 (defun gnus-summary-kill-below (&optional score)
10951   "Mark articles with score below SCORE as read."
10952   (interactive "P")
10953   (gnus-summary-mark-below score gnus-killed-mark))
10954
10955 (defun gnus-summary-clear-above (&optional score)
10956   "Clear all marks from articles with score above SCORE."
10957   (interactive "P")
10958   (gnus-summary-mark-above score gnus-unread-mark))
10959
10960 (defun gnus-summary-tick-above (&optional score)
10961   "Tick all articles with score above SCORE."
10962   (interactive "P")
10963   (gnus-summary-mark-above score gnus-ticked-mark))
10964
10965 (defun gnus-summary-mark-above (score mark)
10966   "Mark articles with score over 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 (and (progn
10974                   (when (> (gnus-summary-article-score) score)
10975                     (gnus-summary-mark-article nil mark))
10976                   t)
10977                 (gnus-summary-find-next)))))
10978
10979 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10980 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10981 (defun gnus-summary-limit-include-expunged (&optional no-error)
10982   "Display all the hidden articles that were expunged for low scores."
10983   (interactive)
10984   (let ((inhibit-read-only t))
10985     (let ((scored gnus-newsgroup-scored)
10986           headers h)
10987       (while scored
10988         (unless (gnus-summary-article-header (caar scored))
10989           (and (setq h (gnus-number-to-header (caar scored)))
10990                (< (cdar scored) gnus-summary-expunge-below)
10991                (push h headers)))
10992         (setq scored (cdr scored)))
10993       (if (not headers)
10994           (when (not no-error)
10995             (error "No expunged articles hidden"))
10996         (goto-char (point-min))
10997         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10998         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10999         (dolist (x headers)
11000           (push (mail-header-number x) gnus-newsgroup-limit))
11001         (gnus-summary-prepare-unthreaded (nreverse headers))
11002         (goto-char (point-min))
11003         (gnus-summary-position-point)
11004         t))))
11005
11006 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11007   "Mark all unread articles in this newsgroup as read.
11008 If prefix argument ALL is non-nil, ticked and dormant articles will
11009 also be marked as read.
11010 If QUIETLY is non-nil, no questions will be asked.
11011
11012 If TO-HERE is non-nil, it should be a point in the buffer.  All
11013 articles before (after, if REVERSE is set) this point will be marked
11014 as read.
11015
11016 Note that this function will only catch up the unread article
11017 in the current summary buffer limitation.
11018
11019 The number of articles marked as read is returned."
11020   (interactive "P")
11021   (prog1
11022       (save-excursion
11023         (when (or quietly
11024                   (not gnus-interactive-catchup) ;Without confirmation?
11025                   gnus-expert-user
11026                   (gnus-y-or-n-p
11027                    (if all
11028                        "Mark absolutely all articles as read? "
11029                      "Mark all unread articles as read? ")))
11030           (if (and not-mark
11031                    (not gnus-newsgroup-adaptive)
11032                    (not gnus-newsgroup-auto-expire)
11033                    (not gnus-suppress-duplicates)
11034                    (or (not gnus-use-cache)
11035                        (eq gnus-use-cache 'passive)))
11036               (progn
11037                 (when all
11038                   (setq gnus-newsgroup-marked nil
11039                         gnus-newsgroup-spam-marked nil
11040                         gnus-newsgroup-dormant nil))
11041                 (setq gnus-newsgroup-unreads
11042                       (gnus-sorted-nunion
11043                        (gnus-sorted-intersection gnus-newsgroup-unreads
11044                                                  gnus-newsgroup-downloadable)
11045                        (gnus-sorted-difference gnus-newsgroup-unfetched
11046                                                gnus-newsgroup-cached))))
11047             ;; We actually mark all articles as canceled, which we
11048             ;; have to do when using auto-expiry or adaptive scoring.
11049             (gnus-summary-show-all-threads)
11050             (if (and to-here reverse)
11051                 (progn
11052                   (goto-char to-here)
11053                   (gnus-summary-mark-current-read-and-unread-as-read
11054                    gnus-catchup-mark)
11055                   (while (gnus-summary-find-next (not all))
11056                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11057               (when (gnus-summary-first-subject (not all))
11058                 (while (and
11059                         (if to-here (< (point) to-here) t)
11060                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11061                         (gnus-summary-find-next (not all))))))
11062             (gnus-set-mode-line 'summary))
11063           t))
11064     (gnus-summary-position-point)))
11065
11066 (defun gnus-summary-catchup-to-here (&optional all)
11067   "Mark all unticked articles before the current one as read.
11068 If ALL is non-nil, also mark ticked and dormant articles as read."
11069   (interactive "P")
11070   (save-excursion
11071     (gnus-save-hidden-threads
11072       (let ((beg (point)))
11073         ;; We check that there are unread articles.
11074         (when (or all (gnus-summary-find-prev))
11075           (gnus-summary-catchup all t beg)))))
11076   (gnus-summary-position-point))
11077
11078 (defun gnus-summary-catchup-from-here (&optional all)
11079   "Mark all unticked articles after (and including) the current one as read.
11080 If ALL is non-nil, also mark ticked and dormant articles as read."
11081   (interactive "P")
11082   (save-excursion
11083     (gnus-save-hidden-threads
11084       (let ((beg (point)))
11085         ;; We check that there are unread articles.
11086         (when (or all (gnus-summary-find-next))
11087           (gnus-summary-catchup all t beg nil t)))))
11088   (gnus-summary-position-point))
11089
11090 (defun gnus-summary-catchup-all (&optional quietly)
11091   "Mark all articles in this newsgroup as read.
11092 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11093 instead, which marks only unread articles as read."
11094   (interactive "P")
11095   (gnus-summary-catchup t quietly))
11096
11097 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11098   "Mark all unread articles in this group as read, then exit.
11099 If prefix argument ALL is non-nil, all articles are marked as read.
11100 If QUIETLY is non-nil, no questions will be asked."
11101   (interactive "P")
11102   (when (gnus-summary-catchup all quietly nil 'fast)
11103     ;; Select next newsgroup or exit.
11104     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11105              (eq gnus-auto-select-next 'quietly))
11106         (gnus-summary-next-group nil)
11107       (gnus-summary-exit))))
11108
11109 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11110   "Mark all articles in this newsgroup as read, and then exit.
11111 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11112 instead, which marks only unread articles as read."
11113   (interactive "P")
11114   (gnus-summary-catchup-and-exit t quietly))
11115
11116 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11117   "Mark all articles in this group as read and select the next group.
11118 If given a prefix, mark all articles, unread as well as ticked, as
11119 read."
11120   (interactive "P")
11121   (save-excursion
11122     (gnus-summary-catchup all))
11123   (gnus-summary-next-group))
11124
11125 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11126   "Mark all articles in this group as read and select the previous group.
11127 If given a prefix, mark all articles, unread as well as ticked, as
11128 read."
11129   (interactive "P")
11130   (save-excursion
11131     (gnus-summary-catchup all))
11132   (gnus-summary-next-group nil nil t))
11133
11134 ;;;
11135 ;;; with article
11136 ;;;
11137
11138 (defmacro gnus-with-article (article &rest forms)
11139   "Select ARTICLE and perform FORMS in the original article buffer.
11140 Then replace the article with the result."
11141   `(progn
11142      ;; We don't want the article to be marked as read.
11143      (let (gnus-mark-article-hook)
11144        (gnus-summary-select-article t t nil ,article))
11145      (set-buffer gnus-original-article-buffer)
11146      ,@forms
11147      (if (not (gnus-check-backend-function
11148                'request-replace-article (car gnus-article-current)))
11149          (gnus-message 5 "Read-only group; not replacing")
11150        (unless (gnus-request-replace-article
11151                 ,article (car gnus-article-current)
11152                 (current-buffer) t)
11153          (error "Couldn't replace article")))
11154      ;; The cache and backlog have to be flushed somewhat.
11155      (when gnus-keep-backlog
11156        (gnus-backlog-remove-article
11157         (car gnus-article-current) (cdr gnus-article-current)))
11158      (when gnus-use-cache
11159        (gnus-cache-update-article
11160         (car gnus-article-current) (cdr gnus-article-current)))))
11161
11162 (put 'gnus-with-article 'lisp-indent-function 1)
11163 (put 'gnus-with-article 'edebug-form-spec '(form body))
11164
11165 ;; Thread-based commands.
11166
11167 (defun gnus-summary-articles-in-thread (&optional article)
11168   "Return a list of all articles in the current thread.
11169 If ARTICLE is non-nil, return all articles in the thread that starts
11170 with that article."
11171   (let* ((article (or article (gnus-summary-article-number)))
11172          (data (gnus-data-find-list article))
11173          (top-level (gnus-data-level (car data)))
11174          (top-subject
11175           (cond ((null gnus-thread-operation-ignore-subject)
11176                  (gnus-simplify-subject-re
11177                   (mail-header-subject (gnus-data-header (car data)))))
11178                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11179                  (gnus-simplify-subject-fuzzy
11180                   (mail-header-subject (gnus-data-header (car data)))))
11181                 (t nil)))
11182          (end-point (save-excursion
11183                       (if (gnus-summary-go-to-next-thread)
11184                           (point) (point-max))))
11185          articles)
11186     (while (and data
11187                 (< (gnus-data-pos (car data)) end-point))
11188       (when (or (not top-subject)
11189                 (string= top-subject
11190                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11191                              (gnus-simplify-subject-fuzzy
11192                               (mail-header-subject
11193                                (gnus-data-header (car data))))
11194                            (gnus-simplify-subject-re
11195                             (mail-header-subject
11196                              (gnus-data-header (car data)))))))
11197         (push (gnus-data-number (car data)) articles))
11198       (unless (and (setq data (cdr data))
11199                    (> (gnus-data-level (car data)) top-level))
11200         (setq data nil)))
11201     ;; Return the list of articles.
11202     (nreverse articles)))
11203
11204 (defun gnus-summary-rethread-current ()
11205   "Rethread the thread the current article is part of."
11206   (interactive)
11207   (let* ((gnus-show-threads t)
11208          (article (gnus-summary-article-number))
11209          (id (mail-header-id (gnus-summary-article-header)))
11210          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11211     (unless id
11212       (error "No article on the current line"))
11213     (gnus-rebuild-thread id)
11214     (gnus-summary-goto-subject article)))
11215
11216 (defun gnus-summary-reparent-thread ()
11217   "Make the current article child of the marked (or previous) article.
11218
11219 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11220 is non-nil or the Subject: of both articles are the same."
11221   (interactive)
11222   (unless (not (gnus-group-read-only-p))
11223     (error "The current newsgroup does not support article editing"))
11224   (unless (<= (length gnus-newsgroup-processable) 1)
11225     (error "No more than one article may be marked"))
11226   (let ((child (gnus-summary-article-number))
11227         ;; First grab the marked article, otherwise one line up.
11228         (parent (if (not (null gnus-newsgroup-processable))
11229                     (car gnus-newsgroup-processable)
11230                   (save-excursion
11231                     (if (eq (forward-line -1) 0)
11232                         (gnus-summary-article-number)
11233                       (error "Beginning of summary buffer"))))))
11234     (gnus-summary-reparent-children parent (list child))))
11235
11236 (defun gnus-summary-reparent-children (parent children)
11237   "Make PARENT the parent of CHILDREN.
11238 When called interactively, PARENT is the current article and CHILDREN
11239 are the process-marked articles."
11240   (interactive
11241    (list (gnus-summary-article-number)
11242          (gnus-summary-work-articles nil)))
11243   (dolist (child children)
11244     (save-window-excursion
11245       (let ((gnus-article-buffer " *reparent*"))
11246         (unless (not (eq parent child))
11247           (error "An article may not be self-referential"))
11248         (let ((message-id (mail-header-id
11249                            (gnus-summary-article-header parent))))
11250           (unless (and message-id (not (equal message-id "")))
11251             (error "No message-id in desired parent"))
11252           (gnus-with-article child
11253             (save-restriction
11254               (goto-char (point-min))
11255               (message-narrow-to-head)
11256               (if (re-search-forward "^References: " nil t)
11257                   (progn
11258                     (re-search-forward "^[^ \t]" nil t)
11259                     (forward-line -1)
11260                     (end-of-line)
11261                     (insert " " message-id))
11262                 (insert "References: " message-id "\n"))))
11263           (set-buffer gnus-summary-buffer)
11264           (gnus-summary-unmark-all-processable)
11265           (gnus-summary-update-article child)
11266           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11267             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11268           (gnus-summary-rethread-current)
11269           (gnus-message 3 "Article %d is now the child of article %d"
11270                         child parent))))))
11271
11272 (defun gnus-summary-toggle-threads (&optional arg)
11273   "Toggle showing conversation threads.
11274 If ARG is positive number, turn showing conversation threads on."
11275   (interactive "P")
11276   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11277     (setq gnus-show-threads
11278           (if (null arg) (not gnus-show-threads)
11279             (> (prefix-numeric-value arg) 0)))
11280     (gnus-summary-prepare)
11281     (gnus-summary-goto-subject current)
11282     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11283     (gnus-summary-position-point)))
11284
11285 (eval-and-compile
11286   (if (fboundp 'remove-overlays)
11287       (defalias 'gnus-remove-overlays 'remove-overlays)
11288     (defun gnus-remove-overlays (beg end name val)
11289       "Clear BEG and END of overlays whose property NAME has value VAL.
11290 For compatibility with Emacs 21 and XEmacs."
11291       (dolist (ov (gnus-overlays-in beg end))
11292         (when (eq (gnus-overlay-get ov name) val)
11293           (gnus-delete-overlay ov))))))
11294
11295 (defun gnus-summary-show-all-threads ()
11296   "Show all threads."
11297   (interactive)
11298   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11299   (gnus-summary-position-point))
11300
11301 (defun gnus-summary-show-thread ()
11302   "Show thread subtrees.
11303 Returns nil if no thread was there to be shown."
11304   (interactive)
11305   (let* ((orig (point))
11306          (end (point-at-eol))
11307          ;; Leave point at bol
11308          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11309          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11310                 (if (fboundp 'next-single-char-property-change)
11311                     (or (next-single-char-property-change end 'invisible)
11312                         (point-max))
11313                   (while (progn
11314                            (end-of-line 2)
11315                            (and (not (eobp))
11316                                 (eq (get-char-property (point) 'invisible)
11317                                     'gnus-sum))))
11318                   (point)))))
11319     (when eoi
11320       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11321       (goto-char orig)
11322       (gnus-summary-position-point)
11323       eoi)))
11324
11325 (defun gnus-summary-maybe-hide-threads ()
11326   "If requested, hide the threads that should be hidden."
11327   (when (and gnus-show-threads
11328              gnus-thread-hide-subtree)
11329     (gnus-summary-hide-all-threads
11330      (if (or (consp gnus-thread-hide-subtree)
11331              (functionp gnus-thread-hide-subtree))
11332          (gnus-make-predicate gnus-thread-hide-subtree)
11333        nil))))
11334
11335 ;;; Hiding predicates.
11336
11337 (defun gnus-article-unread-p (header)
11338   (memq (mail-header-number header) gnus-newsgroup-unreads))
11339
11340 (defun gnus-article-unseen-p (header)
11341   (memq (mail-header-number header) gnus-newsgroup-unseen))
11342
11343 (defun gnus-map-articles (predicate articles)
11344   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11345   (apply 'gnus-or (mapcar predicate
11346                           (mapcar (lambda (number)
11347                                     (gnus-summary-article-header number))
11348                                   articles))))
11349
11350 (defun gnus-summary-hide-all-threads (&optional predicate)
11351   "Hide all thread subtrees.
11352 If PREDICATE is supplied, threads that satisfy this predicate
11353 will not be hidden."
11354   (interactive)
11355   (save-excursion
11356     (goto-char (point-min))
11357     (let ((end nil))
11358       (while (not end)
11359         (when (or (not predicate)
11360                   (gnus-map-articles
11361                    predicate (gnus-summary-article-children)))
11362             (gnus-summary-hide-thread))
11363         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11364   (gnus-summary-position-point))
11365
11366 (defun gnus-summary-hide-thread ()
11367   "Hide thread subtrees.
11368 If PREDICATE is supplied, threads that satisfy this predicate
11369 will not be hidden.
11370 Returns nil if no threads were there to be hidden."
11371   (interactive)
11372   (let ((start (point))
11373         (starteol (line-end-position))
11374         (article (gnus-summary-article-number)))
11375     (goto-char start)
11376     ;; Go forward until either the buffer ends or the subthread ends.
11377     (when (and (not (eobp))
11378                (or (zerop (gnus-summary-next-thread 1 t))
11379                    (goto-char (point-max))))
11380       (if (and (> (point) start)
11381                ;; FIXME: this should actually search for a non-invisible \n.
11382                (search-backward "\n" start t))
11383           (progn
11384             (when (> (point) starteol)
11385               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11386               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11387                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11388                 (gnus-overlay-put ol 'evaporate t)))
11389             (gnus-summary-goto-subject article))
11390         (goto-char start)
11391         nil))))
11392
11393 (defun gnus-summary-go-to-next-thread (&optional previous)
11394   "Go to the same level (or less) next thread.
11395 If PREVIOUS is non-nil, go to previous thread instead.
11396 Return the article number moved to, or nil if moving was impossible."
11397   (let ((level (gnus-summary-thread-level))
11398         (way (if previous -1 1))
11399         (beg (point)))
11400     (forward-line way)
11401     (while (and (not (eobp))
11402                 (< level (gnus-summary-thread-level)))
11403       (forward-line way))
11404     (if (eobp)
11405         (progn
11406           (goto-char beg)
11407           nil)
11408       (setq beg (point))
11409       (prog1
11410           (gnus-summary-article-number)
11411         (goto-char beg)))))
11412
11413 (defun gnus-summary-next-thread (n &optional silent)
11414   "Go to the same level next N'th thread.
11415 If N is negative, search backward instead.
11416 Returns the difference between N and the number of skips actually
11417 done.
11418
11419 If SILENT, don't output messages."
11420   (interactive "p")
11421   (let ((backward (< n 0))
11422         (n (abs n)))
11423     (while (and (> n 0)
11424                 (gnus-summary-go-to-next-thread backward))
11425       (decf n))
11426     (unless silent
11427       (gnus-summary-position-point))
11428     (when (and (not silent) (/= 0 n))
11429       (gnus-message 7 "No more threads"))
11430     n))
11431
11432 (defun gnus-summary-prev-thread (n)
11433   "Go to the same level previous N'th thread.
11434 Returns the difference between N and the number of skips actually
11435 done."
11436   (interactive "p")
11437   (gnus-summary-next-thread (- n)))
11438
11439 (defun gnus-summary-go-down-thread ()
11440   "Go down one level in the current thread."
11441   (let ((children (gnus-summary-article-children)))
11442     (when children
11443       (gnus-summary-goto-subject (car children)))))
11444
11445 (defun gnus-summary-go-up-thread ()
11446   "Go up one level in the current thread."
11447   (let ((parent (gnus-summary-article-parent)))
11448     (when parent
11449       (gnus-summary-goto-subject parent))))
11450
11451 (defun gnus-summary-down-thread (n)
11452   "Go down thread N steps.
11453 If N is negative, go up instead.
11454 Returns the difference between N and how many steps down that were
11455 taken."
11456   (interactive "p")
11457   (let ((up (< n 0))
11458         (n (abs n)))
11459     (while (and (> n 0)
11460                 (if up (gnus-summary-go-up-thread)
11461                   (gnus-summary-go-down-thread)))
11462       (setq n (1- n)))
11463     (gnus-summary-position-point)
11464     (when (/= 0 n)
11465       (gnus-message 7 "Can't go further"))
11466     n))
11467
11468 (defun gnus-summary-up-thread (n)
11469   "Go up thread N steps.
11470 If N is negative, go down instead.
11471 Returns the difference between N and how many steps down that were
11472 taken."
11473   (interactive "p")
11474   (gnus-summary-down-thread (- n)))
11475
11476 (defun gnus-summary-top-thread ()
11477   "Go to the top of the thread."
11478   (interactive)
11479   (while (gnus-summary-go-up-thread))
11480   (gnus-summary-article-number))
11481
11482 (defun gnus-summary-expire-thread ()
11483   "Mark articles under current thread as expired."
11484   (interactive)
11485   (gnus-summary-kill-thread 0))
11486
11487 (defun gnus-summary-kill-thread (&optional unmark)
11488   "Mark articles under current thread as read.
11489 If the prefix argument is positive, remove any kinds of marks.
11490 If the prefix argument is zero, mark thread as expired.
11491 If the prefix argument is negative, tick articles instead."
11492   (interactive "P")
11493   (when unmark
11494     (setq unmark (prefix-numeric-value unmark)))
11495   (let ((articles (gnus-summary-articles-in-thread))
11496         (hide (or (null unmark) (= unmark 0))))
11497     (save-excursion
11498       ;; Expand the thread.
11499       (gnus-summary-show-thread)
11500       ;; Mark all the articles.
11501       (while articles
11502         (gnus-summary-goto-subject (car articles))
11503         (cond ((null unmark)
11504                (gnus-summary-mark-article-as-read gnus-killed-mark))
11505               ((> unmark 0)
11506                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11507               ((= unmark 0)
11508                (gnus-summary-mark-article nil gnus-expirable-mark))
11509               (t
11510                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11511         (setq articles (cdr articles))))
11512     ;; Hide killed subtrees when hide is true.
11513     (and hide
11514          gnus-thread-hide-killed
11515          (gnus-summary-hide-thread))
11516     ;; If hide is t, go to next unread subject.
11517     (when hide
11518       ;; Go to next unread subject.
11519       (gnus-summary-next-subject 1 t)))
11520   (gnus-set-mode-line 'summary))
11521
11522 ;; Summary sorting commands
11523
11524 (defun gnus-summary-sort-by-number (&optional reverse)
11525   "Sort the summary buffer by article number.
11526 Argument REVERSE means reverse order."
11527   (interactive "P")
11528   (gnus-summary-sort 'number reverse))
11529
11530 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11531   "Sort the summary buffer by most recent article number.
11532 Argument REVERSE means reverse order."
11533   (interactive "P")
11534   (gnus-summary-sort 'most-recent-number reverse))
11535
11536 (defun gnus-summary-sort-by-random (&optional reverse)
11537   "Randomize the order in the summary buffer.
11538 Argument REVERSE means to randomize in reverse order."
11539   (interactive "P")
11540   (gnus-summary-sort 'random reverse))
11541
11542 (defun gnus-summary-sort-by-author (&optional reverse)
11543   "Sort the summary buffer by author name alphabetically.
11544 If `case-fold-search' is non-nil, case of letters is ignored.
11545 Argument REVERSE means reverse order."
11546   (interactive "P")
11547   (gnus-summary-sort 'author reverse))
11548
11549 (defun gnus-summary-sort-by-recipient (&optional reverse)
11550   "Sort the summary buffer by recipient name alphabetically.
11551 If `case-fold-search' is non-nil, case of letters is ignored.
11552 Argument REVERSE means reverse order."
11553   (interactive "P")
11554   (gnus-summary-sort 'recipient reverse))
11555
11556 (defun gnus-summary-sort-by-subject (&optional reverse)
11557   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11558 If `case-fold-search' is non-nil, case of letters is ignored.
11559 Argument REVERSE means reverse order."
11560   (interactive "P")
11561   (gnus-summary-sort 'subject reverse))
11562
11563 (defun gnus-summary-sort-by-date (&optional reverse)
11564   "Sort the summary buffer by date.
11565 Argument REVERSE means reverse order."
11566   (interactive "P")
11567   (gnus-summary-sort 'date reverse))
11568
11569 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11570   "Sort the summary buffer by most recent date.
11571 Argument REVERSE means reverse order."
11572   (interactive "P")
11573   (gnus-summary-sort 'most-recent-date reverse))
11574
11575 (defun gnus-summary-sort-by-score (&optional reverse)
11576   "Sort the summary buffer by score.
11577 Argument REVERSE means reverse order."
11578   (interactive "P")
11579   (gnus-summary-sort 'score reverse))
11580
11581 (defun gnus-summary-sort-by-lines (&optional reverse)
11582   "Sort the summary buffer by the number of lines.
11583 Argument REVERSE means reverse order."
11584   (interactive "P")
11585   (gnus-summary-sort 'lines reverse))
11586
11587 (defun gnus-summary-sort-by-chars (&optional reverse)
11588   "Sort the summary buffer by article length.
11589 Argument REVERSE means reverse order."
11590   (interactive "P")
11591   (gnus-summary-sort 'chars reverse))
11592
11593 (defun gnus-summary-sort-by-original (&optional reverse)
11594   "Sort the summary buffer using the default sorting method.
11595 Argument REVERSE means reverse order."
11596   (interactive "P")
11597   (let* ((inhibit-read-only t)
11598          (gnus-summary-prepare-hook nil))
11599     ;; We do the sorting by regenerating the threads.
11600     (gnus-summary-prepare)
11601     ;; Hide subthreads if needed.
11602     (gnus-summary-maybe-hide-threads)))
11603
11604 (defun gnus-summary-sort (predicate reverse)
11605   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11606   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11607          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11608          (gnus-thread-sort-functions
11609           (if (not reverse)
11610               thread
11611             `(lambda (t1 t2)
11612                (,thread t2 t1))))
11613          (gnus-sort-gathered-threads-function
11614           gnus-thread-sort-functions)
11615          (gnus-article-sort-functions
11616           (if (not reverse)
11617               article
11618             `(lambda (t1 t2)
11619                (,article t2 t1))))
11620          (inhibit-read-only t)
11621          (gnus-summary-prepare-hook nil))
11622     ;; We do the sorting by regenerating the threads.
11623     (gnus-summary-prepare)
11624     ;; Hide subthreads if needed.
11625     (gnus-summary-maybe-hide-threads)))
11626
11627 ;; Summary saving commands.
11628
11629 (defun gnus-summary-save-article (&optional n not-saved)
11630   "Save the current article using the default saver function.
11631 If N is a positive number, save the N next articles.
11632 If N is a negative number, save the N previous articles.
11633 If N is nil and any articles have been marked with the process mark,
11634 save those articles instead.
11635 The variable `gnus-default-article-saver' specifies the saver function.
11636
11637 If the optional second argument NOT-SAVED is non-nil, articles saved
11638 will not be marked as saved."
11639   (interactive "P")
11640   (require 'gnus-art)
11641   (let* ((articles (gnus-summary-work-articles n))
11642          (save-buffer (save-excursion
11643                         (nnheader-set-temp-buffer " *Gnus Save*")))
11644          (num (length articles))
11645          ;; Whether to save decoded articles or raw articles.
11646          (decode (when gnus-article-save-coding-system
11647                    (get gnus-default-article-saver :decode)))
11648          ;; When saving many articles in a single file, use the other
11649          ;; function to save articles other than the first one.
11650          (saver2 (get gnus-default-article-saver :function))
11651          (gnus-prompt-before-saving (if saver2
11652                                         t
11653                                       gnus-prompt-before-saving))
11654          (gnus-default-article-saver gnus-default-article-saver)
11655          header file)
11656     (dolist (article articles)
11657       (setq header (gnus-summary-article-header article))
11658       (if (not (vectorp header))
11659           ;; This is a pseudo-article.
11660           (if (assq 'name header)
11661               (gnus-copy-file (cdr (assq 'name header)))
11662             (gnus-message 1 "Article %d is unsaveable" article))
11663         ;; This is a real article.
11664         (save-window-excursion
11665           (gnus-summary-select-article decode decode nil article)
11666           (gnus-summary-goto-subject article))
11667         (with-current-buffer save-buffer
11668           (erase-buffer)
11669           (insert-buffer-substring (if decode
11670                                        gnus-article-buffer
11671                                      gnus-original-article-buffer)))
11672         (setq file (gnus-article-save save-buffer file num))
11673         (gnus-summary-remove-process-mark article)
11674         (unless not-saved
11675           (gnus-summary-set-saved-mark article)))
11676       (when saver2
11677         (setq gnus-default-article-saver saver2
11678               saver2 nil)))
11679     (gnus-kill-buffer save-buffer)
11680     (gnus-summary-position-point)
11681     (gnus-set-mode-line 'summary)
11682     n))
11683
11684 (defun gnus-summary-pipe-output (&optional n sym)
11685   "Pipe the current article to a subprocess.
11686 If N is a positive number, pipe the N next articles.
11687 If N is a negative number, pipe the N previous articles.
11688 If N is nil and any articles have been marked with the process mark,
11689 pipe those articles instead.
11690 The default command to which articles are piped is specified by the
11691 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11692 will be prompted for the command.
11693
11694 The properties `:decode' and `:headers' that are put to the function
11695 symbol `gnus-summary-save-in-pipe' control whether this function
11696 decodes articles and what headers to keep (see the doc string for the
11697 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11698 is neither omitted nor the symbol `r', force including all headers
11699 regardless of the `:headers' property.  If it is the symbol `r',
11700 articles that are not decoded and include all headers will be piped
11701 no matter what the properties `:decode' and `:headers' are."
11702   (interactive (gnus-interactive "P\ny"))
11703   (require 'gnus-art)
11704   (let* ((articles (gnus-summary-work-articles n))
11705          (result-buffer "*Shell Command Output*")
11706          (all-headers (not (memq sym '(nil r))))
11707          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11708          (raw (eq sym 'r))
11709          (headers (get 'gnus-summary-save-in-pipe :headers))
11710          command result)
11711     (unless (numberp (car articles))
11712       (error "No article to pipe"))
11713     (setq command (gnus-read-shell-command
11714                    (concat "Shell command on "
11715                            (if (cdr articles)
11716                                (format "these %d articles" (length articles))
11717                              "this article")
11718                            ": ")
11719                    gnus-summary-pipe-output-default-command))
11720     (when (string-equal command "")
11721       (error "A command is required"))
11722     (when all-headers
11723       (put 'gnus-summary-save-in-pipe :headers nil))
11724     (unwind-protect
11725         (while articles
11726           (gnus-summary-goto-subject (pop articles))
11727           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11728           (when (and (get-buffer result-buffer)
11729                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11730             (setq result (concat result (with-current-buffer result-buffer
11731                                           (buffer-string))))))
11732       (put 'gnus-summary-save-in-pipe :headers headers))
11733     (unless (zerop (length result))
11734       (if (with-current-buffer (get-buffer-create result-buffer)
11735             (erase-buffer)
11736             (insert result)
11737             (prog1
11738                 (and (= (count-lines (point-min) (point)) 1)
11739                      (progn
11740                        (end-of-line 0)
11741                        (<= (current-column)
11742                            (window-width (minibuffer-window)))))
11743               (goto-char (point-min))))
11744           (message "%s" (substring result 0 -1))
11745         (message nil)
11746         (gnus-configure-windows 'pipe)))))
11747
11748 (defun gnus-summary-save-article-mail (&optional arg)
11749   "Append the current article to a Unix mail box file.
11750 If N is a positive number, save the N next articles.
11751 If N is a negative number, save the N previous articles.
11752 If N is nil and any articles have been marked with the process mark,
11753 save those articles instead."
11754   (interactive "P")
11755   (require 'gnus-art)
11756   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11757     (gnus-summary-save-article arg)))
11758
11759 (defun gnus-summary-save-article-rmail (&optional arg)
11760   "Append the current article to an rmail file.
11761 If N is a positive number, save the N next articles.
11762 If N is a negative number, save the N previous articles.
11763 If N is nil and any articles have been marked with the process mark,
11764 save those articles instead."
11765   (interactive "P")
11766   (require 'gnus-art)
11767   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11768     (gnus-summary-save-article arg)))
11769
11770 (defun gnus-summary-save-article-file (&optional arg)
11771   "Append the current article to a file.
11772 If N is a positive number, save the N next articles.
11773 If N is a negative number, save the N previous articles.
11774 If N is nil and any articles have been marked with the process mark,
11775 save those articles instead."
11776   (interactive "P")
11777   (require 'gnus-art)
11778   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11779     (gnus-summary-save-article arg)))
11780
11781 (defun gnus-summary-write-article-file (&optional arg)
11782   "Write the current article to a file, deleting the previous file.
11783 If N is a positive number, save the N next articles.
11784 If N is a negative number, save the N previous articles.
11785 If N is nil and any articles have been marked with the process mark,
11786 save those articles instead."
11787   (interactive "P")
11788   (require 'gnus-art)
11789   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11790     (gnus-summary-save-article arg)))
11791
11792 (defun gnus-summary-save-article-body-file (&optional arg)
11793   "Append the current article body to a file.
11794 If N is a positive number, save the N next articles.
11795 If N is a negative number, save the N previous articles.
11796 If N is nil and any articles have been marked with the process mark,
11797 save those articles instead."
11798   (interactive "P")
11799   (require 'gnus-art)
11800   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11801     (gnus-summary-save-article arg)))
11802
11803 (defun gnus-summary-write-article-body-file (&optional arg)
11804   "Write the current article body to a file, deleting the previous file.
11805 If N is a positive number, save the N next articles.
11806 If N is a negative number, save the N previous articles.
11807 If N is nil and any articles have been marked with the process mark,
11808 save those articles instead."
11809   (interactive "P")
11810   (require 'gnus-art)
11811   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11812     (gnus-summary-save-article arg)))
11813
11814 (defun gnus-summary-muttprint (&optional arg)
11815   "Print the current article using Muttprint.
11816 If N is a positive number, save the N next articles.
11817 If N is a negative number, save the N previous articles.
11818 If N is nil and any articles have been marked with the process mark,
11819 save those articles instead."
11820   (interactive "P")
11821   (require 'gnus-art)
11822   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11823     (gnus-summary-save-article arg t)))
11824
11825 (defun gnus-summary-pipe-message (program)
11826   "Pipe the current article through PROGRAM."
11827   (interactive "sProgram: ")
11828   (gnus-summary-select-article)
11829   (let ((mail-header-separator ""))
11830     (gnus-eval-in-buffer-window gnus-article-buffer
11831       (save-restriction
11832         (widen)
11833         (let ((start (window-start))
11834               (inhibit-read-only t))
11835           (message-pipe-buffer-body program)
11836           (set-window-start (get-buffer-window (current-buffer)) start))))))
11837
11838 (defun gnus-get-split-value (methods)
11839   "Return a value based on the split METHODS."
11840   (let (split-name method result match)
11841     (when methods
11842       (with-current-buffer gnus-original-article-buffer
11843         (save-restriction
11844           (nnheader-narrow-to-headers)
11845           (while (and methods (not split-name))
11846             (goto-char (point-min))
11847             (setq method (pop methods))
11848             (setq match (car method))
11849             (when (cond
11850                    ((stringp match)
11851                     ;; Regular expression.
11852                     (ignore-errors
11853                       (re-search-forward match nil t)))
11854                    ((functionp match)
11855                     ;; Function.
11856                     (save-restriction
11857                       (widen)
11858                       (setq result (funcall match gnus-newsgroup-name))))
11859                    ((consp match)
11860                     ;; Form.
11861                     (save-restriction
11862                       (widen)
11863                       (setq result (eval match)))))
11864               (setq split-name (cdr method))
11865               (cond ((stringp result)
11866                      (push (expand-file-name
11867                             result gnus-article-save-directory)
11868                            split-name))
11869                     ((consp result)
11870                      (setq split-name (append result split-name)))))))))
11871     (nreverse split-name)))
11872
11873 (defun gnus-valid-move-group-p (group)
11874   (and (boundp group)
11875        (symbol-name group)
11876        (symbol-value group)
11877        (gnus-get-function (gnus-find-method-for-group
11878                            (symbol-name group)) 'request-accept-article t)))
11879
11880 (defun gnus-read-move-group-name (prompt default articles prefix)
11881   "Read a group name."
11882   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11883          (minibuffer-confirm-incomplete nil) ; XEmacs
11884          (prom
11885           (format "%s %s to"
11886                   prompt
11887                   (if (> (length articles) 1)
11888                       (format "these %d articles" (length articles))
11889                     "this article")))
11890          (to-newsgroup
11891           (let (active group)
11892             (when (or (null split-name) (= 1 (length split-name)))
11893               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11894               (mapatoms (lambda (symbol)
11895                           (setq group (symbol-name symbol))
11896                           (when (string-match "[^\000-\177]" group)
11897                             (setq group (gnus-group-decoded-name group)))
11898                           (set (intern group active) group))
11899                         gnus-active-hashtb))
11900             (cond
11901              ((null split-name)
11902               (gnus-completing-read-with-default
11903                default prom active 'gnus-valid-move-group-p nil prefix
11904                'gnus-group-history))
11905              ((= 1 (length split-name))
11906               (gnus-completing-read-with-default
11907                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11908                'gnus-group-history))
11909              (t
11910               (gnus-completing-read-with-default
11911                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11912                'gnus-group-history)))))
11913          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11914          encoded)
11915     (when to-newsgroup
11916       (if (or (string= to-newsgroup "")
11917               (string= to-newsgroup prefix))
11918           (setq to-newsgroup default))
11919       (unless to-newsgroup
11920         (error "No group name entered"))
11921       (setq encoded (mm-encode-coding-string
11922                      to-newsgroup
11923                      (gnus-group-name-charset to-method to-newsgroup)))
11924       (or (gnus-active encoded)
11925           (gnus-activate-group encoded nil nil to-method)
11926           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11927                                      to-newsgroup))
11928               (or (and (gnus-request-create-group encoded to-method)
11929                        (gnus-activate-group encoded nil nil to-method)
11930                        (gnus-subscribe-group encoded))
11931                   (error "Couldn't create group %s" to-newsgroup)))
11932           (error "No such group: %s" to-newsgroup))
11933       encoded)))
11934
11935 (defvar gnus-summary-save-parts-counter)
11936 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11937
11938 (defun gnus-summary-save-parts (type dir n &optional reverse)
11939   "Save parts matching TYPE to DIR.
11940 If REVERSE, save parts that do not match TYPE."
11941   (interactive
11942    (list (read-string "Save parts of type: "
11943                       (or (car gnus-summary-save-parts-type-history)
11944                           gnus-summary-save-parts-default-mime)
11945                       'gnus-summary-save-parts-type-history)
11946          (setq gnus-summary-save-parts-last-directory
11947                (read-file-name "Save to directory: "
11948                                gnus-summary-save-parts-last-directory
11949                                nil t))
11950          current-prefix-arg))
11951   (gnus-summary-iterate n
11952     (let ((gnus-display-mime-function nil)
11953           gnus-article-prepare-hook
11954           gnus-article-decode-hook
11955           gnus-display-mime-function
11956           gnus-break-pages
11957           (gnus-inhibit-treatment t))
11958       (gnus-summary-select-article))
11959     (with-current-buffer gnus-article-buffer
11960       (let ((handles (or gnus-article-mime-handles
11961                          (mm-dissect-buffer nil gnus-article-loose-mime)
11962                          (and gnus-article-emulate-mime
11963                               (mm-uu-dissect))))
11964             (gnus-summary-save-parts-counter 1))
11965         (when handles
11966           (gnus-summary-save-parts-1 type dir handles reverse)
11967           (unless gnus-article-mime-handles ;; Don't destroy this case.
11968             (mm-destroy-parts handles)))))))
11969
11970 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11971   (if (stringp (car handle))
11972       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11973               (cdr handle))
11974     (when (if reverse
11975               (not (string-match type (mm-handle-media-type handle)))
11976             (string-match type (mm-handle-media-type handle)))
11977       (let ((file (expand-file-name
11978                    (gnus-map-function
11979                     mm-file-name-rewrite-functions
11980                     (file-name-nondirectory
11981                      (or
11982                       (mail-content-type-get
11983                        (mm-handle-disposition handle) 'filename)
11984                       (mail-content-type-get
11985                        (mm-handle-type handle) 'name)
11986                       (format "%s.%d.%d" gnus-newsgroup-name
11987                               (cdr gnus-article-current)
11988                               gnus-summary-save-parts-counter))))
11989                    dir)))
11990         (incf gnus-summary-save-parts-counter)
11991         (unless (file-exists-p file)
11992           (mm-save-part-to-file handle file))))))
11993
11994 ;; Summary extract commands
11995
11996 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11997   (let ((inhibit-read-only t)
11998         (article (gnus-summary-article-number))
11999         after-article b e)
12000     (unless (gnus-summary-goto-subject article)
12001       (error "No such article: %d" article))
12002     (gnus-summary-position-point)
12003     ;; If all commands are to be bunched up on one line, we collect
12004     ;; them here.
12005     (unless gnus-view-pseudos-separately
12006       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12007             files action)
12008         (while ps
12009           (setq action (cdr (assq 'action (car ps))))
12010           (setq files (list (cdr (assq 'name (car ps)))))
12011           (while (and ps (cdr ps)
12012                       (string= (or action "1")
12013                                (or (cdr (assq 'action (cadr ps))) "2")))
12014             (push (cdr (assq 'name (cadr ps))) files)
12015             (setcdr ps (cddr ps)))
12016           (when files
12017             (when (not (string-match "%s" action))
12018               (push " " files))
12019             (push " " files)
12020             (when (assq 'execute (car ps))
12021               (setcdr (assq 'execute (car ps))
12022                       (funcall (if (string-match "%s" action)
12023                                    'format 'concat)
12024                                action
12025                                (mapconcat
12026                                 (lambda (f)
12027                                   (if (equal f " ")
12028                                       f
12029                                     (shell-quote-argument f)))
12030                                 files " ")))))
12031           (setq ps (cdr ps)))))
12032     (if (and gnus-view-pseudos (not not-view))
12033         (while pslist
12034           (when (assq 'execute (car pslist))
12035             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12036                                   (eq gnus-view-pseudos 'not-confirm)))
12037           (setq pslist (cdr pslist)))
12038       (save-excursion
12039         (while pslist
12040           (setq after-article (or (cdr (assq 'article (car pslist)))
12041                                   (gnus-summary-article-number)))
12042           (gnus-summary-goto-subject after-article)
12043           (forward-line 1)
12044           (setq b (point))
12045           (insert "    " (file-name-nondirectory
12046                           (cdr (assq 'name (car pslist))))
12047                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12048           (setq e (point))
12049           (forward-line -1)             ; back to `b'
12050           (gnus-add-text-properties
12051            b (1- e) (list 'gnus-number gnus-reffed-article-number
12052                           gnus-mouse-face-prop gnus-mouse-face))
12053           (gnus-data-enter
12054            after-article gnus-reffed-article-number
12055            gnus-unread-mark b (car pslist) 0 (- e b))
12056           (setq gnus-newsgroup-unreads
12057                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12058                                          gnus-reffed-article-number))
12059           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12060           (setq pslist (cdr pslist)))))))
12061
12062 (defun gnus-pseudos< (p1 p2)
12063   (let ((c1 (cdr (assq 'action p1)))
12064         (c2 (cdr (assq 'action p2))))
12065     (and c1 c2 (string< c1 c2))))
12066
12067 (defun gnus-request-pseudo-article (props)
12068   (cond ((assq 'execute props)
12069          (gnus-execute-command (cdr (assq 'execute props)))))
12070   (let ((gnus-current-article (gnus-summary-article-number)))
12071     (gnus-run-hooks 'gnus-mark-article-hook)))
12072
12073 (defun gnus-execute-command (command &optional automatic)
12074   (save-excursion
12075     (gnus-article-setup-buffer)
12076     (set-buffer gnus-article-buffer)
12077     (setq buffer-read-only nil)
12078     (let ((command (if automatic command
12079                      (read-string "Command: " (cons command 0)))))
12080       (erase-buffer)
12081       (insert "$ " command "\n\n")
12082       (if gnus-view-pseudo-asynchronously
12083           (start-process "gnus-execute" (current-buffer) shell-file-name
12084                          shell-command-switch command)
12085         (call-process shell-file-name nil t nil
12086                       shell-command-switch command)))))
12087
12088 ;; Summary kill commands.
12089
12090 (defun gnus-summary-edit-global-kill (article)
12091   "Edit the \"global\" kill file."
12092   (interactive (list (gnus-summary-article-number)))
12093   (gnus-group-edit-global-kill article))
12094
12095 (defun gnus-summary-edit-local-kill ()
12096   "Edit a local kill file applied to the current newsgroup."
12097   (interactive)
12098   (setq gnus-current-headers (gnus-summary-article-header))
12099   (gnus-group-edit-local-kill
12100    (gnus-summary-article-number) gnus-newsgroup-name))
12101
12102 ;;; Header reading.
12103
12104 (defun gnus-read-header (id &optional header)
12105   "Read the headers of article ID and enter them into the Gnus system."
12106   (let ((group gnus-newsgroup-name)
12107         (gnus-override-method
12108          (or
12109           gnus-override-method
12110           (and (gnus-news-group-p gnus-newsgroup-name)
12111                (car (gnus-refer-article-methods)))))
12112         where)
12113     ;; First we check to see whether the header in question is already
12114     ;; fetched.
12115     (if (stringp id)
12116         ;; This is a Message-ID.
12117         (setq header (or header (gnus-id-to-header id)))
12118       ;; This is an article number.
12119       (setq header (or header (gnus-summary-article-header id))))
12120     (if (and header
12121              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12122         ;; We have found the header.
12123         header
12124       ;; We have to really fetch the header to this article.
12125       (with-current-buffer nntp-server-buffer
12126         (when (setq where (gnus-request-head id group))
12127           (nnheader-fold-continuation-lines)
12128           (goto-char (point-max))
12129           (insert ".\n")
12130           (goto-char (point-min))
12131           (insert "211 ")
12132           (princ (cond
12133                   ((numberp id) id)
12134                   ((cdr where) (cdr where))
12135                   (header (mail-header-number header))
12136                   (t gnus-reffed-article-number))
12137                  (current-buffer))
12138           (insert " Article retrieved.\n"))
12139         (if (or (not where)
12140                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12141             ()                          ; Malformed head.
12142           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12143             (when (and (stringp id)
12144                        (or
12145                         (not (string= (gnus-group-real-name group)
12146                                       (car where)))
12147                         (not (gnus-server-equal gnus-override-method
12148                                                 (gnus-group-method group)))))
12149               ;; If we fetched by Message-ID and the article came from
12150               ;; a different group (or server), we fudge some bogus
12151               ;; article numbers for this article.
12152               (mail-header-set-number header gnus-reffed-article-number))
12153             (with-current-buffer gnus-summary-buffer
12154               (decf gnus-reffed-article-number)
12155               (gnus-remove-header (mail-header-number header))
12156               (push header gnus-newsgroup-headers)
12157               (setq gnus-current-headers header)
12158               (push (mail-header-number header) gnus-newsgroup-limit)))
12159           header)))))
12160
12161 (defun gnus-remove-header (number)
12162   "Remove header NUMBER from `gnus-newsgroup-headers'."
12163   (if (and gnus-newsgroup-headers
12164            (= number (mail-header-number (car gnus-newsgroup-headers))))
12165       (pop gnus-newsgroup-headers)
12166     (let ((headers gnus-newsgroup-headers))
12167       (while (and (cdr headers)
12168                   (not (= number (mail-header-number (cadr headers)))))
12169         (pop headers))
12170       (when (cdr headers)
12171         (setcdr headers (cddr headers))))))
12172
12173 ;;;
12174 ;;; summary highlights
12175 ;;;
12176
12177 (defun gnus-highlight-selected-summary ()
12178   "Highlight selected article in summary buffer."
12179   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12180   (when gnus-summary-selected-face
12181     (save-excursion
12182       (let* ((beg (point-at-bol))
12183              (end (point-at-eol))
12184              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12185              (from (if (get-text-property beg gnus-mouse-face-prop)
12186                        beg
12187                      (or (next-single-property-change
12188                           beg gnus-mouse-face-prop nil end)
12189                          beg)))
12190              (to
12191               (if (= from end)
12192                   (- from 2)
12193                 (or (next-single-property-change
12194                      from gnus-mouse-face-prop nil end)
12195                     end))))
12196         ;; If no mouse-face prop on line we will have to = from = end,
12197         ;; so we highlight the entire line instead.
12198         (when (= (+ to 2) from)
12199           (setq from beg)
12200           (setq to end))
12201         (if gnus-newsgroup-selected-overlay
12202             ;; Move old overlay.
12203             (gnus-move-overlay
12204              gnus-newsgroup-selected-overlay from to (current-buffer))
12205           ;; Create new overlay.
12206           (gnus-overlay-put
12207            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12208            'face gnus-summary-selected-face))))))
12209
12210 (defvar gnus-summary-highlight-line-cached nil)
12211 (defvar gnus-summary-highlight-line-trigger nil)
12212
12213 (defun gnus-summary-highlight-line-0 ()
12214   (if (and (eq gnus-summary-highlight-line-trigger
12215                gnus-summary-highlight)
12216            gnus-summary-highlight-line-cached)
12217       gnus-summary-highlight-line-cached
12218     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12219           gnus-summary-highlight-line-cached
12220           (let* ((cond (list 'cond))
12221                  (c cond)
12222                  (list gnus-summary-highlight))
12223             (while list
12224               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12225                               nil))
12226               (setq c (cdr c)
12227                     list (cdr list)))
12228             (gnus-byte-compile (list 'lambda nil cond))))))
12229
12230 (defun gnus-summary-highlight-line ()
12231   "Highlight current line according to `gnus-summary-highlight'."
12232   (let* ((beg (point-at-bol))
12233          (article (or (gnus-summary-article-number) gnus-current-article))
12234          (score (or (cdr (assq article
12235                                gnus-newsgroup-scored))
12236                     gnus-summary-default-score 0))
12237          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12238          (inhibit-read-only t)
12239          (default gnus-summary-default-score)
12240          (default-high gnus-summary-default-high-score)
12241          (default-low gnus-summary-default-low-score)
12242          (uncached (and gnus-summary-use-undownloaded-faces
12243                         (memq article gnus-newsgroup-undownloaded)
12244                         (not (memq article gnus-newsgroup-cached)))))
12245     (let ((face (funcall (gnus-summary-highlight-line-0))))
12246       (unless (eq face (get-text-property beg 'face))
12247         (gnus-put-text-property-excluding-characters-with-faces
12248          beg (point-at-eol) 'face
12249          (setq face (if (boundp face) (symbol-value face) face)))
12250         (when gnus-summary-highlight-line-function
12251           (funcall gnus-summary-highlight-line-function article face))))))
12252
12253 (defun gnus-update-read-articles (group unread &optional compute)
12254   "Update the list of read articles in GROUP.
12255 UNREAD is a sorted list."
12256   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12257         (info (gnus-get-info group))
12258         (prev 1)
12259         read)
12260     (if (or (not info) (not active))
12261         ;; There is no info on this group if it was, in fact,
12262         ;; killed.  Gnus stores no information on killed groups, so
12263         ;; there's nothing to be done.
12264         ;; One could store the information somewhere temporarily,
12265         ;; perhaps...  Hmmm...
12266         ()
12267       ;; Remove any negative articles numbers.
12268       (while (and unread (< (car unread) 0))
12269         (setq unread (cdr unread)))
12270       ;; Remove any expired article numbers
12271       (while (and unread (< (car unread) (car active)))
12272         (setq unread (cdr unread)))
12273       ;; Compute the ranges of read articles by looking at the list of
12274       ;; unread articles.
12275       (while unread
12276         (when (/= (car unread) prev)
12277           (push (if (= prev (1- (car unread))) prev
12278                   (cons prev (1- (car unread))))
12279                 read))
12280         (setq prev (1+ (car unread)))
12281         (setq unread (cdr unread)))
12282       (when (<= prev (cdr active))
12283         (push (cons prev (cdr active)) read))
12284       (setq read (if (> (length read) 1) (nreverse read) read))
12285       (if compute
12286           read
12287         (save-excursion
12288           (let (setmarkundo)
12289             ;; Propagate the read marks to the backend.
12290             (when (and gnus-propagate-marks
12291                        (gnus-check-backend-function 'request-set-mark group))
12292               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12293                     (add (gnus-remove-from-range read (gnus-info-read info))))
12294                 (when (or add del)
12295                   (unless (gnus-check-group group)
12296                     (error "Can't open server for %s" group))
12297                   (gnus-request-set-mark
12298                    group (delq nil (list (if add (list add 'add '(read)))
12299                                          (if del (list del 'del '(read))))))
12300                   (setq setmarkundo
12301                         `(gnus-request-set-mark
12302                           ,group
12303                           ',(delq nil (list
12304                                        (if del (list del 'add '(read)))
12305                                        (if add (list add 'del '(read))))))))))
12306             (set-buffer gnus-group-buffer)
12307             (gnus-undo-register
12308               `(progn
12309                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12310                  (gnus-info-set-read ',info ',(gnus-info-read info))
12311                  (gnus-get-unread-articles-in-group ',info
12312                                                     (gnus-active ,group))
12313                  (gnus-group-update-group ,group t)
12314                  ,setmarkundo))))
12315         ;; Enter this list into the group info.
12316         (gnus-info-set-read info read)
12317         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12318         (gnus-get-unread-articles-in-group info (gnus-active group))
12319         t))))
12320
12321 (defun gnus-offer-save-summaries ()
12322   "Offer to save all active summary buffers."
12323   (let (buffers)
12324     ;; Go through all buffers and find all summaries.
12325     (dolist (buffer (buffer-list))
12326       (when (and (setq buffer (buffer-name buffer))
12327                  (string-match "Summary" buffer)
12328                  (with-current-buffer buffer
12329                    ;; We check that this is, indeed, a summary buffer.
12330                    (and (eq major-mode 'gnus-summary-mode)
12331                         ;; Also make sure this isn't bogus.
12332                         gnus-newsgroup-prepared
12333                         ;; Also make sure that this isn't a
12334                         ;; dead summary buffer.
12335                         (not gnus-dead-summary-mode))))
12336         (push buffer buffers)))
12337     ;; Go through all these summary buffers and offer to save them.
12338     (when buffers
12339       (save-excursion
12340         (map-y-or-n-p
12341          "Update summary buffer %s? "
12342          (lambda (buf)
12343            (switch-to-buffer buf)
12344            (gnus-summary-exit))
12345          buffers)))))
12346
12347 (defun gnus-summary-setup-default-charset ()
12348   "Setup newsgroup default charset."
12349   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12350       (setq gnus-newsgroup-charset nil)
12351     (let* ((ignored-charsets
12352             (or gnus-newsgroup-ephemeral-ignored-charsets
12353                 (append
12354                  (and gnus-newsgroup-name
12355                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12356                  gnus-newsgroup-ignored-charsets))))
12357       (setq gnus-newsgroup-charset
12358             (or gnus-newsgroup-ephemeral-charset
12359                 (and gnus-newsgroup-name
12360                      (gnus-parameter-charset gnus-newsgroup-name))
12361                 gnus-default-charset))
12362       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12363            ignored-charsets))))
12364
12365 ;;;
12366 ;;; Mime Commands
12367 ;;;
12368
12369 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12370   "Display the current article buffer fully MIME-buttonized.
12371 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12372 treated as multipart/mixed."
12373   (interactive "P")
12374   (require 'gnus-art)
12375   (let ((gnus-unbuttonized-mime-types nil)
12376         (gnus-mime-display-multipart-as-mixed show-all-parts))
12377     (gnus-summary-show-article)))
12378
12379 (defun gnus-summary-repair-multipart (article)
12380   "Add a Content-Type header to a multipart article without one."
12381   (interactive (list (gnus-summary-article-number)))
12382   (gnus-with-article article
12383     (message-narrow-to-head)
12384     (message-remove-header "Mime-Version")
12385     (goto-char (point-max))
12386     (insert "Mime-Version: 1.0\n")
12387     (widen)
12388     (when (search-forward "\n--" nil t)
12389       (let ((separator (buffer-substring (point) (point-at-eol))))
12390         (message-narrow-to-head)
12391         (message-remove-header "Content-Type")
12392         (goto-char (point-max))
12393         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12394                         separator))
12395         (widen))))
12396   (let (gnus-mark-article-hook)
12397     (gnus-summary-select-article t t nil article)))
12398
12399 (defun gnus-summary-toggle-display-buttonized ()
12400   "Toggle the buttonizing of the article buffer."
12401   (interactive)
12402   (require 'gnus-art)
12403   (if (setq gnus-inhibit-mime-unbuttonizing
12404             (not gnus-inhibit-mime-unbuttonizing))
12405       (let ((gnus-unbuttonized-mime-types nil))
12406         (gnus-summary-show-article))
12407     (gnus-summary-show-article)))
12408
12409 ;;;
12410 ;;; Generic summary marking commands
12411 ;;;
12412
12413 (defvar gnus-summary-marking-alist
12414   '((read gnus-del-mark "d")
12415     (unread gnus-unread-mark "u")
12416     (ticked gnus-ticked-mark "!")
12417     (dormant gnus-dormant-mark "?")
12418     (expirable gnus-expirable-mark "e"))
12419   "An alist of names/marks/keystrokes.")
12420
12421 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12422 (defvar gnus-summary-mark-map)
12423
12424 (defun gnus-summary-make-all-marking-commands ()
12425   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12426   (dolist (elem gnus-summary-marking-alist)
12427     (apply 'gnus-summary-make-marking-command elem)))
12428
12429 (defun gnus-summary-make-marking-command (name mark keystroke)
12430   (let ((map (make-sparse-keymap)))
12431     (define-key gnus-summary-generic-mark-map keystroke map)
12432     (dolist (lway `((next "next" next nil "n")
12433                     (next-unread "next unread" next t "N")
12434                     (prev "previous" prev nil "p")
12435                     (prev-unread "previous unread" prev t "P")
12436                     (nomove "" nil nil ,keystroke)))
12437       (let ((func (gnus-summary-make-marking-command-1
12438                    mark (car lway) lway name)))
12439         (setq func (eval func))
12440         (define-key map (nth 4 lway) func)))))
12441
12442 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12443   `(defun ,(intern
12444             (format "gnus-summary-put-mark-as-%s%s"
12445                     name (if (eq way 'nomove)
12446                              ""
12447                            (concat "-" (symbol-name way)))))
12448      (n)
12449      ,(format
12450        "Mark the current article as %s%s.
12451 If N, the prefix, then repeat N times.
12452 If N is negative, move in reverse order.
12453 The difference between N and the actual number of articles marked is
12454 returned."
12455        name (cadr lway))
12456      (interactive "p")
12457      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12458
12459 (defun gnus-summary-generic-mark (n mark move unread)
12460   "Mark N articles with MARK."
12461   (unless (eq major-mode 'gnus-summary-mode)
12462     (error "This command can only be used in the summary buffer"))
12463   (gnus-summary-show-thread)
12464   (let ((nummove
12465          (cond
12466           ((eq move 'next) 1)
12467           ((eq move 'prev) -1)
12468           (t 0))))
12469     (if (zerop nummove)
12470         (setq n 1)
12471       (when (< n 0)
12472         (setq n (abs n)
12473               nummove (* -1 nummove))))
12474     (while (and (> n 0)
12475                 (gnus-summary-mark-article nil mark)
12476                 (zerop (gnus-summary-next-subject nummove unread t)))
12477       (setq n (1- n)))
12478     (when (/= 0 n)
12479       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12480     (gnus-summary-recenter)
12481     (gnus-summary-position-point)
12482     (gnus-set-mode-line 'summary)
12483     n))
12484
12485 (defun gnus-summary-insert-articles (articles)
12486   (when (setq articles
12487               (gnus-sorted-difference articles
12488                                       (mapcar (lambda (h)
12489                                                 (mail-header-number h))
12490                                               gnus-newsgroup-headers)))
12491     (setq gnus-newsgroup-headers
12492           (gnus-merge 'list
12493                       gnus-newsgroup-headers
12494                       (gnus-fetch-headers articles)
12495                       'gnus-article-sort-by-number))
12496     ;; Suppress duplicates?
12497     (when gnus-suppress-duplicates
12498       (gnus-dup-suppress-articles))
12499
12500     (if (and gnus-fetch-old-headers
12501              (eq gnus-headers-retrieved-by 'nov))
12502         ;; We might want to build some more threads first.
12503         (if (eq gnus-fetch-old-headers 'invisible)
12504             (gnus-build-all-threads)
12505           (gnus-build-old-threads))
12506       ;; Mark the inserted articles that are unread as unread.
12507       (setq gnus-newsgroup-unreads
12508             (gnus-sorted-nunion
12509              gnus-newsgroup-unreads
12510              (gnus-sorted-nintersection
12511               (gnus-list-of-unread-articles gnus-newsgroup-name)
12512               articles)))
12513       ;; Mark the inserted articles as selected so that the information
12514       ;; of the marks having been changed by a user may be updated when
12515       ;; exiting this group.  See `gnus-summary-update-info'.
12516       (dolist (art articles)
12517         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12518     ;; Let the Gnus agent mark articles as read.
12519     (when gnus-agent
12520       (gnus-agent-get-undownloaded-list))
12521     ;; Remove list identifiers from subject
12522     (when gnus-list-identifiers
12523       (gnus-summary-remove-list-identifiers))
12524     ;; First and last article in this newsgroup.
12525     (when gnus-newsgroup-headers
12526       (setq gnus-newsgroup-begin
12527             (mail-header-number (car gnus-newsgroup-headers))
12528             gnus-newsgroup-end
12529             (mail-header-number
12530              (gnus-last-element gnus-newsgroup-headers))))
12531     (when gnus-use-scoring
12532       (gnus-possibly-score-headers))))
12533
12534 (defun gnus-summary-insert-old-articles (&optional all)
12535   "Insert all old articles in this group.
12536 If ALL is non-nil, already read articles become readable.
12537 If ALL is a number, fetch this number of articles."
12538   (interactive "P")
12539   (prog1
12540       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12541             older len)
12542         (setq older
12543               ;; Some nntp servers lie about their active range.  When
12544               ;; this happens, the active range can be in the millions.
12545               ;; Use a compressed range to avoid creating a huge list.
12546               (gnus-range-difference (list gnus-newsgroup-active) old))
12547         (setq len (gnus-range-length older))
12548         (cond
12549          ((null older) nil)
12550          ((numberp all)
12551           (if (< all len)
12552               (let ((older-range (nreverse older)))
12553                 (setq older nil)
12554
12555                 (while (> all 0)
12556                   (let* ((r (pop older-range))
12557                          (min (if (numberp r) r (car r)))
12558                          (max (if (numberp r) r (cdr r))))
12559                     (while (and (<= min max)
12560                                 (> all 0))
12561                       (push max older)
12562                       (setq all (1- all)
12563                             max (1- max))))))
12564             (setq older (gnus-uncompress-range older))))
12565          (all
12566           (setq older (gnus-uncompress-range older)))
12567          (t
12568           (when (and (numberp gnus-large-newsgroup)
12569                    (> len gnus-large-newsgroup))
12570               (let* ((cursor-in-echo-area nil)
12571                      (initial (gnus-parameter-large-newsgroup-initial
12572                                gnus-newsgroup-name))
12573                      (input
12574                       (read-string
12575                        (format
12576                         "How many articles from %s (%s %d): "
12577                         (gnus-group-decoded-name gnus-newsgroup-name)
12578                         (if initial "max" "default")
12579                         len)
12580                        (if initial
12581                            (cons (number-to-string initial)
12582                                  0)))))
12583                 (unless (string-match "^[ \t]*$" input)
12584                   (setq all (string-to-number input))
12585                   (if (< all len)
12586                       (let ((older-range (nreverse older)))
12587                         (setq older nil)
12588
12589                         (while (> all 0)
12590                           (let* ((r (pop older-range))
12591                                  (min (if (numberp r) r (car r)))
12592                                  (max (if (numberp r) r (cdr r))))
12593                             (while (and (<= min max)
12594                                         (> all 0))
12595                               (push max older)
12596                               (setq all (1- all)
12597                                     max (1- max))))))))))
12598           (setq older (gnus-uncompress-range older))))
12599         (if (not older)
12600             (message "No old news.")
12601           (gnus-summary-insert-articles older)
12602           (gnus-summary-limit (gnus-sorted-nunion old older))))
12603     (gnus-summary-position-point)))
12604
12605 (defun gnus-summary-insert-new-articles ()
12606   "Insert all new articles in this group."
12607   (interactive)
12608   (prog1
12609       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12610             (old-active gnus-newsgroup-active)
12611             (nnmail-fetched-sources (list t))
12612             i new)
12613         (setq gnus-newsgroup-active
12614               (gnus-activate-group gnus-newsgroup-name 'scan))
12615         (setq i (cdr gnus-newsgroup-active))
12616         (while (> i (cdr old-active))
12617           (push i new)
12618           (decf i))
12619         (if (not new)
12620             (message "No gnus is bad news")
12621           (gnus-summary-insert-articles new)
12622           (setq gnus-newsgroup-unreads
12623                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12624           (gnus-summary-limit (gnus-sorted-nunion old new))))
12625     (gnus-summary-position-point)))
12626
12627 ;;; Bookmark support for Gnus.
12628 (declare-function bookmark-make-record-default
12629                   "bookmark" (&optional no-file no-context posn))
12630 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12631 (declare-function bookmark-default-handler "bookmark" (bmk))
12632 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12633 (defvar bookmark-yank-point)
12634 (defvar bookmark-current-buffer)
12635
12636 (defun gnus-summary-bookmark-make-record ()
12637   "Make a bookmark entry for a Gnus summary buffer."
12638   (let (pos buf)
12639     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12640       (save-restriction              ; FIXME is it necessary to widen?
12641         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12642       (setq buf "art") ; We are recording bookmark from article buffer.
12643       (setq bookmark-yank-point (point))
12644       (setq bookmark-current-buffer (current-buffer))
12645       (gnus-article-show-summary))      ; Go back in summary buffer.
12646     ;; We are now recording bookmark from summary buffer.
12647     (unless buf (setq buf "sum"))
12648     (let* ((subject (elt (gnus-summary-article-header) 1))
12649            (grp     (car gnus-article-current))
12650            (art     (cdr gnus-article-current))
12651            (head    (gnus-summary-article-header art))
12652            (id      (mail-header-id head)))
12653       `(,subject
12654         ,@(condition-case nil
12655               (bookmark-make-record-default 'no-file 'no-context pos)
12656             (wrong-number-of-arguments
12657              (bookmark-make-record-default 'point-only)))
12658         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12659         (group . ,grp) (article . ,art)
12660         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12661
12662 ;;;###autoload
12663 (defun gnus-summary-bookmark-jump (bookmark)
12664   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12665 BOOKMARK is a bookmark name or a bookmark record."
12666   (let ((group    (bookmark-prop-get bookmark 'group))
12667         (article  (bookmark-prop-get bookmark 'article))
12668         (id       (bookmark-prop-get bookmark 'message-id))
12669         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12670     (gnus-fetch-group group (list article))
12671     (gnus-summary-insert-cached-articles)
12672     (gnus-summary-goto-article id nil 'force)
12673     ;; FIXME we have to wait article buffer is ready (only large buffer)
12674     ;; Is there a better solution to know that?
12675     ;; If we don't wait `bookmark-default-handler' will have no chance
12676     ;; to set position. However there is no error, just wrong pos.
12677     (sit-for 1)
12678     (when (string= buf "Gnus-art")
12679       (other-window 1))
12680     (bookmark-default-handler
12681      `(""
12682        (buffer . ,(current-buffer))
12683        . ,(bookmark-get-bookmark-record bookmark)))))
12684
12685 (gnus-summary-make-all-marking-commands)
12686
12687 (gnus-ems-redefine)
12688
12689 (provide 'gnus-sum)
12690
12691 (run-hooks 'gnus-sum-load-hook)
12692
12693 ;; Local Variables:
12694 ;; coding: iso-8859-1
12695 ;; End:
12696
12697 ;;; gnus-sum.el ends here