Add sanity check to not delete marks outside the active range.
[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 nil
989   "*A hook called when a summary line is changed.
990 The hook will not be called if `gnus-visual' is nil.
991
992 The default function `gnus-summary-highlight-line' will
993 highlight the line according to the `gnus-summary-highlight'
994 variable."
995   :group 'gnus-summary-visual
996   :type 'hook)
997
998 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
999   "*A hook called when an article is selected for the first time.
1000 The hook is intended to mark an article as read (or unread)
1001 automatically when it is selected."
1002   :group 'gnus-summary-choose
1003   :type 'hook)
1004
1005 (defcustom gnus-group-no-more-groups-hook nil
1006   "*A hook run when returning to group mode having no more (unread) groups."
1007   :group 'gnus-group-select
1008   :type 'hook)
1009
1010 (defcustom gnus-ps-print-hook nil
1011   "*A hook run before ps-printing something from Gnus."
1012   :group 'gnus-summary
1013   :type 'hook)
1014
1015 (defcustom gnus-summary-article-move-hook nil
1016   "*A hook called after an article is moved, copied, respooled, or crossposted."
1017   :version "22.1"
1018   :group 'gnus-summary
1019   :type 'hook)
1020
1021 (defcustom gnus-summary-article-delete-hook nil
1022   "*A hook called after an article is deleted."
1023   :version "22.1"
1024   :group 'gnus-summary
1025   :type 'hook)
1026
1027 (defcustom gnus-summary-article-expire-hook nil
1028   "*A hook called after an article is expired."
1029   :version "22.1"
1030   :group 'gnus-summary
1031   :type 'hook)
1032
1033 (defcustom gnus-summary-display-arrow
1034   (and (fboundp 'display-graphic-p)
1035        (display-graphic-p))
1036   "*If non-nil, display an arrow highlighting the current article."
1037   :version "22.1"
1038   :group 'gnus-summary
1039   :type 'boolean)
1040
1041 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1042   "Face used for highlighting the current article in the summary buffer."
1043   :group 'gnus-summary-visual
1044   :type 'face)
1045
1046 (defvar gnus-tmp-downloaded nil)
1047
1048 (defcustom gnus-summary-highlight
1049   '(((eq mark gnus-canceled-mark)
1050      . gnus-summary-cancelled)
1051     ((and uncached (> score default-high))
1052      . gnus-summary-high-undownloaded)
1053     ((and uncached (< score default-low))
1054      . gnus-summary-low-undownloaded)
1055     (uncached
1056      . gnus-summary-normal-undownloaded)
1057     ((and (> score default-high)
1058           (or (eq mark gnus-dormant-mark)
1059               (eq mark gnus-ticked-mark)))
1060      . gnus-summary-high-ticked)
1061     ((and (< score default-low)
1062           (or (eq mark gnus-dormant-mark)
1063               (eq mark gnus-ticked-mark)))
1064      . gnus-summary-low-ticked)
1065     ((or (eq mark gnus-dormant-mark)
1066          (eq mark gnus-ticked-mark))
1067      . gnus-summary-normal-ticked)
1068     ((and (> score default-high) (eq mark gnus-ancient-mark))
1069      . gnus-summary-high-ancient)
1070     ((and (< score default-low) (eq mark gnus-ancient-mark))
1071      . gnus-summary-low-ancient)
1072     ((eq mark gnus-ancient-mark)
1073      . gnus-summary-normal-ancient)
1074     ((and (> score default-high) (eq mark gnus-unread-mark))
1075      . gnus-summary-high-unread)
1076     ((and (< score default-low) (eq mark gnus-unread-mark))
1077      . gnus-summary-low-unread)
1078     ((eq mark gnus-unread-mark)
1079      . gnus-summary-normal-unread)
1080     ((> score default-high)
1081      . gnus-summary-high-read)
1082     ((< score default-low)
1083      . gnus-summary-low-read)
1084     (t
1085      . gnus-summary-normal-read))
1086   "*Controls the highlighting of summary buffer lines.
1087
1088 A list of (FORM . FACE) pairs.  When deciding how a particular
1089 summary line should be displayed, each form is evaluated.  The content
1090 of the face field after the first true form is used.  You can change
1091 how those summary lines are displayed, by editing the face field.
1092
1093 You can use the following variables in the FORM field.
1094
1095 score:        The article's score.
1096 default:      The default article score.
1097 default-high: The default score for high scored articles.
1098 default-low:  The default score for low scored articles.
1099 below:        The score below which articles are automatically marked as read.
1100 mark:         The article's mark.
1101 uncached:     Non-nil if the article is uncached."
1102   :group 'gnus-summary-visual
1103   :type '(repeat (cons (sexp :tag "Form" nil)
1104                        face)))
1105 (put 'gnus-summary-highlight 'risky-local-variable t)
1106
1107 (defcustom gnus-alter-header-function nil
1108   "Function called to allow alteration of article header structures.
1109 The function is called with one parameter, the article header vector,
1110 which it may alter in any way."
1111   :type '(choice (const :tag "None" nil)
1112                  function)
1113   :group 'gnus-summary)
1114
1115 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1116   "Function used to decode a string with encoded words.")
1117
1118 (defvar gnus-decode-encoded-address-function
1119   'mail-decode-encoded-address-string
1120   "Function used to decode addresses with encoded words.")
1121
1122 (defcustom gnus-extra-headers '(To Newsgroups)
1123   "*Extra headers to parse."
1124   :version "21.1"
1125   :group 'gnus-summary
1126   :type '(repeat symbol))
1127
1128 (defcustom gnus-ignored-from-addresses
1129   (and user-mail-address
1130        (not (string= user-mail-address ""))
1131        (regexp-quote user-mail-address))
1132   "*From headers that may be suppressed in favor of To headers.
1133 This can be a regexp or a list of regexps."
1134   :version "21.1"
1135   :group 'gnus-summary
1136   :type '(choice regexp
1137                  (repeat :tag "Regexp List" regexp)))
1138
1139 (defsubst gnus-ignored-from-addresses ()
1140   (gmm-regexp-concat gnus-ignored-from-addresses))
1141
1142 (defcustom gnus-summary-to-prefix "-> "
1143   "*String prefixed to the To field in the summary line when
1144 using `gnus-ignored-from-addresses'."
1145   :version "22.1"
1146   :group 'gnus-summary
1147   :type 'string)
1148
1149 (defcustom gnus-summary-newsgroup-prefix "=> "
1150   "*String prefixed to the Newsgroup field in the summary
1151 line when using `gnus-ignored-from-addresses'."
1152   :version "22.1"
1153   :group 'gnus-summary
1154   :type 'string)
1155
1156 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1157   "List of charsets that should be ignored.
1158 When these charsets are used in the \"charset\" parameter, the
1159 default charset will be used instead."
1160   :version "21.1"
1161   :type '(repeat symbol)
1162   :group 'gnus-charset)
1163
1164 (defcustom gnus-newsgroup-maximum-articles nil
1165   "The maximum number of articles a newsgroup.
1166 If this is a number, old articles in a newsgroup exceeding this number
1167 are silently ignored.  If it is nil, no article is ignored.  Note that
1168 setting this variable to a number might prevent you from reading very
1169 old articles."
1170   :group 'gnus-group-select
1171   :version "22.2"
1172   :type '(choice (const :tag "No limit" nil)
1173                  integer))
1174
1175 (gnus-define-group-parameter
1176  ignored-charsets
1177  :type list
1178  :function-document
1179  "Return the ignored charsets of GROUP."
1180  :variable gnus-group-ignored-charsets-alist
1181  :variable-default
1182  '(("alt\\.chinese\\.text" iso-8859-1))
1183  :variable-document
1184  "Alist of regexps (to match group names) and charsets that should be ignored.
1185 When these charsets are used in the \"charset\" parameter, the
1186 default charset will be used instead."
1187  :variable-group gnus-charset
1188  :variable-type '(repeat (cons (regexp :tag "Group")
1189                                (repeat symbol)))
1190  :parameter-type '(choice :tag "Ignored charsets"
1191                           :value nil
1192                           (repeat (symbol)))
1193  :parameter-document       "\
1194 List of charsets that should be ignored.
1195
1196 When these charsets are used in the \"charset\" parameter, the
1197 default charset will be used instead.")
1198
1199 (defcustom gnus-group-highlight-words-alist nil
1200   "Alist of group regexps and highlight regexps.
1201 This variable uses the same syntax as `gnus-emphasis-alist'."
1202   :version "21.1"
1203   :type '(repeat (cons (regexp :tag "Group")
1204                        (repeat (list (regexp :tag "Highlight regexp")
1205                                      (number :tag "Group for entire word" 0)
1206                                      (number :tag "Group for displayed part" 0)
1207                                      (symbol :tag "Face"
1208                                              gnus-emphasis-highlight-words)))))
1209   :group 'gnus-summary-visual)
1210
1211 (defcustom gnus-summary-show-article-charset-alist
1212   nil
1213   "Alist of number and charset.
1214 The article will be shown with the charset corresponding to the
1215 numbered argument.
1216 For example: ((1 . cn-gb-2312) (2 . big5))."
1217   :version "21.1"
1218   :type '(repeat (cons (number :tag "Argument" 1)
1219                        (symbol :tag "Charset")))
1220   :group 'gnus-charset)
1221
1222 (defcustom gnus-preserve-marks t
1223   "Whether marks are preserved when moving, copying and respooling messages."
1224   :version "21.1"
1225   :type 'boolean
1226   :group 'gnus-summary-marks)
1227
1228 (defcustom gnus-propagate-marks t
1229   "If non-nil, do not propagate marks to the backends."
1230   :version "23.1" ;; No Gnus
1231   :type 'boolean
1232   :group 'gnus-summary-marks)
1233
1234 (defcustom gnus-alter-articles-to-read-function nil
1235   "Function to be called to alter the list of articles to be selected."
1236   :type '(choice (const nil) function)
1237   :group 'gnus-summary)
1238
1239 (defcustom gnus-orphan-score nil
1240   "*All orphans get this score added.  Set in the score file."
1241   :group 'gnus-score-default
1242   :type '(choice (const nil)
1243                  integer))
1244
1245 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1246   "*A regexp to match MIME parts when saving multiple parts of a
1247 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1248 This regexp will be used by default when prompting the user for which
1249 type of files to save."
1250   :group 'gnus-summary
1251   :type 'regexp)
1252
1253 (defcustom gnus-read-all-available-headers nil
1254   "Whether Gnus should parse all headers made available to it.
1255 This is mostly relevant for slow back ends where the user may
1256 wish to widen the summary buffer to include all headers
1257 that were fetched."
1258   :version "22.1"
1259   :group 'gnus-summary
1260   :type '(choice boolean regexp))
1261
1262 (defcustom gnus-summary-pipe-output-default-command nil
1263   "Command (and optional arguments) used to pipe article to subprocess.
1264 This will be used as the default command if it is non-nil.  The value
1265 will be updated if you modify it when executing the command
1266 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1267   :version "23.1" ;; No Gnus
1268   :group 'gnus-summary
1269   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1270
1271 (defcustom gnus-summary-muttprint-program "muttprint"
1272   "Command (and optional arguments) used to run Muttprint.
1273 The value will be updated if you modify it when executing the command
1274 `gnus-summary-muttprint'."
1275   :version "22.1"
1276   :group 'gnus-summary
1277   :type 'string)
1278
1279 (defcustom gnus-article-loose-mime t
1280   "If non-nil, don't require MIME-Version header.
1281 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1282 supply the MIME-Version header or deliberately strip it from the mail.
1283 If non-nil (the default), Gnus will treat some articles as MIME
1284 even if the MIME-Version header is missing."
1285   :version "22.1"
1286   :type 'boolean
1287   :group 'gnus-article-mime)
1288
1289 (defcustom gnus-article-emulate-mime t
1290   "If non-nil, use MIME emulation for uuencode and the like.
1291 This means that Gnus will search message bodies for text that look
1292 like uuencoded bits, yEncoded bits, and so on, and present that using
1293 the normal Gnus MIME machinery."
1294   :version "22.1"
1295   :type 'boolean
1296   :group 'gnus-article-mime)
1297
1298 ;;; Internal variables
1299
1300 (defvar gnus-summary-display-cache nil)
1301 (defvar gnus-article-mime-handles nil)
1302 (defvar gnus-article-decoded-p nil)
1303 (defvar gnus-article-charset nil)
1304 (defvar gnus-article-ignored-charsets nil)
1305 (defvar gnus-scores-exclude-files nil)
1306 (defvar gnus-page-broken nil)
1307
1308 (defvar gnus-original-article nil)
1309 (defvar gnus-article-internal-prepare-hook nil)
1310 (defvar gnus-newsgroup-process-stack nil)
1311
1312 (defvar gnus-thread-indent-array nil)
1313 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1314 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1315   "Function called to sort the articles within a thread after it has been gathered together.")
1316
1317 (defvar gnus-summary-save-parts-type-history nil)
1318 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1319
1320 ;; Avoid highlighting in kill files.
1321 (defvar gnus-summary-inhibit-highlight nil)
1322 (defvar gnus-newsgroup-selected-overlay nil)
1323 (defvar gnus-inhibit-limiting nil)
1324 (defvar gnus-newsgroup-adaptive-score-file nil)
1325 (defvar gnus-current-score-file nil)
1326 (defvar gnus-current-move-group nil)
1327 (defvar gnus-current-copy-group nil)
1328 (defvar gnus-current-crosspost-group nil)
1329 (defvar gnus-newsgroup-display nil)
1330
1331 (defvar gnus-newsgroup-dependencies nil)
1332 (defvar gnus-newsgroup-adaptive nil)
1333 (defvar gnus-summary-display-article-function nil)
1334 (defvar gnus-summary-highlight-line-function nil
1335   "Function called after highlighting a summary line.")
1336
1337 (defvar gnus-summary-line-format-alist
1338   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1339     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1340     (?s gnus-tmp-subject-or-nil ?s)
1341     (?n gnus-tmp-name ?s)
1342     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1343         ?s)
1344     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1345             gnus-tmp-from) ?s)
1346     (?F gnus-tmp-from ?s)
1347     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1348     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1349     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1350     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1351     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1352     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1353     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1354     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1355     (?L gnus-tmp-lines ?s)
1356     (?O gnus-tmp-downloaded ?c)
1357     (?I gnus-tmp-indentation ?s)
1358     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1359     (?R gnus-tmp-replied ?c)
1360     (?\[ gnus-tmp-opening-bracket ?c)
1361     (?\] gnus-tmp-closing-bracket ?c)
1362     (?\> (make-string gnus-tmp-level ? ) ?s)
1363     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1364     (?i gnus-tmp-score ?d)
1365     (?z gnus-tmp-score-char ?c)
1366     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1367     (?U gnus-tmp-unread ?c)
1368     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1369         ?s)
1370     (?t (gnus-summary-number-of-articles-in-thread
1371          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1372         ?d)
1373     (?e (gnus-summary-number-of-articles-in-thread
1374          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1375         ?c)
1376     (?u gnus-tmp-user-defined ?s)
1377     (?P (gnus-pick-line-number) ?d)
1378     (?B gnus-tmp-thread-tree-header-string ?s)
1379     (user-date (gnus-user-date
1380                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1381   "An alist of format specifications that can appear in summary lines.
1382 These are paired with what variables they correspond with, along with
1383 the type of the variable (string, integer, character, etc).")
1384
1385 (defvar gnus-summary-dummy-line-format-alist
1386   `((?S gnus-tmp-subject ?s)
1387     (?N gnus-tmp-number ?d)
1388     (?u gnus-tmp-user-defined ?s)))
1389
1390 (defvar gnus-summary-mode-line-format-alist
1391   `((?G gnus-tmp-group-name ?s)
1392     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1393     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1394     (?A gnus-tmp-article-number ?d)
1395     (?Z gnus-tmp-unread-and-unselected ?s)
1396     (?V gnus-version ?s)
1397     (?U gnus-tmp-unread-and-unticked ?d)
1398     (?S gnus-tmp-subject ?s)
1399     (?e gnus-tmp-unselected ?d)
1400     (?u gnus-tmp-user-defined ?s)
1401     (?d (length gnus-newsgroup-dormant) ?d)
1402     (?t (length gnus-newsgroup-marked) ?d)
1403     (?h (length gnus-newsgroup-spam-marked) ?d)
1404     (?r (length gnus-newsgroup-reads) ?d)
1405     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1406     (?E gnus-newsgroup-expunged-tally ?d)
1407     (?s (gnus-current-score-file-nondirectory) ?s)))
1408
1409 ;; This is here rather than in gnus-art for compilation reasons.
1410 (defvar gnus-article-mode-line-format-alist
1411   (nconc '((?w (gnus-article-wash-status) ?s)
1412            (?m (gnus-article-mime-part-status) ?s))
1413          gnus-summary-mode-line-format-alist))
1414
1415 (defvar gnus-last-search-regexp nil
1416   "Default regexp for article search command.")
1417
1418 (defvar gnus-last-shell-command nil
1419   "Default shell command on article.")
1420
1421 (defvar gnus-newsgroup-agentized nil
1422   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1423 (defvar gnus-newsgroup-begin nil)
1424 (defvar gnus-newsgroup-end nil)
1425 (defvar gnus-newsgroup-last-rmail nil)
1426 (defvar gnus-newsgroup-last-mail nil)
1427 (defvar gnus-newsgroup-last-folder nil)
1428 (defvar gnus-newsgroup-last-file nil)
1429 (defvar gnus-newsgroup-last-directory nil)
1430 (defvar gnus-newsgroup-auto-expire nil)
1431 (defvar gnus-newsgroup-active nil)
1432
1433 (defvar gnus-newsgroup-data nil)
1434 (defvar gnus-newsgroup-data-reverse nil)
1435 (defvar gnus-newsgroup-limit nil)
1436 (defvar gnus-newsgroup-limits nil)
1437 (defvar gnus-summary-use-undownloaded-faces nil)
1438
1439 (defvar gnus-newsgroup-unreads nil
1440   "Sorted list of unread articles in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-unselected nil
1443   "Sorted list of unselected unread articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-reads nil
1446   "Alist of read articles and article marks in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-expunged-tally nil)
1449
1450 (defvar gnus-newsgroup-marked nil
1451   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1452
1453 (defvar gnus-newsgroup-spam-marked nil
1454   "List of ranges of articles that have been marked as spam.")
1455
1456 (defvar gnus-newsgroup-killed nil
1457   "List of ranges of articles that have been through the scoring process.")
1458
1459 (defvar gnus-newsgroup-cached nil
1460   "Sorted list of articles that come from the article cache.")
1461
1462 (defvar gnus-newsgroup-saved nil
1463   "List of articles that have been saved.")
1464
1465 (defvar gnus-newsgroup-kill-headers nil)
1466
1467 (defvar gnus-newsgroup-replied nil
1468   "List of articles that have been replied to in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-forwarded nil
1471   "List of articles that have been forwarded in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-recent nil
1474   "List of articles that have are recent in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-expirable nil
1477   "Sorted list of articles in the current newsgroup that can be expired.")
1478
1479 (defvar gnus-newsgroup-processable nil
1480   "List of articles in the current newsgroup that can be processed.")
1481
1482 (defvar gnus-newsgroup-downloadable nil
1483   "Sorted list of articles in the current newsgroup that can be processed.")
1484
1485 (defvar gnus-newsgroup-unfetched nil
1486   "Sorted list of articles in the current newsgroup whose headers have
1487 not been fetched into the agent.
1488
1489 This list will always be a subset of gnus-newsgroup-undownloaded.")
1490
1491 (defvar gnus-newsgroup-undownloaded nil
1492   "List of articles in the current newsgroup that haven't been downloaded.")
1493
1494 (defvar gnus-newsgroup-unsendable nil
1495   "List of articles in the current newsgroup that won't be sent.")
1496
1497 (defvar gnus-newsgroup-bookmarks nil
1498   "List of articles in the current newsgroup that have bookmarks.")
1499
1500 (defvar gnus-newsgroup-dormant nil
1501   "Sorted list of dormant articles in the current newsgroup.")
1502
1503 (defvar gnus-newsgroup-unseen nil
1504   "List of unseen articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-seen nil
1507   "Range of seen articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-articles nil
1510   "List of articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-scored nil
1513   "List of scored articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-headers nil
1516   "List of article headers in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-threads nil)
1519
1520 (defvar gnus-newsgroup-prepared nil
1521   "Whether the current group has been prepared properly.")
1522
1523 (defvar gnus-newsgroup-ancient nil
1524   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-sparse nil)
1527
1528 (defvar gnus-current-article nil)
1529 (defvar gnus-article-current nil)
1530 (defvar gnus-current-headers nil)
1531 (defvar gnus-have-all-headers nil)
1532 (defvar gnus-last-article nil)
1533 (defvar gnus-newsgroup-history nil)
1534 (defvar gnus-newsgroup-charset nil)
1535 (defvar gnus-newsgroup-ephemeral-charset nil)
1536 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1537
1538 (defvar gnus-article-before-search nil)
1539
1540 (defvar gnus-summary-local-variables
1541   '(gnus-newsgroup-name
1542     gnus-newsgroup-begin gnus-newsgroup-end
1543     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1544     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1545     gnus-newsgroup-last-directory
1546     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1547     gnus-newsgroup-unselected gnus-newsgroup-marked
1548     gnus-newsgroup-spam-marked
1549     gnus-newsgroup-reads gnus-newsgroup-saved
1550     gnus-newsgroup-replied gnus-newsgroup-forwarded
1551     gnus-newsgroup-recent
1552     gnus-newsgroup-expirable
1553     gnus-newsgroup-processable gnus-newsgroup-killed
1554     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1555     gnus-newsgroup-unfetched
1556     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1557     gnus-newsgroup-seen gnus-newsgroup-articles
1558     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1559     gnus-newsgroup-headers gnus-newsgroup-threads
1560     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1561     gnus-current-article gnus-current-headers gnus-have-all-headers
1562     gnus-last-article gnus-article-internal-prepare-hook
1563     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1564     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1565     gnus-thread-expunge-below
1566     gnus-score-alist gnus-current-score-file
1567     (gnus-summary-expunge-below . global)
1568     (gnus-summary-mark-below . global)
1569     (gnus-orphan-score . global)
1570     gnus-newsgroup-active gnus-scores-exclude-files
1571     gnus-newsgroup-history gnus-newsgroup-ancient
1572     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1573     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1574     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1575     (gnus-newsgroup-expunged-tally . 0)
1576     gnus-cache-removable-articles gnus-newsgroup-cached
1577     gnus-newsgroup-data gnus-newsgroup-data-reverse
1578     gnus-newsgroup-limit gnus-newsgroup-limits
1579     gnus-newsgroup-charset gnus-newsgroup-display
1580     gnus-summary-use-undownloaded-faces)
1581   "Variables that are buffer-local to the summary buffers.")
1582
1583 (defvar gnus-newsgroup-variables nil
1584   "A list of variables that have separate values in different newsgroups.
1585 A list of newsgroup (summary buffer) local variables, or cons of
1586 variables and their default expressions to be evalled (when the default
1587 values are not nil), that should be made global while the summary buffer
1588 is active.
1589
1590 Note: The default expressions will be evaluated (using function `eval')
1591 before assignment to the local variable rather than just assigned to it.
1592 If the default expression is the symbol `global', that symbol will not
1593 be evaluated but the global value of the local variable will be used
1594 instead.
1595
1596 These variables can be used to set variables in the group parameters
1597 while still allowing them to affect operations done in other buffers.
1598 For example:
1599
1600 \(setq gnus-newsgroup-variables
1601      '(message-use-followup-to
1602        (gnus-visible-headers .
1603          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1604 ")
1605
1606 (eval-when-compile
1607   ;; Bind features so that require will believe that gnus-sum has
1608   ;; already been loaded (avoids infinite recursion)
1609   (let ((features (cons 'gnus-sum features)))
1610     (require 'gnus-art)))
1611
1612 ;; MIME stuff.
1613
1614 (defvar gnus-decode-encoded-word-methods
1615   '(mail-decode-encoded-word-string)
1616   "List of methods used to decode encoded words.
1617
1618 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1619 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1620 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1621 whose names match REGEXP.
1622
1623 For example:
1624 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1625  mail-decode-encoded-word-string
1626  (\"chinese\" . rfc1843-decode-string))")
1627
1628 (defvar gnus-decode-encoded-word-methods-cache nil)
1629
1630 (defun gnus-multi-decode-encoded-word-string (string)
1631   "Apply the functions from `gnus-encoded-word-methods' that match."
1632   (unless (and gnus-decode-encoded-word-methods-cache
1633                (eq gnus-newsgroup-name
1634                    (car gnus-decode-encoded-word-methods-cache)))
1635     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1636     (dolist (method gnus-decode-encoded-word-methods)
1637       (if (symbolp method)
1638           (nconc gnus-decode-encoded-word-methods-cache (list method))
1639         (if (and gnus-newsgroup-name
1640                  (string-match (car method) gnus-newsgroup-name))
1641             (nconc gnus-decode-encoded-word-methods-cache
1642                    (list (cdr method)))))))
1643   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1644     (setq string (funcall method string))))
1645
1646 ;; Subject simplification.
1647
1648 (defun gnus-simplify-whitespace (str)
1649   "Remove excessive whitespace from STR."
1650   ;; Multiple spaces.
1651   (while (string-match "[ \t][ \t]+" str)
1652     (setq str (concat (substring str 0 (match-beginning 0))
1653                         " "
1654                         (substring str (match-end 0)))))
1655   ;; Leading spaces.
1656   (when (string-match "^[ \t]+" str)
1657     (setq str (substring str (match-end 0))))
1658   ;; Trailing spaces.
1659   (when (string-match "[ \t]+$" str)
1660     (setq str (substring str 0 (match-beginning 0))))
1661   str)
1662
1663 (defun gnus-simplify-all-whitespace (str)
1664   "Remove all whitespace from STR."
1665   (while (string-match "[ \t\n]+" str)
1666     (setq str (replace-match "" nil nil str)))
1667   str)
1668
1669 (defsubst gnus-simplify-subject-re (subject)
1670   "Remove \"Re:\" from subject lines."
1671   (if (string-match message-subject-re-regexp subject)
1672       (substring subject (match-end 0))
1673     subject))
1674
1675 (defun gnus-simplify-subject (subject &optional re-only)
1676   "Remove `Re:' and words in parentheses.
1677 If RE-ONLY is non-nil, strip leading `Re:'s only."
1678   (let ((case-fold-search t))           ;Ignore case.
1679     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1680     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1681       (setq subject (substring subject (match-end 0))))
1682     ;; Remove uninteresting prefixes.
1683     (when (and (not re-only)
1684                gnus-simplify-ignored-prefixes
1685                (string-match gnus-simplify-ignored-prefixes subject))
1686       (setq subject (substring subject (match-end 0))))
1687     ;; Remove words in parentheses from end.
1688     (unless re-only
1689       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1690         (setq subject (substring subject 0 (match-beginning 0)))))
1691     ;; Return subject string.
1692     subject))
1693
1694 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1695 ;; all whitespace.
1696 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1697   (goto-char (point-min))
1698   (while (re-search-forward regexp nil t)
1699     (replace-match (or newtext ""))))
1700
1701 (defun gnus-simplify-buffer-fuzzy ()
1702   "Simplify string in the buffer fuzzily.
1703 The string in the accessible portion of the current buffer is simplified.
1704 It is assumed to be a single-line subject.
1705 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1706 matter is removed.  Additional things can be deleted by setting
1707 `gnus-simplify-subject-fuzzy-regexp'."
1708   (let ((case-fold-search t)
1709         (modified-tick))
1710     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1711
1712     (while (not (eq modified-tick (buffer-modified-tick)))
1713       (setq modified-tick (buffer-modified-tick))
1714       (cond
1715        ((listp gnus-simplify-subject-fuzzy-regexp)
1716         (mapc 'gnus-simplify-buffer-fuzzy-step
1717               gnus-simplify-subject-fuzzy-regexp))
1718        (gnus-simplify-subject-fuzzy-regexp
1719         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1720       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1721       (gnus-simplify-buffer-fuzzy-step
1722        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1723       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1724
1725     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1726     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1727     (gnus-simplify-buffer-fuzzy-step " $")
1728     (gnus-simplify-buffer-fuzzy-step "^ +")))
1729
1730 (defun gnus-simplify-subject-fuzzy (subject)
1731   "Simplify a subject string fuzzily.
1732 See `gnus-simplify-buffer-fuzzy' for details."
1733   (save-excursion
1734     (gnus-set-work-buffer)
1735     (let ((case-fold-search t))
1736       ;; Remove uninteresting prefixes.
1737       (when (and gnus-simplify-ignored-prefixes
1738                  (string-match gnus-simplify-ignored-prefixes subject))
1739         (setq subject (substring subject (match-end 0))))
1740       (insert subject)
1741       (inline (gnus-simplify-buffer-fuzzy))
1742       (buffer-string))))
1743
1744 (defsubst gnus-simplify-subject-fully (subject)
1745   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1746   (cond
1747    (gnus-simplify-subject-functions
1748     (gnus-map-function gnus-simplify-subject-functions subject))
1749    ((null gnus-summary-gather-subject-limit)
1750     (gnus-simplify-subject-re subject))
1751    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1752     (gnus-simplify-subject-fuzzy subject))
1753    ((numberp gnus-summary-gather-subject-limit)
1754     (truncate-string-to-width (gnus-simplify-subject-re subject)
1755                               gnus-summary-gather-subject-limit))
1756    (t
1757     subject)))
1758
1759 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1760   "Check whether two subjects are equal.
1761 If optional argument SIMPLE-FIRST is t, first argument is already
1762 simplified."
1763   (cond
1764    ((null simple-first)
1765     (equal (gnus-simplify-subject-fully s1)
1766            (gnus-simplify-subject-fully s2)))
1767    (t
1768     (equal s1
1769            (gnus-simplify-subject-fully s2)))))
1770
1771 (defun gnus-summary-bubble-group ()
1772   "Increase the score of the current group.
1773 This is a handy function to add to `gnus-summary-exit-hook' to
1774 increase the score of each group you read."
1775   (gnus-group-add-score gnus-newsgroup-name))
1776
1777 \f
1778 ;;;
1779 ;;; Gnus summary mode
1780 ;;;
1781
1782 (put 'gnus-summary-mode 'mode-class 'special)
1783
1784 (defvar gnus-article-commands-menu)
1785
1786 ;; Non-orthogonal keys
1787
1788 (gnus-define-keys gnus-summary-mode-map
1789   " " gnus-summary-next-page
1790   "\177" gnus-summary-prev-page
1791   [delete] gnus-summary-prev-page
1792   [backspace] gnus-summary-prev-page
1793   "\r" gnus-summary-scroll-up
1794   "\M-\r" gnus-summary-scroll-down
1795   "n" gnus-summary-next-unread-article
1796   "p" gnus-summary-prev-unread-article
1797   "N" gnus-summary-next-article
1798   "P" gnus-summary-prev-article
1799   "\M-\C-n" gnus-summary-next-same-subject
1800   "\M-\C-p" gnus-summary-prev-same-subject
1801   "\M-n" gnus-summary-next-unread-subject
1802   "\M-p" gnus-summary-prev-unread-subject
1803   "." gnus-summary-first-unread-article
1804   "," gnus-summary-best-unread-article
1805   "\M-s" gnus-summary-search-article-forward
1806   "\M-r" gnus-summary-search-article-backward
1807   "\M-S" gnus-summary-repeat-search-article-forward
1808   "\M-R" gnus-summary-repeat-search-article-backward
1809   "<" gnus-summary-beginning-of-article
1810   ">" gnus-summary-end-of-article
1811   "j" gnus-summary-goto-article
1812   "^" gnus-summary-refer-parent-article
1813   "\M-^" gnus-summary-refer-article
1814   "u" gnus-summary-tick-article-forward
1815   "!" gnus-summary-tick-article-forward
1816   "U" gnus-summary-tick-article-backward
1817   "d" gnus-summary-mark-as-read-forward
1818   "D" gnus-summary-mark-as-read-backward
1819   "E" gnus-summary-mark-as-expirable
1820   "\M-u" gnus-summary-clear-mark-forward
1821   "\M-U" gnus-summary-clear-mark-backward
1822   "k" gnus-summary-kill-same-subject-and-select
1823   "\C-k" gnus-summary-kill-same-subject
1824   "\M-\C-k" gnus-summary-kill-thread
1825   "\M-\C-l" gnus-summary-lower-thread
1826   "e" gnus-summary-edit-article
1827   "#" gnus-summary-mark-as-processable
1828   "\M-#" gnus-summary-unmark-as-processable
1829   "\M-\C-t" gnus-summary-toggle-threads
1830   "\M-\C-s" gnus-summary-show-thread
1831   "\M-\C-h" gnus-summary-hide-thread
1832   "\M-\C-f" gnus-summary-next-thread
1833   "\M-\C-b" gnus-summary-prev-thread
1834   [(meta down)] gnus-summary-next-thread
1835   [(meta up)] gnus-summary-prev-thread
1836   "\M-\C-u" gnus-summary-up-thread
1837   "\M-\C-d" gnus-summary-down-thread
1838   "&" gnus-summary-execute-command
1839   "c" gnus-summary-catchup-and-exit
1840   "\C-w" gnus-summary-mark-region-as-read
1841   "\C-t" gnus-summary-toggle-truncation
1842   "?" gnus-summary-mark-as-dormant
1843   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1844   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1845   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1846   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1847   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1848   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1849   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1850   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1851   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1852   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1853   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1854   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1855   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1856   "=" gnus-summary-expand-window
1857   "\C-x\C-s" gnus-summary-reselect-current-group
1858   "\M-g" gnus-summary-rescan-group
1859   "\C-c\C-r" gnus-summary-caesar-message
1860   "f" gnus-summary-followup
1861   "F" gnus-summary-followup-with-original
1862   "C" gnus-summary-cancel-article
1863   "r" gnus-summary-reply
1864   "R" gnus-summary-reply-with-original
1865   "\C-c\C-f" gnus-summary-mail-forward
1866   "o" gnus-summary-save-article
1867   "\C-o" gnus-summary-save-article-mail
1868   "|" gnus-summary-pipe-output
1869   "\M-k" gnus-summary-edit-local-kill
1870   "\M-K" gnus-summary-edit-global-kill
1871   ;; "V" gnus-version
1872   "\C-c\C-d" gnus-summary-describe-group
1873   "q" gnus-summary-exit
1874   "Q" gnus-summary-exit-no-update
1875   "\C-c\C-i" gnus-info-find-node
1876   gnus-mouse-2 gnus-mouse-pick-article
1877   [follow-link] mouse-face
1878   "m" gnus-summary-mail-other-window
1879   "a" gnus-summary-post-news
1880   "x" gnus-summary-limit-to-unread
1881   "s" gnus-summary-isearch-article
1882   "t" gnus-summary-toggle-header
1883   "g" gnus-summary-show-article
1884   "l" gnus-summary-goto-last-article
1885   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1886   "\C-d" gnus-summary-enter-digest-group
1887   "\M-\C-d" gnus-summary-read-document
1888   "\M-\C-e" gnus-summary-edit-parameters
1889   "\M-\C-a" gnus-summary-customize-parameters
1890   "\C-c\C-b" gnus-bug
1891   "*" gnus-cache-enter-article
1892   "\M-*" gnus-cache-remove-article
1893   "\M-&" gnus-summary-universal-argument
1894   "\C-l" gnus-recenter
1895   "I" gnus-summary-increase-score
1896   "L" gnus-summary-lower-score
1897   "\M-i" gnus-symbolic-argument
1898   "h" gnus-summary-select-article-buffer
1899
1900   "b" gnus-article-view-part
1901   "\M-t" gnus-summary-toggle-display-buttonized
1902
1903   "V" gnus-summary-score-map
1904   "X" gnus-uu-extract-map
1905   "S" gnus-summary-send-map)
1906
1907 ;; Sort of orthogonal keymap
1908 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1909   "t" gnus-summary-tick-article-forward
1910   "!" gnus-summary-tick-article-forward
1911   "d" gnus-summary-mark-as-read-forward
1912   "r" gnus-summary-mark-as-read-forward
1913   "c" gnus-summary-clear-mark-forward
1914   " " gnus-summary-clear-mark-forward
1915   "e" gnus-summary-mark-as-expirable
1916   "x" gnus-summary-mark-as-expirable
1917   "?" gnus-summary-mark-as-dormant
1918   "b" gnus-summary-set-bookmark
1919   "B" gnus-summary-remove-bookmark
1920   "#" gnus-summary-mark-as-processable
1921   "\M-#" gnus-summary-unmark-as-processable
1922   "S" gnus-summary-limit-include-expunged
1923   "C" gnus-summary-catchup
1924   "H" gnus-summary-catchup-to-here
1925   "h" gnus-summary-catchup-from-here
1926   "\C-c" gnus-summary-catchup-all
1927   "k" gnus-summary-kill-same-subject-and-select
1928   "K" gnus-summary-kill-same-subject
1929   "P" gnus-uu-mark-map)
1930
1931 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1932   "c" gnus-summary-clear-above
1933   "u" gnus-summary-tick-above
1934   "m" gnus-summary-mark-above
1935   "k" gnus-summary-kill-below)
1936
1937 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1938   "/" gnus-summary-limit-to-subject
1939   "n" gnus-summary-limit-to-articles
1940   "b" gnus-summary-limit-to-bodies
1941   "h" gnus-summary-limit-to-headers
1942   "w" gnus-summary-pop-limit
1943   "s" gnus-summary-limit-to-subject
1944   "a" gnus-summary-limit-to-author
1945   "u" gnus-summary-limit-to-unread
1946   "m" gnus-summary-limit-to-marks
1947   "M" gnus-summary-limit-exclude-marks
1948   "v" gnus-summary-limit-to-score
1949   "*" gnus-summary-limit-include-cached
1950   "D" gnus-summary-limit-include-dormant
1951   "T" gnus-summary-limit-include-thread
1952   "d" gnus-summary-limit-exclude-dormant
1953   "t" gnus-summary-limit-to-age
1954   "." gnus-summary-limit-to-unseen
1955   "x" gnus-summary-limit-to-extra
1956   "p" gnus-summary-limit-to-display-predicate
1957   "E" gnus-summary-limit-include-expunged
1958   "c" gnus-summary-limit-exclude-childless-dormant
1959   "C" gnus-summary-limit-mark-excluded-as-read
1960   "o" gnus-summary-insert-old-articles
1961   "N" gnus-summary-insert-new-articles
1962   "S" gnus-summary-limit-to-singletons
1963   "r" gnus-summary-limit-to-replied
1964   "R" gnus-summary-limit-to-recipient
1965   "A" gnus-summary-limit-to-address)
1966
1967 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1968   "n" gnus-summary-next-unread-article
1969   "p" gnus-summary-prev-unread-article
1970   "N" gnus-summary-next-article
1971   "P" gnus-summary-prev-article
1972   "\C-n" gnus-summary-next-same-subject
1973   "\C-p" gnus-summary-prev-same-subject
1974   "\M-n" gnus-summary-next-unread-subject
1975   "\M-p" gnus-summary-prev-unread-subject
1976   "f" gnus-summary-first-unread-article
1977   "b" gnus-summary-best-unread-article
1978   "j" gnus-summary-goto-article
1979   "g" gnus-summary-goto-subject
1980   "l" gnus-summary-goto-last-article
1981   "o" gnus-summary-pop-article)
1982
1983 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1984   "k" gnus-summary-kill-thread
1985   "E" gnus-summary-expire-thread
1986   "l" gnus-summary-lower-thread
1987   "i" gnus-summary-raise-thread
1988   "T" gnus-summary-toggle-threads
1989   "t" gnus-summary-rethread-current
1990   "^" gnus-summary-reparent-thread
1991   "\M-^" gnus-summary-reparent-children
1992   "s" gnus-summary-show-thread
1993   "S" gnus-summary-show-all-threads
1994   "h" gnus-summary-hide-thread
1995   "H" gnus-summary-hide-all-threads
1996   "n" gnus-summary-next-thread
1997   "p" gnus-summary-prev-thread
1998   "u" gnus-summary-up-thread
1999   "o" gnus-summary-top-thread
2000   "d" gnus-summary-down-thread
2001   "#" gnus-uu-mark-thread
2002   "\M-#" gnus-uu-unmark-thread)
2003
2004 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2005   "g" gnus-summary-prepare
2006   "c" gnus-summary-insert-cached-articles
2007   "d" gnus-summary-insert-dormant-articles
2008   "t" gnus-summary-insert-ticked-articles)
2009
2010 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2011   "c" gnus-summary-catchup-and-exit
2012   "C" gnus-summary-catchup-all-and-exit
2013   "E" gnus-summary-exit-no-update
2014   "Q" gnus-summary-exit
2015   "Z" gnus-summary-exit
2016   "n" gnus-summary-catchup-and-goto-next-group
2017   "p" gnus-summary-catchup-and-goto-prev-group
2018   "R" gnus-summary-reselect-current-group
2019   "G" gnus-summary-rescan-group
2020   "N" gnus-summary-next-group
2021   "s" gnus-summary-save-newsrc
2022   "P" gnus-summary-prev-group)
2023
2024 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2025   " " gnus-summary-next-page
2026   "n" gnus-summary-next-page
2027   "\177" gnus-summary-prev-page
2028   [delete] gnus-summary-prev-page
2029   "p" gnus-summary-prev-page
2030   "\r" gnus-summary-scroll-up
2031   "\M-\r" gnus-summary-scroll-down
2032   "<" gnus-summary-beginning-of-article
2033   ">" gnus-summary-end-of-article
2034   "b" gnus-summary-beginning-of-article
2035   "e" gnus-summary-end-of-article
2036   "^" gnus-summary-refer-parent-article
2037   "r" gnus-summary-refer-parent-article
2038   "D" gnus-summary-enter-digest-group
2039   "R" gnus-summary-refer-references
2040   "T" gnus-summary-refer-thread
2041   "g" gnus-summary-show-article
2042   "s" gnus-summary-isearch-article
2043   "P" gnus-summary-print-article
2044   "S" gnus-sticky-article
2045   "M" gnus-mailing-list-insinuate
2046   "t" gnus-article-babel)
2047
2048 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2049   "b" gnus-article-add-buttons
2050   "B" gnus-article-add-buttons-to-head
2051   "o" gnus-article-treat-overstrike
2052   "e" gnus-article-emphasize
2053   "w" gnus-article-fill-cited-article
2054   "Q" gnus-article-fill-long-lines
2055   "L" gnus-article-toggle-truncate-lines
2056   "C" gnus-article-capitalize-sentences
2057   "c" gnus-article-remove-cr
2058   "q" gnus-article-de-quoted-unreadable
2059   "6" gnus-article-de-base64-unreadable
2060   "Z" gnus-article-decode-HZ
2061   "A" gnus-article-treat-ansi-sequences
2062   "h" gnus-article-wash-html
2063   "u" gnus-article-unsplit-urls
2064   "s" gnus-summary-force-verify-and-decrypt
2065   "f" gnus-article-display-x-face
2066   "l" gnus-summary-stop-page-breaking
2067   "r" gnus-summary-caesar-message
2068   "m" gnus-summary-morse-message
2069   "t" gnus-summary-toggle-header
2070   "g" gnus-treat-smiley
2071   "v" gnus-summary-verbose-headers
2072   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2073   "p" gnus-article-verify-x-pgp-sig
2074   "d" gnus-article-treat-dumbquotes
2075   "i" gnus-summary-idna-message)
2076
2077 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2078   ;; mnemonic: deuglif*Y*
2079   "u" gnus-article-outlook-unwrap-lines
2080   "a" gnus-article-outlook-repair-attribution
2081   "c" gnus-article-outlook-rearrange-citation
2082   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2083
2084 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2085   "a" gnus-article-hide
2086   "h" gnus-article-hide-headers
2087   "b" gnus-article-hide-boring-headers
2088   "s" gnus-article-hide-signature
2089   "c" gnus-article-hide-citation
2090   "C" gnus-article-hide-citation-in-followups
2091   "l" gnus-article-hide-list-identifiers
2092   "B" gnus-article-strip-banner
2093   "P" gnus-article-hide-pem
2094   "\C-c" gnus-article-hide-citation-maybe)
2095
2096 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2097   "a" gnus-article-highlight
2098   "h" gnus-article-highlight-headers
2099   "c" gnus-article-highlight-citation
2100   "s" gnus-article-highlight-signature)
2101
2102 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2103   "f" gnus-article-treat-fold-headers
2104   "u" gnus-article-treat-unfold-headers
2105   "n" gnus-article-treat-fold-newsgroups)
2106
2107 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2108   "x" gnus-article-display-x-face
2109   "d" gnus-article-display-face
2110   "s" gnus-treat-smiley
2111   "D" gnus-article-remove-images
2112   "W" gnus-html-show-images
2113   "f" gnus-treat-from-picon
2114   "m" gnus-treat-mail-picon
2115   "n" gnus-treat-newsgroups-picon)
2116
2117 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2118   "w" gnus-article-decode-mime-words
2119   "c" gnus-article-decode-charset
2120   "v" gnus-mime-view-all-parts
2121   "b" gnus-article-view-part)
2122
2123 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2124   "z" gnus-article-date-ut
2125   "u" gnus-article-date-ut
2126   "l" gnus-article-date-local
2127   "p" gnus-article-date-english
2128   "e" gnus-article-date-lapsed
2129   "o" gnus-article-date-original
2130   "i" gnus-article-date-iso8601
2131   "s" gnus-article-date-user)
2132
2133 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2134   "t" gnus-article-remove-trailing-blank-lines
2135   "l" gnus-article-strip-leading-blank-lines
2136   "m" gnus-article-strip-multiple-blank-lines
2137   "a" gnus-article-strip-blank-lines
2138   "A" gnus-article-strip-all-blank-lines
2139   "s" gnus-article-strip-leading-space
2140   "e" gnus-article-strip-trailing-space
2141   "w" gnus-article-remove-leading-whitespace)
2142
2143 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2144   "v" gnus-version
2145   "f" gnus-summary-fetch-faq
2146   "d" gnus-summary-describe-group
2147   "h" gnus-summary-describe-briefly
2148   "i" gnus-info-find-node
2149   "c" gnus-group-fetch-charter
2150   "C" gnus-group-fetch-control)
2151
2152 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2153   "e" gnus-summary-expire-articles
2154   "\M-\C-e" gnus-summary-expire-articles-now
2155   "\177" gnus-summary-delete-article
2156   [delete] gnus-summary-delete-article
2157   [backspace] gnus-summary-delete-article
2158   "m" gnus-summary-move-article
2159   "r" gnus-summary-respool-article
2160   "w" gnus-summary-edit-article
2161   "c" gnus-summary-copy-article
2162   "B" gnus-summary-crosspost-article
2163   "q" gnus-summary-respool-query
2164   "t" gnus-summary-respool-trace
2165   "i" gnus-summary-import-article
2166   "I" gnus-summary-create-article
2167   "p" gnus-summary-article-posted-p)
2168
2169 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2170   "o" gnus-summary-save-article
2171   "m" gnus-summary-save-article-mail
2172   "F" gnus-summary-write-article-file
2173   "r" gnus-summary-save-article-rmail
2174   "f" gnus-summary-save-article-file
2175   "b" gnus-summary-save-article-body-file
2176   "B" gnus-summary-write-article-body-file
2177   "h" gnus-summary-save-article-folder
2178   "v" gnus-summary-save-article-vm
2179   "p" gnus-summary-pipe-output
2180   "P" gnus-summary-muttprint)
2181
2182 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2183   "b" gnus-summary-display-buttonized
2184   "m" gnus-summary-repair-multipart
2185   "v" gnus-article-view-part
2186   "o" gnus-article-save-part
2187   "O" gnus-article-save-part-and-strip
2188   "r" gnus-article-replace-part
2189   "d" gnus-article-delete-part
2190   "t" gnus-article-view-part-as-type
2191   "j" gnus-article-jump-to-part
2192   "c" gnus-article-copy-part
2193   "C" gnus-article-view-part-as-charset
2194   "e" gnus-article-view-part-externally
2195   "H" gnus-article-browse-html-article
2196   "E" gnus-article-encrypt-body
2197   "i" gnus-article-inline-part
2198   "|" gnus-article-pipe-part)
2199
2200 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2201   "p" gnus-summary-mark-as-processable
2202   "u" gnus-summary-unmark-as-processable
2203   "U" gnus-summary-unmark-all-processable
2204   "v" gnus-uu-mark-over
2205   "s" gnus-uu-mark-series
2206   "r" gnus-uu-mark-region
2207   "g" gnus-uu-unmark-region
2208   "R" gnus-uu-mark-by-regexp
2209   "G" gnus-uu-unmark-by-regexp
2210   "t" gnus-uu-mark-thread
2211   "T" gnus-uu-unmark-thread
2212   "a" gnus-uu-mark-all
2213   "b" gnus-uu-mark-buffer
2214   "S" gnus-uu-mark-sparse
2215   "k" gnus-summary-kill-process-mark
2216   "y" gnus-summary-yank-process-mark
2217   "w" gnus-summary-save-process-mark
2218   "i" gnus-uu-invert-processable)
2219
2220 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2221   ;;"x" gnus-uu-extract-any
2222   "m" gnus-summary-save-parts
2223   "u" gnus-uu-decode-uu
2224   "U" gnus-uu-decode-uu-and-save
2225   "s" gnus-uu-decode-unshar
2226   "S" gnus-uu-decode-unshar-and-save
2227   "o" gnus-uu-decode-save
2228   "O" gnus-uu-decode-save
2229   "b" gnus-uu-decode-binhex
2230   "B" gnus-uu-decode-binhex
2231   "Y" gnus-uu-decode-yenc
2232   "p" gnus-uu-decode-postscript
2233   "P" gnus-uu-decode-postscript-and-save)
2234
2235 (gnus-define-keys
2236     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2237   "u" gnus-uu-decode-uu-view
2238   "U" gnus-uu-decode-uu-and-save-view
2239   "s" gnus-uu-decode-unshar-view
2240   "S" gnus-uu-decode-unshar-and-save-view
2241   "o" gnus-uu-decode-save-view
2242   "O" gnus-uu-decode-save-view
2243   "b" gnus-uu-decode-binhex-view
2244   "B" gnus-uu-decode-binhex-view
2245   "p" gnus-uu-decode-postscript-view
2246   "P" gnus-uu-decode-postscript-and-save-view)
2247
2248 (defvar gnus-article-post-menu nil)
2249
2250 (defconst gnus-summary-menu-maxlen 20)
2251
2252 (defun gnus-summary-menu-split (menu)
2253   ;; If we have lots of elements, divide them into groups of 20
2254   ;; and make a pane (or submenu) for each one.
2255   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2256       (let ((menu menu) sublists next
2257             (i 1))
2258         (while menu
2259           ;; Pull off the next gnus-summary-menu-maxlen elements
2260           ;; and make them the next element of sublist.
2261           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2262           (if next
2263               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2264                       nil))
2265           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2266                                              (aref (car (last menu)) 0)) menu)
2267                                sublists))
2268           (setq i (1+ i))
2269           (setq menu next))
2270         (nreverse sublists))
2271     ;; Few elements--put them all in one pane.
2272     menu))
2273
2274 (defun gnus-summary-make-menu-bar ()
2275   (gnus-turn-off-edit-menu 'summary)
2276
2277   (unless (boundp 'gnus-summary-misc-menu)
2278
2279     (easy-menu-define
2280       gnus-summary-kill-menu gnus-summary-mode-map ""
2281       (cons
2282        "Score"
2283        (nconc
2284         (list
2285          ["Customize" gnus-score-customize t])
2286         (gnus-make-score-map 'increase)
2287         (gnus-make-score-map 'lower)
2288         '(("Mark"
2289            ["Kill below" gnus-summary-kill-below t]
2290            ["Mark above" gnus-summary-mark-above t]
2291            ["Tick above" gnus-summary-tick-above t]
2292            ["Clear above" gnus-summary-clear-above t])
2293           ["Current score" gnus-summary-current-score t]
2294           ["Set score" gnus-summary-set-score t]
2295           ["Switch current score file..." gnus-score-change-score-file t]
2296           ["Set mark below..." gnus-score-set-mark-below t]
2297           ["Set expunge below..." gnus-score-set-expunge-below t]
2298           ["Edit current score file" gnus-score-edit-current-scores t]
2299           ["Edit score file..." gnus-score-edit-file t]
2300           ["Trace score" gnus-score-find-trace t]
2301           ["Find words" gnus-score-find-favourite-words t]
2302           ["Rescore buffer" gnus-summary-rescore t]
2303           ["Increase score..." gnus-summary-increase-score t]
2304           ["Lower score..." gnus-summary-lower-score t]))))
2305
2306     ;; Define both the Article menu in the summary buffer and the
2307     ;; equivalent Commands menu in the article buffer here for
2308     ;; consistency.
2309     (let ((innards
2310            `(("Hide"
2311               ["All" gnus-article-hide t]
2312               ["Headers" gnus-article-hide-headers t]
2313               ["Signature" gnus-article-hide-signature t]
2314               ["Citation" gnus-article-hide-citation t]
2315               ["List identifiers" gnus-article-hide-list-identifiers t]
2316               ["Banner" gnus-article-strip-banner t]
2317               ["Boring headers" gnus-article-hide-boring-headers t])
2318              ("Highlight"
2319               ["All" gnus-article-highlight t]
2320               ["Headers" gnus-article-highlight-headers t]
2321               ["Signature" gnus-article-highlight-signature t]
2322               ["Citation" gnus-article-highlight-citation t])
2323              ("MIME"
2324               ["Words" gnus-article-decode-mime-words t]
2325               ["Charset" gnus-article-decode-charset t]
2326               ["QP" gnus-article-de-quoted-unreadable t]
2327               ["Base64" gnus-article-de-base64-unreadable t]
2328               ["View MIME buttons" gnus-summary-display-buttonized t]
2329               ["View all" gnus-mime-view-all-parts t]
2330               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2331               ["Encrypt body" gnus-article-encrypt-body
2332                :active (not (gnus-group-read-only-p))
2333                ,@(if (featurep 'xemacs) nil
2334                    '(:help "Encrypt the message body on disk"))]
2335               ["Extract all parts..." gnus-summary-save-parts t]
2336               ("Multipart"
2337                ["Repair multipart" gnus-summary-repair-multipart t]
2338                ["Pipe part..." gnus-article-pipe-part t]
2339                ["Inline part" gnus-article-inline-part t]
2340                ["View part as type..." gnus-article-view-part-as-type t]
2341                ["Encrypt body" gnus-article-encrypt-body
2342                 :active (not (gnus-group-read-only-p))
2343                ,@(if (featurep 'xemacs) nil
2344                    '(:help "Encrypt the message body on disk"))]
2345                ["View part externally" gnus-article-view-part-externally t]
2346                ["View HTML parts in browser" gnus-article-browse-html-article t]
2347                ["View part with charset..." gnus-article-view-part-as-charset t]
2348                ["Copy part" gnus-article-copy-part t]
2349                ["Save part..." gnus-article-save-part t]
2350                ["View part" gnus-article-view-part t]))
2351              ("Date"
2352               ["Local" gnus-article-date-local t]
2353               ["ISO8601" gnus-article-date-iso8601 t]
2354               ["UT" gnus-article-date-ut t]
2355               ["Original" gnus-article-date-original t]
2356               ["Lapsed" gnus-article-date-lapsed t]
2357               ["User-defined" gnus-article-date-user t])
2358              ("Display"
2359               ["Remove images" gnus-article-remove-images t]
2360               ["Toggle smiley" gnus-treat-smiley t]
2361               ["Show X-Face" gnus-article-display-x-face t]
2362               ["Show picons in From" gnus-treat-from-picon t]
2363               ["Show picons in mail headers" gnus-treat-mail-picon t]
2364               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2365               ("View as different encoding"
2366                ,@(gnus-summary-menu-split
2367                   (mapcar
2368                    (lambda (cs)
2369                      ;; Since easymenu under Emacs doesn't allow
2370                      ;; lambda forms for menu commands, we should
2371                      ;; provide intern'ed function symbols.
2372                      (let ((command (intern (format "\
2373 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2374                        (fset command
2375                              `(lambda ()
2376                                 (interactive)
2377                                 (let ((gnus-summary-show-article-charset-alist
2378                                        '((1 . ,cs))))
2379                                   (gnus-summary-show-article 1))))
2380                        `[,(symbol-name cs) ,command t]))
2381                    (sort (if (fboundp 'coding-system-list)
2382                              (coding-system-list)
2383                            (mapcar 'car mm-mime-mule-charset-alist))
2384                          'string<)))))
2385              ("Washing"
2386               ("Remove Blanks"
2387                ["Leading" gnus-article-strip-leading-blank-lines t]
2388                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2389                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2390                ["All of the above" gnus-article-strip-blank-lines t]
2391                ["All" gnus-article-strip-all-blank-lines t]
2392                ["Leading space" gnus-article-strip-leading-space t]
2393                ["Trailing space" gnus-article-strip-trailing-space t]
2394                ["Leading space in headers"
2395                 gnus-article-remove-leading-whitespace t])
2396               ["Overstrike" gnus-article-treat-overstrike t]
2397               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2398               ["Emphasis" gnus-article-emphasize t]
2399               ["Word wrap" gnus-article-fill-cited-article t]
2400               ["Fill long lines" gnus-article-fill-long-lines t]
2401               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2402               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2403               ["Remove CR" gnus-article-remove-cr t]
2404               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2405               ["Base64" gnus-article-de-base64-unreadable t]
2406               ["Rot 13" gnus-summary-caesar-message
2407                ,@(if (featurep 'xemacs) '(t)
2408                    '(:help "\"Caesar rotate\" article by 13"))]
2409               ["De-IDNA" gnus-summary-idna-message t]
2410               ["Morse decode" gnus-summary-morse-message t]
2411               ["Unix pipe..." gnus-summary-pipe-message t]
2412               ["Add buttons" gnus-article-add-buttons t]
2413               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2414               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2415               ["Verbose header" gnus-summary-verbose-headers t]
2416               ["Toggle header" gnus-summary-toggle-header t]
2417               ["Unfold headers" gnus-article-treat-unfold-headers t]
2418               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2419               ["Html" gnus-article-wash-html t]
2420               ["Unsplit URLs" gnus-article-unsplit-urls t]
2421               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2422               ["Decode HZ" gnus-article-decode-HZ t]
2423               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2424               ("(Outlook) Deuglify"
2425                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2426                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2427                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2428                ["Full (Outlook) deuglify"
2429                 gnus-article-outlook-deuglify-article t])
2430               )
2431              ("Output"
2432               ["Save in default format..." gnus-summary-save-article
2433                ,@(if (featurep 'xemacs) '(t)
2434                    '(:help "Save article using default method"))]
2435               ["Save in file..." gnus-summary-save-article-file
2436                ,@(if (featurep 'xemacs) '(t)
2437                    '(:help "Save article in file"))]
2438               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2439               ["Save in MH folder..." gnus-summary-save-article-folder t]
2440               ["Save in VM folder..." gnus-summary-save-article-vm t]
2441               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2442               ["Save body in file..." gnus-summary-save-article-body-file t]
2443               ["Pipe through a filter..." gnus-summary-pipe-output t]
2444               ["Print with Muttprint..." gnus-summary-muttprint t]
2445               ["Print" gnus-summary-print-article
2446                ,@(if (featurep 'xemacs) '(t)
2447                    '(:help "Generate and print a PostScript image"))])
2448              ("Copy, move,... (Backend)"
2449               ,@(if (featurep 'xemacs) nil
2450                   '(:help "Copying, moving, expiring articles..."))
2451               ["Respool article..." gnus-summary-respool-article t]
2452               ["Move article..." gnus-summary-move-article
2453                (gnus-check-backend-function
2454                 'request-move-article gnus-newsgroup-name)]
2455               ["Copy article..." gnus-summary-copy-article t]
2456               ["Crosspost article..." gnus-summary-crosspost-article
2457                (gnus-check-backend-function
2458                 'request-replace-article gnus-newsgroup-name)]
2459               ["Import file..." gnus-summary-import-article
2460                (gnus-check-backend-function
2461                 'request-accept-article gnus-newsgroup-name)]
2462               ["Create article..." gnus-summary-create-article
2463                (gnus-check-backend-function
2464                 'request-accept-article gnus-newsgroup-name)]
2465               ["Check if posted" gnus-summary-article-posted-p t]
2466               ["Edit article" gnus-summary-edit-article
2467                (not (gnus-group-read-only-p))]
2468               ["Delete article" gnus-summary-delete-article
2469                (gnus-check-backend-function
2470                 'request-expire-articles gnus-newsgroup-name)]
2471               ["Query respool" gnus-summary-respool-query t]
2472               ["Trace respool" gnus-summary-respool-trace t]
2473               ["Delete expirable articles" gnus-summary-expire-articles-now
2474                (gnus-check-backend-function
2475                 'request-expire-articles gnus-newsgroup-name)])
2476              ("Extract"
2477               ["Uudecode" gnus-uu-decode-uu
2478                ,@(if (featurep 'xemacs) '(t)
2479                    '(:help "Decode uuencoded article(s)"))]
2480               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2481               ["Unshar" gnus-uu-decode-unshar t]
2482               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2483               ["Save" gnus-uu-decode-save t]
2484               ["Binhex" gnus-uu-decode-binhex t]
2485               ["Postscript" gnus-uu-decode-postscript t]
2486               ["All MIME parts" gnus-summary-save-parts t])
2487              ("Cache"
2488               ["Enter article" gnus-cache-enter-article t]
2489               ["Remove article" gnus-cache-remove-article t])
2490              ["Translate" gnus-article-babel t]
2491              ["Select article buffer" gnus-summary-select-article-buffer t]
2492              ["Make article buffer sticky" gnus-sticky-article t]
2493              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2494              ["Isearch article..." gnus-summary-isearch-article t]
2495              ["Beginning of the article" gnus-summary-beginning-of-article t]
2496              ["End of the article" gnus-summary-end-of-article t]
2497              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2498              ["Fetch referenced articles" gnus-summary-refer-references t]
2499              ["Fetch current thread" gnus-summary-refer-thread t]
2500              ["Fetch article with id..." gnus-summary-refer-article t]
2501              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2502              ["Redisplay" gnus-summary-show-article t]
2503              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2504       (easy-menu-define
2505         gnus-summary-article-menu gnus-summary-mode-map ""
2506         (cons "Article" innards))
2507
2508       (if (not (keymapp gnus-summary-article-menu))
2509           (easy-menu-define
2510             gnus-article-commands-menu gnus-article-mode-map ""
2511             (cons "Commands" innards))
2512         ;; in Emacs, don't share menu.
2513         (setq gnus-article-commands-menu
2514               (copy-keymap gnus-summary-article-menu))
2515         (define-key gnus-article-mode-map [menu-bar commands]
2516           (cons "Commands" gnus-article-commands-menu))))
2517
2518     (easy-menu-define
2519       gnus-summary-thread-menu gnus-summary-mode-map ""
2520       '("Threads"
2521         ["Find all messages in thread" gnus-summary-refer-thread t]
2522         ["Toggle threading" gnus-summary-toggle-threads t]
2523         ["Hide threads" gnus-summary-hide-all-threads t]
2524         ["Show threads" gnus-summary-show-all-threads t]
2525         ["Hide thread" gnus-summary-hide-thread t]
2526         ["Show thread" gnus-summary-show-thread t]
2527         ["Go to next thread" gnus-summary-next-thread t]
2528         ["Go to previous thread" gnus-summary-prev-thread t]
2529         ["Go down thread" gnus-summary-down-thread t]
2530         ["Go up thread" gnus-summary-up-thread t]
2531         ["Top of thread" gnus-summary-top-thread t]
2532         ["Mark thread as read" gnus-summary-kill-thread t]
2533         ["Mark thread as expired" gnus-summary-expire-thread t]
2534         ["Lower thread score" gnus-summary-lower-thread t]
2535         ["Raise thread score" gnus-summary-raise-thread t]
2536         ["Rethread current" gnus-summary-rethread-current t]))
2537
2538     (easy-menu-define
2539       gnus-summary-post-menu gnus-summary-mode-map ""
2540       `("Post"
2541         ["Send a message (mail or news)" gnus-summary-post-news
2542          ,@(if (featurep 'xemacs) '(t)
2543              '(:help "Compose a new message (mail or news)"))]
2544         ["Followup" gnus-summary-followup
2545          ,@(if (featurep 'xemacs) '(t)
2546              '(:help "Post followup to this article"))]
2547         ["Followup and yank" gnus-summary-followup-with-original
2548          ,@(if (featurep 'xemacs) '(t)
2549              '(:help "Post followup to this article, quoting its contents"))]
2550         ["Supersede article" gnus-summary-supersede-article t]
2551         ["Cancel article" gnus-summary-cancel-article
2552          ,@(if (featurep 'xemacs) '(t)
2553              '(:help "Cancel an article you posted"))]
2554         ["Reply" gnus-summary-reply t]
2555         ["Reply and yank" gnus-summary-reply-with-original t]
2556         ["Wide reply" gnus-summary-wide-reply t]
2557         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2558          ,@(if (featurep 'xemacs) '(t)
2559              '(:help "Mail a reply, quoting this article"))]
2560         ["Very wide reply" gnus-summary-very-wide-reply t]
2561         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2562          ,@(if (featurep 'xemacs) '(t)
2563              '(:help "Mail a very wide reply, quoting this article"))]
2564         ["Mail forward" gnus-summary-mail-forward t]
2565         ["Post forward" gnus-summary-post-forward t]
2566         ["Digest and mail" gnus-uu-digest-mail-forward t]
2567         ["Digest and post" gnus-uu-digest-post-forward t]
2568         ["Resend message" gnus-summary-resend-message t]
2569         ["Resend message edit" gnus-summary-resend-message-edit t]
2570         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2571         ["Send a mail" gnus-summary-mail-other-window t]
2572         ["Create a local message" gnus-summary-news-other-window t]
2573         ["Uuencode and post" gnus-uu-post-news
2574          ,@(if (featurep 'xemacs) '(t)
2575              '(:help "Post a uuencoded article"))]
2576         ["Followup via news" gnus-summary-followup-to-mail t]
2577         ["Followup via news and yank"
2578          gnus-summary-followup-to-mail-with-original t]
2579         ["Strip signature on reply"
2580          (lambda ()
2581            (interactive)
2582            (if (not (memq message-cite-function
2583                           '(message-cite-original-without-signature
2584                             message-cite-original)))
2585                ;; Stupid workaround for XEmacs not honoring :visible.
2586                (message "Can't toggle this value of `message-cite-function'")
2587              (setq message-cite-function
2588                    (if (eq message-cite-function
2589                            'message-cite-original-without-signature)
2590                        'message-cite-original
2591                      'message-cite-original-without-signature))))
2592          ;; XEmacs barfs on :visible.
2593          ,@(if (featurep 'xemacs) nil
2594              '(:visible (memq message-cite-function
2595                               '(message-cite-original-without-signature
2596                                 message-cite-original))))
2597          :style toggle
2598          :selected (eq message-cite-function
2599                        'message-cite-original-without-signature)
2600          ,@(if (featurep 'xemacs) nil
2601              '(:help "Strip signature from cited article when replying."))]
2602         ;;("Draft"
2603         ;;["Send" gnus-summary-send-draft t]
2604         ;;["Send bounced" gnus-resend-bounced-mail t])
2605         ))
2606
2607     (cond
2608      ((not (keymapp gnus-summary-post-menu))
2609       (setq gnus-article-post-menu gnus-summary-post-menu))
2610      ((not gnus-article-post-menu)
2611       ;; Don't share post menu.
2612       (setq gnus-article-post-menu
2613             (copy-keymap gnus-summary-post-menu))))
2614     (define-key gnus-article-mode-map [menu-bar post]
2615       (cons "Post" gnus-article-post-menu))
2616
2617     (easy-menu-define
2618       gnus-summary-misc-menu gnus-summary-mode-map ""
2619       `("Gnus"
2620         ("Mark Read"
2621          ["Mark as read" gnus-summary-mark-as-read-forward t]
2622          ["Mark same subject and select"
2623           gnus-summary-kill-same-subject-and-select t]
2624          ["Mark same subject" gnus-summary-kill-same-subject t]
2625          ["Catchup" gnus-summary-catchup
2626           ,@(if (featurep 'xemacs) '(t)
2627               '(:help "Mark unread articles in this group as read"))]
2628          ["Catchup all" gnus-summary-catchup-all t]
2629          ["Catchup to here" gnus-summary-catchup-to-here t]
2630          ["Catchup from here" gnus-summary-catchup-from-here t]
2631          ["Catchup region" gnus-summary-mark-region-as-read
2632           (gnus-mark-active-p)]
2633          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2634         ("Mark Various"
2635          ["Tick" gnus-summary-tick-article-forward t]
2636          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2637          ["Remove marks" gnus-summary-clear-mark-forward t]
2638          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2639          ["Set bookmark" gnus-summary-set-bookmark t]
2640          ["Remove bookmark" gnus-summary-remove-bookmark t])
2641         ("Limit to"
2642          ["Marks..." gnus-summary-limit-to-marks t]
2643          ["Subject..." gnus-summary-limit-to-subject t]
2644          ["Author..." gnus-summary-limit-to-author t]
2645          ["Recipient..." gnus-summary-limit-to-recipient t]
2646          ["Address..." gnus-summary-limit-to-address t]
2647          ["Age..." gnus-summary-limit-to-age t]
2648          ["Extra..." gnus-summary-limit-to-extra t]
2649          ["Score..." gnus-summary-limit-to-score t]
2650          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2651          ["Unread" gnus-summary-limit-to-unread t]
2652          ["Unseen" gnus-summary-limit-to-unseen t]
2653          ["Singletons" gnus-summary-limit-to-singletons t]
2654          ["Replied" gnus-summary-limit-to-replied t]
2655          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2656          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2657          ["Pop limit" gnus-summary-pop-limit t]
2658          ["Show dormant" gnus-summary-limit-include-dormant t]
2659          ["Hide childless dormant"
2660           gnus-summary-limit-exclude-childless-dormant t]
2661          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2662          ["Hide marked" gnus-summary-limit-exclude-marks t]
2663          ["Show expunged" gnus-summary-limit-include-expunged t])
2664         ("Process Mark"
2665          ["Set mark" gnus-summary-mark-as-processable t]
2666          ["Remove mark" gnus-summary-unmark-as-processable t]
2667          ["Remove all marks" gnus-summary-unmark-all-processable t]
2668          ["Invert marks" gnus-uu-invert-processable t]
2669          ["Mark above" gnus-uu-mark-over t]
2670          ["Mark series" gnus-uu-mark-series t]
2671          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2672          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2673          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2674          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2675          ["Mark all" gnus-uu-mark-all t]
2676          ["Mark buffer" gnus-uu-mark-buffer t]
2677          ["Mark sparse" gnus-uu-mark-sparse t]
2678          ["Mark thread" gnus-uu-mark-thread t]
2679          ["Unmark thread" gnus-uu-unmark-thread t]
2680          ("Process Mark Sets"
2681           ["Kill" gnus-summary-kill-process-mark t]
2682           ["Yank" gnus-summary-yank-process-mark
2683            gnus-newsgroup-process-stack]
2684           ["Save" gnus-summary-save-process-mark t]
2685           ["Run command on marked..." gnus-summary-universal-argument t]))
2686         ("Registry Marks")
2687         ("Scroll article"
2688          ["Page forward" gnus-summary-next-page
2689           ,@(if (featurep 'xemacs) '(t)
2690               '(:help "Show next page of article"))]
2691          ["Page backward" gnus-summary-prev-page
2692           ,@(if (featurep 'xemacs) '(t)
2693               '(:help "Show previous page of article"))]
2694          ["Line forward" gnus-summary-scroll-up t])
2695         ("Move"
2696          ["Next unread article" gnus-summary-next-unread-article t]
2697          ["Previous unread article" gnus-summary-prev-unread-article t]
2698          ["Next article" gnus-summary-next-article t]
2699          ["Previous article" gnus-summary-prev-article t]
2700          ["Next unread subject" gnus-summary-next-unread-subject t]
2701          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2702          ["Next article same subject" gnus-summary-next-same-subject t]
2703          ["Previous article same subject" gnus-summary-prev-same-subject t]
2704          ["First unread article" gnus-summary-first-unread-article t]
2705          ["Best unread article" gnus-summary-best-unread-article t]
2706          ["Go to subject number..." gnus-summary-goto-subject t]
2707          ["Go to article number..." gnus-summary-goto-article t]
2708          ["Go to the last article" gnus-summary-goto-last-article t]
2709          ["Pop article off history" gnus-summary-pop-article t])
2710         ("Sort"
2711          ["Sort by number" gnus-summary-sort-by-number t]
2712          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2713          ["Sort by author" gnus-summary-sort-by-author t]
2714          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2715          ["Sort by subject" gnus-summary-sort-by-subject t]
2716          ["Sort by date" gnus-summary-sort-by-date t]
2717          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2718          ["Sort by score" gnus-summary-sort-by-score t]
2719          ["Sort by lines" gnus-summary-sort-by-lines t]
2720          ["Sort by characters" gnus-summary-sort-by-chars t]
2721          ["Randomize" gnus-summary-sort-by-random t]
2722          ["Original sort" gnus-summary-sort-by-original t])
2723         ("Help"
2724          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2725          ["Describe group" gnus-summary-describe-group t]
2726          ["Fetch charter" gnus-group-fetch-charter
2727           ,@(if (featurep 'xemacs) nil
2728               '(:help "Display the charter of the current group"))]
2729          ["Fetch control message" gnus-group-fetch-control
2730           ,@(if (featurep 'xemacs) nil
2731               '(:help "Display the archived control message for the current group"))]
2732          ["Read manual" gnus-info-find-node t])
2733         ("Modes"
2734          ["Pick and read" gnus-pick-mode t]
2735          ["Binary" gnus-binary-mode t])
2736         ("Regeneration"
2737          ["Regenerate" gnus-summary-prepare t]
2738          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2739          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2740          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2741          ["Toggle threading" gnus-summary-toggle-threads t])
2742         ["See old articles" gnus-summary-insert-old-articles t]
2743         ["See new articles" gnus-summary-insert-new-articles t]
2744         ["Filter articles..." gnus-summary-execute-command t]
2745         ["Run command on articles..." gnus-summary-universal-argument t]
2746         ["Search articles forward..." gnus-summary-search-article-forward t]
2747         ["Search articles backward..." gnus-summary-search-article-backward t]
2748         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2749         ["Expand window" gnus-summary-expand-window t]
2750         ["Expire expirable articles" gnus-summary-expire-articles
2751          (gnus-check-backend-function
2752           'request-expire-articles gnus-newsgroup-name)]
2753         ["Edit local kill file" gnus-summary-edit-local-kill t]
2754         ["Edit main kill file" gnus-summary-edit-global-kill t]
2755         ["Edit group parameters" gnus-summary-edit-parameters t]
2756         ["Customize group parameters" gnus-summary-customize-parameters t]
2757         ["Send a bug report" gnus-bug t]
2758         ("Exit"
2759          ["Catchup and exit" gnus-summary-catchup-and-exit
2760           ,@(if (featurep 'xemacs) '(t)
2761               '(:help "Mark unread articles in this group as read, then exit"))]
2762          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2763          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2764          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2765          ["Exit group" gnus-summary-exit
2766           ,@(if (featurep 'xemacs) '(t)
2767               '(:help "Exit current group, return to group selection mode"))]
2768          ["Exit group without updating" gnus-summary-exit-no-update t]
2769          ["Exit and goto next group" gnus-summary-next-group t]
2770          ["Exit and goto prev group" gnus-summary-prev-group t]
2771          ["Reselect group" gnus-summary-reselect-current-group t]
2772          ["Rescan group" gnus-summary-rescan-group t]
2773          ["Update dribble" gnus-summary-save-newsrc t])))
2774
2775     (gnus-run-hooks 'gnus-summary-menu-hook)))
2776
2777 (defvar gnus-summary-tool-bar-map nil)
2778
2779 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2780 ;; affect _new_ message buffers.  We might add a function that walks thru all
2781 ;; summary-mode buffers and force the update.
2782 (defun gnus-summary-tool-bar-update (&optional symbol value)
2783   "Update summary mode toolbar.
2784 Setter function for custom variables."
2785   (setq-default gnus-summary-tool-bar-map nil)
2786   (when symbol
2787     ;; When used as ":set" function:
2788     (set-default symbol value))
2789   (when (gnus-buffer-live-p gnus-summary-buffer)
2790     (with-current-buffer gnus-summary-buffer
2791       (gnus-summary-make-tool-bar))))
2792
2793 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2794                                      'gnus-summary-tool-bar-gnome
2795                                    'gnus-summary-tool-bar-retro)
2796   "Specifies the Gnus summary tool bar.
2797
2798 It can be either a list or a symbol refering to a list.  See
2799 `gmm-tool-bar-from-list' for the format of the list.  The
2800 default key map is `gnus-summary-mode-map'.
2801
2802 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2803 `gnus-summary-tool-bar-retro'."
2804   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2805                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2806                  (repeat :tag "User defined list" gmm-tool-bar-item)
2807                  (symbol))
2808   :version "23.1" ;; No Gnus
2809   :initialize 'custom-initialize-default
2810   :set 'gnus-summary-tool-bar-update
2811   :group 'gnus-summary)
2812
2813 (defcustom gnus-summary-tool-bar-gnome
2814   '((gnus-summary-post-news "mail/compose" nil)
2815     (gnus-summary-insert-new-articles "mail/inbox" nil
2816                                       :visible (or (not gnus-agent)
2817                                                    gnus-plugged))
2818     (gnus-summary-reply-with-original "mail/reply")
2819     (gnus-summary-reply "mail/reply" nil :visible nil)
2820     (gnus-summary-followup-with-original "mail/reply-all")
2821     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2822     (gnus-summary-mail-forward "mail/forward")
2823     (gnus-summary-save-article "mail/save")
2824     (gnus-summary-search-article-forward "search" nil :visible nil)
2825     (gnus-summary-print-article "print")
2826     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2827     ;; Some new commands that may need more suitable icons:
2828     (gnus-summary-save-newsrc "save" nil :visible nil)
2829     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2830     (gnus-summary-prev-article "left-arrow")
2831     (gnus-summary-next-article "right-arrow")
2832     (gnus-summary-next-page "next-page")
2833     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2834     ;;
2835     ;; Maybe some sort-by-... could be added:
2836     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2837     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2838     (gnus-summary-mark-as-expirable
2839      "delete" nil
2840      :visible (gnus-check-backend-function 'request-expire-articles
2841                                            gnus-newsgroup-name))
2842     (gnus-summary-mark-as-spam
2843      "mail/spam" t
2844      :visible (and (fboundp 'spam-group-ham-contents-p)
2845                    (spam-group-ham-contents-p gnus-newsgroup-name))
2846      :help "Mark as spam")
2847     (gnus-summary-mark-as-read-forward
2848      "mail/not-spam" nil
2849      :visible (and (fboundp 'spam-group-spam-contents-p)
2850                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2851     ;;
2852     (gnus-summary-exit "exit")
2853     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2854     (gnus-info-find-node "help"))
2855   "List of functions for the summary tool bar (GNOME style).
2856
2857 See `gmm-tool-bar-from-list' for the format of the list."
2858   :type '(repeat gmm-tool-bar-item)
2859   :version "23.1" ;; No Gnus
2860   :initialize 'custom-initialize-default
2861   :set 'gnus-summary-tool-bar-update
2862   :group 'gnus-summary)
2863
2864 (defcustom gnus-summary-tool-bar-retro
2865   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2866     (gnus-summary-next-unread-article "gnus/next-ur")
2867     (gnus-summary-post-news "gnus/post")
2868     (gnus-summary-followup-with-original "gnus/fuwo")
2869     (gnus-summary-followup "gnus/followup")
2870     (gnus-summary-reply-with-original "gnus/reply-wo")
2871     (gnus-summary-reply "gnus/reply")
2872     (gnus-summary-caesar-message "gnus/rot13")
2873     (gnus-uu-decode-uu "gnus/uu-decode")
2874     (gnus-summary-save-article-file "gnus/save-aif")
2875     (gnus-summary-save-article "gnus/save-art")
2876     (gnus-uu-post-news "gnus/uu-post")
2877     (gnus-summary-catchup "gnus/catchup")
2878     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2879     (gnus-summary-exit "gnus/exit-summ")
2880     ;; Some new command that may need more suitable icons:
2881     (gnus-summary-print-article "gnus/print" nil :visible nil)
2882     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2883     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2884     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2885     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2886     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2887     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2888     ;;
2889     (gnus-info-find-node "gnus/help" nil :visible nil))
2890   "List of functions for the summary tool bar (retro look).
2891
2892 See `gmm-tool-bar-from-list' for the format of the list."
2893   :type '(repeat gmm-tool-bar-item)
2894   :version "23.1" ;; No Gnus
2895   :initialize 'custom-initialize-default
2896   :set 'gnus-summary-tool-bar-update
2897   :group 'gnus-summary)
2898
2899 (defcustom gnus-summary-tool-bar-zap-list t
2900   "List of icon items from the global tool bar.
2901 These items are not displayed in the Gnus summary mode tool bar.
2902
2903 See `gmm-tool-bar-from-list' for the format of the list."
2904   :type 'gmm-tool-bar-zap-list
2905   :version "23.1" ;; No Gnus
2906   :initialize 'custom-initialize-default
2907   :set 'gnus-summary-tool-bar-update
2908   :group 'gnus-summary)
2909
2910 (defvar image-load-path)
2911 (defvar tool-bar-map)
2912
2913 (defun gnus-summary-make-tool-bar (&optional force)
2914   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2915 When FORCE, rebuild the tool bar."
2916   (when (and (not (featurep 'xemacs))
2917              (boundp 'tool-bar-mode)
2918              tool-bar-mode
2919              (or (not gnus-summary-tool-bar-map) force))
2920     (let* ((load-path
2921             (gmm-image-load-path-for-library "gnus"
2922                                              "mail/save.xpm"
2923                                              nil t))
2924            (image-load-path (cons (car load-path)
2925                                   (when (boundp 'image-load-path)
2926                                     image-load-path)))
2927            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2928                                         gnus-summary-tool-bar-zap-list
2929                                         'gnus-summary-mode-map)))
2930       (when map
2931         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2932         ;; uses it's value.
2933         (setq gnus-summary-tool-bar-map map))))
2934   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2935
2936 (defun gnus-score-set-default (var value)
2937   "A version of set that updates the GNU Emacs menu-bar."
2938   (set var value)
2939   ;; It is the message that forces the active status to be updated.
2940   (message ""))
2941
2942 (defun gnus-make-score-map (type)
2943   "Make a summary score map of type TYPE."
2944   (if t
2945       nil
2946     (let ((headers '(("author" "from" string)
2947                      ("subject" "subject" string)
2948                      ("article body" "body" string)
2949                      ("article head" "head" string)
2950                      ("xref" "xref" string)
2951                      ("extra header" "extra" string)
2952                      ("lines" "lines" number)
2953                      ("followups to author" "followup" string)))
2954           (types '((number ("less than" <)
2955                            ("greater than" >)
2956                            ("equal" =))
2957                    (string ("substring" s)
2958                            ("exact string" e)
2959                            ("fuzzy string" f)
2960                            ("regexp" r))))
2961           (perms '(("temporary" (current-time-string))
2962                    ("permanent" nil)
2963                    ("immediate" now)))
2964           header)
2965       (list
2966        (apply
2967         'nconc
2968         (list
2969          (if (eq type 'lower)
2970              "Lower score"
2971            "Increase score"))
2972         (let (outh)
2973           (while headers
2974             (setq header (car headers))
2975             (setq outh
2976                   (cons
2977                    (apply
2978                     'nconc
2979                     (list (car header))
2980                     (let ((ts (cdr (assoc (nth 2 header) types)))
2981                           outt)
2982                       (while ts
2983                         (setq outt
2984                               (cons
2985                                (apply
2986                                 'nconc
2987                                 (list (caar ts))
2988                                 (let ((ps perms)
2989                                       outp)
2990                                   (while ps
2991                                     (setq outp
2992                                           (cons
2993                                            (vector
2994                                             (caar ps)
2995                                             (list
2996                                              'gnus-summary-score-entry
2997                                              (nth 1 header)
2998                                              (if (or (string= (nth 1 header)
2999                                                               "head")
3000                                                      (string= (nth 1 header)
3001                                                               "body"))
3002                                                  ""
3003                                                (list 'gnus-summary-header
3004                                                      (nth 1 header)))
3005                                              (list 'quote (nth 1 (car ts)))
3006                                              (list 'gnus-score-delta-default
3007                                                    nil)
3008                                              (nth 1 (car ps))
3009                                              t)
3010                                             t)
3011                                            outp))
3012                                     (setq ps (cdr ps)))
3013                                   (list (nreverse outp))))
3014                                outt))
3015                         (setq ts (cdr ts)))
3016                       (list (nreverse outt))))
3017                    outh))
3018             (setq headers (cdr headers)))
3019           (list (nreverse outh))))))))
3020
3021
3022 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3023 (defvar bookmark-make-record-function)
3024 \f
3025
3026 (defun gnus-summary-mode (&optional group)
3027   "Major mode for reading articles.
3028
3029 All normal editing commands are switched off.
3030 \\<gnus-summary-mode-map>
3031 Each line in this buffer represents one article.  To read an
3032 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3033 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3034 respectively.
3035
3036 You can also post articles and send mail from this buffer.  To
3037 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3038 of an article, type `\\[gnus-summary-reply]'.
3039
3040 There are approx. one gazillion commands you can execute in this
3041 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3042
3043 The following commands are available:
3044
3045 \\{gnus-summary-mode-map}"
3046   (interactive)
3047   (kill-all-local-variables)
3048   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3049     (gnus-summary-make-local-variables))
3050   (gnus-summary-make-local-variables)
3051   (setq gnus-newsgroup-name group)
3052   (when (gnus-visual-p 'summary-menu 'menu)
3053     (gnus-summary-make-menu-bar)
3054     (gnus-summary-make-tool-bar))
3055   (gnus-make-thread-indent-array)
3056   (gnus-simplify-mode-line)
3057   (setq major-mode 'gnus-summary-mode)
3058   (setq mode-name "Summary")
3059   (use-local-map gnus-summary-mode-map)
3060   (buffer-disable-undo)
3061   (setq buffer-read-only t              ;Disable modification
3062         show-trailing-whitespace nil)
3063   (setq truncate-lines t)
3064   (add-to-invisibility-spec '(gnus-sum . t))
3065   (gnus-summary-set-display-table)
3066   (gnus-set-default-directory)
3067   (make-local-variable 'gnus-summary-line-format)
3068   (make-local-variable 'gnus-summary-line-format-spec)
3069   (make-local-variable 'gnus-summary-dummy-line-format)
3070   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3071   (make-local-variable 'gnus-summary-mark-positions)
3072   (gnus-make-local-hook 'pre-command-hook)
3073   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3074   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3075   (turn-on-gnus-mailing-list-mode)
3076   (mm-enable-multibyte)
3077   (set (make-local-variable 'bookmark-make-record-function)
3078        'gnus-summary-bookmark-make-record)
3079   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3080   (gnus-update-summary-mark-positions))
3081
3082 (defun gnus-summary-make-local-variables ()
3083   "Make all the local summary buffer variables."
3084   (let (global)
3085     (dolist (local gnus-summary-local-variables)
3086       (if (consp local)
3087           (progn
3088             (if (eq (cdr local) 'global)
3089                 ;; Copy the global value of the variable.
3090                 (setq global (symbol-value (car local)))
3091               ;; Use the value from the list.
3092               (setq global (eval (cdr local))))
3093             (set (make-local-variable (car local)) global))
3094         ;; Simple nil-valued local variable.
3095         (set (make-local-variable local) nil)))))
3096
3097 (defun gnus-summary-clear-local-variables ()
3098   (let ((locals gnus-summary-local-variables))
3099     (while locals
3100       (if (consp (car locals))
3101           (and (symbolp (caar locals))
3102                (set (caar locals) nil))
3103         (and (symbolp (car locals))
3104              (set (car locals) nil)))
3105       (setq locals (cdr locals)))))
3106
3107 ;; Summary data functions.
3108
3109 (defmacro gnus-data-number (data)
3110   `(car ,data))
3111
3112 (defmacro gnus-data-set-number (data number)
3113   `(setcar ,data ,number))
3114
3115 (defmacro gnus-data-mark (data)
3116   `(nth 1 ,data))
3117
3118 (defmacro gnus-data-set-mark (data mark)
3119   `(setcar (nthcdr 1 ,data) ,mark))
3120
3121 (defmacro gnus-data-pos (data)
3122   `(nth 2 ,data))
3123
3124 (defmacro gnus-data-set-pos (data pos)
3125   `(setcar (nthcdr 2 ,data) ,pos))
3126
3127 (defmacro gnus-data-header (data)
3128   `(nth 3 ,data))
3129
3130 (defmacro gnus-data-set-header (data header)
3131   `(setf (nth 3 ,data) ,header))
3132
3133 (defmacro gnus-data-level (data)
3134   `(nth 4 ,data))
3135
3136 (defmacro gnus-data-unread-p (data)
3137   `(= (nth 1 ,data) gnus-unread-mark))
3138
3139 (defmacro gnus-data-read-p (data)
3140   `(/= (nth 1 ,data) gnus-unread-mark))
3141
3142 (defmacro gnus-data-pseudo-p (data)
3143   `(consp (nth 3 ,data)))
3144
3145 (defmacro gnus-data-find (number)
3146   `(assq ,number gnus-newsgroup-data))
3147
3148 (defmacro gnus-data-find-list (number &optional data)
3149   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3150      (memq (assq ,number bdata)
3151            bdata)))
3152
3153 (defmacro gnus-data-make (number mark pos header level)
3154   `(list ,number ,mark ,pos ,header ,level))
3155
3156 (defun gnus-data-enter (after-article number mark pos header level offset)
3157   (let ((data (gnus-data-find-list after-article)))
3158     (unless data
3159       (error "No such article: %d" after-article))
3160     (setcdr data (cons (gnus-data-make number mark pos header level)
3161                        (cdr data)))
3162     (setq gnus-newsgroup-data-reverse nil)
3163     (gnus-data-update-list (cddr data) offset)))
3164
3165 (defun gnus-data-enter-list (after-article list &optional offset)
3166   (when list
3167     (let ((data (and after-article (gnus-data-find-list after-article)))
3168           (ilist list))
3169       (if (not (or data
3170                    after-article))
3171           (let ((odata gnus-newsgroup-data))
3172             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3173             (when offset
3174               (gnus-data-update-list odata offset)))
3175         ;; Find the last element in the list to be spliced into the main
3176         ;; list.
3177         (setq list (last list))
3178         (if (not data)
3179             (progn
3180               (setcdr list gnus-newsgroup-data)
3181               (setq gnus-newsgroup-data ilist)
3182               (when offset
3183                 (gnus-data-update-list (cdr list) offset)))
3184           (setcdr list (cdr data))
3185           (setcdr data ilist)
3186           (when offset
3187             (gnus-data-update-list (cdr list) offset))))
3188       (setq gnus-newsgroup-data-reverse nil))))
3189
3190 (defun gnus-data-remove (article &optional offset)
3191   (let ((data gnus-newsgroup-data))
3192     (if (= (gnus-data-number (car data)) article)
3193         (progn
3194           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3195                 gnus-newsgroup-data-reverse nil)
3196           (when offset
3197             (gnus-data-update-list gnus-newsgroup-data offset)))
3198       (while (cdr data)
3199         (when (= (gnus-data-number (cadr data)) article)
3200           (setcdr data (cddr data))
3201           (when offset
3202             (gnus-data-update-list (cdr data) offset))
3203           (setq data nil
3204                 gnus-newsgroup-data-reverse nil))
3205         (setq data (cdr data))))))
3206
3207 (defmacro gnus-data-list (backward)
3208   `(if ,backward
3209        (or gnus-newsgroup-data-reverse
3210            (setq gnus-newsgroup-data-reverse
3211                  (reverse gnus-newsgroup-data)))
3212      gnus-newsgroup-data))
3213
3214 (defun gnus-data-update-list (data offset)
3215   "Add OFFSET to the POS of all data entries in DATA."
3216   (setq gnus-newsgroup-data-reverse nil)
3217   (while data
3218     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3219     (setq data (cdr data))))
3220
3221 (defun gnus-summary-article-pseudo-p (article)
3222   "Say whether this article is a pseudo article or not."
3223   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3224
3225 (defmacro gnus-summary-article-sparse-p (article)
3226   "Say whether this article is a sparse article or not."
3227   `(memq ,article gnus-newsgroup-sparse))
3228
3229 (defmacro gnus-summary-article-ancient-p (article)
3230   "Say whether this article is a sparse article or not."
3231   `(memq ,article gnus-newsgroup-ancient))
3232
3233 (defun gnus-article-parent-p (number)
3234   "Say whether this article is a parent or not."
3235   (let ((data (gnus-data-find-list number)))
3236     (and (cdr data)              ; There has to be an article after...
3237          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3238             (gnus-data-level (nth 1 data))))))
3239
3240 (defun gnus-article-children (number)
3241   "Return a list of all children to NUMBER."
3242   (let* ((data (gnus-data-find-list number))
3243          (level (gnus-data-level (car data)))
3244          children)
3245     (setq data (cdr data))
3246     (while (and data
3247                 (= (gnus-data-level (car data)) (1+ level)))
3248       (push (gnus-data-number (car data)) children)
3249       (setq data (cdr data)))
3250     children))
3251
3252 (defmacro gnus-summary-skip-intangible ()
3253   "If the current article is intangible, then jump to a different article."
3254   '(let ((to (get-text-property (point) 'gnus-intangible)))
3255      (and to (gnus-summary-goto-subject to))))
3256
3257 (defmacro gnus-summary-article-intangible-p ()
3258   "Say whether this article is intangible or not."
3259   '(get-text-property (point) 'gnus-intangible))
3260
3261 (defun gnus-article-read-p (article)
3262   "Say whether ARTICLE is read or not."
3263   (not (or (memq article gnus-newsgroup-marked)
3264            (memq article gnus-newsgroup-spam-marked)
3265            (memq article gnus-newsgroup-unreads)
3266            (memq article gnus-newsgroup-unselected)
3267            (memq article gnus-newsgroup-dormant))))
3268
3269 ;; Some summary mode macros.
3270
3271 (defmacro gnus-summary-article-number ()
3272   "The article number of the article on the current line.
3273 If there isn't an article number here, then we return the current
3274 article number."
3275   '(progn
3276      (gnus-summary-skip-intangible)
3277      (or (get-text-property (point) 'gnus-number)
3278          (gnus-summary-last-subject))))
3279
3280 (defmacro gnus-summary-article-header (&optional number)
3281   "Return the header of article NUMBER."
3282   `(gnus-data-header (gnus-data-find
3283                       ,(or number '(gnus-summary-article-number)))))
3284
3285 (defmacro gnus-summary-thread-level (&optional number)
3286   "Return the level of thread that starts with article NUMBER."
3287   `(if (and (eq gnus-summary-make-false-root 'dummy)
3288             (get-text-property (point) 'gnus-intangible))
3289        0
3290      (gnus-data-level (gnus-data-find
3291                        ,(or number '(gnus-summary-article-number))))))
3292
3293 (defmacro gnus-summary-article-mark (&optional number)
3294   "Return the mark of article NUMBER."
3295   `(gnus-data-mark (gnus-data-find
3296                     ,(or number '(gnus-summary-article-number)))))
3297
3298 (defmacro gnus-summary-article-pos (&optional number)
3299   "Return the position of the line of article NUMBER."
3300   `(gnus-data-pos (gnus-data-find
3301                    ,(or number '(gnus-summary-article-number)))))
3302
3303 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3304 (defmacro gnus-summary-article-subject (&optional number)
3305   "Return current subject string or nil if nothing."
3306   `(let ((headers
3307           ,(if number
3308                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3309              '(gnus-data-header (assq (gnus-summary-article-number)
3310                                       gnus-newsgroup-data)))))
3311      (and headers
3312           (vectorp headers)
3313           (mail-header-subject headers))))
3314
3315 (defmacro gnus-summary-article-score (&optional number)
3316   "Return current article score."
3317   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3318                   gnus-newsgroup-scored))
3319        gnus-summary-default-score 0))
3320
3321 (defun gnus-summary-article-children (&optional number)
3322   "Return a list of article numbers that are children of article NUMBER."
3323   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3324          (level (gnus-data-level (car data)))
3325          l children)
3326     (while (and (setq data (cdr data))
3327                 (> (setq l (gnus-data-level (car data))) level))
3328       (and (= (1+ level) l)
3329            (push (gnus-data-number (car data))
3330                  children)))
3331     (nreverse children)))
3332
3333 (defun gnus-summary-article-parent (&optional number)
3334   "Return the article number of the parent of article NUMBER."
3335   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3336                                     (gnus-data-list t)))
3337          (level (gnus-data-level (car data))))
3338     (if (zerop level)
3339         ()                              ; This is a root.
3340       ;; We search until we find an article with a level less than
3341       ;; this one.  That function has to be the parent.
3342       (while (and (setq data (cdr data))
3343                   (not (< (gnus-data-level (car data)) level))))
3344       (and data (gnus-data-number (car data))))))
3345
3346 (defun gnus-unread-mark-p (mark)
3347   "Say whether MARK is the unread mark."
3348   (= mark gnus-unread-mark))
3349
3350 (defun gnus-read-mark-p (mark)
3351   "Say whether MARK is one of the marks that mark as read.
3352 This is all marks except unread, ticked, dormant, and expirable."
3353   (not (or (= mark gnus-unread-mark)
3354            (= mark gnus-ticked-mark)
3355            (= mark gnus-spam-mark)
3356            (= mark gnus-dormant-mark)
3357            (= mark gnus-expirable-mark))))
3358
3359 (defmacro gnus-article-mark (number)
3360   "Return the MARK of article NUMBER.
3361 This macro should only be used when computing the mark the \"first\"
3362 time; i.e., when generating the summary lines.  After that,
3363 `gnus-summary-article-mark' should be used to examine the
3364 marks of articles."
3365   `(cond
3366     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3367     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3368     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3369     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3370     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3371     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3372     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3373     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3374            gnus-ancient-mark))))
3375
3376 ;; Saving hidden threads.
3377
3378 (defmacro gnus-save-hidden-threads (&rest forms)
3379   "Save hidden threads, eval FORMS, and restore the hidden threads."
3380   (let ((config (make-symbol "config")))
3381     `(let ((,config (gnus-hidden-threads-configuration)))
3382        (unwind-protect
3383            (save-excursion
3384              ,@forms)
3385          (gnus-restore-hidden-threads-configuration ,config)))))
3386 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3387 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3388
3389 (defun gnus-data-compute-positions ()
3390   "Compute the positions of all articles."
3391   (setq gnus-newsgroup-data-reverse nil)
3392   (let ((data gnus-newsgroup-data))
3393     (save-excursion
3394       (gnus-save-hidden-threads
3395         (gnus-summary-show-all-threads)
3396         (goto-char (point-min))
3397         (while data
3398           (while (get-text-property (point) 'gnus-intangible)
3399             (forward-line 1))
3400           (gnus-data-set-pos (car data) (+ (point) 3))
3401           (setq data (cdr data))
3402           (forward-line 1))))))
3403
3404 (defun gnus-hidden-threads-configuration ()
3405   "Return the current hidden threads configuration."
3406   (save-excursion
3407     (let (config)
3408       (goto-char (point-min))
3409       (while (not (eobp))
3410         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3411           (push (save-excursion (forward-line 0) (point)) config))
3412         (forward-line 1))
3413       config)))
3414
3415 (defun gnus-restore-hidden-threads-configuration (config)
3416   "Restore hidden threads configuration from CONFIG."
3417   (save-excursion
3418     (let (point (inhibit-read-only t))
3419       (while (setq point (pop config))
3420         (goto-char point)
3421         (gnus-summary-hide-thread)))))
3422
3423 ;; Various summary mode internalish functions.
3424
3425 (defun gnus-mouse-pick-article (e)
3426   (interactive "e")
3427   (mouse-set-point e)
3428   (gnus-summary-next-page nil t))
3429
3430 (defun gnus-summary-set-display-table ()
3431   "Change the display table.
3432 Odd characters have a tendency to mess
3433 up nicely formatted displays - we make all possible glyphs
3434 display only a single character."
3435
3436   ;; We start from the standard display table, if any.
3437   (let ((table (or (copy-sequence standard-display-table)
3438                    (make-display-table)))
3439         (i 32))
3440     ;; Nix out all the control chars...
3441     (while (>= (setq i (1- i)) 0)
3442       (gnus-put-display-table i [??] table))
3443    ;; ... but not newline and cr, of course.  (cr is necessary for the
3444     ;; selective display).
3445     (gnus-put-display-table ?\n nil table)
3446     (gnus-put-display-table ?\r nil table)
3447     ;; We keep TAB as well.
3448     (gnus-put-display-table ?\t nil table)
3449     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3450     ;; Emacs 23 (unicode), that are not set already.
3451     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3452                  160
3453                256)))
3454       (while (>= (setq i (1- i)) 127)
3455         ;; Only modify if the entry is nil.
3456         (unless (gnus-get-display-table i table)
3457           (gnus-put-display-table i [??] table))))
3458     (setq buffer-display-table table)))
3459
3460 (defun gnus-summary-set-article-display-arrow (pos)
3461   "Update the overlay arrow to point to line at position POS."
3462   (when gnus-summary-display-arrow
3463     (make-local-variable 'overlay-arrow-position)
3464     (make-local-variable 'overlay-arrow-string)
3465     (save-excursion
3466       (goto-char pos)
3467       (beginning-of-line)
3468       (unless overlay-arrow-position
3469         (setq overlay-arrow-position (make-marker)))
3470       (setq overlay-arrow-string "=>"
3471             overlay-arrow-position (set-marker overlay-arrow-position
3472                                                (point)
3473                                                (current-buffer))))))
3474
3475 (defun gnus-summary-setup-buffer (group)
3476   "Initialize summary buffer."
3477   (let ((buffer (gnus-summary-buffer-name group))
3478         (dead-name (concat "*Dead Summary "
3479                            (gnus-group-decoded-name group) "*")))
3480     ;; If a dead summary buffer exists, we kill it.
3481     (when (gnus-buffer-live-p dead-name)
3482       (gnus-kill-buffer dead-name))
3483     (if (get-buffer buffer)
3484         (progn
3485           (set-buffer buffer)
3486           (setq gnus-summary-buffer (current-buffer))
3487           (not gnus-newsgroup-prepared))
3488       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3489       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3490       (gnus-summary-mode group)
3491       (when gnus-carpal
3492         (gnus-carpal-setup-buffer 'summary))
3493       (when (gnus-group-quit-config group)
3494         (set (make-local-variable 'gnus-single-article-buffer) nil))
3495       (make-local-variable 'gnus-article-buffer)
3496       (make-local-variable 'gnus-article-current)
3497       (make-local-variable 'gnus-original-article-buffer)
3498       (setq gnus-newsgroup-name group)
3499       ;; Set any local variables in the group parameters.
3500       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3501       t)))
3502
3503 (defun gnus-set-global-variables ()
3504   "Set the global equivalents of the buffer-local variables.
3505 They are set to the latest values they had.  These reflect the summary
3506 buffer that was in action when the last article was fetched."
3507   (when (eq major-mode 'gnus-summary-mode)
3508     (setq gnus-summary-buffer (current-buffer))
3509     (let ((name gnus-newsgroup-name)
3510           (marked gnus-newsgroup-marked)
3511           (spam gnus-newsgroup-spam-marked)
3512           (unread gnus-newsgroup-unreads)
3513           (headers gnus-current-headers)
3514           (data gnus-newsgroup-data)
3515           (summary gnus-summary-buffer)
3516           (article-buffer gnus-article-buffer)
3517           (original gnus-original-article-buffer)
3518           (gac gnus-article-current)
3519           (reffed gnus-reffed-article-number)
3520           (score-file gnus-current-score-file)
3521           (default-charset gnus-newsgroup-charset)
3522           vlist)
3523       (let ((locals gnus-newsgroup-variables))
3524         (while locals
3525           (if (consp (car locals))
3526               (push (eval (caar locals)) vlist)
3527             (push (eval (car locals)) vlist))
3528           (setq locals (cdr locals)))
3529         (setq vlist (nreverse vlist)))
3530       (with-current-buffer gnus-group-buffer
3531         (setq gnus-newsgroup-name name
3532               gnus-newsgroup-marked marked
3533               gnus-newsgroup-spam-marked spam
3534               gnus-newsgroup-unreads unread
3535               gnus-current-headers headers
3536               gnus-newsgroup-data data
3537               gnus-article-current gac
3538               gnus-summary-buffer summary
3539               gnus-article-buffer article-buffer
3540               gnus-original-article-buffer original
3541               gnus-reffed-article-number reffed
3542               gnus-current-score-file score-file
3543               gnus-newsgroup-charset default-charset)
3544         (let ((locals gnus-newsgroup-variables))
3545           (while locals
3546             (if (consp (car locals))
3547                 (set (caar locals) (pop vlist))
3548               (set (car locals) (pop vlist)))
3549             (setq locals (cdr locals))))
3550         ;; The article buffer also has local variables.
3551         (when (gnus-buffer-live-p gnus-article-buffer)
3552           (set-buffer gnus-article-buffer)
3553           (setq gnus-summary-buffer summary))))))
3554
3555 (defun gnus-summary-article-unread-p (article)
3556   "Say whether ARTICLE is unread or not."
3557   (memq article gnus-newsgroup-unreads))
3558
3559 (defun gnus-summary-first-article-p (&optional article)
3560   "Return whether ARTICLE is the first article in the buffer."
3561   (if (not (setq article (or article (gnus-summary-article-number))))
3562       nil
3563     (eq article (caar gnus-newsgroup-data))))
3564
3565 (defun gnus-summary-last-article-p (&optional article)
3566   "Return whether ARTICLE is the last article in the buffer."
3567   (if (not (setq article (or article (gnus-summary-article-number))))
3568       ;; All non-existent numbers are the last article.  :-)
3569       t
3570     (not (cdr (gnus-data-find-list article)))))
3571
3572 (defun gnus-make-thread-indent-array (&optional n)
3573   (when (or n
3574             (progn (setq n 200) nil)
3575             (null gnus-thread-indent-array)
3576             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3577     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3578           gnus-thread-indent-array-level gnus-thread-indent-level)
3579     (while (>= n 0)
3580       (aset gnus-thread-indent-array n
3581             (make-string (* n gnus-thread-indent-level) ? ))
3582       (setq n (1- n)))))
3583
3584 (defun gnus-update-summary-mark-positions ()
3585   "Compute where the summary marks are to go."
3586   (save-excursion
3587     (when (gnus-buffer-exists-p gnus-summary-buffer)
3588       (set-buffer gnus-summary-buffer))
3589     (let ((spec gnus-summary-line-format-spec)
3590           pos)
3591       (save-excursion
3592         (gnus-set-work-buffer)
3593         (let ((gnus-tmp-unread ?Z)
3594               (gnus-replied-mark ?Z)
3595               (gnus-score-below-mark ?Z)
3596               (gnus-score-over-mark ?Z)
3597               (gnus-undownloaded-mark ?Z)
3598               (gnus-summary-line-format-spec spec)
3599               (gnus-newsgroup-downloadable '(0))
3600               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3601               case-fold-search ignores)
3602           ;; Here, all marks are bound to Z.
3603           (gnus-summary-insert-line header
3604                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3605           (goto-char (point-min))
3606           ;; Memorize the positions of the same characters as dummy marks.
3607           (while (re-search-forward "[A-D]" nil t)
3608             (push (point) ignores))
3609           (erase-buffer)
3610           ;; We use A-D as dummy marks in order to know column positions
3611           ;; where marks should be inserted.
3612           (setq gnus-tmp-unread ?A
3613                 gnus-replied-mark ?B
3614                 gnus-score-below-mark ?C
3615                 gnus-score-over-mark ?C
3616                 gnus-undownloaded-mark ?D)
3617           (gnus-summary-insert-line header
3618                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3619           ;; Ignore characters which aren't dummy marks.
3620           (dolist (p ignores)
3621             (delete-region (goto-char (1- p)) p)
3622             (insert ?Z))
3623           (goto-char (point-min))
3624           (setq pos (list (cons 'unread
3625                                 (and (search-forward "A" nil t)
3626                                      (- (point) (point-min) 1)))))
3627           (goto-char (point-min))
3628           (push (cons 'replied (and (search-forward "B" nil t)
3629                                     (- (point) (point-min) 1)))
3630                 pos)
3631           (goto-char (point-min))
3632           (push (cons 'score (and (search-forward "C" nil t)
3633                                   (- (point) (point-min) 1)))
3634                 pos)
3635           (goto-char (point-min))
3636           (push (cons 'download (and (search-forward "D" nil t)
3637                                      (- (point) (point-min) 1)))
3638                 pos)))
3639       (setq gnus-summary-mark-positions pos))))
3640
3641 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3642   "Insert a dummy root in the summary buffer."
3643   (beginning-of-line)
3644   (gnus-add-text-properties
3645    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3646    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3647
3648 (defun gnus-summary-extract-address-component (from)
3649   (or (car (funcall gnus-extract-address-components from))
3650       from))
3651
3652 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3653   (let ((mail-parse-charset gnus-newsgroup-charset)
3654         (ignored-from-addresses (gnus-ignored-from-addresses))
3655         ; Is it really necessary to do this next part for each summary line?
3656         ; Luckily, doesn't seem to slow things down much.
3657         (mail-parse-ignored-charsets
3658          (with-current-buffer gnus-summary-buffer
3659            gnus-newsgroup-ignored-charsets)))
3660     (or
3661      (and ignored-from-addresses
3662           (string-match ignored-from-addresses gnus-tmp-from)
3663           (let ((extra-headers (mail-header-extra header))
3664                 to
3665                 newsgroups)
3666             (cond
3667              ((setq to (cdr (assq 'To extra-headers)))
3668               (concat gnus-summary-to-prefix
3669                       (inline
3670                         (gnus-summary-extract-address-component
3671                          (funcall gnus-decode-encoded-address-function to)))))
3672              ((setq newsgroups
3673                     (or
3674                      (cdr (assq 'Newsgroups extra-headers))
3675                      (and
3676                       (memq 'Newsgroups gnus-extra-headers)
3677                       (eq (car (gnus-find-method-for-group
3678                                 gnus-newsgroup-name)) 'nntp)
3679                       (gnus-group-real-name gnus-newsgroup-name))))
3680               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3681      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3682
3683 (defun gnus-summary-insert-line (gnus-tmp-header
3684                                  gnus-tmp-level gnus-tmp-current
3685                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3686                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3687                                  &optional gnus-tmp-dummy gnus-tmp-score
3688                                  gnus-tmp-process)
3689   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3690       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3691                                           gnus-tmp-level)))
3692   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3693          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3694          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3695          (gnus-tmp-score-char
3696           (if (or (null gnus-summary-default-score)
3697                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3698                       gnus-summary-zcore-fuzz))
3699               ?                         ;Whitespace
3700             (if (< gnus-tmp-score gnus-summary-default-score)
3701                 gnus-score-below-mark gnus-score-over-mark)))
3702          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3703          (gnus-tmp-replied
3704           (cond (gnus-tmp-process gnus-process-mark)
3705                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3706                  gnus-cached-mark)
3707                 (gnus-tmp-replied gnus-replied-mark)
3708                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3709                  gnus-forwarded-mark)
3710                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3711                  gnus-saved-mark)
3712                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3713                  gnus-recent-mark)
3714                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3715                  gnus-unseen-mark)
3716                 (t gnus-no-mark)))
3717          (gnus-tmp-downloaded
3718           (cond (undownloaded
3719                  gnus-undownloaded-mark)
3720                 (gnus-newsgroup-agentized
3721                  gnus-downloaded-mark)
3722                 (t
3723                  gnus-no-mark)))
3724          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3725          (gnus-tmp-name
3726           (cond
3727            ((string-match "<[^>]+> *$" gnus-tmp-from)
3728             (let ((beg (match-beginning 0)))
3729               (or (and (string-match "^\".+\"" gnus-tmp-from)
3730                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3731                   (substring gnus-tmp-from 0 beg))))
3732            ((string-match "(.+)" gnus-tmp-from)
3733             (substring gnus-tmp-from
3734                        (1+ (match-beginning 0)) (1- (match-end 0))))
3735            (t gnus-tmp-from)))
3736          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3737          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3738          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3739          (inhibit-read-only t))
3740     (when (string= gnus-tmp-name "")
3741       (setq gnus-tmp-name gnus-tmp-from))
3742     (unless (numberp gnus-tmp-lines)
3743       (setq gnus-tmp-lines -1))
3744     (if (= gnus-tmp-lines -1)
3745         (setq gnus-tmp-lines "?")
3746       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3747     (condition-case ()
3748         (gnus-put-text-property
3749          (point)
3750          (progn (eval gnus-summary-line-format-spec) (point))
3751          'gnus-number gnus-tmp-number)
3752       (error (gnus-message 5 "Error updating the summary line")))
3753     (when (gnus-visual-p 'summary-highlight 'highlight)
3754       (forward-line -1)
3755       (gnus-summary-highlight-line)
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-summary-highlight-line)
3789         (gnus-run-hooks 'gnus-summary-update-hook)))))
3790
3791 (defvar gnus-tmp-new-adopts nil)
3792
3793 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3794   "Return the number of articles in THREAD.
3795 This may be 0 in some cases -- if none of the articles in
3796 the thread are to be displayed."
3797   (let* ((number
3798          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3799           (cond
3800            ((not (listp thread))
3801             1)
3802            ((and (consp thread) (cdr thread))
3803             (apply
3804              '+ 1 (mapcar
3805                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3806            ((null thread)
3807             1)
3808            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3809             1)
3810            (t 0))))
3811     (when (and level (zerop level) gnus-tmp-new-adopts)
3812       (incf number
3813             (apply '+ (mapcar
3814                        'gnus-summary-number-of-articles-in-thread
3815                        gnus-tmp-new-adopts))))
3816     (if char
3817         (if (> number 1) gnus-not-empty-thread-mark
3818           gnus-empty-thread-mark)
3819       number)))
3820
3821 (defsubst gnus-summary-line-message-size (head)
3822   "Return pretty-printed version of message size.
3823 This function is intended to be used in
3824 `gnus-summary-line-format-alist'."
3825   (let ((c (or (mail-header-chars head) -1)))
3826     (cond ((< c 0) "n/a")               ; chars not available
3827           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3828           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3829           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3830           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3831
3832
3833 (defun gnus-summary-set-local-parameters (group)
3834   "Go through the local params of GROUP and set all variable specs in that list."
3835   (let ((vars '(quit-config)))          ; Ignore quit-config.
3836     (dolist (elem (gnus-group-find-parameter group))
3837       (and (consp elem)                 ; Has to be a cons.
3838            (consp (cdr elem))           ; The cdr has to be a list.
3839            (symbolp (car elem))         ; Has to be a symbol in there.
3840            (not (memq (car elem) vars))
3841            (ignore-errors
3842              (push (car elem) vars)
3843              ;; Variables like `gnus-show-threads' that are globally
3844              ;; bound, if used as group parameters, need to get to be
3845              ;; buffer-local, whereas just parameters like `gcc-self',
3846              ;; `timestamp', etc. should not be bound as variables.
3847              (if (boundp (car elem))
3848                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3849                (eval (nth 1 elem))))))))
3850
3851 (defun gnus-summary-read-group (group &optional show-all no-article
3852                                       kill-buffer no-display backward
3853                                       select-articles)
3854   "Start reading news in newsgroup GROUP.
3855 If SHOW-ALL is non-nil, already read articles are also listed.
3856 If NO-ARTICLE is non-nil, no article is selected initially.
3857 If NO-DISPLAY, don't generate a summary buffer."
3858   (let (result)
3859     (while (and group
3860                 (null (setq result
3861                             (let ((gnus-auto-select-next nil))
3862                               (or (gnus-summary-read-group-1
3863                                    group show-all no-article
3864                                    kill-buffer no-display
3865                                    select-articles)
3866                                   (setq show-all nil
3867                                         select-articles nil)))))
3868                 (eq gnus-auto-select-next 'quietly))
3869       (set-buffer gnus-group-buffer)
3870       ;; The entry function called above goes to the next
3871       ;; group automatically, so we go two groups back
3872       ;; if we are searching for the previous group.
3873       (when backward
3874         (gnus-group-prev-unread-group 2))
3875       (if (not (equal group (gnus-group-group-name)))
3876           (setq group (gnus-group-group-name))
3877         (setq group nil)))
3878     result))
3879
3880 (defun gnus-summary-read-group-1 (group show-all no-article
3881                                         kill-buffer no-display
3882                                         &optional select-articles)
3883   ;; Killed foreign groups can't be entered.
3884   ;;  (when (and (not (gnus-group-native-p group))
3885   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3886   ;;    (error "Dead non-native groups can't be entered"))
3887   (gnus-message 5 "Retrieving newsgroup: %s..."
3888                 (gnus-group-decoded-name group))
3889   (let* ((new-group (gnus-summary-setup-buffer group))
3890          (quit-config (gnus-group-quit-config group))
3891          (did-select (and new-group (gnus-select-newsgroup
3892                                      group show-all select-articles))))
3893     (cond
3894      ;; This summary buffer exists already, so we just select it.
3895      ((not new-group)
3896       (gnus-set-global-variables)
3897       (when kill-buffer
3898         (gnus-kill-or-deaden-summary kill-buffer))
3899       (gnus-configure-windows 'summary 'force)
3900       (gnus-set-mode-line 'summary)
3901       (gnus-summary-position-point)
3902       (message "")
3903       t)
3904      ;; We couldn't select this group.
3905      ((null did-select)
3906       (when (and (eq major-mode 'gnus-summary-mode)
3907                  (not (equal (current-buffer) kill-buffer)))
3908         (kill-buffer (current-buffer))
3909         (if (not quit-config)
3910             (progn
3911               ;; Update the info -- marks might need to be removed,
3912               ;; for instance.
3913               (gnus-summary-update-info)
3914               (set-buffer gnus-group-buffer)
3915               (gnus-group-jump-to-group group)
3916               (gnus-group-next-unread-group 1))
3917           (gnus-handle-ephemeral-exit quit-config)))
3918       (let ((grpinfo (gnus-get-info group)))
3919         (if (null (gnus-info-read grpinfo))
3920             (gnus-message 3 "Group %s contains no messages"
3921                           (gnus-group-decoded-name group))
3922           (gnus-message 3 "Can't select group")))
3923       nil)
3924      ;; The user did a `C-g' while prompting for number of articles,
3925      ;; so we exit this group.
3926      ((eq did-select 'quit)
3927       (and (eq major-mode 'gnus-summary-mode)
3928            (not (equal (current-buffer) kill-buffer))
3929            (kill-buffer (current-buffer)))
3930       (when kill-buffer
3931         (gnus-kill-or-deaden-summary kill-buffer))
3932       (if (not quit-config)
3933           (progn
3934             (set-buffer gnus-group-buffer)
3935             (gnus-group-jump-to-group group)
3936             (gnus-configure-windows 'group 'force))
3937         (gnus-handle-ephemeral-exit quit-config))
3938       ;; Finally signal the quit.
3939       (signal 'quit nil))
3940      ;; The group was successfully selected.
3941      (t
3942       (gnus-set-global-variables)
3943       ;; Save the active value in effect when the group was entered.
3944       (setq gnus-newsgroup-active
3945             (gnus-copy-sequence
3946              (gnus-active gnus-newsgroup-name)))
3947       ;; You can change the summary buffer in some way with this hook.
3948       (gnus-run-hooks 'gnus-select-group-hook)
3949       (when (memq 'summary (gnus-update-format-specifications
3950                             nil 'summary 'summary-mode 'summary-dummy))
3951         ;; The format specification for the summary line was updated,
3952         ;; so we need to update the mark positions as well.
3953         (gnus-update-summary-mark-positions))
3954       ;; Do score processing.
3955       (when gnus-use-scoring
3956         (gnus-possibly-score-headers))
3957       ;; Check whether to fill in the gaps in the threads.
3958       (when gnus-build-sparse-threads
3959         (gnus-build-sparse-threads))
3960       ;; Find the initial limit.
3961       (if show-all
3962           (let ((gnus-newsgroup-dormant nil))
3963             (gnus-summary-initial-limit show-all))
3964         (gnus-summary-initial-limit show-all))
3965       ;; Generate the summary buffer.
3966       (unless no-display
3967         (gnus-summary-prepare))
3968       (when gnus-use-trees
3969         (gnus-tree-open group)
3970         (setq gnus-summary-highlight-line-function
3971               'gnus-tree-highlight-article))
3972       ;; If the summary buffer is empty, but there are some low-scored
3973       ;; articles or some excluded dormants, we include these in the
3974       ;; buffer.
3975       (when (and (zerop (buffer-size))
3976                  (not no-display))
3977         (cond (gnus-newsgroup-dormant
3978                (gnus-summary-limit-include-dormant))
3979               ((and gnus-newsgroup-scored show-all)
3980                (gnus-summary-limit-include-expunged t))))
3981       ;; Function `gnus-apply-kill-file' must be called in this hook.
3982       (gnus-run-hooks 'gnus-apply-kill-hook)
3983       (if (and (zerop (buffer-size))
3984                (not no-display))
3985           (progn
3986             ;; This newsgroup is empty.
3987             (gnus-summary-catchup-and-exit nil t)
3988             (gnus-message 6 "No unread news")
3989             (when kill-buffer
3990               (gnus-kill-or-deaden-summary kill-buffer))
3991             ;; Return nil from this function.
3992             nil)
3993         ;; Hide conversation thread subtrees.  We cannot do this in
3994         ;; gnus-summary-prepare-hook since kill processing may not
3995         ;; work with hidden articles.
3996         (gnus-summary-maybe-hide-threads)
3997         (when kill-buffer
3998           (gnus-kill-or-deaden-summary kill-buffer))
3999         (gnus-summary-auto-select-subject)
4000         ;; Show first unread article if requested.
4001         (if (and (not no-article)
4002                  (not no-display)
4003                  gnus-newsgroup-unreads
4004                  gnus-auto-select-first)
4005             (progn
4006               (gnus-configure-windows 'summary)
4007               (let ((art (gnus-summary-article-number)))
4008                 (unless (and (not gnus-plugged)
4009                              (or (memq art gnus-newsgroup-undownloaded)
4010                                  (memq art gnus-newsgroup-downloadable)))
4011                   (gnus-summary-goto-article art))))
4012           ;; Don't select any articles.
4013           (gnus-summary-position-point)
4014           (gnus-configure-windows 'summary 'force)
4015           (gnus-set-mode-line 'summary))
4016         (when (and gnus-auto-center-group
4017                    (get-buffer-window gnus-group-buffer t))
4018           ;; Gotta use windows, because recenter does weird stuff if
4019           ;; the current buffer ain't the displayed window.
4020           (let ((owin (selected-window)))
4021             (select-window (get-buffer-window gnus-group-buffer t))
4022             (when (gnus-group-goto-group group)
4023               (recenter))
4024             (select-window owin)))
4025         ;; Mark this buffer as "prepared".
4026         (setq gnus-newsgroup-prepared t)
4027         (gnus-run-hooks 'gnus-summary-prepared-hook)
4028         (unless (gnus-ephemeral-group-p group)
4029           (gnus-group-update-group group))
4030         t)))))
4031
4032 (defun gnus-summary-auto-select-subject ()
4033   "Select the subject line on initial group entry."
4034   (goto-char (point-min))
4035   (cond
4036    ((eq gnus-auto-select-subject 'best)
4037     (gnus-summary-best-unread-subject))
4038    ((eq gnus-auto-select-subject 'unread)
4039     (gnus-summary-first-unread-subject))
4040    ((eq gnus-auto-select-subject 'unseen)
4041     (gnus-summary-first-unseen-subject))
4042    ((eq gnus-auto-select-subject 'unseen-or-unread)
4043     (gnus-summary-first-unseen-or-unread-subject))
4044    ((eq gnus-auto-select-subject 'first)
4045     ;; Do nothing.
4046     )
4047    ((functionp gnus-auto-select-subject)
4048     (funcall gnus-auto-select-subject))))
4049
4050 (defun gnus-summary-prepare ()
4051   "Generate the summary buffer."
4052   (interactive)
4053   (let ((inhibit-read-only t))
4054     (erase-buffer)
4055     (setq gnus-newsgroup-data nil
4056           gnus-newsgroup-data-reverse nil)
4057     (gnus-run-hooks 'gnus-summary-generate-hook)
4058     ;; Generate the buffer, either with threads or without.
4059     (when gnus-newsgroup-headers
4060       (gnus-summary-prepare-threads
4061        (if gnus-show-threads
4062            (gnus-sort-gathered-threads
4063             (funcall gnus-summary-thread-gathering-function
4064                      (gnus-sort-threads
4065                       (gnus-cut-threads (gnus-make-threads)))))
4066          ;; Unthreaded display.
4067          (gnus-sort-articles gnus-newsgroup-headers))))
4068     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4069     ;; Call hooks for modifying summary buffer.
4070     (goto-char (point-min))
4071     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4072
4073 (defsubst gnus-general-simplify-subject (subject)
4074   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4075   (setq subject
4076         (cond
4077          ;; Truncate the subject.
4078          (gnus-simplify-subject-functions
4079           (gnus-map-function gnus-simplify-subject-functions subject))
4080          ((numberp gnus-summary-gather-subject-limit)
4081           (setq subject (gnus-simplify-subject-re subject))
4082           (if (> (length subject) gnus-summary-gather-subject-limit)
4083               (substring subject 0 gnus-summary-gather-subject-limit)
4084             subject))
4085          ;; Fuzzily simplify it.
4086          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4087           (gnus-simplify-subject-fuzzy subject))
4088          ;; Just remove the leading "Re:".
4089          (t
4090           (gnus-simplify-subject-re subject))))
4091
4092   (if (and gnus-summary-gather-exclude-subject
4093            (string-match gnus-summary-gather-exclude-subject subject))
4094       nil                         ; This article shouldn't be gathered
4095     subject))
4096
4097 (defun gnus-summary-simplify-subject-query ()
4098   "Query where the respool algorithm would put this article."
4099   (interactive)
4100   (gnus-summary-select-article)
4101   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4102
4103 (defun gnus-gather-threads-by-subject (threads)
4104   "Gather threads by looking at Subject headers."
4105   (if (not gnus-summary-make-false-root)
4106       threads
4107     (let ((hashtb (gnus-make-hashtable 1024))
4108           (prev threads)
4109           (result threads)
4110           subject hthread whole-subject)
4111       (while threads
4112         (setq subject (gnus-general-simplify-subject
4113                        (setq whole-subject (mail-header-subject
4114                                             (caar threads)))))
4115         (when subject
4116           (if (setq hthread (gnus-gethash subject hashtb))
4117               (progn
4118                 ;; We enter a dummy root into the thread, if we
4119                 ;; haven't done that already.
4120                 (unless (stringp (caar hthread))
4121                   (setcar hthread (list whole-subject (car hthread))))
4122                 ;; We add this new gathered thread to this gathered
4123                 ;; thread.
4124                 (setcdr (car hthread)
4125                         (nconc (cdar hthread) (list (car threads))))
4126                 ;; Remove it from the list of threads.
4127                 (setcdr prev (cdr threads))
4128                 (setq threads prev))
4129             ;; Enter this thread into the hash table.
4130             (gnus-sethash subject
4131                           (if gnus-summary-make-false-root-always
4132                               (progn
4133                                 ;; If you want a dummy root above all
4134                                 ;; threads...
4135                                 (setcar threads (list whole-subject
4136                                                       (car threads)))
4137                                 threads)
4138                             threads)
4139                           hashtb)))
4140         (setq prev threads)
4141         (setq threads (cdr threads)))
4142       result)))
4143
4144 (defun gnus-gather-threads-by-references (threads)
4145   "Gather threads by looking at References headers."
4146   (let ((idhashtb (gnus-make-hashtable 1024))
4147         (thhashtb (gnus-make-hashtable 1024))
4148         (prev threads)
4149         (result threads)
4150         ids references id gthread gid entered ref)
4151     (while threads
4152       (when (setq references (mail-header-references (caar threads)))
4153         (setq id (mail-header-id (caar threads))
4154               ids (inline (gnus-split-references references))
4155               entered nil)
4156         (while (setq ref (pop ids))
4157           (setq ids (delete ref ids))
4158           (if (not (setq gid (gnus-gethash ref idhashtb)))
4159               (progn
4160                 (gnus-sethash ref id idhashtb)
4161                 (gnus-sethash id threads thhashtb))
4162             (setq gthread (gnus-gethash gid thhashtb))
4163             (unless entered
4164               ;; We enter a dummy root into the thread, if we
4165               ;; haven't done that already.
4166               (unless (stringp (caar gthread))
4167                 (setcar gthread (list (mail-header-subject (caar gthread))
4168                                       (car gthread))))
4169               ;; We add this new gathered thread to this gathered
4170               ;; thread.
4171               (setcdr (car gthread)
4172                       (nconc (cdar gthread) (list (car threads)))))
4173             ;; Add it into the thread hash table.
4174             (gnus-sethash id gthread thhashtb)
4175             (setq entered t)
4176             ;; Remove it from the list of threads.
4177             (setcdr prev (cdr threads))
4178             (setq threads prev))))
4179       (setq prev threads)
4180       (setq threads (cdr threads)))
4181     result))
4182
4183 (defun gnus-sort-gathered-threads (threads)
4184   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4185   (let ((result threads))
4186     (while threads
4187       (when (stringp (caar threads))
4188         (setcdr (car threads)
4189                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4190       (setq threads (cdr threads)))
4191     result))
4192
4193 (defun gnus-thread-loop-p (root thread)
4194   "Say whether ROOT is in THREAD."
4195   (let ((stack (list thread))
4196         (infloop 0)
4197         th)
4198     (while (setq thread (pop stack))
4199       (setq th (cdr thread))
4200       (while (and th
4201                   (not (eq (caar th) root)))
4202         (pop th))
4203       (if th
4204           ;; We have found a loop.
4205           (let (ref-dep)
4206             (setcdr thread (delq (car th) (cdr thread)))
4207             (if (boundp (setq ref-dep (intern "none"
4208                                               gnus-newsgroup-dependencies)))
4209                 (setcdr (symbol-value ref-dep)
4210                         (nconc (cdr (symbol-value ref-dep))
4211                                (list (car th))))
4212               (set ref-dep (list nil (car th))))
4213             (setq infloop 1
4214                   stack nil))
4215         ;; Push all the subthreads onto the stack.
4216         (push (cdr thread) stack)))
4217     infloop))
4218
4219 (defun gnus-make-threads ()
4220   "Go through the dependency hashtb and find the roots.  Return all threads."
4221   (let (threads)
4222     (while (catch 'infloop
4223              (mapatoms
4224               (lambda (refs)
4225                 ;; Deal with self-referencing References loops.
4226                 (when (and (car (symbol-value refs))
4227                            (not (zerop
4228                                  (apply
4229                                   '+
4230                                   (mapcar
4231                                    (lambda (thread)
4232                                      (gnus-thread-loop-p
4233                                       (car (symbol-value refs)) thread))
4234                                    (cdr (symbol-value refs)))))))
4235                   (setq threads nil)
4236                   (throw 'infloop t))
4237                 (unless (car (symbol-value refs))
4238                   ;; These threads do not refer back to any other
4239                   ;; articles, so they're roots.
4240                   (setq threads (append (cdr (symbol-value refs)) threads))))
4241               gnus-newsgroup-dependencies)))
4242     threads))
4243
4244 ;; Build the thread tree.
4245 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4246   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4247
4248 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4249 if it was already present.
4250
4251 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4252 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4253 Message-IDs will be renamed to a unique Message-ID before being
4254 entered.
4255
4256 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4257   (let* ((id (mail-header-id header))
4258          (id-dep (and id (intern id dependencies)))
4259          parent-id ref ref-dep ref-header replaced)
4260     ;; Enter this `header' in the `dependencies' table.
4261     (cond
4262      ((not id-dep)
4263       (setq header nil))
4264      ;; The first two cases do the normal part: enter a new `header'
4265      ;; in the `dependencies' table.
4266      ((not (boundp id-dep))
4267       (set id-dep (list header)))
4268      ((null (car (symbol-value id-dep)))
4269       (setcar (symbol-value id-dep) header))
4270
4271      ;; From here the `header' was already present in the
4272      ;; `dependencies' table.
4273      (force-new
4274       ;; Overrides an existing entry;
4275       ;; just set the header part of the entry.
4276       (setcar (symbol-value id-dep) header)
4277       (setq replaced t))
4278
4279      ;; Renames the existing `header' to a unique Message-ID.
4280      ((not gnus-summary-ignore-duplicates)
4281       ;; An article with this Message-ID has already been seen.
4282       ;; We rename the Message-ID.
4283       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4284            (list header))
4285       (mail-header-set-id header id))
4286
4287      ;; The last case ignores an existing entry, except it adds any
4288      ;; additional Xrefs (in case the two articles came from different
4289      ;; servers.
4290      ;; Also sets `header' to `nil' meaning that the `dependencies'
4291      ;; table was *not* modified.
4292      (t
4293       (mail-header-set-xref
4294        (car (symbol-value id-dep))
4295        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4296                    "")
4297                (or (mail-header-xref header) "")))
4298       (setq header nil)))
4299
4300     (when (and header (not replaced))
4301       ;; First check that we are not creating a References loop.
4302       (setq parent-id (gnus-parent-id (mail-header-references header)))
4303       (setq ref parent-id)
4304       (while (and ref
4305                   (setq ref-dep (intern-soft ref dependencies))
4306                   (boundp ref-dep)
4307                   (setq ref-header (car (symbol-value ref-dep))))
4308         (if (string= id ref)
4309             ;; Yuk!  This is a reference loop.  Make the article be a
4310             ;; root article.
4311             (progn
4312               (mail-header-set-references (car (symbol-value id-dep)) "none")
4313               (setq ref nil)
4314               (setq parent-id nil))
4315           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4316       (setq ref-dep (intern (or parent-id "none") dependencies))
4317       (if (boundp ref-dep)
4318           (setcdr (symbol-value ref-dep)
4319                   (nconc (cdr (symbol-value ref-dep))
4320                          (list (symbol-value id-dep))))
4321         (set ref-dep (list nil (symbol-value id-dep)))))
4322     header))
4323
4324 (defun gnus-extract-message-id-from-in-reply-to (string)
4325   (if (string-match "<[^>]+>" string)
4326       (substring string (match-beginning 0) (match-end 0))
4327     nil))
4328
4329 (defun gnus-build-sparse-threads ()
4330   (let ((headers gnus-newsgroup-headers)
4331         (mail-parse-charset gnus-newsgroup-charset)
4332         (gnus-summary-ignore-duplicates t)
4333         header references generation relations
4334         subject child end new-child date)
4335     ;; First we create an alist of generations/relations, where
4336     ;; generations is how much we trust the relation, and the relation
4337     ;; is parent/child.
4338     (gnus-message 7 "Making sparse threads...")
4339     (save-excursion
4340       (nnheader-set-temp-buffer " *gnus sparse threads*")
4341       (while (setq header (pop headers))
4342         (when (and (setq references (mail-header-references header))
4343                    (not (string= references "")))
4344           (insert references)
4345           (setq child (mail-header-id header)
4346                 subject (mail-header-subject header)
4347                 date (mail-header-date header)
4348                 generation 0)
4349           (while (search-backward ">" nil t)
4350             (setq end (1+ (point)))
4351             (when (search-backward "<" nil t)
4352               (setq new-child (buffer-substring (point) end))
4353               (push (list (incf generation)
4354                           child (setq child new-child)
4355                           subject date)
4356                     relations)))
4357           (when child
4358             (push (list (1+ generation) child nil subject) relations))
4359           (erase-buffer)))
4360       (kill-buffer (current-buffer)))
4361     ;; Sort over trustworthiness.
4362     (dolist (relation (sort relations 'car-less-than-car))
4363       (when (gnus-dependencies-add-header
4364              (make-full-mail-header
4365               gnus-reffed-article-number
4366               (nth 3 relation) "" (or (nth 4 relation) "")
4367               (nth 1 relation)
4368               (or (nth 2 relation) "") 0 0 "")
4369              gnus-newsgroup-dependencies nil)
4370         (push gnus-reffed-article-number gnus-newsgroup-limit)
4371         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4372         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4373               gnus-newsgroup-reads)
4374         (decf gnus-reffed-article-number)))
4375     (gnus-message 7 "Making sparse threads...done")))
4376
4377 (defun gnus-build-old-threads ()
4378   ;; Look at all the articles that refer back to old articles, and
4379   ;; fetch the headers for the articles that aren't there.  This will
4380   ;; build complete threads - if the roots haven't been expired by the
4381   ;; server, that is.
4382   (let ((mail-parse-charset gnus-newsgroup-charset)
4383         id heads)
4384     (mapatoms
4385      (lambda (refs)
4386        (when (not (car (symbol-value refs)))
4387          (setq heads (cdr (symbol-value refs)))
4388          (while heads
4389            (if (memq (mail-header-number (caar heads))
4390                      gnus-newsgroup-dormant)
4391                (setq heads (cdr heads))
4392              (setq id (symbol-name refs))
4393              (while (and (setq id (gnus-build-get-header id))
4394                          (not (car (gnus-id-to-thread id)))))
4395              (setq heads nil)))))
4396      gnus-newsgroup-dependencies)))
4397
4398 (defsubst gnus-remove-odd-characters (string)
4399   "Translate STRING into something that doesn't contain weird characters."
4400   (mm-subst-char-in-string
4401    ?\r ?\-
4402    (mm-subst-char-in-string ?\n ?\- string t) t))
4403
4404 ;; This function has to be called with point after the article number
4405 ;; on the beginning of the line.
4406 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4407   (let ((eol (point-at-eol))
4408         (buffer (current-buffer))
4409         header references in-reply-to)
4410
4411     ;; overview: [num subject from date id refs chars lines misc]
4412     (unwind-protect
4413         (let (x)
4414           (narrow-to-region (point) eol)
4415           (unless (eobp)
4416             (forward-char))
4417
4418           (setq header
4419                 (make-full-mail-header
4420                  number                 ; number
4421                  (condition-case ()     ; subject
4422                      (gnus-remove-odd-characters
4423                       (funcall gnus-decode-encoded-word-function
4424                                (setq x (nnheader-nov-field))))
4425                    (error x))
4426                  (condition-case ()     ; from
4427                      (gnus-remove-odd-characters
4428                       (funcall gnus-decode-encoded-address-function
4429                                (setq x (nnheader-nov-field))))
4430                    (error x))
4431                  (nnheader-nov-field)   ; date
4432                  (nnheader-nov-read-message-id number)  ; id
4433                  (setq references (nnheader-nov-field)) ; refs
4434                  (nnheader-nov-read-integer) ; chars
4435                  (nnheader-nov-read-integer) ; lines
4436                  (unless (eobp)
4437                    (if (looking-at "Xref: ")
4438                        (goto-char (match-end 0)))
4439                    (nnheader-nov-field)) ; Xref
4440                  (nnheader-nov-parse-extra)))) ; extra
4441
4442       (widen))
4443
4444     (when (and (string= references "")
4445                (setq in-reply-to (mail-header-extra header))
4446                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4447       (mail-header-set-references
4448        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4449
4450     (when gnus-alter-header-function
4451       (funcall gnus-alter-header-function header))
4452     (gnus-dependencies-add-header header dependencies force-new)))
4453
4454 (defun gnus-build-get-header (id)
4455   "Look through the buffer of NOV lines and find the header to ID.
4456 Enter this line into the dependencies hash table, and return
4457 the id of the parent article (if any)."
4458   (let ((deps gnus-newsgroup-dependencies)
4459         found header)
4460     (prog1
4461         (with-current-buffer nntp-server-buffer
4462           (let ((case-fold-search nil))
4463             (goto-char (point-min))
4464             (while (and (not found)
4465                         (search-forward id nil t))
4466               (beginning-of-line)
4467               (setq found (looking-at
4468                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4469                                    (regexp-quote id))))
4470               (or found (beginning-of-line 2)))
4471             (when found
4472               (beginning-of-line)
4473               (and
4474                (setq header (gnus-nov-parse-line
4475                              (read (current-buffer)) deps))
4476                (gnus-parent-id (mail-header-references header))))))
4477       (when header
4478         (let ((number (mail-header-number header)))
4479           (push number gnus-newsgroup-limit)
4480           (push header gnus-newsgroup-headers)
4481           (if (memq number gnus-newsgroup-unselected)
4482               (progn
4483                 (setq gnus-newsgroup-unreads
4484                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4485                                                number))
4486                 (setq gnus-newsgroup-unselected
4487                       (delq number gnus-newsgroup-unselected)))
4488             (push number gnus-newsgroup-ancient)))))))
4489
4490 (defun gnus-build-all-threads ()
4491   "Read all the headers."
4492   (let ((gnus-summary-ignore-duplicates t)
4493         (mail-parse-charset gnus-newsgroup-charset)
4494         (dependencies gnus-newsgroup-dependencies)
4495         header article)
4496     (with-current-buffer nntp-server-buffer
4497       (let ((case-fold-search nil))
4498         (goto-char (point-min))
4499         (while (not (eobp))
4500           (ignore-errors
4501             (setq article (read (current-buffer))
4502                   header (gnus-nov-parse-line article dependencies)))
4503           (when header
4504             (with-current-buffer gnus-summary-buffer
4505               (push header gnus-newsgroup-headers)
4506               (if (memq (setq article (mail-header-number header))
4507                         gnus-newsgroup-unselected)
4508                   (progn
4509                     (setq gnus-newsgroup-unreads
4510                           (gnus-add-to-sorted-list
4511                            gnus-newsgroup-unreads article))
4512                     (setq gnus-newsgroup-unselected
4513                           (delq article gnus-newsgroup-unselected)))
4514                 (push article gnus-newsgroup-ancient)))
4515             (forward-line 1)))))))
4516
4517 (defun gnus-summary-update-article-line (article header)
4518   "Update the line for ARTICLE using HEADER."
4519   (let* ((id (mail-header-id header))
4520          (thread (gnus-id-to-thread id)))
4521     (unless thread
4522       (error "Article in no thread"))
4523     ;; Update the thread.
4524     (setcar thread header)
4525     (gnus-summary-goto-subject article)
4526     (let* ((datal (gnus-data-find-list article))
4527            (data (car datal))
4528            (inhibit-read-only t)
4529            (level (gnus-summary-thread-level)))
4530       (gnus-delete-line)
4531       (let ((inserted (- (point)
4532                          (progn
4533                            (gnus-summary-insert-line
4534                             header level nil
4535                             (memq article gnus-newsgroup-undownloaded)
4536                             (gnus-article-mark article)
4537                             (memq article gnus-newsgroup-replied)
4538                             (memq article gnus-newsgroup-expirable)
4539                             ;; Only insert the Subject string when it's different
4540                             ;; from the previous Subject string.
4541                             (if (and
4542                                  gnus-show-threads
4543                                  (gnus-subject-equal
4544                                   (condition-case ()
4545                                       (mail-header-subject
4546                                        (gnus-data-header
4547                                         (cadr
4548                                          (gnus-data-find-list
4549                                           article
4550                                           (gnus-data-list t)))))
4551                                     ;; Error on the side of excessive subjects.
4552                                     (error ""))
4553                                   (mail-header-subject header)))
4554                                 ""
4555                               (mail-header-subject header))
4556                             nil (cdr (assq article gnus-newsgroup-scored))
4557                             (memq article gnus-newsgroup-processable))
4558                            (point)))))
4559         (when (cdr datal)
4560           (gnus-data-update-list
4561            (cdr datal)
4562            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4563
4564 (defun gnus-summary-update-article (article &optional iheader)
4565   "Update ARTICLE in the summary buffer."
4566   (set-buffer gnus-summary-buffer)
4567   (let* ((header (gnus-summary-article-header article))
4568          (id (mail-header-id header))
4569          (data (gnus-data-find article))
4570          (thread (gnus-id-to-thread id))
4571          (references (mail-header-references header))
4572          (parent
4573           (gnus-id-to-thread
4574            (or (gnus-parent-id
4575                 (when (and references
4576                            (not (equal "" references)))
4577                   references))
4578                "none")))
4579          (inhibit-read-only t)
4580          (old (car thread)))
4581     (when thread
4582       (unless iheader
4583         (setcar thread nil)
4584         (when parent
4585           (delq thread parent)))
4586       (if (gnus-summary-insert-subject id header)
4587           ;; Set the (possibly) new article number in the data structure.
4588           (gnus-data-set-number data (gnus-id-to-article id))
4589         (setcar thread old)
4590         nil))))
4591
4592 (defun gnus-rebuild-thread (id &optional line)
4593   "Rebuild the thread containing ID.
4594 If LINE, insert the rebuilt thread starting on line LINE."
4595   (let ((inhibit-read-only t)
4596         old-pos current thread data)
4597     (if (not gnus-show-threads)
4598         (setq thread (list (car (gnus-id-to-thread id))))
4599       ;; Get the thread this article is part of.
4600       (setq thread (gnus-remove-thread id)))
4601     (setq old-pos (point-at-bol))
4602     (setq current (save-excursion
4603                     (and (re-search-backward "[\r\n]" nil t)
4604                          (gnus-summary-article-number))))
4605     ;; If this is a gathered thread, we have to go some re-gathering.
4606     (when (stringp (car thread))
4607       (let ((subject (car thread))
4608             roots thr)
4609         (setq thread (cdr thread))
4610         (while thread
4611           (unless (memq (setq thr (gnus-id-to-thread
4612                                    (gnus-root-id
4613                                     (mail-header-id (caar thread)))))
4614                         roots)
4615             (push thr roots))
4616           (setq thread (cdr thread)))
4617         ;; We now have all (unique) roots.
4618         (if (= (length roots) 1)
4619             ;; All the loose roots are now one solid root.
4620             (setq thread (car roots))
4621           (setq thread (cons subject (gnus-sort-threads roots))))))
4622     (let (threads)
4623       ;; We then insert this thread into the summary buffer.
4624       (when line
4625         (goto-char (point-min))
4626         (forward-line (1- line)))
4627       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4628         (if gnus-show-threads
4629             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4630           (gnus-summary-prepare-unthreaded thread))
4631         (setq data (nreverse gnus-newsgroup-data))
4632         (setq threads gnus-newsgroup-threads))
4633       ;; We splice the new data into the data structure.
4634       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4635       ;;!!! then we want to insert at the beginning of the buffer.
4636       ;;!!! That happens to be true with Gnus now, but that may
4637       ;;!!! change in the future.  Perhaps.
4638       (gnus-data-enter-list
4639        (if line nil current) data (- (point) old-pos))
4640       (setq gnus-newsgroup-threads
4641             (nconc threads gnus-newsgroup-threads))
4642       (gnus-data-compute-positions))))
4643
4644 (defun gnus-number-to-header (number)
4645   "Return the header for article NUMBER."
4646   (let ((headers gnus-newsgroup-headers))
4647     (while (and headers
4648                 (not (= number (mail-header-number (car headers)))))
4649       (pop headers))
4650     (when headers
4651       (car headers))))
4652
4653 (defun gnus-parent-headers (in-headers &optional generation)
4654   "Return the headers of the GENERATIONeth parent of HEADERS."
4655   (unless generation
4656     (setq generation 1))
4657   (let ((parent t)
4658         (headers in-headers)
4659         references)
4660     (while (and parent
4661                 (not (zerop generation))
4662                 (setq references (mail-header-references headers)))
4663       (setq headers (if (and references
4664                              (setq parent (gnus-parent-id references)))
4665                         (car (gnus-id-to-thread parent))
4666                       nil))
4667       (decf generation))
4668     (and (not (eq headers in-headers))
4669          headers)))
4670
4671 (defun gnus-id-to-thread (id)
4672   "Return the (sub-)thread where ID appears."
4673   (gnus-gethash id gnus-newsgroup-dependencies))
4674
4675 (defun gnus-id-to-article (id)
4676   "Return the article number of ID."
4677   (let ((thread (gnus-id-to-thread id)))
4678     (when (and thread
4679                (car thread))
4680       (mail-header-number (car thread)))))
4681
4682 (defun gnus-id-to-header (id)
4683   "Return the article headers of ID."
4684   (car (gnus-id-to-thread id)))
4685
4686 (defun gnus-article-displayed-root-p (article)
4687   "Say whether ARTICLE is a root(ish) article."
4688   (let ((level (gnus-summary-thread-level article))
4689         (refs (mail-header-references  (gnus-summary-article-header article)))
4690         particle)
4691     (cond
4692      ((null level) nil)
4693      ((zerop level) t)
4694      ((null refs) t)
4695      ((null (gnus-parent-id refs)) t)
4696      ((and (= 1 level)
4697            (null (setq particle (gnus-id-to-article
4698                                  (gnus-parent-id refs))))
4699            (null (gnus-summary-thread-level particle)))))))
4700
4701 (defun gnus-root-id (id)
4702   "Return the id of the root of the thread where ID appears."
4703   (let (last-id prev)
4704     (while (and id (setq prev (car (gnus-id-to-thread id))))
4705       (setq last-id id
4706             id (gnus-parent-id (mail-header-references prev))))
4707     last-id))
4708
4709 (defun gnus-articles-in-thread (thread)
4710   "Return the list of articles in THREAD."
4711   (cons (mail-header-number (car thread))
4712         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4713
4714 (defun gnus-remove-thread (id &optional dont-remove)
4715   "Remove the thread that has ID in it."
4716   (let (headers thread last-id)
4717     ;; First go up in this thread until we find the root.
4718     (setq last-id (gnus-root-id id)
4719           headers (message-flatten-list (gnus-id-to-thread last-id)))
4720     ;; We have now found the real root of this thread.  It might have
4721     ;; been gathered into some loose thread, so we have to search
4722     ;; through the threads to find the thread we wanted.
4723     (let ((threads gnus-newsgroup-threads)
4724           sub)
4725       (while threads
4726         (setq sub (car threads))
4727         (if (stringp (car sub))
4728             ;; This is a gathered thread, so we look at the roots
4729             ;; below it to find whether this article is in this
4730             ;; gathered root.
4731             (progn
4732               (setq sub (cdr sub))
4733               (while sub
4734                 (when (member (caar sub) headers)
4735                   (setq thread (car threads)
4736                         threads nil
4737                         sub nil))
4738                 (setq sub (cdr sub))))
4739           ;; It's an ordinary thread, so we check it.
4740           (when (eq (car sub) (car headers))
4741             (setq thread sub
4742                   threads nil)))
4743         (setq threads (cdr threads)))
4744       ;; If this article is in no thread, then it's a root.
4745       (if thread
4746           (unless dont-remove
4747             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4748         (setq thread (gnus-id-to-thread last-id)))
4749       (when thread
4750         (prog1
4751             thread                      ; We return this thread.
4752           (unless dont-remove
4753             (if (stringp (car thread))
4754                 (progn
4755                   ;; If we use dummy roots, then we have to remove the
4756                   ;; dummy root as well.
4757                   (when (eq gnus-summary-make-false-root 'dummy)
4758                     ;; We go to the dummy root by going to
4759                     ;; the first sub-"thread", and then one line up.
4760                     (gnus-summary-goto-article
4761                      (mail-header-number (caadr thread)))
4762                     (forward-line -1)
4763                     (gnus-delete-line)
4764                     (gnus-data-compute-positions))
4765                   (setq thread (cdr thread))
4766                   (while thread
4767                     (gnus-remove-thread-1 (car thread))
4768                     (setq thread (cdr thread))))
4769               (gnus-remove-thread-1 thread))))))))
4770
4771 (defun gnus-remove-thread-1 (thread)
4772   "Remove the thread THREAD recursively."
4773   (let ((number (mail-header-number (pop thread)))
4774         d)
4775     (setq thread (reverse thread))
4776     (while thread
4777       (gnus-remove-thread-1 (pop thread)))
4778     (when (setq d (gnus-data-find number))
4779       (goto-char (gnus-data-pos d))
4780       (gnus-summary-show-thread)
4781       (gnus-data-remove
4782        number
4783        (- (point-at-bol)
4784           (prog1
4785               (1+ (point-at-eol))
4786             (gnus-delete-line)))))))
4787
4788 (defun gnus-sort-threads-recursive (threads func)
4789   (sort (mapcar (lambda (thread)
4790                   (cons (car thread)
4791                         (and (cdr thread)
4792                              (gnus-sort-threads-recursive (cdr thread) func))))
4793                 threads) func))
4794
4795 (defun gnus-sort-threads-loop (threads func)
4796   (let* ((superthread (cons nil threads))
4797          (stack (list (cons superthread threads)))
4798          remaining-threads thread)
4799     (while stack
4800       (setq remaining-threads (cdr (car stack)))
4801       (if remaining-threads
4802           (progn (setq thread (car remaining-threads))
4803                  (setcdr (car stack) (cdr remaining-threads))
4804                  (if (cdr thread)
4805                      (push (cons thread (cdr thread)) stack)))
4806         (setq thread (caar stack))
4807         (setcdr thread (sort (cdr thread) func))
4808         (pop stack)))
4809     (cdr superthread)))
4810
4811 (defun gnus-sort-threads (threads)
4812   "Sort THREADS."
4813   (if (not gnus-thread-sort-functions)
4814       threads
4815     (gnus-message 8 "Sorting threads...")
4816     (prog1
4817         (condition-case nil
4818             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4819               (gnus-sort-threads-recursive
4820                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4821           ;; Even after binding max-lisp-eval-depth, the recursive
4822           ;; sorter might fail for very long threads.  In that case,
4823           ;; try using a (less well-tested) non-recursive sorter.
4824           (error (gnus-message 9 "Sorting threads with loop...")
4825                  (gnus-sort-threads-loop
4826                   threads (gnus-make-sort-function
4827                            gnus-thread-sort-functions))))
4828       (gnus-message 8 "Sorting threads...done"))))
4829
4830 (defun gnus-sort-articles (articles)
4831   "Sort ARTICLES."
4832   (when gnus-article-sort-functions
4833     (gnus-message 7 "Sorting articles...")
4834     (prog1
4835         (setq gnus-newsgroup-headers
4836               (sort articles (gnus-make-sort-function
4837                               gnus-article-sort-functions)))
4838       (gnus-message 7 "Sorting articles...done"))))
4839
4840 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4841 (defmacro gnus-thread-header (thread)
4842   "Return header of first article in THREAD.
4843 Note that THREAD must never, ever be anything else than a variable -
4844 using some other form will lead to serious barfage."
4845   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4846   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4847   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4848         (vector thread) 2))
4849
4850 (defsubst gnus-article-sort-by-number (h1 h2)
4851   "Sort articles by article number."
4852   (< (mail-header-number h1)
4853      (mail-header-number h2)))
4854
4855 (defun gnus-thread-sort-by-number (h1 h2)
4856   "Sort threads by root article number."
4857   (gnus-article-sort-by-number
4858    (gnus-thread-header h1) (gnus-thread-header h2)))
4859
4860 (defsubst gnus-article-sort-by-random (h1 h2)
4861   "Sort articles randomly."
4862   (zerop (random 2)))
4863
4864 (defun gnus-thread-sort-by-random (h1 h2)
4865   "Sort threads randomly."
4866   (gnus-article-sort-by-random
4867    (gnus-thread-header h1) (gnus-thread-header h2)))
4868
4869 (defsubst gnus-article-sort-by-lines (h1 h2)
4870   "Sort articles by article Lines header."
4871   (< (mail-header-lines h1)
4872      (mail-header-lines h2)))
4873
4874 (defun gnus-thread-sort-by-lines (h1 h2)
4875   "Sort threads by root article Lines header."
4876   (gnus-article-sort-by-lines
4877    (gnus-thread-header h1) (gnus-thread-header h2)))
4878
4879 (defsubst gnus-article-sort-by-chars (h1 h2)
4880   "Sort articles by octet length."
4881   (< (mail-header-chars h1)
4882      (mail-header-chars h2)))
4883
4884 (defun gnus-thread-sort-by-chars (h1 h2)
4885   "Sort threads by root article octet length."
4886   (gnus-article-sort-by-chars
4887    (gnus-thread-header h1) (gnus-thread-header h2)))
4888
4889 (defsubst gnus-article-sort-by-author (h1 h2)
4890   "Sort articles by root author."
4891   (gnus-string<
4892    (let ((extract (funcall
4893                    gnus-extract-address-components
4894                    (mail-header-from h1))))
4895      (or (car extract) (cadr extract) ""))
4896    (let ((extract (funcall
4897                    gnus-extract-address-components
4898                    (mail-header-from h2))))
4899      (or (car extract) (cadr extract) ""))))
4900
4901 (defun gnus-thread-sort-by-author (h1 h2)
4902   "Sort threads by root author."
4903   (gnus-article-sort-by-author
4904    (gnus-thread-header h1)  (gnus-thread-header h2)))
4905
4906 (defsubst gnus-article-sort-by-recipient (h1 h2)
4907   "Sort articles by recipient."
4908   (gnus-string<
4909    (let ((extract (funcall
4910                    gnus-extract-address-components
4911                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4912      (or (car extract) (cadr extract)))
4913    (let ((extract (funcall
4914                    gnus-extract-address-components
4915                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4916      (or (car extract) (cadr extract)))))
4917
4918 (defun gnus-thread-sort-by-recipient (h1 h2)
4919   "Sort threads by root recipient."
4920   (gnus-article-sort-by-recipient
4921    (gnus-thread-header h1) (gnus-thread-header h2)))
4922
4923 (defsubst gnus-article-sort-by-subject (h1 h2)
4924   "Sort articles by root subject."
4925   (gnus-string<
4926    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4927    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4928
4929 (defun gnus-thread-sort-by-subject (h1 h2)
4930   "Sort threads by root subject."
4931   (gnus-article-sort-by-subject
4932    (gnus-thread-header h1) (gnus-thread-header h2)))
4933
4934 (defsubst gnus-article-sort-by-date (h1 h2)
4935   "Sort articles by root article date."
4936   (time-less-p
4937    (gnus-date-get-time (mail-header-date h1))
4938    (gnus-date-get-time (mail-header-date h2))))
4939
4940 (defun gnus-thread-sort-by-date (h1 h2)
4941   "Sort threads by root article date."
4942   (gnus-article-sort-by-date
4943    (gnus-thread-header h1) (gnus-thread-header h2)))
4944
4945 (defsubst gnus-article-sort-by-score (h1 h2)
4946   "Sort articles by root article score.
4947 Unscored articles will be counted as having a score of zero."
4948   (> (or (cdr (assq (mail-header-number h1)
4949                     gnus-newsgroup-scored))
4950          gnus-summary-default-score 0)
4951      (or (cdr (assq (mail-header-number h2)
4952                     gnus-newsgroup-scored))
4953          gnus-summary-default-score 0)))
4954
4955 (defun gnus-thread-sort-by-score (h1 h2)
4956   "Sort threads by root article score."
4957   (gnus-article-sort-by-score
4958    (gnus-thread-header h1) (gnus-thread-header h2)))
4959
4960 (defun gnus-thread-sort-by-total-score (h1 h2)
4961   "Sort threads by the sum of all scores in the thread.
4962 Unscored articles will be counted as having a score of zero."
4963   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4964
4965 (defun gnus-thread-total-score (thread)
4966   ;; This function find the total score of THREAD.
4967   (cond
4968    ((null thread)
4969     0)
4970    ((consp thread)
4971     (if (stringp (car thread))
4972         (apply gnus-thread-score-function 0
4973                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4974       (gnus-thread-total-score-1 thread)))
4975    (t
4976     (gnus-thread-total-score-1 (list thread)))))
4977
4978 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4979   "Sort threads such that the thread with the most recently arrived article comes first."
4980   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4981
4982 (defun gnus-thread-highest-number (thread)
4983   "Return the highest article number in THREAD."
4984   (apply 'max (mapcar (lambda (header)
4985                         (mail-header-number header))
4986                       (message-flatten-list thread))))
4987
4988 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4989   "Sort threads such that the thread with the most recently dated article comes first."
4990   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4991
4992 ; Since this is called not only to sort the top-level threads, but
4993 ; also in recursive sorts to order the articles within a thread, each
4994 ; article will be processed many times.  Thus it speeds things up
4995 ; quite a bit to use gnus-date-get-time, which caches the time value.
4996 (defun gnus-thread-latest-date (thread)
4997   "Return the highest article date in THREAD."
4998   (apply 'max
4999          (mapcar (lambda (header) (gnus-float-time
5000                                    (gnus-date-get-time
5001                                     (mail-header-date header))))
5002                  (message-flatten-list thread))))
5003
5004 (defun gnus-thread-total-score-1 (root)
5005   ;; This function find the total score of the thread below ROOT.
5006   (setq root (car root))
5007   (apply gnus-thread-score-function
5008          (or (append
5009               (mapcar 'gnus-thread-total-score
5010                       (cdr (gnus-id-to-thread (mail-header-id root))))
5011               (when (> (mail-header-number root) 0)
5012                 (list (or (cdr (assq (mail-header-number root)
5013                                      gnus-newsgroup-scored))
5014                           gnus-summary-default-score 0))))
5015              (list gnus-summary-default-score)
5016              '(0))))
5017
5018 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5019 (defvar gnus-tmp-prev-subject nil)
5020 (defvar gnus-tmp-false-parent nil)
5021 (defvar gnus-tmp-root-expunged nil)
5022 (defvar gnus-tmp-dummy-line nil)
5023
5024 (defun gnus-extra-header (type &optional header)
5025   "Return the extra header of TYPE."
5026   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5027       ""))
5028
5029 (defvar gnus-tmp-thread-tree-header-string "")
5030
5031 (defcustom gnus-sum-thread-tree-root "> "
5032   "With %B spec, used for the root of a thread.
5033 If nil, use subject instead."
5034   :version "22.1"
5035   :type '(radio (const :format "%v  " nil) string)
5036   :group 'gnus-thread)
5037
5038 (defcustom gnus-sum-thread-tree-false-root "> "
5039   "With %B spec, used for a false root of a thread.
5040 If nil, use subject instead."
5041   :version "22.1"
5042   :type '(radio (const :format "%v  " nil) string)
5043   :group 'gnus-thread)
5044
5045 (defcustom gnus-sum-thread-tree-single-indent ""
5046   "With %B spec, used for a thread with just one message.
5047 If nil, use subject instead."
5048   :version "22.1"
5049   :type '(radio (const :format "%v  " nil) string)
5050   :group 'gnus-thread)
5051
5052 (defcustom gnus-sum-thread-tree-vertical "| "
5053   "With %B spec, used for drawing a vertical line."
5054   :version "22.1"
5055   :type 'string
5056   :group 'gnus-thread)
5057
5058 (defcustom gnus-sum-thread-tree-indent "  "
5059   "With %B spec, used for indenting."
5060   :version "22.1"
5061   :type 'string
5062   :group 'gnus-thread)
5063
5064 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5065   "With %B spec, used for a leaf with brothers."
5066   :version "22.1"
5067   :type 'string
5068   :group 'gnus-thread)
5069
5070 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5071   "With %B spec, used for a leaf without brothers."
5072   :version "22.1"
5073   :type 'string
5074   :group 'gnus-thread)
5075
5076 (defcustom gnus-summary-display-while-building nil
5077   "If non-nil, show and update the summary buffer as it's being built.
5078 If the value is t, update the buffer after every line is inserted.  If
5079 the value is an integer (N), update the display every N lines."
5080   :version "22.1"
5081   :group 'gnus-thread
5082   :type '(choice (const :tag "off" nil)
5083                  number
5084                  (const :tag "frequently" t)))
5085
5086 (defun gnus-summary-prepare-threads (threads)
5087   "Prepare summary buffer from THREADS and indentation LEVEL.
5088 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5089 or a straight list of headers."
5090   (gnus-message 7 "Generating summary...")
5091
5092   (setq gnus-newsgroup-threads threads)
5093   (beginning-of-line)
5094
5095   (let ((gnus-tmp-level 0)
5096         (default-score (or gnus-summary-default-score 0))
5097         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5098         (building-line-count gnus-summary-display-while-building)
5099         (building-count (integerp gnus-summary-display-while-building))
5100         thread number subject stack state gnus-tmp-gathered beg-match
5101         new-roots gnus-tmp-new-adopts thread-end simp-subject
5102         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5103         gnus-tmp-replied gnus-tmp-subject-or-nil
5104         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5105         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5106         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5107         tree-stack)
5108
5109     (setq gnus-tmp-prev-subject nil
5110           gnus-tmp-thread-tree-header-string "")
5111
5112     (if (vectorp (car threads))
5113         ;; If this is a straight (sic) list of headers, then a
5114         ;; threaded summary display isn't required, so we just create
5115         ;; an unthreaded one.
5116         (gnus-summary-prepare-unthreaded threads)
5117
5118       ;; Do the threaded display.
5119
5120       (if gnus-summary-display-while-building
5121           (switch-to-buffer (buffer-name)))
5122       (while (or threads stack gnus-tmp-new-adopts new-roots)
5123
5124         (if (and (= gnus-tmp-level 0)
5125                  (or (not stack)
5126                      (= (caar stack) 0))
5127                  (not gnus-tmp-false-parent)
5128                  (or gnus-tmp-new-adopts new-roots))
5129             (if gnus-tmp-new-adopts
5130                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5131                       thread (list (car gnus-tmp-new-adopts))
5132                       gnus-tmp-header (caar thread)
5133                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5134               (when new-roots
5135                 (setq thread (list (car new-roots))
5136                       gnus-tmp-header (caar thread)
5137                       new-roots (cdr new-roots))))
5138
5139           (if threads
5140               ;; If there are some threads, we do them before the
5141               ;; threads on the stack.
5142               (setq thread threads
5143                     gnus-tmp-header (caar thread))
5144             ;; There were no current threads, so we pop something off
5145             ;; the stack.
5146             (setq state (car stack)
5147                   gnus-tmp-level (car state)
5148                   tree-stack (cadr state)
5149                   thread (caddr state)
5150                   stack (cdr stack)
5151                   gnus-tmp-header (caar thread))))
5152
5153         (setq gnus-tmp-false-parent nil)
5154         (setq gnus-tmp-root-expunged nil)
5155         (setq thread-end nil)
5156
5157         (if (stringp gnus-tmp-header)
5158             ;; The header is a dummy root.
5159             (cond
5160              ((eq gnus-summary-make-false-root 'adopt)
5161               ;; We let the first article adopt the rest.
5162               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5163                                                (cddar thread)))
5164               (setq gnus-tmp-gathered
5165                     (nconc (mapcar
5166                             (lambda (h) (mail-header-number (car h)))
5167                             (cddar thread))
5168                            gnus-tmp-gathered))
5169               (setq thread (cons (list (caar thread)
5170                                        (cadar thread))
5171                                  (cdr thread)))
5172               (setq gnus-tmp-level -1
5173                     gnus-tmp-false-parent t))
5174              ((eq gnus-summary-make-false-root 'empty)
5175               ;; We print adopted articles with empty subject fields.
5176               (setq gnus-tmp-gathered
5177                     (nconc (mapcar
5178                             (lambda (h) (mail-header-number (car h)))
5179                             (cddar thread))
5180                            gnus-tmp-gathered))
5181               (setq gnus-tmp-level -1))
5182              ((eq gnus-summary-make-false-root 'dummy)
5183               ;; We remember that we probably want to output a dummy
5184               ;; root.
5185               (setq gnus-tmp-dummy-line gnus-tmp-header)
5186               (setq gnus-tmp-prev-subject gnus-tmp-header))
5187              (t
5188               ;; We do not make a root for the gathered
5189               ;; sub-threads at all.
5190               (setq gnus-tmp-level -1)))
5191
5192           (setq number (mail-header-number gnus-tmp-header)
5193                 subject (mail-header-subject gnus-tmp-header)
5194                 simp-subject (gnus-simplify-subject-fully subject))
5195
5196           (cond
5197            ;; If the thread has changed subject, we might want to make
5198            ;; this subthread into a root.
5199            ((and (null gnus-thread-ignore-subject)
5200                  (not (zerop gnus-tmp-level))
5201                  gnus-tmp-prev-subject
5202                  (not (string= gnus-tmp-prev-subject simp-subject)))
5203             (setq new-roots (nconc new-roots (list (car thread)))
5204                   thread-end t
5205                   gnus-tmp-header nil))
5206            ;; If the article lies outside the current limit,
5207            ;; then we do not display it.
5208            ((not (memq number gnus-newsgroup-limit))
5209             (setq gnus-tmp-gathered
5210                   (nconc (mapcar
5211                           (lambda (h) (mail-header-number (car h)))
5212                           (cdar thread))
5213                          gnus-tmp-gathered))
5214             (setq gnus-tmp-new-adopts (if (cdar thread)
5215                                           (append gnus-tmp-new-adopts
5216                                                   (cdar thread))
5217                                         gnus-tmp-new-adopts)
5218                   thread-end t
5219                   gnus-tmp-header nil)
5220             (when (zerop gnus-tmp-level)
5221               (setq gnus-tmp-root-expunged t)))
5222            ;; Perhaps this article is to be marked as read?
5223            ((and gnus-summary-mark-below
5224                  (< (or (cdr (assq number gnus-newsgroup-scored))
5225                         default-score)
5226                     gnus-summary-mark-below)
5227                  ;; Don't touch sparse articles.
5228                  (not (gnus-summary-article-sparse-p number))
5229                  (not (gnus-summary-article-ancient-p number)))
5230             (setq gnus-newsgroup-unreads
5231                   (delq number gnus-newsgroup-unreads))
5232             (if gnus-newsgroup-auto-expire
5233                 (setq gnus-newsgroup-expirable
5234                       (gnus-add-to-sorted-list
5235                        gnus-newsgroup-expirable number))
5236               (push (cons number gnus-low-score-mark)
5237                     gnus-newsgroup-reads))))
5238
5239           (when gnus-tmp-header
5240             ;; We may have an old dummy line to output before this
5241             ;; article.
5242             (when (and gnus-tmp-dummy-line
5243                        (gnus-subject-equal
5244                         gnus-tmp-dummy-line
5245                         (mail-header-subject gnus-tmp-header)))
5246               (gnus-summary-insert-dummy-line
5247                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5248               (setq gnus-tmp-dummy-line nil))
5249
5250             ;; Compute the mark.
5251             (setq gnus-tmp-unread (gnus-article-mark number))
5252
5253             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5254                                   gnus-tmp-header gnus-tmp-level)
5255                   gnus-newsgroup-data)
5256
5257             ;; Actually insert the line.
5258             (setq
5259              gnus-tmp-subject-or-nil
5260              (cond
5261               ((and gnus-thread-ignore-subject
5262                     gnus-tmp-prev-subject
5263                     (not (string= gnus-tmp-prev-subject simp-subject)))
5264                subject)
5265               ((zerop gnus-tmp-level)
5266                (if (and (eq gnus-summary-make-false-root 'empty)
5267                         (memq number gnus-tmp-gathered)
5268                         gnus-tmp-prev-subject
5269                         (string= gnus-tmp-prev-subject simp-subject))
5270                    gnus-summary-same-subject
5271                  subject))
5272               (t gnus-summary-same-subject)))
5273             (if (and (eq gnus-summary-make-false-root 'adopt)
5274                      (= gnus-tmp-level 1)
5275                      (memq number gnus-tmp-gathered))
5276                 (setq gnus-tmp-opening-bracket ?\<
5277                       gnus-tmp-closing-bracket ?\>)
5278               (setq gnus-tmp-opening-bracket ?\[
5279                     gnus-tmp-closing-bracket ?\]))
5280             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5281                 (gnus-make-thread-indent-array
5282                  (max (* 2 (length gnus-thread-indent-array))
5283                       gnus-tmp-level)))
5284             (setq
5285              gnus-tmp-indentation
5286              (aref gnus-thread-indent-array gnus-tmp-level)
5287              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5288              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5289                                 gnus-summary-default-score 0)
5290              gnus-tmp-score-char
5291              (if (or (null gnus-summary-default-score)
5292                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5293                          gnus-summary-zcore-fuzz))
5294                  ?                      ;Whitespace
5295                (if (< gnus-tmp-score gnus-summary-default-score)
5296                    gnus-score-below-mark gnus-score-over-mark))
5297              gnus-tmp-replied
5298              (cond ((memq number gnus-newsgroup-processable)
5299                     gnus-process-mark)
5300                    ((memq number gnus-newsgroup-cached)
5301                     gnus-cached-mark)
5302                    ((memq number gnus-newsgroup-replied)
5303                     gnus-replied-mark)
5304                    ((memq number gnus-newsgroup-forwarded)
5305                     gnus-forwarded-mark)
5306                    ((memq number gnus-newsgroup-saved)
5307                     gnus-saved-mark)
5308                    ((memq number gnus-newsgroup-recent)
5309                     gnus-recent-mark)
5310                    ((memq number gnus-newsgroup-unseen)
5311                     gnus-unseen-mark)
5312                    (t gnus-no-mark))
5313              gnus-tmp-downloaded
5314              (cond ((memq number gnus-newsgroup-undownloaded)
5315                     gnus-undownloaded-mark)
5316                    (gnus-newsgroup-agentized
5317                     gnus-downloaded-mark)
5318                    (t
5319                     gnus-no-mark))
5320              gnus-tmp-from (mail-header-from gnus-tmp-header)
5321              gnus-tmp-name
5322              (cond
5323               ((string-match "<[^>]+> *$" gnus-tmp-from)
5324                (setq beg-match (match-beginning 0))
5325                (or (and (string-match "^\".+\"" gnus-tmp-from)
5326                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5327                    (substring gnus-tmp-from 0 beg-match)))
5328               ((string-match "(.+)" gnus-tmp-from)
5329                (substring gnus-tmp-from
5330                           (1+ (match-beginning 0)) (1- (match-end 0))))
5331               (t gnus-tmp-from))
5332
5333              ;; Do the %B string
5334              gnus-tmp-thread-tree-header-string
5335              (cond
5336               ((not gnus-show-threads) "")
5337               ((zerop gnus-tmp-level)
5338                (cond ((cdar thread)
5339                       (or gnus-sum-thread-tree-root subject))
5340                      (gnus-tmp-new-adopts
5341                       (or gnus-sum-thread-tree-false-root subject))
5342                      (t
5343                       (or gnus-sum-thread-tree-single-indent subject))))
5344               (t
5345                (concat (apply 'concat
5346                               (mapcar (lambda (item)
5347                                         (if (= item 1)
5348                                             gnus-sum-thread-tree-vertical
5349                                           gnus-sum-thread-tree-indent))
5350                                       (cdr (reverse tree-stack))))
5351                        (if (nth 1 thread)
5352                            gnus-sum-thread-tree-leaf-with-other
5353                          gnus-sum-thread-tree-single-leaf)))))
5354             (when (string= gnus-tmp-name "")
5355               (setq gnus-tmp-name gnus-tmp-from))
5356             (unless (numberp gnus-tmp-lines)
5357               (setq gnus-tmp-lines -1))
5358             (if (= gnus-tmp-lines -1)
5359                 (setq gnus-tmp-lines "?")
5360               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5361               (gnus-put-text-property
5362                (point)
5363                (progn (eval gnus-summary-line-format-spec) (point))
5364                'gnus-number number)
5365               (when gnus-visual-p
5366                 (forward-line -1)
5367                 (gnus-summary-highlight-line)
5368                 (when gnus-summary-update-hook
5369                   (gnus-run-hooks 'gnus-summary-update-hook))
5370                 (forward-line 1))
5371
5372               (setq gnus-tmp-prev-subject simp-subject)))
5373
5374         (when (nth 1 thread)
5375           (push (list (max 0 gnus-tmp-level)
5376                       (copy-sequence tree-stack)
5377                       (nthcdr 1 thread))
5378                 stack))
5379         (push (if (nth 1 thread) 1 0) tree-stack)
5380         (incf gnus-tmp-level)
5381         (setq threads (if thread-end nil (cdar thread)))
5382         (if gnus-summary-display-while-building
5383             (if building-count
5384                 (progn
5385                   ;; use a set frequency
5386                   (setq building-line-count (1- building-line-count))
5387                   (when (= building-line-count 0)
5388                     (sit-for 0)
5389                     (setq building-line-count
5390                           gnus-summary-display-while-building)))
5391               ;; always
5392               (sit-for 0)))
5393         (unless threads
5394           (setq gnus-tmp-level 0)))))
5395   (gnus-message 7 "Generating summary...done"))
5396
5397 (defun gnus-summary-prepare-unthreaded (headers)
5398   "Generate an unthreaded summary buffer based on HEADERS."
5399   (let (header number mark)
5400
5401     (beginning-of-line)
5402
5403     (while headers
5404       ;; We may have to root out some bad articles...
5405       (when (memq (setq number (mail-header-number
5406                                 (setq header (pop headers))))
5407                   gnus-newsgroup-limit)
5408         ;; Mark article as read when it has a low score.
5409         (when (and gnus-summary-mark-below
5410                    (< (or (cdr (assq number gnus-newsgroup-scored))
5411                           gnus-summary-default-score 0)
5412                       gnus-summary-mark-below)
5413                    (not (gnus-summary-article-ancient-p number)))
5414           (setq gnus-newsgroup-unreads
5415                 (delq number gnus-newsgroup-unreads))
5416           (if gnus-newsgroup-auto-expire
5417               (push number gnus-newsgroup-expirable)
5418             (push (cons number gnus-low-score-mark)
5419                   gnus-newsgroup-reads)))
5420
5421         (setq mark (gnus-article-mark number))
5422         (push (gnus-data-make number mark (1+ (point)) header 0)
5423               gnus-newsgroup-data)
5424         (gnus-summary-insert-line
5425          header 0 number
5426          (memq number gnus-newsgroup-undownloaded)
5427          mark (memq number gnus-newsgroup-replied)
5428          (memq number gnus-newsgroup-expirable)
5429          (mail-header-subject header) nil
5430          (cdr (assq number gnus-newsgroup-scored))
5431          (memq number gnus-newsgroup-processable))))))
5432
5433 (defun gnus-summary-remove-list-identifiers ()
5434   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5435   (let ((regexp (if (consp gnus-list-identifiers)
5436                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5437                   gnus-list-identifiers))
5438         changed subject)
5439     (when regexp
5440       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5441       (dolist (header gnus-newsgroup-headers)
5442         (setq subject (mail-header-subject header)
5443               changed nil)
5444         (while (string-match regexp subject)
5445           (setq subject
5446                 (concat (substring subject 0 (match-beginning 1))
5447                         (substring subject (match-end 0)))
5448                 changed t))
5449         (when changed
5450           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5451             (setq subject
5452                   (concat (substring subject 0 (match-beginning 1))
5453                           (substring subject (match-end 1)))))
5454           (mail-header-set-subject header subject))))))
5455
5456 (defun gnus-fetch-headers (articles)
5457   "Fetch headers of ARTICLES."
5458   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5459     (gnus-message 5 "Fetching headers for %s..." name)
5460     (prog1
5461         (if (eq 'nov
5462                 (setq gnus-headers-retrieved-by
5463                       (gnus-retrieve-headers
5464                        articles gnus-newsgroup-name
5465                        ;; We might want to fetch old headers, but
5466                        ;; not if there is only 1 article.
5467                        (and (or (and
5468                                  (not (eq gnus-fetch-old-headers 'some))
5469                                  (not (numberp gnus-fetch-old-headers)))
5470                                 (> (length articles) 1))
5471                             gnus-fetch-old-headers))))
5472             (gnus-get-newsgroup-headers-xover
5473              articles nil nil gnus-newsgroup-name t)
5474           (gnus-get-newsgroup-headers))
5475       (gnus-message 5 "Fetching headers for %s...done" name))))
5476
5477 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5478   "Select newsgroup GROUP.
5479 If READ-ALL is non-nil, all articles in the group are selected.
5480 If SELECT-ARTICLES, only select those articles from GROUP."
5481   (let* ((entry (gnus-group-entry group))
5482          ;;!!! Dirty hack; should be removed.
5483          (gnus-summary-ignore-duplicates
5484           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5485               t
5486             gnus-summary-ignore-duplicates))
5487          (info (nth 2 entry))
5488          charset articles fetched-articles cached)
5489
5490     (unless (gnus-check-server
5491              (set (make-local-variable 'gnus-current-select-method)
5492                   (gnus-find-method-for-group group)))
5493       (error "Couldn't open server"))
5494     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5495
5496     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5497         (gnus-activate-group group)     ; Or we can activate it...
5498         (progn                          ; Or we bug out.
5499           (when (equal major-mode 'gnus-summary-mode)
5500             (gnus-kill-buffer (current-buffer)))
5501           (error
5502            "Couldn't activate group %s: %s"
5503            (mm-decode-coding-string group charset)
5504            (mm-decode-coding-string (gnus-status-message group) charset))))
5505
5506     (unless (gnus-request-group group t)
5507       (when (equal major-mode 'gnus-summary-mode)
5508         (gnus-kill-buffer (current-buffer)))
5509       (error "Couldn't request group %s: %s"
5510              (mm-decode-coding-string group charset)
5511              (mm-decode-coding-string (gnus-status-message group) charset)))
5512
5513     (when gnus-agent
5514       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5515
5516       (setq gnus-summary-use-undownloaded-faces
5517             (gnus-agent-find-parameter
5518              group
5519              'agent-enable-undownloaded-faces)))
5520
5521     (setq gnus-newsgroup-name group
5522           gnus-newsgroup-unselected nil
5523           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5524
5525     (let ((display (gnus-group-find-parameter group 'display)))
5526       (setq gnus-newsgroup-display
5527             (cond
5528              ((not (zerop (or (car-safe read-all) 0)))
5529               ;; The user entered the group with C-u SPC/RET, let's show
5530               ;; all articles.
5531               'gnus-not-ignore)
5532              ((eq display 'all)
5533               'gnus-not-ignore)
5534              ((arrayp display)
5535               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5536              ((numberp display)
5537               ;; The following is probably the "correct" solution, but
5538               ;; it makes Gnus fetch all headers and then limit the
5539               ;; articles (which is slow), so instead we hack the
5540               ;; select-articles parameter instead. -- Simon Josefsson
5541               ;; <jas@kth.se>
5542               ;;
5543               ;; (gnus-byte-compile
5544               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5545               ;;                         display)))))
5546               (setq select-articles
5547                     (gnus-uncompress-range
5548                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5549                              (if (> tmp 0)
5550                                  tmp
5551                                1))
5552                            (cdr (gnus-active group)))))
5553               nil)
5554              (t
5555               nil))))
5556
5557     (gnus-summary-setup-default-charset)
5558
5559     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5560     (when (gnus-virtual-group-p group)
5561       (setq cached gnus-newsgroup-cached))
5562
5563     (setq gnus-newsgroup-unreads
5564           (gnus-sorted-ndifference
5565            (gnus-sorted-ndifference gnus-newsgroup-unreads
5566                                     gnus-newsgroup-marked)
5567            gnus-newsgroup-dormant))
5568
5569     (setq gnus-newsgroup-processable nil)
5570
5571     (gnus-update-read-articles group gnus-newsgroup-unreads)
5572
5573     ;; Adjust and set lists of article marks.
5574     (when info
5575       (gnus-adjust-marked-articles info))
5576     (if (setq articles select-articles)
5577         (setq gnus-newsgroup-unselected
5578               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5579       (setq articles (gnus-articles-to-read group read-all)))
5580
5581     (cond
5582      ((null articles)
5583       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5584       'quit)
5585      ((eq articles 0) nil)
5586      (t
5587       ;; Init the dependencies hash table.
5588       (setq gnus-newsgroup-dependencies
5589             (gnus-make-hashtable (length articles)))
5590       (gnus-set-global-variables)
5591       ;; Retrieve the headers and read them in.
5592
5593       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5594
5595       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5596       (when cached
5597         (setq gnus-newsgroup-cached cached))
5598
5599       ;; Suppress duplicates?
5600       (when gnus-suppress-duplicates
5601         (gnus-dup-suppress-articles))
5602
5603       ;; Set the initial limit.
5604       (setq gnus-newsgroup-limit (copy-sequence articles))
5605       ;; Remove canceled articles from the list of unread articles.
5606       (setq fetched-articles
5607             (mapcar (lambda (headers) (mail-header-number headers))
5608                     gnus-newsgroup-headers))
5609       (setq gnus-newsgroup-articles fetched-articles)
5610       (setq gnus-newsgroup-unreads
5611             (gnus-sorted-nintersection
5612              gnus-newsgroup-unreads fetched-articles))
5613       (gnus-compute-unseen-list)
5614
5615       ;; Removed marked articles that do not exist.
5616       (gnus-update-missing-marks
5617        (gnus-sorted-difference articles fetched-articles))
5618       ;; We might want to build some more threads first.
5619       (when (and gnus-fetch-old-headers
5620                  (eq gnus-headers-retrieved-by 'nov))
5621         (if (eq gnus-fetch-old-headers 'invisible)
5622             (gnus-build-all-threads)
5623           (gnus-build-old-threads)))
5624       ;; Let the Gnus agent mark articles as read.
5625       (when gnus-agent
5626         (gnus-agent-get-undownloaded-list))
5627       ;; Remove list identifiers from subject
5628       (when gnus-list-identifiers
5629         (gnus-summary-remove-list-identifiers))
5630       ;; Check whether auto-expire is to be done in this group.
5631       (setq gnus-newsgroup-auto-expire
5632             (gnus-group-auto-expirable-p group))
5633       ;; Set up the article buffer now, if necessary.
5634       (unless (and gnus-single-article-buffer
5635                    (equal gnus-article-buffer "*Article*"))
5636         (gnus-article-setup-buffer))
5637       ;; First and last article in this newsgroup.
5638       (when gnus-newsgroup-headers
5639         (setq gnus-newsgroup-begin
5640               (mail-header-number (car gnus-newsgroup-headers))
5641               gnus-newsgroup-end
5642               (mail-header-number
5643                (gnus-last-element gnus-newsgroup-headers))))
5644       ;; GROUP is successfully selected.
5645       (or gnus-newsgroup-headers t)))))
5646
5647 (defun gnus-compute-unseen-list ()
5648   ;; The `seen' marks are treated specially.
5649   (if (not gnus-newsgroup-seen)
5650       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5651     (setq gnus-newsgroup-unseen
5652           (gnus-inverse-list-range-intersection
5653            gnus-newsgroup-articles gnus-newsgroup-seen))))
5654
5655 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5656
5657 (defun gnus-summary-display-make-predicate (display)
5658   (require 'gnus-agent)
5659   (when (= (length display) 1)
5660     (setq display (car display)))
5661   (unless gnus-summary-display-cache
5662     (dolist (elem (append '((unread . unread)
5663                             (read . read)
5664                             (unseen . unseen))
5665                           gnus-article-mark-lists))
5666       (push (cons (cdr elem)
5667                   (gnus-byte-compile
5668                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5669             gnus-summary-display-cache)))
5670   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5671         (gnus-category-predicate-cache gnus-summary-display-cache))
5672     (gnus-get-predicate display)))
5673
5674 ;; Uses the dynamically bound `number' variable.
5675 (defvar number)
5676 (defun gnus-article-marked-p (type &optional article)
5677   (let ((article (or article number)))
5678     (cond
5679      ((eq type 'tick)
5680       (memq article gnus-newsgroup-marked))
5681      ((eq type 'spam)
5682       (memq article gnus-newsgroup-spam-marked))
5683      ((eq type 'unsend)
5684       (memq article gnus-newsgroup-unsendable))
5685      ((eq type 'undownload)
5686       (memq article gnus-newsgroup-undownloaded))
5687      ((eq type 'download)
5688       (memq article gnus-newsgroup-downloadable))
5689      ((eq type 'unread)
5690       (memq article gnus-newsgroup-unreads))
5691      ((eq type 'read)
5692       (memq article gnus-newsgroup-reads))
5693      ((eq type 'dormant)
5694       (memq article gnus-newsgroup-dormant) )
5695      ((eq type 'expire)
5696       (memq article gnus-newsgroup-expirable))
5697      ((eq type 'reply)
5698       (memq article gnus-newsgroup-replied))
5699      ((eq type 'killed)
5700       (memq article gnus-newsgroup-killed))
5701      ((eq type 'bookmark)
5702       (assq article gnus-newsgroup-bookmarks))
5703      ((eq type 'score)
5704       (assq article gnus-newsgroup-scored))
5705      ((eq type 'save)
5706       (memq article gnus-newsgroup-saved))
5707      ((eq type 'cache)
5708       (memq article gnus-newsgroup-cached))
5709      ((eq type 'forward)
5710       (memq article gnus-newsgroup-forwarded))
5711      ((eq type 'seen)
5712       (not (memq article gnus-newsgroup-unseen)))
5713      ((eq type 'recent)
5714       (memq article gnus-newsgroup-recent))
5715      (t t))))
5716
5717 (defun gnus-articles-to-read (group &optional read-all)
5718   "Find out what articles the user wants to read."
5719   (let* ((articles
5720           ;; Select all articles if `read-all' is non-nil, or if there
5721           ;; are no unread articles.
5722           (if (or read-all
5723                   (and (zerop (length gnus-newsgroup-marked))
5724                        (zerop (length gnus-newsgroup-unreads)))
5725                   ;; Fetch all if the predicate is non-nil.
5726                   gnus-newsgroup-display)
5727               ;; We want to select the headers for all the articles in
5728               ;; the group, so we select either all the active
5729               ;; articles in the group, or (if that's nil), the
5730               ;; articles in the cache.
5731               (or
5732                (if gnus-newsgroup-maximum-articles
5733                    (let ((active (gnus-active group)))
5734                      (gnus-uncompress-range
5735                       (cons (max (car active)
5736                                  (- (cdr active)
5737                                     gnus-newsgroup-maximum-articles
5738                                     -1))
5739                             (cdr active))))
5740                  (gnus-uncompress-range (gnus-active group)))
5741                (gnus-cache-articles-in-group group))
5742             ;; Select only the "normal" subset of articles.
5743             (gnus-sorted-nunion
5744              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5745              gnus-newsgroup-unreads)))
5746          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5747          (scored (length scored-list))
5748          (number (length articles))
5749          (marked (+ (length gnus-newsgroup-marked)
5750                     (length gnus-newsgroup-dormant)))
5751          (select
5752           (cond
5753            ((numberp read-all)
5754             read-all)
5755            ((numberp gnus-newsgroup-display)
5756             gnus-newsgroup-display)
5757            (t
5758             (condition-case ()
5759                 (cond
5760                  ((and (or (<= scored marked) (= scored number))
5761                        (numberp gnus-large-newsgroup)
5762                        (> number gnus-large-newsgroup))
5763                   (let* ((cursor-in-echo-area nil)
5764                          (initial (gnus-parameter-large-newsgroup-initial
5765                                    gnus-newsgroup-name))
5766                          (input
5767                           (read-string
5768                            (format
5769                             "How many articles from %s (%s %d): "
5770                             (gnus-group-decoded-name gnus-newsgroup-name)
5771                             (if initial "max" "default")
5772                             number)
5773                            (if initial
5774                                (cons (number-to-string initial)
5775                                      0)))))
5776                     (if (string-match "^[ \t]*$" input) number input)))
5777                  ((and (> scored marked) (< scored number)
5778                        (> (- scored number) 20))
5779                   (let ((input
5780                          (read-string
5781                           (format "%s %s (%d scored, %d total): "
5782                                   "How many articles from"
5783                                   (gnus-group-decoded-name group)
5784                                   scored number))))
5785                     (if (string-match "^[ \t]*$" input)
5786                         number input)))
5787                  (t number))
5788               (quit
5789                (message "Quit getting the articles to read")
5790                nil))))))
5791     (setq select (if (stringp select) (string-to-number select) select))
5792     (if (or (null select) (zerop select))
5793         select
5794       (if (and (not (zerop scored)) (<= (abs select) scored))
5795           (progn
5796             (setq articles (sort scored-list '<))
5797             (setq number (length articles)))
5798         (setq articles (copy-sequence articles)))
5799
5800       (when (< (abs select) number)
5801         (if (< select 0)
5802             ;; Select the N oldest articles.
5803             (setcdr (nthcdr (1- (abs select)) articles) nil)
5804           ;; Select the N most recent articles.
5805           (setq articles (nthcdr (- number select) articles))))
5806       (setq gnus-newsgroup-unselected
5807             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5808       (when gnus-alter-articles-to-read-function
5809         (setq articles
5810               (sort
5811                (funcall gnus-alter-articles-to-read-function
5812                         gnus-newsgroup-name articles)
5813                '<)))
5814       articles)))
5815
5816 (defun gnus-killed-articles (killed articles)
5817   (let (out)
5818     (while articles
5819       (when (inline (gnus-member-of-range (car articles) killed))
5820         (push (car articles) out))
5821       (setq articles (cdr articles)))
5822     out))
5823
5824 (defun gnus-uncompress-marks (marks)
5825   "Uncompress the mark ranges in MARKS."
5826   (let ((uncompressed '(score bookmark))
5827         out)
5828     (while marks
5829       (if (memq (caar marks) uncompressed)
5830           (push (car marks) out)
5831         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5832       (setq marks (cdr marks)))
5833     out))
5834
5835 (defun gnus-article-mark-to-type (mark)
5836   "Return the type of MARK."
5837   (or (cadr (assq mark gnus-article-special-mark-lists))
5838       'list))
5839
5840 (defun gnus-article-unpropagatable-p (mark)
5841   "Return whether MARK should be propagated to back end."
5842   (memq mark gnus-article-unpropagated-mark-lists))
5843
5844 (defun gnus-adjust-marked-articles (info)
5845   "Set all article lists and remove all marks that are no longer valid."
5846   (let* ((marked-lists (gnus-info-marks info))
5847          (active (gnus-active (gnus-info-group info)))
5848          (min (car active))
5849          (max (cdr active))
5850          (types gnus-article-mark-lists)
5851          marks var articles article mark mark-type
5852          bgn end)
5853     ;; Hack to avoid adjusting marks for imap.
5854     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5855               'nnimap)
5856       (setq min 1))
5857
5858     (dolist (marks marked-lists)
5859       (setq mark (car marks)
5860             mark-type (gnus-article-mark-to-type mark)
5861             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5862
5863       ;; We set the variable according to the type of the marks list,
5864       ;; and then adjust the marks to a subset of the active articles.
5865       (cond
5866        ;; Adjust "simple" lists - compressed yet unsorted
5867        ((eq mark-type 'list)
5868         ;; Simultaneously uncompress and clip to active range
5869         ;; See gnus-uncompress-range for a description of possible marks
5870         (let (l lh)
5871           (if (not (cadr marks))
5872               (set var nil)
5873             (setq articles (if (numberp (cddr marks))
5874                                (list (cdr marks))
5875                              (cdr marks))
5876                   lh (cons nil nil)
5877                   l lh)
5878
5879             (while (setq article (pop articles))
5880               (cond ((consp article)
5881                      (setq bgn (max (car article) min)
5882                            end (min (cdr article) max))
5883                      (while (<= bgn end)
5884                        (setq l (setcdr l (cons bgn nil))
5885                              bgn (1+ bgn))))
5886                     ((and (<= min article)
5887                           (>= max article))
5888                      (setq l (setcdr l (cons article nil))))))
5889             (set var (cdr lh)))))
5890        ;; Adjust assocs.
5891        ((eq mark-type 'tuple)
5892         (set var (setq articles (cdr marks)))
5893         (when (not (listp (cdr (symbol-value var))))
5894           (set var (list (symbol-value var))))
5895         (when (not (listp (cdr articles)))
5896           (setq articles (list articles)))
5897         (while articles
5898           (when (or (not (consp (setq article (pop articles))))
5899                     (< (car article) min)
5900                     (> (car article) max))
5901             (set var (delq article (symbol-value var))))))
5902        ;; Adjust ranges (sloppily).
5903        ((eq mark-type 'range)
5904         (cond
5905          ((eq mark 'seen)
5906           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5907           ;; It should be (seen (NUM1 . NUM2)).
5908           (when (numberp (cddr marks))
5909             (setcdr marks (list (cdr marks))))
5910           (setq articles (cdr marks))
5911           (while (and articles
5912                       (or (and (consp (car articles))
5913                                (> min (cdar articles)))
5914                           (and (numberp (car articles))
5915                                (> min (car articles)))))
5916             (pop articles))
5917           (set var articles))))))))
5918
5919 (defun gnus-update-missing-marks (missing)
5920   "Go through the list of MISSING articles and remove them from the mark lists."
5921   (when missing
5922     (let (var m)
5923       ;; Go through all types.
5924       (dolist (elem gnus-article-mark-lists)
5925         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5926           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5927           (when (symbol-value var)
5928             ;; This list has articles.  So we delete all missing
5929             ;; articles from it.
5930             (setq m missing)
5931             (while m
5932               (set var (delq (pop m) (symbol-value var))))))))))
5933
5934 (defun gnus-update-marks ()
5935   "Enter the various lists of marked articles into the newsgroup info list."
5936   (let ((types gnus-article-mark-lists)
5937         (info (gnus-get-info gnus-newsgroup-name))
5938         type list newmarked symbol delta-marks)
5939     (when info
5940       ;; Add all marks lists to the list of marks lists.
5941       (while (setq type (pop types))
5942         (setq list (symbol-value
5943                     (setq symbol
5944                           (intern (format "gnus-newsgroup-%s" (car type))))))
5945
5946         (when list
5947           ;; Get rid of the entries of the articles that have the
5948           ;; default score.
5949           (when (and (eq (cdr type) 'score)
5950                      gnus-save-score
5951                      list)
5952             (let* ((arts list)
5953                    (prev (cons nil list))
5954                    (all prev))
5955               (while arts
5956                 (if (or (not (consp (car arts)))
5957                         (= (cdar arts) gnus-summary-default-score))
5958                     (setcdr prev (cdr arts))
5959                   (setq prev arts))
5960                 (setq arts (cdr arts)))
5961               (setq list (cdr all)))))
5962
5963         (when (eq (cdr type) 'seen)
5964           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5965
5966         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5967           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5968
5969         (when (and (gnus-check-backend-function
5970                     'request-set-mark gnus-newsgroup-name)
5971                    (not (gnus-article-unpropagatable-p (cdr type))))
5972           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5973                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5974                  (add (gnus-remove-from-range
5975                        (gnus-copy-sequence list) old)))
5976             (when add
5977               (push (list add 'add (list (cdr type))) delta-marks))
5978             (when del
5979               ;; Don't delete marks from outside the active range.  This
5980               ;; shouldn't happen, but is a sanity check.
5981               (setq del (gnus-sorted-range-intersection
5982                          (gnus-active gnus-newsgroup-name) del))
5983               (push (list del 'del (list (cdr type))) delta-marks))))
5984
5985         (when list
5986           (push (cons (cdr type) list) newmarked)))
5987
5988       (when delta-marks
5989         (unless (gnus-check-group gnus-newsgroup-name)
5990           (error "Can't open server for %s" gnus-newsgroup-name))
5991         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5992
5993       ;; Enter these new marks into the info of the group.
5994       (if (nthcdr 3 info)
5995           (setcar (nthcdr 3 info) newmarked)
5996         ;; Add the marks lists to the end of the info.
5997         (when newmarked
5998           (setcdr (nthcdr 2 info) (list newmarked))))
5999
6000       ;; Cut off the end of the info if there's nothing else there.
6001       (let ((i 5))
6002         (while (and (> i 2)
6003                     (not (nth i info)))
6004           (when (nthcdr (decf i) info)
6005             (setcdr (nthcdr i info) nil)))))))
6006
6007 (defun gnus-set-mode-line (where)
6008   "Set the mode line of the article or summary buffers.
6009 If WHERE is `summary', the summary mode line format will be used."
6010   ;; Is this mode line one we keep updated?
6011   (when (and (memq where gnus-updated-mode-lines)
6012              (symbol-value
6013               (intern (format "gnus-%s-mode-line-format-spec" where))))
6014     (let (mode-string)
6015       ;; We evaluate this in the summary buffer since these
6016       ;; variables are buffer-local to that buffer.
6017       (with-current-buffer gnus-summary-buffer
6018         ;; We bind all these variables that are used in the `eval' form
6019         ;; below.
6020         (let* ((mformat (symbol-value
6021                          (intern
6022                           (format "gnus-%s-mode-line-format-spec" where))))
6023                (gnus-tmp-group-name (gnus-mode-string-quote
6024                                      (gnus-group-decoded-name
6025                                       gnus-newsgroup-name)))
6026                (gnus-tmp-article-number (or gnus-current-article 0))
6027                (gnus-tmp-unread gnus-newsgroup-unreads)
6028                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6029                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6030                (gnus-tmp-unread-and-unselected
6031                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6032                             (zerop gnus-tmp-unselected))
6033                        "")
6034                       ((zerop gnus-tmp-unselected)
6035                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6036                       (t (format "{%d(+%d) more}"
6037                                  gnus-tmp-unread-and-unticked
6038                                  gnus-tmp-unselected))))
6039                (gnus-tmp-subject
6040                 (if (and gnus-current-headers
6041                          (vectorp gnus-current-headers))
6042                     (gnus-mode-string-quote
6043                      (mail-header-subject gnus-current-headers))
6044                   ""))
6045                bufname-length max-len
6046                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6047           (setq mode-string (eval mformat))
6048           (setq bufname-length (if (string-match "%b" mode-string)
6049                                    (- (length
6050                                        (buffer-name
6051                                         (if (eq where 'summary)
6052                                             nil
6053                                           (get-buffer gnus-article-buffer))))
6054                                       2)
6055                                  0))
6056           (setq max-len (max 4 (if gnus-mode-non-string-length
6057                                    (- (window-width)
6058                                       gnus-mode-non-string-length
6059                                       bufname-length)
6060                                  (length mode-string))))
6061           ;; We might have to chop a bit of the string off...
6062           (when (> (length mode-string) max-len)
6063             (setq mode-string
6064                   (concat (truncate-string-to-width mode-string (- max-len 3))
6065                           "...")))))
6066       ;; Update the mode line.
6067       (setq mode-line-buffer-identification
6068             (gnus-mode-line-buffer-identification (list mode-string)))
6069       (set-buffer-modified-p t))))
6070
6071 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6072   "Go through the HEADERS list and add all Xrefs to a hash table.
6073 The resulting hash table is returned, or nil if no Xrefs were found."
6074   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6075          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6076          (xref-hashtb (gnus-make-hashtable))
6077          start group entry number xrefs header)
6078     (while headers
6079       (setq header (pop headers))
6080       (when (and (setq xrefs (mail-header-xref header))
6081                  (not (memq (setq number (mail-header-number header))
6082                             unreads)))
6083         (setq start 0)
6084         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6085           (setq start (match-end 0))
6086           (setq group (if prefix
6087                           (concat prefix (substring xrefs (match-beginning 1)
6088                                                     (match-end 1)))
6089                         (substring xrefs (match-beginning 1) (match-end 1))))
6090           (setq number
6091                 (string-to-number (substring xrefs (match-beginning 2)
6092                                           (match-end 2))))
6093           (if (setq entry (gnus-gethash group xref-hashtb))
6094               (setcdr entry (cons number (cdr entry)))
6095             (gnus-sethash group (cons number nil) xref-hashtb)))))
6096     (and start xref-hashtb)))
6097
6098 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6099   "Look through all the headers and mark the Xrefs as read."
6100   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6101         name info xref-hashtb idlist method nth4)
6102     (with-current-buffer gnus-group-buffer
6103       (when (setq xref-hashtb
6104                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6105         (mapatoms
6106          (lambda (group)
6107            (unless (string= from-newsgroup (setq name (symbol-name group)))
6108              (setq idlist (symbol-value group))
6109              ;; Dead groups are not updated.
6110              (and (prog1
6111                       (setq info (gnus-get-info name))
6112                     (when (stringp (setq nth4 (gnus-info-method info)))
6113                       (setq nth4 (gnus-server-to-method nth4))))
6114                   ;; Only do the xrefs if the group has the same
6115                   ;; select method as the group we have just read.
6116                   (or (gnus-methods-equal-p
6117                        nth4 (gnus-find-method-for-group from-newsgroup))
6118                       virtual
6119                       (equal nth4 (setq method (gnus-find-method-for-group
6120                                                 from-newsgroup)))
6121                       (and (equal (car nth4) (car method))
6122                            (equal (nth 1 nth4) (nth 1 method))))
6123                   gnus-use-cross-reference
6124                   (or (not (eq gnus-use-cross-reference t))
6125                       virtual
6126                       ;; Only do cross-references on subscribed
6127                       ;; groups, if that is what is wanted.
6128                       (<= (gnus-info-level info) gnus-level-subscribed))
6129                   (gnus-group-make-articles-read name idlist))))
6130          xref-hashtb)))))
6131
6132 (defun gnus-compute-read-articles (group articles)
6133   (let* ((entry (gnus-group-entry group))
6134          (info (nth 2 entry))
6135          (active (gnus-active group))
6136          ninfo)
6137     (when entry
6138       ;; First peel off all invalid article numbers.
6139       (when active
6140         (let ((ids articles)
6141               id first)
6142           (while (setq id (pop ids))
6143             (when (and first (> id (cdr active)))
6144               ;; We'll end up in this situation in one particular
6145               ;; obscure situation.  If you re-scan a group and get
6146               ;; a new article that is cross-posted to a different
6147               ;; group that has not been re-scanned, you might get
6148               ;; crossposted article that has a higher number than
6149               ;; Gnus believes possible.  So we re-activate this
6150               ;; group as well.  This might mean doing the
6151               ;; crossposting thingy will *increase* the number
6152               ;; of articles in some groups.  Tsk, tsk.
6153               (setq active (or (gnus-activate-group group) active)))
6154             (when (or (> id (cdr active))
6155                       (< id (car active)))
6156               (setq articles (delq id articles))))))
6157       ;; If the read list is nil, we init it.
6158       (if (and active
6159                (null (gnus-info-read info))
6160                (> (car active) 1))
6161           (setq ninfo (cons 1 (1- (car active))))
6162         (setq ninfo (gnus-info-read info)))
6163       ;; Then we add the read articles to the range.
6164       (gnus-add-to-range
6165        ninfo (setq articles (sort articles '<))))))
6166
6167 (defun gnus-group-make-articles-read (group articles)
6168   "Update the info of GROUP to say that ARTICLES are read."
6169   (let* ((num 0)
6170          (entry (gnus-group-entry group))
6171          (info (nth 2 entry))
6172          (active (gnus-active group))
6173          range)
6174     (when entry
6175       (setq range (gnus-compute-read-articles group articles))
6176       (with-current-buffer gnus-group-buffer
6177         (gnus-undo-register
6178           `(progn
6179              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6180              (gnus-info-set-read ',info ',(gnus-info-read info))
6181              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6182              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6183              (gnus-group-update-group ,group t))))
6184       ;; Add the read articles to the range.
6185       (gnus-info-set-read info range)
6186       (gnus-request-set-mark group (list (list range 'add '(read))))
6187       ;; Then we have to re-compute how many unread
6188       ;; articles there are in this group.
6189       (when active
6190         (cond
6191          ((not range)
6192           (setq num (- (1+ (cdr active)) (car active))))
6193          ((not (listp (cdr range)))
6194           (setq num (- (cdr active) (- (1+ (cdr range))
6195                                        (car range)))))
6196          (t
6197           (while range
6198             (if (numberp (car range))
6199                 (setq num (1+ num))
6200               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6201             (setq range (cdr range)))
6202           (setq num (- (cdr active) num))))
6203         ;; Update the number of unread articles.
6204         (setcar entry num)
6205         ;; Update the group buffer.
6206         (unless (gnus-ephemeral-group-p group)
6207           (gnus-group-update-group group t))))))
6208
6209 (defvar gnus-newsgroup-none-id 0)
6210
6211 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6212   (let ((cur nntp-server-buffer)
6213         (dependencies
6214          (or dependencies
6215              (with-current-buffer gnus-summary-buffer
6216                gnus-newsgroup-dependencies)))
6217         headers id end ref number
6218         (mail-parse-charset gnus-newsgroup-charset)
6219         (mail-parse-ignored-charsets
6220          (save-current-buffer (condition-case nil
6221                                   (set-buffer gnus-summary-buffer)
6222                                 (error))
6223                               gnus-newsgroup-ignored-charsets)))
6224     (with-current-buffer nntp-server-buffer
6225       ;; Translate all TAB characters into SPACE characters.
6226       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6227       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6228       (ietf-drums-unfold-fws)
6229       (gnus-run-hooks 'gnus-parse-headers-hook)
6230       (let ((case-fold-search t)
6231             in-reply-to header p lines chars)
6232         (goto-char (point-min))
6233         ;; Search to the beginning of the next header.  Error messages
6234         ;; do not begin with 2 or 3.
6235         (while (re-search-forward "^[23][0-9]+ " nil t)
6236           (setq id nil
6237                 ref nil)
6238           ;; This implementation of this function, with nine
6239           ;; search-forwards instead of the one re-search-forward and
6240           ;; a case (which basically was the old function) is actually
6241           ;; about twice as fast, even though it looks messier.  You
6242           ;; can't have everything, I guess.  Speed and elegance
6243           ;; doesn't always go hand in hand.
6244           (setq
6245            header
6246            (vector
6247             ;; Number.
6248             (prog1
6249                 (setq number (read cur))
6250               (end-of-line)
6251               (setq p (point))
6252               (narrow-to-region (point)
6253                                 (or (and (search-forward "\n.\n" nil t)
6254                                          (- (point) 2))
6255                                     (point))))
6256             ;; Subject.
6257             (progn
6258               (goto-char p)
6259               (if (search-forward "\nsubject:" nil t)
6260                   (funcall gnus-decode-encoded-word-function
6261                            (nnheader-header-value))
6262                 "(none)"))
6263             ;; From.
6264             (progn
6265               (goto-char p)
6266               (if (search-forward "\nfrom:" nil t)
6267                   (funcall gnus-decode-encoded-address-function
6268                            (nnheader-header-value))
6269                 "(nobody)"))
6270             ;; Date.
6271             (progn
6272               (goto-char p)
6273               (if (search-forward "\ndate:" nil t)
6274                   (nnheader-header-value) ""))
6275             ;; Message-ID.
6276             (progn
6277               (goto-char p)
6278               (setq id (if (re-search-forward
6279                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6280                            ;; We do it this way to make sure the Message-ID
6281                            ;; is (somewhat) syntactically valid.
6282                            (buffer-substring (match-beginning 1)
6283                                              (match-end 1))
6284                          ;; If there was no message-id, we just fake one
6285                          ;; to make subsequent routines simpler.
6286                          (nnheader-generate-fake-message-id number))))
6287             ;; References.
6288             (progn
6289               (goto-char p)
6290               (if (search-forward "\nreferences:" nil t)
6291                   (progn
6292                     (setq end (point))
6293                     (prog1
6294                         (nnheader-header-value)
6295                       (setq ref
6296                             (buffer-substring
6297                              (progn
6298                                (end-of-line)
6299                                (search-backward ">" end t)
6300                                (1+ (point)))
6301                              (progn
6302                                (search-backward "<" end t)
6303                                (point))))))
6304                 ;; Get the references from the in-reply-to header if there
6305                 ;; were no references and the in-reply-to header looks
6306                 ;; promising.
6307                 (if (and (search-forward "\nin-reply-to:" nil t)
6308                          (setq in-reply-to (nnheader-header-value))
6309                          (string-match "<[^>]+>" in-reply-to))
6310                     (let (ref2)
6311                       (setq ref (substring in-reply-to (match-beginning 0)
6312                                            (match-end 0)))
6313                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6314                         (setq ref2 (substring in-reply-to (match-beginning 0)
6315                                               (match-end 0)))
6316                         (when (> (length ref2) (length ref))
6317                           (setq ref ref2)))
6318                       ref)
6319                   (setq ref nil))))
6320             ;; Chars.
6321             (progn
6322               (goto-char p)
6323               (if (search-forward "\nchars: " nil t)
6324                   (if (numberp (setq chars (ignore-errors (read cur))))
6325                       chars -1)
6326                 -1))
6327             ;; Lines.
6328             (progn
6329               (goto-char p)
6330               (if (search-forward "\nlines: " nil t)
6331                   (if (numberp (setq lines (ignore-errors (read cur))))
6332                       lines -1)
6333                 -1))
6334             ;; Xref.
6335             (progn
6336               (goto-char p)
6337               (and (search-forward "\nxref:" nil t)
6338                    (nnheader-header-value)))
6339             ;; Extra.
6340             (when gnus-extra-headers
6341               (let ((extra gnus-extra-headers)
6342                     out)
6343                 (while extra
6344                   (goto-char p)
6345                   (when (search-forward
6346                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6347                     (push (cons (car extra) (nnheader-header-value))
6348                           out))
6349                   (pop extra))
6350                 out))))
6351           (when (equal id ref)
6352             (setq ref nil))
6353
6354           (when gnus-alter-header-function
6355             (funcall gnus-alter-header-function header)
6356             (setq id (mail-header-id header)
6357                   ref (gnus-parent-id (mail-header-references header))))
6358
6359           (when (setq header
6360                       (gnus-dependencies-add-header
6361                        header dependencies force-new))
6362             (push header headers))
6363           (goto-char (point-max))
6364           (widen))
6365         (nreverse headers)))))
6366
6367 ;; Goes through the xover lines and returns a list of vectors
6368 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6369                                                   force-new dependencies
6370                                                   group also-fetch-heads)
6371   "Parse the news overview data in the server buffer.
6372 Return a list of headers that match SEQUENCE (see
6373 `nntp-retrieve-headers')."
6374   ;; Get the Xref when the users reads the articles since most/some
6375   ;; NNTP servers do not include Xrefs when using XOVER.
6376   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6377   (let ((mail-parse-charset gnus-newsgroup-charset)
6378         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6379         (cur nntp-server-buffer)
6380         (dependencies (or dependencies gnus-newsgroup-dependencies))
6381         (allp (cond
6382                ((eq gnus-read-all-available-headers t)
6383                 t)
6384                ((and (stringp gnus-read-all-available-headers)
6385                      group)
6386                 (string-match gnus-read-all-available-headers group))
6387                (t
6388                 nil)))
6389         number headers header)
6390     (with-current-buffer nntp-server-buffer
6391       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6392       ;; Allow the user to mangle the headers before parsing them.
6393       (gnus-run-hooks 'gnus-parse-headers-hook)
6394       (goto-char (point-min))
6395       (gnus-parse-without-error
6396         (while (and (or sequence allp)
6397                     (not (eobp)))
6398           (setq number (read cur))
6399           (when (not allp)
6400             (while (and sequence
6401                         (< (car sequence) number))
6402               (setq sequence (cdr sequence))))
6403           (when (and (or allp
6404                          (and sequence
6405                               (eq number (car sequence))))
6406                      (progn
6407                        (setq sequence (cdr sequence))
6408                        (setq header (inline
6409                                       (gnus-nov-parse-line
6410                                        number dependencies force-new)))))
6411             (push header headers))
6412           (forward-line 1)))
6413       ;; A common bug in inn is that if you have posted an article and
6414       ;; then retrieves the active file, it will answer correctly --
6415       ;; the new article is included.  However, a NOV entry for the
6416       ;; article may not have been generated yet, so this may fail.
6417       ;; We work around this problem by retrieving the last few
6418       ;; headers using HEAD.
6419       (if (or (not also-fetch-heads)
6420               (not sequence))
6421           ;; We (probably) got all the headers.
6422           (nreverse headers)
6423         (let ((gnus-nov-is-evil t))
6424           (nconc
6425            (nreverse headers)
6426            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6427              (gnus-get-newsgroup-headers))))))))
6428
6429 (defun gnus-article-get-xrefs ()
6430   "Fill in the Xref value in `gnus-current-headers', if necessary.
6431 This is meant to be called in `gnus-article-internal-prepare-hook'."
6432   (let ((headers (with-current-buffer gnus-summary-buffer
6433                    gnus-current-headers)))
6434     (or (not gnus-use-cross-reference)
6435         (not headers)
6436         (and (mail-header-xref headers)
6437              (not (string= (mail-header-xref headers) "")))
6438         (let ((case-fold-search t)
6439               xref)
6440           (save-restriction
6441             (nnheader-narrow-to-headers)
6442             (goto-char (point-min))
6443             (when (or (and (not (eobp))
6444                            (eq (downcase (char-after)) ?x)
6445                            (looking-at "Xref:"))
6446                       (search-forward "\nXref:" nil t))
6447               (goto-char (1+ (match-end 0)))
6448               (setq xref (buffer-substring (point) (point-at-eol)))
6449               (mail-header-set-xref headers xref)))))))
6450
6451 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6452   "Find article ID and insert the summary line for that article.
6453 OLD-HEADER can either be a header or a line number to insert
6454 the subject line on."
6455   (let* ((line (and (numberp old-header) old-header))
6456          (old-header (and (vectorp old-header) old-header))
6457          (header (cond ((and old-header use-old-header)
6458                         old-header)
6459                        ((and (numberp id)
6460                              (gnus-number-to-header id))
6461                         (gnus-number-to-header id))
6462                        (t
6463                         (gnus-read-header id))))
6464          (number (and (numberp id) id))
6465          d)
6466     (when header
6467       ;; Rebuild the thread that this article is part of and go to the
6468       ;; article we have fetched.
6469       (when (and (not gnus-show-threads)
6470                  old-header)
6471         (when (and number
6472                    (setq d (gnus-data-find (mail-header-number old-header))))
6473           (goto-char (gnus-data-pos d))
6474           (gnus-data-remove
6475            number
6476            (- (point-at-bol)
6477               (prog1
6478                   (1+ (point-at-eol))
6479                 (gnus-delete-line))))))
6480       ;; Remove list identifiers from subject.
6481       (when gnus-list-identifiers
6482         (let ((gnus-newsgroup-headers (list header)))
6483           (gnus-summary-remove-list-identifiers)))
6484       (when old-header
6485         (mail-header-set-number header (mail-header-number old-header)))
6486       (setq gnus-newsgroup-sparse
6487             (delq (setq number (mail-header-number header))
6488                   gnus-newsgroup-sparse))
6489       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6490       (push number gnus-newsgroup-limit)
6491       (gnus-rebuild-thread (mail-header-id header) line)
6492       (gnus-summary-goto-subject number nil t))
6493     (when (and (numberp number)
6494                (> number 0))
6495       ;; We have to update the boundaries even if we can't fetch the
6496       ;; article if ID is a number -- so that the next `P' or `N'
6497       ;; command will fetch the previous (or next) article even
6498       ;; if the one we tried to fetch this time has been canceled.
6499       (when (> number gnus-newsgroup-end)
6500         (setq gnus-newsgroup-end number))
6501       (when (< number gnus-newsgroup-begin)
6502         (setq gnus-newsgroup-begin number))
6503       (setq gnus-newsgroup-unselected
6504             (delq number gnus-newsgroup-unselected)))
6505     ;; Report back a success?
6506     (and header (mail-header-number header))))
6507
6508 ;;; Process/prefix in the summary buffer
6509
6510 (defun gnus-summary-work-articles (n)
6511   "Return a list of articles to be worked upon.
6512 The prefix argument, the list of process marked articles, and the
6513 current article will be taken into consideration."
6514   (with-current-buffer gnus-summary-buffer
6515     (cond
6516      (n
6517       ;; A numerical prefix has been given.
6518       (setq n (prefix-numeric-value n))
6519       (let ((backward (< n 0))
6520             (n (abs (prefix-numeric-value n)))
6521             articles article)
6522         (save-excursion
6523           (while
6524               (and (> n 0)
6525                    (push (setq article (gnus-summary-article-number))
6526                          articles)
6527                    (if backward
6528                        (gnus-summary-find-prev nil article)
6529                      (gnus-summary-find-next nil article)))
6530             (decf n)))
6531         (nreverse articles)))
6532      ((and (gnus-region-active-p) (mark))
6533       (message "region active")
6534       ;; Work on the region between point and mark.
6535       (let ((max (max (point) (mark)))
6536             articles article)
6537         (save-excursion
6538           (goto-char (min (point) (mark)))
6539           (while
6540               (and
6541                (push (setq article (gnus-summary-article-number)) articles)
6542                (gnus-summary-find-next nil article)
6543                (< (point) max)))
6544           (nreverse articles))))
6545      (gnus-newsgroup-processable
6546       ;; There are process-marked articles present.
6547       ;; Save current state.
6548       (gnus-summary-save-process-mark)
6549       ;; Return the list.
6550       (reverse gnus-newsgroup-processable))
6551      (t
6552       ;; Just return the current article.
6553       (list (gnus-summary-article-number))))))
6554
6555 (defmacro gnus-summary-iterate (arg &rest forms)
6556   "Iterate over the process/prefixed articles and do FORMS.
6557 ARG is the interactive prefix given to the command.  FORMS will be
6558 executed with point over the summary line of the articles."
6559   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6560     `(let ((,articles (gnus-summary-work-articles ,arg)))
6561        (while ,articles
6562          (gnus-summary-goto-subject (car ,articles))
6563          ,@forms
6564          (pop ,articles)))))
6565
6566 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6567 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6568
6569 (defun gnus-summary-save-process-mark ()
6570   "Push the current set of process marked articles on the stack."
6571   (interactive)
6572   (push (copy-sequence gnus-newsgroup-processable)
6573         gnus-newsgroup-process-stack))
6574
6575 (defun gnus-summary-kill-process-mark ()
6576   "Push the current set of process marked articles on the stack and unmark."
6577   (interactive)
6578   (gnus-summary-save-process-mark)
6579   (gnus-summary-unmark-all-processable))
6580
6581 (defun gnus-summary-yank-process-mark ()
6582   "Pop the last process mark state off the stack and restore it."
6583   (interactive)
6584   (unless gnus-newsgroup-process-stack
6585     (error "Empty mark stack"))
6586   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6587
6588 (defun gnus-summary-process-mark-set (set)
6589   "Make SET into the current process marked articles."
6590   (gnus-summary-unmark-all-processable)
6591   (mapc 'gnus-summary-set-process-mark set))
6592
6593 ;;; Searching and stuff
6594
6595 (defun gnus-summary-search-group (&optional backward use-level)
6596   "Search for next unread newsgroup.
6597 If optional argument BACKWARD is non-nil, search backward instead."
6598   (with-current-buffer gnus-group-buffer
6599     (when (gnus-group-search-forward
6600            backward nil (if use-level (gnus-group-group-level) nil))
6601       (gnus-group-group-name))))
6602
6603 (defun gnus-summary-best-group (&optional exclude-group)
6604   "Find the name of the best unread group.
6605 If EXCLUDE-GROUP, do not go to this group."
6606   (with-current-buffer gnus-group-buffer
6607     (save-excursion
6608       (gnus-group-best-unread-group exclude-group))))
6609
6610 (defun gnus-summary-find-next (&optional unread article backward)
6611   (if backward
6612       (gnus-summary-find-prev unread article)
6613     (let* ((dummy (gnus-summary-article-intangible-p))
6614            (article (or article (gnus-summary-article-number)))
6615            (data (gnus-data-find-list article))
6616            result)
6617       (when (and (not dummy)
6618                  (or (not gnus-summary-check-current)
6619                      (not unread)
6620                      (not (gnus-data-unread-p (car data)))))
6621         (setq data (cdr data)))
6622       (when (setq result
6623                   (if unread
6624                       (progn
6625                         (while data
6626                           (unless (memq (gnus-data-number (car data))
6627                                         (cond
6628                                          ((eq gnus-auto-goto-ignores
6629                                               'always-undownloaded)
6630                                           gnus-newsgroup-undownloaded)
6631                                          (gnus-plugged
6632                                           nil)
6633                                          ((eq gnus-auto-goto-ignores
6634                                               'unfetched)
6635                                           gnus-newsgroup-unfetched)
6636                                          ((eq gnus-auto-goto-ignores
6637                                               'undownloaded)
6638                                           gnus-newsgroup-undownloaded)))
6639                             (when (gnus-data-unread-p (car data))
6640                               (setq result (car data)
6641                                     data nil)))
6642                           (setq data (cdr data)))
6643                         result)
6644                     (car data)))
6645         (goto-char (gnus-data-pos result))
6646         (gnus-data-number result)))))
6647
6648 (defun gnus-summary-find-prev (&optional unread article)
6649   (let* ((eobp (eobp))
6650          (article (or article (gnus-summary-article-number)))
6651          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6652          result)
6653     (when (and (not eobp)
6654                (or (not gnus-summary-check-current)
6655                    (not unread)
6656                    (not (gnus-data-unread-p (car data)))))
6657       (setq data (cdr data)))
6658     (when (setq result
6659                 (if unread
6660                     (progn
6661                       (while data
6662                         (unless (memq (gnus-data-number (car data))
6663                                       (cond
6664                                        ((eq gnus-auto-goto-ignores
6665                                             'always-undownloaded)
6666                                         gnus-newsgroup-undownloaded)
6667                                        (gnus-plugged
6668                                         nil)
6669                                        ((eq gnus-auto-goto-ignores
6670                                             'unfetched)
6671                                         gnus-newsgroup-unfetched)
6672                                        ((eq gnus-auto-goto-ignores
6673                                             'undownloaded)
6674                                         gnus-newsgroup-undownloaded)))
6675                           (when (gnus-data-unread-p (car data))
6676                             (setq result (car data)
6677                                   data nil)))
6678                         (setq data (cdr data)))
6679                       result)
6680                   (car data)))
6681       (goto-char (gnus-data-pos result))
6682       (gnus-data-number result))))
6683
6684 (defun gnus-summary-find-subject (subject &optional unread backward article)
6685   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6686          (article (or article (gnus-summary-article-number)))
6687          (articles (gnus-data-list backward))
6688          (arts (gnus-data-find-list article articles))
6689          result)
6690     (when (or (not gnus-summary-check-current)
6691               (not unread)
6692               (not (gnus-data-unread-p (car arts))))
6693       (setq arts (cdr arts)))
6694     (while arts
6695       (and (or (not unread)
6696                (gnus-data-unread-p (car arts)))
6697            (vectorp (gnus-data-header (car arts)))
6698            (gnus-subject-equal
6699             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6700            (setq result (car arts)
6701                  arts nil))
6702       (setq arts (cdr arts)))
6703     (and result
6704          (goto-char (gnus-data-pos result))
6705          (gnus-data-number result))))
6706
6707 (defun gnus-summary-search-forward (&optional unread subject backward)
6708   "Search forward for an article.
6709 If UNREAD, look for unread articles.  If SUBJECT, look for
6710 articles with that subject.  If BACKWARD, search backward instead."
6711   (cond (subject (gnus-summary-find-subject subject unread backward))
6712         (backward (gnus-summary-find-prev unread))
6713         (t (gnus-summary-find-next unread))))
6714
6715 (defun gnus-recenter (&optional n)
6716   "Center point in window and redisplay frame.
6717 Also do horizontal recentering."
6718   (interactive "P")
6719   (when (and gnus-auto-center-summary
6720              (not (eq gnus-auto-center-summary 'vertical)))
6721     (gnus-horizontal-recenter))
6722   (if (fboundp 'recenter-top-bottom)
6723       (recenter-top-bottom n)
6724     (recenter n)))
6725
6726 (put 'gnus-recenter 'isearch-scroll t)
6727
6728 (defun gnus-forward-line-ignore-invisible (n)
6729   "Move N lines forward (backward if N is negative).
6730 Like forward-line, but skip over (and don't count) invisible lines."
6731   (let (done)
6732     (while (and (> n 0) (not done))
6733       ;; If the following character is currently invisible,
6734       ;; skip all characters with that same `invisible' property value.
6735       (while (gnus-invisible-p (point))
6736         (goto-char (gnus-next-char-property-change (point))))
6737       (forward-line 1)
6738       (if (eobp)
6739           (setq done t)
6740         (setq n (1- n))))
6741     (while (and (< n 0) (not done))
6742       (forward-line -1)
6743       (if (bobp) (setq done t)
6744         (setq n (1+ n))
6745         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6746           (goto-char (gnus-previous-char-property-change (point))))))))
6747
6748 (defun gnus-summary-recenter ()
6749   "Center point in the summary window.
6750 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6751 displayed, no centering will be performed."
6752   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6753   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6754   (interactive)
6755   ;; The user has to want it.
6756   (when gnus-auto-center-summary
6757     (let* ((top (cond ((< (window-height) 4) 0)
6758                       ((< (window-height) 7) 1)
6759                       (t (if (numberp gnus-auto-center-summary)
6760                              gnus-auto-center-summary
6761                            (/ (1- (window-height)) 2)))))
6762            (height (1- (window-height)))
6763            (bottom (save-excursion
6764                      (goto-char (point-max))
6765                      (gnus-forward-line-ignore-invisible (- height))
6766                      (point)))
6767            (window (get-buffer-window (current-buffer))))
6768       (when (get-buffer-window gnus-article-buffer)
6769         ;; Only do recentering when the article buffer is displayed,
6770         ;; Set the window start to either `bottom', which is the biggest
6771         ;; possible valid number, or the second line from the top,
6772         ;; whichever is the least.
6773         (let ((top-pos (save-excursion
6774                          (gnus-forward-line-ignore-invisible (- top))
6775                          (point))))
6776           (if (> bottom top-pos)
6777               ;; Keep the second line from the top visible
6778               (set-window-start window top-pos)
6779             ;; Try to keep the bottom line visible; if it's partially
6780             ;; obscured, either scroll one more line to make it fully
6781             ;; visible, or revert to using TOP-POS.
6782             (save-excursion
6783               (goto-char (point-max))
6784               (gnus-forward-line-ignore-invisible -1)
6785               (let ((last-line-start (point)))
6786                 (goto-char bottom)
6787                 (set-window-start window (point) t)
6788                 (when (not (pos-visible-in-window-p last-line-start window))
6789                   (gnus-forward-line-ignore-invisible 1)
6790                   (set-window-start window (min (point) top-pos) t)))))))
6791       ;; Do horizontal recentering while we're at it.
6792       (when (and (get-buffer-window (current-buffer) t)
6793                  (not (eq gnus-auto-center-summary 'vertical)))
6794         (let ((selected (selected-window)))
6795           (select-window (get-buffer-window (current-buffer) t))
6796           (gnus-summary-position-point)
6797           (gnus-horizontal-recenter)
6798           (select-window selected))))))
6799
6800 (defun gnus-summary-jump-to-group (newsgroup)
6801   "Move point to NEWSGROUP in group mode buffer."
6802   ;; Keep update point of group mode buffer if visible.
6803   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6804       (save-window-excursion
6805         ;; Take care of tree window mode.
6806         (when (get-buffer-window gnus-group-buffer)
6807           (pop-to-buffer gnus-group-buffer))
6808         (gnus-group-jump-to-group newsgroup))
6809     (save-excursion
6810       ;; Take care of tree window mode.
6811       (if (get-buffer-window gnus-group-buffer 0)
6812           (pop-to-buffer gnus-group-buffer)
6813         (set-buffer gnus-group-buffer))
6814       (gnus-group-jump-to-group newsgroup))))
6815
6816 ;; This function returns a list of article numbers based on the
6817 ;; difference between the ranges of read articles in this group and
6818 ;; the range of active articles.
6819 (defun gnus-list-of-unread-articles (group)
6820   (let* ((read (gnus-info-read (gnus-get-info group)))
6821          (active (or (gnus-active group) (gnus-activate-group group)))
6822          (last (or (cdr active)
6823                    (error "Group %s couldn't be activated " group)))
6824          (bottom (if gnus-newsgroup-maximum-articles
6825                      (max (car active)
6826                           (- last gnus-newsgroup-maximum-articles -1))
6827                    (car active)))
6828          first nlast unread)
6829     ;; If none are read, then all are unread.
6830     (if (not read)
6831         (setq first bottom)
6832       ;; If the range of read articles is a single range, then the
6833       ;; first unread article is the article after the last read
6834       ;; article.  Sounds logical, doesn't it?
6835       (if (and (not (listp (cdr read)))
6836                (or (< (car read) bottom)
6837                    (progn (setq read (list read))
6838                           nil)))
6839           (setq first (max bottom (1+ (cdr read))))
6840         ;; `read' is a list of ranges.
6841         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6842                                   (caar read)))
6843                   1)
6844           (setq first bottom))
6845         (while read
6846           (when first
6847             (while (< first nlast)
6848               (setq unread (cons first unread)
6849                     first (1+ first))))
6850           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6851           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6852           (setq read (cdr read)))))
6853     ;; And add the last unread articles.
6854     (while (<= first last)
6855       (setq unread (cons first unread)
6856             first (1+ first)))
6857     ;; Return the list of unread articles.
6858     (delq 0 (nreverse unread))))
6859
6860 (defun gnus-list-of-read-articles (group)
6861   "Return a list of unread, unticked and non-dormant articles."
6862   (let* ((info (gnus-get-info group))
6863          (marked (gnus-info-marks info))
6864          (active (gnus-active group)))
6865     (and info active
6866          (gnus-list-range-difference
6867           (gnus-list-range-difference
6868            (gnus-sorted-complement
6869             (gnus-uncompress-range
6870              (if gnus-newsgroup-maximum-articles
6871                  (cons (max (car active)
6872                             (- (cdr active)
6873                                gnus-newsgroup-maximum-articles
6874                                -1))
6875                        (cdr active))
6876                active))
6877             (gnus-list-of-unread-articles group))
6878            (cdr (assq 'dormant marked)))
6879           (cdr (assq 'tick marked))))))
6880
6881 ;; This function returns a sequence of article numbers based on the
6882 ;; difference between the ranges of read articles in this group and
6883 ;; the range of active articles.
6884 (defun gnus-sequence-of-unread-articles (group)
6885   (let* ((read (gnus-info-read (gnus-get-info group)))
6886          (active (or (gnus-active group) (gnus-activate-group group)))
6887          (last (cdr active))
6888          (bottom (if gnus-newsgroup-maximum-articles
6889                      (max (car active)
6890                           (- last gnus-newsgroup-maximum-articles -1))
6891                    (car active)))
6892          first nlast unread)
6893     ;; If none are read, then all are unread.
6894     (if (not read)
6895         (setq first bottom)
6896       ;; If the range of read articles is a single range, then the
6897       ;; first unread article is the article after the last read
6898       ;; article.  Sounds logical, doesn't it?
6899       (if (and (not (listp (cdr read)))
6900                (or (< (car read) bottom)
6901                    (progn (setq read (list read))
6902                           nil)))
6903           (setq first (max bottom (1+ (cdr read))))
6904         ;; `read' is a list of ranges.
6905         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6906                                   (caar read)))
6907                   1)
6908           (setq first bottom))
6909         (while read
6910           (when first
6911             (push (cons first nlast) unread))
6912           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6913           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6914           (setq read (cdr read)))))
6915     ;; And add the last unread articles.
6916     (cond ((not (and first last))
6917            nil)
6918           ((< first last)
6919            (push (cons first last) unread))
6920           ((= first last)
6921            (push first unread)))
6922     ;; Return the sequence of unread articles.
6923     (delq 0 (nreverse unread))))
6924
6925 ;; Various summary commands
6926
6927 (defun gnus-summary-select-article-buffer ()
6928   "Reconfigure windows to show article buffer."
6929   (interactive)
6930   (if (not (gnus-buffer-live-p gnus-article-buffer))
6931       (error "There is no article buffer for this summary buffer")
6932     (gnus-configure-windows 'article)
6933     (select-window (get-buffer-window gnus-article-buffer))))
6934
6935 (defun gnus-summary-universal-argument (arg)
6936   "Perform any operation on all articles that are process/prefixed."
6937   (interactive "P")
6938   (let ((articles (gnus-summary-work-articles arg))
6939         func article)
6940     (if (eq
6941          (setq
6942           func
6943           (key-binding
6944            (read-key-sequence
6945             (substitute-command-keys
6946              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6947          'undefined)
6948         (gnus-error 1 "Undefined key")
6949       (save-excursion
6950         (while articles
6951           (gnus-summary-goto-subject (setq article (pop articles)))
6952           (let (gnus-newsgroup-processable)
6953             (command-execute func))
6954           (gnus-summary-remove-process-mark article)))))
6955   (gnus-summary-position-point))
6956
6957 (defun gnus-summary-toggle-truncation (&optional arg)
6958   "Toggle truncation of summary lines.
6959 With ARG, turn line truncation on if ARG is positive."
6960   (interactive "P")
6961   (setq truncate-lines
6962         (if (null arg) (not truncate-lines)
6963           (> (prefix-numeric-value arg) 0)))
6964   (redraw-display))
6965
6966 (defun gnus-summary-find-for-reselect ()
6967   "Return the number of an article to stay on across a reselect.
6968 The current article is considered, then following articles, then previous
6969 articles.  An article is sought which is not cancelled and isn't a temporary
6970 insertion from another group.  If there's no such then return a dummy 0."
6971   (let (found)
6972     (dolist (rev '(nil t))
6973       (unless found      ; don't demand the reverse list if we don't need it
6974         (let ((data (gnus-data-find-list
6975                      (gnus-summary-article-number) (gnus-data-list rev))))
6976           (while (and data (not found))
6977             (if (and (< 0 (gnus-data-number (car data)))
6978                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6979                 (setq found (gnus-data-number (car data))))
6980             (setq data (cdr data))))))
6981     (or found 0)))
6982
6983 (defun gnus-summary-reselect-current-group (&optional all rescan)
6984   "Exit and then reselect the current newsgroup.
6985 The prefix argument ALL means to select all articles."
6986   (interactive "P")
6987   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6988     (error "Ephemeral groups can't be reselected"))
6989   (let ((current-subject (gnus-summary-find-for-reselect))
6990         (group gnus-newsgroup-name))
6991     (setq gnus-newsgroup-begin nil)
6992     (gnus-summary-exit nil 'leave-hidden)
6993     ;; We have to adjust the point of group mode buffer because
6994     ;; point was moved to the next unread newsgroup by exiting.
6995     (gnus-summary-jump-to-group group)
6996     (when rescan
6997       (save-excursion
6998         (gnus-group-get-new-news-this-group 1)))
6999     (gnus-group-read-group all t)
7000     (gnus-summary-goto-subject current-subject nil t)))
7001
7002 (defun gnus-summary-rescan-group (&optional all)
7003   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7004   (interactive "P")
7005   (gnus-summary-reselect-current-group all t))
7006
7007 (defun gnus-summary-update-info (&optional non-destructive)
7008   (save-excursion
7009     (let ((group gnus-newsgroup-name))
7010       (when group
7011         (when gnus-newsgroup-kill-headers
7012           (setq gnus-newsgroup-killed
7013                 (gnus-compress-sequence
7014                  (gnus-sorted-union
7015                   (gnus-list-range-intersection
7016                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7017                   gnus-newsgroup-unreads)
7018                  t)))
7019         (unless (listp (cdr gnus-newsgroup-killed))
7020           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7021         (let ((headers gnus-newsgroup-headers))
7022           ;; Set the new ranges of read articles.
7023           (with-current-buffer gnus-group-buffer
7024             (gnus-undo-force-boundary))
7025           (gnus-update-read-articles
7026            group (gnus-sorted-union
7027                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7028           ;; Set the current article marks.
7029           (let ((gnus-newsgroup-scored
7030                  (if (and (not gnus-save-score)
7031                           (not non-destructive))
7032                      nil
7033                    gnus-newsgroup-scored)))
7034             (save-excursion
7035               (gnus-update-marks)))
7036           ;; Do the cross-ref thing.
7037           (when gnus-use-cross-reference
7038             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7039           ;; Do not switch windows but change the buffer to work.
7040           (set-buffer gnus-group-buffer)
7041           (unless (gnus-ephemeral-group-p group)
7042             (gnus-group-update-group group)))))))
7043
7044 (defun gnus-summary-save-newsrc (&optional force)
7045   "Save the current number of read/marked articles in the dribble buffer.
7046 The dribble buffer will then be saved.
7047 If FORCE (the prefix), also save the .newsrc file(s)."
7048   (interactive "P")
7049   (gnus-summary-update-info t)
7050   (if force
7051       (gnus-save-newsrc-file)
7052     (gnus-dribble-save)))
7053
7054 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7055
7056 (defun gnus-summary-exit (&optional temporary leave-hidden)
7057   "Exit reading current newsgroup, and then return to group selection mode.
7058 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7059   (interactive)
7060   (gnus-set-global-variables)
7061   (when (gnus-buffer-live-p gnus-article-buffer)
7062     (with-current-buffer gnus-article-buffer
7063       (mm-destroy-parts gnus-article-mime-handles)
7064       ;; Set it to nil for safety reason.
7065       (setq gnus-article-mime-handle-alist nil)
7066       (setq gnus-article-mime-handles nil)))
7067   (gnus-kill-save-kill-buffer)
7068   (gnus-async-halt-prefetch)
7069   (let* ((group gnus-newsgroup-name)
7070          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7071          (gnus-group-is-exiting-p t)
7072          (mode major-mode)
7073          (group-point nil)
7074          (buf (current-buffer)))
7075     (unless quit-config
7076       ;; Do adaptive scoring, and possibly save score files.
7077       (when gnus-newsgroup-adaptive
7078         (gnus-score-adaptive))
7079       (when gnus-use-scoring
7080         (gnus-score-save)))
7081     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7082     ;; If we have several article buffers, we kill them at exit.
7083     (unless gnus-single-article-buffer
7084       (when (gnus-buffer-live-p gnus-article-buffer)
7085         (with-current-buffer gnus-article-buffer
7086           ;; Don't kill sticky article buffers
7087           (unless (eq major-mode 'gnus-sticky-article-mode)
7088             (gnus-kill-buffer gnus-article-buffer)
7089             (setq gnus-article-current nil))))
7090       (gnus-kill-buffer gnus-original-article-buffer))
7091     (when gnus-use-cache
7092       (gnus-cache-possibly-remove-articles)
7093       (gnus-cache-save-buffers))
7094     (gnus-async-prefetch-remove-group group)
7095     (when gnus-suppress-duplicates
7096       (gnus-dup-enter-articles))
7097     (when gnus-use-trees
7098       (gnus-tree-close group))
7099     (when gnus-use-cache
7100       (gnus-cache-write-active))
7101     ;; Remove entries for this group.
7102     (nnmail-purge-split-history (gnus-group-real-name group))
7103     ;; Make all changes in this group permanent.
7104     (unless quit-config
7105       (gnus-run-hooks 'gnus-exit-group-hook)
7106       (gnus-summary-update-info))
7107     (gnus-close-group group)
7108     ;; Make sure where we were, and go to next newsgroup.
7109     (set-buffer gnus-group-buffer)
7110     (unless quit-config
7111       (gnus-group-jump-to-group group))
7112     (gnus-run-hooks 'gnus-summary-exit-hook)
7113     (unless (or quit-config
7114                 (not gnus-summary-next-group-on-exit)
7115                 ;; If this group has disappeared from the summary
7116                 ;; buffer, don't skip forwards.
7117                 (not (string= group (gnus-group-group-name))))
7118       (gnus-group-next-unread-group 1))
7119     (setq group-point (point))
7120     (if temporary
7121         nil                             ;Nothing to do.
7122       (set-buffer buf)
7123       (if (not gnus-kill-summary-on-exit)
7124           (progn
7125             (gnus-deaden-summary)
7126             (setq mode nil))
7127         ;; We set all buffer-local variables to nil.  It is unclear why
7128         ;; this is needed, but if we don't, buffer-local variables are
7129         ;; not garbage-collected, it seems.  This would the lead to en
7130         ;; ever-growing Emacs.
7131         (gnus-summary-clear-local-variables)
7132         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7133           (gnus-summary-clear-local-variables))
7134         (when (get-buffer gnus-article-buffer)
7135           (bury-buffer gnus-article-buffer))
7136         ;; Return to group mode buffer.
7137         (when (eq mode 'gnus-summary-mode)
7138           (gnus-kill-buffer buf)))
7139       (setq gnus-current-select-method gnus-select-method)
7140       (set-buffer gnus-group-buffer)
7141       (if quit-config
7142           (gnus-handle-ephemeral-exit quit-config)
7143         (goto-char group-point)
7144         ;; If gnus-group-buffer is already displayed, make sure we also move
7145         ;; the cursor in the window that displays it.
7146         (let ((win (get-buffer-window (current-buffer) 0)))
7147           (if win (set-window-point win (point))))
7148         (unless leave-hidden
7149           (gnus-configure-windows 'group 'force)))
7150       ;; Clear the current group name.
7151       (unless quit-config
7152         (setq gnus-newsgroup-name nil)))))
7153
7154 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7155 (defun gnus-summary-exit-no-update (&optional no-questions)
7156   "Quit reading current newsgroup without updating read article info."
7157   (interactive)
7158   (let* ((group gnus-newsgroup-name)
7159          (gnus-group-is-exiting-p t)
7160          (gnus-group-is-exiting-without-update-p t)
7161          (quit-config (gnus-group-quit-config group)))
7162     (when (or no-questions
7163               gnus-expert-user
7164               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7165       (gnus-async-halt-prefetch)
7166       (run-hooks 'gnus-summary-prepare-exit-hook)
7167       (when (gnus-buffer-live-p gnus-article-buffer)
7168         (with-current-buffer gnus-article-buffer
7169           (mm-destroy-parts gnus-article-mime-handles)
7170           ;; Set it to nil for safety reason.
7171           (setq gnus-article-mime-handle-alist nil)
7172           (setq gnus-article-mime-handles nil)))
7173       ;; If we have several article buffers, we kill them at exit.
7174       (unless gnus-single-article-buffer
7175         (gnus-kill-buffer gnus-article-buffer)
7176         (gnus-kill-buffer gnus-original-article-buffer)
7177         (setq gnus-article-current nil))
7178       (if (not gnus-kill-summary-on-exit)
7179           (gnus-deaden-summary)
7180         (gnus-close-group group)
7181         (gnus-summary-clear-local-variables)
7182         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7183           (gnus-summary-clear-local-variables))
7184         (gnus-kill-buffer gnus-summary-buffer))
7185       (unless gnus-single-article-buffer
7186         (setq gnus-article-current nil))
7187       (when gnus-use-trees
7188         (gnus-tree-close group))
7189       (gnus-async-prefetch-remove-group group)
7190       (when (get-buffer gnus-article-buffer)
7191         (bury-buffer gnus-article-buffer))
7192       ;; Return to the group buffer.
7193       (gnus-configure-windows 'group 'force)
7194       ;; Clear the current group name.
7195       (setq gnus-newsgroup-name nil)
7196       (unless (gnus-ephemeral-group-p group)
7197         (gnus-group-update-group group))
7198       (when (equal (gnus-group-group-name) group)
7199         (gnus-group-next-unread-group 1))
7200       (when quit-config
7201         (gnus-handle-ephemeral-exit quit-config)))))
7202
7203 (defun gnus-handle-ephemeral-exit (quit-config)
7204   "Handle movement when leaving an ephemeral group.
7205 The state which existed when entering the ephemeral is reset."
7206   (if (not (buffer-name (car quit-config)))
7207       (gnus-configure-windows 'group 'force)
7208     (set-buffer (car quit-config))
7209     (cond ((eq major-mode 'gnus-summary-mode)
7210            (gnus-set-global-variables))
7211           ((eq major-mode 'gnus-article-mode)
7212            (save-current-buffer
7213              ;; The `gnus-summary-buffer' variable may point
7214              ;; to the old summary buffer when using a single
7215              ;; article buffer.
7216              (unless (gnus-buffer-live-p gnus-summary-buffer)
7217                (set-buffer gnus-group-buffer))
7218              (set-buffer gnus-summary-buffer)
7219              (gnus-set-global-variables))))
7220     (if (or (eq (cdr quit-config) 'article)
7221             (eq (cdr quit-config) 'pick))
7222         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7223             (gnus-configure-windows 'pick 'force)
7224           (gnus-configure-windows (cdr quit-config) 'force))
7225       (gnus-configure-windows (cdr quit-config) 'force))
7226     (when (eq major-mode 'gnus-summary-mode)
7227       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7228                                                      next-unread-noselect))
7229           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7230                                   'next-noselect)
7231                               (gnus-summary-next-subject 1 nil t))
7232                              ((eq gnus-auto-select-on-ephemeral-exit
7233                                   'next-unread-noselect)
7234                               (gnus-summary-next-subject 1 t t))))
7235             ;; Hide the article buffer which displays the article different
7236             ;; from the one that the cursor points to in the summary buffer.
7237             (gnus-configure-windows 'summary 'force))
7238         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7239                (gnus-summary-next-subject 1))
7240               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7241                (gnus-summary-next-subject 1 t))))
7242       (gnus-summary-recenter)
7243       (gnus-summary-position-point))))
7244
7245 ;;; Dead summaries.
7246
7247 (defvar gnus-dead-summary-mode-map
7248   (let ((map (make-keymap)))
7249     (suppress-keymap map)
7250     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7251     (dolist (key '("\C-d" "\r" "\177" [delete]))
7252       (define-key map key 'gnus-summary-wake-up-the-dead))
7253     (dolist (key '("q" "Q"))
7254       (define-key map key 'bury-buffer))
7255     map))
7256
7257 (define-minor-mode gnus-dead-summary-mode
7258   "Minor mode for Gnus summary buffers."
7259   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7260   (unless (derived-mode-p 'gnus-summary-mode)
7261     (setq gnus-dead-summary-mode nil)))
7262
7263 (defun gnus-deaden-summary ()
7264   "Make the current summary buffer into a dead summary buffer."
7265   ;; Kill any previous dead summary buffer.
7266   (when (and gnus-dead-summary
7267              (buffer-name gnus-dead-summary))
7268     (with-current-buffer gnus-dead-summary
7269       (when gnus-dead-summary-mode
7270         (kill-buffer (current-buffer)))))
7271   ;; Make this the current dead summary.
7272   (setq gnus-dead-summary (current-buffer))
7273   (gnus-dead-summary-mode 1)
7274   (let ((name (buffer-name)))
7275     (when (string-match "Summary" name)
7276       (rename-buffer
7277        (concat (substring name 0 (match-beginning 0)) "Dead "
7278                (substring name (match-beginning 0)))
7279        t)
7280       (bury-buffer))))
7281
7282 (defun gnus-kill-or-deaden-summary (buffer)
7283   "Kill or deaden the summary BUFFER."
7284   (save-excursion
7285     (when (and (buffer-name buffer)
7286                (not gnus-single-article-buffer))
7287       (with-current-buffer buffer
7288         (gnus-kill-buffer gnus-article-buffer)
7289         (gnus-kill-buffer gnus-original-article-buffer)))
7290     (cond
7291      ;; Kill the buffer.
7292      (gnus-kill-summary-on-exit
7293       (when (and gnus-use-trees
7294                  (gnus-buffer-exists-p buffer))
7295         (with-current-buffer buffer
7296           (gnus-tree-close gnus-newsgroup-name)))
7297       (gnus-kill-buffer buffer))
7298      ;; Deaden the buffer.
7299      ((gnus-buffer-exists-p buffer)
7300       (with-current-buffer buffer
7301         (gnus-deaden-summary))))))
7302
7303 (defun gnus-summary-wake-up-the-dead (&rest args)
7304   "Wake up the dead summary buffer."
7305   (interactive)
7306   (gnus-dead-summary-mode -1)
7307   (let ((name (buffer-name)))
7308     (when (string-match "Dead " name)
7309       (rename-buffer
7310        (concat (substring name 0 (match-beginning 0))
7311                (substring name (match-end 0)))
7312        t)))
7313   (gnus-message 3 "This dead summary is now alive again"))
7314
7315 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7316 (defun gnus-summary-fetch-faq (&optional faq-dir)
7317   "Fetch the FAQ for the current group.
7318 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7319 in."
7320   (interactive
7321    (list
7322     (when current-prefix-arg
7323       (completing-read
7324        "FAQ dir: " (and (listp gnus-group-faq-directory)
7325                         (mapcar 'list
7326                                 gnus-group-faq-directory))))))
7327   (let (gnus-faq-buffer)
7328     (when (setq gnus-faq-buffer
7329                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7330       (gnus-configure-windows 'summary-faq))))
7331
7332 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7333 (defun gnus-summary-describe-group (&optional force)
7334   "Describe the current newsgroup."
7335   (interactive "P")
7336   (gnus-group-describe-group force gnus-newsgroup-name))
7337
7338 (defun gnus-summary-describe-briefly ()
7339   "Describe summary mode commands briefly."
7340   (interactive)
7341   (gnus-message 6 "%s" (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info   \\[gnus-summary-describe-briefly]:This help")))
7342
7343 ;; Walking around group mode buffer from summary mode.
7344
7345 (defun gnus-summary-next-group (&optional no-article target-group backward)
7346   "Exit current newsgroup and then select next unread newsgroup.
7347 If prefix argument NO-ARTICLE is non-nil, no article is selected
7348 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7349 previous group instead."
7350   (interactive "P")
7351   ;; Stop pre-fetching.
7352   (gnus-async-halt-prefetch)
7353   (let ((current-group gnus-newsgroup-name)
7354         (current-buffer (current-buffer))
7355         entered)
7356     ;; First we semi-exit this group to update Xrefs and all variables.
7357     ;; We can't do a real exit, because the window conf must remain
7358     ;; the same in case the user is prompted for info, and we don't
7359     ;; want the window conf to change before that...
7360     (gnus-summary-exit t)
7361     (while (not entered)
7362       ;; Then we find what group we are supposed to enter.
7363       (set-buffer gnus-group-buffer)
7364       (gnus-group-jump-to-group current-group)
7365       (setq target-group
7366             (or target-group
7367                 (if (eq gnus-keep-same-level 'best)
7368                     (gnus-summary-best-group gnus-newsgroup-name)
7369                   (gnus-summary-search-group backward gnus-keep-same-level))))
7370       (if (not target-group)
7371           ;; There are no further groups, so we return to the group
7372           ;; buffer.
7373           (progn
7374             (gnus-message 5 "Returning to the group buffer")
7375             (setq entered t)
7376             (when (gnus-buffer-live-p current-buffer)
7377               (set-buffer current-buffer)
7378               (gnus-summary-exit))
7379             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7380         ;; We try to enter the target group.
7381         (gnus-group-jump-to-group target-group)
7382         (let ((unreads (gnus-group-group-unread)))
7383           (if (and (or (eq t unreads)
7384                        (and unreads (not (zerop unreads))))
7385                    (gnus-summary-read-group
7386                     target-group nil no-article
7387                     (and (buffer-name current-buffer) current-buffer)
7388                     nil backward))
7389               (setq entered t)
7390             (setq current-group target-group
7391                   target-group nil)))))))
7392
7393 (defun gnus-summary-prev-group (&optional no-article)
7394   "Exit current newsgroup and then select previous unread newsgroup.
7395 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7396   (interactive "P")
7397   (gnus-summary-next-group no-article nil t))
7398
7399 ;; Walking around summary lines.
7400
7401 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7402   "Go to the first subject satisfying any non-nil constraint.
7403 If UNREAD is non-nil, the article should be unread.
7404 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7405 If UNSEEN is non-nil, the article should be unseen as well as unread.
7406 Returns the article selected or nil if there are no matching articles."
7407   (interactive "P")
7408   (cond
7409    ;; Empty summary.
7410    ((null gnus-newsgroup-data)
7411     (gnus-message 3 "No articles in the group")
7412     nil)
7413    ;; Pick the first article.
7414    ((not (or unread undownloaded unseen))
7415     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7416     (gnus-data-number (car gnus-newsgroup-data)))
7417    ;; Find the first unread article.
7418    (t
7419     (let ((data gnus-newsgroup-data))
7420       (while (and data
7421                   (let ((num (gnus-data-number (car data))))
7422                     (or (memq num gnus-newsgroup-unfetched)
7423                         (not (or (and unread
7424                                       (memq num gnus-newsgroup-unreads))
7425                                  (and undownloaded
7426                                       (memq num gnus-newsgroup-undownloaded))
7427                                  (and unseen
7428                                       (memq num gnus-newsgroup-unseen)
7429                                       (memq num gnus-newsgroup-unreads)))))))
7430         (setq data (cdr data)))
7431       (prog1
7432           (if data
7433               (progn
7434                 (goto-char (gnus-data-pos (car data)))
7435                 (gnus-data-number (car data)))
7436             (gnus-message 3 "No more%s articles"
7437                           (let* ((r (when unread " unread"))
7438                                  (d (when undownloaded " undownloaded"))
7439                                  (s (when unseen " unseen"))
7440                                  (l (delq nil (list r d s))))
7441                             (cond ((= 3 (length l))
7442                                    (concat r "," d ", or" s))
7443                                   ((= 2 (length l))
7444                                    (concat (car l) ", or" (cadr l)))
7445                                   ((= 1 (length l))
7446                                    (car l))
7447                                   (t
7448                                    ""))))
7449             nil
7450             )
7451         (gnus-summary-position-point))))))
7452
7453 (defun gnus-summary-next-subject (n &optional unread dont-display)
7454   "Go to next N'th summary line.
7455 If N is negative, go to the previous N'th subject line.
7456 If UNREAD is non-nil, only unread articles are selected.
7457 The difference between N and the actual number of steps taken is
7458 returned."
7459   (interactive "p")
7460   (let ((backward (< n 0))
7461         (n (abs n)))
7462     (while (and (> n 0)
7463                 (if backward
7464                     (gnus-summary-find-prev unread)
7465                   (gnus-summary-find-next unread)))
7466       (unless (zerop (setq n (1- n)))
7467         (gnus-summary-show-thread)))
7468     (when (/= 0 n)
7469       (gnus-message 7 "No more%s articles"
7470                     (if unread " unread" "")))
7471     (unless dont-display
7472       (gnus-summary-recenter)
7473       (gnus-summary-position-point))
7474     n))
7475
7476 (defun gnus-summary-next-unread-subject (n)
7477   "Go to next N'th unread summary line."
7478   (interactive "p")
7479   (gnus-summary-next-subject n t))
7480
7481 (defun gnus-summary-prev-subject (n &optional unread)
7482   "Go to previous N'th summary line.
7483 If optional argument UNREAD is non-nil, only unread article is selected."
7484   (interactive "p")
7485   (gnus-summary-next-subject (- n) unread))
7486
7487 (defun gnus-summary-prev-unread-subject (n)
7488   "Go to previous N'th unread summary line."
7489   (interactive "p")
7490   (gnus-summary-next-subject (- n) t))
7491
7492 (defun gnus-summary-goto-subjects (articles)
7493   "Insert the subject header for ARTICLES in the current buffer."
7494   (save-excursion
7495     (dolist (article articles)
7496       (gnus-summary-goto-subject article t)))
7497   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7498   (gnus-summary-position-point))
7499
7500 (defun gnus-summary-goto-subject (article &optional force silent)
7501   "Go to the subject line of ARTICLE.
7502 If FORCE, also allow jumping to articles not currently shown."
7503   (interactive "nArticle number: ")
7504   (unless (numberp article)
7505     (error "Article %s is not a number" article))
7506   (let ((b (point))
7507         (data (gnus-data-find article)))
7508     ;; We read in the article if we have to.
7509     (and (not data)
7510          force
7511          (gnus-summary-insert-subject
7512           article
7513           (if (or (numberp force) (vectorp force)) force)
7514           t)
7515          (setq data (gnus-data-find article)))
7516     (goto-char b)
7517     (if (not data)
7518         (progn
7519           (unless silent
7520             (gnus-message 3 "Can't find article %d" article))
7521           nil)
7522       (let ((pt (gnus-data-pos data)))
7523         (goto-char pt)
7524         (gnus-summary-set-article-display-arrow pt))
7525       (gnus-summary-position-point)
7526       article)))
7527
7528 ;; Walking around summary lines with displaying articles.
7529
7530 (defun gnus-summary-expand-window (&optional arg)
7531   "Make the summary buffer take up the entire Emacs frame.
7532 Given a prefix, will force an `article' buffer configuration."
7533   (interactive "P")
7534   (if arg
7535       (gnus-configure-windows 'article 'force)
7536     (gnus-configure-windows 'summary 'force)))
7537
7538 (defun gnus-summary-display-article (article &optional all-header)
7539   "Display ARTICLE in article buffer."
7540   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7541                (with-current-buffer gnus-article-buffer
7542                  (eq major-mode 'gnus-article-mode)))
7543     (gnus-article-setup-buffer))
7544   (gnus-set-global-variables)
7545   (with-current-buffer gnus-article-buffer
7546     (setq gnus-article-charset gnus-newsgroup-charset)
7547     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7548     (mm-enable-multibyte))
7549   (if (null article)
7550       nil
7551     (prog1
7552         (if gnus-summary-display-article-function
7553             (funcall gnus-summary-display-article-function article all-header)
7554           (gnus-article-prepare article all-header))
7555       (gnus-run-hooks 'gnus-select-article-hook)
7556       (when (and gnus-current-article
7557                  (not (zerop gnus-current-article)))
7558         (gnus-summary-goto-subject gnus-current-article))
7559       (gnus-summary-recenter)
7560       (when (and gnus-use-trees gnus-show-threads)
7561         (gnus-possibly-generate-tree article)
7562         (gnus-highlight-selected-tree article))
7563       ;; Successfully display article.
7564       (gnus-article-set-window-start
7565        (cdr (assq article gnus-newsgroup-bookmarks))))))
7566
7567 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7568   "Select the current article.
7569 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7570 non-nil, the article will be re-fetched even if it already present in
7571 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7572 be displayed."
7573   ;; Make sure we are in the summary buffer to work around bbdb bug.
7574   (unless (eq major-mode 'gnus-summary-mode)
7575     (set-buffer gnus-summary-buffer))
7576   (let ((article (or article (gnus-summary-article-number)))
7577         (all-headers (not (not all-headers))) ;Must be t or nil.
7578         gnus-summary-display-article-function)
7579     (and (not pseudo)
7580          (gnus-summary-article-pseudo-p article)
7581          (error "This is a pseudo-article"))
7582     (with-current-buffer gnus-summary-buffer
7583       (if (or (and gnus-single-article-buffer
7584                    (or (null gnus-current-article)
7585                        (null gnus-article-current)
7586                        (null (get-buffer gnus-article-buffer))
7587                        (not (eq article (cdr gnus-article-current)))
7588                        (not (equal (car gnus-article-current)
7589                                    gnus-newsgroup-name))))
7590               (and (not gnus-single-article-buffer)
7591                    (or (null gnus-current-article)
7592                        (not (eq gnus-current-article article))))
7593               force)
7594           ;; The requested article is different from the current article.
7595           (progn
7596             (gnus-summary-display-article article all-headers)
7597             (when (gnus-buffer-live-p gnus-article-buffer)
7598               (with-current-buffer gnus-article-buffer
7599                 (if (not gnus-article-decoded-p) ;; a local variable
7600                     (mm-disable-multibyte))))
7601             (gnus-article-set-window-start
7602              (cdr (assq article gnus-newsgroup-bookmarks)))
7603             article)
7604         'old))))
7605
7606 (defun gnus-summary-force-verify-and-decrypt ()
7607   "Display buttons for signed/encrypted parts and verify/decrypt them."
7608   (interactive)
7609   (let ((mm-verify-option 'known)
7610         (mm-decrypt-option 'known)
7611         (gnus-article-emulate-mime t)
7612         (gnus-buttonized-mime-types (append (list "multipart/signed"
7613                                                   "multipart/encrypted")
7614                                             gnus-buttonized-mime-types)))
7615     (gnus-summary-select-article nil 'force)))
7616
7617 (defun gnus-summary-set-current-mark (&optional current-mark)
7618   "Obsolete function."
7619   nil)
7620
7621 (defun gnus-summary-next-article (&optional unread subject backward push)
7622   "Select the next article.
7623 If UNREAD, only unread articles are selected.
7624 If SUBJECT, only articles with SUBJECT are selected.
7625 If BACKWARD, the previous article is selected instead of the next."
7626   (interactive "P")
7627   ;; Make sure we are in the summary buffer.
7628   (unless (eq major-mode 'gnus-summary-mode)
7629     (set-buffer gnus-summary-buffer))
7630   (cond
7631    ;; Is there such an article?
7632    ((and (gnus-summary-search-forward unread subject backward)
7633          (or (gnus-summary-display-article (gnus-summary-article-number))
7634              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7635     (gnus-summary-position-point))
7636    ;; If not, we try the first unread, if that is wanted.
7637    ((and subject
7638          gnus-auto-select-same
7639          (gnus-summary-first-unread-article))
7640     (gnus-summary-position-point)
7641     (gnus-message 6 "Wrapped"))
7642    ;; Try to get next/previous article not displayed in this group.
7643    ((and gnus-auto-extend-newsgroup
7644          (not unread) (not subject))
7645     (gnus-summary-goto-article
7646      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7647      nil (count-lines (point-min) (point))))
7648    ;; Go to next/previous group.
7649    (t
7650     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7651       (gnus-summary-jump-to-group gnus-newsgroup-name))
7652     (let ((cmd (if (featurep 'xemacs)
7653                    last-command-char
7654                  last-command-event))
7655           (point
7656            (with-current-buffer gnus-group-buffer
7657              (point)))
7658           (group
7659            (if (eq gnus-keep-same-level 'best)
7660                (gnus-summary-best-group gnus-newsgroup-name)
7661              (gnus-summary-search-group backward gnus-keep-same-level))))
7662       ;; For some reason, the group window gets selected.  We change
7663       ;; it back.
7664       (select-window (get-buffer-window (current-buffer)))
7665       ;; Select next unread newsgroup automagically.
7666       (cond
7667        ((or (not gnus-auto-select-next)
7668             (not cmd))
7669         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7670        ((or (eq gnus-auto-select-next 'quietly)
7671             (and (eq gnus-auto-select-next 'slightly-quietly)
7672                  push)
7673             (and (eq gnus-auto-select-next 'almost-quietly)
7674                  (gnus-summary-last-article-p)))
7675         ;; Select quietly.
7676         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7677             (gnus-summary-exit)
7678           (gnus-message 7 "No more%s articles (%s)..."
7679                         (if unread " unread" "")
7680                         (if group (concat "selecting " group)
7681                           "exiting"))
7682           (gnus-summary-next-group nil group backward)))
7683        (t
7684         (when (gnus-key-press-event-p last-input-event)
7685           (gnus-summary-walk-group-buffer
7686            gnus-newsgroup-name cmd unread backward point))))))))
7687
7688 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7689   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7690                       (?\C-p (gnus-group-prev-unread-group 1))))
7691         (cursor-in-echo-area t)
7692         keve key group ended prompt)
7693     (with-current-buffer gnus-group-buffer
7694       (goto-char start)
7695       (setq group
7696             (if (eq gnus-keep-same-level 'best)
7697                 (gnus-summary-best-group gnus-newsgroup-name)
7698               (gnus-summary-search-group backward gnus-keep-same-level))))
7699     (while (not ended)
7700       (setq prompt
7701             (format
7702              "No more%s articles%s " (if unread " unread" "")
7703              (if (and group
7704                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7705                  (format " (Type %s for %s [%s])"
7706                          (single-key-description cmd)
7707                          (gnus-group-decoded-name group)
7708                          (gnus-group-unread group))
7709                (format " (Type %s to exit %s)"
7710                        (single-key-description cmd)
7711                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7712       ;; Confirm auto selection.
7713       (setq key (car (setq keve (gnus-read-event-char prompt)))
7714             ended t)
7715       (cond
7716        ((assq key keystrokes)
7717         (let ((obuf (current-buffer)))
7718           (switch-to-buffer gnus-group-buffer)
7719           (when group
7720             (gnus-group-jump-to-group group))
7721           (eval (cadr (assq key keystrokes)))
7722           (setq group (gnus-group-group-name))
7723           (switch-to-buffer obuf))
7724         (setq ended nil))
7725        ((equal key cmd)
7726         (if (or (not group)
7727                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7728             (gnus-summary-exit)
7729           (gnus-summary-next-group nil group backward)))
7730        (t
7731         (push (cdr keve) unread-command-events))))))
7732
7733 (defun gnus-summary-next-unread-article ()
7734   "Select unread article after current one."
7735   (interactive)
7736   (gnus-summary-next-article
7737    (or (not (eq gnus-summary-goto-unread 'never))
7738        (gnus-summary-last-article-p (gnus-summary-article-number)))
7739    (and gnus-auto-select-same
7740         (gnus-summary-article-subject))))
7741
7742 (defun gnus-summary-prev-article (&optional unread subject)
7743   "Select the article before the current one.
7744 If UNREAD is non-nil, only unread articles are selected."
7745   (interactive "P")
7746   (gnus-summary-next-article unread subject t))
7747
7748 (defun gnus-summary-prev-unread-article ()
7749   "Select unread article before current one."
7750   (interactive)
7751   (gnus-summary-prev-article
7752    (or (not (eq gnus-summary-goto-unread 'never))
7753        (gnus-summary-first-article-p (gnus-summary-article-number)))
7754    (and gnus-auto-select-same
7755         (gnus-summary-article-subject))))
7756
7757 (defun gnus-summary-next-page (&optional lines circular stop)
7758   "Show next page of the selected article.
7759 If at the end of the current article, select the next article.
7760 LINES says how many lines should be scrolled up.
7761
7762 If CIRCULAR is non-nil, go to the start of the article instead of
7763 selecting the next article when reaching the end of the current
7764 article.
7765
7766 If STOP is non-nil, just stop when reaching the end of the message.
7767
7768 Also see the variable `gnus-article-skip-boring'."
7769   (interactive "P")
7770   (setq gnus-summary-buffer (current-buffer))
7771   (gnus-set-global-variables)
7772   (let ((article (gnus-summary-article-number))
7773         (article-window (get-buffer-window gnus-article-buffer t))
7774         endp)
7775     ;; If the buffer is empty, we have no article.
7776     (unless article
7777       (error "No article to select"))
7778     (gnus-configure-windows 'article)
7779     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7780         (if (and (eq gnus-summary-goto-unread 'never)
7781                  (not (gnus-summary-last-article-p article)))
7782             (gnus-summary-next-article)
7783           (gnus-summary-next-unread-article))
7784       (if (or (null gnus-current-article)
7785               (null gnus-article-current)
7786               (/= article (cdr gnus-article-current))
7787               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7788           ;; Selected subject is different from current article's.
7789           (gnus-summary-display-article article)
7790         (when article-window
7791           (gnus-eval-in-buffer-window gnus-article-buffer
7792             (setq endp (or (gnus-article-next-page lines)
7793                            (gnus-article-only-boring-p))))
7794           (when endp
7795             (cond ((or stop gnus-summary-stop-at-end-of-message)
7796                    (gnus-message 3 "End of message"))
7797                   (circular
7798                    (gnus-summary-beginning-of-article))
7799                   (lines
7800                    (gnus-message 3 "End of message"))
7801                   ((null lines)
7802                    (if (and (eq gnus-summary-goto-unread 'never)
7803                             (not (gnus-summary-last-article-p article)))
7804                        (gnus-summary-next-article)
7805                      (gnus-summary-next-unread-article))))))))
7806     (gnus-summary-recenter)
7807     (gnus-summary-position-point)))
7808
7809 (defun gnus-summary-prev-page (&optional lines move)
7810   "Show previous page of selected article.
7811 Argument LINES specifies lines to be scrolled down.
7812 If MOVE, move to the previous unread article if point is at
7813 the beginning of the buffer."
7814   (interactive "P")
7815   (let ((article (gnus-summary-article-number))
7816         (article-window (get-buffer-window gnus-article-buffer t))
7817         endp)
7818     (gnus-configure-windows 'article)
7819     (if (or (null gnus-current-article)
7820             (null gnus-article-current)
7821             (/= article (cdr gnus-article-current))
7822             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7823         ;; Selected subject is different from current article's.
7824         (gnus-summary-display-article article)
7825       (gnus-summary-recenter)
7826       (when article-window
7827         (gnus-eval-in-buffer-window gnus-article-buffer
7828           (setq endp (gnus-article-prev-page lines)))
7829         (when (and move endp)
7830           (cond (lines
7831                  (gnus-message 3 "Beginning of message"))
7832                 ((null lines)
7833                  (if (and (eq gnus-summary-goto-unread 'never)
7834                           (not (gnus-summary-first-article-p article)))
7835                      (gnus-summary-prev-article)
7836                    (gnus-summary-prev-unread-article))))))))
7837   (gnus-summary-position-point))
7838
7839 (defun gnus-summary-prev-page-or-article (&optional lines)
7840   "Show previous page of selected article.
7841 Argument LINES specifies lines to be scrolled down.
7842 If at the beginning of the article, go to the next article."
7843   (interactive "P")
7844   (gnus-summary-prev-page lines t))
7845
7846 (defun gnus-summary-scroll-up (lines)
7847   "Scroll up (or down) one line current article.
7848 Argument LINES specifies lines to be scrolled up (or down if negative)."
7849   (interactive "p")
7850   (gnus-configure-windows 'article)
7851   (gnus-summary-show-thread)
7852   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7853     (gnus-eval-in-buffer-window gnus-article-buffer
7854       (cond ((> lines 0)
7855              (when (gnus-article-next-page lines)
7856                (gnus-message 3 "End of message")))
7857             ((< lines 0)
7858              (gnus-article-prev-page (- lines))))))
7859   (gnus-summary-recenter)
7860   (gnus-summary-position-point))
7861
7862 (defun gnus-summary-scroll-down (lines)
7863   "Scroll down (or up) one line current article.
7864 Argument LINES specifies lines to be scrolled down (or up if negative)."
7865   (interactive "p")
7866   (gnus-summary-scroll-up (- lines)))
7867
7868 (defun gnus-summary-next-same-subject ()
7869   "Select next article which has the same subject as current one."
7870   (interactive)
7871   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7872
7873 (defun gnus-summary-prev-same-subject ()
7874   "Select previous article which has the same subject as current one."
7875   (interactive)
7876   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7877
7878 (defun gnus-summary-next-unread-same-subject ()
7879   "Select next unread article which has the same subject as current one."
7880   (interactive)
7881   (gnus-summary-next-article t (gnus-summary-article-subject)))
7882
7883 (defun gnus-summary-prev-unread-same-subject ()
7884   "Select previous unread article which has the same subject as current one."
7885   (interactive)
7886   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7887
7888 (defun gnus-summary-first-unread-article ()
7889   "Select the first unread article.
7890 Return nil if there are no unread articles."
7891   (interactive)
7892   (prog1
7893       (when (gnus-summary-first-subject t)
7894         (gnus-summary-show-thread)
7895         (gnus-summary-first-subject t)
7896         (gnus-summary-display-article (gnus-summary-article-number)))
7897     (gnus-summary-position-point)))
7898
7899 (defun gnus-summary-first-unread-subject ()
7900   "Place the point on the subject line of the first unread article.
7901 Return nil if there are no unread articles."
7902   (interactive)
7903   (prog1
7904       (when (gnus-summary-first-subject t)
7905         (gnus-summary-show-thread)
7906         (gnus-summary-first-subject t))
7907     (gnus-summary-position-point)))
7908
7909 (defun gnus-summary-first-unseen-subject ()
7910   "Place the point on the subject line of the first unseen article.
7911 Return nil if there are no unseen articles."
7912   (interactive)
7913   (prog1
7914       (when (gnus-summary-first-subject nil nil t)
7915         (gnus-summary-show-thread)
7916         (gnus-summary-first-subject nil nil t))
7917     (gnus-summary-position-point)))
7918
7919 (defun gnus-summary-first-unseen-or-unread-subject ()
7920   "Place the point on the subject line of the first unseen and unread article.
7921 If all article have been seen, on the subject line of the first unread
7922 article."
7923   (interactive)
7924   (prog1
7925       (unless (when (gnus-summary-first-subject nil nil t)
7926                 (gnus-summary-show-thread)
7927                 (gnus-summary-first-subject nil nil t))
7928         (when (gnus-summary-first-subject t)
7929           (gnus-summary-show-thread)
7930           (gnus-summary-first-subject t)))
7931     (gnus-summary-position-point)))
7932
7933 (defun gnus-summary-first-article ()
7934   "Select the first article.
7935 Return nil if there are no articles."
7936   (interactive)
7937   (prog1
7938       (when (gnus-summary-first-subject)
7939         (gnus-summary-show-thread)
7940         (gnus-summary-first-subject)
7941         (gnus-summary-display-article (gnus-summary-article-number)))
7942     (gnus-summary-position-point)))
7943
7944 (defun gnus-summary-best-unread-article (&optional arg)
7945   "Select the unread article with the highest score.
7946 If given a prefix argument, select the next unread article that has a
7947 score higher than the default score."
7948   (interactive "P")
7949   (let ((article (if arg
7950                      (gnus-summary-better-unread-subject)
7951                    (gnus-summary-best-unread-subject))))
7952     (if article
7953         (gnus-summary-goto-article article)
7954       (error "No unread articles"))))
7955
7956 (defun gnus-summary-best-unread-subject ()
7957   "Select the unread subject with the highest score."
7958   (interactive)
7959   (let ((best -1000000)
7960         (data gnus-newsgroup-data)
7961         article score)
7962     (while data
7963       (and (gnus-data-unread-p (car data))
7964            (> (setq score
7965                     (gnus-summary-article-score (gnus-data-number (car data))))
7966               best)
7967            (setq best score
7968                  article (gnus-data-number (car data))))
7969       (setq data (cdr data)))
7970     (when article
7971       (gnus-summary-goto-subject article))
7972     (gnus-summary-position-point)
7973     article))
7974
7975 (defun gnus-summary-better-unread-subject ()
7976   "Select the first unread subject that has a score over the default score."
7977   (interactive)
7978   (let ((data gnus-newsgroup-data)
7979         article score)
7980     (while (and (setq article (gnus-data-number (car data)))
7981                 (or (gnus-data-read-p (car data))
7982                     (not (> (gnus-summary-article-score article)
7983                             gnus-summary-default-score))))
7984       (setq data (cdr data)))
7985     (when article
7986       (gnus-summary-goto-subject article))
7987     (gnus-summary-position-point)
7988     article))
7989
7990 (defun gnus-summary-last-subject ()
7991   "Go to the last displayed subject line in the group."
7992   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7993     (when article
7994       (gnus-summary-goto-subject article))))
7995
7996 (defun gnus-summary-goto-article (article &optional all-headers force)
7997   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7998 If ALL-HEADERS is non-nil, no header lines are hidden.
7999 If FORCE, go to the article even if it isn't displayed.  If FORCE
8000 is a number, it is the line the article is to be displayed on."
8001   (interactive
8002    (list
8003     (completing-read
8004      "Article number or Message-ID: "
8005      (mapcar (lambda (number) (list (int-to-string number)))
8006              gnus-newsgroup-limit))
8007     current-prefix-arg
8008     t))
8009   (prog1
8010       (if (and (stringp article)
8011                (string-match "@\\|%40" article))
8012           (gnus-summary-refer-article article)
8013         (when (stringp article)
8014           (setq article (string-to-number article)))
8015         (if (gnus-summary-goto-subject article force)
8016             (gnus-summary-display-article article all-headers)
8017           (gnus-message 4 "Couldn't go to article %s" article) nil))
8018     (gnus-summary-position-point)))
8019
8020 (defun gnus-summary-goto-last-article ()
8021   "Go to the previously read article."
8022   (interactive)
8023   (prog1
8024       (when gnus-last-article
8025         (gnus-summary-goto-article gnus-last-article nil t))
8026     (gnus-summary-position-point)))
8027
8028 (defun gnus-summary-pop-article (number)
8029   "Pop one article off the history and go to the previous.
8030 NUMBER articles will be popped off."
8031   (interactive "p")
8032   (let (to)
8033     (setq gnus-newsgroup-history
8034           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8035     (if to
8036         (gnus-summary-goto-article (car to) nil t)
8037       (error "Article history empty")))
8038   (gnus-summary-position-point))
8039
8040 ;; Summary commands and functions for limiting the summary buffer.
8041
8042 (defun gnus-summary-limit-to-articles (n)
8043   "Limit the summary buffer to the next N articles.
8044 If not given a prefix, use the process marked articles instead."
8045   (interactive "P")
8046   (prog1
8047       (let ((articles (gnus-summary-work-articles n)))
8048         (setq gnus-newsgroup-processable nil)
8049         (gnus-summary-limit articles))
8050     (gnus-summary-position-point)))
8051
8052 (defun gnus-summary-pop-limit (&optional total)
8053   "Restore the previous limit.
8054 If given a prefix, remove all limits."
8055   (interactive "P")
8056   (when total
8057     (setq gnus-newsgroup-limits
8058           (list (mapcar (lambda (h) (mail-header-number h))
8059                         gnus-newsgroup-headers))))
8060   (unless gnus-newsgroup-limits
8061     (error "No limit to pop"))
8062   (prog1
8063       (gnus-summary-limit nil 'pop)
8064     (gnus-summary-position-point)))
8065
8066 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8067   "Limit the summary buffer to articles that have subjects that match a regexp.
8068 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8069   (interactive
8070    (list (read-string (if current-prefix-arg
8071                           "Exclude subject (regexp): "
8072                         "Limit to subject (regexp): "))
8073          nil current-prefix-arg))
8074   (unless header
8075     (setq header "subject"))
8076   (when (not (equal "" subject))
8077     (prog1
8078         (let ((articles (gnus-summary-find-matching
8079                          (or header "subject") subject 'all nil nil
8080                          not-matching)))
8081           (unless articles
8082             (error "Found no matches for \"%s\"" subject))
8083           (gnus-summary-limit articles))
8084       (gnus-summary-position-point))))
8085
8086 (defun gnus-summary-limit-to-author (from &optional not-matching)
8087   "Limit the summary buffer to articles that have authors that match a regexp.
8088 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8089   (interactive
8090    (list (read-string (if current-prefix-arg
8091                           "Exclude author (regexp): "
8092                         "Limit to author (regexp): "))
8093          current-prefix-arg))
8094   (gnus-summary-limit-to-subject from "from" not-matching))
8095
8096 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8097   "Limit the summary buffer to articles with the given RECIPIENT.
8098
8099 If NOT-MATCHING, exclude RECIPIENT.
8100
8101 To and Cc headers are checked.  You need to include them in
8102 `nnmail-extra-headers'."
8103   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8104   (interactive
8105    (list (read-string (format "%s recipient (regexp): "
8106                               (if current-prefix-arg "Exclude" "Limit to")))
8107          current-prefix-arg))
8108   (when (not (equal "" recipient))
8109     (prog1 (let* ((to
8110                    (if (memq 'To nnmail-extra-headers)
8111                        (gnus-summary-find-matching
8112                         (cons 'extra 'To) recipient 'all nil nil
8113                         not-matching)
8114                      (gnus-message
8115                       1 "`To' isn't present in `nnmail-extra-headers'")
8116                      (sit-for 1)
8117                      nil))
8118                   (cc
8119                    (if (memq 'Cc nnmail-extra-headers)
8120                        (gnus-summary-find-matching
8121                         (cons 'extra 'Cc) recipient 'all nil nil
8122                         not-matching)
8123                      (gnus-message
8124                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8125                      (sit-for 1)
8126                      nil))
8127                   (articles
8128                    (if not-matching
8129                        ;; We need the numbers that are in both lists:
8130                        (mapcar (lambda (a)
8131                                  (and (memq a to) a))
8132                                cc)
8133                      (nconc to cc))))
8134              (unless articles
8135                (error "Found no matches for \"%s\"" recipient))
8136              (gnus-summary-limit articles))
8137       (gnus-summary-position-point))))
8138
8139 (defun gnus-summary-limit-to-address (address &optional not-matching)
8140   "Limit the summary buffer to articles with the given ADDRESS.
8141
8142 If NOT-MATCHING, exclude ADDRESS.
8143
8144 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8145 in `nnmail-extra-headers'."
8146   (interactive
8147    (list (read-string (format "%s address (regexp): "
8148                               (if current-prefix-arg "Exclude" "Limit to")))
8149          current-prefix-arg))
8150   (when (not (equal "" address))
8151     (prog1 (let* ((to
8152                    (if (memq 'To nnmail-extra-headers)
8153                        (gnus-summary-find-matching
8154                         (cons 'extra 'To) address 'all nil nil
8155                         not-matching)
8156                      (gnus-message
8157                       1 "`To' isn't present in `nnmail-extra-headers'")
8158                      (sit-for 1)
8159                      t))
8160                   (cc
8161                    (if (memq 'Cc nnmail-extra-headers)
8162                        (gnus-summary-find-matching
8163                         (cons 'extra 'Cc) address 'all nil nil
8164                         not-matching)
8165                      (gnus-message
8166                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8167                      (sit-for 1)
8168                      t))
8169                   (from
8170                    (gnus-summary-find-matching "from" address
8171                                                'all nil nil not-matching))
8172                   (articles
8173                    (if not-matching
8174                        ;; We need the numbers that are in all lists:
8175                        (if (eq cc t)
8176                            (if (eq to t)
8177                                from
8178                              (mapcar (lambda (a) (car (memq a from))) to))
8179                          (if (eq to t)
8180                              (mapcar (lambda (a) (car (memq a from))) cc)
8181                            (mapcar (lambda (a) (car (memq a from)))
8182                                    (mapcar (lambda (a) (car (memq a to)))
8183                                            cc))))
8184                      (nconc (if (eq to t) nil to)
8185                             (if (eq cc t) nil cc)
8186                             from))))
8187              (unless articles
8188                (error "Found no matches for \"%s\"" address))
8189              (gnus-summary-limit articles))
8190       (gnus-summary-position-point))))
8191
8192 (defun gnus-summary-limit-strange-charsets-predicate (header)
8193   (when (fboundp 'char-charset)
8194     (let ((string (concat (mail-header-subject header)
8195                           (mail-header-from header)))
8196           charset found)
8197       (dotimes (i (1- (length string)))
8198         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8199         (when (string-match "unicode\\|big\\|japanese" charset)
8200           (setq found t)))
8201       found)))
8202
8203 (defun gnus-summary-limit-to-predicate (predicate)
8204   "Limit to articles where PREDICATE returns non-nil.
8205 PREDICATE will be called with the header structures of the
8206 articles."
8207   (let ((articles nil)
8208         (case-fold-search t))
8209     (dolist (header gnus-newsgroup-headers)
8210       (when (funcall predicate header)
8211         (push (mail-header-number header) articles)))
8212     (gnus-summary-limit (nreverse articles))))
8213
8214 (defun gnus-summary-limit-to-age (age &optional younger-p)
8215   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8216 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8217 articles that are younger than AGE days."
8218   (interactive
8219    (let ((younger current-prefix-arg)
8220          (days-got nil)
8221          days)
8222      (while (not days-got)
8223        (setq days (if younger
8224                       (read-string "Limit to articles younger than (in days, older when negative): ")
8225                     (read-string
8226                      "Limit to articles older than (in days, younger when negative): ")))
8227        (when (> (length days) 0)
8228          (setq days (read days)))
8229        (if (numberp days)
8230            (progn
8231              (setq days-got t)
8232              (when (< days 0)
8233                (setq younger (not younger))
8234                (setq days (* days -1))))
8235          (message "Please enter a number.")
8236          (sleep-for 1)))
8237      (list days younger)))
8238   (prog1
8239       (let ((data gnus-newsgroup-data)
8240             (cutoff (days-to-time age))
8241             articles d date is-younger)
8242         (while (setq d (pop data))
8243           (when (and (vectorp (gnus-data-header d))
8244                      (setq date (mail-header-date (gnus-data-header d))))
8245             (setq is-younger (time-less-p
8246                               (time-since (gnus-date-get-time date))
8247                               cutoff))
8248             (when (if younger-p
8249                       is-younger
8250                     (not is-younger))
8251               (push (gnus-data-number d) articles))))
8252         (gnus-summary-limit (nreverse articles)))
8253     (gnus-summary-position-point)))
8254
8255 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8256   "Limit the summary buffer to articles that match an 'extra' header."
8257   (interactive
8258    (let ((header
8259           (intern
8260            (gnus-completing-read-with-default
8261             (symbol-name (car gnus-extra-headers))
8262             (if current-prefix-arg
8263                 "Exclude extra header"
8264               "Limit extra header")
8265             (mapcar (lambda (x)
8266                       (cons (symbol-name x) x))
8267                     gnus-extra-headers)
8268             nil
8269             t))))
8270      (list header
8271            (read-string (format "%s header %s (regexp): "
8272                                 (if current-prefix-arg "Exclude" "Limit to")
8273                                 header))
8274            current-prefix-arg)))
8275   (when (not (equal "" regexp))
8276     (prog1
8277         (let ((articles (gnus-summary-find-matching
8278                          (cons 'extra header) regexp 'all nil nil
8279                          not-matching)))
8280           (unless articles
8281             (error "Found no matches for \"%s\"" regexp))
8282           (gnus-summary-limit articles))
8283       (gnus-summary-position-point))))
8284
8285 (defun gnus-summary-limit-to-display-predicate ()
8286   "Limit the summary buffer to the predicated in the `display' group parameter."
8287   (interactive)
8288   (unless gnus-newsgroup-display
8289     (error "There is no `display' group parameter"))
8290   (let (articles)
8291     (dolist (number gnus-newsgroup-articles)
8292       (when (funcall gnus-newsgroup-display)
8293         (push number articles)))
8294     (gnus-summary-limit articles))
8295   (gnus-summary-position-point))
8296
8297 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8298 (make-obsolete
8299  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8300
8301 (defun gnus-summary-limit-to-unread (&optional all)
8302   "Limit the summary buffer to articles that are not marked as read.
8303 If ALL is non-nil, limit strictly to unread articles."
8304   (interactive "P")
8305   (if all
8306       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8307     (gnus-summary-limit-to-marks
8308      ;; Concat all the marks that say that an article is read and have
8309      ;; those removed.
8310      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8311            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8312            gnus-low-score-mark gnus-expirable-mark
8313            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8314            gnus-duplicate-mark)
8315      'reverse)))
8316
8317 (defun gnus-summary-limit-to-headers (match &optional reverse)
8318   "Limit the summary buffer to articles that have headers that match MATCH.
8319 If REVERSE (the prefix), limit to articles that don't match."
8320   (interactive "sMatch headers (regexp): \nP")
8321   (gnus-summary-limit-to-bodies match reverse t))
8322
8323 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8324   "Limit the summary buffer to articles that have bodies that match MATCH.
8325 If REVERSE (the prefix), limit to articles that don't match."
8326   (interactive "sMatch body (regexp): \nP")
8327   (let ((articles nil)
8328         (gnus-select-article-hook nil)  ;Disable hook.
8329         (gnus-article-prepare-hook nil)
8330         (gnus-use-article-prefetch nil)
8331         (gnus-keep-backlog nil)
8332         (gnus-break-pages nil)
8333         (gnus-summary-display-arrow nil)
8334         (gnus-updated-mode-lines nil)
8335         (gnus-auto-center-summary nil)
8336         (gnus-display-mime-function nil))
8337     (dolist (data gnus-newsgroup-data)
8338       (let (gnus-mark-article-hook)
8339         (gnus-summary-select-article t t nil (gnus-data-number data)))
8340       (with-current-buffer gnus-article-buffer
8341         (article-goto-body)
8342         (let* ((case-fold-search t)
8343                (found (if headersp
8344                           (re-search-backward match nil t)
8345                         (re-search-forward match nil t))))
8346           (when (or (and found
8347                          (not reverse))
8348                     (and (not found)
8349                          reverse))
8350             (push (gnus-data-number data) articles)))))
8351     (if (not articles)
8352         (message "No messages matched")
8353       (gnus-summary-limit articles)))
8354   (gnus-summary-position-point))
8355
8356 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8357   "Limit the summary buffer to articles that aren't part on any thread.
8358 If THREADSP (the prefix), limit to articles that are in threads."
8359   (interactive "P")
8360   (let ((articles nil)
8361         thread-articles
8362         threads)
8363     (dolist (thread gnus-newsgroup-threads)
8364       (if (stringp (car thread))
8365           (dolist (thread (cdr thread))
8366             (push thread threads))
8367         (push thread threads)))
8368     (dolist (thread threads)
8369       (setq thread-articles (gnus-articles-in-thread thread))
8370       (when (or (and threadsp
8371                      (> (length thread-articles) 1))
8372                 (and (not threadsp)
8373                      (= (length thread-articles) 1)))
8374         (setq articles (nconc thread-articles articles))))
8375     (if (not articles)
8376         (message "No messages matched")
8377       (gnus-summary-limit articles))
8378     (gnus-summary-position-point)))
8379
8380 (defun gnus-summary-limit-to-replied (&optional unreplied)
8381   "Limit the summary buffer to replied articles.
8382 If UNREPLIED (the prefix), limit to unreplied articles."
8383   (interactive "P")
8384   (if unreplied
8385       (gnus-summary-limit
8386        (gnus-set-difference gnus-newsgroup-articles
8387         gnus-newsgroup-replied))
8388     (gnus-summary-limit gnus-newsgroup-replied))
8389   (gnus-summary-position-point))
8390
8391 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8392 (make-obsolete 'gnus-summary-delete-marked-with
8393                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8394
8395 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8396   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8397 If REVERSE, limit the summary buffer to articles that are marked
8398 with MARKS.  MARKS can either be a string of marks or a list of marks.
8399 Returns how many articles were removed."
8400   (interactive "sMarks: ")
8401   (gnus-summary-limit-to-marks marks t))
8402
8403 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8404   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8405 If REVERSE (the prefix), limit the summary buffer to articles that are
8406 not marked with MARKS.  MARKS can either be a string of marks or a
8407 list of marks.
8408 Returns how many articles were removed."
8409   (interactive "sMarks: \nP")
8410   (prog1
8411       (let ((data gnus-newsgroup-data)
8412             (marks (if (listp marks) marks
8413                      (append marks nil))) ; Transform to list.
8414             articles)
8415         (while data
8416           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8417                   (memq (gnus-data-mark (car data)) marks))
8418             (push (gnus-data-number (car data)) articles))
8419           (setq data (cdr data)))
8420         (gnus-summary-limit articles))
8421     (gnus-summary-position-point)))
8422
8423 (defun gnus-summary-limit-to-score (score)
8424   "Limit to articles with score at or above SCORE."
8425   (interactive "NLimit to articles with score of at least: ")
8426   (let ((data gnus-newsgroup-data)
8427         articles)
8428     (while data
8429       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8430                 score)
8431         (push (gnus-data-number (car data)) articles))
8432       (setq data (cdr data)))
8433     (prog1
8434         (gnus-summary-limit articles)
8435       (gnus-summary-position-point))))
8436
8437 (defun gnus-summary-limit-to-unseen ()
8438   "Limit to unseen articles."
8439   (interactive)
8440   (prog1
8441       (gnus-summary-limit gnus-newsgroup-unseen)
8442     (gnus-summary-position-point)))
8443
8444 (defun gnus-summary-limit-include-thread (id)
8445   "Display all the hidden articles that is in the thread with ID in it.
8446 When called interactively, ID is the Message-ID of the current
8447 article."
8448   (interactive (list (mail-header-id (gnus-summary-article-header))))
8449   (let ((articles (gnus-articles-in-thread
8450                    (gnus-id-to-thread (gnus-root-id id)))))
8451     (prog1
8452         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8453       (gnus-summary-limit-include-matching-articles
8454        "subject"
8455        (regexp-quote (gnus-simplify-subject-re
8456                       (mail-header-subject (gnus-id-to-header id)))))
8457       (gnus-summary-position-point))))
8458
8459 (defun gnus-summary-limit-include-matching-articles (header regexp)
8460   "Display all the hidden articles that have HEADERs that match REGEXP."
8461   (interactive (list (read-string "Match on header: ")
8462                      (read-string "Regexp: ")))
8463   (let ((articles (gnus-find-matching-articles header regexp)))
8464     (prog1
8465         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8466       (gnus-summary-position-point))))
8467
8468 (defun gnus-summary-insert-dormant-articles ()
8469   "Insert all the dormant articles for this group into the current buffer."
8470   (interactive)
8471   (let ((gnus-verbose (max 6 gnus-verbose)))
8472     (if (not gnus-newsgroup-dormant)
8473         (gnus-message 3 "No dormant articles for this group")
8474       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8475
8476 (defun gnus-summary-insert-ticked-articles ()
8477   "Insert ticked articles for this group into the current buffer."
8478   (interactive)
8479   (let ((gnus-verbose (max 6 gnus-verbose)))
8480     (if (not gnus-newsgroup-marked)
8481         (gnus-message 3 "No ticked articles for this group")
8482       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8483
8484 (defun gnus-summary-limit-include-dormant ()
8485   "Display all the hidden articles that are marked as dormant.
8486 Note that this command only works on a subset of the articles currently
8487 fetched for this group."
8488   (interactive)
8489   (unless gnus-newsgroup-dormant
8490     (error "There are no dormant articles in this group"))
8491   (prog1
8492       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8493     (gnus-summary-position-point)))
8494
8495 (defun gnus-summary-limit-exclude-dormant ()
8496   "Hide all dormant articles."
8497   (interactive)
8498   (prog1
8499       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8500     (gnus-summary-position-point)))
8501
8502 (defun gnus-summary-limit-exclude-childless-dormant ()
8503   "Hide all dormant articles that have no children."
8504   (interactive)
8505   (let ((data (gnus-data-list t))
8506         articles d children)
8507     ;; Find all articles that are either not dormant or have
8508     ;; children.
8509     (while (setq d (pop data))
8510       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8511                 (and (setq children
8512                            (gnus-article-children (gnus-data-number d)))
8513                      (let (found)
8514                        (while children
8515                          (when (memq (car children) articles)
8516                            (setq children nil
8517                                  found t))
8518                          (pop children))
8519                        found)))
8520         (push (gnus-data-number d) articles)))
8521     ;; Do the limiting.
8522     (prog1
8523         (gnus-summary-limit articles)
8524       (gnus-summary-position-point))))
8525
8526 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8527   "Mark all unread excluded articles as read.
8528 If ALL, mark even excluded ticked and dormants as read."
8529   (interactive "P")
8530   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8531   (let ((articles (gnus-sorted-ndifference
8532                    (sort
8533                     (mapcar (lambda (h) (mail-header-number h))
8534                             gnus-newsgroup-headers)
8535                     '<)
8536                    gnus-newsgroup-limit))
8537         article)
8538     (setq gnus-newsgroup-unreads
8539           (gnus-sorted-intersection gnus-newsgroup-unreads
8540                                     gnus-newsgroup-limit))
8541     (if all
8542         (setq gnus-newsgroup-dormant nil
8543               gnus-newsgroup-marked nil
8544               gnus-newsgroup-reads
8545               (nconc
8546                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8547                gnus-newsgroup-reads))
8548       (while (setq article (pop articles))
8549         (unless (or (memq article gnus-newsgroup-dormant)
8550                     (memq article gnus-newsgroup-marked))
8551           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8552
8553 (defun gnus-summary-limit (articles &optional pop)
8554   (if pop
8555       ;; We pop the previous limit off the stack and use that.
8556       (setq articles (car gnus-newsgroup-limits)
8557             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8558     ;; We use the new limit, so we push the old limit on the stack.
8559     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8560   ;; Set the limit.
8561   (setq gnus-newsgroup-limit articles)
8562   (let ((total (length gnus-newsgroup-data))
8563         (data (gnus-data-find-list (gnus-summary-article-number)))
8564         (gnus-summary-mark-below nil)   ; Inhibit this.
8565         found)
8566     ;; This will do all the work of generating the new summary buffer
8567     ;; according to the new limit.
8568     (gnus-summary-prepare)
8569     ;; Hide any threads, possibly.
8570     (gnus-summary-maybe-hide-threads)
8571     ;; Try to return to the article you were at, or one in the
8572     ;; neighborhood.
8573     (when data
8574       ;; We try to find some article after the current one.
8575       (while data
8576         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8577           (setq data nil
8578                 found t))
8579         (setq data (cdr data))))
8580     (unless found
8581       ;; If there is no data, that means that we were after the last
8582       ;; article.  The same goes when we can't find any articles
8583       ;; after the current one.
8584       (goto-char (point-max))
8585       (gnus-summary-find-prev))
8586     (gnus-set-mode-line 'summary)
8587     ;; We return how many articles were removed from the summary
8588     ;; buffer as a result of the new limit.
8589     (- total (length gnus-newsgroup-data))))
8590
8591 (defsubst gnus-invisible-cut-children (threads)
8592   (let ((num 0))
8593     (while threads
8594       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8595         (incf num))
8596       (pop threads))
8597     (< num 2)))
8598
8599 (defsubst gnus-cut-thread (thread)
8600   "Go forwards in the thread until we find an article that we want to display."
8601   (when (or (eq gnus-fetch-old-headers 'some)
8602             (eq gnus-fetch-old-headers 'invisible)
8603             (numberp gnus-fetch-old-headers)
8604             (eq gnus-build-sparse-threads 'some)
8605             (eq gnus-build-sparse-threads 'more))
8606     ;; Deal with old-fetched headers and sparse threads.
8607     (while (and
8608             thread
8609             (or
8610              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8611              (gnus-summary-article-ancient-p
8612               (mail-header-number (car thread))))
8613             (if (or (<= (length (cdr thread)) 1)
8614                     (eq gnus-fetch-old-headers 'invisible))
8615                 (setq gnus-newsgroup-limit
8616                       (delq (mail-header-number (car thread))
8617                             gnus-newsgroup-limit)
8618                       thread (cadr thread))
8619               (when (gnus-invisible-cut-children (cdr thread))
8620                 (let ((th (cdr thread)))
8621                   (while th
8622                     (if (memq (mail-header-number (caar th))
8623                               gnus-newsgroup-limit)
8624                         (setq thread (car th)
8625                               th nil)
8626                       (setq th (cdr th))))))))))
8627   thread)
8628
8629 (defun gnus-cut-threads (threads)
8630   "Cut off all uninteresting articles from the beginning of THREADS."
8631   (when (or (eq gnus-fetch-old-headers 'some)
8632             (eq gnus-fetch-old-headers 'invisible)
8633             (numberp gnus-fetch-old-headers)
8634             (eq gnus-build-sparse-threads 'some)
8635             (eq gnus-build-sparse-threads 'more))
8636     (let ((th threads))
8637       (while th
8638         (setcar th (gnus-cut-thread (car th)))
8639         (setq th (cdr th)))))
8640   ;; Remove nixed out threads.
8641   (delq nil threads))
8642
8643 (defun gnus-summary-initial-limit (&optional show-if-empty)
8644   "Figure out what the initial limit is supposed to be on group entry.
8645 This entails weeding out unwanted dormants, low-scored articles,
8646 fetch-old-headers verbiage, and so on."
8647   ;; Most groups have nothing to remove.
8648   (unless (or gnus-inhibit-limiting
8649               (and (null gnus-newsgroup-dormant)
8650                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8651                    (not (eq gnus-fetch-old-headers 'some))
8652                    (not (numberp gnus-fetch-old-headers))
8653                    (not (eq gnus-fetch-old-headers 'invisible))
8654                    (null gnus-summary-expunge-below)
8655                    (not (eq gnus-build-sparse-threads 'some))
8656                    (not (eq gnus-build-sparse-threads 'more))
8657                    (null gnus-thread-expunge-below)
8658                    (not gnus-use-nocem)))
8659     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8660     (setq gnus-newsgroup-limit nil)
8661     (mapatoms
8662      (lambda (node)
8663        (unless (car (symbol-value node))
8664          ;; These threads have no parents -- they are roots.
8665          (let ((nodes (cdr (symbol-value node)))
8666                thread)
8667            (while nodes
8668              (if (and gnus-thread-expunge-below
8669                       (< (gnus-thread-total-score (car nodes))
8670                          gnus-thread-expunge-below))
8671                  (gnus-expunge-thread (pop nodes))
8672                (setq thread (pop nodes))
8673                (gnus-summary-limit-children thread))))))
8674      gnus-newsgroup-dependencies)
8675     ;; If this limitation resulted in an empty group, we might
8676     ;; pop the previous limit and use it instead.
8677     (when (and (not gnus-newsgroup-limit)
8678                show-if-empty)
8679       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8680     gnus-newsgroup-limit))
8681
8682 (defun gnus-summary-limit-children (thread)
8683   "Return 1 if this subthread is visible and 0 if it is not."
8684   ;; First we get the number of visible children to this thread.  This
8685   ;; is done by recursing down the thread using this function, so this
8686   ;; will really go down to a leaf article first, before slowly
8687   ;; working its way up towards the root.
8688   (when thread
8689     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8690            (children
8691            (if (cdr thread)
8692                (apply '+ (mapcar 'gnus-summary-limit-children
8693                                  (cdr thread)))
8694              0))
8695           (number (mail-header-number (car thread)))
8696           score)
8697       (if (and
8698            (not (memq number gnus-newsgroup-marked))
8699            (or
8700             ;; If this article is dormant and has absolutely no visible
8701             ;; children, then this article isn't visible.
8702             (and (memq number gnus-newsgroup-dormant)
8703                  (zerop children))
8704             ;; If this is "fetch-old-headered" and there is no
8705             ;; visible children, then we don't want this article.
8706             (and (or (eq gnus-fetch-old-headers 'some)
8707                      (numberp gnus-fetch-old-headers))
8708                  (gnus-summary-article-ancient-p number)
8709                  (zerop children))
8710             ;; If this is "fetch-old-headered" and `invisible', then
8711             ;; we don't want this article.
8712             (and (eq gnus-fetch-old-headers 'invisible)
8713                  (gnus-summary-article-ancient-p number))
8714             ;; If this is a sparsely inserted article with no children,
8715             ;; we don't want it.
8716             (and (eq gnus-build-sparse-threads 'some)
8717                  (gnus-summary-article-sparse-p number)
8718                  (zerop children))
8719             ;; If we use expunging, and this article is really
8720             ;; low-scored, then we don't want this article.
8721             (when (and gnus-summary-expunge-below
8722                        (< (setq score
8723                                 (or (cdr (assq number gnus-newsgroup-scored))
8724                                     gnus-summary-default-score))
8725                           gnus-summary-expunge-below))
8726               ;; We increase the expunge-tally here, but that has
8727               ;; nothing to do with the limits, really.
8728               (incf gnus-newsgroup-expunged-tally)
8729               ;; We also mark as read here, if that's wanted.
8730               (when (and gnus-summary-mark-below
8731                          (< score gnus-summary-mark-below))
8732                 (setq gnus-newsgroup-unreads
8733                       (delq number gnus-newsgroup-unreads))
8734                 (if gnus-newsgroup-auto-expire
8735                     (push number gnus-newsgroup-expirable)
8736                   (push (cons number gnus-low-score-mark)
8737                         gnus-newsgroup-reads)))
8738               t)
8739             ;; Do the `display' group parameter.
8740             (and gnus-newsgroup-display
8741                  (not (funcall gnus-newsgroup-display)))
8742             ;; Check NoCeM things.
8743             (when (and gnus-use-nocem
8744                        (gnus-nocem-unwanted-article-p
8745                         (mail-header-id (car thread))))
8746               (setq gnus-newsgroup-unreads
8747                     (delq number gnus-newsgroup-unreads))
8748               t)))
8749           ;; Nope, invisible article.
8750           0
8751         ;; Ok, this article is to be visible, so we add it to the limit
8752         ;; and return 1.
8753         (push number gnus-newsgroup-limit)
8754         1))))
8755
8756 (defun gnus-expunge-thread (thread)
8757   "Mark all articles in THREAD as read."
8758   (let* ((number (mail-header-number (car thread))))
8759     (incf gnus-newsgroup-expunged-tally)
8760     ;; We also mark as read here, if that's wanted.
8761     (setq gnus-newsgroup-unreads
8762           (delq number gnus-newsgroup-unreads))
8763     (if gnus-newsgroup-auto-expire
8764         (push number gnus-newsgroup-expirable)
8765       (push (cons number gnus-low-score-mark)
8766             gnus-newsgroup-reads)))
8767   ;; Go recursively through all subthreads.
8768   (mapcar 'gnus-expunge-thread (cdr thread)))
8769
8770 ;; Summary article oriented commands
8771
8772 (defun gnus-summary-refer-parent-article (n)
8773   "Refer parent article N times.
8774 If N is negative, go to ancestor -N instead.
8775 The difference between N and the number of articles fetched is returned."
8776   (interactive "p")
8777   (let ((skip 1)
8778         error header ref)
8779     (when (not (natnump n))
8780       (setq skip (abs n)
8781             n 1))
8782     (while (and (> n 0)
8783                 (not error))
8784       (setq header (gnus-summary-article-header))
8785       (if (and (eq (mail-header-number header)
8786                    (cdr gnus-article-current))
8787                (equal gnus-newsgroup-name
8788                       (car gnus-article-current)))
8789           ;; If we try to find the parent of the currently
8790           ;; displayed article, then we take a look at the actual
8791           ;; References header, since this is slightly more
8792           ;; reliable than the References field we got from the
8793           ;; server.
8794           (with-current-buffer gnus-original-article-buffer
8795             (nnheader-narrow-to-headers)
8796             (unless (setq ref (message-fetch-field "references"))
8797               (when (setq ref (message-fetch-field "in-reply-to"))
8798                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8799             (widen))
8800         (setq ref
8801               ;; It's not the current article, so we take a bet on
8802               ;; the value we got from the server.
8803               (mail-header-references header)))
8804       (if (and ref
8805                (not (equal ref "")))
8806           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8807             (gnus-message 1 "Couldn't find parent"))
8808         (gnus-message 1 "No references in article %d"
8809                       (gnus-summary-article-number))
8810         (setq error t))
8811       (decf n))
8812     (gnus-summary-position-point)
8813     n))
8814
8815 (defun gnus-summary-refer-references ()
8816   "Fetch all articles mentioned in the References header.
8817 Return the number of articles fetched."
8818   (interactive)
8819   (let ((ref (mail-header-references (gnus-summary-article-header)))
8820         (current (gnus-summary-article-number))
8821         (n 0))
8822     (if (or (not ref)
8823             (equal ref ""))
8824         (error "No References in the current article")
8825       ;; For each Message-ID in the References header...
8826       (while (string-match "<[^>]*>" ref)
8827         (incf n)
8828         ;; ... fetch that article.
8829         (gnus-summary-refer-article
8830          (prog1 (match-string 0 ref)
8831            (setq ref (substring ref (match-end 0))))))
8832       (gnus-summary-goto-subject current)
8833       (gnus-summary-position-point)
8834       n)))
8835
8836 (defun gnus-summary-refer-thread (&optional limit)
8837   "Fetch all articles in the current thread.
8838 If LIMIT (the numerical prefix), fetch that many old headers instead
8839 of what's specified by the `gnus-refer-thread-limit' variable."
8840   (interactive "P")
8841   (let ((id (mail-header-id (gnus-summary-article-header)))
8842         (limit (if limit (prefix-numeric-value limit)
8843                  gnus-refer-thread-limit)))
8844     (unless (eq gnus-fetch-old-headers 'invisible)
8845       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8846       ;; Retrieve the headers and read them in.
8847       (if (eq (if (numberp limit)
8848                   (gnus-retrieve-headers
8849                    (list (min
8850                           (+ (mail-header-number
8851                               (gnus-summary-article-header))
8852                              limit)
8853                           gnus-newsgroup-end))
8854                    gnus-newsgroup-name (* limit 2))
8855                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8856                 ;; headers.
8857                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8858                                        gnus-newsgroup-name limit))
8859               'nov)
8860           (gnus-build-all-threads)
8861         (error "Can't fetch thread from back ends that don't support NOV"))
8862       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8863     (gnus-summary-limit-include-thread id)))
8864
8865 (defun gnus-summary-refer-article (message-id)
8866   "Fetch an article specified by MESSAGE-ID."
8867   (interactive "sMessage-ID: ")
8868   (when (and (stringp message-id)
8869              (not (zerop (length message-id))))
8870     (setq message-id (gnus-replace-in-string message-id " " ""))
8871     ;; Construct the correct Message-ID if necessary.
8872     ;; Suggested by tale@pawl.rpi.edu.
8873     (unless (string-match "^<" message-id)
8874       (setq message-id (concat "<" message-id)))
8875     (unless (string-match ">$" message-id)
8876       (setq message-id (concat message-id ">")))
8877     ;; People often post MIDs from URLs, so unhex it:
8878     (unless (string-match "@" message-id)
8879       (setq message-id (gnus-url-unhex-string message-id)))
8880     (let* ((header (gnus-id-to-header message-id))
8881            (sparse (and header
8882                         (gnus-summary-article-sparse-p
8883                          (mail-header-number header))
8884                         (memq (mail-header-number header)
8885                               gnus-newsgroup-limit)))
8886            number)
8887       (cond
8888        ;; If the article is present in the buffer we just go to it.
8889        ((and header
8890              (or (not (gnus-summary-article-sparse-p
8891                        (mail-header-number header)))
8892                  sparse))
8893         (prog1
8894             (gnus-summary-goto-article
8895              (mail-header-number header) nil t)
8896           (when sparse
8897             (gnus-summary-update-article (mail-header-number header)))))
8898        (t
8899         ;; We fetch the article.
8900         (catch 'found
8901           (dolist (gnus-override-method (gnus-refer-article-methods))
8902             (when (and (gnus-check-server gnus-override-method)
8903                        ;; Fetch the header,
8904                        (setq number (gnus-summary-insert-subject message-id)))
8905               ;; and display the article.
8906               (gnus-summary-select-article nil nil nil number)
8907               (throw 'found t)))
8908           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8909
8910 (defun gnus-refer-article-methods ()
8911   "Return a list of referable methods."
8912   (cond
8913    ;; No method, so we default to current and native.
8914    ((null gnus-refer-article-method)
8915     (list gnus-current-select-method gnus-select-method))
8916    ;; Current.
8917    ((eq 'current gnus-refer-article-method)
8918     (list gnus-current-select-method))
8919    ;; List of select methods.
8920    ((not (and (symbolp (car gnus-refer-article-method))
8921               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8922     (let (out)
8923       (dolist (method gnus-refer-article-method)
8924         (push (if (eq 'current method)
8925                   gnus-current-select-method
8926                 method)
8927               out))
8928       (nreverse out)))
8929    ;; One single select method.
8930    (t
8931     (list gnus-refer-article-method))))
8932
8933 (defun gnus-summary-edit-parameters ()
8934   "Edit the group parameters of the current group."
8935   (interactive)
8936   (gnus-group-edit-group gnus-newsgroup-name 'params))
8937
8938 (defun gnus-summary-customize-parameters ()
8939   "Customize the group parameters of the current group."
8940   (interactive)
8941   (gnus-group-customize gnus-newsgroup-name))
8942
8943 (defun gnus-summary-enter-digest-group (&optional force)
8944   "Enter an nndoc group based on the current article.
8945 If FORCE, force a digest interpretation.  If not, try
8946 to guess what the document format is."
8947   (interactive "P")
8948   (let ((conf gnus-current-window-configuration))
8949     (save-window-excursion
8950       (save-excursion
8951         (let (gnus-article-prepare-hook
8952               gnus-display-mime-function
8953               gnus-break-pages)
8954           (gnus-summary-select-article))))
8955     (setq gnus-current-window-configuration conf)
8956     (let* ((name (format "%s-%d"
8957                          (gnus-group-prefixed-name
8958                           gnus-newsgroup-name (list 'nndoc ""))
8959                          (with-current-buffer gnus-summary-buffer
8960                            gnus-current-article)))
8961            (ogroup gnus-newsgroup-name)
8962            (params (append (gnus-info-params (gnus-get-info ogroup))
8963                            (list (cons 'to-group ogroup))
8964                            (list (cons 'parent-group ogroup))
8965                            (list (cons 'save-article-group ogroup))))
8966            (case-fold-search t)
8967            (buf (current-buffer))
8968            dig to-address)
8969       (with-current-buffer gnus-original-article-buffer
8970         ;; Have the digest group inherit the main mail address of
8971         ;; the parent article.
8972         (when (setq to-address (or (gnus-fetch-field "reply-to")
8973                                    (gnus-fetch-field "from")))
8974           (setq params
8975                 (append
8976                  (list (cons 'to-address
8977                              (funcall gnus-decode-encoded-address-function
8978                                       to-address))))))
8979         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8980         (insert-buffer-substring gnus-original-article-buffer)
8981         ;; Remove lines that may lead nndoc to misinterpret the
8982         ;; document type.
8983         (narrow-to-region
8984          (goto-char (point-min))
8985          (or (search-forward "\n\n" nil t) (point)))
8986         (goto-char (point-min))
8987         (delete-matching-lines "^Path:\\|^From ")
8988         (widen))
8989       (unwind-protect
8990           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8991                     (gnus-newsgroup-ephemeral-ignored-charsets
8992                      gnus-newsgroup-ignored-charsets))
8993                 (gnus-group-read-ephemeral-group
8994                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8995                               (nndoc-article-type
8996                                ,(if force 'mbox 'guess)))
8997                  t nil nil nil
8998                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8999                                                         "ADAPT")))))
9000               ;; Make all postings to this group go to the parent group.
9001               (nconc (gnus-info-params (gnus-get-info name))
9002                      params)
9003             ;; Couldn't select this doc group.
9004             (switch-to-buffer buf)
9005             (gnus-set-global-variables)
9006             (gnus-configure-windows 'summary)
9007             (gnus-message 3 "Article couldn't be entered?"))
9008         (kill-buffer dig)))))
9009
9010 (defun gnus-summary-read-document (n)
9011   "Open a new group based on the current article(s).
9012 This will allow you to read digests and other similar
9013 documents as newsgroups.
9014 Obeys the standard process/prefix convention."
9015   (interactive "P")
9016   (let* ((ogroup gnus-newsgroup-name)
9017          (params (append (gnus-info-params (gnus-get-info ogroup))
9018                          (list (cons 'to-group ogroup))))
9019          group egroup groups vgroup)
9020     (dolist (article (gnus-summary-work-articles n))
9021       (setq group (format "%s-%d" gnus-newsgroup-name article))
9022       (gnus-summary-remove-process-mark article)
9023       (when (gnus-summary-display-article article)
9024         (save-excursion ;;What for?
9025           (with-temp-buffer
9026             (insert-buffer-substring gnus-original-article-buffer)
9027             ;; Remove some headers that may lead nndoc to make
9028             ;; the wrong guess.
9029             (message-narrow-to-head)
9030             (goto-char (point-min))
9031             (delete-matching-lines "^Path:\\|^From ")
9032             (widen)
9033             (if (setq egroup
9034                       (gnus-group-read-ephemeral-group
9035                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9036                                      (nndoc-article-type guess))
9037                        t nil t))
9038                 (progn
9039                   ;; Make all postings to this group go to the parent group.
9040                   (nconc (gnus-info-params (gnus-get-info egroup))
9041                          params)
9042                   (push egroup groups))
9043               ;; Couldn't select this doc group.
9044               (gnus-error 3 "Article couldn't be entered"))))))
9045     ;; Now we have selected all the documents.
9046     (cond
9047      ((not groups)
9048       (error "None of the articles could be interpreted as documents"))
9049      ((gnus-group-read-ephemeral-group
9050        (setq vgroup (format
9051                      "nnvirtual:%s-%s" gnus-newsgroup-name
9052                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9053        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9054        t
9055        (cons (current-buffer) 'summary)))
9056      (t
9057       (error "Couldn't select virtual nndoc group")))))
9058
9059 (defun gnus-summary-isearch-article (&optional regexp-p)
9060   "Do incremental search forward on the current article.
9061 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9062   (interactive "P")
9063   (gnus-summary-select-article)
9064   (gnus-configure-windows 'article)
9065   (gnus-eval-in-buffer-window gnus-article-buffer
9066     (save-restriction
9067       (widen)
9068       (isearch-forward regexp-p))))
9069
9070 (defun gnus-summary-repeat-search-article-forward ()
9071   "Repeat the previous search forwards."
9072   (interactive)
9073   (unless gnus-last-search-regexp
9074     (error "No previous search"))
9075   (gnus-summary-search-article-forward gnus-last-search-regexp))
9076
9077 (defun gnus-summary-repeat-search-article-backward ()
9078   "Repeat the previous search backwards."
9079   (interactive)
9080   (unless gnus-last-search-regexp
9081     (error "No previous search"))
9082   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9083
9084 (defun gnus-summary-search-article-forward (regexp &optional backward)
9085   "Search for an article containing REGEXP forward.
9086 If BACKWARD, search backward instead."
9087   (interactive
9088    (list (read-string
9089           (format "Search article %s (regexp%s): "
9090                   (if current-prefix-arg "backward" "forward")
9091                   (if gnus-last-search-regexp
9092                       (concat ", default " gnus-last-search-regexp)
9093                     "")))
9094          current-prefix-arg))
9095   (if (string-equal regexp "")
9096       (setq regexp (or gnus-last-search-regexp ""))
9097     (setq gnus-last-search-regexp regexp)
9098     (setq gnus-article-before-search gnus-current-article))
9099   ;; Intentionally set gnus-last-article.
9100   (setq gnus-last-article gnus-article-before-search)
9101   (let ((gnus-last-article gnus-last-article))
9102     (if (gnus-summary-search-article regexp backward)
9103         (gnus-summary-show-thread)
9104       (signal 'search-failed (list regexp)))))
9105
9106 (defun gnus-summary-search-article-backward (regexp)
9107   "Search for an article containing REGEXP backward."
9108   (interactive
9109    (list (read-string
9110           (format "Search article backward (regexp%s): "
9111                   (if gnus-last-search-regexp
9112                       (concat ", default " gnus-last-search-regexp)
9113                     "")))))
9114   (gnus-summary-search-article-forward regexp 'backward))
9115
9116 (defun gnus-summary-search-article (regexp &optional backward)
9117   "Search for an article containing REGEXP.
9118 Optional argument BACKWARD means do search for backward.
9119 `gnus-select-article-hook' is not called during the search."
9120   ;; We have to require this here to make sure that the following
9121   ;; dynamic binding isn't shadowed by autoloading.
9122   (require 'gnus-async)
9123   (require 'gnus-art)
9124   (let ((gnus-select-article-hook nil)  ;Disable hook.
9125         (gnus-article-prepare-hook nil)
9126         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9127         (gnus-use-article-prefetch nil)
9128         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9129         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9130         (gnus-visual nil)
9131         (gnus-keep-backlog nil)
9132         (gnus-break-pages nil)
9133         (gnus-summary-display-arrow nil)
9134         (gnus-updated-mode-lines nil)
9135         (gnus-auto-center-summary nil)
9136         (sum (current-buffer))
9137         (gnus-display-mime-function nil)
9138         (found nil)
9139         point)
9140     (gnus-save-hidden-threads
9141       (gnus-summary-select-article)
9142       (set-buffer gnus-article-buffer)
9143       (goto-char (window-point (get-buffer-window (current-buffer))))
9144       (when backward
9145         (forward-line -1))
9146       (while (not found)
9147         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9148         (if (if backward
9149                 (re-search-backward regexp nil t)
9150               (re-search-forward regexp nil t))
9151             ;; We found the regexp.
9152             (progn
9153               (setq found 'found)
9154               (beginning-of-line)
9155               (set-window-start
9156                (get-buffer-window (current-buffer))
9157                (point))
9158               (forward-line 1)
9159               (set-window-point
9160                (get-buffer-window (current-buffer))
9161                (point))
9162               (set-buffer sum)
9163               (setq point (point)))
9164           ;; We didn't find it, so we go to the next article.
9165           (set-buffer sum)
9166           (setq found 'not)
9167           (while (eq found 'not)
9168             (if (not (if backward (gnus-summary-find-prev)
9169                        (gnus-summary-find-next)))
9170                 ;; No more articles.
9171                 (setq found t)
9172               ;; Select the next article and adjust point.
9173               (unless (gnus-summary-article-sparse-p
9174                        (gnus-summary-article-number))
9175                 (setq found nil)
9176                 (gnus-summary-select-article)
9177                 (set-buffer gnus-article-buffer)
9178                 (widen)
9179                 (goto-char (if backward (point-max) (point-min))))))))
9180       (gnus-message 7 ""))
9181     ;; Return whether we found the regexp.
9182     (when (eq found 'found)
9183       (goto-char point)
9184       (gnus-summary-show-thread)
9185       (gnus-summary-goto-subject gnus-current-article)
9186       (gnus-summary-position-point)
9187       t)))
9188
9189 (defun gnus-find-matching-articles (header regexp)
9190   "Return a list of all articles that match REGEXP on HEADER.
9191 This search includes all articles in the current group that Gnus has
9192 fetched headers for, whether they are displayed or not."
9193   (let ((articles nil)
9194         ;; Can't eta-reduce because it's a macro.
9195         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9196         (case-fold-search t))
9197     (dolist (header gnus-newsgroup-headers)
9198       (when (string-match regexp (funcall func header))
9199         (push (mail-header-number header) articles)))
9200     (nreverse articles)))
9201
9202 (defun gnus-summary-find-matching (header regexp &optional backward unread
9203                                           not-case-fold not-matching)
9204   "Return a list of all articles that match REGEXP on HEADER.
9205 The search stars on the current article and goes forwards unless
9206 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9207 If UNREAD is non-nil, only unread articles will
9208 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9209 in the comparisons. If NOT-MATCHING, return a list of all articles that
9210 not match REGEXP on HEADER."
9211   (let ((case-fold-search (not not-case-fold))
9212         articles d func)
9213     (if (consp header)
9214         (if (eq (car header) 'extra)
9215             (setq func
9216                   `(lambda (h)
9217                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9218                          "")))
9219           (error "%s is an invalid header" header))
9220       (unless (fboundp (intern (concat "mail-header-" header)))
9221         (error "%s is not a valid header" header))
9222       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9223     (dolist (d (if (eq backward 'all)
9224                    gnus-newsgroup-data
9225                  (gnus-data-find-list
9226                   (gnus-summary-article-number)
9227                   (gnus-data-list backward))))
9228       (when (and (or (not unread)       ; We want all articles...
9229                      (gnus-data-unread-p d)) ; Or just unreads.
9230                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9231                  (if not-matching
9232                      (not (string-match
9233                            regexp
9234                            (funcall func (gnus-data-header d))))
9235                    (string-match regexp
9236                                  (funcall func (gnus-data-header d)))))
9237         (push (gnus-data-number d) articles))) ; Success!
9238     (nreverse articles)))
9239
9240 (defun gnus-summary-execute-command (header regexp command &optional backward)
9241   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9242 If HEADER is an empty string (or nil), the match is done on the entire
9243 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9244   (interactive
9245    (list (let ((completion-ignore-case t))
9246            (completing-read
9247             "Header name: "
9248             (mapcar (lambda (header) (list (format "%s" header)))
9249                     (append
9250                      '("Number" "Subject" "From" "Lines" "Date"
9251                        "Message-ID" "Xref" "References" "Body")
9252                      gnus-extra-headers))
9253             nil 'require-match))
9254          (read-string "Regexp: ")
9255          (read-key-sequence "Command: ")
9256          current-prefix-arg))
9257   (when (equal header "Body")
9258     (setq header ""))
9259   ;; Hidden thread subtrees must be searched as well.
9260   (gnus-summary-show-all-threads)
9261   ;; We don't want to change current point nor window configuration.
9262   (save-excursion
9263     (save-window-excursion
9264       (let (gnus-visual
9265             gnus-treat-strip-trailing-blank-lines
9266             gnus-treat-strip-leading-blank-lines
9267             gnus-treat-strip-multiple-blank-lines
9268             gnus-treat-hide-boring-headers
9269             gnus-treat-fold-newsgroups
9270             gnus-article-prepare-hook)
9271         (gnus-message 6 "Executing %s..." (key-description command))
9272         ;; We'd like to execute COMMAND interactively so as to give arguments.
9273         (gnus-execute header regexp
9274                       `(call-interactively ',(key-binding command))
9275                       backward)
9276         (gnus-message 6 "Executing %s...done" (key-description command))))))
9277
9278 (defun gnus-summary-beginning-of-article ()
9279   "Scroll the article back to the beginning."
9280   (interactive)
9281   (gnus-summary-select-article)
9282   (gnus-configure-windows 'article)
9283   (gnus-eval-in-buffer-window gnus-article-buffer
9284     (widen)
9285     (goto-char (point-min))
9286     (when gnus-break-pages
9287       (gnus-narrow-to-page))))
9288
9289 (defun gnus-summary-end-of-article ()
9290   "Scroll to the end of the article."
9291   (interactive)
9292   (gnus-summary-select-article)
9293   (gnus-configure-windows 'article)
9294   (gnus-eval-in-buffer-window gnus-article-buffer
9295     (widen)
9296     (goto-char (point-max))
9297     (recenter -3)
9298     (when gnus-break-pages
9299       (gnus-narrow-to-page))))
9300
9301 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9302   "Truncate to LEN and quote all \"(\"'s in STRING."
9303   (gnus-replace-in-string (if (and len (> (length string) len))
9304                               (substring string 0 len)
9305                             string)
9306                           "[()]" "\\\\\\&"))
9307
9308 (defun gnus-summary-print-article (&optional filename n)
9309   "Generate and print a PostScript image of the process-marked (mail) articles.
9310
9311 If used interactively, print the current article if none are
9312 process-marked.  With prefix arg, prompt the user for the name of the
9313 file to save in.
9314
9315 When used from Lisp, accept two optional args FILENAME and N.  N means
9316 to print the next N articles.  If N is negative, print the N previous
9317 articles.  If N is nil and articles have been marked with the process
9318 mark, print these instead.
9319
9320 If the optional first argument FILENAME is nil, send the image to the
9321 printer.  If FILENAME is a string, save the PostScript image in a file with
9322 that name.  If FILENAME is a number, prompt the user for the name of the file
9323 to save in."
9324   (interactive (list (ps-print-preprint current-prefix-arg)))
9325   (dolist (article (gnus-summary-work-articles n))
9326     (gnus-summary-select-article nil nil 'pseudo article)
9327     (gnus-eval-in-buffer-window gnus-article-buffer
9328       (gnus-print-buffer))
9329     (gnus-summary-remove-process-mark article))
9330   (ps-despool filename))
9331
9332 (defun gnus-print-buffer ()
9333   (let ((buffer (generate-new-buffer " *print*")))
9334     (unwind-protect
9335         (progn
9336           (copy-to-buffer buffer (point-min) (point-max))
9337           (set-buffer buffer)
9338           (gnus-remove-text-with-property 'gnus-decoration)
9339           (when (gnus-visual-p 'article-highlight 'highlight)
9340             ;; Copy-to-buffer doesn't copy overlay.  So redo
9341             ;; highlight.
9342             (let ((gnus-article-buffer buffer))
9343               (gnus-article-highlight-citation t)
9344               (gnus-article-highlight-signature)
9345               (gnus-article-emphasize)
9346               (gnus-article-delete-invisible-text)))
9347           (let ((ps-left-header
9348                  (list
9349                   (concat "("
9350                           (gnus-summary-print-truncate-and-quote
9351                            (mail-header-subject gnus-current-headers)
9352                            66) ")")
9353                   (concat "("
9354                           (gnus-summary-print-truncate-and-quote
9355                            (mail-header-from gnus-current-headers)
9356                            45) ")")))
9357                 (ps-right-header
9358                  (list
9359                   "/pagenumberstring load"
9360                   (concat "("
9361                           (mail-header-date gnus-current-headers) ")"))))
9362             (gnus-run-hooks 'gnus-ps-print-hook)
9363             (save-excursion
9364               (if ps-print-color-p
9365                   (ps-spool-buffer-with-faces)
9366                 (ps-spool-buffer)))))
9367       (kill-buffer buffer))))
9368
9369 (defun gnus-summary-show-article (&optional arg)
9370   "Force redisplaying of the current article.
9371 If ARG (the prefix) is a number, show the article with the charset
9372 defined in `gnus-summary-show-article-charset-alist', or the charset
9373 input.
9374 If ARG (the prefix) is non-nil and not a number, show the raw article
9375 without any article massaging functions being run.  Normally, the key
9376 strokes are `C-u g'."
9377   (interactive "P")
9378   (cond
9379    ((numberp arg)
9380     (gnus-summary-show-article t)
9381     (let ((gnus-newsgroup-charset
9382            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9383                (mm-read-coding-system
9384                 "View as charset: " ;; actually it is coding system.
9385                 (with-current-buffer gnus-article-buffer
9386                   (mm-detect-coding-region (point) (point-max))))))
9387           (gnus-newsgroup-ignored-charsets 'gnus-all))
9388       (gnus-summary-select-article nil 'force)
9389       (let ((deps gnus-newsgroup-dependencies)
9390             head header lines)
9391         (with-current-buffer gnus-original-article-buffer
9392           (save-restriction
9393             (message-narrow-to-head)
9394             (setq head (buffer-string))
9395             (goto-char (point-min))
9396             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9397               (goto-char (point-max))
9398               (widen)
9399               (setq lines (1- (count-lines (point) (point-max))))))
9400           (with-temp-buffer
9401             (insert (format "211 %d Article retrieved.\n"
9402                             (cdr gnus-article-current)))
9403             (insert head)
9404             (if lines (insert (format "Lines: %d\n" lines)))
9405             (insert ".\n")
9406             (let ((nntp-server-buffer (current-buffer)))
9407               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9408         (gnus-data-set-header
9409          (gnus-data-find (cdr gnus-article-current))
9410          header)
9411         (gnus-summary-update-article-line
9412          (cdr gnus-article-current) header)
9413         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9414           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9415    ((not arg)
9416     ;; Select the article the normal way.
9417     (gnus-summary-select-article nil 'force))
9418    (t
9419     ;; We have to require this here to make sure that the following
9420     ;; dynamic binding isn't shadowed by autoloading.
9421     (require 'gnus-async)
9422     (require 'gnus-art)
9423     ;; Bind the article treatment functions to nil.
9424     (let ((gnus-have-all-headers t)
9425           gnus-article-prepare-hook
9426           gnus-article-decode-hook
9427           gnus-display-mime-function
9428           gnus-break-pages)
9429       ;; Destroy any MIME parts.
9430       (when (gnus-buffer-live-p gnus-article-buffer)
9431         (with-current-buffer gnus-article-buffer
9432           (mm-destroy-parts gnus-article-mime-handles)
9433           ;; Set it to nil for safety reason.
9434           (setq gnus-article-mime-handle-alist nil)
9435           (setq gnus-article-mime-handles nil)))
9436       (gnus-summary-select-article nil 'force))))
9437   (gnus-summary-goto-subject gnus-current-article)
9438   (gnus-summary-position-point))
9439
9440 (defun gnus-summary-show-raw-article ()
9441   "Show the raw article without any article massaging functions being run."
9442   (interactive)
9443   (gnus-summary-show-article t))
9444
9445 (defun gnus-summary-verbose-headers (&optional arg)
9446   "Toggle permanent full header display.
9447 If ARG is a positive number, turn header display on.
9448 If ARG is a negative number, turn header display off."
9449   (interactive "P")
9450   (setq gnus-show-all-headers
9451         (cond ((or (not (numberp arg))
9452                    (zerop arg))
9453                (not gnus-show-all-headers))
9454               ((natnump arg)
9455                t)))
9456   (gnus-summary-show-article))
9457
9458 (defun gnus-summary-toggle-header (&optional arg)
9459   "Show the headers if they are hidden, or hide them if they are shown.
9460 If ARG is a positive number, show the entire header.
9461 If ARG is a negative number, hide the unwanted header lines."
9462   (interactive "P")
9463   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9464                      (get-buffer-window gnus-article-buffer t))))
9465     (with-current-buffer gnus-article-buffer
9466       (widen)
9467       (article-narrow-to-head)
9468       (let* ((inhibit-read-only t)
9469              (inhibit-point-motion-hooks t)
9470              (hidden (if (numberp arg)
9471                          (>= arg 0)
9472                        (or (not (looking-at "[^ \t\n]+:"))
9473                            (gnus-article-hidden-text-p 'headers))))
9474              s e)
9475         (delete-region (point-min) (point-max))
9476         (with-current-buffer gnus-original-article-buffer
9477           (goto-char (setq s (point-min)))
9478           (setq e (if (search-forward "\n\n" nil t)
9479                       (1- (point))
9480                     (point-max))))
9481         (insert-buffer-substring gnus-original-article-buffer s e)
9482         (run-hooks 'gnus-article-decode-hook)
9483         (if hidden
9484             (let ((gnus-treat-hide-headers nil)
9485                   (gnus-treat-hide-boring-headers nil))
9486               (gnus-delete-wash-type 'headers)
9487               (gnus-treat-article 'head))
9488           (gnus-treat-article 'head))
9489         (widen)
9490         (if window
9491             (set-window-start window (goto-char (point-min))))
9492         (if gnus-break-pages
9493             (gnus-narrow-to-page)
9494           (when (gnus-visual-p 'page-marker)
9495             (let ((inhibit-read-only t))
9496               (gnus-remove-text-with-property 'gnus-prev)
9497               (gnus-remove-text-with-property 'gnus-next))))
9498         (gnus-set-mode-line 'article)))))
9499
9500 (defun gnus-summary-show-all-headers ()
9501   "Make all header lines visible."
9502   (interactive)
9503   (gnus-summary-toggle-header 1))
9504
9505 (defun gnus-summary-caesar-message (&optional arg)
9506   "Caesar rotate the current article by 13.
9507 With a non-numerical prefix, also rotate headers.  A numerical
9508 prefix specifies how many places to rotate each letter forward."
9509   (interactive "P")
9510   (gnus-summary-select-article)
9511   (let ((mail-header-separator ""))
9512     (gnus-eval-in-buffer-window gnus-article-buffer
9513       (save-restriction
9514         (widen)
9515         (let ((start (window-start))
9516               (inhibit-read-only t))
9517           (if (equal arg '(4))
9518               (message-caesar-buffer-body nil t)
9519             (message-caesar-buffer-body arg))
9520           (set-window-start (get-buffer-window (current-buffer)) start)))))
9521   ;; Create buttons and stuff...
9522   (gnus-treat-article nil))
9523
9524 (declare-function idna-to-unicode "ext:idna" (str))
9525
9526 (defun gnus-summary-idna-message (&optional arg)
9527   "Decode IDNA encoded domain names in the current articles.
9528 IDNA encoded domain names looks like `xn--bar'.  If a string
9529 remain unencoded after running this function, it is likely an
9530 invalid IDNA string (`xn--bar' is invalid).
9531
9532 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9533 installed for this command to work."
9534   (interactive "P")
9535   (if (not (and (condition-case nil (require 'idna)
9536                   (file-error))
9537                 (mm-coding-system-p 'utf-8)
9538                 (executable-find (symbol-value 'idna-program))))
9539       (gnus-message
9540        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9541     (gnus-summary-select-article)
9542     (let ((mail-header-separator ""))
9543       (gnus-eval-in-buffer-window gnus-article-buffer
9544         (save-restriction
9545           (widen)
9546           (let ((start (window-start))
9547                 buffer-read-only)
9548             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9549               (replace-match (idna-to-unicode (match-string 1))))
9550             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9551
9552 (defun gnus-summary-morse-message (&optional arg)
9553   "Morse decode the current article."
9554   (interactive "P")
9555   (gnus-summary-select-article)
9556   (let ((mail-header-separator ""))
9557     (gnus-eval-in-buffer-window gnus-article-buffer
9558       (save-excursion
9559         (save-restriction
9560           (widen)
9561           (let ((pos (window-start))
9562                 (inhibit-read-only t))
9563             (goto-char (point-min))
9564             (when (message-goto-body)
9565               (gnus-narrow-to-body))
9566             (goto-char (point-min))
9567             (while (search-forward "·" (point-max) t)
9568               (replace-match "."))
9569             (unmorse-region (point-min) (point-max))
9570             (widen)
9571             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9572
9573 (defun gnus-summary-stop-page-breaking ()
9574   "Stop page breaking in the current article."
9575   (interactive)
9576   (gnus-summary-select-article)
9577   (gnus-eval-in-buffer-window gnus-article-buffer
9578     (widen)
9579     (when (gnus-visual-p 'page-marker)
9580       (let ((inhibit-read-only t))
9581         (gnus-remove-text-with-property 'gnus-prev)
9582         (gnus-remove-text-with-property 'gnus-next))
9583       (setq gnus-page-broken nil))))
9584
9585 (defun gnus-summary-move-article (&optional n to-newsgroup
9586                                             select-method action)
9587   "Move the current article to a different newsgroup.
9588 If N is a positive number, move the N next articles.
9589 If N is a negative number, move the N previous articles.
9590 If N is nil and any articles have been marked with the process mark,
9591 move those articles instead.
9592 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9593 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9594 re-spool using this method.
9595
9596 When called interactively with TO-NEWSGROUP being nil, the value of
9597 the variable `gnus-move-split-methods' is used for finding a default
9598 for the target newsgroup.
9599
9600 For this function to work, both the current newsgroup and the
9601 newsgroup that you want to move to have to support the `request-move'
9602 and `request-accept' functions.
9603
9604 ACTION can be either `move' (the default), `crosspost' or `copy'."
9605   (interactive "P")
9606   (unless action
9607     (setq action 'move))
9608   ;; Check whether the source group supports the required functions.
9609   (cond ((and (eq action 'move)
9610               (not (gnus-check-backend-function
9611                     'request-move-article gnus-newsgroup-name)))
9612          (error "The current group does not support article moving"))
9613         ((and (eq action 'crosspost)
9614               (not (gnus-check-backend-function
9615                     'request-replace-article gnus-newsgroup-name)))
9616          (error "The current group does not support article editing")))
9617   (let ((articles (gnus-summary-work-articles n))
9618         (prefix (if (gnus-check-backend-function
9619                      'request-move-article gnus-newsgroup-name)
9620                     (funcall gnus-move-group-prefix-function
9621                              gnus-newsgroup-name)
9622                   ""))
9623         (names '((move "Move" "Moving")
9624                  (copy "Copy" "Copying")
9625                  (crosspost "Crosspost" "Crossposting")))
9626         (copy-buf (save-excursion
9627                     (nnheader-set-temp-buffer " *copy article*")))
9628         art-group to-method new-xref article to-groups
9629         articles-to-update-marks encoded)
9630     (unless (assq action names)
9631       (error "Unknown action %s" action))
9632     ;; Read the newsgroup name.
9633     (when (and (not to-newsgroup)
9634                (not select-method))
9635       (if (and gnus-move-split-methods
9636                (not
9637                 (and (memq gnus-current-article articles)
9638                      (gnus-buffer-live-p gnus-original-article-buffer))))
9639           ;; When `gnus-move-split-methods' is non-nil, we have to
9640           ;; select an article to give `gnus-read-move-group-name' an
9641           ;; opportunity to suggest an appropriate default.  However,
9642           ;; we needn't render or mark the article.
9643           (let ((gnus-display-mime-function nil)
9644                 (gnus-article-prepare-hook nil)
9645                 (gnus-mark-article-hook nil))
9646             (gnus-summary-select-article nil nil nil (car articles))))
9647       (setq to-newsgroup (gnus-read-move-group-name
9648                           (cadr (assq action names))
9649                           (symbol-value
9650                            (intern (format "gnus-current-%s-group" action)))
9651                           articles prefix)
9652             encoded to-newsgroup
9653             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9654       (set (intern (format "gnus-current-%s-group" action))
9655            (mm-decode-coding-string
9656             to-newsgroup
9657             (gnus-group-name-charset to-method to-newsgroup))))
9658     (unless to-method
9659       (setq to-method (or select-method
9660                           (gnus-server-to-method
9661                            (gnus-group-method to-newsgroup)))))
9662     (setq to-newsgroup
9663           (or encoded
9664               (and to-newsgroup
9665                    (mm-encode-coding-string
9666                     to-newsgroup
9667                     (gnus-group-name-charset to-method to-newsgroup)))))
9668     ;; Check the method we are to move this article to...
9669     (unless (gnus-check-backend-function
9670              'request-accept-article (car to-method))
9671       (error "%s does not support article copying" (car to-method)))
9672     (unless (gnus-check-server to-method)
9673       (error "Can't open server %s" (car to-method)))
9674     (gnus-message 6 "%s to %s: %s..."
9675                   (caddr (assq action names))
9676                   (or (car select-method)
9677                       (gnus-group-decoded-name to-newsgroup))
9678                   articles)
9679     (while articles
9680       (setq article (pop articles))
9681       (setq
9682        art-group
9683        (cond
9684         ;; Move the article.
9685         ((eq action 'move)
9686          ;; Remove this article from future suppression.
9687          (gnus-dup-unsuppress-article article)
9688          (let* ((from-method (gnus-find-method-for-group
9689                               gnus-newsgroup-name))
9690                 (to-method (or select-method
9691                                (gnus-find-method-for-group to-newsgroup)))
9692                 (move-is-internal (gnus-server-equal from-method to-method)))
9693            (gnus-request-move-article
9694             article                     ; Article to move
9695             gnus-newsgroup-name         ; From newsgroup
9696             (nth 1 (gnus-find-method-for-group
9697                     gnus-newsgroup-name)) ; Server
9698             (list 'gnus-request-accept-article
9699                   to-newsgroup (list 'quote select-method)
9700                   (not articles) t)     ; Accept form
9701             (not articles)              ; Only save nov last time
9702             (and move-is-internal
9703                  to-newsgroup           ; Not respooling
9704                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9705         ;; Copy the article.
9706         ((eq action 'copy)
9707          (with-current-buffer copy-buf
9708            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9709              (save-restriction
9710                (nnheader-narrow-to-headers)
9711                (dolist (hdr gnus-copy-article-ignored-headers)
9712                  (message-remove-header hdr t)))
9713              (gnus-request-accept-article
9714               to-newsgroup select-method (not articles) t))))
9715         ;; Crosspost the article.
9716         ((eq action 'crosspost)
9717          (let ((xref (message-tokenize-header
9718                       (mail-header-xref (gnus-summary-article-header article))
9719                       " ")))
9720            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9721                                   ":" (number-to-string article)))
9722            (unless xref
9723              (setq xref (list (system-name))))
9724            (setq new-xref
9725                  (concat
9726                   (mapconcat 'identity
9727                              (delete "Xref:" (delete new-xref xref))
9728                              " ")
9729                   " " new-xref))
9730            (with-current-buffer copy-buf
9731              ;; First put the article in the destination group.
9732              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9733              (when (consp (setq art-group
9734                                 (gnus-request-accept-article
9735                                  to-newsgroup select-method (not articles) t)))
9736                (setq new-xref (concat new-xref " " (car art-group)
9737                                       ":"
9738                                       (number-to-string (cdr art-group))))
9739                ;; Now we have the new Xrefs header, so we insert
9740                ;; it and replace the new article.
9741                (nnheader-replace-header "Xref" new-xref)
9742                (gnus-request-replace-article
9743                 (cdr art-group) to-newsgroup (current-buffer) t)
9744                art-group))))))
9745       (cond
9746        ((not art-group)
9747         (gnus-message 1 "Couldn't %s article %s: %s"
9748                       (cadr (assq action names)) article
9749                       (nnheader-get-report (car to-method))))
9750        ((eq art-group 'junk)
9751         (when (eq action 'move)
9752           (gnus-summary-mark-article article gnus-canceled-mark)
9753           (gnus-message 4 "Deleted article %s" article)
9754           ;; run the delete hook
9755           (run-hook-with-args 'gnus-summary-article-delete-hook
9756                               action
9757                               (gnus-data-header
9758                                (assoc article (gnus-data-list nil)))
9759                               gnus-newsgroup-name nil
9760                               select-method)))
9761        (t
9762         (let* ((pto-group (gnus-group-prefixed-name
9763                            (car art-group) to-method))
9764                (info (gnus-get-info pto-group))
9765                (to-group (gnus-info-group info))
9766                to-marks)
9767           ;; Update the group that has been moved to.
9768           (when (and info
9769                      (memq action '(move copy)))
9770             (unless (member to-group to-groups)
9771               (push to-group to-groups))
9772
9773             (unless (memq article gnus-newsgroup-unreads)
9774               (push 'read to-marks)
9775               (gnus-info-set-read
9776                info (gnus-add-to-range (gnus-info-read info)
9777                                        (list (cdr art-group)))))
9778
9779             ;; See whether the article is to be put in the cache.
9780             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9781                    (marks (if expirable
9782                               gnus-article-mark-lists
9783                             (delete '(expirable . expire)
9784                                     (copy-sequence gnus-article-mark-lists))))
9785                    (to-article (cdr art-group)))
9786
9787               ;; Enter the article into the cache in the new group,
9788               ;; if that is required.
9789               (when gnus-use-cache
9790                 (gnus-cache-possibly-enter-article
9791                  to-group to-article
9792                  (memq article gnus-newsgroup-marked)
9793                  (memq article gnus-newsgroup-dormant)
9794                  (memq article gnus-newsgroup-unreads)))
9795
9796               (when gnus-preserve-marks
9797                 ;; Copy any marks over to the new group.
9798                 (when (and (equal to-group gnus-newsgroup-name)
9799                            (not (memq article gnus-newsgroup-unreads)))
9800                   ;; Mark this article as read in this group.
9801                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9802                   (setcdr (gnus-active to-group) to-article)
9803                   (setcdr gnus-newsgroup-active to-article))
9804
9805                 (while marks
9806                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9807                     (when (memq article (symbol-value
9808                                          (intern (format "gnus-newsgroup-%s"
9809                                                          (caar marks)))))
9810                       (push (cdar marks) to-marks)
9811                       ;; If the other group is the same as this group,
9812                       ;; then we have to add the mark to the list.
9813                       (when (equal to-group gnus-newsgroup-name)
9814                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9815                              (cons to-article
9816                                    (symbol-value
9817                                     (intern (format "gnus-newsgroup-%s"
9818                                                     (caar marks)))))))
9819                       ;; Copy the marks to other group.
9820                       (gnus-add-marked-articles
9821                        to-group (cdar marks) (list to-article) info)))
9822                   (setq marks (cdr marks)))
9823
9824                 (when (and expirable
9825                            gnus-mark-copied-or-moved-articles-as-expirable
9826                            (not (memq 'expire to-marks)))
9827                   ;; Mark this article as expirable.
9828                   (push 'expire to-marks)
9829                   (when (equal to-group gnus-newsgroup-name)
9830                     (push to-article gnus-newsgroup-expirable))
9831                   ;; Copy the expirable mark to other group.
9832                   (gnus-add-marked-articles
9833                    to-group 'expire (list to-article) info))
9834
9835                 (when to-marks
9836                   (gnus-request-set-mark
9837                    to-group (list (list (list to-article) 'add to-marks)))))
9838
9839               (gnus-dribble-enter
9840                (concat "(gnus-group-set-info '"
9841                        (gnus-prin1-to-string (gnus-get-info to-group))
9842                        ")"))))
9843
9844           ;; Update the Xref header in this article to point to
9845           ;; the new crossposted article we have just created.
9846           (when (eq action 'crosspost)
9847             (with-current-buffer copy-buf
9848               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9849               (nnheader-replace-header "Xref" new-xref)
9850               (gnus-request-replace-article
9851                article gnus-newsgroup-name (current-buffer) t)))
9852
9853           ;; run the move/copy/crosspost/respool hook
9854           (run-hook-with-args 'gnus-summary-article-move-hook
9855                               action
9856                               (gnus-data-header
9857                                (assoc article (gnus-data-list nil)))
9858                               gnus-newsgroup-name
9859                               to-newsgroup
9860                               select-method))
9861
9862         ;;;!!!Why is this necessary?
9863         (set-buffer gnus-summary-buffer)
9864
9865         (when (eq action 'move)
9866           (save-excursion
9867             (gnus-summary-goto-subject article)
9868             (gnus-summary-mark-article article gnus-canceled-mark)))))
9869       (push article articles-to-update-marks))
9870
9871     (save-excursion
9872       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9873     ;; Re-activate all groups that have been moved to.
9874     (with-current-buffer gnus-group-buffer
9875       (let ((gnus-group-marked to-groups))
9876         (gnus-group-get-new-news-this-group nil t)))
9877
9878     (gnus-kill-buffer copy-buf)
9879     (gnus-summary-position-point)
9880     (gnus-set-mode-line 'summary)))
9881
9882 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9883   "Copy the current article to some other group.
9884 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9885 When called interactively, if TO-NEWSGROUP is nil, use the value of
9886 the variable `gnus-move-split-methods' for finding a default target
9887 newsgroup.
9888 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9889 re-spool using this method."
9890   (interactive "P")
9891   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9892
9893 (defun gnus-summary-crosspost-article (&optional n)
9894   "Crosspost the current article to some other group."
9895   (interactive "P")
9896   (gnus-summary-move-article n nil nil 'crosspost))
9897
9898 (defcustom gnus-summary-respool-default-method nil
9899   "Default method type for respooling an article.
9900 If nil, use to the current newsgroup method."
9901   :type 'symbol
9902   :group 'gnus-summary-mail)
9903
9904 (defun gnus-summary-respool-article (&optional n method)
9905   "Respool the current article.
9906 The article will be squeezed through the mail spooling process again,
9907 which means that it will be put in some mail newsgroup or other
9908 depending on `nnmail-split-methods'.
9909 If N is a positive number, respool the N next articles.
9910 If N is a negative number, respool the N previous articles.
9911 If N is nil and any articles have been marked with the process mark,
9912 respool those articles instead.
9913
9914 Respooling can be done both from mail groups and \"real\" newsgroups.
9915 In the former case, the articles in question will be moved from the
9916 current group into whatever groups they are destined to.  In the
9917 latter case, they will be copied into the relevant groups."
9918   (interactive
9919    (list current-prefix-arg
9920          (let* ((methods (gnus-methods-using 'respool))
9921                 (methname
9922                  (symbol-name (or gnus-summary-respool-default-method
9923                                   (car (gnus-find-method-for-group
9924                                         gnus-newsgroup-name)))))
9925                 (method
9926                  (gnus-completing-read-with-default
9927                   methname "Backend to use when respooling"
9928                   methods nil t nil 'gnus-mail-method-history))
9929                 ms)
9930            (cond
9931             ((zerop (length (setq ms (gnus-servers-using-backend
9932                                       (intern method)))))
9933              (list (intern method) ""))
9934             ((= 1 (length ms))
9935              (car ms))
9936             (t
9937              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9938                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9939                            ms-alist))))))))
9940   (unless method
9941     (error "No method given for respooling"))
9942   (if (assoc (symbol-name
9943               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9944              (gnus-methods-using 'respool))
9945       (gnus-summary-move-article n nil method)
9946     (gnus-summary-copy-article n nil method)))
9947
9948 (defun gnus-summary-import-article (file &optional edit)
9949   "Import an arbitrary file into a mail newsgroup."
9950   (interactive "fImport file: \nP")
9951   (let ((group gnus-newsgroup-name)
9952         (now (current-time))
9953         atts lines group-art)
9954     (unless (gnus-check-backend-function 'request-accept-article group)
9955       (error "%s does not support article importing" group))
9956     (or (file-readable-p file)
9957         (not (file-regular-p file))
9958         (error "Can't read %s" file))
9959     (with-current-buffer (gnus-get-buffer-create " *import file*")
9960       (erase-buffer)
9961       (nnheader-insert-file-contents file)
9962       (goto-char (point-min))
9963       (if (nnheader-article-p)
9964           (save-restriction
9965             (goto-char (point-min))
9966             (search-forward "\n\n" nil t)
9967             (narrow-to-region (point-min) (1- (point)))
9968             (goto-char (point-min))
9969             (unless (re-search-forward "^date:" nil t)
9970               (goto-char (point-max))
9971               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9972        ;; This doesn't look like an article, so we fudge some headers.
9973         (setq atts (file-attributes file)
9974               lines (count-lines (point-min) (point-max)))
9975         (insert "From: " (read-string "From: ") "\n"
9976                 "Subject: " (read-string "Subject: ") "\n"
9977                 "Date: " (message-make-date (nth 5 atts)) "\n"
9978                 "Message-ID: " (message-make-message-id) "\n"
9979                 "Lines: " (int-to-string lines) "\n"
9980                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9981       (setq group-art (gnus-request-accept-article group nil t))
9982       (kill-buffer (current-buffer)))
9983     (setq gnus-newsgroup-active (gnus-activate-group group))
9984     (forward-line 1)
9985     (gnus-summary-goto-article (cdr group-art) nil t)
9986     (when edit
9987       (gnus-summary-edit-article))))
9988
9989 (defun gnus-summary-create-article ()
9990   "Create an article in a mail newsgroup."
9991   (interactive)
9992   (let ((group gnus-newsgroup-name)
9993         (now (current-time))
9994         group-art)
9995     (unless (gnus-check-backend-function 'request-accept-article group)
9996       (error "%s does not support article importing" group))
9997     (with-current-buffer (gnus-get-buffer-create " *import file*")
9998       (erase-buffer)
9999       (goto-char (point-min))
10000       ;; This doesn't look like an article, so we fudge some headers.
10001       (insert "From: " (read-string "From: ") "\n"
10002               "Subject: " (read-string "Subject: ") "\n"
10003               "Date: " (message-make-date now) "\n"
10004               "Message-ID: " (message-make-message-id) "\n")
10005       (setq group-art (gnus-request-accept-article group nil t))
10006       (kill-buffer (current-buffer)))
10007     (setq gnus-newsgroup-active (gnus-activate-group group))
10008     (forward-line 1)
10009     (gnus-summary-goto-article (cdr group-art) nil t)
10010     (gnus-summary-edit-article)))
10011
10012 (defun gnus-summary-article-posted-p ()
10013   "Say whether the current (mail) article is available from news as well.
10014 This will be the case if the article has both been mailed and posted."
10015   (interactive)
10016   (let ((id (mail-header-references (gnus-summary-article-header)))
10017         (gnus-override-method (car (gnus-refer-article-methods))))
10018     (if (gnus-request-head id "")
10019         (gnus-message 2 "The current message was found on %s"
10020                       gnus-override-method)
10021       (gnus-message 2 "The current message couldn't be found on %s"
10022                     gnus-override-method)
10023       nil)))
10024
10025 (defun gnus-summary-expire-articles (&optional now)
10026   "Expire all articles that are marked as expirable in the current group."
10027   (interactive)
10028   (when (and (not gnus-group-is-exiting-without-update-p)
10029              (gnus-check-backend-function
10030               'request-expire-articles gnus-newsgroup-name))
10031     ;; This backend supports expiry.
10032     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10033            (expirable (if total
10034                           (progn
10035                             ;; We need to update the info for
10036                             ;; this group for `gnus-list-of-read-articles'
10037                             ;; to give us the right answer.
10038                             (gnus-run-hooks 'gnus-exit-group-hook)
10039                             (gnus-summary-update-info)
10040                             (gnus-list-of-read-articles gnus-newsgroup-name))
10041                         (setq gnus-newsgroup-expirable
10042                               (sort gnus-newsgroup-expirable '<))))
10043            (expiry-wait (if now 'immediate
10044                           (gnus-group-find-parameter
10045                            gnus-newsgroup-name 'expiry-wait)))
10046            (nnmail-expiry-target
10047             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10048                 nnmail-expiry-target))
10049            es)
10050       (when expirable
10051         ;; There are expirable articles in this group, so we run them
10052         ;; through the expiry process.
10053         (gnus-message 6 "Expiring articles...")
10054         (unless (gnus-check-group gnus-newsgroup-name)
10055           (error "Can't open server for %s" gnus-newsgroup-name))
10056         ;; The list of articles that weren't expired is returned.
10057         (save-excursion
10058           (if expiry-wait
10059               (let ((nnmail-expiry-wait-function nil)
10060                     (nnmail-expiry-wait expiry-wait))
10061                 (setq es (gnus-request-expire-articles
10062                           expirable gnus-newsgroup-name)))
10063             (setq es (gnus-request-expire-articles
10064                       expirable gnus-newsgroup-name)))
10065           (unless total
10066             (setq gnus-newsgroup-expirable es))
10067           ;; We go through the old list of expirable, and mark all
10068           ;; really expired articles as nonexistent.
10069           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10070             (let ((gnus-use-cache nil))
10071               (dolist (article expirable)
10072                 (when (and (not (memq article es))
10073                            (gnus-data-find article))
10074                   (gnus-summary-mark-article article gnus-canceled-mark)
10075                   (run-hook-with-args 'gnus-summary-article-expire-hook
10076                                       'delete
10077                                       (gnus-data-header
10078                                        (assoc article (gnus-data-list nil)))
10079                                       gnus-newsgroup-name
10080                                       nil
10081                                       nil))))))
10082         (gnus-message 6 "Expiring articles...done")))))
10083
10084 (defun gnus-summary-expire-articles-now ()
10085   "Expunge all expirable articles in the current group.
10086 This means that *all* articles that are marked as expirable will be
10087 deleted forever, right now."
10088   (interactive)
10089   (or gnus-expert-user
10090       (gnus-yes-or-no-p
10091        "Are you really, really sure you want to delete all expirable messages? ")
10092       (error "Phew!"))
10093   (gnus-summary-expire-articles t))
10094
10095 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10096 (defun gnus-summary-delete-article (&optional n)
10097   "Delete the N next (mail) articles.
10098 This command actually deletes articles.  This is not a marking
10099 command.  The article will disappear forever from your life, never to
10100 return.
10101
10102 If N is negative, delete backwards.
10103 If N is nil and articles have been marked with the process mark,
10104 delete these instead.
10105
10106 If `gnus-novice-user' is non-nil you will be asked for
10107 confirmation before the articles are deleted."
10108   (interactive "P")
10109   (unless (gnus-check-backend-function 'request-expire-articles
10110                                        gnus-newsgroup-name)
10111     (error "The current newsgroup does not support article deletion"))
10112   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10113     (error "Couldn't open server"))
10114   ;; Compute the list of articles to delete.
10115   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10116         (nnmail-expiry-target 'delete)
10117         not-deleted)
10118     (if (and gnus-novice-user
10119              (not (gnus-yes-or-no-p
10120                    (format "Do you really want to delete %s forever? "
10121                            (if (> (length articles) 1)
10122                                (format "these %s articles" (length articles))
10123                              "this article")))))
10124         ()
10125       ;; Delete the articles.
10126       (setq not-deleted (gnus-request-expire-articles
10127                          articles gnus-newsgroup-name 'force))
10128       (save-excursion
10129         (while articles
10130           (gnus-summary-remove-process-mark (car articles))
10131           ;; The backend might not have been able to delete the article
10132           ;; after all.
10133           (unless (memq (car articles) not-deleted)
10134             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10135           (let* ((article (car articles))
10136                  (ghead  (gnus-data-header
10137                           (assoc article (gnus-data-list nil)))))
10138             (run-hook-with-args 'gnus-summary-article-delete-hook
10139                                 'delete ghead gnus-newsgroup-name nil
10140                                 nil))
10141           (setq articles (cdr articles))))
10142       (when not-deleted
10143         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10144     (gnus-summary-position-point)
10145     (gnus-set-mode-line 'summary)
10146     not-deleted))
10147
10148 (defun gnus-summary-edit-article (&optional arg)
10149   "Edit the current article.
10150 This will have permanent effect only in mail groups.
10151 If ARG is nil, edit the decoded articles.
10152 If ARG is 1, edit the raw articles.
10153 If ARG is 2, edit the raw articles even in read-only groups.
10154 If ARG is 3, edit the articles with the current handles.
10155 Otherwise, allow editing of articles even in read-only
10156 groups."
10157   (interactive "P")
10158   (let (force raw current-handles)
10159     (cond
10160      ((null arg))
10161      ((eq arg 1)
10162       (setq raw t))
10163      ((eq arg 2)
10164       (setq raw t
10165             force t))
10166      ((eq arg 3)
10167       (setq current-handles
10168             (and (gnus-buffer-live-p gnus-article-buffer)
10169                  (with-current-buffer gnus-article-buffer
10170                    (prog1
10171                        gnus-article-mime-handles
10172                      (setq gnus-article-mime-handles nil))))))
10173      (t
10174       (setq force t)))
10175     (when (and raw (not force)
10176                (member gnus-newsgroup-name '("nndraft:delayed"
10177                                              "nndraft:drafts"
10178                                              "nndraft:queue")))
10179       (error "Can't edit the raw article in group %s"
10180              gnus-newsgroup-name))
10181     (with-current-buffer gnus-summary-buffer
10182       (let ((mail-parse-charset gnus-newsgroup-charset)
10183             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10184         (gnus-set-global-variables)
10185         (when (and (not force)
10186                    (gnus-group-read-only-p))
10187           (error "The current newsgroup does not support article editing"))
10188         (gnus-summary-show-article t)
10189         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10190           (with-current-buffer gnus-article-buffer
10191             (mm-enable-multibyte)))
10192         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10193             (setq raw t))
10194         (gnus-article-edit-article
10195          (if raw 'ignore
10196            `(lambda ()
10197               (let ((mbl mml-buffer-list))
10198                 (setq mml-buffer-list nil)
10199                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10200                   (mime-to-mml ,'current-handles))
10201                 (let ((mbl1 mml-buffer-list))
10202                   (setq mml-buffer-list mbl)
10203                   (set (make-local-variable 'mml-buffer-list) mbl1))
10204                 (gnus-make-local-hook 'kill-buffer-hook)
10205                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10206          `(lambda (no-highlight)
10207             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10208                   (message-options message-options)
10209                   (message-options-set-recipient)
10210                   (mail-parse-ignored-charsets
10211                    ',gnus-newsgroup-ignored-charsets)
10212                   (rfc2047-header-encoding-alist
10213                    ',(let ((charset (gnus-group-name-charset
10214                                      (gnus-find-method-for-group
10215                                       gnus-newsgroup-name)
10216                                      gnus-newsgroup-name)))
10217                        (append (list (cons "Newsgroups" charset)
10218                                      (cons "Followup-To" charset)
10219                                      (cons "Xref" charset))
10220                                rfc2047-header-encoding-alist))))
10221               ,(if (not raw) '(progn
10222                                 (mml-to-mime)
10223                                 (mml-destroy-buffers)
10224                                 (remove-hook 'kill-buffer-hook
10225                                              'mml-destroy-buffers t)
10226                                 (kill-local-variable 'mml-buffer-list)))
10227               (gnus-summary-edit-article-done
10228                ,(or (mail-header-references gnus-current-headers) "")
10229                ,(gnus-group-read-only-p)
10230                ,gnus-summary-buffer no-highlight))))))))
10231
10232 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10233
10234 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10235                                                  no-highlight)
10236   "Make edits to the current article permanent."
10237   (interactive)
10238   (save-excursion
10239    ;; The buffer restriction contains the entire article if it exists.
10240     (when (article-goto-body)
10241       (let ((lines (count-lines (point) (point-max)))
10242             (length (- (point-max) (point)))
10243             (case-fold-search t)
10244             (body (copy-marker (point))))
10245         (goto-char (point-min))
10246         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10247           (delete-region (match-beginning 1) (match-end 1))
10248           (insert (number-to-string length)))
10249         (goto-char (point-min))
10250         (when (re-search-forward
10251                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10252           (delete-region (match-beginning 1) (match-end 1))
10253           (insert (number-to-string length)))
10254         (goto-char (point-min))
10255         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10256           (delete-region (match-beginning 1) (match-end 1))
10257           (insert (number-to-string lines))))))
10258   ;; Replace the article.
10259   (let ((buf (current-buffer)))
10260     (with-temp-buffer
10261       (insert-buffer-substring buf)
10262
10263       (if (and (not read-only)
10264                (not (gnus-request-replace-article
10265                      (cdr gnus-article-current) (car gnus-article-current)
10266                      (current-buffer) t)))
10267           (error "Couldn't replace article")
10268         ;; Update the summary buffer.
10269         (if (and references
10270                  (equal (message-tokenize-header references " ")
10271                         (message-tokenize-header
10272                          (or (message-fetch-field "references") "") " ")))
10273             ;; We only have to update this line.
10274             (save-excursion
10275               (save-restriction
10276                 (message-narrow-to-head)
10277                 (let ((head (buffer-substring-no-properties
10278                              (point-min) (point-max)))
10279                       header)
10280                   (with-temp-buffer
10281                     (insert (format "211 %d Article retrieved.\n"
10282                                     (cdr gnus-article-current)))
10283                     (insert head)
10284                     (insert ".\n")
10285                     (let ((nntp-server-buffer (current-buffer)))
10286                       (setq header (car (gnus-get-newsgroup-headers
10287                                          nil t))))
10288                     (with-current-buffer gnus-summary-buffer
10289                       (gnus-data-set-header
10290                        (gnus-data-find (cdr gnus-article-current))
10291                        header)
10292                       (gnus-summary-update-article-line
10293                        (cdr gnus-article-current) header)
10294                       (if (gnus-summary-goto-subject
10295                            (cdr gnus-article-current) nil t)
10296                           (gnus-summary-update-secondary-mark
10297                            (cdr gnus-article-current))))))))
10298           ;; Update threads.
10299           (set-buffer (or buffer gnus-summary-buffer))
10300           (gnus-summary-update-article (cdr gnus-article-current))
10301           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10302               (gnus-summary-update-secondary-mark
10303                (cdr gnus-article-current))))
10304         ;; Prettify the article buffer again.
10305         (unless no-highlight
10306           (with-current-buffer gnus-article-buffer
10307             ;;;!!! Fix this -- article should be rehighlighted.
10308             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10309             (set-buffer gnus-original-article-buffer)
10310             (gnus-request-article
10311              (cdr gnus-article-current)
10312              (car gnus-article-current) (current-buffer))))
10313         ;; Prettify the summary buffer line.
10314         (when (gnus-visual-p 'summary-highlight 'highlight)
10315           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10316
10317 (defun gnus-summary-edit-wash (key)
10318   "Perform editing command KEY in the article buffer."
10319   (interactive
10320    (list
10321     (progn
10322       (message "%s" (concat (this-command-keys) "- "))
10323       (read-char))))
10324   (message "")
10325   (gnus-summary-edit-article)
10326   (execute-kbd-macro (concat (this-command-keys) key))
10327   (gnus-article-edit-done))
10328
10329 ;;; Respooling
10330
10331 (defun gnus-summary-respool-query (&optional silent trace)
10332   "Query where the respool algorithm would put this article."
10333   (interactive)
10334   (let (gnus-mark-article-hook)
10335     (gnus-summary-select-article)
10336     (with-current-buffer gnus-original-article-buffer
10337       (let ((groups (nnmail-article-group 'identity trace)))
10338         (unless silent
10339           (if groups
10340               (message "This message would go to %s"
10341                        (mapconcat 'car groups ", "))
10342             (message "This message would go to no groups"))
10343           groups)))))
10344
10345 (defun gnus-summary-respool-trace ()
10346   "Trace where the respool algorithm would put this article.
10347 Display a buffer showing all fancy splitting patterns which matched."
10348   (interactive)
10349   (gnus-summary-respool-query nil t))
10350
10351 ;; Summary marking commands.
10352
10353 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10354   "Mark articles which has the same subject as read, and then select the next.
10355 If UNMARK is positive, remove any kind of mark.
10356 If UNMARK is negative, tick articles."
10357   (interactive "P")
10358   (when unmark
10359     (setq unmark (prefix-numeric-value unmark)))
10360   (let ((count
10361          (gnus-summary-mark-same-subject
10362           (gnus-summary-article-subject) unmark)))
10363     ;; Select next unread article.  If auto-select-same mode, should
10364     ;; select the first unread article.
10365     (gnus-summary-next-article t (and gnus-auto-select-same
10366                                       (gnus-summary-article-subject)))
10367     (gnus-message 7 "%d article%s marked as %s"
10368                   count (if (= count 1) " is" "s are")
10369                   (if unmark "unread" "read"))))
10370
10371 (defun gnus-summary-kill-same-subject (&optional unmark)
10372   "Mark articles which has the same subject as read.
10373 If UNMARK is positive, remove any kind of mark.
10374 If UNMARK is negative, tick articles."
10375   (interactive "P")
10376   (when unmark
10377     (setq unmark (prefix-numeric-value unmark)))
10378   (let ((count
10379          (gnus-summary-mark-same-subject
10380           (gnus-summary-article-subject) unmark)))
10381     ;; If marked as read, go to next unread subject.
10382     (when (null unmark)
10383       ;; Go to next unread subject.
10384       (gnus-summary-next-subject 1 t))
10385     (gnus-message 7 "%d articles are marked as %s"
10386                   count (if unmark "unread" "read"))))
10387
10388 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10389   "Mark articles with same SUBJECT as read, and return marked number.
10390 If optional argument UNMARK is positive, remove any kinds of marks.
10391 If optional argument UNMARK is negative, mark articles as unread instead."
10392   (let ((count 1))
10393     (save-excursion
10394       (cond
10395        ((null unmark)                   ; Mark as read.
10396         (while (and
10397                 (progn
10398                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10399                   (gnus-summary-show-thread) t)
10400                 (gnus-summary-find-subject subject))
10401           (setq count (1+ count))))
10402        ((> unmark 0)                    ; Tick.
10403         (while (and
10404                 (progn
10405                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10406                   (gnus-summary-show-thread) t)
10407                 (gnus-summary-find-subject subject))
10408           (setq count (1+ count))))
10409        (t                               ; Mark as unread.
10410         (while (and
10411                 (progn
10412                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10413                   (gnus-summary-show-thread) t)
10414                 (gnus-summary-find-subject subject))
10415           (setq count (1+ count)))))
10416       (gnus-set-mode-line 'summary)
10417       ;; Return the number of marked articles.
10418       count)))
10419
10420 (defun gnus-summary-mark-as-processable (n &optional unmark)
10421   "Set the process mark on the next N articles.
10422 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10423 the process mark instead.  The difference between N and the actual
10424 number of articles marked is returned."
10425   (interactive "P")
10426   (if (and (null n) (gnus-region-active-p))
10427       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10428     (setq n (prefix-numeric-value n))
10429     (let ((backward (< n 0))
10430           (n (abs n)))
10431       (while (and
10432               (> n 0)
10433               (if unmark
10434                   (gnus-summary-remove-process-mark
10435                    (gnus-summary-article-number))
10436                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10437               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10438         (setq n (1- n)))
10439       (when (/= 0 n)
10440         (gnus-message 7 "No more articles"))
10441       (gnus-summary-recenter)
10442       (gnus-summary-position-point)
10443       n)))
10444
10445 (defun gnus-summary-unmark-as-processable (n)
10446   "Remove the process mark from the next N articles.
10447 If N is negative, unmark backward instead.  The difference between N and
10448 the actual number of articles unmarked is returned."
10449   (interactive "P")
10450   (gnus-summary-mark-as-processable n t))
10451
10452 (defun gnus-summary-unmark-all-processable ()
10453   "Remove the process mark from all articles."
10454   (interactive)
10455   (save-excursion
10456     (while gnus-newsgroup-processable
10457       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10458   (gnus-summary-position-point))
10459
10460 (defun gnus-summary-add-mark (article type)
10461   "Mark ARTICLE with a mark of TYPE."
10462   (let ((vtype (car (assq type gnus-article-mark-lists)))
10463         var)
10464     (if (not vtype)
10465         (error "No such mark type: %s" type)
10466       (setq var (intern (format "gnus-newsgroup-%s" type)))
10467       (set var (cons article (symbol-value var)))
10468       (if (memq type '(processable cached replied forwarded recent saved))
10469           (gnus-summary-update-secondary-mark article)
10470         ;;; !!! This is bogus.  We should find out what primary
10471         ;;; !!! mark we want to set.
10472         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10473
10474 (defun gnus-summary-mark-as-expirable (n)
10475   "Mark N articles forward as expirable.
10476 If N is negative, mark backward instead.  The difference between N and
10477 the actual number of articles marked is returned."
10478   (interactive "p")
10479   (gnus-summary-mark-forward n gnus-expirable-mark))
10480
10481 (defun gnus-summary-mark-as-spam (n)
10482   "Mark N articles forward as spam.
10483 If N is negative, mark backward instead.  The difference between N and
10484 the actual number of articles marked is returned."
10485   (interactive "p")
10486   (gnus-summary-mark-forward n gnus-spam-mark))
10487
10488 (defun gnus-summary-mark-article-as-replied (article)
10489   "Mark ARTICLE as replied to and update the summary line.
10490 ARTICLE can also be a list of articles."
10491   (interactive (list (gnus-summary-article-number)))
10492   (let ((articles (if (listp article) article (list article))))
10493     (dolist (article articles)
10494       (unless (numberp article)
10495         (error "%s is not a number" article))
10496       (push article gnus-newsgroup-replied)
10497       (let ((inhibit-read-only t))
10498         (when (gnus-summary-goto-subject article nil t)
10499           (gnus-summary-update-secondary-mark article))))))
10500
10501 (defun gnus-summary-mark-article-as-forwarded (article)
10502   "Mark ARTICLE as forwarded and update the summary line.
10503 ARTICLE can also be a list of articles."
10504   (let ((articles (if (listp article) article (list article))))
10505     (dolist (article articles)
10506       (push article gnus-newsgroup-forwarded)
10507       (let ((inhibit-read-only t))
10508         (when (gnus-summary-goto-subject article nil t)
10509           (gnus-summary-update-secondary-mark article))))))
10510
10511 (defun gnus-summary-set-bookmark (article)
10512   "Set a bookmark in current article."
10513   (interactive (list (gnus-summary-article-number)))
10514   (when (or (not (get-buffer gnus-article-buffer))
10515             (not gnus-current-article)
10516             (not gnus-article-current)
10517             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10518     (error "No current article selected"))
10519   ;; Remove old bookmark, if one exists.
10520   (gnus-pull article gnus-newsgroup-bookmarks)
10521   ;; Set the new bookmark, which is on the form
10522   ;; (article-number . line-number-in-body).
10523   (push
10524    (cons article
10525          (with-current-buffer gnus-article-buffer
10526            (count-lines
10527             (min (point)
10528                  (save-excursion
10529                    (article-goto-body)
10530                    (point)))
10531             (point))))
10532    gnus-newsgroup-bookmarks)
10533   (gnus-message 6 "A bookmark has been added to the current article."))
10534
10535 (defun gnus-summary-remove-bookmark (article)
10536   "Remove the bookmark from the current article."
10537   (interactive (list (gnus-summary-article-number)))
10538   ;; Remove old bookmark, if one exists.
10539   (if (not (assq article gnus-newsgroup-bookmarks))
10540       (gnus-message 6 "No bookmark in current article.")
10541     (gnus-pull article gnus-newsgroup-bookmarks)
10542     (gnus-message 6 "Removed bookmark.")))
10543
10544 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10545 (defun gnus-summary-mark-as-dormant (n)
10546   "Mark N articles forward as dormant.
10547 If N is negative, mark backward instead.  The difference between N and
10548 the actual number of articles marked is returned."
10549   (interactive "p")
10550   (gnus-summary-mark-forward n gnus-dormant-mark))
10551
10552 (defun gnus-summary-set-process-mark (article)
10553   "Set the process mark on ARTICLE and update the summary line."
10554   (setq gnus-newsgroup-processable
10555         (cons article
10556               (delq article gnus-newsgroup-processable)))
10557   (when (gnus-summary-goto-subject article)
10558     (gnus-summary-show-thread)
10559     (gnus-summary-goto-subject article)
10560     (gnus-summary-update-secondary-mark article)))
10561
10562 (defun gnus-summary-remove-process-mark (&rest articles)
10563   "Remove the process mark from ARTICLES and update the summary line."
10564   (dolist (article articles)
10565     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10566     (when (gnus-summary-goto-subject article)
10567       (gnus-summary-show-thread)
10568       (gnus-summary-goto-subject article)
10569       (gnus-summary-update-secondary-mark article)))
10570   t)
10571
10572 (defun gnus-summary-set-saved-mark (article)
10573   "Set the process mark on ARTICLE and update the summary line."
10574   (push article gnus-newsgroup-saved)
10575   (when (gnus-summary-goto-subject article)
10576     (gnus-summary-update-secondary-mark article)))
10577
10578 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10579   "Mark N articles as read forwards.
10580 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10581 The difference between N and the actual number of articles marked is
10582 returned.
10583 If NO-EXPIRE, auto-expiry will be inhibited."
10584   (interactive "p")
10585   (gnus-summary-show-thread)
10586   (let ((backward (< n 0))
10587         (gnus-summary-goto-unread
10588          (and gnus-summary-goto-unread
10589               (not (eq gnus-summary-goto-unread 'never))
10590               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10591                                     gnus-ticked-mark gnus-dormant-mark)))))
10592         (n (abs n))
10593         (mark (or mark gnus-del-mark)))
10594     (while (and (> n 0)
10595                 (gnus-summary-mark-article nil mark no-expire)
10596                 (zerop (gnus-summary-next-subject
10597                         (if backward -1 1)
10598                         (and gnus-summary-goto-unread
10599                              (not (eq gnus-summary-goto-unread 'never)))
10600                         t)))
10601       (setq n (1- n)))
10602     (when (/= 0 n)
10603       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10604     (gnus-summary-recenter)
10605     (gnus-summary-position-point)
10606     (gnus-set-mode-line 'summary)
10607     n))
10608
10609 (defun gnus-summary-mark-article-as-read (mark)
10610   "Mark the current article quickly as read with MARK."
10611   (let ((article (gnus-summary-article-number)))
10612     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10613     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10614     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10615     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10616     (push (cons article mark) gnus-newsgroup-reads)
10617     ;; Possibly remove from cache, if that is used.
10618     (when gnus-use-cache
10619       (gnus-cache-enter-remove-article article))
10620     ;; Allow the backend to change the mark.
10621     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10622     ;; Check for auto-expiry.
10623     (when (and gnus-newsgroup-auto-expire
10624                (memq mark gnus-auto-expirable-marks))
10625       (setq mark gnus-expirable-mark)
10626       ;; Let the backend know about the mark change.
10627       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10628       (push article gnus-newsgroup-expirable))
10629     ;; Set the mark in the buffer.
10630     (gnus-summary-update-mark mark 'unread)
10631     t))
10632
10633 (defun gnus-summary-mark-article-as-unread (mark)
10634   "Mark the current article quickly as unread with MARK."
10635   (let* ((article (gnus-summary-article-number))
10636          (old-mark (gnus-summary-article-mark article)))
10637     ;; Allow the backend to change the mark.
10638     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10639     (if (eq mark old-mark)
10640         t
10641       (if (<= article 0)
10642           (progn
10643             (gnus-error 1 "Can't mark negative article numbers")
10644             nil)
10645         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10646         (setq gnus-newsgroup-spam-marked
10647               (delq article gnus-newsgroup-spam-marked))
10648         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10649         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10650         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10651         (cond ((= mark gnus-ticked-mark)
10652                (setq gnus-newsgroup-marked
10653                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10654                                               article)))
10655               ((= mark gnus-spam-mark)
10656                (setq gnus-newsgroup-spam-marked
10657                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10658                                               article)))
10659               ((= mark gnus-dormant-mark)
10660                (setq gnus-newsgroup-dormant
10661                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10662                                               article)))
10663               (t
10664                (setq gnus-newsgroup-unreads
10665                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10666                                               article))))
10667         (gnus-pull article gnus-newsgroup-reads)
10668
10669         ;; See whether the article is to be put in the cache.
10670         (and gnus-use-cache
10671              (vectorp (gnus-summary-article-header article))
10672              (save-excursion
10673                (gnus-cache-possibly-enter-article
10674                 gnus-newsgroup-name article
10675                 (= mark gnus-ticked-mark)
10676                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10677
10678         ;; Fix the mark.
10679         (gnus-summary-update-mark mark 'unread)
10680         t))))
10681
10682 (defun gnus-summary-mark-article (&optional article mark no-expire)
10683   "Mark ARTICLE with MARK.  MARK can be any character.
10684 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10685 `??' (dormant) and `?E' (expirable).
10686 If MARK is nil, then the default character `?r' is used.
10687 If ARTICLE is nil, then the article on the current line will be
10688 marked.
10689 If NO-EXPIRE, auto-expiry will be inhibited."
10690   ;; The mark might be a string.
10691   (when (stringp mark)
10692     (setq mark (aref mark 0)))
10693   ;; If no mark is given, then we check auto-expiring.
10694   (when (null mark)
10695     (setq mark gnus-del-mark))
10696   (when (and (not no-expire)
10697              gnus-newsgroup-auto-expire
10698              (memq mark gnus-auto-expirable-marks))
10699     (setq mark gnus-expirable-mark))
10700   (let ((article (or article (gnus-summary-article-number)))
10701         (old-mark (gnus-summary-article-mark article)))
10702     ;; Allow the backend to change the mark.
10703     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10704     (if (eq mark old-mark)
10705         t
10706       (unless article
10707         (error "No article on current line"))
10708       (if (not (if (or (= mark gnus-unread-mark)
10709                        (= mark gnus-ticked-mark)
10710                        (= mark gnus-spam-mark)
10711                        (= mark gnus-dormant-mark))
10712                    (gnus-mark-article-as-unread article mark)
10713                  (gnus-mark-article-as-read article mark)))
10714           t
10715         ;; See whether the article is to be put in the cache.
10716         (and gnus-use-cache
10717              (not (= mark gnus-canceled-mark))
10718              (vectorp (gnus-summary-article-header article))
10719              (save-excursion
10720                (gnus-cache-possibly-enter-article
10721                 gnus-newsgroup-name article
10722                 (= mark gnus-ticked-mark)
10723                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10724
10725         (when (gnus-summary-goto-subject article nil t)
10726           (let ((inhibit-read-only t))
10727             (gnus-summary-show-thread)
10728             ;; Fix the mark.
10729             (gnus-summary-update-mark mark 'unread)
10730             t))))))
10731
10732 (defun gnus-summary-update-secondary-mark (article)
10733   "Update the secondary (read, process, cache) mark."
10734   (gnus-summary-update-mark
10735    (cond ((memq article gnus-newsgroup-processable)
10736           gnus-process-mark)
10737          ((memq article gnus-newsgroup-cached)
10738           gnus-cached-mark)
10739          ((memq article gnus-newsgroup-replied)
10740           gnus-replied-mark)
10741          ((memq article gnus-newsgroup-forwarded)
10742           gnus-forwarded-mark)
10743          ((memq article gnus-newsgroup-saved)
10744           gnus-saved-mark)
10745          ((memq article gnus-newsgroup-recent)
10746           gnus-recent-mark)
10747          ((memq article gnus-newsgroup-unseen)
10748           gnus-unseen-mark)
10749          (t gnus-no-mark))
10750    'replied)
10751   (when (gnus-visual-p 'summary-highlight 'highlight)
10752     (gnus-summary-highlight-line)
10753     (gnus-run-hooks 'gnus-summary-update-hook))
10754   t)
10755
10756 (defun gnus-summary-update-download-mark (article)
10757   "Update the download mark."
10758   (gnus-summary-update-mark
10759    (cond ((memq article gnus-newsgroup-undownloaded)
10760           gnus-undownloaded-mark)
10761          (gnus-newsgroup-agentized
10762           gnus-downloaded-mark)
10763          (t
10764           gnus-no-mark))
10765    'download)
10766   (gnus-summary-update-line t)
10767   t)
10768
10769 (defun gnus-summary-update-mark (mark type)
10770   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10771         (inhibit-read-only t))
10772     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10773     (when forward
10774       (when (looking-at "\r")
10775         (incf forward))
10776       (when (<= (+ forward (point)) (point-max))
10777         ;; Go to the right position on the line.
10778         (goto-char (+ forward (point)))
10779         ;; Replace the old mark with the new mark.
10780         (let ((to-insert
10781                (mm-subst-char-in-string
10782                 (char-after) mark
10783                 (buffer-substring (point) (1+ (point))))))
10784           (delete-region (point) (1+ (point)))
10785           (insert to-insert))
10786         ;; Optionally update the marks by some user rule.
10787         (when (eq type 'unread)
10788           (gnus-data-set-mark
10789            (gnus-data-find (gnus-summary-article-number)) mark)
10790           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10791
10792 (defun gnus-mark-article-as-read (article &optional mark)
10793   "Enter ARTICLE in the pertinent lists and remove it from others."
10794   ;; Make the article expirable.
10795   (let ((mark (or mark gnus-del-mark)))
10796     (setq gnus-newsgroup-expirable
10797           (if (= mark gnus-expirable-mark)
10798               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10799             (delq article gnus-newsgroup-expirable)))
10800     ;; Remove from unread and marked lists.
10801     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10802     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10803     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10804     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10805     (push (cons article mark) gnus-newsgroup-reads)
10806     ;; Possibly remove from cache, if that is used.
10807     (when gnus-use-cache
10808       (gnus-cache-enter-remove-article article))
10809     t))
10810
10811 (defun gnus-mark-article-as-unread (article &optional mark)
10812   "Enter ARTICLE in the pertinent lists and remove it from others."
10813   (let ((mark (or mark gnus-ticked-mark)))
10814     (if (<= article 0)
10815         (progn
10816           (gnus-error 1 "Can't mark negative article numbers")
10817           nil)
10818       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10819             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10820             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10821             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10822             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10823
10824       ;; Unsuppress duplicates?
10825       (when gnus-suppress-duplicates
10826         (gnus-dup-unsuppress-article article))
10827
10828       (cond ((= mark gnus-ticked-mark)
10829              (setq gnus-newsgroup-marked
10830                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10831             ((= mark gnus-spam-mark)
10832              (setq gnus-newsgroup-spam-marked
10833                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10834                                             article)))
10835             ((= mark gnus-dormant-mark)
10836              (setq gnus-newsgroup-dormant
10837                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10838             (t
10839              (setq gnus-newsgroup-unreads
10840                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10841       (gnus-pull article gnus-newsgroup-reads)
10842       t)))
10843
10844 (defalias 'gnus-summary-mark-as-unread-forward
10845   'gnus-summary-tick-article-forward)
10846 (make-obsolete 'gnus-summary-mark-as-unread-forward
10847                'gnus-summary-tick-article-forward "Emacs 20.4")
10848 (defun gnus-summary-tick-article-forward (n)
10849   "Tick N articles forwards.
10850 If N is negative, tick backwards instead.
10851 The difference between N and the number of articles ticked is returned."
10852   (interactive "p")
10853   (gnus-summary-mark-forward n gnus-ticked-mark))
10854
10855 (defalias 'gnus-summary-mark-as-unread-backward
10856   'gnus-summary-tick-article-backward)
10857 (make-obsolete 'gnus-summary-mark-as-unread-backward
10858                'gnus-summary-tick-article-backward "Emacs 20.4")
10859 (defun gnus-summary-tick-article-backward (n)
10860   "Tick N articles backwards.
10861 The difference between N and the number of articles ticked is returned."
10862   (interactive "p")
10863   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10864
10865 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10866 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10867 (defun gnus-summary-tick-article (&optional article clear-mark)
10868   "Mark current article as unread.
10869 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10870 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10871   (interactive)
10872   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10873                                        gnus-ticked-mark)))
10874
10875 (defun gnus-summary-mark-as-read-forward (n)
10876   "Mark N articles as read forwards.
10877 If N is negative, mark backwards instead.
10878 The difference between N and the actual number of articles marked is
10879 returned."
10880   (interactive "p")
10881   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10882
10883 (defun gnus-summary-mark-as-read-backward (n)
10884   "Mark the N articles as read backwards.
10885 The difference between N and the actual number of articles marked is
10886 returned."
10887   (interactive "p")
10888   (gnus-summary-mark-forward
10889    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10890
10891 (defun gnus-summary-mark-as-read (&optional article mark)
10892   "Mark current article as read.
10893 ARTICLE specifies the article to be marked as read.
10894 MARK specifies a string to be inserted at the beginning of the line."
10895   (gnus-summary-mark-article article mark))
10896
10897 (defun gnus-summary-clear-mark-forward (n)
10898   "Clear marks from N articles forward.
10899 If N is negative, clear backward instead.
10900 The difference between N and the number of marks cleared is returned."
10901   (interactive "p")
10902   (gnus-summary-mark-forward n gnus-unread-mark))
10903
10904 (defun gnus-summary-clear-mark-backward (n)
10905   "Clear marks from N articles backward.
10906 The difference between N and the number of marks cleared is returned."
10907   (interactive "p")
10908   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10909
10910 (defun gnus-summary-mark-unread-as-read ()
10911   "Intended to be used by `gnus-mark-article-hook'."
10912   (when (memq gnus-current-article gnus-newsgroup-unreads)
10913     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10914
10915 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10916   "Intended to be used by `gnus-mark-article-hook'."
10917   (let ((mark (gnus-summary-article-mark)))
10918     (when (or (gnus-unread-mark-p mark)
10919               (gnus-read-mark-p mark))
10920       (gnus-summary-mark-article gnus-current-article
10921                                  (or new-mark gnus-read-mark)))))
10922
10923 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10924   "Intended to be used by `gnus-mark-article-hook'."
10925   (let ((mark (gnus-summary-article-mark)))
10926     (when (or (gnus-unread-mark-p mark)
10927               (gnus-read-mark-p mark))
10928       (gnus-summary-mark-article (gnus-summary-article-number)
10929                                  (or new-mark gnus-read-mark)))))
10930
10931 (defun gnus-summary-mark-unread-as-ticked ()
10932   "Intended to be used by `gnus-mark-article-hook'."
10933   (when (memq gnus-current-article gnus-newsgroup-unreads)
10934     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10935
10936 (defun gnus-summary-mark-region-as-read (point mark all)
10937   "Mark all unread articles between point and mark as read.
10938 If given a prefix, mark all articles between point and mark as read,
10939 even ticked and dormant ones."
10940   (interactive "r\nP")
10941   (save-excursion
10942     (let (article)
10943       (goto-char point)
10944       (beginning-of-line)
10945       (while (and
10946               (< (point) mark)
10947               (progn
10948                 (when (or all
10949                           (memq (setq article (gnus-summary-article-number))
10950                                 gnus-newsgroup-unreads))
10951                   (gnus-summary-mark-article article gnus-del-mark))
10952                 t)
10953               (gnus-summary-find-next))))))
10954
10955 (defun gnus-summary-mark-below (score mark)
10956   "Mark articles with score less than SCORE with MARK."
10957   (interactive "P\ncMark: ")
10958   (setq score (if score
10959                   (prefix-numeric-value score)
10960                 (or gnus-summary-default-score 0)))
10961   (with-current-buffer gnus-summary-buffer
10962     (goto-char (point-min))
10963     (while
10964         (progn
10965           (and (< (gnus-summary-article-score) score)
10966                (gnus-summary-mark-article nil mark))
10967           (gnus-summary-find-next)))))
10968
10969 (defun gnus-summary-kill-below (&optional score)
10970   "Mark articles with score below SCORE as read."
10971   (interactive "P")
10972   (gnus-summary-mark-below score gnus-killed-mark))
10973
10974 (defun gnus-summary-clear-above (&optional score)
10975   "Clear all marks from articles with score above SCORE."
10976   (interactive "P")
10977   (gnus-summary-mark-above score gnus-unread-mark))
10978
10979 (defun gnus-summary-tick-above (&optional score)
10980   "Tick all articles with score above SCORE."
10981   (interactive "P")
10982   (gnus-summary-mark-above score gnus-ticked-mark))
10983
10984 (defun gnus-summary-mark-above (score mark)
10985   "Mark articles with score over SCORE with MARK."
10986   (interactive "P\ncMark: ")
10987   (setq score (if score
10988                   (prefix-numeric-value score)
10989                 (or gnus-summary-default-score 0)))
10990   (with-current-buffer gnus-summary-buffer
10991     (goto-char (point-min))
10992     (while (and (progn
10993                   (when (> (gnus-summary-article-score) score)
10994                     (gnus-summary-mark-article nil mark))
10995                   t)
10996                 (gnus-summary-find-next)))))
10997
10998 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10999 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11000 (defun gnus-summary-limit-include-expunged (&optional no-error)
11001   "Display all the hidden articles that were expunged for low scores."
11002   (interactive)
11003   (let ((inhibit-read-only t))
11004     (let ((scored gnus-newsgroup-scored)
11005           headers h)
11006       (while scored
11007         (unless (gnus-summary-article-header (caar scored))
11008           (and (setq h (gnus-number-to-header (caar scored)))
11009                (< (cdar scored) gnus-summary-expunge-below)
11010                (push h headers)))
11011         (setq scored (cdr scored)))
11012       (if (not headers)
11013           (when (not no-error)
11014             (error "No expunged articles hidden"))
11015         (goto-char (point-min))
11016         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11017         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11018         (dolist (x headers)
11019           (push (mail-header-number x) gnus-newsgroup-limit))
11020         (gnus-summary-prepare-unthreaded (nreverse headers))
11021         (goto-char (point-min))
11022         (gnus-summary-position-point)
11023         t))))
11024
11025 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11026   "Mark all unread articles in this newsgroup as read.
11027 If prefix argument ALL is non-nil, ticked and dormant articles will
11028 also be marked as read.
11029 If QUIETLY is non-nil, no questions will be asked.
11030
11031 If TO-HERE is non-nil, it should be a point in the buffer.  All
11032 articles before (after, if REVERSE is set) this point will be marked
11033 as read.
11034
11035 Note that this function will only catch up the unread article
11036 in the current summary buffer limitation.
11037
11038 The number of articles marked as read is returned."
11039   (interactive "P")
11040   (prog1
11041       (save-excursion
11042         (when (or quietly
11043                   (not gnus-interactive-catchup) ;Without confirmation?
11044                   gnus-expert-user
11045                   (gnus-y-or-n-p
11046                    (if all
11047                        "Mark absolutely all articles as read? "
11048                      "Mark all unread articles as read? ")))
11049           (if (and not-mark
11050                    (not gnus-newsgroup-adaptive)
11051                    (not gnus-newsgroup-auto-expire)
11052                    (not gnus-suppress-duplicates)
11053                    (or (not gnus-use-cache)
11054                        (eq gnus-use-cache 'passive)))
11055               (progn
11056                 (when all
11057                   (setq gnus-newsgroup-marked nil
11058                         gnus-newsgroup-spam-marked nil
11059                         gnus-newsgroup-dormant nil))
11060                 (setq gnus-newsgroup-unreads
11061                       (gnus-sorted-nunion
11062                        (gnus-sorted-intersection gnus-newsgroup-unreads
11063                                                  gnus-newsgroup-downloadable)
11064                        (gnus-sorted-difference gnus-newsgroup-unfetched
11065                                                gnus-newsgroup-cached))))
11066             ;; We actually mark all articles as canceled, which we
11067             ;; have to do when using auto-expiry or adaptive scoring.
11068             (gnus-summary-show-all-threads)
11069             (if (and to-here reverse)
11070                 (progn
11071                   (goto-char to-here)
11072                   (gnus-summary-mark-current-read-and-unread-as-read
11073                    gnus-catchup-mark)
11074                   (while (gnus-summary-find-next (not all))
11075                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11076               (when (gnus-summary-first-subject (not all))
11077                 (while (and
11078                         (if to-here (< (point) to-here) t)
11079                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11080                         (gnus-summary-find-next (not all))))))
11081             (gnus-set-mode-line 'summary))
11082           t))
11083     (gnus-summary-position-point)))
11084
11085 (defun gnus-summary-catchup-to-here (&optional all)
11086   "Mark all unticked articles before the current one as read.
11087 If ALL is non-nil, also mark ticked and dormant articles as read."
11088   (interactive "P")
11089   (save-excursion
11090     (gnus-save-hidden-threads
11091       (let ((beg (point)))
11092         ;; We check that there are unread articles.
11093         (when (or all (gnus-summary-find-prev))
11094           (gnus-summary-catchup all t beg)))))
11095   (gnus-summary-position-point))
11096
11097 (defun gnus-summary-catchup-from-here (&optional all)
11098   "Mark all unticked articles after (and including) the current one as read.
11099 If ALL is non-nil, also mark ticked and dormant articles as read."
11100   (interactive "P")
11101   (save-excursion
11102     (gnus-save-hidden-threads
11103       (let ((beg (point)))
11104         ;; We check that there are unread articles.
11105         (when (or all (gnus-summary-find-next))
11106           (gnus-summary-catchup all t beg nil t)))))
11107   (gnus-summary-position-point))
11108
11109 (defun gnus-summary-catchup-all (&optional quietly)
11110   "Mark all articles in this newsgroup as read.
11111 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11112 instead, which marks only unread articles as read."
11113   (interactive "P")
11114   (gnus-summary-catchup t quietly))
11115
11116 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11117   "Mark all unread articles in this group as read, then exit.
11118 If prefix argument ALL is non-nil, all articles are marked as read.
11119 If QUIETLY is non-nil, no questions will be asked."
11120   (interactive "P")
11121   (when (gnus-summary-catchup all quietly nil 'fast)
11122     ;; Select next newsgroup or exit.
11123     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11124              (eq gnus-auto-select-next 'quietly))
11125         (gnus-summary-next-group nil)
11126       (gnus-summary-exit))))
11127
11128 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11129   "Mark all articles in this newsgroup as read, and then exit.
11130 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11131 instead, which marks only unread articles as read."
11132   (interactive "P")
11133   (gnus-summary-catchup-and-exit t quietly))
11134
11135 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11136   "Mark all articles in this group as read and select the next group.
11137 If given a prefix, mark all articles, unread as well as ticked, as
11138 read."
11139   (interactive "P")
11140   (save-excursion
11141     (gnus-summary-catchup all))
11142   (gnus-summary-next-group))
11143
11144 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11145   "Mark all articles in this group as read and select the previous group.
11146 If given a prefix, mark all articles, unread as well as ticked, as
11147 read."
11148   (interactive "P")
11149   (save-excursion
11150     (gnus-summary-catchup all))
11151   (gnus-summary-next-group nil nil t))
11152
11153 ;;;
11154 ;;; with article
11155 ;;;
11156
11157 (defmacro gnus-with-article (article &rest forms)
11158   "Select ARTICLE and perform FORMS in the original article buffer.
11159 Then replace the article with the result."
11160   `(progn
11161      ;; We don't want the article to be marked as read.
11162      (let (gnus-mark-article-hook)
11163        (gnus-summary-select-article t t nil ,article))
11164      (set-buffer gnus-original-article-buffer)
11165      ,@forms
11166      (if (not (gnus-check-backend-function
11167                'request-replace-article (car gnus-article-current)))
11168          (gnus-message 5 "Read-only group; not replacing")
11169        (unless (gnus-request-replace-article
11170                 ,article (car gnus-article-current)
11171                 (current-buffer) t)
11172          (error "Couldn't replace article")))
11173      ;; The cache and backlog have to be flushed somewhat.
11174      (when gnus-keep-backlog
11175        (gnus-backlog-remove-article
11176         (car gnus-article-current) (cdr gnus-article-current)))
11177      (when gnus-use-cache
11178        (gnus-cache-update-article
11179         (car gnus-article-current) (cdr gnus-article-current)))))
11180
11181 (put 'gnus-with-article 'lisp-indent-function 1)
11182 (put 'gnus-with-article 'edebug-form-spec '(form body))
11183
11184 ;; Thread-based commands.
11185
11186 (defun gnus-summary-articles-in-thread (&optional article)
11187   "Return a list of all articles in the current thread.
11188 If ARTICLE is non-nil, return all articles in the thread that starts
11189 with that article."
11190   (let* ((article (or article (gnus-summary-article-number)))
11191          (data (gnus-data-find-list article))
11192          (top-level (gnus-data-level (car data)))
11193          (top-subject
11194           (cond ((null gnus-thread-operation-ignore-subject)
11195                  (gnus-simplify-subject-re
11196                   (mail-header-subject (gnus-data-header (car data)))))
11197                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11198                  (gnus-simplify-subject-fuzzy
11199                   (mail-header-subject (gnus-data-header (car data)))))
11200                 (t nil)))
11201          (end-point (save-excursion
11202                       (if (gnus-summary-go-to-next-thread)
11203                           (point) (point-max))))
11204          articles)
11205     (while (and data
11206                 (< (gnus-data-pos (car data)) end-point))
11207       (when (or (not top-subject)
11208                 (string= top-subject
11209                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11210                              (gnus-simplify-subject-fuzzy
11211                               (mail-header-subject
11212                                (gnus-data-header (car data))))
11213                            (gnus-simplify-subject-re
11214                             (mail-header-subject
11215                              (gnus-data-header (car data)))))))
11216         (push (gnus-data-number (car data)) articles))
11217       (unless (and (setq data (cdr data))
11218                    (> (gnus-data-level (car data)) top-level))
11219         (setq data nil)))
11220     ;; Return the list of articles.
11221     (nreverse articles)))
11222
11223 (defun gnus-summary-rethread-current ()
11224   "Rethread the thread the current article is part of."
11225   (interactive)
11226   (let* ((gnus-show-threads t)
11227          (article (gnus-summary-article-number))
11228          (id (mail-header-id (gnus-summary-article-header)))
11229          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11230     (unless id
11231       (error "No article on the current line"))
11232     (gnus-rebuild-thread id)
11233     (gnus-summary-goto-subject article)))
11234
11235 (defun gnus-summary-reparent-thread ()
11236   "Make the current article child of the marked (or previous) article.
11237
11238 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11239 is non-nil or the Subject: of both articles are the same."
11240   (interactive)
11241   (unless (not (gnus-group-read-only-p))
11242     (error "The current newsgroup does not support article editing"))
11243   (unless (<= (length gnus-newsgroup-processable) 1)
11244     (error "No more than one article may be marked"))
11245   (let ((child (gnus-summary-article-number))
11246         ;; First grab the marked article, otherwise one line up.
11247         (parent (if (not (null gnus-newsgroup-processable))
11248                     (car gnus-newsgroup-processable)
11249                   (save-excursion
11250                     (if (eq (forward-line -1) 0)
11251                         (gnus-summary-article-number)
11252                       (error "Beginning of summary buffer"))))))
11253     (gnus-summary-reparent-children parent (list child))))
11254
11255 (defun gnus-summary-reparent-children (parent children)
11256   "Make PARENT the parent of CHILDREN.
11257 When called interactively, PARENT is the current article and CHILDREN
11258 are the process-marked articles."
11259   (interactive
11260    (list (gnus-summary-article-number)
11261          (gnus-summary-work-articles nil)))
11262   (dolist (child children)
11263     (save-window-excursion
11264       (let ((gnus-article-buffer " *reparent*"))
11265         (unless (not (eq parent child))
11266           (error "An article may not be self-referential"))
11267         (let ((message-id (mail-header-id
11268                            (gnus-summary-article-header parent))))
11269           (unless (and message-id (not (equal message-id "")))
11270             (error "No message-id in desired parent"))
11271           (gnus-with-article child
11272             (save-restriction
11273               (goto-char (point-min))
11274               (message-narrow-to-head)
11275               (if (re-search-forward "^References: " nil t)
11276                   (progn
11277                     (re-search-forward "^[^ \t]" nil t)
11278                     (forward-line -1)
11279                     (end-of-line)
11280                     (insert " " message-id))
11281                 (insert "References: " message-id "\n"))))
11282           (set-buffer gnus-summary-buffer)
11283           (gnus-summary-unmark-all-processable)
11284           (gnus-summary-update-article child)
11285           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11286             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11287           (gnus-summary-rethread-current)
11288           (gnus-message 3 "Article %d is now the child of article %d"
11289                         child parent))))))
11290
11291 (defun gnus-summary-toggle-threads (&optional arg)
11292   "Toggle showing conversation threads.
11293 If ARG is positive number, turn showing conversation threads on."
11294   (interactive "P")
11295   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11296     (setq gnus-show-threads
11297           (if (null arg) (not gnus-show-threads)
11298             (> (prefix-numeric-value arg) 0)))
11299     (gnus-summary-prepare)
11300     (gnus-summary-goto-subject current)
11301     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11302     (gnus-summary-position-point)))
11303
11304 (eval-and-compile
11305   (if (fboundp 'remove-overlays)
11306       (defalias 'gnus-remove-overlays 'remove-overlays)
11307     (defun gnus-remove-overlays (beg end name val)
11308       "Clear BEG and END of overlays whose property NAME has value VAL.
11309 For compatibility with Emacs 21 and XEmacs."
11310       (dolist (ov (gnus-overlays-in beg end))
11311         (when (eq (gnus-overlay-get ov name) val)
11312           (gnus-delete-overlay ov))))))
11313
11314 (defun gnus-summary-show-all-threads ()
11315   "Show all threads."
11316   (interactive)
11317   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11318   (gnus-summary-position-point))
11319
11320 (defun gnus-summary-show-thread ()
11321   "Show thread subtrees.
11322 Returns nil if no thread was there to be shown."
11323   (interactive)
11324   (let* ((orig (point))
11325          (end (point-at-eol))
11326          ;; Leave point at bol
11327          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11328          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11329                 (if (fboundp 'next-single-char-property-change)
11330                     (or (next-single-char-property-change end 'invisible)
11331                         (point-max))
11332                   (while (progn
11333                            (end-of-line 2)
11334                            (and (not (eobp))
11335                                 (eq (get-char-property (point) 'invisible)
11336                                     'gnus-sum))))
11337                   (point)))))
11338     (when eoi
11339       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11340       (goto-char orig)
11341       (gnus-summary-position-point)
11342       eoi)))
11343
11344 (defun gnus-summary-maybe-hide-threads ()
11345   "If requested, hide the threads that should be hidden."
11346   (when (and gnus-show-threads
11347              gnus-thread-hide-subtree)
11348     (gnus-summary-hide-all-threads
11349      (if (or (consp gnus-thread-hide-subtree)
11350              (functionp gnus-thread-hide-subtree))
11351          (gnus-make-predicate gnus-thread-hide-subtree)
11352        nil))))
11353
11354 ;;; Hiding predicates.
11355
11356 (defun gnus-article-unread-p (header)
11357   (memq (mail-header-number header) gnus-newsgroup-unreads))
11358
11359 (defun gnus-article-unseen-p (header)
11360   (memq (mail-header-number header) gnus-newsgroup-unseen))
11361
11362 (defun gnus-map-articles (predicate articles)
11363   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11364   (apply 'gnus-or (mapcar predicate
11365                           (mapcar (lambda (number)
11366                                     (gnus-summary-article-header number))
11367                                   articles))))
11368
11369 (defun gnus-summary-hide-all-threads (&optional predicate)
11370   "Hide all thread subtrees.
11371 If PREDICATE is supplied, threads that satisfy this predicate
11372 will not be hidden."
11373   (interactive)
11374   (save-excursion
11375     (goto-char (point-min))
11376     (let ((end nil))
11377       (while (not end)
11378         (when (or (not predicate)
11379                   (gnus-map-articles
11380                    predicate (gnus-summary-article-children)))
11381             (gnus-summary-hide-thread))
11382         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11383   (gnus-summary-position-point))
11384
11385 (defun gnus-summary-hide-thread ()
11386   "Hide thread subtrees.
11387 If PREDICATE is supplied, threads that satisfy this predicate
11388 will not be hidden.
11389 Returns nil if no threads were there to be hidden."
11390   (interactive)
11391   (let ((start (point))
11392         (starteol (line-end-position))
11393         (article (gnus-summary-article-number)))
11394     (goto-char start)
11395     ;; Go forward until either the buffer ends or the subthread ends.
11396     (when (and (not (eobp))
11397                (or (zerop (gnus-summary-next-thread 1 t))
11398                    (goto-char (point-max))))
11399       (if (and (> (point) start)
11400                ;; FIXME: this should actually search for a non-invisible \n.
11401                (search-backward "\n" start t))
11402           (progn
11403             (when (> (point) starteol)
11404               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11405               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11406                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11407                 (gnus-overlay-put ol 'evaporate t)))
11408             (gnus-summary-goto-subject article))
11409         (goto-char start)
11410         nil))))
11411
11412 (defun gnus-summary-go-to-next-thread (&optional previous)
11413   "Go to the same level (or less) next thread.
11414 If PREVIOUS is non-nil, go to previous thread instead.
11415 Return the article number moved to, or nil if moving was impossible."
11416   (let ((level (gnus-summary-thread-level))
11417         (way (if previous -1 1))
11418         (beg (point)))
11419     (forward-line way)
11420     (while (and (not (eobp))
11421                 (< level (gnus-summary-thread-level)))
11422       (forward-line way))
11423     (if (eobp)
11424         (progn
11425           (goto-char beg)
11426           nil)
11427       (setq beg (point))
11428       (prog1
11429           (gnus-summary-article-number)
11430         (goto-char beg)))))
11431
11432 (defun gnus-summary-next-thread (n &optional silent)
11433   "Go to the same level next N'th thread.
11434 If N is negative, search backward instead.
11435 Returns the difference between N and the number of skips actually
11436 done.
11437
11438 If SILENT, don't output messages."
11439   (interactive "p")
11440   (let ((backward (< n 0))
11441         (n (abs n)))
11442     (while (and (> n 0)
11443                 (gnus-summary-go-to-next-thread backward))
11444       (decf n))
11445     (unless silent
11446       (gnus-summary-position-point))
11447     (when (and (not silent) (/= 0 n))
11448       (gnus-message 7 "No more threads"))
11449     n))
11450
11451 (defun gnus-summary-prev-thread (n)
11452   "Go to the same level previous N'th thread.
11453 Returns the difference between N and the number of skips actually
11454 done."
11455   (interactive "p")
11456   (gnus-summary-next-thread (- n)))
11457
11458 (defun gnus-summary-go-down-thread ()
11459   "Go down one level in the current thread."
11460   (let ((children (gnus-summary-article-children)))
11461     (when children
11462       (gnus-summary-goto-subject (car children)))))
11463
11464 (defun gnus-summary-go-up-thread ()
11465   "Go up one level in the current thread."
11466   (let ((parent (gnus-summary-article-parent)))
11467     (when parent
11468       (gnus-summary-goto-subject parent))))
11469
11470 (defun gnus-summary-down-thread (n)
11471   "Go down thread N steps.
11472 If N is negative, go up instead.
11473 Returns the difference between N and how many steps down that were
11474 taken."
11475   (interactive "p")
11476   (let ((up (< n 0))
11477         (n (abs n)))
11478     (while (and (> n 0)
11479                 (if up (gnus-summary-go-up-thread)
11480                   (gnus-summary-go-down-thread)))
11481       (setq n (1- n)))
11482     (gnus-summary-position-point)
11483     (when (/= 0 n)
11484       (gnus-message 7 "Can't go further"))
11485     n))
11486
11487 (defun gnus-summary-up-thread (n)
11488   "Go up thread N steps.
11489 If N is negative, go down instead.
11490 Returns the difference between N and how many steps down that were
11491 taken."
11492   (interactive "p")
11493   (gnus-summary-down-thread (- n)))
11494
11495 (defun gnus-summary-top-thread ()
11496   "Go to the top of the thread."
11497   (interactive)
11498   (while (gnus-summary-go-up-thread))
11499   (gnus-summary-article-number))
11500
11501 (defun gnus-summary-expire-thread ()
11502   "Mark articles under current thread as expired."
11503   (interactive)
11504   (gnus-summary-kill-thread 0))
11505
11506 (defun gnus-summary-kill-thread (&optional unmark)
11507   "Mark articles under current thread as read.
11508 If the prefix argument is positive, remove any kinds of marks.
11509 If the prefix argument is zero, mark thread as expired.
11510 If the prefix argument is negative, tick articles instead."
11511   (interactive "P")
11512   (when unmark
11513     (setq unmark (prefix-numeric-value unmark)))
11514   (let ((articles (gnus-summary-articles-in-thread))
11515         (hide (or (null unmark) (= unmark 0))))
11516     (save-excursion
11517       ;; Expand the thread.
11518       (gnus-summary-show-thread)
11519       ;; Mark all the articles.
11520       (while articles
11521         (gnus-summary-goto-subject (car articles))
11522         (cond ((null unmark)
11523                (gnus-summary-mark-article-as-read gnus-killed-mark))
11524               ((> unmark 0)
11525                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11526               ((= unmark 0)
11527                (gnus-summary-mark-article nil gnus-expirable-mark))
11528               (t
11529                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11530         (setq articles (cdr articles))))
11531     ;; Hide killed subtrees when hide is true.
11532     (and hide
11533          gnus-thread-hide-killed
11534          (gnus-summary-hide-thread))
11535     ;; If hide is t, go to next unread subject.
11536     (when hide
11537       ;; Go to next unread subject.
11538       (gnus-summary-next-subject 1 t)))
11539   (gnus-set-mode-line 'summary))
11540
11541 ;; Summary sorting commands
11542
11543 (defun gnus-summary-sort-by-number (&optional reverse)
11544   "Sort the summary buffer by article number.
11545 Argument REVERSE means reverse order."
11546   (interactive "P")
11547   (gnus-summary-sort 'number reverse))
11548
11549 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11550   "Sort the summary buffer by most recent article number.
11551 Argument REVERSE means reverse order."
11552   (interactive "P")
11553   (gnus-summary-sort 'most-recent-number reverse))
11554
11555 (defun gnus-summary-sort-by-random (&optional reverse)
11556   "Randomize the order in the summary buffer.
11557 Argument REVERSE means to randomize in reverse order."
11558   (interactive "P")
11559   (gnus-summary-sort 'random reverse))
11560
11561 (defun gnus-summary-sort-by-author (&optional reverse)
11562   "Sort the summary buffer by author name alphabetically.
11563 If `case-fold-search' is non-nil, case of letters is ignored.
11564 Argument REVERSE means reverse order."
11565   (interactive "P")
11566   (gnus-summary-sort 'author reverse))
11567
11568 (defun gnus-summary-sort-by-recipient (&optional reverse)
11569   "Sort the summary buffer by recipient name alphabetically.
11570 If `case-fold-search' is non-nil, case of letters is ignored.
11571 Argument REVERSE means reverse order."
11572   (interactive "P")
11573   (gnus-summary-sort 'recipient reverse))
11574
11575 (defun gnus-summary-sort-by-subject (&optional reverse)
11576   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11577 If `case-fold-search' is non-nil, case of letters is ignored.
11578 Argument REVERSE means reverse order."
11579   (interactive "P")
11580   (gnus-summary-sort 'subject reverse))
11581
11582 (defun gnus-summary-sort-by-date (&optional reverse)
11583   "Sort the summary buffer by date.
11584 Argument REVERSE means reverse order."
11585   (interactive "P")
11586   (gnus-summary-sort 'date reverse))
11587
11588 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11589   "Sort the summary buffer by most recent date.
11590 Argument REVERSE means reverse order."
11591   (interactive "P")
11592   (gnus-summary-sort 'most-recent-date reverse))
11593
11594 (defun gnus-summary-sort-by-score (&optional reverse)
11595   "Sort the summary buffer by score.
11596 Argument REVERSE means reverse order."
11597   (interactive "P")
11598   (gnus-summary-sort 'score reverse))
11599
11600 (defun gnus-summary-sort-by-lines (&optional reverse)
11601   "Sort the summary buffer by the number of lines.
11602 Argument REVERSE means reverse order."
11603   (interactive "P")
11604   (gnus-summary-sort 'lines reverse))
11605
11606 (defun gnus-summary-sort-by-chars (&optional reverse)
11607   "Sort the summary buffer by article length.
11608 Argument REVERSE means reverse order."
11609   (interactive "P")
11610   (gnus-summary-sort 'chars reverse))
11611
11612 (defun gnus-summary-sort-by-original (&optional reverse)
11613   "Sort the summary buffer using the default sorting method.
11614 Argument REVERSE means reverse order."
11615   (interactive "P")
11616   (let* ((inhibit-read-only t)
11617          (gnus-summary-prepare-hook nil))
11618     ;; We do the sorting by regenerating the threads.
11619     (gnus-summary-prepare)
11620     ;; Hide subthreads if needed.
11621     (gnus-summary-maybe-hide-threads)))
11622
11623 (defun gnus-summary-sort (predicate reverse)
11624   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11625   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11626          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11627          (gnus-thread-sort-functions
11628           (if (not reverse)
11629               thread
11630             `(lambda (t1 t2)
11631                (,thread t2 t1))))
11632          (gnus-sort-gathered-threads-function
11633           gnus-thread-sort-functions)
11634          (gnus-article-sort-functions
11635           (if (not reverse)
11636               article
11637             `(lambda (t1 t2)
11638                (,article t2 t1))))
11639          (inhibit-read-only t)
11640          (gnus-summary-prepare-hook nil))
11641     ;; We do the sorting by regenerating the threads.
11642     (gnus-summary-prepare)
11643     ;; Hide subthreads if needed.
11644     (gnus-summary-maybe-hide-threads)))
11645
11646 ;; Summary saving commands.
11647
11648 (defun gnus-summary-save-article (&optional n not-saved)
11649   "Save the current article using the default saver function.
11650 If N is a positive number, save the N next articles.
11651 If N is a negative number, save the N previous articles.
11652 If N is nil and any articles have been marked with the process mark,
11653 save those articles instead.
11654 The variable `gnus-default-article-saver' specifies the saver function.
11655
11656 If the optional second argument NOT-SAVED is non-nil, articles saved
11657 will not be marked as saved."
11658   (interactive "P")
11659   (require 'gnus-art)
11660   (let* ((articles (gnus-summary-work-articles n))
11661          (save-buffer (save-excursion
11662                         (nnheader-set-temp-buffer " *Gnus Save*")))
11663          (num (length articles))
11664          ;; Whether to save decoded articles or raw articles.
11665          (decode (when gnus-article-save-coding-system
11666                    (get gnus-default-article-saver :decode)))
11667          ;; When saving many articles in a single file, use the other
11668          ;; function to save articles other than the first one.
11669          (saver2 (get gnus-default-article-saver :function))
11670          (gnus-prompt-before-saving (if saver2
11671                                         t
11672                                       gnus-prompt-before-saving))
11673          (gnus-default-article-saver gnus-default-article-saver)
11674          header file)
11675     (dolist (article articles)
11676       (setq header (gnus-summary-article-header article))
11677       (if (not (vectorp header))
11678           ;; This is a pseudo-article.
11679           (if (assq 'name header)
11680               (gnus-copy-file (cdr (assq 'name header)))
11681             (gnus-message 1 "Article %d is unsaveable" article))
11682         ;; This is a real article.
11683         (save-window-excursion
11684           (gnus-summary-select-article decode decode nil article)
11685           (gnus-summary-goto-subject article))
11686         (with-current-buffer save-buffer
11687           (erase-buffer)
11688           (insert-buffer-substring (if decode
11689                                        gnus-article-buffer
11690                                      gnus-original-article-buffer)))
11691         (setq file (gnus-article-save save-buffer file num))
11692         (gnus-summary-remove-process-mark article)
11693         (unless not-saved
11694           (gnus-summary-set-saved-mark article)))
11695       (when saver2
11696         (setq gnus-default-article-saver saver2
11697               saver2 nil)))
11698     (gnus-kill-buffer save-buffer)
11699     (gnus-summary-position-point)
11700     (gnus-set-mode-line 'summary)
11701     n))
11702
11703 (defun gnus-summary-pipe-output (&optional n sym)
11704   "Pipe the current article to a subprocess.
11705 If N is a positive number, pipe the N next articles.
11706 If N is a negative number, pipe the N previous articles.
11707 If N is nil and any articles have been marked with the process mark,
11708 pipe those articles instead.
11709 The default command to which articles are piped is specified by the
11710 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11711 will be prompted for the command.
11712
11713 The properties `:decode' and `:headers' that are put to the function
11714 symbol `gnus-summary-save-in-pipe' control whether this function
11715 decodes articles and what headers to keep (see the doc string for the
11716 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11717 is neither omitted nor the symbol `r', force including all headers
11718 regardless of the `:headers' property.  If it is the symbol `r',
11719 articles that are not decoded and include all headers will be piped
11720 no matter what the properties `:decode' and `:headers' are."
11721   (interactive (gnus-interactive "P\ny"))
11722   (require 'gnus-art)
11723   (let* ((articles (gnus-summary-work-articles n))
11724          (result-buffer "*Shell Command Output*")
11725          (all-headers (not (memq sym '(nil r))))
11726          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11727          (raw (eq sym 'r))
11728          (headers (get 'gnus-summary-save-in-pipe :headers))
11729          command result)
11730     (unless (numberp (car articles))
11731       (error "No article to pipe"))
11732     (setq command (gnus-read-shell-command
11733                    (concat "Shell command on "
11734                            (if (cdr articles)
11735                                (format "these %d articles" (length articles))
11736                              "this article")
11737                            ": ")
11738                    gnus-summary-pipe-output-default-command))
11739     (when (string-equal command "")
11740       (error "A command is required"))
11741     (when all-headers
11742       (put 'gnus-summary-save-in-pipe :headers nil))
11743     (unwind-protect
11744         (while articles
11745           (gnus-summary-goto-subject (pop articles))
11746           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11747           (when (and (get-buffer result-buffer)
11748                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11749             (setq result (concat result (with-current-buffer result-buffer
11750                                           (buffer-string))))))
11751       (put 'gnus-summary-save-in-pipe :headers headers))
11752     (unless (zerop (length result))
11753       (if (with-current-buffer (get-buffer-create result-buffer)
11754             (erase-buffer)
11755             (insert result)
11756             (prog1
11757                 (and (= (count-lines (point-min) (point)) 1)
11758                      (progn
11759                        (end-of-line 0)
11760                        (<= (current-column)
11761                            (window-width (minibuffer-window)))))
11762               (goto-char (point-min))))
11763           (message "%s" (substring result 0 -1))
11764         (message nil)
11765         (gnus-configure-windows 'pipe)))))
11766
11767 (defun gnus-summary-save-article-mail (&optional arg)
11768   "Append the current article to a Unix mail box file.
11769 If N is a positive number, save the N next articles.
11770 If N is a negative number, save the N previous articles.
11771 If N is nil and any articles have been marked with the process mark,
11772 save those articles instead."
11773   (interactive "P")
11774   (require 'gnus-art)
11775   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11776     (gnus-summary-save-article arg)))
11777
11778 (defun gnus-summary-save-article-rmail (&optional arg)
11779   "Append the current article to an rmail file.
11780 If N is a positive number, save the N next articles.
11781 If N is a negative number, save the N previous articles.
11782 If N is nil and any articles have been marked with the process mark,
11783 save those articles instead."
11784   (interactive "P")
11785   (require 'gnus-art)
11786   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11787     (gnus-summary-save-article arg)))
11788
11789 (defun gnus-summary-save-article-file (&optional arg)
11790   "Append the current article to a file.
11791 If N is a positive number, save the N next articles.
11792 If N is a negative number, save the N previous articles.
11793 If N is nil and any articles have been marked with the process mark,
11794 save those articles instead."
11795   (interactive "P")
11796   (require 'gnus-art)
11797   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11798     (gnus-summary-save-article arg)))
11799
11800 (defun gnus-summary-write-article-file (&optional arg)
11801   "Write the current article to a file, deleting the previous file.
11802 If N is a positive number, save the N next articles.
11803 If N is a negative number, save the N previous articles.
11804 If N is nil and any articles have been marked with the process mark,
11805 save those articles instead."
11806   (interactive "P")
11807   (require 'gnus-art)
11808   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11809     (gnus-summary-save-article arg)))
11810
11811 (defun gnus-summary-save-article-body-file (&optional arg)
11812   "Append the current article body to a file.
11813 If N is a positive number, save the N next articles.
11814 If N is a negative number, save the N previous articles.
11815 If N is nil and any articles have been marked with the process mark,
11816 save those articles instead."
11817   (interactive "P")
11818   (require 'gnus-art)
11819   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11820     (gnus-summary-save-article arg)))
11821
11822 (defun gnus-summary-write-article-body-file (&optional arg)
11823   "Write the current article body to a file, deleting the previous file.
11824 If N is a positive number, save the N next articles.
11825 If N is a negative number, save the N previous articles.
11826 If N is nil and any articles have been marked with the process mark,
11827 save those articles instead."
11828   (interactive "P")
11829   (require 'gnus-art)
11830   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11831     (gnus-summary-save-article arg)))
11832
11833 (defun gnus-summary-muttprint (&optional arg)
11834   "Print the current article using Muttprint.
11835 If N is a positive number, save the N next articles.
11836 If N is a negative number, save the N previous articles.
11837 If N is nil and any articles have been marked with the process mark,
11838 save those articles instead."
11839   (interactive "P")
11840   (require 'gnus-art)
11841   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11842     (gnus-summary-save-article arg t)))
11843
11844 (defun gnus-summary-pipe-message (program)
11845   "Pipe the current article through PROGRAM."
11846   (interactive "sProgram: ")
11847   (gnus-summary-select-article)
11848   (let ((mail-header-separator ""))
11849     (gnus-eval-in-buffer-window gnus-article-buffer
11850       (save-restriction
11851         (widen)
11852         (let ((start (window-start))
11853               (inhibit-read-only t))
11854           (message-pipe-buffer-body program)
11855           (set-window-start (get-buffer-window (current-buffer)) start))))))
11856
11857 (defun gnus-get-split-value (methods)
11858   "Return a value based on the split METHODS."
11859   (let (split-name method result match)
11860     (when methods
11861       (with-current-buffer gnus-original-article-buffer
11862         (save-restriction
11863           (nnheader-narrow-to-headers)
11864           (while (and methods (not split-name))
11865             (goto-char (point-min))
11866             (setq method (pop methods))
11867             (setq match (car method))
11868             (when (cond
11869                    ((stringp match)
11870                     ;; Regular expression.
11871                     (ignore-errors
11872                       (re-search-forward match nil t)))
11873                    ((functionp match)
11874                     ;; Function.
11875                     (save-restriction
11876                       (widen)
11877                       (setq result (funcall match gnus-newsgroup-name))))
11878                    ((consp match)
11879                     ;; Form.
11880                     (save-restriction
11881                       (widen)
11882                       (setq result (eval match)))))
11883               (setq split-name (cdr method))
11884               (cond ((stringp result)
11885                      (push (expand-file-name
11886                             result gnus-article-save-directory)
11887                            split-name))
11888                     ((consp result)
11889                      (setq split-name (append result split-name)))))))))
11890     (nreverse split-name)))
11891
11892 (defun gnus-valid-move-group-p (group)
11893   (and (boundp group)
11894        (symbol-name group)
11895        (symbol-value group)
11896        (gnus-get-function (gnus-find-method-for-group
11897                            (symbol-name group)) 'request-accept-article t)))
11898
11899 (defun gnus-read-move-group-name (prompt default articles prefix)
11900   "Read a group name."
11901   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11902          (minibuffer-confirm-incomplete nil) ; XEmacs
11903          (prom
11904           (format "%s %s to"
11905                   prompt
11906                   (if (> (length articles) 1)
11907                       (format "these %d articles" (length articles))
11908                     "this article")))
11909          (to-newsgroup
11910           (let (active group)
11911             (when (or (null split-name) (= 1 (length split-name)))
11912               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11913               (mapatoms (lambda (symbol)
11914                           (setq group (symbol-name symbol))
11915                           (when (string-match "[^\000-\177]" group)
11916                             (setq group (gnus-group-decoded-name group)))
11917                           (set (intern group active) group))
11918                         gnus-active-hashtb))
11919             (cond
11920              ((null split-name)
11921               (gnus-completing-read-with-default
11922                default prom active 'gnus-valid-move-group-p nil prefix
11923                'gnus-group-history))
11924              ((= 1 (length split-name))
11925               (gnus-completing-read-with-default
11926                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11927                'gnus-group-history))
11928              (t
11929               (gnus-completing-read-with-default
11930                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11931                'gnus-group-history)))))
11932          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11933          encoded)
11934     (when to-newsgroup
11935       (if (or (string= to-newsgroup "")
11936               (string= to-newsgroup prefix))
11937           (setq to-newsgroup default))
11938       (unless to-newsgroup
11939         (error "No group name entered"))
11940       (setq encoded (mm-encode-coding-string
11941                      to-newsgroup
11942                      (gnus-group-name-charset to-method to-newsgroup)))
11943       (or (gnus-active encoded)
11944           (gnus-activate-group encoded nil nil to-method)
11945           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11946                                      to-newsgroup))
11947               (or (and (gnus-request-create-group encoded to-method)
11948                        (gnus-activate-group encoded nil nil to-method)
11949                        (gnus-subscribe-group encoded))
11950                   (error "Couldn't create group %s" to-newsgroup)))
11951           (error "No such group: %s" to-newsgroup))
11952       encoded)))
11953
11954 (defvar gnus-summary-save-parts-counter)
11955 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11956
11957 (defun gnus-summary-save-parts (type dir n &optional reverse)
11958   "Save parts matching TYPE to DIR.
11959 If REVERSE, save parts that do not match TYPE."
11960   (interactive
11961    (list (read-string "Save parts of type: "
11962                       (or (car gnus-summary-save-parts-type-history)
11963                           gnus-summary-save-parts-default-mime)
11964                       'gnus-summary-save-parts-type-history)
11965          (setq gnus-summary-save-parts-last-directory
11966                (read-file-name "Save to directory: "
11967                                gnus-summary-save-parts-last-directory
11968                                nil t))
11969          current-prefix-arg))
11970   (gnus-summary-iterate n
11971     (let ((gnus-display-mime-function nil)
11972           gnus-article-prepare-hook
11973           gnus-article-decode-hook
11974           gnus-display-mime-function
11975           gnus-break-pages
11976           (gnus-inhibit-treatment t))
11977       (gnus-summary-select-article))
11978     (with-current-buffer gnus-article-buffer
11979       (let ((handles (or gnus-article-mime-handles
11980                          (mm-dissect-buffer nil gnus-article-loose-mime)
11981                          (and gnus-article-emulate-mime
11982                               (mm-uu-dissect))))
11983             (gnus-summary-save-parts-counter 1))
11984         (when handles
11985           (gnus-summary-save-parts-1 type dir handles reverse)
11986           (unless gnus-article-mime-handles ;; Don't destroy this case.
11987             (mm-destroy-parts handles)))))))
11988
11989 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11990   (if (stringp (car handle))
11991       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11992               (cdr handle))
11993     (when (if reverse
11994               (not (string-match type (mm-handle-media-type handle)))
11995             (string-match type (mm-handle-media-type handle)))
11996       (let ((file (expand-file-name
11997                    (gnus-map-function
11998                     mm-file-name-rewrite-functions
11999                     (file-name-nondirectory
12000                      (or
12001                       (mail-content-type-get
12002                        (mm-handle-disposition handle) 'filename)
12003                       (mail-content-type-get
12004                        (mm-handle-type handle) 'name)
12005                       (format "%s.%d.%d" gnus-newsgroup-name
12006                               (cdr gnus-article-current)
12007                               gnus-summary-save-parts-counter))))
12008                    dir)))
12009         (incf gnus-summary-save-parts-counter)
12010         (unless (file-exists-p file)
12011           (mm-save-part-to-file handle file))))))
12012
12013 ;; Summary extract commands
12014
12015 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12016   (let ((inhibit-read-only t)
12017         (article (gnus-summary-article-number))
12018         after-article b e)
12019     (unless (gnus-summary-goto-subject article)
12020       (error "No such article: %d" article))
12021     (gnus-summary-position-point)
12022     ;; If all commands are to be bunched up on one line, we collect
12023     ;; them here.
12024     (unless gnus-view-pseudos-separately
12025       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12026             files action)
12027         (while ps
12028           (setq action (cdr (assq 'action (car ps))))
12029           (setq files (list (cdr (assq 'name (car ps)))))
12030           (while (and ps (cdr ps)
12031                       (string= (or action "1")
12032                                (or (cdr (assq 'action (cadr ps))) "2")))
12033             (push (cdr (assq 'name (cadr ps))) files)
12034             (setcdr ps (cddr ps)))
12035           (when files
12036             (when (not (string-match "%s" action))
12037               (push " " files))
12038             (push " " files)
12039             (when (assq 'execute (car ps))
12040               (setcdr (assq 'execute (car ps))
12041                       (funcall (if (string-match "%s" action)
12042                                    'format 'concat)
12043                                action
12044                                (mapconcat
12045                                 (lambda (f)
12046                                   (if (equal f " ")
12047                                       f
12048                                     (shell-quote-argument f)))
12049                                 files " ")))))
12050           (setq ps (cdr ps)))))
12051     (if (and gnus-view-pseudos (not not-view))
12052         (while pslist
12053           (when (assq 'execute (car pslist))
12054             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12055                                   (eq gnus-view-pseudos 'not-confirm)))
12056           (setq pslist (cdr pslist)))
12057       (save-excursion
12058         (while pslist
12059           (setq after-article (or (cdr (assq 'article (car pslist)))
12060                                   (gnus-summary-article-number)))
12061           (gnus-summary-goto-subject after-article)
12062           (forward-line 1)
12063           (setq b (point))
12064           (insert "    " (file-name-nondirectory
12065                           (cdr (assq 'name (car pslist))))
12066                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12067           (setq e (point))
12068           (forward-line -1)             ; back to `b'
12069           (gnus-add-text-properties
12070            b (1- e) (list 'gnus-number gnus-reffed-article-number
12071                           gnus-mouse-face-prop gnus-mouse-face))
12072           (gnus-data-enter
12073            after-article gnus-reffed-article-number
12074            gnus-unread-mark b (car pslist) 0 (- e b))
12075           (setq gnus-newsgroup-unreads
12076                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12077                                          gnus-reffed-article-number))
12078           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12079           (setq pslist (cdr pslist)))))))
12080
12081 (defun gnus-pseudos< (p1 p2)
12082   (let ((c1 (cdr (assq 'action p1)))
12083         (c2 (cdr (assq 'action p2))))
12084     (and c1 c2 (string< c1 c2))))
12085
12086 (defun gnus-request-pseudo-article (props)
12087   (cond ((assq 'execute props)
12088          (gnus-execute-command (cdr (assq 'execute props)))))
12089   (let ((gnus-current-article (gnus-summary-article-number)))
12090     (gnus-run-hooks 'gnus-mark-article-hook)))
12091
12092 (defun gnus-execute-command (command &optional automatic)
12093   (save-excursion
12094     (gnus-article-setup-buffer)
12095     (set-buffer gnus-article-buffer)
12096     (setq buffer-read-only nil)
12097     (let ((command (if automatic command
12098                      (read-string "Command: " (cons command 0)))))
12099       (erase-buffer)
12100       (insert "$ " command "\n\n")
12101       (if gnus-view-pseudo-asynchronously
12102           (start-process "gnus-execute" (current-buffer) shell-file-name
12103                          shell-command-switch command)
12104         (call-process shell-file-name nil t nil
12105                       shell-command-switch command)))))
12106
12107 ;; Summary kill commands.
12108
12109 (defun gnus-summary-edit-global-kill (article)
12110   "Edit the \"global\" kill file."
12111   (interactive (list (gnus-summary-article-number)))
12112   (gnus-group-edit-global-kill article))
12113
12114 (defun gnus-summary-edit-local-kill ()
12115   "Edit a local kill file applied to the current newsgroup."
12116   (interactive)
12117   (setq gnus-current-headers (gnus-summary-article-header))
12118   (gnus-group-edit-local-kill
12119    (gnus-summary-article-number) gnus-newsgroup-name))
12120
12121 ;;; Header reading.
12122
12123 (defun gnus-read-header (id &optional header)
12124   "Read the headers of article ID and enter them into the Gnus system."
12125   (let ((group gnus-newsgroup-name)
12126         (gnus-override-method
12127          (or
12128           gnus-override-method
12129           (and (gnus-news-group-p gnus-newsgroup-name)
12130                (car (gnus-refer-article-methods)))))
12131         where)
12132     ;; First we check to see whether the header in question is already
12133     ;; fetched.
12134     (if (stringp id)
12135         ;; This is a Message-ID.
12136         (setq header (or header (gnus-id-to-header id)))
12137       ;; This is an article number.
12138       (setq header (or header (gnus-summary-article-header id))))
12139     (if (and header
12140              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12141         ;; We have found the header.
12142         header
12143       ;; We have to really fetch the header to this article.
12144       (with-current-buffer nntp-server-buffer
12145         (when (setq where (gnus-request-head id group))
12146           (nnheader-fold-continuation-lines)
12147           (goto-char (point-max))
12148           (insert ".\n")
12149           (goto-char (point-min))
12150           (insert "211 ")
12151           (princ (cond
12152                   ((numberp id) id)
12153                   ((cdr where) (cdr where))
12154                   (header (mail-header-number header))
12155                   (t gnus-reffed-article-number))
12156                  (current-buffer))
12157           (insert " Article retrieved.\n"))
12158         (if (or (not where)
12159                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12160             ()                          ; Malformed head.
12161           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12162             (when (and (stringp id)
12163                        (or
12164                         (not (string= (gnus-group-real-name group)
12165                                       (car where)))
12166                         (not (gnus-server-equal gnus-override-method
12167                                                 (gnus-group-method group)))))
12168               ;; If we fetched by Message-ID and the article came from
12169               ;; a different group (or server), we fudge some bogus
12170               ;; article numbers for this article.
12171               (mail-header-set-number header gnus-reffed-article-number))
12172             (with-current-buffer gnus-summary-buffer
12173               (decf gnus-reffed-article-number)
12174               (gnus-remove-header (mail-header-number header))
12175               (push header gnus-newsgroup-headers)
12176               (setq gnus-current-headers header)
12177               (push (mail-header-number header) gnus-newsgroup-limit)))
12178           header)))))
12179
12180 (defun gnus-remove-header (number)
12181   "Remove header NUMBER from `gnus-newsgroup-headers'."
12182   (if (and gnus-newsgroup-headers
12183            (= number (mail-header-number (car gnus-newsgroup-headers))))
12184       (pop gnus-newsgroup-headers)
12185     (let ((headers gnus-newsgroup-headers))
12186       (while (and (cdr headers)
12187                   (not (= number (mail-header-number (cadr headers)))))
12188         (pop headers))
12189       (when (cdr headers)
12190         (setcdr headers (cddr headers))))))
12191
12192 ;;;
12193 ;;; summary highlights
12194 ;;;
12195
12196 (defun gnus-highlight-selected-summary ()
12197   "Highlight selected article in summary buffer."
12198   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12199   (when gnus-summary-selected-face
12200     (save-excursion
12201       (let* ((beg (point-at-bol))
12202              (end (point-at-eol))
12203              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12204              (from (if (get-text-property beg gnus-mouse-face-prop)
12205                        beg
12206                      (or (next-single-property-change
12207                           beg gnus-mouse-face-prop nil end)
12208                          beg)))
12209              (to
12210               (if (= from end)
12211                   (- from 2)
12212                 (or (next-single-property-change
12213                      from gnus-mouse-face-prop nil end)
12214                     end))))
12215         ;; If no mouse-face prop on line we will have to = from = end,
12216         ;; so we highlight the entire line instead.
12217         (when (= (+ to 2) from)
12218           (setq from beg)
12219           (setq to end))
12220         (if gnus-newsgroup-selected-overlay
12221             ;; Move old overlay.
12222             (gnus-move-overlay
12223              gnus-newsgroup-selected-overlay from to (current-buffer))
12224           ;; Create new overlay.
12225           (gnus-overlay-put
12226            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12227            'face gnus-summary-selected-face))))))
12228
12229 (defvar gnus-summary-highlight-line-cached nil)
12230 (defvar gnus-summary-highlight-line-trigger nil)
12231
12232 (defun gnus-summary-highlight-line-0 ()
12233   (if (and (eq gnus-summary-highlight-line-trigger
12234                gnus-summary-highlight)
12235            gnus-summary-highlight-line-cached)
12236       gnus-summary-highlight-line-cached
12237     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12238           gnus-summary-highlight-line-cached
12239           (let* ((cond (list 'cond))
12240                  (c cond)
12241                  (list gnus-summary-highlight))
12242             (while list
12243               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12244                               nil))
12245               (setq c (cdr c)
12246                     list (cdr list)))
12247             (gnus-byte-compile (list 'lambda nil cond))))))
12248
12249 (defun gnus-summary-highlight-line ()
12250   "Highlight current line according to `gnus-summary-highlight'."
12251   (let* ((beg (point-at-bol))
12252          (article (or (gnus-summary-article-number) gnus-current-article))
12253          (score (or (cdr (assq article
12254                                gnus-newsgroup-scored))
12255                     gnus-summary-default-score 0))
12256          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12257          (inhibit-read-only t)
12258          (default gnus-summary-default-score)
12259          (default-high gnus-summary-default-high-score)
12260          (default-low gnus-summary-default-low-score)
12261          (uncached (and gnus-summary-use-undownloaded-faces
12262                         (memq article gnus-newsgroup-undownloaded)
12263                         (not (memq article gnus-newsgroup-cached)))))
12264     (let ((face (funcall (gnus-summary-highlight-line-0))))
12265       (unless (eq face (get-text-property beg 'face))
12266         (gnus-put-text-property-excluding-characters-with-faces
12267          beg (point-at-eol) 'face
12268          (setq face (if (boundp face) (symbol-value face) face)))
12269         (when gnus-summary-highlight-line-function
12270           (funcall gnus-summary-highlight-line-function article face))))))
12271
12272 (defun gnus-update-read-articles (group unread &optional compute)
12273   "Update the list of read articles in GROUP.
12274 UNREAD is a sorted list."
12275   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12276         (info (gnus-get-info group))
12277         (prev 1)
12278         read)
12279     (if (or (not info) (not active))
12280         ;; There is no info on this group if it was, in fact,
12281         ;; killed.  Gnus stores no information on killed groups, so
12282         ;; there's nothing to be done.
12283         ;; One could store the information somewhere temporarily,
12284         ;; perhaps...  Hmmm...
12285         ()
12286       ;; Remove any negative articles numbers.
12287       (while (and unread (< (car unread) 0))
12288         (setq unread (cdr unread)))
12289       ;; Remove any expired article numbers
12290       (while (and unread (< (car unread) (car active)))
12291         (setq unread (cdr unread)))
12292       ;; Compute the ranges of read articles by looking at the list of
12293       ;; unread articles.
12294       (while unread
12295         (when (/= (car unread) prev)
12296           (push (if (= prev (1- (car unread))) prev
12297                   (cons prev (1- (car unread))))
12298                 read))
12299         (setq prev (1+ (car unread)))
12300         (setq unread (cdr unread)))
12301       (when (<= prev (cdr active))
12302         (push (cons prev (cdr active)) read))
12303       (setq read (if (> (length read) 1) (nreverse read) read))
12304       (if compute
12305           read
12306         (save-excursion
12307           (let (setmarkundo)
12308             ;; Propagate the read marks to the backend.
12309             (when (and gnus-propagate-marks
12310                        (gnus-check-backend-function 'request-set-mark group))
12311               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12312                     (add (gnus-remove-from-range read (gnus-info-read info))))
12313                 (when (or add del)
12314                   (unless (gnus-check-group group)
12315                     (error "Can't open server for %s" group))
12316                   (gnus-request-set-mark
12317                    group (delq nil (list (if add (list add 'add '(read)))
12318                                          (if del (list del 'del '(read))))))
12319                   (setq setmarkundo
12320                         `(gnus-request-set-mark
12321                           ,group
12322                           ',(delq nil (list
12323                                        (if del (list del 'add '(read)))
12324                                        (if add (list add 'del '(read))))))))))
12325             (set-buffer gnus-group-buffer)
12326             (gnus-undo-register
12327               `(progn
12328                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12329                  (gnus-info-set-read ',info ',(gnus-info-read info))
12330                  (gnus-get-unread-articles-in-group ',info
12331                                                     (gnus-active ,group))
12332                  (gnus-group-update-group ,group t)
12333                  ,setmarkundo))))
12334         ;; Enter this list into the group info.
12335         (gnus-info-set-read info read)
12336         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12337         (gnus-get-unread-articles-in-group info (gnus-active group))
12338         t))))
12339
12340 (defun gnus-offer-save-summaries ()
12341   "Offer to save all active summary buffers."
12342   (let (buffers)
12343     ;; Go through all buffers and find all summaries.
12344     (dolist (buffer (buffer-list))
12345       (when (and (setq buffer (buffer-name buffer))
12346                  (string-match "Summary" buffer)
12347                  (with-current-buffer buffer
12348                    ;; We check that this is, indeed, a summary buffer.
12349                    (and (eq major-mode 'gnus-summary-mode)
12350                         ;; Also make sure this isn't bogus.
12351                         gnus-newsgroup-prepared
12352                         ;; Also make sure that this isn't a
12353                         ;; dead summary buffer.
12354                         (not gnus-dead-summary-mode))))
12355         (push buffer buffers)))
12356     ;; Go through all these summary buffers and offer to save them.
12357     (when buffers
12358       (save-excursion
12359         (map-y-or-n-p
12360          "Update summary buffer %s? "
12361          (lambda (buf)
12362            (switch-to-buffer buf)
12363            (gnus-summary-exit))
12364          buffers)))))
12365
12366 (defun gnus-summary-setup-default-charset ()
12367   "Setup newsgroup default charset."
12368   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12369       (setq gnus-newsgroup-charset nil)
12370     (let* ((ignored-charsets
12371             (or gnus-newsgroup-ephemeral-ignored-charsets
12372                 (append
12373                  (and gnus-newsgroup-name
12374                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12375                  gnus-newsgroup-ignored-charsets))))
12376       (setq gnus-newsgroup-charset
12377             (or gnus-newsgroup-ephemeral-charset
12378                 (and gnus-newsgroup-name
12379                      (gnus-parameter-charset gnus-newsgroup-name))
12380                 gnus-default-charset))
12381       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12382            ignored-charsets))))
12383
12384 ;;;
12385 ;;; Mime Commands
12386 ;;;
12387
12388 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12389   "Display the current article buffer fully MIME-buttonized.
12390 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12391 treated as multipart/mixed."
12392   (interactive "P")
12393   (require 'gnus-art)
12394   (let ((gnus-unbuttonized-mime-types nil)
12395         (gnus-mime-display-multipart-as-mixed show-all-parts))
12396     (gnus-summary-show-article)))
12397
12398 (defun gnus-summary-repair-multipart (article)
12399   "Add a Content-Type header to a multipart article without one."
12400   (interactive (list (gnus-summary-article-number)))
12401   (gnus-with-article article
12402     (message-narrow-to-head)
12403     (message-remove-header "Mime-Version")
12404     (goto-char (point-max))
12405     (insert "Mime-Version: 1.0\n")
12406     (widen)
12407     (when (search-forward "\n--" nil t)
12408       (let ((separator (buffer-substring (point) (point-at-eol))))
12409         (message-narrow-to-head)
12410         (message-remove-header "Content-Type")
12411         (goto-char (point-max))
12412         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12413                         separator))
12414         (widen))))
12415   (let (gnus-mark-article-hook)
12416     (gnus-summary-select-article t t nil article)))
12417
12418 (defun gnus-summary-toggle-display-buttonized ()
12419   "Toggle the buttonizing of the article buffer."
12420   (interactive)
12421   (require 'gnus-art)
12422   (if (setq gnus-inhibit-mime-unbuttonizing
12423             (not gnus-inhibit-mime-unbuttonizing))
12424       (let ((gnus-unbuttonized-mime-types nil))
12425         (gnus-summary-show-article))
12426     (gnus-summary-show-article)))
12427
12428 ;;;
12429 ;;; Generic summary marking commands
12430 ;;;
12431
12432 (defvar gnus-summary-marking-alist
12433   '((read gnus-del-mark "d")
12434     (unread gnus-unread-mark "u")
12435     (ticked gnus-ticked-mark "!")
12436     (dormant gnus-dormant-mark "?")
12437     (expirable gnus-expirable-mark "e"))
12438   "An alist of names/marks/keystrokes.")
12439
12440 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12441 (defvar gnus-summary-mark-map)
12442
12443 (defun gnus-summary-make-all-marking-commands ()
12444   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12445   (dolist (elem gnus-summary-marking-alist)
12446     (apply 'gnus-summary-make-marking-command elem)))
12447
12448 (defun gnus-summary-make-marking-command (name mark keystroke)
12449   (let ((map (make-sparse-keymap)))
12450     (define-key gnus-summary-generic-mark-map keystroke map)
12451     (dolist (lway `((next "next" next nil "n")
12452                     (next-unread "next unread" next t "N")
12453                     (prev "previous" prev nil "p")
12454                     (prev-unread "previous unread" prev t "P")
12455                     (nomove "" nil nil ,keystroke)))
12456       (let ((func (gnus-summary-make-marking-command-1
12457                    mark (car lway) lway name)))
12458         (setq func (eval func))
12459         (define-key map (nth 4 lway) func)))))
12460
12461 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12462   `(defun ,(intern
12463             (format "gnus-summary-put-mark-as-%s%s"
12464                     name (if (eq way 'nomove)
12465                              ""
12466                            (concat "-" (symbol-name way)))))
12467      (n)
12468      ,(format
12469        "Mark the current article as %s%s.
12470 If N, the prefix, then repeat N times.
12471 If N is negative, move in reverse order.
12472 The difference between N and the actual number of articles marked is
12473 returned."
12474        name (cadr lway))
12475      (interactive "p")
12476      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12477
12478 (defun gnus-summary-generic-mark (n mark move unread)
12479   "Mark N articles with MARK."
12480   (unless (eq major-mode 'gnus-summary-mode)
12481     (error "This command can only be used in the summary buffer"))
12482   (gnus-summary-show-thread)
12483   (let ((nummove
12484          (cond
12485           ((eq move 'next) 1)
12486           ((eq move 'prev) -1)
12487           (t 0))))
12488     (if (zerop nummove)
12489         (setq n 1)
12490       (when (< n 0)
12491         (setq n (abs n)
12492               nummove (* -1 nummove))))
12493     (while (and (> n 0)
12494                 (gnus-summary-mark-article nil mark)
12495                 (zerop (gnus-summary-next-subject nummove unread t)))
12496       (setq n (1- n)))
12497     (when (/= 0 n)
12498       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12499     (gnus-summary-recenter)
12500     (gnus-summary-position-point)
12501     (gnus-set-mode-line 'summary)
12502     n))
12503
12504 (defun gnus-summary-insert-articles (articles)
12505   (when (setq articles
12506               (gnus-sorted-difference articles
12507                                       (mapcar (lambda (h)
12508                                                 (mail-header-number h))
12509                                               gnus-newsgroup-headers)))
12510     (setq gnus-newsgroup-headers
12511           (gnus-merge 'list
12512                       gnus-newsgroup-headers
12513                       (gnus-fetch-headers articles)
12514                       'gnus-article-sort-by-number))
12515     ;; Suppress duplicates?
12516     (when gnus-suppress-duplicates
12517       (gnus-dup-suppress-articles))
12518
12519     (if (and gnus-fetch-old-headers
12520              (eq gnus-headers-retrieved-by 'nov))
12521         ;; We might want to build some more threads first.
12522         (if (eq gnus-fetch-old-headers 'invisible)
12523             (gnus-build-all-threads)
12524           (gnus-build-old-threads))
12525       ;; Mark the inserted articles that are unread as unread.
12526       (setq gnus-newsgroup-unreads
12527             (gnus-sorted-nunion
12528              gnus-newsgroup-unreads
12529              (gnus-sorted-nintersection
12530               (gnus-list-of-unread-articles gnus-newsgroup-name)
12531               articles)))
12532       ;; Mark the inserted articles as selected so that the information
12533       ;; of the marks having been changed by a user may be updated when
12534       ;; exiting this group.  See `gnus-summary-update-info'.
12535       (dolist (art articles)
12536         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12537     ;; Let the Gnus agent mark articles as read.
12538     (when gnus-agent
12539       (gnus-agent-get-undownloaded-list))
12540     ;; Remove list identifiers from subject
12541     (when gnus-list-identifiers
12542       (gnus-summary-remove-list-identifiers))
12543     ;; First and last article in this newsgroup.
12544     (when gnus-newsgroup-headers
12545       (setq gnus-newsgroup-begin
12546             (mail-header-number (car gnus-newsgroup-headers))
12547             gnus-newsgroup-end
12548             (mail-header-number
12549              (gnus-last-element gnus-newsgroup-headers))))
12550     (when gnus-use-scoring
12551       (gnus-possibly-score-headers))))
12552
12553 (defun gnus-summary-insert-old-articles (&optional all)
12554   "Insert all old articles in this group.
12555 If ALL is non-nil, already read articles become readable.
12556 If ALL is a number, fetch this number of articles."
12557   (interactive "P")
12558   (prog1
12559       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12560             older len)
12561         (setq older
12562               ;; Some nntp servers lie about their active range.  When
12563               ;; this happens, the active range can be in the millions.
12564               ;; Use a compressed range to avoid creating a huge list.
12565               (gnus-range-difference (list gnus-newsgroup-active) old))
12566         (setq len (gnus-range-length older))
12567         (cond
12568          ((null older) nil)
12569          ((numberp all)
12570           (if (< all len)
12571               (let ((older-range (nreverse older)))
12572                 (setq older nil)
12573
12574                 (while (> all 0)
12575                   (let* ((r (pop older-range))
12576                          (min (if (numberp r) r (car r)))
12577                          (max (if (numberp r) r (cdr r))))
12578                     (while (and (<= min max)
12579                                 (> all 0))
12580                       (push max older)
12581                       (setq all (1- all)
12582                             max (1- max))))))
12583             (setq older (gnus-uncompress-range older))))
12584          (all
12585           (setq older (gnus-uncompress-range older)))
12586          (t
12587           (when (and (numberp gnus-large-newsgroup)
12588                    (> len gnus-large-newsgroup))
12589               (let* ((cursor-in-echo-area nil)
12590                      (initial (gnus-parameter-large-newsgroup-initial
12591                                gnus-newsgroup-name))
12592                      (input
12593                       (read-string
12594                        (format
12595                         "How many articles from %s (%s %d): "
12596                         (gnus-group-decoded-name gnus-newsgroup-name)
12597                         (if initial "max" "default")
12598                         len)
12599                        (if initial
12600                            (cons (number-to-string initial)
12601                                  0)))))
12602                 (unless (string-match "^[ \t]*$" input)
12603                   (setq all (string-to-number input))
12604                   (if (< all len)
12605                       (let ((older-range (nreverse older)))
12606                         (setq older nil)
12607
12608                         (while (> all 0)
12609                           (let* ((r (pop older-range))
12610                                  (min (if (numberp r) r (car r)))
12611                                  (max (if (numberp r) r (cdr r))))
12612                             (while (and (<= min max)
12613                                         (> all 0))
12614                               (push max older)
12615                               (setq all (1- all)
12616                                     max (1- max))))))))))
12617           (setq older (gnus-uncompress-range older))))
12618         (if (not older)
12619             (message "No old news.")
12620           (gnus-summary-insert-articles older)
12621           (gnus-summary-limit (gnus-sorted-nunion old older))))
12622     (gnus-summary-position-point)))
12623
12624 (defun gnus-summary-insert-new-articles ()
12625   "Insert all new articles in this group."
12626   (interactive)
12627   (prog1
12628       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12629             (old-active gnus-newsgroup-active)
12630             (nnmail-fetched-sources (list t))
12631             i new)
12632         (setq gnus-newsgroup-active
12633               (gnus-activate-group gnus-newsgroup-name 'scan))
12634         (setq i (cdr gnus-newsgroup-active))
12635         (while (> i (cdr old-active))
12636           (push i new)
12637           (decf i))
12638         (if (not new)
12639             (message "No gnus is bad news")
12640           (gnus-summary-insert-articles new)
12641           (setq gnus-newsgroup-unreads
12642                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12643           (gnus-summary-limit (gnus-sorted-nunion old new))))
12644     (gnus-summary-position-point)))
12645
12646 ;;; Bookmark support for Gnus.
12647 (declare-function bookmark-make-record-default
12648                   "bookmark" (&optional no-file no-context posn))
12649 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12650 (declare-function bookmark-default-handler "bookmark" (bmk))
12651 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12652 (defvar bookmark-yank-point)
12653 (defvar bookmark-current-buffer)
12654
12655 (defun gnus-summary-bookmark-make-record ()
12656   "Make a bookmark entry for a Gnus summary buffer."
12657   (let (pos buf)
12658     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12659       (save-restriction              ; FIXME is it necessary to widen?
12660         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12661       (setq buf "art") ; We are recording bookmark from article buffer.
12662       (setq bookmark-yank-point (point))
12663       (setq bookmark-current-buffer (current-buffer))
12664       (gnus-article-show-summary))      ; Go back in summary buffer.
12665     ;; We are now recording bookmark from summary buffer.
12666     (unless buf (setq buf "sum"))
12667     (let* ((subject (elt (gnus-summary-article-header) 1))
12668            (grp     (car gnus-article-current))
12669            (art     (cdr gnus-article-current))
12670            (head    (gnus-summary-article-header art))
12671            (id      (mail-header-id head)))
12672       `(,subject
12673         ,@(condition-case nil
12674               (bookmark-make-record-default 'no-file 'no-context pos)
12675             (wrong-number-of-arguments
12676              (bookmark-make-record-default 'point-only)))
12677         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12678         (group . ,grp) (article . ,art)
12679         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12680
12681 ;;;###autoload
12682 (defun gnus-summary-bookmark-jump (bookmark)
12683   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12684 BOOKMARK is a bookmark name or a bookmark record."
12685   (let ((group    (bookmark-prop-get bookmark 'group))
12686         (article  (bookmark-prop-get bookmark 'article))
12687         (id       (bookmark-prop-get bookmark 'message-id))
12688         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12689     (gnus-fetch-group group (list article))
12690     (gnus-summary-insert-cached-articles)
12691     (gnus-summary-goto-article id nil 'force)
12692     ;; FIXME we have to wait article buffer is ready (only large buffer)
12693     ;; Is there a better solution to know that?
12694     ;; If we don't wait `bookmark-default-handler' will have no chance
12695     ;; to set position. However there is no error, just wrong pos.
12696     (sit-for 1)
12697     (when (string= buf "Gnus-art")
12698       (other-window 1))
12699     (bookmark-default-handler
12700      `(""
12701        (buffer . ,(current-buffer))
12702        . ,(bookmark-get-bookmark-record bookmark)))))
12703
12704 (gnus-summary-make-all-marking-commands)
12705
12706 (gnus-ems-redefine)
12707
12708 (provide 'gnus-sum)
12709
12710 (run-hooks 'gnus-sum-load-hook)
12711
12712 ;; Local Variables:
12713 ;; coding: iso-8859-1
12714 ;; End:
12715
12716 ;;; gnus-sum.el ends here