Merge branch 'master' of https://git.gnus.org/gnus
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (eval-when-compile
34   (when (featurep 'xemacs)
35     (require 'easy-mmode))) ; for `define-minor-mode'
36
37 (defvar tool-bar-mode)
38 (defvar gnus-tmp-header)
39
40 (require 'gnus)
41 (require 'gnus-group)
42 (require 'gnus-spec)
43 (require 'gnus-range)
44 (require 'gnus-int)
45 (require 'gnus-undo)
46 (require 'gnus-util)
47 (require 'gmm-utils)
48 (require 'mm-decode)
49 (require 'nnoo)
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
54 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
55 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-summary-next-group-on-exit t
72   "If non-nil, go to the next unread newsgroup on summary exit.
73 See `gnus-group-goto-unread'."
74   :link '(custom-manual "(gnus)Group Maneuvering")
75   :group 'gnus-summary-exit
76   :version "23.1" ;; No Gnus
77   :type 'boolean)
78
79 (defcustom gnus-summary-stop-at-end-of-message nil
80   "If non-nil, don't select the next message when using `SPC'."
81   :link '(custom-manual "(gnus)Group Maneuvering")
82   :group 'gnus-summary-maneuvering
83   :version "24.1"
84   :type 'boolean)
85
86 (defcustom gnus-fetch-old-headers nil
87   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
88 If an unread article in the group refers to an older, already
89 read (or just marked as read) article, the old article will not
90 normally be displayed in the Summary buffer.  If this variable is
91 t, Gnus will attempt to grab the headers to the old articles, and
92 thereby build complete threads.  If it has the value `some', all
93 old headers will be fetched but only enough headers to connect
94 otherwise loose threads will be displayed.  This variable can
95 also be a number.  In that case, no more than that number of old
96 headers will be fetched.  If it has the value `invisible', all
97 old headers will be fetched, but none will be displayed.
98
99 The server has to support NOV for any of this to work.
100
101 This feature can seriously impact performance it ignores all
102 locally cached header entries.  Setting it to t for groups for a
103 server that doesn't expire articles (such as news.gmane.org),
104 leads to very slow summary generation."
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const :tag "on" t)
108                  (const some)
109                  (const invisible)
110                  number
111                  (sexp :menu-tag "other" t)))
112
113 (defcustom gnus-refer-thread-limit 500
114   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
115 If t, fetch all the available old headers."
116   :group 'gnus-thread
117   :type '(choice number
118                  (sexp :menu-tag "other" t)))
119
120 (defcustom gnus-summary-make-false-root 'adopt
121   "*nil means that Gnus won't gather loose threads.
122 If the root of a thread has expired or been read in a previous
123 session, the information necessary to build a complete thread has been
124 lost.  Instead of having many small sub-threads from this original thread
125 scattered all over the summary buffer, Gnus can gather them.
126
127 If non-nil, Gnus will try to gather all loose sub-threads from an
128 original thread into one large thread.
129
130 If this variable is non-nil, it should be one of `none', `adopt',
131 `dummy' or `empty'.
132
133 If this variable is `none', Gnus will not make a false root, but just
134 present the sub-threads after another.
135 If this variable is `dummy', Gnus will create a dummy root that will
136 have all the sub-threads as children.
137 If this variable is `adopt', Gnus will make one of the \"children\"
138 the parent and mark all the step-children as such.
139 If this variable is `empty', the \"children\" are printed with empty
140 subject fields.  (Or rather, they will be printed with a string
141 given by the `gnus-summary-same-subject' variable.)"
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const none)
145                  (const dummy)
146                  (const adopt)
147                  (const empty)))
148
149 (defcustom gnus-summary-make-false-root-always nil
150   "Always make a false dummy root."
151   :version "22.1"
152   :group 'gnus-thread
153   :type 'boolean)
154
155 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
156   "*A regexp to match subjects to be excluded from loose thread gathering.
157 As loose thread gathering is done on subjects only, that means that
158 there can be many false gatherings performed.  By rooting out certain
159 common subjects, gathering might become saner."
160   :group 'gnus-thread
161   :type 'regexp)
162
163 (defcustom gnus-summary-gather-subject-limit nil
164   "*Maximum length of subject comparisons when gathering loose threads.
165 Use nil to compare full subjects.  Setting this variable to a low
166 number will help gather threads that have been corrupted by
167 newsreaders chopping off subject lines, but it might also mean that
168 unrelated articles that have subject that happen to begin with the
169 same few characters will be incorrectly gathered.
170
171 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
172 comparing subjects."
173   :group 'gnus-thread
174   :type '(choice (const :tag "off" nil)
175                  (const fuzzy)
176                  (sexp :menu-tag "on" t)))
177
178 (defcustom gnus-simplify-subject-functions nil
179   "List of functions taking a string argument that simplify subjects.
180 The functions are applied recursively.
181
182 Useful functions to put in this list include:
183 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
184 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
185   :group 'gnus-thread
186   :type '(repeat function))
187
188 (defcustom gnus-simplify-ignored-prefixes nil
189   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
190   :group 'gnus-thread
191   :type '(choice (const :tag "off" nil)
192                  regexp))
193
194 (defcustom gnus-build-sparse-threads nil
195   "*If non-nil, fill in the gaps in threads.
196 If `some', only fill in the gaps that are needed to tie loose threads
197 together.  If `more', fill in all leaf nodes that Gnus can find.  If
198 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  (const some)
202                  (const more)
203                  (sexp :menu-tag "all" t)))
204
205 (defcustom gnus-summary-thread-gathering-function
206   'gnus-gather-threads-by-subject
207   "*Function used for gathering loose threads.
208 There are two pre-defined functions: `gnus-gather-threads-by-subject',
209 which only takes Subjects into consideration; and
210 `gnus-gather-threads-by-references', which compared the References
211 headers of the articles to find matches."
212   :group 'gnus-thread
213   :type '(radio (function-item gnus-gather-threads-by-subject)
214                 (function-item gnus-gather-threads-by-references)
215                 (function :tag "other")))
216
217 (defcustom gnus-summary-same-subject ""
218   "*String indicating that the current article has the same subject as the previous.
219 This variable will only be used if the value of
220 `gnus-summary-make-false-root' is `empty'."
221   :group 'gnus-summary-format
222   :type 'string)
223
224 (defcustom gnus-summary-goto-unread nil
225   "*If t, many commands will go to the next unread article.
226 This applies to marking commands as well as other commands that
227 \"naturally\" select the next article, like, for instance, `SPC' at
228 the end of an article.
229
230 If nil, the marking commands do NOT go to the next unread article
231 \(they go to the next article instead).  If `never', commands that
232 usually go to the next unread article, will go to the next article,
233 whether it is read or not."
234   :version "24.1"
235   :group 'gnus-summary-marks
236   :link '(custom-manual "(gnus)Setting Marks")
237   :type '(choice (const :tag "off" nil)
238                  (const never)
239                  (sexp :menu-tag "on" t)))
240
241 (defcustom gnus-summary-default-score 0
242   "*Default article score level.
243 All scores generated by the score files will be added to this score.
244 If this variable is nil, scoring will be disabled."
245   :group 'gnus-score-default
246   :type '(choice (const :tag "disable")
247                  integer))
248
249 (defcustom gnus-summary-default-high-score 0
250   "*Default threshold for a high scored article.
251 An article will be highlighted as high scored if its score is greater
252 than this score."
253   :version "22.1"
254   :group 'gnus-score-default
255   :type 'integer)
256
257 (defcustom gnus-summary-default-low-score 0
258   "*Default threshold for a low scored article.
259 An article will be highlighted as low scored if its score is smaller
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-zcore-fuzz 0
266   "*Fuzziness factor for the zcore in the summary buffer.
267 Articles with scores closer than this to `gnus-summary-default-score'
268 will not be marked."
269   :group 'gnus-summary-format
270   :type 'integer)
271
272 (defcustom gnus-simplify-subject-fuzzy-regexp nil
273   "*Strings to be removed when doing fuzzy matches.
274 This can either be a regular expression or list of regular expressions
275 that will be removed from subject strings if fuzzy subject
276 simplification is selected."
277   :group 'gnus-thread
278   :type '(repeat regexp))
279
280 (defcustom gnus-show-threads t
281   "*If non-nil, display threads in summary mode."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-hide-subtree nil
286   "*If non-nil, hide all threads initially.
287 This can be a predicate specifier which says which threads to hide.
288 If threads are hidden, you have to run the command
289 `gnus-summary-show-thread' by hand or select an article."
290   :group 'gnus-thread
291   :type '(radio (sexp :format "Non-nil\n"
292                       :match (lambda (widget value)
293                                (not (or (consp value) (functionp value))))
294                       :value t)
295                 (const nil)
296                 (sexp :tag "Predicate specifier")))
297
298 (defcustom gnus-thread-hide-killed t
299   "*If non-nil, hide killed threads automatically."
300   :group 'gnus-thread
301   :type 'boolean)
302
303 (defcustom gnus-thread-ignore-subject t
304   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
305 If nil, articles that have different subjects from their parents will
306 start separate threads."
307   :group 'gnus-thread
308   :type 'boolean)
309
310 (defcustom gnus-thread-operation-ignore-subject t
311   "*If non-nil, subjects will be ignored when doing thread commands.
312 This affects commands like `gnus-summary-kill-thread' and
313 `gnus-summary-lower-thread'.
314
315 If this variable is nil, articles in the same thread with different
316 subjects will not be included in the operation in question.  If this
317 variable is `fuzzy', only articles that have subjects that are fuzzily
318 equal will be included."
319   :group 'gnus-thread
320   :type '(choice (const :tag "off" nil)
321                  (const fuzzy)
322                  (sexp :tag "on" t)))
323
324 (defcustom gnus-thread-indent-level 4
325   "*Number that says how much each sub-thread should be indented."
326   :group 'gnus-thread
327   :type 'integer)
328
329 (defcustom gnus-auto-extend-newsgroup t
330   "*If non-nil, extend newsgroup forward and backward when requested."
331   :group 'gnus-summary-choose
332   :type 'boolean)
333
334 (defcustom gnus-auto-select-first t
335   "If non-nil, select an article on group entry.
336 An article is selected automatically when entering a group
337 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
338 `gnus-summary-catchup-and-goto-next-group'.
339
340 Which article is selected is controlled by the variable
341 `gnus-auto-select-subject'.
342
343 If you want to prevent automatic selection of articles in some
344 newsgroups, set the variable to nil in `gnus-select-group-hook'."
345   ;; Commands include...
346   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
347   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
349   :group 'gnus-group-select
350   :type '(choice (const :tag "none" nil)
351                  (sexp :menu-tag "first" t)))
352
353 (defcustom gnus-auto-select-subject 'unseen-or-unread
354   "*Says what subject to place under point when entering a group.
355
356 This variable can either be the symbols `first' (place point on the
357 first subject), `unread' (place point on the subject line of the first
358 unread article), `best' (place point on the subject line of the
359 higest-scored article), `unseen' (place point on the subject line of
360 the first unseen article), `unseen-or-unread' (place point on the subject
361 line of the first unseen article or, if all article have been seen, on the
362 subject line of the first unread article), or a function to be called to
363 place point on some subject line."
364   :version "24.1"
365   :group 'gnus-group-select
366   :type '(choice (const best)
367                  (const unread)
368                  (const first)
369                  (const unseen)
370                  (const unseen-or-unread)))
371
372 (defcustom gnus-auto-select-next t
373   "*If non-nil, offer to go to the next group from the end of the previous.
374 If the value is t and the next newsgroup is empty, Gnus will exit
375 summary mode and go back to group mode.  If the value is neither nil
376 nor t, Gnus will select the following unread newsgroup.  In
377 particular, if the value is the symbol `quietly', the next unread
378 newsgroup will be selected without any confirmation, and if it is
379 `almost-quietly', the next group will be selected without any
380 confirmation if you are located on the last article in the group.
381 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
382 will go to the next group without confirmation."
383   :group 'gnus-summary-maneuvering
384   :type '(choice (const :tag "off" nil)
385                  (const quietly)
386                  (const almost-quietly)
387                  (const slightly-quietly)
388                  (sexp :menu-tag "on" t)))
389
390 (defcustom gnus-auto-select-same nil
391   "*If non-nil, select the next article with the same subject.
392 If there are no more articles with the same subject, go to
393 the first unread article."
394   :group 'gnus-summary-maneuvering
395   :type 'boolean)
396
397 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
398   "What article should be selected after exiting an ephemeral group.
399 Valid values include:
400
401 `next'
402   Select the next article.
403 `next-unread'
404   Select the next unread article.
405 `next-noselect'
406   Move the cursor to the next article.  This is the default.
407 `next-unread-noselect'
408   Move the cursor to the next unread article.
409
410 If it has any other value or there is no next (unread) article, the
411 article selected before entering to the ephemeral group will appear."
412   :version "23.1" ;; No Gnus
413   :group 'gnus-summary-maneuvering
414   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
415                  (const next) (const next-unread)
416                  (const next-noselect) (const next-unread-noselect)
417                  (sexp :tag "other" :value nil)))
418
419 (defcustom gnus-auto-goto-ignores 'unfetched
420   "*Says how to handle unfetched articles when maneuvering.
421
422 This variable can either be the symbols nil (maneuver to any
423 article), `undownloaded' (maneuvering while unplugged ignores articles
424 that have not been fetched), `always-undownloaded' (maneuvering always
425 ignores articles that have not been fetched), `unfetched' (maneuvering
426 ignores articles whose headers have not been fetched).
427
428 NOTE: The list of unfetched articles will always be nil when plugged
429 and, when unplugged, a subset of the undownloaded article list."
430   :version "22.1"
431   :group 'gnus-summary-maneuvering
432   :type '(choice (const :tag "None" nil)
433                  (const :tag "Undownloaded when unplugged" undownloaded)
434                  (const :tag "Undownloaded" always-undownloaded)
435                  (const :tag "Unfetched" unfetched)))
436
437 (defcustom gnus-summary-check-current nil
438   "*If non-nil, consider the current article when moving.
439 The \"unread\" movement commands will stay on the same line if the
440 current article is unread."
441   :group 'gnus-summary-maneuvering
442   :type 'boolean)
443
444 (defcustom gnus-auto-center-summary 2
445   "*If non-nil, always center the current summary buffer.
446 In particular, if `vertical' do only vertical recentering.  If non-nil
447 and non-`vertical', do both horizontal and vertical recentering."
448   :group 'gnus-summary-maneuvering
449   :type '(choice (const :tag "none" nil)
450                  (const vertical)
451                  (integer :tag "height")
452                  (sexp :menu-tag "both" t)))
453
454 (defcustom gnus-auto-center-group t
455   "If non-nil, always center the group buffer."
456   :group 'gnus-summary-maneuvering
457   :type 'boolean)
458
459 (defcustom gnus-show-all-headers nil
460   "*If non-nil, don't hide any headers."
461   :group 'gnus-article-hiding
462   :group 'gnus-article-headers
463   :type 'boolean)
464
465 (defcustom gnus-summary-ignore-duplicates nil
466   "*If non-nil, ignore articles with identical Message-ID headers."
467   :group 'gnus-summary
468   :type 'boolean)
469
470 (defcustom gnus-single-article-buffer nil
471   "*If non-nil, display all articles in the same buffer.
472 If nil, each group will get its own article buffer."
473   :version "24.1"
474   :group 'gnus-article-various
475   :type 'boolean)
476
477 (defcustom gnus-break-pages t
478   "*If non-nil, do page breaking on articles.
479 The page delimiter is specified by the `gnus-page-delimiter'
480 variable."
481   :group 'gnus-article-various
482   :type 'boolean)
483
484 (defcustom gnus-move-split-methods nil
485   "*Variable used to suggest where articles are to be moved to.
486 It uses the same syntax as the `gnus-split-methods' variable.
487 However, whereas `gnus-split-methods' specifies file names as targets,
488 this variable specifies group names."
489   :group 'gnus-summary-mail
490   :type '(repeat (choice (list :value (fun) function)
491                          (cons :value ("" "") regexp (repeat string))
492                          (sexp :value nil))))
493
494 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
495   "Function used to compute default prefix for article move/copy/etc prompts.
496 The function should take one argument, a group name, and return a
497 string with the suggested prefix."
498   :group 'gnus-summary-mail
499   :type 'function)
500
501 ;; FIXME: Although the custom type is `character' for the following variables,
502 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
503
504 (defcustom gnus-unread-mark ?           ;Whitespace
505   "*Mark used for unread articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-ticked-mark ?!
510   "*Mark used for ticked articles."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-dormant-mark ??
515   "*Mark used for dormant articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-del-mark ?r
520   "*Mark used for del'd articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-read-mark ?R
525   "*Mark used for read articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-expirable-mark ?E
530   "*Mark used for expirable articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-killed-mark ?K
535   "*Mark used for killed articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-spam-mark ?$
540   "*Mark used for spam articles."
541   :version "22.1"
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-kill-file-mark ?X
546   "*Mark used for articles killed by kill files."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-low-score-mark ?Y
551   "*Mark used for articles with a low score."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-catchup-mark ?C
556   "*Mark used for articles that are caught up."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-replied-mark ?A
561   "*Mark used for articles that have been replied to."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-forwarded-mark ?F
566   "*Mark used for articles that have been forwarded."
567   :version "22.1"
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-recent-mark ?N
572   "*Mark used for articles that are recent."
573   :version "22.1"
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-cached-mark ?*
578   "*Mark used for articles that are in the cache."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-saved-mark ?S
583   "*Mark used for articles that have been saved."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-unseen-mark ?.
588   "*Mark used for articles that haven't been seen."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-no-mark ?               ;Whitespace
594   "*Mark used for articles that have no other secondary mark."
595   :version "22.1"
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-ancient-mark ?O
600   "*Mark used for ancient articles."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-sparse-mark ?Q
605   "*Mark used for sparsely reffed articles."
606   :group 'gnus-summary-marks
607   :type 'character)
608
609 (defcustom gnus-canceled-mark ?G
610   "*Mark used for canceled articles."
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-duplicate-mark ?M
615   "*Mark used for duplicate articles."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-undownloaded-mark ?-
620   "*Mark used for articles that weren't downloaded."
621   :version "22.1"
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-downloaded-mark ?+
626   "*Mark used for articles that were downloaded."
627   :group 'gnus-summary-marks
628   :type 'character)
629
630 (defcustom gnus-downloadable-mark ?%
631   "*Mark used for articles that are to be downloaded."
632   :group 'gnus-summary-marks
633   :type 'character)
634
635 (defcustom gnus-unsendable-mark ?=
636   "*Mark used for articles that won't be sent."
637   :group 'gnus-summary-marks
638   :type 'character)
639
640 (defcustom gnus-score-over-mark ?+
641   "*Score mark used for articles with high scores."
642   :group 'gnus-summary-marks
643   :type 'character)
644
645 (defcustom gnus-score-below-mark ?-
646   "*Score mark used for articles with low scores."
647   :group 'gnus-summary-marks
648   :type 'character)
649
650 (defcustom gnus-empty-thread-mark ?     ;Whitespace
651   "*There is no thread under the article."
652   :group 'gnus-summary-marks
653   :type 'character)
654
655 (defcustom gnus-not-empty-thread-mark ?=
656   "*There is a thread under the article."
657   :group 'gnus-summary-marks
658   :type 'character)
659
660 (defcustom gnus-view-pseudo-asynchronously nil
661   "*If non-nil, Gnus will view pseudo-articles asynchronously."
662   :group 'gnus-extract-view
663   :type 'boolean)
664
665 (defcustom gnus-auto-expirable-marks
666   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
667         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
668         gnus-duplicate-mark)
669   "*The list of marks converted into expiration if a group is auto-expirable."
670   :version "24.1"
671   :group 'gnus-summary
672   :type '(repeat character))
673
674 (defcustom gnus-inhibit-user-auto-expire t
675   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
676   :version "21.1"
677   :group 'gnus-summary
678   :type 'boolean)
679
680 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
681   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
682 If nil, the expirable marks will be unchanged except that the marks
683 will be removed when copying or moving articles to a group that has
684 not turned auto-expire on.  If non-nil, articles that have been read
685 will be marked as expirable when being copied or moved to a group in
686 which auto-expire is turned on."
687   :version "23.2"
688   :type 'boolean
689   :group 'gnus-summary-marks)
690
691 (defcustom gnus-view-pseudos nil
692   "*If `automatic', pseudo-articles will be viewed automatically.
693 If `not-confirm', pseudos will be viewed automatically, and the user
694 will not be asked to confirm the command."
695   :group 'gnus-extract-view
696   :type '(choice (const :tag "off" nil)
697                  (const automatic)
698                  (const not-confirm)))
699
700 (defcustom gnus-view-pseudos-separately t
701   "*If non-nil, one pseudo-article will be created for each file to be viewed.
702 If nil, all files that use the same viewing command will be given as a
703 list of parameters to that command."
704   :group 'gnus-extract-view
705   :type 'boolean)
706
707 (defcustom gnus-insert-pseudo-articles t
708   "*If non-nil, insert pseudo-articles when decoding articles."
709   :group 'gnus-extract-view
710   :type 'boolean)
711
712 (defcustom gnus-summary-dummy-line-format
713   "   %(:                             :%) %S\n"
714   "*The format specification for the dummy roots in the summary buffer.
715 It works along the same lines as a normal formatting string,
716 with some simple extensions.
717
718 %S  The subject
719
720 General format specifiers can also be used.
721 See `(gnus)Formatting Variables'."
722   :link '(custom-manual "(gnus)Formatting Variables")
723   :group 'gnus-threading
724   :type 'string)
725
726 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
727   "*The format specification for the summary mode line.
728 It works along the same lines as a normal formatting string,
729 with some simple extensions:
730
731 %G  Group name
732 %p  Unprefixed group name
733 %A  Current article number
734 %z  Current article score
735 %V  Gnus version
736 %U  Number of unread articles in the group
737 %e  Number of unselected articles in the group
738 %Z  A string with unread/unselected article counts
739 %g  Shortish group name
740 %S  Subject of the current article
741 %u  User-defined spec
742 %s  Current score file name
743 %d  Number of dormant articles
744 %r  Number of articles that have been marked as read in this session
745 %E  Number of articles expunged by the score files"
746   :group 'gnus-summary-format
747   :type 'string)
748
749 (defcustom gnus-list-identifiers nil
750   "Regexp that matches list identifiers to be removed from subject.
751 This can also be a list of regexps."
752   :version "21.1"
753   :group 'gnus-summary-format
754   :group 'gnus-article-hiding
755   :type '(choice (const :tag "none" nil)
756                  (regexp :value ".*")
757                  (repeat :value (".*") regexp)))
758
759 (defcustom gnus-summary-mark-below 0
760   "*Mark all articles with a score below this variable as read.
761 This variable is local to each summary buffer and usually set by the
762 score file."
763   :group 'gnus-score-default
764   :type 'integer)
765
766 (defun gnus-widget-reversible-match (widget value)
767   "Ignoring WIDGET, convert VALUE to internal form.
768 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
769   ;; (debug value)
770   (or (symbolp value)
771       (and (listp value)
772            (eq (length value) 2)
773            (eq (nth 0 value) 'not)
774            (symbolp (nth 1 value)))))
775
776 (defun gnus-widget-reversible-to-internal (widget value)
777   "Ignoring WIDGET, convert VALUE to internal form.
778 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
779 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
780   ;; (debug value)
781   (if (atom value)
782       (list value nil)
783     (list (nth 1 value) t)))
784
785 (defun gnus-widget-reversible-to-external (widget value)
786   "Ignoring WIDGET, convert VALUE to external form.
787 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
788 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
789   ;; (debug value)
790   (if (nth 1 value)
791       (list 'not (nth 0 value))
792     (nth 0 value)))
793
794 (define-widget 'gnus-widget-reversible 'group
795   "A `group' that convert values."
796   :match 'gnus-widget-reversible-match
797   :value-to-internal 'gnus-widget-reversible-to-internal
798   :value-to-external 'gnus-widget-reversible-to-external)
799
800 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
801   "*List of functions used for sorting articles in the summary buffer.
802
803 Each function takes two articles and returns non-nil if the first
804 article should be sorted before the other.  If you use more than one
805 function, the primary sort function should be the last.  You should
806 probably always include `gnus-article-sort-by-number' in the list of
807 sorting functions -- preferably first.  Also note that sorting by date
808 is often much slower than sorting by number, and the sorting order is
809 very similar.  (Sorting by date means sorting by the time the message
810 was sent, sorting by number means sorting by arrival time.)
811
812 Each item can also be a list `(not F)' where F is a function;
813 this reverses the sort order.
814
815 Ready-made functions include `gnus-article-sort-by-number',
816 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
817 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
818 and `gnus-article-sort-by-score'.
819
820 When threading is turned on, the variable `gnus-thread-sort-functions'
821 controls how articles are sorted."
822   :group 'gnus-summary-sort
823   :type '(repeat (gnus-widget-reversible
824                   (choice (function-item gnus-article-sort-by-number)
825                           (function-item gnus-article-sort-by-author)
826                           (function-item gnus-article-sort-by-subject)
827                           (function-item gnus-article-sort-by-date)
828                           (function-item gnus-article-sort-by-score)
829                           (function-item gnus-article-sort-by-random)
830                           (function :tag "other"))
831                   (boolean :tag "Reverse order"))))
832
833
834 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
835   "*List of functions used for sorting threads in the summary buffer.
836 By default, threads are sorted by article number.
837
838 Each function takes two threads and returns non-nil if the first
839 thread should be sorted before the other.  If you use more than one
840 function, the primary sort function should be the last.  You should
841 probably always include `gnus-thread-sort-by-number' in the list of
842 sorting functions -- preferably first.  Also note that sorting by date
843 is often much slower than sorting by number, and the sorting order is
844 very similar.  (Sorting by date means sorting by the time the message
845 was sent, sorting by number means sorting by arrival time.)
846
847 Each list item can also be a list `(not F)' where F is a
848 function; this specifies reversed sort order.
849
850 Ready-made functions include `gnus-thread-sort-by-number',
851 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
852 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
853 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
854 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
855 and `gnus-thread-sort-by-total-score' (see
856 `gnus-thread-score-function').
857
858 When threading is turned off, the variable
859 `gnus-article-sort-functions' controls how articles are sorted."
860   :group 'gnus-summary-sort
861   :type '(repeat
862           (gnus-widget-reversible
863            (choice (function-item gnus-thread-sort-by-number)
864                    (function-item gnus-thread-sort-by-author)
865                    (function-item gnus-thread-sort-by-recipient)
866                    (function-item gnus-thread-sort-by-subject)
867                    (function-item gnus-thread-sort-by-date)
868                    (function-item gnus-thread-sort-by-score)
869                    (function-item gnus-thread-sort-by-most-recent-number)
870                    (function-item gnus-thread-sort-by-most-recent-date)
871                    (function-item gnus-thread-sort-by-random)
872                    (function-item gnus-thread-sort-by-total-score)
873                    (function :tag "other"))
874            (boolean :tag "Reverse order"))))
875
876 (defcustom gnus-thread-score-function '+
877   "*Function used for calculating the total score of a thread.
878
879 The function is called with the scores of the article and each
880 subthread and should then return the score of the thread.
881
882 Some functions you can use are `+', `max', or `min'."
883   :group 'gnus-summary-sort
884   :type 'function)
885
886 (defcustom gnus-summary-expunge-below nil
887   "All articles that have a score less than this variable will be expunged.
888 This variable is local to the summary buffers."
889   :group 'gnus-score-default
890   :type '(choice (const :tag "off" nil)
891                  integer))
892
893 (defcustom gnus-thread-expunge-below nil
894   "All threads that have a total score less than this variable will be expunged.
895 See `gnus-thread-score-function' for en explanation of what a
896 \"thread score\" is.
897
898 This variable is local to the summary buffers."
899   :group 'gnus-threading
900   :group 'gnus-score-default
901   :type '(choice (const :tag "off" nil)
902                  integer))
903
904 (defcustom gnus-summary-mode-hook nil
905   "*A hook for Gnus summary mode.
906 This hook is run before any variables are set in the summary buffer."
907   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
908   :group 'gnus-summary-various
909   :type 'hook)
910
911 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
912 (when (featurep 'xemacs)
913   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
914   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
915   (add-hook 'gnus-summary-mode-hook
916             'gnus-xmas-switch-horizontal-scrollbar-off))
917
918 (defcustom gnus-summary-menu-hook nil
919   "*Hook run after the creation of the summary mode menu."
920   :group 'gnus-summary-visual
921   :type 'hook)
922
923 (defcustom gnus-summary-exit-hook nil
924   "*A hook called on exit from the summary buffer.
925 It will be called with point in the group buffer."
926   :group 'gnus-summary-exit
927   :type 'hook)
928
929 (defcustom gnus-summary-prepare-hook nil
930   "*A hook called after the summary buffer has been generated.
931 If you want to modify the summary buffer, you can use this hook."
932   :group 'gnus-summary-various
933   :type 'hook)
934
935 (defcustom gnus-summary-prepared-hook nil
936   "*A hook called as the last thing after the summary buffer has been generated."
937   :group 'gnus-summary-various
938   :type 'hook)
939
940 (defcustom gnus-summary-generate-hook nil
941   "*A hook run just before generating the summary buffer.
942 This hook is commonly used to customize threading variables and the
943 like."
944   :group 'gnus-summary-various
945   :type 'hook)
946
947 (defcustom gnus-select-group-hook nil
948   "*A hook called when a newsgroup is selected.
949
950 If you'd like to simplify subjects like the
951 `gnus-summary-next-same-subject' command does, you can use the
952 following hook:
953
954  (add-hook gnus-select-group-hook
955            (lambda ()
956              (mapcar (lambda (header)
957                        (mail-header-set-subject
958                         header
959                         (gnus-simplify-subject
960                          (mail-header-subject header) 're-only)))
961                      gnus-newsgroup-headers)))"
962   :group 'gnus-group-select
963   :type 'hook)
964
965 (defcustom gnus-select-article-hook nil
966   "*A hook called when an article is selected."
967   :group 'gnus-summary-choose
968   :options '(gnus-agent-fetch-selected-article)
969   :type 'hook)
970
971 (defcustom gnus-visual-mark-article-hook
972   (list 'gnus-highlight-selected-summary)
973   "*Hook run after selecting an article in the summary buffer.
974 It is meant to be used for highlighting the article in some way.  It
975 is not run if `gnus-visual' is nil."
976   :group 'gnus-summary-visual
977   :type 'hook)
978
979 (defcustom gnus-parse-headers-hook nil
980   "*A hook called before parsing the headers."
981   :group 'gnus-various
982   :type 'hook)
983
984 (defcustom gnus-exit-group-hook nil
985   "*A hook called when exiting summary mode.
986 This hook is not called from the non-updating exit commands like `Q'."
987   :group 'gnus-various
988   :type 'hook)
989
990 (defcustom gnus-summary-update-hook nil
991   "*A hook called when a summary line is changed.
992 The hook will not be called if `gnus-visual' is nil.
993
994 The default function `gnus-summary-highlight-line' will
995 highlight the line according to the `gnus-summary-highlight'
996 variable."
997   :group 'gnus-summary-visual
998   :type 'hook)
999
1000 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1001   "*A hook called when an article is selected for the first time.
1002 The hook is intended to mark an article as read (or unread)
1003 automatically when it is selected."
1004   :group 'gnus-summary-choose
1005   :type 'hook)
1006
1007 (defcustom gnus-group-no-more-groups-hook nil
1008   "*A hook run when returning to group mode having no more (unread) groups."
1009   :group 'gnus-group-select
1010   :type 'hook)
1011
1012 (defcustom gnus-ps-print-hook nil
1013   "*A hook run before ps-printing something from Gnus."
1014   :group 'gnus-summary
1015   :type 'hook)
1016
1017 (defcustom gnus-summary-article-move-hook nil
1018   "*A hook called after an article is moved, copied, respooled, or crossposted."
1019   :version "22.1"
1020   :group 'gnus-summary
1021   :type 'hook)
1022
1023 (defcustom gnus-summary-article-delete-hook nil
1024   "*A hook called after an article is deleted."
1025   :version "22.1"
1026   :group 'gnus-summary
1027   :type 'hook)
1028
1029 (defcustom gnus-summary-article-expire-hook nil
1030   "*A hook called after an article is expired."
1031   :version "22.1"
1032   :group 'gnus-summary
1033   :type 'hook)
1034
1035 (defcustom gnus-summary-display-arrow
1036   (and (fboundp 'display-graphic-p)
1037        (display-graphic-p))
1038   "*If non-nil, display an arrow highlighting the current article."
1039   :version "22.1"
1040   :group 'gnus-summary
1041   :type 'boolean)
1042
1043 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1044   "Face used for highlighting the current article in the summary buffer."
1045   :group 'gnus-summary-visual
1046   :type 'face)
1047
1048 (defvar gnus-tmp-downloaded nil)
1049
1050 (defcustom gnus-summary-highlight
1051   '(((eq mark gnus-canceled-mark)
1052      . gnus-summary-cancelled)
1053     ((and uncached (> score default-high))
1054      . gnus-summary-high-undownloaded)
1055     ((and uncached (< score default-low))
1056      . gnus-summary-low-undownloaded)
1057     (uncached
1058      . gnus-summary-normal-undownloaded)
1059     ((and (> score default-high)
1060           (or (eq mark gnus-dormant-mark)
1061               (eq mark gnus-ticked-mark)))
1062      . gnus-summary-high-ticked)
1063     ((and (< score default-low)
1064           (or (eq mark gnus-dormant-mark)
1065               (eq mark gnus-ticked-mark)))
1066      . gnus-summary-low-ticked)
1067     ((or (eq mark gnus-dormant-mark)
1068          (eq mark gnus-ticked-mark))
1069      . gnus-summary-normal-ticked)
1070     ((and (> score default-high) (eq mark gnus-ancient-mark))
1071      . gnus-summary-high-ancient)
1072     ((and (< score default-low) (eq mark gnus-ancient-mark))
1073      . gnus-summary-low-ancient)
1074     ((eq mark gnus-ancient-mark)
1075      . gnus-summary-normal-ancient)
1076     ((and (> score default-high) (eq mark gnus-unread-mark))
1077      . gnus-summary-high-unread)
1078     ((and (< score default-low) (eq mark gnus-unread-mark))
1079      . gnus-summary-low-unread)
1080     ((eq mark gnus-unread-mark)
1081      . gnus-summary-normal-unread)
1082     ((> score default-high)
1083      . gnus-summary-high-read)
1084     ((< score default-low)
1085      . gnus-summary-low-read)
1086     (t
1087      . gnus-summary-normal-read))
1088   "*Controls the highlighting of summary buffer lines.
1089
1090 A list of (FORM . FACE) pairs.  When deciding how a particular
1091 summary line should be displayed, each form is evaluated.  The content
1092 of the face field after the first true form is used.  You can change
1093 how those summary lines are displayed, by editing the face field.
1094
1095 You can use the following variables in the FORM field.
1096
1097 score:        The article's score.
1098 default:      The default article score.
1099 default-high: The default score for high scored articles.
1100 default-low:  The default score for low scored articles.
1101 below:        The score below which articles are automatically marked as read.
1102 mark:         The article's mark.
1103 uncached:     Non-nil if the article is uncached."
1104   :group 'gnus-summary-visual
1105   :type '(repeat (cons (sexp :tag "Form" nil)
1106                        face)))
1107 (put 'gnus-summary-highlight 'risky-local-variable t)
1108
1109 (defcustom gnus-alter-header-function nil
1110   "Function called to allow alteration of article header structures.
1111 The function is called with one parameter, the article header vector,
1112 which it may alter in any way."
1113   :type '(choice (const :tag "None" nil)
1114                  function)
1115   :group 'gnus-summary)
1116
1117 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1118   "Function used to decode a string with encoded words.")
1119
1120 (defvar gnus-decode-encoded-address-function
1121   'mail-decode-encoded-address-string
1122   "Function used to decode addresses with encoded words.")
1123
1124 (defcustom gnus-extra-headers '(To Newsgroups)
1125   "*Extra headers to parse."
1126   :version "21.1"
1127   :group 'gnus-summary
1128   :type '(repeat symbol))
1129
1130 (defcustom gnus-ignored-from-addresses
1131   (and user-mail-address
1132        (not (string= user-mail-address ""))
1133        (regexp-quote user-mail-address))
1134   "*From headers that may be suppressed in favor of To headers.
1135 This can be a regexp or a list of regexps."
1136   :version "21.1"
1137   :group 'gnus-summary
1138   :type '(choice regexp
1139                  (repeat :tag "Regexp List" regexp)))
1140
1141 (defsubst gnus-ignored-from-addresses ()
1142   (gmm-regexp-concat gnus-ignored-from-addresses))
1143
1144 (defcustom gnus-summary-to-prefix "-> "
1145   "*String prefixed to the To field in the summary line when
1146 using `gnus-ignored-from-addresses'."
1147   :version "22.1"
1148   :group 'gnus-summary
1149   :type 'string)
1150
1151 (defcustom gnus-summary-newsgroup-prefix "=> "
1152   "*String prefixed to the Newsgroup field in the summary
1153 line when using `gnus-ignored-from-addresses'."
1154   :version "22.1"
1155   :group 'gnus-summary
1156   :type 'string)
1157
1158 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1159   "List of charsets that should be ignored.
1160 When these charsets are used in the \"charset\" parameter, the
1161 default charset will be used instead."
1162   :version "21.1"
1163   :type '(repeat symbol)
1164   :group 'gnus-charset)
1165
1166 (defcustom gnus-newsgroup-maximum-articles nil
1167   "The maximum number of articles a newsgroup.
1168 If this is a number, old articles in a newsgroup exceeding this number
1169 are silently ignored.  If it is nil, no article is ignored.  Note that
1170 setting this variable to a number might prevent you from reading very
1171 old articles."
1172   :group 'gnus-group-select
1173   :version "22.2"
1174   :type '(choice (const :tag "No limit" nil)
1175                  integer))
1176
1177 (gnus-define-group-parameter
1178  ignored-charsets
1179  :type list
1180  :function-document
1181  "Return the ignored charsets of GROUP."
1182  :variable gnus-group-ignored-charsets-alist
1183  :variable-default
1184  '(("alt\\.chinese\\.text" iso-8859-1))
1185  :variable-document
1186  "Alist of regexps (to match group names) and charsets that should be ignored.
1187 When these charsets are used in the \"charset\" parameter, the
1188 default charset will be used instead."
1189  :variable-group gnus-charset
1190  :variable-type '(repeat (cons (regexp :tag "Group")
1191                                (repeat symbol)))
1192  :parameter-type '(choice :tag "Ignored charsets"
1193                           :value nil
1194                           (repeat (symbol)))
1195  :parameter-document       "\
1196 List of charsets that should be ignored.
1197
1198 When these charsets are used in the \"charset\" parameter, the
1199 default charset will be used instead.")
1200
1201 (defcustom gnus-group-highlight-words-alist nil
1202   "Alist of group regexps and highlight regexps.
1203 This variable uses the same syntax as `gnus-emphasis-alist'."
1204   :version "21.1"
1205   :type '(repeat (cons (regexp :tag "Group")
1206                        (repeat (list (regexp :tag "Highlight regexp")
1207                                      (number :tag "Group for entire word" 0)
1208                                      (number :tag "Group for displayed part" 0)
1209                                      (symbol :tag "Face"
1210                                              gnus-emphasis-highlight-words)))))
1211   :group 'gnus-summary-visual)
1212
1213 (defcustom gnus-summary-show-article-charset-alist
1214   nil
1215   "Alist of number and charset.
1216 The article will be shown with the charset corresponding to the
1217 numbered argument.
1218 For example: ((1 . cn-gb-2312) (2 . big5))."
1219   :version "21.1"
1220   :type '(repeat (cons (number :tag "Argument" 1)
1221                        (symbol :tag "Charset")))
1222   :group 'gnus-charset)
1223
1224 (defcustom gnus-preserve-marks t
1225   "Whether marks are preserved when moving, copying and respooling messages."
1226   :version "21.1"
1227   :type 'boolean
1228   :group 'gnus-summary-marks)
1229
1230 (defcustom gnus-propagate-marks t
1231   "If non-nil, do not propagate marks to the backends."
1232   :version "23.1" ;; No Gnus
1233   :type 'boolean
1234   :group 'gnus-summary-marks)
1235
1236 (defcustom gnus-alter-articles-to-read-function nil
1237   "Function to be called to alter the list of articles to be selected."
1238   :type '(choice (const nil) function)
1239   :group 'gnus-summary)
1240
1241 (defcustom gnus-orphan-score nil
1242   "*All orphans get this score added.  Set in the score file."
1243   :group 'gnus-score-default
1244   :type '(choice (const nil)
1245                  integer))
1246
1247 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1248   "*A regexp to match MIME parts when saving multiple parts of a
1249 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1250 This regexp will be used by default when prompting the user for which
1251 type of files to save."
1252   :group 'gnus-summary
1253   :type 'regexp)
1254
1255 (defcustom gnus-read-all-available-headers nil
1256   "Whether Gnus should parse all headers made available to it.
1257 This is mostly relevant for slow back ends where the user may
1258 wish to widen the summary buffer to include all headers
1259 that were fetched."
1260   :version "22.1"
1261   :group 'gnus-summary
1262   :type '(choice boolean regexp))
1263
1264 (defcustom gnus-summary-pipe-output-default-command nil
1265   "Command (and optional arguments) used to pipe article to subprocess.
1266 This will be used as the default command if it is non-nil.  The value
1267 will be updated if you modify it when executing the command
1268 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1269   :version "23.1" ;; No Gnus
1270   :group 'gnus-summary
1271   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1272
1273 (defcustom gnus-summary-muttprint-program "muttprint"
1274   "Command (and optional arguments) used to run Muttprint.
1275 The value will be updated if you modify it when executing the command
1276 `gnus-summary-muttprint'."
1277   :version "22.1"
1278   :group 'gnus-summary
1279   :type 'string)
1280
1281 (defcustom gnus-article-loose-mime t
1282   "If non-nil, don't require MIME-Version header.
1283 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1284 supply the MIME-Version header or deliberately strip it from the mail.
1285 If non-nil (the default), Gnus will treat some articles as MIME
1286 even if the MIME-Version header is missing."
1287   :version "22.1"
1288   :type 'boolean
1289   :group 'gnus-article-mime)
1290
1291 (defcustom gnus-article-emulate-mime t
1292   "If non-nil, use MIME emulation for uuencode and the like.
1293 This means that Gnus will search message bodies for text that look
1294 like uuencoded bits, yEncoded bits, and so on, and present that using
1295 the normal Gnus MIME machinery."
1296   :version "22.1"
1297   :type 'boolean
1298   :group 'gnus-article-mime)
1299
1300 ;;; Internal variables
1301
1302 (defvar gnus-summary-display-cache nil)
1303 (defvar gnus-article-mime-handles nil)
1304 (defvar gnus-article-decoded-p nil)
1305 (defvar gnus-article-charset nil)
1306 (defvar gnus-article-ignored-charsets nil)
1307 (defvar gnus-scores-exclude-files nil)
1308 (defvar gnus-page-broken nil)
1309
1310 (defvar gnus-original-article nil)
1311 (defvar gnus-article-internal-prepare-hook nil)
1312 (defvar gnus-newsgroup-process-stack nil)
1313
1314 (defvar gnus-thread-indent-array nil)
1315 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1316 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1317   "Function called to sort the articles within a thread after it has been gathered together.")
1318
1319 (defvar gnus-summary-save-parts-type-history nil)
1320 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1321
1322 ;; Avoid highlighting in kill files.
1323 (defvar gnus-summary-inhibit-highlight nil)
1324 (defvar gnus-newsgroup-selected-overlay nil)
1325 (defvar gnus-inhibit-limiting nil)
1326 (defvar gnus-newsgroup-adaptive-score-file nil)
1327 (defvar gnus-current-score-file nil)
1328 (defvar gnus-current-move-group nil)
1329 (defvar gnus-current-copy-group nil)
1330 (defvar gnus-current-crosspost-group nil)
1331 (defvar gnus-newsgroup-display nil)
1332
1333 (defvar gnus-newsgroup-dependencies nil)
1334 (defvar gnus-newsgroup-adaptive nil)
1335 (defvar gnus-summary-display-article-function nil)
1336 (defvar gnus-summary-highlight-line-function nil
1337   "Function called after highlighting a summary line.")
1338
1339 (defvar gnus-summary-line-format-alist
1340   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1341     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1342     (?s gnus-tmp-subject-or-nil ?s)
1343     (?n gnus-tmp-name ?s)
1344     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1345         ?s)
1346     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1347             gnus-tmp-from) ?s)
1348     (?F gnus-tmp-from ?s)
1349     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1350     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1351     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1352     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1353     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1354     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1355     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1356     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1357     (?L gnus-tmp-lines ?s)
1358     (?O gnus-tmp-downloaded ?c)
1359     (?I gnus-tmp-indentation ?s)
1360     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1361     (?R gnus-tmp-replied ?c)
1362     (?\[ gnus-tmp-opening-bracket ?c)
1363     (?\] gnus-tmp-closing-bracket ?c)
1364     (?\> (make-string gnus-tmp-level ? ) ?s)
1365     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1366     (?i gnus-tmp-score ?d)
1367     (?z gnus-tmp-score-char ?c)
1368     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1369     (?U gnus-tmp-unread ?c)
1370     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1371         ?s)
1372     (?t (gnus-summary-number-of-articles-in-thread
1373          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1374         ?d)
1375     (?e (gnus-summary-number-of-articles-in-thread
1376          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1377         ?c)
1378     (?u gnus-tmp-user-defined ?s)
1379     (?P (gnus-pick-line-number) ?d)
1380     (?B gnus-tmp-thread-tree-header-string ?s)
1381     (user-date (gnus-user-date
1382                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1383   "An alist of format specifications that can appear in summary lines.
1384 These are paired with what variables they correspond with, along with
1385 the type of the variable (string, integer, character, etc).")
1386
1387 (defvar gnus-summary-dummy-line-format-alist
1388   `((?S gnus-tmp-subject ?s)
1389     (?N gnus-tmp-number ?d)
1390     (?u gnus-tmp-user-defined ?s)))
1391
1392 (defvar gnus-summary-mode-line-format-alist
1393   `((?G gnus-tmp-group-name ?s)
1394     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1395     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1396     (?A gnus-tmp-article-number ?d)
1397     (?Z gnus-tmp-unread-and-unselected ?s)
1398     (?V gnus-version ?s)
1399     (?U gnus-tmp-unread-and-unticked ?d)
1400     (?S gnus-tmp-subject ?s)
1401     (?e gnus-tmp-unselected ?d)
1402     (?u gnus-tmp-user-defined ?s)
1403     (?d (length gnus-newsgroup-dormant) ?d)
1404     (?t (length gnus-newsgroup-marked) ?d)
1405     (?h (length gnus-newsgroup-spam-marked) ?d)
1406     (?r (length gnus-newsgroup-reads) ?d)
1407     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1408     (?E gnus-newsgroup-expunged-tally ?d)
1409     (?s (gnus-current-score-file-nondirectory) ?s)))
1410
1411 ;; This is here rather than in gnus-art for compilation reasons.
1412 (defvar gnus-article-mode-line-format-alist
1413   (nconc '((?w (gnus-article-wash-status) ?s)
1414            (?m (gnus-article-mime-part-status) ?s))
1415          gnus-summary-mode-line-format-alist))
1416
1417 (defvar gnus-last-search-regexp nil
1418   "Default regexp for article search command.")
1419
1420 (defvar gnus-last-shell-command nil
1421   "Default shell command on article.")
1422
1423 (defvar gnus-newsgroup-agentized nil
1424   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1425 (defvar gnus-newsgroup-begin nil)
1426 (defvar gnus-newsgroup-end nil)
1427 (defvar gnus-newsgroup-last-rmail nil)
1428 (defvar gnus-newsgroup-last-mail nil)
1429 (defvar gnus-newsgroup-last-folder nil)
1430 (defvar gnus-newsgroup-last-file nil)
1431 (defvar gnus-newsgroup-last-directory nil)
1432 (defvar gnus-newsgroup-auto-expire nil)
1433 (defvar gnus-newsgroup-active nil)
1434 (defvar gnus-newsgroup-highest nil)
1435
1436 (defvar gnus-newsgroup-data nil)
1437 (defvar gnus-newsgroup-data-reverse nil)
1438 (defvar gnus-newsgroup-limit nil)
1439 (defvar gnus-newsgroup-limits nil)
1440 (defvar gnus-summary-use-undownloaded-faces nil)
1441
1442 (defvar gnus-newsgroup-unreads nil
1443   "Sorted list of unread articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-unselected nil
1446   "Sorted list of unselected unread articles in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-reads nil
1449   "Alist of read articles and article marks in the current newsgroup.")
1450
1451 (defvar gnus-newsgroup-expunged-tally nil)
1452
1453 (defvar gnus-newsgroup-marked nil
1454   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1455
1456 (defvar gnus-newsgroup-spam-marked nil
1457   "List of ranges of articles that have been marked as spam.")
1458
1459 (defvar gnus-newsgroup-killed nil
1460   "List of ranges of articles that have been through the scoring process.")
1461
1462 (defvar gnus-newsgroup-cached nil
1463   "Sorted list of articles that come from the article cache.")
1464
1465 (defvar gnus-newsgroup-saved nil
1466   "List of articles that have been saved.")
1467
1468 (defvar gnus-newsgroup-kill-headers nil)
1469
1470 (defvar gnus-newsgroup-replied nil
1471   "List of articles that have been replied to in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-forwarded nil
1474   "List of articles that have been forwarded in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-recent nil
1477   "List of articles that have are recent in the current newsgroup.")
1478
1479 (defvar gnus-newsgroup-expirable nil
1480   "Sorted list of articles in the current newsgroup that can be expired.")
1481
1482 (defvar gnus-newsgroup-processable nil
1483   "List of articles in the current newsgroup that can be processed.")
1484
1485 (defvar gnus-newsgroup-downloadable nil
1486   "Sorted list of articles in the current newsgroup that can be processed.")
1487
1488 (defvar gnus-newsgroup-unfetched nil
1489   "Sorted list of articles in the current newsgroup whose headers have
1490 not been fetched into the agent.
1491
1492 This list will always be a subset of gnus-newsgroup-undownloaded.")
1493
1494 (defvar gnus-newsgroup-undownloaded nil
1495   "List of articles in the current newsgroup that haven't been downloaded.")
1496
1497 (defvar gnus-newsgroup-unsendable nil
1498   "List of articles in the current newsgroup that won't be sent.")
1499
1500 (defvar gnus-newsgroup-bookmarks nil
1501   "List of articles in the current newsgroup that have bookmarks.")
1502
1503 (defvar gnus-newsgroup-dormant nil
1504   "Sorted list of dormant articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-unseen nil
1507   "List of unseen articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-seen nil
1510   "Range of seen articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-articles nil
1513   "List of articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-scored nil
1516   "List of scored articles in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-headers nil
1519   "List of article headers in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-threads nil)
1522
1523 (defvar gnus-newsgroup-prepared nil
1524   "Whether the current group has been prepared properly.")
1525
1526 (defvar gnus-newsgroup-ancient nil
1527   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-sparse nil)
1530
1531 (defvar gnus-current-article nil)
1532 (defvar gnus-article-current nil)
1533 (defvar gnus-current-headers nil)
1534 (defvar gnus-have-all-headers nil)
1535 (defvar gnus-last-article nil)
1536 (defvar gnus-newsgroup-history nil)
1537 (defvar gnus-newsgroup-charset nil)
1538 (defvar gnus-newsgroup-ephemeral-charset nil)
1539 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1540
1541 (defvar gnus-article-before-search nil)
1542
1543 (defvar gnus-summary-local-variables
1544   '(gnus-newsgroup-name
1545
1546     ;; Marks lists
1547     gnus-newsgroup-unreads
1548     gnus-newsgroup-unselected
1549     gnus-newsgroup-marked
1550     gnus-newsgroup-spam-marked
1551     gnus-newsgroup-reads
1552     gnus-newsgroup-saved
1553     gnus-newsgroup-replied
1554     gnus-newsgroup-forwarded
1555     gnus-newsgroup-recent
1556     gnus-newsgroup-expirable
1557     gnus-newsgroup-killed
1558     gnus-newsgroup-unseen
1559     gnus-newsgroup-seen
1560     gnus-newsgroup-cached
1561     gnus-newsgroup-downloadable
1562     gnus-newsgroup-undownloaded
1563     gnus-newsgroup-unsendable
1564
1565     gnus-newsgroup-begin gnus-newsgroup-end
1566     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1567     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1568     gnus-newsgroup-last-directory
1569     gnus-newsgroup-auto-expire
1570     gnus-newsgroup-processable
1571     gnus-newsgroup-unfetched
1572     gnus-newsgroup-articles
1573     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1574     gnus-newsgroup-headers gnus-newsgroup-threads
1575     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1576     gnus-current-article gnus-current-headers gnus-have-all-headers
1577     gnus-last-article gnus-article-internal-prepare-hook
1578     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1579     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1580     gnus-thread-expunge-below
1581     gnus-score-alist gnus-current-score-file
1582     (gnus-summary-expunge-below . global)
1583     (gnus-summary-mark-below . global)
1584     (gnus-orphan-score . global)
1585     gnus-newsgroup-active gnus-scores-exclude-files
1586     gnus-newsgroup-highest
1587     gnus-newsgroup-history gnus-newsgroup-ancient
1588     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1589     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1590     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1591     (gnus-newsgroup-expunged-tally . 0)
1592     gnus-cache-removable-articles
1593     gnus-newsgroup-data gnus-newsgroup-data-reverse
1594     gnus-newsgroup-limit gnus-newsgroup-limits
1595     gnus-newsgroup-charset gnus-newsgroup-display
1596     gnus-summary-use-undownloaded-faces)
1597   "Variables that are buffer-local to the summary buffers.")
1598
1599 (defvar gnus-newsgroup-variables nil
1600   "A list of variables that have separate values in different newsgroups.
1601 A list of newsgroup (summary buffer) local variables, or cons of
1602 variables and their default expressions to be evalled (when the default
1603 values are not nil), that should be made global while the summary buffer
1604 is active.
1605
1606 Note: The default expressions will be evaluated (using function `eval')
1607 before assignment to the local variable rather than just assigned to it.
1608 If the default expression is the symbol `global', that symbol will not
1609 be evaluated but the global value of the local variable will be used
1610 instead.
1611
1612 These variables can be used to set variables in the group parameters
1613 while still allowing them to affect operations done in other buffers.
1614 For example:
1615
1616 \(setq gnus-newsgroup-variables
1617      '(message-use-followup-to
1618        (gnus-visible-headers .
1619          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1620 ")
1621
1622 (eval-when-compile
1623   ;; Bind features so that require will believe that gnus-sum has
1624   ;; already been loaded (avoids infinite recursion)
1625   (let ((features (cons 'gnus-sum features)))
1626     (require 'gnus-art)))
1627
1628 ;; MIME stuff.
1629
1630 (defvar gnus-decode-encoded-word-methods
1631   '(mail-decode-encoded-word-string)
1632   "List of methods used to decode encoded words.
1633
1634 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1635 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1636 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1637 whose names match REGEXP.
1638
1639 For example:
1640 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1641  mail-decode-encoded-word-string
1642  (\"chinese\" . rfc1843-decode-string))")
1643
1644 (defvar gnus-decode-encoded-word-methods-cache nil)
1645
1646 (defun gnus-multi-decode-encoded-word-string (string)
1647   "Apply the functions from `gnus-encoded-word-methods' that match."
1648   (unless (and gnus-decode-encoded-word-methods-cache
1649                (eq gnus-newsgroup-name
1650                    (car gnus-decode-encoded-word-methods-cache)))
1651     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1652     (dolist (method gnus-decode-encoded-word-methods)
1653       (if (symbolp method)
1654           (nconc gnus-decode-encoded-word-methods-cache (list method))
1655         (if (and gnus-newsgroup-name
1656                  (string-match (car method) gnus-newsgroup-name))
1657             (nconc gnus-decode-encoded-word-methods-cache
1658                    (list (cdr method)))))))
1659   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1660     (setq string (funcall method string))))
1661
1662 ;; Subject simplification.
1663
1664 (defun gnus-simplify-whitespace (str)
1665   "Remove excessive whitespace from STR."
1666   ;; Multiple spaces.
1667   (while (string-match "[ \t][ \t]+" str)
1668     (setq str (concat (substring str 0 (match-beginning 0))
1669                         " "
1670                         (substring str (match-end 0)))))
1671   ;; Leading spaces.
1672   (when (string-match "^[ \t]+" str)
1673     (setq str (substring str (match-end 0))))
1674   ;; Trailing spaces.
1675   (when (string-match "[ \t]+$" str)
1676     (setq str (substring str 0 (match-beginning 0))))
1677   str)
1678
1679 (defun gnus-simplify-all-whitespace (str)
1680   "Remove all whitespace from STR."
1681   (while (string-match "[ \t\n]+" str)
1682     (setq str (replace-match "" nil nil str)))
1683   str)
1684
1685 (defsubst gnus-simplify-subject-re (subject)
1686   "Remove \"Re:\" from subject lines."
1687   (if (string-match message-subject-re-regexp subject)
1688       (substring subject (match-end 0))
1689     subject))
1690
1691 (defun gnus-simplify-subject (subject &optional re-only)
1692   "Remove `Re:' and words in parentheses.
1693 If RE-ONLY is non-nil, strip leading `Re:'s only."
1694   (let ((case-fold-search t))           ;Ignore case.
1695     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1696     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1697       (setq subject (substring subject (match-end 0))))
1698     ;; Remove uninteresting prefixes.
1699     (when (and (not re-only)
1700                gnus-simplify-ignored-prefixes
1701                (string-match gnus-simplify-ignored-prefixes subject))
1702       (setq subject (substring subject (match-end 0))))
1703     ;; Remove words in parentheses from end.
1704     (unless re-only
1705       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1706         (setq subject (substring subject 0 (match-beginning 0)))))
1707     ;; Return subject string.
1708     subject))
1709
1710 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1711 ;; all whitespace.
1712 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1713   (goto-char (point-min))
1714   (while (re-search-forward regexp nil t)
1715     (replace-match (or newtext ""))))
1716
1717 (defun gnus-simplify-buffer-fuzzy ()
1718   "Simplify string in the buffer fuzzily.
1719 The string in the accessible portion of the current buffer is simplified.
1720 It is assumed to be a single-line subject.
1721 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1722 matter is removed.  Additional things can be deleted by setting
1723 `gnus-simplify-subject-fuzzy-regexp'."
1724   (let ((case-fold-search t)
1725         (modified-tick))
1726     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1727
1728     (while (not (eq modified-tick (buffer-modified-tick)))
1729       (setq modified-tick (buffer-modified-tick))
1730       (cond
1731        ((listp gnus-simplify-subject-fuzzy-regexp)
1732         (mapc 'gnus-simplify-buffer-fuzzy-step
1733               gnus-simplify-subject-fuzzy-regexp))
1734        (gnus-simplify-subject-fuzzy-regexp
1735         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1736       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1737       (gnus-simplify-buffer-fuzzy-step
1738        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1739       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1740
1741     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1742     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1743     (gnus-simplify-buffer-fuzzy-step " $")
1744     (gnus-simplify-buffer-fuzzy-step "^ +")))
1745
1746 (defun gnus-simplify-subject-fuzzy (subject)
1747   "Simplify a subject string fuzzily.
1748 See `gnus-simplify-buffer-fuzzy' for details."
1749   (save-excursion
1750     (gnus-set-work-buffer)
1751     (let ((case-fold-search t))
1752       ;; Remove uninteresting prefixes.
1753       (when (and gnus-simplify-ignored-prefixes
1754                  (string-match gnus-simplify-ignored-prefixes subject))
1755         (setq subject (substring subject (match-end 0))))
1756       (insert subject)
1757       (inline (gnus-simplify-buffer-fuzzy))
1758       (buffer-string))))
1759
1760 (defsubst gnus-simplify-subject-fully (subject)
1761   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1762   (cond
1763    (gnus-simplify-subject-functions
1764     (gnus-map-function gnus-simplify-subject-functions subject))
1765    ((null gnus-summary-gather-subject-limit)
1766     (gnus-simplify-subject-re subject))
1767    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1768     (gnus-simplify-subject-fuzzy subject))
1769    ((numberp gnus-summary-gather-subject-limit)
1770     (truncate-string-to-width (gnus-simplify-subject-re subject)
1771                               gnus-summary-gather-subject-limit))
1772    (t
1773     subject)))
1774
1775 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1776   "Check whether two subjects are equal.
1777 If optional argument SIMPLE-FIRST is t, first argument is already
1778 simplified."
1779   (cond
1780    ((null simple-first)
1781     (equal (gnus-simplify-subject-fully s1)
1782            (gnus-simplify-subject-fully s2)))
1783    (t
1784     (equal s1
1785            (gnus-simplify-subject-fully s2)))))
1786
1787 (defun gnus-summary-bubble-group ()
1788   "Increase the score of the current group.
1789 This is a handy function to add to `gnus-summary-exit-hook' to
1790 increase the score of each group you read."
1791   (gnus-group-add-score gnus-newsgroup-name))
1792
1793 \f
1794 ;;;
1795 ;;; Gnus summary mode
1796 ;;;
1797
1798 (put 'gnus-summary-mode 'mode-class 'special)
1799
1800 (defvar gnus-article-commands-menu)
1801
1802 ;; Non-orthogonal keys
1803
1804 (gnus-define-keys gnus-summary-mode-map
1805   " " gnus-summary-next-page
1806   "\177" gnus-summary-prev-page
1807   [delete] gnus-summary-prev-page
1808   [backspace] gnus-summary-prev-page
1809   "\r" gnus-summary-scroll-up
1810   "\M-\r" gnus-summary-scroll-down
1811   "n" gnus-summary-next-unread-article
1812   "p" gnus-summary-prev-unread-article
1813   "N" gnus-summary-next-article
1814   "P" gnus-summary-prev-article
1815   "\M-\C-n" gnus-summary-next-same-subject
1816   "\M-\C-p" gnus-summary-prev-same-subject
1817   "\M-n" gnus-summary-next-unread-subject
1818   "\M-p" gnus-summary-prev-unread-subject
1819   "." gnus-summary-first-unread-article
1820   "," gnus-summary-best-unread-article
1821   "\M-s" gnus-summary-search-article-forward
1822   "\M-r" gnus-summary-search-article-backward
1823   "\M-S" gnus-summary-repeat-search-article-forward
1824   "\M-R" gnus-summary-repeat-search-article-backward
1825   "<" gnus-summary-beginning-of-article
1826   ">" gnus-summary-end-of-article
1827   "j" gnus-summary-goto-article
1828   "^" gnus-summary-refer-parent-article
1829   "\M-^" gnus-summary-refer-article
1830   "u" gnus-summary-tick-article-forward
1831   "!" gnus-summary-tick-article-forward
1832   "U" gnus-summary-tick-article-backward
1833   "d" gnus-summary-mark-as-read-forward
1834   "D" gnus-summary-mark-as-read-backward
1835   "E" gnus-summary-mark-as-expirable
1836   "\M-u" gnus-summary-clear-mark-forward
1837   "\M-U" gnus-summary-clear-mark-backward
1838   "k" gnus-summary-kill-same-subject-and-select
1839   "\C-k" gnus-summary-kill-same-subject
1840   "\M-\C-k" gnus-summary-kill-thread
1841   "\M-\C-l" gnus-summary-lower-thread
1842   "e" gnus-summary-edit-article
1843   "#" gnus-summary-mark-as-processable
1844   "\M-#" gnus-summary-unmark-as-processable
1845   "\M-\C-t" gnus-summary-toggle-threads
1846   "\M-\C-s" gnus-summary-show-thread
1847   "\M-\C-h" gnus-summary-hide-thread
1848   "\M-\C-f" gnus-summary-next-thread
1849   "\M-\C-b" gnus-summary-prev-thread
1850   [(meta down)] gnus-summary-next-thread
1851   [(meta up)] gnus-summary-prev-thread
1852   "\M-\C-u" gnus-summary-up-thread
1853   "\M-\C-d" gnus-summary-down-thread
1854   "&" gnus-summary-execute-command
1855   "c" gnus-summary-catchup-and-exit
1856   "\C-w" gnus-summary-mark-region-as-read
1857   "\C-t" gnus-summary-toggle-truncation
1858   "?" gnus-summary-mark-as-dormant
1859   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1860   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1861   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1862   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1863   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1864   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1865   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1866   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1867   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1868   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1869   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1870   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1871   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1872   "=" gnus-summary-expand-window
1873   "\C-x\C-s" gnus-summary-reselect-current-group
1874   "\M-g" gnus-summary-rescan-group
1875   "\C-c\C-r" gnus-summary-caesar-message
1876   "f" gnus-summary-followup
1877   "F" gnus-summary-followup-with-original
1878   "C" gnus-summary-cancel-article
1879   "r" gnus-summary-reply
1880   "R" gnus-summary-reply-with-original
1881   "\C-c\C-f" gnus-summary-mail-forward
1882   "o" gnus-summary-save-article
1883   "\C-o" gnus-summary-save-article-mail
1884   "|" gnus-summary-pipe-output
1885   "\M-k" gnus-summary-edit-local-kill
1886   "\M-K" gnus-summary-edit-global-kill
1887   ;; "V" gnus-version
1888   "\C-c\C-d" gnus-summary-describe-group
1889   "q" gnus-summary-exit
1890   "Q" gnus-summary-exit-no-update
1891   "\C-c\C-i" gnus-info-find-node
1892   gnus-mouse-2 gnus-mouse-pick-article
1893   [follow-link] mouse-face
1894   "m" gnus-summary-mail-other-window
1895   "a" gnus-summary-post-news
1896   "x" gnus-summary-limit-to-unread
1897   "s" gnus-summary-isearch-article
1898   "t" gnus-summary-toggle-header
1899   "g" gnus-summary-show-article
1900   "l" gnus-summary-goto-last-article
1901   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1902   "\C-d" gnus-summary-enter-digest-group
1903   "\M-\C-d" gnus-summary-read-document
1904   "\M-\C-e" gnus-summary-edit-parameters
1905   "\M-\C-a" gnus-summary-customize-parameters
1906   "\C-c\C-b" gnus-bug
1907   "*" gnus-cache-enter-article
1908   "\M-*" gnus-cache-remove-article
1909   "\M-&" gnus-summary-universal-argument
1910   "\C-l" gnus-recenter
1911   "I" gnus-summary-increase-score
1912   "L" gnus-summary-lower-score
1913   "\M-i" gnus-symbolic-argument
1914   "h" gnus-summary-select-article-buffer
1915
1916   "b" gnus-article-view-part
1917   "\M-t" gnus-summary-toggle-display-buttonized
1918
1919   "V" gnus-summary-score-map
1920   "X" gnus-uu-extract-map
1921   "S" gnus-summary-send-map)
1922
1923 ;; Sort of orthogonal keymap
1924 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1925   "t" gnus-summary-tick-article-forward
1926   "!" gnus-summary-tick-article-forward
1927   "d" gnus-summary-mark-as-read-forward
1928   "r" gnus-summary-mark-as-read-forward
1929   "c" gnus-summary-clear-mark-forward
1930   " " gnus-summary-clear-mark-forward
1931   "e" gnus-summary-mark-as-expirable
1932   "x" gnus-summary-mark-as-expirable
1933   "?" gnus-summary-mark-as-dormant
1934   "b" gnus-summary-set-bookmark
1935   "B" gnus-summary-remove-bookmark
1936   "#" gnus-summary-mark-as-processable
1937   "\M-#" gnus-summary-unmark-as-processable
1938   "S" gnus-summary-limit-include-expunged
1939   "C" gnus-summary-catchup
1940   "H" gnus-summary-catchup-to-here
1941   "h" gnus-summary-catchup-from-here
1942   "\C-c" gnus-summary-catchup-all
1943   "k" gnus-summary-kill-same-subject-and-select
1944   "K" gnus-summary-kill-same-subject
1945   "P" gnus-uu-mark-map)
1946
1947 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1948   "c" gnus-summary-clear-above
1949   "u" gnus-summary-tick-above
1950   "m" gnus-summary-mark-above
1951   "k" gnus-summary-kill-below)
1952
1953 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1954   "/" gnus-summary-limit-to-subject
1955   "n" gnus-summary-limit-to-articles
1956   "b" gnus-summary-limit-to-bodies
1957   "h" gnus-summary-limit-to-headers
1958   "w" gnus-summary-pop-limit
1959   "s" gnus-summary-limit-to-subject
1960   "a" gnus-summary-limit-to-author
1961   "u" gnus-summary-limit-to-unread
1962   "m" gnus-summary-limit-to-marks
1963   "M" gnus-summary-limit-exclude-marks
1964   "v" gnus-summary-limit-to-score
1965   "*" gnus-summary-limit-include-cached
1966   "D" gnus-summary-limit-include-dormant
1967   "T" gnus-summary-limit-include-thread
1968   "d" gnus-summary-limit-exclude-dormant
1969   "t" gnus-summary-limit-to-age
1970   "." gnus-summary-limit-to-unseen
1971   "x" gnus-summary-limit-to-extra
1972   "p" gnus-summary-limit-to-display-predicate
1973   "E" gnus-summary-limit-include-expunged
1974   "c" gnus-summary-limit-exclude-childless-dormant
1975   "C" gnus-summary-limit-mark-excluded-as-read
1976   "o" gnus-summary-insert-old-articles
1977   "N" gnus-summary-insert-new-articles
1978   "S" gnus-summary-limit-to-singletons
1979   "r" gnus-summary-limit-to-replied
1980   "R" gnus-summary-limit-to-recipient
1981   "A" gnus-summary-limit-to-address)
1982
1983 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1984   "n" gnus-summary-next-unread-article
1985   "p" gnus-summary-prev-unread-article
1986   "N" gnus-summary-next-article
1987   "P" gnus-summary-prev-article
1988   "\C-n" gnus-summary-next-same-subject
1989   "\C-p" gnus-summary-prev-same-subject
1990   "\M-n" gnus-summary-next-unread-subject
1991   "\M-p" gnus-summary-prev-unread-subject
1992   "f" gnus-summary-first-unread-article
1993   "b" gnus-summary-best-unread-article
1994   "j" gnus-summary-goto-article
1995   "g" gnus-summary-goto-subject
1996   "l" gnus-summary-goto-last-article
1997   "o" gnus-summary-pop-article)
1998
1999 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2000   "k" gnus-summary-kill-thread
2001   "E" gnus-summary-expire-thread
2002   "l" gnus-summary-lower-thread
2003   "i" gnus-summary-raise-thread
2004   "T" gnus-summary-toggle-threads
2005   "t" gnus-summary-rethread-current
2006   "^" gnus-summary-reparent-thread
2007   "\M-^" gnus-summary-reparent-children
2008   "s" gnus-summary-show-thread
2009   "S" gnus-summary-show-all-threads
2010   "h" gnus-summary-hide-thread
2011   "H" gnus-summary-hide-all-threads
2012   "n" gnus-summary-next-thread
2013   "p" gnus-summary-prev-thread
2014   "u" gnus-summary-up-thread
2015   "o" gnus-summary-top-thread
2016   "d" gnus-summary-down-thread
2017   "#" gnus-uu-mark-thread
2018   "\M-#" gnus-uu-unmark-thread)
2019
2020 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2021   "g" gnus-summary-prepare
2022   "c" gnus-summary-insert-cached-articles
2023   "d" gnus-summary-insert-dormant-articles
2024   "t" gnus-summary-insert-ticked-articles)
2025
2026 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2027   "c" gnus-summary-catchup-and-exit
2028   "C" gnus-summary-catchup-all-and-exit
2029   "E" gnus-summary-exit-no-update
2030   "Q" gnus-summary-exit
2031   "Z" gnus-summary-exit
2032   "n" gnus-summary-catchup-and-goto-next-group
2033   "p" gnus-summary-catchup-and-goto-prev-group
2034   "R" gnus-summary-reselect-current-group
2035   "G" gnus-summary-rescan-group
2036   "N" gnus-summary-next-group
2037   "s" gnus-summary-save-newsrc
2038   "P" gnus-summary-prev-group)
2039
2040 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2041   " " gnus-summary-next-page
2042   "n" gnus-summary-next-page
2043   "\177" gnus-summary-prev-page
2044   [delete] gnus-summary-prev-page
2045   "p" gnus-summary-prev-page
2046   "\r" gnus-summary-scroll-up
2047   "\M-\r" gnus-summary-scroll-down
2048   "<" gnus-summary-beginning-of-article
2049   ">" gnus-summary-end-of-article
2050   "b" gnus-summary-beginning-of-article
2051   "e" gnus-summary-end-of-article
2052   "^" gnus-summary-refer-parent-article
2053   "r" gnus-summary-refer-parent-article
2054   "C" gnus-summary-show-complete-article
2055   "D" gnus-summary-enter-digest-group
2056   "R" gnus-summary-refer-references
2057   "T" gnus-summary-refer-thread
2058   "g" gnus-summary-show-article
2059   "s" gnus-summary-isearch-article
2060   "P" gnus-summary-print-article
2061   "S" gnus-sticky-article
2062   "M" gnus-mailing-list-insinuate
2063   "t" gnus-article-babel)
2064
2065 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2066   "b" gnus-article-add-buttons
2067   "B" gnus-article-add-buttons-to-head
2068   "o" gnus-article-treat-overstrike
2069   "e" gnus-article-emphasize
2070   "w" gnus-article-fill-cited-article
2071   "Q" gnus-article-fill-long-lines
2072   "L" gnus-article-toggle-truncate-lines
2073   "C" gnus-article-capitalize-sentences
2074   "c" gnus-article-remove-cr
2075   "q" gnus-article-de-quoted-unreadable
2076   "6" gnus-article-de-base64-unreadable
2077   "Z" gnus-article-decode-HZ
2078   "A" gnus-article-treat-ansi-sequences
2079   "h" gnus-article-wash-html
2080   "u" gnus-article-unsplit-urls
2081   "s" gnus-summary-force-verify-and-decrypt
2082   "f" gnus-article-display-x-face
2083   "l" gnus-summary-stop-page-breaking
2084   "r" gnus-summary-caesar-message
2085   "m" gnus-summary-morse-message
2086   "t" gnus-summary-toggle-header
2087   "g" gnus-treat-smiley
2088   "v" gnus-summary-verbose-headers
2089   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2090   "p" gnus-article-verify-x-pgp-sig
2091   "d" gnus-article-treat-dumbquotes
2092   "i" gnus-summary-idna-message)
2093
2094 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2095   ;; mnemonic: deuglif*Y*
2096   "u" gnus-article-outlook-unwrap-lines
2097   "a" gnus-article-outlook-repair-attribution
2098   "c" gnus-article-outlook-rearrange-citation
2099   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2100
2101 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2102   "a" gnus-article-hide
2103   "h" gnus-article-hide-headers
2104   "b" gnus-article-hide-boring-headers
2105   "s" gnus-article-hide-signature
2106   "c" gnus-article-hide-citation
2107   "C" gnus-article-hide-citation-in-followups
2108   "l" gnus-article-hide-list-identifiers
2109   "B" gnus-article-strip-banner
2110   "P" gnus-article-hide-pem
2111   "\C-c" gnus-article-hide-citation-maybe)
2112
2113 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2114   "a" gnus-article-highlight
2115   "h" gnus-article-highlight-headers
2116   "c" gnus-article-highlight-citation
2117   "s" gnus-article-highlight-signature)
2118
2119 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2120   "f" gnus-article-treat-fold-headers
2121   "u" gnus-article-treat-unfold-headers
2122   "n" gnus-article-treat-fold-newsgroups)
2123
2124 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2125   "x" gnus-article-display-x-face
2126   "d" gnus-article-display-face
2127   "s" gnus-treat-smiley
2128   "D" gnus-article-remove-images
2129   "W" gnus-html-show-images
2130   "f" gnus-treat-from-picon
2131   "m" gnus-treat-mail-picon
2132   "n" gnus-treat-newsgroups-picon
2133   "g" gnus-treat-from-gravatar
2134   "h" gnus-treat-mail-gravatar)
2135
2136 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2137   "w" gnus-article-decode-mime-words
2138   "c" gnus-article-decode-charset
2139   "v" gnus-mime-view-all-parts
2140   "b" gnus-article-view-part)
2141
2142 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2143   "z" gnus-article-date-ut
2144   "u" gnus-article-date-ut
2145   "l" gnus-article-date-local
2146   "p" gnus-article-date-english
2147   "e" gnus-article-date-lapsed
2148   "o" gnus-article-date-original
2149   "i" gnus-article-date-iso8601
2150   "s" gnus-article-date-user)
2151
2152 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2153   "t" gnus-article-remove-trailing-blank-lines
2154   "l" gnus-article-strip-leading-blank-lines
2155   "m" gnus-article-strip-multiple-blank-lines
2156   "a" gnus-article-strip-blank-lines
2157   "A" gnus-article-strip-all-blank-lines
2158   "s" gnus-article-strip-leading-space
2159   "e" gnus-article-strip-trailing-space
2160   "w" gnus-article-remove-leading-whitespace)
2161
2162 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2163   "v" gnus-version
2164   "d" gnus-summary-describe-group
2165   "h" gnus-summary-describe-briefly
2166   "i" gnus-info-find-node
2167   "C" gnus-group-fetch-control)
2168
2169 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2170   "e" gnus-summary-expire-articles
2171   "\M-\C-e" gnus-summary-expire-articles-now
2172   "\177" gnus-summary-delete-article
2173   [delete] gnus-summary-delete-article
2174   [backspace] gnus-summary-delete-article
2175   "m" gnus-summary-move-article
2176   "r" gnus-summary-respool-article
2177   "w" gnus-summary-edit-article
2178   "c" gnus-summary-copy-article
2179   "B" gnus-summary-crosspost-article
2180   "q" gnus-summary-respool-query
2181   "t" gnus-summary-respool-trace
2182   "i" gnus-summary-import-article
2183   "I" gnus-summary-create-article
2184   "p" gnus-summary-article-posted-p)
2185
2186 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2187   "o" gnus-summary-save-article
2188   "m" gnus-summary-save-article-mail
2189   "F" gnus-summary-write-article-file
2190   "r" gnus-summary-save-article-rmail
2191   "f" gnus-summary-save-article-file
2192   "b" gnus-summary-save-article-body-file
2193   "B" gnus-summary-write-article-body-file
2194   "h" gnus-summary-save-article-folder
2195   "v" gnus-summary-save-article-vm
2196   "p" gnus-summary-pipe-output
2197   "P" gnus-summary-muttprint)
2198
2199 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2200   "b" gnus-summary-display-buttonized
2201   "m" gnus-summary-repair-multipart
2202   "v" gnus-article-view-part
2203   "o" gnus-article-save-part
2204   "O" gnus-article-save-part-and-strip
2205   "r" gnus-article-replace-part
2206   "d" gnus-article-delete-part
2207   "t" gnus-article-view-part-as-type
2208   "j" gnus-article-jump-to-part
2209   "c" gnus-article-copy-part
2210   "C" gnus-article-view-part-as-charset
2211   "e" gnus-article-view-part-externally
2212   "H" gnus-article-browse-html-article
2213   "E" gnus-article-encrypt-body
2214   "i" gnus-article-inline-part
2215   "|" gnus-article-pipe-part)
2216
2217 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2218   "p" gnus-summary-mark-as-processable
2219   "u" gnus-summary-unmark-as-processable
2220   "U" gnus-summary-unmark-all-processable
2221   "v" gnus-uu-mark-over
2222   "s" gnus-uu-mark-series
2223   "r" gnus-uu-mark-region
2224   "g" gnus-uu-unmark-region
2225   "R" gnus-uu-mark-by-regexp
2226   "G" gnus-uu-unmark-by-regexp
2227   "t" gnus-uu-mark-thread
2228   "T" gnus-uu-unmark-thread
2229   "a" gnus-uu-mark-all
2230   "b" gnus-uu-mark-buffer
2231   "S" gnus-uu-mark-sparse
2232   "k" gnus-summary-kill-process-mark
2233   "y" gnus-summary-yank-process-mark
2234   "w" gnus-summary-save-process-mark
2235   "i" gnus-uu-invert-processable)
2236
2237 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2238   ;;"x" gnus-uu-extract-any
2239   "m" gnus-summary-save-parts
2240   "u" gnus-uu-decode-uu
2241   "U" gnus-uu-decode-uu-and-save
2242   "s" gnus-uu-decode-unshar
2243   "S" gnus-uu-decode-unshar-and-save
2244   "o" gnus-uu-decode-save
2245   "O" gnus-uu-decode-save
2246   "b" gnus-uu-decode-binhex
2247   "B" gnus-uu-decode-binhex
2248   "Y" gnus-uu-decode-yenc
2249   "p" gnus-uu-decode-postscript
2250   "P" gnus-uu-decode-postscript-and-save)
2251
2252 (gnus-define-keys
2253     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2254   "u" gnus-uu-decode-uu-view
2255   "U" gnus-uu-decode-uu-and-save-view
2256   "s" gnus-uu-decode-unshar-view
2257   "S" gnus-uu-decode-unshar-and-save-view
2258   "o" gnus-uu-decode-save-view
2259   "O" gnus-uu-decode-save-view
2260   "b" gnus-uu-decode-binhex-view
2261   "B" gnus-uu-decode-binhex-view
2262   "p" gnus-uu-decode-postscript-view
2263   "P" gnus-uu-decode-postscript-and-save-view)
2264
2265 (defvar gnus-article-post-menu nil)
2266
2267 (defconst gnus-summary-menu-maxlen 20)
2268
2269 (defun gnus-summary-menu-split (menu)
2270   ;; If we have lots of elements, divide them into groups of 20
2271   ;; and make a pane (or submenu) for each one.
2272   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2273       (let ((menu menu) sublists next
2274             (i 1))
2275         (while menu
2276           ;; Pull off the next gnus-summary-menu-maxlen elements
2277           ;; and make them the next element of sublist.
2278           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2279           (if next
2280               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2281                       nil))
2282           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2283                                              (aref (car (last menu)) 0)) menu)
2284                                sublists))
2285           (setq i (1+ i))
2286           (setq menu next))
2287         (nreverse sublists))
2288     ;; Few elements--put them all in one pane.
2289     menu))
2290
2291 (defun gnus-summary-make-menu-bar ()
2292   (gnus-turn-off-edit-menu 'summary)
2293
2294   (unless (boundp 'gnus-summary-misc-menu)
2295
2296     (easy-menu-define
2297       gnus-summary-kill-menu gnus-summary-mode-map ""
2298       (cons
2299        "Score"
2300        (nconc
2301         (list
2302          ["Customize" gnus-score-customize t])
2303         (gnus-make-score-map 'increase)
2304         (gnus-make-score-map 'lower)
2305         '(("Mark"
2306            ["Kill below" gnus-summary-kill-below t]
2307            ["Mark above" gnus-summary-mark-above t]
2308            ["Tick above" gnus-summary-tick-above t]
2309            ["Clear above" gnus-summary-clear-above t])
2310           ["Current score" gnus-summary-current-score t]
2311           ["Set score" gnus-summary-set-score t]
2312           ["Switch current score file..." gnus-score-change-score-file t]
2313           ["Set mark below..." gnus-score-set-mark-below t]
2314           ["Set expunge below..." gnus-score-set-expunge-below t]
2315           ["Edit current score file" gnus-score-edit-current-scores t]
2316           ["Edit score file..." gnus-score-edit-file t]
2317           ["Trace score" gnus-score-find-trace t]
2318           ["Find words" gnus-score-find-favourite-words t]
2319           ["Rescore buffer" gnus-summary-rescore t]
2320           ["Increase score..." gnus-summary-increase-score t]
2321           ["Lower score..." gnus-summary-lower-score t]))))
2322
2323     ;; Define both the Article menu in the summary buffer and the
2324     ;; equivalent Commands menu in the article buffer here for
2325     ;; consistency.
2326     (let ((innards
2327            `(("Hide"
2328               ["All" gnus-article-hide t]
2329               ["Headers" gnus-article-hide-headers t]
2330               ["Signature" gnus-article-hide-signature t]
2331               ["Citation" gnus-article-hide-citation t]
2332               ["List identifiers" gnus-article-hide-list-identifiers t]
2333               ["Banner" gnus-article-strip-banner t]
2334               ["Boring headers" gnus-article-hide-boring-headers t])
2335              ("Highlight"
2336               ["All" gnus-article-highlight t]
2337               ["Headers" gnus-article-highlight-headers t]
2338               ["Signature" gnus-article-highlight-signature t]
2339               ["Citation" gnus-article-highlight-citation t])
2340              ("MIME"
2341               ["Words" gnus-article-decode-mime-words t]
2342               ["Charset" gnus-article-decode-charset t]
2343               ["QP" gnus-article-de-quoted-unreadable t]
2344               ["Base64" gnus-article-de-base64-unreadable t]
2345               ["View MIME buttons" gnus-summary-display-buttonized t]
2346               ["View all" gnus-mime-view-all-parts t]
2347               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2348               ["Encrypt body" gnus-article-encrypt-body
2349                :active (not (gnus-group-read-only-p))
2350                ,@(if (featurep 'xemacs) nil
2351                    '(:help "Encrypt the message body on disk"))]
2352               ["Extract all parts..." gnus-summary-save-parts t]
2353               ("Multipart"
2354                ["Repair multipart" gnus-summary-repair-multipart t]
2355                ["Pipe part..." gnus-article-pipe-part t]
2356                ["Inline part" gnus-article-inline-part t]
2357                ["View part as type..." gnus-article-view-part-as-type t]
2358                ["Encrypt body" gnus-article-encrypt-body
2359                 :active (not (gnus-group-read-only-p))
2360                ,@(if (featurep 'xemacs) nil
2361                    '(:help "Encrypt the message body on disk"))]
2362                ["View part externally" gnus-article-view-part-externally t]
2363                ["View HTML parts in browser" gnus-article-browse-html-article t]
2364                ["View part with charset..." gnus-article-view-part-as-charset t]
2365                ["Copy part" gnus-article-copy-part t]
2366                ["Save part..." gnus-article-save-part t]
2367                ["View part" gnus-article-view-part t]))
2368              ("Date"
2369               ["Local" gnus-article-date-local t]
2370               ["ISO8601" gnus-article-date-iso8601 t]
2371               ["UT" gnus-article-date-ut t]
2372               ["Original" gnus-article-date-original t]
2373               ["Lapsed" gnus-article-date-lapsed t]
2374               ["User-defined" gnus-article-date-user t])
2375              ("Display"
2376               ["Remove images" gnus-article-remove-images t]
2377               ["Toggle smiley" gnus-treat-smiley t]
2378               ["Show X-Face" gnus-article-display-x-face t]
2379               ["Show picons in From" gnus-treat-from-picon t]
2380               ["Show picons in mail headers" gnus-treat-mail-picon t]
2381               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2382               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2383               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2384               ("View as different encoding"
2385                ,@(gnus-summary-menu-split
2386                   (mapcar
2387                    (lambda (cs)
2388                      ;; Since easymenu under Emacs doesn't allow
2389                      ;; lambda forms for menu commands, we should
2390                      ;; provide intern'ed function symbols.
2391                      (let ((command (intern (format "\
2392 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2393                        (fset command
2394                              `(lambda ()
2395                                 (interactive)
2396                                 (let ((gnus-summary-show-article-charset-alist
2397                                        '((1 . ,cs))))
2398                                   (gnus-summary-show-article 1))))
2399                        `[,(symbol-name cs) ,command t]))
2400                    (sort (if (fboundp 'coding-system-list)
2401                              (coding-system-list)
2402                            (mapcar 'car mm-mime-mule-charset-alist))
2403                          'string<)))))
2404              ("Washing"
2405               ("Remove Blanks"
2406                ["Leading" gnus-article-strip-leading-blank-lines t]
2407                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2408                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2409                ["All of the above" gnus-article-strip-blank-lines t]
2410                ["All" gnus-article-strip-all-blank-lines t]
2411                ["Leading space" gnus-article-strip-leading-space t]
2412                ["Trailing space" gnus-article-strip-trailing-space t]
2413                ["Leading space in headers"
2414                 gnus-article-remove-leading-whitespace t])
2415               ["Overstrike" gnus-article-treat-overstrike t]
2416               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2417               ["Emphasis" gnus-article-emphasize t]
2418               ["Word wrap" gnus-article-fill-cited-article t]
2419               ["Fill long lines" gnus-article-fill-long-lines t]
2420               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2421               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2422               ["Remove CR" gnus-article-remove-cr t]
2423               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2424               ["Base64" gnus-article-de-base64-unreadable t]
2425               ["Rot 13" gnus-summary-caesar-message
2426                ,@(if (featurep 'xemacs) '(t)
2427                    '(:help "\"Caesar rotate\" article by 13"))]
2428               ["De-IDNA" gnus-summary-idna-message t]
2429               ["Morse decode" gnus-summary-morse-message t]
2430               ["Unix pipe..." gnus-summary-pipe-message t]
2431               ["Add buttons" gnus-article-add-buttons t]
2432               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2433               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2434               ["Verbose header" gnus-summary-verbose-headers t]
2435               ["Toggle header" gnus-summary-toggle-header t]
2436               ["Unfold headers" gnus-article-treat-unfold-headers t]
2437               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2438               ["Html" gnus-article-wash-html t]
2439               ["Unsplit URLs" gnus-article-unsplit-urls t]
2440               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2441               ["Decode HZ" gnus-article-decode-HZ t]
2442               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2443               ("(Outlook) Deuglify"
2444                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2445                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2446                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2447                ["Full (Outlook) deuglify"
2448                 gnus-article-outlook-deuglify-article t])
2449               )
2450              ("Output"
2451               ["Save in default format..." gnus-summary-save-article
2452                ,@(if (featurep 'xemacs) '(t)
2453                    '(:help "Save article using default method"))]
2454               ["Save in file..." gnus-summary-save-article-file
2455                ,@(if (featurep 'xemacs) '(t)
2456                    '(:help "Save article in file"))]
2457               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2458               ["Save in MH folder..." gnus-summary-save-article-folder t]
2459               ["Save in VM folder..." gnus-summary-save-article-vm t]
2460               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2461               ["Save body in file..." gnus-summary-save-article-body-file t]
2462               ["Pipe through a filter..." gnus-summary-pipe-output t]
2463               ["Print with Muttprint..." gnus-summary-muttprint t]
2464               ["Print" gnus-summary-print-article
2465                ,@(if (featurep 'xemacs) '(t)
2466                    '(:help "Generate and print a PostScript image"))])
2467              ("Copy, move,... (Backend)"
2468               ,@(if (featurep 'xemacs) nil
2469                   '(:help "Copying, moving, expiring articles..."))
2470               ["Respool article..." gnus-summary-respool-article t]
2471               ["Move article..." gnus-summary-move-article
2472                (gnus-check-backend-function
2473                 'request-move-article gnus-newsgroup-name)]
2474               ["Copy article..." gnus-summary-copy-article t]
2475               ["Crosspost article..." gnus-summary-crosspost-article
2476                (gnus-check-backend-function
2477                 'request-replace-article gnus-newsgroup-name)]
2478               ["Import file..." gnus-summary-import-article
2479                (gnus-check-backend-function
2480                 'request-accept-article gnus-newsgroup-name)]
2481               ["Create article..." gnus-summary-create-article
2482                (gnus-check-backend-function
2483                 'request-accept-article gnus-newsgroup-name)]
2484               ["Check if posted" gnus-summary-article-posted-p t]
2485               ["Edit article" gnus-summary-edit-article
2486                (not (gnus-group-read-only-p))]
2487               ["Delete article" gnus-summary-delete-article
2488                (gnus-check-backend-function
2489                 'request-expire-articles gnus-newsgroup-name)]
2490               ["Query respool" gnus-summary-respool-query t]
2491               ["Trace respool" gnus-summary-respool-trace t]
2492               ["Delete expirable articles" gnus-summary-expire-articles-now
2493                (gnus-check-backend-function
2494                 'request-expire-articles gnus-newsgroup-name)])
2495              ("Extract"
2496               ["Uudecode" gnus-uu-decode-uu
2497                ,@(if (featurep 'xemacs) '(t)
2498                    '(:help "Decode uuencoded article(s)"))]
2499               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2500               ["Unshar" gnus-uu-decode-unshar t]
2501               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2502               ["Save" gnus-uu-decode-save t]
2503               ["Binhex" gnus-uu-decode-binhex t]
2504               ["Postscript" gnus-uu-decode-postscript t]
2505               ["All MIME parts" gnus-summary-save-parts t])
2506              ("Cache"
2507               ["Enter article" gnus-cache-enter-article t]
2508               ["Remove article" gnus-cache-remove-article t])
2509              ["Translate" gnus-article-babel t]
2510              ["Select article buffer" gnus-summary-select-article-buffer t]
2511              ["Make article buffer sticky" gnus-sticky-article t]
2512              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2513              ["Isearch article..." gnus-summary-isearch-article t]
2514              ["Beginning of the article" gnus-summary-beginning-of-article t]
2515              ["End of the article" gnus-summary-end-of-article t]
2516              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2517              ["Fetch referenced articles" gnus-summary-refer-references t]
2518              ["Fetch current thread" gnus-summary-refer-thread t]
2519              ["Fetch article with id..." gnus-summary-refer-article t]
2520              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2521              ["Redisplay" gnus-summary-show-article t]
2522              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2523       (easy-menu-define
2524         gnus-summary-article-menu gnus-summary-mode-map ""
2525         (cons "Article" innards))
2526
2527       (if (not (keymapp gnus-summary-article-menu))
2528           (easy-menu-define
2529             gnus-article-commands-menu gnus-article-mode-map ""
2530             (cons "Commands" innards))
2531         ;; in Emacs, don't share menu.
2532         (setq gnus-article-commands-menu
2533               (copy-keymap gnus-summary-article-menu))
2534         (define-key gnus-article-mode-map [menu-bar commands]
2535           (cons "Commands" gnus-article-commands-menu))))
2536
2537     (easy-menu-define
2538       gnus-summary-thread-menu gnus-summary-mode-map ""
2539       '("Threads"
2540         ["Find all messages in thread" gnus-summary-refer-thread t]
2541         ["Toggle threading" gnus-summary-toggle-threads t]
2542         ["Hide threads" gnus-summary-hide-all-threads t]
2543         ["Show threads" gnus-summary-show-all-threads t]
2544         ["Hide thread" gnus-summary-hide-thread t]
2545         ["Show thread" gnus-summary-show-thread t]
2546         ["Go to next thread" gnus-summary-next-thread t]
2547         ["Go to previous thread" gnus-summary-prev-thread t]
2548         ["Go down thread" gnus-summary-down-thread t]
2549         ["Go up thread" gnus-summary-up-thread t]
2550         ["Top of thread" gnus-summary-top-thread t]
2551         ["Mark thread as read" gnus-summary-kill-thread t]
2552         ["Mark thread as expired" gnus-summary-expire-thread t]
2553         ["Lower thread score" gnus-summary-lower-thread t]
2554         ["Raise thread score" gnus-summary-raise-thread t]
2555         ["Rethread current" gnus-summary-rethread-current t]))
2556
2557     (easy-menu-define
2558       gnus-summary-post-menu gnus-summary-mode-map ""
2559       `("Post"
2560         ["Send a message (mail or news)" gnus-summary-post-news
2561          ,@(if (featurep 'xemacs) '(t)
2562              '(:help "Compose a new message (mail or news)"))]
2563         ["Followup" gnus-summary-followup
2564          ,@(if (featurep 'xemacs) '(t)
2565              '(:help "Post followup to this article"))]
2566         ["Followup and yank" gnus-summary-followup-with-original
2567          ,@(if (featurep 'xemacs) '(t)
2568              '(:help "Post followup to this article, quoting its contents"))]
2569         ["Supersede article" gnus-summary-supersede-article t]
2570         ["Cancel article" gnus-summary-cancel-article
2571          ,@(if (featurep 'xemacs) '(t)
2572              '(:help "Cancel an article you posted"))]
2573         ["Reply" gnus-summary-reply t]
2574         ["Reply and yank" gnus-summary-reply-with-original t]
2575         ["Wide reply" gnus-summary-wide-reply t]
2576         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2577          ,@(if (featurep 'xemacs) '(t)
2578              '(:help "Mail a reply, quoting this article"))]
2579         ["Very wide reply" gnus-summary-very-wide-reply t]
2580         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2581          ,@(if (featurep 'xemacs) '(t)
2582              '(:help "Mail a very wide reply, quoting this article"))]
2583         ["Mail forward" gnus-summary-mail-forward t]
2584         ["Post forward" gnus-summary-post-forward t]
2585         ["Digest and mail" gnus-uu-digest-mail-forward t]
2586         ["Digest and post" gnus-uu-digest-post-forward t]
2587         ["Resend message" gnus-summary-resend-message t]
2588         ["Resend message edit" gnus-summary-resend-message-edit t]
2589         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2590         ["Send a mail" gnus-summary-mail-other-window t]
2591         ["Create a local message" gnus-summary-news-other-window t]
2592         ["Uuencode and post" gnus-uu-post-news
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Post a uuencoded article"))]
2595         ["Followup via news" gnus-summary-followup-to-mail t]
2596         ["Followup via news and yank"
2597          gnus-summary-followup-to-mail-with-original t]
2598         ["Strip signature on reply"
2599          (lambda ()
2600            (interactive)
2601            (if (not (memq message-cite-function
2602                           '(message-cite-original-without-signature
2603                             message-cite-original)))
2604                ;; Stupid workaround for XEmacs not honoring :visible.
2605                (message "Can't toggle this value of `message-cite-function'")
2606              (setq message-cite-function
2607                    (if (eq message-cite-function
2608                            'message-cite-original-without-signature)
2609                        'message-cite-original
2610                      'message-cite-original-without-signature))))
2611          ;; XEmacs barfs on :visible.
2612          ,@(if (featurep 'xemacs) nil
2613              '(:visible (memq message-cite-function
2614                               '(message-cite-original-without-signature
2615                                 message-cite-original))))
2616          :style toggle
2617          :selected (eq message-cite-function
2618                        'message-cite-original-without-signature)
2619          ,@(if (featurep 'xemacs) nil
2620              '(:help "Strip signature from cited article when replying."))]
2621         ;;("Draft"
2622         ;;["Send" gnus-summary-send-draft t]
2623         ;;["Send bounced" gnus-resend-bounced-mail t])
2624         ))
2625
2626     (cond
2627      ((not (keymapp gnus-summary-post-menu))
2628       (setq gnus-article-post-menu gnus-summary-post-menu))
2629      ((not gnus-article-post-menu)
2630       ;; Don't share post menu.
2631       (setq gnus-article-post-menu
2632             (copy-keymap gnus-summary-post-menu))))
2633     (define-key gnus-article-mode-map [menu-bar post]
2634       (cons "Post" gnus-article-post-menu))
2635
2636     (easy-menu-define
2637       gnus-summary-misc-menu gnus-summary-mode-map ""
2638       `("Gnus"
2639         ("Mark Read"
2640          ["Mark as read" gnus-summary-mark-as-read-forward t]
2641          ["Mark same subject and select"
2642           gnus-summary-kill-same-subject-and-select t]
2643          ["Mark same subject" gnus-summary-kill-same-subject t]
2644          ["Catchup" gnus-summary-catchup
2645           ,@(if (featurep 'xemacs) '(t)
2646               '(:help "Mark unread articles in this group as read"))]
2647          ["Catchup all" gnus-summary-catchup-all t]
2648          ["Catchup to here" gnus-summary-catchup-to-here t]
2649          ["Catchup from here" gnus-summary-catchup-from-here t]
2650          ["Catchup region" gnus-summary-mark-region-as-read
2651           (gnus-mark-active-p)]
2652          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2653         ("Mark Various"
2654          ["Tick" gnus-summary-tick-article-forward t]
2655          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2656          ["Remove marks" gnus-summary-clear-mark-forward t]
2657          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2658          ["Set bookmark" gnus-summary-set-bookmark t]
2659          ["Remove bookmark" gnus-summary-remove-bookmark t])
2660         ("Limit to"
2661          ["Marks..." gnus-summary-limit-to-marks t]
2662          ["Subject..." gnus-summary-limit-to-subject t]
2663          ["Author..." gnus-summary-limit-to-author t]
2664          ["Recipient..." gnus-summary-limit-to-recipient t]
2665          ["Address..." gnus-summary-limit-to-address t]
2666          ["Age..." gnus-summary-limit-to-age t]
2667          ["Extra..." gnus-summary-limit-to-extra t]
2668          ["Score..." gnus-summary-limit-to-score t]
2669          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2670          ["Unread" gnus-summary-limit-to-unread t]
2671          ["Unseen" gnus-summary-limit-to-unseen t]
2672          ["Singletons" gnus-summary-limit-to-singletons t]
2673          ["Replied" gnus-summary-limit-to-replied t]
2674          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2675          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2676          ["Pop limit" gnus-summary-pop-limit t]
2677          ["Show dormant" gnus-summary-limit-include-dormant t]
2678          ["Hide childless dormant"
2679           gnus-summary-limit-exclude-childless-dormant t]
2680          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2681          ["Hide marked" gnus-summary-limit-exclude-marks t]
2682          ["Show expunged" gnus-summary-limit-include-expunged t])
2683         ("Process Mark"
2684          ["Set mark" gnus-summary-mark-as-processable t]
2685          ["Remove mark" gnus-summary-unmark-as-processable t]
2686          ["Remove all marks" gnus-summary-unmark-all-processable t]
2687          ["Invert marks" gnus-uu-invert-processable t]
2688          ["Mark above" gnus-uu-mark-over t]
2689          ["Mark series" gnus-uu-mark-series t]
2690          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2691          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2692          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2693          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2694          ["Mark all" gnus-uu-mark-all t]
2695          ["Mark buffer" gnus-uu-mark-buffer t]
2696          ["Mark sparse" gnus-uu-mark-sparse t]
2697          ["Mark thread" gnus-uu-mark-thread t]
2698          ["Unmark thread" gnus-uu-unmark-thread t]
2699          ("Process Mark Sets"
2700           ["Kill" gnus-summary-kill-process-mark t]
2701           ["Yank" gnus-summary-yank-process-mark
2702            gnus-newsgroup-process-stack]
2703           ["Save" gnus-summary-save-process-mark t]
2704           ["Run command on marked..." gnus-summary-universal-argument t]))
2705         ("Registry Marks")
2706         ("Scroll article"
2707          ["Page forward" gnus-summary-next-page
2708           ,@(if (featurep 'xemacs) '(t)
2709               '(:help "Show next page of article"))]
2710          ["Page backward" gnus-summary-prev-page
2711           ,@(if (featurep 'xemacs) '(t)
2712               '(:help "Show previous page of article"))]
2713          ["Line forward" gnus-summary-scroll-up t])
2714         ("Move"
2715          ["Next unread article" gnus-summary-next-unread-article t]
2716          ["Previous unread article" gnus-summary-prev-unread-article t]
2717          ["Next article" gnus-summary-next-article t]
2718          ["Previous article" gnus-summary-prev-article t]
2719          ["Next unread subject" gnus-summary-next-unread-subject t]
2720          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2721          ["Next article same subject" gnus-summary-next-same-subject t]
2722          ["Previous article same subject" gnus-summary-prev-same-subject t]
2723          ["First unread article" gnus-summary-first-unread-article t]
2724          ["Best unread article" gnus-summary-best-unread-article t]
2725          ["Go to subject number..." gnus-summary-goto-subject t]
2726          ["Go to article number..." gnus-summary-goto-article t]
2727          ["Go to the last article" gnus-summary-goto-last-article t]
2728          ["Pop article off history" gnus-summary-pop-article t])
2729         ("Sort"
2730          ["Sort by number" gnus-summary-sort-by-number t]
2731          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2732          ["Sort by author" gnus-summary-sort-by-author t]
2733          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2734          ["Sort by subject" gnus-summary-sort-by-subject t]
2735          ["Sort by date" gnus-summary-sort-by-date t]
2736          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2737          ["Sort by score" gnus-summary-sort-by-score t]
2738          ["Sort by lines" gnus-summary-sort-by-lines t]
2739          ["Sort by characters" gnus-summary-sort-by-chars t]
2740          ["Randomize" gnus-summary-sort-by-random t]
2741          ["Original sort" gnus-summary-sort-by-original t])
2742         ("Help"
2743          ["Describe group" gnus-summary-describe-group t]
2744          ["Fetch control message" gnus-group-fetch-control
2745           ,@(if (featurep 'xemacs) nil
2746               '(:help "Display the archived control message for the current group"))]
2747          ["Read manual" gnus-info-find-node t])
2748         ("Modes"
2749          ["Pick and read" gnus-pick-mode t]
2750          ["Binary" gnus-binary-mode t])
2751         ("Regeneration"
2752          ["Regenerate" gnus-summary-prepare t]
2753          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2754          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2755          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2756          ["Toggle threading" gnus-summary-toggle-threads t])
2757         ["See old articles" gnus-summary-insert-old-articles t]
2758         ["See new articles" gnus-summary-insert-new-articles t]
2759         ["Filter articles..." gnus-summary-execute-command t]
2760         ["Run command on articles..." gnus-summary-universal-argument t]
2761         ["Search articles forward..." gnus-summary-search-article-forward t]
2762         ["Search articles backward..." gnus-summary-search-article-backward t]
2763         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2764         ["Expand window" gnus-summary-expand-window t]
2765         ["Expire expirable articles" gnus-summary-expire-articles
2766          (gnus-check-backend-function
2767           'request-expire-articles gnus-newsgroup-name)]
2768         ["Edit local kill file" gnus-summary-edit-local-kill t]
2769         ["Edit main kill file" gnus-summary-edit-global-kill t]
2770         ["Edit group parameters" gnus-summary-edit-parameters t]
2771         ["Customize group parameters" gnus-summary-customize-parameters t]
2772         ["Send a bug report" gnus-bug t]
2773         ("Exit"
2774          ["Catchup and exit" gnus-summary-catchup-and-exit
2775           ,@(if (featurep 'xemacs) '(t)
2776               '(:help "Mark unread articles in this group as read, then exit"))]
2777          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2778          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2779          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2780          ["Exit group" gnus-summary-exit
2781           ,@(if (featurep 'xemacs) '(t)
2782               '(:help "Exit current group, return to group selection mode"))]
2783          ["Exit group without updating" gnus-summary-exit-no-update t]
2784          ["Exit and goto next group" gnus-summary-next-group t]
2785          ["Exit and goto prev group" gnus-summary-prev-group t]
2786          ["Reselect group" gnus-summary-reselect-current-group t]
2787          ["Rescan group" gnus-summary-rescan-group t]
2788          ["Update dribble" gnus-summary-save-newsrc t])))
2789
2790     (gnus-run-hooks 'gnus-summary-menu-hook)))
2791
2792 (defvar gnus-summary-tool-bar-map nil)
2793
2794 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2795 ;; affect _new_ message buffers.  We might add a function that walks thru all
2796 ;; summary-mode buffers and force the update.
2797 (defun gnus-summary-tool-bar-update (&optional symbol value)
2798   "Update summary mode toolbar.
2799 Setter function for custom variables."
2800   (setq-default gnus-summary-tool-bar-map nil)
2801   (when symbol
2802     ;; When used as ":set" function:
2803     (set-default symbol value))
2804   (when (gnus-buffer-live-p gnus-summary-buffer)
2805     (with-current-buffer gnus-summary-buffer
2806       (gnus-summary-make-tool-bar))))
2807
2808 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2809                                      'gnus-summary-tool-bar-gnome
2810                                    'gnus-summary-tool-bar-retro)
2811   "Specifies the Gnus summary tool bar.
2812
2813 It can be either a list or a symbol refering to a list.  See
2814 `gmm-tool-bar-from-list' for the format of the list.  The
2815 default key map is `gnus-summary-mode-map'.
2816
2817 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2818 `gnus-summary-tool-bar-retro'."
2819   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2820                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2821                  (repeat :tag "User defined list" gmm-tool-bar-item)
2822                  (symbol))
2823   :version "23.1" ;; No Gnus
2824   :initialize 'custom-initialize-default
2825   :set 'gnus-summary-tool-bar-update
2826   :group 'gnus-summary)
2827
2828 (defcustom gnus-summary-tool-bar-gnome
2829   '((gnus-summary-post-news "mail/compose" nil)
2830     (gnus-summary-insert-new-articles "mail/inbox" nil
2831                                       :visible (or (not gnus-agent)
2832                                                    gnus-plugged))
2833     (gnus-summary-reply-with-original "mail/reply")
2834     (gnus-summary-reply "mail/reply" nil :visible nil)
2835     (gnus-summary-followup-with-original "mail/reply-all")
2836     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2837     (gnus-summary-mail-forward "mail/forward")
2838     (gnus-summary-save-article "mail/save")
2839     (gnus-summary-search-article-forward "search" nil :visible nil)
2840     (gnus-summary-print-article "print")
2841     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2842     ;; Some new commands that may need more suitable icons:
2843     (gnus-summary-save-newsrc "save" nil :visible nil)
2844     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2845     (gnus-summary-prev-article "left-arrow")
2846     (gnus-summary-next-article "right-arrow")
2847     (gnus-summary-next-page "next-page")
2848     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2849     ;;
2850     ;; Maybe some sort-by-... could be added:
2851     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2852     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2853     (gnus-summary-mark-as-expirable
2854      "delete" nil
2855      :visible (gnus-check-backend-function 'request-expire-articles
2856                                            gnus-newsgroup-name))
2857     (gnus-summary-mark-as-spam
2858      "mail/spam" t
2859      :visible (and (fboundp 'spam-group-ham-contents-p)
2860                    (spam-group-ham-contents-p gnus-newsgroup-name))
2861      :help "Mark as spam")
2862     (gnus-summary-mark-as-read-forward
2863      "mail/not-spam" nil
2864      :visible (and (fboundp 'spam-group-spam-contents-p)
2865                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2866     ;;
2867     (gnus-summary-exit "exit")
2868     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2869     (gnus-info-find-node "help"))
2870   "List of functions for the summary tool bar (GNOME style).
2871
2872 See `gmm-tool-bar-from-list' for the format of the list."
2873   :type '(repeat gmm-tool-bar-item)
2874   :version "23.1" ;; No Gnus
2875   :initialize 'custom-initialize-default
2876   :set 'gnus-summary-tool-bar-update
2877   :group 'gnus-summary)
2878
2879 (defcustom gnus-summary-tool-bar-retro
2880   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2881     (gnus-summary-next-unread-article "gnus/next-ur")
2882     (gnus-summary-post-news "gnus/post")
2883     (gnus-summary-followup-with-original "gnus/fuwo")
2884     (gnus-summary-followup "gnus/followup")
2885     (gnus-summary-reply-with-original "gnus/reply-wo")
2886     (gnus-summary-reply "gnus/reply")
2887     (gnus-summary-caesar-message "gnus/rot13")
2888     (gnus-uu-decode-uu "gnus/uu-decode")
2889     (gnus-summary-save-article-file "gnus/save-aif")
2890     (gnus-summary-save-article "gnus/save-art")
2891     (gnus-uu-post-news "gnus/uu-post")
2892     (gnus-summary-catchup "gnus/catchup")
2893     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2894     (gnus-summary-exit "gnus/exit-summ")
2895     ;; Some new command that may need more suitable icons:
2896     (gnus-summary-print-article "gnus/print" nil :visible nil)
2897     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2898     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2899     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2900     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2901     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2902     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2903     ;;
2904     (gnus-info-find-node "gnus/help" nil :visible nil))
2905   "List of functions for the summary tool bar (retro look).
2906
2907 See `gmm-tool-bar-from-list' for the format of the list."
2908   :type '(repeat gmm-tool-bar-item)
2909   :version "23.1" ;; No Gnus
2910   :initialize 'custom-initialize-default
2911   :set 'gnus-summary-tool-bar-update
2912   :group 'gnus-summary)
2913
2914 (defcustom gnus-summary-tool-bar-zap-list t
2915   "List of icon items from the global tool bar.
2916 These items are not displayed in the Gnus summary mode tool bar.
2917
2918 See `gmm-tool-bar-from-list' for the format of the list."
2919   :type 'gmm-tool-bar-zap-list
2920   :version "23.1" ;; No Gnus
2921   :initialize 'custom-initialize-default
2922   :set 'gnus-summary-tool-bar-update
2923   :group 'gnus-summary)
2924
2925 (defvar image-load-path)
2926 (defvar tool-bar-map)
2927
2928 (defun gnus-summary-make-tool-bar (&optional force)
2929   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2930 When FORCE, rebuild the tool bar."
2931   (when (and (not (featurep 'xemacs))
2932              (boundp 'tool-bar-mode)
2933              tool-bar-mode
2934              (or (not gnus-summary-tool-bar-map) force))
2935     (let* ((load-path
2936             (gmm-image-load-path-for-library "gnus"
2937                                              "mail/save.xpm"
2938                                              nil t))
2939            (image-load-path (cons (car load-path)
2940                                   (when (boundp 'image-load-path)
2941                                     image-load-path)))
2942            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2943                                         gnus-summary-tool-bar-zap-list
2944                                         'gnus-summary-mode-map)))
2945       (when map
2946         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2947         ;; uses it's value.
2948         (setq gnus-summary-tool-bar-map map))))
2949   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2950
2951 (defun gnus-score-set-default (var value)
2952   "A version of set that updates the GNU Emacs menu-bar."
2953   (set var value)
2954   ;; It is the message that forces the active status to be updated.
2955   (message ""))
2956
2957 (defun gnus-make-score-map (type)
2958   "Make a summary score map of type TYPE."
2959   (if t
2960       nil
2961     (let ((headers '(("author" "from" string)
2962                      ("subject" "subject" string)
2963                      ("article body" "body" string)
2964                      ("article head" "head" string)
2965                      ("xref" "xref" string)
2966                      ("extra header" "extra" string)
2967                      ("lines" "lines" number)
2968                      ("followups to author" "followup" string)))
2969           (types '((number ("less than" <)
2970                            ("greater than" >)
2971                            ("equal" =))
2972                    (string ("substring" s)
2973                            ("exact string" e)
2974                            ("fuzzy string" f)
2975                            ("regexp" r))))
2976           (perms '(("temporary" (current-time-string))
2977                    ("permanent" nil)
2978                    ("immediate" now)))
2979           header)
2980       (list
2981        (apply
2982         'nconc
2983         (list
2984          (if (eq type 'lower)
2985              "Lower score"
2986            "Increase score"))
2987         (let (outh)
2988           (while headers
2989             (setq header (car headers))
2990             (setq outh
2991                   (cons
2992                    (apply
2993                     'nconc
2994                     (list (car header))
2995                     (let ((ts (cdr (assoc (nth 2 header) types)))
2996                           outt)
2997                       (while ts
2998                         (setq outt
2999                               (cons
3000                                (apply
3001                                 'nconc
3002                                 (list (caar ts))
3003                                 (let ((ps perms)
3004                                       outp)
3005                                   (while ps
3006                                     (setq outp
3007                                           (cons
3008                                            (vector
3009                                             (caar ps)
3010                                             (list
3011                                              'gnus-summary-score-entry
3012                                              (nth 1 header)
3013                                              (if (or (string= (nth 1 header)
3014                                                               "head")
3015                                                      (string= (nth 1 header)
3016                                                               "body"))
3017                                                  ""
3018                                                (list 'gnus-summary-header
3019                                                      (nth 1 header)))
3020                                              (list 'quote (nth 1 (car ts)))
3021                                              (list 'gnus-score-delta-default
3022                                                    nil)
3023                                              (nth 1 (car ps))
3024                                              t)
3025                                             t)
3026                                            outp))
3027                                     (setq ps (cdr ps)))
3028                                   (list (nreverse outp))))
3029                                outt))
3030                         (setq ts (cdr ts)))
3031                       (list (nreverse outt))))
3032                    outh))
3033             (setq headers (cdr headers)))
3034           (list (nreverse outh))))))))
3035
3036
3037 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3038 (defvar bookmark-make-record-function)
3039 \f
3040
3041 (defun gnus-summary-mode (&optional group)
3042   "Major mode for reading articles.
3043
3044 All normal editing commands are switched off.
3045 \\<gnus-summary-mode-map>
3046 Each line in this buffer represents one article.  To read an
3047 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3048 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3049 respectively.
3050
3051 You can also post articles and send mail from this buffer.  To
3052 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3053 of an article, type `\\[gnus-summary-reply]'.
3054
3055 There are approx. one gazillion commands you can execute in this
3056 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3057
3058 The following commands are available:
3059
3060 \\{gnus-summary-mode-map}"
3061   (interactive)
3062   (kill-all-local-variables)
3063   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3064     (gnus-summary-make-local-variables))
3065   (gnus-summary-make-local-variables)
3066   (setq gnus-newsgroup-name group)
3067   (when (gnus-visual-p 'summary-menu 'menu)
3068     (gnus-summary-make-menu-bar)
3069     (gnus-summary-make-tool-bar))
3070   (gnus-make-thread-indent-array)
3071   (gnus-simplify-mode-line)
3072   (setq major-mode 'gnus-summary-mode)
3073   (setq mode-name "Summary")
3074   (use-local-map gnus-summary-mode-map)
3075   (buffer-disable-undo)
3076   (setq buffer-read-only t              ;Disable modification
3077         show-trailing-whitespace nil)
3078   (setq truncate-lines t)
3079   (add-to-invisibility-spec '(gnus-sum . t))
3080   (gnus-summary-set-display-table)
3081   (gnus-set-default-directory)
3082   (make-local-variable 'gnus-summary-line-format)
3083   (make-local-variable 'gnus-summary-line-format-spec)
3084   (make-local-variable 'gnus-summary-dummy-line-format)
3085   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3086   (make-local-variable 'gnus-summary-mark-positions)
3087   (gnus-make-local-hook 'pre-command-hook)
3088   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3089   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3090   (turn-on-gnus-mailing-list-mode)
3091   (mm-enable-multibyte)
3092   (set (make-local-variable 'bookmark-make-record-function)
3093        'gnus-summary-bookmark-make-record)
3094   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3095   (gnus-update-summary-mark-positions))
3096
3097 (defun gnus-summary-make-local-variables ()
3098   "Make all the local summary buffer variables."
3099   (let (global)
3100     (dolist (local gnus-summary-local-variables)
3101       (if (consp local)
3102           (progn
3103             (if (eq (cdr local) 'global)
3104                 ;; Copy the global value of the variable.
3105                 (setq global (symbol-value (car local)))
3106               ;; Use the value from the list.
3107               (setq global (eval (cdr local))))
3108             (set (make-local-variable (car local)) global))
3109         ;; Simple nil-valued local variable.
3110         (set (make-local-variable local) nil)))))
3111
3112 ;; Summary data functions.
3113
3114 (defmacro gnus-data-number (data)
3115   `(car ,data))
3116
3117 (defmacro gnus-data-set-number (data number)
3118   `(setcar ,data ,number))
3119
3120 (defmacro gnus-data-mark (data)
3121   `(nth 1 ,data))
3122
3123 (defmacro gnus-data-set-mark (data mark)
3124   `(setcar (nthcdr 1 ,data) ,mark))
3125
3126 (defmacro gnus-data-pos (data)
3127   `(nth 2 ,data))
3128
3129 (defmacro gnus-data-set-pos (data pos)
3130   `(setcar (nthcdr 2 ,data) ,pos))
3131
3132 (defmacro gnus-data-header (data)
3133   `(nth 3 ,data))
3134
3135 (defmacro gnus-data-set-header (data header)
3136   `(setf (nth 3 ,data) ,header))
3137
3138 (defmacro gnus-data-level (data)
3139   `(nth 4 ,data))
3140
3141 (defmacro gnus-data-unread-p (data)
3142   `(= (nth 1 ,data) gnus-unread-mark))
3143
3144 (defmacro gnus-data-read-p (data)
3145   `(/= (nth 1 ,data) gnus-unread-mark))
3146
3147 (defmacro gnus-data-pseudo-p (data)
3148   `(consp (nth 3 ,data)))
3149
3150 (defmacro gnus-data-find (number)
3151   `(assq ,number gnus-newsgroup-data))
3152
3153 (defmacro gnus-data-find-list (number &optional data)
3154   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3155      (memq (assq ,number bdata)
3156            bdata)))
3157
3158 (defmacro gnus-data-make (number mark pos header level)
3159   `(list ,number ,mark ,pos ,header ,level))
3160
3161 (defun gnus-data-enter (after-article number mark pos header level offset)
3162   (let ((data (gnus-data-find-list after-article)))
3163     (unless data
3164       (error "No such article: %d" after-article))
3165     (setcdr data (cons (gnus-data-make number mark pos header level)
3166                        (cdr data)))
3167     (setq gnus-newsgroup-data-reverse nil)
3168     (gnus-data-update-list (cddr data) offset)))
3169
3170 (defun gnus-data-enter-list (after-article list &optional offset)
3171   (when list
3172     (let ((data (and after-article (gnus-data-find-list after-article)))
3173           (ilist list))
3174       (if (not (or data
3175                    after-article))
3176           (let ((odata gnus-newsgroup-data))
3177             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3178             (when offset
3179               (gnus-data-update-list odata offset)))
3180         ;; Find the last element in the list to be spliced into the main
3181         ;; list.
3182         (setq list (last list))
3183         (if (not data)
3184             (progn
3185               (setcdr list gnus-newsgroup-data)
3186               (setq gnus-newsgroup-data ilist)
3187               (when offset
3188                 (gnus-data-update-list (cdr list) offset)))
3189           (setcdr list (cdr data))
3190           (setcdr data ilist)
3191           (when offset
3192             (gnus-data-update-list (cdr list) offset))))
3193       (setq gnus-newsgroup-data-reverse nil))))
3194
3195 (defun gnus-data-remove (article &optional offset)
3196   (let ((data gnus-newsgroup-data))
3197     (if (= (gnus-data-number (car data)) article)
3198         (progn
3199           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3200                 gnus-newsgroup-data-reverse nil)
3201           (when offset
3202             (gnus-data-update-list gnus-newsgroup-data offset)))
3203       (while (cdr data)
3204         (when (= (gnus-data-number (cadr data)) article)
3205           (setcdr data (cddr data))
3206           (when offset
3207             (gnus-data-update-list (cdr data) offset))
3208           (setq data nil
3209                 gnus-newsgroup-data-reverse nil))
3210         (setq data (cdr data))))))
3211
3212 (defmacro gnus-data-list (backward)
3213   `(if ,backward
3214        (or gnus-newsgroup-data-reverse
3215            (setq gnus-newsgroup-data-reverse
3216                  (reverse gnus-newsgroup-data)))
3217      gnus-newsgroup-data))
3218
3219 (defun gnus-data-update-list (data offset)
3220   "Add OFFSET to the POS of all data entries in DATA."
3221   (setq gnus-newsgroup-data-reverse nil)
3222   (while data
3223     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3224     (setq data (cdr data))))
3225
3226 (defun gnus-summary-article-pseudo-p (article)
3227   "Say whether this article is a pseudo article or not."
3228   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3229
3230 (defmacro gnus-summary-article-sparse-p (article)
3231   "Say whether this article is a sparse article or not."
3232   `(memq ,article gnus-newsgroup-sparse))
3233
3234 (defmacro gnus-summary-article-ancient-p (article)
3235   "Say whether this article is a sparse article or not."
3236   `(memq ,article gnus-newsgroup-ancient))
3237
3238 (defun gnus-article-parent-p (number)
3239   "Say whether this article is a parent or not."
3240   (let ((data (gnus-data-find-list number)))
3241     (and (cdr data)              ; There has to be an article after...
3242          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3243             (gnus-data-level (nth 1 data))))))
3244
3245 (defun gnus-article-children (number)
3246   "Return a list of all children to NUMBER."
3247   (let* ((data (gnus-data-find-list number))
3248          (level (gnus-data-level (car data)))
3249          children)
3250     (setq data (cdr data))
3251     (while (and data
3252                 (= (gnus-data-level (car data)) (1+ level)))
3253       (push (gnus-data-number (car data)) children)
3254       (setq data (cdr data)))
3255     children))
3256
3257 (defmacro gnus-summary-skip-intangible ()
3258   "If the current article is intangible, then jump to a different article."
3259   '(let ((to (get-text-property (point) 'gnus-intangible)))
3260      (and to (gnus-summary-goto-subject to))))
3261
3262 (defmacro gnus-summary-article-intangible-p ()
3263   "Say whether this article is intangible or not."
3264   '(get-text-property (point) 'gnus-intangible))
3265
3266 (defun gnus-article-read-p (article)
3267   "Say whether ARTICLE is read or not."
3268   (not (or (memq article gnus-newsgroup-marked)
3269            (memq article gnus-newsgroup-spam-marked)
3270            (memq article gnus-newsgroup-unreads)
3271            (memq article gnus-newsgroup-unselected)
3272            (memq article gnus-newsgroup-dormant))))
3273
3274 ;; Some summary mode macros.
3275
3276 (defmacro gnus-summary-article-number ()
3277   "The article number of the article on the current line.
3278 If there isn't an article number here, then we return the current
3279 article number."
3280   '(progn
3281      (gnus-summary-skip-intangible)
3282      (or (get-text-property (point) 'gnus-number)
3283          (gnus-summary-last-subject))))
3284
3285 (defmacro gnus-summary-article-header (&optional number)
3286   "Return the header of article NUMBER."
3287   `(gnus-data-header (gnus-data-find
3288                       ,(or number '(gnus-summary-article-number)))))
3289
3290 (defmacro gnus-summary-thread-level (&optional number)
3291   "Return the level of thread that starts with article NUMBER."
3292   `(if (and (eq gnus-summary-make-false-root 'dummy)
3293             (get-text-property (point) 'gnus-intangible))
3294        0
3295      (gnus-data-level (gnus-data-find
3296                        ,(or number '(gnus-summary-article-number))))))
3297
3298 (defmacro gnus-summary-article-mark (&optional number)
3299   "Return the mark of article NUMBER."
3300   `(gnus-data-mark (gnus-data-find
3301                     ,(or number '(gnus-summary-article-number)))))
3302
3303 (defmacro gnus-summary-article-pos (&optional number)
3304   "Return the position of the line of article NUMBER."
3305   `(gnus-data-pos (gnus-data-find
3306                    ,(or number '(gnus-summary-article-number)))))
3307
3308 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3309 (defmacro gnus-summary-article-subject (&optional number)
3310   "Return current subject string or nil if nothing."
3311   `(let ((headers
3312           ,(if number
3313                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3314              '(gnus-data-header (assq (gnus-summary-article-number)
3315                                       gnus-newsgroup-data)))))
3316      (and headers
3317           (vectorp headers)
3318           (mail-header-subject headers))))
3319
3320 (defmacro gnus-summary-article-score (&optional number)
3321   "Return current article score."
3322   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3323                   gnus-newsgroup-scored))
3324        gnus-summary-default-score 0))
3325
3326 (defun gnus-summary-article-children (&optional number)
3327   "Return a list of article numbers that are children of article NUMBER."
3328   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3329          (level (gnus-data-level (car data)))
3330          l children)
3331     (while (and (setq data (cdr data))
3332                 (> (setq l (gnus-data-level (car data))) level))
3333       (and (= (1+ level) l)
3334            (push (gnus-data-number (car data))
3335                  children)))
3336     (nreverse children)))
3337
3338 (defun gnus-summary-article-parent (&optional number)
3339   "Return the article number of the parent of article NUMBER."
3340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3341                                     (gnus-data-list t)))
3342          (level (gnus-data-level (car data))))
3343     (if (zerop level)
3344         ()                              ; This is a root.
3345       ;; We search until we find an article with a level less than
3346       ;; this one.  That function has to be the parent.
3347       (while (and (setq data (cdr data))
3348                   (not (< (gnus-data-level (car data)) level))))
3349       (and data (gnus-data-number (car data))))))
3350
3351 (defun gnus-unread-mark-p (mark)
3352   "Say whether MARK is the unread mark."
3353   (= mark gnus-unread-mark))
3354
3355 (defun gnus-read-mark-p (mark)
3356   "Say whether MARK is one of the marks that mark as read.
3357 This is all marks except unread, ticked, dormant, and expirable."
3358   (not (or (= mark gnus-unread-mark)
3359            (= mark gnus-ticked-mark)
3360            (= mark gnus-spam-mark)
3361            (= mark gnus-dormant-mark)
3362            (= mark gnus-expirable-mark))))
3363
3364 (defmacro gnus-article-mark (number)
3365   "Return the MARK of article NUMBER.
3366 This macro should only be used when computing the mark the \"first\"
3367 time; i.e., when generating the summary lines.  After that,
3368 `gnus-summary-article-mark' should be used to examine the
3369 marks of articles."
3370   `(cond
3371     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3372     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3373     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3374     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3375     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3376     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3377     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3378     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3379            gnus-ancient-mark))))
3380
3381 ;; Saving hidden threads.
3382
3383 (defmacro gnus-save-hidden-threads (&rest forms)
3384   "Save hidden threads, eval FORMS, and restore the hidden threads."
3385   (let ((config (make-symbol "config")))
3386     `(let ((,config (gnus-hidden-threads-configuration)))
3387        (unwind-protect
3388            (save-excursion
3389              ,@forms)
3390          (gnus-restore-hidden-threads-configuration ,config)))))
3391 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3392 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3393
3394 (defun gnus-data-compute-positions ()
3395   "Compute the positions of all articles."
3396   (setq gnus-newsgroup-data-reverse nil)
3397   (let ((data gnus-newsgroup-data))
3398     (save-excursion
3399       (gnus-save-hidden-threads
3400         (gnus-summary-show-all-threads)
3401         (goto-char (point-min))
3402         (while data
3403           (while (get-text-property (point) 'gnus-intangible)
3404             (forward-line 1))
3405           (gnus-data-set-pos (car data) (+ (point) 3))
3406           (setq data (cdr data))
3407           (forward-line 1))))))
3408
3409 (defun gnus-hidden-threads-configuration ()
3410   "Return the current hidden threads configuration."
3411   (save-excursion
3412     (let (config)
3413       (goto-char (point-min))
3414       (while (not (eobp))
3415         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3416           (push (save-excursion (forward-line 0) (point)) config))
3417         (forward-line 1))
3418       config)))
3419
3420 (defun gnus-restore-hidden-threads-configuration (config)
3421   "Restore hidden threads configuration from CONFIG."
3422   (save-excursion
3423     (let (point (inhibit-read-only t))
3424       (while (setq point (pop config))
3425         (goto-char point)
3426         (gnus-summary-hide-thread)))))
3427
3428 ;; Various summary mode internalish functions.
3429
3430 (defun gnus-mouse-pick-article (e)
3431   (interactive "e")
3432   (mouse-set-point e)
3433   (gnus-summary-next-page nil t))
3434
3435 (defun gnus-summary-set-display-table ()
3436   "Change the display table.
3437 Odd characters have a tendency to mess
3438 up nicely formatted displays - we make all possible glyphs
3439 display only a single character."
3440
3441   ;; We start from the standard display table, if any.
3442   (let ((table (or (copy-sequence standard-display-table)
3443                    (make-display-table)))
3444         (i 32))
3445     ;; Nix out all the control chars...
3446     (while (>= (setq i (1- i)) 0)
3447       (gnus-put-display-table i [??] table))
3448    ;; ... but not newline and cr, of course.  (cr is necessary for the
3449     ;; selective display).
3450     (gnus-put-display-table ?\n nil table)
3451     (gnus-put-display-table ?\r nil table)
3452     ;; We keep TAB as well.
3453     (gnus-put-display-table ?\t nil table)
3454     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3455     ;; Emacs 23 (unicode), that are not set already.
3456     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3457                  160
3458                256)))
3459       (while (>= (setq i (1- i)) 127)
3460         ;; Only modify if the entry is nil.
3461         (unless (gnus-get-display-table i table)
3462           (gnus-put-display-table i [??] table))))
3463     (setq buffer-display-table table)))
3464
3465 (defun gnus-summary-set-article-display-arrow (pos)
3466   "Update the overlay arrow to point to line at position POS."
3467   (when gnus-summary-display-arrow
3468     (make-local-variable 'overlay-arrow-position)
3469     (make-local-variable 'overlay-arrow-string)
3470     (save-excursion
3471       (goto-char pos)
3472       (beginning-of-line)
3473       (unless overlay-arrow-position
3474         (setq overlay-arrow-position (make-marker)))
3475       (setq overlay-arrow-string "=>"
3476             overlay-arrow-position (set-marker overlay-arrow-position
3477                                                (point)
3478                                                (current-buffer))))))
3479
3480 (defun gnus-summary-setup-buffer (group)
3481   "Initialize summary buffer."
3482   (let ((buffer (gnus-summary-buffer-name group))
3483         (dead-name (concat "*Dead Summary "
3484                            (gnus-group-decoded-name group) "*")))
3485     ;; If a dead summary buffer exists, we kill it.
3486     (when (gnus-buffer-live-p dead-name)
3487       (gnus-kill-buffer dead-name))
3488     (if (get-buffer buffer)
3489         (progn
3490           (set-buffer buffer)
3491           (setq gnus-summary-buffer (current-buffer))
3492           (not gnus-newsgroup-prepared))
3493       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3494       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3495       (gnus-summary-mode group)
3496       (when (gnus-group-quit-config group)
3497         (set (make-local-variable 'gnus-single-article-buffer) nil))
3498       (make-local-variable 'gnus-article-buffer)
3499       (make-local-variable 'gnus-article-current)
3500       (make-local-variable 'gnus-original-article-buffer)
3501       (setq gnus-newsgroup-name group)
3502       ;; Set any local variables in the group parameters.
3503       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3504       t)))
3505
3506 (defun gnus-set-global-variables ()
3507   "Set the global equivalents of the buffer-local variables.
3508 They are set to the latest values they had.  These reflect the summary
3509 buffer that was in action when the last article was fetched."
3510   (when (eq major-mode 'gnus-summary-mode)
3511     (setq gnus-summary-buffer (current-buffer))
3512     (let ((name gnus-newsgroup-name)
3513           (marked gnus-newsgroup-marked)
3514           (spam gnus-newsgroup-spam-marked)
3515           (unread gnus-newsgroup-unreads)
3516           (headers gnus-current-headers)
3517           (data gnus-newsgroup-data)
3518           (summary gnus-summary-buffer)
3519           (article-buffer gnus-article-buffer)
3520           (original gnus-original-article-buffer)
3521           (gac gnus-article-current)
3522           (reffed gnus-reffed-article-number)
3523           (score-file gnus-current-score-file)
3524           (default-charset gnus-newsgroup-charset)
3525           vlist)
3526       (let ((locals gnus-newsgroup-variables))
3527         (while locals
3528           (if (consp (car locals))
3529               (push (eval (caar locals)) vlist)
3530             (push (eval (car locals)) vlist))
3531           (setq locals (cdr locals)))
3532         (setq vlist (nreverse vlist)))
3533       (with-current-buffer gnus-group-buffer
3534         (setq gnus-newsgroup-name name
3535               gnus-newsgroup-marked marked
3536               gnus-newsgroup-spam-marked spam
3537               gnus-newsgroup-unreads unread
3538               gnus-current-headers headers
3539               gnus-newsgroup-data data
3540               gnus-article-current gac
3541               gnus-summary-buffer summary
3542               gnus-article-buffer article-buffer
3543               gnus-original-article-buffer original
3544               gnus-reffed-article-number reffed
3545               gnus-current-score-file score-file
3546               gnus-newsgroup-charset default-charset)
3547         (let ((locals gnus-newsgroup-variables))
3548           (while locals
3549             (if (consp (car locals))
3550                 (set (caar locals) (pop vlist))
3551               (set (car locals) (pop vlist)))
3552             (setq locals (cdr locals))))
3553         ;; The article buffer also has local variables.
3554         (when (gnus-buffer-live-p gnus-article-buffer)
3555           (set-buffer gnus-article-buffer)
3556           (setq gnus-summary-buffer summary))))))
3557
3558 (defun gnus-summary-article-unread-p (article)
3559   "Say whether ARTICLE is unread or not."
3560   (memq article gnus-newsgroup-unreads))
3561
3562 (defun gnus-summary-first-article-p (&optional article)
3563   "Return whether ARTICLE is the first article in the buffer."
3564   (if (not (setq article (or article (gnus-summary-article-number))))
3565       nil
3566     (eq article (caar gnus-newsgroup-data))))
3567
3568 (defun gnus-summary-last-article-p (&optional article)
3569   "Return whether ARTICLE is the last article in the buffer."
3570   (if (not (setq article (or article (gnus-summary-article-number))))
3571       ;; All non-existent numbers are the last article.  :-)
3572       t
3573     (not (cdr (gnus-data-find-list article)))))
3574
3575 (defun gnus-make-thread-indent-array (&optional n)
3576   (when (or n
3577             (progn (setq n 200) nil)
3578             (null gnus-thread-indent-array)
3579             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3580     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3581           gnus-thread-indent-array-level gnus-thread-indent-level)
3582     (while (>= n 0)
3583       (aset gnus-thread-indent-array n
3584             (make-string (* n gnus-thread-indent-level) ? ))
3585       (setq n (1- n)))))
3586
3587 (defun gnus-update-summary-mark-positions ()
3588   "Compute where the summary marks are to go."
3589   (save-excursion
3590     (when (gnus-buffer-exists-p gnus-summary-buffer)
3591       (set-buffer gnus-summary-buffer))
3592     (let ((spec gnus-summary-line-format-spec)
3593           pos)
3594       (save-excursion
3595         (gnus-set-work-buffer)
3596         (let ((gnus-tmp-unread ?Z)
3597               (gnus-replied-mark ?Z)
3598               (gnus-score-below-mark ?Z)
3599               (gnus-score-over-mark ?Z)
3600               (gnus-undownloaded-mark ?Z)
3601               (gnus-summary-line-format-spec spec)
3602               (gnus-newsgroup-downloadable '(0))
3603               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3604               case-fold-search ignores)
3605           ;; Here, all marks are bound to Z.
3606           (gnus-summary-insert-line header
3607                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3608           (goto-char (point-min))
3609           ;; Memorize the positions of the same characters as dummy marks.
3610           (while (re-search-forward "[A-D]" nil t)
3611             (push (point) ignores))
3612           (erase-buffer)
3613           ;; We use A-D as dummy marks in order to know column positions
3614           ;; where marks should be inserted.
3615           (setq gnus-tmp-unread ?A
3616                 gnus-replied-mark ?B
3617                 gnus-score-below-mark ?C
3618                 gnus-score-over-mark ?C
3619                 gnus-undownloaded-mark ?D)
3620           (gnus-summary-insert-line header
3621                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3622           ;; Ignore characters which aren't dummy marks.
3623           (dolist (p ignores)
3624             (delete-region (goto-char (1- p)) p)
3625             (insert ?Z))
3626           (goto-char (point-min))
3627           (setq pos (list (cons 'unread
3628                                 (and (search-forward "A" nil t)
3629                                      (- (point) (point-min) 1)))))
3630           (goto-char (point-min))
3631           (push (cons 'replied (and (search-forward "B" nil t)
3632                                     (- (point) (point-min) 1)))
3633                 pos)
3634           (goto-char (point-min))
3635           (push (cons 'score (and (search-forward "C" nil t)
3636                                   (- (point) (point-min) 1)))
3637                 pos)
3638           (goto-char (point-min))
3639           (push (cons 'download (and (search-forward "D" nil t)
3640                                      (- (point) (point-min) 1)))
3641                 pos)))
3642       (setq gnus-summary-mark-positions pos))))
3643
3644 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3645   "Insert a dummy root in the summary buffer."
3646   (beginning-of-line)
3647   (gnus-add-text-properties
3648    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3649    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3650
3651 (defun gnus-summary-extract-address-component (from)
3652   (or (car (funcall gnus-extract-address-components from))
3653       from))
3654
3655 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3656   (let ((mail-parse-charset gnus-newsgroup-charset)
3657         (ignored-from-addresses (gnus-ignored-from-addresses))
3658         ; Is it really necessary to do this next part for each summary line?
3659         ; Luckily, doesn't seem to slow things down much.
3660         (mail-parse-ignored-charsets
3661          (with-current-buffer gnus-summary-buffer
3662            gnus-newsgroup-ignored-charsets)))
3663     (or
3664      (and ignored-from-addresses
3665           (string-match ignored-from-addresses gnus-tmp-from)
3666           (let ((extra-headers (mail-header-extra header))
3667                 to
3668                 newsgroups)
3669             (cond
3670              ((setq to (cdr (assq 'To extra-headers)))
3671               (concat gnus-summary-to-prefix
3672                       (inline
3673                         (gnus-summary-extract-address-component
3674                          (funcall gnus-decode-encoded-address-function to)))))
3675              ((setq newsgroups
3676                     (or
3677                      (cdr (assq 'Newsgroups extra-headers))
3678                      (and
3679                       (memq 'Newsgroups gnus-extra-headers)
3680                       (eq (car (gnus-find-method-for-group
3681                                 gnus-newsgroup-name)) 'nntp)
3682                       (gnus-group-real-name gnus-newsgroup-name))))
3683               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3684      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3685
3686 (defun gnus-summary-insert-line (gnus-tmp-header
3687                                  gnus-tmp-level gnus-tmp-current
3688                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3689                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3690                                  &optional gnus-tmp-dummy gnus-tmp-score
3691                                  gnus-tmp-process)
3692   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3693       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3694                                           gnus-tmp-level)))
3695   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3696          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3697          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3698          (gnus-tmp-score-char
3699           (if (or (null gnus-summary-default-score)
3700                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3701                       gnus-summary-zcore-fuzz))
3702               ?                         ;Whitespace
3703             (if (< gnus-tmp-score gnus-summary-default-score)
3704                 gnus-score-below-mark gnus-score-over-mark)))
3705          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3706          (gnus-tmp-replied
3707           (cond (gnus-tmp-process gnus-process-mark)
3708                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3709                  gnus-cached-mark)
3710                 (gnus-tmp-replied gnus-replied-mark)
3711                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3712                  gnus-forwarded-mark)
3713                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3714                  gnus-saved-mark)
3715                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3716                  gnus-recent-mark)
3717                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3718                  gnus-unseen-mark)
3719                 (t gnus-no-mark)))
3720          (gnus-tmp-downloaded
3721           (cond (undownloaded
3722                  gnus-undownloaded-mark)
3723                 (gnus-newsgroup-agentized
3724                  gnus-downloaded-mark)
3725                 (t
3726                  gnus-no-mark)))
3727          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3728          (gnus-tmp-name
3729           (cond
3730            ((string-match "<[^>]+> *$" gnus-tmp-from)
3731             (let ((beg (match-beginning 0)))
3732               (or (and (string-match "^\".+\"" gnus-tmp-from)
3733                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3734                   (substring gnus-tmp-from 0 beg))))
3735            ((string-match "(.+)" gnus-tmp-from)
3736             (substring gnus-tmp-from
3737                        (1+ (match-beginning 0)) (1- (match-end 0))))
3738            (t gnus-tmp-from)))
3739          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3740          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3741          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3742          (inhibit-read-only t))
3743     (when (string= gnus-tmp-name "")
3744       (setq gnus-tmp-name gnus-tmp-from))
3745     (unless (numberp gnus-tmp-lines)
3746       (setq gnus-tmp-lines -1))
3747     (if (= gnus-tmp-lines -1)
3748         (setq gnus-tmp-lines "?")
3749       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3750     (condition-case ()
3751         (gnus-put-text-property
3752          (point)
3753          (progn (eval gnus-summary-line-format-spec) (point))
3754          'gnus-number gnus-tmp-number)
3755       (error (gnus-message 5 "Error updating the summary line")))
3756     (when (gnus-visual-p 'summary-highlight 'highlight)
3757       (forward-line -1)
3758       (gnus-summary-highlight-line)
3759       (gnus-run-hooks 'gnus-summary-update-hook)
3760       (forward-line 1))))
3761
3762 (defun gnus-summary-update-line (&optional dont-update)
3763   "Update summary line after change."
3764   (when (and gnus-summary-default-score
3765              (not gnus-summary-inhibit-highlight))
3766     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3767            (article (gnus-summary-article-number))
3768            (score (gnus-summary-article-score article)))
3769       (unless dont-update
3770         (if (and gnus-summary-mark-below
3771                  (< (gnus-summary-article-score)
3772                     gnus-summary-mark-below))
3773             ;; This article has a low score, so we mark it as read.
3774             (when (memq article gnus-newsgroup-unreads)
3775               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3776           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3777             ;; This article was previously marked as read on account
3778             ;; of a low score, but now it has risen, so we mark it as
3779             ;; unread.
3780             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3781         (gnus-summary-update-mark
3782          (if (or (null gnus-summary-default-score)
3783                  (<= (abs (- score gnus-summary-default-score))
3784                      gnus-summary-zcore-fuzz))
3785              ?                          ;Whitespace
3786            (if (< score gnus-summary-default-score)
3787                gnus-score-below-mark gnus-score-over-mark))
3788          'score))
3789       ;; Do visual highlighting.
3790       (when (gnus-visual-p 'summary-highlight 'highlight)
3791         (gnus-summary-highlight-line)
3792         (gnus-run-hooks 'gnus-summary-update-hook)))))
3793
3794 (defvar gnus-tmp-new-adopts nil)
3795
3796 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3797   "Return the number of articles in THREAD.
3798 This may be 0 in some cases -- if none of the articles in
3799 the thread are to be displayed."
3800   (let* ((number
3801          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3802           (cond
3803            ((not (listp thread))
3804             1)
3805            ((and (consp thread) (cdr thread))
3806             (apply
3807              '+ 1 (mapcar
3808                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3809            ((null thread)
3810             1)
3811            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3812             1)
3813            (t 0))))
3814     (when (and level (zerop level) gnus-tmp-new-adopts)
3815       (incf number
3816             (apply '+ (mapcar
3817                        'gnus-summary-number-of-articles-in-thread
3818                        gnus-tmp-new-adopts))))
3819     (if char
3820         (if (> number 1) gnus-not-empty-thread-mark
3821           gnus-empty-thread-mark)
3822       number)))
3823
3824 (defsubst gnus-summary-line-message-size (head)
3825   "Return pretty-printed version of message size.
3826 This function is intended to be used in
3827 `gnus-summary-line-format-alist'."
3828   (let ((c (or (mail-header-chars head) -1)))
3829     (cond ((< c 0) "n/a")               ; chars not available
3830           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3831           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3832           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3833           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3834
3835
3836 (defun gnus-summary-set-local-parameters (group)
3837   "Go through the local params of GROUP and set all variable specs in that list."
3838   (let ((vars '(quit-config)))          ; Ignore quit-config.
3839     (dolist (elem (gnus-group-find-parameter group))
3840       (and (consp elem)                 ; Has to be a cons.
3841            (consp (cdr elem))           ; The cdr has to be a list.
3842            (symbolp (car elem))         ; Has to be a symbol in there.
3843            (not (memq (car elem) vars))
3844            (ignore-errors
3845              (push (car elem) vars)
3846              ;; Variables like `gnus-show-threads' that are globally
3847              ;; bound, if used as group parameters, need to get to be
3848              ;; buffer-local, whereas just parameters like `gcc-self',
3849              ;; `timestamp', etc. should not be bound as variables.
3850              (if (boundp (car elem))
3851                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3852                (eval (nth 1 elem))))))))
3853
3854 (defun gnus-summary-read-group (group &optional show-all no-article
3855                                       kill-buffer no-display backward
3856                                       select-articles)
3857   "Start reading news in newsgroup GROUP.
3858 If SHOW-ALL is non-nil, already read articles are also listed.
3859 If NO-ARTICLE is non-nil, no article is selected initially.
3860 If NO-DISPLAY, don't generate a summary buffer."
3861   (let (result)
3862     (while (and group
3863                 (null (setq result
3864                             (let ((gnus-auto-select-next nil))
3865                               (or (gnus-summary-read-group-1
3866                                    group show-all no-article
3867                                    kill-buffer no-display
3868                                    select-articles)
3869                                   (setq show-all nil
3870                                         select-articles nil)))))
3871                 (eq gnus-auto-select-next 'quietly))
3872       (set-buffer gnus-group-buffer)
3873       ;; The entry function called above goes to the next
3874       ;; group automatically, so we go two groups back
3875       ;; if we are searching for the previous group.
3876       (when backward
3877         (gnus-group-prev-unread-group 2))
3878       (if (not (equal group (gnus-group-group-name)))
3879           (setq group (gnus-group-group-name))
3880         (setq group nil)))
3881     result))
3882
3883 (defun gnus-summary-read-group-1 (group show-all no-article
3884                                         kill-buffer no-display
3885                                         &optional select-articles)
3886   ;; Killed foreign groups can't be entered.
3887   ;;  (when (and (not (gnus-group-native-p group))
3888   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3889   ;;    (error "Dead non-native groups can't be entered"))
3890   (gnus-message 5 "Retrieving newsgroup: %s..."
3891                 (gnus-group-decoded-name group))
3892   (let* ((new-group (gnus-summary-setup-buffer group))
3893          (quit-config (gnus-group-quit-config group))
3894          (did-select (and new-group (gnus-select-newsgroup
3895                                      group show-all select-articles))))
3896     (cond
3897      ;; This summary buffer exists already, so we just select it.
3898      ((not new-group)
3899       (gnus-set-global-variables)
3900       (when kill-buffer
3901         (gnus-kill-or-deaden-summary kill-buffer))
3902       (gnus-configure-windows 'summary 'force)
3903       (gnus-set-mode-line 'summary)
3904       (gnus-summary-position-point)
3905       (message "")
3906       t)
3907      ;; We couldn't select this group.
3908      ((null did-select)
3909       (when (and (eq major-mode 'gnus-summary-mode)
3910                  (not (equal (current-buffer) kill-buffer)))
3911         (kill-buffer (current-buffer))
3912         (if (not quit-config)
3913             (progn
3914               ;; Update the info -- marks might need to be removed,
3915               ;; for instance.
3916               (gnus-summary-update-info)
3917               (set-buffer gnus-group-buffer)
3918               (gnus-group-jump-to-group group)
3919               (gnus-group-next-unread-group 1))
3920           (gnus-handle-ephemeral-exit quit-config)))
3921       (let ((grpinfo (gnus-get-info group)))
3922         (if (null (gnus-info-read grpinfo))
3923             (gnus-message 3 "Group %s contains no messages"
3924                           (gnus-group-decoded-name group))
3925           (gnus-message 3 "Can't select group")))
3926       nil)
3927      ;; The user did a `C-g' while prompting for number of articles,
3928      ;; so we exit this group.
3929      ((eq did-select 'quit)
3930       (and (eq major-mode 'gnus-summary-mode)
3931            (not (equal (current-buffer) kill-buffer))
3932            (kill-buffer (current-buffer)))
3933       (when kill-buffer
3934         (gnus-kill-or-deaden-summary kill-buffer))
3935       (if (not quit-config)
3936           (progn
3937             (set-buffer gnus-group-buffer)
3938             (gnus-group-jump-to-group group)
3939             (gnus-configure-windows 'group 'force))
3940         (gnus-handle-ephemeral-exit quit-config))
3941       ;; Finally signal the quit.
3942       (signal 'quit nil))
3943      ;; The group was successfully selected.
3944      (t
3945       (gnus-set-global-variables)
3946       ;; Save the active value in effect when the group was entered.
3947       (setq gnus-newsgroup-active
3948             (gnus-copy-sequence
3949              (gnus-active gnus-newsgroup-name)))
3950       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3951       ;; You can change the summary buffer in some way with this hook.
3952       (gnus-run-hooks 'gnus-select-group-hook)
3953       (when (memq 'summary (gnus-update-format-specifications
3954                             nil 'summary 'summary-mode 'summary-dummy))
3955         ;; The format specification for the summary line was updated,
3956         ;; so we need to update the mark positions as well.
3957         (gnus-update-summary-mark-positions))
3958       ;; Do score processing.
3959       (when gnus-use-scoring
3960         (gnus-possibly-score-headers))
3961       ;; Check whether to fill in the gaps in the threads.
3962       (when gnus-build-sparse-threads
3963         (gnus-build-sparse-threads))
3964       ;; Find the initial limit.
3965       (if show-all
3966           (let ((gnus-newsgroup-dormant nil))
3967             (gnus-summary-initial-limit show-all))
3968         (gnus-summary-initial-limit show-all))
3969       ;; Generate the summary buffer.
3970       (unless no-display
3971         (gnus-summary-prepare))
3972       (when gnus-use-trees
3973         (gnus-tree-open group)
3974         (setq gnus-summary-highlight-line-function
3975               'gnus-tree-highlight-article))
3976       ;; If the summary buffer is empty, but there are some low-scored
3977       ;; articles or some excluded dormants, we include these in the
3978       ;; buffer.
3979       (when (and (zerop (buffer-size))
3980                  (not no-display))
3981         (cond (gnus-newsgroup-dormant
3982                (gnus-summary-limit-include-dormant))
3983               ((and gnus-newsgroup-scored show-all)
3984                (gnus-summary-limit-include-expunged t))))
3985       ;; Function `gnus-apply-kill-file' must be called in this hook.
3986       (gnus-run-hooks 'gnus-apply-kill-hook)
3987       (if (and (zerop (buffer-size))
3988                (not no-display))
3989           (progn
3990             ;; This newsgroup is empty.
3991             (gnus-summary-catchup-and-exit nil t)
3992             (gnus-message 6 "No unread news")
3993             (when kill-buffer
3994               (gnus-kill-or-deaden-summary kill-buffer))
3995             ;; Return nil from this function.
3996             nil)
3997         ;; Hide conversation thread subtrees.  We cannot do this in
3998         ;; gnus-summary-prepare-hook since kill processing may not
3999         ;; work with hidden articles.
4000         (gnus-summary-maybe-hide-threads)
4001         (when kill-buffer
4002           (gnus-kill-or-deaden-summary kill-buffer))
4003         (gnus-summary-auto-select-subject)
4004         ;; Show first unread article if requested.
4005         (if (and (not no-article)
4006                  (not no-display)
4007                  gnus-newsgroup-unreads
4008                  gnus-auto-select-first)
4009             (progn
4010               (gnus-configure-windows 'summary)
4011               (let ((art (gnus-summary-article-number)))
4012                 (unless (and (not gnus-plugged)
4013                              (or (memq art gnus-newsgroup-undownloaded)
4014                                  (memq art gnus-newsgroup-downloadable)))
4015                   (gnus-summary-goto-article art))))
4016           ;; Don't select any articles.
4017           (gnus-summary-position-point)
4018           (gnus-configure-windows 'summary 'force)
4019           (gnus-set-mode-line 'summary))
4020         (when (and gnus-auto-center-group
4021                    (get-buffer-window gnus-group-buffer t))
4022           ;; Gotta use windows, because recenter does weird stuff if
4023           ;; the current buffer ain't the displayed window.
4024           (let ((owin (selected-window)))
4025             (select-window (get-buffer-window gnus-group-buffer t))
4026             (when (gnus-group-goto-group group)
4027               (recenter))
4028             (select-window owin)))
4029         ;; Mark this buffer as "prepared".
4030         (setq gnus-newsgroup-prepared t)
4031         (gnus-run-hooks 'gnus-summary-prepared-hook)
4032         (unless (gnus-ephemeral-group-p group)
4033           (gnus-group-update-group group))
4034         t)))))
4035
4036 (defun gnus-summary-auto-select-subject ()
4037   "Select the subject line on initial group entry."
4038   (goto-char (point-min))
4039   (cond
4040    ((eq gnus-auto-select-subject 'best)
4041     (gnus-summary-best-unread-subject))
4042    ((eq gnus-auto-select-subject 'unread)
4043     (gnus-summary-first-unread-subject))
4044    ((eq gnus-auto-select-subject 'unseen)
4045     (gnus-summary-first-unseen-subject))
4046    ((eq gnus-auto-select-subject 'unseen-or-unread)
4047     (gnus-summary-first-unseen-or-unread-subject))
4048    ((eq gnus-auto-select-subject 'first)
4049     ;; Do nothing.
4050     )
4051    ((functionp gnus-auto-select-subject)
4052     (funcall gnus-auto-select-subject))))
4053
4054 (defun gnus-summary-prepare ()
4055   "Generate the summary buffer."
4056   (interactive)
4057   (let ((inhibit-read-only t))
4058     (erase-buffer)
4059     (setq gnus-newsgroup-data nil
4060           gnus-newsgroup-data-reverse nil)
4061     (gnus-run-hooks 'gnus-summary-generate-hook)
4062     ;; Generate the buffer, either with threads or without.
4063     (when gnus-newsgroup-headers
4064       (gnus-summary-prepare-threads
4065        (if gnus-show-threads
4066            (gnus-sort-gathered-threads
4067             (funcall gnus-summary-thread-gathering-function
4068                      (gnus-sort-threads
4069                       (gnus-cut-threads (gnus-make-threads)))))
4070          ;; Unthreaded display.
4071          (gnus-sort-articles gnus-newsgroup-headers))))
4072     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4073     ;; Call hooks for modifying summary buffer.
4074     (goto-char (point-min))
4075     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4076
4077 (defsubst gnus-general-simplify-subject (subject)
4078   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4079   (setq subject
4080         (cond
4081          ;; Truncate the subject.
4082          (gnus-simplify-subject-functions
4083           (gnus-map-function gnus-simplify-subject-functions subject))
4084          ((numberp gnus-summary-gather-subject-limit)
4085           (setq subject (gnus-simplify-subject-re subject))
4086           (if (> (length subject) gnus-summary-gather-subject-limit)
4087               (substring subject 0 gnus-summary-gather-subject-limit)
4088             subject))
4089          ;; Fuzzily simplify it.
4090          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4091           (gnus-simplify-subject-fuzzy subject))
4092          ;; Just remove the leading "Re:".
4093          (t
4094           (gnus-simplify-subject-re subject))))
4095
4096   (if (and gnus-summary-gather-exclude-subject
4097            (string-match gnus-summary-gather-exclude-subject subject))
4098       nil                         ; This article shouldn't be gathered
4099     subject))
4100
4101 (defun gnus-summary-simplify-subject-query ()
4102   "Query where the respool algorithm would put this article."
4103   (interactive)
4104   (gnus-summary-select-article)
4105   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4106
4107 (defun gnus-gather-threads-by-subject (threads)
4108   "Gather threads by looking at Subject headers."
4109   (if (not gnus-summary-make-false-root)
4110       threads
4111     (let ((hashtb (gnus-make-hashtable 1024))
4112           (prev threads)
4113           (result threads)
4114           subject hthread whole-subject)
4115       (while threads
4116         (setq subject (gnus-general-simplify-subject
4117                        (setq whole-subject (mail-header-subject
4118                                             (caar threads)))))
4119         (when subject
4120           (if (setq hthread (gnus-gethash subject hashtb))
4121               (progn
4122                 ;; We enter a dummy root into the thread, if we
4123                 ;; haven't done that already.
4124                 (unless (stringp (caar hthread))
4125                   (setcar hthread (list whole-subject (car hthread))))
4126                 ;; We add this new gathered thread to this gathered
4127                 ;; thread.
4128                 (setcdr (car hthread)
4129                         (nconc (cdar hthread) (list (car threads))))
4130                 ;; Remove it from the list of threads.
4131                 (setcdr prev (cdr threads))
4132                 (setq threads prev))
4133             ;; Enter this thread into the hash table.
4134             (gnus-sethash subject
4135                           (if gnus-summary-make-false-root-always
4136                               (progn
4137                                 ;; If you want a dummy root above all
4138                                 ;; threads...
4139                                 (setcar threads (list whole-subject
4140                                                       (car threads)))
4141                                 threads)
4142                             threads)
4143                           hashtb)))
4144         (setq prev threads)
4145         (setq threads (cdr threads)))
4146       result)))
4147
4148 (defun gnus-gather-threads-by-references (threads)
4149   "Gather threads by looking at References headers."
4150   (let ((idhashtb (gnus-make-hashtable 1024))
4151         (thhashtb (gnus-make-hashtable 1024))
4152         (prev threads)
4153         (result threads)
4154         ids references id gthread gid entered ref)
4155     (while threads
4156       (when (setq references (mail-header-references (caar threads)))
4157         (setq id (mail-header-id (caar threads))
4158               ids (inline (gnus-split-references references))
4159               entered nil)
4160         (while (setq ref (pop ids))
4161           (setq ids (delete ref ids))
4162           (if (not (setq gid (gnus-gethash ref idhashtb)))
4163               (progn
4164                 (gnus-sethash ref id idhashtb)
4165                 (gnus-sethash id threads thhashtb))
4166             (setq gthread (gnus-gethash gid thhashtb))
4167             (unless entered
4168               ;; We enter a dummy root into the thread, if we
4169               ;; haven't done that already.
4170               (unless (stringp (caar gthread))
4171                 (setcar gthread (list (mail-header-subject (caar gthread))
4172                                       (car gthread))))
4173               ;; We add this new gathered thread to this gathered
4174               ;; thread.
4175               (setcdr (car gthread)
4176                       (nconc (cdar gthread) (list (car threads)))))
4177             ;; Add it into the thread hash table.
4178             (gnus-sethash id gthread thhashtb)
4179             (setq entered t)
4180             ;; Remove it from the list of threads.
4181             (setcdr prev (cdr threads))
4182             (setq threads prev))))
4183       (setq prev threads)
4184       (setq threads (cdr threads)))
4185     result))
4186
4187 (defun gnus-sort-gathered-threads (threads)
4188   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4189   (let ((result threads))
4190     (while threads
4191       (when (stringp (caar threads))
4192         (setcdr (car threads)
4193                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4194       (setq threads (cdr threads)))
4195     result))
4196
4197 (defun gnus-thread-loop-p (root thread)
4198   "Say whether ROOT is in THREAD."
4199   (let ((stack (list thread))
4200         (infloop 0)
4201         th)
4202     (while (setq thread (pop stack))
4203       (setq th (cdr thread))
4204       (while (and th
4205                   (not (eq (caar th) root)))
4206         (pop th))
4207       (if th
4208           ;; We have found a loop.
4209           (let (ref-dep)
4210             (setcdr thread (delq (car th) (cdr thread)))
4211             (if (boundp (setq ref-dep (intern "none"
4212                                               gnus-newsgroup-dependencies)))
4213                 (setcdr (symbol-value ref-dep)
4214                         (nconc (cdr (symbol-value ref-dep))
4215                                (list (car th))))
4216               (set ref-dep (list nil (car th))))
4217             (setq infloop 1
4218                   stack nil))
4219         ;; Push all the subthreads onto the stack.
4220         (push (cdr thread) stack)))
4221     infloop))
4222
4223 (defun gnus-make-threads ()
4224   "Go through the dependency hashtb and find the roots.  Return all threads."
4225   (let (threads)
4226     (while (catch 'infloop
4227              (mapatoms
4228               (lambda (refs)
4229                 ;; Deal with self-referencing References loops.
4230                 (when (and (car (symbol-value refs))
4231                            (not (zerop
4232                                  (apply
4233                                   '+
4234                                   (mapcar
4235                                    (lambda (thread)
4236                                      (gnus-thread-loop-p
4237                                       (car (symbol-value refs)) thread))
4238                                    (cdr (symbol-value refs)))))))
4239                   (setq threads nil)
4240                   (throw 'infloop t))
4241                 (unless (car (symbol-value refs))
4242                   ;; These threads do not refer back to any other
4243                   ;; articles, so they're roots.
4244                   (setq threads (append (cdr (symbol-value refs)) threads))))
4245               gnus-newsgroup-dependencies)))
4246     threads))
4247
4248 ;; Build the thread tree.
4249 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4250   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4251
4252 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4253 if it was already present.
4254
4255 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4256 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4257 Message-IDs will be renamed to a unique Message-ID before being
4258 entered.
4259
4260 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4261   (let* ((id (mail-header-id header))
4262          (id-dep (and id (intern id dependencies)))
4263          parent-id ref ref-dep ref-header replaced)
4264     ;; Enter this `header' in the `dependencies' table.
4265     (cond
4266      ((not id-dep)
4267       (setq header nil))
4268      ;; The first two cases do the normal part: enter a new `header'
4269      ;; in the `dependencies' table.
4270      ((not (boundp id-dep))
4271       (set id-dep (list header)))
4272      ((null (car (symbol-value id-dep)))
4273       (setcar (symbol-value id-dep) header))
4274
4275      ;; From here the `header' was already present in the
4276      ;; `dependencies' table.
4277      (force-new
4278       ;; Overrides an existing entry;
4279       ;; just set the header part of the entry.
4280       (setcar (symbol-value id-dep) header)
4281       (setq replaced t))
4282
4283      ;; Renames the existing `header' to a unique Message-ID.
4284      ((not gnus-summary-ignore-duplicates)
4285       ;; An article with this Message-ID has already been seen.
4286       ;; We rename the Message-ID.
4287       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4288            (list header))
4289       (mail-header-set-id header id))
4290
4291      ;; The last case ignores an existing entry, except it adds any
4292      ;; additional Xrefs (in case the two articles came from different
4293      ;; servers.
4294      ;; Also sets `header' to `nil' meaning that the `dependencies'
4295      ;; table was *not* modified.
4296      (t
4297       (mail-header-set-xref
4298        (car (symbol-value id-dep))
4299        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4300                    "")
4301                (or (mail-header-xref header) "")))
4302       (setq header nil)))
4303
4304     (when (and header (not replaced))
4305       ;; First check that we are not creating a References loop.
4306       (setq parent-id (gnus-parent-id (mail-header-references header)))
4307       (setq ref parent-id)
4308       (while (and ref
4309                   (setq ref-dep (intern-soft ref dependencies))
4310                   (boundp ref-dep)
4311                   (setq ref-header (car (symbol-value ref-dep))))
4312         (if (string= id ref)
4313             ;; Yuk!  This is a reference loop.  Make the article be a
4314             ;; root article.
4315             (progn
4316               (mail-header-set-references (car (symbol-value id-dep)) "none")
4317               (setq ref nil)
4318               (setq parent-id nil))
4319           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4320       (setq ref-dep (intern (or parent-id "none") dependencies))
4321       (if (boundp ref-dep)
4322           (setcdr (symbol-value ref-dep)
4323                   (nconc (cdr (symbol-value ref-dep))
4324                          (list (symbol-value id-dep))))
4325         (set ref-dep (list nil (symbol-value id-dep)))))
4326     header))
4327
4328 (defun gnus-extract-message-id-from-in-reply-to (string)
4329   (if (string-match "<[^>]+>" string)
4330       (substring string (match-beginning 0) (match-end 0))
4331     nil))
4332
4333 (defun gnus-build-sparse-threads ()
4334   (let ((headers gnus-newsgroup-headers)
4335         (mail-parse-charset gnus-newsgroup-charset)
4336         (gnus-summary-ignore-duplicates t)
4337         header references generation relations
4338         subject child end new-child date)
4339     ;; First we create an alist of generations/relations, where
4340     ;; generations is how much we trust the relation, and the relation
4341     ;; is parent/child.
4342     (gnus-message 7 "Making sparse threads...")
4343     (save-excursion
4344       (nnheader-set-temp-buffer " *gnus sparse threads*")
4345       (while (setq header (pop headers))
4346         (when (and (setq references (mail-header-references header))
4347                    (not (string= references "")))
4348           (insert references)
4349           (setq child (mail-header-id header)
4350                 subject (mail-header-subject header)
4351                 date (mail-header-date header)
4352                 generation 0)
4353           (while (search-backward ">" nil t)
4354             (setq end (1+ (point)))
4355             (when (search-backward "<" nil t)
4356               (setq new-child (buffer-substring (point) end))
4357               (push (list (incf generation)
4358                           child (setq child new-child)
4359                           subject date)
4360                     relations)))
4361           (when child
4362             (push (list (1+ generation) child nil subject) relations))
4363           (erase-buffer)))
4364       (kill-buffer (current-buffer)))
4365     ;; Sort over trustworthiness.
4366     (dolist (relation (sort relations 'car-less-than-car))
4367       (when (gnus-dependencies-add-header
4368              (make-full-mail-header
4369               gnus-reffed-article-number
4370               (nth 3 relation) "" (or (nth 4 relation) "")
4371               (nth 1 relation)
4372               (or (nth 2 relation) "") 0 0 "")
4373              gnus-newsgroup-dependencies nil)
4374         (push gnus-reffed-article-number gnus-newsgroup-limit)
4375         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4376         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4377               gnus-newsgroup-reads)
4378         (decf gnus-reffed-article-number)))
4379     (gnus-message 7 "Making sparse threads...done")))
4380
4381 (defun gnus-build-old-threads ()
4382   ;; Look at all the articles that refer back to old articles, and
4383   ;; fetch the headers for the articles that aren't there.  This will
4384   ;; build complete threads - if the roots haven't been expired by the
4385   ;; server, that is.
4386   (let ((mail-parse-charset gnus-newsgroup-charset)
4387         id heads)
4388     (mapatoms
4389      (lambda (refs)
4390        (when (not (car (symbol-value refs)))
4391          (setq heads (cdr (symbol-value refs)))
4392          (while heads
4393            (if (memq (mail-header-number (caar heads))
4394                      gnus-newsgroup-dormant)
4395                (setq heads (cdr heads))
4396              (setq id (symbol-name refs))
4397              (while (and (setq id (gnus-build-get-header id))
4398                          (not (car (gnus-id-to-thread id)))))
4399              (setq heads nil)))))
4400      gnus-newsgroup-dependencies)))
4401
4402 (defsubst gnus-remove-odd-characters (string)
4403   "Translate STRING into something that doesn't contain weird characters."
4404   (mm-subst-char-in-string
4405    ?\r ?\-
4406    (mm-subst-char-in-string ?\n ?\- string t) t))
4407
4408 ;; This function has to be called with point after the article number
4409 ;; on the beginning of the line.
4410 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4411   (let ((eol (point-at-eol))
4412         (buffer (current-buffer))
4413         header references in-reply-to)
4414
4415     ;; overview: [num subject from date id refs chars lines misc]
4416     (unwind-protect
4417         (let (x)
4418           (narrow-to-region (point) eol)
4419           (unless (eobp)
4420             (forward-char))
4421
4422           (setq header
4423                 (make-full-mail-header
4424                  number                 ; number
4425                  (condition-case ()     ; subject
4426                      (gnus-remove-odd-characters
4427                       (funcall gnus-decode-encoded-word-function
4428                                (setq x (nnheader-nov-field))))
4429                    (error x))
4430                  (condition-case ()     ; from
4431                      (gnus-remove-odd-characters
4432                       (funcall gnus-decode-encoded-address-function
4433                                (setq x (nnheader-nov-field))))
4434                    (error x))
4435                  (nnheader-nov-field)   ; date
4436                  (nnheader-nov-read-message-id number)  ; id
4437                  (setq references (nnheader-nov-field)) ; refs
4438                  (nnheader-nov-read-integer) ; chars
4439                  (nnheader-nov-read-integer) ; lines
4440                  (unless (eobp)
4441                    (if (looking-at "Xref: ")
4442                        (goto-char (match-end 0)))
4443                    (nnheader-nov-field)) ; Xref
4444                  (nnheader-nov-parse-extra)))) ; extra
4445
4446       (widen))
4447
4448     (when (and (string= references "")
4449                (setq in-reply-to (mail-header-extra header))
4450                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4451       (mail-header-set-references
4452        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4453
4454     (when gnus-alter-header-function
4455       (funcall gnus-alter-header-function header))
4456     (gnus-dependencies-add-header header dependencies force-new)))
4457
4458 (defun gnus-build-get-header (id)
4459   "Look through the buffer of NOV lines and find the header to ID.
4460 Enter this line into the dependencies hash table, and return
4461 the id of the parent article (if any)."
4462   (let ((deps gnus-newsgroup-dependencies)
4463         found header)
4464     (prog1
4465         (with-current-buffer nntp-server-buffer
4466           (let ((case-fold-search nil))
4467             (goto-char (point-min))
4468             (while (and (not found)
4469                         (search-forward id nil t))
4470               (beginning-of-line)
4471               (setq found (looking-at
4472                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4473                                    (regexp-quote id))))
4474               (or found (beginning-of-line 2)))
4475             (when found
4476               (beginning-of-line)
4477               (and
4478                (setq header (gnus-nov-parse-line
4479                              (read (current-buffer)) deps))
4480                (gnus-parent-id (mail-header-references header))))))
4481       (when header
4482         (let ((number (mail-header-number header)))
4483           (push number gnus-newsgroup-limit)
4484           (push header gnus-newsgroup-headers)
4485           (if (memq number gnus-newsgroup-unselected)
4486               (progn
4487                 (setq gnus-newsgroup-unreads
4488                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4489                                                number))
4490                 (setq gnus-newsgroup-unselected
4491                       (delq number gnus-newsgroup-unselected)))
4492             (push number gnus-newsgroup-ancient)))))))
4493
4494 (defun gnus-build-all-threads ()
4495   "Read all the headers."
4496   (let ((gnus-summary-ignore-duplicates t)
4497         (mail-parse-charset gnus-newsgroup-charset)
4498         (dependencies gnus-newsgroup-dependencies)
4499         header article)
4500     (with-current-buffer nntp-server-buffer
4501       (let ((case-fold-search nil))
4502         (goto-char (point-min))
4503         (while (not (eobp))
4504           (ignore-errors
4505             (setq article (read (current-buffer))
4506                   header (gnus-nov-parse-line article dependencies)))
4507           (when header
4508             (with-current-buffer gnus-summary-buffer
4509               (push header gnus-newsgroup-headers)
4510               (if (memq (setq article (mail-header-number header))
4511                         gnus-newsgroup-unselected)
4512                   (progn
4513                     (setq gnus-newsgroup-unreads
4514                           (gnus-add-to-sorted-list
4515                            gnus-newsgroup-unreads article))
4516                     (setq gnus-newsgroup-unselected
4517                           (delq article gnus-newsgroup-unselected)))
4518                 (push article gnus-newsgroup-ancient)))
4519             (forward-line 1)))))))
4520
4521 (defun gnus-summary-update-article-line (article header)
4522   "Update the line for ARTICLE using HEADER."
4523   (let* ((id (mail-header-id header))
4524          (thread (gnus-id-to-thread id)))
4525     (unless thread
4526       (error "Article in no thread"))
4527     ;; Update the thread.
4528     (setcar thread header)
4529     (gnus-summary-goto-subject article)
4530     (let* ((datal (gnus-data-find-list article))
4531            (data (car datal))
4532            (inhibit-read-only t)
4533            (level (gnus-summary-thread-level)))
4534       (gnus-delete-line)
4535       (let ((inserted (- (point)
4536                          (progn
4537                            (gnus-summary-insert-line
4538                             header level nil
4539                             (memq article gnus-newsgroup-undownloaded)
4540                             (gnus-article-mark article)
4541                             (memq article gnus-newsgroup-replied)
4542                             (memq article gnus-newsgroup-expirable)
4543                             ;; Only insert the Subject string when it's different
4544                             ;; from the previous Subject string.
4545                             (if (and
4546                                  gnus-show-threads
4547                                  (gnus-subject-equal
4548                                   (condition-case ()
4549                                       (mail-header-subject
4550                                        (gnus-data-header
4551                                         (cadr
4552                                          (gnus-data-find-list
4553                                           article
4554                                           (gnus-data-list t)))))
4555                                     ;; Error on the side of excessive subjects.
4556                                     (error ""))
4557                                   (mail-header-subject header)))
4558                                 ""
4559                               (mail-header-subject header))
4560                             nil (cdr (assq article gnus-newsgroup-scored))
4561                             (memq article gnus-newsgroup-processable))
4562                            (point)))))
4563         (when (cdr datal)
4564           (gnus-data-update-list
4565            (cdr datal)
4566            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4567
4568 (defun gnus-summary-update-article (article &optional iheader)
4569   "Update ARTICLE in the summary buffer."
4570   (set-buffer gnus-summary-buffer)
4571   (let* ((header (gnus-summary-article-header article))
4572          (id (mail-header-id header))
4573          (data (gnus-data-find article))
4574          (thread (gnus-id-to-thread id))
4575          (references (mail-header-references header))
4576          (parent
4577           (gnus-id-to-thread
4578            (or (gnus-parent-id
4579                 (when (and references
4580                            (not (equal "" references)))
4581                   references))
4582                "none")))
4583          (inhibit-read-only t)
4584          (old (car thread)))
4585     (when thread
4586       (unless iheader
4587         (setcar thread nil)
4588         (when parent
4589           (delq thread parent)))
4590       (if (gnus-summary-insert-subject id header)
4591           ;; Set the (possibly) new article number in the data structure.
4592           (gnus-data-set-number data (gnus-id-to-article id))
4593         (setcar thread old)
4594         nil))))
4595
4596 (defun gnus-rebuild-thread (id &optional line)
4597   "Rebuild the thread containing ID.
4598 If LINE, insert the rebuilt thread starting on line LINE."
4599   (let ((inhibit-read-only t)
4600         old-pos current thread data)
4601     (if (not gnus-show-threads)
4602         (setq thread (list (car (gnus-id-to-thread id))))
4603       ;; Get the thread this article is part of.
4604       (setq thread (gnus-remove-thread id)))
4605     (setq old-pos (point-at-bol))
4606     (setq current (save-excursion
4607                     (and (re-search-backward "[\r\n]" nil t)
4608                          (gnus-summary-article-number))))
4609     ;; If this is a gathered thread, we have to go some re-gathering.
4610     (when (stringp (car thread))
4611       (let ((subject (car thread))
4612             roots thr)
4613         (setq thread (cdr thread))
4614         (while thread
4615           (unless (memq (setq thr (gnus-id-to-thread
4616                                    (gnus-root-id
4617                                     (mail-header-id (caar thread)))))
4618                         roots)
4619             (push thr roots))
4620           (setq thread (cdr thread)))
4621         ;; We now have all (unique) roots.
4622         (if (= (length roots) 1)
4623             ;; All the loose roots are now one solid root.
4624             (setq thread (car roots))
4625           (setq thread (cons subject (gnus-sort-threads roots))))))
4626     (let (threads)
4627       ;; We then insert this thread into the summary buffer.
4628       (when line
4629         (goto-char (point-min))
4630         (forward-line (1- line)))
4631       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4632         (if gnus-show-threads
4633             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4634           (gnus-summary-prepare-unthreaded thread))
4635         (setq data (nreverse gnus-newsgroup-data))
4636         (setq threads gnus-newsgroup-threads))
4637       ;; We splice the new data into the data structure.
4638       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4639       ;;!!! then we want to insert at the beginning of the buffer.
4640       ;;!!! That happens to be true with Gnus now, but that may
4641       ;;!!! change in the future.  Perhaps.
4642       (gnus-data-enter-list
4643        (if line nil current) data (- (point) old-pos))
4644       (setq gnus-newsgroup-threads
4645             (nconc threads gnus-newsgroup-threads))
4646       (gnus-data-compute-positions))))
4647
4648 (defun gnus-number-to-header (number)
4649   "Return the header for article NUMBER."
4650   (let ((headers gnus-newsgroup-headers))
4651     (while (and headers
4652                 (not (= number (mail-header-number (car headers)))))
4653       (pop headers))
4654     (when headers
4655       (car headers))))
4656
4657 (defun gnus-parent-headers (in-headers &optional generation)
4658   "Return the headers of the GENERATIONeth parent of HEADERS."
4659   (unless generation
4660     (setq generation 1))
4661   (let ((parent t)
4662         (headers in-headers)
4663         references)
4664     (while (and parent
4665                 (not (zerop generation))
4666                 (setq references (mail-header-references headers)))
4667       (setq headers (if (and references
4668                              (setq parent (gnus-parent-id references)))
4669                         (car (gnus-id-to-thread parent))
4670                       nil))
4671       (decf generation))
4672     (and (not (eq headers in-headers))
4673          headers)))
4674
4675 (defun gnus-id-to-thread (id)
4676   "Return the (sub-)thread where ID appears."
4677   (gnus-gethash id gnus-newsgroup-dependencies))
4678
4679 (defun gnus-id-to-article (id)
4680   "Return the article number of ID."
4681   (let ((thread (gnus-id-to-thread id)))
4682     (when (and thread
4683                (car thread))
4684       (mail-header-number (car thread)))))
4685
4686 (defun gnus-id-to-header (id)
4687   "Return the article headers of ID."
4688   (car (gnus-id-to-thread id)))
4689
4690 (defun gnus-article-displayed-root-p (article)
4691   "Say whether ARTICLE is a root(ish) article."
4692   (let ((level (gnus-summary-thread-level article))
4693         (refs (mail-header-references  (gnus-summary-article-header article)))
4694         particle)
4695     (cond
4696      ((null level) nil)
4697      ((zerop level) t)
4698      ((null refs) t)
4699      ((null (gnus-parent-id refs)) t)
4700      ((and (= 1 level)
4701            (null (setq particle (gnus-id-to-article
4702                                  (gnus-parent-id refs))))
4703            (null (gnus-summary-thread-level particle)))))))
4704
4705 (defun gnus-root-id (id)
4706   "Return the id of the root of the thread where ID appears."
4707   (let (last-id prev)
4708     (while (and id (setq prev (car (gnus-id-to-thread id))))
4709       (setq last-id id
4710             id (gnus-parent-id (mail-header-references prev))))
4711     last-id))
4712
4713 (defun gnus-articles-in-thread (thread)
4714   "Return the list of articles in THREAD."
4715   (cons (mail-header-number (car thread))
4716         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4717
4718 (defun gnus-remove-thread (id &optional dont-remove)
4719   "Remove the thread that has ID in it."
4720   (let (headers thread last-id)
4721     ;; First go up in this thread until we find the root.
4722     (setq last-id (gnus-root-id id)
4723           headers (message-flatten-list (gnus-id-to-thread last-id)))
4724     ;; We have now found the real root of this thread.  It might have
4725     ;; been gathered into some loose thread, so we have to search
4726     ;; through the threads to find the thread we wanted.
4727     (let ((threads gnus-newsgroup-threads)
4728           sub)
4729       (while threads
4730         (setq sub (car threads))
4731         (if (stringp (car sub))
4732             ;; This is a gathered thread, so we look at the roots
4733             ;; below it to find whether this article is in this
4734             ;; gathered root.
4735             (progn
4736               (setq sub (cdr sub))
4737               (while sub
4738                 (when (member (caar sub) headers)
4739                   (setq thread (car threads)
4740                         threads nil
4741                         sub nil))
4742                 (setq sub (cdr sub))))
4743           ;; It's an ordinary thread, so we check it.
4744           (when (eq (car sub) (car headers))
4745             (setq thread sub
4746                   threads nil)))
4747         (setq threads (cdr threads)))
4748       ;; If this article is in no thread, then it's a root.
4749       (if thread
4750           (unless dont-remove
4751             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4752         (setq thread (gnus-id-to-thread last-id)))
4753       (when thread
4754         (prog1
4755             thread                      ; We return this thread.
4756           (unless dont-remove
4757             (if (stringp (car thread))
4758                 (progn
4759                   ;; If we use dummy roots, then we have to remove the
4760                   ;; dummy root as well.
4761                   (when (eq gnus-summary-make-false-root 'dummy)
4762                     ;; We go to the dummy root by going to
4763                     ;; the first sub-"thread", and then one line up.
4764                     (gnus-summary-goto-article
4765                      (mail-header-number (caadr thread)))
4766                     (forward-line -1)
4767                     (gnus-delete-line)
4768                     (gnus-data-compute-positions))
4769                   (setq thread (cdr thread))
4770                   (while thread
4771                     (gnus-remove-thread-1 (car thread))
4772                     (setq thread (cdr thread))))
4773               (gnus-remove-thread-1 thread))))))))
4774
4775 (defun gnus-remove-thread-1 (thread)
4776   "Remove the thread THREAD recursively."
4777   (let ((number (mail-header-number (pop thread)))
4778         d)
4779     (setq thread (reverse thread))
4780     (while thread
4781       (gnus-remove-thread-1 (pop thread)))
4782     (when (setq d (gnus-data-find number))
4783       (goto-char (gnus-data-pos d))
4784       (gnus-summary-show-thread)
4785       (gnus-data-remove
4786        number
4787        (- (point-at-bol)
4788           (prog1
4789               (1+ (point-at-eol))
4790             (gnus-delete-line)))))))
4791
4792 (defun gnus-sort-threads-recursive (threads func)
4793   (sort (mapcar (lambda (thread)
4794                   (cons (car thread)
4795                         (and (cdr thread)
4796                              (gnus-sort-threads-recursive (cdr thread) func))))
4797                 threads) func))
4798
4799 (defun gnus-sort-threads-loop (threads func)
4800   (let* ((superthread (cons nil threads))
4801          (stack (list (cons superthread threads)))
4802          remaining-threads thread)
4803     (while stack
4804       (setq remaining-threads (cdr (car stack)))
4805       (if remaining-threads
4806           (progn (setq thread (car remaining-threads))
4807                  (setcdr (car stack) (cdr remaining-threads))
4808                  (if (cdr thread)
4809                      (push (cons thread (cdr thread)) stack)))
4810         (setq thread (caar stack))
4811         (setcdr thread (sort (cdr thread) func))
4812         (pop stack)))
4813     (cdr superthread)))
4814
4815 (defun gnus-sort-threads (threads)
4816   "Sort THREADS."
4817   (if (not gnus-thread-sort-functions)
4818       threads
4819     (gnus-message 8 "Sorting threads...")
4820     (prog1
4821         (condition-case nil
4822             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4823               (gnus-sort-threads-recursive
4824                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4825           ;; Even after binding max-lisp-eval-depth, the recursive
4826           ;; sorter might fail for very long threads.  In that case,
4827           ;; try using a (less well-tested) non-recursive sorter.
4828           (error (gnus-message 9 "Sorting threads with loop...")
4829                  (gnus-sort-threads-loop
4830                   threads (gnus-make-sort-function
4831                            gnus-thread-sort-functions))))
4832       (gnus-message 8 "Sorting threads...done"))))
4833
4834 (defun gnus-sort-articles (articles)
4835   "Sort ARTICLES."
4836   (when gnus-article-sort-functions
4837     (gnus-message 7 "Sorting articles...")
4838     (prog1
4839         (setq gnus-newsgroup-headers
4840               (sort articles (gnus-make-sort-function
4841                               gnus-article-sort-functions)))
4842       (gnus-message 7 "Sorting articles...done"))))
4843
4844 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4845 (defmacro gnus-thread-header (thread)
4846   "Return header of first article in THREAD.
4847 Note that THREAD must never, ever be anything else than a variable -
4848 using some other form will lead to serious barfage."
4849   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4850   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4851   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4852         (vector thread) 2))
4853
4854 (defsubst gnus-article-sort-by-number (h1 h2)
4855   "Sort articles by article number."
4856   (< (mail-header-number h1)
4857      (mail-header-number h2)))
4858
4859 (defun gnus-thread-sort-by-number (h1 h2)
4860   "Sort threads by root article number."
4861   (gnus-article-sort-by-number
4862    (gnus-thread-header h1) (gnus-thread-header h2)))
4863
4864 (defsubst gnus-article-sort-by-random (h1 h2)
4865   "Sort articles randomly."
4866   (zerop (random 2)))
4867
4868 (defun gnus-thread-sort-by-random (h1 h2)
4869   "Sort threads randomly."
4870   (gnus-article-sort-by-random
4871    (gnus-thread-header h1) (gnus-thread-header h2)))
4872
4873 (defsubst gnus-article-sort-by-lines (h1 h2)
4874   "Sort articles by article Lines header."
4875   (< (mail-header-lines h1)
4876      (mail-header-lines h2)))
4877
4878 (defun gnus-thread-sort-by-lines (h1 h2)
4879   "Sort threads by root article Lines header."
4880   (gnus-article-sort-by-lines
4881    (gnus-thread-header h1) (gnus-thread-header h2)))
4882
4883 (defsubst gnus-article-sort-by-chars (h1 h2)
4884   "Sort articles by octet length."
4885   (< (mail-header-chars h1)
4886      (mail-header-chars h2)))
4887
4888 (defun gnus-thread-sort-by-chars (h1 h2)
4889   "Sort threads by root article octet length."
4890   (gnus-article-sort-by-chars
4891    (gnus-thread-header h1) (gnus-thread-header h2)))
4892
4893 (defsubst gnus-article-sort-by-author (h1 h2)
4894   "Sort articles by root author."
4895   (gnus-string<
4896    (let ((extract (funcall
4897                    gnus-extract-address-components
4898                    (mail-header-from h1))))
4899      (or (car extract) (cadr extract) ""))
4900    (let ((extract (funcall
4901                    gnus-extract-address-components
4902                    (mail-header-from h2))))
4903      (or (car extract) (cadr extract) ""))))
4904
4905 (defun gnus-thread-sort-by-author (h1 h2)
4906   "Sort threads by root author."
4907   (gnus-article-sort-by-author
4908    (gnus-thread-header h1)  (gnus-thread-header h2)))
4909
4910 (defsubst gnus-article-sort-by-recipient (h1 h2)
4911   "Sort articles by recipient."
4912   (gnus-string<
4913    (let ((extract (funcall
4914                    gnus-extract-address-components
4915                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4916      (or (car extract) (cadr extract)))
4917    (let ((extract (funcall
4918                    gnus-extract-address-components
4919                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4920      (or (car extract) (cadr extract)))))
4921
4922 (defun gnus-thread-sort-by-recipient (h1 h2)
4923   "Sort threads by root recipient."
4924   (gnus-article-sort-by-recipient
4925    (gnus-thread-header h1) (gnus-thread-header h2)))
4926
4927 (defsubst gnus-article-sort-by-subject (h1 h2)
4928   "Sort articles by root subject."
4929   (gnus-string<
4930    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4931    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4932
4933 (defun gnus-thread-sort-by-subject (h1 h2)
4934   "Sort threads by root subject."
4935   (gnus-article-sort-by-subject
4936    (gnus-thread-header h1) (gnus-thread-header h2)))
4937
4938 (defsubst gnus-article-sort-by-date (h1 h2)
4939   "Sort articles by root article date."
4940   (time-less-p
4941    (gnus-date-get-time (mail-header-date h1))
4942    (gnus-date-get-time (mail-header-date h2))))
4943
4944 (defun gnus-thread-sort-by-date (h1 h2)
4945   "Sort threads by root article date."
4946   (gnus-article-sort-by-date
4947    (gnus-thread-header h1) (gnus-thread-header h2)))
4948
4949 (defsubst gnus-article-sort-by-score (h1 h2)
4950   "Sort articles by root article score.
4951 Unscored articles will be counted as having a score of zero."
4952   (> (or (cdr (assq (mail-header-number h1)
4953                     gnus-newsgroup-scored))
4954          gnus-summary-default-score 0)
4955      (or (cdr (assq (mail-header-number h2)
4956                     gnus-newsgroup-scored))
4957          gnus-summary-default-score 0)))
4958
4959 (defun gnus-thread-sort-by-score (h1 h2)
4960   "Sort threads by root article score."
4961   (gnus-article-sort-by-score
4962    (gnus-thread-header h1) (gnus-thread-header h2)))
4963
4964 (defun gnus-thread-sort-by-total-score (h1 h2)
4965   "Sort threads by the sum of all scores in the thread.
4966 Unscored articles will be counted as having a score of zero."
4967   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4968
4969 (defun gnus-thread-total-score (thread)
4970   ;; This function find the total score of THREAD.
4971   (cond
4972    ((null thread)
4973     0)
4974    ((consp thread)
4975     (if (stringp (car thread))
4976         (apply gnus-thread-score-function 0
4977                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4978       (gnus-thread-total-score-1 thread)))
4979    (t
4980     (gnus-thread-total-score-1 (list thread)))))
4981
4982 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4983   "Sort threads such that the thread with the most recently arrived article comes first."
4984   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4985
4986 (defun gnus-thread-highest-number (thread)
4987   "Return the highest article number in THREAD."
4988   (apply 'max (mapcar (lambda (header)
4989                         (mail-header-number header))
4990                       (message-flatten-list thread))))
4991
4992 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4993   "Sort threads such that the thread with the most recently dated article comes first."
4994   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4995
4996 ; Since this is called not only to sort the top-level threads, but
4997 ; also in recursive sorts to order the articles within a thread, each
4998 ; article will be processed many times.  Thus it speeds things up
4999 ; quite a bit to use gnus-date-get-time, which caches the time value.
5000 (defun gnus-thread-latest-date (thread)
5001   "Return the highest article date in THREAD."
5002   (apply 'max
5003          (mapcar (lambda (header) (gnus-float-time
5004                                    (gnus-date-get-time
5005                                     (mail-header-date header))))
5006                  (message-flatten-list thread))))
5007
5008 (defun gnus-thread-total-score-1 (root)
5009   ;; This function find the total score of the thread below ROOT.
5010   (setq root (car root))
5011   (apply gnus-thread-score-function
5012          (or (append
5013               (mapcar 'gnus-thread-total-score
5014                       (cdr (gnus-id-to-thread (mail-header-id root))))
5015               (when (> (mail-header-number root) 0)
5016                 (list (or (cdr (assq (mail-header-number root)
5017                                      gnus-newsgroup-scored))
5018                           gnus-summary-default-score 0))))
5019              (list gnus-summary-default-score)
5020              '(0))))
5021
5022 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5023 (defvar gnus-tmp-prev-subject nil)
5024 (defvar gnus-tmp-false-parent nil)
5025 (defvar gnus-tmp-root-expunged nil)
5026 (defvar gnus-tmp-dummy-line nil)
5027
5028 (defun gnus-extra-header (type &optional header)
5029   "Return the extra header of TYPE."
5030   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5031       ""))
5032
5033 (defvar gnus-tmp-thread-tree-header-string "")
5034
5035 (defcustom gnus-sum-thread-tree-root "> "
5036   "With %B spec, used for the root of a thread.
5037 If nil, use subject instead."
5038   :version "22.1"
5039   :type '(radio (const :format "%v  " nil) string)
5040   :group 'gnus-thread)
5041
5042 (defcustom gnus-sum-thread-tree-false-root "> "
5043   "With %B spec, used for a false root of a thread.
5044 If nil, use subject instead."
5045   :version "22.1"
5046   :type '(radio (const :format "%v  " nil) string)
5047   :group 'gnus-thread)
5048
5049 (defcustom gnus-sum-thread-tree-single-indent ""
5050   "With %B spec, used for a thread with just one message.
5051 If nil, use subject instead."
5052   :version "22.1"
5053   :type '(radio (const :format "%v  " nil) string)
5054   :group 'gnus-thread)
5055
5056 (defcustom gnus-sum-thread-tree-vertical "| "
5057   "With %B spec, used for drawing a vertical line."
5058   :version "22.1"
5059   :type 'string
5060   :group 'gnus-thread)
5061
5062 (defcustom gnus-sum-thread-tree-indent "  "
5063   "With %B spec, used for indenting."
5064   :version "22.1"
5065   :type 'string
5066   :group 'gnus-thread)
5067
5068 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5069   "With %B spec, used for a leaf with brothers."
5070   :version "22.1"
5071   :type 'string
5072   :group 'gnus-thread)
5073
5074 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5075   "With %B spec, used for a leaf without brothers."
5076   :version "22.1"
5077   :type 'string
5078   :group 'gnus-thread)
5079
5080 (defcustom gnus-summary-display-while-building nil
5081   "If non-nil, show and update the summary buffer as it's being built.
5082 If the value is t, update the buffer after every line is inserted.  If
5083 the value is an integer (N), update the display every N lines."
5084   :version "22.1"
5085   :group 'gnus-thread
5086   :type '(choice (const :tag "off" nil)
5087                  number
5088                  (const :tag "frequently" t)))
5089
5090 (defun gnus-summary-prepare-threads (threads)
5091   "Prepare summary buffer from THREADS and indentation LEVEL.
5092 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5093 or a straight list of headers."
5094   (gnus-message 7 "Generating summary...")
5095
5096   (setq gnus-newsgroup-threads threads)
5097   (beginning-of-line)
5098
5099   (let ((gnus-tmp-level 0)
5100         (default-score (or gnus-summary-default-score 0))
5101         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5102         (building-line-count gnus-summary-display-while-building)
5103         (building-count (integerp gnus-summary-display-while-building))
5104         thread number subject stack state gnus-tmp-gathered beg-match
5105         new-roots gnus-tmp-new-adopts thread-end simp-subject
5106         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5107         gnus-tmp-replied gnus-tmp-subject-or-nil
5108         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5109         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5110         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5111         tree-stack)
5112
5113     (setq gnus-tmp-prev-subject nil
5114           gnus-tmp-thread-tree-header-string "")
5115
5116     (if (vectorp (car threads))
5117         ;; If this is a straight (sic) list of headers, then a
5118         ;; threaded summary display isn't required, so we just create
5119         ;; an unthreaded one.
5120         (gnus-summary-prepare-unthreaded threads)
5121
5122       ;; Do the threaded display.
5123
5124       (if gnus-summary-display-while-building
5125           (switch-to-buffer (buffer-name)))
5126       (while (or threads stack gnus-tmp-new-adopts new-roots)
5127
5128         (if (and (= gnus-tmp-level 0)
5129                  (or (not stack)
5130                      (= (caar stack) 0))
5131                  (not gnus-tmp-false-parent)
5132                  (or gnus-tmp-new-adopts new-roots))
5133             (if gnus-tmp-new-adopts
5134                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5135                       thread (list (car gnus-tmp-new-adopts))
5136                       gnus-tmp-header (caar thread)
5137                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5138               (when new-roots
5139                 (setq thread (list (car new-roots))
5140                       gnus-tmp-header (caar thread)
5141                       new-roots (cdr new-roots))))
5142
5143           (if threads
5144               ;; If there are some threads, we do them before the
5145               ;; threads on the stack.
5146               (setq thread threads
5147                     gnus-tmp-header (caar thread))
5148             ;; There were no current threads, so we pop something off
5149             ;; the stack.
5150             (setq state (car stack)
5151                   gnus-tmp-level (car state)
5152                   tree-stack (cadr state)
5153                   thread (caddr state)
5154                   stack (cdr stack)
5155                   gnus-tmp-header (caar thread))))
5156
5157         (setq gnus-tmp-false-parent nil)
5158         (setq gnus-tmp-root-expunged nil)
5159         (setq thread-end nil)
5160
5161         (if (stringp gnus-tmp-header)
5162             ;; The header is a dummy root.
5163             (cond
5164              ((eq gnus-summary-make-false-root 'adopt)
5165               ;; We let the first article adopt the rest.
5166               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5167                                                (cddar thread)))
5168               (setq gnus-tmp-gathered
5169                     (nconc (mapcar
5170                             (lambda (h) (mail-header-number (car h)))
5171                             (cddar thread))
5172                            gnus-tmp-gathered))
5173               (setq thread (cons (list (caar thread)
5174                                        (cadar thread))
5175                                  (cdr thread)))
5176               (setq gnus-tmp-level -1
5177                     gnus-tmp-false-parent t))
5178              ((eq gnus-summary-make-false-root 'empty)
5179               ;; We print adopted articles with empty subject fields.
5180               (setq gnus-tmp-gathered
5181                     (nconc (mapcar
5182                             (lambda (h) (mail-header-number (car h)))
5183                             (cddar thread))
5184                            gnus-tmp-gathered))
5185               (setq gnus-tmp-level -1))
5186              ((eq gnus-summary-make-false-root 'dummy)
5187               ;; We remember that we probably want to output a dummy
5188               ;; root.
5189               (setq gnus-tmp-dummy-line gnus-tmp-header)
5190               (setq gnus-tmp-prev-subject gnus-tmp-header))
5191              (t
5192               ;; We do not make a root for the gathered
5193               ;; sub-threads at all.
5194               (setq gnus-tmp-level -1)))
5195
5196           (setq number (mail-header-number gnus-tmp-header)
5197                 subject (mail-header-subject gnus-tmp-header)
5198                 simp-subject (gnus-simplify-subject-fully subject))
5199
5200           (cond
5201            ;; If the thread has changed subject, we might want to make
5202            ;; this subthread into a root.
5203            ((and (null gnus-thread-ignore-subject)
5204                  (not (zerop gnus-tmp-level))
5205                  gnus-tmp-prev-subject
5206                  (not (string= gnus-tmp-prev-subject simp-subject)))
5207             (setq new-roots (nconc new-roots (list (car thread)))
5208                   thread-end t
5209                   gnus-tmp-header nil))
5210            ;; If the article lies outside the current limit,
5211            ;; then we do not display it.
5212            ((not (memq number gnus-newsgroup-limit))
5213             (setq gnus-tmp-gathered
5214                   (nconc (mapcar
5215                           (lambda (h) (mail-header-number (car h)))
5216                           (cdar thread))
5217                          gnus-tmp-gathered))
5218             (setq gnus-tmp-new-adopts (if (cdar thread)
5219                                           (append gnus-tmp-new-adopts
5220                                                   (cdar thread))
5221                                         gnus-tmp-new-adopts)
5222                   thread-end t
5223                   gnus-tmp-header nil)
5224             (when (zerop gnus-tmp-level)
5225               (setq gnus-tmp-root-expunged t)))
5226            ;; Perhaps this article is to be marked as read?
5227            ((and gnus-summary-mark-below
5228                  (< (or (cdr (assq number gnus-newsgroup-scored))
5229                         default-score)
5230                     gnus-summary-mark-below)
5231                  ;; Don't touch sparse articles.
5232                  (not (gnus-summary-article-sparse-p number))
5233                  (not (gnus-summary-article-ancient-p number)))
5234             (setq gnus-newsgroup-unreads
5235                   (delq number gnus-newsgroup-unreads))
5236             (if gnus-newsgroup-auto-expire
5237                 (setq gnus-newsgroup-expirable
5238                       (gnus-add-to-sorted-list
5239                        gnus-newsgroup-expirable number))
5240               (push (cons number gnus-low-score-mark)
5241                     gnus-newsgroup-reads))))
5242
5243           (when gnus-tmp-header
5244             ;; We may have an old dummy line to output before this
5245             ;; article.
5246             (when (and gnus-tmp-dummy-line
5247                        (gnus-subject-equal
5248                         gnus-tmp-dummy-line
5249                         (mail-header-subject gnus-tmp-header)))
5250               (gnus-summary-insert-dummy-line
5251                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5252               (setq gnus-tmp-dummy-line nil))
5253
5254             ;; Compute the mark.
5255             (setq gnus-tmp-unread (gnus-article-mark number))
5256
5257             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5258                                   gnus-tmp-header gnus-tmp-level)
5259                   gnus-newsgroup-data)
5260
5261             ;; Actually insert the line.
5262             (setq
5263              gnus-tmp-subject-or-nil
5264              (cond
5265               ((and gnus-thread-ignore-subject
5266                     gnus-tmp-prev-subject
5267                     (not (string= gnus-tmp-prev-subject simp-subject)))
5268                subject)
5269               ((zerop gnus-tmp-level)
5270                (if (and (eq gnus-summary-make-false-root 'empty)
5271                         (memq number gnus-tmp-gathered)
5272                         gnus-tmp-prev-subject
5273                         (string= gnus-tmp-prev-subject simp-subject))
5274                    gnus-summary-same-subject
5275                  subject))
5276               (t gnus-summary-same-subject)))
5277             (if (and (eq gnus-summary-make-false-root 'adopt)
5278                      (= gnus-tmp-level 1)
5279                      (memq number gnus-tmp-gathered))
5280                 (setq gnus-tmp-opening-bracket ?\<
5281                       gnus-tmp-closing-bracket ?\>)
5282               (setq gnus-tmp-opening-bracket ?\[
5283                     gnus-tmp-closing-bracket ?\]))
5284             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5285                 (gnus-make-thread-indent-array
5286                  (max (* 2 (length gnus-thread-indent-array))
5287                       gnus-tmp-level)))
5288             (setq
5289              gnus-tmp-indentation
5290              (aref gnus-thread-indent-array gnus-tmp-level)
5291              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5292              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5293                                 gnus-summary-default-score 0)
5294              gnus-tmp-score-char
5295              (if (or (null gnus-summary-default-score)
5296                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5297                          gnus-summary-zcore-fuzz))
5298                  ?                      ;Whitespace
5299                (if (< gnus-tmp-score gnus-summary-default-score)
5300                    gnus-score-below-mark gnus-score-over-mark))
5301              gnus-tmp-replied
5302              (cond ((memq number gnus-newsgroup-processable)
5303                     gnus-process-mark)
5304                    ((memq number gnus-newsgroup-cached)
5305                     gnus-cached-mark)
5306                    ((memq number gnus-newsgroup-replied)
5307                     gnus-replied-mark)
5308                    ((memq number gnus-newsgroup-forwarded)
5309                     gnus-forwarded-mark)
5310                    ((memq number gnus-newsgroup-saved)
5311                     gnus-saved-mark)
5312                    ((memq number gnus-newsgroup-recent)
5313                     gnus-recent-mark)
5314                    ((memq number gnus-newsgroup-unseen)
5315                     gnus-unseen-mark)
5316                    (t gnus-no-mark))
5317              gnus-tmp-downloaded
5318              (cond ((memq number gnus-newsgroup-undownloaded)
5319                     gnus-undownloaded-mark)
5320                    (gnus-newsgroup-agentized
5321                     gnus-downloaded-mark)
5322                    (t
5323                     gnus-no-mark))
5324              gnus-tmp-from (mail-header-from gnus-tmp-header)
5325              gnus-tmp-name
5326              (cond
5327               ((string-match "<[^>]+> *$" gnus-tmp-from)
5328                (setq beg-match (match-beginning 0))
5329                (or (and (string-match "^\".+\"" gnus-tmp-from)
5330                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5331                    (substring gnus-tmp-from 0 beg-match)))
5332               ((string-match "(.+)" gnus-tmp-from)
5333                (substring gnus-tmp-from
5334                           (1+ (match-beginning 0)) (1- (match-end 0))))
5335               (t gnus-tmp-from))
5336
5337              ;; Do the %B string
5338              gnus-tmp-thread-tree-header-string
5339              (cond
5340               ((not gnus-show-threads) "")
5341               ((zerop gnus-tmp-level)
5342                (cond ((cdar thread)
5343                       (or gnus-sum-thread-tree-root subject))
5344                      (gnus-tmp-new-adopts
5345                       (or gnus-sum-thread-tree-false-root subject))
5346                      (t
5347                       (or gnus-sum-thread-tree-single-indent subject))))
5348               (t
5349                (concat (apply 'concat
5350                               (mapcar (lambda (item)
5351                                         (if (= item 1)
5352                                             gnus-sum-thread-tree-vertical
5353                                           gnus-sum-thread-tree-indent))
5354                                       (cdr (reverse tree-stack))))
5355                        (if (nth 1 thread)
5356                            gnus-sum-thread-tree-leaf-with-other
5357                          gnus-sum-thread-tree-single-leaf)))))
5358             (when (string= gnus-tmp-name "")
5359               (setq gnus-tmp-name gnus-tmp-from))
5360             (unless (numberp gnus-tmp-lines)
5361               (setq gnus-tmp-lines -1))
5362             (if (= gnus-tmp-lines -1)
5363                 (setq gnus-tmp-lines "?")
5364               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5365             (gnus-put-text-property
5366              (point)
5367              (progn (eval gnus-summary-line-format-spec) (point))
5368              'gnus-number number)
5369             (when gnus-visual-p
5370               (forward-line -1)
5371               (gnus-summary-highlight-line)
5372               (when gnus-summary-update-hook
5373                 (gnus-run-hooks 'gnus-summary-update-hook))
5374               (forward-line 1))
5375
5376             (setq gnus-tmp-prev-subject simp-subject)))
5377
5378         (when (nth 1 thread)
5379           (push (list (max 0 gnus-tmp-level)
5380                       (copy-sequence tree-stack)
5381                       (nthcdr 1 thread))
5382                 stack))
5383         (push (if (nth 1 thread) 1 0) tree-stack)
5384         (incf gnus-tmp-level)
5385         (setq threads (if thread-end nil (cdar thread)))
5386         (if gnus-summary-display-while-building
5387             (if building-count
5388                 (progn
5389                   ;; use a set frequency
5390                   (setq building-line-count (1- building-line-count))
5391                   (when (= building-line-count 0)
5392                     (sit-for 0)
5393                     (setq building-line-count
5394                           gnus-summary-display-while-building)))
5395               ;; always
5396               (sit-for 0)))
5397         (unless threads
5398           (setq gnus-tmp-level 0)))))
5399   (gnus-message 7 "Generating summary...done"))
5400
5401 (defun gnus-summary-prepare-unthreaded (headers)
5402   "Generate an unthreaded summary buffer based on HEADERS."
5403   (let (header number mark)
5404
5405     (beginning-of-line)
5406
5407     (while headers
5408       ;; We may have to root out some bad articles...
5409       (when (memq (setq number (mail-header-number
5410                                 (setq header (pop headers))))
5411                   gnus-newsgroup-limit)
5412         ;; Mark article as read when it has a low score.
5413         (when (and gnus-summary-mark-below
5414                    (< (or (cdr (assq number gnus-newsgroup-scored))
5415                           gnus-summary-default-score 0)
5416                       gnus-summary-mark-below)
5417                    (not (gnus-summary-article-ancient-p number)))
5418           (setq gnus-newsgroup-unreads
5419                 (delq number gnus-newsgroup-unreads))
5420           (if gnus-newsgroup-auto-expire
5421               (push number gnus-newsgroup-expirable)
5422             (push (cons number gnus-low-score-mark)
5423                   gnus-newsgroup-reads)))
5424
5425         (setq mark (gnus-article-mark number))
5426         (push (gnus-data-make number mark (1+ (point)) header 0)
5427               gnus-newsgroup-data)
5428         (gnus-summary-insert-line
5429          header 0 number
5430          (memq number gnus-newsgroup-undownloaded)
5431          mark (memq number gnus-newsgroup-replied)
5432          (memq number gnus-newsgroup-expirable)
5433          (mail-header-subject header) nil
5434          (cdr (assq number gnus-newsgroup-scored))
5435          (memq number gnus-newsgroup-processable))))))
5436
5437 (defun gnus-summary-remove-list-identifiers ()
5438   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5439   (let ((regexp (if (consp gnus-list-identifiers)
5440                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5441                   gnus-list-identifiers))
5442         changed subject)
5443     (when regexp
5444       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5445       (dolist (header gnus-newsgroup-headers)
5446         (setq subject (mail-header-subject header)
5447               changed nil)
5448         (while (string-match regexp subject)
5449           (setq subject
5450                 (concat (substring subject 0 (match-beginning 1))
5451                         (substring subject (match-end 0)))
5452                 changed t))
5453         (when changed
5454           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5455             (setq subject
5456                   (concat (substring subject 0 (match-beginning 1))
5457                           (substring subject (match-end 1)))))
5458           (mail-header-set-subject header subject))))))
5459
5460 (defun gnus-fetch-headers (articles)
5461   "Fetch headers of ARTICLES."
5462   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5463     (gnus-message 5 "Fetching headers for %s..." name)
5464     (prog1
5465         (if (eq 'nov
5466                 (setq gnus-headers-retrieved-by
5467                       (gnus-retrieve-headers
5468                        articles gnus-newsgroup-name
5469                        ;; We might want to fetch old headers, but
5470                        ;; not if there is only 1 article.
5471                        (and (or (and
5472                                  (not (eq gnus-fetch-old-headers 'some))
5473                                  (not (numberp gnus-fetch-old-headers)))
5474                                 (> (length articles) 1))
5475                             gnus-fetch-old-headers))))
5476             (gnus-get-newsgroup-headers-xover
5477              articles nil nil gnus-newsgroup-name t)
5478           (gnus-get-newsgroup-headers))
5479       (gnus-message 5 "Fetching headers for %s...done" name))))
5480
5481 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5482   "Select newsgroup GROUP.
5483 If READ-ALL is non-nil, all articles in the group are selected.
5484 If SELECT-ARTICLES, only select those articles from GROUP."
5485   (let* ((entry (gnus-group-entry group))
5486          ;;!!! Dirty hack; should be removed.
5487          (gnus-summary-ignore-duplicates
5488           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5489               t
5490             gnus-summary-ignore-duplicates))
5491          (info (nth 2 entry))
5492          charset articles fetched-articles cached)
5493
5494     (unless (gnus-check-server
5495              (set (make-local-variable 'gnus-current-select-method)
5496                   (gnus-find-method-for-group group)))
5497       (error "Couldn't open server"))
5498     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5499
5500     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5501         (gnus-activate-group group)     ; Or we can activate it...
5502         (progn                          ; Or we bug out.
5503           (when (equal major-mode 'gnus-summary-mode)
5504             (gnus-kill-buffer (current-buffer)))
5505           (error
5506            "Couldn't activate group %s: %s"
5507            (mm-decode-coding-string group charset)
5508            (mm-decode-coding-string (gnus-status-message group) charset))))
5509
5510     (unless (gnus-request-group group t)
5511       (when (equal major-mode 'gnus-summary-mode)
5512         (gnus-kill-buffer (current-buffer)))
5513       (error "Couldn't request group %s: %s"
5514              (mm-decode-coding-string group charset)
5515              (mm-decode-coding-string (gnus-status-message group) charset)))
5516
5517     (when gnus-agent
5518       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5519
5520       (setq gnus-summary-use-undownloaded-faces
5521             (gnus-agent-find-parameter
5522              group
5523              'agent-enable-undownloaded-faces)))
5524
5525     (setq gnus-newsgroup-name group
5526           gnus-newsgroup-unselected nil
5527           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5528
5529     (let ((display (gnus-group-find-parameter group 'display)))
5530       (setq gnus-newsgroup-display
5531             (cond
5532              ((not (zerop (or (car-safe read-all) 0)))
5533               ;; The user entered the group with C-u SPC/RET, let's show
5534               ;; all articles.
5535               'gnus-not-ignore)
5536              ((eq display 'all)
5537               'gnus-not-ignore)
5538              ((arrayp display)
5539               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5540              ((numberp display)
5541               ;; The following is probably the "correct" solution, but
5542               ;; it makes Gnus fetch all headers and then limit the
5543               ;; articles (which is slow), so instead we hack the
5544               ;; select-articles parameter instead. -- Simon Josefsson
5545               ;; <jas@kth.se>
5546               ;;
5547               ;; (gnus-byte-compile
5548               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5549               ;;                         display)))))
5550               (setq select-articles
5551                     (gnus-uncompress-range
5552                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5553                              (if (> tmp 0)
5554                                  tmp
5555                                1))
5556                            (cdr (gnus-active group)))))
5557               nil)
5558              (t
5559               nil))))
5560
5561     (gnus-summary-setup-default-charset)
5562
5563     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5564     (when (gnus-virtual-group-p group)
5565       (setq cached gnus-newsgroup-cached))
5566
5567     (setq gnus-newsgroup-unreads
5568           (gnus-sorted-ndifference
5569            (gnus-sorted-ndifference gnus-newsgroup-unreads
5570                                     gnus-newsgroup-marked)
5571            gnus-newsgroup-dormant))
5572
5573     (setq gnus-newsgroup-processable nil)
5574
5575     (gnus-update-read-articles group gnus-newsgroup-unreads)
5576
5577     ;; Adjust and set lists of article marks.
5578     (when info
5579       (gnus-adjust-marked-articles info))
5580     (if (setq articles select-articles)
5581         (setq gnus-newsgroup-unselected
5582               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5583       (setq articles (gnus-articles-to-read group read-all)))
5584
5585     (cond
5586      ((null articles)
5587       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5588       'quit)
5589      ((eq articles 0) nil)
5590      (t
5591       ;; Init the dependencies hash table.
5592       (setq gnus-newsgroup-dependencies
5593             (gnus-make-hashtable (length articles)))
5594       (gnus-set-global-variables)
5595       ;; Retrieve the headers and read them in.
5596
5597       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5598
5599       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5600       (when cached
5601         (setq gnus-newsgroup-cached cached))
5602
5603       ;; Suppress duplicates?
5604       (when gnus-suppress-duplicates
5605         (gnus-dup-suppress-articles))
5606
5607       ;; Set the initial limit.
5608       (setq gnus-newsgroup-limit (copy-sequence articles))
5609       ;; Remove canceled articles from the list of unread articles.
5610       (setq fetched-articles
5611             (mapcar (lambda (headers) (mail-header-number headers))
5612                     gnus-newsgroup-headers))
5613       (setq gnus-newsgroup-articles fetched-articles)
5614       (setq gnus-newsgroup-unreads
5615             (gnus-sorted-nintersection
5616              gnus-newsgroup-unreads fetched-articles))
5617       (gnus-compute-unseen-list)
5618
5619       ;; Removed marked articles that do not exist.
5620       (gnus-update-missing-marks
5621        (gnus-sorted-difference articles fetched-articles))
5622       ;; We might want to build some more threads first.
5623       (when (and gnus-fetch-old-headers
5624                  (eq gnus-headers-retrieved-by 'nov))
5625         (if (eq gnus-fetch-old-headers 'invisible)
5626             (gnus-build-all-threads)
5627           (gnus-build-old-threads)))
5628       ;; Let the Gnus agent mark articles as read.
5629       (when gnus-agent
5630         (gnus-agent-get-undownloaded-list))
5631       ;; Remove list identifiers from subject
5632       (when gnus-list-identifiers
5633         (gnus-summary-remove-list-identifiers))
5634       ;; Check whether auto-expire is to be done in this group.
5635       (setq gnus-newsgroup-auto-expire
5636             (gnus-group-auto-expirable-p group))
5637       ;; Set up the article buffer now, if necessary.
5638       (unless (and gnus-single-article-buffer
5639                    (equal gnus-article-buffer "*Article*"))
5640         (gnus-article-setup-buffer))
5641       ;; First and last article in this newsgroup.
5642       (when gnus-newsgroup-headers
5643         (setq gnus-newsgroup-begin
5644               (mail-header-number (car gnus-newsgroup-headers))
5645               gnus-newsgroup-end
5646               (mail-header-number
5647                (gnus-last-element gnus-newsgroup-headers))))
5648       ;; GROUP is successfully selected.
5649       (or gnus-newsgroup-headers t)))))
5650
5651 (defun gnus-compute-unseen-list ()
5652   ;; The `seen' marks are treated specially.
5653   (if (not gnus-newsgroup-seen)
5654       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5655     (setq gnus-newsgroup-unseen
5656           (gnus-inverse-list-range-intersection
5657            gnus-newsgroup-articles gnus-newsgroup-seen))))
5658
5659 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5660
5661 (defun gnus-summary-display-make-predicate (display)
5662   (require 'gnus-agent)
5663   (when (= (length display) 1)
5664     (setq display (car display)))
5665   (unless gnus-summary-display-cache
5666     (dolist (elem (append '((unread . unread)
5667                             (read . read)
5668                             (unseen . unseen))
5669                           gnus-article-mark-lists))
5670       (push (cons (cdr elem)
5671                   (gnus-byte-compile
5672                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5673             gnus-summary-display-cache)))
5674   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5675         (gnus-category-predicate-cache gnus-summary-display-cache))
5676     (gnus-get-predicate display)))
5677
5678 ;; Uses the dynamically bound `number' variable.
5679 (defvar number)
5680 (defun gnus-article-marked-p (type &optional article)
5681   (let ((article (or article number)))
5682     (cond
5683      ((eq type 'tick)
5684       (memq article gnus-newsgroup-marked))
5685      ((eq type 'spam)
5686       (memq article gnus-newsgroup-spam-marked))
5687      ((eq type 'unsend)
5688       (memq article gnus-newsgroup-unsendable))
5689      ((eq type 'undownload)
5690       (memq article gnus-newsgroup-undownloaded))
5691      ((eq type 'download)
5692       (memq article gnus-newsgroup-downloadable))
5693      ((eq type 'unread)
5694       (memq article gnus-newsgroup-unreads))
5695      ((eq type 'read)
5696       (memq article gnus-newsgroup-reads))
5697      ((eq type 'dormant)
5698       (memq article gnus-newsgroup-dormant) )
5699      ((eq type 'expire)
5700       (memq article gnus-newsgroup-expirable))
5701      ((eq type 'reply)
5702       (memq article gnus-newsgroup-replied))
5703      ((eq type 'killed)
5704       (memq article gnus-newsgroup-killed))
5705      ((eq type 'bookmark)
5706       (assq article gnus-newsgroup-bookmarks))
5707      ((eq type 'score)
5708       (assq article gnus-newsgroup-scored))
5709      ((eq type 'save)
5710       (memq article gnus-newsgroup-saved))
5711      ((eq type 'cache)
5712       (memq article gnus-newsgroup-cached))
5713      ((eq type 'forward)
5714       (memq article gnus-newsgroup-forwarded))
5715      ((eq type 'seen)
5716       (not (memq article gnus-newsgroup-unseen)))
5717      ((eq type 'recent)
5718       (memq article gnus-newsgroup-recent))
5719      (t t))))
5720
5721 (defun gnus-articles-to-read (group &optional read-all)
5722   "Find out what articles the user wants to read."
5723   (let* ((articles
5724           ;; Select all articles if `read-all' is non-nil, or if there
5725           ;; are no unread articles.
5726           (if (or read-all
5727                   (and (zerop (length gnus-newsgroup-marked))
5728                        (zerop (length gnus-newsgroup-unreads)))
5729                   ;; Fetch all if the predicate is non-nil.
5730                   gnus-newsgroup-display)
5731               ;; We want to select the headers for all the articles in
5732               ;; the group, so we select either all the active
5733               ;; articles in the group, or (if that's nil), the
5734               ;; articles in the cache.
5735               (or
5736                (if gnus-newsgroup-maximum-articles
5737                    (let ((active (gnus-active group)))
5738                      (gnus-uncompress-range
5739                       (cons (max (car active)
5740                                  (- (cdr active)
5741                                     gnus-newsgroup-maximum-articles
5742                                     -1))
5743                             (cdr active))))
5744                  (gnus-uncompress-range (gnus-active group)))
5745                (gnus-cache-articles-in-group group))
5746             ;; Select only the "normal" subset of articles.
5747             (gnus-sorted-nunion
5748              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5749              gnus-newsgroup-unreads)))
5750          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5751          (scored (length scored-list))
5752          (number (length articles))
5753          (marked (+ (length gnus-newsgroup-marked)
5754                     (length gnus-newsgroup-dormant)))
5755          (select
5756           (cond
5757            ((numberp read-all)
5758             read-all)
5759            ((numberp gnus-newsgroup-display)
5760             gnus-newsgroup-display)
5761            (t
5762             (condition-case ()
5763                 (cond
5764                  ((and (or (<= scored marked) (= scored number))
5765                        (numberp gnus-large-newsgroup)
5766                        (> number gnus-large-newsgroup))
5767                   (let* ((cursor-in-echo-area nil)
5768                          (initial (gnus-parameter-large-newsgroup-initial
5769                                    gnus-newsgroup-name))
5770                          (input
5771                           (read-string
5772                            (format
5773                             "How many articles from %s (%s %d): "
5774                             (gnus-group-decoded-name gnus-newsgroup-name)
5775                             (if initial "max" "default")
5776                             number)
5777                            (if initial
5778                                (cons (number-to-string initial)
5779                                      0)))))
5780                     (if (string-match "^[ \t]*$" input) number input)))
5781                  ((and (> scored marked) (< scored number)
5782                        (> (- scored number) 20))
5783                   (let ((input
5784                          (read-string
5785                           (format "%s %s (%d scored, %d total): "
5786                                   "How many articles from"
5787                                   (gnus-group-decoded-name group)
5788                                   scored number))))
5789                     (if (string-match "^[ \t]*$" input)
5790                         number input)))
5791                  (t number))
5792               (quit
5793                (message "Quit getting the articles to read")
5794                nil))))))
5795     (setq select (if (stringp select) (string-to-number select) select))
5796     (if (or (null select) (zerop select))
5797         select
5798       (if (and (not (zerop scored)) (<= (abs select) scored))
5799           (progn
5800             (setq articles (sort scored-list '<))
5801             (setq number (length articles)))
5802         (setq articles (copy-sequence articles)))
5803
5804       (when (< (abs select) number)
5805         (if (< select 0)
5806             ;; Select the N oldest articles.
5807             (setcdr (nthcdr (1- (abs select)) articles) nil)
5808           ;; Select the N most recent articles.
5809           (setq articles (nthcdr (- number select) articles))))
5810       (setq gnus-newsgroup-unselected
5811             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5812       (when gnus-alter-articles-to-read-function
5813         (setq articles
5814               (sort
5815                (funcall gnus-alter-articles-to-read-function
5816                         gnus-newsgroup-name articles)
5817                '<)))
5818       articles)))
5819
5820 (defun gnus-killed-articles (killed articles)
5821   (let (out)
5822     (while articles
5823       (when (inline (gnus-member-of-range (car articles) killed))
5824         (push (car articles) out))
5825       (setq articles (cdr articles)))
5826     out))
5827
5828 (defun gnus-uncompress-marks (marks)
5829   "Uncompress the mark ranges in MARKS."
5830   (let ((uncompressed '(score bookmark))
5831         out)
5832     (while marks
5833       (if (memq (caar marks) uncompressed)
5834           (push (car marks) out)
5835         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5836       (setq marks (cdr marks)))
5837     out))
5838
5839 (defun gnus-article-mark-to-type (mark)
5840   "Return the type of MARK."
5841   (or (cadr (assq mark gnus-article-special-mark-lists))
5842       'list))
5843
5844 (defun gnus-article-unpropagatable-p (mark)
5845   "Return whether MARK should be propagated to back end."
5846   (memq mark gnus-article-unpropagated-mark-lists))
5847
5848 (defun gnus-adjust-marked-articles (info)
5849   "Set all article lists and remove all marks that are no longer valid."
5850   (let* ((marked-lists (gnus-info-marks info))
5851          (active (gnus-active (gnus-info-group info)))
5852          (min (car active))
5853          (max (cdr active))
5854          (types gnus-article-mark-lists)
5855          marks var articles article mark mark-type
5856          bgn end)
5857     ;; Hack to avoid adjusting marks for imap.
5858     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5859               'nnimap)
5860       (setq min 1))
5861
5862     (dolist (marks marked-lists)
5863       (setq mark (car marks)
5864             mark-type (gnus-article-mark-to-type mark)
5865             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5866
5867       ;; We set the variable according to the type of the marks list,
5868       ;; and then adjust the marks to a subset of the active articles.
5869       (cond
5870        ;; Adjust "simple" lists - compressed yet unsorted
5871        ((eq mark-type 'list)
5872         ;; Simultaneously uncompress and clip to active range
5873         ;; See gnus-uncompress-range for a description of possible marks
5874         (let (l lh)
5875           (if (not (cadr marks))
5876               (set var nil)
5877             (setq articles (if (numberp (cddr marks))
5878                                (list (cdr marks))
5879                              (cdr marks))
5880                   lh (cons nil nil)
5881                   l lh)
5882
5883             (while (setq article (pop articles))
5884               (cond ((consp article)
5885                      (setq bgn (max (car article) min)
5886                            end (min (cdr article) max))
5887                      (while (<= bgn end)
5888                        (setq l (setcdr l (cons bgn nil))
5889                              bgn (1+ bgn))))
5890                     ((and (<= min article)
5891                           (>= max article))
5892                      (setq l (setcdr l (cons article nil))))))
5893             (set var (cdr lh)))))
5894        ;; Adjust assocs.
5895        ((eq mark-type 'tuple)
5896         (set var (setq articles (cdr marks)))
5897         (when (not (listp (cdr (symbol-value var))))
5898           (set var (list (symbol-value var))))
5899         (when (not (listp (cdr articles)))
5900           (setq articles (list articles)))
5901         (while articles
5902           (when (or (not (consp (setq article (pop articles))))
5903                     (< (car article) min)
5904                     (> (car article) max))
5905             (set var (delq article (symbol-value var))))))
5906        ;; Adjust ranges (sloppily).
5907        ((eq mark-type 'range)
5908         (cond
5909          ((eq mark 'seen)
5910           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5911           ;; It should be (seen (NUM1 . NUM2)).
5912           (when (numberp (cddr marks))
5913             (setcdr marks (list (cdr marks))))
5914           (setq articles (cdr marks))
5915           (while (and articles
5916                       (or (and (consp (car articles))
5917                                (> min (cdar articles)))
5918                           (and (numberp (car articles))
5919                                (> min (car articles)))))
5920             (pop articles))
5921           (set var articles))))))))
5922
5923 (defun gnus-update-missing-marks (missing)
5924   "Go through the list of MISSING articles and remove them from the mark lists."
5925   (when missing
5926     (let (var m)
5927       ;; Go through all types.
5928       (dolist (elem gnus-article-mark-lists)
5929         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5930           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5931           (when (symbol-value var)
5932             ;; This list has articles.  So we delete all missing
5933             ;; articles from it.
5934             (setq m missing)
5935             (while m
5936               (set var (delq (pop m) (symbol-value var))))))))))
5937
5938 (defun gnus-update-marks ()
5939   "Enter the various lists of marked articles into the newsgroup info list."
5940   (let ((types gnus-article-mark-lists)
5941         (info (gnus-get-info gnus-newsgroup-name))
5942         type list newmarked symbol delta-marks)
5943     (when info
5944       ;; Add all marks lists to the list of marks lists.
5945       (while (setq type (pop types))
5946         (setq list (symbol-value
5947                     (setq symbol
5948                           (intern (format "gnus-newsgroup-%s" (car type))))))
5949
5950         (when list
5951           ;; Get rid of the entries of the articles that have the
5952           ;; default score.
5953           (when (and (eq (cdr type) 'score)
5954                      gnus-save-score
5955                      list)
5956             (let* ((arts list)
5957                    (prev (cons nil list))
5958                    (all prev))
5959               (while arts
5960                 (if (or (not (consp (car arts)))
5961                         (= (cdar arts) gnus-summary-default-score))
5962                     (setcdr prev (cdr arts))
5963                   (setq prev arts))
5964                 (setq arts (cdr arts)))
5965               (setq list (cdr all)))))
5966
5967         (when (eq (cdr type) 'seen)
5968           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5969
5970         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5971           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5972
5973         (when (and (gnus-check-backend-function
5974                     'request-set-mark gnus-newsgroup-name)
5975                    (not (gnus-article-unpropagatable-p (cdr type))))
5976           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5977                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5978                  (add (gnus-remove-from-range
5979                        (gnus-copy-sequence list) old)))
5980             (when add
5981               (push (list add 'add (list (cdr type))) delta-marks))
5982             (when del
5983               ;; Don't delete marks from outside the active range.  This
5984               ;; shouldn't happen, but is a sanity check.
5985               (setq del (gnus-sorted-range-intersection
5986                          (gnus-active gnus-newsgroup-name) del))
5987               (push (list del 'del (list (cdr type))) delta-marks))))
5988
5989         (when list
5990           (push (cons (cdr type) list) newmarked)))
5991
5992       (when delta-marks
5993         (unless (gnus-check-group gnus-newsgroup-name)
5994           (error "Can't open server for %s" gnus-newsgroup-name))
5995         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5996
5997       ;; Enter these new marks into the info of the group.
5998       (if (nthcdr 3 info)
5999           (setcar (nthcdr 3 info) newmarked)
6000         ;; Add the marks lists to the end of the info.
6001         (when newmarked
6002           (setcdr (nthcdr 2 info) (list newmarked))))
6003
6004       ;; Cut off the end of the info if there's nothing else there.
6005       (let ((i 5))
6006         (while (and (> i 2)
6007                     (not (nth i info)))
6008           (when (nthcdr (decf i) info)
6009             (setcdr (nthcdr i info) nil)))))))
6010
6011 (defun gnus-set-mode-line (where)
6012   "Set the mode line of the article or summary buffers.
6013 If WHERE is `summary', the summary mode line format will be used."
6014   ;; Is this mode line one we keep updated?
6015   (when (and (memq where gnus-updated-mode-lines)
6016              (symbol-value
6017               (intern (format "gnus-%s-mode-line-format-spec" where))))
6018     (let (mode-string)
6019       ;; We evaluate this in the summary buffer since these
6020       ;; variables are buffer-local to that buffer.
6021       (with-current-buffer gnus-summary-buffer
6022         ;; We bind all these variables that are used in the `eval' form
6023         ;; below.
6024         (let* ((mformat (symbol-value
6025                          (intern
6026                           (format "gnus-%s-mode-line-format-spec" where))))
6027                (gnus-tmp-group-name (gnus-mode-string-quote
6028                                      (gnus-group-decoded-name
6029                                       gnus-newsgroup-name)))
6030                (gnus-tmp-article-number (or gnus-current-article 0))
6031                (gnus-tmp-unread gnus-newsgroup-unreads)
6032                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6033                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6034                (gnus-tmp-unread-and-unselected
6035                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6036                             (zerop gnus-tmp-unselected))
6037                        "")
6038                       ((zerop gnus-tmp-unselected)
6039                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6040                       (t (format "{%d(+%d) more}"
6041                                  gnus-tmp-unread-and-unticked
6042                                  gnus-tmp-unselected))))
6043                (gnus-tmp-subject
6044                 (if (and gnus-current-headers
6045                          (vectorp gnus-current-headers))
6046                     (gnus-mode-string-quote
6047                      (mail-header-subject gnus-current-headers))
6048                   ""))
6049                bufname-length max-len
6050                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6051           (setq mode-string (eval mformat))
6052           (setq bufname-length (if (string-match "%b" mode-string)
6053                                    (- (length
6054                                        (buffer-name
6055                                         (if (eq where 'summary)
6056                                             nil
6057                                           (get-buffer gnus-article-buffer))))
6058                                       2)
6059                                  0))
6060           (setq max-len (max 4 (if gnus-mode-non-string-length
6061                                    (- (window-width)
6062                                       gnus-mode-non-string-length
6063                                       bufname-length)
6064                                  (length mode-string))))
6065           ;; We might have to chop a bit of the string off...
6066           (when (> (length mode-string) max-len)
6067             (setq mode-string
6068                   (concat (truncate-string-to-width mode-string (- max-len 3))
6069                           "...")))))
6070       ;; Update the mode line.
6071       (setq mode-line-buffer-identification
6072             (gnus-mode-line-buffer-identification (list mode-string)))
6073       (set-buffer-modified-p t))))
6074
6075 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6076   "Go through the HEADERS list and add all Xrefs to a hash table.
6077 The resulting hash table is returned, or nil if no Xrefs were found."
6078   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6079          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6080          (xref-hashtb (gnus-make-hashtable))
6081          start group entry number xrefs header)
6082     (while headers
6083       (setq header (pop headers))
6084       (when (and (setq xrefs (mail-header-xref header))
6085                  (not (memq (setq number (mail-header-number header))
6086                             unreads)))
6087         (setq start 0)
6088         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6089           (setq start (match-end 0))
6090           (setq group (if prefix
6091                           (concat prefix (substring xrefs (match-beginning 1)
6092                                                     (match-end 1)))
6093                         (substring xrefs (match-beginning 1) (match-end 1))))
6094           (setq number
6095                 (string-to-number (substring xrefs (match-beginning 2)
6096                                           (match-end 2))))
6097           (if (setq entry (gnus-gethash group xref-hashtb))
6098               (setcdr entry (cons number (cdr entry)))
6099             (gnus-sethash group (cons number nil) xref-hashtb)))))
6100     (and start xref-hashtb)))
6101
6102 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6103   "Look through all the headers and mark the Xrefs as read."
6104   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6105         name info xref-hashtb idlist method nth4)
6106     (with-current-buffer gnus-group-buffer
6107       (when (setq xref-hashtb
6108                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6109         (mapatoms
6110          (lambda (group)
6111            (unless (string= from-newsgroup (setq name (symbol-name group)))
6112              (setq idlist (symbol-value group))
6113              ;; Dead groups are not updated.
6114              (and (prog1
6115                       (setq info (gnus-get-info name))
6116                     (when (stringp (setq nth4 (gnus-info-method info)))
6117                       (setq nth4 (gnus-server-to-method nth4))))
6118                   ;; Only do the xrefs if the group has the same
6119                   ;; select method as the group we have just read.
6120                   (or (gnus-methods-equal-p
6121                        nth4 (gnus-find-method-for-group from-newsgroup))
6122                       virtual
6123                       (equal nth4 (setq method (gnus-find-method-for-group
6124                                                 from-newsgroup)))
6125                       (and (equal (car nth4) (car method))
6126                            (equal (nth 1 nth4) (nth 1 method))))
6127                   gnus-use-cross-reference
6128                   (or (not (eq gnus-use-cross-reference t))
6129                       virtual
6130                       ;; Only do cross-references on subscribed
6131                       ;; groups, if that is what is wanted.
6132                       (<= (gnus-info-level info) gnus-level-subscribed))
6133                   (gnus-group-make-articles-read name idlist))))
6134          xref-hashtb)))))
6135
6136 (defun gnus-compute-read-articles (group articles)
6137   (let* ((entry (gnus-group-entry group))
6138          (info (nth 2 entry))
6139          (active (gnus-active group))
6140          ninfo)
6141     (when entry
6142       ;; First peel off all invalid article numbers.
6143       (when active
6144         (let ((ids articles)
6145               id first)
6146           (while (setq id (pop ids))
6147             (when (and first (> id (cdr active)))
6148               ;; We'll end up in this situation in one particular
6149               ;; obscure situation.  If you re-scan a group and get
6150               ;; a new article that is cross-posted to a different
6151               ;; group that has not been re-scanned, you might get
6152               ;; crossposted article that has a higher number than
6153               ;; Gnus believes possible.  So we re-activate this
6154               ;; group as well.  This might mean doing the
6155               ;; crossposting thingy will *increase* the number
6156               ;; of articles in some groups.  Tsk, tsk.
6157               (setq active (or (gnus-activate-group group) active)))
6158             (when (or (> id (cdr active))
6159                       (< id (car active)))
6160               (setq articles (delq id articles))))))
6161       ;; If the read list is nil, we init it.
6162       (if (and active
6163                (null (gnus-info-read info))
6164                (> (car active) 1))
6165           (setq ninfo (cons 1 (1- (car active))))
6166         (setq ninfo (gnus-info-read info)))
6167       ;; Then we add the read articles to the range.
6168       (gnus-add-to-range
6169        ninfo (setq articles (sort articles '<))))))
6170
6171 (defun gnus-group-make-articles-read (group articles)
6172   "Update the info of GROUP to say that ARTICLES are read."
6173   (let* ((num 0)
6174          (entry (gnus-group-entry group))
6175          (info (nth 2 entry))
6176          (active (gnus-active group))
6177          range)
6178     (when entry
6179       (setq range (gnus-compute-read-articles group articles))
6180       (with-current-buffer gnus-group-buffer
6181         (gnus-undo-register
6182           `(progn
6183              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6184              (gnus-info-set-read ',info ',(gnus-info-read info))
6185              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6186              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6187              (gnus-group-update-group ,group t))))
6188       ;; Add the read articles to the range.
6189       (gnus-info-set-read info range)
6190       (gnus-request-set-mark group (list (list range 'add '(read))))
6191       ;; Then we have to re-compute how many unread
6192       ;; articles there are in this group.
6193       (when active
6194         (cond
6195          ((not range)
6196           (setq num (- (1+ (cdr active)) (car active))))
6197          ((not (listp (cdr range)))
6198           (setq num (- (cdr active) (- (1+ (cdr range))
6199                                        (car range)))))
6200          (t
6201           (while range
6202             (if (numberp (car range))
6203                 (setq num (1+ num))
6204               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6205             (setq range (cdr range)))
6206           (setq num (- (cdr active) num))))
6207         ;; Update the number of unread articles.
6208         (setcar entry num)
6209         ;; Update the group buffer.
6210         (unless (gnus-ephemeral-group-p group)
6211           (gnus-group-update-group group t))))))
6212
6213 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6214   (let ((cur nntp-server-buffer)
6215         (dependencies
6216          (or dependencies
6217              (with-current-buffer gnus-summary-buffer
6218                gnus-newsgroup-dependencies)))
6219         headers id end ref number
6220         (mail-parse-charset gnus-newsgroup-charset)
6221         (mail-parse-ignored-charsets
6222          (save-current-buffer (condition-case nil
6223                                   (set-buffer gnus-summary-buffer)
6224                                 (error))
6225                               gnus-newsgroup-ignored-charsets)))
6226     (with-current-buffer nntp-server-buffer
6227       ;; Translate all TAB characters into SPACE characters.
6228       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6229       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6230       (ietf-drums-unfold-fws)
6231       (gnus-run-hooks 'gnus-parse-headers-hook)
6232       (let ((case-fold-search t)
6233             in-reply-to header p lines chars)
6234         (goto-char (point-min))
6235         ;; Search to the beginning of the next header.  Error messages
6236         ;; do not begin with 2 or 3.
6237         (while (re-search-forward "^[23][0-9]+ " nil t)
6238           (setq id nil
6239                 ref nil)
6240           ;; This implementation of this function, with nine
6241           ;; search-forwards instead of the one re-search-forward and
6242           ;; a case (which basically was the old function) is actually
6243           ;; about twice as fast, even though it looks messier.  You
6244           ;; can't have everything, I guess.  Speed and elegance
6245           ;; doesn't always go hand in hand.
6246           (setq
6247            header
6248            (vector
6249             ;; Number.
6250             (prog1
6251                 (setq number (read cur))
6252               (end-of-line)
6253               (setq p (point))
6254               (narrow-to-region (point)
6255                                 (or (and (search-forward "\n.\n" nil t)
6256                                          (- (point) 2))
6257                                     (point))))
6258             ;; Subject.
6259             (progn
6260               (goto-char p)
6261               (if (search-forward "\nsubject:" nil t)
6262                   (funcall gnus-decode-encoded-word-function
6263                            (nnheader-header-value))
6264                 "(none)"))
6265             ;; From.
6266             (progn
6267               (goto-char p)
6268               (if (search-forward "\nfrom:" nil t)
6269                   (funcall gnus-decode-encoded-address-function
6270                            (nnheader-header-value))
6271                 "(nobody)"))
6272             ;; Date.
6273             (progn
6274               (goto-char p)
6275               (if (search-forward "\ndate:" nil t)
6276                   (nnheader-header-value) ""))
6277             ;; Message-ID.
6278             (progn
6279               (goto-char p)
6280               (setq id (if (re-search-forward
6281                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6282                            ;; We do it this way to make sure the Message-ID
6283                            ;; is (somewhat) syntactically valid.
6284                            (buffer-substring (match-beginning 1)
6285                                              (match-end 1))
6286                          ;; If there was no message-id, we just fake one
6287                          ;; to make subsequent routines simpler.
6288                          (nnheader-generate-fake-message-id number))))
6289             ;; References.
6290             (progn
6291               (goto-char p)
6292               (if (search-forward "\nreferences:" nil t)
6293                   (progn
6294                     (setq end (point))
6295                     (prog1
6296                         (nnheader-header-value)
6297                       (setq ref
6298                             (buffer-substring
6299                              (progn
6300                                (end-of-line)
6301                                (search-backward ">" end t)
6302                                (1+ (point)))
6303                              (progn
6304                                (search-backward "<" end t)
6305                                (point))))))
6306                 ;; Get the references from the in-reply-to header if there
6307                 ;; were no references and the in-reply-to header looks
6308                 ;; promising.
6309                 (if (and (search-forward "\nin-reply-to:" nil t)
6310                          (setq in-reply-to (nnheader-header-value))
6311                          (string-match "<[^>]+>" in-reply-to))
6312                     (let (ref2)
6313                       (setq ref (substring in-reply-to (match-beginning 0)
6314                                            (match-end 0)))
6315                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6316                         (setq ref2 (substring in-reply-to (match-beginning 0)
6317                                               (match-end 0)))
6318                         (when (> (length ref2) (length ref))
6319                           (setq ref ref2)))
6320                       ref)
6321                   (setq ref nil))))
6322             ;; Chars.
6323             (progn
6324               (goto-char p)
6325               (if (search-forward "\nchars: " nil t)
6326                   (if (numberp (setq chars (ignore-errors (read cur))))
6327                       chars -1)
6328                 -1))
6329             ;; Lines.
6330             (progn
6331               (goto-char p)
6332               (if (search-forward "\nlines: " nil t)
6333                   (if (numberp (setq lines (ignore-errors (read cur))))
6334                       lines -1)
6335                 -1))
6336             ;; Xref.
6337             (progn
6338               (goto-char p)
6339               (and (search-forward "\nxref:" nil t)
6340                    (nnheader-header-value)))
6341             ;; Extra.
6342             (when gnus-extra-headers
6343               (let ((extra gnus-extra-headers)
6344                     out)
6345                 (while extra
6346                   (goto-char p)
6347                   (when (search-forward
6348                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6349                     (push (cons (car extra) (nnheader-header-value))
6350                           out))
6351                   (pop extra))
6352                 out))))
6353           (when (equal id ref)
6354             (setq ref nil))
6355
6356           (when gnus-alter-header-function
6357             (funcall gnus-alter-header-function header)
6358             (setq id (mail-header-id header)
6359                   ref (gnus-parent-id (mail-header-references header))))
6360
6361           (when (setq header
6362                       (gnus-dependencies-add-header
6363                        header dependencies force-new))
6364             (push header headers))
6365           (goto-char (point-max))
6366           (widen))
6367         (nreverse headers)))))
6368
6369 ;; Goes through the xover lines and returns a list of vectors
6370 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6371                                                   force-new dependencies
6372                                                   group also-fetch-heads)
6373   "Parse the news overview data in the server buffer.
6374 Return a list of headers that match SEQUENCE (see
6375 `nntp-retrieve-headers')."
6376   ;; Get the Xref when the users reads the articles since most/some
6377   ;; NNTP servers do not include Xrefs when using XOVER.
6378   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6379   (let ((mail-parse-charset gnus-newsgroup-charset)
6380         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6381         (cur nntp-server-buffer)
6382         (dependencies (or dependencies gnus-newsgroup-dependencies))
6383         (allp (cond
6384                ((eq gnus-read-all-available-headers t)
6385                 t)
6386                ((and (stringp gnus-read-all-available-headers)
6387                      group)
6388                 (string-match gnus-read-all-available-headers group))
6389                (t
6390                 nil)))
6391         number headers header)
6392     (with-current-buffer nntp-server-buffer
6393       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6394       ;; Allow the user to mangle the headers before parsing them.
6395       (gnus-run-hooks 'gnus-parse-headers-hook)
6396       (goto-char (point-min))
6397       (gnus-parse-without-error
6398         (while (and (or sequence allp)
6399                     (not (eobp)))
6400           (setq number (read cur))
6401           (when (not allp)
6402             (while (and sequence
6403                         (< (car sequence) number))
6404               (setq sequence (cdr sequence))))
6405           (when (and (or allp
6406                          (and sequence
6407                               (eq number (car sequence))))
6408                      (progn
6409                        (setq sequence (cdr sequence))
6410                        (setq header (inline
6411                                       (gnus-nov-parse-line
6412                                        number dependencies force-new)))))
6413             (push header headers))
6414           (forward-line 1)))
6415       ;; A common bug in inn is that if you have posted an article and
6416       ;; then retrieves the active file, it will answer correctly --
6417       ;; the new article is included.  However, a NOV entry for the
6418       ;; article may not have been generated yet, so this may fail.
6419       ;; We work around this problem by retrieving the last few
6420       ;; headers using HEAD.
6421       (if (or (not also-fetch-heads)
6422               (not sequence))
6423           ;; We (probably) got all the headers.
6424           (nreverse headers)
6425         (let ((gnus-nov-is-evil t))
6426           (nconc
6427            (nreverse headers)
6428            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6429              (gnus-get-newsgroup-headers))))))))
6430
6431 (defun gnus-article-get-xrefs ()
6432   "Fill in the Xref value in `gnus-current-headers', if necessary.
6433 This is meant to be called in `gnus-article-internal-prepare-hook'."
6434   (let ((headers (with-current-buffer gnus-summary-buffer
6435                    gnus-current-headers)))
6436     (or (not gnus-use-cross-reference)
6437         (not headers)
6438         (and (mail-header-xref headers)
6439              (not (string= (mail-header-xref headers) "")))
6440         (let ((case-fold-search t)
6441               xref)
6442           (save-restriction
6443             (nnheader-narrow-to-headers)
6444             (goto-char (point-min))
6445             (when (or (and (not (eobp))
6446                            (eq (downcase (char-after)) ?x)
6447                            (looking-at "Xref:"))
6448                       (search-forward "\nXref:" nil t))
6449               (goto-char (1+ (match-end 0)))
6450               (setq xref (buffer-substring (point) (point-at-eol)))
6451               (mail-header-set-xref headers xref)))))))
6452
6453 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6454   "Find article ID and insert the summary line for that article.
6455 OLD-HEADER can either be a header or a line number to insert
6456 the subject line on."
6457   (let* ((line (and (numberp old-header) old-header))
6458          (old-header (and (vectorp old-header) old-header))
6459          (header (cond ((and old-header use-old-header)
6460                         old-header)
6461                        ((and (numberp id)
6462                              (gnus-number-to-header id))
6463                         (gnus-number-to-header id))
6464                        (t
6465                         (gnus-read-header id))))
6466          (number (and (numberp id) id))
6467          d)
6468     (when header
6469       ;; Rebuild the thread that this article is part of and go to the
6470       ;; article we have fetched.
6471       (when (and (not gnus-show-threads)
6472                  old-header)
6473         (when (and number
6474                    (setq d (gnus-data-find (mail-header-number old-header))))
6475           (goto-char (gnus-data-pos d))
6476           (gnus-data-remove
6477            number
6478            (- (point-at-bol)
6479               (prog1
6480                   (1+ (point-at-eol))
6481                 (gnus-delete-line))))))
6482       ;; Remove list identifiers from subject.
6483       (when gnus-list-identifiers
6484         (let ((gnus-newsgroup-headers (list header)))
6485           (gnus-summary-remove-list-identifiers)))
6486       (when old-header
6487         (mail-header-set-number header (mail-header-number old-header)))
6488       (setq gnus-newsgroup-sparse
6489             (delq (setq number (mail-header-number header))
6490                   gnus-newsgroup-sparse))
6491       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6492       (push number gnus-newsgroup-limit)
6493       (gnus-rebuild-thread (mail-header-id header) line)
6494       (gnus-summary-goto-subject number nil t))
6495     (when (and (numberp number)
6496                (> number 0))
6497       ;; We have to update the boundaries even if we can't fetch the
6498       ;; article if ID is a number -- so that the next `P' or `N'
6499       ;; command will fetch the previous (or next) article even
6500       ;; if the one we tried to fetch this time has been canceled.
6501       (when (> number gnus-newsgroup-end)
6502         (setq gnus-newsgroup-end number))
6503       (when (< number gnus-newsgroup-begin)
6504         (setq gnus-newsgroup-begin number))
6505       (setq gnus-newsgroup-unselected
6506             (delq number gnus-newsgroup-unselected)))
6507     ;; Report back a success?
6508     (and header (mail-header-number header))))
6509
6510 ;;; Process/prefix in the summary buffer
6511
6512 (defun gnus-summary-work-articles (n)
6513   "Return a list of articles to be worked upon.
6514 The prefix argument, the list of process marked articles, and the
6515 current article will be taken into consideration."
6516   (with-current-buffer gnus-summary-buffer
6517     (cond
6518      (n
6519       ;; A numerical prefix has been given.
6520       (setq n (prefix-numeric-value n))
6521       (let ((backward (< n 0))
6522             (n (abs (prefix-numeric-value n)))
6523             articles article)
6524         (save-excursion
6525           (while
6526               (and (> n 0)
6527                    (push (setq article (gnus-summary-article-number))
6528                          articles)
6529                    (if backward
6530                        (gnus-summary-find-prev nil article)
6531                      (gnus-summary-find-next nil article)))
6532             (decf n)))
6533         (nreverse articles)))
6534      ((and (gnus-region-active-p) (mark))
6535       (message "region active")
6536       ;; Work on the region between point and mark.
6537       (let ((max (max (point) (mark)))
6538             articles article)
6539         (save-excursion
6540           (goto-char (min (point) (mark)))
6541           (while
6542               (and
6543                (push (setq article (gnus-summary-article-number)) articles)
6544                (gnus-summary-find-next nil article)
6545                (< (point) max)))
6546           (nreverse articles))))
6547      (gnus-newsgroup-processable
6548       ;; There are process-marked articles present.
6549       ;; Save current state.
6550       (gnus-summary-save-process-mark)
6551       ;; Return the list.
6552       (reverse gnus-newsgroup-processable))
6553      (t
6554       ;; Just return the current article.
6555       (list (gnus-summary-article-number))))))
6556
6557 (defmacro gnus-summary-iterate (arg &rest forms)
6558   "Iterate over the process/prefixed articles and do FORMS.
6559 ARG is the interactive prefix given to the command.  FORMS will be
6560 executed with point over the summary line of the articles."
6561   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6562     `(let ((,articles (gnus-summary-work-articles ,arg)))
6563        (while ,articles
6564          (gnus-summary-goto-subject (car ,articles))
6565          ,@forms
6566          (pop ,articles)))))
6567
6568 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6569 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6570
6571 (defun gnus-summary-save-process-mark ()
6572   "Push the current set of process marked articles on the stack."
6573   (interactive)
6574   (push (copy-sequence gnus-newsgroup-processable)
6575         gnus-newsgroup-process-stack))
6576
6577 (defun gnus-summary-kill-process-mark ()
6578   "Push the current set of process marked articles on the stack and unmark."
6579   (interactive)
6580   (gnus-summary-save-process-mark)
6581   (gnus-summary-unmark-all-processable))
6582
6583 (defun gnus-summary-yank-process-mark ()
6584   "Pop the last process mark state off the stack and restore it."
6585   (interactive)
6586   (unless gnus-newsgroup-process-stack
6587     (error "Empty mark stack"))
6588   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6589
6590 (defun gnus-summary-process-mark-set (set)
6591   "Make SET into the current process marked articles."
6592   (gnus-summary-unmark-all-processable)
6593   (mapc 'gnus-summary-set-process-mark set))
6594
6595 ;;; Searching and stuff
6596
6597 (defun gnus-summary-search-group (&optional backward use-level)
6598   "Search for next unread newsgroup.
6599 If optional argument BACKWARD is non-nil, search backward instead."
6600   (with-current-buffer gnus-group-buffer
6601     (when (gnus-group-search-forward
6602            backward nil (if use-level (gnus-group-group-level) nil))
6603       (gnus-group-group-name))))
6604
6605 (defun gnus-summary-best-group (&optional exclude-group)
6606   "Find the name of the best unread group.
6607 If EXCLUDE-GROUP, do not go to this group."
6608   (with-current-buffer gnus-group-buffer
6609     (save-excursion
6610       (gnus-group-best-unread-group exclude-group))))
6611
6612 (defun gnus-summary-find-next (&optional unread article backward)
6613   (if backward
6614       (gnus-summary-find-prev unread article)
6615     (let* ((dummy (gnus-summary-article-intangible-p))
6616            (article (or article (gnus-summary-article-number)))
6617            (data (gnus-data-find-list article))
6618            result)
6619       (when (and (not dummy)
6620                  (or (not gnus-summary-check-current)
6621                      (not unread)
6622                      (not (gnus-data-unread-p (car data)))))
6623         (setq data (cdr data)))
6624       (when (setq result
6625                   (if unread
6626                       (progn
6627                         (while data
6628                           (unless (memq (gnus-data-number (car data))
6629                                         (cond
6630                                          ((eq gnus-auto-goto-ignores
6631                                               'always-undownloaded)
6632                                           gnus-newsgroup-undownloaded)
6633                                          (gnus-plugged
6634                                           nil)
6635                                          ((eq gnus-auto-goto-ignores
6636                                               'unfetched)
6637                                           gnus-newsgroup-unfetched)
6638                                          ((eq gnus-auto-goto-ignores
6639                                               'undownloaded)
6640                                           gnus-newsgroup-undownloaded)))
6641                             (when (gnus-data-unread-p (car data))
6642                               (setq result (car data)
6643                                     data nil)))
6644                           (setq data (cdr data)))
6645                         result)
6646                     (car data)))
6647         (goto-char (gnus-data-pos result))
6648         (gnus-data-number result)))))
6649
6650 (defun gnus-summary-find-prev (&optional unread article)
6651   (let* ((eobp (eobp))
6652          (article (or article (gnus-summary-article-number)))
6653          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6654          result)
6655     (when (and (not eobp)
6656                (or (not gnus-summary-check-current)
6657                    (not unread)
6658                    (not (gnus-data-unread-p (car data)))))
6659       (setq data (cdr data)))
6660     (when (setq result
6661                 (if unread
6662                     (progn
6663                       (while data
6664                         (unless (memq (gnus-data-number (car data))
6665                                       (cond
6666                                        ((eq gnus-auto-goto-ignores
6667                                             'always-undownloaded)
6668                                         gnus-newsgroup-undownloaded)
6669                                        (gnus-plugged
6670                                         nil)
6671                                        ((eq gnus-auto-goto-ignores
6672                                             'unfetched)
6673                                         gnus-newsgroup-unfetched)
6674                                        ((eq gnus-auto-goto-ignores
6675                                             'undownloaded)
6676                                         gnus-newsgroup-undownloaded)))
6677                           (when (gnus-data-unread-p (car data))
6678                             (setq result (car data)
6679                                   data nil)))
6680                         (setq data (cdr data)))
6681                       result)
6682                   (car data)))
6683       (goto-char (gnus-data-pos result))
6684       (gnus-data-number result))))
6685
6686 (defun gnus-summary-find-subject (subject &optional unread backward article)
6687   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6688          (article (or article (gnus-summary-article-number)))
6689          (articles (gnus-data-list backward))
6690          (arts (gnus-data-find-list article articles))
6691          result)
6692     (when (or (not gnus-summary-check-current)
6693               (not unread)
6694               (not (gnus-data-unread-p (car arts))))
6695       (setq arts (cdr arts)))
6696     (while arts
6697       (and (or (not unread)
6698                (gnus-data-unread-p (car arts)))
6699            (vectorp (gnus-data-header (car arts)))
6700            (gnus-subject-equal
6701             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6702            (setq result (car arts)
6703                  arts nil))
6704       (setq arts (cdr arts)))
6705     (and result
6706          (goto-char (gnus-data-pos result))
6707          (gnus-data-number result))))
6708
6709 (defun gnus-summary-search-forward (&optional unread subject backward)
6710   "Search forward for an article.
6711 If UNREAD, look for unread articles.  If SUBJECT, look for
6712 articles with that subject.  If BACKWARD, search backward instead."
6713   (cond (subject (gnus-summary-find-subject subject unread backward))
6714         (backward (gnus-summary-find-prev unread))
6715         (t (gnus-summary-find-next unread))))
6716
6717 (defun gnus-recenter (&optional n)
6718   "Center point in window and redisplay frame.
6719 Also do horizontal recentering."
6720   (interactive "P")
6721   (when (and gnus-auto-center-summary
6722              (not (eq gnus-auto-center-summary 'vertical)))
6723     (gnus-horizontal-recenter))
6724   (if (fboundp 'recenter-top-bottom)
6725       (recenter-top-bottom n)
6726     (recenter n)))
6727
6728 (put 'gnus-recenter 'isearch-scroll t)
6729
6730 (defun gnus-forward-line-ignore-invisible (n)
6731   "Move N lines forward (backward if N is negative).
6732 Like forward-line, but skip over (and don't count) invisible lines."
6733   (let (done)
6734     (while (and (> n 0) (not done))
6735       ;; If the following character is currently invisible,
6736       ;; skip all characters with that same `invisible' property value.
6737       (while (gnus-invisible-p (point))
6738         (goto-char (gnus-next-char-property-change (point))))
6739       (forward-line 1)
6740       (if (eobp)
6741           (setq done t)
6742         (setq n (1- n))))
6743     (while (and (< n 0) (not done))
6744       (forward-line -1)
6745       (if (bobp) (setq done t)
6746         (setq n (1+ n))
6747         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6748           (goto-char (gnus-previous-char-property-change (point))))))))
6749
6750 (defun gnus-summary-recenter ()
6751   "Center point in the summary window.
6752 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6753 displayed, no centering will be performed."
6754   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6755   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6756   (interactive)
6757   ;; The user has to want it.
6758   (when gnus-auto-center-summary
6759     (let* ((top (cond ((< (window-height) 4) 0)
6760                       ((< (window-height) 7) 1)
6761                       (t (if (numberp gnus-auto-center-summary)
6762                              gnus-auto-center-summary
6763                            (/ (1- (window-height)) 2)))))
6764            (height (1- (window-height)))
6765            (bottom (save-excursion
6766                      (goto-char (point-max))
6767                      (gnus-forward-line-ignore-invisible (- height))
6768                      (point)))
6769            (window (get-buffer-window (current-buffer))))
6770       (when (get-buffer-window gnus-article-buffer)
6771         ;; Only do recentering when the article buffer is displayed,
6772         ;; Set the window start to either `bottom', which is the biggest
6773         ;; possible valid number, or the second line from the top,
6774         ;; whichever is the least.
6775         (let ((top-pos (save-excursion
6776                          (gnus-forward-line-ignore-invisible (- top))
6777                          (point))))
6778           (if (> bottom top-pos)
6779               ;; Keep the second line from the top visible
6780               (set-window-start window top-pos)
6781             ;; Try to keep the bottom line visible; if it's partially
6782             ;; obscured, either scroll one more line to make it fully
6783             ;; visible, or revert to using TOP-POS.
6784             (save-excursion
6785               (goto-char (point-max))
6786               (gnus-forward-line-ignore-invisible -1)
6787               (let ((last-line-start (point)))
6788                 (goto-char bottom)
6789                 (set-window-start window (point) t)
6790                 (when (not (pos-visible-in-window-p last-line-start window))
6791                   (gnus-forward-line-ignore-invisible 1)
6792                   (set-window-start window (min (point) top-pos) t)))))))
6793       ;; Do horizontal recentering while we're at it.
6794       (when (and (get-buffer-window (current-buffer) t)
6795                  (not (eq gnus-auto-center-summary 'vertical)))
6796         (let ((selected (selected-window)))
6797           (select-window (get-buffer-window (current-buffer) t))
6798           (gnus-summary-position-point)
6799           (gnus-horizontal-recenter)
6800           (select-window selected))))))
6801
6802 (defun gnus-summary-jump-to-group (newsgroup)
6803   "Move point to NEWSGROUP in group mode buffer."
6804   ;; Keep update point of group mode buffer if visible.
6805   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6806       (save-window-excursion
6807         ;; Take care of tree window mode.
6808         (when (get-buffer-window gnus-group-buffer)
6809           (pop-to-buffer gnus-group-buffer))
6810         (gnus-group-jump-to-group newsgroup))
6811     (save-excursion
6812       ;; Take care of tree window mode.
6813       (if (get-buffer-window gnus-group-buffer 0)
6814           (pop-to-buffer gnus-group-buffer)
6815         (set-buffer gnus-group-buffer))
6816       (gnus-group-jump-to-group newsgroup))))
6817
6818 ;; This function returns a list of article numbers based on the
6819 ;; difference between the ranges of read articles in this group and
6820 ;; the range of active articles.
6821 (defun gnus-list-of-unread-articles (group)
6822   (let* ((read (gnus-info-read (gnus-get-info group)))
6823          (active (or (gnus-active group) (gnus-activate-group group)))
6824          (last (or (cdr active)
6825                    (error "Group %s couldn't be activated " group)))
6826          (bottom (if gnus-newsgroup-maximum-articles
6827                      (max (car active)
6828                           (- last gnus-newsgroup-maximum-articles -1))
6829                    (car active)))
6830          first nlast unread)
6831     ;; If none are read, then all are unread.
6832     (if (not read)
6833         (setq first bottom)
6834       ;; If the range of read articles is a single range, then the
6835       ;; first unread article is the article after the last read
6836       ;; article.  Sounds logical, doesn't it?
6837       (if (and (not (listp (cdr read)))
6838                (or (< (car read) bottom)
6839                    (progn (setq read (list read))
6840                           nil)))
6841           (setq first (max bottom (1+ (cdr read))))
6842         ;; `read' is a list of ranges.
6843         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6844                                   (caar read)))
6845                   1)
6846           (setq first bottom))
6847         (while read
6848           (when first
6849             (while (< first nlast)
6850               (setq unread (cons first unread)
6851                     first (1+ first))))
6852           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6853           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6854           (setq read (cdr read)))))
6855     ;; And add the last unread articles.
6856     (while (<= first last)
6857       (setq unread (cons first unread)
6858             first (1+ first)))
6859     ;; Return the list of unread articles.
6860     (delq 0 (nreverse unread))))
6861
6862 (defun gnus-list-of-read-articles (group)
6863   "Return a list of unread, unticked and non-dormant articles."
6864   (let* ((info (gnus-get-info group))
6865          (marked (gnus-info-marks info))
6866          (active (gnus-active group)))
6867     (and info active
6868          (gnus-list-range-difference
6869           (gnus-list-range-difference
6870            (gnus-sorted-complement
6871             (gnus-uncompress-range
6872              (if gnus-newsgroup-maximum-articles
6873                  (cons (max (car active)
6874                             (- (cdr active)
6875                                gnus-newsgroup-maximum-articles
6876                                -1))
6877                        (cdr active))
6878                active))
6879             (gnus-list-of-unread-articles group))
6880            (cdr (assq 'dormant marked)))
6881           (cdr (assq 'tick marked))))))
6882
6883 ;; This function returns a sequence of article numbers based on the
6884 ;; difference between the ranges of read articles in this group and
6885 ;; the range of active articles.
6886 (defun gnus-sequence-of-unread-articles (group)
6887   (let* ((read (gnus-info-read (gnus-get-info group)))
6888          (active (or (gnus-active group) (gnus-activate-group group)))
6889          (last (cdr active))
6890          (bottom (if gnus-newsgroup-maximum-articles
6891                      (max (car active)
6892                           (- last gnus-newsgroup-maximum-articles -1))
6893                    (car active)))
6894          first nlast unread)
6895     ;; If none are read, then all are unread.
6896     (if (not read)
6897         (setq first bottom)
6898       ;; If the range of read articles is a single range, then the
6899       ;; first unread article is the article after the last read
6900       ;; article.  Sounds logical, doesn't it?
6901       (if (and (not (listp (cdr read)))
6902                (or (< (car read) bottom)
6903                    (progn (setq read (list read))
6904                           nil)))
6905           (setq first (max bottom (1+ (cdr read))))
6906         ;; `read' is a list of ranges.
6907         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6908                                   (caar read)))
6909                   1)
6910           (setq first bottom))
6911         (while read
6912           (when first
6913             (push (cons first nlast) unread))
6914           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6915           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6916           (setq read (cdr read)))))
6917     ;; And add the last unread articles.
6918     (cond ((not (and first last))
6919            nil)
6920           ((< first last)
6921            (push (cons first last) unread))
6922           ((= first last)
6923            (push first unread)))
6924     ;; Return the sequence of unread articles.
6925     (delq 0 (nreverse unread))))
6926
6927 ;; Various summary commands
6928
6929 (defun gnus-summary-select-article-buffer ()
6930   "Reconfigure windows to show the article buffer."
6931   (interactive)
6932   (if (not (gnus-buffer-live-p gnus-article-buffer))
6933       (error "There is no article buffer for this summary buffer")
6934     (unless (get-buffer-window gnus-article-buffer)
6935       (gnus-summary-show-article))
6936     (gnus-configure-windows 'article t)
6937     (select-window (get-buffer-window gnus-article-buffer))))
6938
6939 (defun gnus-summary-universal-argument (arg)
6940   "Perform any operation on all articles that are process/prefixed."
6941   (interactive "P")
6942   (let ((articles (gnus-summary-work-articles arg))
6943         func article)
6944     (if (eq
6945          (setq
6946           func
6947           (key-binding
6948            (read-key-sequence
6949             (substitute-command-keys
6950              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6951          'undefined)
6952         (gnus-error 1 "Undefined key")
6953       (save-excursion
6954         (while articles
6955           (gnus-summary-goto-subject (setq article (pop articles)))
6956           (let (gnus-newsgroup-processable)
6957             (command-execute func))
6958           (gnus-summary-remove-process-mark article)))))
6959   (gnus-summary-position-point))
6960
6961 (defun gnus-summary-toggle-truncation (&optional arg)
6962   "Toggle truncation of summary lines.
6963 With ARG, turn line truncation on if ARG is positive."
6964   (interactive "P")
6965   (setq truncate-lines
6966         (if (null arg) (not truncate-lines)
6967           (> (prefix-numeric-value arg) 0)))
6968   (redraw-display))
6969
6970 (defun gnus-summary-find-for-reselect ()
6971   "Return the number of an article to stay on across a reselect.
6972 The current article is considered, then following articles, then previous
6973 articles.  An article is sought which is not cancelled and isn't a temporary
6974 insertion from another group.  If there's no such then return a dummy 0."
6975   (let (found)
6976     (dolist (rev '(nil t))
6977       (unless found      ; don't demand the reverse list if we don't need it
6978         (let ((data (gnus-data-find-list
6979                      (gnus-summary-article-number) (gnus-data-list rev))))
6980           (while (and data (not found))
6981             (if (and (< 0 (gnus-data-number (car data)))
6982                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6983                 (setq found (gnus-data-number (car data))))
6984             (setq data (cdr data))))))
6985     (or found 0)))
6986
6987 (defun gnus-summary-reselect-current-group (&optional all rescan)
6988   "Exit and then reselect the current newsgroup.
6989 The prefix argument ALL means to select all articles."
6990   (interactive "P")
6991   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6992     (error "Ephemeral groups can't be reselected"))
6993   (let ((current-subject (gnus-summary-find-for-reselect))
6994         (group gnus-newsgroup-name))
6995     (setq gnus-newsgroup-begin nil)
6996     (gnus-summary-exit nil 'leave-hidden)
6997     ;; We have to adjust the point of group mode buffer because
6998     ;; point was moved to the next unread newsgroup by exiting.
6999     (gnus-summary-jump-to-group group)
7000     (when rescan
7001       (save-excursion
7002         (gnus-group-get-new-news-this-group 1)))
7003     (gnus-group-read-group all t)
7004     (gnus-summary-goto-subject current-subject nil t)))
7005
7006 (defun gnus-summary-rescan-group (&optional all)
7007   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7008   (interactive "P")
7009   (gnus-summary-reselect-current-group all t))
7010
7011 (defun gnus-summary-update-info (&optional non-destructive)
7012   (save-excursion
7013     (let ((group gnus-newsgroup-name))
7014       (when group
7015         (when gnus-newsgroup-kill-headers
7016           (setq gnus-newsgroup-killed
7017                 (gnus-compress-sequence
7018                  (gnus-sorted-union
7019                   (gnus-list-range-intersection
7020                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7021                   gnus-newsgroup-unreads)
7022                  t)))
7023         (unless (listp (cdr gnus-newsgroup-killed))
7024           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7025         (let ((headers gnus-newsgroup-headers))
7026           ;; Set the new ranges of read articles.
7027           (with-current-buffer gnus-group-buffer
7028             (gnus-undo-force-boundary))
7029           (gnus-update-read-articles
7030            group (gnus-sorted-union
7031                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7032           ;; Set the current article marks.
7033           (let ((gnus-newsgroup-scored
7034                  (if (and (not gnus-save-score)
7035                           (not non-destructive))
7036                      nil
7037                    gnus-newsgroup-scored)))
7038             (save-excursion
7039               (gnus-update-marks)))
7040           ;; Do the cross-ref thing.
7041           (when gnus-use-cross-reference
7042             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7043           ;; Do not switch windows but change the buffer to work.
7044           (set-buffer gnus-group-buffer)
7045           (unless (gnus-ephemeral-group-p group)
7046             (gnus-group-update-group group)))))))
7047
7048 (defun gnus-summary-save-newsrc (&optional force)
7049   "Save the current number of read/marked articles in the dribble buffer.
7050 The dribble buffer will then be saved.
7051 If FORCE (the prefix), also save the .newsrc file(s)."
7052   (interactive "P")
7053   (gnus-summary-update-info t)
7054   (if force
7055       (gnus-save-newsrc-file)
7056     (gnus-dribble-save)))
7057
7058 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7059
7060 (defun gnus-summary-exit (&optional temporary leave-hidden)
7061   "Exit reading current newsgroup, and then return to group selection mode.
7062 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7063   (interactive)
7064   (gnus-set-global-variables)
7065   (when (gnus-buffer-live-p gnus-article-buffer)
7066     (with-current-buffer gnus-article-buffer
7067       (mm-destroy-parts gnus-article-mime-handles)
7068       ;; Set it to nil for safety reason.
7069       (setq gnus-article-mime-handle-alist nil)
7070       (setq gnus-article-mime-handles nil)))
7071   (gnus-kill-save-kill-buffer)
7072   (gnus-async-halt-prefetch)
7073   (let* ((group gnus-newsgroup-name)
7074          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7075          (gnus-group-is-exiting-p t)
7076          (mode major-mode)
7077          (group-point nil)
7078          (buf (current-buffer)))
7079     (unless quit-config
7080       ;; Do adaptive scoring, and possibly save score files.
7081       (when gnus-newsgroup-adaptive
7082         (gnus-score-adaptive))
7083       (when gnus-use-scoring
7084         (gnus-score-save)))
7085     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7086     (when gnus-use-cache
7087       (gnus-cache-possibly-remove-articles)
7088       (gnus-cache-save-buffers))
7089     (gnus-async-prefetch-remove-group group)
7090     (when gnus-suppress-duplicates
7091       (gnus-dup-enter-articles))
7092     (when gnus-use-trees
7093       (gnus-tree-close group))
7094     (when gnus-use-cache
7095       (gnus-cache-write-active))
7096     ;; Remove entries for this group.
7097     (nnmail-purge-split-history (gnus-group-real-name group))
7098     ;; Make all changes in this group permanent.
7099     (unless quit-config
7100       (gnus-run-hooks 'gnus-exit-group-hook)
7101       (gnus-summary-update-info))
7102     (gnus-close-group group)
7103     ;; Make sure where we were, and go to next newsgroup.
7104     (set-buffer gnus-group-buffer)
7105     (unless quit-config
7106       (gnus-group-jump-to-group group))
7107     (gnus-run-hooks 'gnus-summary-exit-hook)
7108     (unless (or quit-config
7109                 (not gnus-summary-next-group-on-exit)
7110                 ;; If this group has disappeared from the summary
7111                 ;; buffer, don't skip forwards.
7112                 (not (string= group (gnus-group-group-name))))
7113       (gnus-group-next-unread-group 1))
7114     (setq group-point (point))
7115     (if temporary
7116         nil                             ;Nothing to do.
7117       (set-buffer buf)
7118       (if (not gnus-kill-summary-on-exit)
7119           (progn
7120             (gnus-deaden-summary)
7121             (setq mode nil))
7122         (when (get-buffer gnus-article-buffer)
7123           (bury-buffer gnus-article-buffer))
7124         ;; Return to group mode buffer.
7125         (when (eq mode 'gnus-summary-mode)
7126           (gnus-kill-buffer buf)))
7127
7128       ;; If we have several article buffers, we kill them at exit.
7129       (unless gnus-single-article-buffer
7130         (when (gnus-buffer-live-p gnus-article-buffer)
7131           (with-current-buffer gnus-article-buffer
7132             ;; Don't kill sticky article buffers
7133             (unless (eq major-mode 'gnus-sticky-article-mode)
7134               (gnus-kill-buffer gnus-article-buffer)
7135               (setq gnus-article-current nil))))
7136         (gnus-kill-buffer gnus-original-article-buffer))
7137
7138       (setq gnus-current-select-method gnus-select-method)
7139       (set-buffer gnus-group-buffer)
7140       (if quit-config
7141           (gnus-handle-ephemeral-exit quit-config)
7142         (goto-char group-point)
7143         ;; If gnus-group-buffer is already displayed, make sure we also move
7144         ;; the cursor in the window that displays it.
7145         (let ((win (get-buffer-window (current-buffer) 0)))
7146           (if win (set-window-point win (point))))
7147         (unless leave-hidden
7148           (gnus-configure-windows 'group 'force)))
7149       ;; Clear the current group name.
7150       (unless quit-config
7151         (setq gnus-newsgroup-name nil)))))
7152
7153 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7154 (defun gnus-summary-exit-no-update (&optional no-questions)
7155   "Quit reading current newsgroup without updating read article info."
7156   (interactive)
7157   (let* ((group gnus-newsgroup-name)
7158          (gnus-group-is-exiting-p t)
7159          (gnus-group-is-exiting-without-update-p t)
7160          (quit-config (gnus-group-quit-config group)))
7161     (when (or no-questions
7162               gnus-expert-user
7163               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7164       (gnus-async-halt-prefetch)
7165       (run-hooks 'gnus-summary-prepare-exit-hook)
7166       (when (gnus-buffer-live-p gnus-article-buffer)
7167         (with-current-buffer gnus-article-buffer
7168           (mm-destroy-parts gnus-article-mime-handles)
7169           ;; Set it to nil for safety reason.
7170           (setq gnus-article-mime-handle-alist nil)
7171           (setq gnus-article-mime-handles nil)))
7172       ;; If we have several article buffers, we kill them at exit.
7173       (unless gnus-single-article-buffer
7174         (gnus-kill-buffer gnus-article-buffer)
7175         (gnus-kill-buffer gnus-original-article-buffer)
7176         (setq gnus-article-current nil))
7177       (if (not gnus-kill-summary-on-exit)
7178           (gnus-deaden-summary)
7179         (gnus-close-group group)
7180         (gnus-kill-buffer gnus-summary-buffer))
7181       (unless gnus-single-article-buffer
7182         (setq gnus-article-current nil))
7183       (when gnus-use-trees
7184         (gnus-tree-close group))
7185       (gnus-async-prefetch-remove-group group)
7186       (when (get-buffer gnus-article-buffer)
7187         (bury-buffer gnus-article-buffer))
7188       ;; Return to the group buffer.
7189       (gnus-configure-windows 'group 'force)
7190       ;; Clear the current group name.
7191       (setq gnus-newsgroup-name nil)
7192       (unless (gnus-ephemeral-group-p group)
7193         (gnus-group-update-group group))
7194       (when (equal (gnus-group-group-name) group)
7195         (gnus-group-next-unread-group 1))
7196       (when quit-config
7197         (gnus-handle-ephemeral-exit quit-config)))))
7198
7199 (defun gnus-handle-ephemeral-exit (quit-config)
7200   "Handle movement when leaving an ephemeral group.
7201 The state which existed when entering the ephemeral is reset."
7202   (if (not (buffer-name (car quit-config)))
7203       (gnus-configure-windows 'group 'force)
7204     (set-buffer (car quit-config))
7205     (cond ((eq major-mode 'gnus-summary-mode)
7206            (gnus-set-global-variables))
7207           ((eq major-mode 'gnus-article-mode)
7208            (save-current-buffer
7209              ;; The `gnus-summary-buffer' variable may point
7210              ;; to the old summary buffer when using a single
7211              ;; article buffer.
7212              (unless (gnus-buffer-live-p gnus-summary-buffer)
7213                (set-buffer gnus-group-buffer))
7214              (set-buffer gnus-summary-buffer)
7215              (gnus-set-global-variables))))
7216     (if (or (eq (cdr quit-config) 'article)
7217             (eq (cdr quit-config) 'pick))
7218         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7219             (gnus-configure-windows 'pick 'force)
7220           (gnus-configure-windows (cdr quit-config) 'force))
7221       (gnus-configure-windows (cdr quit-config) 'force))
7222     (when (eq major-mode 'gnus-summary-mode)
7223       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7224                                                      next-unread-noselect))
7225           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7226                                   'next-noselect)
7227                               (gnus-summary-next-subject 1 nil t))
7228                              ((eq gnus-auto-select-on-ephemeral-exit
7229                                   'next-unread-noselect)
7230                               (gnus-summary-next-subject 1 t t))))
7231             ;; Hide the article buffer which displays the article different
7232             ;; from the one that the cursor points to in the summary buffer.
7233             (gnus-configure-windows 'summary 'force))
7234         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7235                (gnus-summary-next-subject 1))
7236               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7237                (gnus-summary-next-subject 1 t))))
7238       (gnus-summary-recenter)
7239       (gnus-summary-position-point))))
7240
7241 ;;; Dead summaries.
7242
7243 (defvar gnus-dead-summary-mode-map
7244   (let ((map (make-keymap)))
7245     (suppress-keymap map)
7246     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7247     (dolist (key '("\C-d" "\r" "\177" [delete]))
7248       (define-key map key 'gnus-summary-wake-up-the-dead))
7249     (dolist (key '("q" "Q"))
7250       (define-key map key 'bury-buffer))
7251     map))
7252
7253 (define-minor-mode gnus-dead-summary-mode
7254   "Minor mode for Gnus summary buffers."
7255   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7256   (unless (derived-mode-p 'gnus-summary-mode)
7257     (setq gnus-dead-summary-mode nil)))
7258
7259 (defun gnus-deaden-summary ()
7260   "Make the current summary buffer into a dead summary buffer."
7261   ;; Kill any previous dead summary buffer.
7262   (when (and gnus-dead-summary
7263              (buffer-name gnus-dead-summary))
7264     (with-current-buffer gnus-dead-summary
7265       (when gnus-dead-summary-mode
7266         (kill-buffer (current-buffer)))))
7267   ;; Make this the current dead summary.
7268   (setq gnus-dead-summary (current-buffer))
7269   (gnus-dead-summary-mode 1)
7270   (let ((name (buffer-name)))
7271     (when (string-match "Summary" name)
7272       (rename-buffer
7273        (concat (substring name 0 (match-beginning 0)) "Dead "
7274                (substring name (match-beginning 0)))
7275        t)
7276       (bury-buffer))))
7277
7278 (defun gnus-kill-or-deaden-summary (buffer)
7279   "Kill or deaden the summary BUFFER."
7280   (save-excursion
7281     (when (and (buffer-name buffer)
7282                (not gnus-single-article-buffer))
7283       (with-current-buffer buffer
7284         (gnus-kill-buffer gnus-article-buffer)
7285         (gnus-kill-buffer gnus-original-article-buffer)))
7286     (cond
7287      ;; Kill the buffer.
7288      (gnus-kill-summary-on-exit
7289       (when (and gnus-use-trees
7290                  (gnus-buffer-exists-p buffer))
7291         (with-current-buffer buffer
7292           (gnus-tree-close gnus-newsgroup-name)))
7293       (gnus-kill-buffer buffer))
7294      ;; Deaden the buffer.
7295      ((gnus-buffer-exists-p buffer)
7296       (with-current-buffer buffer
7297         (gnus-deaden-summary))))))
7298
7299 (defun gnus-summary-wake-up-the-dead (&rest args)
7300   "Wake up the dead summary buffer."
7301   (interactive)
7302   (gnus-dead-summary-mode -1)
7303   (let ((name (buffer-name)))
7304     (when (string-match "Dead " name)
7305       (rename-buffer
7306        (concat (substring name 0 (match-beginning 0))
7307                (substring name (match-end 0)))
7308        t)))
7309   (gnus-message 3 "This dead summary is now alive again"))
7310
7311 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7312 (defun gnus-summary-describe-group (&optional force)
7313   "Describe the current newsgroup."
7314   (interactive "P")
7315   (gnus-group-describe-group force gnus-newsgroup-name))
7316
7317 (defun gnus-summary-describe-briefly ()
7318   "Describe summary mode commands briefly."
7319   (interactive)
7320   (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")))
7321
7322 ;; Walking around group mode buffer from summary mode.
7323
7324 (defun gnus-summary-next-group (&optional no-article target-group backward)
7325   "Exit current newsgroup and then select next unread newsgroup.
7326 If prefix argument NO-ARTICLE is non-nil, no article is selected
7327 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7328 previous group instead."
7329   (interactive "P")
7330   ;; Stop pre-fetching.
7331   (gnus-async-halt-prefetch)
7332   (let ((current-group gnus-newsgroup-name)
7333         (current-buffer (current-buffer))
7334         entered)
7335     ;; First we semi-exit this group to update Xrefs and all variables.
7336     ;; We can't do a real exit, because the window conf must remain
7337     ;; the same in case the user is prompted for info, and we don't
7338     ;; want the window conf to change before that...
7339     (gnus-summary-exit t)
7340     (while (not entered)
7341       ;; Then we find what group we are supposed to enter.
7342       (set-buffer gnus-group-buffer)
7343       (gnus-group-jump-to-group current-group)
7344       (setq target-group
7345             (or target-group
7346                 (if (eq gnus-keep-same-level 'best)
7347                     (gnus-summary-best-group gnus-newsgroup-name)
7348                   (gnus-summary-search-group backward gnus-keep-same-level))))
7349       (if (not target-group)
7350           ;; There are no further groups, so we return to the group
7351           ;; buffer.
7352           (progn
7353             (gnus-message 5 "Returning to the group buffer")
7354             (setq entered t)
7355             (when (gnus-buffer-live-p current-buffer)
7356               (set-buffer current-buffer)
7357               (gnus-summary-exit))
7358             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7359         ;; We try to enter the target group.
7360         (gnus-group-jump-to-group target-group)
7361         (let ((unreads (gnus-group-group-unread)))
7362           (if (and (or (eq t unreads)
7363                        (and unreads (not (zerop unreads))))
7364                    (gnus-summary-read-group
7365                     target-group nil no-article
7366                     (and (buffer-name current-buffer) current-buffer)
7367                     nil backward))
7368               (setq entered t)
7369             (setq current-group target-group
7370                   target-group nil)))))))
7371
7372 (defun gnus-summary-prev-group (&optional no-article)
7373   "Exit current newsgroup and then select previous unread newsgroup.
7374 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7375   (interactive "P")
7376   (gnus-summary-next-group no-article nil t))
7377
7378 ;; Walking around summary lines.
7379
7380 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7381   "Go to the first subject satisfying any non-nil constraint.
7382 If UNREAD is non-nil, the article should be unread.
7383 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7384 If UNSEEN is non-nil, the article should be unseen as well as unread.
7385 Returns the article selected or nil if there are no matching articles."
7386   (interactive "P")
7387   (cond
7388    ;; Empty summary.
7389    ((null gnus-newsgroup-data)
7390     (gnus-message 3 "No articles in the group")
7391     nil)
7392    ;; Pick the first article.
7393    ((not (or unread undownloaded unseen))
7394     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7395     (gnus-data-number (car gnus-newsgroup-data)))
7396    ;; Find the first unread article.
7397    (t
7398     (let ((data gnus-newsgroup-data))
7399       (while (and data
7400                   (let ((num (gnus-data-number (car data))))
7401                     (or (memq num gnus-newsgroup-unfetched)
7402                         (not (or (and unread
7403                                       (memq num gnus-newsgroup-unreads))
7404                                  (and undownloaded
7405                                       (memq num gnus-newsgroup-undownloaded))
7406                                  (and unseen
7407                                       (memq num gnus-newsgroup-unseen)
7408                                       (memq num gnus-newsgroup-unreads)))))))
7409         (setq data (cdr data)))
7410       (prog1
7411           (if data
7412               (progn
7413                 (goto-char (gnus-data-pos (car data)))
7414                 (gnus-data-number (car data)))
7415             (gnus-message 3 "No more%s articles"
7416                           (let* ((r (when unread " unread"))
7417                                  (d (when undownloaded " undownloaded"))
7418                                  (s (when unseen " unseen"))
7419                                  (l (delq nil (list r d s))))
7420                             (cond ((= 3 (length l))
7421                                    (concat r "," d ", or" s))
7422                                   ((= 2 (length l))
7423                                    (concat (car l) ", or" (cadr l)))
7424                                   ((= 1 (length l))
7425                                    (car l))
7426                                   (t
7427                                    ""))))
7428             nil
7429             )
7430         (gnus-summary-position-point))))))
7431
7432 (defun gnus-summary-next-subject (n &optional unread dont-display)
7433   "Go to next N'th summary line.
7434 If N is negative, go to the previous N'th subject line.
7435 If UNREAD is non-nil, only unread articles are selected.
7436 The difference between N and the actual number of steps taken is
7437 returned."
7438   (interactive "p")
7439   (let ((backward (< n 0))
7440         (n (abs n)))
7441     (while (and (> n 0)
7442                 (if backward
7443                     (gnus-summary-find-prev unread)
7444                   (gnus-summary-find-next unread)))
7445       (unless (zerop (setq n (1- n)))
7446         (gnus-summary-show-thread)))
7447     (when (/= 0 n)
7448       (gnus-message 7 "No more%s articles"
7449                     (if unread " unread" "")))
7450     (unless dont-display
7451       (gnus-summary-recenter)
7452       (gnus-summary-position-point))
7453     n))
7454
7455 (defun gnus-summary-next-unread-subject (n)
7456   "Go to next N'th unread summary line."
7457   (interactive "p")
7458   (gnus-summary-next-subject n t))
7459
7460 (defun gnus-summary-prev-subject (n &optional unread)
7461   "Go to previous N'th summary line.
7462 If optional argument UNREAD is non-nil, only unread article is selected."
7463   (interactive "p")
7464   (gnus-summary-next-subject (- n) unread))
7465
7466 (defun gnus-summary-prev-unread-subject (n)
7467   "Go to previous N'th unread summary line."
7468   (interactive "p")
7469   (gnus-summary-next-subject (- n) t))
7470
7471 (defun gnus-summary-goto-subjects (articles)
7472   "Insert the subject header for ARTICLES in the current buffer."
7473   (save-excursion
7474     (dolist (article articles)
7475       (gnus-summary-goto-subject article t)))
7476   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7477   (gnus-summary-position-point))
7478
7479 (defun gnus-summary-goto-subject (article &optional force silent)
7480   "Go to the subject line of ARTICLE.
7481 If FORCE, also allow jumping to articles not currently shown."
7482   (interactive "nArticle number: ")
7483   (unless (numberp article)
7484     (error "Article %s is not a number" article))
7485   (let ((b (point))
7486         (data (gnus-data-find article)))
7487     ;; We read in the article if we have to.
7488     (and (not data)
7489          force
7490          (gnus-summary-insert-subject
7491           article
7492           (if (or (numberp force) (vectorp force)) force)
7493           t)
7494          (setq data (gnus-data-find article)))
7495     (goto-char b)
7496     (if (not data)
7497         (progn
7498           (unless silent
7499             (gnus-message 3 "Can't find article %d" article))
7500           nil)
7501       (let ((pt (gnus-data-pos data)))
7502         (goto-char pt)
7503         (gnus-summary-set-article-display-arrow pt))
7504       (gnus-summary-position-point)
7505       article)))
7506
7507 ;; Walking around summary lines with displaying articles.
7508
7509 (defun gnus-summary-expand-window (&optional arg)
7510   "Make the summary buffer take up the entire Emacs frame.
7511 Given a prefix, will force an `article' buffer configuration."
7512   (interactive "P")
7513   (if arg
7514       (gnus-configure-windows 'article 'force)
7515     (gnus-configure-windows 'summary 'force)))
7516
7517 (defun gnus-summary-display-article (article &optional all-header)
7518   "Display ARTICLE in article buffer."
7519   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7520                (with-current-buffer gnus-article-buffer
7521                  (eq major-mode 'gnus-article-mode)))
7522     (gnus-article-setup-buffer))
7523   (gnus-set-global-variables)
7524   (with-current-buffer gnus-article-buffer
7525     (setq gnus-article-charset gnus-newsgroup-charset)
7526     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7527     (mm-enable-multibyte))
7528   (if (null article)
7529       nil
7530     (prog1
7531         (if gnus-summary-display-article-function
7532             (funcall gnus-summary-display-article-function article all-header)
7533           (gnus-article-prepare article all-header))
7534       (gnus-run-hooks 'gnus-select-article-hook)
7535       (when (and gnus-current-article
7536                  (not (zerop gnus-current-article)))
7537         (gnus-summary-goto-subject gnus-current-article))
7538       (gnus-summary-recenter)
7539       (when (and gnus-use-trees gnus-show-threads)
7540         (gnus-possibly-generate-tree article)
7541         (gnus-highlight-selected-tree article))
7542       ;; Successfully display article.
7543       (gnus-article-set-window-start
7544        (cdr (assq article gnus-newsgroup-bookmarks))))))
7545
7546 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7547   "Select the current article.
7548 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7549 non-nil, the article will be re-fetched even if it already present in
7550 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7551 be displayed."
7552   ;; Make sure we are in the summary buffer to work around bbdb bug.
7553   (unless (eq major-mode 'gnus-summary-mode)
7554     (set-buffer gnus-summary-buffer))
7555   (let ((article (or article (gnus-summary-article-number)))
7556         (all-headers (not (not all-headers))) ;Must be t or nil.
7557         gnus-summary-display-article-function)
7558     (and (not pseudo)
7559          (gnus-summary-article-pseudo-p article)
7560          (error "This is a pseudo-article"))
7561     (with-current-buffer gnus-summary-buffer
7562       (if (or (and gnus-single-article-buffer
7563                    (or (null gnus-current-article)
7564                        (null gnus-article-current)
7565                        (null (get-buffer gnus-article-buffer))
7566                        (not (eq article (cdr gnus-article-current)))
7567                        (not (equal (car gnus-article-current)
7568                                    gnus-newsgroup-name))))
7569               (and (not gnus-single-article-buffer)
7570                    (or (null gnus-current-article)
7571                        (not (eq gnus-current-article article))))
7572               force)
7573           ;; The requested article is different from the current article.
7574           (progn
7575             (gnus-summary-display-article article all-headers)
7576             (when (gnus-buffer-live-p gnus-article-buffer)
7577               (with-current-buffer gnus-article-buffer
7578                 (if (not gnus-article-decoded-p) ;; a local variable
7579                     (mm-disable-multibyte))))
7580             (gnus-article-set-window-start
7581              (cdr (assq article gnus-newsgroup-bookmarks)))
7582             article)
7583         'old))))
7584
7585 (defun gnus-summary-force-verify-and-decrypt ()
7586   "Display buttons for signed/encrypted parts and verify/decrypt them."
7587   (interactive)
7588   (let ((mm-verify-option 'known)
7589         (mm-decrypt-option 'known)
7590         (gnus-article-emulate-mime t)
7591         (gnus-buttonized-mime-types (append (list "multipart/signed"
7592                                                   "multipart/encrypted")
7593                                             gnus-buttonized-mime-types)))
7594     (gnus-summary-select-article nil 'force)))
7595
7596 (defun gnus-summary-set-current-mark (&optional current-mark)
7597   "Obsolete function."
7598   nil)
7599
7600 (defun gnus-summary-next-article (&optional unread subject backward push)
7601   "Select the next article.
7602 If UNREAD, only unread articles are selected.
7603 If SUBJECT, only articles with SUBJECT are selected.
7604 If BACKWARD, the previous article is selected instead of the next."
7605   (interactive "P")
7606   ;; Make sure we are in the summary buffer.
7607   (unless (eq major-mode 'gnus-summary-mode)
7608     (set-buffer gnus-summary-buffer))
7609   (cond
7610    ;; Is there such an article?
7611    ((and (gnus-summary-search-forward unread subject backward)
7612          (or (gnus-summary-display-article (gnus-summary-article-number))
7613              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7614     (gnus-summary-position-point))
7615    ;; If not, we try the first unread, if that is wanted.
7616    ((and subject
7617          gnus-auto-select-same
7618          (gnus-summary-first-unread-article))
7619     (gnus-summary-position-point)
7620     (gnus-message 6 "Wrapped"))
7621    ;; Try to get next/previous article not displayed in this group.
7622    ((and gnus-auto-extend-newsgroup
7623          (not unread) (not subject))
7624     (gnus-summary-goto-article
7625      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7626      nil (count-lines (point-min) (point))))
7627    ;; Go to next/previous group.
7628    (t
7629     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7630       (gnus-summary-jump-to-group gnus-newsgroup-name))
7631     (let ((cmd (if (featurep 'xemacs)
7632                    last-command-char
7633                  last-command-event))
7634           (point
7635            (with-current-buffer gnus-group-buffer
7636              (point)))
7637           (group
7638            (if (eq gnus-keep-same-level 'best)
7639                (gnus-summary-best-group gnus-newsgroup-name)
7640              (gnus-summary-search-group backward gnus-keep-same-level))))
7641       ;; For some reason, the group window gets selected.  We change
7642       ;; it back.
7643       (select-window (get-buffer-window (current-buffer)))
7644       ;; Select next unread newsgroup automagically.
7645       (cond
7646        ((or (not gnus-auto-select-next)
7647             (not cmd))
7648         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7649        ((or (eq gnus-auto-select-next 'quietly)
7650             (and (eq gnus-auto-select-next 'slightly-quietly)
7651                  push)
7652             (and (eq gnus-auto-select-next 'almost-quietly)
7653                  (gnus-summary-last-article-p)))
7654         ;; Select quietly.
7655         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7656             (gnus-summary-exit)
7657           (gnus-message 7 "No more%s articles (%s)..."
7658                         (if unread " unread" "")
7659                         (if group (concat "selecting " group)
7660                           "exiting"))
7661           (gnus-summary-next-group nil group backward)))
7662        (t
7663         (when (gnus-key-press-event-p last-input-event)
7664           (gnus-summary-walk-group-buffer
7665            gnus-newsgroup-name cmd unread backward point))))))))
7666
7667 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7668   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7669                       (?\C-p (gnus-group-prev-unread-group 1))))
7670         (cursor-in-echo-area t)
7671         keve key group ended prompt)
7672     (with-current-buffer gnus-group-buffer
7673       (goto-char start)
7674       (setq group
7675             (if (eq gnus-keep-same-level 'best)
7676                 (gnus-summary-best-group gnus-newsgroup-name)
7677               (gnus-summary-search-group backward gnus-keep-same-level))))
7678     (while (not ended)
7679       (setq prompt
7680             (format
7681              "No more%s articles%s " (if unread " unread" "")
7682              (if (and group
7683                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7684                  (format " (Type %s for %s [%s])"
7685                          (single-key-description cmd)
7686                          (gnus-group-decoded-name group)
7687                          (gnus-group-unread group))
7688                (format " (Type %s to exit %s)"
7689                        (single-key-description cmd)
7690                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7691       ;; Confirm auto selection.
7692       (setq key (car (setq keve (gnus-read-event-char prompt)))
7693             ended t)
7694       (cond
7695        ((assq key keystrokes)
7696         (let ((obuf (current-buffer)))
7697           (switch-to-buffer gnus-group-buffer)
7698           (when group
7699             (gnus-group-jump-to-group group))
7700           (eval (cadr (assq key keystrokes)))
7701           (setq group (gnus-group-group-name))
7702           (switch-to-buffer obuf))
7703         (setq ended nil))
7704        ((equal key cmd)
7705         (if (or (not group)
7706                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7707             (gnus-summary-exit)
7708           (gnus-summary-next-group nil group backward)))
7709        (t
7710         (push (cdr keve) unread-command-events))))))
7711
7712 (defun gnus-summary-next-unread-article ()
7713   "Select unread article after current one."
7714   (interactive)
7715   (gnus-summary-next-article
7716    (or (not (eq gnus-summary-goto-unread 'never))
7717        (gnus-summary-last-article-p (gnus-summary-article-number)))
7718    (and gnus-auto-select-same
7719         (gnus-summary-article-subject))))
7720
7721 (defun gnus-summary-prev-article (&optional unread subject)
7722   "Select the article before the current one.
7723 If UNREAD is non-nil, only unread articles are selected."
7724   (interactive "P")
7725   (gnus-summary-next-article unread subject t))
7726
7727 (defun gnus-summary-prev-unread-article ()
7728   "Select unread article before current one."
7729   (interactive)
7730   (gnus-summary-prev-article
7731    (or (not (eq gnus-summary-goto-unread 'never))
7732        (gnus-summary-first-article-p (gnus-summary-article-number)))
7733    (and gnus-auto-select-same
7734         (gnus-summary-article-subject))))
7735
7736 (defun gnus-summary-next-page (&optional lines circular stop)
7737   "Show next page of the selected article.
7738 If at the end of the current article, select the next article.
7739 LINES says how many lines should be scrolled up.
7740
7741 If CIRCULAR is non-nil, go to the start of the article instead of
7742 selecting the next article when reaching the end of the current
7743 article.
7744
7745 If STOP is non-nil, just stop when reaching the end of the message.
7746
7747 Also see the variable `gnus-article-skip-boring'."
7748   (interactive "P")
7749   (setq gnus-summary-buffer (current-buffer))
7750   (gnus-set-global-variables)
7751   (let ((article (gnus-summary-article-number))
7752         (article-window (get-buffer-window gnus-article-buffer t))
7753         endp)
7754     ;; If the buffer is empty, we have no article.
7755     (unless article
7756       (error "No article to select"))
7757     (gnus-configure-windows 'article)
7758     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7759         (if (and (eq gnus-summary-goto-unread 'never)
7760                  (not (gnus-summary-last-article-p article)))
7761             (gnus-summary-next-article)
7762           (gnus-summary-next-unread-article))
7763       (if (or (null gnus-current-article)
7764               (null gnus-article-current)
7765               (/= article (cdr gnus-article-current))
7766               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7767           ;; Selected subject is different from current article's.
7768           (gnus-summary-display-article article)
7769         (when article-window
7770           (gnus-eval-in-buffer-window gnus-article-buffer
7771             (setq endp (or (gnus-article-next-page lines)
7772                            (gnus-article-only-boring-p))))
7773           (when endp
7774             (cond ((or stop gnus-summary-stop-at-end-of-message)
7775                    (gnus-message 3 "End of message"))
7776                   (circular
7777                    (gnus-summary-beginning-of-article))
7778                   (lines
7779                    (gnus-message 3 "End of message"))
7780                   ((null lines)
7781                    (if (and (eq gnus-summary-goto-unread 'never)
7782                             (not (gnus-summary-last-article-p article)))
7783                        (gnus-summary-next-article)
7784                      (gnus-summary-next-unread-article))))))))
7785     (gnus-summary-recenter)
7786     (gnus-summary-position-point)))
7787
7788 (defun gnus-summary-prev-page (&optional lines move)
7789   "Show previous page of selected article.
7790 Argument LINES specifies lines to be scrolled down.
7791 If MOVE, move to the previous unread article if point is at
7792 the beginning of the buffer."
7793   (interactive "P")
7794   (let ((article (gnus-summary-article-number))
7795         (article-window (get-buffer-window gnus-article-buffer t))
7796         endp)
7797     (gnus-configure-windows 'article)
7798     (if (or (null gnus-current-article)
7799             (null gnus-article-current)
7800             (/= article (cdr gnus-article-current))
7801             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7802         ;; Selected subject is different from current article's.
7803         (gnus-summary-display-article article)
7804       (gnus-summary-recenter)
7805       (when article-window
7806         (gnus-eval-in-buffer-window gnus-article-buffer
7807           (setq endp (gnus-article-prev-page lines)))
7808         (when (and move endp)
7809           (cond (lines
7810                  (gnus-message 3 "Beginning of message"))
7811                 ((null lines)
7812                  (if (and (eq gnus-summary-goto-unread 'never)
7813                           (not (gnus-summary-first-article-p article)))
7814                      (gnus-summary-prev-article)
7815                    (gnus-summary-prev-unread-article))))))))
7816   (gnus-summary-position-point))
7817
7818 (defun gnus-summary-prev-page-or-article (&optional lines)
7819   "Show previous page of selected article.
7820 Argument LINES specifies lines to be scrolled down.
7821 If at the beginning of the article, go to the next article."
7822   (interactive "P")
7823   (gnus-summary-prev-page lines t))
7824
7825 (defun gnus-summary-scroll-up (lines)
7826   "Scroll up (or down) one line current article.
7827 Argument LINES specifies lines to be scrolled up (or down if negative).
7828 If no article is selected, then the current article will be selected first."
7829   (interactive "p")
7830   (gnus-configure-windows 'article)
7831   (gnus-summary-show-thread)
7832   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7833     (gnus-eval-in-buffer-window gnus-article-buffer
7834       (cond ((> lines 0)
7835              (when (gnus-article-next-page lines)
7836                (gnus-message 3 "End of message")))
7837             ((< lines 0)
7838              (gnus-article-prev-page (- lines))))))
7839   (gnus-summary-recenter)
7840   (gnus-summary-position-point))
7841
7842 (defun gnus-summary-scroll-down (lines)
7843   "Scroll down (or up) one line current article.
7844 Argument LINES specifies lines to be scrolled down (or up if negative).
7845 If no article is selected, then the current article will be selected first."
7846   (interactive "p")
7847   (gnus-summary-scroll-up (- lines)))
7848
7849 (defun gnus-summary-next-same-subject ()
7850   "Select next article which has the same subject as current one."
7851   (interactive)
7852   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7853
7854 (defun gnus-summary-prev-same-subject ()
7855   "Select previous article which has the same subject as current one."
7856   (interactive)
7857   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7858
7859 (defun gnus-summary-next-unread-same-subject ()
7860   "Select next unread article which has the same subject as current one."
7861   (interactive)
7862   (gnus-summary-next-article t (gnus-summary-article-subject)))
7863
7864 (defun gnus-summary-prev-unread-same-subject ()
7865   "Select previous unread article which has the same subject as current one."
7866   (interactive)
7867   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7868
7869 (defun gnus-summary-first-unread-article ()
7870   "Select the first unread article.
7871 Return nil if there are no unread articles."
7872   (interactive)
7873   (prog1
7874       (when (gnus-summary-first-subject t)
7875         (gnus-summary-show-thread)
7876         (gnus-summary-first-subject t)
7877         (gnus-summary-display-article (gnus-summary-article-number)))
7878     (gnus-summary-position-point)))
7879
7880 (defun gnus-summary-first-unread-subject ()
7881   "Place the point on the subject line of the first unread article.
7882 Return nil if there are no unread articles."
7883   (interactive)
7884   (prog1
7885       (when (gnus-summary-first-subject t)
7886         (gnus-summary-show-thread)
7887         (gnus-summary-first-subject t))
7888     (gnus-summary-position-point)))
7889
7890 (defun gnus-summary-first-unseen-subject ()
7891   "Place the point on the subject line of the first unseen article.
7892 Return nil if there are no unseen articles."
7893   (interactive)
7894   (prog1
7895       (when (gnus-summary-first-subject nil nil t)
7896         (gnus-summary-show-thread)
7897         (gnus-summary-first-subject nil nil t))
7898     (gnus-summary-position-point)))
7899
7900 (defun gnus-summary-first-unseen-or-unread-subject ()
7901   "Place the point on the subject line of the first unseen and unread article.
7902 If all article have been seen, on the subject line of the first unread
7903 article."
7904   (interactive)
7905   (prog1
7906       (unless (when (gnus-summary-first-subject nil nil t)
7907                 (gnus-summary-show-thread)
7908                 (gnus-summary-first-subject nil nil t))
7909         (when (gnus-summary-first-subject t)
7910           (gnus-summary-show-thread)
7911           (gnus-summary-first-subject t)))
7912     (gnus-summary-position-point)))
7913
7914 (defun gnus-summary-first-article ()
7915   "Select the first article.
7916 Return nil if there are no articles."
7917   (interactive)
7918   (prog1
7919       (when (gnus-summary-first-subject)
7920         (gnus-summary-show-thread)
7921         (gnus-summary-first-subject)
7922         (gnus-summary-display-article (gnus-summary-article-number)))
7923     (gnus-summary-position-point)))
7924
7925 (defun gnus-summary-best-unread-article (&optional arg)
7926   "Select the unread article with the highest score.
7927 If given a prefix argument, select the next unread article that has a
7928 score higher than the default score."
7929   (interactive "P")
7930   (let ((article (if arg
7931                      (gnus-summary-better-unread-subject)
7932                    (gnus-summary-best-unread-subject))))
7933     (if article
7934         (gnus-summary-goto-article article)
7935       (error "No unread articles"))))
7936
7937 (defun gnus-summary-best-unread-subject ()
7938   "Select the unread subject with the highest score."
7939   (interactive)
7940   (let ((best -1000000)
7941         (data gnus-newsgroup-data)
7942         article score)
7943     (while data
7944       (and (gnus-data-unread-p (car data))
7945            (> (setq score
7946                     (gnus-summary-article-score (gnus-data-number (car data))))
7947               best)
7948            (setq best score
7949                  article (gnus-data-number (car data))))
7950       (setq data (cdr data)))
7951     (when article
7952       (gnus-summary-goto-subject article))
7953     (gnus-summary-position-point)
7954     article))
7955
7956 (defun gnus-summary-better-unread-subject ()
7957   "Select the first unread subject that has a score over the default score."
7958   (interactive)
7959   (let ((data gnus-newsgroup-data)
7960         article score)
7961     (while (and (setq article (gnus-data-number (car data)))
7962                 (or (gnus-data-read-p (car data))
7963                     (not (> (gnus-summary-article-score article)
7964                             gnus-summary-default-score))))
7965       (setq data (cdr data)))
7966     (when article
7967       (gnus-summary-goto-subject article))
7968     (gnus-summary-position-point)
7969     article))
7970
7971 (defun gnus-summary-last-subject ()
7972   "Go to the last displayed subject line in the group."
7973   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7974     (when article
7975       (gnus-summary-goto-subject article))))
7976
7977 (defun gnus-summary-goto-article (article &optional all-headers force)
7978   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7979 If ALL-HEADERS is non-nil, no header lines are hidden.
7980 If FORCE, go to the article even if it isn't displayed.  If FORCE
7981 is a number, it is the line the article is to be displayed on."
7982   (interactive
7983    (list
7984     (gnus-completing-read
7985      "Article number or Message-ID"
7986      (mapcar 'int-to-string gnus-newsgroup-limit))
7987     current-prefix-arg
7988     t))
7989   (prog1
7990       (if (and (stringp article)
7991                (string-match "@\\|%40" article))
7992           (gnus-summary-refer-article article)
7993         (when (stringp article)
7994           (setq article (string-to-number article)))
7995         (if (gnus-summary-goto-subject article force)
7996             (gnus-summary-display-article article all-headers)
7997           (gnus-message 4 "Couldn't go to article %s" article) nil))
7998     (gnus-summary-position-point)))
7999
8000 (defun gnus-summary-goto-last-article ()
8001   "Go to the previously read article."
8002   (interactive)
8003   (prog1
8004       (when gnus-last-article
8005         (gnus-summary-goto-article gnus-last-article nil t))
8006     (gnus-summary-position-point)))
8007
8008 (defun gnus-summary-pop-article (number)
8009   "Pop one article off the history and go to the previous.
8010 NUMBER articles will be popped off."
8011   (interactive "p")
8012   (let (to)
8013     (setq gnus-newsgroup-history
8014           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8015     (if to
8016         (gnus-summary-goto-article (car to) nil t)
8017       (error "Article history empty")))
8018   (gnus-summary-position-point))
8019
8020 ;; Summary commands and functions for limiting the summary buffer.
8021
8022 (defun gnus-summary-limit-to-articles (n)
8023   "Limit the summary buffer to the next N articles.
8024 If not given a prefix, use the process marked articles instead."
8025   (interactive "P")
8026   (prog1
8027       (let ((articles (gnus-summary-work-articles n)))
8028         (setq gnus-newsgroup-processable nil)
8029         (gnus-summary-limit articles))
8030     (gnus-summary-position-point)))
8031
8032 (defun gnus-summary-pop-limit (&optional total)
8033   "Restore the previous limit.
8034 If given a prefix, remove all limits."
8035   (interactive "P")
8036   (when total
8037     (setq gnus-newsgroup-limits
8038           (list (mapcar (lambda (h) (mail-header-number h))
8039                         gnus-newsgroup-headers))))
8040   (unless gnus-newsgroup-limits
8041     (error "No limit to pop"))
8042   (prog1
8043       (gnus-summary-limit nil 'pop)
8044     (gnus-summary-position-point)))
8045
8046 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8047   "Limit the summary buffer to articles that have subjects that match a regexp.
8048 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8049   (interactive
8050    (list (read-string (if current-prefix-arg
8051                           "Exclude subject (regexp): "
8052                         "Limit to subject (regexp): "))
8053          nil current-prefix-arg))
8054   (unless header
8055     (setq header "subject"))
8056   (when (not (equal "" subject))
8057     (prog1
8058         (let ((articles (gnus-summary-find-matching
8059                          (or header "subject") subject 'all nil nil
8060                          not-matching)))
8061           (unless articles
8062             (error "Found no matches for \"%s\"" subject))
8063           (gnus-summary-limit articles))
8064       (gnus-summary-position-point))))
8065
8066 (defun gnus-summary-limit-to-author (from &optional not-matching)
8067   "Limit the summary buffer to articles that have authors that match a regexp.
8068 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8069   (interactive
8070    (list (read-string (if current-prefix-arg
8071                           "Exclude author (regexp): "
8072                         "Limit to author (regexp): "))
8073          current-prefix-arg))
8074   (gnus-summary-limit-to-subject from "from" not-matching))
8075
8076 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8077   "Limit the summary buffer to articles with the given RECIPIENT.
8078
8079 If NOT-MATCHING, exclude RECIPIENT.
8080
8081 To and Cc headers are checked.  You need to include them in
8082 `nnmail-extra-headers'."
8083   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8084   (interactive
8085    (list (read-string (format "%s recipient (regexp): "
8086                               (if current-prefix-arg "Exclude" "Limit to")))
8087          current-prefix-arg))
8088   (when (not (equal "" recipient))
8089     (prog1 (let* ((to
8090                    (if (memq 'To nnmail-extra-headers)
8091                        (gnus-summary-find-matching
8092                         (cons 'extra 'To) recipient 'all nil nil
8093                         not-matching)
8094                      (gnus-message
8095                       1 "`To' isn't present in `nnmail-extra-headers'")
8096                      (sit-for 1)
8097                      nil))
8098                   (cc
8099                    (if (memq 'Cc nnmail-extra-headers)
8100                        (gnus-summary-find-matching
8101                         (cons 'extra 'Cc) recipient 'all nil nil
8102                         not-matching)
8103                      (gnus-message
8104                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8105                      (sit-for 1)
8106                      nil))
8107                   (articles
8108                    (if not-matching
8109                        ;; We need the numbers that are in both lists:
8110                        (mapcar (lambda (a)
8111                                  (and (memq a to) a))
8112                                cc)
8113                      (nconc to cc))))
8114              (unless articles
8115                (error "Found no matches for \"%s\"" recipient))
8116              (gnus-summary-limit articles))
8117       (gnus-summary-position-point))))
8118
8119 (defun gnus-summary-limit-to-address (address &optional not-matching)
8120   "Limit the summary buffer to articles with the given ADDRESS.
8121
8122 If NOT-MATCHING, exclude ADDRESS.
8123
8124 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8125 in `nnmail-extra-headers'."
8126   (interactive
8127    (list (read-string (format "%s address (regexp): "
8128                               (if current-prefix-arg "Exclude" "Limit to")))
8129          current-prefix-arg))
8130   (when (not (equal "" address))
8131     (prog1 (let* ((to
8132                    (if (memq 'To nnmail-extra-headers)
8133                        (gnus-summary-find-matching
8134                         (cons 'extra 'To) address 'all nil nil
8135                         not-matching)
8136                      (gnus-message
8137                       1 "`To' isn't present in `nnmail-extra-headers'")
8138                      (sit-for 1)
8139                      t))
8140                   (cc
8141                    (if (memq 'Cc nnmail-extra-headers)
8142                        (gnus-summary-find-matching
8143                         (cons 'extra 'Cc) address 'all nil nil
8144                         not-matching)
8145                      (gnus-message
8146                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8147                      (sit-for 1)
8148                      t))
8149                   (from
8150                    (gnus-summary-find-matching "from" address
8151                                                'all nil nil not-matching))
8152                   (articles
8153                    (if not-matching
8154                        ;; We need the numbers that are in all lists:
8155                        (if (eq cc t)
8156                            (if (eq to t)
8157                                from
8158                              (mapcar (lambda (a) (car (memq a from))) to))
8159                          (if (eq to t)
8160                              (mapcar (lambda (a) (car (memq a from))) cc)
8161                            (mapcar (lambda (a) (car (memq a from)))
8162                                    (mapcar (lambda (a) (car (memq a to)))
8163                                            cc))))
8164                      (nconc (if (eq to t) nil to)
8165                             (if (eq cc t) nil cc)
8166                             from))))
8167              (unless articles
8168                (error "Found no matches for \"%s\"" address))
8169              (gnus-summary-limit articles))
8170       (gnus-summary-position-point))))
8171
8172 (defun gnus-summary-limit-strange-charsets-predicate (header)
8173   (when (fboundp 'char-charset)
8174     (let ((string (concat (mail-header-subject header)
8175                           (mail-header-from header)))
8176           charset found)
8177       (dotimes (i (1- (length string)))
8178         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8179         (when (string-match "unicode\\|big\\|japanese" charset)
8180           (setq found t)))
8181       found)))
8182
8183 (defun gnus-summary-limit-to-predicate (predicate)
8184   "Limit to articles where PREDICATE returns non-nil.
8185 PREDICATE will be called with the header structures of the
8186 articles."
8187   (let ((articles nil)
8188         (case-fold-search t))
8189     (dolist (header gnus-newsgroup-headers)
8190       (when (funcall predicate header)
8191         (push (mail-header-number header) articles)))
8192     (gnus-summary-limit (nreverse articles))))
8193
8194 (defun gnus-summary-limit-to-age (age &optional younger-p)
8195   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8196 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8197 articles that are younger than AGE days."
8198   (interactive
8199    (let ((younger current-prefix-arg)
8200          (days-got nil)
8201          days)
8202      (while (not days-got)
8203        (setq days (if younger
8204                       (read-string "Limit to articles younger than (in days, older when negative): ")
8205                     (read-string
8206                      "Limit to articles older than (in days, younger when negative): ")))
8207        (when (> (length days) 0)
8208          (setq days (read days)))
8209        (if (numberp days)
8210            (progn
8211              (setq days-got t)
8212              (when (< days 0)
8213                (setq younger (not younger))
8214                (setq days (* days -1))))
8215          (message "Please enter a number.")
8216          (sleep-for 1)))
8217      (list days younger)))
8218   (prog1
8219       (let ((data gnus-newsgroup-data)
8220             (cutoff (days-to-time age))
8221             articles d date is-younger)
8222         (while (setq d (pop data))
8223           (when (and (vectorp (gnus-data-header d))
8224                      (setq date (mail-header-date (gnus-data-header d))))
8225             (setq is-younger (time-less-p
8226                               (time-since (gnus-date-get-time date))
8227                               cutoff))
8228             (when (if younger-p
8229                       is-younger
8230                     (not is-younger))
8231               (push (gnus-data-number d) articles))))
8232         (gnus-summary-limit (nreverse articles)))
8233     (gnus-summary-position-point)))
8234
8235 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8236   "Limit the summary buffer to articles that match an 'extra' header."
8237   (interactive
8238    (let ((header
8239           (intern
8240            (gnus-completing-read
8241             (if current-prefix-arg
8242                 "Exclude extra header"
8243               "Limit extra header")
8244             (mapcar 'symbol-name gnus-extra-headers)
8245             t nil nil
8246             (symbol-name (car gnus-extra-headers))))))
8247      (list header
8248            (read-string (format "%s header %s (regexp): "
8249                                 (if current-prefix-arg "Exclude" "Limit to")
8250                                 header))
8251            current-prefix-arg)))
8252   (when (not (equal "" regexp))
8253     (prog1
8254         (let ((articles (gnus-summary-find-matching
8255                          (cons 'extra header) regexp 'all nil nil
8256                          not-matching)))
8257           (unless articles
8258             (error "Found no matches for \"%s\"" regexp))
8259           (gnus-summary-limit articles))
8260       (gnus-summary-position-point))))
8261
8262 (defun gnus-summary-limit-to-display-predicate ()
8263   "Limit the summary buffer to the predicated in the `display' group parameter."
8264   (interactive)
8265   (unless gnus-newsgroup-display
8266     (error "There is no `display' group parameter"))
8267   (let (articles)
8268     (dolist (number gnus-newsgroup-articles)
8269       (when (funcall gnus-newsgroup-display)
8270         (push number articles)))
8271     (gnus-summary-limit articles))
8272   (gnus-summary-position-point))
8273
8274 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8275 (make-obsolete
8276  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8277
8278 (defun gnus-summary-limit-to-unread (&optional all)
8279   "Limit the summary buffer to articles that are not marked as read.
8280 If ALL is non-nil, limit strictly to unread articles."
8281   (interactive "P")
8282   (if all
8283       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8284     (gnus-summary-limit-to-marks
8285      ;; Concat all the marks that say that an article is read and have
8286      ;; those removed.
8287      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8288            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8289            gnus-low-score-mark gnus-expirable-mark
8290            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8291            gnus-duplicate-mark)
8292      'reverse)))
8293
8294 (defun gnus-summary-limit-to-headers (match &optional reverse)
8295   "Limit the summary buffer to articles that have headers that match MATCH.
8296 If REVERSE (the prefix), limit to articles that don't match."
8297   (interactive "sMatch headers (regexp): \nP")
8298   (gnus-summary-limit-to-bodies match reverse t))
8299
8300 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8301   "Limit the summary buffer to articles that have bodies that match MATCH.
8302 If REVERSE (the prefix), limit to articles that don't match."
8303   (interactive "sMatch body (regexp): \nP")
8304   (let ((articles nil)
8305         (gnus-select-article-hook nil)  ;Disable hook.
8306         (gnus-article-prepare-hook nil)
8307         (gnus-use-article-prefetch nil)
8308         (gnus-keep-backlog nil)
8309         (gnus-break-pages nil)
8310         (gnus-summary-display-arrow nil)
8311         (gnus-updated-mode-lines nil)
8312         (gnus-auto-center-summary nil)
8313         (gnus-display-mime-function nil))
8314     (dolist (data gnus-newsgroup-data)
8315       (let (gnus-mark-article-hook)
8316         (gnus-summary-select-article t t nil (gnus-data-number data)))
8317       (with-current-buffer gnus-article-buffer
8318         (article-goto-body)
8319         (let* ((case-fold-search t)
8320                (found (if headersp
8321                           (re-search-backward match nil t)
8322                         (re-search-forward match nil t))))
8323           (when (or (and found
8324                          (not reverse))
8325                     (and (not found)
8326                          reverse))
8327             (push (gnus-data-number data) articles)))))
8328     (if (not articles)
8329         (message "No messages matched")
8330       (gnus-summary-limit articles)))
8331   (gnus-summary-position-point))
8332
8333 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8334   "Limit the summary buffer to articles that aren't part on any thread.
8335 If THREADSP (the prefix), limit to articles that are in threads."
8336   (interactive "P")
8337   (let ((articles nil)
8338         thread-articles
8339         threads)
8340     (dolist (thread gnus-newsgroup-threads)
8341       (if (stringp (car thread))
8342           (dolist (thread (cdr thread))
8343             (push thread threads))
8344         (push thread threads)))
8345     (dolist (thread threads)
8346       (setq thread-articles (gnus-articles-in-thread thread))
8347       (when (or (and threadsp
8348                      (> (length thread-articles) 1))
8349                 (and (not threadsp)
8350                      (= (length thread-articles) 1)))
8351         (setq articles (nconc thread-articles articles))))
8352     (if (not articles)
8353         (message "No messages matched")
8354       (gnus-summary-limit articles))
8355     (gnus-summary-position-point)))
8356
8357 (defun gnus-summary-limit-to-replied (&optional unreplied)
8358   "Limit the summary buffer to replied articles.
8359 If UNREPLIED (the prefix), limit to unreplied articles."
8360   (interactive "P")
8361   (if unreplied
8362       (gnus-summary-limit
8363        (gnus-set-difference gnus-newsgroup-articles
8364         gnus-newsgroup-replied))
8365     (gnus-summary-limit gnus-newsgroup-replied))
8366   (gnus-summary-position-point))
8367
8368 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8369 (make-obsolete 'gnus-summary-delete-marked-with
8370                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8371
8372 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8373   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8374 If REVERSE, limit the summary buffer to articles that are marked
8375 with MARKS.  MARKS can either be a string of marks or a list of marks.
8376 Returns how many articles were removed."
8377   (interactive "sMarks: ")
8378   (gnus-summary-limit-to-marks marks t))
8379
8380 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8381   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8382 If REVERSE (the prefix), limit the summary buffer to articles that are
8383 not marked with MARKS.  MARKS can either be a string of marks or a
8384 list of marks.
8385 Returns how many articles were removed."
8386   (interactive "sMarks: \nP")
8387   (prog1
8388       (let ((data gnus-newsgroup-data)
8389             (marks (if (listp marks) marks
8390                      (append marks nil))) ; Transform to list.
8391             articles)
8392         (while data
8393           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8394                   (memq (gnus-data-mark (car data)) marks))
8395             (push (gnus-data-number (car data)) articles))
8396           (setq data (cdr data)))
8397         (gnus-summary-limit articles))
8398     (gnus-summary-position-point)))
8399
8400 (defun gnus-summary-limit-to-score (score)
8401   "Limit to articles with score at or above SCORE."
8402   (interactive "NLimit to articles with score of at least: ")
8403   (let ((data gnus-newsgroup-data)
8404         articles)
8405     (while data
8406       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8407                 score)
8408         (push (gnus-data-number (car data)) articles))
8409       (setq data (cdr data)))
8410     (prog1
8411         (gnus-summary-limit articles)
8412       (gnus-summary-position-point))))
8413
8414 (defun gnus-summary-limit-to-unseen ()
8415   "Limit to unseen articles."
8416   (interactive)
8417   (prog1
8418       (gnus-summary-limit gnus-newsgroup-unseen)
8419     (gnus-summary-position-point)))
8420
8421 (defun gnus-summary-limit-include-thread (id)
8422   "Display all the hidden articles that is in the thread with ID in it.
8423 When called interactively, ID is the Message-ID of the current
8424 article."
8425   (interactive (list (mail-header-id (gnus-summary-article-header))))
8426   (let ((articles (gnus-articles-in-thread
8427                    (gnus-id-to-thread (gnus-root-id id)))))
8428     (prog1
8429         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8430       (gnus-summary-limit-include-matching-articles
8431        "subject"
8432        (regexp-quote (gnus-simplify-subject-re
8433                       (mail-header-subject (gnus-id-to-header id)))))
8434       (gnus-summary-position-point))))
8435
8436 (defun gnus-summary-limit-include-matching-articles (header regexp)
8437   "Display all the hidden articles that have HEADERs that match REGEXP."
8438   (interactive (list (read-string "Match on header: ")
8439                      (read-string "Regexp: ")))
8440   (let ((articles (gnus-find-matching-articles header regexp)))
8441     (prog1
8442         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8443       (gnus-summary-position-point))))
8444
8445 (defun gnus-summary-insert-dormant-articles ()
8446   "Insert all the dormant articles for this group into the current buffer."
8447   (interactive)
8448   (let ((gnus-verbose (max 6 gnus-verbose)))
8449     (if (not gnus-newsgroup-dormant)
8450         (gnus-message 3 "No dormant articles for this group")
8451       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8452
8453 (defun gnus-summary-insert-ticked-articles ()
8454   "Insert ticked articles for this group into the current buffer."
8455   (interactive)
8456   (let ((gnus-verbose (max 6 gnus-verbose)))
8457     (if (not gnus-newsgroup-marked)
8458         (gnus-message 3 "No ticked articles for this group")
8459       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8460
8461 (defun gnus-summary-limit-include-dormant ()
8462   "Display all the hidden articles that are marked as dormant.
8463 Note that this command only works on a subset of the articles currently
8464 fetched for this group."
8465   (interactive)
8466   (unless gnus-newsgroup-dormant
8467     (error "There are no dormant articles in this group"))
8468   (prog1
8469       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8470     (gnus-summary-position-point)))
8471
8472 (defun gnus-summary-limit-exclude-dormant ()
8473   "Hide all dormant articles."
8474   (interactive)
8475   (prog1
8476       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8477     (gnus-summary-position-point)))
8478
8479 (defun gnus-summary-limit-exclude-childless-dormant ()
8480   "Hide all dormant articles that have no children."
8481   (interactive)
8482   (let ((data (gnus-data-list t))
8483         articles d children)
8484     ;; Find all articles that are either not dormant or have
8485     ;; children.
8486     (while (setq d (pop data))
8487       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8488                 (and (setq children
8489                            (gnus-article-children (gnus-data-number d)))
8490                      (let (found)
8491                        (while children
8492                          (when (memq (car children) articles)
8493                            (setq children nil
8494                                  found t))
8495                          (pop children))
8496                        found)))
8497         (push (gnus-data-number d) articles)))
8498     ;; Do the limiting.
8499     (prog1
8500         (gnus-summary-limit articles)
8501       (gnus-summary-position-point))))
8502
8503 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8504   "Mark all unread excluded articles as read.
8505 If ALL, mark even excluded ticked and dormants as read."
8506   (interactive "P")
8507   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8508   (let ((articles (gnus-sorted-ndifference
8509                    (sort
8510                     (mapcar (lambda (h) (mail-header-number h))
8511                             gnus-newsgroup-headers)
8512                     '<)
8513                    gnus-newsgroup-limit))
8514         article)
8515     (setq gnus-newsgroup-unreads
8516           (gnus-sorted-intersection gnus-newsgroup-unreads
8517                                     gnus-newsgroup-limit))
8518     (if all
8519         (setq gnus-newsgroup-dormant nil
8520               gnus-newsgroup-marked nil
8521               gnus-newsgroup-reads
8522               (nconc
8523                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8524                gnus-newsgroup-reads))
8525       (while (setq article (pop articles))
8526         (unless (or (memq article gnus-newsgroup-dormant)
8527                     (memq article gnus-newsgroup-marked))
8528           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8529
8530 (defun gnus-summary-limit (articles &optional pop)
8531   (if pop
8532       ;; We pop the previous limit off the stack and use that.
8533       (setq articles (car gnus-newsgroup-limits)
8534             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8535     ;; We use the new limit, so we push the old limit on the stack.
8536     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8537   ;; Set the limit.
8538   (setq gnus-newsgroup-limit articles)
8539   (let ((total (length gnus-newsgroup-data))
8540         (data (gnus-data-find-list (gnus-summary-article-number)))
8541         (gnus-summary-mark-below nil)   ; Inhibit this.
8542         found)
8543     ;; This will do all the work of generating the new summary buffer
8544     ;; according to the new limit.
8545     (gnus-summary-prepare)
8546     ;; Hide any threads, possibly.
8547     (gnus-summary-maybe-hide-threads)
8548     ;; Try to return to the article you were at, or one in the
8549     ;; neighborhood.
8550     (when data
8551       ;; We try to find some article after the current one.
8552       (while data
8553         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8554           (setq data nil
8555                 found t))
8556         (setq data (cdr data))))
8557     (unless found
8558       ;; If there is no data, that means that we were after the last
8559       ;; article.  The same goes when we can't find any articles
8560       ;; after the current one.
8561       (goto-char (point-max))
8562       (gnus-summary-find-prev))
8563     (gnus-set-mode-line 'summary)
8564     ;; We return how many articles were removed from the summary
8565     ;; buffer as a result of the new limit.
8566     (- total (length gnus-newsgroup-data))))
8567
8568 (defsubst gnus-invisible-cut-children (threads)
8569   (let ((num 0))
8570     (while threads
8571       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8572         (incf num))
8573       (pop threads))
8574     (< num 2)))
8575
8576 (defsubst gnus-cut-thread (thread)
8577   "Go forwards in the thread until we find an article that we want to display."
8578   (when (or (eq gnus-fetch-old-headers 'some)
8579             (eq gnus-fetch-old-headers 'invisible)
8580             (numberp gnus-fetch-old-headers)
8581             (eq gnus-build-sparse-threads 'some)
8582             (eq gnus-build-sparse-threads 'more))
8583     ;; Deal with old-fetched headers and sparse threads.
8584     (while (and
8585             thread
8586             (or
8587              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8588              (gnus-summary-article-ancient-p
8589               (mail-header-number (car thread))))
8590             (if (or (<= (length (cdr thread)) 1)
8591                     (eq gnus-fetch-old-headers 'invisible))
8592                 (setq gnus-newsgroup-limit
8593                       (delq (mail-header-number (car thread))
8594                             gnus-newsgroup-limit)
8595                       thread (cadr thread))
8596               (when (gnus-invisible-cut-children (cdr thread))
8597                 (let ((th (cdr thread)))
8598                   (while th
8599                     (if (memq (mail-header-number (caar th))
8600                               gnus-newsgroup-limit)
8601                         (setq thread (car th)
8602                               th nil)
8603                       (setq th (cdr th))))))))))
8604   thread)
8605
8606 (defun gnus-cut-threads (threads)
8607   "Cut off all uninteresting articles from the beginning of THREADS."
8608   (when (or (eq gnus-fetch-old-headers 'some)
8609             (eq gnus-fetch-old-headers 'invisible)
8610             (numberp gnus-fetch-old-headers)
8611             (eq gnus-build-sparse-threads 'some)
8612             (eq gnus-build-sparse-threads 'more))
8613     (let ((th threads))
8614       (while th
8615         (setcar th (gnus-cut-thread (car th)))
8616         (setq th (cdr th)))))
8617   ;; Remove nixed out threads.
8618   (delq nil threads))
8619
8620 (defun gnus-summary-initial-limit (&optional show-if-empty)
8621   "Figure out what the initial limit is supposed to be on group entry.
8622 This entails weeding out unwanted dormants, low-scored articles,
8623 fetch-old-headers verbiage, and so on."
8624   ;; Most groups have nothing to remove.
8625   (unless (or gnus-inhibit-limiting
8626               (and (null gnus-newsgroup-dormant)
8627                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8628                    (not (eq gnus-fetch-old-headers 'some))
8629                    (not (numberp gnus-fetch-old-headers))
8630                    (not (eq gnus-fetch-old-headers 'invisible))
8631                    (null gnus-summary-expunge-below)
8632                    (not (eq gnus-build-sparse-threads 'some))
8633                    (not (eq gnus-build-sparse-threads 'more))
8634                    (null gnus-thread-expunge-below)))
8635     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8636     (setq gnus-newsgroup-limit nil)
8637     (mapatoms
8638      (lambda (node)
8639        (unless (car (symbol-value node))
8640          ;; These threads have no parents -- they are roots.
8641          (let ((nodes (cdr (symbol-value node)))
8642                thread)
8643            (while nodes
8644              (if (and gnus-thread-expunge-below
8645                       (< (gnus-thread-total-score (car nodes))
8646                          gnus-thread-expunge-below))
8647                  (gnus-expunge-thread (pop nodes))
8648                (setq thread (pop nodes))
8649                (gnus-summary-limit-children thread))))))
8650      gnus-newsgroup-dependencies)
8651     ;; If this limitation resulted in an empty group, we might
8652     ;; pop the previous limit and use it instead.
8653     (when (and (not gnus-newsgroup-limit)
8654                show-if-empty)
8655       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8656     gnus-newsgroup-limit))
8657
8658 (defun gnus-summary-limit-children (thread)
8659   "Return 1 if this subthread is visible and 0 if it is not."
8660   ;; First we get the number of visible children to this thread.  This
8661   ;; is done by recursing down the thread using this function, so this
8662   ;; will really go down to a leaf article first, before slowly
8663   ;; working its way up towards the root.
8664   (when thread
8665     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8666            (children
8667            (if (cdr thread)
8668                (apply '+ (mapcar 'gnus-summary-limit-children
8669                                  (cdr thread)))
8670              0))
8671           (number (mail-header-number (car thread)))
8672           score)
8673       (if (and
8674            (not (memq number gnus-newsgroup-marked))
8675            (or
8676             ;; If this article is dormant and has absolutely no visible
8677             ;; children, then this article isn't visible.
8678             (and (memq number gnus-newsgroup-dormant)
8679                  (zerop children))
8680             ;; If this is "fetch-old-headered" and there is no
8681             ;; visible children, then we don't want this article.
8682             (and (or (eq gnus-fetch-old-headers 'some)
8683                      (numberp gnus-fetch-old-headers))
8684                  (gnus-summary-article-ancient-p number)
8685                  (zerop children))
8686             ;; If this is "fetch-old-headered" and `invisible', then
8687             ;; we don't want this article.
8688             (and (eq gnus-fetch-old-headers 'invisible)
8689                  (gnus-summary-article-ancient-p number))
8690             ;; If this is a sparsely inserted article with no children,
8691             ;; we don't want it.
8692             (and (eq gnus-build-sparse-threads 'some)
8693                  (gnus-summary-article-sparse-p number)
8694                  (zerop children))
8695             ;; If we use expunging, and this article is really
8696             ;; low-scored, then we don't want this article.
8697             (when (and gnus-summary-expunge-below
8698                        (< (setq score
8699                                 (or (cdr (assq number gnus-newsgroup-scored))
8700                                     gnus-summary-default-score))
8701                           gnus-summary-expunge-below))
8702               ;; We increase the expunge-tally here, but that has
8703               ;; nothing to do with the limits, really.
8704               (incf gnus-newsgroup-expunged-tally)
8705               ;; We also mark as read here, if that's wanted.
8706               (when (and gnus-summary-mark-below
8707                          (< score gnus-summary-mark-below))
8708                 (setq gnus-newsgroup-unreads
8709                       (delq number gnus-newsgroup-unreads))
8710                 (if gnus-newsgroup-auto-expire
8711                     (push number gnus-newsgroup-expirable)
8712                   (push (cons number gnus-low-score-mark)
8713                         gnus-newsgroup-reads)))
8714               t)
8715             ;; Do the `display' group parameter.
8716             (and gnus-newsgroup-display
8717                  (not (funcall gnus-newsgroup-display)))))
8718           ;; Nope, invisible article.
8719           0
8720         ;; Ok, this article is to be visible, so we add it to the limit
8721         ;; and return 1.
8722         (push number gnus-newsgroup-limit)
8723         1))))
8724
8725 (defun gnus-expunge-thread (thread)
8726   "Mark all articles in THREAD as read."
8727   (let* ((number (mail-header-number (car thread))))
8728     (incf gnus-newsgroup-expunged-tally)
8729     ;; We also mark as read here, if that's wanted.
8730     (setq gnus-newsgroup-unreads
8731           (delq number gnus-newsgroup-unreads))
8732     (if gnus-newsgroup-auto-expire
8733         (push number gnus-newsgroup-expirable)
8734       (push (cons number gnus-low-score-mark)
8735             gnus-newsgroup-reads)))
8736   ;; Go recursively through all subthreads.
8737   (mapcar 'gnus-expunge-thread (cdr thread)))
8738
8739 ;; Summary article oriented commands
8740
8741 (defun gnus-summary-refer-parent-article (n)
8742   "Refer parent article N times.
8743 If N is negative, go to ancestor -N instead.
8744 The difference between N and the number of articles fetched is returned."
8745   (interactive "p")
8746   (let ((skip 1)
8747         error header ref)
8748     (when (not (natnump n))
8749       (setq skip (abs n)
8750             n 1))
8751     (while (and (> n 0)
8752                 (not error))
8753       (setq header (gnus-summary-article-header))
8754       (if (and (eq (mail-header-number header)
8755                    (cdr gnus-article-current))
8756                (equal gnus-newsgroup-name
8757                       (car gnus-article-current)))
8758           ;; If we try to find the parent of the currently
8759           ;; displayed article, then we take a look at the actual
8760           ;; References header, since this is slightly more
8761           ;; reliable than the References field we got from the
8762           ;; server.
8763           (with-current-buffer gnus-original-article-buffer
8764             (nnheader-narrow-to-headers)
8765             (unless (setq ref (message-fetch-field "references"))
8766               (when (setq ref (message-fetch-field "in-reply-to"))
8767                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8768             (widen))
8769         (setq ref
8770               ;; It's not the current article, so we take a bet on
8771               ;; the value we got from the server.
8772               (mail-header-references header)))
8773       (if (and ref
8774                (not (equal ref "")))
8775           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8776             (gnus-message 1 "Couldn't find parent"))
8777         (gnus-message 1 "No references in article %d"
8778                       (gnus-summary-article-number))
8779         (setq error t))
8780       (decf n))
8781     (gnus-summary-position-point)
8782     n))
8783
8784 (defun gnus-summary-refer-references ()
8785   "Fetch all articles mentioned in the References header.
8786 Return the number of articles fetched."
8787   (interactive)
8788   (let ((ref (mail-header-references (gnus-summary-article-header)))
8789         (current (gnus-summary-article-number))
8790         (n 0))
8791     (if (or (not ref)
8792             (equal ref ""))
8793         (error "No References in the current article")
8794       ;; For each Message-ID in the References header...
8795       (while (string-match "<[^>]*>" ref)
8796         (incf n)
8797         ;; ... fetch that article.
8798         (gnus-summary-refer-article
8799          (prog1 (match-string 0 ref)
8800            (setq ref (substring ref (match-end 0))))))
8801       (gnus-summary-goto-subject current)
8802       (gnus-summary-position-point)
8803       n)))
8804
8805 (defun gnus-summary-refer-thread (&optional limit)
8806   "Fetch all articles in the current thread.
8807 If LIMIT (the numerical prefix), fetch that many old headers instead
8808 of what's specified by the `gnus-refer-thread-limit' variable."
8809   (interactive "P")
8810   (let ((id (mail-header-id (gnus-summary-article-header)))
8811         (limit (if limit (prefix-numeric-value limit)
8812                  gnus-refer-thread-limit)))
8813     (unless (eq gnus-fetch-old-headers 'invisible)
8814       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8815       ;; Retrieve the headers and read them in.
8816       (if (eq (if (numberp limit)
8817                   (gnus-retrieve-headers
8818                    (list (min
8819                           (+ (mail-header-number
8820                               (gnus-summary-article-header))
8821                              limit)
8822                           gnus-newsgroup-end))
8823                    gnus-newsgroup-name (* limit 2))
8824                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8825                 ;; headers.
8826                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8827                                        gnus-newsgroup-name limit))
8828               'nov)
8829           (gnus-build-all-threads)
8830         (error "Can't fetch thread from back ends that don't support NOV"))
8831       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8832     (gnus-summary-limit-include-thread id)))
8833
8834 (defun gnus-summary-refer-article (message-id)
8835   "Fetch an article specified by MESSAGE-ID."
8836   (interactive "sMessage-ID: ")
8837   (when (and (stringp message-id)
8838              (not (zerop (length message-id))))
8839     (setq message-id (gnus-replace-in-string message-id " " ""))
8840     ;; Construct the correct Message-ID if necessary.
8841     ;; Suggested by tale@pawl.rpi.edu.
8842     (unless (string-match "^<" message-id)
8843       (setq message-id (concat "<" message-id)))
8844     (unless (string-match ">$" message-id)
8845       (setq message-id (concat message-id ">")))
8846     ;; People often post MIDs from URLs, so unhex it:
8847     (unless (string-match "@" message-id)
8848       (setq message-id (gnus-url-unhex-string message-id)))
8849     (let* ((header (gnus-id-to-header message-id))
8850            (sparse (and header
8851                         (gnus-summary-article-sparse-p
8852                          (mail-header-number header))
8853                         (memq (mail-header-number header)
8854                               gnus-newsgroup-limit)))
8855            number)
8856       (cond
8857        ;; If the article is present in the buffer we just go to it.
8858        ((and header
8859              (or (not (gnus-summary-article-sparse-p
8860                        (mail-header-number header)))
8861                  sparse))
8862         (prog1
8863             (gnus-summary-goto-article
8864              (mail-header-number header) nil t)
8865           (when sparse
8866             (gnus-summary-update-article (mail-header-number header)))))
8867        (t
8868         ;; We fetch the article.
8869         (catch 'found
8870           (dolist (gnus-override-method (gnus-refer-article-methods))
8871             (when (and (gnus-check-server gnus-override-method)
8872                        ;; Fetch the header,
8873                        (setq number (gnus-summary-insert-subject message-id)))
8874               ;; and display the article.
8875               (gnus-summary-select-article nil nil nil number)
8876               (throw 'found t)))
8877           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8878
8879 (defun gnus-refer-article-methods ()
8880   "Return a list of referable methods."
8881   (cond
8882    ;; No method, so we default to current and native.
8883    ((null gnus-refer-article-method)
8884     (list gnus-current-select-method gnus-select-method))
8885    ;; Current.
8886    ((eq 'current gnus-refer-article-method)
8887     (list gnus-current-select-method))
8888    ;; List of select methods.
8889    ((not (and (symbolp (car gnus-refer-article-method))
8890               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8891     (let (out)
8892       (dolist (method gnus-refer-article-method)
8893         (push (if (eq 'current method)
8894                   gnus-current-select-method
8895                 method)
8896               out))
8897       (nreverse out)))
8898    ;; One single select method.
8899    (t
8900     (list gnus-refer-article-method))))
8901
8902 (defun gnus-summary-edit-parameters ()
8903   "Edit the group parameters of the current group."
8904   (interactive)
8905   (gnus-group-edit-group gnus-newsgroup-name 'params))
8906
8907 (defun gnus-summary-customize-parameters ()
8908   "Customize the group parameters of the current group."
8909   (interactive)
8910   (gnus-group-customize gnus-newsgroup-name))
8911
8912 (defun gnus-summary-enter-digest-group (&optional force)
8913   "Enter an nndoc group based on the current article.
8914 If FORCE, force a digest interpretation.  If not, try
8915 to guess what the document format is."
8916   (interactive "P")
8917   (let ((conf gnus-current-window-configuration))
8918     (save-window-excursion
8919       (save-excursion
8920         (let (gnus-article-prepare-hook
8921               gnus-display-mime-function
8922               gnus-break-pages)
8923           (gnus-summary-select-article))))
8924     (setq gnus-current-window-configuration conf)
8925     (let* ((name (format "%s-%d"
8926                          (gnus-group-prefixed-name
8927                           gnus-newsgroup-name (list 'nndoc ""))
8928                          (with-current-buffer gnus-summary-buffer
8929                            gnus-current-article)))
8930            (ogroup gnus-newsgroup-name)
8931            (params (append (gnus-info-params (gnus-get-info ogroup))
8932                            (list (cons 'to-group ogroup))
8933                            (list (cons 'parent-group ogroup))
8934                            (list (cons 'save-article-group ogroup))))
8935            (case-fold-search t)
8936            (buf (current-buffer))
8937            dig to-address)
8938       (with-current-buffer gnus-original-article-buffer
8939         ;; Have the digest group inherit the main mail address of
8940         ;; the parent article.
8941         (when (setq to-address (or (gnus-fetch-field "reply-to")
8942                                    (gnus-fetch-field "from")))
8943           (setq params
8944                 (append
8945                  (list (cons 'to-address
8946                              (funcall gnus-decode-encoded-address-function
8947                                       to-address))))))
8948         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8949         (insert-buffer-substring gnus-original-article-buffer)
8950         ;; Remove lines that may lead nndoc to misinterpret the
8951         ;; document type.
8952         (narrow-to-region
8953          (goto-char (point-min))
8954          (or (search-forward "\n\n" nil t) (point)))
8955         (goto-char (point-min))
8956         (delete-matching-lines "^Path:\\|^From ")
8957         (widen))
8958       (unwind-protect
8959           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8960                     (gnus-newsgroup-ephemeral-ignored-charsets
8961                      gnus-newsgroup-ignored-charsets))
8962                 (gnus-group-read-ephemeral-group
8963                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8964                               (nndoc-article-type
8965                                ,(if force 'mbox 'guess)))
8966                  t nil nil nil
8967                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8968                                                         "ADAPT")))))
8969               ;; Make all postings to this group go to the parent group.
8970               (nconc (gnus-info-params (gnus-get-info name))
8971                      params)
8972             ;; Couldn't select this doc group.
8973             (switch-to-buffer buf)
8974             (gnus-set-global-variables)
8975             (gnus-configure-windows 'summary)
8976             (gnus-message 3 "Article couldn't be entered?"))
8977         (kill-buffer dig)))))
8978
8979 (defun gnus-summary-read-document (n)
8980   "Open a new group based on the current article(s).
8981 This will allow you to read digests and other similar
8982 documents as newsgroups.
8983 Obeys the standard process/prefix convention."
8984   (interactive "P")
8985   (let* ((ogroup gnus-newsgroup-name)
8986          (params (append (gnus-info-params (gnus-get-info ogroup))
8987                          (list (cons 'to-group ogroup))))
8988          group egroup groups vgroup)
8989     (dolist (article (gnus-summary-work-articles n))
8990       (setq group (format "%s-%d" gnus-newsgroup-name article))
8991       (gnus-summary-remove-process-mark article)
8992       (when (gnus-summary-display-article article)
8993         (save-excursion ;;What for?
8994           (with-temp-buffer
8995             (insert-buffer-substring gnus-original-article-buffer)
8996             ;; Remove some headers that may lead nndoc to make
8997             ;; the wrong guess.
8998             (message-narrow-to-head)
8999             (goto-char (point-min))
9000             (delete-matching-lines "^Path:\\|^From ")
9001             (widen)
9002             (if (setq egroup
9003                       (gnus-group-read-ephemeral-group
9004                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9005                                      (nndoc-article-type guess))
9006                        t nil t))
9007                 (progn
9008                   ;; Make all postings to this group go to the parent group.
9009                   (nconc (gnus-info-params (gnus-get-info egroup))
9010                          params)
9011                   (push egroup groups))
9012               ;; Couldn't select this doc group.
9013               (gnus-error 3 "Article couldn't be entered"))))))
9014     ;; Now we have selected all the documents.
9015     (cond
9016      ((not groups)
9017       (error "None of the articles could be interpreted as documents"))
9018      ((gnus-group-read-ephemeral-group
9019        (setq vgroup (format
9020                      "nnvirtual:%s-%s" gnus-newsgroup-name
9021                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9022        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9023        t
9024        (cons (current-buffer) 'summary)))
9025      (t
9026       (error "Couldn't select virtual nndoc group")))))
9027
9028 (defun gnus-summary-isearch-article (&optional regexp-p)
9029   "Do incremental search forward on the current article.
9030 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9031   (interactive "P")
9032   (gnus-summary-select-article)
9033   (gnus-configure-windows 'article)
9034   (gnus-eval-in-buffer-window gnus-article-buffer
9035     (save-restriction
9036       (widen)
9037       (isearch-forward regexp-p))))
9038
9039 (defun gnus-summary-repeat-search-article-forward ()
9040   "Repeat the previous search forwards."
9041   (interactive)
9042   (unless gnus-last-search-regexp
9043     (error "No previous search"))
9044   (gnus-summary-search-article-forward gnus-last-search-regexp))
9045
9046 (defun gnus-summary-repeat-search-article-backward ()
9047   "Repeat the previous search backwards."
9048   (interactive)
9049   (unless gnus-last-search-regexp
9050     (error "No previous search"))
9051   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9052
9053 (defun gnus-summary-search-article-forward (regexp &optional backward)
9054   "Search for an article containing REGEXP forward.
9055 If BACKWARD, search backward instead."
9056   (interactive
9057    (list (read-string
9058           (format "Search article %s (regexp%s): "
9059                   (if current-prefix-arg "backward" "forward")
9060                   (if gnus-last-search-regexp
9061                       (concat ", default " gnus-last-search-regexp)
9062                     "")))
9063          current-prefix-arg))
9064   (if (string-equal regexp "")
9065       (setq regexp (or gnus-last-search-regexp ""))
9066     (setq gnus-last-search-regexp regexp)
9067     (setq gnus-article-before-search gnus-current-article))
9068   ;; Intentionally set gnus-last-article.
9069   (setq gnus-last-article gnus-article-before-search)
9070   (let ((gnus-last-article gnus-last-article))
9071     (if (gnus-summary-search-article regexp backward)
9072         (gnus-summary-show-thread)
9073       (signal 'search-failed (list regexp)))))
9074
9075 (defun gnus-summary-search-article-backward (regexp)
9076   "Search for an article containing REGEXP backward."
9077   (interactive
9078    (list (read-string
9079           (format "Search article backward (regexp%s): "
9080                   (if gnus-last-search-regexp
9081                       (concat ", default " gnus-last-search-regexp)
9082                     "")))))
9083   (gnus-summary-search-article-forward regexp 'backward))
9084
9085 (defun gnus-summary-search-article (regexp &optional backward)
9086   "Search for an article containing REGEXP.
9087 Optional argument BACKWARD means do search for backward.
9088 `gnus-select-article-hook' is not called during the search."
9089   ;; We have to require this here to make sure that the following
9090   ;; dynamic binding isn't shadowed by autoloading.
9091   (require 'gnus-async)
9092   (require 'gnus-art)
9093   (let ((gnus-select-article-hook nil)  ;Disable hook.
9094         (gnus-article-prepare-hook nil)
9095         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9096         (gnus-use-article-prefetch nil)
9097         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9098         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9099         (gnus-visual nil)
9100         (gnus-keep-backlog nil)
9101         (gnus-break-pages nil)
9102         (gnus-summary-display-arrow nil)
9103         (gnus-updated-mode-lines nil)
9104         (gnus-auto-center-summary nil)
9105         (sum (current-buffer))
9106         (gnus-display-mime-function nil)
9107         (found nil)
9108         point)
9109     (gnus-save-hidden-threads
9110       (gnus-summary-select-article)
9111       (set-buffer gnus-article-buffer)
9112       (goto-char (window-point (get-buffer-window (current-buffer))))
9113       (when backward
9114         (forward-line -1))
9115       (while (not found)
9116         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9117         (if (if backward
9118                 (re-search-backward regexp nil t)
9119               (re-search-forward regexp nil t))
9120             ;; We found the regexp.
9121             (progn
9122               (setq found 'found)
9123               (beginning-of-line)
9124               (set-window-start
9125                (get-buffer-window (current-buffer))
9126                (point))
9127               (forward-line 1)
9128               (set-window-point
9129                (get-buffer-window (current-buffer))
9130                (point))
9131               (set-buffer sum)
9132               (setq point (point)))
9133           ;; We didn't find it, so we go to the next article.
9134           (set-buffer sum)
9135           (setq found 'not)
9136           (while (eq found 'not)
9137             (if (not (if backward (gnus-summary-find-prev)
9138                        (gnus-summary-find-next)))
9139                 ;; No more articles.
9140                 (setq found t)
9141               ;; Select the next article and adjust point.
9142               (unless (gnus-summary-article-sparse-p
9143                        (gnus-summary-article-number))
9144                 (setq found nil)
9145                 (gnus-summary-select-article)
9146                 (set-buffer gnus-article-buffer)
9147                 (widen)
9148                 (goto-char (if backward (point-max) (point-min))))))))
9149       (gnus-message 7 ""))
9150     ;; Return whether we found the regexp.
9151     (when (eq found 'found)
9152       (goto-char point)
9153       (gnus-summary-show-thread)
9154       (gnus-summary-goto-subject gnus-current-article)
9155       (gnus-summary-position-point)
9156       t)))
9157
9158 (defun gnus-find-matching-articles (header regexp)
9159   "Return a list of all articles that match REGEXP on HEADER.
9160 This search includes all articles in the current group that Gnus has
9161 fetched headers for, whether they are displayed or not."
9162   (let ((articles nil)
9163         ;; Can't eta-reduce because it's a macro.
9164         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9165         (case-fold-search t))
9166     (dolist (header gnus-newsgroup-headers)
9167       (when (string-match regexp (funcall func header))
9168         (push (mail-header-number header) articles)))
9169     (nreverse articles)))
9170
9171 (defun gnus-summary-find-matching (header regexp &optional backward unread
9172                                           not-case-fold not-matching)
9173   "Return a list of all articles that match REGEXP on HEADER.
9174 The search stars on the current article and goes forwards unless
9175 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9176 If UNREAD is non-nil, only unread articles will
9177 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9178 in the comparisons. If NOT-MATCHING, return a list of all articles that
9179 not match REGEXP on HEADER."
9180   (let ((case-fold-search (not not-case-fold))
9181         articles d func)
9182     (if (consp header)
9183         (if (eq (car header) 'extra)
9184             (setq func
9185                   `(lambda (h)
9186                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9187                          "")))
9188           (error "%s is an invalid header" header))
9189       (unless (fboundp (intern (concat "mail-header-" header)))
9190         (error "%s is not a valid header" header))
9191       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9192     (dolist (d (if (eq backward 'all)
9193                    gnus-newsgroup-data
9194                  (gnus-data-find-list
9195                   (gnus-summary-article-number)
9196                   (gnus-data-list backward))))
9197       (when (and (or (not unread)       ; We want all articles...
9198                      (gnus-data-unread-p d)) ; Or just unreads.
9199                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9200                  (if not-matching
9201                      (not (string-match
9202                            regexp
9203                            (funcall func (gnus-data-header d))))
9204                    (string-match regexp
9205                                  (funcall func (gnus-data-header d)))))
9206         (push (gnus-data-number d) articles))) ; Success!
9207     (nreverse articles)))
9208
9209 (defun gnus-summary-execute-command (header regexp command &optional backward)
9210   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9211 If HEADER is an empty string (or nil), the match is done on the entire
9212 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9213   (interactive
9214    (list (let ((completion-ignore-case t))
9215            (gnus-completing-read
9216             "Header name"
9217             (mapcar 'symbol-name
9218                     (append
9219                      '(Number Subject From Lines Date
9220                        Message-ID Xref References Body)
9221                      gnus-extra-headers))
9222             'require-match))
9223          (read-string "Regexp: ")
9224          (read-key-sequence "Command: ")
9225          current-prefix-arg))
9226   (when (equal header "Body")
9227     (setq header ""))
9228   ;; Hidden thread subtrees must be searched as well.
9229   (gnus-summary-show-all-threads)
9230   ;; We don't want to change current point nor window configuration.
9231   (save-excursion
9232     (save-window-excursion
9233       (let (gnus-visual
9234             gnus-treat-strip-trailing-blank-lines
9235             gnus-treat-strip-leading-blank-lines
9236             gnus-treat-strip-multiple-blank-lines
9237             gnus-treat-hide-boring-headers
9238             gnus-treat-fold-newsgroups
9239             gnus-article-prepare-hook)
9240         (gnus-message 6 "Executing %s..." (key-description command))
9241         ;; We'd like to execute COMMAND interactively so as to give arguments.
9242         (gnus-execute header regexp
9243                       `(call-interactively ',(key-binding command))
9244                       backward)
9245         (gnus-message 6 "Executing %s...done" (key-description command))))))
9246
9247 (defun gnus-summary-beginning-of-article ()
9248   "Scroll the article back to the beginning."
9249   (interactive)
9250   (gnus-summary-select-article)
9251   (gnus-configure-windows 'article)
9252   (gnus-eval-in-buffer-window gnus-article-buffer
9253     (widen)
9254     (goto-char (point-min))
9255     (when gnus-break-pages
9256       (gnus-narrow-to-page))))
9257
9258 (defun gnus-summary-end-of-article ()
9259   "Scroll to the end of the article."
9260   (interactive)
9261   (gnus-summary-select-article)
9262   (gnus-configure-windows 'article)
9263   (gnus-eval-in-buffer-window gnus-article-buffer
9264     (widen)
9265     (goto-char (point-max))
9266     (recenter -3)
9267     (when gnus-break-pages
9268       (gnus-narrow-to-page))))
9269
9270 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9271   "Truncate to LEN and quote all \"(\"'s in STRING."
9272   (gnus-replace-in-string (if (and len (> (length string) len))
9273                               (substring string 0 len)
9274                             string)
9275                           "[()]" "\\\\\\&"))
9276
9277 (defun gnus-summary-print-article (&optional filename n)
9278   "Generate and print a PostScript image of the process-marked (mail) articles.
9279
9280 If used interactively, print the current article if none are
9281 process-marked.  With prefix arg, prompt the user for the name of the
9282 file to save in.
9283
9284 When used from Lisp, accept two optional args FILENAME and N.  N means
9285 to print the next N articles.  If N is negative, print the N previous
9286 articles.  If N is nil and articles have been marked with the process
9287 mark, print these instead.
9288
9289 If the optional first argument FILENAME is nil, send the image to the
9290 printer.  If FILENAME is a string, save the PostScript image in a file with
9291 that name.  If FILENAME is a number, prompt the user for the name of the file
9292 to save in."
9293   (interactive (list (ps-print-preprint current-prefix-arg)))
9294   (dolist (article (gnus-summary-work-articles n))
9295     (gnus-summary-select-article nil nil 'pseudo article)
9296     (gnus-eval-in-buffer-window gnus-article-buffer
9297       (gnus-print-buffer))
9298     (gnus-summary-remove-process-mark article))
9299   (ps-despool filename))
9300
9301 (defun gnus-print-buffer ()
9302   (let ((buffer (generate-new-buffer " *print*")))
9303     (unwind-protect
9304         (progn
9305           (copy-to-buffer buffer (point-min) (point-max))
9306           (set-buffer buffer)
9307           (gnus-remove-text-with-property 'gnus-decoration)
9308           (when (gnus-visual-p 'article-highlight 'highlight)
9309             ;; Copy-to-buffer doesn't copy overlay.  So redo
9310             ;; highlight.
9311             (let ((gnus-article-buffer buffer))
9312               (gnus-article-highlight-citation t)
9313               (gnus-article-highlight-signature)
9314               (gnus-article-emphasize)
9315               (gnus-article-delete-invisible-text)))
9316           (let ((ps-left-header
9317                  (list
9318                   (concat "("
9319                           (gnus-summary-print-truncate-and-quote
9320                            (mail-header-subject gnus-current-headers)
9321                            66) ")")
9322                   (concat "("
9323                           (gnus-summary-print-truncate-and-quote
9324                            (mail-header-from gnus-current-headers)
9325                            45) ")")))
9326                 (ps-right-header
9327                  (list
9328                   "/pagenumberstring load"
9329                   (concat "("
9330                           (mail-header-date gnus-current-headers) ")"))))
9331             (gnus-run-hooks 'gnus-ps-print-hook)
9332             (save-excursion
9333               (if ps-print-color-p
9334                   (ps-spool-buffer-with-faces)
9335                 (ps-spool-buffer)))))
9336       (kill-buffer buffer))))
9337
9338 (defun gnus-summary-show-complete-article ()
9339   "Show a complete version of the current article.
9340 This is only useful if you're looking at a partial version of the
9341 article currently."
9342   (interactive)
9343   (let ((gnus-keep-backlog nil)
9344         (gnus-use-cache nil)
9345         (gnus-agent nil)
9346         (variable (intern
9347                    (format "%s-fetch-partial-articles"
9348                            (car (gnus-find-method-for-group
9349                                  gnus-newsgroup-name)))
9350                    obarray))
9351         old-val)
9352     (unwind-protect
9353         (progn
9354           (setq old-val (symbol-value variable))
9355           (set variable nil)
9356           (gnus-flush-original-article-buffer)
9357           (gnus-summary-show-article))
9358       (set variable old-val))))
9359
9360 (defun gnus-summary-show-article (&optional arg)
9361   "Force redisplaying of the current article.
9362 If ARG (the prefix) is a number, show the article with the charset
9363 defined in `gnus-summary-show-article-charset-alist', or the charset
9364 input.
9365 If ARG (the prefix) is non-nil and not a number, show the raw article
9366 without any article massaging functions being run.  Normally, the key
9367 strokes are `C-u g'."
9368   (interactive "P")
9369   (cond
9370    ((numberp arg)
9371     (gnus-summary-show-article t)
9372     (let ((gnus-newsgroup-charset
9373            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9374                (mm-read-coding-system
9375                 "View as charset: " ;; actually it is coding system.
9376                 (with-current-buffer gnus-article-buffer
9377                   (mm-detect-coding-region (point) (point-max))))))
9378           (gnus-newsgroup-ignored-charsets 'gnus-all))
9379       (gnus-summary-select-article nil 'force)
9380       (let ((deps gnus-newsgroup-dependencies)
9381             head header lines)
9382         (with-current-buffer gnus-original-article-buffer
9383           (save-restriction
9384             (message-narrow-to-head)
9385             (setq head (buffer-string))
9386             (goto-char (point-min))
9387             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9388               (goto-char (point-max))
9389               (widen)
9390               (setq lines (1- (count-lines (point) (point-max))))))
9391           (with-temp-buffer
9392             (insert (format "211 %d Article retrieved.\n"
9393                             (cdr gnus-article-current)))
9394             (insert head)
9395             (if lines (insert (format "Lines: %d\n" lines)))
9396             (insert ".\n")
9397             (let ((nntp-server-buffer (current-buffer)))
9398               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9399         (gnus-data-set-header
9400          (gnus-data-find (cdr gnus-article-current))
9401          header)
9402         (gnus-summary-update-article-line
9403          (cdr gnus-article-current) header)
9404         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9405           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9406    ((not arg)
9407     ;; Select the article the normal way.
9408     (gnus-summary-select-article nil 'force))
9409    (t
9410     ;; We have to require this here to make sure that the following
9411     ;; dynamic binding isn't shadowed by autoloading.
9412     (require 'gnus-async)
9413     (require 'gnus-art)
9414     ;; Bind the article treatment functions to nil.
9415     (let ((gnus-have-all-headers t)
9416           gnus-article-prepare-hook
9417           gnus-article-decode-hook
9418           gnus-display-mime-function
9419           gnus-break-pages)
9420       ;; Destroy any MIME parts.
9421       (when (gnus-buffer-live-p gnus-article-buffer)
9422         (with-current-buffer gnus-article-buffer
9423           (mm-destroy-parts gnus-article-mime-handles)
9424           ;; Set it to nil for safety reason.
9425           (setq gnus-article-mime-handle-alist nil)
9426           (setq gnus-article-mime-handles nil)))
9427       (gnus-summary-select-article nil 'force))))
9428   (gnus-summary-goto-subject gnus-current-article)
9429   (gnus-summary-position-point))
9430
9431 (defun gnus-summary-show-raw-article ()
9432   "Show the raw article without any article massaging functions being run."
9433   (interactive)
9434   (gnus-summary-show-article t))
9435
9436 (defun gnus-summary-verbose-headers (&optional arg)
9437   "Toggle permanent full header display.
9438 If ARG is a positive number, turn header display on.
9439 If ARG is a negative number, turn header display off."
9440   (interactive "P")
9441   (setq gnus-show-all-headers
9442         (cond ((or (not (numberp arg))
9443                    (zerop arg))
9444                (not gnus-show-all-headers))
9445               ((natnump arg)
9446                t)))
9447   (gnus-summary-show-article))
9448
9449 (defun gnus-summary-toggle-header (&optional arg)
9450   "Show the headers if they are hidden, or hide them if they are shown.
9451 If ARG is a positive number, show the entire header.
9452 If ARG is a negative number, hide the unwanted header lines."
9453   (interactive "P")
9454   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9455                      (get-buffer-window gnus-article-buffer t))))
9456     (with-current-buffer gnus-article-buffer
9457       (widen)
9458       (article-narrow-to-head)
9459       (let* ((inhibit-read-only t)
9460              (inhibit-point-motion-hooks t)
9461              (hidden (if (numberp arg)
9462                          (>= arg 0)
9463                        (or (not (looking-at "[^ \t\n]+:"))
9464                            (gnus-article-hidden-text-p 'headers))))
9465              s e)
9466         (delete-region (point-min) (point-max))
9467         (with-current-buffer gnus-original-article-buffer
9468           (goto-char (setq s (point-min)))
9469           (setq e (if (search-forward "\n\n" nil t)
9470                       (1- (point))
9471                     (point-max))))
9472         (insert-buffer-substring gnus-original-article-buffer s e)
9473         (run-hooks 'gnus-article-decode-hook)
9474         (if hidden
9475             (let ((gnus-treat-hide-headers nil)
9476                   (gnus-treat-hide-boring-headers nil))
9477               (gnus-delete-wash-type 'headers)
9478               (gnus-treat-article 'head))
9479           (gnus-treat-article 'head))
9480         (widen)
9481         (if window
9482             (set-window-start window (goto-char (point-min))))
9483         (if gnus-break-pages
9484             (gnus-narrow-to-page)
9485           (when (gnus-visual-p 'page-marker)
9486             (let ((inhibit-read-only t))
9487               (gnus-remove-text-with-property 'gnus-prev)
9488               (gnus-remove-text-with-property 'gnus-next))))
9489         (gnus-set-mode-line 'article)))))
9490
9491 (defun gnus-summary-show-all-headers ()
9492   "Make all header lines visible."
9493   (interactive)
9494   (gnus-summary-toggle-header 1))
9495
9496 (defun gnus-summary-caesar-message (&optional arg)
9497   "Caesar rotate the current article by 13.
9498 With a non-numerical prefix, also rotate headers.  A numerical
9499 prefix specifies how many places to rotate each letter forward."
9500   (interactive "P")
9501   (gnus-summary-select-article)
9502   (let ((mail-header-separator ""))
9503     (gnus-eval-in-buffer-window gnus-article-buffer
9504       (save-restriction
9505         (widen)
9506         (let ((start (window-start))
9507               (inhibit-read-only t))
9508           (if (equal arg '(4))
9509               (message-caesar-buffer-body nil t)
9510             (message-caesar-buffer-body arg))
9511           (set-window-start (get-buffer-window (current-buffer)) start)))))
9512   ;; Create buttons and stuff...
9513   (gnus-treat-article nil))
9514
9515 (declare-function idna-to-unicode "ext:idna" (str))
9516
9517 (defun gnus-summary-idna-message (&optional arg)
9518   "Decode IDNA encoded domain names in the current articles.
9519 IDNA encoded domain names looks like `xn--bar'.  If a string
9520 remain unencoded after running this function, it is likely an
9521 invalid IDNA string (`xn--bar' is invalid).
9522
9523 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9524 installed for this command to work."
9525   (interactive "P")
9526   (if (not (and (condition-case nil (require 'idna)
9527                   (file-error))
9528                 (mm-coding-system-p 'utf-8)
9529                 (executable-find (symbol-value 'idna-program))))
9530       (gnus-message
9531        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9532     (gnus-summary-select-article)
9533     (let ((mail-header-separator ""))
9534       (gnus-eval-in-buffer-window gnus-article-buffer
9535         (save-restriction
9536           (widen)
9537           (let ((start (window-start))
9538                 buffer-read-only)
9539             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9540               (replace-match (idna-to-unicode (match-string 1))))
9541             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9542
9543 (defun gnus-summary-morse-message (&optional arg)
9544   "Morse decode the current article."
9545   (interactive "P")
9546   (gnus-summary-select-article)
9547   (let ((mail-header-separator ""))
9548     (gnus-eval-in-buffer-window gnus-article-buffer
9549       (save-excursion
9550         (save-restriction
9551           (widen)
9552           (let ((pos (window-start))
9553                 (inhibit-read-only t))
9554             (goto-char (point-min))
9555             (when (message-goto-body)
9556               (gnus-narrow-to-body))
9557             (goto-char (point-min))
9558             (while (search-forward "·" (point-max) t)
9559               (replace-match "."))
9560             (unmorse-region (point-min) (point-max))
9561             (widen)
9562             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9563
9564 (defun gnus-summary-stop-page-breaking ()
9565   "Stop page breaking in the current article."
9566   (interactive)
9567   (gnus-summary-select-article)
9568   (gnus-eval-in-buffer-window gnus-article-buffer
9569     (widen)
9570     (when (gnus-visual-p 'page-marker)
9571       (let ((inhibit-read-only t))
9572         (gnus-remove-text-with-property 'gnus-prev)
9573         (gnus-remove-text-with-property 'gnus-next))
9574       (setq gnus-page-broken nil))))
9575
9576 (defun gnus-summary-move-article (&optional n to-newsgroup
9577                                             select-method action)
9578   "Move the current article to a different newsgroup.
9579 If N is a positive number, move the N next articles.
9580 If N is a negative number, move the N previous articles.
9581 If N is nil and any articles have been marked with the process mark,
9582 move those articles instead.
9583 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9584 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9585 re-spool using this method.
9586
9587 When called interactively with TO-NEWSGROUP being nil, the value of
9588 the variable `gnus-move-split-methods' is used for finding a default
9589 for the target newsgroup.
9590
9591 For this function to work, both the current newsgroup and the
9592 newsgroup that you want to move to have to support the `request-move'
9593 and `request-accept' functions.
9594
9595 ACTION can be either `move' (the default), `crosspost' or `copy'."
9596   (interactive "P")
9597   (unless action
9598     (setq action 'move))
9599   ;; Check whether the source group supports the required functions.
9600   (cond ((and (eq action 'move)
9601               (not (gnus-check-backend-function
9602                     'request-move-article gnus-newsgroup-name)))
9603          (error "The current group does not support article moving"))
9604         ((and (eq action 'crosspost)
9605               (not (gnus-check-backend-function
9606                     'request-replace-article gnus-newsgroup-name)))
9607          (error "The current group does not support article editing")))
9608   (let ((articles (gnus-summary-work-articles n))
9609         (prefix (if (gnus-check-backend-function
9610                      'request-move-article gnus-newsgroup-name)
9611                     (funcall gnus-move-group-prefix-function
9612                              gnus-newsgroup-name)
9613                   ""))
9614         (names '((move "Move" "Moving")
9615                  (copy "Copy" "Copying")
9616                  (crosspost "Crosspost" "Crossposting")))
9617         (copy-buf (save-excursion
9618                     (nnheader-set-temp-buffer " *copy article*")))
9619         art-group to-method new-xref article to-groups
9620         articles-to-update-marks encoded)
9621     (unless (assq action names)
9622       (error "Unknown action %s" action))
9623     ;; Read the newsgroup name.
9624     (when (and (not to-newsgroup)
9625                (not select-method))
9626       (if (and gnus-move-split-methods
9627                (not
9628                 (and (memq gnus-current-article articles)
9629                      (gnus-buffer-live-p gnus-original-article-buffer))))
9630           ;; When `gnus-move-split-methods' is non-nil, we have to
9631           ;; select an article to give `gnus-read-move-group-name' an
9632           ;; opportunity to suggest an appropriate default.  However,
9633           ;; we needn't render or mark the article.
9634           (let ((gnus-display-mime-function nil)
9635                 (gnus-article-prepare-hook nil)
9636                 (gnus-mark-article-hook nil))
9637             (gnus-summary-select-article nil nil nil (car articles))))
9638       (setq to-newsgroup (gnus-read-move-group-name
9639                           (cadr (assq action names))
9640                           (symbol-value
9641                            (intern (format "gnus-current-%s-group" action)))
9642                           articles prefix)
9643             encoded to-newsgroup
9644             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9645       (set (intern (format "gnus-current-%s-group" action))
9646            (mm-decode-coding-string
9647             to-newsgroup
9648             (gnus-group-name-charset to-method to-newsgroup))))
9649     (unless to-method
9650       (setq to-method (or select-method
9651                           (gnus-server-to-method
9652                            (gnus-group-method to-newsgroup)))))
9653     (setq to-newsgroup
9654           (or encoded
9655               (and to-newsgroup
9656                    (mm-encode-coding-string
9657                     to-newsgroup
9658                     (gnus-group-name-charset to-method to-newsgroup)))))
9659     ;; Check the method we are to move this article to...
9660     (unless (gnus-check-backend-function
9661              'request-accept-article (car to-method))
9662       (error "%s does not support article copying" (car to-method)))
9663     (unless (gnus-check-server to-method)
9664       (error "Can't open server %s" (car to-method)))
9665     (gnus-message 6 "%s to %s: %s..."
9666                   (caddr (assq action names))
9667                   (or (car select-method)
9668                       (gnus-group-decoded-name to-newsgroup))
9669                   articles)
9670     (while articles
9671       (setq article (pop articles))
9672       (setq
9673        art-group
9674        (cond
9675         ;; Move the article.
9676         ((eq action 'move)
9677          ;; Remove this article from future suppression.
9678          (gnus-dup-unsuppress-article article)
9679          (let* ((from-method (gnus-find-method-for-group
9680                               gnus-newsgroup-name))
9681                 (to-method (or select-method
9682                                (gnus-find-method-for-group to-newsgroup)))
9683                 (move-is-internal (gnus-server-equal from-method to-method)))
9684            (gnus-request-move-article
9685             article                     ; Article to move
9686             gnus-newsgroup-name         ; From newsgroup
9687             (nth 1 (gnus-find-method-for-group
9688                     gnus-newsgroup-name)) ; Server
9689             (list 'gnus-request-accept-article
9690                   to-newsgroup (list 'quote select-method)
9691                   (not articles) t)     ; Accept form
9692             (not articles)              ; Only save nov last time
9693             (and move-is-internal
9694                  to-newsgroup           ; Not respooling
9695                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9696         ;; Copy the article.
9697         ((eq action 'copy)
9698          (with-current-buffer copy-buf
9699            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9700              (save-restriction
9701                (nnheader-narrow-to-headers)
9702                (dolist (hdr gnus-copy-article-ignored-headers)
9703                  (message-remove-header hdr t)))
9704              (gnus-request-accept-article
9705               to-newsgroup select-method (not articles) t))))
9706         ;; Crosspost the article.
9707         ((eq action 'crosspost)
9708          (let ((xref (message-tokenize-header
9709                       (mail-header-xref (gnus-summary-article-header article))
9710                       " ")))
9711            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9712                                   ":" (number-to-string article)))
9713            (unless xref
9714              (setq xref (list (system-name))))
9715            (setq new-xref
9716                  (concat
9717                   (mapconcat 'identity
9718                              (delete "Xref:" (delete new-xref xref))
9719                              " ")
9720                   " " new-xref))
9721            (with-current-buffer copy-buf
9722              ;; First put the article in the destination group.
9723              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9724              (when (consp (setq art-group
9725                                 (gnus-request-accept-article
9726                                  to-newsgroup select-method (not articles) t)))
9727                (setq new-xref (concat new-xref " " (car art-group)
9728                                       ":"
9729                                       (number-to-string (cdr art-group))))
9730                ;; Now we have the new Xrefs header, so we insert
9731                ;; it and replace the new article.
9732                (nnheader-replace-header "Xref" new-xref)
9733                (gnus-request-replace-article
9734                 (cdr art-group) to-newsgroup (current-buffer) t)
9735                art-group))))))
9736       (cond
9737        ((not art-group)
9738         (gnus-message 1 "Couldn't %s article %s: %s"
9739                       (cadr (assq action names)) article
9740                       (nnheader-get-report (car to-method))))
9741        ((eq art-group 'junk)
9742         (when (eq action 'move)
9743           (gnus-summary-mark-article article gnus-canceled-mark)
9744           (gnus-message 4 "Deleted article %s" article)
9745           ;; run the delete hook
9746           (run-hook-with-args 'gnus-summary-article-delete-hook
9747                               action
9748                               (gnus-data-header
9749                                (assoc article (gnus-data-list nil)))
9750                               gnus-newsgroup-name nil
9751                               select-method)))
9752        (t
9753         (let* ((pto-group (gnus-group-prefixed-name
9754                            (car art-group) to-method))
9755                (info (gnus-get-info pto-group))
9756                (to-group (gnus-info-group info))
9757                to-marks)
9758           ;; Update the group that has been moved to.
9759           (when (and info
9760                      (memq action '(move copy)))
9761             (unless (member to-group to-groups)
9762               (push to-group to-groups))
9763
9764             (unless (memq article gnus-newsgroup-unreads)
9765               (push 'read to-marks)
9766               (gnus-info-set-read
9767                info (gnus-add-to-range (gnus-info-read info)
9768                                        (list (cdr art-group)))))
9769
9770             ;; See whether the article is to be put in the cache.
9771             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9772                    (marks (if expirable
9773                               gnus-article-mark-lists
9774                             (delete '(expirable . expire)
9775                                     (copy-sequence gnus-article-mark-lists))))
9776                    (to-article (cdr art-group)))
9777
9778               ;; Enter the article into the cache in the new group,
9779               ;; if that is required.
9780               (when gnus-use-cache
9781                 (gnus-cache-possibly-enter-article
9782                  to-group to-article
9783                  (memq article gnus-newsgroup-marked)
9784                  (memq article gnus-newsgroup-dormant)
9785                  (memq article gnus-newsgroup-unreads)))
9786
9787               (when gnus-preserve-marks
9788                 ;; Copy any marks over to the new group.
9789                 (when (and (equal to-group gnus-newsgroup-name)
9790                            (not (memq article gnus-newsgroup-unreads)))
9791                   ;; Mark this article as read in this group.
9792                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9793                   ;; Increase the active status of this group.
9794                   (setcdr (gnus-active to-group) to-article)
9795                   (setcdr gnus-newsgroup-active to-article))
9796
9797                 (while marks
9798                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9799                     (when (memq article (symbol-value
9800                                          (intern (format "gnus-newsgroup-%s"
9801                                                          (caar marks)))))
9802                       (push (cdar marks) to-marks)
9803                       ;; If the other group is the same as this group,
9804                       ;; then we have to add the mark to the list.
9805                       (when (equal to-group gnus-newsgroup-name)
9806                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9807                              (cons to-article
9808                                    (symbol-value
9809                                     (intern (format "gnus-newsgroup-%s"
9810                                                     (caar marks)))))))
9811                       ;; Copy the marks to other group.
9812                       (gnus-add-marked-articles
9813                        to-group (cdar marks) (list to-article) info)))
9814                   (setq marks (cdr marks)))
9815
9816                 (when (and expirable
9817                            gnus-mark-copied-or-moved-articles-as-expirable
9818                            (not (memq 'expire to-marks)))
9819                   ;; Mark this article as expirable.
9820                   (push 'expire to-marks)
9821                   (when (equal to-group gnus-newsgroup-name)
9822                     (push to-article gnus-newsgroup-expirable))
9823                   ;; Copy the expirable mark to other group.
9824                   (gnus-add-marked-articles
9825                    to-group 'expire (list to-article) info))
9826
9827                 (when to-marks
9828                   (gnus-request-set-mark
9829                    to-group (list (list (list to-article) 'add to-marks)))))
9830
9831               (gnus-dribble-enter
9832                (concat "(gnus-group-set-info '"
9833                        (gnus-prin1-to-string (gnus-get-info to-group))
9834                        ")"))))
9835
9836           ;; Update the Xref header in this article to point to
9837           ;; the new crossposted article we have just created.
9838           (when (eq action 'crosspost)
9839             (with-current-buffer copy-buf
9840               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9841               (nnheader-replace-header "Xref" new-xref)
9842               (gnus-request-replace-article
9843                article gnus-newsgroup-name (current-buffer) t)))
9844
9845           ;; run the move/copy/crosspost/respool hook
9846           (run-hook-with-args 'gnus-summary-article-move-hook
9847                               action
9848                               (gnus-data-header
9849                                (assoc article (gnus-data-list nil)))
9850                               gnus-newsgroup-name
9851                               to-newsgroup
9852                               select-method))
9853
9854         ;;;!!!Why is this necessary?
9855         (set-buffer gnus-summary-buffer)
9856
9857         (when (eq action 'move)
9858           (save-excursion
9859             (gnus-summary-goto-subject article)
9860             (gnus-summary-mark-article article gnus-canceled-mark)))))
9861       (push article articles-to-update-marks))
9862
9863     (save-excursion
9864       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9865     ;; Re-activate all groups that have been moved to.
9866     (with-current-buffer gnus-group-buffer
9867       (let ((gnus-group-marked to-groups))
9868         (gnus-group-get-new-news-this-group nil t)))
9869
9870     (gnus-kill-buffer copy-buf)
9871     (gnus-summary-position-point)
9872     (gnus-set-mode-line 'summary)))
9873
9874 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9875   "Copy the current article to some other group.
9876 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9877 When called interactively, if TO-NEWSGROUP is nil, use the value of
9878 the variable `gnus-move-split-methods' for finding a default target
9879 newsgroup.
9880 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9881 re-spool using this method."
9882   (interactive "P")
9883   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9884
9885 (defun gnus-summary-crosspost-article (&optional n)
9886   "Crosspost the current article to some other group."
9887   (interactive "P")
9888   (gnus-summary-move-article n nil nil 'crosspost))
9889
9890 (defcustom gnus-summary-respool-default-method nil
9891   "Default method type for respooling an article.
9892 If nil, use to the current newsgroup method."
9893   :type 'symbol
9894   :group 'gnus-summary-mail)
9895
9896 (defun gnus-summary-respool-article (&optional n method)
9897   "Respool the current article.
9898 The article will be squeezed through the mail spooling process again,
9899 which means that it will be put in some mail newsgroup or other
9900 depending on `nnmail-split-methods'.
9901 If N is a positive number, respool the N next articles.
9902 If N is a negative number, respool the N previous articles.
9903 If N is nil and any articles have been marked with the process mark,
9904 respool those articles instead.
9905
9906 Respooling can be done both from mail groups and \"real\" newsgroups.
9907 In the former case, the articles in question will be moved from the
9908 current group into whatever groups they are destined to.  In the
9909 latter case, they will be copied into the relevant groups."
9910   (interactive
9911    (list current-prefix-arg
9912          (let* ((methods (gnus-methods-using 'respool))
9913                 (methname
9914                  (symbol-name (or gnus-summary-respool-default-method
9915                                   (car (gnus-find-method-for-group
9916                                         gnus-newsgroup-name)))))
9917                 (method
9918                  (gnus-completing-read
9919                   "Backend to use when respooling"
9920                   methods t nil 'gnus-mail-method-history methname))
9921                 ms)
9922            (cond
9923             ((zerop (length (setq ms (gnus-servers-using-backend
9924                                       (intern method)))))
9925              (list (intern method) ""))
9926             ((= 1 (length ms))
9927              (car ms))
9928             (t
9929              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9930                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
9931                            ms-alist))))))))
9932   (unless method
9933     (error "No method given for respooling"))
9934   (if (assoc (symbol-name
9935               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9936              (gnus-methods-using 'respool))
9937       (gnus-summary-move-article n nil method)
9938     (gnus-summary-copy-article n nil method)))
9939
9940 (defun gnus-summary-import-article (file &optional edit)
9941   "Import an arbitrary file into a mail newsgroup."
9942   (interactive "fImport file: \nP")
9943   (let ((group gnus-newsgroup-name)
9944         (now (current-time))
9945         atts lines group-art)
9946     (unless (gnus-check-backend-function 'request-accept-article group)
9947       (error "%s does not support article importing" group))
9948     (or (file-readable-p file)
9949         (not (file-regular-p file))
9950         (error "Can't read %s" file))
9951     (with-current-buffer (gnus-get-buffer-create " *import file*")
9952       (erase-buffer)
9953       (nnheader-insert-file-contents file)
9954       (goto-char (point-min))
9955       (if (nnheader-article-p)
9956           (save-restriction
9957             (goto-char (point-min))
9958             (search-forward "\n\n" nil t)
9959             (narrow-to-region (point-min) (1- (point)))
9960             (goto-char (point-min))
9961             (unless (re-search-forward "^date:" nil t)
9962               (goto-char (point-max))
9963               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9964        ;; This doesn't look like an article, so we fudge some headers.
9965         (setq atts (file-attributes file)
9966               lines (count-lines (point-min) (point-max)))
9967         (insert "From: " (read-string "From: ") "\n"
9968                 "Subject: " (read-string "Subject: ") "\n"
9969                 "Date: " (message-make-date (nth 5 atts)) "\n"
9970                 "Message-ID: " (message-make-message-id) "\n"
9971                 "Lines: " (int-to-string lines) "\n"
9972                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9973       (setq group-art (gnus-request-accept-article group nil t))
9974       (kill-buffer (current-buffer)))
9975     (setq gnus-newsgroup-active (gnus-activate-group group))
9976     (forward-line 1)
9977     (gnus-summary-goto-article (cdr group-art) nil t)
9978     (when edit
9979       (gnus-summary-edit-article))))
9980
9981 (defun gnus-summary-create-article ()
9982   "Create an article in a mail newsgroup."
9983   (interactive)
9984   (let ((group gnus-newsgroup-name)
9985         (now (current-time))
9986         group-art)
9987     (unless (gnus-check-backend-function 'request-accept-article group)
9988       (error "%s does not support article importing" group))
9989     (with-current-buffer (gnus-get-buffer-create " *import file*")
9990       (erase-buffer)
9991       (goto-char (point-min))
9992       ;; This doesn't look like an article, so we fudge some headers.
9993       (insert "From: " (read-string "From: ") "\n"
9994               "Subject: " (read-string "Subject: ") "\n"
9995               "Date: " (message-make-date now) "\n"
9996               "Message-ID: " (message-make-message-id) "\n")
9997       (setq group-art (gnus-request-accept-article group nil t))
9998       (kill-buffer (current-buffer)))
9999     (setq gnus-newsgroup-active (gnus-activate-group group))
10000     (forward-line 1)
10001     (gnus-summary-goto-article (cdr group-art) nil t)
10002     (gnus-summary-edit-article)))
10003
10004 (defun gnus-summary-article-posted-p ()
10005   "Say whether the current (mail) article is available from news as well.
10006 This will be the case if the article has both been mailed and posted."
10007   (interactive)
10008   (let ((id (mail-header-references (gnus-summary-article-header)))
10009         (gnus-override-method (car (gnus-refer-article-methods))))
10010     (if (gnus-request-head id "")
10011         (gnus-message 2 "The current message was found on %s"
10012                       gnus-override-method)
10013       (gnus-message 2 "The current message couldn't be found on %s"
10014                     gnus-override-method)
10015       nil)))
10016
10017 (defun gnus-summary-expire-articles (&optional now)
10018   "Expire all articles that are marked as expirable in the current group."
10019   (interactive)
10020   (when (and (not gnus-group-is-exiting-without-update-p)
10021              (gnus-check-backend-function
10022               'request-expire-articles gnus-newsgroup-name))
10023     ;; This backend supports expiry.
10024     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10025            (expirable (if total
10026                           (progn
10027                             ;; We need to update the info for
10028                             ;; this group for `gnus-list-of-read-articles'
10029                             ;; to give us the right answer.
10030                             (gnus-run-hooks 'gnus-exit-group-hook)
10031                             (gnus-summary-update-info)
10032                             (gnus-list-of-read-articles gnus-newsgroup-name))
10033                         (setq gnus-newsgroup-expirable
10034                               (sort gnus-newsgroup-expirable '<))))
10035            (expiry-wait (if now 'immediate
10036                           (gnus-group-find-parameter
10037                            gnus-newsgroup-name 'expiry-wait)))
10038            (nnmail-expiry-target
10039             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10040                 nnmail-expiry-target))
10041            es)
10042       (when expirable
10043         ;; There are expirable articles in this group, so we run them
10044         ;; through the expiry process.
10045         (gnus-message 6 "Expiring articles...")
10046         (unless (gnus-check-group gnus-newsgroup-name)
10047           (error "Can't open server for %s" gnus-newsgroup-name))
10048         ;; The list of articles that weren't expired is returned.
10049         (save-excursion
10050           (if expiry-wait
10051               (let ((nnmail-expiry-wait-function nil)
10052                     (nnmail-expiry-wait expiry-wait))
10053                 (setq es (gnus-request-expire-articles
10054                           expirable gnus-newsgroup-name)))
10055             (setq es (gnus-request-expire-articles
10056                       expirable gnus-newsgroup-name)))
10057           (unless total
10058             (setq gnus-newsgroup-expirable es))
10059           ;; We go through the old list of expirable, and mark all
10060           ;; really expired articles as nonexistent.
10061           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10062             (let ((gnus-use-cache nil))
10063               (dolist (article expirable)
10064                 (when (and (not (memq article es))
10065                            (gnus-data-find article))
10066                   (gnus-summary-mark-article article gnus-canceled-mark)
10067                   (run-hook-with-args 'gnus-summary-article-expire-hook
10068                                       'delete
10069                                       (gnus-data-header
10070                                        (assoc article (gnus-data-list nil)))
10071                                       gnus-newsgroup-name
10072                                       nil
10073                                       nil))))))
10074         (gnus-message 6 "Expiring articles...done")))))
10075
10076 (defun gnus-summary-expire-articles-now ()
10077   "Expunge all expirable articles in the current group.
10078 This means that *all* articles that are marked as expirable will be
10079 deleted forever, right now."
10080   (interactive)
10081   (or gnus-expert-user
10082       (gnus-yes-or-no-p
10083        "Are you really, really sure you want to delete all expirable messages? ")
10084       (error "Phew!"))
10085   (gnus-summary-expire-articles t))
10086
10087 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10088 (defun gnus-summary-delete-article (&optional n)
10089   "Delete the N next (mail) articles.
10090 This command actually deletes articles.  This is not a marking
10091 command.  The article will disappear forever from your life, never to
10092 return.
10093
10094 If N is negative, delete backwards.
10095 If N is nil and articles have been marked with the process mark,
10096 delete these instead.
10097
10098 If `gnus-novice-user' is non-nil you will be asked for
10099 confirmation before the articles are deleted."
10100   (interactive "P")
10101   (unless (gnus-check-backend-function 'request-expire-articles
10102                                        gnus-newsgroup-name)
10103     (error "The current newsgroup does not support article deletion"))
10104   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10105     (error "Couldn't open server"))
10106   ;; Compute the list of articles to delete.
10107   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10108         (nnmail-expiry-target 'delete)
10109         not-deleted)
10110     (if (and gnus-novice-user
10111              (not (gnus-yes-or-no-p
10112                    (format "Do you really want to delete %s forever? "
10113                            (if (> (length articles) 1)
10114                                (format "these %s articles" (length articles))
10115                              "this article")))))
10116         ()
10117       ;; Delete the articles.
10118       (setq not-deleted (gnus-request-expire-articles
10119                          articles gnus-newsgroup-name 'force))
10120       (save-excursion
10121         (while articles
10122           (gnus-summary-remove-process-mark (car articles))
10123           ;; The backend might not have been able to delete the article
10124           ;; after all.
10125           (unless (memq (car articles) not-deleted)
10126             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10127           (let* ((article (car articles))
10128                  (ghead  (gnus-data-header
10129                           (assoc article (gnus-data-list nil)))))
10130             (run-hook-with-args 'gnus-summary-article-delete-hook
10131                                 'delete ghead gnus-newsgroup-name nil
10132                                 nil))
10133           (setq articles (cdr articles))))
10134       (when not-deleted
10135         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10136     (gnus-summary-position-point)
10137     (gnus-set-mode-line 'summary)
10138     not-deleted))
10139
10140 (defun gnus-summary-edit-article (&optional arg)
10141   "Edit the current article.
10142 This will have permanent effect only in mail groups.
10143 If ARG is nil, edit the decoded articles.
10144 If ARG is 1, edit the raw articles.
10145 If ARG is 2, edit the raw articles even in read-only groups.
10146 If ARG is 3, edit the articles with the current handles.
10147 Otherwise, allow editing of articles even in read-only
10148 groups."
10149   (interactive "P")
10150   (let (force raw current-handles)
10151     (cond
10152      ((null arg))
10153      ((eq arg 1)
10154       (setq raw t))
10155      ((eq arg 2)
10156       (setq raw t
10157             force t))
10158      ((eq arg 3)
10159       (setq current-handles
10160             (and (gnus-buffer-live-p gnus-article-buffer)
10161                  (with-current-buffer gnus-article-buffer
10162                    (prog1
10163                        gnus-article-mime-handles
10164                      (setq gnus-article-mime-handles nil))))))
10165      (t
10166       (setq force t)))
10167     (when (and raw (not force)
10168                (member gnus-newsgroup-name '("nndraft:delayed"
10169                                              "nndraft:drafts"
10170                                              "nndraft:queue")))
10171       (error "Can't edit the raw article in group %s"
10172              gnus-newsgroup-name))
10173     (with-current-buffer gnus-summary-buffer
10174       (let ((mail-parse-charset gnus-newsgroup-charset)
10175             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10176         (gnus-set-global-variables)
10177         (when (and (not force)
10178                    (gnus-group-read-only-p))
10179           (error "The current newsgroup does not support article editing"))
10180         (gnus-summary-show-article t)
10181         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10182           (with-current-buffer gnus-article-buffer
10183             (mm-enable-multibyte)))
10184         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10185             (setq raw t))
10186         (gnus-article-edit-article
10187          (if raw 'ignore
10188            `(lambda ()
10189               (let ((mbl mml-buffer-list))
10190                 (setq mml-buffer-list nil)
10191                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10192                   (mime-to-mml ,'current-handles))
10193                 (let ((mbl1 mml-buffer-list))
10194                   (setq mml-buffer-list mbl)
10195                   (set (make-local-variable 'mml-buffer-list) mbl1))
10196                 (gnus-make-local-hook 'kill-buffer-hook)
10197                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10198          `(lambda (no-highlight)
10199             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10200                   (message-options message-options)
10201                   (message-options-set-recipient)
10202                   (mail-parse-ignored-charsets
10203                    ',gnus-newsgroup-ignored-charsets)
10204                   (rfc2047-header-encoding-alist
10205                    ',(let ((charset (gnus-group-name-charset
10206                                      (gnus-find-method-for-group
10207                                       gnus-newsgroup-name)
10208                                      gnus-newsgroup-name)))
10209                        (append (list (cons "Newsgroups" charset)
10210                                      (cons "Followup-To" charset)
10211                                      (cons "Xref" charset))
10212                                rfc2047-header-encoding-alist))))
10213               ,(if (not raw) '(progn
10214                                 (mml-to-mime)
10215                                 (mml-destroy-buffers)
10216                                 (remove-hook 'kill-buffer-hook
10217                                              'mml-destroy-buffers t)
10218                                 (kill-local-variable 'mml-buffer-list)))
10219               (gnus-summary-edit-article-done
10220                ,(or (mail-header-references gnus-current-headers) "")
10221                ,(gnus-group-read-only-p)
10222                ,gnus-summary-buffer no-highlight))))))))
10223
10224 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10225
10226 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10227                                                  no-highlight)
10228   "Make edits to the current article permanent."
10229   (interactive)
10230   (save-excursion
10231    ;; The buffer restriction contains the entire article if it exists.
10232     (when (article-goto-body)
10233       (let ((lines (count-lines (point) (point-max)))
10234             (length (- (point-max) (point)))
10235             (case-fold-search t)
10236             (body (copy-marker (point))))
10237         (goto-char (point-min))
10238         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10239           (delete-region (match-beginning 1) (match-end 1))
10240           (insert (number-to-string length)))
10241         (goto-char (point-min))
10242         (when (re-search-forward
10243                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10244           (delete-region (match-beginning 1) (match-end 1))
10245           (insert (number-to-string length)))
10246         (goto-char (point-min))
10247         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10248           (delete-region (match-beginning 1) (match-end 1))
10249           (insert (number-to-string lines))))))
10250   ;; Replace the article.
10251   (let ((buf (current-buffer)))
10252     (with-temp-buffer
10253       (insert-buffer-substring buf)
10254
10255       (if (and (not read-only)
10256                (not (gnus-request-replace-article
10257                      (cdr gnus-article-current) (car gnus-article-current)
10258                      (current-buffer) t)))
10259           (error "Couldn't replace article")
10260         ;; Update the summary buffer.
10261         (if (and references
10262                  (equal (message-tokenize-header references " ")
10263                         (message-tokenize-header
10264                          (or (message-fetch-field "references") "") " ")))
10265             ;; We only have to update this line.
10266             (save-excursion
10267               (save-restriction
10268                 (message-narrow-to-head)
10269                 (let ((head (buffer-substring-no-properties
10270                              (point-min) (point-max)))
10271                       header)
10272                   (with-temp-buffer
10273                     (insert (format "211 %d Article retrieved.\n"
10274                                     (cdr gnus-article-current)))
10275                     (insert head)
10276                     (insert ".\n")
10277                     (let ((nntp-server-buffer (current-buffer)))
10278                       (setq header (car (gnus-get-newsgroup-headers
10279                                          nil t))))
10280                     (with-current-buffer gnus-summary-buffer
10281                       (gnus-data-set-header
10282                        (gnus-data-find (cdr gnus-article-current))
10283                        header)
10284                       (gnus-summary-update-article-line
10285                        (cdr gnus-article-current) header)
10286                       (if (gnus-summary-goto-subject
10287                            (cdr gnus-article-current) nil t)
10288                           (gnus-summary-update-secondary-mark
10289                            (cdr gnus-article-current))))))))
10290           ;; Update threads.
10291           (set-buffer (or buffer gnus-summary-buffer))
10292           (gnus-summary-update-article (cdr gnus-article-current))
10293           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10294               (gnus-summary-update-secondary-mark
10295                (cdr gnus-article-current))))
10296         ;; Prettify the article buffer again.
10297         (unless no-highlight
10298           (with-current-buffer gnus-article-buffer
10299             ;;;!!! Fix this -- article should be rehighlighted.
10300             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10301             (set-buffer gnus-original-article-buffer)
10302             (gnus-request-article
10303              (cdr gnus-article-current)
10304              (car gnus-article-current) (current-buffer))))
10305         ;; Prettify the summary buffer line.
10306         (when (gnus-visual-p 'summary-highlight 'highlight)
10307           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10308
10309 (defun gnus-summary-edit-wash (key)
10310   "Perform editing command KEY in the article buffer."
10311   (interactive
10312    (list
10313     (progn
10314       (message "%s" (concat (this-command-keys) "- "))
10315       (read-char))))
10316   (message "")
10317   (gnus-summary-edit-article)
10318   (execute-kbd-macro (concat (this-command-keys) key))
10319   (gnus-article-edit-done))
10320
10321 ;;; Respooling
10322
10323 (defun gnus-summary-respool-query (&optional silent trace)
10324   "Query where the respool algorithm would put this article."
10325   (interactive)
10326   (let (gnus-mark-article-hook)
10327     (gnus-summary-select-article)
10328     (with-current-buffer gnus-original-article-buffer
10329       (let ((groups (nnmail-article-group 'identity trace)))
10330         (unless silent
10331           (if groups
10332               (message "This message would go to %s"
10333                        (mapconcat 'car groups ", "))
10334             (message "This message would go to no groups"))
10335           groups)))))
10336
10337 (defun gnus-summary-respool-trace ()
10338   "Trace where the respool algorithm would put this article.
10339 Display a buffer showing all fancy splitting patterns which matched."
10340   (interactive)
10341   (gnus-summary-respool-query nil t))
10342
10343 ;; Summary marking commands.
10344
10345 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10346   "Mark articles which has the same subject as read, and then select the next.
10347 If UNMARK is positive, remove any kind of mark.
10348 If UNMARK is negative, tick articles."
10349   (interactive "P")
10350   (when unmark
10351     (setq unmark (prefix-numeric-value unmark)))
10352   (let ((count
10353          (gnus-summary-mark-same-subject
10354           (gnus-summary-article-subject) unmark)))
10355     ;; Select next unread article.  If auto-select-same mode, should
10356     ;; select the first unread article.
10357     (gnus-summary-next-article t (and gnus-auto-select-same
10358                                       (gnus-summary-article-subject)))
10359     (gnus-message 7 "%d article%s marked as %s"
10360                   count (if (= count 1) " is" "s are")
10361                   (if unmark "unread" "read"))))
10362
10363 (defun gnus-summary-kill-same-subject (&optional unmark)
10364   "Mark articles which has the same subject as read.
10365 If UNMARK is positive, remove any kind of mark.
10366 If UNMARK is negative, tick articles."
10367   (interactive "P")
10368   (when unmark
10369     (setq unmark (prefix-numeric-value unmark)))
10370   (let ((count
10371          (gnus-summary-mark-same-subject
10372           (gnus-summary-article-subject) unmark)))
10373     ;; If marked as read, go to next unread subject.
10374     (when (null unmark)
10375       ;; Go to next unread subject.
10376       (gnus-summary-next-subject 1 t))
10377     (gnus-message 7 "%d articles are marked as %s"
10378                   count (if unmark "unread" "read"))))
10379
10380 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10381   "Mark articles with same SUBJECT as read, and return marked number.
10382 If optional argument UNMARK is positive, remove any kinds of marks.
10383 If optional argument UNMARK is negative, mark articles as unread instead."
10384   (let ((count 1))
10385     (save-excursion
10386       (cond
10387        ((null unmark)                   ; Mark as read.
10388         (while (and
10389                 (progn
10390                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10391                   (gnus-summary-show-thread) t)
10392                 (gnus-summary-find-subject subject))
10393           (setq count (1+ count))))
10394        ((> unmark 0)                    ; Tick.
10395         (while (and
10396                 (progn
10397                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10398                   (gnus-summary-show-thread) t)
10399                 (gnus-summary-find-subject subject))
10400           (setq count (1+ count))))
10401        (t                               ; Mark as unread.
10402         (while (and
10403                 (progn
10404                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10405                   (gnus-summary-show-thread) t)
10406                 (gnus-summary-find-subject subject))
10407           (setq count (1+ count)))))
10408       (gnus-set-mode-line 'summary)
10409       ;; Return the number of marked articles.
10410       count)))
10411
10412 (defun gnus-summary-mark-as-processable (n &optional unmark)
10413   "Set the process mark on the next N articles.
10414 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10415 the process mark instead.  The difference between N and the actual
10416 number of articles marked is returned."
10417   (interactive "P")
10418   (if (and (null n) (gnus-region-active-p))
10419       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10420     (setq n (prefix-numeric-value n))
10421     (let ((backward (< n 0))
10422           (n (abs n)))
10423       (while (and
10424               (> n 0)
10425               (if unmark
10426                   (gnus-summary-remove-process-mark
10427                    (gnus-summary-article-number))
10428                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10429               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10430         (setq n (1- n)))
10431       (when (/= 0 n)
10432         (gnus-message 7 "No more articles"))
10433       (gnus-summary-recenter)
10434       (gnus-summary-position-point)
10435       n)))
10436
10437 (defun gnus-summary-unmark-as-processable (n)
10438   "Remove the process mark from the next N articles.
10439 If N is negative, unmark backward instead.  The difference between N and
10440 the actual number of articles unmarked is returned."
10441   (interactive "P")
10442   (gnus-summary-mark-as-processable n t))
10443
10444 (defun gnus-summary-unmark-all-processable ()
10445   "Remove the process mark from all articles."
10446   (interactive)
10447   (save-excursion
10448     (while gnus-newsgroup-processable
10449       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10450   (gnus-summary-position-point))
10451
10452 (defun gnus-summary-add-mark (article type)
10453   "Mark ARTICLE with a mark of TYPE."
10454   (let ((vtype (car (assq type gnus-article-mark-lists)))
10455         var)
10456     (if (not vtype)
10457         (error "No such mark type: %s" type)
10458       (setq var (intern (format "gnus-newsgroup-%s" type)))
10459       (set var (cons article (symbol-value var)))
10460       (if (memq type '(processable cached replied forwarded recent saved))
10461           (gnus-summary-update-secondary-mark article)
10462         ;;; !!! This is bogus.  We should find out what primary
10463         ;;; !!! mark we want to set.
10464         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10465
10466 (defun gnus-summary-mark-as-expirable (n)
10467   "Mark N articles forward as expirable.
10468 If N is negative, mark backward instead.  The difference between N and
10469 the actual number of articles marked is returned."
10470   (interactive "p")
10471   (gnus-summary-mark-forward n gnus-expirable-mark))
10472
10473 (defun gnus-summary-mark-as-spam (n)
10474   "Mark N articles forward as spam.
10475 If N is negative, mark backward instead.  The difference between N and
10476 the actual number of articles marked is returned."
10477   (interactive "p")
10478   (gnus-summary-mark-forward n gnus-spam-mark))
10479
10480 (defun gnus-summary-mark-article-as-replied (article)
10481   "Mark ARTICLE as replied to and update the summary line.
10482 ARTICLE can also be a list of articles."
10483   (interactive (list (gnus-summary-article-number)))
10484   (let ((articles (if (listp article) article (list article))))
10485     (dolist (article articles)
10486       (unless (numberp article)
10487         (error "%s is not a number" article))
10488       (push article gnus-newsgroup-replied)
10489       (let ((inhibit-read-only t))
10490         (when (gnus-summary-goto-subject article nil t)
10491           (gnus-summary-update-secondary-mark article))))))
10492
10493 (defun gnus-summary-mark-article-as-forwarded (article)
10494   "Mark ARTICLE as forwarded and update the summary line.
10495 ARTICLE can also be a list of articles."
10496   (let ((articles (if (listp article) article (list article))))
10497     (dolist (article articles)
10498       (push article gnus-newsgroup-forwarded)
10499       (let ((inhibit-read-only t))
10500         (when (gnus-summary-goto-subject article nil t)
10501           (gnus-summary-update-secondary-mark article))))))
10502
10503 (defun gnus-summary-set-bookmark (article)
10504   "Set a bookmark in current article."
10505   (interactive (list (gnus-summary-article-number)))
10506   (when (or (not (get-buffer gnus-article-buffer))
10507             (not gnus-current-article)
10508             (not gnus-article-current)
10509             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10510     (error "No current article selected"))
10511   ;; Remove old bookmark, if one exists.
10512   (gnus-pull article gnus-newsgroup-bookmarks)
10513   ;; Set the new bookmark, which is on the form
10514   ;; (article-number . line-number-in-body).
10515   (push
10516    (cons article
10517          (with-current-buffer gnus-article-buffer
10518            (count-lines
10519             (min (point)
10520                  (save-excursion
10521                    (article-goto-body)
10522                    (point)))
10523             (point))))
10524    gnus-newsgroup-bookmarks)
10525   (gnus-message 6 "A bookmark has been added to the current article."))
10526
10527 (defun gnus-summary-remove-bookmark (article)
10528   "Remove the bookmark from the current article."
10529   (interactive (list (gnus-summary-article-number)))
10530   ;; Remove old bookmark, if one exists.
10531   (if (not (assq article gnus-newsgroup-bookmarks))
10532       (gnus-message 6 "No bookmark in current article.")
10533     (gnus-pull article gnus-newsgroup-bookmarks)
10534     (gnus-message 6 "Removed bookmark.")))
10535
10536 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10537 (defun gnus-summary-mark-as-dormant (n)
10538   "Mark N articles forward as dormant.
10539 If N is negative, mark backward instead.  The difference between N and
10540 the actual number of articles marked is returned."
10541   (interactive "p")
10542   (gnus-summary-mark-forward n gnus-dormant-mark))
10543
10544 (defun gnus-summary-set-process-mark (article)
10545   "Set the process mark on ARTICLE and update the summary line."
10546   (setq gnus-newsgroup-processable
10547         (cons article
10548               (delq article gnus-newsgroup-processable)))
10549   (when (gnus-summary-goto-subject article)
10550     (gnus-summary-show-thread)
10551     (gnus-summary-goto-subject article)
10552     (gnus-summary-update-secondary-mark article)))
10553
10554 (defun gnus-summary-remove-process-mark (&rest articles)
10555   "Remove the process mark from ARTICLES and update the summary line."
10556   (dolist (article articles)
10557     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10558     (when (gnus-summary-goto-subject article)
10559       (gnus-summary-show-thread)
10560       (gnus-summary-goto-subject article)
10561       (gnus-summary-update-secondary-mark article)))
10562   t)
10563
10564 (defun gnus-summary-set-saved-mark (article)
10565   "Set the process mark on ARTICLE and update the summary line."
10566   (push article gnus-newsgroup-saved)
10567   (when (gnus-summary-goto-subject article)
10568     (gnus-summary-update-secondary-mark article)))
10569
10570 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10571   "Mark N articles as read forwards.
10572 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10573 The difference between N and the actual number of articles marked is
10574 returned.
10575 If NO-EXPIRE, auto-expiry will be inhibited."
10576   (interactive "p")
10577   (gnus-summary-show-thread)
10578   (let ((backward (< n 0))
10579         (gnus-summary-goto-unread
10580          (and gnus-summary-goto-unread
10581               (not (eq gnus-summary-goto-unread 'never))
10582               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10583                                     gnus-ticked-mark gnus-dormant-mark)))))
10584         (n (abs n))
10585         (mark (or mark gnus-del-mark)))
10586     (while (and (> n 0)
10587                 (gnus-summary-mark-article nil mark no-expire)
10588                 (zerop (gnus-summary-next-subject
10589                         (if backward -1 1)
10590                         (and gnus-summary-goto-unread
10591                              (not (eq gnus-summary-goto-unread 'never)))
10592                         t)))
10593       (setq n (1- n)))
10594     (when (/= 0 n)
10595       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10596     (gnus-summary-recenter)
10597     (gnus-summary-position-point)
10598     (gnus-set-mode-line 'summary)
10599     n))
10600
10601 (defun gnus-summary-mark-article-as-read (mark)
10602   "Mark the current article quickly as read with MARK."
10603   (let ((article (gnus-summary-article-number)))
10604     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10605     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10606     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10607     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10608     (push (cons article mark) gnus-newsgroup-reads)
10609     ;; Possibly remove from cache, if that is used.
10610     (when gnus-use-cache
10611       (gnus-cache-enter-remove-article article))
10612     ;; Allow the backend to change the mark.
10613     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10614     ;; Check for auto-expiry.
10615     (when (and gnus-newsgroup-auto-expire
10616                (memq mark gnus-auto-expirable-marks))
10617       (setq mark gnus-expirable-mark)
10618       ;; Let the backend know about the mark change.
10619       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10620       (push article gnus-newsgroup-expirable))
10621     ;; Set the mark in the buffer.
10622     (gnus-summary-update-mark mark 'unread)
10623     t))
10624
10625 (defun gnus-summary-mark-article-as-unread (mark)
10626   "Mark the current article quickly as unread with MARK."
10627   (let* ((article (gnus-summary-article-number))
10628          (old-mark (gnus-summary-article-mark article)))
10629     ;; Allow the backend to change the mark.
10630     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10631     (if (eq mark old-mark)
10632         t
10633       (if (<= article 0)
10634           (progn
10635             (gnus-error 1 "Can't mark negative article numbers")
10636             nil)
10637         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10638         (setq gnus-newsgroup-spam-marked
10639               (delq article gnus-newsgroup-spam-marked))
10640         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10641         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10642         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10643         (cond ((= mark gnus-ticked-mark)
10644                (setq gnus-newsgroup-marked
10645                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10646                                               article)))
10647               ((= mark gnus-spam-mark)
10648                (setq gnus-newsgroup-spam-marked
10649                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10650                                               article)))
10651               ((= mark gnus-dormant-mark)
10652                (setq gnus-newsgroup-dormant
10653                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10654                                               article)))
10655               (t
10656                (setq gnus-newsgroup-unreads
10657                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10658                                               article))))
10659         (gnus-pull article gnus-newsgroup-reads)
10660
10661         ;; See whether the article is to be put in the cache.
10662         (and gnus-use-cache
10663              (vectorp (gnus-summary-article-header article))
10664              (save-excursion
10665                (gnus-cache-possibly-enter-article
10666                 gnus-newsgroup-name article
10667                 (= mark gnus-ticked-mark)
10668                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10669
10670         ;; Fix the mark.
10671         (gnus-summary-update-mark mark 'unread)
10672         t))))
10673
10674 (defun gnus-summary-mark-article (&optional article mark no-expire)
10675   "Mark ARTICLE with MARK.  MARK can be any character.
10676 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10677 `??' (dormant) and `?E' (expirable).
10678 If MARK is nil, then the default character `?r' is used.
10679 If ARTICLE is nil, then the article on the current line will be
10680 marked.
10681 If NO-EXPIRE, auto-expiry will be inhibited."
10682   ;; The mark might be a string.
10683   (when (stringp mark)
10684     (setq mark (aref mark 0)))
10685   ;; If no mark is given, then we check auto-expiring.
10686   (when (null mark)
10687     (setq mark gnus-del-mark))
10688   (when (and (not no-expire)
10689              gnus-newsgroup-auto-expire
10690              (memq mark gnus-auto-expirable-marks))
10691     (setq mark gnus-expirable-mark))
10692   (let ((article (or article (gnus-summary-article-number)))
10693         (old-mark (gnus-summary-article-mark article)))
10694     ;; Allow the backend to change the mark.
10695     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10696     (if (eq mark old-mark)
10697         t
10698       (unless article
10699         (error "No article on current line"))
10700       (if (not (if (or (= mark gnus-unread-mark)
10701                        (= mark gnus-ticked-mark)
10702                        (= mark gnus-spam-mark)
10703                        (= mark gnus-dormant-mark))
10704                    (gnus-mark-article-as-unread article mark)
10705                  (gnus-mark-article-as-read article mark)))
10706           t
10707         ;; See whether the article is to be put in the cache.
10708         (and gnus-use-cache
10709              (not (= mark gnus-canceled-mark))
10710              (vectorp (gnus-summary-article-header article))
10711              (save-excursion
10712                (gnus-cache-possibly-enter-article
10713                 gnus-newsgroup-name article
10714                 (= mark gnus-ticked-mark)
10715                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10716
10717         (when (gnus-summary-goto-subject article nil t)
10718           (let ((inhibit-read-only t))
10719             (gnus-summary-show-thread)
10720             ;; Fix the mark.
10721             (gnus-summary-update-mark mark 'unread)
10722             t))))))
10723
10724 (defun gnus-summary-update-secondary-mark (article)
10725   "Update the secondary (read, process, cache) mark."
10726   (gnus-summary-update-mark
10727    (cond ((memq article gnus-newsgroup-processable)
10728           gnus-process-mark)
10729          ((memq article gnus-newsgroup-cached)
10730           gnus-cached-mark)
10731          ((memq article gnus-newsgroup-replied)
10732           gnus-replied-mark)
10733          ((memq article gnus-newsgroup-forwarded)
10734           gnus-forwarded-mark)
10735          ((memq article gnus-newsgroup-saved)
10736           gnus-saved-mark)
10737          ((memq article gnus-newsgroup-recent)
10738           gnus-recent-mark)
10739          ((memq article gnus-newsgroup-unseen)
10740           gnus-unseen-mark)
10741          (t gnus-no-mark))
10742    'replied)
10743   (when (gnus-visual-p 'summary-highlight 'highlight)
10744     (gnus-summary-highlight-line)
10745     (gnus-run-hooks 'gnus-summary-update-hook))
10746   t)
10747
10748 (defun gnus-summary-update-download-mark (article)
10749   "Update the download mark."
10750   (gnus-summary-update-mark
10751    (cond ((memq article gnus-newsgroup-undownloaded)
10752           gnus-undownloaded-mark)
10753          (gnus-newsgroup-agentized
10754           gnus-downloaded-mark)
10755          (t
10756           gnus-no-mark))
10757    'download)
10758   (gnus-summary-update-line t)
10759   t)
10760
10761 (defun gnus-summary-update-mark (mark type)
10762   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10763         (inhibit-read-only t))
10764     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10765     (when forward
10766       (when (looking-at "\r")
10767         (incf forward))
10768       (when (<= (+ forward (point)) (point-max))
10769         ;; Go to the right position on the line.
10770         (goto-char (+ forward (point)))
10771         ;; Replace the old mark with the new mark.
10772         (let ((to-insert
10773                (mm-subst-char-in-string
10774                 (char-after) mark
10775                 (buffer-substring (point) (1+ (point))))))
10776           (delete-region (point) (1+ (point)))
10777           (insert to-insert))
10778         ;; Optionally update the marks by some user rule.
10779         (when (eq type 'unread)
10780           (gnus-data-set-mark
10781            (gnus-data-find (gnus-summary-article-number)) mark)
10782           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10783
10784 (defun gnus-mark-article-as-read (article &optional mark)
10785   "Enter ARTICLE in the pertinent lists and remove it from others."
10786   ;; Make the article expirable.
10787   (let ((mark (or mark gnus-del-mark)))
10788     (setq gnus-newsgroup-expirable
10789           (if (= mark gnus-expirable-mark)
10790               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10791             (delq article gnus-newsgroup-expirable)))
10792     ;; Remove from unread and marked lists.
10793     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10794     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10795     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10796     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10797     (push (cons article mark) gnus-newsgroup-reads)
10798     ;; Possibly remove from cache, if that is used.
10799     (when gnus-use-cache
10800       (gnus-cache-enter-remove-article article))
10801     t))
10802
10803 (defun gnus-mark-article-as-unread (article &optional mark)
10804   "Enter ARTICLE in the pertinent lists and remove it from others."
10805   (let ((mark (or mark gnus-ticked-mark)))
10806     (if (<= article 0)
10807         (progn
10808           (gnus-error 1 "Can't mark negative article numbers")
10809           nil)
10810       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10811             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10812             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10813             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10814             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10815
10816       ;; Unsuppress duplicates?
10817       (when gnus-suppress-duplicates
10818         (gnus-dup-unsuppress-article article))
10819
10820       (cond ((= mark gnus-ticked-mark)
10821              (setq gnus-newsgroup-marked
10822                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10823             ((= mark gnus-spam-mark)
10824              (setq gnus-newsgroup-spam-marked
10825                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10826                                             article)))
10827             ((= mark gnus-dormant-mark)
10828              (setq gnus-newsgroup-dormant
10829                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10830             (t
10831              (setq gnus-newsgroup-unreads
10832                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10833       (gnus-pull article gnus-newsgroup-reads)
10834       t)))
10835
10836 (defalias 'gnus-summary-mark-as-unread-forward
10837   'gnus-summary-tick-article-forward)
10838 (make-obsolete 'gnus-summary-mark-as-unread-forward
10839                'gnus-summary-tick-article-forward "Emacs 20.4")
10840 (defun gnus-summary-tick-article-forward (n)
10841   "Tick N articles forwards.
10842 If N is negative, tick backwards instead.
10843 The difference between N and the number of articles ticked is returned."
10844   (interactive "p")
10845   (gnus-summary-mark-forward n gnus-ticked-mark))
10846
10847 (defalias 'gnus-summary-mark-as-unread-backward
10848   'gnus-summary-tick-article-backward)
10849 (make-obsolete 'gnus-summary-mark-as-unread-backward
10850                'gnus-summary-tick-article-backward "Emacs 20.4")
10851 (defun gnus-summary-tick-article-backward (n)
10852   "Tick N articles backwards.
10853 The difference between N and the number of articles ticked is returned."
10854   (interactive "p")
10855   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10856
10857 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10858 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10859 (defun gnus-summary-tick-article (&optional article clear-mark)
10860   "Mark current article as unread.
10861 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10862 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10863   (interactive)
10864   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10865                                        gnus-ticked-mark)))
10866
10867 (defun gnus-summary-mark-as-read-forward (n)
10868   "Mark N articles as read forwards.
10869 If N is negative, mark backwards instead.
10870 The difference between N and the actual number of articles marked is
10871 returned."
10872   (interactive "p")
10873   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10874
10875 (defun gnus-summary-mark-as-read-backward (n)
10876   "Mark the N articles as read backwards.
10877 The difference between N and the actual number of articles marked is
10878 returned."
10879   (interactive "p")
10880   (gnus-summary-mark-forward
10881    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10882
10883 (defun gnus-summary-mark-as-read (&optional article mark)
10884   "Mark current article as read.
10885 ARTICLE specifies the article to be marked as read.
10886 MARK specifies a string to be inserted at the beginning of the line."
10887   (gnus-summary-mark-article article mark))
10888
10889 (defun gnus-summary-clear-mark-forward (n)
10890   "Clear marks from N articles forward.
10891 If N is negative, clear backward instead.
10892 The difference between N and the number of marks cleared is returned."
10893   (interactive "p")
10894   (gnus-summary-mark-forward n gnus-unread-mark))
10895
10896 (defun gnus-summary-clear-mark-backward (n)
10897   "Clear marks from N articles backward.
10898 The difference between N and the number of marks cleared is returned."
10899   (interactive "p")
10900   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10901
10902 (defun gnus-summary-mark-unread-as-read ()
10903   "Intended to be used by `gnus-mark-article-hook'."
10904   (when (memq gnus-current-article gnus-newsgroup-unreads)
10905     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10906
10907 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10908   "Intended to be used by `gnus-mark-article-hook'."
10909   (let ((mark (gnus-summary-article-mark)))
10910     (when (or (gnus-unread-mark-p mark)
10911               (gnus-read-mark-p mark))
10912       (gnus-summary-mark-article gnus-current-article
10913                                  (or new-mark gnus-read-mark)))))
10914
10915 (defun gnus-summary-mark-current-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-summary-article-number)
10921                                  (or new-mark gnus-read-mark)))))
10922
10923 (defun gnus-summary-mark-unread-as-ticked ()
10924   "Intended to be used by `gnus-mark-article-hook'."
10925   (when (memq gnus-current-article gnus-newsgroup-unreads)
10926     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10927
10928 (defun gnus-summary-mark-region-as-read (point mark all)
10929   "Mark all unread articles between point and mark as read.
10930 If given a prefix, mark all articles between point and mark as read,
10931 even ticked and dormant ones."
10932   (interactive "r\nP")
10933   (save-excursion
10934     (let (article)
10935       (goto-char point)
10936       (beginning-of-line)
10937       (while (and
10938               (< (point) mark)
10939               (progn
10940                 (when (or all
10941                           (memq (setq article (gnus-summary-article-number))
10942                                 gnus-newsgroup-unreads))
10943                   (gnus-summary-mark-article article gnus-del-mark))
10944                 t)
10945               (gnus-summary-find-next))))))
10946
10947 (defun gnus-summary-mark-below (score mark)
10948   "Mark articles with score less than SCORE with MARK."
10949   (interactive "P\ncMark: ")
10950   (setq score (if score
10951                   (prefix-numeric-value score)
10952                 (or gnus-summary-default-score 0)))
10953   (with-current-buffer gnus-summary-buffer
10954     (goto-char (point-min))
10955     (while
10956         (progn
10957           (and (< (gnus-summary-article-score) score)
10958                (gnus-summary-mark-article nil mark))
10959           (gnus-summary-find-next)))))
10960
10961 (defun gnus-summary-kill-below (&optional score)
10962   "Mark articles with score below SCORE as read."
10963   (interactive "P")
10964   (gnus-summary-mark-below score gnus-killed-mark))
10965
10966 (defun gnus-summary-clear-above (&optional score)
10967   "Clear all marks from articles with score above SCORE."
10968   (interactive "P")
10969   (gnus-summary-mark-above score gnus-unread-mark))
10970
10971 (defun gnus-summary-tick-above (&optional score)
10972   "Tick all articles with score above SCORE."
10973   (interactive "P")
10974   (gnus-summary-mark-above score gnus-ticked-mark))
10975
10976 (defun gnus-summary-mark-above (score mark)
10977   "Mark articles with score over SCORE with MARK."
10978   (interactive "P\ncMark: ")
10979   (setq score (if score
10980                   (prefix-numeric-value score)
10981                 (or gnus-summary-default-score 0)))
10982   (with-current-buffer gnus-summary-buffer
10983     (goto-char (point-min))
10984     (while (and (progn
10985                   (when (> (gnus-summary-article-score) score)
10986                     (gnus-summary-mark-article nil mark))
10987                   t)
10988                 (gnus-summary-find-next)))))
10989
10990 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10991 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10992 (defun gnus-summary-limit-include-expunged (&optional no-error)
10993   "Display all the hidden articles that were expunged for low scores."
10994   (interactive)
10995   (let ((inhibit-read-only t))
10996     (let ((scored gnus-newsgroup-scored)
10997           headers h)
10998       (while scored
10999         (unless (gnus-summary-article-header (caar scored))
11000           (and (setq h (gnus-number-to-header (caar scored)))
11001                (< (cdar scored) gnus-summary-expunge-below)
11002                (push h headers)))
11003         (setq scored (cdr scored)))
11004       (if (not headers)
11005           (when (not no-error)
11006             (error "No expunged articles hidden"))
11007         (goto-char (point-min))
11008         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11009         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11010         (dolist (x headers)
11011           (push (mail-header-number x) gnus-newsgroup-limit))
11012         (gnus-summary-prepare-unthreaded (nreverse headers))
11013         (goto-char (point-min))
11014         (gnus-summary-position-point)
11015         t))))
11016
11017 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11018   "Mark all unread articles in this newsgroup as read.
11019 If prefix argument ALL is non-nil, ticked and dormant articles will
11020 also be marked as read.
11021 If QUIETLY is non-nil, no questions will be asked.
11022
11023 If TO-HERE is non-nil, it should be a point in the buffer.  All
11024 articles before (after, if REVERSE is set) this point will be marked
11025 as read.
11026
11027 Note that this function will only catch up the unread article
11028 in the current summary buffer limitation.
11029
11030 The number of articles marked as read is returned."
11031   (interactive "P")
11032   (prog1
11033       (save-excursion
11034         (when (or quietly
11035                   (not gnus-interactive-catchup) ;Without confirmation?
11036                   gnus-expert-user
11037                   (gnus-y-or-n-p
11038                    (if all
11039                        "Mark absolutely all articles as read? "
11040                      "Mark all unread articles as read? ")))
11041           (if (and not-mark
11042                    (not gnus-newsgroup-adaptive)
11043                    (not gnus-newsgroup-auto-expire)
11044                    (not gnus-suppress-duplicates)
11045                    (or (not gnus-use-cache)
11046                        (eq gnus-use-cache 'passive)))
11047               (progn
11048                 (when all
11049                   (setq gnus-newsgroup-marked nil
11050                         gnus-newsgroup-spam-marked nil
11051                         gnus-newsgroup-dormant nil))
11052                 (setq gnus-newsgroup-unreads
11053                       (gnus-sorted-nunion
11054                        (gnus-sorted-intersection gnus-newsgroup-unreads
11055                                                  gnus-newsgroup-downloadable)
11056                        (gnus-sorted-difference gnus-newsgroup-unfetched
11057                                                gnus-newsgroup-cached))))
11058             ;; We actually mark all articles as canceled, which we
11059             ;; have to do when using auto-expiry or adaptive scoring.
11060             (gnus-summary-show-all-threads)
11061             (if (and to-here reverse)
11062                 (progn
11063                   (goto-char to-here)
11064                   (gnus-summary-mark-current-read-and-unread-as-read
11065                    gnus-catchup-mark)
11066                   (while (gnus-summary-find-next (not all))
11067                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11068               (when (gnus-summary-first-subject (not all))
11069                 (while (and
11070                         (if to-here (< (point) to-here) t)
11071                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11072                         (gnus-summary-find-next (not all))))))
11073             (gnus-set-mode-line 'summary))
11074           t))
11075     (gnus-summary-position-point)))
11076
11077 (defun gnus-summary-catchup-to-here (&optional all)
11078   "Mark all unticked articles before the current one as read.
11079 If ALL is non-nil, also mark ticked and dormant articles as read."
11080   (interactive "P")
11081   (save-excursion
11082     (gnus-save-hidden-threads
11083       (let ((beg (point)))
11084         ;; We check that there are unread articles.
11085         (when (or all (gnus-summary-find-prev))
11086           (gnus-summary-catchup all t beg)))))
11087   (gnus-summary-position-point))
11088
11089 (defun gnus-summary-catchup-from-here (&optional all)
11090   "Mark all unticked articles after (and including) the current one as read.
11091 If ALL is non-nil, also mark ticked and dormant articles as read."
11092   (interactive "P")
11093   (save-excursion
11094     (gnus-save-hidden-threads
11095       (let ((beg (point)))
11096         ;; We check that there are unread articles.
11097         (when (or all (gnus-summary-find-next))
11098           (gnus-summary-catchup all t beg nil t)))))
11099   (gnus-summary-position-point))
11100
11101 (defun gnus-summary-catchup-all (&optional quietly)
11102   "Mark all articles in this newsgroup as read.
11103 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11104 instead, which marks only unread articles as read."
11105   (interactive "P")
11106   (gnus-summary-catchup t quietly))
11107
11108 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11109   "Mark all unread articles in this group as read, then exit.
11110 If prefix argument ALL is non-nil, all articles are marked as read.
11111 If QUIETLY is non-nil, no questions will be asked."
11112   (interactive "P")
11113   (when (gnus-summary-catchup all quietly nil 'fast)
11114     ;; Select next newsgroup or exit.
11115     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11116              (eq gnus-auto-select-next 'quietly))
11117         (gnus-summary-next-group nil)
11118       (gnus-summary-exit))))
11119
11120 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11121   "Mark all articles in this newsgroup as read, and then exit.
11122 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11123 instead, which marks only unread articles as read."
11124   (interactive "P")
11125   (gnus-summary-catchup-and-exit t quietly))
11126
11127 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11128   "Mark all articles in this group as read and select the next group.
11129 If given a prefix, mark all articles, unread as well as ticked, as
11130 read."
11131   (interactive "P")
11132   (save-excursion
11133     (gnus-summary-catchup all))
11134   (gnus-summary-next-group))
11135
11136 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11137   "Mark all articles in this group as read and select the previous group.
11138 If given a prefix, mark all articles, unread as well as ticked, as
11139 read."
11140   (interactive "P")
11141   (save-excursion
11142     (gnus-summary-catchup all))
11143   (gnus-summary-next-group nil nil t))
11144
11145 ;;;
11146 ;;; with article
11147 ;;;
11148
11149 (defmacro gnus-with-article (article &rest forms)
11150   "Select ARTICLE and perform FORMS in the original article buffer.
11151 Then replace the article with the result."
11152   `(progn
11153      ;; We don't want the article to be marked as read.
11154      (let (gnus-mark-article-hook)
11155        (gnus-summary-select-article t t nil ,article))
11156      (set-buffer gnus-original-article-buffer)
11157      ,@forms
11158      (if (not (gnus-check-backend-function
11159                'request-replace-article (car gnus-article-current)))
11160          (gnus-message 5 "Read-only group; not replacing")
11161        (unless (gnus-request-replace-article
11162                 ,article (car gnus-article-current)
11163                 (current-buffer) t)
11164          (error "Couldn't replace article")))
11165      ;; The cache and backlog have to be flushed somewhat.
11166      (when gnus-keep-backlog
11167        (gnus-backlog-remove-article
11168         (car gnus-article-current) (cdr gnus-article-current)))
11169      (when gnus-use-cache
11170        (gnus-cache-update-article
11171         (car gnus-article-current) (cdr gnus-article-current)))))
11172
11173 (put 'gnus-with-article 'lisp-indent-function 1)
11174 (put 'gnus-with-article 'edebug-form-spec '(form body))
11175
11176 ;; Thread-based commands.
11177
11178 (defun gnus-summary-articles-in-thread (&optional article)
11179   "Return a list of all articles in the current thread.
11180 If ARTICLE is non-nil, return all articles in the thread that starts
11181 with that article."
11182   (let* ((article (or article (gnus-summary-article-number)))
11183          (data (gnus-data-find-list article))
11184          (top-level (gnus-data-level (car data)))
11185          (top-subject
11186           (cond ((null gnus-thread-operation-ignore-subject)
11187                  (gnus-simplify-subject-re
11188                   (mail-header-subject (gnus-data-header (car data)))))
11189                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11190                  (gnus-simplify-subject-fuzzy
11191                   (mail-header-subject (gnus-data-header (car data)))))
11192                 (t nil)))
11193          (end-point (save-excursion
11194                       (if (gnus-summary-go-to-next-thread)
11195                           (point) (point-max))))
11196          articles)
11197     (while (and data
11198                 (< (gnus-data-pos (car data)) end-point))
11199       (when (or (not top-subject)
11200                 (string= top-subject
11201                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11202                              (gnus-simplify-subject-fuzzy
11203                               (mail-header-subject
11204                                (gnus-data-header (car data))))
11205                            (gnus-simplify-subject-re
11206                             (mail-header-subject
11207                              (gnus-data-header (car data)))))))
11208         (push (gnus-data-number (car data)) articles))
11209       (unless (and (setq data (cdr data))
11210                    (> (gnus-data-level (car data)) top-level))
11211         (setq data nil)))
11212     ;; Return the list of articles.
11213     (nreverse articles)))
11214
11215 (defun gnus-summary-rethread-current ()
11216   "Rethread the thread the current article is part of."
11217   (interactive)
11218   (let* ((gnus-show-threads t)
11219          (article (gnus-summary-article-number))
11220          (id (mail-header-id (gnus-summary-article-header)))
11221          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11222     (unless id
11223       (error "No article on the current line"))
11224     (gnus-rebuild-thread id)
11225     (gnus-summary-goto-subject article)))
11226
11227 (defun gnus-summary-reparent-thread ()
11228   "Make the current article child of the marked (or previous) article.
11229
11230 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11231 is non-nil or the Subject: of both articles are the same."
11232   (interactive)
11233   (unless (not (gnus-group-read-only-p))
11234     (error "The current newsgroup does not support article editing"))
11235   (unless (<= (length gnus-newsgroup-processable) 1)
11236     (error "No more than one article may be marked"))
11237   (let ((child (gnus-summary-article-number))
11238         ;; First grab the marked article, otherwise one line up.
11239         (parent (if (not (null gnus-newsgroup-processable))
11240                     (car gnus-newsgroup-processable)
11241                   (save-excursion
11242                     (if (eq (forward-line -1) 0)
11243                         (gnus-summary-article-number)
11244                       (error "Beginning of summary buffer"))))))
11245     (gnus-summary-reparent-children parent (list child))))
11246
11247 (defun gnus-summary-reparent-children (parent children)
11248   "Make PARENT the parent of CHILDREN.
11249 When called interactively, PARENT is the current article and CHILDREN
11250 are the process-marked articles."
11251   (interactive
11252    (list (gnus-summary-article-number)
11253          (gnus-summary-work-articles nil)))
11254   (dolist (child children)
11255     (save-window-excursion
11256       (let ((gnus-article-buffer " *reparent*"))
11257         (unless (not (eq parent child))
11258           (error "An article may not be self-referential"))
11259         (let ((message-id (mail-header-id
11260                            (gnus-summary-article-header parent))))
11261           (unless (and message-id (not (equal message-id "")))
11262             (error "No message-id in desired parent"))
11263           (gnus-with-article child
11264             (save-restriction
11265               (goto-char (point-min))
11266               (message-narrow-to-head)
11267               (if (re-search-forward "^References: " nil t)
11268                   (progn
11269                     (re-search-forward "^[^ \t]" nil t)
11270                     (forward-line -1)
11271                     (end-of-line)
11272                     (insert " " message-id))
11273                 (insert "References: " message-id "\n"))))
11274           (set-buffer gnus-summary-buffer)
11275           (gnus-summary-unmark-all-processable)
11276           (gnus-summary-update-article child)
11277           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11278             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11279           (gnus-summary-rethread-current)
11280           (gnus-message 3 "Article %d is now the child of article %d"
11281                         child parent))))))
11282
11283 (defun gnus-summary-toggle-threads (&optional arg)
11284   "Toggle showing conversation threads.
11285 If ARG is positive number, turn showing conversation threads on."
11286   (interactive "P")
11287   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11288     (setq gnus-show-threads
11289           (if (null arg) (not gnus-show-threads)
11290             (> (prefix-numeric-value arg) 0)))
11291     (gnus-summary-prepare)
11292     (gnus-summary-goto-subject current)
11293     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11294     (gnus-summary-position-point)))
11295
11296 (eval-and-compile
11297   (if (fboundp 'remove-overlays)
11298       (defalias 'gnus-remove-overlays 'remove-overlays)
11299     (defun gnus-remove-overlays (beg end name val)
11300       "Clear BEG and END of overlays whose property NAME has value VAL.
11301 For compatibility with XEmacs."
11302       (dolist (ov (gnus-overlays-in beg end))
11303         (when (eq (gnus-overlay-get ov name) val)
11304           (gnus-delete-overlay ov))))))
11305
11306 (defun gnus-summary-show-all-threads ()
11307   "Show all threads."
11308   (interactive)
11309   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11310   (gnus-summary-position-point))
11311
11312 (defsubst gnus-summary--inv (p)
11313   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11314
11315 (defun gnus-summary-show-thread ()
11316   "Show thread subtrees.
11317 Returns nil if no thread was there to be shown."
11318   (interactive)
11319   (let* ((orig (point))
11320          (end (point-at-eol))
11321          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11322          ;; Leave point at bol
11323          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11324          (eoi (when end
11325                 (if (fboundp 'next-single-char-property-change)
11326                     (or (next-single-char-property-change end 'invisible)
11327                         (point-max))
11328                   (while (progn
11329                            (end-of-line 2)
11330                            (and (not (eobp))
11331                                 (eq (get-char-property (point) 'invisible)
11332                                     'gnus-sum))))
11333                   (point)))))
11334     (when eoi
11335       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11336       (goto-char orig)
11337       (gnus-summary-position-point)
11338       eoi)))
11339
11340 (defun gnus-summary-maybe-hide-threads ()
11341   "If requested, hide the threads that should be hidden."
11342   (when (and gnus-show-threads
11343              gnus-thread-hide-subtree)
11344     (gnus-summary-hide-all-threads
11345      (if (or (consp gnus-thread-hide-subtree)
11346              (functionp gnus-thread-hide-subtree))
11347          (gnus-make-predicate gnus-thread-hide-subtree)
11348        nil))))
11349
11350 ;;; Hiding predicates.
11351
11352 (defun gnus-article-unread-p (header)
11353   (memq (mail-header-number header) gnus-newsgroup-unreads))
11354
11355 (defun gnus-article-unseen-p (header)
11356   (memq (mail-header-number header) gnus-newsgroup-unseen))
11357
11358 (defun gnus-map-articles (predicate articles)
11359   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11360   (apply 'gnus-or (mapcar predicate
11361                           (mapcar (lambda (number)
11362                                     (gnus-summary-article-header number))
11363                                   articles))))
11364
11365 (defun gnus-summary-hide-all-threads (&optional predicate)
11366   "Hide all thread subtrees.
11367 If PREDICATE is supplied, threads that satisfy this predicate
11368 will not be hidden."
11369   (interactive)
11370   (save-excursion
11371     (goto-char (point-min))
11372     (let ((end nil))
11373       (while (not end)
11374         (when (or (not predicate)
11375                   (gnus-map-articles
11376                    predicate (gnus-summary-article-children)))
11377             (gnus-summary-hide-thread))
11378         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11379   (gnus-summary-position-point))
11380
11381 (defun gnus-summary-hide-thread ()
11382   "Hide thread subtrees.
11383 If PREDICATE is supplied, threads that satisfy this predicate
11384 will not be hidden.
11385 Returns nil if no threads were there to be hidden."
11386   (interactive)
11387   (let ((start (point))
11388         (starteol (line-end-position))
11389         (article (gnus-summary-article-number)))
11390     (goto-char start)
11391     ;; Go forward until either the buffer ends or the subthread ends.
11392     (when (and (not (eobp))
11393                (or (zerop (gnus-summary-next-thread 1 t))
11394                    (goto-char (point-max))))
11395       (if (and (> (point) start)
11396                ;; FIXME: this should actually search for a non-invisible \n.
11397                (search-backward "\n" start t))
11398           (progn
11399             (when (> (point) starteol)
11400               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11401               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11402                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11403                 (gnus-overlay-put ol 'evaporate t)))
11404             (gnus-summary-goto-subject article))
11405         (goto-char start)
11406         nil))))
11407
11408 (defun gnus-summary-go-to-next-thread (&optional previous)
11409   "Go to the same level (or less) next thread.
11410 If PREVIOUS is non-nil, go to previous thread instead.
11411 Return the article number moved to, or nil if moving was impossible."
11412   (let ((level (gnus-summary-thread-level))
11413         (way (if previous -1 1))
11414         (beg (point)))
11415     (forward-line way)
11416     (while (and (not (eobp))
11417                 (< level (gnus-summary-thread-level)))
11418       (forward-line way))
11419     (if (eobp)
11420         (progn
11421           (goto-char beg)
11422           nil)
11423       (setq beg (point))
11424       (prog1
11425           (gnus-summary-article-number)
11426         (goto-char beg)))))
11427
11428 (defun gnus-summary-next-thread (n &optional silent)
11429   "Go to the same level next N'th thread.
11430 If N is negative, search backward instead.
11431 Returns the difference between N and the number of skips actually
11432 done.
11433
11434 If SILENT, don't output messages."
11435   (interactive "p")
11436   (let ((backward (< n 0))
11437         (n (abs n)))
11438     (while (and (> n 0)
11439                 (gnus-summary-go-to-next-thread backward))
11440       (decf n))
11441     (unless silent
11442       (gnus-summary-position-point))
11443     (when (and (not silent) (/= 0 n))
11444       (gnus-message 7 "No more threads"))
11445     n))
11446
11447 (defun gnus-summary-prev-thread (n)
11448   "Go to the same level previous N'th thread.
11449 Returns the difference between N and the number of skips actually
11450 done."
11451   (interactive "p")
11452   (gnus-summary-next-thread (- n)))
11453
11454 (defun gnus-summary-go-down-thread ()
11455   "Go down one level in the current thread."
11456   (let ((children (gnus-summary-article-children)))
11457     (when children
11458       (gnus-summary-goto-subject (car children)))))
11459
11460 (defun gnus-summary-go-up-thread ()
11461   "Go up one level in the current thread."
11462   (let ((parent (gnus-summary-article-parent)))
11463     (when parent
11464       (gnus-summary-goto-subject parent))))
11465
11466 (defun gnus-summary-down-thread (n)
11467   "Go down thread N steps.
11468 If N is negative, go up instead.
11469 Returns the difference between N and how many steps down that were
11470 taken."
11471   (interactive "p")
11472   (let ((up (< n 0))
11473         (n (abs n)))
11474     (while (and (> n 0)
11475                 (if up (gnus-summary-go-up-thread)
11476                   (gnus-summary-go-down-thread)))
11477       (setq n (1- n)))
11478     (gnus-summary-position-point)
11479     (when (/= 0 n)
11480       (gnus-message 7 "Can't go further"))
11481     n))
11482
11483 (defun gnus-summary-up-thread (n)
11484   "Go up thread N steps.
11485 If N is negative, go down instead.
11486 Returns the difference between N and how many steps down that were
11487 taken."
11488   (interactive "p")
11489   (gnus-summary-down-thread (- n)))
11490
11491 (defun gnus-summary-top-thread ()
11492   "Go to the top of the thread."
11493   (interactive)
11494   (while (gnus-summary-go-up-thread))
11495   (gnus-summary-article-number))
11496
11497 (defun gnus-summary-expire-thread ()
11498   "Mark articles under current thread as expired."
11499   (interactive)
11500   (gnus-summary-kill-thread 0))
11501
11502 (defun gnus-summary-kill-thread (&optional unmark)
11503   "Mark articles under current thread as read.
11504 If the prefix argument is positive, remove any kinds of marks.
11505 If the prefix argument is zero, mark thread as expired.
11506 If the prefix argument is negative, tick articles instead."
11507   (interactive "P")
11508   (when unmark
11509     (setq unmark (prefix-numeric-value unmark)))
11510   (let ((articles (gnus-summary-articles-in-thread))
11511         (hide (or (null unmark) (= unmark 0))))
11512     (save-excursion
11513       ;; Expand the thread.
11514       (gnus-summary-show-thread)
11515       ;; Mark all the articles.
11516       (while articles
11517         (gnus-summary-goto-subject (car articles))
11518         (cond ((null unmark)
11519                (gnus-summary-mark-article-as-read gnus-killed-mark))
11520               ((> unmark 0)
11521                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11522               ((= unmark 0)
11523                (gnus-summary-mark-article nil gnus-expirable-mark))
11524               (t
11525                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11526         (setq articles (cdr articles))))
11527     ;; Hide killed subtrees when hide is true.
11528     (and hide
11529          gnus-thread-hide-killed
11530          (gnus-summary-hide-thread))
11531     ;; If hide is t, go to next unread subject.
11532     (when hide
11533       ;; Go to next unread subject.
11534       (gnus-summary-next-subject 1 t)))
11535   (gnus-set-mode-line 'summary))
11536
11537 ;; Summary sorting commands
11538
11539 (defun gnus-summary-sort-by-number (&optional reverse)
11540   "Sort the summary buffer by article number.
11541 Argument REVERSE means reverse order."
11542   (interactive "P")
11543   (gnus-summary-sort 'number reverse))
11544
11545 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11546   "Sort the summary buffer by most recent article number.
11547 Argument REVERSE means reverse order."
11548   (interactive "P")
11549   (gnus-summary-sort 'most-recent-number reverse))
11550
11551 (defun gnus-summary-sort-by-random (&optional reverse)
11552   "Randomize the order in the summary buffer.
11553 Argument REVERSE means to randomize in reverse order."
11554   (interactive "P")
11555   (gnus-summary-sort 'random reverse))
11556
11557 (defun gnus-summary-sort-by-author (&optional reverse)
11558   "Sort the summary buffer by author name alphabetically.
11559 If `case-fold-search' is non-nil, case of letters is ignored.
11560 Argument REVERSE means reverse order."
11561   (interactive "P")
11562   (gnus-summary-sort 'author reverse))
11563
11564 (defun gnus-summary-sort-by-recipient (&optional reverse)
11565   "Sort the summary buffer by recipient name alphabetically.
11566 If `case-fold-search' is non-nil, case of letters is ignored.
11567 Argument REVERSE means reverse order."
11568   (interactive "P")
11569   (gnus-summary-sort 'recipient reverse))
11570
11571 (defun gnus-summary-sort-by-subject (&optional reverse)
11572   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11573 If `case-fold-search' is non-nil, case of letters is ignored.
11574 Argument REVERSE means reverse order."
11575   (interactive "P")
11576   (gnus-summary-sort 'subject reverse))
11577
11578 (defun gnus-summary-sort-by-date (&optional reverse)
11579   "Sort the summary buffer by date.
11580 Argument REVERSE means reverse order."
11581   (interactive "P")
11582   (gnus-summary-sort 'date reverse))
11583
11584 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11585   "Sort the summary buffer by most recent date.
11586 Argument REVERSE means reverse order."
11587   (interactive "P")
11588   (gnus-summary-sort 'most-recent-date reverse))
11589
11590 (defun gnus-summary-sort-by-score (&optional reverse)
11591   "Sort the summary buffer by score.
11592 Argument REVERSE means reverse order."
11593   (interactive "P")
11594   (gnus-summary-sort 'score reverse))
11595
11596 (defun gnus-summary-sort-by-lines (&optional reverse)
11597   "Sort the summary buffer by the number of lines.
11598 Argument REVERSE means reverse order."
11599   (interactive "P")
11600   (gnus-summary-sort 'lines reverse))
11601
11602 (defun gnus-summary-sort-by-chars (&optional reverse)
11603   "Sort the summary buffer by article length.
11604 Argument REVERSE means reverse order."
11605   (interactive "P")
11606   (gnus-summary-sort 'chars reverse))
11607
11608 (defun gnus-summary-sort-by-original (&optional reverse)
11609   "Sort the summary buffer using the default sorting method.
11610 Argument REVERSE means reverse order."
11611   (interactive "P")
11612   (let* ((inhibit-read-only t)
11613          (gnus-summary-prepare-hook nil))
11614     ;; We do the sorting by regenerating the threads.
11615     (gnus-summary-prepare)
11616     ;; Hide subthreads if needed.
11617     (gnus-summary-maybe-hide-threads)))
11618
11619 (defun gnus-summary-sort (predicate reverse)
11620   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11621   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11622          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11623          (gnus-thread-sort-functions
11624           (if (not reverse)
11625               thread
11626             `(lambda (t1 t2)
11627                (,thread t2 t1))))
11628          (gnus-sort-gathered-threads-function
11629           gnus-thread-sort-functions)
11630          (gnus-article-sort-functions
11631           (if (not reverse)
11632               article
11633             `(lambda (t1 t2)
11634                (,article t2 t1))))
11635          (inhibit-read-only t)
11636          (gnus-summary-prepare-hook nil))
11637     ;; We do the sorting by regenerating the threads.
11638     (gnus-summary-prepare)
11639     ;; Hide subthreads if needed.
11640     (gnus-summary-maybe-hide-threads)))
11641
11642 ;; Summary saving commands.
11643
11644 (defun gnus-summary-save-article (&optional n not-saved)
11645   "Save the current article using the default saver function.
11646 If N is a positive number, save the N next articles.
11647 If N is a negative number, save the N previous articles.
11648 If N is nil and any articles have been marked with the process mark,
11649 save those articles instead.
11650 The variable `gnus-default-article-saver' specifies the saver function.
11651
11652 If the optional second argument NOT-SAVED is non-nil, articles saved
11653 will not be marked as saved."
11654   (interactive "P")
11655   (require 'gnus-art)
11656   (let* ((articles (gnus-summary-work-articles n))
11657          (save-buffer (save-excursion
11658                         (nnheader-set-temp-buffer " *Gnus Save*")))
11659          (num (length articles))
11660          ;; Whether to save decoded articles or raw articles.
11661          (decode (when gnus-article-save-coding-system
11662                    (get gnus-default-article-saver :decode)))
11663          ;; When saving many articles in a single file, use the other
11664          ;; function to save articles other than the first one.
11665          (saver2 (get gnus-default-article-saver :function))
11666          (gnus-prompt-before-saving (if saver2
11667                                         t
11668                                       gnus-prompt-before-saving))
11669          (gnus-default-article-saver gnus-default-article-saver)
11670          header file)
11671     (dolist (article articles)
11672       (setq header (gnus-summary-article-header article))
11673       (if (not (vectorp header))
11674           ;; This is a pseudo-article.
11675           (if (assq 'name header)
11676               (gnus-copy-file (cdr (assq 'name header)))
11677             (gnus-message 1 "Article %d is unsaveable" article))
11678         ;; This is a real article.
11679         (save-window-excursion
11680           (gnus-summary-select-article decode decode nil article)
11681           (gnus-summary-goto-subject article))
11682         (with-current-buffer save-buffer
11683           (erase-buffer)
11684           (insert-buffer-substring (if decode
11685                                        gnus-article-buffer
11686                                      gnus-original-article-buffer)))
11687         (setq file (gnus-article-save save-buffer file num))
11688         (gnus-summary-remove-process-mark article)
11689         (unless not-saved
11690           (gnus-summary-set-saved-mark article)))
11691       (when saver2
11692         (setq gnus-default-article-saver saver2
11693               saver2 nil)))
11694     (gnus-kill-buffer save-buffer)
11695     (gnus-summary-position-point)
11696     (gnus-set-mode-line 'summary)
11697     n))
11698
11699 (defun gnus-summary-pipe-output (&optional n sym)
11700   "Pipe the current article to a subprocess.
11701 If N is a positive number, pipe the N next articles.
11702 If N is a negative number, pipe the N previous articles.
11703 If N is nil and any articles have been marked with the process mark,
11704 pipe those articles instead.
11705 The default command to which articles are piped is specified by the
11706 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11707 will be prompted for the command.
11708
11709 The properties `:decode' and `:headers' that are put to the function
11710 symbol `gnus-summary-save-in-pipe' control whether this function
11711 decodes articles and what headers to keep (see the doc string for the
11712 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11713 is neither omitted nor the symbol `r', force including all headers
11714 regardless of the `:headers' property.  If it is the symbol `r',
11715 articles that are not decoded and include all headers will be piped
11716 no matter what the properties `:decode' and `:headers' are."
11717   (interactive (gnus-interactive "P\ny"))
11718   (require 'gnus-art)
11719   (let* ((articles (gnus-summary-work-articles n))
11720          (result-buffer "*Shell Command Output*")
11721          (all-headers (not (memq sym '(nil r))))
11722          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11723          (raw (eq sym 'r))
11724          (headers (get 'gnus-summary-save-in-pipe :headers))
11725          command result)
11726     (unless (numberp (car articles))
11727       (error "No article to pipe"))
11728     (setq command (gnus-read-shell-command
11729                    (concat "Shell command on "
11730                            (if (cdr articles)
11731                                (format "these %d articles" (length articles))
11732                              "this article")
11733                            ": ")
11734                    gnus-summary-pipe-output-default-command))
11735     (when (string-equal command "")
11736       (error "A command is required"))
11737     (when all-headers
11738       (put 'gnus-summary-save-in-pipe :headers nil))
11739     (unwind-protect
11740         (while articles
11741           (gnus-summary-goto-subject (pop articles))
11742           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11743           (when (and (get-buffer result-buffer)
11744                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11745             (setq result (concat result (with-current-buffer result-buffer
11746                                           (buffer-string))))))
11747       (put 'gnus-summary-save-in-pipe :headers headers))
11748     (unless (zerop (length result))
11749       (if (with-current-buffer (get-buffer-create result-buffer)
11750             (erase-buffer)
11751             (insert result)
11752             (prog1
11753                 (and (= (count-lines (point-min) (point)) 1)
11754                      (progn
11755                        (end-of-line 0)
11756                        (<= (current-column)
11757                            (window-width (minibuffer-window)))))
11758               (goto-char (point-min))))
11759           (message "%s" (substring result 0 -1))
11760         (message nil)
11761         (gnus-configure-windows 'pipe)))))
11762
11763 (defun gnus-summary-save-article-mail (&optional arg)
11764   "Append the current article to a Unix mail box file.
11765 If N is a positive number, save the N next articles.
11766 If N is a negative number, save the N previous articles.
11767 If N is nil and any articles have been marked with the process mark,
11768 save those articles instead."
11769   (interactive "P")
11770   (require 'gnus-art)
11771   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11772     (gnus-summary-save-article arg)))
11773
11774 (defun gnus-summary-save-article-rmail (&optional arg)
11775   "Append the current article to an rmail file.
11776 If N is a positive number, save the N next articles.
11777 If N is a negative number, save the N previous articles.
11778 If N is nil and any articles have been marked with the process mark,
11779 save those articles instead."
11780   (interactive "P")
11781   (require 'gnus-art)
11782   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11783     (gnus-summary-save-article arg)))
11784
11785 (defun gnus-summary-save-article-file (&optional arg)
11786   "Append the current article to a file.
11787 If N is a positive number, save the N next articles.
11788 If N is a negative number, save the N previous articles.
11789 If N is nil and any articles have been marked with the process mark,
11790 save those articles instead."
11791   (interactive "P")
11792   (require 'gnus-art)
11793   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11794     (gnus-summary-save-article arg)))
11795
11796 (defun gnus-summary-write-article-file (&optional arg)
11797   "Write the current article to a file, deleting the previous file.
11798 If N is a positive number, save the N next articles.
11799 If N is a negative number, save the N previous articles.
11800 If N is nil and any articles have been marked with the process mark,
11801 save those articles instead."
11802   (interactive "P")
11803   (require 'gnus-art)
11804   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11805     (gnus-summary-save-article arg)))
11806
11807 (defun gnus-summary-save-article-body-file (&optional arg)
11808   "Append the current article body to a file.
11809 If N is a positive number, save the N next articles.
11810 If N is a negative number, save the N previous articles.
11811 If N is nil and any articles have been marked with the process mark,
11812 save those articles instead."
11813   (interactive "P")
11814   (require 'gnus-art)
11815   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11816     (gnus-summary-save-article arg)))
11817
11818 (defun gnus-summary-write-article-body-file (&optional arg)
11819   "Write the current article body to a file, deleting the previous file.
11820 If N is a positive number, save the N next articles.
11821 If N is a negative number, save the N previous articles.
11822 If N is nil and any articles have been marked with the process mark,
11823 save those articles instead."
11824   (interactive "P")
11825   (require 'gnus-art)
11826   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11827     (gnus-summary-save-article arg)))
11828
11829 (defun gnus-summary-muttprint (&optional arg)
11830   "Print the current article using Muttprint.
11831 If N is a positive number, save the N next articles.
11832 If N is a negative number, save the N previous articles.
11833 If N is nil and any articles have been marked with the process mark,
11834 save those articles instead."
11835   (interactive "P")
11836   (require 'gnus-art)
11837   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11838     (gnus-summary-save-article arg t)))
11839
11840 (defun gnus-summary-pipe-message (program)
11841   "Pipe the current article through PROGRAM."
11842   (interactive "sProgram: ")
11843   (gnus-summary-select-article)
11844   (let ((mail-header-separator ""))
11845     (gnus-eval-in-buffer-window gnus-article-buffer
11846       (save-restriction
11847         (widen)
11848         (let ((start (window-start))
11849               (inhibit-read-only t))
11850           (message-pipe-buffer-body program)
11851           (set-window-start (get-buffer-window (current-buffer)) start))))))
11852
11853 (defun gnus-get-split-value (methods)
11854   "Return a value based on the split METHODS."
11855   (let (split-name method result match)
11856     (when methods
11857       (with-current-buffer gnus-original-article-buffer
11858         (save-restriction
11859           (nnheader-narrow-to-headers)
11860           (while (and methods (not split-name))
11861             (goto-char (point-min))
11862             (setq method (pop methods))
11863             (setq match (car method))
11864             (when (cond
11865                    ((stringp match)
11866                     ;; Regular expression.
11867                     (ignore-errors
11868                       (re-search-forward match nil t)))
11869                    ((functionp match)
11870                     ;; Function.
11871                     (save-restriction
11872                       (widen)
11873                       (setq result (funcall match gnus-newsgroup-name))))
11874                    ((consp match)
11875                     ;; Form.
11876                     (save-restriction
11877                       (widen)
11878                       (setq result (eval match)))))
11879               (setq split-name (cdr method))
11880               (cond ((stringp result)
11881                      (push (expand-file-name
11882                             result gnus-article-save-directory)
11883                            split-name))
11884                     ((consp result)
11885                      (setq split-name (append result split-name)))))))))
11886     (nreverse split-name)))
11887
11888 (defun gnus-valid-move-group-p (group)
11889   (and (symbolp group)
11890        (boundp group)
11891        (symbol-name group)
11892        (symbol-value group)
11893        (gnus-get-function (gnus-find-method-for-group
11894                            (symbol-name group)) 'request-accept-article t)))
11895
11896 (defun gnus-read-move-group-name (prompt default articles prefix)
11897   "Read a group name."
11898   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11899          (minibuffer-confirm-incomplete nil) ; XEmacs
11900          (prom
11901           (format "%s %s to"
11902                   prompt
11903                   (if (> (length articles) 1)
11904                       (format "these %d articles" (length articles))
11905                     "this article")))
11906          (to-newsgroup
11907           (cond
11908            ((null split-name)
11909             (gnus-group-completing-read
11910              prom
11911              (remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb)
11912              nil prefix nil default))
11913            ((= 1 (length split-name))
11914             (gnus-group-completing-read
11915              prom (remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb)
11916              nil prefix 'gnus-group-history (car split-name)))
11917            (t
11918             (gnus-completing-read
11919              prom (nreverse split-name) nil nil 'gnus-group-history))))
11920          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11921          encoded)
11922     (when to-newsgroup
11923       (if (or (string= to-newsgroup "")
11924               (string= to-newsgroup prefix))
11925           (setq to-newsgroup default))
11926       (unless to-newsgroup
11927         (error "No group name entered"))
11928       (setq encoded (mm-encode-coding-string
11929                      to-newsgroup
11930                      (gnus-group-name-charset to-method to-newsgroup)))
11931       (or (gnus-active encoded)
11932           (gnus-activate-group encoded nil nil to-method)
11933           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11934                                      to-newsgroup))
11935               (or (and (gnus-request-create-group encoded to-method)
11936                        (gnus-activate-group encoded nil nil to-method)
11937                        (gnus-subscribe-group encoded))
11938                   (error "Couldn't create group %s" to-newsgroup)))
11939           (error "No such group: %s" to-newsgroup))
11940       encoded)))
11941
11942 (defvar gnus-summary-save-parts-counter)
11943 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11944
11945 (defun gnus-summary-save-parts (type dir n &optional reverse)
11946   "Save parts matching TYPE to DIR.
11947 If REVERSE, save parts that do not match TYPE."
11948   (interactive
11949    (list (read-string "Save parts of type: "
11950                       (or (car gnus-summary-save-parts-type-history)
11951                           gnus-summary-save-parts-default-mime)
11952                       'gnus-summary-save-parts-type-history)
11953          (setq gnus-summary-save-parts-last-directory
11954                (read-file-name "Save to directory: "
11955                                gnus-summary-save-parts-last-directory
11956                                nil t))
11957          current-prefix-arg))
11958   (gnus-summary-iterate n
11959     (let ((gnus-display-mime-function nil)
11960           gnus-article-prepare-hook
11961           gnus-article-decode-hook
11962           gnus-display-mime-function
11963           gnus-break-pages
11964           (gnus-inhibit-treatment t))
11965       (gnus-summary-select-article))
11966     (with-current-buffer gnus-article-buffer
11967       (let ((handles (or gnus-article-mime-handles
11968                          (mm-dissect-buffer nil gnus-article-loose-mime)
11969                          (and gnus-article-emulate-mime
11970                               (mm-uu-dissect))))
11971             (gnus-summary-save-parts-counter 1))
11972         (when handles
11973           (gnus-summary-save-parts-1 type dir handles reverse)
11974           (unless gnus-article-mime-handles ;; Don't destroy this case.
11975             (mm-destroy-parts handles)))))))
11976
11977 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11978   (if (stringp (car handle))
11979       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11980               (cdr handle))
11981     (when (if reverse
11982               (not (string-match type (mm-handle-media-type handle)))
11983             (string-match type (mm-handle-media-type handle)))
11984       (let ((file (expand-file-name
11985                    (gnus-map-function
11986                     mm-file-name-rewrite-functions
11987                     (file-name-nondirectory
11988                      (or
11989                       (mail-content-type-get
11990                        (mm-handle-disposition handle) 'filename)
11991                       (mail-content-type-get
11992                        (mm-handle-type handle) 'name)
11993                       (format "%s.%d.%d" gnus-newsgroup-name
11994                               (cdr gnus-article-current)
11995                               gnus-summary-save-parts-counter))))
11996                    dir)))
11997         (incf gnus-summary-save-parts-counter)
11998         (unless (file-exists-p file)
11999           (mm-save-part-to-file handle file))))))
12000
12001 ;; Summary extract commands
12002
12003 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12004   (let ((inhibit-read-only t)
12005         (article (gnus-summary-article-number))
12006         after-article b e)
12007     (unless (gnus-summary-goto-subject article)
12008       (error "No such article: %d" article))
12009     (gnus-summary-position-point)
12010     ;; If all commands are to be bunched up on one line, we collect
12011     ;; them here.
12012     (unless gnus-view-pseudos-separately
12013       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12014             files action)
12015         (while ps
12016           (setq action (cdr (assq 'action (car ps))))
12017           (setq files (list (cdr (assq 'name (car ps)))))
12018           (while (and ps (cdr ps)
12019                       (string= (or action "1")
12020                                (or (cdr (assq 'action (cadr ps))) "2")))
12021             (push (cdr (assq 'name (cadr ps))) files)
12022             (setcdr ps (cddr ps)))
12023           (when files
12024             (when (not (string-match "%s" action))
12025               (push " " files))
12026             (push " " files)
12027             (when (assq 'execute (car ps))
12028               (setcdr (assq 'execute (car ps))
12029                       (funcall (if (string-match "%s" action)
12030                                    'format 'concat)
12031                                action
12032                                (mapconcat
12033                                 (lambda (f)
12034                                   (if (equal f " ")
12035                                       f
12036                                     (shell-quote-argument f)))
12037                                 files " ")))))
12038           (setq ps (cdr ps)))))
12039     (if (and gnus-view-pseudos (not not-view))
12040         (while pslist
12041           (when (assq 'execute (car pslist))
12042             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12043                                   (eq gnus-view-pseudos 'not-confirm)))
12044           (setq pslist (cdr pslist)))
12045       (save-excursion
12046         (while pslist
12047           (setq after-article (or (cdr (assq 'article (car pslist)))
12048                                   (gnus-summary-article-number)))
12049           (gnus-summary-goto-subject after-article)
12050           (forward-line 1)
12051           (setq b (point))
12052           (insert "    " (file-name-nondirectory
12053                           (cdr (assq 'name (car pslist))))
12054                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12055           (setq e (point))
12056           (forward-line -1)             ; back to `b'
12057           (gnus-add-text-properties
12058            b (1- e) (list 'gnus-number gnus-reffed-article-number
12059                           gnus-mouse-face-prop gnus-mouse-face))
12060           (gnus-data-enter
12061            after-article gnus-reffed-article-number
12062            gnus-unread-mark b (car pslist) 0 (- e b))
12063           (setq gnus-newsgroup-unreads
12064                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12065                                          gnus-reffed-article-number))
12066           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12067           (setq pslist (cdr pslist)))))))
12068
12069 (defun gnus-pseudos< (p1 p2)
12070   (let ((c1 (cdr (assq 'action p1)))
12071         (c2 (cdr (assq 'action p2))))
12072     (and c1 c2 (string< c1 c2))))
12073
12074 (defun gnus-request-pseudo-article (props)
12075   (cond ((assq 'execute props)
12076          (gnus-execute-command (cdr (assq 'execute props)))))
12077   (let ((gnus-current-article (gnus-summary-article-number)))
12078     (gnus-run-hooks 'gnus-mark-article-hook)))
12079
12080 (defun gnus-execute-command (command &optional automatic)
12081   (save-excursion
12082     (gnus-article-setup-buffer)
12083     (set-buffer gnus-article-buffer)
12084     (setq buffer-read-only nil)
12085     (let ((command (if automatic command
12086                      (read-string "Command: " (cons command 0)))))
12087       (erase-buffer)
12088       (insert "$ " command "\n\n")
12089       (if gnus-view-pseudo-asynchronously
12090           (start-process "gnus-execute" (current-buffer) shell-file-name
12091                          shell-command-switch command)
12092         (call-process shell-file-name nil t nil
12093                       shell-command-switch command)))))
12094
12095 ;; Summary kill commands.
12096
12097 (defun gnus-summary-edit-global-kill (article)
12098   "Edit the \"global\" kill file."
12099   (interactive (list (gnus-summary-article-number)))
12100   (gnus-group-edit-global-kill article))
12101
12102 (defun gnus-summary-edit-local-kill ()
12103   "Edit a local kill file applied to the current newsgroup."
12104   (interactive)
12105   (setq gnus-current-headers (gnus-summary-article-header))
12106   (gnus-group-edit-local-kill
12107    (gnus-summary-article-number) gnus-newsgroup-name))
12108
12109 ;;; Header reading.
12110
12111 (defun gnus-read-header (id &optional header)
12112   "Read the headers of article ID and enter them into the Gnus system."
12113   (let ((group gnus-newsgroup-name)
12114         (gnus-override-method
12115          (or
12116           gnus-override-method
12117           (and (gnus-news-group-p gnus-newsgroup-name)
12118                (car (gnus-refer-article-methods)))))
12119         where)
12120     ;; First we check to see whether the header in question is already
12121     ;; fetched.
12122     (if (stringp id)
12123         ;; This is a Message-ID.
12124         (setq header (or header (gnus-id-to-header id)))
12125       ;; This is an article number.
12126       (setq header (or header (gnus-summary-article-header id))))
12127     (if (and header
12128              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12129         ;; We have found the header.
12130         header
12131       ;; We have to really fetch the header to this article.
12132       (with-current-buffer nntp-server-buffer
12133         (when (setq where (gnus-request-head id group))
12134           (nnheader-fold-continuation-lines)
12135           (goto-char (point-max))
12136           (insert ".\n")
12137           (goto-char (point-min))
12138           (insert "211 ")
12139           (princ (cond
12140                   ((numberp id) id)
12141                   ((cdr where) (cdr where))
12142                   (header (mail-header-number header))
12143                   (t gnus-reffed-article-number))
12144                  (current-buffer))
12145           (insert " Article retrieved.\n"))
12146         (if (or (not where)
12147                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12148             ()                          ; Malformed head.
12149           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12150             (when (and (stringp id)
12151                        (or
12152                         (not (string= (gnus-group-real-name group)
12153                                       (car where)))
12154                         (not (gnus-server-equal gnus-override-method
12155                                                 (gnus-group-method group)))))
12156               ;; If we fetched by Message-ID and the article came from
12157               ;; a different group (or server), we fudge some bogus
12158               ;; article numbers for this article.
12159               (mail-header-set-number header gnus-reffed-article-number))
12160             (with-current-buffer gnus-summary-buffer
12161               (decf gnus-reffed-article-number)
12162               (gnus-remove-header (mail-header-number header))
12163               (push header gnus-newsgroup-headers)
12164               (setq gnus-current-headers header)
12165               (push (mail-header-number header) gnus-newsgroup-limit)))
12166           header)))))
12167
12168 (defun gnus-remove-header (number)
12169   "Remove header NUMBER from `gnus-newsgroup-headers'."
12170   (if (and gnus-newsgroup-headers
12171            (= number (mail-header-number (car gnus-newsgroup-headers))))
12172       (pop gnus-newsgroup-headers)
12173     (let ((headers gnus-newsgroup-headers))
12174       (while (and (cdr headers)
12175                   (not (= number (mail-header-number (cadr headers)))))
12176         (pop headers))
12177       (when (cdr headers)
12178         (setcdr headers (cddr headers))))))
12179
12180 ;;;
12181 ;;; summary highlights
12182 ;;;
12183
12184 (defun gnus-highlight-selected-summary ()
12185   "Highlight selected article in summary buffer."
12186   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12187   (when gnus-summary-selected-face
12188     (save-excursion
12189       (let* ((beg (point-at-bol))
12190              (end (point-at-eol))
12191              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12192              (from (if (get-text-property beg gnus-mouse-face-prop)
12193                        beg
12194                      (or (next-single-property-change
12195                           beg gnus-mouse-face-prop nil end)
12196                          beg)))
12197              (to
12198               (if (= from end)
12199                   (- from 2)
12200                 (or (next-single-property-change
12201                      from gnus-mouse-face-prop nil end)
12202                     end))))
12203         ;; If no mouse-face prop on line we will have to = from = end,
12204         ;; so we highlight the entire line instead.
12205         (when (= (+ to 2) from)
12206           (setq from beg)
12207           (setq to end))
12208         (if gnus-newsgroup-selected-overlay
12209             ;; Move old overlay.
12210             (gnus-move-overlay
12211              gnus-newsgroup-selected-overlay from to (current-buffer))
12212           ;; Create new overlay.
12213           (gnus-overlay-put
12214            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12215            'face gnus-summary-selected-face))))))
12216
12217 (defvar gnus-summary-highlight-line-cached nil)
12218 (defvar gnus-summary-highlight-line-trigger nil)
12219
12220 (defun gnus-summary-highlight-line-0 ()
12221   (if (and (eq gnus-summary-highlight-line-trigger
12222                gnus-summary-highlight)
12223            gnus-summary-highlight-line-cached)
12224       gnus-summary-highlight-line-cached
12225     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12226           gnus-summary-highlight-line-cached
12227           (let* ((cond (list 'cond))
12228                  (c cond)
12229                  (list gnus-summary-highlight))
12230             (while list
12231               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12232                               nil))
12233               (setq c (cdr c)
12234                     list (cdr list)))
12235             (gnus-byte-compile (list 'lambda nil cond))))))
12236
12237 (defun gnus-summary-highlight-line ()
12238   "Highlight current line according to `gnus-summary-highlight'."
12239   (let* ((beg (point-at-bol))
12240          (article (or (gnus-summary-article-number) gnus-current-article))
12241          (score (or (cdr (assq article
12242                                gnus-newsgroup-scored))
12243                     gnus-summary-default-score 0))
12244          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12245          (inhibit-read-only t)
12246          (default gnus-summary-default-score)
12247          (default-high gnus-summary-default-high-score)
12248          (default-low gnus-summary-default-low-score)
12249          (uncached (and gnus-summary-use-undownloaded-faces
12250                         (memq article gnus-newsgroup-undownloaded)
12251                         (not (memq article gnus-newsgroup-cached)))))
12252     (let ((face (funcall (gnus-summary-highlight-line-0))))
12253       (unless (eq face (get-text-property beg 'face))
12254         (gnus-put-text-property-excluding-characters-with-faces
12255          beg (point-at-eol) 'face
12256          (setq face (if (boundp face) (symbol-value face) face)))
12257         (when gnus-summary-highlight-line-function
12258           (funcall gnus-summary-highlight-line-function article face))))))
12259
12260 (defun gnus-update-read-articles (group unread &optional compute)
12261   "Update the list of read articles in GROUP.
12262 UNREAD is a sorted list."
12263   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12264         (info (gnus-get-info group))
12265         (prev 1)
12266         read)
12267     (if (or (not info) (not active))
12268         ;; There is no info on this group if it was, in fact,
12269         ;; killed.  Gnus stores no information on killed groups, so
12270         ;; there's nothing to be done.
12271         ;; One could store the information somewhere temporarily,
12272         ;; perhaps...  Hmmm...
12273         ()
12274       ;; Remove any negative articles numbers.
12275       (while (and unread (< (car unread) 0))
12276         (setq unread (cdr unread)))
12277       ;; Remove any expired article numbers
12278       (while (and unread (< (car unread) (car active)))
12279         (setq unread (cdr unread)))
12280       ;; Compute the ranges of read articles by looking at the list of
12281       ;; unread articles.
12282       (while unread
12283         (when (/= (car unread) prev)
12284           (push (if (= prev (1- (car unread))) prev
12285                   (cons prev (1- (car unread))))
12286                 read))
12287         (setq prev (1+ (car unread)))
12288         (setq unread (cdr unread)))
12289       (when (<= prev (cdr active))
12290         (push (cons prev (cdr active)) read))
12291       (setq read (if (> (length read) 1) (nreverse read) read))
12292       (if compute
12293           read
12294         (save-excursion
12295           (let (setmarkundo)
12296             ;; Propagate the read marks to the backend.
12297             (when (and gnus-propagate-marks
12298                        (gnus-check-backend-function 'request-set-mark group))
12299               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12300                     (add (gnus-remove-from-range read (gnus-info-read info))))
12301                 (when (or add del)
12302                   (unless (gnus-check-group group)
12303                     (error "Can't open server for %s" group))
12304                   (gnus-request-set-mark
12305                    group (delq nil (list (if add (list add 'add '(read)))
12306                                          (if del (list del 'del '(read))))))
12307                   (setq setmarkundo
12308                         `(gnus-request-set-mark
12309                           ,group
12310                           ',(delq nil (list
12311                                        (if del (list del 'add '(read)))
12312                                        (if add (list add 'del '(read))))))))))
12313             (set-buffer gnus-group-buffer)
12314             (gnus-undo-register
12315               `(progn
12316                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12317                  (gnus-info-set-read ',info ',(gnus-info-read info))
12318                  (gnus-get-unread-articles-in-group ',info
12319                                                     (gnus-active ,group))
12320                  (gnus-group-update-group ,group t)
12321                  ,setmarkundo))))
12322         ;; Enter this list into the group info.
12323         (gnus-info-set-read info read)
12324         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12325         (gnus-get-unread-articles-in-group info (gnus-active group))
12326         t))))
12327
12328 (defun gnus-offer-save-summaries ()
12329   "Offer to save all active summary buffers."
12330   (let (buffers)
12331     ;; Go through all buffers and find all summaries.
12332     (dolist (buffer (buffer-list))
12333       (when (and (setq buffer (buffer-name buffer))
12334                  (string-match "Summary" buffer)
12335                  (with-current-buffer buffer
12336                    ;; We check that this is, indeed, a summary buffer.
12337                    (and (eq major-mode 'gnus-summary-mode)
12338                         ;; Also make sure this isn't bogus.
12339                         gnus-newsgroup-prepared
12340                         ;; Also make sure that this isn't a
12341                         ;; dead summary buffer.
12342                         (not gnus-dead-summary-mode))))
12343         (push buffer buffers)))
12344     ;; Go through all these summary buffers and offer to save them.
12345     (when buffers
12346       (save-excursion
12347         (map-y-or-n-p
12348          "Update summary buffer %s? "
12349          (lambda (buf)
12350            (switch-to-buffer buf)
12351            (gnus-summary-exit))
12352          buffers)))))
12353
12354 (defun gnus-summary-setup-default-charset ()
12355   "Setup newsgroup default charset."
12356   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12357       (setq gnus-newsgroup-charset nil)
12358     (let* ((ignored-charsets
12359             (or gnus-newsgroup-ephemeral-ignored-charsets
12360                 (append
12361                  (and gnus-newsgroup-name
12362                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12363                  gnus-newsgroup-ignored-charsets))))
12364       (setq gnus-newsgroup-charset
12365             (or gnus-newsgroup-ephemeral-charset
12366                 (and gnus-newsgroup-name
12367                      (gnus-parameter-charset gnus-newsgroup-name))
12368                 gnus-default-charset))
12369       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12370            ignored-charsets))))
12371
12372 ;;;
12373 ;;; Mime Commands
12374 ;;;
12375
12376 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12377   "Display the current article buffer fully MIME-buttonized.
12378 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12379 treated as multipart/mixed."
12380   (interactive "P")
12381   (require 'gnus-art)
12382   (let ((gnus-unbuttonized-mime-types nil)
12383         (gnus-mime-display-multipart-as-mixed show-all-parts))
12384     (gnus-summary-show-article)))
12385
12386 (defun gnus-summary-repair-multipart (article)
12387   "Add a Content-Type header to a multipart article without one."
12388   (interactive (list (gnus-summary-article-number)))
12389   (gnus-with-article article
12390     (message-narrow-to-head)
12391     (message-remove-header "Mime-Version")
12392     (goto-char (point-max))
12393     (insert "Mime-Version: 1.0\n")
12394     (widen)
12395     (when (search-forward "\n--" nil t)
12396       (let ((separator (buffer-substring (point) (point-at-eol))))
12397         (message-narrow-to-head)
12398         (message-remove-header "Content-Type")
12399         (goto-char (point-max))
12400         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12401                         separator))
12402         (widen))))
12403   (let (gnus-mark-article-hook)
12404     (gnus-summary-select-article t t nil article)))
12405
12406 (defun gnus-summary-toggle-display-buttonized ()
12407   "Toggle the buttonizing of the article buffer."
12408   (interactive)
12409   (require 'gnus-art)
12410   (if (setq gnus-inhibit-mime-unbuttonizing
12411             (not gnus-inhibit-mime-unbuttonizing))
12412       (let ((gnus-unbuttonized-mime-types nil))
12413         (gnus-summary-show-article))
12414     (gnus-summary-show-article)))
12415
12416 ;;;
12417 ;;; Generic summary marking commands
12418 ;;;
12419
12420 (defvar gnus-summary-marking-alist
12421   '((read gnus-del-mark "d")
12422     (unread gnus-unread-mark "u")
12423     (ticked gnus-ticked-mark "!")
12424     (dormant gnus-dormant-mark "?")
12425     (expirable gnus-expirable-mark "e"))
12426   "An alist of names/marks/keystrokes.")
12427
12428 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12429 (defvar gnus-summary-mark-map)
12430
12431 (defun gnus-summary-make-all-marking-commands ()
12432   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12433   (dolist (elem gnus-summary-marking-alist)
12434     (apply 'gnus-summary-make-marking-command elem)))
12435
12436 (defun gnus-summary-make-marking-command (name mark keystroke)
12437   (let ((map (make-sparse-keymap)))
12438     (define-key gnus-summary-generic-mark-map keystroke map)
12439     (dolist (lway `((next "next" next nil "n")
12440                     (next-unread "next unread" next t "N")
12441                     (prev "previous" prev nil "p")
12442                     (prev-unread "previous unread" prev t "P")
12443                     (nomove "" nil nil ,keystroke)))
12444       (let ((func (gnus-summary-make-marking-command-1
12445                    mark (car lway) lway name)))
12446         (setq func (eval func))
12447         (define-key map (nth 4 lway) func)))))
12448
12449 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12450   `(defun ,(intern
12451             (format "gnus-summary-put-mark-as-%s%s"
12452                     name (if (eq way 'nomove)
12453                              ""
12454                            (concat "-" (symbol-name way)))))
12455      (n)
12456      ,(format
12457        "Mark the current article as %s%s.
12458 If N, the prefix, then repeat N times.
12459 If N is negative, move in reverse order.
12460 The difference between N and the actual number of articles marked is
12461 returned."
12462        name (cadr lway))
12463      (interactive "p")
12464      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12465
12466 (defun gnus-summary-generic-mark (n mark move unread)
12467   "Mark N articles with MARK."
12468   (unless (eq major-mode 'gnus-summary-mode)
12469     (error "This command can only be used in the summary buffer"))
12470   (gnus-summary-show-thread)
12471   (let ((nummove
12472          (cond
12473           ((eq move 'next) 1)
12474           ((eq move 'prev) -1)
12475           (t 0))))
12476     (if (zerop nummove)
12477         (setq n 1)
12478       (when (< n 0)
12479         (setq n (abs n)
12480               nummove (* -1 nummove))))
12481     (while (and (> n 0)
12482                 (gnus-summary-mark-article nil mark)
12483                 (zerop (gnus-summary-next-subject nummove unread t)))
12484       (setq n (1- n)))
12485     (when (/= 0 n)
12486       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12487     (gnus-summary-recenter)
12488     (gnus-summary-position-point)
12489     (gnus-set-mode-line 'summary)
12490     n))
12491
12492 (defun gnus-summary-insert-articles (articles)
12493   (when (setq articles
12494               (gnus-sorted-difference articles
12495                                       (mapcar (lambda (h)
12496                                                 (mail-header-number h))
12497                                               gnus-newsgroup-headers)))
12498     (setq gnus-newsgroup-headers
12499           (gnus-merge 'list
12500                       gnus-newsgroup-headers
12501                       (gnus-fetch-headers articles)
12502                       'gnus-article-sort-by-number))
12503     ;; Suppress duplicates?
12504     (when gnus-suppress-duplicates
12505       (gnus-dup-suppress-articles))
12506
12507     (if (and gnus-fetch-old-headers
12508              (eq gnus-headers-retrieved-by 'nov))
12509         ;; We might want to build some more threads first.
12510         (if (eq gnus-fetch-old-headers 'invisible)
12511             (gnus-build-all-threads)
12512           (gnus-build-old-threads))
12513       ;; Mark the inserted articles that are unread as unread.
12514       (setq gnus-newsgroup-unreads
12515             (gnus-sorted-nunion
12516              gnus-newsgroup-unreads
12517              (gnus-sorted-nintersection
12518               (gnus-list-of-unread-articles gnus-newsgroup-name)
12519               articles)))
12520       ;; Mark the inserted articles as selected so that the information
12521       ;; of the marks having been changed by a user may be updated when
12522       ;; exiting this group.  See `gnus-summary-update-info'.
12523       (dolist (art articles)
12524         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12525     ;; Let the Gnus agent mark articles as read.
12526     (when gnus-agent
12527       (gnus-agent-get-undownloaded-list))
12528     ;; Remove list identifiers from subject
12529     (when gnus-list-identifiers
12530       (gnus-summary-remove-list-identifiers))
12531     ;; First and last article in this newsgroup.
12532     (when gnus-newsgroup-headers
12533       (setq gnus-newsgroup-begin
12534             (mail-header-number (car gnus-newsgroup-headers))
12535             gnus-newsgroup-end
12536             (mail-header-number
12537              (gnus-last-element gnus-newsgroup-headers))))
12538     (when gnus-use-scoring
12539       (gnus-possibly-score-headers))))
12540
12541 (defun gnus-summary-insert-old-articles (&optional all)
12542   "Insert all old articles in this group.
12543 If ALL is non-nil, already read articles become readable.
12544 If ALL is a number, fetch this number of articles."
12545   (interactive "P")
12546   (prog1
12547       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12548             older len)
12549         (setq older
12550               ;; Some nntp servers lie about their active range.  When
12551               ;; this happens, the active range can be in the millions.
12552               ;; Use a compressed range to avoid creating a huge list.
12553               (gnus-range-difference (list gnus-newsgroup-active) old))
12554         (setq len (gnus-range-length older))
12555         (cond
12556          ((null older) nil)
12557          ((numberp all)
12558           (if (< all len)
12559               (let ((older-range (nreverse older)))
12560                 (setq older nil)
12561
12562                 (while (> all 0)
12563                   (let* ((r (pop older-range))
12564                          (min (if (numberp r) r (car r)))
12565                          (max (if (numberp r) r (cdr r))))
12566                     (while (and (<= min max)
12567                                 (> all 0))
12568                       (push max older)
12569                       (setq all (1- all)
12570                             max (1- max))))))
12571             (setq older (gnus-uncompress-range older))))
12572          (all
12573           (setq older (gnus-uncompress-range older)))
12574          (t
12575           (when (and (numberp gnus-large-newsgroup)
12576                    (> len gnus-large-newsgroup))
12577               (let* ((cursor-in-echo-area nil)
12578                      (initial (gnus-parameter-large-newsgroup-initial
12579                                gnus-newsgroup-name))
12580                      (input
12581                       (read-string
12582                        (format
12583                         "How many articles from %s (%s %d): "
12584                         (gnus-group-decoded-name gnus-newsgroup-name)
12585                         (if initial "max" "default")
12586                         len)
12587                        (if initial
12588                            (cons (number-to-string initial)
12589                                  0)))))
12590                 (unless (string-match "^[ \t]*$" input)
12591                   (setq all (string-to-number input))
12592                   (if (< all len)
12593                       (let ((older-range (nreverse older)))
12594                         (setq older nil)
12595
12596                         (while (> all 0)
12597                           (let* ((r (pop older-range))
12598                                  (min (if (numberp r) r (car r)))
12599                                  (max (if (numberp r) r (cdr r))))
12600                             (while (and (<= min max)
12601                                         (> all 0))
12602                               (push max older)
12603                               (setq all (1- all)
12604                                     max (1- max))))))))))
12605           (setq older (gnus-uncompress-range older))))
12606         (if (not older)
12607             (message "No old news.")
12608           (gnus-summary-insert-articles older)
12609           (gnus-summary-limit (gnus-sorted-nunion old older))))
12610     (gnus-summary-position-point)))
12611
12612 (defun gnus-summary-insert-new-articles ()
12613   "Insert all new articles in this group."
12614   (interactive)
12615   (prog1
12616       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12617             (old-high gnus-newsgroup-highest)
12618             (nnmail-fetched-sources (list t))
12619             i new)
12620         (setq gnus-newsgroup-active
12621               (gnus-copy-sequence
12622                (gnus-activate-group gnus-newsgroup-name 'scan)))
12623         (setq i (cdr gnus-newsgroup-active)
12624               gnus-newsgroup-highest i)
12625         (while (> i old-high)
12626           (push i new)
12627           (decf i))
12628         (if (not new)
12629             (message "No gnus is bad news")
12630           (gnus-summary-insert-articles new)
12631           (setq gnus-newsgroup-unreads
12632                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12633           (gnus-summary-limit (gnus-sorted-nunion old new))))
12634     (gnus-summary-position-point)))
12635
12636 ;;; Bookmark support for Gnus.
12637 (declare-function bookmark-make-record-default
12638                   "bookmark" (&optional no-file no-context posn))
12639 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12640 (declare-function bookmark-default-handler "bookmark" (bmk))
12641 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12642 (defvar bookmark-yank-point)
12643 (defvar bookmark-current-buffer)
12644
12645 (defun gnus-summary-bookmark-make-record ()
12646   "Make a bookmark entry for a Gnus summary buffer."
12647   (let (pos buf)
12648     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12649       (save-restriction              ; FIXME is it necessary to widen?
12650         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12651       (setq buf "art") ; We are recording bookmark from article buffer.
12652       (setq bookmark-yank-point (point))
12653       (setq bookmark-current-buffer (current-buffer))
12654       (gnus-article-show-summary))      ; Go back in summary buffer.
12655     ;; We are now recording bookmark from summary buffer.
12656     (unless buf (setq buf "sum"))
12657     (let* ((subject (elt (gnus-summary-article-header) 1))
12658            (grp     (car gnus-article-current))
12659            (art     (cdr gnus-article-current))
12660            (head    (gnus-summary-article-header art))
12661            (id      (mail-header-id head)))
12662       `(,subject
12663         ,@(condition-case nil
12664               (bookmark-make-record-default 'no-file 'no-context pos)
12665             (wrong-number-of-arguments
12666              (bookmark-make-record-default 'point-only)))
12667         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12668         (group . ,grp) (article . ,art)
12669         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12670
12671 ;;;###autoload
12672 (defun gnus-summary-bookmark-jump (bookmark)
12673   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12674 BOOKMARK is a bookmark name or a bookmark record."
12675   (let ((group    (bookmark-prop-get bookmark 'group))
12676         (article  (bookmark-prop-get bookmark 'article))
12677         (id       (bookmark-prop-get bookmark 'message-id))
12678         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12679     (gnus-fetch-group group (list article))
12680     (gnus-summary-insert-cached-articles)
12681     (gnus-summary-goto-article id nil 'force)
12682     ;; FIXME we have to wait article buffer is ready (only large buffer)
12683     ;; Is there a better solution to know that?
12684     ;; If we don't wait `bookmark-default-handler' will have no chance
12685     ;; to set position. However there is no error, just wrong pos.
12686     (sit-for 1)
12687     (when (string= buf "Gnus-art")
12688       (other-window 1))
12689     (bookmark-default-handler
12690      `(""
12691        (buffer . ,(current-buffer))
12692        . ,(bookmark-get-bookmark-record bookmark)))))
12693
12694 (gnus-summary-make-all-marking-commands)
12695
12696 (gnus-ems-redefine)
12697
12698 (provide 'gnus-sum)
12699
12700 (run-hooks 'gnus-sum-load-hook)
12701
12702 ;; Local Variables:
12703 ;; coding: iso-8859-1
12704 ;; End:
12705
12706 ;;; gnus-sum.el ends here